ab8500-usb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * drivers/usb/otg/ab8500_usb.c
  3. *
  4. * USB transceiver driver for AB8500 chip
  5. *
  6. * Copyright (C) 2010 ST-Ericsson AB
  7. * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/usb/otg.h>
  27. #include <linux/slab.h>
  28. #include <linux/notifier.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/mfd/abx500.h>
  32. #include <linux/mfd/ab8500.h>
  33. #define AB8500_MAIN_WD_CTRL_REG 0x01
  34. #define AB8500_USB_LINE_STAT_REG 0x80
  35. #define AB8500_USB_PHY_CTRL_REG 0x8A
  36. #define AB8500_BIT_OTG_STAT_ID (1 << 0)
  37. #define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
  38. #define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
  39. #define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
  40. #define AB8500_BIT_WD_CTRL_KICK (1 << 1)
  41. #define AB8500_V1x_LINK_STAT_WAIT (HZ/10)
  42. #define AB8500_WD_KICK_DELAY_US 100 /* usec */
  43. #define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
  44. #define AB8500_WD_V10_DISABLE_DELAY_MS 100 /* ms */
  45. /* Usb line status register */
  46. enum ab8500_usb_link_status {
  47. USB_LINK_NOT_CONFIGURED = 0,
  48. USB_LINK_STD_HOST_NC,
  49. USB_LINK_STD_HOST_C_NS,
  50. USB_LINK_STD_HOST_C_S,
  51. USB_LINK_HOST_CHG_NM,
  52. USB_LINK_HOST_CHG_HS,
  53. USB_LINK_HOST_CHG_HS_CHIRP,
  54. USB_LINK_DEDICATED_CHG,
  55. USB_LINK_ACA_RID_A,
  56. USB_LINK_ACA_RID_B,
  57. USB_LINK_ACA_RID_C_NM,
  58. USB_LINK_ACA_RID_C_HS,
  59. USB_LINK_ACA_RID_C_HS_CHIRP,
  60. USB_LINK_HM_IDGND,
  61. USB_LINK_RESERVED,
  62. USB_LINK_NOT_VALID_LINK
  63. };
  64. struct ab8500_usb {
  65. struct otg_transceiver otg;
  66. struct device *dev;
  67. int irq_num_id_rise;
  68. int irq_num_id_fall;
  69. int irq_num_vbus_rise;
  70. int irq_num_vbus_fall;
  71. int irq_num_link_status;
  72. unsigned vbus_draw;
  73. struct delayed_work dwork;
  74. struct work_struct phy_dis_work;
  75. unsigned long link_status_wait;
  76. int rev;
  77. };
  78. static inline struct ab8500_usb *xceiv_to_ab(struct otg_transceiver *x)
  79. {
  80. return container_of(x, struct ab8500_usb, otg);
  81. }
  82. static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
  83. {
  84. abx500_set_register_interruptible(ab->dev,
  85. AB8500_SYS_CTRL2_BLOCK,
  86. AB8500_MAIN_WD_CTRL_REG,
  87. AB8500_BIT_WD_CTRL_ENABLE);
  88. udelay(AB8500_WD_KICK_DELAY_US);
  89. abx500_set_register_interruptible(ab->dev,
  90. AB8500_SYS_CTRL2_BLOCK,
  91. AB8500_MAIN_WD_CTRL_REG,
  92. (AB8500_BIT_WD_CTRL_ENABLE
  93. | AB8500_BIT_WD_CTRL_KICK));
  94. if (ab->rev > 0x10) /* v1.1 v2.0 */
  95. udelay(AB8500_WD_V11_DISABLE_DELAY_US);
  96. else /* v1.0 */
  97. msleep(AB8500_WD_V10_DISABLE_DELAY_MS);
  98. abx500_set_register_interruptible(ab->dev,
  99. AB8500_SYS_CTRL2_BLOCK,
  100. AB8500_MAIN_WD_CTRL_REG,
  101. 0);
  102. }
  103. static void ab8500_usb_phy_ctrl(struct ab8500_usb *ab, bool sel_host,
  104. bool enable)
  105. {
  106. u8 ctrl_reg;
  107. abx500_get_register_interruptible(ab->dev,
  108. AB8500_USB,
  109. AB8500_USB_PHY_CTRL_REG,
  110. &ctrl_reg);
  111. if (sel_host) {
  112. if (enable)
  113. ctrl_reg |= AB8500_BIT_PHY_CTRL_HOST_EN;
  114. else
  115. ctrl_reg &= ~AB8500_BIT_PHY_CTRL_HOST_EN;
  116. } else {
  117. if (enable)
  118. ctrl_reg |= AB8500_BIT_PHY_CTRL_DEVICE_EN;
  119. else
  120. ctrl_reg &= ~AB8500_BIT_PHY_CTRL_DEVICE_EN;
  121. }
  122. abx500_set_register_interruptible(ab->dev,
  123. AB8500_USB,
  124. AB8500_USB_PHY_CTRL_REG,
  125. ctrl_reg);
  126. /* Needed to enable the phy.*/
  127. if (enable)
  128. ab8500_usb_wd_workaround(ab);
  129. }
  130. #define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_ctrl(ab, true, true)
  131. #define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_ctrl(ab, true, false)
  132. #define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_ctrl(ab, false, true)
  133. #define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_ctrl(ab, false, false)
  134. static int ab8500_usb_link_status_update(struct ab8500_usb *ab)
  135. {
  136. u8 reg;
  137. enum ab8500_usb_link_status lsts;
  138. void *v = NULL;
  139. enum usb_xceiv_events event;
  140. abx500_get_register_interruptible(ab->dev,
  141. AB8500_USB,
  142. AB8500_USB_LINE_STAT_REG,
  143. &reg);
  144. lsts = (reg >> 3) & 0x0F;
  145. switch (lsts) {
  146. case USB_LINK_NOT_CONFIGURED:
  147. case USB_LINK_RESERVED:
  148. case USB_LINK_NOT_VALID_LINK:
  149. /* TODO: Disable regulators. */
  150. ab8500_usb_host_phy_dis(ab);
  151. ab8500_usb_peri_phy_dis(ab);
  152. ab->otg.state = OTG_STATE_B_IDLE;
  153. ab->otg.default_a = false;
  154. ab->vbus_draw = 0;
  155. event = USB_EVENT_NONE;
  156. break;
  157. case USB_LINK_STD_HOST_NC:
  158. case USB_LINK_STD_HOST_C_NS:
  159. case USB_LINK_STD_HOST_C_S:
  160. case USB_LINK_HOST_CHG_NM:
  161. case USB_LINK_HOST_CHG_HS:
  162. case USB_LINK_HOST_CHG_HS_CHIRP:
  163. if (ab->otg.gadget) {
  164. /* TODO: Enable regulators. */
  165. ab8500_usb_peri_phy_en(ab);
  166. v = ab->otg.gadget;
  167. }
  168. event = USB_EVENT_VBUS;
  169. break;
  170. case USB_LINK_HM_IDGND:
  171. if (ab->otg.host) {
  172. /* TODO: Enable regulators. */
  173. ab8500_usb_host_phy_en(ab);
  174. v = ab->otg.host;
  175. }
  176. ab->otg.state = OTG_STATE_A_IDLE;
  177. ab->otg.default_a = true;
  178. event = USB_EVENT_ID;
  179. break;
  180. case USB_LINK_ACA_RID_A:
  181. case USB_LINK_ACA_RID_B:
  182. /* TODO */
  183. case USB_LINK_ACA_RID_C_NM:
  184. case USB_LINK_ACA_RID_C_HS:
  185. case USB_LINK_ACA_RID_C_HS_CHIRP:
  186. case USB_LINK_DEDICATED_CHG:
  187. /* TODO: vbus_draw */
  188. event = USB_EVENT_CHARGER;
  189. break;
  190. }
  191. atomic_notifier_call_chain(&ab->otg.notifier, event, v);
  192. return 0;
  193. }
  194. static void ab8500_usb_delayed_work(struct work_struct *work)
  195. {
  196. struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
  197. dwork.work);
  198. ab8500_usb_link_status_update(ab);
  199. }
  200. static irqreturn_t ab8500_usb_v1x_common_irq(int irq, void *data)
  201. {
  202. struct ab8500_usb *ab = (struct ab8500_usb *) data;
  203. /* Wait for link status to become stable. */
  204. schedule_delayed_work(&ab->dwork, ab->link_status_wait);
  205. return IRQ_HANDLED;
  206. }
  207. static irqreturn_t ab8500_usb_v1x_vbus_fall_irq(int irq, void *data)
  208. {
  209. struct ab8500_usb *ab = (struct ab8500_usb *) data;
  210. /* Link status will not be updated till phy is disabled. */
  211. ab8500_usb_peri_phy_dis(ab);
  212. /* Wait for link status to become stable. */
  213. schedule_delayed_work(&ab->dwork, ab->link_status_wait);
  214. return IRQ_HANDLED;
  215. }
  216. static irqreturn_t ab8500_usb_v20_irq(int irq, void *data)
  217. {
  218. struct ab8500_usb *ab = (struct ab8500_usb *) data;
  219. ab8500_usb_link_status_update(ab);
  220. return IRQ_HANDLED;
  221. }
  222. static void ab8500_usb_phy_disable_work(struct work_struct *work)
  223. {
  224. struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
  225. phy_dis_work);
  226. if (!ab->otg.host)
  227. ab8500_usb_host_phy_dis(ab);
  228. if (!ab->otg.gadget)
  229. ab8500_usb_peri_phy_dis(ab);
  230. }
  231. static int ab8500_usb_set_power(struct otg_transceiver *otg, unsigned mA)
  232. {
  233. struct ab8500_usb *ab;
  234. if (!otg)
  235. return -ENODEV;
  236. ab = xceiv_to_ab(otg);
  237. ab->vbus_draw = mA;
  238. if (mA)
  239. atomic_notifier_call_chain(&ab->otg.notifier,
  240. USB_EVENT_ENUMERATED, ab->otg.gadget);
  241. return 0;
  242. }
  243. /* TODO: Implement some way for charging or other drivers to read
  244. * ab->vbus_draw.
  245. */
  246. static int ab8500_usb_set_suspend(struct otg_transceiver *x, int suspend)
  247. {
  248. /* TODO */
  249. return 0;
  250. }
  251. static int ab8500_usb_set_peripheral(struct otg_transceiver *otg,
  252. struct usb_gadget *gadget)
  253. {
  254. struct ab8500_usb *ab;
  255. if (!otg)
  256. return -ENODEV;
  257. ab = xceiv_to_ab(otg);
  258. /* Some drivers call this function in atomic context.
  259. * Do not update ab8500 registers directly till this
  260. * is fixed.
  261. */
  262. if (!gadget) {
  263. /* TODO: Disable regulators. */
  264. ab->otg.gadget = NULL;
  265. schedule_work(&ab->phy_dis_work);
  266. } else {
  267. ab->otg.gadget = gadget;
  268. ab->otg.state = OTG_STATE_B_IDLE;
  269. /* Phy will not be enabled if cable is already
  270. * plugged-in. Schedule to enable phy.
  271. * Use same delay to avoid any race condition.
  272. */
  273. schedule_delayed_work(&ab->dwork, ab->link_status_wait);
  274. }
  275. return 0;
  276. }
  277. static int ab8500_usb_set_host(struct otg_transceiver *otg,
  278. struct usb_bus *host)
  279. {
  280. struct ab8500_usb *ab;
  281. if (!otg)
  282. return -ENODEV;
  283. ab = xceiv_to_ab(otg);
  284. /* Some drivers call this function in atomic context.
  285. * Do not update ab8500 registers directly till this
  286. * is fixed.
  287. */
  288. if (!host) {
  289. /* TODO: Disable regulators. */
  290. ab->otg.host = NULL;
  291. schedule_work(&ab->phy_dis_work);
  292. } else {
  293. ab->otg.host = host;
  294. /* Phy will not be enabled if cable is already
  295. * plugged-in. Schedule to enable phy.
  296. * Use same delay to avoid any race condition.
  297. */
  298. schedule_delayed_work(&ab->dwork, ab->link_status_wait);
  299. }
  300. return 0;
  301. }
  302. static void ab8500_usb_irq_free(struct ab8500_usb *ab)
  303. {
  304. if (ab->rev < 0x20) {
  305. free_irq(ab->irq_num_id_rise, ab);
  306. free_irq(ab->irq_num_id_fall, ab);
  307. free_irq(ab->irq_num_vbus_rise, ab);
  308. free_irq(ab->irq_num_vbus_fall, ab);
  309. } else {
  310. free_irq(ab->irq_num_link_status, ab);
  311. }
  312. }
  313. static int ab8500_usb_v1x_res_setup(struct platform_device *pdev,
  314. struct ab8500_usb *ab)
  315. {
  316. int err;
  317. ab->irq_num_id_rise = platform_get_irq_byname(pdev, "ID_WAKEUP_R");
  318. if (ab->irq_num_id_rise < 0) {
  319. dev_err(&pdev->dev, "ID rise irq not found\n");
  320. return ab->irq_num_id_rise;
  321. }
  322. err = request_threaded_irq(ab->irq_num_id_rise, NULL,
  323. ab8500_usb_v1x_common_irq,
  324. IRQF_NO_SUSPEND | IRQF_SHARED,
  325. "usb-id-rise", ab);
  326. if (err < 0) {
  327. dev_err(ab->dev, "request_irq failed for ID rise irq\n");
  328. goto fail0;
  329. }
  330. ab->irq_num_id_fall = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
  331. if (ab->irq_num_id_fall < 0) {
  332. dev_err(&pdev->dev, "ID fall irq not found\n");
  333. return ab->irq_num_id_fall;
  334. }
  335. err = request_threaded_irq(ab->irq_num_id_fall, NULL,
  336. ab8500_usb_v1x_common_irq,
  337. IRQF_NO_SUSPEND | IRQF_SHARED,
  338. "usb-id-fall", ab);
  339. if (err < 0) {
  340. dev_err(ab->dev, "request_irq failed for ID fall irq\n");
  341. goto fail1;
  342. }
  343. ab->irq_num_vbus_rise = platform_get_irq_byname(pdev, "VBUS_DET_R");
  344. if (ab->irq_num_vbus_rise < 0) {
  345. dev_err(&pdev->dev, "VBUS rise irq not found\n");
  346. return ab->irq_num_vbus_rise;
  347. }
  348. err = request_threaded_irq(ab->irq_num_vbus_rise, NULL,
  349. ab8500_usb_v1x_common_irq,
  350. IRQF_NO_SUSPEND | IRQF_SHARED,
  351. "usb-vbus-rise", ab);
  352. if (err < 0) {
  353. dev_err(ab->dev, "request_irq failed for Vbus rise irq\n");
  354. goto fail2;
  355. }
  356. ab->irq_num_vbus_fall = platform_get_irq_byname(pdev, "VBUS_DET_F");
  357. if (ab->irq_num_vbus_fall < 0) {
  358. dev_err(&pdev->dev, "VBUS fall irq not found\n");
  359. return ab->irq_num_vbus_fall;
  360. }
  361. err = request_threaded_irq(ab->irq_num_vbus_fall, NULL,
  362. ab8500_usb_v1x_vbus_fall_irq,
  363. IRQF_NO_SUSPEND | IRQF_SHARED,
  364. "usb-vbus-fall", ab);
  365. if (err < 0) {
  366. dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
  367. goto fail3;
  368. }
  369. return 0;
  370. fail3:
  371. free_irq(ab->irq_num_vbus_rise, ab);
  372. fail2:
  373. free_irq(ab->irq_num_id_fall, ab);
  374. fail1:
  375. free_irq(ab->irq_num_id_rise, ab);
  376. fail0:
  377. return err;
  378. }
  379. static int ab8500_usb_v2_res_setup(struct platform_device *pdev,
  380. struct ab8500_usb *ab)
  381. {
  382. int err;
  383. ab->irq_num_link_status = platform_get_irq_byname(pdev,
  384. "USB_LINK_STATUS");
  385. if (ab->irq_num_link_status < 0) {
  386. dev_err(&pdev->dev, "Link status irq not found\n");
  387. return ab->irq_num_link_status;
  388. }
  389. err = request_threaded_irq(ab->irq_num_link_status, NULL,
  390. ab8500_usb_v20_irq,
  391. IRQF_NO_SUSPEND | IRQF_SHARED,
  392. "usb-link-status", ab);
  393. if (err < 0) {
  394. dev_err(ab->dev,
  395. "request_irq failed for link status irq\n");
  396. return err;
  397. }
  398. return 0;
  399. }
  400. static int __devinit ab8500_usb_probe(struct platform_device *pdev)
  401. {
  402. struct ab8500_usb *ab;
  403. int err;
  404. int rev;
  405. rev = abx500_get_chip_id(&pdev->dev);
  406. if (rev < 0) {
  407. dev_err(&pdev->dev, "Chip id read failed\n");
  408. return rev;
  409. } else if (rev < 0x10) {
  410. dev_err(&pdev->dev, "Unsupported AB8500 chip\n");
  411. return -ENODEV;
  412. }
  413. ab = kzalloc(sizeof *ab, GFP_KERNEL);
  414. if (!ab)
  415. return -ENOMEM;
  416. ab->dev = &pdev->dev;
  417. ab->rev = rev;
  418. ab->otg.dev = ab->dev;
  419. ab->otg.label = "ab8500";
  420. ab->otg.state = OTG_STATE_UNDEFINED;
  421. ab->otg.set_host = ab8500_usb_set_host;
  422. ab->otg.set_peripheral = ab8500_usb_set_peripheral;
  423. ab->otg.set_suspend = ab8500_usb_set_suspend;
  424. ab->otg.set_power = ab8500_usb_set_power;
  425. platform_set_drvdata(pdev, ab);
  426. ATOMIC_INIT_NOTIFIER_HEAD(&ab->otg.notifier);
  427. /* v1: Wait for link status to become stable.
  428. * all: Updates form set_host and set_peripheral as they are atomic.
  429. */
  430. INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
  431. /* all: Disable phy when called from set_host and set_peripheral */
  432. INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
  433. if (ab->rev < 0x20) {
  434. err = ab8500_usb_v1x_res_setup(pdev, ab);
  435. ab->link_status_wait = AB8500_V1x_LINK_STAT_WAIT;
  436. } else {
  437. err = ab8500_usb_v2_res_setup(pdev, ab);
  438. }
  439. if (err < 0)
  440. goto fail0;
  441. err = otg_set_transceiver(&ab->otg);
  442. if (err) {
  443. dev_err(&pdev->dev, "Can't register transceiver\n");
  444. goto fail1;
  445. }
  446. dev_info(&pdev->dev, "AB8500 usb driver initialized\n");
  447. return 0;
  448. fail1:
  449. ab8500_usb_irq_free(ab);
  450. fail0:
  451. kfree(ab);
  452. return err;
  453. }
  454. static int __devexit ab8500_usb_remove(struct platform_device *pdev)
  455. {
  456. struct ab8500_usb *ab = platform_get_drvdata(pdev);
  457. ab8500_usb_irq_free(ab);
  458. cancel_delayed_work_sync(&ab->dwork);
  459. cancel_work_sync(&ab->phy_dis_work);
  460. otg_set_transceiver(NULL);
  461. ab8500_usb_host_phy_dis(ab);
  462. ab8500_usb_peri_phy_dis(ab);
  463. platform_set_drvdata(pdev, NULL);
  464. kfree(ab);
  465. return 0;
  466. }
  467. static struct platform_driver ab8500_usb_driver = {
  468. .probe = ab8500_usb_probe,
  469. .remove = __devexit_p(ab8500_usb_remove),
  470. .driver = {
  471. .name = "ab8500-usb",
  472. .owner = THIS_MODULE,
  473. },
  474. };
  475. static int __init ab8500_usb_init(void)
  476. {
  477. return platform_driver_register(&ab8500_usb_driver);
  478. }
  479. subsys_initcall(ab8500_usb_init);
  480. static void __exit ab8500_usb_exit(void)
  481. {
  482. platform_driver_unregister(&ab8500_usb_driver);
  483. }
  484. module_exit(ab8500_usb_exit);
  485. MODULE_ALIAS("platform:ab8500_usb");
  486. MODULE_AUTHOR("ST-Ericsson AB");
  487. MODULE_DESCRIPTION("AB8500 usb transceiver driver");
  488. MODULE_LICENSE("GPL");