extcon-intel-cht-wc.c 11 KB

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