extcon-axp288.c 9.8 KB

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