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.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/power_supply.h>
12 #include <linux/slab.h>
13 #include <linux/greybus.h>
17 struct gb_power_supply_prop {
18 enum power_supply_property prop;
25 struct gb_power_supply {
28 struct power_supply *psy;
29 struct power_supply_desc desc;
31 struct gb_power_supplies *supplies;
32 struct delayed_work work;
38 u8 properties_count_str;
39 unsigned long last_update;
41 unsigned int update_interval;
43 struct gb_power_supply_prop *props;
44 enum power_supply_property *props_raw;
46 struct mutex supply_lock;
49 struct gb_power_supplies {
50 struct gb_connection *connection;
52 struct gb_power_supply *supply;
53 struct mutex supplies_lock;
56 #define to_gb_power_supply(x) power_supply_get_drvdata(x)
59 * General power supply properties that could be absent from various reasons,
60 * like kernel versions or vendor specific versions
62 #ifndef POWER_SUPPLY_PROP_VOLTAGE_BOOT
63 #define POWER_SUPPLY_PROP_VOLTAGE_BOOT -1
65 #ifndef POWER_SUPPLY_PROP_CURRENT_BOOT
66 #define POWER_SUPPLY_PROP_CURRENT_BOOT -1
68 #ifndef POWER_SUPPLY_PROP_CALIBRATE
69 #define POWER_SUPPLY_PROP_CALIBRATE -1
72 /* cache time in milliseconds, if cache_time is set to 0 cache is disable */
73 static unsigned int cache_time = 1000;
75 * update interval initial and maximum value, between the two will
76 * back-off exponential
78 static unsigned int update_interval_init = 1 * HZ;
79 static unsigned int update_interval_max = 30 * HZ;
81 struct gb_power_supply_changes {
82 enum power_supply_property prop;
84 void (*prop_changed)(struct gb_power_supply *gbpsy,
85 struct gb_power_supply_prop *prop);
88 static void gb_power_supply_state_change(struct gb_power_supply *gbpsy,
89 struct gb_power_supply_prop *prop);
91 static const struct gb_power_supply_changes psy_props_changes[] = {
92 { .prop = GB_POWER_SUPPLY_PROP_STATUS,
93 .tolerance_change = 0,
94 .prop_changed = gb_power_supply_state_change,
96 { .prop = GB_POWER_SUPPLY_PROP_TEMP,
97 .tolerance_change = 500,
100 { .prop = GB_POWER_SUPPLY_PROP_ONLINE,
101 .tolerance_change = 0,
102 .prop_changed = NULL,
106 static int get_psp_from_gb_prop(int gb_prop, enum power_supply_property *psp)
111 case GB_POWER_SUPPLY_PROP_STATUS:
112 prop = POWER_SUPPLY_PROP_STATUS;
114 case GB_POWER_SUPPLY_PROP_CHARGE_TYPE:
115 prop = POWER_SUPPLY_PROP_CHARGE_TYPE;
117 case GB_POWER_SUPPLY_PROP_HEALTH:
118 prop = POWER_SUPPLY_PROP_HEALTH;
120 case GB_POWER_SUPPLY_PROP_PRESENT:
121 prop = POWER_SUPPLY_PROP_PRESENT;
123 case GB_POWER_SUPPLY_PROP_ONLINE:
124 prop = POWER_SUPPLY_PROP_ONLINE;
126 case GB_POWER_SUPPLY_PROP_AUTHENTIC:
127 prop = POWER_SUPPLY_PROP_AUTHENTIC;
129 case GB_POWER_SUPPLY_PROP_TECHNOLOGY:
130 prop = POWER_SUPPLY_PROP_TECHNOLOGY;
132 case GB_POWER_SUPPLY_PROP_CYCLE_COUNT:
133 prop = POWER_SUPPLY_PROP_CYCLE_COUNT;
135 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX:
136 prop = POWER_SUPPLY_PROP_VOLTAGE_MAX;
138 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN:
139 prop = POWER_SUPPLY_PROP_VOLTAGE_MIN;
141 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
142 prop = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN;
144 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
145 prop = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN;
147 case GB_POWER_SUPPLY_PROP_VOLTAGE_NOW:
148 prop = POWER_SUPPLY_PROP_VOLTAGE_NOW;
150 case GB_POWER_SUPPLY_PROP_VOLTAGE_AVG:
151 prop = POWER_SUPPLY_PROP_VOLTAGE_AVG;
153 case GB_POWER_SUPPLY_PROP_VOLTAGE_OCV:
154 prop = POWER_SUPPLY_PROP_VOLTAGE_OCV;
156 case GB_POWER_SUPPLY_PROP_VOLTAGE_BOOT:
157 prop = POWER_SUPPLY_PROP_VOLTAGE_BOOT;
159 case GB_POWER_SUPPLY_PROP_CURRENT_MAX:
160 prop = POWER_SUPPLY_PROP_CURRENT_MAX;
162 case GB_POWER_SUPPLY_PROP_CURRENT_NOW:
163 prop = POWER_SUPPLY_PROP_CURRENT_NOW;
165 case GB_POWER_SUPPLY_PROP_CURRENT_AVG:
166 prop = POWER_SUPPLY_PROP_CURRENT_AVG;
168 case GB_POWER_SUPPLY_PROP_CURRENT_BOOT:
169 prop = POWER_SUPPLY_PROP_CURRENT_BOOT;
171 case GB_POWER_SUPPLY_PROP_POWER_NOW:
172 prop = POWER_SUPPLY_PROP_POWER_NOW;
174 case GB_POWER_SUPPLY_PROP_POWER_AVG:
175 prop = POWER_SUPPLY_PROP_POWER_AVG;
177 case GB_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
178 prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
180 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
181 prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
183 case GB_POWER_SUPPLY_PROP_CHARGE_FULL:
184 prop = POWER_SUPPLY_PROP_CHARGE_FULL;
186 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY:
187 prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
189 case GB_POWER_SUPPLY_PROP_CHARGE_NOW:
190 prop = POWER_SUPPLY_PROP_CHARGE_NOW;
192 case GB_POWER_SUPPLY_PROP_CHARGE_AVG:
193 prop = POWER_SUPPLY_PROP_CHARGE_AVG;
195 case GB_POWER_SUPPLY_PROP_CHARGE_COUNTER:
196 prop = POWER_SUPPLY_PROP_CHARGE_COUNTER;
198 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
199 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
201 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
202 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
204 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
205 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE;
207 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
208 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX;
210 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
211 prop = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT;
213 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
214 prop = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX;
216 case GB_POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
217 prop = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
219 case GB_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
220 prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
222 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
223 prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
225 case GB_POWER_SUPPLY_PROP_ENERGY_FULL:
226 prop = POWER_SUPPLY_PROP_ENERGY_FULL;
228 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY:
229 prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
231 case GB_POWER_SUPPLY_PROP_ENERGY_NOW:
232 prop = POWER_SUPPLY_PROP_ENERGY_NOW;
234 case GB_POWER_SUPPLY_PROP_ENERGY_AVG:
235 prop = POWER_SUPPLY_PROP_ENERGY_AVG;
237 case GB_POWER_SUPPLY_PROP_CAPACITY:
238 prop = POWER_SUPPLY_PROP_CAPACITY;
240 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
241 prop = POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN;
243 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX:
244 prop = POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX;
246 case GB_POWER_SUPPLY_PROP_CAPACITY_LEVEL:
247 prop = POWER_SUPPLY_PROP_CAPACITY_LEVEL;
249 case GB_POWER_SUPPLY_PROP_TEMP:
250 prop = POWER_SUPPLY_PROP_TEMP;
252 case GB_POWER_SUPPLY_PROP_TEMP_MAX:
253 prop = POWER_SUPPLY_PROP_TEMP_MAX;
255 case GB_POWER_SUPPLY_PROP_TEMP_MIN:
256 prop = POWER_SUPPLY_PROP_TEMP_MIN;
258 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
259 prop = POWER_SUPPLY_PROP_TEMP_ALERT_MIN;
261 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
262 prop = POWER_SUPPLY_PROP_TEMP_ALERT_MAX;
264 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT:
265 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT;
267 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
268 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN;
270 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
271 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX;
273 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
274 prop = POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW;
276 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
277 prop = POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG;
279 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
280 prop = POWER_SUPPLY_PROP_TIME_TO_FULL_NOW;
282 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
283 prop = POWER_SUPPLY_PROP_TIME_TO_FULL_AVG;
285 case GB_POWER_SUPPLY_PROP_TYPE:
286 prop = POWER_SUPPLY_PROP_TYPE;
288 case GB_POWER_SUPPLY_PROP_SCOPE:
289 prop = POWER_SUPPLY_PROP_SCOPE;
291 case GB_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
292 prop = POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT;
294 case GB_POWER_SUPPLY_PROP_CALIBRATE:
295 prop = POWER_SUPPLY_PROP_CALIBRATE;
305 *psp = (enum power_supply_property)prop;
310 static struct gb_connection *get_conn_from_psy(struct gb_power_supply *gbpsy)
312 return gbpsy->supplies->connection;
315 static struct gb_power_supply_prop *get_psy_prop(struct gb_power_supply *gbpsy,
316 enum power_supply_property psp)
320 for (i = 0; i < gbpsy->properties_count; i++)
321 if (gbpsy->props[i].prop == psp)
322 return &gbpsy->props[i];
326 static int is_psy_prop_writeable(struct gb_power_supply *gbpsy,
327 enum power_supply_property psp)
329 struct gb_power_supply_prop *prop;
331 prop = get_psy_prop(gbpsy, psp);
334 return prop->is_writeable ? 1 : 0;
337 static int is_prop_valint(enum power_supply_property psp)
339 return ((psp < POWER_SUPPLY_PROP_MODEL_NAME) ? 1 : 0);
342 static void next_interval(struct gb_power_supply *gbpsy)
344 if (gbpsy->update_interval == update_interval_max)
347 /* do some exponential back-off in the update interval */
348 gbpsy->update_interval *= 2;
349 if (gbpsy->update_interval > update_interval_max)
350 gbpsy->update_interval = update_interval_max;
353 static void __gb_power_supply_changed(struct gb_power_supply *gbpsy)
355 power_supply_changed(gbpsy->psy);
358 static void gb_power_supply_state_change(struct gb_power_supply *gbpsy,
359 struct gb_power_supply_prop *prop)
361 struct gb_connection *connection = get_conn_from_psy(gbpsy);
365 * Check gbpsy->pm_acquired to make sure only one pair of 'get_sync'
366 * and 'put_autosuspend' runtime pm call for state property change.
368 mutex_lock(&gbpsy->supply_lock);
370 if ((prop->val == GB_POWER_SUPPLY_STATUS_CHARGING) &&
371 !gbpsy->pm_acquired) {
372 ret = gb_pm_runtime_get_sync(connection->bundle);
374 dev_err(&connection->bundle->dev,
375 "Fail to set wake lock for charging state\n");
377 gbpsy->pm_acquired = true;
379 if (gbpsy->pm_acquired) {
380 ret = gb_pm_runtime_put_autosuspend(connection->bundle);
382 dev_err(&connection->bundle->dev,
383 "Fail to set wake unlock for none charging\n");
385 gbpsy->pm_acquired = false;
389 mutex_unlock(&gbpsy->supply_lock);
392 static void check_changed(struct gb_power_supply *gbpsy,
393 struct gb_power_supply_prop *prop)
395 const struct gb_power_supply_changes *psyc;
397 int prev_val = prop->previous_val;
398 bool changed = false;
401 for (i = 0; i < ARRAY_SIZE(psy_props_changes); i++) {
402 psyc = &psy_props_changes[i];
403 if (prop->prop == psyc->prop) {
404 if (!psyc->tolerance_change)
406 else if (val < prev_val &&
407 prev_val - val > psyc->tolerance_change)
409 else if (val > prev_val &&
410 val - prev_val > psyc->tolerance_change)
413 if (changed && psyc->prop_changed)
414 psyc->prop_changed(gbpsy, prop);
417 gbpsy->changed = true;
423 static int total_props(struct gb_power_supply *gbpsy)
425 /* this return the intval plus the strval properties */
426 return (gbpsy->properties_count + gbpsy->properties_count_str);
429 static void prop_append(struct gb_power_supply *gbpsy,
430 enum power_supply_property prop)
432 enum power_supply_property *new_props_raw;
434 gbpsy->properties_count_str++;
435 new_props_raw = krealloc(gbpsy->props_raw, total_props(gbpsy) *
436 sizeof(enum power_supply_property),
440 gbpsy->props_raw = new_props_raw;
441 gbpsy->props_raw[total_props(gbpsy) - 1] = prop;
444 static int __gb_power_supply_set_name(char *init_name, char *name, size_t len)
448 struct power_supply *psy;
450 if (!strlen(init_name))
451 init_name = "gb_power_supply";
452 strscpy(name, init_name, len);
454 while ((ret < len) && (psy = power_supply_get_by_name(name))) {
455 power_supply_put(psy);
457 ret = snprintf(name, len, "%s_%u", init_name, ++i);
464 static void _gb_power_supply_append_props(struct gb_power_supply *gbpsy)
466 if (strlen(gbpsy->manufacturer))
467 prop_append(gbpsy, POWER_SUPPLY_PROP_MANUFACTURER);
468 if (strlen(gbpsy->model_name))
469 prop_append(gbpsy, POWER_SUPPLY_PROP_MODEL_NAME);
470 if (strlen(gbpsy->serial_number))
471 prop_append(gbpsy, POWER_SUPPLY_PROP_SERIAL_NUMBER);
474 static int gb_power_supply_description_get(struct gb_power_supply *gbpsy)
476 struct gb_connection *connection = get_conn_from_psy(gbpsy);
477 struct gb_power_supply_get_description_request req;
478 struct gb_power_supply_get_description_response resp;
481 req.psy_id = gbpsy->id;
483 ret = gb_operation_sync(connection,
484 GB_POWER_SUPPLY_TYPE_GET_DESCRIPTION,
485 &req, sizeof(req), &resp, sizeof(resp));
489 gbpsy->manufacturer = kstrndup(resp.manufacturer, PROP_MAX, GFP_KERNEL);
490 if (!gbpsy->manufacturer)
492 gbpsy->model_name = kstrndup(resp.model, PROP_MAX, GFP_KERNEL);
493 if (!gbpsy->model_name)
495 gbpsy->serial_number = kstrndup(resp.serial_number, PROP_MAX,
497 if (!gbpsy->serial_number)
500 gbpsy->type = le16_to_cpu(resp.type);
501 gbpsy->properties_count = resp.properties_count;
506 static int gb_power_supply_prop_descriptors_get(struct gb_power_supply *gbpsy)
508 struct gb_connection *connection = get_conn_from_psy(gbpsy);
509 struct gb_power_supply_get_property_descriptors_request *req;
510 struct gb_power_supply_get_property_descriptors_response *resp;
511 struct gb_operation *op;
512 u8 props_count = gbpsy->properties_count;
513 enum power_supply_property psp;
517 if (props_count == 0)
520 op = gb_operation_create(connection,
521 GB_POWER_SUPPLY_TYPE_GET_PROP_DESCRIPTORS,
523 struct_size(resp, props, props_count),
528 req = op->request->payload;
529 req->psy_id = gbpsy->id;
531 ret = gb_operation_request_send_sync(op);
533 goto out_put_operation;
535 resp = op->response->payload;
537 /* validate received properties */
538 for (i = 0; i < props_count; i++) {
539 ret = get_psp_from_gb_prop(resp->props[i].property, &psp);
541 dev_warn(&connection->bundle->dev,
542 "greybus property %u it is not supported by this kernel, dropped\n",
543 resp->props[i].property);
544 gbpsy->properties_count--;
548 gbpsy->props = kcalloc(gbpsy->properties_count, sizeof(*gbpsy->props),
552 goto out_put_operation;
555 gbpsy->props_raw = kcalloc(gbpsy->properties_count,
556 sizeof(*gbpsy->props_raw), GFP_KERNEL);
557 if (!gbpsy->props_raw) {
559 goto out_put_operation;
562 /* Store available properties, skip the ones we do not support */
563 for (i = 0; i < props_count; i++) {
564 ret = get_psp_from_gb_prop(resp->props[i].property, &psp);
569 gbpsy->props[i - r].prop = psp;
570 gbpsy->props[i - r].gb_prop = resp->props[i].property;
571 gbpsy->props_raw[i - r] = psp;
572 if (resp->props[i].is_writeable)
573 gbpsy->props[i - r].is_writeable = true;
577 * now append the properties that we already got information in the
578 * get_description operation. (char * ones)
580 _gb_power_supply_append_props(gbpsy);
584 gb_operation_put(op);
589 static int __gb_power_supply_property_update(struct gb_power_supply *gbpsy,
590 enum power_supply_property psp)
592 struct gb_connection *connection = get_conn_from_psy(gbpsy);
593 struct gb_power_supply_prop *prop;
594 struct gb_power_supply_get_property_request req;
595 struct gb_power_supply_get_property_response resp;
599 prop = get_psy_prop(gbpsy, psp);
602 req.psy_id = gbpsy->id;
603 req.property = prop->gb_prop;
605 ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_GET_PROPERTY,
606 &req, sizeof(req), &resp, sizeof(resp));
610 val = le32_to_cpu(resp.prop_val);
611 if (val == prop->val)
614 prop->previous_val = prop->val;
617 check_changed(gbpsy, prop);
622 static int __gb_power_supply_property_get(struct gb_power_supply *gbpsy,
623 enum power_supply_property psp,
624 union power_supply_propval *val)
626 struct gb_power_supply_prop *prop;
628 prop = get_psy_prop(gbpsy, psp);
632 val->intval = prop->val;
636 static int __gb_power_supply_property_strval_get(struct gb_power_supply *gbpsy,
637 enum power_supply_property psp,
638 union power_supply_propval *val)
641 case POWER_SUPPLY_PROP_MODEL_NAME:
642 val->strval = gbpsy->model_name;
644 case POWER_SUPPLY_PROP_MANUFACTURER:
645 val->strval = gbpsy->manufacturer;
647 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
648 val->strval = gbpsy->serial_number;
657 static int _gb_power_supply_property_get(struct gb_power_supply *gbpsy,
658 enum power_supply_property psp,
659 union power_supply_propval *val)
661 struct gb_connection *connection = get_conn_from_psy(gbpsy);
665 * Properties of type const char *, were already fetched on
666 * get_description operation and should be cached in gb
668 if (is_prop_valint(psp))
669 ret = __gb_power_supply_property_get(gbpsy, psp, val);
671 ret = __gb_power_supply_property_strval_get(gbpsy, psp, val);
674 dev_err(&connection->bundle->dev, "get property %u\n", psp);
679 static int is_cache_valid(struct gb_power_supply *gbpsy)
681 /* check if cache is good enough or it has expired */
682 if (gbpsy->cache_invalid) {
683 gbpsy->cache_invalid = 0;
687 if (gbpsy->last_update &&
688 time_is_after_jiffies(gbpsy->last_update +
689 msecs_to_jiffies(cache_time)))
695 static int gb_power_supply_status_get(struct gb_power_supply *gbpsy)
697 struct gb_connection *connection = get_conn_from_psy(gbpsy);
701 if (is_cache_valid(gbpsy))
704 ret = gb_pm_runtime_get_sync(connection->bundle);
708 for (i = 0; i < gbpsy->properties_count; i++) {
709 ret = __gb_power_supply_property_update(gbpsy,
710 gbpsy->props[i].prop);
716 gbpsy->last_update = jiffies;
718 gb_pm_runtime_put_autosuspend(connection->bundle);
722 static void gb_power_supply_status_update(struct gb_power_supply *gbpsy)
724 /* check if there a change that need to be reported */
725 gb_power_supply_status_get(gbpsy);
730 gbpsy->update_interval = update_interval_init;
731 __gb_power_supply_changed(gbpsy);
732 gbpsy->changed = false;
735 static void gb_power_supply_work(struct work_struct *work)
737 struct gb_power_supply *gbpsy = container_of(work,
738 struct gb_power_supply,
742 * if the poll interval is not set, disable polling, this is helpful
743 * specially at unregister time.
745 if (!gbpsy->update_interval)
748 gb_power_supply_status_update(gbpsy);
749 next_interval(gbpsy);
750 schedule_delayed_work(&gbpsy->work, gbpsy->update_interval);
753 static int get_property(struct power_supply *b,
754 enum power_supply_property psp,
755 union power_supply_propval *val)
757 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
759 gb_power_supply_status_get(gbpsy);
761 return _gb_power_supply_property_get(gbpsy, psp, val);
764 static int gb_power_supply_property_set(struct gb_power_supply *gbpsy,
765 enum power_supply_property psp,
768 struct gb_connection *connection = get_conn_from_psy(gbpsy);
769 struct gb_power_supply_prop *prop;
770 struct gb_power_supply_set_property_request req;
773 ret = gb_pm_runtime_get_sync(connection->bundle);
777 prop = get_psy_prop(gbpsy, psp);
783 req.psy_id = gbpsy->id;
784 req.property = prop->gb_prop;
785 req.prop_val = cpu_to_le32((s32)val);
787 ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_SET_PROPERTY,
788 &req, sizeof(req), NULL, 0);
792 /* cache immediately the new value */
796 gb_pm_runtime_put_autosuspend(connection->bundle);
800 static int set_property(struct power_supply *b,
801 enum power_supply_property psp,
802 const union power_supply_propval *val)
804 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
806 return gb_power_supply_property_set(gbpsy, psp, val->intval);
809 static int property_is_writeable(struct power_supply *b,
810 enum power_supply_property psp)
812 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
814 return is_psy_prop_writeable(gbpsy, psp);
817 static int gb_power_supply_register(struct gb_power_supply *gbpsy)
819 struct gb_connection *connection = get_conn_from_psy(gbpsy);
820 struct power_supply_config cfg = {};
822 cfg.drv_data = gbpsy;
824 gbpsy->desc.name = gbpsy->name;
825 gbpsy->desc.type = gbpsy->type;
826 gbpsy->desc.properties = gbpsy->props_raw;
827 gbpsy->desc.num_properties = total_props(gbpsy);
828 gbpsy->desc.get_property = get_property;
829 gbpsy->desc.set_property = set_property;
830 gbpsy->desc.property_is_writeable = property_is_writeable;
832 gbpsy->psy = power_supply_register(&connection->bundle->dev,
834 return PTR_ERR_OR_ZERO(gbpsy->psy);
837 static void _gb_power_supply_free(struct gb_power_supply *gbpsy)
839 kfree(gbpsy->serial_number);
840 kfree(gbpsy->model_name);
841 kfree(gbpsy->manufacturer);
842 kfree(gbpsy->props_raw);
846 static void _gb_power_supply_release(struct gb_power_supply *gbpsy)
848 gbpsy->update_interval = 0;
850 cancel_delayed_work_sync(&gbpsy->work);
852 if (gbpsy->registered)
853 power_supply_unregister(gbpsy->psy);
855 _gb_power_supply_free(gbpsy);
858 static void _gb_power_supplies_release(struct gb_power_supplies *supplies)
862 if (!supplies->supply)
865 mutex_lock(&supplies->supplies_lock);
866 for (i = 0; i < supplies->supplies_count; i++)
867 _gb_power_supply_release(&supplies->supply[i]);
868 kfree(supplies->supply);
869 mutex_unlock(&supplies->supplies_lock);
873 static int gb_power_supplies_get_count(struct gb_power_supplies *supplies)
875 struct gb_power_supply_get_supplies_response resp;
878 ret = gb_operation_sync(supplies->connection,
879 GB_POWER_SUPPLY_TYPE_GET_SUPPLIES,
880 NULL, 0, &resp, sizeof(resp));
884 if (!resp.supplies_count)
887 supplies->supplies_count = resp.supplies_count;
892 static int gb_power_supply_config(struct gb_power_supplies *supplies, int id)
894 struct gb_power_supply *gbpsy = &supplies->supply[id];
897 gbpsy->supplies = supplies;
900 ret = gb_power_supply_description_get(gbpsy);
904 return gb_power_supply_prop_descriptors_get(gbpsy);
907 static int gb_power_supply_enable(struct gb_power_supply *gbpsy)
911 /* guarantee that we have an unique name, before register */
912 ret = __gb_power_supply_set_name(gbpsy->model_name, gbpsy->name,
913 sizeof(gbpsy->name));
917 mutex_init(&gbpsy->supply_lock);
919 ret = gb_power_supply_register(gbpsy);
923 gbpsy->update_interval = update_interval_init;
924 INIT_DELAYED_WORK(&gbpsy->work, gb_power_supply_work);
925 schedule_delayed_work(&gbpsy->work, 0);
927 /* everything went fine, mark it for release code to know */
928 gbpsy->registered = true;
933 static int gb_power_supplies_setup(struct gb_power_supplies *supplies)
935 struct gb_connection *connection = supplies->connection;
939 mutex_lock(&supplies->supplies_lock);
941 ret = gb_power_supplies_get_count(supplies);
945 supplies->supply = kcalloc(supplies->supplies_count,
946 sizeof(struct gb_power_supply),
949 if (!supplies->supply) {
954 for (i = 0; i < supplies->supplies_count; i++) {
955 ret = gb_power_supply_config(supplies, i);
957 dev_err(&connection->bundle->dev,
958 "Fail to configure supplies devices\n");
963 mutex_unlock(&supplies->supplies_lock);
967 static int gb_power_supplies_register(struct gb_power_supplies *supplies)
969 struct gb_connection *connection = supplies->connection;
973 mutex_lock(&supplies->supplies_lock);
975 for (i = 0; i < supplies->supplies_count; i++) {
976 ret = gb_power_supply_enable(&supplies->supply[i]);
978 dev_err(&connection->bundle->dev,
979 "Fail to enable supplies devices\n");
984 mutex_unlock(&supplies->supplies_lock);
988 static int gb_supplies_request_handler(struct gb_operation *op)
990 struct gb_connection *connection = op->connection;
991 struct gb_power_supplies *supplies = gb_connection_get_data(connection);
992 struct gb_power_supply *gbpsy;
993 struct gb_message *request;
994 struct gb_power_supply_event_request *payload;
999 if (op->type != GB_POWER_SUPPLY_TYPE_EVENT) {
1000 dev_err(&connection->bundle->dev,
1001 "Unsupported unsolicited event: %u\n", op->type);
1005 request = op->request;
1007 if (request->payload_size < sizeof(*payload)) {
1008 dev_err(&connection->bundle->dev,
1009 "Wrong event size received (%zu < %zu)\n",
1010 request->payload_size, sizeof(*payload));
1014 payload = request->payload;
1015 psy_id = payload->psy_id;
1016 mutex_lock(&supplies->supplies_lock);
1017 if (psy_id >= supplies->supplies_count ||
1018 !supplies->supply[psy_id].registered) {
1019 dev_err(&connection->bundle->dev,
1020 "Event received for unconfigured power_supply id: %d\n",
1026 event = payload->event;
1028 * we will only handle events after setup is done and before release is
1029 * running. For that just check update_interval.
1031 gbpsy = &supplies->supply[psy_id];
1032 if (!gbpsy->update_interval) {
1037 if (event & GB_POWER_SUPPLY_UPDATE) {
1039 * we need to make sure we invalidate cache, if not no new
1040 * values for the properties will be fetch and the all propose
1041 * of this event is missed
1043 gbpsy->cache_invalid = 1;
1044 gb_power_supply_status_update(gbpsy);
1048 mutex_unlock(&supplies->supplies_lock);
1052 static int gb_power_supply_probe(struct gb_bundle *bundle,
1053 const struct greybus_bundle_id *id)
1055 struct greybus_descriptor_cport *cport_desc;
1056 struct gb_connection *connection;
1057 struct gb_power_supplies *supplies;
1060 if (bundle->num_cports != 1)
1063 cport_desc = &bundle->cport_desc[0];
1064 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_POWER_SUPPLY)
1067 supplies = kzalloc(sizeof(*supplies), GFP_KERNEL);
1071 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1072 gb_supplies_request_handler);
1073 if (IS_ERR(connection)) {
1074 ret = PTR_ERR(connection);
1078 supplies->connection = connection;
1079 gb_connection_set_data(connection, supplies);
1081 mutex_init(&supplies->supplies_lock);
1083 greybus_set_drvdata(bundle, supplies);
1085 /* We aren't ready to receive an incoming request yet */
1086 ret = gb_connection_enable_tx(connection);
1088 goto error_connection_destroy;
1090 ret = gb_power_supplies_setup(supplies);
1092 goto error_connection_disable;
1094 /* We are ready to receive an incoming request now, enable RX as well */
1095 ret = gb_connection_enable(connection);
1097 goto error_connection_disable;
1099 ret = gb_power_supplies_register(supplies);
1101 goto error_connection_disable;
1103 gb_pm_runtime_put_autosuspend(bundle);
1106 error_connection_disable:
1107 gb_connection_disable(connection);
1108 error_connection_destroy:
1109 gb_connection_destroy(connection);
1111 _gb_power_supplies_release(supplies);
1115 static void gb_power_supply_disconnect(struct gb_bundle *bundle)
1117 struct gb_power_supplies *supplies = greybus_get_drvdata(bundle);
1119 gb_connection_disable(supplies->connection);
1120 gb_connection_destroy(supplies->connection);
1122 _gb_power_supplies_release(supplies);
1125 static const struct greybus_bundle_id gb_power_supply_id_table[] = {
1126 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_POWER_SUPPLY) },
1129 MODULE_DEVICE_TABLE(greybus, gb_power_supply_id_table);
1131 static struct greybus_driver gb_power_supply_driver = {
1132 .name = "power_supply",
1133 .probe = gb_power_supply_probe,
1134 .disconnect = gb_power_supply_disconnect,
1135 .id_table = gb_power_supply_id_table,
1137 module_greybus_driver(gb_power_supply_driver);
1139 MODULE_LICENSE("GPL v2");