diff options
author | Hans de Goede <hdegoede@redhat.com> | 2015-08-01 04:39:38 -0400 |
---|---|---|
committer | Sebastian Reichel <sre@kernel.org> | 2015-09-22 07:35:28 -0400 |
commit | 69fb4dcada7704beeba86e3f401a66c726aa8504 (patch) | |
tree | f5e8a7cb8e77116447c44234f6235925ea9e508e | |
parent | 264905209a58779566a1d80c96110eea69b09440 (diff) |
power: Add an axp20x-usb-power driver
This adds a driver for the usb power_supply bits of the axp20x PMICs.
I initially started writing my own driver, before coming aware of
Bruno Prémont's excellent earlier RFC with a driver for this.
My driver was lacking CURRENT_MAX and VOLTAGE_MIN support Bruno's
drvier has, so I've copied the code for those from his driver.
Note that the AC-power-supply and battery charger bits will need separate
drivers. Each one needs its own devictree child-node so that other
devicetree nodes can reference the right power-supply, and thus each one
will get its own mfd-cell / platform_device and platform-driver.
Cc: Bruno Prémont <bonbons@linux-vserver.org>
Acked-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Bruno Prémont <bonbons@linux-vserver.org>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Sebastian Reichel <sre@kernel.org>
-rw-r--r-- | drivers/power/Kconfig | 7 | ||||
-rw-r--r-- | drivers/power/Makefile | 1 | ||||
-rw-r--r-- | drivers/power/axp20x_usb_power.c | 248 | ||||
-rw-r--r-- | include/linux/mfd/axp20x.h | 24 |
4 files changed, 280 insertions, 0 deletions
diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig index f8758d6febf8..914167e14d2e 100644 --- a/drivers/power/Kconfig +++ b/drivers/power/Kconfig | |||
@@ -472,6 +472,13 @@ config CHARGER_RT9455 | |||
472 | help | 472 | help |
473 | Say Y to enable support for Richtek RT9455 battery charger. | 473 | Say Y to enable support for Richtek RT9455 battery charger. |
474 | 474 | ||
475 | config AXP20X_POWER | ||
476 | tristate "AXP20x power supply driver" | ||
477 | depends on MFD_AXP20X | ||
478 | help | ||
479 | This driver provides support for the power supply features of | ||
480 | AXP20x PMIC. | ||
481 | |||
475 | source "drivers/power/reset/Kconfig" | 482 | source "drivers/power/reset/Kconfig" |
476 | 483 | ||
477 | endif # POWER_SUPPLY | 484 | endif # POWER_SUPPLY |
diff --git a/drivers/power/Makefile b/drivers/power/Makefile index 5752ce818f51..fb4413e5dd37 100644 --- a/drivers/power/Makefile +++ b/drivers/power/Makefile | |||
@@ -9,6 +9,7 @@ obj-$(CONFIG_GENERIC_ADC_BATTERY) += generic-adc-battery.o | |||
9 | 9 | ||
10 | obj-$(CONFIG_PDA_POWER) += pda_power.o | 10 | obj-$(CONFIG_PDA_POWER) += pda_power.o |
11 | obj-$(CONFIG_APM_POWER) += apm_power.o | 11 | obj-$(CONFIG_APM_POWER) += apm_power.o |
12 | obj-$(CONFIG_AXP20X_POWER) += axp20x_usb_power.o | ||
12 | obj-$(CONFIG_MAX8925_POWER) += max8925_power.o | 13 | obj-$(CONFIG_MAX8925_POWER) += max8925_power.o |
13 | obj-$(CONFIG_WM831X_BACKUP) += wm831x_backup.o | 14 | obj-$(CONFIG_WM831X_BACKUP) += wm831x_backup.o |
14 | obj-$(CONFIG_WM831X_POWER) += wm831x_power.o | 15 | obj-$(CONFIG_WM831X_POWER) += wm831x_power.o |
diff --git a/drivers/power/axp20x_usb_power.c b/drivers/power/axp20x_usb_power.c new file mode 100644 index 000000000000..421a90b83567 --- /dev/null +++ b/drivers/power/axp20x_usb_power.c | |||
@@ -0,0 +1,248 @@ | |||
1 | /* | ||
2 | * AXP20x PMIC USB power supply status driver | ||
3 | * | ||
4 | * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com> | ||
5 | * 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. | ||
11 | */ | ||
12 | |||
13 | #include <linux/device.h> | ||
14 | #include <linux/init.h> | ||
15 | #include <linux/interrupt.h> | ||
16 | #include <linux/kernel.h> | ||
17 | #include <linux/mfd/axp20x.h> | ||
18 | #include <linux/module.h> | ||
19 | #include <linux/of.h> | ||
20 | #include <linux/platform_device.h> | ||
21 | #include <linux/power_supply.h> | ||
22 | #include <linux/regmap.h> | ||
23 | #include <linux/slab.h> | ||
24 | |||
25 | #define DRVNAME "axp20x-usb-power-supply" | ||
26 | |||
27 | #define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5) | ||
28 | #define AXP20X_PWR_STATUS_VBUS_USED BIT(4) | ||
29 | |||
30 | #define AXP20X_USB_STATUS_VBUS_VALID BIT(2) | ||
31 | |||
32 | #define AXP20X_VBUS_VHOLD_uV(b) (4000000 + (((b) >> 3) & 7) * 100000) | ||
33 | #define AXP20X_VBUS_CLIMIT_MASK 3 | ||
34 | #define AXP20X_VBUC_CLIMIT_900mA 0 | ||
35 | #define AXP20X_VBUC_CLIMIT_500mA 1 | ||
36 | #define AXP20X_VBUC_CLIMIT_100mA 2 | ||
37 | #define AXP20X_VBUC_CLIMIT_NONE 3 | ||
38 | |||
39 | #define AXP20X_ADC_EN1_VBUS_CURR BIT(2) | ||
40 | #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3) | ||
41 | |||
42 | #define AXP20X_VBUS_MON_VBUS_VALID BIT(3) | ||
43 | |||
44 | struct axp20x_usb_power { | ||
45 | struct regmap *regmap; | ||
46 | struct power_supply *supply; | ||
47 | }; | ||
48 | |||
49 | static irqreturn_t axp20x_usb_power_irq(int irq, void *devid) | ||
50 | { | ||
51 | struct axp20x_usb_power *power = devid; | ||
52 | |||
53 | power_supply_changed(power->supply); | ||
54 | |||
55 | return IRQ_HANDLED; | ||
56 | } | ||
57 | |||
58 | static int axp20x_usb_power_get_property(struct power_supply *psy, | ||
59 | enum power_supply_property psp, union power_supply_propval *val) | ||
60 | { | ||
61 | struct axp20x_usb_power *power = power_supply_get_drvdata(psy); | ||
62 | unsigned int input, v; | ||
63 | int ret; | ||
64 | |||
65 | switch (psp) { | ||
66 | case POWER_SUPPLY_PROP_VOLTAGE_MIN: | ||
67 | ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v); | ||
68 | if (ret) | ||
69 | return ret; | ||
70 | |||
71 | val->intval = AXP20X_VBUS_VHOLD_uV(v); | ||
72 | return 0; | ||
73 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | ||
74 | ret = axp20x_read_variable_width(power->regmap, | ||
75 | AXP20X_VBUS_V_ADC_H, 12); | ||
76 | if (ret < 0) | ||
77 | return ret; | ||
78 | |||
79 | val->intval = ret * 1700; /* 1 step = 1.7 mV */ | ||
80 | return 0; | ||
81 | case POWER_SUPPLY_PROP_CURRENT_MAX: | ||
82 | ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v); | ||
83 | if (ret) | ||
84 | return ret; | ||
85 | |||
86 | switch (v & AXP20X_VBUS_CLIMIT_MASK) { | ||
87 | case AXP20X_VBUC_CLIMIT_100mA: | ||
88 | val->intval = 100000; | ||
89 | break; | ||
90 | case AXP20X_VBUC_CLIMIT_500mA: | ||
91 | val->intval = 500000; | ||
92 | break; | ||
93 | case AXP20X_VBUC_CLIMIT_900mA: | ||
94 | val->intval = 900000; | ||
95 | break; | ||
96 | case AXP20X_VBUC_CLIMIT_NONE: | ||
97 | val->intval = -1; | ||
98 | break; | ||
99 | } | ||
100 | return 0; | ||
101 | case POWER_SUPPLY_PROP_CURRENT_NOW: | ||
102 | ret = axp20x_read_variable_width(power->regmap, | ||
103 | AXP20X_VBUS_I_ADC_H, 12); | ||
104 | if (ret < 0) | ||
105 | return ret; | ||
106 | |||
107 | val->intval = ret * 375; /* 1 step = 0.375 mA */ | ||
108 | return 0; | ||
109 | default: | ||
110 | break; | ||
111 | } | ||
112 | |||
113 | /* All the properties below need the input-status reg value */ | ||
114 | ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input); | ||
115 | if (ret) | ||
116 | return ret; | ||
117 | |||
118 | switch (psp) { | ||
119 | case POWER_SUPPLY_PROP_HEALTH: | ||
120 | if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) { | ||
121 | val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; | ||
122 | break; | ||
123 | } | ||
124 | |||
125 | ret = regmap_read(power->regmap, AXP20X_USB_OTG_STATUS, &v); | ||
126 | if (ret) | ||
127 | return ret; | ||
128 | |||
129 | if (!(v & AXP20X_USB_STATUS_VBUS_VALID)) { | ||
130 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; | ||
131 | break; | ||
132 | } | ||
133 | |||
134 | val->intval = POWER_SUPPLY_HEALTH_GOOD; | ||
135 | break; | ||
136 | case POWER_SUPPLY_PROP_PRESENT: | ||
137 | val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT); | ||
138 | break; | ||
139 | case POWER_SUPPLY_PROP_ONLINE: | ||
140 | val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED); | ||
141 | break; | ||
142 | default: | ||
143 | return -EINVAL; | ||
144 | } | ||
145 | |||
146 | return 0; | ||
147 | } | ||
148 | |||
149 | static enum power_supply_property axp20x_usb_power_properties[] = { | ||
150 | POWER_SUPPLY_PROP_HEALTH, | ||
151 | POWER_SUPPLY_PROP_PRESENT, | ||
152 | POWER_SUPPLY_PROP_ONLINE, | ||
153 | POWER_SUPPLY_PROP_VOLTAGE_MIN, | ||
154 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | ||
155 | POWER_SUPPLY_PROP_CURRENT_MAX, | ||
156 | POWER_SUPPLY_PROP_CURRENT_NOW, | ||
157 | }; | ||
158 | |||
159 | static const struct power_supply_desc axp20x_usb_power_desc = { | ||
160 | .name = "axp20x-usb", | ||
161 | .type = POWER_SUPPLY_TYPE_USB, | ||
162 | .properties = axp20x_usb_power_properties, | ||
163 | .num_properties = ARRAY_SIZE(axp20x_usb_power_properties), | ||
164 | .get_property = axp20x_usb_power_get_property, | ||
165 | }; | ||
166 | |||
167 | static int axp20x_usb_power_probe(struct platform_device *pdev) | ||
168 | { | ||
169 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); | ||
170 | struct power_supply_config psy_cfg = {}; | ||
171 | struct axp20x_usb_power *power; | ||
172 | static const char * const irq_names[] = { "VBUS_PLUGIN", | ||
173 | "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID" }; | ||
174 | int i, irq, ret; | ||
175 | |||
176 | if (!of_device_is_available(pdev->dev.of_node)) | ||
177 | return -ENODEV; | ||
178 | |||
179 | if (!axp20x) { | ||
180 | dev_err(&pdev->dev, "Parent drvdata not set\n"); | ||
181 | return -EINVAL; | ||
182 | } | ||
183 | |||
184 | power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL); | ||
185 | if (!power) | ||
186 | return -ENOMEM; | ||
187 | |||
188 | power->regmap = axp20x->regmap; | ||
189 | |||
190 | /* Enable vbus valid checking */ | ||
191 | ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON, | ||
192 | AXP20X_VBUS_MON_VBUS_VALID, AXP20X_VBUS_MON_VBUS_VALID); | ||
193 | if (ret) | ||
194 | return ret; | ||
195 | |||
196 | /* Enable vbus voltage and current measurement */ | ||
197 | ret = regmap_update_bits(power->regmap, AXP20X_ADC_EN1, | ||
198 | AXP20X_ADC_EN1_VBUS_CURR | AXP20X_ADC_EN1_VBUS_VOLT, | ||
199 | AXP20X_ADC_EN1_VBUS_CURR | AXP20X_ADC_EN1_VBUS_VOLT); | ||
200 | if (ret) | ||
201 | return ret; | ||
202 | |||
203 | psy_cfg.of_node = pdev->dev.of_node; | ||
204 | psy_cfg.drv_data = power; | ||
205 | |||
206 | power->supply = devm_power_supply_register(&pdev->dev, | ||
207 | &axp20x_usb_power_desc, &psy_cfg); | ||
208 | if (IS_ERR(power->supply)) | ||
209 | return PTR_ERR(power->supply); | ||
210 | |||
211 | /* Request irqs after registering, as irqs may trigger immediately */ | ||
212 | for (i = 0; i < ARRAY_SIZE(irq_names); i++) { | ||
213 | irq = platform_get_irq_byname(pdev, irq_names[i]); | ||
214 | if (irq < 0) { | ||
215 | dev_warn(&pdev->dev, "No IRQ for %s: %d\n", | ||
216 | irq_names[i], irq); | ||
217 | continue; | ||
218 | } | ||
219 | irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq); | ||
220 | ret = devm_request_any_context_irq(&pdev->dev, irq, | ||
221 | axp20x_usb_power_irq, 0, DRVNAME, power); | ||
222 | if (ret < 0) | ||
223 | dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n", | ||
224 | irq_names[i], ret); | ||
225 | } | ||
226 | |||
227 | return 0; | ||
228 | } | ||
229 | |||
230 | static const struct of_device_id axp20x_usb_power_match[] = { | ||
231 | { .compatible = "x-powers,axp202-usb-power-supply" }, | ||
232 | { } | ||
233 | }; | ||
234 | MODULE_DEVICE_TABLE(of, axp20x_usb_power_match); | ||
235 | |||
236 | static struct platform_driver axp20x_usb_power_driver = { | ||
237 | .probe = axp20x_usb_power_probe, | ||
238 | .driver = { | ||
239 | .name = DRVNAME, | ||
240 | .of_match_table = axp20x_usb_power_match, | ||
241 | }, | ||
242 | }; | ||
243 | |||
244 | module_platform_driver(axp20x_usb_power_driver); | ||
245 | |||
246 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); | ||
247 | MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver"); | ||
248 | MODULE_LICENSE("GPL"); | ||
diff --git a/include/linux/mfd/axp20x.h b/include/linux/mfd/axp20x.h index cc8ad1e1a307..b24c771cebd5 100644 --- a/include/linux/mfd/axp20x.h +++ b/include/linux/mfd/axp20x.h | |||
@@ -11,6 +11,8 @@ | |||
11 | #ifndef __LINUX_MFD_AXP20X_H | 11 | #ifndef __LINUX_MFD_AXP20X_H |
12 | #define __LINUX_MFD_AXP20X_H | 12 | #define __LINUX_MFD_AXP20X_H |
13 | 13 | ||
14 | #include <linux/regmap.h> | ||
15 | |||
14 | enum { | 16 | enum { |
15 | AXP152_ID = 0, | 17 | AXP152_ID = 0, |
16 | AXP202_ID, | 18 | AXP202_ID, |
@@ -438,4 +440,26 @@ struct axp288_extcon_pdata { | |||
438 | struct gpio_desc *gpio_mux_cntl; | 440 | struct gpio_desc *gpio_mux_cntl; |
439 | }; | 441 | }; |
440 | 442 | ||
443 | /* generic helper function for reading 9-16 bit wide regs */ | ||
444 | static inline int axp20x_read_variable_width(struct regmap *regmap, | ||
445 | unsigned int reg, unsigned int width) | ||
446 | { | ||
447 | unsigned int reg_val, result; | ||
448 | int err; | ||
449 | |||
450 | err = regmap_read(regmap, reg, ®_val); | ||
451 | if (err) | ||
452 | return err; | ||
453 | |||
454 | result = reg_val << (width - 8); | ||
455 | |||
456 | err = regmap_read(regmap, reg + 1, ®_val); | ||
457 | if (err) | ||
458 | return err; | ||
459 | |||
460 | result |= reg_val; | ||
461 | |||
462 | return result; | ||
463 | } | ||
464 | |||
441 | #endif /* __LINUX_MFD_AXP20X_H */ | 465 | #endif /* __LINUX_MFD_AXP20X_H */ |