isp1704_charger.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. char model[7];
  56. unsigned present:1;
  57. };
  58. /*
  59. * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
  60. * is actually a dedicated charger, the following steps need to be taken.
  61. */
  62. static inline int isp1704_charger_verify(struct isp1704_charger *isp)
  63. {
  64. int ret = 0;
  65. u8 r;
  66. /* Reset the transceiver */
  67. r = otg_io_read(isp->otg, ULPI_FUNC_CTRL);
  68. r |= ULPI_FUNC_CTRL_RESET;
  69. otg_io_write(isp->otg, ULPI_FUNC_CTRL, r);
  70. usleep_range(1000, 2000);
  71. /* Set normal mode */
  72. r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK);
  73. otg_io_write(isp->otg, ULPI_FUNC_CTRL, r);
  74. /* Clear the DP and DM pull-down bits */
  75. r = ULPI_OTG_CTRL_DP_PULLDOWN | ULPI_OTG_CTRL_DM_PULLDOWN;
  76. otg_io_write(isp->otg, ULPI_CLR(ULPI_OTG_CTRL), r);
  77. /* Enable strong pull-up on DP (1.5K) and reset */
  78. r = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
  79. otg_io_write(isp->otg, ULPI_SET(ULPI_FUNC_CTRL), r);
  80. usleep_range(1000, 2000);
  81. /* Read the line state */
  82. if (!otg_io_read(isp->otg, ULPI_DEBUG)) {
  83. /* Disable strong pull-up on DP (1.5K) */
  84. otg_io_write(isp->otg, ULPI_CLR(ULPI_FUNC_CTRL),
  85. ULPI_FUNC_CTRL_TERMSELECT);
  86. return 1;
  87. }
  88. /* Is it a charger or PS/2 connection */
  89. /* Enable weak pull-up resistor on DP */
  90. otg_io_write(isp->otg, ULPI_SET(ISP1704_PWR_CTRL),
  91. ISP1704_PWR_CTRL_DP_WKPU_EN);
  92. /* Disable strong pull-up on DP (1.5K) */
  93. otg_io_write(isp->otg, ULPI_CLR(ULPI_FUNC_CTRL),
  94. ULPI_FUNC_CTRL_TERMSELECT);
  95. /* Enable weak pull-down resistor on DM */
  96. otg_io_write(isp->otg, ULPI_SET(ULPI_OTG_CTRL),
  97. ULPI_OTG_CTRL_DM_PULLDOWN);
  98. /* It's a charger if the line states are clear */
  99. if (!(otg_io_read(isp->otg, ULPI_DEBUG)))
  100. ret = 1;
  101. /* Disable weak pull-up resistor on DP */
  102. otg_io_write(isp->otg, ULPI_CLR(ISP1704_PWR_CTRL),
  103. ISP1704_PWR_CTRL_DP_WKPU_EN);
  104. return ret;
  105. }
  106. static inline int isp1704_charger_detect(struct isp1704_charger *isp)
  107. {
  108. unsigned long timeout;
  109. u8 r;
  110. int ret = 0;
  111. /* set SW control bit in PWR_CTRL register */
  112. otg_io_write(isp->otg, ISP1704_PWR_CTRL,
  113. ISP1704_PWR_CTRL_SWCTRL);
  114. /* enable manual charger detection */
  115. r = (ISP1704_PWR_CTRL_SWCTRL | ISP1704_PWR_CTRL_DPVSRC_EN);
  116. otg_io_write(isp->otg, ULPI_SET(ISP1704_PWR_CTRL), r);
  117. usleep_range(1000, 2000);
  118. timeout = jiffies + msecs_to_jiffies(300);
  119. do {
  120. /* Check if there is a charger */
  121. if (otg_io_read(isp->otg, ISP1704_PWR_CTRL)
  122. & ISP1704_PWR_CTRL_VDAT_DET) {
  123. ret = isp1704_charger_verify(isp);
  124. break;
  125. }
  126. } while (!time_after(jiffies, timeout));
  127. return ret;
  128. }
  129. static void isp1704_charger_work(struct work_struct *data)
  130. {
  131. int detect;
  132. struct isp1704_charger *isp =
  133. container_of(data, struct isp1704_charger, work);
  134. /*
  135. * FIXME Only supporting dedicated chargers even though isp1704 can
  136. * detect HUB and HOST chargers. If the device has already been
  137. * enumerated, the detection will break the connection.
  138. */
  139. if (isp->otg->state != OTG_STATE_B_IDLE)
  140. return;
  141. /* disable data pullups */
  142. if (isp->otg->gadget)
  143. usb_gadget_disconnect(isp->otg->gadget);
  144. /* detect charger */
  145. detect = isp1704_charger_detect(isp);
  146. if (detect) {
  147. isp->present = detect;
  148. power_supply_changed(&isp->psy);
  149. }
  150. /* enable data pullups */
  151. if (isp->otg->gadget)
  152. usb_gadget_connect(isp->otg->gadget);
  153. }
  154. static int isp1704_notifier_call(struct notifier_block *nb,
  155. unsigned long event, void *unused)
  156. {
  157. struct isp1704_charger *isp =
  158. container_of(nb, struct isp1704_charger, nb);
  159. switch (event) {
  160. case USB_EVENT_VBUS:
  161. schedule_work(&isp->work);
  162. break;
  163. case USB_EVENT_NONE:
  164. if (isp->present) {
  165. isp->present = 0;
  166. power_supply_changed(&isp->psy);
  167. }
  168. break;
  169. default:
  170. return NOTIFY_DONE;
  171. }
  172. return NOTIFY_OK;
  173. }
  174. static int isp1704_charger_get_property(struct power_supply *psy,
  175. enum power_supply_property psp,
  176. union power_supply_propval *val)
  177. {
  178. struct isp1704_charger *isp =
  179. container_of(psy, struct isp1704_charger, psy);
  180. switch (psp) {
  181. case POWER_SUPPLY_PROP_PRESENT:
  182. val->intval = isp->present;
  183. break;
  184. case POWER_SUPPLY_PROP_MODEL_NAME:
  185. val->strval = isp->model;
  186. break;
  187. case POWER_SUPPLY_PROP_MANUFACTURER:
  188. val->strval = "NXP";
  189. break;
  190. default:
  191. return -EINVAL;
  192. }
  193. return 0;
  194. }
  195. static enum power_supply_property power_props[] = {
  196. POWER_SUPPLY_PROP_PRESENT,
  197. POWER_SUPPLY_PROP_MODEL_NAME,
  198. POWER_SUPPLY_PROP_MANUFACTURER,
  199. };
  200. static inline int isp1704_test_ulpi(struct isp1704_charger *isp)
  201. {
  202. int vendor;
  203. int product;
  204. int i;
  205. int ret = -ENODEV;
  206. /* Test ULPI interface */
  207. ret = otg_io_write(isp->otg, ULPI_SCRATCH, 0xaa);
  208. if (ret < 0)
  209. return ret;
  210. ret = otg_io_read(isp->otg, ULPI_SCRATCH);
  211. if (ret < 0)
  212. return ret;
  213. if (ret != 0xaa)
  214. return -ENODEV;
  215. /* Verify the product and vendor id matches */
  216. vendor = otg_io_read(isp->otg, ULPI_VENDOR_ID_LOW);
  217. vendor |= otg_io_read(isp->otg, ULPI_VENDOR_ID_HIGH) << 8;
  218. if (vendor != NXP_VENDOR_ID)
  219. return -ENODEV;
  220. product = otg_io_read(isp->otg, ULPI_PRODUCT_ID_LOW);
  221. product |= otg_io_read(isp->otg, ULPI_PRODUCT_ID_HIGH) << 8;
  222. for (i = 0; i < ARRAY_SIZE(isp170x_id); i++) {
  223. if (product == isp170x_id[i]) {
  224. sprintf(isp->model, "isp%x", product);
  225. return product;
  226. }
  227. }
  228. dev_err(isp->dev, "product id %x not matching known ids", product);
  229. return -ENODEV;
  230. }
  231. static int __devinit isp1704_charger_probe(struct platform_device *pdev)
  232. {
  233. struct isp1704_charger *isp;
  234. int ret = -ENODEV;
  235. isp = kzalloc(sizeof *isp, GFP_KERNEL);
  236. if (!isp)
  237. return -ENOMEM;
  238. isp->otg = otg_get_transceiver();
  239. if (!isp->otg)
  240. goto fail0;
  241. ret = isp1704_test_ulpi(isp);
  242. if (ret < 0)
  243. goto fail1;
  244. isp->dev = &pdev->dev;
  245. platform_set_drvdata(pdev, isp);
  246. isp->psy.name = "isp1704";
  247. isp->psy.type = POWER_SUPPLY_TYPE_USB;
  248. isp->psy.properties = power_props;
  249. isp->psy.num_properties = ARRAY_SIZE(power_props);
  250. isp->psy.get_property = isp1704_charger_get_property;
  251. ret = power_supply_register(isp->dev, &isp->psy);
  252. if (ret)
  253. goto fail1;
  254. /*
  255. * REVISIT: using work in order to allow the otg notifications to be
  256. * made atomically in the future.
  257. */
  258. INIT_WORK(&isp->work, isp1704_charger_work);
  259. isp->nb.notifier_call = isp1704_notifier_call;
  260. ret = otg_register_notifier(isp->otg, &isp->nb);
  261. if (ret)
  262. goto fail2;
  263. dev_info(isp->dev, "registered with product id %s\n", isp->model);
  264. return 0;
  265. fail2:
  266. power_supply_unregister(&isp->psy);
  267. fail1:
  268. otg_put_transceiver(isp->otg);
  269. fail0:
  270. kfree(isp);
  271. dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret);
  272. return ret;
  273. }
  274. static int __devexit isp1704_charger_remove(struct platform_device *pdev)
  275. {
  276. struct isp1704_charger *isp = platform_get_drvdata(pdev);
  277. otg_unregister_notifier(isp->otg, &isp->nb);
  278. power_supply_unregister(&isp->psy);
  279. otg_put_transceiver(isp->otg);
  280. kfree(isp);
  281. return 0;
  282. }
  283. static struct platform_driver isp1704_charger_driver = {
  284. .driver = {
  285. .name = "isp1704_charger",
  286. },
  287. .probe = isp1704_charger_probe,
  288. .remove = __devexit_p(isp1704_charger_remove),
  289. };
  290. static int __init isp1704_charger_init(void)
  291. {
  292. return platform_driver_register(&isp1704_charger_driver);
  293. }
  294. module_init(isp1704_charger_init);
  295. static void __exit isp1704_charger_exit(void)
  296. {
  297. platform_driver_unregister(&isp1704_charger_driver);
  298. }
  299. module_exit(isp1704_charger_exit);
  300. MODULE_ALIAS("platform:isp1704_charger");
  301. MODULE_AUTHOR("Nokia Corporation");
  302. MODULE_DESCRIPTION("ISP170x USB Charger driver");
  303. MODULE_LICENSE("GPL");