2 * AXP20x PMIC USB power supply status driver
4 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
5 * Copyright (C) 2014 Bruno Prémont <bonbons@linux-vserver.org>
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.
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>
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>
26 #define DRVNAME "axp20x-usb-power-supply"
28 #define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5)
29 #define AXP20X_PWR_STATUS_VBUS_USED BIT(4)
31 #define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
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
42 #define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
43 #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
45 #define AXP20X_VBUS_MON_VBUS_VALID BIT(3)
47 struct axp20x_usb_power {
48 struct device_node *np;
49 struct regmap *regmap;
50 struct power_supply *supply;
54 static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
56 struct axp20x_usb_power *power = devid;
58 power_supply_changed(power->supply);
63 static int axp20x_usb_power_get_property(struct power_supply *psy,
64 enum power_supply_property psp, union power_supply_propval *val)
66 struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
67 unsigned int input, v;
71 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
72 ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
76 val->intval = AXP20X_VBUS_VHOLD_uV(v);
78 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
79 ret = axp20x_read_variable_width(power->regmap,
80 AXP20X_VBUS_V_ADC_H, 12);
84 val->intval = ret * 1700; /* 1 step = 1.7 mV */
86 case POWER_SUPPLY_PROP_CURRENT_MAX:
87 ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
91 switch (v & AXP20X_VBUS_CLIMIT_MASK) {
92 case AXP20X_VBUC_CLIMIT_100mA:
93 if (power->axp20x_id == AXP202_ID) {
96 val->intval = -1; /* No 100mA limit */
99 case AXP20X_VBUC_CLIMIT_500mA:
100 val->intval = 500000;
102 case AXP20X_VBUC_CLIMIT_900mA:
103 val->intval = 900000;
105 case AXP20X_VBUC_CLIMIT_NONE:
110 case POWER_SUPPLY_PROP_CURRENT_NOW:
111 ret = axp20x_read_variable_width(power->regmap,
112 AXP20X_VBUS_I_ADC_H, 12);
116 val->intval = ret * 375; /* 1 step = 0.375 mA */
122 /* All the properties below need the input-status reg value */
123 ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
128 case POWER_SUPPLY_PROP_HEALTH:
129 if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
130 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
134 val->intval = POWER_SUPPLY_HEALTH_GOOD;
136 if (power->axp20x_id == AXP202_ID) {
137 ret = regmap_read(power->regmap,
138 AXP20X_USB_OTG_STATUS, &v);
142 if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
144 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
147 case POWER_SUPPLY_PROP_PRESENT:
148 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
150 case POWER_SUPPLY_PROP_ONLINE:
151 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
160 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
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);
186 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
193 if (power->axp20x_id == AXP221_ID)
197 val = (900000 - intval) / 400000;
198 return regmap_update_bits(power->regmap,
199 AXP20X_VBUS_IPSOUT_MGMT,
200 AXP20X_VBUS_CLIMIT_MASK, val);
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)
212 struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
215 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
216 return axp20x_usb_power_set_voltage_min(power, val->intval);
218 case POWER_SUPPLY_PROP_CURRENT_MAX:
219 return axp20x_usb_power_set_current_max(power, val->intval);
228 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
229 enum power_supply_property psp)
231 return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
232 psp == POWER_SUPPLY_PROP_CURRENT_MAX;
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,
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,
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,
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,
273 static int axp20x_usb_power_probe(struct platform_device *pdev)
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;
286 if (!of_device_is_available(pdev->dev.of_node))
290 dev_err(&pdev->dev, "Parent drvdata not set\n");
294 power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
298 power->axp20x_id = (int)of_device_get_match_data(&pdev->dev);
300 power->np = pdev->dev.of_node;
301 power->regmap = axp20x->regmap;
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);
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);
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;
324 dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
329 psy_cfg.of_node = pdev->dev.of_node;
330 psy_cfg.drv_data = power;
332 power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
334 if (IS_ERR(power->supply))
335 return PTR_ERR(power->supply);
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]);
341 dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
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);
349 dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
356 static const struct of_device_id axp20x_usb_power_match[] = {
358 .compatible = "x-powers,axp202-usb-power-supply",
359 .data = (void *)AXP202_ID,
361 .compatible = "x-powers,axp221-usb-power-supply",
362 .data = (void *)AXP221_ID,
363 }, { /* sentinel */ }
365 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
367 static struct platform_driver axp20x_usb_power_driver = {
368 .probe = axp20x_usb_power_probe,
371 .of_match_table = axp20x_usb_power_match,
375 module_platform_driver(axp20x_usb_power_driver);
377 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
378 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
379 MODULE_LICENSE("GPL");