extcon-intel-cht-wc.c 11 KB

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