isp1704_charger.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/usb/otg.h>
  32. #include <linux/usb/ulpi.h>
  33. #include <linux/usb/ch9.h>
  34. #include <linux/usb/gadget.h>
  35. #include <linux/power/isp1704_charger.h>
  36. /* Vendor specific Power Control register */
  37. #define ISP1704_PWR_CTRL 0x3d
  38. #define ISP1704_PWR_CTRL_SWCTRL (1 << 0)
  39. #define ISP1704_PWR_CTRL_DET_COMP (1 << 1)
  40. #define ISP1704_PWR_CTRL_BVALID_RISE (1 << 2)
  41. #define ISP1704_PWR_CTRL_BVALID_FALL (1 << 3)
  42. #define ISP1704_PWR_CTRL_DP_WKPU_EN (1 << 4)
  43. #define ISP1704_PWR_CTRL_VDAT_DET (1 << 5)
  44. #define ISP1704_PWR_CTRL_DPVSRC_EN (1 << 6)
  45. #define ISP1704_PWR_CTRL_HWDETECT (1 << 7)
  46. #define NXP_VENDOR_ID 0x04cc
  47. static u16 isp170x_id[] = {
  48. 0x1704,
  49. 0x1707,
  50. };
  51. struct isp1704_charger {
  52. struct device *dev;
  53. struct power_supply psy;
  54. struct usb_phy *phy;
  55. struct notifier_block nb;
  56. struct work_struct work;
  57. /* properties */
  58. char model[8];
  59. unsigned present:1;
  60. unsigned online:1;
  61. unsigned current_max;
  62. };
  63. static inline int isp1704_read(struct isp1704_charger *isp, u32 reg)
  64. {
  65. return usb_phy_io_read(isp->phy, reg);
  66. }
  67. static inline int isp1704_write(struct isp1704_charger *isp, u32 val, u32 reg)
  68. {
  69. return usb_phy_io_write(isp->phy, val, reg);
  70. }
  71. /*
  72. * Disable/enable the power from the isp1704 if a function for it
  73. * has been provided with platform data.
  74. */
  75. static void isp1704_charger_set_power(struct isp1704_charger *isp, bool on)
  76. {
  77. struct isp1704_charger_data *board = isp->dev->platform_data;
  78. if (board && board->set_power)
  79. board->set_power(on);
  80. }
  81. /*
  82. * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
  83. * chargers).
  84. *
  85. * REVISIT: The method is defined in Battery Charging Specification and is
  86. * applicable to any ULPI transceiver. Nothing isp170x specific here.
  87. */
  88. static inline int isp1704_charger_type(struct isp1704_charger *isp)
  89. {
  90. u8 reg;
  91. u8 func_ctrl;
  92. u8 otg_ctrl;
  93. int type = POWER_SUPPLY_TYPE_USB_DCP;
  94. func_ctrl = isp1704_read(isp, ULPI_FUNC_CTRL);
  95. otg_ctrl = isp1704_read(isp, ULPI_OTG_CTRL);
  96. /* disable pulldowns */
  97. reg = ULPI_OTG_CTRL_DM_PULLDOWN | ULPI_OTG_CTRL_DP_PULLDOWN;
  98. isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), reg);
  99. /* full speed */
  100. isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
  101. ULPI_FUNC_CTRL_XCVRSEL_MASK);
  102. isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL),
  103. ULPI_FUNC_CTRL_FULL_SPEED);
  104. /* Enable strong pull-up on DP (1.5K) and reset */
  105. reg = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
  106. isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), reg);
  107. usleep_range(1000, 2000);
  108. reg = isp1704_read(isp, ULPI_DEBUG);
  109. if ((reg & 3) != 3)
  110. type = POWER_SUPPLY_TYPE_USB_CDP;
  111. /* recover original state */
  112. isp1704_write(isp, ULPI_FUNC_CTRL, func_ctrl);
  113. isp1704_write(isp, ULPI_OTG_CTRL, otg_ctrl);
  114. return type;
  115. }
  116. /*
  117. * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
  118. * is actually a dedicated charger, the following steps need to be taken.
  119. */
  120. static inline int isp1704_charger_verify(struct isp1704_charger *isp)
  121. {
  122. int ret = 0;
  123. u8 r;
  124. /* Reset the transceiver */
  125. r = isp1704_read(isp, ULPI_FUNC_CTRL);
  126. r |= ULPI_FUNC_CTRL_RESET;
  127. isp1704_write(isp, ULPI_FUNC_CTRL, r);
  128. usleep_range(1000, 2000);
  129. /* Set normal mode */
  130. r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK);
  131. isp1704_write(isp, ULPI_FUNC_CTRL, r);
  132. /* Clear the DP and DM pull-down bits */
  133. r = ULPI_OTG_CTRL_DP_PULLDOWN | ULPI_OTG_CTRL_DM_PULLDOWN;
  134. isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), r);
  135. /* Enable strong pull-up on DP (1.5K) and reset */
  136. r = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
  137. isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), r);
  138. usleep_range(1000, 2000);
  139. /* Read the line state */
  140. if (!isp1704_read(isp, ULPI_DEBUG)) {
  141. /* Disable strong pull-up on DP (1.5K) */
  142. isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
  143. ULPI_FUNC_CTRL_TERMSELECT);
  144. return 1;
  145. }
  146. /* Is it a charger or PS/2 connection */
  147. /* Enable weak pull-up resistor on DP */
  148. isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
  149. ISP1704_PWR_CTRL_DP_WKPU_EN);
  150. /* Disable strong pull-up on DP (1.5K) */
  151. isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
  152. ULPI_FUNC_CTRL_TERMSELECT);
  153. /* Enable weak pull-down resistor on DM */
  154. isp1704_write(isp, ULPI_SET(ULPI_OTG_CTRL),
  155. ULPI_OTG_CTRL_DM_PULLDOWN);
  156. /* It's a charger if the line states are clear */
  157. if (!(isp1704_read(isp, ULPI_DEBUG)))
  158. ret = 1;
  159. /* Disable weak pull-up resistor on DP */
  160. isp1704_write(isp, ULPI_CLR(ISP1704_PWR_CTRL),
  161. ISP1704_PWR_CTRL_DP_WKPU_EN);
  162. return ret;
  163. }
  164. static inline int isp1704_charger_detect(struct isp1704_charger *isp)
  165. {
  166. unsigned long timeout;
  167. u8 pwr_ctrl;
  168. int ret = 0;
  169. pwr_ctrl = isp1704_read(isp, ISP1704_PWR_CTRL);
  170. /* set SW control bit in PWR_CTRL register */
  171. isp1704_write(isp, ISP1704_PWR_CTRL,
  172. ISP1704_PWR_CTRL_SWCTRL);
  173. /* enable manual charger detection */
  174. isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
  175. ISP1704_PWR_CTRL_SWCTRL
  176. | ISP1704_PWR_CTRL_DPVSRC_EN);
  177. usleep_range(1000, 2000);
  178. timeout = jiffies + msecs_to_jiffies(300);
  179. do {
  180. /* Check if there is a charger */
  181. if (isp1704_read(isp, ISP1704_PWR_CTRL)
  182. & ISP1704_PWR_CTRL_VDAT_DET) {
  183. ret = isp1704_charger_verify(isp);
  184. break;
  185. }
  186. } while (!time_after(jiffies, timeout) && isp->online);
  187. /* recover original state */
  188. isp1704_write(isp, ISP1704_PWR_CTRL, pwr_ctrl);
  189. return ret;
  190. }
  191. static inline int isp1704_charger_detect_dcp(struct isp1704_charger *isp)
  192. {
  193. if (isp1704_charger_detect(isp) &&
  194. isp1704_charger_type(isp) == POWER_SUPPLY_TYPE_USB_DCP)
  195. return true;
  196. else
  197. return false;
  198. }
  199. static void isp1704_charger_work(struct work_struct *data)
  200. {
  201. struct isp1704_charger *isp =
  202. container_of(data, struct isp1704_charger, work);
  203. static DEFINE_MUTEX(lock);
  204. mutex_lock(&lock);
  205. switch (isp->phy->last_event) {
  206. case USB_EVENT_VBUS:
  207. /* do not call wall charger detection more times */
  208. if (!isp->present) {
  209. isp->online = true;
  210. isp->present = 1;
  211. isp1704_charger_set_power(isp, 1);
  212. /* detect wall charger */
  213. if (isp1704_charger_detect_dcp(isp)) {
  214. isp->psy.type = POWER_SUPPLY_TYPE_USB_DCP;
  215. isp->current_max = 1800;
  216. } else {
  217. isp->psy.type = POWER_SUPPLY_TYPE_USB;
  218. isp->current_max = 500;
  219. }
  220. /* enable data pullups */
  221. if (isp->phy->otg->gadget)
  222. usb_gadget_connect(isp->phy->otg->gadget);
  223. }
  224. if (isp->psy.type != POWER_SUPPLY_TYPE_USB_DCP) {
  225. /*
  226. * Only 500mA here or high speed chirp
  227. * handshaking may break
  228. */
  229. if (isp->current_max > 500)
  230. isp->current_max = 500;
  231. if (isp->current_max > 100)
  232. isp->psy.type = POWER_SUPPLY_TYPE_USB_CDP;
  233. }
  234. break;
  235. case USB_EVENT_NONE:
  236. isp->online = false;
  237. isp->present = 0;
  238. isp->current_max = 0;
  239. isp->psy.type = POWER_SUPPLY_TYPE_USB;
  240. /*
  241. * Disable data pullups. We need to prevent the controller from
  242. * enumerating.
  243. *
  244. * FIXME: This is here to allow charger detection with Host/HUB
  245. * chargers. The pullups may be enabled elsewhere, so this can
  246. * not be the final solution.
  247. */
  248. if (isp->phy->otg->gadget)
  249. usb_gadget_disconnect(isp->phy->otg->gadget);
  250. isp1704_charger_set_power(isp, 0);
  251. break;
  252. default:
  253. goto out;
  254. }
  255. power_supply_changed(&isp->psy);
  256. out:
  257. mutex_unlock(&lock);
  258. }
  259. static int isp1704_notifier_call(struct notifier_block *nb,
  260. unsigned long val, void *v)
  261. {
  262. struct isp1704_charger *isp =
  263. container_of(nb, struct isp1704_charger, nb);
  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 = isp1704_write(isp, ULPI_SCRATCH, 0xaa);
  309. if (ret < 0)
  310. return ret;
  311. ret = isp1704_read(isp, 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 = isp1704_read(isp, ULPI_VENDOR_ID_LOW);
  318. vendor |= isp1704_read(isp, ULPI_VENDOR_ID_HIGH) << 8;
  319. if (vendor != NXP_VENDOR_ID)
  320. return -ENODEV;
  321. product = isp1704_read(isp, ULPI_PRODUCT_ID_LOW);
  322. product |= isp1704_read(isp, 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 isp1704_charger_probe(struct platform_device *pdev)
  333. {
  334. struct isp1704_charger *isp;
  335. int ret = -ENODEV;
  336. isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
  337. if (!isp)
  338. return -ENOMEM;
  339. isp->phy = usb_get_phy(USB_PHY_TYPE_USB2);
  340. if (IS_ERR_OR_NULL(isp->phy))
  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 usb 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 = usb_register_notifier(isp->phy, &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->phy->otg->gadget)
  374. usb_gadget_disconnect(isp->phy->otg->gadget);
  375. if (isp->phy->last_event == USB_EVENT_NONE)
  376. isp1704_charger_set_power(isp, 0);
  377. /* Detect charger if VBUS is valid (the cable was already plugged). */
  378. if (isp->phy->last_event == USB_EVENT_VBUS &&
  379. !isp->phy->otg->default_a)
  380. schedule_work(&isp->work);
  381. return 0;
  382. fail2:
  383. power_supply_unregister(&isp->psy);
  384. fail1:
  385. isp1704_charger_set_power(isp, 0);
  386. usb_put_phy(isp->phy);
  387. fail0:
  388. dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret);
  389. return ret;
  390. }
  391. static int isp1704_charger_remove(struct platform_device *pdev)
  392. {
  393. struct isp1704_charger *isp = platform_get_drvdata(pdev);
  394. usb_unregister_notifier(isp->phy, &isp->nb);
  395. power_supply_unregister(&isp->psy);
  396. usb_put_phy(isp->phy);
  397. isp1704_charger_set_power(isp, 0);
  398. return 0;
  399. }
  400. static struct platform_driver isp1704_charger_driver = {
  401. .driver = {
  402. .name = "isp1704_charger",
  403. },
  404. .probe = isp1704_charger_probe,
  405. .remove = isp1704_charger_remove,
  406. };
  407. module_platform_driver(isp1704_charger_driver);
  408. MODULE_ALIAS("platform:isp1704_charger");
  409. MODULE_AUTHOR("Nokia Corporation");
  410. MODULE_DESCRIPTION("ISP170x USB Charger driver");
  411. MODULE_LICENSE("GPL");