Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 1 | /* |
| 2 | * extcon-axp288.c - X-Power AXP288 PMIC extcon cable detection driver |
| 3 | * |
| 4 | * Copyright (C) 2015 Intel Corporation |
| 5 | * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/property.h> |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 24 | #include <linux/notifier.h> |
| 25 | #include <linux/extcon.h> |
| 26 | #include <linux/regmap.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/gpio/consumer.h> |
| 29 | #include <linux/mfd/axp20x.h> |
| 30 | |
| 31 | /* Power source status register */ |
| 32 | #define PS_STAT_VBUS_TRIGGER BIT(0) |
| 33 | #define PS_STAT_BAT_CHRG_DIR BIT(2) |
| 34 | #define PS_STAT_VBUS_ABOVE_VHOLD BIT(3) |
| 35 | #define PS_STAT_VBUS_VALID BIT(4) |
| 36 | #define PS_STAT_VBUS_PRESENT BIT(5) |
| 37 | |
| 38 | /* BC module global register */ |
| 39 | #define BC_GLOBAL_RUN BIT(0) |
| 40 | #define BC_GLOBAL_DET_STAT BIT(2) |
| 41 | #define BC_GLOBAL_DBP_TOUT BIT(3) |
| 42 | #define BC_GLOBAL_VLGC_COM_SEL BIT(4) |
| 43 | #define BC_GLOBAL_DCD_TOUT_MASK (BIT(6)|BIT(5)) |
| 44 | #define BC_GLOBAL_DCD_TOUT_300MS 0 |
| 45 | #define BC_GLOBAL_DCD_TOUT_100MS 1 |
| 46 | #define BC_GLOBAL_DCD_TOUT_500MS 2 |
| 47 | #define BC_GLOBAL_DCD_TOUT_900MS 3 |
| 48 | #define BC_GLOBAL_DCD_DET_SEL BIT(7) |
| 49 | |
| 50 | /* BC module vbus control and status register */ |
| 51 | #define VBUS_CNTL_DPDM_PD_EN BIT(4) |
| 52 | #define VBUS_CNTL_DPDM_FD_EN BIT(5) |
| 53 | #define VBUS_CNTL_FIRST_PO_STAT BIT(6) |
| 54 | |
| 55 | /* BC USB status register */ |
| 56 | #define USB_STAT_BUS_STAT_MASK (BIT(3)|BIT(2)|BIT(1)|BIT(0)) |
| 57 | #define USB_STAT_BUS_STAT_SHIFT 0 |
| 58 | #define USB_STAT_BUS_STAT_ATHD 0 |
| 59 | #define USB_STAT_BUS_STAT_CONN 1 |
| 60 | #define USB_STAT_BUS_STAT_SUSP 2 |
| 61 | #define USB_STAT_BUS_STAT_CONF 3 |
| 62 | #define USB_STAT_USB_SS_MODE BIT(4) |
| 63 | #define USB_STAT_DEAD_BAT_DET BIT(6) |
| 64 | #define USB_STAT_DBP_UNCFG BIT(7) |
| 65 | |
| 66 | /* BC detect status register */ |
| 67 | #define DET_STAT_MASK (BIT(7)|BIT(6)|BIT(5)) |
| 68 | #define DET_STAT_SHIFT 5 |
| 69 | #define DET_STAT_SDP 1 |
| 70 | #define DET_STAT_CDP 2 |
| 71 | #define DET_STAT_DCP 3 |
| 72 | |
| 73 | /* IRQ enable-1 register */ |
| 74 | #define PWRSRC_IRQ_CFG_MASK (BIT(4)|BIT(3)|BIT(2)) |
| 75 | |
| 76 | /* IRQ enable-6 register */ |
| 77 | #define BC12_IRQ_CFG_MASK BIT(1) |
| 78 | |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 79 | enum axp288_extcon_reg { |
| 80 | AXP288_PS_STAT_REG = 0x00, |
| 81 | AXP288_PS_BOOT_REASON_REG = 0x02, |
| 82 | AXP288_BC_GLOBAL_REG = 0x2c, |
| 83 | AXP288_BC_VBUS_CNTL_REG = 0x2d, |
| 84 | AXP288_BC_USB_STAT_REG = 0x2e, |
| 85 | AXP288_BC_DET_STAT_REG = 0x2f, |
| 86 | AXP288_PWRSRC_IRQ_CFG_REG = 0x40, |
| 87 | AXP288_BC12_IRQ_CFG_REG = 0x45, |
| 88 | }; |
| 89 | |
| 90 | enum axp288_mux_select { |
| 91 | EXTCON_GPIO_MUX_SEL_PMIC = 0, |
| 92 | EXTCON_GPIO_MUX_SEL_SOC, |
| 93 | }; |
| 94 | |
| 95 | enum axp288_extcon_irq { |
| 96 | VBUS_FALLING_IRQ = 0, |
| 97 | VBUS_RISING_IRQ, |
| 98 | MV_CHNG_IRQ, |
| 99 | BC_USB_CHNG_IRQ, |
| 100 | EXTCON_IRQ_END, |
| 101 | }; |
| 102 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 103 | static const unsigned int axp288_extcon_cables[] = { |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 104 | EXTCON_CHG_USB_SDP, |
| 105 | EXTCON_CHG_USB_CDP, |
| 106 | EXTCON_CHG_USB_DCP, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 107 | EXTCON_NONE, |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct axp288_extcon_info { |
| 111 | struct device *dev; |
| 112 | struct regmap *regmap; |
| 113 | struct regmap_irq_chip_data *regmap_irqc; |
Hans de Goede | 7688424 | 2016-12-19 01:13:06 +0100 | [diff] [blame] | 114 | struct gpio_desc *gpio_mux_cntl; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 115 | int irq[EXTCON_IRQ_END]; |
| 116 | struct extcon_dev *edev; |
| 117 | struct notifier_block extcon_nb; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* Power up/down reason string array */ |
| 121 | static char *axp288_pwr_up_down_info[] = { |
| 122 | "Last wake caused by user pressing the power button", |
| 123 | "Last wake caused by a charger insertion", |
| 124 | "Last wake caused by a battery insertion", |
| 125 | "Last wake caused by SOC initiated global reset", |
| 126 | "Last wake caused by cold reset", |
| 127 | "Last shutdown caused by PMIC UVLO threshold", |
| 128 | "Last shutdown caused by SOC initiated cold off", |
| 129 | "Last shutdown caused by user pressing the power button", |
| 130 | NULL, |
| 131 | }; |
| 132 | |
| 133 | /* |
| 134 | * Decode and log the given "reset source indicator" (rsi) |
| 135 | * register and then clear it. |
| 136 | */ |
| 137 | static void axp288_extcon_log_rsi(struct axp288_extcon_info *info) |
| 138 | { |
| 139 | char **rsi; |
| 140 | unsigned int val, i, clear_mask = 0; |
| 141 | int ret; |
| 142 | |
| 143 | ret = regmap_read(info->regmap, AXP288_PS_BOOT_REASON_REG, &val); |
| 144 | for (i = 0, rsi = axp288_pwr_up_down_info; *rsi; rsi++, i++) { |
| 145 | if (val & BIT(i)) { |
| 146 | dev_dbg(info->dev, "%s\n", *rsi); |
| 147 | clear_mask |= BIT(i); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* Clear the register value for next reboot (write 1 to clear bit) */ |
| 152 | regmap_write(info->regmap, AXP288_PS_BOOT_REASON_REG, clear_mask); |
| 153 | } |
| 154 | |
| 155 | static int axp288_handle_chrg_det_event(struct axp288_extcon_info *info) |
| 156 | { |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 157 | static unsigned int cable; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 158 | int ret, stat, cfg, pwr_stat; |
| 159 | u8 chrg_type; |
| 160 | bool vbus_attach = false; |
| 161 | |
| 162 | ret = regmap_read(info->regmap, AXP288_PS_STAT_REG, &pwr_stat); |
| 163 | if (ret < 0) { |
| 164 | dev_err(info->dev, "failed to read vbus status\n"); |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | vbus_attach = (pwr_stat & PS_STAT_VBUS_PRESENT); |
| 169 | if (!vbus_attach) |
Hans de Goede | 3fe1e0e | 2016-12-19 01:13:08 +0100 | [diff] [blame^] | 170 | goto no_vbus; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 171 | |
| 172 | /* Check charger detection completion status */ |
| 173 | ret = regmap_read(info->regmap, AXP288_BC_GLOBAL_REG, &cfg); |
| 174 | if (ret < 0) |
| 175 | goto dev_det_ret; |
| 176 | if (cfg & BC_GLOBAL_DET_STAT) { |
| 177 | dev_dbg(info->dev, "can't complete the charger detection\n"); |
| 178 | goto dev_det_ret; |
| 179 | } |
| 180 | |
| 181 | ret = regmap_read(info->regmap, AXP288_BC_DET_STAT_REG, &stat); |
| 182 | if (ret < 0) |
| 183 | goto dev_det_ret; |
| 184 | |
| 185 | chrg_type = (stat & DET_STAT_MASK) >> DET_STAT_SHIFT; |
| 186 | |
| 187 | switch (chrg_type) { |
| 188 | case DET_STAT_SDP: |
Colin Ian King | 525867db | 2016-08-17 19:02:35 +0100 | [diff] [blame] | 189 | dev_dbg(info->dev, "sdp cable is connected\n"); |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 190 | cable = EXTCON_CHG_USB_SDP; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 191 | break; |
| 192 | case DET_STAT_CDP: |
Colin Ian King | 525867db | 2016-08-17 19:02:35 +0100 | [diff] [blame] | 193 | dev_dbg(info->dev, "cdp cable is connected\n"); |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 194 | cable = EXTCON_CHG_USB_CDP; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 195 | break; |
| 196 | case DET_STAT_DCP: |
Colin Ian King | 525867db | 2016-08-17 19:02:35 +0100 | [diff] [blame] | 197 | dev_dbg(info->dev, "dcp cable is connected\n"); |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 198 | cable = EXTCON_CHG_USB_DCP; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 199 | break; |
| 200 | default: |
| 201 | dev_warn(info->dev, |
| 202 | "disconnect or unknown or ID event\n"); |
| 203 | } |
| 204 | |
Hans de Goede | 3fe1e0e | 2016-12-19 01:13:08 +0100 | [diff] [blame^] | 205 | no_vbus: |
| 206 | /* |
| 207 | * If VBUS is absent Connect D+/D- lines to PMIC for BC |
| 208 | * detection. Else connect them to SOC for USB communication. |
| 209 | */ |
| 210 | if (info->gpio_mux_cntl) |
| 211 | gpiod_set_value(info->gpio_mux_cntl, |
| 212 | vbus_attach ? EXTCON_GPIO_MUX_SEL_SOC |
| 213 | : EXTCON_GPIO_MUX_SEL_PMIC); |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 214 | |
Hans de Goede | 3fe1e0e | 2016-12-19 01:13:08 +0100 | [diff] [blame^] | 215 | extcon_set_state_sync(info->edev, cable, vbus_attach); |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 216 | |
| 217 | return 0; |
| 218 | |
| 219 | dev_det_ret: |
| 220 | if (ret < 0) |
| 221 | dev_err(info->dev, "failed to detect BC Mod\n"); |
| 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | static irqreturn_t axp288_extcon_isr(int irq, void *data) |
| 227 | { |
| 228 | struct axp288_extcon_info *info = data; |
| 229 | int ret; |
| 230 | |
| 231 | ret = axp288_handle_chrg_det_event(info); |
| 232 | if (ret < 0) |
| 233 | dev_err(info->dev, "failed to handle the interrupt\n"); |
| 234 | |
| 235 | return IRQ_HANDLED; |
| 236 | } |
| 237 | |
| 238 | static void axp288_extcon_enable_irq(struct axp288_extcon_info *info) |
| 239 | { |
| 240 | /* Unmask VBUS interrupt */ |
| 241 | regmap_write(info->regmap, AXP288_PWRSRC_IRQ_CFG_REG, |
| 242 | PWRSRC_IRQ_CFG_MASK); |
| 243 | regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG, |
| 244 | BC_GLOBAL_RUN, 0); |
| 245 | /* Unmask the BC1.2 complete interrupts */ |
| 246 | regmap_write(info->regmap, AXP288_BC12_IRQ_CFG_REG, BC12_IRQ_CFG_MASK); |
| 247 | /* Enable the charger detection logic */ |
| 248 | regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG, |
| 249 | BC_GLOBAL_RUN, BC_GLOBAL_RUN); |
| 250 | } |
| 251 | |
| 252 | static int axp288_extcon_probe(struct platform_device *pdev) |
| 253 | { |
| 254 | struct axp288_extcon_info *info; |
| 255 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); |
Hans de Goede | 7688424 | 2016-12-19 01:13:06 +0100 | [diff] [blame] | 256 | struct axp288_extcon_pdata *pdata = pdev->dev.platform_data; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 257 | int ret, i, pirq, gpio; |
| 258 | |
| 259 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 260 | if (!info) |
| 261 | return -ENOMEM; |
| 262 | |
| 263 | info->dev = &pdev->dev; |
| 264 | info->regmap = axp20x->regmap; |
| 265 | info->regmap_irqc = axp20x->regmap_irqc; |
Hans de Goede | 7688424 | 2016-12-19 01:13:06 +0100 | [diff] [blame] | 266 | if (pdata) |
| 267 | info->gpio_mux_cntl = pdata->gpio_mux_cntl; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 268 | |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 269 | platform_set_drvdata(pdev, info); |
| 270 | |
| 271 | axp288_extcon_log_rsi(info); |
| 272 | |
| 273 | /* Initialize extcon device */ |
| 274 | info->edev = devm_extcon_dev_allocate(&pdev->dev, |
| 275 | axp288_extcon_cables); |
| 276 | if (IS_ERR(info->edev)) { |
| 277 | dev_err(&pdev->dev, "failed to allocate memory for extcon\n"); |
| 278 | return PTR_ERR(info->edev); |
| 279 | } |
| 280 | |
| 281 | /* Register extcon device */ |
| 282 | ret = devm_extcon_dev_register(&pdev->dev, info->edev); |
| 283 | if (ret) { |
| 284 | dev_err(&pdev->dev, "failed to register extcon device\n"); |
| 285 | return ret; |
| 286 | } |
| 287 | |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 288 | /* Set up gpio control for USB Mux */ |
Hans de Goede | 7688424 | 2016-12-19 01:13:06 +0100 | [diff] [blame] | 289 | if (info->gpio_mux_cntl) { |
| 290 | gpio = desc_to_gpio(info->gpio_mux_cntl); |
Vaishali Thakkar | 4bf27b7 | 2015-09-14 23:19:35 +0530 | [diff] [blame] | 291 | ret = devm_gpio_request(&pdev->dev, gpio, "USB_MUX"); |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 292 | if (ret < 0) { |
| 293 | dev_err(&pdev->dev, |
| 294 | "failed to request the gpio=%d\n", gpio); |
Vaishali Thakkar | 4bf27b7 | 2015-09-14 23:19:35 +0530 | [diff] [blame] | 295 | return ret; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 296 | } |
Hans de Goede | 7688424 | 2016-12-19 01:13:06 +0100 | [diff] [blame] | 297 | gpiod_direction_output(info->gpio_mux_cntl, |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 298 | EXTCON_GPIO_MUX_SEL_PMIC); |
| 299 | } |
| 300 | |
| 301 | for (i = 0; i < EXTCON_IRQ_END; i++) { |
| 302 | pirq = platform_get_irq(pdev, i); |
| 303 | info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq); |
| 304 | if (info->irq[i] < 0) { |
| 305 | dev_err(&pdev->dev, |
| 306 | "failed to get virtual interrupt=%d\n", pirq); |
| 307 | ret = info->irq[i]; |
Vaishali Thakkar | 4bf27b7 | 2015-09-14 23:19:35 +0530 | [diff] [blame] | 308 | return ret; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | ret = devm_request_threaded_irq(&pdev->dev, info->irq[i], |
| 312 | NULL, axp288_extcon_isr, |
| 313 | IRQF_ONESHOT | IRQF_NO_SUSPEND, |
| 314 | pdev->name, info); |
| 315 | if (ret) { |
| 316 | dev_err(&pdev->dev, "failed to request interrupt=%d\n", |
| 317 | info->irq[i]); |
Vaishali Thakkar | 4bf27b7 | 2015-09-14 23:19:35 +0530 | [diff] [blame] | 318 | return ret; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | /* Enable interrupts */ |
| 323 | axp288_extcon_enable_irq(info); |
| 324 | |
| 325 | return 0; |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static struct platform_driver axp288_extcon_driver = { |
| 329 | .probe = axp288_extcon_probe, |
Ramakrishna Pallala | f031237 | 2015-04-30 20:44:45 +0530 | [diff] [blame] | 330 | .driver = { |
| 331 | .name = "axp288_extcon", |
| 332 | }, |
| 333 | }; |
| 334 | module_platform_driver(axp288_extcon_driver); |
| 335 | |
| 336 | MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>"); |
| 337 | MODULE_DESCRIPTION("X-Powers AXP288 extcon driver"); |
| 338 | MODULE_LICENSE("GPL v2"); |