msm72k_otg.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /* Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/clk.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/err.h>
  25. #include <linux/delay.h>
  26. #include <linux/io.h>
  27. #include <linux/ioport.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/usb.h>
  32. #include <linux/usb/otg.h>
  33. #include <linux/usb/ulpi.h>
  34. #include <linux/usb/gadget.h>
  35. #include <linux/usb/hcd.h>
  36. #include <linux/usb/msm_hsusb.h>
  37. #include <linux/usb/msm_hsusb_hw.h>
  38. #include <mach/clk.h>
  39. #define MSM_USB_BASE (motg->regs)
  40. #define DRIVER_NAME "msm_otg"
  41. #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
  42. static int ulpi_read(struct otg_transceiver *otg, u32 reg)
  43. {
  44. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  45. int cnt = 0;
  46. /* initiate read operation */
  47. writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
  48. USB_ULPI_VIEWPORT);
  49. /* wait for completion */
  50. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  51. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  52. break;
  53. udelay(1);
  54. cnt++;
  55. }
  56. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  57. dev_err(otg->dev, "ulpi_read: timeout %08x\n",
  58. readl(USB_ULPI_VIEWPORT));
  59. return -ETIMEDOUT;
  60. }
  61. return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
  62. }
  63. static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
  64. {
  65. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  66. int cnt = 0;
  67. /* initiate write operation */
  68. writel(ULPI_RUN | ULPI_WRITE |
  69. ULPI_ADDR(reg) | ULPI_DATA(val),
  70. USB_ULPI_VIEWPORT);
  71. /* wait for completion */
  72. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  73. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  74. break;
  75. udelay(1);
  76. cnt++;
  77. }
  78. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  79. dev_err(otg->dev, "ulpi_write: timeout\n");
  80. return -ETIMEDOUT;
  81. }
  82. return 0;
  83. }
  84. static struct otg_io_access_ops msm_otg_io_ops = {
  85. .read = ulpi_read,
  86. .write = ulpi_write,
  87. };
  88. static void ulpi_init(struct msm_otg *motg)
  89. {
  90. struct msm_otg_platform_data *pdata = motg->pdata;
  91. int *seq = pdata->phy_init_seq;
  92. if (!seq)
  93. return;
  94. while (seq[0] >= 0) {
  95. dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
  96. seq[0], seq[1]);
  97. ulpi_write(&motg->otg, seq[0], seq[1]);
  98. seq += 2;
  99. }
  100. }
  101. static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
  102. {
  103. int ret;
  104. if (assert) {
  105. ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
  106. if (ret)
  107. dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
  108. } else {
  109. ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
  110. if (ret)
  111. dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
  112. }
  113. return ret;
  114. }
  115. static int msm_otg_phy_clk_reset(struct msm_otg *motg)
  116. {
  117. int ret;
  118. ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
  119. if (ret) {
  120. dev_err(motg->otg.dev, "usb phy clk assert failed\n");
  121. return ret;
  122. }
  123. usleep_range(10000, 12000);
  124. ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
  125. if (ret)
  126. dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
  127. return ret;
  128. }
  129. static int msm_otg_phy_reset(struct msm_otg *motg)
  130. {
  131. u32 val;
  132. int ret;
  133. int retries;
  134. ret = msm_otg_link_clk_reset(motg, 1);
  135. if (ret)
  136. return ret;
  137. ret = msm_otg_phy_clk_reset(motg);
  138. if (ret)
  139. return ret;
  140. ret = msm_otg_link_clk_reset(motg, 0);
  141. if (ret)
  142. return ret;
  143. val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
  144. writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
  145. for (retries = 3; retries > 0; retries--) {
  146. ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
  147. ULPI_CLR(ULPI_FUNC_CTRL));
  148. if (!ret)
  149. break;
  150. ret = msm_otg_phy_clk_reset(motg);
  151. if (ret)
  152. return ret;
  153. }
  154. if (!retries)
  155. return -ETIMEDOUT;
  156. /* This reset calibrates the phy, if the above write succeeded */
  157. ret = msm_otg_phy_clk_reset(motg);
  158. if (ret)
  159. return ret;
  160. for (retries = 3; retries > 0; retries--) {
  161. ret = ulpi_read(&motg->otg, ULPI_DEBUG);
  162. if (ret != -ETIMEDOUT)
  163. break;
  164. ret = msm_otg_phy_clk_reset(motg);
  165. if (ret)
  166. return ret;
  167. }
  168. if (!retries)
  169. return -ETIMEDOUT;
  170. dev_info(motg->otg.dev, "phy_reset: success\n");
  171. return 0;
  172. }
  173. #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
  174. static int msm_otg_reset(struct otg_transceiver *otg)
  175. {
  176. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  177. struct msm_otg_platform_data *pdata = motg->pdata;
  178. int cnt = 0;
  179. int ret;
  180. u32 val = 0;
  181. u32 ulpi_val = 0;
  182. ret = msm_otg_phy_reset(motg);
  183. if (ret) {
  184. dev_err(otg->dev, "phy_reset failed\n");
  185. return ret;
  186. }
  187. ulpi_init(motg);
  188. writel(USBCMD_RESET, USB_USBCMD);
  189. while (cnt < LINK_RESET_TIMEOUT_USEC) {
  190. if (!(readl(USB_USBCMD) & USBCMD_RESET))
  191. break;
  192. udelay(1);
  193. cnt++;
  194. }
  195. if (cnt >= LINK_RESET_TIMEOUT_USEC)
  196. return -ETIMEDOUT;
  197. /* select ULPI phy */
  198. writel(0x80000000, USB_PORTSC);
  199. msleep(100);
  200. writel(0x0, USB_AHBBURST);
  201. writel(0x00, USB_AHBMODE);
  202. if (pdata->otg_control == OTG_PHY_CONTROL) {
  203. val = readl(USB_OTGSC);
  204. if (pdata->mode == USB_OTG) {
  205. ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
  206. val |= OTGSC_IDIE | OTGSC_BSVIE;
  207. } else if (pdata->mode == USB_PERIPHERAL) {
  208. ulpi_val = ULPI_INT_SESS_VALID;
  209. val |= OTGSC_BSVIE;
  210. }
  211. writel(val, USB_OTGSC);
  212. ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
  213. ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
  214. }
  215. return 0;
  216. }
  217. static void msm_otg_start_host(struct otg_transceiver *otg, int on)
  218. {
  219. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  220. struct msm_otg_platform_data *pdata = motg->pdata;
  221. struct usb_hcd *hcd;
  222. if (!otg->host)
  223. return;
  224. hcd = bus_to_hcd(otg->host);
  225. if (on) {
  226. dev_dbg(otg->dev, "host on\n");
  227. if (pdata->vbus_power)
  228. pdata->vbus_power(1);
  229. /*
  230. * Some boards have a switch cotrolled by gpio
  231. * to enable/disable internal HUB. Enable internal
  232. * HUB before kicking the host.
  233. */
  234. if (pdata->setup_gpio)
  235. pdata->setup_gpio(OTG_STATE_A_HOST);
  236. #ifdef CONFIG_USB
  237. usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  238. #endif
  239. } else {
  240. dev_dbg(otg->dev, "host off\n");
  241. #ifdef CONFIG_USB
  242. usb_remove_hcd(hcd);
  243. #endif
  244. if (pdata->setup_gpio)
  245. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  246. if (pdata->vbus_power)
  247. pdata->vbus_power(0);
  248. }
  249. }
  250. static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
  251. {
  252. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  253. struct usb_hcd *hcd;
  254. /*
  255. * Fail host registration if this board can support
  256. * only peripheral configuration.
  257. */
  258. if (motg->pdata->mode == USB_PERIPHERAL) {
  259. dev_info(otg->dev, "Host mode is not supported\n");
  260. return -ENODEV;
  261. }
  262. if (!host) {
  263. if (otg->state == OTG_STATE_A_HOST) {
  264. msm_otg_start_host(otg, 0);
  265. otg->host = NULL;
  266. otg->state = OTG_STATE_UNDEFINED;
  267. schedule_work(&motg->sm_work);
  268. } else {
  269. otg->host = NULL;
  270. }
  271. return 0;
  272. }
  273. hcd = bus_to_hcd(host);
  274. hcd->power_budget = motg->pdata->power_budget;
  275. otg->host = host;
  276. dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
  277. /*
  278. * Kick the state machine work, if peripheral is not supported
  279. * or peripheral is already registered with us.
  280. */
  281. if (motg->pdata->mode == USB_HOST || otg->gadget)
  282. schedule_work(&motg->sm_work);
  283. return 0;
  284. }
  285. static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
  286. {
  287. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  288. struct msm_otg_platform_data *pdata = motg->pdata;
  289. if (!otg->gadget)
  290. return;
  291. if (on) {
  292. dev_dbg(otg->dev, "gadget on\n");
  293. /*
  294. * Some boards have a switch cotrolled by gpio
  295. * to enable/disable internal HUB. Disable internal
  296. * HUB before kicking the gadget.
  297. */
  298. if (pdata->setup_gpio)
  299. pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
  300. usb_gadget_vbus_connect(otg->gadget);
  301. } else {
  302. dev_dbg(otg->dev, "gadget off\n");
  303. usb_gadget_vbus_disconnect(otg->gadget);
  304. if (pdata->setup_gpio)
  305. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  306. }
  307. }
  308. static int msm_otg_set_peripheral(struct otg_transceiver *otg,
  309. struct usb_gadget *gadget)
  310. {
  311. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  312. /*
  313. * Fail peripheral registration if this board can support
  314. * only host configuration.
  315. */
  316. if (motg->pdata->mode == USB_HOST) {
  317. dev_info(otg->dev, "Peripheral mode is not supported\n");
  318. return -ENODEV;
  319. }
  320. if (!gadget) {
  321. if (otg->state == OTG_STATE_B_PERIPHERAL) {
  322. msm_otg_start_peripheral(otg, 0);
  323. otg->gadget = NULL;
  324. otg->state = OTG_STATE_UNDEFINED;
  325. schedule_work(&motg->sm_work);
  326. } else {
  327. otg->gadget = NULL;
  328. }
  329. return 0;
  330. }
  331. otg->gadget = gadget;
  332. dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
  333. /*
  334. * Kick the state machine work, if host is not supported
  335. * or host is already registered with us.
  336. */
  337. if (motg->pdata->mode == USB_PERIPHERAL || otg->host)
  338. schedule_work(&motg->sm_work);
  339. return 0;
  340. }
  341. /*
  342. * We support OTG, Peripheral only and Host only configurations. In case
  343. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  344. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  345. * enabled when switch is controlled by user and default mode is supplied
  346. * by board file, which can be changed by userspace later.
  347. */
  348. static void msm_otg_init_sm(struct msm_otg *motg)
  349. {
  350. struct msm_otg_platform_data *pdata = motg->pdata;
  351. u32 otgsc = readl(USB_OTGSC);
  352. switch (pdata->mode) {
  353. case USB_OTG:
  354. if (pdata->otg_control == OTG_PHY_CONTROL) {
  355. if (otgsc & OTGSC_ID)
  356. set_bit(ID, &motg->inputs);
  357. else
  358. clear_bit(ID, &motg->inputs);
  359. if (otgsc & OTGSC_BSV)
  360. set_bit(B_SESS_VLD, &motg->inputs);
  361. else
  362. clear_bit(B_SESS_VLD, &motg->inputs);
  363. } else if (pdata->otg_control == OTG_USER_CONTROL) {
  364. if (pdata->default_mode == USB_HOST) {
  365. clear_bit(ID, &motg->inputs);
  366. } else if (pdata->default_mode == USB_PERIPHERAL) {
  367. set_bit(ID, &motg->inputs);
  368. set_bit(B_SESS_VLD, &motg->inputs);
  369. } else {
  370. set_bit(ID, &motg->inputs);
  371. clear_bit(B_SESS_VLD, &motg->inputs);
  372. }
  373. }
  374. break;
  375. case USB_HOST:
  376. clear_bit(ID, &motg->inputs);
  377. break;
  378. case USB_PERIPHERAL:
  379. set_bit(ID, &motg->inputs);
  380. if (otgsc & OTGSC_BSV)
  381. set_bit(B_SESS_VLD, &motg->inputs);
  382. else
  383. clear_bit(B_SESS_VLD, &motg->inputs);
  384. break;
  385. default:
  386. break;
  387. }
  388. }
  389. static void msm_otg_sm_work(struct work_struct *w)
  390. {
  391. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  392. struct otg_transceiver *otg = &motg->otg;
  393. switch (otg->state) {
  394. case OTG_STATE_UNDEFINED:
  395. dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
  396. msm_otg_reset(otg);
  397. msm_otg_init_sm(motg);
  398. otg->state = OTG_STATE_B_IDLE;
  399. /* FALL THROUGH */
  400. case OTG_STATE_B_IDLE:
  401. dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
  402. if (!test_bit(ID, &motg->inputs) && otg->host) {
  403. /* disable BSV bit */
  404. writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
  405. msm_otg_start_host(otg, 1);
  406. otg->state = OTG_STATE_A_HOST;
  407. } else if (test_bit(B_SESS_VLD, &motg->inputs) && otg->gadget) {
  408. msm_otg_start_peripheral(otg, 1);
  409. otg->state = OTG_STATE_B_PERIPHERAL;
  410. }
  411. break;
  412. case OTG_STATE_B_PERIPHERAL:
  413. dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
  414. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  415. !test_bit(ID, &motg->inputs)) {
  416. msm_otg_start_peripheral(otg, 0);
  417. otg->state = OTG_STATE_B_IDLE;
  418. msm_otg_reset(otg);
  419. schedule_work(w);
  420. }
  421. break;
  422. case OTG_STATE_A_HOST:
  423. dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
  424. if (test_bit(ID, &motg->inputs)) {
  425. msm_otg_start_host(otg, 0);
  426. otg->state = OTG_STATE_B_IDLE;
  427. msm_otg_reset(otg);
  428. schedule_work(w);
  429. }
  430. break;
  431. default:
  432. break;
  433. }
  434. }
  435. static irqreturn_t msm_otg_irq(int irq, void *data)
  436. {
  437. struct msm_otg *motg = data;
  438. struct otg_transceiver *otg = &motg->otg;
  439. u32 otgsc = 0;
  440. otgsc = readl(USB_OTGSC);
  441. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  442. return IRQ_NONE;
  443. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  444. if (otgsc & OTGSC_ID)
  445. set_bit(ID, &motg->inputs);
  446. else
  447. clear_bit(ID, &motg->inputs);
  448. dev_dbg(otg->dev, "ID set/clear\n");
  449. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  450. if (otgsc & OTGSC_BSV)
  451. set_bit(B_SESS_VLD, &motg->inputs);
  452. else
  453. clear_bit(B_SESS_VLD, &motg->inputs);
  454. dev_dbg(otg->dev, "BSV set/clear\n");
  455. }
  456. writel(otgsc, USB_OTGSC);
  457. schedule_work(&motg->sm_work);
  458. return IRQ_HANDLED;
  459. }
  460. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  461. {
  462. struct msm_otg *motg = s->private;
  463. struct otg_transceiver *otg = &motg->otg;
  464. switch (otg->state) {
  465. case OTG_STATE_A_HOST:
  466. seq_printf(s, "host\n");
  467. break;
  468. case OTG_STATE_B_PERIPHERAL:
  469. seq_printf(s, "peripheral\n");
  470. break;
  471. default:
  472. seq_printf(s, "none\n");
  473. break;
  474. }
  475. return 0;
  476. }
  477. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  478. {
  479. return single_open(file, msm_otg_mode_show, inode->i_private);
  480. }
  481. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  482. size_t count, loff_t *ppos)
  483. {
  484. struct msm_otg *motg = file->private_data;
  485. char buf[16];
  486. struct otg_transceiver *otg = &motg->otg;
  487. int status = count;
  488. enum usb_mode_type req_mode;
  489. memset(buf, 0x00, sizeof(buf));
  490. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  491. status = -EFAULT;
  492. goto out;
  493. }
  494. if (!strncmp(buf, "host", 4)) {
  495. req_mode = USB_HOST;
  496. } else if (!strncmp(buf, "peripheral", 10)) {
  497. req_mode = USB_PERIPHERAL;
  498. } else if (!strncmp(buf, "none", 4)) {
  499. req_mode = USB_NONE;
  500. } else {
  501. status = -EINVAL;
  502. goto out;
  503. }
  504. switch (req_mode) {
  505. case USB_NONE:
  506. switch (otg->state) {
  507. case OTG_STATE_A_HOST:
  508. case OTG_STATE_B_PERIPHERAL:
  509. set_bit(ID, &motg->inputs);
  510. clear_bit(B_SESS_VLD, &motg->inputs);
  511. break;
  512. default:
  513. goto out;
  514. }
  515. break;
  516. case USB_PERIPHERAL:
  517. switch (otg->state) {
  518. case OTG_STATE_B_IDLE:
  519. case OTG_STATE_A_HOST:
  520. set_bit(ID, &motg->inputs);
  521. set_bit(B_SESS_VLD, &motg->inputs);
  522. break;
  523. default:
  524. goto out;
  525. }
  526. break;
  527. case USB_HOST:
  528. switch (otg->state) {
  529. case OTG_STATE_B_IDLE:
  530. case OTG_STATE_B_PERIPHERAL:
  531. clear_bit(ID, &motg->inputs);
  532. break;
  533. default:
  534. goto out;
  535. }
  536. break;
  537. default:
  538. goto out;
  539. }
  540. schedule_work(&motg->sm_work);
  541. out:
  542. return status;
  543. }
  544. const struct file_operations msm_otg_mode_fops = {
  545. .open = msm_otg_mode_open,
  546. .read = seq_read,
  547. .write = msm_otg_mode_write,
  548. .llseek = seq_lseek,
  549. .release = single_release,
  550. };
  551. static struct dentry *msm_otg_dbg_root;
  552. static struct dentry *msm_otg_dbg_mode;
  553. static int msm_otg_debugfs_init(struct msm_otg *motg)
  554. {
  555. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  556. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  557. return -ENODEV;
  558. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  559. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  560. if (!msm_otg_dbg_mode) {
  561. debugfs_remove(msm_otg_dbg_root);
  562. msm_otg_dbg_root = NULL;
  563. return -ENODEV;
  564. }
  565. return 0;
  566. }
  567. static void msm_otg_debugfs_cleanup(void)
  568. {
  569. debugfs_remove(msm_otg_dbg_mode);
  570. debugfs_remove(msm_otg_dbg_root);
  571. }
  572. static int __init msm_otg_probe(struct platform_device *pdev)
  573. {
  574. int ret = 0;
  575. struct resource *res;
  576. struct msm_otg *motg;
  577. struct otg_transceiver *otg;
  578. dev_info(&pdev->dev, "msm_otg probe\n");
  579. if (!pdev->dev.platform_data) {
  580. dev_err(&pdev->dev, "No platform data given. Bailing out\n");
  581. return -ENODEV;
  582. }
  583. motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
  584. if (!motg) {
  585. dev_err(&pdev->dev, "unable to allocate msm_otg\n");
  586. return -ENOMEM;
  587. }
  588. motg->pdata = pdev->dev.platform_data;
  589. otg = &motg->otg;
  590. otg->dev = &pdev->dev;
  591. motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
  592. if (IS_ERR(motg->phy_reset_clk)) {
  593. dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
  594. ret = PTR_ERR(motg->phy_reset_clk);
  595. goto free_motg;
  596. }
  597. motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
  598. if (IS_ERR(motg->clk)) {
  599. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  600. ret = PTR_ERR(motg->clk);
  601. goto put_phy_reset_clk;
  602. }
  603. motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
  604. if (IS_ERR(motg->pclk)) {
  605. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  606. ret = PTR_ERR(motg->pclk);
  607. goto put_clk;
  608. }
  609. /*
  610. * USB core clock is not present on all MSM chips. This
  611. * clock is introduced to remove the dependency on AXI
  612. * bus frequency.
  613. */
  614. motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
  615. if (IS_ERR(motg->core_clk))
  616. motg->core_clk = NULL;
  617. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  618. if (!res) {
  619. dev_err(&pdev->dev, "failed to get platform resource mem\n");
  620. ret = -ENODEV;
  621. goto put_core_clk;
  622. }
  623. motg->regs = ioremap(res->start, resource_size(res));
  624. if (!motg->regs) {
  625. dev_err(&pdev->dev, "ioremap failed\n");
  626. ret = -ENOMEM;
  627. goto put_core_clk;
  628. }
  629. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  630. motg->irq = platform_get_irq(pdev, 0);
  631. if (!motg->irq) {
  632. dev_err(&pdev->dev, "platform_get_irq failed\n");
  633. ret = -ENODEV;
  634. goto free_regs;
  635. }
  636. clk_enable(motg->clk);
  637. clk_enable(motg->pclk);
  638. if (motg->core_clk)
  639. clk_enable(motg->core_clk);
  640. writel(0, USB_USBINTR);
  641. writel(0, USB_OTGSC);
  642. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  643. ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
  644. "msm_otg", motg);
  645. if (ret) {
  646. dev_err(&pdev->dev, "request irq failed\n");
  647. goto disable_clks;
  648. }
  649. otg->init = msm_otg_reset;
  650. otg->set_host = msm_otg_set_host;
  651. otg->set_peripheral = msm_otg_set_peripheral;
  652. otg->io_ops = &msm_otg_io_ops;
  653. ret = otg_set_transceiver(&motg->otg);
  654. if (ret) {
  655. dev_err(&pdev->dev, "otg_set_transceiver failed\n");
  656. goto free_irq;
  657. }
  658. platform_set_drvdata(pdev, motg);
  659. device_init_wakeup(&pdev->dev, 1);
  660. if (motg->pdata->mode == USB_OTG &&
  661. motg->pdata->otg_control == OTG_USER_CONTROL) {
  662. ret = msm_otg_debugfs_init(motg);
  663. if (ret)
  664. dev_dbg(&pdev->dev, "mode debugfs file is"
  665. "not available\n");
  666. }
  667. return 0;
  668. free_irq:
  669. free_irq(motg->irq, motg);
  670. disable_clks:
  671. clk_disable(motg->pclk);
  672. clk_disable(motg->clk);
  673. free_regs:
  674. iounmap(motg->regs);
  675. put_core_clk:
  676. if (motg->core_clk)
  677. clk_put(motg->core_clk);
  678. clk_put(motg->pclk);
  679. put_clk:
  680. clk_put(motg->clk);
  681. put_phy_reset_clk:
  682. clk_put(motg->phy_reset_clk);
  683. free_motg:
  684. kfree(motg);
  685. return ret;
  686. }
  687. static int __devexit msm_otg_remove(struct platform_device *pdev)
  688. {
  689. struct msm_otg *motg = platform_get_drvdata(pdev);
  690. struct otg_transceiver *otg = &motg->otg;
  691. if (otg->host || otg->gadget)
  692. return -EBUSY;
  693. msm_otg_debugfs_cleanup();
  694. cancel_work_sync(&motg->sm_work);
  695. device_init_wakeup(&pdev->dev, 0);
  696. otg_set_transceiver(NULL);
  697. free_irq(motg->irq, motg);
  698. clk_disable(motg->pclk);
  699. clk_disable(motg->clk);
  700. if (motg->core_clk)
  701. clk_disable(motg->core_clk);
  702. iounmap(motg->regs);
  703. clk_put(motg->phy_reset_clk);
  704. clk_put(motg->pclk);
  705. clk_put(motg->clk);
  706. if (motg->core_clk)
  707. clk_put(motg->core_clk);
  708. kfree(motg);
  709. return 0;
  710. }
  711. static struct platform_driver msm_otg_driver = {
  712. .remove = __devexit_p(msm_otg_remove),
  713. .driver = {
  714. .name = DRIVER_NAME,
  715. .owner = THIS_MODULE,
  716. },
  717. };
  718. static int __init msm_otg_init(void)
  719. {
  720. return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
  721. }
  722. static void __exit msm_otg_exit(void)
  723. {
  724. platform_driver_unregister(&msm_otg_driver);
  725. }
  726. module_init(msm_otg_init);
  727. module_exit(msm_otg_exit);
  728. MODULE_LICENSE("GPL v2");
  729. MODULE_DESCRIPTION("MSM USB transceiver driver");