isp1704_charger.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ISP1704 USB Charger Detection driver
  4. *
  5. * Copyright (C) 2010 Nokia Corporation
  6. * Copyright (C) 2012 - 2013 Pali Rohár <pali@kernel.org>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <linux/device.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/power_supply.h>
  17. #include <linux/delay.h>
  18. #include <linux/of.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/usb/otg.h>
  21. #include <linux/usb/ulpi.h>
  22. #include <linux/usb/ch9.h>
  23. #include <linux/usb/gadget.h>
  24. /* Vendor specific Power Control register */
  25. #define ISP1704_PWR_CTRL 0x3d
  26. #define ISP1704_PWR_CTRL_SWCTRL (1 << 0)
  27. #define ISP1704_PWR_CTRL_DET_COMP (1 << 1)
  28. #define ISP1704_PWR_CTRL_BVALID_RISE (1 << 2)
  29. #define ISP1704_PWR_CTRL_BVALID_FALL (1 << 3)
  30. #define ISP1704_PWR_CTRL_DP_WKPU_EN (1 << 4)
  31. #define ISP1704_PWR_CTRL_VDAT_DET (1 << 5)
  32. #define ISP1704_PWR_CTRL_DPVSRC_EN (1 << 6)
  33. #define ISP1704_PWR_CTRL_HWDETECT (1 << 7)
  34. #define NXP_VENDOR_ID 0x04cc
  35. static u16 isp170x_id[] = {
  36. 0x1704,
  37. 0x1707,
  38. };
  39. struct isp1704_charger {
  40. struct device *dev;
  41. struct power_supply *psy;
  42. struct power_supply_desc psy_desc;
  43. struct gpio_desc *enable_gpio;
  44. struct usb_phy *phy;
  45. struct notifier_block nb;
  46. struct work_struct work;
  47. /* properties */
  48. char model[8];
  49. unsigned present:1;
  50. unsigned online:1;
  51. unsigned current_max;
  52. };
  53. static inline int isp1704_read(struct isp1704_charger *isp, u32 reg)
  54. {
  55. return usb_phy_io_read(isp->phy, reg);
  56. }
  57. static inline int isp1704_write(struct isp1704_charger *isp, u32 reg, u32 val)
  58. {
  59. return usb_phy_io_write(isp->phy, val, reg);
  60. }
  61. static void isp1704_charger_set_power(struct isp1704_charger *isp, bool on)
  62. {
  63. gpiod_set_value(isp->enable_gpio, on);
  64. }
  65. /*
  66. * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
  67. * chargers).
  68. *
  69. * REVISIT: The method is defined in Battery Charging Specification and is
  70. * applicable to any ULPI transceiver. Nothing isp170x specific here.
  71. */
  72. static inline int isp1704_charger_type(struct isp1704_charger *isp)
  73. {
  74. u8 reg;
  75. u8 func_ctrl;
  76. u8 otg_ctrl;
  77. int type = POWER_SUPPLY_TYPE_USB_DCP;
  78. func_ctrl = isp1704_read(isp, ULPI_FUNC_CTRL);
  79. otg_ctrl = isp1704_read(isp, ULPI_OTG_CTRL);
  80. /* disable pulldowns */
  81. reg = ULPI_OTG_CTRL_DM_PULLDOWN | ULPI_OTG_CTRL_DP_PULLDOWN;
  82. isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), reg);
  83. /* full speed */
  84. isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
  85. ULPI_FUNC_CTRL_XCVRSEL_MASK);
  86. isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL),
  87. ULPI_FUNC_CTRL_FULL_SPEED);
  88. /* Enable strong pull-up on DP (1.5K) and reset */
  89. reg = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
  90. isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), reg);
  91. usleep_range(1000, 2000);
  92. reg = isp1704_read(isp, ULPI_DEBUG);
  93. if ((reg & 3) != 3)
  94. type = POWER_SUPPLY_TYPE_USB_CDP;
  95. /* recover original state */
  96. isp1704_write(isp, ULPI_FUNC_CTRL, func_ctrl);
  97. isp1704_write(isp, ULPI_OTG_CTRL, otg_ctrl);
  98. return type;
  99. }
  100. /*
  101. * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
  102. * is actually a dedicated charger, the following steps need to be taken.
  103. */
  104. static inline int isp1704_charger_verify(struct isp1704_charger *isp)
  105. {
  106. int ret = 0;
  107. u8 r;
  108. /* Reset the transceiver */
  109. r = isp1704_read(isp, ULPI_FUNC_CTRL);
  110. r |= ULPI_FUNC_CTRL_RESET;
  111. isp1704_write(isp, ULPI_FUNC_CTRL, r);
  112. usleep_range(1000, 2000);
  113. /* Set normal mode */
  114. r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK);
  115. isp1704_write(isp, ULPI_FUNC_CTRL, r);
  116. /* Clear the DP and DM pull-down bits */
  117. r = ULPI_OTG_CTRL_DP_PULLDOWN | ULPI_OTG_CTRL_DM_PULLDOWN;
  118. isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), r);
  119. /* Enable strong pull-up on DP (1.5K) and reset */
  120. r = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
  121. isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), r);
  122. usleep_range(1000, 2000);
  123. /* Read the line state */
  124. if (!isp1704_read(isp, ULPI_DEBUG)) {
  125. /* Disable strong pull-up on DP (1.5K) */
  126. isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
  127. ULPI_FUNC_CTRL_TERMSELECT);
  128. return 1;
  129. }
  130. /* Is it a charger or PS/2 connection */
  131. /* Enable weak pull-up resistor on DP */
  132. isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
  133. ISP1704_PWR_CTRL_DP_WKPU_EN);
  134. /* Disable strong pull-up on DP (1.5K) */
  135. isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
  136. ULPI_FUNC_CTRL_TERMSELECT);
  137. /* Enable weak pull-down resistor on DM */
  138. isp1704_write(isp, ULPI_SET(ULPI_OTG_CTRL),
  139. ULPI_OTG_CTRL_DM_PULLDOWN);
  140. /* It's a charger if the line states are clear */
  141. if (!(isp1704_read(isp, ULPI_DEBUG)))
  142. ret = 1;
  143. /* Disable weak pull-up resistor on DP */
  144. isp1704_write(isp, ULPI_CLR(ISP1704_PWR_CTRL),
  145. ISP1704_PWR_CTRL_DP_WKPU_EN);
  146. return ret;
  147. }
  148. static inline int isp1704_charger_detect(struct isp1704_charger *isp)
  149. {
  150. unsigned long timeout;
  151. u8 pwr_ctrl;
  152. int ret = 0;
  153. pwr_ctrl = isp1704_read(isp, ISP1704_PWR_CTRL);
  154. /* set SW control bit in PWR_CTRL register */
  155. isp1704_write(isp, ISP1704_PWR_CTRL,
  156. ISP1704_PWR_CTRL_SWCTRL);
  157. /* enable manual charger detection */
  158. isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
  159. ISP1704_PWR_CTRL_SWCTRL
  160. | ISP1704_PWR_CTRL_DPVSRC_EN);
  161. usleep_range(1000, 2000);
  162. timeout = jiffies + msecs_to_jiffies(300);
  163. do {
  164. /* Check if there is a charger */
  165. if (isp1704_read(isp, ISP1704_PWR_CTRL)
  166. & ISP1704_PWR_CTRL_VDAT_DET) {
  167. ret = isp1704_charger_verify(isp);
  168. break;
  169. }
  170. } while (!time_after(jiffies, timeout) && isp->online);
  171. /* recover original state */
  172. isp1704_write(isp, ISP1704_PWR_CTRL, pwr_ctrl);
  173. return ret;
  174. }
  175. static inline int isp1704_charger_detect_dcp(struct isp1704_charger *isp)
  176. {
  177. if (isp1704_charger_detect(isp) &&
  178. isp1704_charger_type(isp) == POWER_SUPPLY_TYPE_USB_DCP)
  179. return true;
  180. else
  181. return false;
  182. }
  183. static void isp1704_charger_work(struct work_struct *data)
  184. {
  185. struct isp1704_charger *isp =
  186. container_of(data, struct isp1704_charger, work);
  187. static DEFINE_MUTEX(lock);
  188. mutex_lock(&lock);
  189. switch (isp->phy->last_event) {
  190. case USB_EVENT_VBUS:
  191. /* do not call wall charger detection more times */
  192. if (!isp->present) {
  193. isp->online = true;
  194. isp->present = 1;
  195. isp1704_charger_set_power(isp, 1);
  196. /* detect wall charger */
  197. if (isp1704_charger_detect_dcp(isp)) {
  198. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB_DCP;
  199. isp->current_max = 1800;
  200. } else {
  201. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
  202. isp->current_max = 500;
  203. }
  204. /* enable data pullups */
  205. if (isp->phy->otg->gadget)
  206. usb_gadget_connect(isp->phy->otg->gadget);
  207. }
  208. if (isp->psy_desc.type != POWER_SUPPLY_TYPE_USB_DCP) {
  209. /*
  210. * Only 500mA here or high speed chirp
  211. * handshaking may break
  212. */
  213. if (isp->current_max > 500)
  214. isp->current_max = 500;
  215. if (isp->current_max > 100)
  216. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB_CDP;
  217. }
  218. break;
  219. case USB_EVENT_NONE:
  220. isp->online = false;
  221. isp->present = 0;
  222. isp->current_max = 0;
  223. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
  224. /*
  225. * Disable data pullups. We need to prevent the controller from
  226. * enumerating.
  227. *
  228. * FIXME: This is here to allow charger detection with Host/HUB
  229. * chargers. The pullups may be enabled elsewhere, so this can
  230. * not be the final solution.
  231. */
  232. if (isp->phy->otg->gadget)
  233. usb_gadget_disconnect(isp->phy->otg->gadget);
  234. isp1704_charger_set_power(isp, 0);
  235. break;
  236. default:
  237. goto out;
  238. }
  239. power_supply_changed(isp->psy);
  240. out:
  241. mutex_unlock(&lock);
  242. }
  243. static int isp1704_notifier_call(struct notifier_block *nb,
  244. unsigned long val, void *v)
  245. {
  246. struct isp1704_charger *isp =
  247. container_of(nb, struct isp1704_charger, nb);
  248. schedule_work(&isp->work);
  249. return NOTIFY_OK;
  250. }
  251. static int isp1704_charger_get_property(struct power_supply *psy,
  252. enum power_supply_property psp,
  253. union power_supply_propval *val)
  254. {
  255. struct isp1704_charger *isp = power_supply_get_drvdata(psy);
  256. switch (psp) {
  257. case POWER_SUPPLY_PROP_PRESENT:
  258. val->intval = isp->present;
  259. break;
  260. case POWER_SUPPLY_PROP_ONLINE:
  261. val->intval = isp->online;
  262. break;
  263. case POWER_SUPPLY_PROP_CURRENT_MAX:
  264. val->intval = isp->current_max;
  265. break;
  266. case POWER_SUPPLY_PROP_MODEL_NAME:
  267. val->strval = isp->model;
  268. break;
  269. case POWER_SUPPLY_PROP_MANUFACTURER:
  270. val->strval = "NXP";
  271. break;
  272. default:
  273. return -EINVAL;
  274. }
  275. return 0;
  276. }
  277. static enum power_supply_property power_props[] = {
  278. POWER_SUPPLY_PROP_PRESENT,
  279. POWER_SUPPLY_PROP_ONLINE,
  280. POWER_SUPPLY_PROP_CURRENT_MAX,
  281. POWER_SUPPLY_PROP_MODEL_NAME,
  282. POWER_SUPPLY_PROP_MANUFACTURER,
  283. };
  284. static inline int isp1704_test_ulpi(struct isp1704_charger *isp)
  285. {
  286. int vendor;
  287. int product;
  288. int i;
  289. int ret;
  290. /* Test ULPI interface */
  291. ret = isp1704_write(isp, ULPI_SCRATCH, 0xaa);
  292. if (ret < 0)
  293. return ret;
  294. ret = isp1704_read(isp, ULPI_SCRATCH);
  295. if (ret < 0)
  296. return ret;
  297. if (ret != 0xaa)
  298. return -ENODEV;
  299. /* Verify the product and vendor id matches */
  300. vendor = isp1704_read(isp, ULPI_VENDOR_ID_LOW);
  301. vendor |= isp1704_read(isp, ULPI_VENDOR_ID_HIGH) << 8;
  302. if (vendor != NXP_VENDOR_ID)
  303. return -ENODEV;
  304. product = isp1704_read(isp, ULPI_PRODUCT_ID_LOW);
  305. product |= isp1704_read(isp, ULPI_PRODUCT_ID_HIGH) << 8;
  306. for (i = 0; i < ARRAY_SIZE(isp170x_id); i++) {
  307. if (product == isp170x_id[i]) {
  308. sprintf(isp->model, "isp%x", product);
  309. return product;
  310. }
  311. }
  312. dev_err(isp->dev, "product id %x not matching known ids", product);
  313. return -ENODEV;
  314. }
  315. static int isp1704_charger_probe(struct platform_device *pdev)
  316. {
  317. struct isp1704_charger *isp;
  318. int ret = -ENODEV;
  319. struct power_supply_config psy_cfg = {};
  320. isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
  321. if (!isp)
  322. return -ENOMEM;
  323. isp->enable_gpio = devm_gpiod_get(&pdev->dev, "nxp,enable",
  324. GPIOD_OUT_HIGH);
  325. if (IS_ERR(isp->enable_gpio)) {
  326. ret = PTR_ERR(isp->enable_gpio);
  327. dev_err(&pdev->dev, "Could not get reset gpio: %d\n", ret);
  328. return ret;
  329. }
  330. if (pdev->dev.of_node)
  331. isp->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
  332. else
  333. isp->phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  334. if (IS_ERR(isp->phy)) {
  335. ret = PTR_ERR(isp->phy);
  336. dev_err(&pdev->dev, "usb_get_phy failed\n");
  337. goto fail0;
  338. }
  339. isp->dev = &pdev->dev;
  340. platform_set_drvdata(pdev, isp);
  341. isp1704_charger_set_power(isp, 1);
  342. ret = isp1704_test_ulpi(isp);
  343. if (ret < 0) {
  344. dev_err(&pdev->dev, "isp1704_test_ulpi failed\n");
  345. goto fail1;
  346. }
  347. isp->psy_desc.name = "isp1704";
  348. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
  349. isp->psy_desc.properties = power_props;
  350. isp->psy_desc.num_properties = ARRAY_SIZE(power_props);
  351. isp->psy_desc.get_property = isp1704_charger_get_property;
  352. psy_cfg.drv_data = isp;
  353. isp->psy = power_supply_register(isp->dev, &isp->psy_desc, &psy_cfg);
  354. if (IS_ERR(isp->psy)) {
  355. ret = PTR_ERR(isp->psy);
  356. dev_err(&pdev->dev, "power_supply_register failed\n");
  357. goto fail1;
  358. }
  359. /*
  360. * REVISIT: using work in order to allow the usb notifications to be
  361. * made atomically in the future.
  362. */
  363. INIT_WORK(&isp->work, isp1704_charger_work);
  364. isp->nb.notifier_call = isp1704_notifier_call;
  365. ret = usb_register_notifier(isp->phy, &isp->nb);
  366. if (ret) {
  367. dev_err(&pdev->dev, "usb_register_notifier failed\n");
  368. goto fail2;
  369. }
  370. dev_info(isp->dev, "registered with product id %s\n", isp->model);
  371. /*
  372. * Taking over the D+ pullup.
  373. *
  374. * FIXME: The device will be disconnected if it was already
  375. * enumerated. The charger driver should be always loaded before any
  376. * gadget is loaded.
  377. */
  378. if (isp->phy->otg->gadget)
  379. usb_gadget_disconnect(isp->phy->otg->gadget);
  380. if (isp->phy->last_event == USB_EVENT_NONE)
  381. isp1704_charger_set_power(isp, 0);
  382. /* Detect charger if VBUS is valid (the cable was already plugged). */
  383. if (isp->phy->last_event == USB_EVENT_VBUS &&
  384. !isp->phy->otg->default_a)
  385. schedule_work(&isp->work);
  386. return 0;
  387. fail2:
  388. power_supply_unregister(isp->psy);
  389. fail1:
  390. isp1704_charger_set_power(isp, 0);
  391. fail0:
  392. dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret);
  393. return ret;
  394. }
  395. static int isp1704_charger_remove(struct platform_device *pdev)
  396. {
  397. struct isp1704_charger *isp = platform_get_drvdata(pdev);
  398. usb_unregister_notifier(isp->phy, &isp->nb);
  399. power_supply_unregister(isp->psy);
  400. isp1704_charger_set_power(isp, 0);
  401. return 0;
  402. }
  403. #ifdef CONFIG_OF
  404. static const struct of_device_id omap_isp1704_of_match[] = {
  405. { .compatible = "nxp,isp1704", },
  406. { .compatible = "nxp,isp1707", },
  407. {},
  408. };
  409. MODULE_DEVICE_TABLE(of, omap_isp1704_of_match);
  410. #endif
  411. static struct platform_driver isp1704_charger_driver = {
  412. .driver = {
  413. .name = "isp1704_charger",
  414. .of_match_table = of_match_ptr(omap_isp1704_of_match),
  415. },
  416. .probe = isp1704_charger_probe,
  417. .remove = isp1704_charger_remove,
  418. };
  419. module_platform_driver(isp1704_charger_driver);
  420. MODULE_ALIAS("platform:isp1704_charger");
  421. MODULE_AUTHOR("Nokia Corporation");
  422. MODULE_DESCRIPTION("ISP170x USB Charger driver");
  423. MODULE_LICENSE("GPL");