summaryrefslogtreecommitdiff
path: root/components/drivers/power/supply/supply.c
blob: bdd8d84e2280450b2ae69fa9d5c7e108074fe2d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-25     GuEe-GUI     the first version
 * 2026-03-27     Evlers       add CLI snapshot/name helpers and OFW guards
 */

#include <rtdevice.h>

#define DBG_TAG "rtdm.power_supply"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#ifndef INT_MAX
#define INT_MAX (RT_UINT32_MAX >> 1)
#endif

#ifndef INT_MIN
#define INT_MIN (-INT_MAX - 1)
#endif

static RT_DEFINE_SPINLOCK(nodes_lock);
static rt_list_t power_supply_nodes = RT_LIST_OBJECT_INIT(power_supply_nodes);
static rt_list_t power_supply_notifier_nodes = RT_LIST_OBJECT_INIT(power_supply_notifier_nodes);

const char *rt_power_supply_name(struct rt_power_supply *psy)
{
    struct rt_device *dev = psy ? psy->dev : RT_NULL;

#ifdef RT_USING_DM
    return rt_dm_dev_get_name(dev);
#else
    return dev ? dev->parent.name : "<no-dev>";
#endif
}

static rt_bool_t power_supply_have_property(struct rt_power_supply *psy,
        enum rt_power_supply_property prop);

#ifdef RT_USING_THERMAL
static rt_err_t power_supply_thermal_zone_get_temp(struct rt_thermal_zone_device *zdev,
        int *out_temp)
{
    rt_err_t err;
    union rt_power_supply_property_val val;
    struct rt_power_supply *psy = zdev->priv;

    if ((err = rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_TEMP, &val)))
    {
        return err;
    }

    *out_temp = val.intval;

    return RT_EOK;
}

const static struct rt_thermal_zone_ops power_supply_thermal_zone_ops =
{
    .get_temp = power_supply_thermal_zone_get_temp,
};

rt_err_t power_supply_thermal_register(struct rt_power_supply *psy)
{
    if (psy->thermal_dev)
    {
        return RT_EOK;
    }

    if (power_supply_have_property(psy, RT_POWER_SUPPLY_PROP_TEMP))
    {
        rt_err_t err;

        if (!(psy->thermal_dev = rt_calloc(1, sizeof(*psy->thermal_dev))))
        {
            return -RT_ENOMEM;
        }

#ifdef RT_USING_DM
        rt_dm_dev_set_name(&psy->thermal_dev->parent, rt_dm_dev_get_name(psy->dev));
#else
        psy->thermal_dev->parent.parent.name = rt_power_supply_name(psy);
#endif
        psy->thermal_dev->zone_id = 0;
        psy->thermal_dev->ops = &power_supply_thermal_zone_ops;
#ifdef RT_USING_OFW
        psy->thermal_dev->parent.ofw_node = psy->dev->ofw_node;
#endif
        psy->thermal_dev->priv = psy;

        if ((err = rt_thermal_zone_device_register(psy->thermal_dev)))
        {
            rt_free(psy->thermal_dev);
            psy->thermal_dev = RT_NULL;

            return err;
        }
    }

    return RT_EOK;
}

rt_err_t power_supply_thermal_unregister(struct rt_power_supply *psy)
{
    rt_err_t err = RT_EOK;

    if (psy->thermal_dev)
    {
        if (!(err = rt_thermal_zone_device_unregister(psy->thermal_dev)))
        {
            rt_free(psy->thermal_dev);
            psy->thermal_dev = RT_NULL;
        }
    }

    return err;
}
#else
rt_err_t power_supply_thermal_register(struct rt_power_supply *psy)
{
    return RT_EOK;
}

rt_err_t power_supply_thermal_unregister(struct rt_power_supply *psy)
{
    return RT_EOK;
}
#endif /* RT_USING_THERMAL */

