| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * AXP20x PMIC USB power supply status driver |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com> |
|---|
| 5 | 6 | * 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 | 7 | */ |
|---|
| 12 | 8 | |
|---|
| 9 | +#include <linux/bitops.h> |
|---|
| 13 | 10 | #include <linux/device.h> |
|---|
| 14 | 11 | #include <linux/init.h> |
|---|
| 15 | 12 | #include <linux/interrupt.h> |
|---|
| .. | .. |
|---|
| 19 | 16 | #include <linux/of.h> |
|---|
| 20 | 17 | #include <linux/of_device.h> |
|---|
| 21 | 18 | #include <linux/platform_device.h> |
|---|
| 19 | +#include <linux/pm.h> |
|---|
| 22 | 20 | #include <linux/power_supply.h> |
|---|
| 23 | 21 | #include <linux/regmap.h> |
|---|
| 24 | 22 | #include <linux/slab.h> |
|---|
| 25 | 23 | #include <linux/iio/consumer.h> |
|---|
| 24 | +#include <linux/workqueue.h> |
|---|
| 26 | 25 | |
|---|
| 27 | 26 | #define DRVNAME "axp20x-usb-power-supply" |
|---|
| 28 | 27 | |
|---|
| .. | .. |
|---|
| 31 | 30 | |
|---|
| 32 | 31 | #define AXP20X_USB_STATUS_VBUS_VALID BIT(2) |
|---|
| 33 | 32 | |
|---|
| 33 | +#define AXP20X_VBUS_PATH_SEL BIT(7) |
|---|
| 34 | +#define AXP20X_VBUS_PATH_SEL_OFFSET 7 |
|---|
| 35 | + |
|---|
| 34 | 36 | #define AXP20X_VBUS_VHOLD_uV(b) (4000000 + (((b) >> 3) & 7) * 100000) |
|---|
| 35 | 37 | #define AXP20X_VBUS_VHOLD_MASK GENMASK(5, 3) |
|---|
| 36 | 38 | #define AXP20X_VBUS_VHOLD_OFFSET 3 |
|---|
| 37 | 39 | #define AXP20X_VBUS_CLIMIT_MASK 3 |
|---|
| 38 | | -#define AXP20X_VBUC_CLIMIT_900mA 0 |
|---|
| 39 | | -#define AXP20X_VBUC_CLIMIT_500mA 1 |
|---|
| 40 | | -#define AXP20X_VBUC_CLIMIT_100mA 2 |
|---|
| 41 | | -#define AXP20X_VBUC_CLIMIT_NONE 3 |
|---|
| 40 | +#define AXP20X_VBUS_CLIMIT_900mA 0 |
|---|
| 41 | +#define AXP20X_VBUS_CLIMIT_500mA 1 |
|---|
| 42 | +#define AXP20X_VBUS_CLIMIT_100mA 2 |
|---|
| 43 | +#define AXP20X_VBUS_CLIMIT_NONE 3 |
|---|
| 44 | + |
|---|
| 45 | +#define AXP813_VBUS_CLIMIT_900mA 0 |
|---|
| 46 | +#define AXP813_VBUS_CLIMIT_1500mA 1 |
|---|
| 47 | +#define AXP813_VBUS_CLIMIT_2000mA 2 |
|---|
| 48 | +#define AXP813_VBUS_CLIMIT_2500mA 3 |
|---|
| 42 | 49 | |
|---|
| 43 | 50 | #define AXP20X_ADC_EN1_VBUS_CURR BIT(2) |
|---|
| 44 | 51 | #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3) |
|---|
| 45 | 52 | |
|---|
| 46 | 53 | #define AXP20X_VBUS_MON_VBUS_VALID BIT(3) |
|---|
| 47 | 54 | |
|---|
| 55 | +#define AXP813_BC_EN BIT(0) |
|---|
| 56 | + |
|---|
| 57 | +/* |
|---|
| 58 | + * Note do not raise the debounce time, we must report Vusb high within |
|---|
| 59 | + * 100ms otherwise we get Vbus errors in musb. |
|---|
| 60 | + */ |
|---|
| 61 | +#define DEBOUNCE_TIME msecs_to_jiffies(50) |
|---|
| 62 | + |
|---|
| 48 | 63 | struct axp20x_usb_power { |
|---|
| 49 | | - struct device_node *np; |
|---|
| 50 | 64 | struct regmap *regmap; |
|---|
| 51 | 65 | struct power_supply *supply; |
|---|
| 52 | 66 | enum axp20x_variants axp20x_id; |
|---|
| 53 | 67 | struct iio_channel *vbus_v; |
|---|
| 54 | 68 | struct iio_channel *vbus_i; |
|---|
| 69 | + struct delayed_work vbus_detect; |
|---|
| 70 | + unsigned int old_status; |
|---|
| 71 | + unsigned int online; |
|---|
| 72 | + unsigned int num_irqs; |
|---|
| 73 | + unsigned int irqs[]; |
|---|
| 55 | 74 | }; |
|---|
| 75 | + |
|---|
| 76 | +static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power *power) |
|---|
| 77 | +{ |
|---|
| 78 | + /* |
|---|
| 79 | + * Polling is only necessary while VBUS is offline. While online, a |
|---|
| 80 | + * present->absent transition implies an online->offline transition |
|---|
| 81 | + * and will trigger the VBUS_REMOVAL IRQ. |
|---|
| 82 | + */ |
|---|
| 83 | + if (power->axp20x_id >= AXP221_ID && !power->online) |
|---|
| 84 | + return true; |
|---|
| 85 | + |
|---|
| 86 | + return false; |
|---|
| 87 | +} |
|---|
| 56 | 88 | |
|---|
| 57 | 89 | static irqreturn_t axp20x_usb_power_irq(int irq, void *devid) |
|---|
| 58 | 90 | { |
|---|
| .. | .. |
|---|
| 60 | 92 | |
|---|
| 61 | 93 | power_supply_changed(power->supply); |
|---|
| 62 | 94 | |
|---|
| 95 | + mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME); |
|---|
| 96 | + |
|---|
| 63 | 97 | return IRQ_HANDLED; |
|---|
| 98 | +} |
|---|
| 99 | + |
|---|
| 100 | +static void axp20x_usb_power_poll_vbus(struct work_struct *work) |
|---|
| 101 | +{ |
|---|
| 102 | + struct axp20x_usb_power *power = |
|---|
| 103 | + container_of(work, struct axp20x_usb_power, vbus_detect.work); |
|---|
| 104 | + unsigned int val; |
|---|
| 105 | + int ret; |
|---|
| 106 | + |
|---|
| 107 | + ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &val); |
|---|
| 108 | + if (ret) |
|---|
| 109 | + goto out; |
|---|
| 110 | + |
|---|
| 111 | + val &= (AXP20X_PWR_STATUS_VBUS_PRESENT | AXP20X_PWR_STATUS_VBUS_USED); |
|---|
| 112 | + if (val != power->old_status) |
|---|
| 113 | + power_supply_changed(power->supply); |
|---|
| 114 | + |
|---|
| 115 | + power->old_status = val; |
|---|
| 116 | + power->online = val & AXP20X_PWR_STATUS_VBUS_USED; |
|---|
| 117 | + |
|---|
| 118 | +out: |
|---|
| 119 | + if (axp20x_usb_vbus_needs_polling(power)) |
|---|
| 120 | + mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME); |
|---|
| 121 | +} |
|---|
| 122 | + |
|---|
| 123 | +static int axp20x_get_current_max(struct axp20x_usb_power *power, int *val) |
|---|
| 124 | +{ |
|---|
| 125 | + unsigned int v; |
|---|
| 126 | + int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v); |
|---|
| 127 | + |
|---|
| 128 | + if (ret) |
|---|
| 129 | + return ret; |
|---|
| 130 | + |
|---|
| 131 | + switch (v & AXP20X_VBUS_CLIMIT_MASK) { |
|---|
| 132 | + case AXP20X_VBUS_CLIMIT_100mA: |
|---|
| 133 | + if (power->axp20x_id == AXP221_ID) |
|---|
| 134 | + *val = -1; /* No 100mA limit */ |
|---|
| 135 | + else |
|---|
| 136 | + *val = 100000; |
|---|
| 137 | + break; |
|---|
| 138 | + case AXP20X_VBUS_CLIMIT_500mA: |
|---|
| 139 | + *val = 500000; |
|---|
| 140 | + break; |
|---|
| 141 | + case AXP20X_VBUS_CLIMIT_900mA: |
|---|
| 142 | + *val = 900000; |
|---|
| 143 | + break; |
|---|
| 144 | + case AXP20X_VBUS_CLIMIT_NONE: |
|---|
| 145 | + *val = -1; |
|---|
| 146 | + break; |
|---|
| 147 | + } |
|---|
| 148 | + |
|---|
| 149 | + return 0; |
|---|
| 150 | +} |
|---|
| 151 | + |
|---|
| 152 | +static int axp813_get_current_max(struct axp20x_usb_power *power, int *val) |
|---|
| 153 | +{ |
|---|
| 154 | + unsigned int v; |
|---|
| 155 | + int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v); |
|---|
| 156 | + |
|---|
| 157 | + if (ret) |
|---|
| 158 | + return ret; |
|---|
| 159 | + |
|---|
| 160 | + switch (v & AXP20X_VBUS_CLIMIT_MASK) { |
|---|
| 161 | + case AXP813_VBUS_CLIMIT_900mA: |
|---|
| 162 | + *val = 900000; |
|---|
| 163 | + break; |
|---|
| 164 | + case AXP813_VBUS_CLIMIT_1500mA: |
|---|
| 165 | + *val = 1500000; |
|---|
| 166 | + break; |
|---|
| 167 | + case AXP813_VBUS_CLIMIT_2000mA: |
|---|
| 168 | + *val = 2000000; |
|---|
| 169 | + break; |
|---|
| 170 | + case AXP813_VBUS_CLIMIT_2500mA: |
|---|
| 171 | + *val = 2500000; |
|---|
| 172 | + break; |
|---|
| 173 | + } |
|---|
| 174 | + return 0; |
|---|
| 64 | 175 | } |
|---|
| 65 | 176 | |
|---|
| 66 | 177 | static int axp20x_usb_power_get_property(struct power_supply *psy, |
|---|
| .. | .. |
|---|
| 101 | 212 | val->intval = ret * 1700; /* 1 step = 1.7 mV */ |
|---|
| 102 | 213 | return 0; |
|---|
| 103 | 214 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
|---|
| 104 | | - ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v); |
|---|
| 105 | | - if (ret) |
|---|
| 106 | | - return ret; |
|---|
| 107 | | - |
|---|
| 108 | | - switch (v & AXP20X_VBUS_CLIMIT_MASK) { |
|---|
| 109 | | - case AXP20X_VBUC_CLIMIT_100mA: |
|---|
| 110 | | - if (power->axp20x_id == AXP221_ID) |
|---|
| 111 | | - val->intval = -1; /* No 100mA limit */ |
|---|
| 112 | | - else |
|---|
| 113 | | - val->intval = 100000; |
|---|
| 114 | | - break; |
|---|
| 115 | | - case AXP20X_VBUC_CLIMIT_500mA: |
|---|
| 116 | | - val->intval = 500000; |
|---|
| 117 | | - break; |
|---|
| 118 | | - case AXP20X_VBUC_CLIMIT_900mA: |
|---|
| 119 | | - val->intval = 900000; |
|---|
| 120 | | - break; |
|---|
| 121 | | - case AXP20X_VBUC_CLIMIT_NONE: |
|---|
| 122 | | - val->intval = -1; |
|---|
| 123 | | - break; |
|---|
| 124 | | - } |
|---|
| 125 | | - return 0; |
|---|
| 215 | + if (power->axp20x_id == AXP813_ID) |
|---|
| 216 | + return axp813_get_current_max(power, &val->intval); |
|---|
| 217 | + return axp20x_get_current_max(power, &val->intval); |
|---|
| 126 | 218 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
|---|
| 127 | 219 | if (IS_ENABLED(CONFIG_AXP20X_ADC)) { |
|---|
| 128 | 220 | ret = iio_read_channel_processed(power->vbus_i, |
|---|
| .. | .. |
|---|
| 187 | 279 | return 0; |
|---|
| 188 | 280 | } |
|---|
| 189 | 281 | |
|---|
| 282 | +static int axp813_usb_power_set_online(struct axp20x_usb_power *power, |
|---|
| 283 | + int intval) |
|---|
| 284 | +{ |
|---|
| 285 | + int val = !intval << AXP20X_VBUS_PATH_SEL_OFFSET; |
|---|
| 286 | + |
|---|
| 287 | + return regmap_update_bits(power->regmap, |
|---|
| 288 | + AXP20X_VBUS_IPSOUT_MGMT, |
|---|
| 289 | + AXP20X_VBUS_PATH_SEL, val); |
|---|
| 290 | +} |
|---|
| 291 | + |
|---|
| 190 | 292 | static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power, |
|---|
| 191 | 293 | int intval) |
|---|
| 192 | 294 | { |
|---|
| .. | .. |
|---|
| 213 | 315 | return -EINVAL; |
|---|
| 214 | 316 | } |
|---|
| 215 | 317 | |
|---|
| 318 | +static int axp813_usb_power_set_current_max(struct axp20x_usb_power *power, |
|---|
| 319 | + int intval) |
|---|
| 320 | +{ |
|---|
| 321 | + int val; |
|---|
| 322 | + |
|---|
| 323 | + switch (intval) { |
|---|
| 324 | + case 900000: |
|---|
| 325 | + return regmap_update_bits(power->regmap, |
|---|
| 326 | + AXP20X_VBUS_IPSOUT_MGMT, |
|---|
| 327 | + AXP20X_VBUS_CLIMIT_MASK, |
|---|
| 328 | + AXP813_VBUS_CLIMIT_900mA); |
|---|
| 329 | + case 1500000: |
|---|
| 330 | + case 2000000: |
|---|
| 331 | + case 2500000: |
|---|
| 332 | + val = (intval - 1000000) / 500000; |
|---|
| 333 | + return regmap_update_bits(power->regmap, |
|---|
| 334 | + AXP20X_VBUS_IPSOUT_MGMT, |
|---|
| 335 | + AXP20X_VBUS_CLIMIT_MASK, val); |
|---|
| 336 | + default: |
|---|
| 337 | + return -EINVAL; |
|---|
| 338 | + } |
|---|
| 339 | + |
|---|
| 340 | + return -EINVAL; |
|---|
| 341 | +} |
|---|
| 342 | + |
|---|
| 216 | 343 | static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power, |
|---|
| 217 | 344 | int intval) |
|---|
| 218 | 345 | { |
|---|
| .. | .. |
|---|
| 222 | 349 | case 100000: |
|---|
| 223 | 350 | if (power->axp20x_id == AXP221_ID) |
|---|
| 224 | 351 | return -EINVAL; |
|---|
| 225 | | - /* fall through */ |
|---|
| 352 | + fallthrough; |
|---|
| 226 | 353 | case 500000: |
|---|
| 227 | 354 | case 900000: |
|---|
| 228 | 355 | val = (900000 - intval) / 400000; |
|---|
| .. | .. |
|---|
| 243 | 370 | struct axp20x_usb_power *power = power_supply_get_drvdata(psy); |
|---|
| 244 | 371 | |
|---|
| 245 | 372 | switch (psp) { |
|---|
| 373 | + case POWER_SUPPLY_PROP_ONLINE: |
|---|
| 374 | + if (power->axp20x_id != AXP813_ID) |
|---|
| 375 | + return -EINVAL; |
|---|
| 376 | + return axp813_usb_power_set_online(power, val->intval); |
|---|
| 377 | + |
|---|
| 246 | 378 | case POWER_SUPPLY_PROP_VOLTAGE_MIN: |
|---|
| 247 | 379 | return axp20x_usb_power_set_voltage_min(power, val->intval); |
|---|
| 248 | 380 | |
|---|
| 249 | 381 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
|---|
| 382 | + if (power->axp20x_id == AXP813_ID) |
|---|
| 383 | + return axp813_usb_power_set_current_max(power, |
|---|
| 384 | + val->intval); |
|---|
| 250 | 385 | return axp20x_usb_power_set_current_max(power, val->intval); |
|---|
| 251 | 386 | |
|---|
| 252 | 387 | default: |
|---|
| .. | .. |
|---|
| 259 | 394 | static int axp20x_usb_power_prop_writeable(struct power_supply *psy, |
|---|
| 260 | 395 | enum power_supply_property psp) |
|---|
| 261 | 396 | { |
|---|
| 397 | + struct axp20x_usb_power *power = power_supply_get_drvdata(psy); |
|---|
| 398 | + |
|---|
| 399 | + /* |
|---|
| 400 | + * The VBUS path select flag works differently on on AXP288 and newer: |
|---|
| 401 | + * - On AXP20x and AXP22x, the flag enables VBUS (ignoring N_VBUSEN). |
|---|
| 402 | + * - On AXP288 and AXP8xx, the flag disables VBUS (ignoring N_VBUSEN). |
|---|
| 403 | + * We only expose the control on variants where it can be used to force |
|---|
| 404 | + * the VBUS input offline. |
|---|
| 405 | + */ |
|---|
| 406 | + if (psp == POWER_SUPPLY_PROP_ONLINE) |
|---|
| 407 | + return power->axp20x_id == AXP813_ID; |
|---|
| 408 | + |
|---|
| 262 | 409 | return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN || |
|---|
| 263 | 410 | psp == POWER_SUPPLY_PROP_CURRENT_MAX; |
|---|
| 264 | 411 | } |
|---|
| .. | .. |
|---|
| 301 | 448 | .set_property = axp20x_usb_power_set_property, |
|---|
| 302 | 449 | }; |
|---|
| 303 | 450 | |
|---|
| 451 | +static const char * const axp20x_irq_names[] = { |
|---|
| 452 | + "VBUS_PLUGIN", |
|---|
| 453 | + "VBUS_REMOVAL", |
|---|
| 454 | + "VBUS_VALID", |
|---|
| 455 | + "VBUS_NOT_VALID", |
|---|
| 456 | +}; |
|---|
| 457 | + |
|---|
| 458 | +static const char * const axp22x_irq_names[] = { |
|---|
| 459 | + "VBUS_PLUGIN", |
|---|
| 460 | + "VBUS_REMOVAL", |
|---|
| 461 | +}; |
|---|
| 462 | + |
|---|
| 463 | +struct axp_data { |
|---|
| 464 | + const struct power_supply_desc *power_desc; |
|---|
| 465 | + const char * const *irq_names; |
|---|
| 466 | + unsigned int num_irq_names; |
|---|
| 467 | + enum axp20x_variants axp20x_id; |
|---|
| 468 | +}; |
|---|
| 469 | + |
|---|
| 470 | +static const struct axp_data axp202_data = { |
|---|
| 471 | + .power_desc = &axp20x_usb_power_desc, |
|---|
| 472 | + .irq_names = axp20x_irq_names, |
|---|
| 473 | + .num_irq_names = ARRAY_SIZE(axp20x_irq_names), |
|---|
| 474 | + .axp20x_id = AXP202_ID, |
|---|
| 475 | +}; |
|---|
| 476 | + |
|---|
| 477 | +static const struct axp_data axp221_data = { |
|---|
| 478 | + .power_desc = &axp22x_usb_power_desc, |
|---|
| 479 | + .irq_names = axp22x_irq_names, |
|---|
| 480 | + .num_irq_names = ARRAY_SIZE(axp22x_irq_names), |
|---|
| 481 | + .axp20x_id = AXP221_ID, |
|---|
| 482 | +}; |
|---|
| 483 | + |
|---|
| 484 | +static const struct axp_data axp223_data = { |
|---|
| 485 | + .power_desc = &axp22x_usb_power_desc, |
|---|
| 486 | + .irq_names = axp22x_irq_names, |
|---|
| 487 | + .num_irq_names = ARRAY_SIZE(axp22x_irq_names), |
|---|
| 488 | + .axp20x_id = AXP223_ID, |
|---|
| 489 | +}; |
|---|
| 490 | + |
|---|
| 491 | +static const struct axp_data axp813_data = { |
|---|
| 492 | + .power_desc = &axp22x_usb_power_desc, |
|---|
| 493 | + .irq_names = axp22x_irq_names, |
|---|
| 494 | + .num_irq_names = ARRAY_SIZE(axp22x_irq_names), |
|---|
| 495 | + .axp20x_id = AXP813_ID, |
|---|
| 496 | +}; |
|---|
| 497 | + |
|---|
| 498 | +#ifdef CONFIG_PM_SLEEP |
|---|
| 499 | +static int axp20x_usb_power_suspend(struct device *dev) |
|---|
| 500 | +{ |
|---|
| 501 | + struct axp20x_usb_power *power = dev_get_drvdata(dev); |
|---|
| 502 | + int i = 0; |
|---|
| 503 | + |
|---|
| 504 | + /* |
|---|
| 505 | + * Allow wake via VBUS_PLUGIN only. |
|---|
| 506 | + * |
|---|
| 507 | + * As nested threaded IRQs are not automatically disabled during |
|---|
| 508 | + * suspend, we must explicitly disable the remainder of the IRQs. |
|---|
| 509 | + */ |
|---|
| 510 | + if (device_may_wakeup(&power->supply->dev)) |
|---|
| 511 | + enable_irq_wake(power->irqs[i++]); |
|---|
| 512 | + while (i < power->num_irqs) |
|---|
| 513 | + disable_irq(power->irqs[i++]); |
|---|
| 514 | + |
|---|
| 515 | + return 0; |
|---|
| 516 | +} |
|---|
| 517 | + |
|---|
| 518 | +static int axp20x_usb_power_resume(struct device *dev) |
|---|
| 519 | +{ |
|---|
| 520 | + struct axp20x_usb_power *power = dev_get_drvdata(dev); |
|---|
| 521 | + int i = 0; |
|---|
| 522 | + |
|---|
| 523 | + if (device_may_wakeup(&power->supply->dev)) |
|---|
| 524 | + disable_irq_wake(power->irqs[i++]); |
|---|
| 525 | + while (i < power->num_irqs) |
|---|
| 526 | + enable_irq(power->irqs[i++]); |
|---|
| 527 | + |
|---|
| 528 | + mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME); |
|---|
| 529 | + |
|---|
| 530 | + return 0; |
|---|
| 531 | +} |
|---|
| 532 | +#endif |
|---|
| 533 | + |
|---|
| 534 | +static SIMPLE_DEV_PM_OPS(axp20x_usb_power_pm_ops, axp20x_usb_power_suspend, |
|---|
| 535 | + axp20x_usb_power_resume); |
|---|
| 536 | + |
|---|
| 304 | 537 | static int configure_iio_channels(struct platform_device *pdev, |
|---|
| 305 | 538 | struct axp20x_usb_power *power) |
|---|
| 306 | 539 | { |
|---|
| .. | .. |
|---|
| 336 | 569 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); |
|---|
| 337 | 570 | struct power_supply_config psy_cfg = {}; |
|---|
| 338 | 571 | struct axp20x_usb_power *power; |
|---|
| 339 | | - static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN", |
|---|
| 340 | | - "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL }; |
|---|
| 341 | | - static const char * const axp22x_irq_names[] = { |
|---|
| 342 | | - "VBUS_PLUGIN", "VBUS_REMOVAL", NULL }; |
|---|
| 343 | | - const char * const *irq_names; |
|---|
| 344 | | - const struct power_supply_desc *usb_power_desc; |
|---|
| 572 | + const struct axp_data *axp_data; |
|---|
| 345 | 573 | int i, irq, ret; |
|---|
| 346 | 574 | |
|---|
| 347 | 575 | if (!of_device_is_available(pdev->dev.of_node)) |
|---|
| .. | .. |
|---|
| 352 | 580 | return -EINVAL; |
|---|
| 353 | 581 | } |
|---|
| 354 | 582 | |
|---|
| 355 | | - power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL); |
|---|
| 583 | + axp_data = of_device_get_match_data(&pdev->dev); |
|---|
| 584 | + |
|---|
| 585 | + power = devm_kzalloc(&pdev->dev, |
|---|
| 586 | + struct_size(power, irqs, axp_data->num_irq_names), |
|---|
| 587 | + GFP_KERNEL); |
|---|
| 356 | 588 | if (!power) |
|---|
| 357 | 589 | return -ENOMEM; |
|---|
| 358 | 590 | |
|---|
| 359 | | - power->axp20x_id = (enum axp20x_variants)of_device_get_match_data( |
|---|
| 360 | | - &pdev->dev); |
|---|
| 591 | + platform_set_drvdata(pdev, power); |
|---|
| 361 | 592 | |
|---|
| 362 | | - power->np = pdev->dev.of_node; |
|---|
| 593 | + power->axp20x_id = axp_data->axp20x_id; |
|---|
| 363 | 594 | power->regmap = axp20x->regmap; |
|---|
| 595 | + power->num_irqs = axp_data->num_irq_names; |
|---|
| 596 | + INIT_DELAYED_WORK(&power->vbus_detect, axp20x_usb_power_poll_vbus); |
|---|
| 364 | 597 | |
|---|
| 365 | 598 | if (power->axp20x_id == AXP202_ID) { |
|---|
| 366 | 599 | /* Enable vbus valid checking */ |
|---|
| .. | .. |
|---|
| 377 | 610 | |
|---|
| 378 | 611 | if (ret) |
|---|
| 379 | 612 | return ret; |
|---|
| 613 | + } |
|---|
| 380 | 614 | |
|---|
| 381 | | - usb_power_desc = &axp20x_usb_power_desc; |
|---|
| 382 | | - irq_names = axp20x_irq_names; |
|---|
| 383 | | - } else if (power->axp20x_id == AXP221_ID || |
|---|
| 384 | | - power->axp20x_id == AXP223_ID) { |
|---|
| 385 | | - usb_power_desc = &axp22x_usb_power_desc; |
|---|
| 386 | | - irq_names = axp22x_irq_names; |
|---|
| 387 | | - } else { |
|---|
| 388 | | - dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n", |
|---|
| 389 | | - axp20x->variant); |
|---|
| 390 | | - return -EINVAL; |
|---|
| 615 | + if (power->axp20x_id == AXP813_ID) { |
|---|
| 616 | + /* Enable USB Battery Charging specification detection */ |
|---|
| 617 | + regmap_update_bits(axp20x->regmap, AXP288_BC_GLOBAL, |
|---|
| 618 | + AXP813_BC_EN, AXP813_BC_EN); |
|---|
| 391 | 619 | } |
|---|
| 392 | 620 | |
|---|
| 393 | 621 | psy_cfg.of_node = pdev->dev.of_node; |
|---|
| 394 | 622 | psy_cfg.drv_data = power; |
|---|
| 395 | 623 | |
|---|
| 396 | | - power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc, |
|---|
| 624 | + power->supply = devm_power_supply_register(&pdev->dev, |
|---|
| 625 | + axp_data->power_desc, |
|---|
| 397 | 626 | &psy_cfg); |
|---|
| 398 | 627 | if (IS_ERR(power->supply)) |
|---|
| 399 | 628 | return PTR_ERR(power->supply); |
|---|
| 400 | 629 | |
|---|
| 401 | 630 | /* Request irqs after registering, as irqs may trigger immediately */ |
|---|
| 402 | | - for (i = 0; irq_names[i]; i++) { |
|---|
| 403 | | - irq = platform_get_irq_byname(pdev, irq_names[i]); |
|---|
| 631 | + for (i = 0; i < axp_data->num_irq_names; i++) { |
|---|
| 632 | + irq = platform_get_irq_byname(pdev, axp_data->irq_names[i]); |
|---|
| 404 | 633 | if (irq < 0) { |
|---|
| 405 | | - dev_warn(&pdev->dev, "No IRQ for %s: %d\n", |
|---|
| 406 | | - irq_names[i], irq); |
|---|
| 407 | | - continue; |
|---|
| 634 | + dev_err(&pdev->dev, "No IRQ for %s: %d\n", |
|---|
| 635 | + axp_data->irq_names[i], irq); |
|---|
| 636 | + return irq; |
|---|
| 408 | 637 | } |
|---|
| 409 | | - irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq); |
|---|
| 410 | | - ret = devm_request_any_context_irq(&pdev->dev, irq, |
|---|
| 411 | | - axp20x_usb_power_irq, 0, DRVNAME, power); |
|---|
| 412 | | - if (ret < 0) |
|---|
| 413 | | - dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n", |
|---|
| 414 | | - irq_names[i], ret); |
|---|
| 638 | + power->irqs[i] = regmap_irq_get_virq(axp20x->regmap_irqc, irq); |
|---|
| 639 | + ret = devm_request_any_context_irq(&pdev->dev, power->irqs[i], |
|---|
| 640 | + axp20x_usb_power_irq, 0, |
|---|
| 641 | + DRVNAME, power); |
|---|
| 642 | + if (ret < 0) { |
|---|
| 643 | + dev_err(&pdev->dev, "Error requesting %s IRQ: %d\n", |
|---|
| 644 | + axp_data->irq_names[i], ret); |
|---|
| 645 | + return ret; |
|---|
| 646 | + } |
|---|
| 415 | 647 | } |
|---|
| 648 | + |
|---|
| 649 | + if (axp20x_usb_vbus_needs_polling(power)) |
|---|
| 650 | + queue_delayed_work(system_wq, &power->vbus_detect, 0); |
|---|
| 651 | + |
|---|
| 652 | + return 0; |
|---|
| 653 | +} |
|---|
| 654 | + |
|---|
| 655 | +static int axp20x_usb_power_remove(struct platform_device *pdev) |
|---|
| 656 | +{ |
|---|
| 657 | + struct axp20x_usb_power *power = platform_get_drvdata(pdev); |
|---|
| 658 | + |
|---|
| 659 | + cancel_delayed_work_sync(&power->vbus_detect); |
|---|
| 416 | 660 | |
|---|
| 417 | 661 | return 0; |
|---|
| 418 | 662 | } |
|---|
| .. | .. |
|---|
| 420 | 664 | static const struct of_device_id axp20x_usb_power_match[] = { |
|---|
| 421 | 665 | { |
|---|
| 422 | 666 | .compatible = "x-powers,axp202-usb-power-supply", |
|---|
| 423 | | - .data = (void *)AXP202_ID, |
|---|
| 667 | + .data = &axp202_data, |
|---|
| 424 | 668 | }, { |
|---|
| 425 | 669 | .compatible = "x-powers,axp221-usb-power-supply", |
|---|
| 426 | | - .data = (void *)AXP221_ID, |
|---|
| 670 | + .data = &axp221_data, |
|---|
| 427 | 671 | }, { |
|---|
| 428 | 672 | .compatible = "x-powers,axp223-usb-power-supply", |
|---|
| 429 | | - .data = (void *)AXP223_ID, |
|---|
| 673 | + .data = &axp223_data, |
|---|
| 674 | + }, { |
|---|
| 675 | + .compatible = "x-powers,axp813-usb-power-supply", |
|---|
| 676 | + .data = &axp813_data, |
|---|
| 430 | 677 | }, { /* sentinel */ } |
|---|
| 431 | 678 | }; |
|---|
| 432 | 679 | MODULE_DEVICE_TABLE(of, axp20x_usb_power_match); |
|---|
| 433 | 680 | |
|---|
| 434 | 681 | static struct platform_driver axp20x_usb_power_driver = { |
|---|
| 435 | 682 | .probe = axp20x_usb_power_probe, |
|---|
| 683 | + .remove = axp20x_usb_power_remove, |
|---|
| 436 | 684 | .driver = { |
|---|
| 437 | | - .name = DRVNAME, |
|---|
| 438 | | - .of_match_table = axp20x_usb_power_match, |
|---|
| 685 | + .name = DRVNAME, |
|---|
| 686 | + .of_match_table = axp20x_usb_power_match, |
|---|
| 687 | + .pm = &axp20x_usb_power_pm_ops, |
|---|
| 439 | 688 | }, |
|---|
| 440 | 689 | }; |
|---|
| 441 | 690 | |
|---|