]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/power/supply/axp20x_usb_power.c
power: supply: axp20x_usb_power: set min voltage and max current from sysfs
[linux.git] / drivers / power / supply / axp20x_usb_power.c
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/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/power_supply.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
25
26 #define DRVNAME "axp20x-usb-power-supply"
27
28 #define AXP20X_PWR_STATUS_VBUS_PRESENT  BIT(5)
29 #define AXP20X_PWR_STATUS_VBUS_USED     BIT(4)
30
31 #define AXP20X_USB_STATUS_VBUS_VALID    BIT(2)
32
33 #define AXP20X_VBUS_VHOLD_uV(b)         (4000000 + (((b) >> 3) & 7) * 100000)
34 #define AXP20X_VBUS_VHOLD_MASK          GENMASK(5, 3)
35 #define AXP20X_VBUS_VHOLD_OFFSET        3
36 #define AXP20X_VBUS_CLIMIT_MASK         3
37 #define AXP20X_VBUC_CLIMIT_900mA        0
38 #define AXP20X_VBUC_CLIMIT_500mA        1
39 #define AXP20X_VBUC_CLIMIT_100mA        2
40 #define AXP20X_VBUC_CLIMIT_NONE         3
41
42 #define AXP20X_ADC_EN1_VBUS_CURR        BIT(2)
43 #define AXP20X_ADC_EN1_VBUS_VOLT        BIT(3)
44
45 #define AXP20X_VBUS_MON_VBUS_VALID      BIT(3)
46
47 struct axp20x_usb_power {
48         struct device_node *np;
49         struct regmap *regmap;
50         struct power_supply *supply;
51         int axp20x_id;
52 };
53
54 static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
55 {
56         struct axp20x_usb_power *power = devid;
57
58         power_supply_changed(power->supply);
59
60         return IRQ_HANDLED;
61 }
62
63 static int axp20x_usb_power_get_property(struct power_supply *psy,
64         enum power_supply_property psp, union power_supply_propval *val)
65 {
66         struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
67         unsigned int input, v;
68         int ret;
69
70         switch (psp) {
71         case POWER_SUPPLY_PROP_VOLTAGE_MIN:
72                 ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
73                 if (ret)
74                         return ret;
75
76                 val->intval = AXP20X_VBUS_VHOLD_uV(v);
77                 return 0;
78         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
79                 ret = axp20x_read_variable_width(power->regmap,
80                                                  AXP20X_VBUS_V_ADC_H, 12);
81                 if (ret < 0)
82                         return ret;
83
84                 val->intval = ret * 1700; /* 1 step = 1.7 mV */
85                 return 0;
86         case POWER_SUPPLY_PROP_CURRENT_MAX:
87                 ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
88                 if (ret)
89                         return ret;
90
91                 switch (v & AXP20X_VBUS_CLIMIT_MASK) {
92                 case AXP20X_VBUC_CLIMIT_100mA:
93                         if (power->axp20x_id == AXP202_ID) {
94                                 val->intval = 100000;
95                         } else {
96                                 val->intval = -1; /* No 100mA limit */
97                         }
98                         break;
99                 case AXP20X_VBUC_CLIMIT_500mA:
100                         val->intval = 500000;
101                         break;
102                 case AXP20X_VBUC_CLIMIT_900mA:
103                         val->intval = 900000;
104                         break;
105                 case AXP20X_VBUC_CLIMIT_NONE:
106                         val->intval = -1;
107                         break;
108                 }
109                 return 0;
110         case POWER_SUPPLY_PROP_CURRENT_NOW:
111                 ret = axp20x_read_variable_width(power->regmap,
112                                                  AXP20X_VBUS_I_ADC_H, 12);
113                 if (ret < 0)
114                         return ret;
115
116                 val->intval = ret * 375; /* 1 step = 0.375 mA */
117                 return 0;
118         default:
119                 break;
120         }
121
122         /* All the properties below need the input-status reg value */
123         ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
124         if (ret)
125                 return ret;
126
127         switch (psp) {
128         case POWER_SUPPLY_PROP_HEALTH:
129                 if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
130                         val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
131                         break;
132                 }
133
134                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
135
136                 if (power->axp20x_id == AXP202_ID) {
137                         ret = regmap_read(power->regmap,
138                                           AXP20X_USB_OTG_STATUS, &v);
139                         if (ret)
140                                 return ret;
141
142                         if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
143                                 val->intval =
144                                         POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
145                 }
146                 break;
147         case POWER_SUPPLY_PROP_PRESENT:
148                 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
149                 break;
150         case POWER_SUPPLY_PROP_ONLINE:
151                 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
152                 break;
153         default:
154                 return -EINVAL;
155         }
156
157         return 0;
158 }
159
160 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
161                                             int intval)
162 {
163         int val;
164
165         switch (intval) {
166         case 4000000:
167         case 4100000:
168         case 4200000:
169         case 4300000:
170         case 4400000:
171         case 4500000:
172         case 4600000:
173         case 4700000:
174                 val = (intval - 4000000) / 100000;
175                 return regmap_update_bits(power->regmap,
176                                           AXP20X_VBUS_IPSOUT_MGMT,
177                                           AXP20X_VBUS_VHOLD_MASK,
178                                           val << AXP20X_VBUS_VHOLD_OFFSET);
179         default:
180                 return -EINVAL;
181         }
182
183         return -EINVAL;
184 }
185
186 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
187                                             int intval)
188 {
189         int val;
190
191         switch (intval) {
192         case 100000:
193                 if (power->axp20x_id == AXP221_ID)
194                         return -EINVAL;
195         case 500000:
196         case 900000:
197                 val = (900000 - intval) / 400000;
198                 return regmap_update_bits(power->regmap,
199                                           AXP20X_VBUS_IPSOUT_MGMT,
200                                           AXP20X_VBUS_CLIMIT_MASK, val);
201         default:
202                 return -EINVAL;
203         }
204
205         return -EINVAL;
206 }
207
208 static int axp20x_usb_power_set_property(struct power_supply *psy,
209                                          enum power_supply_property psp,
210                                          const union power_supply_propval *val)
211 {
212         struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
213
214         switch (psp) {
215         case POWER_SUPPLY_PROP_VOLTAGE_MIN:
216                 return axp20x_usb_power_set_voltage_min(power, val->intval);
217
218         case POWER_SUPPLY_PROP_CURRENT_MAX:
219                 return axp20x_usb_power_set_current_max(power, val->intval);
220
221         default:
222                 return -EINVAL;
223         }
224
225         return -EINVAL;
226 }
227
228 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
229                                            enum power_supply_property psp)
230 {
231         return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
232                psp == POWER_SUPPLY_PROP_CURRENT_MAX;
233 }
234
235 static enum power_supply_property axp20x_usb_power_properties[] = {
236         POWER_SUPPLY_PROP_HEALTH,
237         POWER_SUPPLY_PROP_PRESENT,
238         POWER_SUPPLY_PROP_ONLINE,
239         POWER_SUPPLY_PROP_VOLTAGE_MIN,
240         POWER_SUPPLY_PROP_VOLTAGE_NOW,
241         POWER_SUPPLY_PROP_CURRENT_MAX,
242         POWER_SUPPLY_PROP_CURRENT_NOW,
243 };
244
245 static enum power_supply_property axp22x_usb_power_properties[] = {
246         POWER_SUPPLY_PROP_HEALTH,
247         POWER_SUPPLY_PROP_PRESENT,
248         POWER_SUPPLY_PROP_ONLINE,
249         POWER_SUPPLY_PROP_VOLTAGE_MIN,
250         POWER_SUPPLY_PROP_CURRENT_MAX,
251 };
252
253 static const struct power_supply_desc axp20x_usb_power_desc = {
254         .name = "axp20x-usb",
255         .type = POWER_SUPPLY_TYPE_USB,
256         .properties = axp20x_usb_power_properties,
257         .num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
258         .property_is_writeable = axp20x_usb_power_prop_writeable,
259         .get_property = axp20x_usb_power_get_property,
260         .set_property = axp20x_usb_power_set_property,
261 };
262
263 static const struct power_supply_desc axp22x_usb_power_desc = {
264         .name = "axp20x-usb",
265         .type = POWER_SUPPLY_TYPE_USB,
266         .properties = axp22x_usb_power_properties,
267         .num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
268         .property_is_writeable = axp20x_usb_power_prop_writeable,
269         .get_property = axp20x_usb_power_get_property,
270         .set_property = axp20x_usb_power_set_property,
271 };
272
273 static int axp20x_usb_power_probe(struct platform_device *pdev)
274 {
275         struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
276         struct power_supply_config psy_cfg = {};
277         struct axp20x_usb_power *power;
278         static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN",
279                 "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL };
280         static const char * const axp22x_irq_names[] = {
281                 "VBUS_PLUGIN", "VBUS_REMOVAL", NULL };
282         static const char * const *irq_names;
283         const struct power_supply_desc *usb_power_desc;
284         int i, irq, ret;
285
286         if (!of_device_is_available(pdev->dev.of_node))
287                 return -ENODEV;
288
289         if (!axp20x) {
290                 dev_err(&pdev->dev, "Parent drvdata not set\n");
291                 return -EINVAL;
292         }
293
294         power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
295         if (!power)
296                 return -ENOMEM;
297
298         power->axp20x_id = (int)of_device_get_match_data(&pdev->dev);
299
300         power->np = pdev->dev.of_node;
301         power->regmap = axp20x->regmap;
302
303         if (power->axp20x_id == AXP202_ID) {
304                 /* Enable vbus valid checking */
305                 ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON,
306                                          AXP20X_VBUS_MON_VBUS_VALID,
307                                          AXP20X_VBUS_MON_VBUS_VALID);
308                 if (ret)
309                         return ret;
310
311                 /* Enable vbus voltage and current measurement */
312                 ret = regmap_update_bits(power->regmap, AXP20X_ADC_EN1,
313                         AXP20X_ADC_EN1_VBUS_CURR | AXP20X_ADC_EN1_VBUS_VOLT,
314                         AXP20X_ADC_EN1_VBUS_CURR | AXP20X_ADC_EN1_VBUS_VOLT);
315                 if (ret)
316                         return ret;
317
318                 usb_power_desc = &axp20x_usb_power_desc;
319                 irq_names = axp20x_irq_names;
320         } else if (power->axp20x_id == AXP221_ID) {
321                 usb_power_desc = &axp22x_usb_power_desc;
322                 irq_names = axp22x_irq_names;
323         } else {
324                 dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
325                         axp20x->variant);
326                 return -EINVAL;
327         }
328
329         psy_cfg.of_node = pdev->dev.of_node;
330         psy_cfg.drv_data = power;
331
332         power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
333                                                    &psy_cfg);
334         if (IS_ERR(power->supply))
335                 return PTR_ERR(power->supply);
336
337         /* Request irqs after registering, as irqs may trigger immediately */
338         for (i = 0; irq_names[i]; i++) {
339                 irq = platform_get_irq_byname(pdev, irq_names[i]);
340                 if (irq < 0) {
341                         dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
342                                  irq_names[i], irq);
343                         continue;
344                 }
345                 irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
346                 ret = devm_request_any_context_irq(&pdev->dev, irq,
347                                 axp20x_usb_power_irq, 0, DRVNAME, power);
348                 if (ret < 0)
349                         dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
350                                  irq_names[i], ret);
351         }
352
353         return 0;
354 }
355
356 static const struct of_device_id axp20x_usb_power_match[] = {
357         {
358                 .compatible = "x-powers,axp202-usb-power-supply",
359                 .data = (void *)AXP202_ID,
360         }, {
361                 .compatible = "x-powers,axp221-usb-power-supply",
362                 .data = (void *)AXP221_ID,
363         }, { /* sentinel */ }
364 };
365 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
366
367 static struct platform_driver axp20x_usb_power_driver = {
368         .probe = axp20x_usb_power_probe,
369         .driver = {
370                 .name = DRVNAME,
371                 .of_match_table = axp20x_usb_power_match,
372         },
373 };
374
375 module_platform_driver(axp20x_usb_power_driver);
376
377 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
378 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
379 MODULE_LICENSE("GPL");