2 * Power Supply driver for a Greybus module.
4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/power_supply.h>
13 #include <linux/slab.h>
19 struct gb_power_supply_prop {
20 enum power_supply_property prop;
27 struct gb_power_supply {
30 struct power_supply *psy;
31 struct power_supply_desc desc;
33 struct gb_power_supplies *supplies;
34 struct delayed_work work;
40 u8 properties_count_str;
41 unsigned long last_update;
43 unsigned int update_interval;
45 struct gb_power_supply_prop *props;
46 enum power_supply_property *props_raw;
48 struct mutex supply_lock;
51 struct gb_power_supplies {
52 struct gb_connection *connection;
54 struct gb_power_supply *supply;
55 struct mutex supplies_lock;
58 #define to_gb_power_supply(x) power_supply_get_drvdata(x)
61 * General power supply properties that could be absent from various reasons,
62 * like kernel versions or vendor specific versions
64 #ifndef POWER_SUPPLY_PROP_VOLTAGE_BOOT
65 #define POWER_SUPPLY_PROP_VOLTAGE_BOOT -1
67 #ifndef POWER_SUPPLY_PROP_CURRENT_BOOT
68 #define POWER_SUPPLY_PROP_CURRENT_BOOT -1
70 #ifndef POWER_SUPPLY_PROP_CALIBRATE
71 #define POWER_SUPPLY_PROP_CALIBRATE -1
74 /* cache time in milliseconds, if cache_time is set to 0 cache is disable */
75 static unsigned int cache_time = 1000;
77 * update interval initial and maximum value, between the two will
78 * back-off exponential
80 static unsigned int update_interval_init = 1 * HZ;
81 static unsigned int update_interval_max = 30 * HZ;
83 struct gb_power_supply_changes {
84 enum power_supply_property prop;
86 void (*prop_changed)(struct gb_power_supply *gbpsy,
87 struct gb_power_supply_prop *prop);
90 static void gb_power_supply_state_change(struct gb_power_supply *gbpsy,
91 struct gb_power_supply_prop *prop);
93 static const struct gb_power_supply_changes psy_props_changes[] = {
94 { .prop = GB_POWER_SUPPLY_PROP_STATUS,
95 .tolerance_change = 0,
96 .prop_changed = gb_power_supply_state_change,
98 { .prop = GB_POWER_SUPPLY_PROP_TEMP,
99 .tolerance_change = 500,
100 .prop_changed = NULL,
102 { .prop = GB_POWER_SUPPLY_PROP_ONLINE,
103 .tolerance_change = 0,
104 .prop_changed = NULL,
108 static int get_psp_from_gb_prop(int gb_prop, enum power_supply_property *psp)
113 case GB_POWER_SUPPLY_PROP_STATUS:
114 prop = POWER_SUPPLY_PROP_STATUS;
116 case GB_POWER_SUPPLY_PROP_CHARGE_TYPE:
117 prop = POWER_SUPPLY_PROP_CHARGE_TYPE;
119 case GB_POWER_SUPPLY_PROP_HEALTH:
120 prop = POWER_SUPPLY_PROP_HEALTH;
122 case GB_POWER_SUPPLY_PROP_PRESENT:
123 prop = POWER_SUPPLY_PROP_PRESENT;
125 case GB_POWER_SUPPLY_PROP_ONLINE:
126 prop = POWER_SUPPLY_PROP_ONLINE;
128 case GB_POWER_SUPPLY_PROP_AUTHENTIC:
129 prop = POWER_SUPPLY_PROP_AUTHENTIC;
131 case GB_POWER_SUPPLY_PROP_TECHNOLOGY:
132 prop = POWER_SUPPLY_PROP_TECHNOLOGY;
134 case GB_POWER_SUPPLY_PROP_CYCLE_COUNT:
135 prop = POWER_SUPPLY_PROP_CYCLE_COUNT;
137 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX:
138 prop = POWER_SUPPLY_PROP_VOLTAGE_MAX;
140 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN:
141 prop = POWER_SUPPLY_PROP_VOLTAGE_MIN;
143 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
144 prop = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN;
146 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
147 prop = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN;
149 case GB_POWER_SUPPLY_PROP_VOLTAGE_NOW:
150 prop = POWER_SUPPLY_PROP_VOLTAGE_NOW;
152 case GB_POWER_SUPPLY_PROP_VOLTAGE_AVG:
153 prop = POWER_SUPPLY_PROP_VOLTAGE_AVG;
155 case GB_POWER_SUPPLY_PROP_VOLTAGE_OCV:
156 prop = POWER_SUPPLY_PROP_VOLTAGE_OCV;
158 case GB_POWER_SUPPLY_PROP_VOLTAGE_BOOT:
159 prop = POWER_SUPPLY_PROP_VOLTAGE_BOOT;
161 case GB_POWER_SUPPLY_PROP_CURRENT_MAX:
162 prop = POWER_SUPPLY_PROP_CURRENT_MAX;
164 case GB_POWER_SUPPLY_PROP_CURRENT_NOW:
165 prop = POWER_SUPPLY_PROP_CURRENT_NOW;
167 case GB_POWER_SUPPLY_PROP_CURRENT_AVG:
168 prop = POWER_SUPPLY_PROP_CURRENT_AVG;
170 case GB_POWER_SUPPLY_PROP_CURRENT_BOOT:
171 prop = POWER_SUPPLY_PROP_CURRENT_BOOT;
173 case GB_POWER_SUPPLY_PROP_POWER_NOW:
174 prop = POWER_SUPPLY_PROP_POWER_NOW;
176 case GB_POWER_SUPPLY_PROP_POWER_AVG:
177 prop = POWER_SUPPLY_PROP_POWER_AVG;
179 case GB_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
180 prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
182 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
183 prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
185 case GB_POWER_SUPPLY_PROP_CHARGE_FULL:
186 prop = POWER_SUPPLY_PROP_CHARGE_FULL;
188 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY:
189 prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
191 case GB_POWER_SUPPLY_PROP_CHARGE_NOW:
192 prop = POWER_SUPPLY_PROP_CHARGE_NOW;
194 case GB_POWER_SUPPLY_PROP_CHARGE_AVG:
195 prop = POWER_SUPPLY_PROP_CHARGE_AVG;
197 case GB_POWER_SUPPLY_PROP_CHARGE_COUNTER:
198 prop = POWER_SUPPLY_PROP_CHARGE_COUNTER;
200 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
201 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
203 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
204 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
206 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
207 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE;
209 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
210 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX;
212 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
213 prop = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT;
215 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
216 prop = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX;
218 case GB_POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
219 prop = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
221 case GB_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
222 prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
224 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
225 prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
227 case GB_POWER_SUPPLY_PROP_ENERGY_FULL:
228 prop = POWER_SUPPLY_PROP_ENERGY_FULL;
230 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY:
231 prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
233 case GB_POWER_SUPPLY_PROP_ENERGY_NOW:
234 prop = POWER_SUPPLY_PROP_ENERGY_NOW;
236 case GB_POWER_SUPPLY_PROP_ENERGY_AVG:
237 prop = POWER_SUPPLY_PROP_ENERGY_AVG;
239 case GB_POWER_SUPPLY_PROP_CAPACITY:
240 prop = POWER_SUPPLY_PROP_CAPACITY;
242 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
243 prop = POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN;
245 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX:
246 prop = POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX;
248 case GB_POWER_SUPPLY_PROP_CAPACITY_LEVEL:
249 prop = POWER_SUPPLY_PROP_CAPACITY_LEVEL;
251 case GB_POWER_SUPPLY_PROP_TEMP:
252 prop = POWER_SUPPLY_PROP_TEMP;
254 case GB_POWER_SUPPLY_PROP_TEMP_MAX:
255 prop = POWER_SUPPLY_PROP_TEMP_MAX;
257 case GB_POWER_SUPPLY_PROP_TEMP_MIN:
258 prop = POWER_SUPPLY_PROP_TEMP_MIN;
260 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
261 prop = POWER_SUPPLY_PROP_TEMP_ALERT_MIN;
263 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
264 prop = POWER_SUPPLY_PROP_TEMP_ALERT_MAX;
266 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT:
267 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT;
269 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
270 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN;
272 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
273 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX;
275 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
276 prop = POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW;
278 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
279 prop = POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG;
281 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
282 prop = POWER_SUPPLY_PROP_TIME_TO_FULL_NOW;
284 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
285 prop = POWER_SUPPLY_PROP_TIME_TO_FULL_AVG;
287 case GB_POWER_SUPPLY_PROP_TYPE:
288 prop = POWER_SUPPLY_PROP_TYPE;
290 case GB_POWER_SUPPLY_PROP_SCOPE:
291 prop = POWER_SUPPLY_PROP_SCOPE;
293 case GB_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
294 prop = POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT;
296 case GB_POWER_SUPPLY_PROP_CALIBRATE:
297 prop = POWER_SUPPLY_PROP_CALIBRATE;
307 *psp = (enum power_supply_property)prop;
312 static struct gb_connection *get_conn_from_psy(struct gb_power_supply *gbpsy)
314 return gbpsy->supplies->connection;
317 static struct gb_power_supply_prop *get_psy_prop(struct gb_power_supply *gbpsy,
318 enum power_supply_property psp)
322 for (i = 0; i < gbpsy->properties_count; i++)
323 if (gbpsy->props[i].prop == psp)
324 return &gbpsy->props[i];
328 static int is_psy_prop_writeable(struct gb_power_supply *gbpsy,
329 enum power_supply_property psp)
331 struct gb_power_supply_prop *prop;
333 prop = get_psy_prop(gbpsy, psp);
336 return prop->is_writeable ? 1 : 0;
339 static int is_prop_valint(enum power_supply_property psp)
341 return ((psp < POWER_SUPPLY_PROP_MODEL_NAME) ? 1 : 0);
344 static void next_interval(struct gb_power_supply *gbpsy)
346 if (gbpsy->update_interval == update_interval_max)
349 /* do some exponential back-off in the update interval */
350 gbpsy->update_interval *= 2;
351 if (gbpsy->update_interval > update_interval_max)
352 gbpsy->update_interval = update_interval_max;
355 static void __gb_power_supply_changed(struct gb_power_supply *gbpsy)
357 power_supply_changed(gbpsy->psy);
360 static void gb_power_supply_state_change(struct gb_power_supply *gbpsy,
361 struct gb_power_supply_prop *prop)
363 struct gb_connection *connection = get_conn_from_psy(gbpsy);
367 * Check gbpsy->pm_acquired to make sure only one pair of 'get_sync'
368 * and 'put_autosuspend' runtime pm call for state property change.
370 mutex_lock(&gbpsy->supply_lock);
372 if ((prop->val == GB_POWER_SUPPLY_STATUS_CHARGING) &&
373 !gbpsy->pm_acquired) {
374 ret = gb_pm_runtime_get_sync(connection->bundle);
376 dev_err(&connection->bundle->dev,
377 "Fail to set wake lock for charging state\n");
379 gbpsy->pm_acquired = true;
381 if (gbpsy->pm_acquired) {
382 ret = gb_pm_runtime_put_autosuspend(connection->bundle);
384 dev_err(&connection->bundle->dev,
385 "Fail to set wake unlock for none charging\n");
387 gbpsy->pm_acquired = false;
391 mutex_unlock(&gbpsy->supply_lock);
394 static void check_changed(struct gb_power_supply *gbpsy,
395 struct gb_power_supply_prop *prop)
397 const struct gb_power_supply_changes *psyc;
399 int prev_val = prop->previous_val;
400 bool changed = false;
403 for (i = 0; i < ARRAY_SIZE(psy_props_changes); i++) {
404 psyc = &psy_props_changes[i];
405 if (prop->prop == psyc->prop) {
406 if (!psyc->tolerance_change)
408 else if (val < prev_val &&
409 prev_val - val > psyc->tolerance_change)
411 else if (val > prev_val &&
412 val - prev_val > psyc->tolerance_change)
415 if (changed && psyc->prop_changed)
416 psyc->prop_changed(gbpsy, prop);
419 gbpsy->changed = true;
425 static int total_props(struct gb_power_supply *gbpsy)
427 /* this return the intval plus the strval properties */
428 return (gbpsy->properties_count + gbpsy->properties_count_str);
431 static void prop_append(struct gb_power_supply *gbpsy,
432 enum power_supply_property prop)
434 enum power_supply_property *new_props_raw;
436 gbpsy->properties_count_str++;
437 new_props_raw = krealloc(gbpsy->props_raw, total_props(gbpsy) *
438 sizeof(enum power_supply_property),
442 gbpsy->props_raw = new_props_raw;
443 gbpsy->props_raw[total_props(gbpsy) - 1] = prop;
446 static int __gb_power_supply_set_name(char *init_name, char *name, size_t len)
450 struct power_supply *psy;
452 if (!strlen(init_name))
453 init_name = "gb_power_supply";
454 strlcpy(name, init_name, len);
456 while ((ret < len) && (psy = power_supply_get_by_name(name))) {
457 power_supply_put(psy);
459 ret = snprintf(name, len, "%s_%u", init_name, ++i);
466 static void _gb_power_supply_append_props(struct gb_power_supply *gbpsy)
468 if (strlen(gbpsy->manufacturer))
469 prop_append(gbpsy, POWER_SUPPLY_PROP_MANUFACTURER);
470 if (strlen(gbpsy->model_name))
471 prop_append(gbpsy, POWER_SUPPLY_PROP_MODEL_NAME);
472 if (strlen(gbpsy->serial_number))
473 prop_append(gbpsy, POWER_SUPPLY_PROP_SERIAL_NUMBER);
476 static int gb_power_supply_description_get(struct gb_power_supply *gbpsy)
478 struct gb_connection *connection = get_conn_from_psy(gbpsy);
479 struct gb_power_supply_get_description_request req;
480 struct gb_power_supply_get_description_response resp;
483 req.psy_id = gbpsy->id;
485 ret = gb_operation_sync(connection,
486 GB_POWER_SUPPLY_TYPE_GET_DESCRIPTION,
487 &req, sizeof(req), &resp, sizeof(resp));
491 gbpsy->manufacturer = kstrndup(resp.manufacturer, PROP_MAX, GFP_KERNEL);
492 if (!gbpsy->manufacturer)
494 gbpsy->model_name = kstrndup(resp.model, PROP_MAX, GFP_KERNEL);
495 if (!gbpsy->model_name)
497 gbpsy->serial_number = kstrndup(resp.serial_number, PROP_MAX,
499 if (!gbpsy->serial_number)
502 gbpsy->type = le16_to_cpu(resp.type);
503 gbpsy->properties_count = resp.properties_count;
508 static int gb_power_supply_prop_descriptors_get(struct gb_power_supply *gbpsy)
510 struct gb_connection *connection = get_conn_from_psy(gbpsy);
511 struct gb_power_supply_get_property_descriptors_request *req;
512 struct gb_power_supply_get_property_descriptors_response *resp;
513 struct gb_operation *op;
514 u8 props_count = gbpsy->properties_count;
515 enum power_supply_property psp;
519 if (props_count == 0)
522 op = gb_operation_create(connection,
523 GB_POWER_SUPPLY_TYPE_GET_PROP_DESCRIPTORS,
524 sizeof(req), sizeof(*resp) + props_count *
525 sizeof(struct gb_power_supply_props_desc),
530 req = op->request->payload;
531 req->psy_id = gbpsy->id;
533 ret = gb_operation_request_send_sync(op);
535 goto out_put_operation;
537 resp = op->response->payload;
539 /* validate received properties */
540 for (i = 0; i < props_count; i++) {
541 ret = get_psp_from_gb_prop(resp->props[i].property, &psp);
543 dev_warn(&connection->bundle->dev,
544 "greybus property %u it is not supported by this kernel, dropped\n",
545 resp->props[i].property);
546 gbpsy->properties_count--;
550 gbpsy->props = kcalloc(gbpsy->properties_count, sizeof(*gbpsy->props),
554 goto out_put_operation;
557 gbpsy->props_raw = kcalloc(gbpsy->properties_count,
558 sizeof(*gbpsy->props_raw), GFP_KERNEL);
559 if (!gbpsy->props_raw) {
561 goto out_put_operation;
564 /* Store available properties, skip the ones we do not support */
565 for (i = 0; i < props_count; i++) {
566 ret = get_psp_from_gb_prop(resp->props[i].property, &psp);
571 gbpsy->props[i - r].prop = psp;
572 gbpsy->props[i - r].gb_prop = resp->props[i].property;
573 gbpsy->props_raw[i - r] = psp;
574 if (resp->props[i].is_writeable)
575 gbpsy->props[i - r].is_writeable = true;
579 * now append the properties that we already got information in the
580 * get_description operation. (char * ones)
582 _gb_power_supply_append_props(gbpsy);
586 gb_operation_put(op);
591 static int __gb_power_supply_property_update(struct gb_power_supply *gbpsy,
592 enum power_supply_property psp)
594 struct gb_connection *connection = get_conn_from_psy(gbpsy);
595 struct gb_power_supply_prop *prop;
596 struct gb_power_supply_get_property_request req;
597 struct gb_power_supply_get_property_response resp;
601 prop = get_psy_prop(gbpsy, psp);
604 req.psy_id = gbpsy->id;
605 req.property = prop->gb_prop;
607 ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_GET_PROPERTY,
608 &req, sizeof(req), &resp, sizeof(resp));
612 val = le32_to_cpu(resp.prop_val);
613 if (val == prop->val)
616 prop->previous_val = prop->val;
619 check_changed(gbpsy, prop);
624 static int __gb_power_supply_property_get(struct gb_power_supply *gbpsy,
625 enum power_supply_property psp,
626 union power_supply_propval *val)
628 struct gb_power_supply_prop *prop;
630 prop = get_psy_prop(gbpsy, psp);
634 val->intval = prop->val;
638 static int __gb_power_supply_property_strval_get(struct gb_power_supply *gbpsy,
639 enum power_supply_property psp,
640 union power_supply_propval *val)
643 case POWER_SUPPLY_PROP_MODEL_NAME:
644 val->strval = gbpsy->model_name;
646 case POWER_SUPPLY_PROP_MANUFACTURER:
647 val->strval = gbpsy->manufacturer;
649 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
650 val->strval = gbpsy->serial_number;
659 static int _gb_power_supply_property_get(struct gb_power_supply *gbpsy,
660 enum power_supply_property psp,
661 union power_supply_propval *val)
663 struct gb_connection *connection = get_conn_from_psy(gbpsy);
667 * Properties of type const char *, were already fetched on
668 * get_description operation and should be cached in gb
670 if (is_prop_valint(psp))
671 ret = __gb_power_supply_property_get(gbpsy, psp, val);
673 ret = __gb_power_supply_property_strval_get(gbpsy, psp, val);
676 dev_err(&connection->bundle->dev, "get property %u\n", psp);
681 static int is_cache_valid(struct gb_power_supply *gbpsy)
683 /* check if cache is good enough or it has expired */
684 if (gbpsy->cache_invalid) {
685 gbpsy->cache_invalid = 0;
689 if (gbpsy->last_update &&
690 time_is_after_jiffies(gbpsy->last_update +
691 msecs_to_jiffies(cache_time)))
697 static int gb_power_supply_status_get(struct gb_power_supply *gbpsy)
699 struct gb_connection *connection = get_conn_from_psy(gbpsy);
703 if (is_cache_valid(gbpsy))
706 ret = gb_pm_runtime_get_sync(connection->bundle);
710 for (i = 0; i < gbpsy->properties_count; i++) {
711 ret = __gb_power_supply_property_update(gbpsy,
712 gbpsy->props[i].prop);
718 gbpsy->last_update = jiffies;
720 gb_pm_runtime_put_autosuspend(connection->bundle);
724 static void gb_power_supply_status_update(struct gb_power_supply *gbpsy)
726 /* check if there a change that need to be reported */
727 gb_power_supply_status_get(gbpsy);
732 gbpsy->update_interval = update_interval_init;
733 __gb_power_supply_changed(gbpsy);
734 gbpsy->changed = false;
737 static void gb_power_supply_work(struct work_struct *work)
739 struct gb_power_supply *gbpsy = container_of(work,
740 struct gb_power_supply,
744 * if the poll interval is not set, disable polling, this is helpful
745 * specially at unregister time.
747 if (!gbpsy->update_interval)
750 gb_power_supply_status_update(gbpsy);
751 next_interval(gbpsy);
752 schedule_delayed_work(&gbpsy->work, gbpsy->update_interval);
755 static int get_property(struct power_supply *b,
756 enum power_supply_property psp,
757 union power_supply_propval *val)
759 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
761 gb_power_supply_status_get(gbpsy);
763 return _gb_power_supply_property_get(gbpsy, psp, val);
766 static int gb_power_supply_property_set(struct gb_power_supply *gbpsy,
767 enum power_supply_property psp,
770 struct gb_connection *connection = get_conn_from_psy(gbpsy);
771 struct gb_power_supply_prop *prop;
772 struct gb_power_supply_set_property_request req;
775 ret = gb_pm_runtime_get_sync(connection->bundle);
779 prop = get_psy_prop(gbpsy, psp);
785 req.psy_id = gbpsy->id;
786 req.property = prop->gb_prop;
787 req.prop_val = cpu_to_le32((s32)val);
789 ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_SET_PROPERTY,
790 &req, sizeof(req), NULL, 0);
794 /* cache immediately the new value */
798 gb_pm_runtime_put_autosuspend(connection->bundle);
802 static int set_property(struct power_supply *b,
803 enum power_supply_property psp,
804 const union power_supply_propval *val)
806 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
808 return gb_power_supply_property_set(gbpsy, psp, val->intval);
811 static int property_is_writeable(struct power_supply *b,
812 enum power_supply_property psp)
814 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
816 return is_psy_prop_writeable(gbpsy, psp);
819 static int gb_power_supply_register(struct gb_power_supply *gbpsy)
821 struct gb_connection *connection = get_conn_from_psy(gbpsy);
822 struct power_supply_config cfg = {};
824 cfg.drv_data = gbpsy;
826 gbpsy->desc.name = gbpsy->name;
827 gbpsy->desc.type = gbpsy->type;
828 gbpsy->desc.properties = gbpsy->props_raw;
829 gbpsy->desc.num_properties = total_props(gbpsy);
830 gbpsy->desc.get_property = get_property;
831 gbpsy->desc.set_property = set_property;
832 gbpsy->desc.property_is_writeable = property_is_writeable;
834 gbpsy->psy = power_supply_register(&connection->bundle->dev,
836 return PTR_ERR_OR_ZERO(gbpsy->psy);
839 static void _gb_power_supply_free(struct gb_power_supply *gbpsy)
841 kfree(gbpsy->serial_number);
842 kfree(gbpsy->model_name);
843 kfree(gbpsy->manufacturer);
844 kfree(gbpsy->props_raw);
848 static void _gb_power_supply_release(struct gb_power_supply *gbpsy)
850 gbpsy->update_interval = 0;
852 cancel_delayed_work_sync(&gbpsy->work);
854 if (gbpsy->registered)
855 power_supply_unregister(gbpsy->psy);
857 _gb_power_supply_free(gbpsy);
860 static void _gb_power_supplies_release(struct gb_power_supplies *supplies)
864 if (!supplies->supply)
867 mutex_lock(&supplies->supplies_lock);
868 for (i = 0; i < supplies->supplies_count; i++)
869 _gb_power_supply_release(&supplies->supply[i]);
870 kfree(supplies->supply);
871 mutex_unlock(&supplies->supplies_lock);
875 static int gb_power_supplies_get_count(struct gb_power_supplies *supplies)
877 struct gb_power_supply_get_supplies_response resp;
880 ret = gb_operation_sync(supplies->connection,
881 GB_POWER_SUPPLY_TYPE_GET_SUPPLIES,
882 NULL, 0, &resp, sizeof(resp));
886 if (!resp.supplies_count)
889 supplies->supplies_count = resp.supplies_count;
894 static int gb_power_supply_config(struct gb_power_supplies *supplies, int id)
896 struct gb_power_supply *gbpsy = &supplies->supply[id];
899 gbpsy->supplies = supplies;
902 ret = gb_power_supply_description_get(gbpsy);
906 return gb_power_supply_prop_descriptors_get(gbpsy);
909 static int gb_power_supply_enable(struct gb_power_supply *gbpsy)
913 /* guarantee that we have an unique name, before register */
914 ret = __gb_power_supply_set_name(gbpsy->model_name, gbpsy->name,
915 sizeof(gbpsy->name));
919 mutex_init(&gbpsy->supply_lock);
921 ret = gb_power_supply_register(gbpsy);
925 gbpsy->update_interval = update_interval_init;
926 INIT_DELAYED_WORK(&gbpsy->work, gb_power_supply_work);
927 schedule_delayed_work(&gbpsy->work, 0);
929 /* everything went fine, mark it for release code to know */
930 gbpsy->registered = true;
935 static int gb_power_supplies_setup(struct gb_power_supplies *supplies)
937 struct gb_connection *connection = supplies->connection;
941 mutex_lock(&supplies->supplies_lock);
943 ret = gb_power_supplies_get_count(supplies);
947 supplies->supply = kcalloc(supplies->supplies_count,
948 sizeof(struct gb_power_supply),
951 if (!supplies->supply) {
956 for (i = 0; i < supplies->supplies_count; i++) {
957 ret = gb_power_supply_config(supplies, i);
959 dev_err(&connection->bundle->dev,
960 "Fail to configure supplies devices\n");
965 mutex_unlock(&supplies->supplies_lock);
969 static int gb_power_supplies_register(struct gb_power_supplies *supplies)
971 struct gb_connection *connection = supplies->connection;
975 mutex_lock(&supplies->supplies_lock);
977 for (i = 0; i < supplies->supplies_count; i++) {
978 ret = gb_power_supply_enable(&supplies->supply[i]);
980 dev_err(&connection->bundle->dev,
981 "Fail to enable supplies devices\n");
986 mutex_unlock(&supplies->supplies_lock);
990 static int gb_supplies_request_handler(struct gb_operation *op)
992 struct gb_connection *connection = op->connection;
993 struct gb_power_supplies *supplies = gb_connection_get_data(connection);
994 struct gb_power_supply *gbpsy;
995 struct gb_message *request;
996 struct gb_power_supply_event_request *payload;
1001 if (op->type != GB_POWER_SUPPLY_TYPE_EVENT) {
1002 dev_err(&connection->bundle->dev,
1003 "Unsupported unsolicited event: %u\n", op->type);
1007 request = op->request;
1009 if (request->payload_size < sizeof(*payload)) {
1010 dev_err(&connection->bundle->dev,
1011 "Wrong event size received (%zu < %zu)\n",
1012 request->payload_size, sizeof(*payload));
1016 payload = request->payload;
1017 psy_id = payload->psy_id;
1018 mutex_lock(&supplies->supplies_lock);
1019 if (psy_id >= supplies->supplies_count ||
1020 !supplies->supply[psy_id].registered) {
1021 dev_err(&connection->bundle->dev,
1022 "Event received for unconfigured power_supply id: %d\n",
1028 event = payload->event;
1030 * we will only handle events after setup is done and before release is
1031 * running. For that just check update_interval.
1033 gbpsy = &supplies->supply[psy_id];
1034 if (!gbpsy->update_interval) {
1039 if (event & GB_POWER_SUPPLY_UPDATE) {
1041 * we need to make sure we invalidate cache, if not no new
1042 * values for the properties will be fetch and the all propose
1043 * of this event is missed
1045 gbpsy->cache_invalid = 1;
1046 gb_power_supply_status_update(gbpsy);
1050 mutex_unlock(&supplies->supplies_lock);
1054 static int gb_power_supply_probe(struct gb_bundle *bundle,
1055 const struct greybus_bundle_id *id)
1057 struct greybus_descriptor_cport *cport_desc;
1058 struct gb_connection *connection;
1059 struct gb_power_supplies *supplies;
1062 if (bundle->num_cports != 1)
1065 cport_desc = &bundle->cport_desc[0];
1066 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_POWER_SUPPLY)
1069 supplies = kzalloc(sizeof(*supplies), GFP_KERNEL);
1073 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1074 gb_supplies_request_handler);
1075 if (IS_ERR(connection)) {
1076 ret = PTR_ERR(connection);
1080 supplies->connection = connection;
1081 gb_connection_set_data(connection, supplies);
1083 mutex_init(&supplies->supplies_lock);
1085 greybus_set_drvdata(bundle, supplies);
1087 /* We aren't ready to receive an incoming request yet */
1088 ret = gb_connection_enable_tx(connection);
1090 goto error_connection_destroy;
1092 ret = gb_power_supplies_setup(supplies);
1094 goto error_connection_disable;
1096 /* We are ready to receive an incoming request now, enable RX as well */
1097 ret = gb_connection_enable(connection);
1099 goto error_connection_disable;
1101 ret = gb_power_supplies_register(supplies);
1103 goto error_connection_disable;
1105 gb_pm_runtime_put_autosuspend(bundle);
1108 error_connection_disable:
1109 gb_connection_disable(connection);
1110 error_connection_destroy:
1111 gb_connection_destroy(connection);
1113 _gb_power_supplies_release(supplies);
1117 static void gb_power_supply_disconnect(struct gb_bundle *bundle)
1119 struct gb_power_supplies *supplies = greybus_get_drvdata(bundle);
1121 gb_connection_disable(supplies->connection);
1122 gb_connection_destroy(supplies->connection);
1124 _gb_power_supplies_release(supplies);
1127 static const struct greybus_bundle_id gb_power_supply_id_table[] = {
1128 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_POWER_SUPPLY) },
1131 MODULE_DEVICE_TABLE(greybus, gb_power_supply_id_table);
1133 static struct greybus_driver gb_power_supply_driver = {
1134 .name = "power_supply",
1135 .probe = gb_power_supply_probe,
1136 .disconnect = gb_power_supply_disconnect,
1137 .id_table = gb_power_supply_id_table,
1139 module_greybus_driver(gb_power_supply_driver);
1141 MODULE_LICENSE("GPL v2");