1 // SPDX-License-Identifier: GPL-2.0
3 * Power Supply driver for a Greybus module.
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
8 * Released under the GPLv2 only.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/power_supply.h>
14 #include <linux/slab.h>
20 struct gb_power_supply_prop
{
21 enum power_supply_property prop
;
28 struct gb_power_supply
{
31 struct power_supply
*psy
;
32 struct power_supply_desc desc
;
34 struct gb_power_supplies
*supplies
;
35 struct delayed_work work
;
41 u8 properties_count_str
;
42 unsigned long last_update
;
44 unsigned int update_interval
;
46 struct gb_power_supply_prop
*props
;
47 enum power_supply_property
*props_raw
;
49 struct mutex supply_lock
;
52 struct gb_power_supplies
{
53 struct gb_connection
*connection
;
55 struct gb_power_supply
*supply
;
56 struct mutex supplies_lock
;
59 #define to_gb_power_supply(x) power_supply_get_drvdata(x)
62 * General power supply properties that could be absent from various reasons,
63 * like kernel versions or vendor specific versions
65 #ifndef POWER_SUPPLY_PROP_VOLTAGE_BOOT
66 #define POWER_SUPPLY_PROP_VOLTAGE_BOOT -1
68 #ifndef POWER_SUPPLY_PROP_CURRENT_BOOT
69 #define POWER_SUPPLY_PROP_CURRENT_BOOT -1
71 #ifndef POWER_SUPPLY_PROP_CALIBRATE
72 #define POWER_SUPPLY_PROP_CALIBRATE -1
75 /* cache time in milliseconds, if cache_time is set to 0 cache is disable */
76 static unsigned int cache_time
= 1000;
78 * update interval initial and maximum value, between the two will
79 * back-off exponential
81 static unsigned int update_interval_init
= 1 * HZ
;
82 static unsigned int update_interval_max
= 30 * HZ
;
84 struct gb_power_supply_changes
{
85 enum power_supply_property prop
;
87 void (*prop_changed
)(struct gb_power_supply
*gbpsy
,
88 struct gb_power_supply_prop
*prop
);
91 static void gb_power_supply_state_change(struct gb_power_supply
*gbpsy
,
92 struct gb_power_supply_prop
*prop
);
94 static const struct gb_power_supply_changes psy_props_changes
[] = {
95 { .prop
= GB_POWER_SUPPLY_PROP_STATUS
,
96 .tolerance_change
= 0,
97 .prop_changed
= gb_power_supply_state_change
,
99 { .prop
= GB_POWER_SUPPLY_PROP_TEMP
,
100 .tolerance_change
= 500,
101 .prop_changed
= NULL
,
103 { .prop
= GB_POWER_SUPPLY_PROP_ONLINE
,
104 .tolerance_change
= 0,
105 .prop_changed
= NULL
,
109 static int get_psp_from_gb_prop(int gb_prop
, enum power_supply_property
*psp
)
114 case GB_POWER_SUPPLY_PROP_STATUS
:
115 prop
= POWER_SUPPLY_PROP_STATUS
;
117 case GB_POWER_SUPPLY_PROP_CHARGE_TYPE
:
118 prop
= POWER_SUPPLY_PROP_CHARGE_TYPE
;
120 case GB_POWER_SUPPLY_PROP_HEALTH
:
121 prop
= POWER_SUPPLY_PROP_HEALTH
;
123 case GB_POWER_SUPPLY_PROP_PRESENT
:
124 prop
= POWER_SUPPLY_PROP_PRESENT
;
126 case GB_POWER_SUPPLY_PROP_ONLINE
:
127 prop
= POWER_SUPPLY_PROP_ONLINE
;
129 case GB_POWER_SUPPLY_PROP_AUTHENTIC
:
130 prop
= POWER_SUPPLY_PROP_AUTHENTIC
;
132 case GB_POWER_SUPPLY_PROP_TECHNOLOGY
:
133 prop
= POWER_SUPPLY_PROP_TECHNOLOGY
;
135 case GB_POWER_SUPPLY_PROP_CYCLE_COUNT
:
136 prop
= POWER_SUPPLY_PROP_CYCLE_COUNT
;
138 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX
:
139 prop
= POWER_SUPPLY_PROP_VOLTAGE_MAX
;
141 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN
:
142 prop
= POWER_SUPPLY_PROP_VOLTAGE_MIN
;
144 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
145 prop
= POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
;
147 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
148 prop
= POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
;
150 case GB_POWER_SUPPLY_PROP_VOLTAGE_NOW
:
151 prop
= POWER_SUPPLY_PROP_VOLTAGE_NOW
;
153 case GB_POWER_SUPPLY_PROP_VOLTAGE_AVG
:
154 prop
= POWER_SUPPLY_PROP_VOLTAGE_AVG
;
156 case GB_POWER_SUPPLY_PROP_VOLTAGE_OCV
:
157 prop
= POWER_SUPPLY_PROP_VOLTAGE_OCV
;
159 case GB_POWER_SUPPLY_PROP_VOLTAGE_BOOT
:
160 prop
= POWER_SUPPLY_PROP_VOLTAGE_BOOT
;
162 case GB_POWER_SUPPLY_PROP_CURRENT_MAX
:
163 prop
= POWER_SUPPLY_PROP_CURRENT_MAX
;
165 case GB_POWER_SUPPLY_PROP_CURRENT_NOW
:
166 prop
= POWER_SUPPLY_PROP_CURRENT_NOW
;
168 case GB_POWER_SUPPLY_PROP_CURRENT_AVG
:
169 prop
= POWER_SUPPLY_PROP_CURRENT_AVG
;
171 case GB_POWER_SUPPLY_PROP_CURRENT_BOOT
:
172 prop
= POWER_SUPPLY_PROP_CURRENT_BOOT
;
174 case GB_POWER_SUPPLY_PROP_POWER_NOW
:
175 prop
= POWER_SUPPLY_PROP_POWER_NOW
;
177 case GB_POWER_SUPPLY_PROP_POWER_AVG
:
178 prop
= POWER_SUPPLY_PROP_POWER_AVG
;
180 case GB_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
181 prop
= POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
;
183 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
184 prop
= POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
;
186 case GB_POWER_SUPPLY_PROP_CHARGE_FULL
:
187 prop
= POWER_SUPPLY_PROP_CHARGE_FULL
;
189 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY
:
190 prop
= POWER_SUPPLY_PROP_CHARGE_EMPTY
;
192 case GB_POWER_SUPPLY_PROP_CHARGE_NOW
:
193 prop
= POWER_SUPPLY_PROP_CHARGE_NOW
;
195 case GB_POWER_SUPPLY_PROP_CHARGE_AVG
:
196 prop
= POWER_SUPPLY_PROP_CHARGE_AVG
;
198 case GB_POWER_SUPPLY_PROP_CHARGE_COUNTER
:
199 prop
= POWER_SUPPLY_PROP_CHARGE_COUNTER
;
201 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
202 prop
= POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
;
204 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
205 prop
= POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
;
207 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
208 prop
= POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
;
210 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
211 prop
= POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
;
213 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
:
214 prop
= POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
;
216 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
:
217 prop
= POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
;
219 case GB_POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
220 prop
= POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
;
222 case GB_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
223 prop
= POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
;
225 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN
:
226 prop
= POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN
;
228 case GB_POWER_SUPPLY_PROP_ENERGY_FULL
:
229 prop
= POWER_SUPPLY_PROP_ENERGY_FULL
;
231 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY
:
232 prop
= POWER_SUPPLY_PROP_ENERGY_EMPTY
;
234 case GB_POWER_SUPPLY_PROP_ENERGY_NOW
:
235 prop
= POWER_SUPPLY_PROP_ENERGY_NOW
;
237 case GB_POWER_SUPPLY_PROP_ENERGY_AVG
:
238 prop
= POWER_SUPPLY_PROP_ENERGY_AVG
;
240 case GB_POWER_SUPPLY_PROP_CAPACITY
:
241 prop
= POWER_SUPPLY_PROP_CAPACITY
;
243 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
244 prop
= POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
;
246 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX
:
247 prop
= POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX
;
249 case GB_POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
250 prop
= POWER_SUPPLY_PROP_CAPACITY_LEVEL
;
252 case GB_POWER_SUPPLY_PROP_TEMP
:
253 prop
= POWER_SUPPLY_PROP_TEMP
;
255 case GB_POWER_SUPPLY_PROP_TEMP_MAX
:
256 prop
= POWER_SUPPLY_PROP_TEMP_MAX
;
258 case GB_POWER_SUPPLY_PROP_TEMP_MIN
:
259 prop
= POWER_SUPPLY_PROP_TEMP_MIN
;
261 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
262 prop
= POWER_SUPPLY_PROP_TEMP_ALERT_MIN
;
264 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
265 prop
= POWER_SUPPLY_PROP_TEMP_ALERT_MAX
;
267 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT
:
268 prop
= POWER_SUPPLY_PROP_TEMP_AMBIENT
;
270 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN
:
271 prop
= POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN
;
273 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX
:
274 prop
= POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX
;
276 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
277 prop
= POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
;
279 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
280 prop
= POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
;
282 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
283 prop
= POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
;
285 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
286 prop
= POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
;
288 case GB_POWER_SUPPLY_PROP_TYPE
:
289 prop
= POWER_SUPPLY_PROP_TYPE
;
291 case GB_POWER_SUPPLY_PROP_SCOPE
:
292 prop
= POWER_SUPPLY_PROP_SCOPE
;
294 case GB_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
295 prop
= POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
;
297 case GB_POWER_SUPPLY_PROP_CALIBRATE
:
298 prop
= POWER_SUPPLY_PROP_CALIBRATE
;
308 *psp
= (enum power_supply_property
)prop
;
313 static struct gb_connection
*get_conn_from_psy(struct gb_power_supply
*gbpsy
)
315 return gbpsy
->supplies
->connection
;
318 static struct gb_power_supply_prop
*get_psy_prop(struct gb_power_supply
*gbpsy
,
319 enum power_supply_property psp
)
323 for (i
= 0; i
< gbpsy
->properties_count
; i
++)
324 if (gbpsy
->props
[i
].prop
== psp
)
325 return &gbpsy
->props
[i
];
329 static int is_psy_prop_writeable(struct gb_power_supply
*gbpsy
,
330 enum power_supply_property psp
)
332 struct gb_power_supply_prop
*prop
;
334 prop
= get_psy_prop(gbpsy
, psp
);
337 return prop
->is_writeable
? 1 : 0;
340 static int is_prop_valint(enum power_supply_property psp
)
342 return ((psp
< POWER_SUPPLY_PROP_MODEL_NAME
) ? 1 : 0);
345 static void next_interval(struct gb_power_supply
*gbpsy
)
347 if (gbpsy
->update_interval
== update_interval_max
)
350 /* do some exponential back-off in the update interval */
351 gbpsy
->update_interval
*= 2;
352 if (gbpsy
->update_interval
> update_interval_max
)
353 gbpsy
->update_interval
= update_interval_max
;
356 static void __gb_power_supply_changed(struct gb_power_supply
*gbpsy
)
358 power_supply_changed(gbpsy
->psy
);
361 static void gb_power_supply_state_change(struct gb_power_supply
*gbpsy
,
362 struct gb_power_supply_prop
*prop
)
364 struct gb_connection
*connection
= get_conn_from_psy(gbpsy
);
368 * Check gbpsy->pm_acquired to make sure only one pair of 'get_sync'
369 * and 'put_autosuspend' runtime pm call for state property change.
371 mutex_lock(&gbpsy
->supply_lock
);
373 if ((prop
->val
== GB_POWER_SUPPLY_STATUS_CHARGING
) &&
374 !gbpsy
->pm_acquired
) {
375 ret
= gb_pm_runtime_get_sync(connection
->bundle
);
377 dev_err(&connection
->bundle
->dev
,
378 "Fail to set wake lock for charging state\n");
380 gbpsy
->pm_acquired
= true;
382 if (gbpsy
->pm_acquired
) {
383 ret
= gb_pm_runtime_put_autosuspend(connection
->bundle
);
385 dev_err(&connection
->bundle
->dev
,
386 "Fail to set wake unlock for none charging\n");
388 gbpsy
->pm_acquired
= false;
392 mutex_unlock(&gbpsy
->supply_lock
);
395 static void check_changed(struct gb_power_supply
*gbpsy
,
396 struct gb_power_supply_prop
*prop
)
398 const struct gb_power_supply_changes
*psyc
;
400 int prev_val
= prop
->previous_val
;
401 bool changed
= false;
404 for (i
= 0; i
< ARRAY_SIZE(psy_props_changes
); i
++) {
405 psyc
= &psy_props_changes
[i
];
406 if (prop
->prop
== psyc
->prop
) {
407 if (!psyc
->tolerance_change
)
409 else if (val
< prev_val
&&
410 prev_val
- val
> psyc
->tolerance_change
)
412 else if (val
> prev_val
&&
413 val
- prev_val
> psyc
->tolerance_change
)
416 if (changed
&& psyc
->prop_changed
)
417 psyc
->prop_changed(gbpsy
, prop
);
420 gbpsy
->changed
= true;
426 static int total_props(struct gb_power_supply
*gbpsy
)
428 /* this return the intval plus the strval properties */
429 return (gbpsy
->properties_count
+ gbpsy
->properties_count_str
);
432 static void prop_append(struct gb_power_supply
*gbpsy
,
433 enum power_supply_property prop
)
435 enum power_supply_property
*new_props_raw
;
437 gbpsy
->properties_count_str
++;
438 new_props_raw
= krealloc(gbpsy
->props_raw
, total_props(gbpsy
) *
439 sizeof(enum power_supply_property
),
443 gbpsy
->props_raw
= new_props_raw
;
444 gbpsy
->props_raw
[total_props(gbpsy
) - 1] = prop
;
447 static int __gb_power_supply_set_name(char *init_name
, char *name
, size_t len
)
451 struct power_supply
*psy
;
453 if (!strlen(init_name
))
454 init_name
= "gb_power_supply";
455 strlcpy(name
, init_name
, len
);
457 while ((ret
< len
) && (psy
= power_supply_get_by_name(name
))) {
458 power_supply_put(psy
);
460 ret
= snprintf(name
, len
, "%s_%u", init_name
, ++i
);
467 static void _gb_power_supply_append_props(struct gb_power_supply
*gbpsy
)
469 if (strlen(gbpsy
->manufacturer
))
470 prop_append(gbpsy
, POWER_SUPPLY_PROP_MANUFACTURER
);
471 if (strlen(gbpsy
->model_name
))
472 prop_append(gbpsy
, POWER_SUPPLY_PROP_MODEL_NAME
);
473 if (strlen(gbpsy
->serial_number
))
474 prop_append(gbpsy
, POWER_SUPPLY_PROP_SERIAL_NUMBER
);
477 static int gb_power_supply_description_get(struct gb_power_supply
*gbpsy
)
479 struct gb_connection
*connection
= get_conn_from_psy(gbpsy
);
480 struct gb_power_supply_get_description_request req
;
481 struct gb_power_supply_get_description_response resp
;
484 req
.psy_id
= gbpsy
->id
;
486 ret
= gb_operation_sync(connection
,
487 GB_POWER_SUPPLY_TYPE_GET_DESCRIPTION
,
488 &req
, sizeof(req
), &resp
, sizeof(resp
));
492 gbpsy
->manufacturer
= kstrndup(resp
.manufacturer
, PROP_MAX
, GFP_KERNEL
);
493 if (!gbpsy
->manufacturer
)
495 gbpsy
->model_name
= kstrndup(resp
.model
, PROP_MAX
, GFP_KERNEL
);
496 if (!gbpsy
->model_name
)
498 gbpsy
->serial_number
= kstrndup(resp
.serial_number
, PROP_MAX
,
500 if (!gbpsy
->serial_number
)
503 gbpsy
->type
= le16_to_cpu(resp
.type
);
504 gbpsy
->properties_count
= resp
.properties_count
;
509 static int gb_power_supply_prop_descriptors_get(struct gb_power_supply
*gbpsy
)
511 struct gb_connection
*connection
= get_conn_from_psy(gbpsy
);
512 struct gb_power_supply_get_property_descriptors_request
*req
;
513 struct gb_power_supply_get_property_descriptors_response
*resp
;
514 struct gb_operation
*op
;
515 u8 props_count
= gbpsy
->properties_count
;
516 enum power_supply_property psp
;
520 if (props_count
== 0)
523 op
= gb_operation_create(connection
,
524 GB_POWER_SUPPLY_TYPE_GET_PROP_DESCRIPTORS
,
525 sizeof(req
), sizeof(*resp
) + props_count
*
526 sizeof(struct gb_power_supply_props_desc
),
531 req
= op
->request
->payload
;
532 req
->psy_id
= gbpsy
->id
;
534 ret
= gb_operation_request_send_sync(op
);
536 goto out_put_operation
;
538 resp
= op
->response
->payload
;
540 /* validate received properties */
541 for (i
= 0; i
< props_count
; i
++) {
542 ret
= get_psp_from_gb_prop(resp
->props
[i
].property
, &psp
);
544 dev_warn(&connection
->bundle
->dev
,
545 "greybus property %u it is not supported by this kernel, dropped\n",
546 resp
->props
[i
].property
);
547 gbpsy
->properties_count
--;
551 gbpsy
->props
= kcalloc(gbpsy
->properties_count
, sizeof(*gbpsy
->props
),
555 goto out_put_operation
;
558 gbpsy
->props_raw
= kcalloc(gbpsy
->properties_count
,
559 sizeof(*gbpsy
->props_raw
), GFP_KERNEL
);
560 if (!gbpsy
->props_raw
) {
562 goto out_put_operation
;
565 /* Store available properties, skip the ones we do not support */
566 for (i
= 0; i
< props_count
; i
++) {
567 ret
= get_psp_from_gb_prop(resp
->props
[i
].property
, &psp
);
572 gbpsy
->props
[i
- r
].prop
= psp
;
573 gbpsy
->props
[i
- r
].gb_prop
= resp
->props
[i
].property
;
574 gbpsy
->props_raw
[i
- r
] = psp
;
575 if (resp
->props
[i
].is_writeable
)
576 gbpsy
->props
[i
- r
].is_writeable
= true;
580 * now append the properties that we already got information in the
581 * get_description operation. (char * ones)
583 _gb_power_supply_append_props(gbpsy
);
587 gb_operation_put(op
);
592 static int __gb_power_supply_property_update(struct gb_power_supply
*gbpsy
,
593 enum power_supply_property psp
)
595 struct gb_connection
*connection
= get_conn_from_psy(gbpsy
);
596 struct gb_power_supply_prop
*prop
;
597 struct gb_power_supply_get_property_request req
;
598 struct gb_power_supply_get_property_response resp
;
602 prop
= get_psy_prop(gbpsy
, psp
);
605 req
.psy_id
= gbpsy
->id
;
606 req
.property
= prop
->gb_prop
;
608 ret
= gb_operation_sync(connection
, GB_POWER_SUPPLY_TYPE_GET_PROPERTY
,
609 &req
, sizeof(req
), &resp
, sizeof(resp
));
613 val
= le32_to_cpu(resp
.prop_val
);
614 if (val
== prop
->val
)
617 prop
->previous_val
= prop
->val
;
620 check_changed(gbpsy
, prop
);
625 static int __gb_power_supply_property_get(struct gb_power_supply
*gbpsy
,
626 enum power_supply_property psp
,
627 union power_supply_propval
*val
)
629 struct gb_power_supply_prop
*prop
;
631 prop
= get_psy_prop(gbpsy
, psp
);
635 val
->intval
= prop
->val
;
639 static int __gb_power_supply_property_strval_get(struct gb_power_supply
*gbpsy
,
640 enum power_supply_property psp
,
641 union power_supply_propval
*val
)
644 case POWER_SUPPLY_PROP_MODEL_NAME
:
645 val
->strval
= gbpsy
->model_name
;
647 case POWER_SUPPLY_PROP_MANUFACTURER
:
648 val
->strval
= gbpsy
->manufacturer
;
650 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
651 val
->strval
= gbpsy
->serial_number
;
660 static int _gb_power_supply_property_get(struct gb_power_supply
*gbpsy
,
661 enum power_supply_property psp
,
662 union power_supply_propval
*val
)
664 struct gb_connection
*connection
= get_conn_from_psy(gbpsy
);
668 * Properties of type const char *, were already fetched on
669 * get_description operation and should be cached in gb
671 if (is_prop_valint(psp
))
672 ret
= __gb_power_supply_property_get(gbpsy
, psp
, val
);
674 ret
= __gb_power_supply_property_strval_get(gbpsy
, psp
, val
);
677 dev_err(&connection
->bundle
->dev
, "get property %u\n", psp
);
682 static int is_cache_valid(struct gb_power_supply
*gbpsy
)
684 /* check if cache is good enough or it has expired */
685 if (gbpsy
->cache_invalid
) {
686 gbpsy
->cache_invalid
= 0;
690 if (gbpsy
->last_update
&&
691 time_is_after_jiffies(gbpsy
->last_update
+
692 msecs_to_jiffies(cache_time
)))
698 static int gb_power_supply_status_get(struct gb_power_supply
*gbpsy
)
700 struct gb_connection
*connection
= get_conn_from_psy(gbpsy
);
704 if (is_cache_valid(gbpsy
))
707 ret
= gb_pm_runtime_get_sync(connection
->bundle
);
711 for (i
= 0; i
< gbpsy
->properties_count
; i
++) {
712 ret
= __gb_power_supply_property_update(gbpsy
,
713 gbpsy
->props
[i
].prop
);
719 gbpsy
->last_update
= jiffies
;
721 gb_pm_runtime_put_autosuspend(connection
->bundle
);
725 static void gb_power_supply_status_update(struct gb_power_supply
*gbpsy
)
727 /* check if there a change that need to be reported */
728 gb_power_supply_status_get(gbpsy
);
733 gbpsy
->update_interval
= update_interval_init
;
734 __gb_power_supply_changed(gbpsy
);
735 gbpsy
->changed
= false;
738 static void gb_power_supply_work(struct work_struct
*work
)
740 struct gb_power_supply
*gbpsy
= container_of(work
,
741 struct gb_power_supply
,
745 * if the poll interval is not set, disable polling, this is helpful
746 * specially at unregister time.
748 if (!gbpsy
->update_interval
)
751 gb_power_supply_status_update(gbpsy
);
752 next_interval(gbpsy
);
753 schedule_delayed_work(&gbpsy
->work
, gbpsy
->update_interval
);
756 static int get_property(struct power_supply
*b
,
757 enum power_supply_property psp
,
758 union power_supply_propval
*val
)
760 struct gb_power_supply
*gbpsy
= to_gb_power_supply(b
);
762 gb_power_supply_status_get(gbpsy
);
764 return _gb_power_supply_property_get(gbpsy
, psp
, val
);
767 static int gb_power_supply_property_set(struct gb_power_supply
*gbpsy
,
768 enum power_supply_property psp
,
771 struct gb_connection
*connection
= get_conn_from_psy(gbpsy
);
772 struct gb_power_supply_prop
*prop
;
773 struct gb_power_supply_set_property_request req
;
776 ret
= gb_pm_runtime_get_sync(connection
->bundle
);
780 prop
= get_psy_prop(gbpsy
, psp
);
786 req
.psy_id
= gbpsy
->id
;
787 req
.property
= prop
->gb_prop
;
788 req
.prop_val
= cpu_to_le32((s32
)val
);
790 ret
= gb_operation_sync(connection
, GB_POWER_SUPPLY_TYPE_SET_PROPERTY
,
791 &req
, sizeof(req
), NULL
, 0);
795 /* cache immediately the new value */
799 gb_pm_runtime_put_autosuspend(connection
->bundle
);
803 static int set_property(struct power_supply
*b
,
804 enum power_supply_property psp
,
805 const union power_supply_propval
*val
)
807 struct gb_power_supply
*gbpsy
= to_gb_power_supply(b
);
809 return gb_power_supply_property_set(gbpsy
, psp
, val
->intval
);
812 static int property_is_writeable(struct power_supply
*b
,
813 enum power_supply_property psp
)
815 struct gb_power_supply
*gbpsy
= to_gb_power_supply(b
);
817 return is_psy_prop_writeable(gbpsy
, psp
);
820 static int gb_power_supply_register(struct gb_power_supply
*gbpsy
)
822 struct gb_connection
*connection
= get_conn_from_psy(gbpsy
);
823 struct power_supply_config cfg
= {};
825 cfg
.drv_data
= gbpsy
;
827 gbpsy
->desc
.name
= gbpsy
->name
;
828 gbpsy
->desc
.type
= gbpsy
->type
;
829 gbpsy
->desc
.properties
= gbpsy
->props_raw
;
830 gbpsy
->desc
.num_properties
= total_props(gbpsy
);
831 gbpsy
->desc
.get_property
= get_property
;
832 gbpsy
->desc
.set_property
= set_property
;
833 gbpsy
->desc
.property_is_writeable
= property_is_writeable
;
835 gbpsy
->psy
= power_supply_register(&connection
->bundle
->dev
,
837 return PTR_ERR_OR_ZERO(gbpsy
->psy
);
840 static void _gb_power_supply_free(struct gb_power_supply
*gbpsy
)
842 kfree(gbpsy
->serial_number
);
843 kfree(gbpsy
->model_name
);
844 kfree(gbpsy
->manufacturer
);
845 kfree(gbpsy
->props_raw
);
849 static void _gb_power_supply_release(struct gb_power_supply
*gbpsy
)
851 gbpsy
->update_interval
= 0;
853 cancel_delayed_work_sync(&gbpsy
->work
);
855 if (gbpsy
->registered
)
856 power_supply_unregister(gbpsy
->psy
);
858 _gb_power_supply_free(gbpsy
);
861 static void _gb_power_supplies_release(struct gb_power_supplies
*supplies
)
865 if (!supplies
->supply
)
868 mutex_lock(&supplies
->supplies_lock
);
869 for (i
= 0; i
< supplies
->supplies_count
; i
++)
870 _gb_power_supply_release(&supplies
->supply
[i
]);
871 kfree(supplies
->supply
);
872 mutex_unlock(&supplies
->supplies_lock
);
876 static int gb_power_supplies_get_count(struct gb_power_supplies
*supplies
)
878 struct gb_power_supply_get_supplies_response resp
;
881 ret
= gb_operation_sync(supplies
->connection
,
882 GB_POWER_SUPPLY_TYPE_GET_SUPPLIES
,
883 NULL
, 0, &resp
, sizeof(resp
));
887 if (!resp
.supplies_count
)
890 supplies
->supplies_count
= resp
.supplies_count
;
895 static int gb_power_supply_config(struct gb_power_supplies
*supplies
, int id
)
897 struct gb_power_supply
*gbpsy
= &supplies
->supply
[id
];
900 gbpsy
->supplies
= supplies
;
903 ret
= gb_power_supply_description_get(gbpsy
);
907 return gb_power_supply_prop_descriptors_get(gbpsy
);
910 static int gb_power_supply_enable(struct gb_power_supply
*gbpsy
)
914 /* guarantee that we have an unique name, before register */
915 ret
= __gb_power_supply_set_name(gbpsy
->model_name
, gbpsy
->name
,
916 sizeof(gbpsy
->name
));
920 mutex_init(&gbpsy
->supply_lock
);
922 ret
= gb_power_supply_register(gbpsy
);
926 gbpsy
->update_interval
= update_interval_init
;
927 INIT_DELAYED_WORK(&gbpsy
->work
, gb_power_supply_work
);
928 schedule_delayed_work(&gbpsy
->work
, 0);
930 /* everything went fine, mark it for release code to know */
931 gbpsy
->registered
= true;
936 static int gb_power_supplies_setup(struct gb_power_supplies
*supplies
)
938 struct gb_connection
*connection
= supplies
->connection
;
942 mutex_lock(&supplies
->supplies_lock
);
944 ret
= gb_power_supplies_get_count(supplies
);
948 supplies
->supply
= kcalloc(supplies
->supplies_count
,
949 sizeof(struct gb_power_supply
),
952 if (!supplies
->supply
) {
957 for (i
= 0; i
< supplies
->supplies_count
; i
++) {
958 ret
= gb_power_supply_config(supplies
, i
);
960 dev_err(&connection
->bundle
->dev
,
961 "Fail to configure supplies devices\n");
966 mutex_unlock(&supplies
->supplies_lock
);
970 static int gb_power_supplies_register(struct gb_power_supplies
*supplies
)
972 struct gb_connection
*connection
= supplies
->connection
;
976 mutex_lock(&supplies
->supplies_lock
);
978 for (i
= 0; i
< supplies
->supplies_count
; i
++) {
979 ret
= gb_power_supply_enable(&supplies
->supply
[i
]);
981 dev_err(&connection
->bundle
->dev
,
982 "Fail to enable supplies devices\n");
987 mutex_unlock(&supplies
->supplies_lock
);
991 static int gb_supplies_request_handler(struct gb_operation
*op
)
993 struct gb_connection
*connection
= op
->connection
;
994 struct gb_power_supplies
*supplies
= gb_connection_get_data(connection
);
995 struct gb_power_supply
*gbpsy
;
996 struct gb_message
*request
;
997 struct gb_power_supply_event_request
*payload
;
1002 if (op
->type
!= GB_POWER_SUPPLY_TYPE_EVENT
) {
1003 dev_err(&connection
->bundle
->dev
,
1004 "Unsupported unsolicited event: %u\n", op
->type
);
1008 request
= op
->request
;
1010 if (request
->payload_size
< sizeof(*payload
)) {
1011 dev_err(&connection
->bundle
->dev
,
1012 "Wrong event size received (%zu < %zu)\n",
1013 request
->payload_size
, sizeof(*payload
));
1017 payload
= request
->payload
;
1018 psy_id
= payload
->psy_id
;
1019 mutex_lock(&supplies
->supplies_lock
);
1020 if (psy_id
>= supplies
->supplies_count
||
1021 !supplies
->supply
[psy_id
].registered
) {
1022 dev_err(&connection
->bundle
->dev
,
1023 "Event received for unconfigured power_supply id: %d\n",
1029 event
= payload
->event
;
1031 * we will only handle events after setup is done and before release is
1032 * running. For that just check update_interval.
1034 gbpsy
= &supplies
->supply
[psy_id
];
1035 if (!gbpsy
->update_interval
) {
1040 if (event
& GB_POWER_SUPPLY_UPDATE
) {
1042 * we need to make sure we invalidate cache, if not no new
1043 * values for the properties will be fetch and the all propose
1044 * of this event is missed
1046 gbpsy
->cache_invalid
= 1;
1047 gb_power_supply_status_update(gbpsy
);
1051 mutex_unlock(&supplies
->supplies_lock
);
1055 static int gb_power_supply_probe(struct gb_bundle
*bundle
,
1056 const struct greybus_bundle_id
*id
)
1058 struct greybus_descriptor_cport
*cport_desc
;
1059 struct gb_connection
*connection
;
1060 struct gb_power_supplies
*supplies
;
1063 if (bundle
->num_cports
!= 1)
1066 cport_desc
= &bundle
->cport_desc
[0];
1067 if (cport_desc
->protocol_id
!= GREYBUS_PROTOCOL_POWER_SUPPLY
)
1070 supplies
= kzalloc(sizeof(*supplies
), GFP_KERNEL
);
1074 connection
= gb_connection_create(bundle
, le16_to_cpu(cport_desc
->id
),
1075 gb_supplies_request_handler
);
1076 if (IS_ERR(connection
)) {
1077 ret
= PTR_ERR(connection
);
1081 supplies
->connection
= connection
;
1082 gb_connection_set_data(connection
, supplies
);
1084 mutex_init(&supplies
->supplies_lock
);
1086 greybus_set_drvdata(bundle
, supplies
);
1088 /* We aren't ready to receive an incoming request yet */
1089 ret
= gb_connection_enable_tx(connection
);
1091 goto error_connection_destroy
;
1093 ret
= gb_power_supplies_setup(supplies
);
1095 goto error_connection_disable
;
1097 /* We are ready to receive an incoming request now, enable RX as well */
1098 ret
= gb_connection_enable(connection
);
1100 goto error_connection_disable
;
1102 ret
= gb_power_supplies_register(supplies
);
1104 goto error_connection_disable
;
1106 gb_pm_runtime_put_autosuspend(bundle
);
1109 error_connection_disable
:
1110 gb_connection_disable(connection
);
1111 error_connection_destroy
:
1112 gb_connection_destroy(connection
);
1114 _gb_power_supplies_release(supplies
);
1118 static void gb_power_supply_disconnect(struct gb_bundle
*bundle
)
1120 struct gb_power_supplies
*supplies
= greybus_get_drvdata(bundle
);
1122 gb_connection_disable(supplies
->connection
);
1123 gb_connection_destroy(supplies
->connection
);
1125 _gb_power_supplies_release(supplies
);
1128 static const struct greybus_bundle_id gb_power_supply_id_table
[] = {
1129 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_POWER_SUPPLY
) },
1132 MODULE_DEVICE_TABLE(greybus
, gb_power_supply_id_table
);
1134 static struct greybus_driver gb_power_supply_driver
= {
1135 .name
= "power_supply",
1136 .probe
= gb_power_supply_probe
,
1137 .disconnect
= gb_power_supply_disconnect
,
1138 .id_table
= gb_power_supply_id_table
,
1140 module_greybus_driver(gb_power_supply_driver
);
1142 MODULE_LICENSE("GPL v2");