isp1704_charger.c 12 KB

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