]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/greybus/power_supply.c
bc16307c73b803e08d19e6bfdbe310d67ce730d2
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / greybus / power_supply.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Power Supply driver for a Greybus module.
4 *
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
7 *
8 * Released under the GPLv2 only.
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/power_supply.h>
14 #include <linux/slab.h>
15
16 #include "greybus.h"
17
18 #define PROP_MAX 32
19
20 struct gb_power_supply_prop {
21 enum power_supply_property prop;
22 u8 gb_prop;
23 int val;
24 int previous_val;
25 bool is_writeable;
26 };
27
28 struct gb_power_supply {
29 u8 id;
30 bool registered;
31 struct power_supply *psy;
32 struct power_supply_desc desc;
33 char name[64];
34 struct gb_power_supplies *supplies;
35 struct delayed_work work;
36 char *manufacturer;
37 char *model_name;
38 char *serial_number;
39 u8 type;
40 u8 properties_count;
41 u8 properties_count_str;
42 unsigned long last_update;
43 u8 cache_invalid;
44 unsigned int update_interval;
45 bool changed;
46 struct gb_power_supply_prop *props;
47 enum power_supply_property *props_raw;
48 bool pm_acquired;
49 struct mutex supply_lock;
50 };
51
52 struct gb_power_supplies {
53 struct gb_connection *connection;
54 u8 supplies_count;
55 struct gb_power_supply *supply;
56 struct mutex supplies_lock;
57 };
58
59 #define to_gb_power_supply(x) power_supply_get_drvdata(x)
60
61 /*
62 * General power supply properties that could be absent from various reasons,
63 * like kernel versions or vendor specific versions
64 */
65 #ifndef POWER_SUPPLY_PROP_VOLTAGE_BOOT
66 #define POWER_SUPPLY_PROP_VOLTAGE_BOOT -1
67 #endif
68 #ifndef POWER_SUPPLY_PROP_CURRENT_BOOT
69 #define POWER_SUPPLY_PROP_CURRENT_BOOT -1
70 #endif
71 #ifndef POWER_SUPPLY_PROP_CALIBRATE
72 #define POWER_SUPPLY_PROP_CALIBRATE -1
73 #endif
74
75 /* cache time in milliseconds, if cache_time is set to 0 cache is disable */
76 static unsigned int cache_time = 1000;
77 /*
78 * update interval initial and maximum value, between the two will
79 * back-off exponential
80 */
81 static unsigned int update_interval_init = 1 * HZ;
82 static unsigned int update_interval_max = 30 * HZ;
83
84 struct gb_power_supply_changes {
85 enum power_supply_property prop;
86 u32 tolerance_change;
87 void (*prop_changed)(struct gb_power_supply *gbpsy,
88 struct gb_power_supply_prop *prop);
89 };
90
91 static void gb_power_supply_state_change(struct gb_power_supply *gbpsy,
92 struct gb_power_supply_prop *prop);
93
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,
98 },
99 { .prop = GB_POWER_SUPPLY_PROP_TEMP,
100 .tolerance_change = 500,
101 .prop_changed = NULL,
102 },
103 { .prop = GB_POWER_SUPPLY_PROP_ONLINE,
104 .tolerance_change = 0,
105 .prop_changed = NULL,
106 },
107 };
108
109 static int get_psp_from_gb_prop(int gb_prop, enum power_supply_property *psp)
110 {
111 int prop;
112
113 switch (gb_prop) {
114 case GB_POWER_SUPPLY_PROP_STATUS:
115 prop = POWER_SUPPLY_PROP_STATUS;
116 break;
117 case GB_POWER_SUPPLY_PROP_CHARGE_TYPE:
118 prop = POWER_SUPPLY_PROP_CHARGE_TYPE;
119 break;
120 case GB_POWER_SUPPLY_PROP_HEALTH:
121 prop = POWER_SUPPLY_PROP_HEALTH;
122 break;
123 case GB_POWER_SUPPLY_PROP_PRESENT:
124 prop = POWER_SUPPLY_PROP_PRESENT;
125 break;
126 case GB_POWER_SUPPLY_PROP_ONLINE:
127 prop = POWER_SUPPLY_PROP_ONLINE;
128 break;
129 case GB_POWER_SUPPLY_PROP_AUTHENTIC:
130 prop = POWER_SUPPLY_PROP_AUTHENTIC;
131 break;
132 case GB_POWER_SUPPLY_PROP_TECHNOLOGY:
133 prop = POWER_SUPPLY_PROP_TECHNOLOGY;
134 break;
135 case GB_POWER_SUPPLY_PROP_CYCLE_COUNT:
136 prop = POWER_SUPPLY_PROP_CYCLE_COUNT;
137 break;
138 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX:
139 prop = POWER_SUPPLY_PROP_VOLTAGE_MAX;
140 break;
141 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN:
142 prop = POWER_SUPPLY_PROP_VOLTAGE_MIN;
143 break;
144 case GB_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
145 prop = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN;
146 break;
147 case GB_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
148 prop = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN;
149 break;
150 case GB_POWER_SUPPLY_PROP_VOLTAGE_NOW:
151 prop = POWER_SUPPLY_PROP_VOLTAGE_NOW;
152 break;
153 case GB_POWER_SUPPLY_PROP_VOLTAGE_AVG:
154 prop = POWER_SUPPLY_PROP_VOLTAGE_AVG;
155 break;
156 case GB_POWER_SUPPLY_PROP_VOLTAGE_OCV:
157 prop = POWER_SUPPLY_PROP_VOLTAGE_OCV;
158 break;
159 case GB_POWER_SUPPLY_PROP_VOLTAGE_BOOT:
160 prop = POWER_SUPPLY_PROP_VOLTAGE_BOOT;
161 break;
162 case GB_POWER_SUPPLY_PROP_CURRENT_MAX:
163 prop = POWER_SUPPLY_PROP_CURRENT_MAX;
164 break;
165 case GB_POWER_SUPPLY_PROP_CURRENT_NOW:
166 prop = POWER_SUPPLY_PROP_CURRENT_NOW;
167 break;
168 case GB_POWER_SUPPLY_PROP_CURRENT_AVG:
169 prop = POWER_SUPPLY_PROP_CURRENT_AVG;
170 break;
171 case GB_POWER_SUPPLY_PROP_CURRENT_BOOT:
172 prop = POWER_SUPPLY_PROP_CURRENT_BOOT;
173 break;
174 case GB_POWER_SUPPLY_PROP_POWER_NOW:
175 prop = POWER_SUPPLY_PROP_POWER_NOW;
176 break;
177 case GB_POWER_SUPPLY_PROP_POWER_AVG:
178 prop = POWER_SUPPLY_PROP_POWER_AVG;
179 break;
180 case GB_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
181 prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
182 break;
183 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
184 prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
185 break;
186 case GB_POWER_SUPPLY_PROP_CHARGE_FULL:
187 prop = POWER_SUPPLY_PROP_CHARGE_FULL;
188 break;
189 case GB_POWER_SUPPLY_PROP_CHARGE_EMPTY:
190 prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
191 break;
192 case GB_POWER_SUPPLY_PROP_CHARGE_NOW:
193 prop = POWER_SUPPLY_PROP_CHARGE_NOW;
194 break;
195 case GB_POWER_SUPPLY_PROP_CHARGE_AVG:
196 prop = POWER_SUPPLY_PROP_CHARGE_AVG;
197 break;
198 case GB_POWER_SUPPLY_PROP_CHARGE_COUNTER:
199 prop = POWER_SUPPLY_PROP_CHARGE_COUNTER;
200 break;
201 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
202 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
203 break;
204 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
205 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
206 break;
207 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
208 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE;
209 break;
210 case GB_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
211 prop = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX;
212 break;
213 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
214 prop = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT;
215 break;
216 case GB_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
217 prop = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX;
218 break;
219 case GB_POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
220 prop = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
221 break;
222 case GB_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
223 prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
224 break;
225 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
226 prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
227 break;
228 case GB_POWER_SUPPLY_PROP_ENERGY_FULL:
229 prop = POWER_SUPPLY_PROP_ENERGY_FULL;
230 break;
231 case GB_POWER_SUPPLY_PROP_ENERGY_EMPTY:
232 prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
233 break;
234 case GB_POWER_SUPPLY_PROP_ENERGY_NOW:
235 prop = POWER_SUPPLY_PROP_ENERGY_NOW;
236 break;
237 case GB_POWER_SUPPLY_PROP_ENERGY_AVG:
238 prop = POWER_SUPPLY_PROP_ENERGY_AVG;
239 break;
240 case GB_POWER_SUPPLY_PROP_CAPACITY:
241 prop = POWER_SUPPLY_PROP_CAPACITY;
242 break;
243 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
244 prop = POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN;
245 break;
246 case GB_POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX:
247 prop = POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX;
248 break;
249 case GB_POWER_SUPPLY_PROP_CAPACITY_LEVEL:
250 prop = POWER_SUPPLY_PROP_CAPACITY_LEVEL;
251 break;
252 case GB_POWER_SUPPLY_PROP_TEMP:
253 prop = POWER_SUPPLY_PROP_TEMP;
254 break;
255 case GB_POWER_SUPPLY_PROP_TEMP_MAX:
256 prop = POWER_SUPPLY_PROP_TEMP_MAX;
257 break;
258 case GB_POWER_SUPPLY_PROP_TEMP_MIN:
259 prop = POWER_SUPPLY_PROP_TEMP_MIN;
260 break;
261 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
262 prop = POWER_SUPPLY_PROP_TEMP_ALERT_MIN;
263 break;
264 case GB_POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
265 prop = POWER_SUPPLY_PROP_TEMP_ALERT_MAX;
266 break;
267 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT:
268 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT;
269 break;
270 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
271 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN;
272 break;
273 case GB_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
274 prop = POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX;
275 break;
276 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
277 prop = POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW;
278 break;
279 case GB_POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
280 prop = POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG;
281 break;
282 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
283 prop = POWER_SUPPLY_PROP_TIME_TO_FULL_NOW;
284 break;
285 case GB_POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
286 prop = POWER_SUPPLY_PROP_TIME_TO_FULL_AVG;
287 break;
288 case GB_POWER_SUPPLY_PROP_TYPE:
289 prop = POWER_SUPPLY_PROP_TYPE;
290 break;
291 case GB_POWER_SUPPLY_PROP_SCOPE:
292 prop = POWER_SUPPLY_PROP_SCOPE;
293 break;
294 case GB_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
295 prop = POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT;
296 break;
297 case GB_POWER_SUPPLY_PROP_CALIBRATE:
298 prop = POWER_SUPPLY_PROP_CALIBRATE;
299 break;
300 default:
301 prop = -1;
302 break;
303 }
304
305 if (prop < 0)
306 return prop;
307
308 *psp = (enum power_supply_property)prop;
309
310 return 0;
311 }
312
313 static struct gb_connection *get_conn_from_psy(struct gb_power_supply *gbpsy)
314 {
315 return gbpsy->supplies->connection;
316 }
317
318 static struct gb_power_supply_prop *get_psy_prop(struct gb_power_supply *gbpsy,
319 enum power_supply_property psp)
320 {
321 int i;
322
323 for (i = 0; i < gbpsy->properties_count; i++)
324 if (gbpsy->props[i].prop == psp)
325 return &gbpsy->props[i];
326 return NULL;
327 }
328
329 static int is_psy_prop_writeable(struct gb_power_supply *gbpsy,
330 enum power_supply_property psp)
331 {
332 struct gb_power_supply_prop *prop;
333
334 prop = get_psy_prop(gbpsy, psp);
335 if (!prop)
336 return -ENOENT;
337 return prop->is_writeable ? 1 : 0;
338 }
339
340 static int is_prop_valint(enum power_supply_property psp)
341 {
342 return ((psp < POWER_SUPPLY_PROP_MODEL_NAME) ? 1 : 0);
343 }
344
345 static void next_interval(struct gb_power_supply *gbpsy)
346 {
347 if (gbpsy->update_interval == update_interval_max)
348 return;
349
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;
354 }
355
356 static void __gb_power_supply_changed(struct gb_power_supply *gbpsy)
357 {
358 power_supply_changed(gbpsy->psy);
359 }
360
361 static void gb_power_supply_state_change(struct gb_power_supply *gbpsy,
362 struct gb_power_supply_prop *prop)
363 {
364 struct gb_connection *connection = get_conn_from_psy(gbpsy);
365 int ret;
366
367 /*
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.
370 */
371 mutex_lock(&gbpsy->supply_lock);
372
373 if ((prop->val == GB_POWER_SUPPLY_STATUS_CHARGING) &&
374 !gbpsy->pm_acquired) {
375 ret = gb_pm_runtime_get_sync(connection->bundle);
376 if (ret)
377 dev_err(&connection->bundle->dev,
378 "Fail to set wake lock for charging state\n");
379 else
380 gbpsy->pm_acquired = true;
381 } else {
382 if (gbpsy->pm_acquired) {
383 ret = gb_pm_runtime_put_autosuspend(connection->bundle);
384 if (ret)
385 dev_err(&connection->bundle->dev,
386 "Fail to set wake unlock for none charging\n");
387 else
388 gbpsy->pm_acquired = false;
389 }
390 }
391
392 mutex_unlock(&gbpsy->supply_lock);
393 }
394
395 static void check_changed(struct gb_power_supply *gbpsy,
396 struct gb_power_supply_prop *prop)
397 {
398 const struct gb_power_supply_changes *psyc;
399 int val = prop->val;
400 int prev_val = prop->previous_val;
401 bool changed = false;
402 int i;
403
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)
408 changed = true;
409 else if (val < prev_val &&
410 prev_val - val > psyc->tolerance_change)
411 changed = true;
412 else if (val > prev_val &&
413 val - prev_val > psyc->tolerance_change)
414 changed = true;
415
416 if (changed && psyc->prop_changed)
417 psyc->prop_changed(gbpsy, prop);
418
419 if (changed)
420 gbpsy->changed = true;
421 break;
422 }
423 }
424 }
425
426 static int total_props(struct gb_power_supply *gbpsy)
427 {
428 /* this return the intval plus the strval properties */
429 return (gbpsy->properties_count + gbpsy->properties_count_str);
430 }
431
432 static void prop_append(struct gb_power_supply *gbpsy,
433 enum power_supply_property prop)
434 {
435 enum power_supply_property *new_props_raw;
436
437 gbpsy->properties_count_str++;
438 new_props_raw = krealloc(gbpsy->props_raw, total_props(gbpsy) *
439 sizeof(enum power_supply_property),
440 GFP_KERNEL);
441 if (!new_props_raw)
442 return;
443 gbpsy->props_raw = new_props_raw;
444 gbpsy->props_raw[total_props(gbpsy) - 1] = prop;
445 }
446
447 static int __gb_power_supply_set_name(char *init_name, char *name, size_t len)
448 {
449 unsigned int i = 0;
450 int ret = 0;
451 struct power_supply *psy;
452
453 if (!strlen(init_name))
454 init_name = "gb_power_supply";
455 strlcpy(name, init_name, len);
456
457 while ((ret < len) && (psy = power_supply_get_by_name(name))) {
458 power_supply_put(psy);
459
460 ret = snprintf(name, len, "%s_%u", init_name, ++i);
461 }
462 if (ret >= len)
463 return -ENOMEM;
464 return i;
465 }
466
467 static void _gb_power_supply_append_props(struct gb_power_supply *gbpsy)
468 {
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);
475 }
476
477 static int gb_power_supply_description_get(struct gb_power_supply *gbpsy)
478 {
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;
482 int ret;
483
484 req.psy_id = gbpsy->id;
485
486 ret = gb_operation_sync(connection,
487 GB_POWER_SUPPLY_TYPE_GET_DESCRIPTION,
488 &req, sizeof(req), &resp, sizeof(resp));
489 if (ret < 0)
490 return ret;
491
492 gbpsy->manufacturer = kstrndup(resp.manufacturer, PROP_MAX, GFP_KERNEL);
493 if (!gbpsy->manufacturer)
494 return -ENOMEM;
495 gbpsy->model_name = kstrndup(resp.model, PROP_MAX, GFP_KERNEL);
496 if (!gbpsy->model_name)
497 return -ENOMEM;
498 gbpsy->serial_number = kstrndup(resp.serial_number, PROP_MAX,
499 GFP_KERNEL);
500 if (!gbpsy->serial_number)
501 return -ENOMEM;
502
503 gbpsy->type = le16_to_cpu(resp.type);
504 gbpsy->properties_count = resp.properties_count;
505
506 return 0;
507 }
508
509 static int gb_power_supply_prop_descriptors_get(struct gb_power_supply *gbpsy)
510 {
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;
517 int ret;
518 int i, r = 0;
519
520 if (props_count == 0)
521 return 0;
522
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),
527 GFP_KERNEL);
528 if (!op)
529 return -ENOMEM;
530
531 req = op->request->payload;
532 req->psy_id = gbpsy->id;
533
534 ret = gb_operation_request_send_sync(op);
535 if (ret < 0)
536 goto out_put_operation;
537
538 resp = op->response->payload;
539
540 /* validate received properties */
541 for (i = 0; i < props_count; i++) {
542 ret = get_psp_from_gb_prop(resp->props[i].property, &psp);
543 if (ret < 0) {
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--;
548 }
549 }
550
551 gbpsy->props = kcalloc(gbpsy->properties_count, sizeof(*gbpsy->props),
552 GFP_KERNEL);
553 if (!gbpsy->props) {
554 ret = -ENOMEM;
555 goto out_put_operation;
556 }
557
558 gbpsy->props_raw = kcalloc(gbpsy->properties_count,
559 sizeof(*gbpsy->props_raw), GFP_KERNEL);
560 if (!gbpsy->props_raw) {
561 ret = -ENOMEM;
562 goto out_put_operation;
563 }
564
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);
568 if (ret < 0) {
569 r++;
570 continue;
571 }
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;
577 }
578
579 /*
580 * now append the properties that we already got information in the
581 * get_description operation. (char * ones)
582 */
583 _gb_power_supply_append_props(gbpsy);
584
585 ret = 0;
586 out_put_operation:
587 gb_operation_put(op);
588
589 return ret;
590 }
591
592 static int __gb_power_supply_property_update(struct gb_power_supply *gbpsy,
593 enum power_supply_property psp)
594 {
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;
599 int val;
600 int ret;
601
602 prop = get_psy_prop(gbpsy, psp);
603 if (!prop)
604 return -EINVAL;
605 req.psy_id = gbpsy->id;
606 req.property = prop->gb_prop;
607
608 ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_GET_PROPERTY,
609 &req, sizeof(req), &resp, sizeof(resp));
610 if (ret < 0)
611 return ret;
612
613 val = le32_to_cpu(resp.prop_val);
614 if (val == prop->val)
615 return 0;
616
617 prop->previous_val = prop->val;
618 prop->val = val;
619
620 check_changed(gbpsy, prop);
621
622 return 0;
623 }
624
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)
628 {
629 struct gb_power_supply_prop *prop;
630
631 prop = get_psy_prop(gbpsy, psp);
632 if (!prop)
633 return -EINVAL;
634
635 val->intval = prop->val;
636 return 0;
637 }
638
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)
642 {
643 switch (psp) {
644 case POWER_SUPPLY_PROP_MODEL_NAME:
645 val->strval = gbpsy->model_name;
646 break;
647 case POWER_SUPPLY_PROP_MANUFACTURER:
648 val->strval = gbpsy->manufacturer;
649 break;
650 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
651 val->strval = gbpsy->serial_number;
652 break;
653 default:
654 break;
655 }
656
657 return 0;
658 }
659
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)
663 {
664 struct gb_connection *connection = get_conn_from_psy(gbpsy);
665 int ret;
666
667 /*
668 * Properties of type const char *, were already fetched on
669 * get_description operation and should be cached in gb
670 */
671 if (is_prop_valint(psp))
672 ret = __gb_power_supply_property_get(gbpsy, psp, val);
673 else
674 ret = __gb_power_supply_property_strval_get(gbpsy, psp, val);
675
676 if (ret < 0)
677 dev_err(&connection->bundle->dev, "get property %u\n", psp);
678
679 return 0;
680 }
681
682 static int is_cache_valid(struct gb_power_supply *gbpsy)
683 {
684 /* check if cache is good enough or it has expired */
685 if (gbpsy->cache_invalid) {
686 gbpsy->cache_invalid = 0;
687 return 0;
688 }
689
690 if (gbpsy->last_update &&
691 time_is_after_jiffies(gbpsy->last_update +
692 msecs_to_jiffies(cache_time)))
693 return 1;
694
695 return 0;
696 }
697
698 static int gb_power_supply_status_get(struct gb_power_supply *gbpsy)
699 {
700 struct gb_connection *connection = get_conn_from_psy(gbpsy);
701 int ret = 0;
702 int i;
703
704 if (is_cache_valid(gbpsy))
705 return 0;
706
707 ret = gb_pm_runtime_get_sync(connection->bundle);
708 if (ret)
709 return ret;
710
711 for (i = 0; i < gbpsy->properties_count; i++) {
712 ret = __gb_power_supply_property_update(gbpsy,
713 gbpsy->props[i].prop);
714 if (ret < 0)
715 break;
716 }
717
718 if (ret == 0)
719 gbpsy->last_update = jiffies;
720
721 gb_pm_runtime_put_autosuspend(connection->bundle);
722 return ret;
723 }
724
725 static void gb_power_supply_status_update(struct gb_power_supply *gbpsy)
726 {
727 /* check if there a change that need to be reported */
728 gb_power_supply_status_get(gbpsy);
729
730 if (!gbpsy->changed)
731 return;
732
733 gbpsy->update_interval = update_interval_init;
734 __gb_power_supply_changed(gbpsy);
735 gbpsy->changed = false;
736 }
737
738 static void gb_power_supply_work(struct work_struct *work)
739 {
740 struct gb_power_supply *gbpsy = container_of(work,
741 struct gb_power_supply,
742 work.work);
743
744 /*
745 * if the poll interval is not set, disable polling, this is helpful
746 * specially at unregister time.
747 */
748 if (!gbpsy->update_interval)
749 return;
750
751 gb_power_supply_status_update(gbpsy);
752 next_interval(gbpsy);
753 schedule_delayed_work(&gbpsy->work, gbpsy->update_interval);
754 }
755
756 static int get_property(struct power_supply *b,
757 enum power_supply_property psp,
758 union power_supply_propval *val)
759 {
760 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
761
762 gb_power_supply_status_get(gbpsy);
763
764 return _gb_power_supply_property_get(gbpsy, psp, val);
765 }
766
767 static int gb_power_supply_property_set(struct gb_power_supply *gbpsy,
768 enum power_supply_property psp,
769 int val)
770 {
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;
774 int ret;
775
776 ret = gb_pm_runtime_get_sync(connection->bundle);
777 if (ret)
778 return ret;
779
780 prop = get_psy_prop(gbpsy, psp);
781 if (!prop) {
782 ret = -EINVAL;
783 goto out;
784 }
785
786 req.psy_id = gbpsy->id;
787 req.property = prop->gb_prop;
788 req.prop_val = cpu_to_le32((s32)val);
789
790 ret = gb_operation_sync(connection, GB_POWER_SUPPLY_TYPE_SET_PROPERTY,
791 &req, sizeof(req), NULL, 0);
792 if (ret < 0)
793 goto out;
794
795 /* cache immediately the new value */
796 prop->val = val;
797
798 out:
799 gb_pm_runtime_put_autosuspend(connection->bundle);
800 return ret;
801 }
802
803 static int set_property(struct power_supply *b,
804 enum power_supply_property psp,
805 const union power_supply_propval *val)
806 {
807 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
808
809 return gb_power_supply_property_set(gbpsy, psp, val->intval);
810 }
811
812 static int property_is_writeable(struct power_supply *b,
813 enum power_supply_property psp)
814 {
815 struct gb_power_supply *gbpsy = to_gb_power_supply(b);
816
817 return is_psy_prop_writeable(gbpsy, psp);
818 }
819
820 static int gb_power_supply_register(struct gb_power_supply *gbpsy)
821 {
822 struct gb_connection *connection = get_conn_from_psy(gbpsy);
823 struct power_supply_config cfg = {};
824
825 cfg.drv_data = gbpsy;
826
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;
834
835 gbpsy->psy = power_supply_register(&connection->bundle->dev,
836 &gbpsy->desc, &cfg);
837 return PTR_ERR_OR_ZERO(gbpsy->psy);
838 }
839
840 static void _gb_power_supply_free(struct gb_power_supply *gbpsy)
841 {
842 kfree(gbpsy->serial_number);
843 kfree(gbpsy->model_name);
844 kfree(gbpsy->manufacturer);
845 kfree(gbpsy->props_raw);
846 kfree(gbpsy->props);
847 }
848
849 static void _gb_power_supply_release(struct gb_power_supply *gbpsy)
850 {
851 gbpsy->update_interval = 0;
852
853 cancel_delayed_work_sync(&gbpsy->work);
854
855 if (gbpsy->registered)
856 power_supply_unregister(gbpsy->psy);
857
858 _gb_power_supply_free(gbpsy);
859 }
860
861 static void _gb_power_supplies_release(struct gb_power_supplies *supplies)
862 {
863 int i;
864
865 if (!supplies->supply)
866 return;
867
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);
873 kfree(supplies);
874 }
875
876 static int gb_power_supplies_get_count(struct gb_power_supplies *supplies)
877 {
878 struct gb_power_supply_get_supplies_response resp;
879 int ret;
880
881 ret = gb_operation_sync(supplies->connection,
882 GB_POWER_SUPPLY_TYPE_GET_SUPPLIES,
883 NULL, 0, &resp, sizeof(resp));
884 if (ret < 0)
885 return ret;
886
887 if (!resp.supplies_count)
888 return -EINVAL;
889
890 supplies->supplies_count = resp.supplies_count;
891
892 return ret;
893 }
894
895 static int gb_power_supply_config(struct gb_power_supplies *supplies, int id)
896 {
897 struct gb_power_supply *gbpsy = &supplies->supply[id];
898 int ret;
899
900 gbpsy->supplies = supplies;
901 gbpsy->id = id;
902
903 ret = gb_power_supply_description_get(gbpsy);
904 if (ret < 0)
905 return ret;
906
907 return gb_power_supply_prop_descriptors_get(gbpsy);
908 }
909
910 static int gb_power_supply_enable(struct gb_power_supply *gbpsy)
911 {
912 int ret;
913
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));
917 if (ret < 0)
918 return ret;
919
920 mutex_init(&gbpsy->supply_lock);
921
922 ret = gb_power_supply_register(gbpsy);
923 if (ret < 0)
924 return ret;
925
926 gbpsy->update_interval = update_interval_init;
927 INIT_DELAYED_WORK(&gbpsy->work, gb_power_supply_work);
928 schedule_delayed_work(&gbpsy->work, 0);
929
930 /* everything went fine, mark it for release code to know */
931 gbpsy->registered = true;
932
933 return 0;
934 }
935
936 static int gb_power_supplies_setup(struct gb_power_supplies *supplies)
937 {
938 struct gb_connection *connection = supplies->connection;
939 int ret;
940 int i;
941
942 mutex_lock(&supplies->supplies_lock);
943
944 ret = gb_power_supplies_get_count(supplies);
945 if (ret < 0)
946 goto out;
947
948 supplies->supply = kcalloc(supplies->supplies_count,
949 sizeof(struct gb_power_supply),
950 GFP_KERNEL);
951
952 if (!supplies->supply) {
953 ret = -ENOMEM;
954 goto out;
955 }
956
957 for (i = 0; i < supplies->supplies_count; i++) {
958 ret = gb_power_supply_config(supplies, i);
959 if (ret < 0) {
960 dev_err(&connection->bundle->dev,
961 "Fail to configure supplies devices\n");
962 goto out;
963 }
964 }
965 out:
966 mutex_unlock(&supplies->supplies_lock);
967 return ret;
968 }
969
970 static int gb_power_supplies_register(struct gb_power_supplies *supplies)
971 {
972 struct gb_connection *connection = supplies->connection;
973 int ret = 0;
974 int i;
975
976 mutex_lock(&supplies->supplies_lock);
977
978 for (i = 0; i < supplies->supplies_count; i++) {
979 ret = gb_power_supply_enable(&supplies->supply[i]);
980 if (ret < 0) {
981 dev_err(&connection->bundle->dev,
982 "Fail to enable supplies devices\n");
983 break;
984 }
985 }
986
987 mutex_unlock(&supplies->supplies_lock);
988 return ret;
989 }
990
991 static int gb_supplies_request_handler(struct gb_operation *op)
992 {
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;
998 u8 psy_id;
999 u8 event;
1000 int ret = 0;
1001
1002 if (op->type != GB_POWER_SUPPLY_TYPE_EVENT) {
1003 dev_err(&connection->bundle->dev,
1004 "Unsupported unsolicited event: %u\n", op->type);
1005 return -EINVAL;
1006 }
1007
1008 request = op->request;
1009
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));
1014 return -EINVAL;
1015 }
1016
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",
1024 psy_id);
1025 ret = -EINVAL;
1026 goto out_unlock;
1027 }
1028
1029 event = payload->event;
1030 /*
1031 * we will only handle events after setup is done and before release is
1032 * running. For that just check update_interval.
1033 */
1034 gbpsy = &supplies->supply[psy_id];
1035 if (!gbpsy->update_interval) {
1036 ret = -ESHUTDOWN;
1037 goto out_unlock;
1038 }
1039
1040 if (event & GB_POWER_SUPPLY_UPDATE) {
1041 /*
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
1045 */
1046 gbpsy->cache_invalid = 1;
1047 gb_power_supply_status_update(gbpsy);
1048 }
1049
1050 out_unlock:
1051 mutex_unlock(&supplies->supplies_lock);
1052 return ret;
1053 }
1054
1055 static int gb_power_supply_probe(struct gb_bundle *bundle,
1056 const struct greybus_bundle_id *id)
1057 {
1058 struct greybus_descriptor_cport *cport_desc;
1059 struct gb_connection *connection;
1060 struct gb_power_supplies *supplies;
1061 int ret;
1062
1063 if (bundle->num_cports != 1)
1064 return -ENODEV;
1065
1066 cport_desc = &bundle->cport_desc[0];
1067 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_POWER_SUPPLY)
1068 return -ENODEV;
1069
1070 supplies = kzalloc(sizeof(*supplies), GFP_KERNEL);
1071 if (!supplies)
1072 return -ENOMEM;
1073
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);
1078 goto out;
1079 }
1080
1081 supplies->connection = connection;
1082 gb_connection_set_data(connection, supplies);
1083
1084 mutex_init(&supplies->supplies_lock);
1085
1086 greybus_set_drvdata(bundle, supplies);
1087
1088 /* We aren't ready to receive an incoming request yet */
1089 ret = gb_connection_enable_tx(connection);
1090 if (ret)
1091 goto error_connection_destroy;
1092
1093 ret = gb_power_supplies_setup(supplies);
1094 if (ret < 0)
1095 goto error_connection_disable;
1096
1097 /* We are ready to receive an incoming request now, enable RX as well */
1098 ret = gb_connection_enable(connection);
1099 if (ret)
1100 goto error_connection_disable;
1101
1102 ret = gb_power_supplies_register(supplies);
1103 if (ret < 0)
1104 goto error_connection_disable;
1105
1106 gb_pm_runtime_put_autosuspend(bundle);
1107 return 0;
1108
1109 error_connection_disable:
1110 gb_connection_disable(connection);
1111 error_connection_destroy:
1112 gb_connection_destroy(connection);
1113 out:
1114 _gb_power_supplies_release(supplies);
1115 return ret;
1116 }
1117
1118 static void gb_power_supply_disconnect(struct gb_bundle *bundle)
1119 {
1120 struct gb_power_supplies *supplies = greybus_get_drvdata(bundle);
1121
1122 gb_connection_disable(supplies->connection);
1123 gb_connection_destroy(supplies->connection);
1124
1125 _gb_power_supplies_release(supplies);
1126 }
1127
1128 static const struct greybus_bundle_id gb_power_supply_id_table[] = {
1129 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_POWER_SUPPLY) },
1130 { }
1131 };
1132 MODULE_DEVICE_TABLE(greybus, gb_power_supply_id_table);
1133
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,
1139 };
1140 module_greybus_driver(gb_power_supply_driver);
1141
1142 MODULE_LICENSE("GPL v2");