extcon-intel-cht-wc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Extcon charger detection driver for Intel Cherrytrail Whiskey Cove PMIC
  3. * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
  4. *
  5. * Based on various non upstream patches to support the CHT Whiskey Cove PMIC:
  6. * Copyright (C) 2013-2015 Intel Corporation. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. #include <linux/extcon.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mfd/intel_soc_pmic.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/slab.h>
  25. #define CHT_WC_PHYCTRL 0x5e07
  26. #define CHT_WC_CHGRCTRL0 0x5e16
  27. #define CHT_WC_CHGRCTRL0_CHGRRESET BIT(0)
  28. #define CHT_WC_CHGRCTRL0_EMRGCHREN BIT(1)
  29. #define CHT_WC_CHGRCTRL0_EXTCHRDIS BIT(2)
  30. #define CHT_WC_CHGRCTRL0_SWCONTROL BIT(3)
  31. #define CHT_WC_CHGRCTRL0_TTLCK_MASK BIT(4)
  32. #define CHT_WC_CHGRCTRL0_CCSM_OFF_MASK BIT(5)
  33. #define CHT_WC_CHGRCTRL0_DBPOFF_MASK BIT(6)
  34. #define CHT_WC_CHGRCTRL0_WDT_NOKICK BIT(7)
  35. #define CHT_WC_CHGRCTRL1 0x5e17
  36. #define CHT_WC_USBSRC 0x5e29
  37. #define CHT_WC_USBSRC_STS_MASK GENMASK(1, 0)
  38. #define CHT_WC_USBSRC_STS_SUCCESS 2
  39. #define CHT_WC_USBSRC_STS_FAIL 3
  40. #define CHT_WC_USBSRC_TYPE_SHIFT 2
  41. #define CHT_WC_USBSRC_TYPE_MASK GENMASK(5, 2)
  42. #define CHT_WC_USBSRC_TYPE_NONE 0
  43. #define CHT_WC_USBSRC_TYPE_SDP 1
  44. #define CHT_WC_USBSRC_TYPE_DCP 2
  45. #define CHT_WC_USBSRC_TYPE_CDP 3
  46. #define CHT_WC_USBSRC_TYPE_ACA 4
  47. #define CHT_WC_USBSRC_TYPE_SE1 5
  48. #define CHT_WC_USBSRC_TYPE_MHL 6
  49. #define CHT_WC_USBSRC_TYPE_FLOAT_DP_DN 7
  50. #define CHT_WC_USBSRC_TYPE_OTHER 8
  51. #define CHT_WC_USBSRC_TYPE_DCP_EXTPHY 9
  52. #define CHT_WC_PWRSRC_IRQ 0x6e03
  53. #define CHT_WC_PWRSRC_IRQ_MASK 0x6e0f
  54. #define CHT_WC_PWRSRC_STS 0x6e1e
  55. #define CHT_WC_PWRSRC_VBUS BIT(0)
  56. #define CHT_WC_PWRSRC_DC BIT(1)
  57. #define CHT_WC_PWRSRC_BAT BIT(2)
  58. #define CHT_WC_PWRSRC_ID_GND BIT(3)
  59. #define CHT_WC_PWRSRC_ID_FLOAT BIT(4)
  60. #define CHT_WC_VBUS_GPIO_CTLO 0x6e2d
  61. #define CHT_WC_VBUS_GPIO_CTLO_OUTPUT BIT(0)
  62. #define CHT_WC_VBUS_GPIO_CTLO_DRV_OD BIT(4)
  63. #define CHT_WC_VBUS_GPIO_CTLO_DIR_OUT BIT(5)
  64. enum cht_wc_usb_id {
  65. USB_ID_OTG,
  66. USB_ID_GND,
  67. USB_ID_FLOAT,
  68. USB_RID_A,
  69. USB_RID_B,
  70. USB_RID_C,
  71. };
  72. enum cht_wc_mux_select {
  73. MUX_SEL_PMIC = 0,
  74. MUX_SEL_SOC,
  75. };
  76. static const unsigned int cht_wc_extcon_cables[] = {
  77. EXTCON_USB,
  78. EXTCON_USB_HOST,
  79. EXTCON_CHG_USB_SDP,
  80. EXTCON_CHG_USB_CDP,
  81. EXTCON_CHG_USB_DCP,
  82. EXTCON_CHG_USB_ACA,
  83. EXTCON_NONE,
  84. };
  85. struct cht_wc_extcon_data {
  86. struct device *dev;
  87. struct regmap *regmap;
  88. struct extcon_dev *edev;
  89. unsigned int previous_cable;
  90. bool usb_host;
  91. };
  92. static int cht_wc_extcon_get_id(struct cht_wc_extcon_data *ext, int pwrsrc_sts)
  93. {
  94. if (pwrsrc_sts & CHT_WC_PWRSRC_ID_GND)
  95. return USB_ID_GND;
  96. if (pwrsrc_sts & CHT_WC_PWRSRC_ID_FLOAT)
  97. return USB_ID_FLOAT;
  98. /*
  99. * Once we have iio support for the gpadc we should read the USBID
  100. * gpadc channel here and determine ACA role based on that.
  101. */
  102. return USB_ID_FLOAT;
  103. }
  104. static int cht_wc_extcon_get_charger(struct cht_wc_extcon_data *ext,
  105. bool ignore_errors)
  106. {
  107. int ret, usbsrc, status;
  108. unsigned long timeout;
  109. /* Charger detection can take upto 600ms, wait 800ms max. */
  110. timeout = jiffies + msecs_to_jiffies(800);
  111. do {
  112. ret = regmap_read(ext->regmap, CHT_WC_USBSRC, &usbsrc);
  113. if (ret) {
  114. dev_err(ext->dev, "Error reading usbsrc: %d\n", ret);
  115. return ret;
  116. }
  117. status = usbsrc & CHT_WC_USBSRC_STS_MASK;
  118. if (status == CHT_WC_USBSRC_STS_SUCCESS ||
  119. status == CHT_WC_USBSRC_STS_FAIL)
  120. break;
  121. msleep(50); /* Wait a bit before retrying */
  122. } while (time_before(jiffies, timeout));
  123. if (status != CHT_WC_USBSRC_STS_SUCCESS) {
  124. if (ignore_errors)
  125. return EXTCON_CHG_USB_SDP; /* Save fallback */
  126. if (status == CHT_WC_USBSRC_STS_FAIL)
  127. dev_warn(ext->dev, "Could not detect charger type\n");
  128. else
  129. dev_warn(ext->dev, "Timeout detecting charger type\n");
  130. return EXTCON_CHG_USB_SDP; /* Save fallback */
  131. }
  132. usbsrc = (usbsrc & CHT_WC_USBSRC_TYPE_MASK) >> CHT_WC_USBSRC_TYPE_SHIFT;
  133. switch (usbsrc) {
  134. default:
  135. dev_warn(ext->dev,
  136. "Unhandled charger type %d, defaulting to SDP\n",
  137. ret);
  138. return EXTCON_CHG_USB_SDP;
  139. case CHT_WC_USBSRC_TYPE_SDP:
  140. case CHT_WC_USBSRC_TYPE_FLOAT_DP_DN:
  141. case CHT_WC_USBSRC_TYPE_OTHER:
  142. return EXTCON_CHG_USB_SDP;
  143. case CHT_WC_USBSRC_TYPE_CDP:
  144. return EXTCON_CHG_USB_CDP;
  145. case CHT_WC_USBSRC_TYPE_DCP:
  146. case CHT_WC_USBSRC_TYPE_DCP_EXTPHY:
  147. case CHT_WC_USBSRC_TYPE_MHL: /* MHL2+ delivers upto 2A, treat as DCP */
  148. return EXTCON_CHG_USB_DCP;
  149. case CHT_WC_USBSRC_TYPE_ACA:
  150. return EXTCON_CHG_USB_ACA;
  151. }
  152. }
  153. static void cht_wc_extcon_set_phymux(struct cht_wc_extcon_data *ext, u8 state)
  154. {
  155. int ret;
  156. ret = regmap_write(ext->regmap, CHT_WC_PHYCTRL, state);
  157. if (ret)
  158. dev_err(ext->dev, "Error writing phyctrl: %d\n", ret);
  159. }
  160. static void cht_wc_extcon_set_5v_boost(struct cht_wc_extcon_data *ext,
  161. bool enable)
  162. {
  163. int ret, val;
  164. /*
  165. * The 5V boost converter is enabled through a gpio on the PMIC, since
  166. * there currently is no gpio driver we access the gpio reg directly.
  167. */
  168. val = CHT_WC_VBUS_GPIO_CTLO_DRV_OD | CHT_WC_VBUS_GPIO_CTLO_DIR_OUT;
  169. if (enable)
  170. val |= CHT_WC_VBUS_GPIO_CTLO_OUTPUT;
  171. ret = regmap_write(ext->regmap, CHT_WC_VBUS_GPIO_CTLO, val);
  172. if (ret)
  173. dev_err(ext->dev, "Error writing Vbus GPIO CTLO: %d\n", ret);
  174. }
  175. /* Small helper to sync EXTCON_CHG_USB_SDP and EXTCON_USB state */
  176. static void cht_wc_extcon_set_state(struct cht_wc_extcon_data *ext,
  177. unsigned int cable, bool state)
  178. {
  179. extcon_set_state_sync(ext->edev, cable, state);
  180. if (cable == EXTCON_CHG_USB_SDP)
  181. extcon_set_state_sync(ext->edev, EXTCON_USB, state);
  182. }
  183. static void cht_wc_extcon_pwrsrc_event(struct cht_wc_extcon_data *ext)
  184. {
  185. int ret, pwrsrc_sts, id;
  186. unsigned int cable = EXTCON_NONE;
  187. /* Ignore errors in host mode, as the 5v boost converter is on then */
  188. bool ignore_get_charger_errors = ext->usb_host;
  189. ret = regmap_read(ext->regmap, CHT_WC_PWRSRC_STS, &pwrsrc_sts);
  190. if (ret) {
  191. dev_err(ext->dev, "Error reading pwrsrc status: %d\n", ret);
  192. return;
  193. }
  194. id = cht_wc_extcon_get_id(ext, pwrsrc_sts);
  195. if (id == USB_ID_GND) {
  196. /* The 5v boost causes a false VBUS / SDP detect, skip */
  197. goto charger_det_done;
  198. }
  199. /* Plugged into a host/charger or not connected? */
  200. if (!(pwrsrc_sts & CHT_WC_PWRSRC_VBUS)) {
  201. /* Route D+ and D- to PMIC for future charger detection */
  202. cht_wc_extcon_set_phymux(ext, MUX_SEL_PMIC);
  203. goto set_state;
  204. }
  205. ret = cht_wc_extcon_get_charger(ext, ignore_get_charger_errors);
  206. if (ret >= 0)
  207. cable = ret;
  208. charger_det_done:
  209. /* Route D+ and D- to SoC for the host or gadget controller */
  210. cht_wc_extcon_set_phymux(ext, MUX_SEL_SOC);
  211. set_state:
  212. if (cable != ext->previous_cable) {
  213. cht_wc_extcon_set_state(ext, cable, true);
  214. cht_wc_extcon_set_state(ext, ext->previous_cable, false);
  215. ext->previous_cable = cable;
  216. }
  217. ext->usb_host = ((id == USB_ID_GND) || (id == USB_RID_A));
  218. extcon_set_state_sync(ext->edev, EXTCON_USB_HOST, ext->usb_host);
  219. }
  220. static irqreturn_t cht_wc_extcon_isr(int irq, void *data)
  221. {
  222. struct cht_wc_extcon_data *ext = data;
  223. int ret, irqs;
  224. ret = regmap_read(ext->regmap, CHT_WC_PWRSRC_IRQ, &irqs);
  225. if (ret) {
  226. dev_err(ext->dev, "Error reading irqs: %d\n", ret);
  227. return IRQ_NONE;
  228. }
  229. cht_wc_extcon_pwrsrc_event(ext);
  230. ret = regmap_write(ext->regmap, CHT_WC_PWRSRC_IRQ, irqs);
  231. if (ret) {
  232. dev_err(ext->dev, "Error writing irqs: %d\n", ret);
  233. return IRQ_NONE;
  234. }
  235. return IRQ_HANDLED;
  236. }
  237. static int cht_wc_extcon_sw_control(struct cht_wc_extcon_data *ext, bool enable)
  238. {
  239. int ret, mask, val;
  240. mask = CHT_WC_CHGRCTRL0_SWCONTROL | CHT_WC_CHGRCTRL0_CCSM_OFF_MASK;
  241. val = enable ? mask : 0;
  242. ret = regmap_update_bits(ext->regmap, CHT_WC_CHGRCTRL0, mask, val);
  243. if (ret)
  244. dev_err(ext->dev, "Error setting sw control: %d\n", ret);
  245. return ret;
  246. }
  247. static int cht_wc_extcon_probe(struct platform_device *pdev)
  248. {
  249. struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
  250. struct cht_wc_extcon_data *ext;
  251. int irq, ret;
  252. irq = platform_get_irq(pdev, 0);
  253. if (irq < 0)
  254. return irq;
  255. ext = devm_kzalloc(&pdev->dev, sizeof(*ext), GFP_KERNEL);
  256. if (!ext)
  257. return -ENOMEM;
  258. ext->dev = &pdev->dev;
  259. ext->regmap = pmic->regmap;
  260. ext->previous_cable = EXTCON_NONE;
  261. /* Initialize extcon device */
  262. ext->edev = devm_extcon_dev_allocate(ext->dev, cht_wc_extcon_cables);
  263. if (IS_ERR(ext->edev))
  264. return PTR_ERR(ext->edev);
  265. /*
  266. * When a host-cable is detected the BIOS enables an external 5v boost
  267. * converter to power connected devices there are 2 problems with this:
  268. * 1) This gets seen by the external battery charger as a valid Vbus
  269. * supply and it then tries to feed Vsys from this creating a
  270. * feedback loop which causes aprox. 300 mA extra battery drain
  271. * (and unless we drive the external-charger-disable pin high it
  272. * also tries to charge the battery causing even more feedback).
  273. * 2) This gets seen by the pwrsrc block as a SDP USB Vbus supply
  274. * Since the external battery charger has its own 5v boost converter
  275. * which does not have these issues, we simply turn the separate
  276. * external 5v boost converter off and leave it off entirely.
  277. */
  278. cht_wc_extcon_set_5v_boost(ext, false);
  279. /* Enable sw control */
  280. ret = cht_wc_extcon_sw_control(ext, true);
  281. if (ret)
  282. return ret;
  283. /* Register extcon device */
  284. ret = devm_extcon_dev_register(ext->dev, ext->edev);
  285. if (ret) {
  286. dev_err(ext->dev, "Error registering extcon device: %d\n", ret);
  287. goto disable_sw_control;
  288. }
  289. /* Route D+ and D- to PMIC for initial charger detection */
  290. cht_wc_extcon_set_phymux(ext, MUX_SEL_PMIC);
  291. /* Get initial state */
  292. cht_wc_extcon_pwrsrc_event(ext);
  293. ret = devm_request_threaded_irq(ext->dev, irq, NULL, cht_wc_extcon_isr,
  294. IRQF_ONESHOT, pdev->name, ext);
  295. if (ret) {
  296. dev_err(ext->dev, "Error requesting interrupt: %d\n", ret);
  297. goto disable_sw_control;
  298. }
  299. /* Unmask irqs */
  300. ret = regmap_write(ext->regmap, CHT_WC_PWRSRC_IRQ_MASK,
  301. (int)~(CHT_WC_PWRSRC_VBUS | CHT_WC_PWRSRC_ID_GND |
  302. CHT_WC_PWRSRC_ID_FLOAT));
  303. if (ret) {
  304. dev_err(ext->dev, "Error writing irq-mask: %d\n", ret);
  305. goto disable_sw_control;
  306. }
  307. platform_set_drvdata(pdev, ext);
  308. return 0;
  309. disable_sw_control:
  310. cht_wc_extcon_sw_control(ext, false);
  311. return ret;
  312. }
  313. static int cht_wc_extcon_remove(struct platform_device *pdev)
  314. {
  315. struct cht_wc_extcon_data *ext = platform_get_drvdata(pdev);
  316. cht_wc_extcon_sw_control(ext, false);
  317. return 0;
  318. }
  319. static const struct platform_device_id cht_wc_extcon_table[] = {
  320. { .name = "cht_wcove_pwrsrc" },
  321. {},
  322. };
  323. MODULE_DEVICE_TABLE(platform, cht_wc_extcon_table);
  324. static struct platform_driver cht_wc_extcon_driver = {
  325. .probe = cht_wc_extcon_probe,
  326. .remove = cht_wc_extcon_remove,
  327. .id_table = cht_wc_extcon_table,
  328. .driver = {
  329. .name = "cht_wcove_pwrsrc",
  330. },
  331. };
  332. module_platform_driver(cht_wc_extcon_driver);
  333. MODULE_DESCRIPTION("Intel Cherrytrail Whiskey Cove PMIC extcon driver");
  334. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  335. MODULE_LICENSE("GPL v2");