#ifdef RT_USING_LED
static void power_supply_update_battery_led(struct rt_power_supply *psy)
{
    union rt_power_supply_property_val status;

    if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_STATUS, &status))
    {
        return;
    }

    switch (status.intval)
    {
    case RT_POWER_SUPPLY_STATUS_FULL:
        rt_led_set_state(psy->led_dev, RT_LED_S_ON);
        rt_led_set_brightness(psy->led_dev, 255);
        break;

    case RT_POWER_SUPPLY_STATUS_CHARGING:
        rt_led_set_state(psy->led_dev, RT_LED_S_ON);
        rt_led_set_brightness(psy->led_dev, 255 >> 1);
        break;

    default:
        rt_led_set_state(psy->led_dev, RT_LED_S_OFF);
        break;
    }
}
static void power_supply_update_online_led(struct rt_power_supply *psy)
{
    union rt_power_supply_property_val online;

    if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_ONLINE, &online))
    {
        return;
    }

    if (online.intval)
    {
        rt_led_set_state(psy->led_dev, RT_LED_S_ON);
    }
    else
    {
        rt_led_set_state(psy->led_dev, RT_LED_S_OFF);
    }
}

static void power_supply_update_led(struct rt_power_supply *psy)
{
    if (!psy->led_dev)
    {
        return;
    }

    if (psy->type == RT_POWER_SUPPLY_TYPE_BATTERY)
    {
        power_supply_update_battery_led(psy);
    }
    else
    {
        power_supply_update_online_led(psy);
    }
}
#else
static void power_supply_update_led(struct rt_power_supply *psy)
{
}
#endif /* RT_USING_LED */

static void power_supply_changed_work(struct rt_work *work, void *work_data)
{
    struct rt_power_supply *psy = work_data;
    struct rt_power_supply_notifier *notifier, *next_notifier;

    power_supply_update_led(psy);

    rt_spin_lock(&nodes_lock);

    rt_list_for_each_entry_safe(notifier, next_notifier, &power_supply_notifier_nodes, list)
    {
        rt_spin_unlock(&nodes_lock);

        notifier->callback(notifier, psy);

        rt_spin_lock(&nodes_lock);
    }

    rt_spin_unlock(&nodes_lock);
}

rt_err_t rt_power_supply_register(struct rt_power_supply *psy)
{
    rt_err_t err;

    if (!psy || !psy->dev)
    {
        return -RT_EINVAL;
    }

    if (!psy->battery_info && (!psy->properties_nr || !psy->properties || !psy->ops))
    {
        return -RT_EINVAL;
    }

    if ((err = power_supply_thermal_register(psy)))
    {
        return err;
    }

    rt_ref_init(&psy->ref);
    rt_list_init(&psy->list);
    rt_work_init(&psy->changed_work, power_supply_changed_work, psy);

    rt_spin_lock(&nodes_lock);
    rt_list_insert_before(&power_supply_nodes, &psy->list);
    rt_spin_unlock(&nodes_lock);

#ifdef RT_USING_OFW
    if (psy->dev->ofw_node)
    {
#ifdef RT_USING_DM
        rt_dm_dev_bind_fwdata(psy->dev, RT_NULL, psy);
#endif
    }
#endif

    return RT_EOK;
}

rt_err_t rt_power_supply_unregister(struct rt_power_supply *psy)
{
    rt_err_t err;

    if (!psy)
    {
        return -RT_EINVAL;
    }

    rt_spin_lock(&nodes_lock);

    if (rt_ref_read(&psy->ref) > 1)
    {
        err = -RT_EBUSY;
        goto _unlock;
    }

    rt_list_remove(&psy->list);

#ifdef RT_USING_OFW
    if (psy->dev->ofw_node)
    {
#ifdef RT_USING_DM
        rt_dm_dev_unbind_fwdata(psy->dev, RT_NULL);
#endif
    }
#endif

_unlock:
    rt_spin_unlock(&nodes_lock);

    if (!err)
    {
        rt_work_cancel(&psy->changed_work);

        err = power_supply_thermal_unregister(psy);
    }

    return err;
}

