ab8500-usb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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/abx500/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 usb_phy phy;
  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 *phy_to_ab(struct usb_phy *x)
  79. {
  80. return container_of(x, struct ab8500_usb, phy);
  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_phy_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->phy.state = OTG_STATE_B_IDLE;
  153. ab->phy.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->phy.otg->gadget) {
  164. /* TODO: Enable regulators. */
  165. ab8500_usb_peri_phy_en(ab);
  166. v = ab->phy.otg->gadget;
  167. }
  168. event = USB_EVENT_VBUS;
  169. break;
  170. case USB_LINK_HM_IDGND:
  171. if (ab->phy.otg->host) {
  172. /* TODO: Enable regulators. */
  173. ab8500_usb_host_phy_en(ab);
  174. v = ab->phy.otg->host;
  175. }
  176. ab->phy.state = OTG_STATE_A_IDLE;
  177. ab->phy.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->phy.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->phy.otg->host)
  227. ab8500_usb_host_phy_dis(ab);
  228. if (!ab->phy.otg->gadget)
  229. ab8500_usb_peri_phy_dis(ab);
  230. }
  231. static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
  232. {
  233. struct ab8500_usb *ab;
  234. if (!phy)
  235. return -ENODEV;
  236. ab = phy_to_ab(phy);
  237. ab->vbus_draw = mA;
  238. if (mA)
  239. atomic_notifier_call_chain(&ab->phy.notifier,
  240. USB_EVENT_ENUMERATED, ab->phy.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 usb_phy *x, int suspend)
  247. {
  248. /* TODO */
  249. return 0;
  250. }
  251. static int ab8500_usb_set_peripheral(struct usb_otg *otg,
  252. struct usb_gadget *gadget)
  253. {
  254. struct ab8500_usb *ab;
  255. if (!otg)
  256. return -ENODEV;
  257. ab = phy_to_ab(otg->phy);
  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. otg->gadget = NULL;
  265. schedule_work(&ab->phy_dis_work);
  266. } else {
  267. otg->gadget = gadget;
  268. otg->phy->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 usb_otg *otg, struct usb_bus *host)
  278. {
  279. struct ab8500_usb *ab;
  280. if (!otg)
  281. return -ENODEV;
  282. ab = phy_to_ab(otg->phy);
  283. /* Some drivers call this function in atomic context.
  284. * Do not update ab8500 registers directly till this
  285. * is fixed.
  286. */
  287. if (!host) {
  288. /* TODO: Disable regulators. */
  289. otg->host = NULL;
  290. schedule_work(&ab->phy_dis_work);
  291. } else {
  292. otg->host = host;
  293. /* Phy will not be enabled if cable is already
  294. * plugged-in. Schedule to enable phy.
  295. * Use same delay to avoid any race condition.
  296. */
  297. schedule_delayed_work(&ab->dwork, ab->link_status_wait);
  298. }
  299. return 0;
  300. }
  301. static void ab8500_usb_irq_free(struct ab8500_usb *ab)
  302. {
  303. if (ab->rev < 0x20) {
  304. free_irq(ab->irq_num_id_rise, ab);
  305. free_irq(ab->irq_num_id_fall, ab);
  306. free_irq(ab->irq_num_vbus_rise, ab);
  307. free_irq(ab->irq_num_vbus_fall, ab);
  308. } else {
  309. free_irq(ab->irq_num_link_status, ab);
  310. }
  311. }
  312. static int ab8500_usb_v1x_res_setup(struct platform_device *pdev,
  313. struct ab8500_usb *ab)
  314. {
  315. int err;
  316. ab->irq_num_id_rise = platform_get_irq_byname(pdev, "ID_WAKEUP_R");
  317. if (ab->irq_num_id_rise < 0) {
  318. dev_err(&pdev->dev, "ID rise irq not found\n");
  319. return ab->irq_num_id_rise;
  320. }
  321. err = request_threaded_irq(ab->irq_num_id_rise, NULL,
  322. ab8500_usb_v1x_common_irq,
  323. IRQF_NO_SUSPEND | IRQF_SHARED,
  324. "usb-id-rise", ab);
  325. if (err < 0) {
  326. dev_err(ab->dev, "request_irq failed for ID rise irq\n");
  327. goto fail0;
  328. }
  329. ab->irq_num_id_fall = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
  330. if (ab->irq_num_id_fall < 0) {
  331. dev_err(&pdev->dev, "ID fall irq not found\n");
  332. return ab->irq_num_id_fall;
  333. }
  334. err = request_threaded_irq(ab->irq_num_id_fall, NULL,
  335. ab8500_usb_v1x_common_irq,
  336. IRQF_NO_SUSPEND | IRQF_SHARED,
  337. "usb-id-fall", ab);
  338. if (err < 0) {
  339. dev_err(ab->dev, "request_irq failed for ID fall irq\n");
  340. goto fail1;
  341. }
  342. ab->irq_num_vbus_rise = platform_get_irq_byname(pdev, "VBUS_DET_R");
  343. if (ab->irq_num_vbus_rise < 0) {
  344. dev_err(&pdev->dev, "VBUS rise irq not found\n");
  345. return ab->irq_num_vbus_rise;
  346. }
  347. err = request_threaded_irq(ab->irq_num_vbus_rise, NULL,
  348. ab8500_usb_v1x_common_irq,
  349. IRQF_NO_SUSPEND | IRQF_SHARED,
  350. "usb-vbus-rise", ab);
  351. if (err < 0) {
  352. dev_err(ab->dev, "request_irq failed for Vbus rise irq\n");
  353. goto fail2;
  354. }
  355. ab->irq_num_vbus_fall = platform_get_irq_byname(pdev, "VBUS_DET_F");
  356. if (ab->irq_num_vbus_fall < 0) {
  357. dev_err(&pdev->dev, "VBUS fall irq not found\n");
  358. return ab->irq_num_vbus_fall;
  359. }
  360. err = request_threaded_irq(ab->irq_num_vbus_fall, NULL,
  361. ab8500_usb_v1x_vbus_fall_irq,
  362. IRQF_NO_SUSPEND | IRQF_SHARED,
  363. "usb-vbus-fall", ab);
  364. if (err < 0) {
  365. dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
  366. goto fail3;
  367. }
  368. return 0;
  369. fail3:
  370. free_irq(ab->irq_num_vbus_rise, ab);
  371. fail2:
  372. free_irq(ab->irq_num_id_fall, ab);
  373. fail1:
  374. free_irq(ab->irq_num_id_rise, ab);
  375. fail0:
  376. return err;
  377. }
  378. static int ab8500_usb_v2_res_setup(struct platform_device *pdev,
  379. struct ab8500_usb *ab)
  380. {
  381. int err;
  382. ab->irq_num_link_status = platform_get_irq_byname(pdev,
  383. "USB_LINK_STATUS");
  384. if (ab->irq_num_link_status < 0) {
  385. dev_err(&pdev->dev, "Link status irq not found\n");
  386. return ab->irq_num_link_status;
  387. }
  388. err = request_threaded_irq(ab->irq_num_link_status, NULL,
  389. ab8500_usb_v20_irq,
  390. IRQF_NO_SUSPEND | IRQF_SHARED,
  391. "usb-link-status", ab);
  392. if (err < 0) {
  393. dev_err(ab->dev,
  394. "request_irq failed for link status irq\n");
  395. return err;
  396. }
  397. return 0;
  398. }
  399. static int __devinit ab8500_usb_probe(struct platform_device *pdev)
  400. {
  401. struct ab8500_usb *ab;
  402. struct usb_otg *otg;
  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. otg = kzalloc(sizeof *otg, GFP_KERNEL);
  417. if (!otg) {
  418. kfree(ab);
  419. return -ENOMEM;
  420. }
  421. ab->dev = &pdev->dev;
  422. ab->rev = rev;
  423. ab->phy.dev = ab->dev;
  424. ab->phy.otg = otg;
  425. ab->phy.label = "ab8500";
  426. ab->phy.set_suspend = ab8500_usb_set_suspend;
  427. ab->phy.set_power = ab8500_usb_set_power;
  428. ab->phy.state = OTG_STATE_UNDEFINED;
  429. otg->phy = &ab->phy;
  430. otg->set_host = ab8500_usb_set_host;
  431. otg->set_peripheral = ab8500_usb_set_peripheral;
  432. platform_set_drvdata(pdev, ab);
  433. ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
  434. /* v1: Wait for link status to become stable.
  435. * all: Updates form set_host and set_peripheral as they are atomic.
  436. */
  437. INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
  438. /* all: Disable phy when called from set_host and set_peripheral */
  439. INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
  440. if (ab->rev < 0x20) {
  441. err = ab8500_usb_v1x_res_setup(pdev, ab);
  442. ab->link_status_wait = AB8500_V1x_LINK_STAT_WAIT;
  443. } else {
  444. err = ab8500_usb_v2_res_setup(pdev, ab);
  445. }
  446. if (err < 0)
  447. goto fail0;
  448. err = usb_set_transceiver(&ab->phy);
  449. if (err) {
  450. dev_err(&pdev->dev, "Can't register transceiver\n");
  451. goto fail1;
  452. }
  453. dev_info(&pdev->dev, "AB8500 usb driver initialized\n");
  454. return 0;
  455. fail1:
  456. ab8500_usb_irq_free(ab);
  457. fail0:
  458. kfree(otg);
  459. kfree(ab);
  460. return err;
  461. }
  462. static int __devexit ab8500_usb_remove(struct platform_device *pdev)
  463. {
  464. struct ab8500_usb *ab = platform_get_drvdata(pdev);
  465. ab8500_usb_irq_free(ab);
  466. cancel_delayed_work_sync(&ab->dwork);
  467. cancel_work_sync(&ab->phy_dis_work);
  468. usb_set_transceiver(NULL);
  469. ab8500_usb_host_phy_dis(ab);
  470. ab8500_usb_peri_phy_dis(ab);
  471. platform_set_drvdata(pdev, NULL);
  472. kfree(ab->phy.otg);
  473. kfree(ab);
  474. return 0;
  475. }
  476. static struct platform_driver ab8500_usb_driver = {
  477. .probe = ab8500_usb_probe,
  478. .remove = __devexit_p(ab8500_usb_remove),
  479. .driver = {
  480. .name = "ab8500-usb",
  481. .owner = THIS_MODULE,
  482. },
  483. };
  484. static int __init ab8500_usb_init(void)
  485. {
  486. return platform_driver_register(&ab8500_usb_driver);
  487. }
  488. subsys_initcall(ab8500_usb_init);
  489. static void __exit ab8500_usb_exit(void)
  490. {
  491. platform_driver_unregister(&ab8500_usb_driver);
  492. }
  493. module_exit(ab8500_usb_exit);
  494. MODULE_ALIAS("platform:ab8500_usb");
  495. MODULE_AUTHOR("ST-Ericsson AB");
  496. MODULE_DESCRIPTION("AB8500 usb transceiver driver");
  497. MODULE_LICENSE("GPL");