hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/power/supply/axp20x_usb_power.c
....@@ -1,15 +1,12 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * AXP20x PMIC USB power supply status driver
34 *
45 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
56 * Copyright (C) 2014 Bruno Prémont <bonbons@linux-vserver.org>
6
- *
7
- * This program is free software; you can redistribute it and/or modify it
8
- * under the terms of the GNU General Public License as published by the
9
- * Free Software Foundation; either version 2 of the License, or (at your
10
- * option) any later version.
117 */
128
9
+#include <linux/bitops.h>
1310 #include <linux/device.h>
1411 #include <linux/init.h>
1512 #include <linux/interrupt.h>
....@@ -19,10 +16,12 @@
1916 #include <linux/of.h>
2017 #include <linux/of_device.h>
2118 #include <linux/platform_device.h>
19
+#include <linux/pm.h>
2220 #include <linux/power_supply.h>
2321 #include <linux/regmap.h>
2422 #include <linux/slab.h>
2523 #include <linux/iio/consumer.h>
24
+#include <linux/workqueue.h>
2625
2726 #define DRVNAME "axp20x-usb-power-supply"
2827
....@@ -31,28 +30,61 @@
3130
3231 #define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
3332
33
+#define AXP20X_VBUS_PATH_SEL BIT(7)
34
+#define AXP20X_VBUS_PATH_SEL_OFFSET 7
35
+
3436 #define AXP20X_VBUS_VHOLD_uV(b) (4000000 + (((b) >> 3) & 7) * 100000)
3537 #define AXP20X_VBUS_VHOLD_MASK GENMASK(5, 3)
3638 #define AXP20X_VBUS_VHOLD_OFFSET 3
3739 #define AXP20X_VBUS_CLIMIT_MASK 3
38
-#define AXP20X_VBUC_CLIMIT_900mA 0
39
-#define AXP20X_VBUC_CLIMIT_500mA 1
40
-#define AXP20X_VBUC_CLIMIT_100mA 2
41
-#define AXP20X_VBUC_CLIMIT_NONE 3
40
+#define AXP20X_VBUS_CLIMIT_900mA 0
41
+#define AXP20X_VBUS_CLIMIT_500mA 1
42
+#define AXP20X_VBUS_CLIMIT_100mA 2
43
+#define AXP20X_VBUS_CLIMIT_NONE 3
44
+
45
+#define AXP813_VBUS_CLIMIT_900mA 0
46
+#define AXP813_VBUS_CLIMIT_1500mA 1
47
+#define AXP813_VBUS_CLIMIT_2000mA 2
48
+#define AXP813_VBUS_CLIMIT_2500mA 3
4249
4350 #define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
4451 #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
4552
4653 #define AXP20X_VBUS_MON_VBUS_VALID BIT(3)
4754
55
+#define AXP813_BC_EN BIT(0)
56
+
57
+/*
58
+ * Note do not raise the debounce time, we must report Vusb high within
59
+ * 100ms otherwise we get Vbus errors in musb.
60
+ */
61
+#define DEBOUNCE_TIME msecs_to_jiffies(50)
62
+
4863 struct axp20x_usb_power {
49
- struct device_node *np;
5064 struct regmap *regmap;
5165 struct power_supply *supply;
5266 enum axp20x_variants axp20x_id;
5367 struct iio_channel *vbus_v;
5468 struct iio_channel *vbus_i;
69
+ struct delayed_work vbus_detect;
70
+ unsigned int old_status;
71
+ unsigned int online;
72
+ unsigned int num_irqs;
73
+ unsigned int irqs[];
5574 };
75
+
76
+static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power *power)
77
+{
78
+ /*
79
+ * Polling is only necessary while VBUS is offline. While online, a
80
+ * present->absent transition implies an online->offline transition
81
+ * and will trigger the VBUS_REMOVAL IRQ.
82
+ */
83
+ if (power->axp20x_id >= AXP221_ID && !power->online)
84
+ return true;
85
+
86
+ return false;
87
+}
5688
5789 static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
5890 {
....@@ -60,7 +92,86 @@
6092
6193 power_supply_changed(power->supply);
6294
95
+ mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
96
+
6397 return IRQ_HANDLED;
98
+}
99
+
100
+static void axp20x_usb_power_poll_vbus(struct work_struct *work)
101
+{
102
+ struct axp20x_usb_power *power =
103
+ container_of(work, struct axp20x_usb_power, vbus_detect.work);
104
+ unsigned int val;
105
+ int ret;
106
+
107
+ ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &val);
108
+ if (ret)
109
+ goto out;
110
+
111
+ val &= (AXP20X_PWR_STATUS_VBUS_PRESENT | AXP20X_PWR_STATUS_VBUS_USED);
112
+ if (val != power->old_status)
113
+ power_supply_changed(power->supply);
114
+
115
+ power->old_status = val;
116
+ power->online = val & AXP20X_PWR_STATUS_VBUS_USED;
117
+
118
+out:
119
+ if (axp20x_usb_vbus_needs_polling(power))
120
+ mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
121
+}
122
+
123
+static int axp20x_get_current_max(struct axp20x_usb_power *power, int *val)
124
+{
125
+ unsigned int v;
126
+ int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
127
+
128
+ if (ret)
129
+ return ret;
130
+
131
+ switch (v & AXP20X_VBUS_CLIMIT_MASK) {
132
+ case AXP20X_VBUS_CLIMIT_100mA:
133
+ if (power->axp20x_id == AXP221_ID)
134
+ *val = -1; /* No 100mA limit */
135
+ else
136
+ *val = 100000;
137
+ break;
138
+ case AXP20X_VBUS_CLIMIT_500mA:
139
+ *val = 500000;
140
+ break;
141
+ case AXP20X_VBUS_CLIMIT_900mA:
142
+ *val = 900000;
143
+ break;
144
+ case AXP20X_VBUS_CLIMIT_NONE:
145
+ *val = -1;
146
+ break;
147
+ }
148
+
149
+ return 0;
150
+}
151
+
152
+static int axp813_get_current_max(struct axp20x_usb_power *power, int *val)
153
+{
154
+ unsigned int v;
155
+ int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
156
+
157
+ if (ret)
158
+ return ret;
159
+
160
+ switch (v & AXP20X_VBUS_CLIMIT_MASK) {
161
+ case AXP813_VBUS_CLIMIT_900mA:
162
+ *val = 900000;
163
+ break;
164
+ case AXP813_VBUS_CLIMIT_1500mA:
165
+ *val = 1500000;
166
+ break;
167
+ case AXP813_VBUS_CLIMIT_2000mA:
168
+ *val = 2000000;
169
+ break;
170
+ case AXP813_VBUS_CLIMIT_2500mA:
171
+ *val = 2500000;
172
+ break;
173
+ }
174
+ return 0;
64175 }
65176
66177 static int axp20x_usb_power_get_property(struct power_supply *psy,
....@@ -101,28 +212,9 @@
101212 val->intval = ret * 1700; /* 1 step = 1.7 mV */
102213 return 0;
103214 case POWER_SUPPLY_PROP_CURRENT_MAX:
104
- ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
105
- if (ret)
106
- return ret;
107
-
108
- switch (v & AXP20X_VBUS_CLIMIT_MASK) {
109
- case AXP20X_VBUC_CLIMIT_100mA:
110
- if (power->axp20x_id == AXP221_ID)
111
- val->intval = -1; /* No 100mA limit */
112
- else
113
- val->intval = 100000;
114
- break;
115
- case AXP20X_VBUC_CLIMIT_500mA:
116
- val->intval = 500000;
117
- break;
118
- case AXP20X_VBUC_CLIMIT_900mA:
119
- val->intval = 900000;
120
- break;
121
- case AXP20X_VBUC_CLIMIT_NONE:
122
- val->intval = -1;
123
- break;
124
- }
125
- return 0;
215
+ if (power->axp20x_id == AXP813_ID)
216
+ return axp813_get_current_max(power, &val->intval);
217
+ return axp20x_get_current_max(power, &val->intval);
126218 case POWER_SUPPLY_PROP_CURRENT_NOW:
127219 if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
128220 ret = iio_read_channel_processed(power->vbus_i,
....@@ -187,6 +279,16 @@
187279 return 0;
188280 }
189281
282
+static int axp813_usb_power_set_online(struct axp20x_usb_power *power,
283
+ int intval)
284
+{
285
+ int val = !intval << AXP20X_VBUS_PATH_SEL_OFFSET;
286
+
287
+ return regmap_update_bits(power->regmap,
288
+ AXP20X_VBUS_IPSOUT_MGMT,
289
+ AXP20X_VBUS_PATH_SEL, val);
290
+}
291
+
190292 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
191293 int intval)
192294 {
....@@ -213,6 +315,31 @@
213315 return -EINVAL;
214316 }
215317
318
+static int axp813_usb_power_set_current_max(struct axp20x_usb_power *power,
319
+ int intval)
320
+{
321
+ int val;
322
+
323
+ switch (intval) {
324
+ case 900000:
325
+ return regmap_update_bits(power->regmap,
326
+ AXP20X_VBUS_IPSOUT_MGMT,
327
+ AXP20X_VBUS_CLIMIT_MASK,
328
+ AXP813_VBUS_CLIMIT_900mA);
329
+ case 1500000:
330
+ case 2000000:
331
+ case 2500000:
332
+ val = (intval - 1000000) / 500000;
333
+ return regmap_update_bits(power->regmap,
334
+ AXP20X_VBUS_IPSOUT_MGMT,
335
+ AXP20X_VBUS_CLIMIT_MASK, val);
336
+ default:
337
+ return -EINVAL;
338
+ }
339
+
340
+ return -EINVAL;
341
+}
342
+
216343 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
217344 int intval)
218345 {
....@@ -222,7 +349,7 @@
222349 case 100000:
223350 if (power->axp20x_id == AXP221_ID)
224351 return -EINVAL;
225
- /* fall through */
352
+ fallthrough;
226353 case 500000:
227354 case 900000:
228355 val = (900000 - intval) / 400000;
....@@ -243,10 +370,18 @@
243370 struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
244371
245372 switch (psp) {
373
+ case POWER_SUPPLY_PROP_ONLINE:
374
+ if (power->axp20x_id != AXP813_ID)
375
+ return -EINVAL;
376
+ return axp813_usb_power_set_online(power, val->intval);
377
+
246378 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
247379 return axp20x_usb_power_set_voltage_min(power, val->intval);
248380
249381 case POWER_SUPPLY_PROP_CURRENT_MAX:
382
+ if (power->axp20x_id == AXP813_ID)
383
+ return axp813_usb_power_set_current_max(power,
384
+ val->intval);
250385 return axp20x_usb_power_set_current_max(power, val->intval);
251386
252387 default:
....@@ -259,6 +394,18 @@
259394 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
260395 enum power_supply_property psp)
261396 {
397
+ struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
398
+
399
+ /*
400
+ * The VBUS path select flag works differently on on AXP288 and newer:
401
+ * - On AXP20x and AXP22x, the flag enables VBUS (ignoring N_VBUSEN).
402
+ * - On AXP288 and AXP8xx, the flag disables VBUS (ignoring N_VBUSEN).
403
+ * We only expose the control on variants where it can be used to force
404
+ * the VBUS input offline.
405
+ */
406
+ if (psp == POWER_SUPPLY_PROP_ONLINE)
407
+ return power->axp20x_id == AXP813_ID;
408
+
262409 return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
263410 psp == POWER_SUPPLY_PROP_CURRENT_MAX;
264411 }
....@@ -301,6 +448,92 @@
301448 .set_property = axp20x_usb_power_set_property,
302449 };
303450
451
+static const char * const axp20x_irq_names[] = {
452
+ "VBUS_PLUGIN",
453
+ "VBUS_REMOVAL",
454
+ "VBUS_VALID",
455
+ "VBUS_NOT_VALID",
456
+};
457
+
458
+static const char * const axp22x_irq_names[] = {
459
+ "VBUS_PLUGIN",
460
+ "VBUS_REMOVAL",
461
+};
462
+
463
+struct axp_data {
464
+ const struct power_supply_desc *power_desc;
465
+ const char * const *irq_names;
466
+ unsigned int num_irq_names;
467
+ enum axp20x_variants axp20x_id;
468
+};
469
+
470
+static const struct axp_data axp202_data = {
471
+ .power_desc = &axp20x_usb_power_desc,
472
+ .irq_names = axp20x_irq_names,
473
+ .num_irq_names = ARRAY_SIZE(axp20x_irq_names),
474
+ .axp20x_id = AXP202_ID,
475
+};
476
+
477
+static const struct axp_data axp221_data = {
478
+ .power_desc = &axp22x_usb_power_desc,
479
+ .irq_names = axp22x_irq_names,
480
+ .num_irq_names = ARRAY_SIZE(axp22x_irq_names),
481
+ .axp20x_id = AXP221_ID,
482
+};
483
+
484
+static const struct axp_data axp223_data = {
485
+ .power_desc = &axp22x_usb_power_desc,
486
+ .irq_names = axp22x_irq_names,
487
+ .num_irq_names = ARRAY_SIZE(axp22x_irq_names),
488
+ .axp20x_id = AXP223_ID,
489
+};
490
+
491
+static const struct axp_data axp813_data = {
492
+ .power_desc = &axp22x_usb_power_desc,
493
+ .irq_names = axp22x_irq_names,
494
+ .num_irq_names = ARRAY_SIZE(axp22x_irq_names),
495
+ .axp20x_id = AXP813_ID,
496
+};
497
+
498
+#ifdef CONFIG_PM_SLEEP
499
+static int axp20x_usb_power_suspend(struct device *dev)
500
+{
501
+ struct axp20x_usb_power *power = dev_get_drvdata(dev);
502
+ int i = 0;
503
+
504
+ /*
505
+ * Allow wake via VBUS_PLUGIN only.
506
+ *
507
+ * As nested threaded IRQs are not automatically disabled during
508
+ * suspend, we must explicitly disable the remainder of the IRQs.
509
+ */
510
+ if (device_may_wakeup(&power->supply->dev))
511
+ enable_irq_wake(power->irqs[i++]);
512
+ while (i < power->num_irqs)
513
+ disable_irq(power->irqs[i++]);
514
+
515
+ return 0;
516
+}
517
+
518
+static int axp20x_usb_power_resume(struct device *dev)
519
+{
520
+ struct axp20x_usb_power *power = dev_get_drvdata(dev);
521
+ int i = 0;
522
+
523
+ if (device_may_wakeup(&power->supply->dev))
524
+ disable_irq_wake(power->irqs[i++]);
525
+ while (i < power->num_irqs)
526
+ enable_irq(power->irqs[i++]);
527
+
528
+ mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
529
+
530
+ return 0;
531
+}
532
+#endif
533
+
534
+static SIMPLE_DEV_PM_OPS(axp20x_usb_power_pm_ops, axp20x_usb_power_suspend,
535
+ axp20x_usb_power_resume);
536
+
304537 static int configure_iio_channels(struct platform_device *pdev,
305538 struct axp20x_usb_power *power)
306539 {
....@@ -336,12 +569,7 @@
336569 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
337570 struct power_supply_config psy_cfg = {};
338571 struct axp20x_usb_power *power;
339
- static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN",
340
- "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL };
341
- static const char * const axp22x_irq_names[] = {
342
- "VBUS_PLUGIN", "VBUS_REMOVAL", NULL };
343
- const char * const *irq_names;
344
- const struct power_supply_desc *usb_power_desc;
572
+ const struct axp_data *axp_data;
345573 int i, irq, ret;
346574
347575 if (!of_device_is_available(pdev->dev.of_node))
....@@ -352,15 +580,20 @@
352580 return -EINVAL;
353581 }
354582
355
- power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
583
+ axp_data = of_device_get_match_data(&pdev->dev);
584
+
585
+ power = devm_kzalloc(&pdev->dev,
586
+ struct_size(power, irqs, axp_data->num_irq_names),
587
+ GFP_KERNEL);
356588 if (!power)
357589 return -ENOMEM;
358590
359
- power->axp20x_id = (enum axp20x_variants)of_device_get_match_data(
360
- &pdev->dev);
591
+ platform_set_drvdata(pdev, power);
361592
362
- power->np = pdev->dev.of_node;
593
+ power->axp20x_id = axp_data->axp20x_id;
363594 power->regmap = axp20x->regmap;
595
+ power->num_irqs = axp_data->num_irq_names;
596
+ INIT_DELAYED_WORK(&power->vbus_detect, axp20x_usb_power_poll_vbus);
364597
365598 if (power->axp20x_id == AXP202_ID) {
366599 /* Enable vbus valid checking */
....@@ -377,42 +610,53 @@
377610
378611 if (ret)
379612 return ret;
613
+ }
380614
381
- usb_power_desc = &axp20x_usb_power_desc;
382
- irq_names = axp20x_irq_names;
383
- } else if (power->axp20x_id == AXP221_ID ||
384
- power->axp20x_id == AXP223_ID) {
385
- usb_power_desc = &axp22x_usb_power_desc;
386
- irq_names = axp22x_irq_names;
387
- } else {
388
- dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
389
- axp20x->variant);
390
- return -EINVAL;
615
+ if (power->axp20x_id == AXP813_ID) {
616
+ /* Enable USB Battery Charging specification detection */
617
+ regmap_update_bits(axp20x->regmap, AXP288_BC_GLOBAL,
618
+ AXP813_BC_EN, AXP813_BC_EN);
391619 }
392620
393621 psy_cfg.of_node = pdev->dev.of_node;
394622 psy_cfg.drv_data = power;
395623
396
- power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
624
+ power->supply = devm_power_supply_register(&pdev->dev,
625
+ axp_data->power_desc,
397626 &psy_cfg);
398627 if (IS_ERR(power->supply))
399628 return PTR_ERR(power->supply);
400629
401630 /* Request irqs after registering, as irqs may trigger immediately */
402
- for (i = 0; irq_names[i]; i++) {
403
- irq = platform_get_irq_byname(pdev, irq_names[i]);
631
+ for (i = 0; i < axp_data->num_irq_names; i++) {
632
+ irq = platform_get_irq_byname(pdev, axp_data->irq_names[i]);
404633 if (irq < 0) {
405
- dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
406
- irq_names[i], irq);
407
- continue;
634
+ dev_err(&pdev->dev, "No IRQ for %s: %d\n",
635
+ axp_data->irq_names[i], irq);
636
+ return irq;
408637 }
409
- irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
410
- ret = devm_request_any_context_irq(&pdev->dev, irq,
411
- axp20x_usb_power_irq, 0, DRVNAME, power);
412
- if (ret < 0)
413
- dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
414
- irq_names[i], ret);
638
+ power->irqs[i] = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
639
+ ret = devm_request_any_context_irq(&pdev->dev, power->irqs[i],
640
+ axp20x_usb_power_irq, 0,
641
+ DRVNAME, power);
642
+ if (ret < 0) {
643
+ dev_err(&pdev->dev, "Error requesting %s IRQ: %d\n",
644
+ axp_data->irq_names[i], ret);
645
+ return ret;
646
+ }
415647 }
648
+
649
+ if (axp20x_usb_vbus_needs_polling(power))
650
+ queue_delayed_work(system_wq, &power->vbus_detect, 0);
651
+
652
+ return 0;
653
+}
654
+
655
+static int axp20x_usb_power_remove(struct platform_device *pdev)
656
+{
657
+ struct axp20x_usb_power *power = platform_get_drvdata(pdev);
658
+
659
+ cancel_delayed_work_sync(&power->vbus_detect);
416660
417661 return 0;
418662 }
....@@ -420,22 +664,27 @@
420664 static const struct of_device_id axp20x_usb_power_match[] = {
421665 {
422666 .compatible = "x-powers,axp202-usb-power-supply",
423
- .data = (void *)AXP202_ID,
667
+ .data = &axp202_data,
424668 }, {
425669 .compatible = "x-powers,axp221-usb-power-supply",
426
- .data = (void *)AXP221_ID,
670
+ .data = &axp221_data,
427671 }, {
428672 .compatible = "x-powers,axp223-usb-power-supply",
429
- .data = (void *)AXP223_ID,
673
+ .data = &axp223_data,
674
+ }, {
675
+ .compatible = "x-powers,axp813-usb-power-supply",
676
+ .data = &axp813_data,
430677 }, { /* sentinel */ }
431678 };
432679 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
433680
434681 static struct platform_driver axp20x_usb_power_driver = {
435682 .probe = axp20x_usb_power_probe,
683
+ .remove = axp20x_usb_power_remove,
436684 .driver = {
437
- .name = DRVNAME,
438
- .of_match_table = axp20x_usb_power_match,
685
+ .name = DRVNAME,
686
+ .of_match_table = axp20x_usb_power_match,
687
+ .pm = &axp20x_usb_power_pm_ops,
439688 },
440689 };
441690