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>
18 struct gb_power_supply_prop {
19 enum power_supply_property prop;
26 struct gb_power_supply {
29 struct power_supply *psy;
30 struct power_supply_desc desc;
32 struct gb_power_supplies *supplies;
33 struct delayed_work work;
39 u8 properties_count_str;
40 unsigned long last_update;
42 unsigned int update_interval;
44 struct gb_power_supply_prop *props;
45 enum power_supply_property *props_raw;
47 struct mutex supply_lock;
50 struct gb_power_supplies {
51 struct gb_connection *connection;
53 struct gb_power_supply *supply;
54 struct mutex supplies_lock;
57 #define to_gb_power_supply(x) power_supply_get_drvdata(x)
60 * General power supply properties that could be absent from various reasons,
61 * like kernel versions or vendor specific versions
63 #ifndef POWER_SUPPLY_PROP_VOLTAGE_BOOT
64 #define POWER_SUPPLY_PROP_VOLTAGE_BOOT -1
66 #ifndef POWER_SUPPLY_PROP_CURRENT_BOOT
67 #define POWER_SUPPLY_PROP_CURRENT_BOOT -1
69 #ifndef POWER_SUPPLY_PROP_CALIBRATE
70 #define POWER_SUPPLY_PROP_CALIBRATE -1
73 /* cache time in milliseconds, if cache_time is set to 0 cache is disable */
74 static unsigned int cache_time = 1000;
76 * update interval initial and maximum value, between the two will
77 * back-off exponential
79 static unsigned int update_interval_init = 1 * HZ;
80 static unsigned int update_interval_max = 30 * HZ;
82 struct gb_power_supply_changes {
83 enum power_supply_property prop;
85 void (*prop_changed)(struct gb_power_supply *gbpsy,
86 struct gb_power_supply_prop *prop);
89 static void gb_power_supply_state_change(struct gb_power_supply *gbpsy,
90 struct gb_power_supply_prop *prop);
92 static const struct gb_power_supply_changes psy_props_changes[] = {
93 { .prop = GB_POWER_SUPPLY_PROP_STATUS,
94 .tolerance_change = 0,
95 .prop_changed = gb_power_supply_state_change,
97 { .prop = GB_POWER_SUPPLY_PROP_TEMP,
98 .tolerance_change = 500,
101 { .prop = GB_POWER_SUPPLY_PROP_ONLINE,
102 .tolerance_change = 0,
103 .prop_changed = NULL,
107 static int get_psp_from_gb_prop(int gb_prop, enum power_supply_property *psp)
112 case GB_POWER_SUPPLY_PROP_STATUS:
113 prop = POWER_SUPPLY_PROP_STATUS;
115 case GB_POWER_SUPPLY_PROP_CHARGE_TYPE:
116 prop = POWER_SUPPLY_PROP_CHARGE_TYPE;
118 case GB_POWER_SUPPLY_PROP_HEALTH:
119 prop = POWER_SUPPLY_PROP_HEALTH;
121 case GB_POWER_SUPPLY_PROP_PRESENT:
122 prop = POWER_SUPPLY_PROP_PRESENT;
124 case GB_POWER_SUPPLY_PROP_ONLINE:
125 prop = POWER_SUPPLY_PROP_ONLINE;
127 case GB_POWER_SUPPLY_PROP_AUTHENTIC:
128 prop = POWER_SUPPLY_PROP_AUTHENTIC;
130 case GB_POWER_SUPPLY_PROP_TECHNOLOGY:
131 prop = POWER_SUPPLY_PROP_TECHNOLOGY;
133 case GB_POWER_SUPPLY_PROP_CYCLE_COUNT:
134 prop = POWER_SUPPLY_PROP_CYCLE_COUNT;
136 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX:
137 prop = POWER_SUPPLY_PROP_VOLTAGE_MAX;
139 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN:
140 prop = POWER_SUPPLY_PROP_VOLTAGE_MIN;
142 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
143 prop = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN;
145 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
146 prop = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN;
148 case GB_POWER_SUPPLY_PROP_VOLTAGE_NOW:
149 prop = POWER_SUPPLY_PROP_VOLTAGE_NOW;
151 case GB_POWER_SUPPLY_PROP_VOLTAGE_AVG:
152 prop = POWER_SUPPLY_PROP_VOLTAGE_AVG;
154 case GB_POWER_SUPPLY_PROP_VOLTAGE_OCV:
155 prop = POWER_SUPPLY_PROP_VOLTAGE_OCV;
157 case GB_POWER_SUPPLY_PROP_VOLTAGE_BOOT:
158 prop = POWER_SUPPLY_PROP_VOLTAGE_BOOT;
160 case GB_POWER_SUPPLY_PROP_CURRENT_MAX:
161 prop = POWER_SUPPLY_PROP_CURRENT_MAX;
163 case GB_POWER_SUPPLY_PROP_CURRENT_NOW:
164 prop = POWER_SUPPLY_PROP_CURRENT_NOW;
166 case GB_POWER_SUPPLY_PROP_CURRENT_AVG:
167 prop = POWER_SUPPLY_PROP_CURRENT_AVG;
169 case GB_POWER_SUPPLY_PROP_CURRENT_BOOT:
170 prop = POWER_SUPPLY_PROP_CURRENT_BOOT;
172 case GB_POWER_SUPPLY_PROP_POWER_NOW:
173 prop = POWER_SUPPLY_PROP_POWER_NOW;
175 case GB_POWER_SUPPLY_PROP_POWER_AVG:
176 prop = POWER_SUPPLY_PROP_POWER_AVG;
178 case GB_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
179 prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
181 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
182 prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
184 case GB_POWER_SUPPLY_PROP_CHARGE_FULL:
185 prop = POWER_SUPPLY_PROP_CHARGE_FULL;
187 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY:
188 prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
190 case GB_POWER_SUPPLY_PROP_CHARGE_NOW:
191 prop = POWER_SUPPLY_PROP_CHARGE_NOW;
193 case GB_POWER_SUPPLY_PROP_CHARGE_AVG:
194 prop = POWER_SUPPLY_PROP_CHARGE_AVG;
196 case GB_POWER_SUPPLY_PROP_CHARGE_COUNTER:
197 prop = POWER_SUPPLY_PROP_CHARGE_COUNTER;
199 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
200 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
202 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
203 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
205 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
206 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE;
208 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
209 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX;
211 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
212 prop = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT;
214 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
215 prop = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX;
217 case GB_POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
218 prop = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
220 case GB_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
221 prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
223 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
224 prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
226 case GB_POWER_SUPPLY_PROP_ENERGY_FULL:
227 prop = POWER_SUPPLY_PROP_ENERGY_FULL;
229 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY:
230 prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
232 case GB_POWER_SUPPLY_PROP_ENERGY_NOW:
233 prop = POWER_SUPPLY_PROP_ENERGY_NOW;
235 case GB_POWER_SUPPLY_PROP_ENERGY_AVG:
236 prop = POWER_SUPPLY_PROP_ENERGY_AVG;
238 case GB_POWER_SUPPLY_PROP_CAPACITY:
239 prop = POWER_SUPPLY_PROP_CAPACITY;
241 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
242 prop = POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN;
244 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX:
245 prop = POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX;
247 case GB_POWER_SUPPLY_PROP_CAPACITY_LEVEL:
248 prop = POWER_SUPPLY_PROP_CAPACITY_LEVEL;
250 case GB_POWER_SUPPLY_PROP_TEMP:
251 prop = POWER_SUPPLY_PROP_TEMP;
253 case GB_POWER_SUPPLY_PROP_TEMP_MAX:
254 prop = POWER_SUPPLY_PROP_TEMP_MAX;
256 case GB_POWER_SUPPLY_PROP_TEMP_MIN:
257 prop = POWER_SUPPLY_PROP_TEMP_MIN;
259 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
260 prop = POWER_SUPPLY_PROP_TEMP_ALERT_MIN;
262 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
263 prop = POWER_SUPPLY_PROP_TEMP_ALERT_MAX;
265 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT:
266 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT;
268 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
269 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN;
271 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
272 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX;
274 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
275 prop = POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW;
277 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
278 prop = POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG;
280 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
281 prop = POWER_SUPPLY_PROP_TIME_TO_FULL_NOW;
283 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
284 prop = POWER_SUPPLY_PROP_TIME_TO_FULL_AVG;
286 case GB_POWER_SUPPLY_PROP_TYPE:
287 prop = POWER_SUPPLY_PROP_TYPE;
289 case GB_POWER_SUPPLY_PROP_SCOPE:
290 prop = POWER_SUPPLY_PROP_SCOPE;
292 case GB_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
293 prop = POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT;
295 case GB_POWER_SUPPLY_PROP_CALIBRATE:
296 prop = POWER_SUPPLY_PROP_CALIBRATE;
306 *psp = (enum power_supply_property)prop;
311 static struct gb_connection *get_conn_from_psy(struct gb_power_supply *gbpsy)
313 return gbpsy->supplies->connection;
316 static struct gb_power_supply_prop *get_psy_prop(struct gb_power_supply *gbpsy,
317 enum power_supply_property psp)
321 for (i = 0; i < gbpsy->properties_count; i++)
322 if (gbpsy->props[i].prop == psp)
323 return &gbpsy->props[i];
327 static int is_psy_prop_writeable(struct gb_power_supply *gbpsy,
328 enum power_supply_property psp)
330 struct gb_power_supply_prop *prop;
332 prop = get_psy_prop(gbpsy, psp);
335 return prop->is_writeable ? 1 : 0;
338 static int is_prop_valint(enum power_supply_property psp)
340 return ((psp < POWER_SUPPLY_PROP_MODEL_NAME) ? 1 : 0);
343 static void next_interval(struct gb_power_supply *gbpsy)
345 if (gbpsy->update_interval == update_interval_max)
348 /* do some exponential back-off in the update interval */
349 gbpsy->update_interval *= 2;
350 if (gbpsy->update_interval > update_interval_max)
351 gbpsy->update_interval = update_interval_max;
354 static void __gb_power_supply_changed(struct gb_power_supply *gbpsy)
356 power_supply_changed(gbpsy->psy);
359 static void gb_power_supply_state_change(struct gb_power_supply *gbpsy,
360 struct gb_power_supply_prop *prop)
362 struct gb_connection *connection = get_conn_from_psy(gbpsy);
366 * Check gbpsy->pm_acquired to make sure only one pair of 'get_sync'
367 * and 'put_autosuspend' runtime pm call for state property change.
369 mutex_lock(&gbpsy->supply_lock);
371 if ((prop->val == GB_POWER_SUPPLY_STATUS_CHARGING) &&
372 !gbpsy->pm_acquired) {
373 ret = gb_pm_runtime_get_sync(connection->bundle);
375 dev_err(&connection->bundle->dev,
376 "Fail to set wake lock for charging state\n");
378 gbpsy->pm_acquired = true;
380 if (gbpsy->pm_acquired) {
381 ret = gb_pm_runtime_put_autosuspend(connection->bundle);
383 dev_err(&connection->bundle->dev,
384 "Fail to set wake unlock for none charging\n");
386 gbpsy->pm_acquired = false;
390 mutex_unlock(&gbpsy->supply_lock);
393 static void check_changed(struct gb_power_supply *gbpsy,
394 struct gb_power_supply_prop *prop)
396 const struct gb_power_supply_changes *psyc;
398 int prev_val = prop->previous_val;
399 bool changed = false;
402 for (i = 0; i < ARRAY_SIZE(psy_props_changes); i++) {
403 psyc = &psy_props_changes[i];
404 if (prop->prop == psyc->prop) {
405 if (!psyc->tolerance_change)
407 else if (val < prev_val &&
408 prev_val - val > psyc->tolerance_change)
410 else if (val > prev_val &&
411 val - prev_val > psyc->tolerance_change)
414 if (changed && psyc->prop_changed)
415 psyc->prop_changed(gbpsy, prop);
418 gbpsy->changed = true;
424 static int total_props(struct gb_power_supply *gbpsy)
426 /* this return the intval plus the strval properties */
427 return (gbpsy->properties_count + gbpsy->properties_count_str);
430 static void prop_append(struct gb_power_supply *gbpsy,
431 enum power_supply_property prop)
433 enum power_supply_property *new_props_raw;
435 gbpsy->properties_count_str++;
436 new_props_raw = krealloc(gbpsy->props_raw, total_props(gbpsy) *
437 sizeof(enum power_supply_property),
441 gbpsy->props_raw = new_props_raw;
442 gbpsy->props_raw[total_props(gbpsy) - 1] = prop;
445 static int __gb_power_supply_set_name(char *init_name, char *name, size_t len)
449 struct power_supply *psy;
451 if (!strlen(init_name))
452 init_name = "gb_power_supply";
453 strlcpy(name, init_name, len);
455 while ((ret < len) && (psy = power_supply_get_by_name(name))) {
456 power_supply_put(psy);
458 ret = snprintf(name, len, "%s_%u", init_name, ++i);
465 static void _gb_power_supply_append_props(struct gb_power_supply *gbpsy)
467 if (strlen(gbpsy->manufacturer))
468 prop_append(gbpsy, POWER_SUPPLY_PROP_MANUFACTURER);
469 if (strlen(gbpsy->model_name))
470 prop_append(gbpsy, POWER_SUPPLY_PROP_MODEL_NAME);
471 if (strlen(gbpsy->serial_number))
472 prop_append(gbpsy, POWER_SUPPLY_PROP_SERIAL_NUMBER);
475 static int gb_power_supply_description_get(struct gb_power_supply *gbpsy)
477 struct gb_connection *connection = get_conn_from_psy(gbpsy);
478 struct gb_power_supply_get_description_request req;
479 struct gb_power_supply_get_description_response resp;
482 req.psy_id = gbpsy->id;
484 ret = gb_operation_sync(connection,
485 GB_POWER_SUPPLY_TYPE_GET_DESCRIPTION,
486 &req, sizeof(req), &resp, sizeof(resp));
490 gbpsy->manufacturer = kstrndup(resp.manufacturer, PROP_MAX, GFP_KERNEL);
491 if (!gbpsy->manufacturer)
493 gbpsy->model_name = kstrndup(resp.model, PROP_MAX, GFP_KERNEL);
494 if (!gbpsy->model_name)
496 gbpsy->serial_number = kstrndup(resp.serial_number, PROP_MAX,
498 if (!gbpsy->serial_number)
501 gbpsy->type = le16_to_cpu(resp.type);
502 gbpsy->properties_count = resp.properties_count;
507 static int gb_power_supply_prop_descriptors_get(struct gb_power_supply *gbpsy)
509 struct gb_connection *connection = get_conn_from_psy(gbpsy);
510 struct gb_power_supply_get_property_descriptors_request *req;
511 struct gb_power_supply_get_property_descriptors_response *resp;
512 struct gb_operation *op;
513 u8 props_count = gbpsy->properties_count;
514 enum power_supply_property psp;
518 if (props_count == 0)
521 op = gb_operation_create(connection,
522 GB_POWER_SUPPLY_TYPE_GET_PROP_DESCRIPTORS,
523 sizeof(*req), sizeof(*resp) + props_count *
524 sizeof(struct gb_power_supply_props_desc),
529 req = op->request->payload;
530 req->psy_id = gbpsy->id;
532 ret = gb_operation_request_send_sync(op);
534 goto out_put_operation;
536 resp = op->response->payload;
538 /* validate received properties */
539 for (i = 0; i < props_count; i++) {
540 ret = get_psp_from_gb_prop(resp->props[i].property, &psp);
542 dev_warn(&connection->bundle->dev,
543 "greybus property %u it is not supported by this kernel, dropped\n",
544 resp->props[i].property);
545 gbpsy->properties_count--;
549 gbpsy->props = kcalloc(gbpsy->properties_count, sizeof(*gbpsy->props),
553 goto out_put_operation;
556 gbpsy->props_raw = kcalloc(gbpsy->properties_count,
557 sizeof(*gbpsy->props_raw), GFP_KERNEL);
558 if (!gbpsy->props_raw) {
560 goto out_put_operation;
563 /* Store available properties, skip the ones we do not support */
564 for (i = 0; i < props_count; i++) {
565 ret = get_psp_from_gb_prop(resp->props[i].property, &psp);
570 gbpsy->props[i - r].prop = psp;
571 gbpsy->props[i - r].gb_prop = resp->props[i].property;
572 gbpsy->props_raw[i - r] = psp;
573 if (resp->props[i].is_writeable)
574 gbpsy->props[i - r].is_writeable = true;
578 * now append the properties that we already got information in the
579 * get_description operation. (char * ones)
581 _gb_power_supply_append_props(gbpsy);
585 gb_operation_put(op);
590 static int __gb_power_supply_property_update(struct gb_power_supply *gbpsy,
591 enum power_supply_property psp)
593 struct gb_connection *connection = get_conn_from_psy(gbpsy);
594 struct gb_power_supply_prop *prop;
595 struct gb_power_supply_get_property_request req;
596 struct gb_power_supply_get_property_response resp;
600 prop = get_psy_prop(gbpsy, psp);
603 req.psy_id = gbpsy->id;
604 req.property = prop->gb_prop;
606 ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_GET_PROPERTY,
607 &req, sizeof(req), &resp, sizeof(resp));
611 val = le32_to_cpu(resp.prop_val);
612 if (val == prop->val)
615 prop->previous_val = prop->val;
618 check_changed(gbpsy, prop);
623 static int __gb_power_supply_property_get(struct gb_power_supply *gbpsy,
624 enum power_supply_property psp,
625 union power_supply_propval *val)
627 struct gb_power_supply_prop *prop;
629 prop = get_psy_prop(gbpsy, psp);
633 val->intval = prop->val;
637 static int __gb_power_supply_property_strval_get(struct gb_power_supply *gbpsy,
638 enum power_supply_property psp,
639 union power_supply_propval *val)
642 case POWER_SUPPLY_PROP_MODEL_NAME:
643 val->strval = gbpsy->model_name;
645 case POWER_SUPPLY_PROP_MANUFACTURER:
646 val->strval = gbpsy->manufacturer;
648 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
649 val->strval = gbpsy->serial_number;
658 static int _gb_power_supply_property_get(struct gb_power_supply *gbpsy,
659 enum power_supply_property psp,
660 union power_supply_propval *val)
662 struct gb_connection *connection = get_conn_from_psy(gbpsy);
666 * Properties of type const char *, were already fetched on
667 * get_description operation and should be cached in gb
669 if (is_prop_valint(psp))
670 ret = __gb_power_supply_property_get(gbpsy, psp, val);
672 ret = __gb_power_supply_property_strval_get(gbpsy, psp, val);
675 dev_err(&connection->bundle->dev, "get property %u\n", psp);
680 static int is_cache_valid(struct gb_power_supply *gbpsy)
682 /* check if cache is good enough or it has expired */
683 if (gbpsy->cache_invalid) {
684 gbpsy->cache_invalid = 0;
688 if (gbpsy->last_update &&
689 time_is_after_jiffies(gbpsy->last_update +
690 msecs_to_jiffies(cache_time)))
696 static int gb_power_supply_status_get(struct gb_power_supply *gbpsy)
698 struct gb_connection *connection = get_conn_from_psy(gbpsy);
702 if (is_cache_valid(gbpsy))
705 ret = gb_pm_runtime_get_sync(connection->bundle);
709 for (i = 0; i < gbpsy->properties_count; i++) {
710 ret = __gb_power_supply_property_update(gbpsy,
711 gbpsy->props[i].prop);
717 gbpsy->last_update = jiffies;
719 gb_pm_runtime_put_autosuspend(connection->bundle);
723 static void gb_power_supply_status_update(struct gb_power_supply *gbpsy)
725 /* check if there a change that need to be reported */
726 gb_power_supply_status_get(gbpsy);
731 gbpsy->update_interval = update_interval_init;
732 __gb_power_supply_changed(gbpsy);
733 gbpsy->changed = false;
736 static void gb_power_supply_work(struct work_struct *work)
738 struct gb_power_supply *gbpsy = container_of(work,
739 struct gb_power_supply,
743 * if the poll interval is not set, disable polling, this is helpful
744 * specially at unregister time.
746 if (!gbpsy->update_interval)
749 gb_power_supply_status_update(gbpsy);
750 next_interval(gbpsy);
751 schedule_delayed_work(&gbpsy->work, gbpsy->update_interval);
754 static int get_property(struct power_supply *b,
755 enum power_supply_property psp,
756 union power_supply_propval *val)
758 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
760 gb_power_supply_status_get(gbpsy);
762 return _gb_power_supply_property_get(gbpsy, psp, val);
765 static int gb_power_supply_property_set(struct gb_power_supply *gbpsy,
766 enum power_supply_property psp,
769 struct gb_connection *connection = get_conn_from_psy(gbpsy);
770 struct gb_power_supply_prop *prop;
771 struct gb_power_supply_set_property_request req;
774 ret = gb_pm_runtime_get_sync(connection->bundle);
778 prop = get_psy_prop(gbpsy, psp);
784 req.psy_id = gbpsy->id;
785 req.property = prop->gb_prop;
786 req.prop_val = cpu_to_le32((s32)val);
788 ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_SET_PROPERTY,
789 &req, sizeof(req), NULL, 0);
793 /* cache immediately the new value */
797 gb_pm_runtime_put_autosuspend(connection->bundle);
801 static int set_property(struct power_supply *b,
802 enum power_supply_property psp,
803 const union power_supply_propval *val)
805 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
807 return gb_power_supply_property_set(gbpsy, psp, val->intval);
810 static int property_is_writeable(struct power_supply *b,
811 enum power_supply_property psp)
813 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
815 return is_psy_prop_writeable(gbpsy, psp);
818 static int gb_power_supply_register(struct gb_power_supply *gbpsy)
820 struct gb_connection *connection = get_conn_from_psy(gbpsy);
821 struct power_supply_config cfg = {};
823 cfg.drv_data = gbpsy;
825 gbpsy->desc.name = gbpsy->name;
826 gbpsy->desc.type = gbpsy->type;
827 gbpsy->desc.properties = gbpsy->props_raw;
828 gbpsy->desc.num_properties = total_props(gbpsy);
829 gbpsy->desc.get_property = get_property;
830 gbpsy->desc.set_property = set_property;
831 gbpsy->desc.property_is_writeable = property_is_writeable;
833 gbpsy->psy = power_supply_register(&connection->bundle->dev,
835 return PTR_ERR_OR_ZERO(gbpsy->psy);
838 static void _gb_power_supply_free(struct gb_power_supply *gbpsy)
840 kfree(gbpsy->serial_number);
841 kfree(gbpsy->model_name);
842 kfree(gbpsy->manufacturer);
843 kfree(gbpsy->props_raw);
847 static void _gb_power_supply_release(struct gb_power_supply *gbpsy)
849 gbpsy->update_interval = 0;
851 cancel_delayed_work_sync(&gbpsy->work);
853 if (gbpsy->registered)
854 power_supply_unregister(gbpsy->psy);
856 _gb_power_supply_free(gbpsy);
859 static void _gb_power_supplies_release(struct gb_power_supplies *supplies)
863 if (!supplies->supply)
866 mutex_lock(&supplies->supplies_lock);
867 for (i = 0; i < supplies->supplies_count; i++)
868 _gb_power_supply_release(&supplies->supply[i]);
869 kfree(supplies->supply);
870 mutex_unlock(&supplies->supplies_lock);
874 static int gb_power_supplies_get_count(struct gb_power_supplies *supplies)
876 struct gb_power_supply_get_supplies_response resp;
879 ret = gb_operation_sync(supplies->connection,
880 GB_POWER_SUPPLY_TYPE_GET_SUPPLIES,
881 NULL, 0, &resp, sizeof(resp));
885 if (!resp.supplies_count)
888 supplies->supplies_count = resp.supplies_count;
893 static int gb_power_supply_config(struct gb_power_supplies *supplies, int id)
895 struct gb_power_supply *gbpsy = &supplies->supply[id];
898 gbpsy->supplies = supplies;
901 ret = gb_power_supply_description_get(gbpsy);
905 return gb_power_supply_prop_descriptors_get(gbpsy);
908 static int gb_power_supply_enable(struct gb_power_supply *gbpsy)
912 /* guarantee that we have an unique name, before register */
913 ret = __gb_power_supply_set_name(gbpsy->model_name, gbpsy->name,
914 sizeof(gbpsy->name));
918 mutex_init(&gbpsy->supply_lock);
920 ret = gb_power_supply_register(gbpsy);
924 gbpsy->update_interval = update_interval_init;
925 INIT_DELAYED_WORK(&gbpsy->work, gb_power_supply_work);
926 schedule_delayed_work(&gbpsy->work, 0);
928 /* everything went fine, mark it for release code to know */
929 gbpsy->registered = true;
934 static int gb_power_supplies_setup(struct gb_power_supplies *supplies)
936 struct gb_connection *connection = supplies->connection;
940 mutex_lock(&supplies->supplies_lock);
942 ret = gb_power_supplies_get_count(supplies);
946 supplies->supply = kcalloc(supplies->supplies_count,
947 sizeof(struct gb_power_supply),
950 if (!supplies->supply) {
955 for (i = 0; i < supplies->supplies_count; i++) {
956 ret = gb_power_supply_config(supplies, i);
958 dev_err(&connection->bundle->dev,
959 "Fail to configure supplies devices\n");
964 mutex_unlock(&supplies->supplies_lock);
968 static int gb_power_supplies_register(struct gb_power_supplies *supplies)
970 struct gb_connection *connection = supplies->connection;
974 mutex_lock(&supplies->supplies_lock);
976 for (i = 0; i < supplies->supplies_count; i++) {
977 ret = gb_power_supply_enable(&supplies->supply[i]);
979 dev_err(&connection->bundle->dev,
980 "Fail to enable supplies devices\n");
985 mutex_unlock(&supplies->supplies_lock);
989 static int gb_supplies_request_handler(struct gb_operation *op)
991 struct gb_connection *connection = op->connection;
992 struct gb_power_supplies *supplies = gb_connection_get_data(connection);
993 struct gb_power_supply *gbpsy;
994 struct gb_message *request;
995 struct gb_power_supply_event_request *payload;
1000 if (op->type != GB_POWER_SUPPLY_TYPE_EVENT) {
1001 dev_err(&connection->bundle->dev,
1002 "Unsupported unsolicited event: %u\n", op->type);
1006 request = op->request;
1008 if (request->payload_size < sizeof(*payload)) {
1009 dev_err(&connection->bundle->dev,
1010 "Wrong event size received (%zu < %zu)\n",
1011 request->payload_size, sizeof(*payload));
1015 payload = request->payload;
1016 psy_id = payload->psy_id;
1017 mutex_lock(&supplies->supplies_lock);
1018 if (psy_id >= supplies->supplies_count ||
1019 !supplies->supply[psy_id].registered) {
1020 dev_err(&connection->bundle->dev,
1021 "Event received for unconfigured power_supply id: %d\n",
1027 event = payload->event;
1029 * we will only handle events after setup is done and before release is
1030 * running. For that just check update_interval.
1032 gbpsy = &supplies->supply[psy_id];
1033 if (!gbpsy->update_interval) {
1038 if (event & GB_POWER_SUPPLY_UPDATE) {
1040 * we need to make sure we invalidate cache, if not no new
1041 * values for the properties will be fetch and the all propose
1042 * of this event is missed
1044 gbpsy->cache_invalid = 1;
1045 gb_power_supply_status_update(gbpsy);
1049 mutex_unlock(&supplies->supplies_lock);
1053 static int gb_power_supply_probe(struct gb_bundle *bundle,
1054 const struct greybus_bundle_id *id)
1056 struct greybus_descriptor_cport *cport_desc;
1057 struct gb_connection *connection;
1058 struct gb_power_supplies *supplies;
1061 if (bundle->num_cports != 1)
1064 cport_desc = &bundle->cport_desc[0];
1065 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_POWER_SUPPLY)
1068 supplies = kzalloc(sizeof(*supplies), GFP_KERNEL);
1072 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1073 gb_supplies_request_handler);
1074 if (IS_ERR(connection)) {
1075 ret = PTR_ERR(connection);
1079 supplies->connection = connection;
1080 gb_connection_set_data(connection, supplies);
1082 mutex_init(&supplies->supplies_lock);
1084 greybus_set_drvdata(bundle, supplies);
1086 /* We aren't ready to receive an incoming request yet */
1087 ret = gb_connection_enable_tx(connection);
1089 goto error_connection_destroy;
1091 ret = gb_power_supplies_setup(supplies);
1093 goto error_connection_disable;
1095 /* We are ready to receive an incoming request now, enable RX as well */
1096 ret = gb_connection_enable(connection);
1098 goto error_connection_disable;
1100 ret = gb_power_supplies_register(supplies);
1102 goto error_connection_disable;
1104 gb_pm_runtime_put_autosuspend(bundle);
1107 error_connection_disable:
1108 gb_connection_disable(connection);
1109 error_connection_destroy:
1110 gb_connection_destroy(connection);
1112 _gb_power_supplies_release(supplies);
1116 static void gb_power_supply_disconnect(struct gb_bundle *bundle)
1118 struct gb_power_supplies *supplies = greybus_get_drvdata(bundle);
1120 gb_connection_disable(supplies->connection);
1121 gb_connection_destroy(supplies->connection);
1123 _gb_power_supplies_release(supplies);
1126 static const struct greybus_bundle_id gb_power_supply_id_table[] = {
1127 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_POWER_SUPPLY) },
1130 MODULE_DEVICE_TABLE(greybus, gb_power_supply_id_table);
1132 static struct greybus_driver gb_power_supply_driver = {
1133 .name = "power_supply",
1134 .probe = gb_power_supply_probe,
1135 .disconnect = gb_power_supply_disconnect,
1136 .id_table = gb_power_supply_id_table,
1138 module_greybus_driver(gb_power_supply_driver);
1140 MODULE_LICENSE("GPL v2");