isp1704_charger.c 13 KB

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