extcon-ptn5150.c 8.2 KB

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