extcon-ptn5150.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // extcon-ptn5150.c - PTN5150 CC logic extcon driver to support USB detection
  4. //
  5. // Based on extcon-sm5502.c driver
  6. // Copyright (c) 2018-2019 by Vijai Kumar K
  7. // Author: Vijai Kumar K <[email protected]>
  8. // Copyright (c) 2020 Krzysztof Kozlowski <[email protected]>
  9. #include <linux/bitfield.h>
  10. #include <linux/err.h>
  11. #include <linux/i2c.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/regmap.h>
  16. #include <linux/slab.h>
  17. #include <linux/extcon-provider.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/usb/role.h>
  20. /* PTN5150 registers */
  21. #define PTN5150_REG_DEVICE_ID 0x01
  22. #define PTN5150_REG_CONTROL 0x02
  23. #define PTN5150_REG_INT_STATUS 0x03
  24. #define PTN5150_REG_CC_STATUS 0x04
  25. #define PTN5150_REG_CON_DET 0x09
  26. #define PTN5150_REG_VCONN_STATUS 0x0a
  27. #define PTN5150_REG_RESET 0x0b
  28. #define PTN5150_REG_INT_MASK 0x18
  29. #define PTN5150_REG_INT_REG_STATUS 0x19
  30. #define PTN5150_REG_END PTN5150_REG_INT_REG_STATUS
  31. #define PTN5150_DFP_ATTACHED 0x1
  32. #define PTN5150_UFP_ATTACHED 0x2
  33. /* Define PTN5150 MASK/SHIFT constant */
  34. #define PTN5150_REG_DEVICE_ID_VERSION GENMASK(7, 3)
  35. #define PTN5150_REG_DEVICE_ID_VENDOR GENMASK(2, 0)
  36. #define PTN5150_REG_CC_PORT_ATTACHMENT GENMASK(4, 2)
  37. #define PTN5150_REG_CC_VBUS_DETECTION BIT(7)
  38. #define PTN5150_REG_INT_CABLE_ATTACH_MASK BIT(0)
  39. #define PTN5150_REG_INT_CABLE_DETACH_MASK BIT(1)
  40. struct ptn5150_info {
  41. struct device *dev;
  42. struct extcon_dev *edev;
  43. struct i2c_client *i2c;
  44. struct regmap *regmap;
  45. struct gpio_desc *int_gpiod;
  46. struct gpio_desc *vbus_gpiod;
  47. int irq;
  48. struct work_struct irq_work;
  49. struct mutex mutex;
  50. struct usb_role_switch *role_sw;
  51. };
  52. /* List of detectable cables */
  53. static const unsigned int ptn5150_extcon_cable[] = {
  54. EXTCON_USB,
  55. EXTCON_USB_HOST,
  56. EXTCON_NONE,
  57. };
  58. static const struct regmap_config ptn5150_regmap_config = {
  59. .reg_bits = 8,
  60. .val_bits = 8,
  61. .max_register = PTN5150_REG_END,
  62. };
  63. static void ptn5150_check_state(struct ptn5150_info *info)
  64. {
  65. unsigned int port_status, reg_data, vbus;
  66. enum usb_role usb_role = USB_ROLE_NONE;
  67. int ret;
  68. ret = regmap_read(info->regmap, PTN5150_REG_CC_STATUS, &reg_data);
  69. if (ret) {
  70. dev_err(info->dev, "failed to read CC STATUS %d\n", ret);
  71. return;
  72. }
  73. port_status = FIELD_GET(PTN5150_REG_CC_PORT_ATTACHMENT, reg_data);
  74. switch (port_status) {
  75. case PTN5150_DFP_ATTACHED:
  76. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
  77. gpiod_set_value_cansleep(info->vbus_gpiod, 0);
  78. extcon_set_state_sync(info->edev, EXTCON_USB, true);
  79. usb_role = USB_ROLE_DEVICE;
  80. break;
  81. case PTN5150_UFP_ATTACHED:
  82. extcon_set_state_sync(info->edev, EXTCON_USB, false);
  83. vbus = FIELD_GET(PTN5150_REG_CC_VBUS_DETECTION, reg_data);
  84. if (vbus)
  85. gpiod_set_value_cansleep(info->vbus_gpiod, 0);
  86. else
  87. gpiod_set_value_cansleep(info->vbus_gpiod, 1);
  88. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
  89. usb_role = USB_ROLE_HOST;
  90. break;
  91. default:
  92. break;
  93. }
  94. if (usb_role) {
  95. ret = usb_role_switch_set_role(info->role_sw, usb_role);
  96. if (ret)
  97. dev_err(info->dev, "failed to set %s role: %d\n",
  98. usb_role_string(usb_role), ret);
  99. }
  100. }
  101. static void ptn5150_irq_work(struct work_struct *work)
  102. {
  103. struct ptn5150_info *info = container_of(work,
  104. struct ptn5150_info, irq_work);
  105. int ret = 0;
  106. unsigned int int_status;
  107. if (!info->edev)
  108. return;
  109. mutex_lock(&info->mutex);
  110. /* Clear interrupt. Read would clear the register */
  111. ret = regmap_read(info->regmap, PTN5150_REG_INT_STATUS, &int_status);
  112. if (ret) {
  113. dev_err(info->dev, "failed to read INT STATUS %d\n", ret);
  114. mutex_unlock(&info->mutex);
  115. return;
  116. }
  117. if (int_status) {
  118. unsigned int cable_attach;
  119. cable_attach = int_status & PTN5150_REG_INT_CABLE_ATTACH_MASK;
  120. if (cable_attach) {
  121. ptn5150_check_state(info);
  122. } else {
  123. extcon_set_state_sync(info->edev,
  124. EXTCON_USB_HOST, false);
  125. extcon_set_state_sync(info->edev,
  126. EXTCON_USB, false);
  127. gpiod_set_value_cansleep(info->vbus_gpiod, 0);
  128. ret = usb_role_switch_set_role(info->role_sw,
  129. USB_ROLE_NONE);
  130. if (ret)
  131. dev_err(info->dev,
  132. "failed to set none role: %d\n",
  133. ret);
  134. }
  135. }
  136. /* Clear interrupt. Read would clear the register */
  137. ret = regmap_read(info->regmap, PTN5150_REG_INT_REG_STATUS,
  138. &int_status);
  139. if (ret) {
  140. dev_err(info->dev,
  141. "failed to read INT REG STATUS %d\n", ret);
  142. mutex_unlock(&info->mutex);
  143. return;
  144. }
  145. mutex_unlock(&info->mutex);
  146. }
  147. static irqreturn_t ptn5150_irq_handler(int irq, void *data)
  148. {
  149. struct ptn5150_info *info = data;
  150. schedule_work(&info->irq_work);
  151. return IRQ_HANDLED;
  152. }
  153. static int ptn5150_init_dev_type(struct ptn5150_info *info)
  154. {
  155. unsigned int reg_data, vendor_id, version_id;
  156. int ret;
  157. ret = regmap_read(info->regmap, PTN5150_REG_DEVICE_ID, &reg_data);
  158. if (ret) {
  159. dev_err(info->dev, "failed to read DEVICE_ID %d\n", ret);
  160. return -EINVAL;
  161. }
  162. vendor_id = FIELD_GET(PTN5150_REG_DEVICE_ID_VENDOR, reg_data);
  163. version_id = FIELD_GET(PTN5150_REG_DEVICE_ID_VERSION, reg_data);
  164. dev_dbg(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
  165. version_id, vendor_id);
  166. /* Clear any existing interrupts */
  167. ret = regmap_read(info->regmap, PTN5150_REG_INT_STATUS, &reg_data);
  168. if (ret) {
  169. dev_err(info->dev,
  170. "failed to read PTN5150_REG_INT_STATUS %d\n",
  171. ret);
  172. return -EINVAL;
  173. }
  174. ret = regmap_read(info->regmap, PTN5150_REG_INT_REG_STATUS, &reg_data);
  175. if (ret) {
  176. dev_err(info->dev,
  177. "failed to read PTN5150_REG_INT_REG_STATUS %d\n", ret);
  178. return -EINVAL;
  179. }
  180. return 0;
  181. }
  182. static void ptn5150_work_sync_and_put(void *data)
  183. {
  184. struct ptn5150_info *info = data;
  185. cancel_work_sync(&info->irq_work);
  186. usb_role_switch_put(info->role_sw);
  187. }
  188. static int ptn5150_i2c_probe(struct i2c_client *i2c)
  189. {
  190. struct device *dev = &i2c->dev;
  191. struct device_node *np = i2c->dev.of_node;
  192. struct ptn5150_info *info;
  193. int ret;
  194. if (!np)
  195. return -EINVAL;
  196. info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
  197. if (!info)
  198. return -ENOMEM;
  199. i2c_set_clientdata(i2c, info);
  200. info->dev = &i2c->dev;
  201. info->i2c = i2c;
  202. info->vbus_gpiod = devm_gpiod_get(&i2c->dev, "vbus", GPIOD_OUT_LOW);
  203. if (IS_ERR(info->vbus_gpiod)) {
  204. ret = PTR_ERR(info->vbus_gpiod);
  205. if (ret == -ENOENT) {
  206. dev_info(dev, "No VBUS GPIO, ignoring VBUS control\n");
  207. info->vbus_gpiod = NULL;
  208. } else {
  209. return dev_err_probe(dev, ret, "failed to get VBUS GPIO\n");
  210. }
  211. }
  212. mutex_init(&info->mutex);
  213. INIT_WORK(&info->irq_work, ptn5150_irq_work);
  214. info->regmap = devm_regmap_init_i2c(i2c, &ptn5150_regmap_config);
  215. if (IS_ERR(info->regmap)) {
  216. return dev_err_probe(info->dev, PTR_ERR(info->regmap),
  217. "failed to allocate register map\n");
  218. }
  219. if (i2c->irq > 0) {
  220. info->irq = i2c->irq;
  221. } else {
  222. info->int_gpiod = devm_gpiod_get(&i2c->dev, "int", GPIOD_IN);
  223. if (IS_ERR(info->int_gpiod)) {
  224. return dev_err_probe(dev, PTR_ERR(info->int_gpiod),
  225. "failed to get INT GPIO\n");
  226. }
  227. info->irq = gpiod_to_irq(info->int_gpiod);
  228. if (info->irq < 0) {
  229. dev_err(dev, "failed to get INTB IRQ\n");
  230. return info->irq;
  231. }
  232. }
  233. ret = devm_request_threaded_irq(dev, info->irq, NULL,
  234. ptn5150_irq_handler,
  235. IRQF_TRIGGER_FALLING |
  236. IRQF_ONESHOT,
  237. i2c->name, info);
  238. if (ret < 0) {
  239. dev_err(dev, "failed to request handler for INTB IRQ\n");
  240. return ret;
  241. }
  242. /* Allocate extcon device */
  243. info->edev = devm_extcon_dev_allocate(info->dev, ptn5150_extcon_cable);
  244. if (IS_ERR(info->edev)) {
  245. dev_err(info->dev, "failed to allocate memory for extcon\n");
  246. return -ENOMEM;
  247. }
  248. /* Register extcon device */
  249. ret = devm_extcon_dev_register(info->dev, info->edev);
  250. if (ret) {
  251. dev_err(info->dev, "failed to register extcon device\n");
  252. return ret;
  253. }
  254. extcon_set_property_capability(info->edev, EXTCON_USB,
  255. EXTCON_PROP_USB_VBUS);
  256. extcon_set_property_capability(info->edev, EXTCON_USB_HOST,
  257. EXTCON_PROP_USB_VBUS);
  258. extcon_set_property_capability(info->edev, EXTCON_USB_HOST,
  259. EXTCON_PROP_USB_TYPEC_POLARITY);
  260. /* Initialize PTN5150 device and print vendor id and version id */
  261. ret = ptn5150_init_dev_type(info);
  262. if (ret)
  263. return -EINVAL;
  264. info->role_sw = usb_role_switch_get(info->dev);
  265. if (IS_ERR(info->role_sw))
  266. return dev_err_probe(info->dev, PTR_ERR(info->role_sw),
  267. "failed to get role switch\n");
  268. ret = devm_add_action_or_reset(dev, ptn5150_work_sync_and_put, info);
  269. if (ret)
  270. return ret;
  271. /*
  272. * Update current extcon state if for example OTG connection was there
  273. * before the probe
  274. */
  275. mutex_lock(&info->mutex);
  276. ptn5150_check_state(info);
  277. mutex_unlock(&info->mutex);
  278. return 0;
  279. }
  280. static const struct of_device_id ptn5150_dt_match[] = {
  281. { .compatible = "nxp,ptn5150" },
  282. { },
  283. };
  284. MODULE_DEVICE_TABLE(of, ptn5150_dt_match);
  285. static const struct i2c_device_id ptn5150_i2c_id[] = {
  286. { "ptn5150", 0 },
  287. { }
  288. };
  289. MODULE_DEVICE_TABLE(i2c, ptn5150_i2c_id);
  290. static struct i2c_driver ptn5150_i2c_driver = {
  291. .driver = {
  292. .name = "ptn5150",
  293. .of_match_table = ptn5150_dt_match,
  294. },
  295. .probe_new = ptn5150_i2c_probe,
  296. .id_table = ptn5150_i2c_id,
  297. };
  298. module_i2c_driver(ptn5150_i2c_driver);
  299. MODULE_DESCRIPTION("NXP PTN5150 CC logic Extcon driver");
  300. MODULE_AUTHOR("Vijai Kumar K <[email protected]>");
  301. MODULE_AUTHOR("Krzysztof Kozlowski <[email protected]>");
  302. MODULE_LICENSE("GPL v2");