rt_err_t rt_power_supply_notifier_register(struct rt_power_supply_notifier *notifier)
{
    if (!notifier || !notifier->callback)
    {
        return -RT_EINVAL;
    }

    rt_list_init(&notifier->list);

    rt_spin_lock(&nodes_lock);
    rt_list_insert_before(&power_supply_notifier_nodes, &notifier->list);
    rt_spin_unlock(&nodes_lock);

    return RT_EOK;
}

rt_err_t rt_power_supply_notifier_unregister(struct rt_power_supply_notifier *notifier)
{
    if (!notifier)
    {
        return -RT_EINVAL;
    }

    rt_spin_lock(&nodes_lock);
    rt_list_remove(&notifier->list);
    rt_spin_unlock(&nodes_lock);

    return RT_EOK;
}

static rt_bool_t power_supply_have_property(struct rt_power_supply *psy,
        enum rt_power_supply_property prop)
{
    if (!psy->ops->get_property)
    {
        return RT_FALSE;
    }

    for (int i = 0; i < psy->properties_nr; ++i)
    {
        if (psy->properties[i] == prop)
        {
            return RT_TRUE;
        }
    }

    return RT_FALSE;
}

static rt_bool_t power_supply_battery_info_have_property(
        struct rt_power_supply_battery_info *info, enum rt_power_supply_property prop)
{
    if (!info)
    {
        return RT_FALSE;
    }

    switch (prop)
    {
    case RT_POWER_SUPPLY_PROP_TECHNOLOGY:
        return info->technology != RT_POWER_SUPPLY_TECHNOLOGY_UNKNOWN;

    case RT_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
        return info->energy_full_design_uwh >= 0;

    case RT_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
        return info->charge_full_design_uah >= 0;

    case RT_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
        return info->voltage_min_design_uv >= 0;

    case RT_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
        return info->voltage_max_design_uv >= 0;

    case RT_POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
        return info->precharge_current_ua >= 0;

    case RT_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
        return info->charge_term_current_ua >= 0;

    case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
        return info->constant_charge_current_max_ua >= 0;

    case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
        return info->constant_charge_voltage_max_uv >= 0;

    case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
        return info->temp_ambient_alert_min > INT_MIN;

    case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
        return info->temp_ambient_alert_max < INT_MAX;

    case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
        return info->temp_alert_min > INT_MIN;

    case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
        return info->temp_alert_max < INT_MAX;

    case RT_POWER_SUPPLY_PROP_TEMP_MIN:
        return info->temp_min > INT_MIN;

    case RT_POWER_SUPPLY_PROP_TEMP_MAX:
        return info->temp_max < INT_MAX;

    default:
        return RT_FALSE;
    }
}

static rt_err_t power_supply_battery_info_get_property(
        struct rt_power_supply_battery_info *info, enum rt_power_supply_property prop,
        union rt_power_supply_property_val *val)
{
    switch (prop)
    {
    case RT_POWER_SUPPLY_PROP_TECHNOLOGY:
        val->intval = info->technology;
        break;

    case RT_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
        val->intval = info->energy_full_design_uwh;
        break;

    case RT_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
        val->intval = info->charge_full_design_uah;
        break;

    case RT_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
        val->intval = info->voltage_min_design_uv;
        break;

    case RT_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
        val->intval = info->voltage_max_design_uv;
        break;

    case RT_POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
        val->intval = info->precharge_current_ua;
        break;

    case RT_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
        val->intval = info->charge_term_current_ua;
        break;

    case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
        val->intval = info->constant_charge_current_max_ua;
        break;

    case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
        val->intval = info->constant_charge_voltage_max_uv;
        break;

    case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
        val->intval = info->temp_ambient_alert_min;
        break;

    case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
        val->intval = info->temp_ambient_alert_max;
        break;

    case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
        val->intval = info->temp_alert_min;
        break;

    case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
        val->intval = info->temp_alert_max;
        break;

    case RT_POWER_SUPPLY_PROP_TEMP_MIN:
        val->intval = info->temp_min;
        break;

    case RT_POWER_SUPPLY_PROP_TEMP_MAX:
        val->intval = info->temp_max;
        break;

    default:
        return -RT_EINVAL;
    }

