isp1704_charger.c 13 KB

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