isp1704_charger.c 13 KB

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