extcon-intel-mrfld.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * extcon driver for Basin Cove PMIC
  4. *
  5. * Copyright (c) 2019, Intel Corporation.
  6. * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  7. */
  8. #include <linux/extcon-provider.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/mfd/intel_soc_pmic.h>
  11. #include <linux/mfd/intel_soc_pmic_mrfld.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. #include "extcon-intel.h"
  17. #define BCOVE_USBIDCTRL 0x19
  18. #define BCOVE_USBIDCTRL_ID BIT(0)
  19. #define BCOVE_USBIDCTRL_ACA BIT(1)
  20. #define BCOVE_USBIDCTRL_ALL (BCOVE_USBIDCTRL_ID | BCOVE_USBIDCTRL_ACA)
  21. #define BCOVE_USBIDSTS 0x1a
  22. #define BCOVE_USBIDSTS_GND BIT(0)
  23. #define BCOVE_USBIDSTS_RARBRC_MASK GENMASK(2, 1)
  24. #define BCOVE_USBIDSTS_RARBRC_SHIFT 1
  25. #define BCOVE_USBIDSTS_NO_ACA 0
  26. #define BCOVE_USBIDSTS_R_ID_A 1
  27. #define BCOVE_USBIDSTS_R_ID_B 2
  28. #define BCOVE_USBIDSTS_R_ID_C 3
  29. #define BCOVE_USBIDSTS_FLOAT BIT(3)
  30. #define BCOVE_USBIDSTS_SHORT BIT(4)
  31. #define BCOVE_CHGRIRQ_ALL (BCOVE_CHGRIRQ_VBUSDET | BCOVE_CHGRIRQ_DCDET | \
  32. BCOVE_CHGRIRQ_BATTDET | BCOVE_CHGRIRQ_USBIDDET)
  33. #define BCOVE_CHGRCTRL0 0x4b
  34. #define BCOVE_CHGRCTRL0_CHGRRESET BIT(0)
  35. #define BCOVE_CHGRCTRL0_EMRGCHREN BIT(1)
  36. #define BCOVE_CHGRCTRL0_EXTCHRDIS BIT(2)
  37. #define BCOVE_CHGRCTRL0_SWCONTROL BIT(3)
  38. #define BCOVE_CHGRCTRL0_TTLCK BIT(4)
  39. #define BCOVE_CHGRCTRL0_BIT_5 BIT(5)
  40. #define BCOVE_CHGRCTRL0_BIT_6 BIT(6)
  41. #define BCOVE_CHGRCTRL0_CHR_WDT_NOKICK BIT(7)
  42. struct mrfld_extcon_data {
  43. struct device *dev;
  44. struct regmap *regmap;
  45. struct extcon_dev *edev;
  46. unsigned int status;
  47. unsigned int id;
  48. };
  49. static const unsigned int mrfld_extcon_cable[] = {
  50. EXTCON_USB,
  51. EXTCON_USB_HOST,
  52. EXTCON_CHG_USB_SDP,
  53. EXTCON_CHG_USB_CDP,
  54. EXTCON_CHG_USB_DCP,
  55. EXTCON_CHG_USB_ACA,
  56. EXTCON_NONE,
  57. };
  58. static int mrfld_extcon_clear(struct mrfld_extcon_data *data, unsigned int reg,
  59. unsigned int mask)
  60. {
  61. return regmap_update_bits(data->regmap, reg, mask, 0x00);
  62. }
  63. static int mrfld_extcon_set(struct mrfld_extcon_data *data, unsigned int reg,
  64. unsigned int mask)
  65. {
  66. return regmap_update_bits(data->regmap, reg, mask, 0xff);
  67. }
  68. static int mrfld_extcon_sw_control(struct mrfld_extcon_data *data, bool enable)
  69. {
  70. unsigned int mask = BCOVE_CHGRCTRL0_SWCONTROL;
  71. struct device *dev = data->dev;
  72. int ret;
  73. if (enable)
  74. ret = mrfld_extcon_set(data, BCOVE_CHGRCTRL0, mask);
  75. else
  76. ret = mrfld_extcon_clear(data, BCOVE_CHGRCTRL0, mask);
  77. if (ret)
  78. dev_err(dev, "can't set SW control: %d\n", ret);
  79. return ret;
  80. }
  81. static int mrfld_extcon_get_id(struct mrfld_extcon_data *data)
  82. {
  83. struct regmap *regmap = data->regmap;
  84. unsigned int id;
  85. bool ground;
  86. int ret;
  87. ret = regmap_read(regmap, BCOVE_USBIDSTS, &id);
  88. if (ret)
  89. return ret;
  90. if (id & BCOVE_USBIDSTS_FLOAT)
  91. return INTEL_USB_ID_FLOAT;
  92. switch ((id & BCOVE_USBIDSTS_RARBRC_MASK) >> BCOVE_USBIDSTS_RARBRC_SHIFT) {
  93. case BCOVE_USBIDSTS_R_ID_A:
  94. return INTEL_USB_RID_A;
  95. case BCOVE_USBIDSTS_R_ID_B:
  96. return INTEL_USB_RID_B;
  97. case BCOVE_USBIDSTS_R_ID_C:
  98. return INTEL_USB_RID_C;
  99. }
  100. /*
  101. * PMIC A0 reports USBIDSTS_GND = 1 for ID_GND,
  102. * but PMIC B0 reports USBIDSTS_GND = 0 for ID_GND.
  103. * Thus we must check this bit at last.
  104. */
  105. ground = id & BCOVE_USBIDSTS_GND;
  106. switch ('A' + BCOVE_MAJOR(data->id)) {
  107. case 'A':
  108. return ground ? INTEL_USB_ID_GND : INTEL_USB_ID_FLOAT;
  109. case 'B':
  110. return ground ? INTEL_USB_ID_FLOAT : INTEL_USB_ID_GND;
  111. }
  112. /* Unknown or unsupported type */
  113. return INTEL_USB_ID_FLOAT;
  114. }
  115. static int mrfld_extcon_role_detect(struct mrfld_extcon_data *data)
  116. {
  117. unsigned int id;
  118. bool usb_host;
  119. int ret;
  120. ret = mrfld_extcon_get_id(data);
  121. if (ret < 0)
  122. return ret;
  123. id = ret;
  124. usb_host = (id == INTEL_USB_ID_GND) || (id == INTEL_USB_RID_A);
  125. extcon_set_state_sync(data->edev, EXTCON_USB_HOST, usb_host);
  126. return 0;
  127. }
  128. static int mrfld_extcon_cable_detect(struct mrfld_extcon_data *data)
  129. {
  130. struct regmap *regmap = data->regmap;
  131. unsigned int status, change;
  132. int ret;
  133. /*
  134. * It seems SCU firmware clears the content of BCOVE_CHGRIRQ1
  135. * and makes it useless for OS. Instead we compare a previously
  136. * stored status to the current one, provided by BCOVE_SCHGRIRQ1.
  137. */
  138. ret = regmap_read(regmap, BCOVE_SCHGRIRQ1, &status);
  139. if (ret)
  140. return ret;
  141. change = status ^ data->status;
  142. if (!change)
  143. return -ENODATA;
  144. if (change & BCOVE_CHGRIRQ_USBIDDET) {
  145. ret = mrfld_extcon_role_detect(data);
  146. if (ret)
  147. return ret;
  148. }
  149. data->status = status;
  150. return 0;
  151. }
  152. static irqreturn_t mrfld_extcon_interrupt(int irq, void *dev_id)
  153. {
  154. struct mrfld_extcon_data *data = dev_id;
  155. int ret;
  156. ret = mrfld_extcon_cable_detect(data);
  157. mrfld_extcon_clear(data, BCOVE_MIRQLVL1, BCOVE_LVL1_CHGR);
  158. return ret ? IRQ_NONE: IRQ_HANDLED;
  159. }
  160. static int mrfld_extcon_probe(struct platform_device *pdev)
  161. {
  162. struct device *dev = &pdev->dev;
  163. struct intel_soc_pmic *pmic = dev_get_drvdata(dev->parent);
  164. struct regmap *regmap = pmic->regmap;
  165. struct mrfld_extcon_data *data;
  166. unsigned int status;
  167. unsigned int id;
  168. int irq, ret;
  169. irq = platform_get_irq(pdev, 0);
  170. if (irq < 0)
  171. return irq;
  172. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  173. if (!data)
  174. return -ENOMEM;
  175. data->dev = dev;
  176. data->regmap = regmap;
  177. data->edev = devm_extcon_dev_allocate(dev, mrfld_extcon_cable);
  178. if (IS_ERR(data->edev))
  179. return -ENOMEM;
  180. ret = devm_extcon_dev_register(dev, data->edev);
  181. if (ret < 0) {
  182. dev_err(dev, "can't register extcon device: %d\n", ret);
  183. return ret;
  184. }
  185. ret = devm_request_threaded_irq(dev, irq, NULL, mrfld_extcon_interrupt,
  186. IRQF_ONESHOT | IRQF_SHARED, pdev->name,
  187. data);
  188. if (ret) {
  189. dev_err(dev, "can't register IRQ handler: %d\n", ret);
  190. return ret;
  191. }
  192. ret = regmap_read(regmap, BCOVE_ID, &id);
  193. if (ret) {
  194. dev_err(dev, "can't read PMIC ID: %d\n", ret);
  195. return ret;
  196. }
  197. data->id = id;
  198. ret = mrfld_extcon_sw_control(data, true);
  199. if (ret)
  200. return ret;
  201. /* Get initial state */
  202. mrfld_extcon_role_detect(data);
  203. /*
  204. * Cached status value is used for cable detection, see comments
  205. * in mrfld_extcon_cable_detect(), we need to sync cached value
  206. * with a real state of the hardware.
  207. */
  208. regmap_read(regmap, BCOVE_SCHGRIRQ1, &status);
  209. data->status = status;
  210. mrfld_extcon_clear(data, BCOVE_MIRQLVL1, BCOVE_LVL1_CHGR);
  211. mrfld_extcon_clear(data, BCOVE_MCHGRIRQ1, BCOVE_CHGRIRQ_ALL);
  212. mrfld_extcon_set(data, BCOVE_USBIDCTRL, BCOVE_USBIDCTRL_ALL);
  213. platform_set_drvdata(pdev, data);
  214. return 0;
  215. }
  216. static int mrfld_extcon_remove(struct platform_device *pdev)
  217. {
  218. struct mrfld_extcon_data *data = platform_get_drvdata(pdev);
  219. mrfld_extcon_sw_control(data, false);
  220. return 0;
  221. }
  222. static const struct platform_device_id mrfld_extcon_id_table[] = {
  223. { .name = "mrfld_bcove_pwrsrc" },
  224. {}
  225. };
  226. MODULE_DEVICE_TABLE(platform, mrfld_extcon_id_table);
  227. static struct platform_driver mrfld_extcon_driver = {
  228. .driver = {
  229. .name = "mrfld_bcove_pwrsrc",
  230. },
  231. .probe = mrfld_extcon_probe,
  232. .remove = mrfld_extcon_remove,
  233. .id_table = mrfld_extcon_id_table,
  234. };
  235. module_platform_driver(mrfld_extcon_driver);
  236. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  237. MODULE_DESCRIPTION("extcon driver for Intel Merrifield Basin Cove PMIC");
  238. MODULE_LICENSE("GPL v2");