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;
51 enum axp20x_variants axp20x_id;
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 == AXP221_ID)
94 val->intval = -1; /* No 100mA limit */
98 case AXP20X_VBUC_CLIMIT_500mA:
101 case AXP20X_VBUC_CLIMIT_900mA:
102 val->intval = 900000;
104 case AXP20X_VBUC_CLIMIT_NONE:
109 case POWER_SUPPLY_PROP_CURRENT_NOW:
110 ret = axp20x_read_variable_width(power->regmap,
111 AXP20X_VBUS_I_ADC_H, 12);
115 val->intval = ret * 375; /* 1 step = 0.375 mA */
121 /* All the properties below need the input-status reg value */
122 ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
127 case POWER_SUPPLY_PROP_HEALTH:
128 if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
129 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
133 val->intval = POWER_SUPPLY_HEALTH_GOOD;
135 if (power->axp20x_id == AXP202_ID) {
136 ret = regmap_read(power->regmap,
137 AXP20X_USB_OTG_STATUS, &v);
141 if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
143 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
146 case POWER_SUPPLY_PROP_PRESENT:
147 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
149 case POWER_SUPPLY_PROP_ONLINE:
150 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
159 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
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);
185 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
192 if (power->axp20x_id == AXP221_ID)
196 val = (900000 - intval) / 400000;
197 return regmap_update_bits(power->regmap,
198 AXP20X_VBUS_IPSOUT_MGMT,
199 AXP20X_VBUS_CLIMIT_MASK, val);
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)
211 struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
214 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
215 return axp20x_usb_power_set_voltage_min(power, val->intval);
217 case POWER_SUPPLY_PROP_CURRENT_MAX:
218 return axp20x_usb_power_set_current_max(power, val->intval);
227 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
228 enum power_supply_property psp)
230 return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
231 psp == POWER_SUPPLY_PROP_CURRENT_MAX;
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,
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,
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,
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,
272 static int axp20x_usb_power_probe(struct platform_device *pdev)
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;
285 if (!of_device_is_available(pdev->dev.of_node))
289 dev_err(&pdev->dev, "Parent drvdata not set\n");
293 power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
297 power->axp20x_id = (enum axp20x_variants)of_device_get_match_data(
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 power->axp20x_id == AXP223_ID) {
322 usb_power_desc = &axp22x_usb_power_desc;
323 irq_names = axp22x_irq_names;
325 dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
330 psy_cfg.of_node = pdev->dev.of_node;
331 psy_cfg.drv_data = power;
333 power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
335 if (IS_ERR(power->supply))
336 return PTR_ERR(power->supply);
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]);
342 dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
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);
350 dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
357 static const struct of_device_id axp20x_usb_power_match[] = {
359 .compatible = "x-powers,axp202-usb-power-supply",
360 .data = (void *)AXP202_ID,
362 .compatible = "x-powers,axp221-usb-power-supply",
363 .data = (void *)AXP221_ID,
365 .compatible = "x-powers,axp223-usb-power-supply",
366 .data = (void *)AXP223_ID,
367 }, { /* sentinel */ }
369 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
371 static struct platform_driver axp20x_usb_power_driver = {
372 .probe = axp20x_usb_power_probe,
375 .of_match_table = axp20x_usb_power_match,
379 module_platform_driver(axp20x_usb_power_driver);
381 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
382 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
383 MODULE_LICENSE("GPL");