isp1704_charger.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 power_supply_desc psy_desc;
  57. struct usb_phy *phy;
  58. struct notifier_block nb;
  59. struct work_struct work;
  60. /* properties */
  61. char model[8];
  62. unsigned present:1;
  63. unsigned online:1;
  64. unsigned current_max;
  65. };
  66. static inline int isp1704_read(struct isp1704_charger *isp, u32 reg)
  67. {
  68. return usb_phy_io_read(isp->phy, reg);
  69. }
  70. static inline int isp1704_write(struct isp1704_charger *isp, u32 val, u32 reg)
  71. {
  72. return usb_phy_io_write(isp->phy, val, reg);
  73. }
  74. /*
  75. * Disable/enable the power from the isp1704 if a function for it
  76. * has been provided with platform data.
  77. */
  78. static void isp1704_charger_set_power(struct isp1704_charger *isp, bool on)
  79. {
  80. struct isp1704_charger_data *board = isp->dev->platform_data;
  81. if (board && board->set_power)
  82. board->set_power(on);
  83. else if (board)
  84. gpio_set_value(board->enable_gpio, on);
  85. }
  86. /*
  87. * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
  88. * chargers).
  89. *
  90. * REVISIT: The method is defined in Battery Charging Specification and is
  91. * applicable to any ULPI transceiver. Nothing isp170x specific here.
  92. */
  93. static inline int isp1704_charger_type(struct isp1704_charger *isp)
  94. {
  95. u8 reg;
  96. u8 func_ctrl;
  97. u8 otg_ctrl;
  98. int type = POWER_SUPPLY_TYPE_USB_DCP;
  99. func_ctrl = isp1704_read(isp, ULPI_FUNC_CTRL);
  100. otg_ctrl = isp1704_read(isp, ULPI_OTG_CTRL);
  101. /* disable pulldowns */
  102. reg = ULPI_OTG_CTRL_DM_PULLDOWN | ULPI_OTG_CTRL_DP_PULLDOWN;
  103. isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), reg);
  104. /* full speed */
  105. isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
  106. ULPI_FUNC_CTRL_XCVRSEL_MASK);
  107. isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL),
  108. ULPI_FUNC_CTRL_FULL_SPEED);
  109. /* Enable strong pull-up on DP (1.5K) and reset */
  110. reg = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
  111. isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), reg);
  112. usleep_range(1000, 2000);
  113. reg = isp1704_read(isp, ULPI_DEBUG);
  114. if ((reg & 3) != 3)
  115. type = POWER_SUPPLY_TYPE_USB_CDP;
  116. /* recover original state */
  117. isp1704_write(isp, ULPI_FUNC_CTRL, func_ctrl);
  118. isp1704_write(isp, ULPI_OTG_CTRL, otg_ctrl);
  119. return type;
  120. }
  121. /*
  122. * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
  123. * is actually a dedicated charger, the following steps need to be taken.
  124. */
  125. static inline int isp1704_charger_verify(struct isp1704_charger *isp)
  126. {
  127. int ret = 0;
  128. u8 r;
  129. /* Reset the transceiver */
  130. r = isp1704_read(isp, ULPI_FUNC_CTRL);
  131. r |= ULPI_FUNC_CTRL_RESET;
  132. isp1704_write(isp, ULPI_FUNC_CTRL, r);
  133. usleep_range(1000, 2000);
  134. /* Set normal mode */
  135. r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK);
  136. isp1704_write(isp, ULPI_FUNC_CTRL, r);
  137. /* Clear the DP and DM pull-down bits */
  138. r = ULPI_OTG_CTRL_DP_PULLDOWN | ULPI_OTG_CTRL_DM_PULLDOWN;
  139. isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), r);
  140. /* Enable strong pull-up on DP (1.5K) and reset */
  141. r = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
  142. isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), r);
  143. usleep_range(1000, 2000);
  144. /* Read the line state */
  145. if (!isp1704_read(isp, ULPI_DEBUG)) {
  146. /* Disable strong pull-up on DP (1.5K) */
  147. isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
  148. ULPI_FUNC_CTRL_TERMSELECT);
  149. return 1;
  150. }
  151. /* Is it a charger or PS/2 connection */
  152. /* Enable weak pull-up resistor on DP */
  153. isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
  154. ISP1704_PWR_CTRL_DP_WKPU_EN);
  155. /* Disable strong pull-up on DP (1.5K) */
  156. isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
  157. ULPI_FUNC_CTRL_TERMSELECT);
  158. /* Enable weak pull-down resistor on DM */
  159. isp1704_write(isp, ULPI_SET(ULPI_OTG_CTRL),
  160. ULPI_OTG_CTRL_DM_PULLDOWN);
  161. /* It's a charger if the line states are clear */
  162. if (!(isp1704_read(isp, ULPI_DEBUG)))
  163. ret = 1;
  164. /* Disable weak pull-up resistor on DP */
  165. isp1704_write(isp, ULPI_CLR(ISP1704_PWR_CTRL),
  166. ISP1704_PWR_CTRL_DP_WKPU_EN);
  167. return ret;
  168. }
  169. static inline int isp1704_charger_detect(struct isp1704_charger *isp)
  170. {
  171. unsigned long timeout;
  172. u8 pwr_ctrl;
  173. int ret = 0;
  174. pwr_ctrl = isp1704_read(isp, ISP1704_PWR_CTRL);
  175. /* set SW control bit in PWR_CTRL register */
  176. isp1704_write(isp, ISP1704_PWR_CTRL,
  177. ISP1704_PWR_CTRL_SWCTRL);
  178. /* enable manual charger detection */
  179. isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
  180. ISP1704_PWR_CTRL_SWCTRL
  181. | ISP1704_PWR_CTRL_DPVSRC_EN);
  182. usleep_range(1000, 2000);
  183. timeout = jiffies + msecs_to_jiffies(300);
  184. do {
  185. /* Check if there is a charger */
  186. if (isp1704_read(isp, ISP1704_PWR_CTRL)
  187. & ISP1704_PWR_CTRL_VDAT_DET) {
  188. ret = isp1704_charger_verify(isp);
  189. break;
  190. }
  191. } while (!time_after(jiffies, timeout) && isp->online);
  192. /* recover original state */
  193. isp1704_write(isp, ISP1704_PWR_CTRL, pwr_ctrl);
  194. return ret;
  195. }
  196. static inline int isp1704_charger_detect_dcp(struct isp1704_charger *isp)
  197. {
  198. if (isp1704_charger_detect(isp) &&
  199. isp1704_charger_type(isp) == POWER_SUPPLY_TYPE_USB_DCP)
  200. return true;
  201. else
  202. return false;
  203. }
  204. static void isp1704_charger_work(struct work_struct *data)
  205. {
  206. struct isp1704_charger *isp =
  207. container_of(data, struct isp1704_charger, work);
  208. static DEFINE_MUTEX(lock);
  209. mutex_lock(&lock);
  210. switch (isp->phy->last_event) {
  211. case USB_EVENT_VBUS:
  212. /* do not call wall charger detection more times */
  213. if (!isp->present) {
  214. isp->online = true;
  215. isp->present = 1;
  216. isp1704_charger_set_power(isp, 1);
  217. /* detect wall charger */
  218. if (isp1704_charger_detect_dcp(isp)) {
  219. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB_DCP;
  220. isp->current_max = 1800;
  221. } else {
  222. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
  223. isp->current_max = 500;
  224. }
  225. /* enable data pullups */
  226. if (isp->phy->otg->gadget)
  227. usb_gadget_connect(isp->phy->otg->gadget);
  228. }
  229. if (isp->psy_desc.type != POWER_SUPPLY_TYPE_USB_DCP) {
  230. /*
  231. * Only 500mA here or high speed chirp
  232. * handshaking may break
  233. */
  234. if (isp->current_max > 500)
  235. isp->current_max = 500;
  236. if (isp->current_max > 100)
  237. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB_CDP;
  238. }
  239. break;
  240. case USB_EVENT_NONE:
  241. isp->online = false;
  242. isp->present = 0;
  243. isp->current_max = 0;
  244. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
  245. /*
  246. * Disable data pullups. We need to prevent the controller from
  247. * enumerating.
  248. *
  249. * FIXME: This is here to allow charger detection with Host/HUB
  250. * chargers. The pullups may be enabled elsewhere, so this can
  251. * not be the final solution.
  252. */
  253. if (isp->phy->otg->gadget)
  254. usb_gadget_disconnect(isp->phy->otg->gadget);
  255. isp1704_charger_set_power(isp, 0);
  256. break;
  257. default:
  258. goto out;
  259. }
  260. power_supply_changed(isp->psy);
  261. out:
  262. mutex_unlock(&lock);
  263. }
  264. static int isp1704_notifier_call(struct notifier_block *nb,
  265. unsigned long val, void *v)
  266. {
  267. struct isp1704_charger *isp =
  268. container_of(nb, struct isp1704_charger, nb);
  269. schedule_work(&isp->work);
  270. return NOTIFY_OK;
  271. }
  272. static int isp1704_charger_get_property(struct power_supply *psy,
  273. enum power_supply_property psp,
  274. union power_supply_propval *val)
  275. {
  276. struct isp1704_charger *isp = power_supply_get_drvdata(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 power_supply_config psy_cfg = {};
  341. struct isp1704_charger_data *pdata = dev_get_platdata(&pdev->dev);
  342. struct device_node *np = pdev->dev.of_node;
  343. if (np) {
  344. int gpio = of_get_named_gpio(np, "nxp,enable-gpio", 0);
  345. if (gpio < 0)
  346. return gpio;
  347. pdata = devm_kzalloc(&pdev->dev,
  348. sizeof(struct isp1704_charger_data), GFP_KERNEL);
  349. pdata->enable_gpio = gpio;
  350. dev_info(&pdev->dev, "init gpio %d\n", pdata->enable_gpio);
  351. ret = devm_gpio_request_one(&pdev->dev, pdata->enable_gpio,
  352. GPIOF_OUT_INIT_HIGH, "isp1704_reset");
  353. if (ret)
  354. goto fail0;
  355. }
  356. if (!pdata) {
  357. dev_err(&pdev->dev, "missing platform data!\n");
  358. return -ENODEV;
  359. }
  360. isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
  361. if (!isp)
  362. return -ENOMEM;
  363. if (np)
  364. isp->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
  365. else
  366. isp->phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  367. if (IS_ERR(isp->phy)) {
  368. ret = PTR_ERR(isp->phy);
  369. goto fail0;
  370. }
  371. isp->dev = &pdev->dev;
  372. platform_set_drvdata(pdev, isp);
  373. isp1704_charger_set_power(isp, 1);
  374. ret = isp1704_test_ulpi(isp);
  375. if (ret < 0)
  376. goto fail1;
  377. isp->psy_desc.name = "isp1704";
  378. isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
  379. isp->psy_desc.properties = power_props;
  380. isp->psy_desc.num_properties = ARRAY_SIZE(power_props);
  381. isp->psy_desc.get_property = isp1704_charger_get_property;
  382. psy_cfg.drv_data = isp;
  383. isp->psy = power_supply_register(isp->dev, &isp->psy_desc, &psy_cfg);
  384. if (IS_ERR(isp->psy)) {
  385. ret = PTR_ERR(isp->psy);
  386. goto fail1;
  387. }
  388. /*
  389. * REVISIT: using work in order to allow the usb notifications to be
  390. * made atomically in the future.
  391. */
  392. INIT_WORK(&isp->work, isp1704_charger_work);
  393. isp->nb.notifier_call = isp1704_notifier_call;
  394. ret = usb_register_notifier(isp->phy, &isp->nb);
  395. if (ret)
  396. goto fail2;
  397. dev_info(isp->dev, "registered with product id %s\n", isp->model);
  398. /*
  399. * Taking over the D+ pullup.
  400. *
  401. * FIXME: The device will be disconnected if it was already
  402. * enumerated. The charger driver should be always loaded before any
  403. * gadget is loaded.
  404. */
  405. if (isp->phy->otg->gadget)
  406. usb_gadget_disconnect(isp->phy->otg->gadget);
  407. if (isp->phy->last_event == USB_EVENT_NONE)
  408. isp1704_charger_set_power(isp, 0);
  409. /* Detect charger if VBUS is valid (the cable was already plugged). */
  410. if (isp->phy->last_event == USB_EVENT_VBUS &&
  411. !isp->phy->otg->default_a)
  412. schedule_work(&isp->work);
  413. return 0;
  414. fail2:
  415. power_supply_unregister(isp->psy);
  416. fail1:
  417. isp1704_charger_set_power(isp, 0);
  418. fail0:
  419. dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret);
  420. return ret;
  421. }
  422. static int isp1704_charger_remove(struct platform_device *pdev)
  423. {
  424. struct isp1704_charger *isp = platform_get_drvdata(pdev);
  425. usb_unregister_notifier(isp->phy, &isp->nb);
  426. power_supply_unregister(isp->psy);
  427. isp1704_charger_set_power(isp, 0);
  428. return 0;
  429. }
  430. #ifdef CONFIG_OF
  431. static const struct of_device_id omap_isp1704_of_match[] = {
  432. { .compatible = "nxp,isp1704", },
  433. {},
  434. };
  435. MODULE_DEVICE_TABLE(of, omap_isp1704_of_match);
  436. #endif
  437. static struct platform_driver isp1704_charger_driver = {
  438. .driver = {
  439. .name = "isp1704_charger",
  440. .of_match_table = of_match_ptr(omap_isp1704_of_match),
  441. },
  442. .probe = isp1704_charger_probe,
  443. .remove = isp1704_charger_remove,
  444. };
  445. module_platform_driver(isp1704_charger_driver);
  446. MODULE_ALIAS("platform:isp1704_charger");
  447. MODULE_AUTHOR("Nokia Corporation");
  448. MODULE_DESCRIPTION("ISP170x USB Charger driver");
  449. MODULE_LICENSE("GPL");