extcon-axp288.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/io.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/property.h>
  23. #include <linux/notifier.h>
  24. #include <linux/extcon.h>
  25. #include <linux/regmap.h>
  26. #include <linux/gpio.h>
  27. #include <linux/gpio/consumer.h>
  28. #include <linux/mfd/axp20x.h>
  29. /* Power source status register */
  30. #define PS_STAT_VBUS_TRIGGER BIT(0)
  31. #define PS_STAT_BAT_CHRG_DIR BIT(2)
  32. #define PS_STAT_VBUS_ABOVE_VHOLD BIT(3)
  33. #define PS_STAT_VBUS_VALID BIT(4)
  34. #define PS_STAT_VBUS_PRESENT BIT(5)
  35. /* BC module global register */
  36. #define BC_GLOBAL_RUN BIT(0)
  37. #define BC_GLOBAL_DET_STAT BIT(2)
  38. #define BC_GLOBAL_DBP_TOUT BIT(3)
  39. #define BC_GLOBAL_VLGC_COM_SEL BIT(4)
  40. #define BC_GLOBAL_DCD_TOUT_MASK (BIT(6)|BIT(5))
  41. #define BC_GLOBAL_DCD_TOUT_300MS 0
  42. #define BC_GLOBAL_DCD_TOUT_100MS 1
  43. #define BC_GLOBAL_DCD_TOUT_500MS 2
  44. #define BC_GLOBAL_DCD_TOUT_900MS 3
  45. #define BC_GLOBAL_DCD_DET_SEL BIT(7)
  46. /* BC module vbus control and status register */
  47. #define VBUS_CNTL_DPDM_PD_EN BIT(4)
  48. #define VBUS_CNTL_DPDM_FD_EN BIT(5)
  49. #define VBUS_CNTL_FIRST_PO_STAT BIT(6)
  50. /* BC USB status register */
  51. #define USB_STAT_BUS_STAT_MASK (BIT(3)|BIT(2)|BIT(1)|BIT(0))
  52. #define USB_STAT_BUS_STAT_SHIFT 0
  53. #define USB_STAT_BUS_STAT_ATHD 0
  54. #define USB_STAT_BUS_STAT_CONN 1
  55. #define USB_STAT_BUS_STAT_SUSP 2
  56. #define USB_STAT_BUS_STAT_CONF 3
  57. #define USB_STAT_USB_SS_MODE BIT(4)
  58. #define USB_STAT_DEAD_BAT_DET BIT(6)
  59. #define USB_STAT_DBP_UNCFG BIT(7)
  60. /* BC detect status register */
  61. #define DET_STAT_MASK (BIT(7)|BIT(6)|BIT(5))
  62. #define DET_STAT_SHIFT 5
  63. #define DET_STAT_SDP 1
  64. #define DET_STAT_CDP 2
  65. #define DET_STAT_DCP 3
  66. enum axp288_extcon_reg {
  67. AXP288_PS_STAT_REG = 0x00,
  68. AXP288_PS_BOOT_REASON_REG = 0x02,
  69. AXP288_BC_GLOBAL_REG = 0x2c,
  70. AXP288_BC_VBUS_CNTL_REG = 0x2d,
  71. AXP288_BC_USB_STAT_REG = 0x2e,
  72. AXP288_BC_DET_STAT_REG = 0x2f,
  73. };
  74. enum axp288_mux_select {
  75. EXTCON_GPIO_MUX_SEL_PMIC = 0,
  76. EXTCON_GPIO_MUX_SEL_SOC,
  77. };
  78. enum axp288_extcon_irq {
  79. VBUS_FALLING_IRQ = 0,
  80. VBUS_RISING_IRQ,
  81. MV_CHNG_IRQ,
  82. BC_USB_CHNG_IRQ,
  83. EXTCON_IRQ_END,
  84. };
  85. static const unsigned int axp288_extcon_cables[] = {
  86. EXTCON_CHG_USB_SDP,
  87. EXTCON_CHG_USB_CDP,
  88. EXTCON_CHG_USB_DCP,
  89. EXTCON_USB,
  90. EXTCON_NONE,
  91. };
  92. struct axp288_extcon_info {
  93. struct device *dev;
  94. struct regmap *regmap;
  95. struct regmap_irq_chip_data *regmap_irqc;
  96. struct gpio_desc *gpio_mux_cntl;
  97. int irq[EXTCON_IRQ_END];
  98. struct extcon_dev *edev;
  99. struct notifier_block extcon_nb;
  100. unsigned int previous_cable;
  101. };
  102. /* Power up/down reason string array */
  103. static char *axp288_pwr_up_down_info[] = {
  104. "Last wake caused by user pressing the power button",
  105. "Last wake caused by a charger insertion",
  106. "Last wake caused by a battery insertion",
  107. "Last wake caused by SOC initiated global reset",
  108. "Last wake caused by cold reset",
  109. "Last shutdown caused by PMIC UVLO threshold",
  110. "Last shutdown caused by SOC initiated cold off",
  111. "Last shutdown caused by user pressing the power button",
  112. NULL,
  113. };
  114. /*
  115. * Decode and log the given "reset source indicator" (rsi)
  116. * register and then clear it.
  117. */
  118. static void axp288_extcon_log_rsi(struct axp288_extcon_info *info)
  119. {
  120. char **rsi;
  121. unsigned int val, i, clear_mask = 0;
  122. int ret;
  123. ret = regmap_read(info->regmap, AXP288_PS_BOOT_REASON_REG, &val);
  124. for (i = 0, rsi = axp288_pwr_up_down_info; *rsi; rsi++, i++) {
  125. if (val & BIT(i)) {
  126. dev_dbg(info->dev, "%s\n", *rsi);
  127. clear_mask |= BIT(i);
  128. }
  129. }
  130. /* Clear the register value for next reboot (write 1 to clear bit) */
  131. regmap_write(info->regmap, AXP288_PS_BOOT_REASON_REG, clear_mask);
  132. }
  133. static int axp288_handle_chrg_det_event(struct axp288_extcon_info *info)
  134. {
  135. int ret, stat, cfg, pwr_stat;
  136. u8 chrg_type;
  137. unsigned int cable = info->previous_cable;
  138. bool vbus_attach = false;
  139. ret = regmap_read(info->regmap, AXP288_PS_STAT_REG, &pwr_stat);
  140. if (ret < 0) {
  141. dev_err(info->dev, "failed to read vbus status\n");
  142. return ret;
  143. }
  144. vbus_attach = (pwr_stat & PS_STAT_VBUS_VALID);
  145. if (!vbus_attach)
  146. goto no_vbus;
  147. /* Check charger detection completion status */
  148. ret = regmap_read(info->regmap, AXP288_BC_GLOBAL_REG, &cfg);
  149. if (ret < 0)
  150. goto dev_det_ret;
  151. if (cfg & BC_GLOBAL_DET_STAT) {
  152. dev_dbg(info->dev, "can't complete the charger detection\n");
  153. goto dev_det_ret;
  154. }
  155. ret = regmap_read(info->regmap, AXP288_BC_DET_STAT_REG, &stat);
  156. if (ret < 0)
  157. goto dev_det_ret;
  158. chrg_type = (stat & DET_STAT_MASK) >> DET_STAT_SHIFT;
  159. switch (chrg_type) {
  160. case DET_STAT_SDP:
  161. dev_dbg(info->dev, "sdp cable is connected\n");
  162. cable = EXTCON_CHG_USB_SDP;
  163. break;
  164. case DET_STAT_CDP:
  165. dev_dbg(info->dev, "cdp cable is connected\n");
  166. cable = EXTCON_CHG_USB_CDP;
  167. break;
  168. case DET_STAT_DCP:
  169. dev_dbg(info->dev, "dcp cable is connected\n");
  170. cable = EXTCON_CHG_USB_DCP;
  171. break;
  172. default:
  173. dev_warn(info->dev,
  174. "disconnect or unknown or ID event\n");
  175. }
  176. no_vbus:
  177. /*
  178. * If VBUS is absent Connect D+/D- lines to PMIC for BC
  179. * detection. Else connect them to SOC for USB communication.
  180. */
  181. if (info->gpio_mux_cntl)
  182. gpiod_set_value(info->gpio_mux_cntl,
  183. vbus_attach ? EXTCON_GPIO_MUX_SEL_SOC
  184. : EXTCON_GPIO_MUX_SEL_PMIC);
  185. extcon_set_state_sync(info->edev, info->previous_cable, false);
  186. if (info->previous_cable == EXTCON_CHG_USB_SDP)
  187. extcon_set_state_sync(info->edev, EXTCON_USB, false);
  188. if (vbus_attach) {
  189. extcon_set_state_sync(info->edev, cable, vbus_attach);
  190. if (cable == EXTCON_CHG_USB_SDP)
  191. extcon_set_state_sync(info->edev, EXTCON_USB,
  192. vbus_attach);
  193. info->previous_cable = cable;
  194. }
  195. return 0;
  196. dev_det_ret:
  197. if (ret < 0)
  198. dev_err(info->dev, "failed to detect BC Mod\n");
  199. return ret;
  200. }
  201. static irqreturn_t axp288_extcon_isr(int irq, void *data)
  202. {
  203. struct axp288_extcon_info *info = data;
  204. int ret;
  205. ret = axp288_handle_chrg_det_event(info);
  206. if (ret < 0)
  207. dev_err(info->dev, "failed to handle the interrupt\n");
  208. return IRQ_HANDLED;
  209. }
  210. static void axp288_extcon_enable(struct axp288_extcon_info *info)
  211. {
  212. regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG,
  213. BC_GLOBAL_RUN, 0);
  214. /* Enable the charger detection logic */
  215. regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG,
  216. BC_GLOBAL_RUN, BC_GLOBAL_RUN);
  217. }
  218. static int axp288_extcon_probe(struct platform_device *pdev)
  219. {
  220. struct axp288_extcon_info *info;
  221. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  222. struct axp288_extcon_pdata *pdata = pdev->dev.platform_data;
  223. int ret, i, pirq, gpio;
  224. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  225. if (!info)
  226. return -ENOMEM;
  227. info->dev = &pdev->dev;
  228. info->regmap = axp20x->regmap;
  229. info->regmap_irqc = axp20x->regmap_irqc;
  230. info->previous_cable = EXTCON_NONE;
  231. if (pdata)
  232. info->gpio_mux_cntl = pdata->gpio_mux_cntl;
  233. platform_set_drvdata(pdev, info);
  234. axp288_extcon_log_rsi(info);
  235. /* Initialize extcon device */
  236. info->edev = devm_extcon_dev_allocate(&pdev->dev,
  237. axp288_extcon_cables);
  238. if (IS_ERR(info->edev)) {
  239. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  240. return PTR_ERR(info->edev);
  241. }
  242. /* Register extcon device */
  243. ret = devm_extcon_dev_register(&pdev->dev, info->edev);
  244. if (ret) {
  245. dev_err(&pdev->dev, "failed to register extcon device\n");
  246. return ret;
  247. }
  248. /* Set up gpio control for USB Mux */
  249. if (info->gpio_mux_cntl) {
  250. gpio = desc_to_gpio(info->gpio_mux_cntl);
  251. ret = devm_gpio_request(&pdev->dev, gpio, "USB_MUX");
  252. if (ret < 0) {
  253. dev_err(&pdev->dev,
  254. "failed to request the gpio=%d\n", gpio);
  255. return ret;
  256. }
  257. gpiod_direction_output(info->gpio_mux_cntl,
  258. EXTCON_GPIO_MUX_SEL_PMIC);
  259. }
  260. for (i = 0; i < EXTCON_IRQ_END; i++) {
  261. pirq = platform_get_irq(pdev, i);
  262. info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
  263. if (info->irq[i] < 0) {
  264. dev_err(&pdev->dev,
  265. "failed to get virtual interrupt=%d\n", pirq);
  266. ret = info->irq[i];
  267. return ret;
  268. }
  269. ret = devm_request_threaded_irq(&pdev->dev, info->irq[i],
  270. NULL, axp288_extcon_isr,
  271. IRQF_ONESHOT | IRQF_NO_SUSPEND,
  272. pdev->name, info);
  273. if (ret) {
  274. dev_err(&pdev->dev, "failed to request interrupt=%d\n",
  275. info->irq[i]);
  276. return ret;
  277. }
  278. }
  279. /* Start charger cable type detection */
  280. axp288_extcon_enable(info);
  281. return 0;
  282. }
  283. static const struct platform_device_id axp288_extcon_table[] = {
  284. { .name = "axp288_extcon" },
  285. {},
  286. };
  287. MODULE_DEVICE_TABLE(platform, axp288_extcon_table);
  288. static struct platform_driver axp288_extcon_driver = {
  289. .probe = axp288_extcon_probe,
  290. .id_table = axp288_extcon_table,
  291. .driver = {
  292. .name = "axp288_extcon",
  293. },
  294. };
  295. module_platform_driver(axp288_extcon_driver);
  296. MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
  297. MODULE_DESCRIPTION("X-Powers AXP288 extcon driver");
  298. MODULE_LICENSE("GPL v2");