]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/power/supply/axp20x_usb_power.c
632a33fe2d54bf22aa3308e0b2ea3b507b3b2dc3
[karo-tx-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         enum axp20x_variants 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 == AXP221_ID)
94                                 val->intval = -1; /* No 100mA limit */
95                         else
96                                 val->intval = 100000;
97                         break;
98                 case AXP20X_VBUC_CLIMIT_500mA:
99                         val->intval = 500000;
100                         break;
101                 case AXP20X_VBUC_CLIMIT_900mA:
102                         val->intval = 900000;
103                         break;
104                 case AXP20X_VBUC_CLIMIT_NONE:
105                         val->intval = -1;
106                         break;
107                 }
108                 return 0;
109         case POWER_SUPPLY_PROP_CURRENT_NOW:
110                 ret = axp20x_read_variable_width(power->regmap,
111                                                  AXP20X_VBUS_I_ADC_H, 12);
112                 if (ret < 0)
113                         return ret;
114
115                 val->intval = ret * 375; /* 1 step = 0.375 mA */
116                 return 0;
117         default:
118                 break;
119         }
120
121         /* All the properties below need the input-status reg value */
122         ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
123         if (ret)
124                 return ret;
125
126         switch (psp) {
127         case POWER_SUPPLY_PROP_HEALTH:
128                 if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
129                         val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
130                         break;
131                 }
132
133                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
134
135                 if (power->axp20x_id == AXP202_ID) {
136                         ret = regmap_read(power->regmap,
137                                           AXP20X_USB_OTG_STATUS, &v);
138                         if (ret)
139                                 return ret;
140
141                         if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
142                                 val->intval =
143                                         POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
144                 }
145                 break;
146         case POWER_SUPPLY_PROP_PRESENT:
147                 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
148                 break;
149         case POWER_SUPPLY_PROP_ONLINE:
150                 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
151                 break;
152         default:
153                 return -EINVAL;
154         }
155
156         return 0;
157 }
158
159 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
160                                             int intval)
161 {
162         int val;
163
164         switch (intval) {
165         case 4000000:
166         case 4100000:
167         case 4200000:
168         case 4300000:
169         case 4400000:
170         case 4500000:
171         case 4600000:
172         case 4700000:
173                 val = (intval - 4000000) / 100000;
174                 return regmap_update_bits(power->regmap,
175                                           AXP20X_VBUS_IPSOUT_MGMT,
176                                           AXP20X_VBUS_VHOLD_MASK,
177                                           val << AXP20X_VBUS_VHOLD_OFFSET);
178         default:
179                 return -EINVAL;
180         }
181
182         return -EINVAL;
183 }
184
185 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
186                                             int intval)
187 {
188         int val;
189
190         switch (intval) {
191         case 100000:
192                 if (power->axp20x_id == AXP221_ID)
193                         return -EINVAL;
194         case 500000:
195         case 900000:
196                 val = (900000 - intval) / 400000;
197                 return regmap_update_bits(power->regmap,
198                                           AXP20X_VBUS_IPSOUT_MGMT,
199                                           AXP20X_VBUS_CLIMIT_MASK, val);
200         default:
201                 return -EINVAL;
202         }
203
204         return -EINVAL;
205 }
206
207 static int axp20x_usb_power_set_property(struct power_supply *psy,
208                                          enum power_supply_property psp,
209                                          const union power_supply_propval *val)
210 {
211         struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
212
213         switch (psp) {
214         case POWER_SUPPLY_PROP_VOLTAGE_MIN:
215                 return axp20x_usb_power_set_voltage_min(power, val->intval);
216
217         case POWER_SUPPLY_PROP_CURRENT_MAX:
218                 return axp20x_usb_power_set_current_max(power, val->intval);
219
220         default:
221                 return -EINVAL;
222         }
223
224         return -EINVAL;
225 }
226
227 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
228                                            enum power_supply_property psp)
229 {
230         return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
231                psp == POWER_SUPPLY_PROP_CURRENT_MAX;
232 }
233
234 static enum power_supply_property axp20x_usb_power_properties[] = {
235         POWER_SUPPLY_PROP_HEALTH,
236         POWER_SUPPLY_PROP_PRESENT,
237         POWER_SUPPLY_PROP_ONLINE,
238         POWER_SUPPLY_PROP_VOLTAGE_MIN,
239         POWER_SUPPLY_PROP_VOLTAGE_NOW,
240         POWER_SUPPLY_PROP_CURRENT_MAX,
241         POWER_SUPPLY_PROP_CURRENT_NOW,
242 };
243
244 static enum power_supply_property axp22x_usb_power_properties[] = {
245         POWER_SUPPLY_PROP_HEALTH,
246         POWER_SUPPLY_PROP_PRESENT,
247         POWER_SUPPLY_PROP_ONLINE,
248         POWER_SUPPLY_PROP_VOLTAGE_MIN,
249         POWER_SUPPLY_PROP_CURRENT_MAX,
250 };
251
252 static const struct power_supply_desc axp20x_usb_power_desc = {
253         .name = "axp20x-usb",
254         .type = POWER_SUPPLY_TYPE_USB,
255         .properties = axp20x_usb_power_properties,
256         .num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
257         .property_is_writeable = axp20x_usb_power_prop_writeable,
258         .get_property = axp20x_usb_power_get_property,
259         .set_property = axp20x_usb_power_set_property,
260 };
261
262 static const struct power_supply_desc axp22x_usb_power_desc = {
263         .name = "axp20x-usb",
264         .type = POWER_SUPPLY_TYPE_USB,
265         .properties = axp22x_usb_power_properties,
266         .num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
267         .property_is_writeable = axp20x_usb_power_prop_writeable,
268         .get_property = axp20x_usb_power_get_property,
269         .set_property = axp20x_usb_power_set_property,
270 };
271
272 static int axp20x_usb_power_probe(struct platform_device *pdev)
273 {
274         struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
275         struct power_supply_config psy_cfg = {};
276         struct axp20x_usb_power *power;
277         static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN",
278                 "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL };
279         static const char * const axp22x_irq_names[] = {
280                 "VBUS_PLUGIN", "VBUS_REMOVAL", NULL };
281         static const char * const *irq_names;
282         const struct power_supply_desc *usb_power_desc;
283         int i, irq, ret;
284
285         if (!of_device_is_available(pdev->dev.of_node))
286                 return -ENODEV;
287
288         if (!axp20x) {
289                 dev_err(&pdev->dev, "Parent drvdata not set\n");
290                 return -EINVAL;
291         }
292
293         power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
294         if (!power)
295                 return -ENOMEM;
296
297         power->axp20x_id = (enum axp20x_variants)of_device_get_match_data(
298                                                                 &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                    power->axp20x_id == AXP223_ID) {
322                 usb_power_desc = &axp22x_usb_power_desc;
323                 irq_names = axp22x_irq_names;
324         } else {
325                 dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
326                         axp20x->variant);
327                 return -EINVAL;
328         }
329
330         psy_cfg.of_node = pdev->dev.of_node;
331         psy_cfg.drv_data = power;
332
333         power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
334                                                    &psy_cfg);
335         if (IS_ERR(power->supply))
336                 return PTR_ERR(power->supply);
337
338         /* Request irqs after registering, as irqs may trigger immediately */
339         for (i = 0; irq_names[i]; i++) {
340                 irq = platform_get_irq_byname(pdev, irq_names[i]);
341                 if (irq < 0) {
342                         dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
343                                  irq_names[i], irq);
344                         continue;
345                 }
346                 irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
347                 ret = devm_request_any_context_irq(&pdev->dev, irq,
348                                 axp20x_usb_power_irq, 0, DRVNAME, power);
349                 if (ret < 0)
350                         dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
351                                  irq_names[i], ret);
352         }
353
354         return 0;
355 }
356
357 static const struct of_device_id axp20x_usb_power_match[] = {
358         {
359                 .compatible = "x-powers,axp202-usb-power-supply",
360                 .data = (void *)AXP202_ID,
361         }, {
362                 .compatible = "x-powers,axp221-usb-power-supply",
363                 .data = (void *)AXP221_ID,
364         }, {
365                 .compatible = "x-powers,axp223-usb-power-supply",
366                 .data = (void *)AXP223_ID,
367         }, { /* sentinel */ }
368 };
369 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
370
371 static struct platform_driver axp20x_usb_power_driver = {
372         .probe = axp20x_usb_power_probe,
373         .driver = {
374                 .name = DRVNAME,
375                 .of_match_table = axp20x_usb_power_match,
376         },
377 };
378
379 module_platform_driver(axp20x_usb_power_driver);
380
381 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
382 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
383 MODULE_LICENSE("GPL");