    return RT_EOK;
}

rt_err_t rt_power_supply_get_property(struct rt_power_supply *psy,
        enum rt_power_supply_property prop,
        union rt_power_supply_property_val *val)
{
    if (!psy || !val)
    {
        return -RT_EINVAL;
    }

    if (power_supply_have_property(psy, prop))
    {
        return psy->ops->get_property(psy, prop, val);
    }
    else if (power_supply_battery_info_have_property(psy->battery_info, prop))
    {
        return power_supply_battery_info_get_property(psy->battery_info, prop, val);
    }

    return -RT_ENOSYS;
}

rt_err_t rt_power_supply_set_property(struct rt_power_supply *psy,
        enum rt_power_supply_property prop,
        const union rt_power_supply_property_val *val)
{
    if (!psy || !val)
    {
        return -RT_EINVAL;
    }

    if (!psy->ops->set_property)
    {
        return -RT_ENOSYS;
    }

    return psy->ops->set_property(psy, prop, val);
}

void rt_power_supply_changed(struct rt_power_supply *psy)
{
    RT_ASSERT(psy != RT_NULL);

    rt_work_submit(&psy->changed_work, 0);
}

struct rt_power_supply *rt_power_supply_get(struct rt_device *dev, const char *id)
{
    struct rt_power_supply *psy = RT_NULL;

    if (!dev || !id)
    {
        return rt_err_ptr(-RT_EINVAL);
    }

#ifdef RT_USING_OFW
    if (dev->ofw_node)
    {
        struct rt_ofw_node *psy_np = rt_ofw_parse_phandle(dev->ofw_node, id, 0);

        if (!psy_np)
        {
            return psy;
        }

        psy = rt_ofw_data(psy_np);
    }
#endif /* RT_USING_OFW */

    if (!psy)
    {
        struct rt_power_supply *psy_target, *psy_next;

        rt_spin_lock(&nodes_lock);

        rt_list_for_each_entry_safe(psy_target, psy_next, &power_supply_nodes, list)
        {
            if (!rt_strcmp(psy_target->dev->parent.name, id))
            {
                psy = psy_target;
                break;
            }
        }

        rt_spin_unlock(&nodes_lock);
    }

    return psy;
}

static void power_supply_release(struct rt_ref *r)
{
    struct rt_power_supply *psy = rt_container_of(r, struct rt_power_supply, ref);

    rt_power_supply_unregister(psy);
}

void rt_power_supply_put(struct rt_power_supply *psy)
{
    if (!psy)
    {
        return;
    }

    rt_ref_put(&psy->ref, power_supply_release);
}

struct rt_power_supply **rt_power_supply_snapshot(rt_size_t *count)
{
    struct rt_power_supply *psy, *psy_next;
    struct rt_power_supply **nodes;
    rt_size_t total = 0;
    rt_size_t idx = 0;

    if (!count)
    {
        return RT_NULL;
    }

    *count = 0;

    rt_spin_lock(&nodes_lock);
    rt_list_for_each_entry_safe(psy, psy_next, &power_supply_nodes, list)
    {
        total++;
    }
    rt_spin_unlock(&nodes_lock);

    if (!total)
    {
        return RT_NULL;
    }

    nodes = rt_calloc(total, sizeof(*nodes));
    if (!nodes)
    {
        return RT_NULL;
    }

    rt_spin_lock(&nodes_lock);
    rt_list_for_each_entry_safe(psy, psy_next, &power_supply_nodes, list)
    {
        nodes[idx] = psy;
        rt_ref_get(&psy->ref);
        idx++;
    }
    rt_spin_unlock(&nodes_lock);

    *count = total;
    return nodes;
}

void rt_power_supply_snapshot_free(struct rt_power_supply **nodes, rt_size_t count)
{
    if (!nodes)
    {
        return;
    }

    while (count--)
    {
        rt_ref_put(&nodes[count]->ref, power_supply_release);
    }

    rt_free(nodes);
}