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