PageRenderTime 1792ms CodeModel.GetById 13ms RepoModel.GetById 3ms app.codeStats 0ms

/drivers/usb/otg/isp1301_omap.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 1646 lines | 1186 code | 269 blank | 191 comment | 170 complexity | 5a07ee6f8d67e4011941c9a272e2e451 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * isp1301_omap - ISP 1301 USB transceiver, talking to OMAP OTG controller
  3. *
  4. * Copyright (C) 2004 Texas Instruments
  5. * Copyright (C) 2004 David Brownell
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/gpio.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include <linux/usb.h>
  31. #include <linux/usb/otg.h>
  32. #include <linux/i2c.h>
  33. #include <linux/workqueue.h>
  34. #include <asm/irq.h>
  35. #include <asm/mach-types.h>
  36. #include <plat/usb.h>
  37. #include <plat/mux.h>
  38. #ifndef DEBUG
  39. #undef VERBOSE
  40. #endif
  41. #define DRIVER_VERSION "24 August 2004"
  42. #define DRIVER_NAME (isp1301_driver.driver.name)
  43. MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
  44. MODULE_LICENSE("GPL");
  45. struct isp1301 {
  46. struct otg_transceiver otg;
  47. struct i2c_client *client;
  48. void (*i2c_release)(struct device *dev);
  49. int irq_type;
  50. u32 last_otg_ctrl;
  51. unsigned working:1;
  52. struct timer_list timer;
  53. /* use keventd context to change the state for us */
  54. struct work_struct work;
  55. unsigned long todo;
  56. # define WORK_UPDATE_ISP 0 /* update ISP from OTG */
  57. # define WORK_UPDATE_OTG 1 /* update OTG from ISP */
  58. # define WORK_HOST_RESUME 4 /* resume host */
  59. # define WORK_TIMER 6 /* timer fired */
  60. # define WORK_STOP 7 /* don't resubmit */
  61. };
  62. /* bits in OTG_CTRL */
  63. #define OTG_XCEIV_OUTPUTS \
  64. (OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
  65. #define OTG_XCEIV_INPUTS \
  66. (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
  67. #define OTG_CTRL_BITS \
  68. (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
  69. /* and OTG_PULLUP is sometimes written */
  70. #define OTG_CTRL_MASK (OTG_DRIVER_SEL| \
  71. OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
  72. OTG_CTRL_BITS)
  73. /*-------------------------------------------------------------------------*/
  74. /* board-specific PM hooks */
  75. #if defined(CONFIG_MACH_OMAP_H2) || defined(CONFIG_MACH_OMAP_H3)
  76. #if defined(CONFIG_TPS65010) || defined(CONFIG_TPS65010_MODULE)
  77. #include <linux/i2c/tps65010.h>
  78. #else
  79. static inline int tps65010_set_vbus_draw(unsigned mA)
  80. {
  81. pr_debug("tps65010: draw %d mA (STUB)\n", mA);
  82. return 0;
  83. }
  84. #endif
  85. static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
  86. {
  87. int status = tps65010_set_vbus_draw(mA);
  88. if (status < 0)
  89. pr_debug(" VBUS %d mA error %d\n", mA, status);
  90. }
  91. #else
  92. static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
  93. {
  94. /* H4 controls this by DIP switch S2.4; no soft control.
  95. * ON means the charger is always enabled. Leave it OFF
  96. * unless the OTG port is used only in B-peripheral mode.
  97. */
  98. }
  99. #endif
  100. static void enable_vbus_source(struct isp1301 *isp)
  101. {
  102. /* this board won't supply more than 8mA vbus power.
  103. * some boards can switch a 100ma "unit load" (or more).
  104. */
  105. }
  106. /* products will deliver OTG messages with LEDs, GUI, etc */
  107. static inline void notresponding(struct isp1301 *isp)
  108. {
  109. printk(KERN_NOTICE "OTG device not responding.\n");
  110. }
  111. /*-------------------------------------------------------------------------*/
  112. static struct i2c_driver isp1301_driver;
  113. /* smbus apis are used for portability */
  114. static inline u8
  115. isp1301_get_u8(struct isp1301 *isp, u8 reg)
  116. {
  117. return i2c_smbus_read_byte_data(isp->client, reg + 0);
  118. }
  119. static inline int
  120. isp1301_get_u16(struct isp1301 *isp, u8 reg)
  121. {
  122. return i2c_smbus_read_word_data(isp->client, reg);
  123. }
  124. static inline int
  125. isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
  126. {
  127. return i2c_smbus_write_byte_data(isp->client, reg + 0, bits);
  128. }
  129. static inline int
  130. isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
  131. {
  132. return i2c_smbus_write_byte_data(isp->client, reg + 1, bits);
  133. }
  134. /*-------------------------------------------------------------------------*/
  135. /* identification */
  136. #define ISP1301_VENDOR_ID 0x00 /* u16 read */
  137. #define ISP1301_PRODUCT_ID 0x02 /* u16 read */
  138. #define ISP1301_BCD_DEVICE 0x14 /* u16 read */
  139. #define I2C_VENDOR_ID_PHILIPS 0x04cc
  140. #define I2C_PRODUCT_ID_PHILIPS_1301 0x1301
  141. /* operational registers */
  142. #define ISP1301_MODE_CONTROL_1 0x04 /* u8 read, set, +1 clear */
  143. # define MC1_SPEED (1 << 0)
  144. # define MC1_SUSPEND (1 << 1)
  145. # define MC1_DAT_SE0 (1 << 2)
  146. # define MC1_TRANSPARENT (1 << 3)
  147. # define MC1_BDIS_ACON_EN (1 << 4)
  148. # define MC1_OE_INT_EN (1 << 5)
  149. # define MC1_UART_EN (1 << 6)
  150. # define MC1_MASK 0x7f
  151. #define ISP1301_MODE_CONTROL_2 0x12 /* u8 read, set, +1 clear */
  152. # define MC2_GLOBAL_PWR_DN (1 << 0)
  153. # define MC2_SPD_SUSP_CTRL (1 << 1)
  154. # define MC2_BI_DI (1 << 2)
  155. # define MC2_TRANSP_BDIR0 (1 << 3)
  156. # define MC2_TRANSP_BDIR1 (1 << 4)
  157. # define MC2_AUDIO_EN (1 << 5)
  158. # define MC2_PSW_EN (1 << 6)
  159. # define MC2_EN2V7 (1 << 7)
  160. #define ISP1301_OTG_CONTROL_1 0x06 /* u8 read, set, +1 clear */
  161. # define OTG1_DP_PULLUP (1 << 0)
  162. # define OTG1_DM_PULLUP (1 << 1)
  163. # define OTG1_DP_PULLDOWN (1 << 2)
  164. # define OTG1_DM_PULLDOWN (1 << 3)
  165. # define OTG1_ID_PULLDOWN (1 << 4)
  166. # define OTG1_VBUS_DRV (1 << 5)
  167. # define OTG1_VBUS_DISCHRG (1 << 6)
  168. # define OTG1_VBUS_CHRG (1 << 7)
  169. #define ISP1301_OTG_STATUS 0x10 /* u8 readonly */
  170. # define OTG_B_SESS_END (1 << 6)
  171. # define OTG_B_SESS_VLD (1 << 7)
  172. #define ISP1301_INTERRUPT_SOURCE 0x08 /* u8 read */
  173. #define ISP1301_INTERRUPT_LATCH 0x0A /* u8 read, set, +1 clear */
  174. #define ISP1301_INTERRUPT_FALLING 0x0C /* u8 read, set, +1 clear */
  175. #define ISP1301_INTERRUPT_RISING 0x0E /* u8 read, set, +1 clear */
  176. /* same bitfields in all interrupt registers */
  177. # define INTR_VBUS_VLD (1 << 0)
  178. # define INTR_SESS_VLD (1 << 1)
  179. # define INTR_DP_HI (1 << 2)
  180. # define INTR_ID_GND (1 << 3)
  181. # define INTR_DM_HI (1 << 4)
  182. # define INTR_ID_FLOAT (1 << 5)
  183. # define INTR_BDIS_ACON (1 << 6)
  184. # define INTR_CR_INT (1 << 7)
  185. /*-------------------------------------------------------------------------*/
  186. static inline const char *state_name(struct isp1301 *isp)
  187. {
  188. return otg_state_string(isp->otg.state);
  189. }
  190. /*-------------------------------------------------------------------------*/
  191. /* NOTE: some of this ISP1301 setup is specific to H2 boards;
  192. * not everything is guarded by board-specific checks, or even using
  193. * omap_usb_config data to deduce MC1_DAT_SE0 and MC2_BI_DI.
  194. *
  195. * ALSO: this currently doesn't use ISP1301 low-power modes
  196. * while OTG is running.
  197. */
  198. static void power_down(struct isp1301 *isp)
  199. {
  200. isp->otg.state = OTG_STATE_UNDEFINED;
  201. // isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
  202. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
  203. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_ID_PULLDOWN);
  204. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
  205. }
  206. static void power_up(struct isp1301 *isp)
  207. {
  208. // isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
  209. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
  210. /* do this only when cpu is driving transceiver,
  211. * so host won't see a low speed device...
  212. */
  213. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
  214. }
  215. #define NO_HOST_SUSPEND
  216. static int host_suspend(struct isp1301 *isp)
  217. {
  218. #ifdef NO_HOST_SUSPEND
  219. return 0;
  220. #else
  221. struct device *dev;
  222. if (!isp->otg.host)
  223. return -ENODEV;
  224. /* Currently ASSUMES only the OTG port matters;
  225. * other ports could be active...
  226. */
  227. dev = isp->otg.host->controller;
  228. return dev->driver->suspend(dev, 3, 0);
  229. #endif
  230. }
  231. static int host_resume(struct isp1301 *isp)
  232. {
  233. #ifdef NO_HOST_SUSPEND
  234. return 0;
  235. #else
  236. struct device *dev;
  237. if (!isp->otg.host)
  238. return -ENODEV;
  239. dev = isp->otg.host->controller;
  240. return dev->driver->resume(dev, 0);
  241. #endif
  242. }
  243. static int gadget_suspend(struct isp1301 *isp)
  244. {
  245. isp->otg.gadget->b_hnp_enable = 0;
  246. isp->otg.gadget->a_hnp_support = 0;
  247. isp->otg.gadget->a_alt_hnp_support = 0;
  248. return usb_gadget_vbus_disconnect(isp->otg.gadget);
  249. }
  250. /*-------------------------------------------------------------------------*/
  251. #define TIMER_MINUTES 10
  252. #define TIMER_JIFFIES (TIMER_MINUTES * 60 * HZ)
  253. /* Almost all our I2C messaging comes from a work queue's task context.
  254. * NOTE: guaranteeing certain response times might mean we shouldn't
  255. * share keventd's work queue; a realtime task might be safest.
  256. */
  257. static void isp1301_defer_work(struct isp1301 *isp, int work)
  258. {
  259. int status;
  260. if (isp && !test_and_set_bit(work, &isp->todo)) {
  261. (void) get_device(&isp->client->dev);
  262. status = schedule_work(&isp->work);
  263. if (!status && !isp->working)
  264. dev_vdbg(&isp->client->dev,
  265. "work item %d may be lost\n", work);
  266. }
  267. }
  268. /* called from irq handlers */
  269. static void a_idle(struct isp1301 *isp, const char *tag)
  270. {
  271. u32 l;
  272. if (isp->otg.state == OTG_STATE_A_IDLE)
  273. return;
  274. isp->otg.default_a = 1;
  275. if (isp->otg.host) {
  276. isp->otg.host->is_b_host = 0;
  277. host_suspend(isp);
  278. }
  279. if (isp->otg.gadget) {
  280. isp->otg.gadget->is_a_peripheral = 1;
  281. gadget_suspend(isp);
  282. }
  283. isp->otg.state = OTG_STATE_A_IDLE;
  284. l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
  285. omap_writel(l, OTG_CTRL);
  286. isp->last_otg_ctrl = l;
  287. pr_debug(" --> %s/%s\n", state_name(isp), tag);
  288. }
  289. /* called from irq handlers */
  290. static void b_idle(struct isp1301 *isp, const char *tag)
  291. {
  292. u32 l;
  293. if (isp->otg.state == OTG_STATE_B_IDLE)
  294. return;
  295. isp->otg.default_a = 0;
  296. if (isp->otg.host) {
  297. isp->otg.host->is_b_host = 1;
  298. host_suspend(isp);
  299. }
  300. if (isp->otg.gadget) {
  301. isp->otg.gadget->is_a_peripheral = 0;
  302. gadget_suspend(isp);
  303. }
  304. isp->otg.state = OTG_STATE_B_IDLE;
  305. l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
  306. omap_writel(l, OTG_CTRL);
  307. isp->last_otg_ctrl = l;
  308. pr_debug(" --> %s/%s\n", state_name(isp), tag);
  309. }
  310. static void
  311. dump_regs(struct isp1301 *isp, const char *label)
  312. {
  313. #ifdef DEBUG
  314. u8 ctrl = isp1301_get_u8(isp, ISP1301_OTG_CONTROL_1);
  315. u8 status = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
  316. u8 src = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
  317. pr_debug("otg: %06x, %s %s, otg/%02x stat/%02x.%02x\n",
  318. omap_readl(OTG_CTRL), label, state_name(isp),
  319. ctrl, status, src);
  320. /* mode control and irq enables don't change much */
  321. #endif
  322. }
  323. /*-------------------------------------------------------------------------*/
  324. #ifdef CONFIG_USB_OTG
  325. /*
  326. * The OMAP OTG controller handles most of the OTG state transitions.
  327. *
  328. * We translate isp1301 outputs (mostly voltage comparator status) into
  329. * OTG inputs; OTG outputs (mostly pullup/pulldown controls) and HNP state
  330. * flags into isp1301 inputs ... and infer state transitions.
  331. */
  332. #ifdef VERBOSE
  333. static void check_state(struct isp1301 *isp, const char *tag)
  334. {
  335. enum usb_otg_state state = OTG_STATE_UNDEFINED;
  336. u8 fsm = omap_readw(OTG_TEST) & 0x0ff;
  337. unsigned extra = 0;
  338. switch (fsm) {
  339. /* default-b */
  340. case 0x0:
  341. state = OTG_STATE_B_IDLE;
  342. break;
  343. case 0x3:
  344. case 0x7:
  345. extra = 1;
  346. case 0x1:
  347. state = OTG_STATE_B_PERIPHERAL;
  348. break;
  349. case 0x11:
  350. state = OTG_STATE_B_SRP_INIT;
  351. break;
  352. /* extra dual-role default-b states */
  353. case 0x12:
  354. case 0x13:
  355. case 0x16:
  356. extra = 1;
  357. case 0x17:
  358. state = OTG_STATE_B_WAIT_ACON;
  359. break;
  360. case 0x34:
  361. state = OTG_STATE_B_HOST;
  362. break;
  363. /* default-a */
  364. case 0x36:
  365. state = OTG_STATE_A_IDLE;
  366. break;
  367. case 0x3c:
  368. state = OTG_STATE_A_WAIT_VFALL;
  369. break;
  370. case 0x7d:
  371. state = OTG_STATE_A_VBUS_ERR;
  372. break;
  373. case 0x9e:
  374. case 0x9f:
  375. extra = 1;
  376. case 0x89:
  377. state = OTG_STATE_A_PERIPHERAL;
  378. break;
  379. case 0xb7:
  380. state = OTG_STATE_A_WAIT_VRISE;
  381. break;
  382. case 0xb8:
  383. state = OTG_STATE_A_WAIT_BCON;
  384. break;
  385. case 0xb9:
  386. state = OTG_STATE_A_HOST;
  387. break;
  388. case 0xba:
  389. state = OTG_STATE_A_SUSPEND;
  390. break;
  391. default:
  392. break;
  393. }
  394. if (isp->otg.state == state && !extra)
  395. return;
  396. pr_debug("otg: %s FSM %s/%02x, %s, %06x\n", tag,
  397. otg_state_string(state), fsm, state_name(isp),
  398. omap_readl(OTG_CTRL));
  399. }
  400. #else
  401. static inline void check_state(struct isp1301 *isp, const char *tag) { }
  402. #endif
  403. /* outputs from ISP1301_INTERRUPT_SOURCE */
  404. static void update_otg1(struct isp1301 *isp, u8 int_src)
  405. {
  406. u32 otg_ctrl;
  407. otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
  408. otg_ctrl &= ~OTG_XCEIV_INPUTS;
  409. otg_ctrl &= ~(OTG_ID|OTG_ASESSVLD|OTG_VBUSVLD);
  410. if (int_src & INTR_SESS_VLD)
  411. otg_ctrl |= OTG_ASESSVLD;
  412. else if (isp->otg.state == OTG_STATE_A_WAIT_VFALL) {
  413. a_idle(isp, "vfall");
  414. otg_ctrl &= ~OTG_CTRL_BITS;
  415. }
  416. if (int_src & INTR_VBUS_VLD)
  417. otg_ctrl |= OTG_VBUSVLD;
  418. if (int_src & INTR_ID_GND) { /* default-A */
  419. if (isp->otg.state == OTG_STATE_B_IDLE
  420. || isp->otg.state == OTG_STATE_UNDEFINED) {
  421. a_idle(isp, "init");
  422. return;
  423. }
  424. } else { /* default-B */
  425. otg_ctrl |= OTG_ID;
  426. if (isp->otg.state == OTG_STATE_A_IDLE
  427. || isp->otg.state == OTG_STATE_UNDEFINED) {
  428. b_idle(isp, "init");
  429. return;
  430. }
  431. }
  432. omap_writel(otg_ctrl, OTG_CTRL);
  433. }
  434. /* outputs from ISP1301_OTG_STATUS */
  435. static void update_otg2(struct isp1301 *isp, u8 otg_status)
  436. {
  437. u32 otg_ctrl;
  438. otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
  439. otg_ctrl &= ~OTG_XCEIV_INPUTS;
  440. otg_ctrl &= ~(OTG_BSESSVLD | OTG_BSESSEND);
  441. if (otg_status & OTG_B_SESS_VLD)
  442. otg_ctrl |= OTG_BSESSVLD;
  443. else if (otg_status & OTG_B_SESS_END)
  444. otg_ctrl |= OTG_BSESSEND;
  445. omap_writel(otg_ctrl, OTG_CTRL);
  446. }
  447. /* inputs going to ISP1301 */
  448. static void otg_update_isp(struct isp1301 *isp)
  449. {
  450. u32 otg_ctrl, otg_change;
  451. u8 set = OTG1_DM_PULLDOWN, clr = OTG1_DM_PULLUP;
  452. otg_ctrl = omap_readl(OTG_CTRL);
  453. otg_change = otg_ctrl ^ isp->last_otg_ctrl;
  454. isp->last_otg_ctrl = otg_ctrl;
  455. otg_ctrl = otg_ctrl & OTG_XCEIV_INPUTS;
  456. switch (isp->otg.state) {
  457. case OTG_STATE_B_IDLE:
  458. case OTG_STATE_B_PERIPHERAL:
  459. case OTG_STATE_B_SRP_INIT:
  460. if (!(otg_ctrl & OTG_PULLUP)) {
  461. // if (otg_ctrl & OTG_B_HNPEN) {
  462. if (isp->otg.gadget->b_hnp_enable) {
  463. isp->otg.state = OTG_STATE_B_WAIT_ACON;
  464. pr_debug(" --> b_wait_acon\n");
  465. }
  466. goto pulldown;
  467. }
  468. pullup:
  469. set |= OTG1_DP_PULLUP;
  470. clr |= OTG1_DP_PULLDOWN;
  471. break;
  472. case OTG_STATE_A_SUSPEND:
  473. case OTG_STATE_A_PERIPHERAL:
  474. if (otg_ctrl & OTG_PULLUP)
  475. goto pullup;
  476. /* FALLTHROUGH */
  477. // case OTG_STATE_B_WAIT_ACON:
  478. default:
  479. pulldown:
  480. set |= OTG1_DP_PULLDOWN;
  481. clr |= OTG1_DP_PULLUP;
  482. break;
  483. }
  484. # define toggle(OTG,ISP) do { \
  485. if (otg_ctrl & OTG) set |= ISP; \
  486. else clr |= ISP; \
  487. } while (0)
  488. if (!(isp->otg.host))
  489. otg_ctrl &= ~OTG_DRV_VBUS;
  490. switch (isp->otg.state) {
  491. case OTG_STATE_A_SUSPEND:
  492. if (otg_ctrl & OTG_DRV_VBUS) {
  493. set |= OTG1_VBUS_DRV;
  494. break;
  495. }
  496. /* HNP failed for some reason (A_AIDL_BDIS timeout) */
  497. notresponding(isp);
  498. /* FALLTHROUGH */
  499. case OTG_STATE_A_VBUS_ERR:
  500. isp->otg.state = OTG_STATE_A_WAIT_VFALL;
  501. pr_debug(" --> a_wait_vfall\n");
  502. /* FALLTHROUGH */
  503. case OTG_STATE_A_WAIT_VFALL:
  504. /* FIXME usbcore thinks port power is still on ... */
  505. clr |= OTG1_VBUS_DRV;
  506. break;
  507. case OTG_STATE_A_IDLE:
  508. if (otg_ctrl & OTG_DRV_VBUS) {
  509. isp->otg.state = OTG_STATE_A_WAIT_VRISE;
  510. pr_debug(" --> a_wait_vrise\n");
  511. }
  512. /* FALLTHROUGH */
  513. default:
  514. toggle(OTG_DRV_VBUS, OTG1_VBUS_DRV);
  515. }
  516. toggle(OTG_PU_VBUS, OTG1_VBUS_CHRG);
  517. toggle(OTG_PD_VBUS, OTG1_VBUS_DISCHRG);
  518. # undef toggle
  519. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, set);
  520. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, clr);
  521. /* HNP switch to host or peripheral; and SRP */
  522. if (otg_change & OTG_PULLUP) {
  523. u32 l;
  524. switch (isp->otg.state) {
  525. case OTG_STATE_B_IDLE:
  526. if (clr & OTG1_DP_PULLUP)
  527. break;
  528. isp->otg.state = OTG_STATE_B_PERIPHERAL;
  529. pr_debug(" --> b_peripheral\n");
  530. break;
  531. case OTG_STATE_A_SUSPEND:
  532. if (clr & OTG1_DP_PULLUP)
  533. break;
  534. isp->otg.state = OTG_STATE_A_PERIPHERAL;
  535. pr_debug(" --> a_peripheral\n");
  536. break;
  537. default:
  538. break;
  539. }
  540. l = omap_readl(OTG_CTRL);
  541. l |= OTG_PULLUP;
  542. omap_writel(l, OTG_CTRL);
  543. }
  544. check_state(isp, __func__);
  545. dump_regs(isp, "otg->isp1301");
  546. }
  547. static irqreturn_t omap_otg_irq(int irq, void *_isp)
  548. {
  549. u16 otg_irq = omap_readw(OTG_IRQ_SRC);
  550. u32 otg_ctrl;
  551. int ret = IRQ_NONE;
  552. struct isp1301 *isp = _isp;
  553. /* update ISP1301 transciever from OTG controller */
  554. if (otg_irq & OPRT_CHG) {
  555. omap_writew(OPRT_CHG, OTG_IRQ_SRC);
  556. isp1301_defer_work(isp, WORK_UPDATE_ISP);
  557. ret = IRQ_HANDLED;
  558. /* SRP to become b_peripheral failed */
  559. } else if (otg_irq & B_SRP_TMROUT) {
  560. pr_debug("otg: B_SRP_TIMEOUT, %06x\n", omap_readl(OTG_CTRL));
  561. notresponding(isp);
  562. /* gadget drivers that care should monitor all kinds of
  563. * remote wakeup (SRP, normal) using their own timer
  564. * to give "check cable and A-device" messages.
  565. */
  566. if (isp->otg.state == OTG_STATE_B_SRP_INIT)
  567. b_idle(isp, "srp_timeout");
  568. omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
  569. ret = IRQ_HANDLED;
  570. /* HNP to become b_host failed */
  571. } else if (otg_irq & B_HNP_FAIL) {
  572. pr_debug("otg: %s B_HNP_FAIL, %06x\n",
  573. state_name(isp), omap_readl(OTG_CTRL));
  574. notresponding(isp);
  575. otg_ctrl = omap_readl(OTG_CTRL);
  576. otg_ctrl |= OTG_BUSDROP;
  577. otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
  578. omap_writel(otg_ctrl, OTG_CTRL);
  579. /* subset of b_peripheral()... */
  580. isp->otg.state = OTG_STATE_B_PERIPHERAL;
  581. pr_debug(" --> b_peripheral\n");
  582. omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
  583. ret = IRQ_HANDLED;
  584. /* detect SRP from B-device ... */
  585. } else if (otg_irq & A_SRP_DETECT) {
  586. pr_debug("otg: %s SRP_DETECT, %06x\n",
  587. state_name(isp), omap_readl(OTG_CTRL));
  588. isp1301_defer_work(isp, WORK_UPDATE_OTG);
  589. switch (isp->otg.state) {
  590. case OTG_STATE_A_IDLE:
  591. if (!isp->otg.host)
  592. break;
  593. isp1301_defer_work(isp, WORK_HOST_RESUME);
  594. otg_ctrl = omap_readl(OTG_CTRL);
  595. otg_ctrl |= OTG_A_BUSREQ;
  596. otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
  597. & ~OTG_XCEIV_INPUTS
  598. & OTG_CTRL_MASK;
  599. omap_writel(otg_ctrl, OTG_CTRL);
  600. break;
  601. default:
  602. break;
  603. }
  604. omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
  605. ret = IRQ_HANDLED;
  606. /* timer expired: T(a_wait_bcon) and maybe T(a_wait_vrise)
  607. * we don't track them separately
  608. */
  609. } else if (otg_irq & A_REQ_TMROUT) {
  610. otg_ctrl = omap_readl(OTG_CTRL);
  611. pr_info("otg: BCON_TMOUT from %s, %06x\n",
  612. state_name(isp), otg_ctrl);
  613. notresponding(isp);
  614. otg_ctrl |= OTG_BUSDROP;
  615. otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
  616. omap_writel(otg_ctrl, OTG_CTRL);
  617. isp->otg.state = OTG_STATE_A_WAIT_VFALL;
  618. omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
  619. ret = IRQ_HANDLED;
  620. /* A-supplied voltage fell too low; overcurrent */
  621. } else if (otg_irq & A_VBUS_ERR) {
  622. otg_ctrl = omap_readl(OTG_CTRL);
  623. printk(KERN_ERR "otg: %s, VBUS_ERR %04x ctrl %06x\n",
  624. state_name(isp), otg_irq, otg_ctrl);
  625. otg_ctrl |= OTG_BUSDROP;
  626. otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
  627. omap_writel(otg_ctrl, OTG_CTRL);
  628. isp->otg.state = OTG_STATE_A_VBUS_ERR;
  629. omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
  630. ret = IRQ_HANDLED;
  631. /* switch driver; the transciever code activates it,
  632. * ungating the udc clock or resuming OHCI.
  633. */
  634. } else if (otg_irq & DRIVER_SWITCH) {
  635. int kick = 0;
  636. otg_ctrl = omap_readl(OTG_CTRL);
  637. printk(KERN_NOTICE "otg: %s, SWITCH to %s, ctrl %06x\n",
  638. state_name(isp),
  639. (otg_ctrl & OTG_DRIVER_SEL)
  640. ? "gadget" : "host",
  641. otg_ctrl);
  642. isp1301_defer_work(isp, WORK_UPDATE_ISP);
  643. /* role is peripheral */
  644. if (otg_ctrl & OTG_DRIVER_SEL) {
  645. switch (isp->otg.state) {
  646. case OTG_STATE_A_IDLE:
  647. b_idle(isp, __func__);
  648. break;
  649. default:
  650. break;
  651. }
  652. isp1301_defer_work(isp, WORK_UPDATE_ISP);
  653. /* role is host */
  654. } else {
  655. if (!(otg_ctrl & OTG_ID)) {
  656. otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
  657. omap_writel(otg_ctrl | OTG_A_BUSREQ, OTG_CTRL);
  658. }
  659. if (isp->otg.host) {
  660. switch (isp->otg.state) {
  661. case OTG_STATE_B_WAIT_ACON:
  662. isp->otg.state = OTG_STATE_B_HOST;
  663. pr_debug(" --> b_host\n");
  664. kick = 1;
  665. break;
  666. case OTG_STATE_A_WAIT_BCON:
  667. isp->otg.state = OTG_STATE_A_HOST;
  668. pr_debug(" --> a_host\n");
  669. break;
  670. case OTG_STATE_A_PERIPHERAL:
  671. isp->otg.state = OTG_STATE_A_WAIT_BCON;
  672. pr_debug(" --> a_wait_bcon\n");
  673. break;
  674. default:
  675. break;
  676. }
  677. isp1301_defer_work(isp, WORK_HOST_RESUME);
  678. }
  679. }
  680. omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
  681. ret = IRQ_HANDLED;
  682. if (kick)
  683. usb_bus_start_enum(isp->otg.host,
  684. isp->otg.host->otg_port);
  685. }
  686. check_state(isp, __func__);
  687. return ret;
  688. }
  689. static struct platform_device *otg_dev;
  690. static int isp1301_otg_init(struct isp1301 *isp)
  691. {
  692. u32 l;
  693. if (!otg_dev)
  694. return -ENODEV;
  695. dump_regs(isp, __func__);
  696. /* some of these values are board-specific... */
  697. l = omap_readl(OTG_SYSCON_2);
  698. l |= OTG_EN
  699. /* for B-device: */
  700. | SRP_GPDATA /* 9msec Bdev D+ pulse */
  701. | SRP_GPDVBUS /* discharge after VBUS pulse */
  702. // | (3 << 24) /* 2msec VBUS pulse */
  703. /* for A-device: */
  704. | (0 << 20) /* 200ms nominal A_WAIT_VRISE timer */
  705. | SRP_DPW /* detect 167+ns SRP pulses */
  706. | SRP_DATA | SRP_VBUS /* accept both kinds of SRP pulse */
  707. ;
  708. omap_writel(l, OTG_SYSCON_2);
  709. update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
  710. update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
  711. check_state(isp, __func__);
  712. pr_debug("otg: %s, %s %06x\n",
  713. state_name(isp), __func__, omap_readl(OTG_CTRL));
  714. omap_writew(DRIVER_SWITCH | OPRT_CHG
  715. | B_SRP_TMROUT | B_HNP_FAIL
  716. | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT, OTG_IRQ_EN);
  717. l = omap_readl(OTG_SYSCON_2);
  718. l |= OTG_EN;
  719. omap_writel(l, OTG_SYSCON_2);
  720. return 0;
  721. }
  722. static int otg_probe(struct platform_device *dev)
  723. {
  724. // struct omap_usb_config *config = dev->platform_data;
  725. otg_dev = dev;
  726. return 0;
  727. }
  728. static int otg_remove(struct platform_device *dev)
  729. {
  730. otg_dev = NULL;
  731. return 0;
  732. }
  733. static struct platform_driver omap_otg_driver = {
  734. .probe = otg_probe,
  735. .remove = otg_remove,
  736. .driver = {
  737. .owner = THIS_MODULE,
  738. .name = "omap_otg",
  739. },
  740. };
  741. static int otg_bind(struct isp1301 *isp)
  742. {
  743. int status;
  744. if (otg_dev)
  745. return -EBUSY;
  746. status = platform_driver_register(&omap_otg_driver);
  747. if (status < 0)
  748. return status;
  749. if (otg_dev)
  750. status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
  751. IRQF_DISABLED, DRIVER_NAME, isp);
  752. else
  753. status = -ENODEV;
  754. if (status < 0)
  755. platform_driver_unregister(&omap_otg_driver);
  756. return status;
  757. }
  758. static void otg_unbind(struct isp1301 *isp)
  759. {
  760. if (!otg_dev)
  761. return;
  762. free_irq(otg_dev->resource[1].start, isp);
  763. }
  764. #else
  765. /* OTG controller isn't clocked */
  766. #endif /* CONFIG_USB_OTG */
  767. /*-------------------------------------------------------------------------*/
  768. static void b_peripheral(struct isp1301 *isp)
  769. {
  770. u32 l;
  771. l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
  772. omap_writel(l, OTG_CTRL);
  773. usb_gadget_vbus_connect(isp->otg.gadget);
  774. #ifdef CONFIG_USB_OTG
  775. enable_vbus_draw(isp, 8);
  776. otg_update_isp(isp);
  777. #else
  778. enable_vbus_draw(isp, 100);
  779. /* UDC driver just set OTG_BSESSVLD */
  780. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
  781. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
  782. isp->otg.state = OTG_STATE_B_PERIPHERAL;
  783. pr_debug(" --> b_peripheral\n");
  784. dump_regs(isp, "2periph");
  785. #endif
  786. }
  787. static void isp_update_otg(struct isp1301 *isp, u8 stat)
  788. {
  789. u8 isp_stat, isp_bstat;
  790. enum usb_otg_state state = isp->otg.state;
  791. if (stat & INTR_BDIS_ACON)
  792. pr_debug("OTG: BDIS_ACON, %s\n", state_name(isp));
  793. /* start certain state transitions right away */
  794. isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
  795. if (isp_stat & INTR_ID_GND) {
  796. if (isp->otg.default_a) {
  797. switch (state) {
  798. case OTG_STATE_B_IDLE:
  799. a_idle(isp, "idle");
  800. /* FALLTHROUGH */
  801. case OTG_STATE_A_IDLE:
  802. enable_vbus_source(isp);
  803. /* FALLTHROUGH */
  804. case OTG_STATE_A_WAIT_VRISE:
  805. /* we skip over OTG_STATE_A_WAIT_BCON, since
  806. * the HC will transition to A_HOST (or
  807. * A_SUSPEND!) without our noticing except
  808. * when HNP is used.
  809. */
  810. if (isp_stat & INTR_VBUS_VLD)
  811. isp->otg.state = OTG_STATE_A_HOST;
  812. break;
  813. case OTG_STATE_A_WAIT_VFALL:
  814. if (!(isp_stat & INTR_SESS_VLD))
  815. a_idle(isp, "vfell");
  816. break;
  817. default:
  818. if (!(isp_stat & INTR_VBUS_VLD))
  819. isp->otg.state = OTG_STATE_A_VBUS_ERR;
  820. break;
  821. }
  822. isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
  823. } else {
  824. switch (state) {
  825. case OTG_STATE_B_PERIPHERAL:
  826. case OTG_STATE_B_HOST:
  827. case OTG_STATE_B_WAIT_ACON:
  828. usb_gadget_vbus_disconnect(isp->otg.gadget);
  829. break;
  830. default:
  831. break;
  832. }
  833. if (state != OTG_STATE_A_IDLE)
  834. a_idle(isp, "id");
  835. if (isp->otg.host && state == OTG_STATE_A_IDLE)
  836. isp1301_defer_work(isp, WORK_HOST_RESUME);
  837. isp_bstat = 0;
  838. }
  839. } else {
  840. u32 l;
  841. /* if user unplugged mini-A end of cable,
  842. * don't bypass A_WAIT_VFALL.
  843. */
  844. if (isp->otg.default_a) {
  845. switch (state) {
  846. default:
  847. isp->otg.state = OTG_STATE_A_WAIT_VFALL;
  848. break;
  849. case OTG_STATE_A_WAIT_VFALL:
  850. state = OTG_STATE_A_IDLE;
  851. /* khubd may take a while to notice and
  852. * handle this disconnect, so don't go
  853. * to B_IDLE quite yet.
  854. */
  855. break;
  856. case OTG_STATE_A_IDLE:
  857. host_suspend(isp);
  858. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
  859. MC1_BDIS_ACON_EN);
  860. isp->otg.state = OTG_STATE_B_IDLE;
  861. l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
  862. l &= ~OTG_CTRL_BITS;
  863. omap_writel(l, OTG_CTRL);
  864. break;
  865. case OTG_STATE_B_IDLE:
  866. break;
  867. }
  868. }
  869. isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
  870. switch (isp->otg.state) {
  871. case OTG_STATE_B_PERIPHERAL:
  872. case OTG_STATE_B_WAIT_ACON:
  873. case OTG_STATE_B_HOST:
  874. if (likely(isp_bstat & OTG_B_SESS_VLD))
  875. break;
  876. enable_vbus_draw(isp, 0);
  877. #ifndef CONFIG_USB_OTG
  878. /* UDC driver will clear OTG_BSESSVLD */
  879. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
  880. OTG1_DP_PULLDOWN);
  881. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
  882. OTG1_DP_PULLUP);
  883. dump_regs(isp, __func__);
  884. #endif
  885. /* FALLTHROUGH */
  886. case OTG_STATE_B_SRP_INIT:
  887. b_idle(isp, __func__);
  888. l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
  889. omap_writel(l, OTG_CTRL);
  890. /* FALLTHROUGH */
  891. case OTG_STATE_B_IDLE:
  892. if (isp->otg.gadget && (isp_bstat & OTG_B_SESS_VLD)) {
  893. #ifdef CONFIG_USB_OTG
  894. update_otg1(isp, isp_stat);
  895. update_otg2(isp, isp_bstat);
  896. #endif
  897. b_peripheral(isp);
  898. } else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
  899. isp_bstat |= OTG_B_SESS_END;
  900. break;
  901. case OTG_STATE_A_WAIT_VFALL:
  902. break;
  903. default:
  904. pr_debug("otg: unsupported b-device %s\n",
  905. state_name(isp));
  906. break;
  907. }
  908. }
  909. if (state != isp->otg.state)
  910. pr_debug(" isp, %s -> %s\n",
  911. otg_state_string(state), state_name(isp));
  912. #ifdef CONFIG_USB_OTG
  913. /* update the OTG controller state to match the isp1301; may
  914. * trigger OPRT_CHG irqs for changes going to the isp1301.
  915. */
  916. update_otg1(isp, isp_stat);
  917. update_otg2(isp, isp_bstat);
  918. check_state(isp, __func__);
  919. #endif
  920. dump_regs(isp, "isp1301->otg");
  921. }
  922. /*-------------------------------------------------------------------------*/
  923. static u8 isp1301_clear_latch(struct isp1301 *isp)
  924. {
  925. u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
  926. isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
  927. return latch;
  928. }
  929. static void
  930. isp1301_work(struct work_struct *work)
  931. {
  932. struct isp1301 *isp = container_of(work, struct isp1301, work);
  933. int stop;
  934. /* implicit lock: we're the only task using this device */
  935. isp->working = 1;
  936. do {
  937. stop = test_bit(WORK_STOP, &isp->todo);
  938. #ifdef CONFIG_USB_OTG
  939. /* transfer state from otg engine to isp1301 */
  940. if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
  941. otg_update_isp(isp);
  942. put_device(&isp->client->dev);
  943. }
  944. #endif
  945. /* transfer state from isp1301 to otg engine */
  946. if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
  947. u8 stat = isp1301_clear_latch(isp);
  948. isp_update_otg(isp, stat);
  949. put_device(&isp->client->dev);
  950. }
  951. if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
  952. u32 otg_ctrl;
  953. /*
  954. * skip A_WAIT_VRISE; hc transitions invisibly
  955. * skip A_WAIT_BCON; same.
  956. */
  957. switch (isp->otg.state) {
  958. case OTG_STATE_A_WAIT_BCON:
  959. case OTG_STATE_A_WAIT_VRISE:
  960. isp->otg.state = OTG_STATE_A_HOST;
  961. pr_debug(" --> a_host\n");
  962. otg_ctrl = omap_readl(OTG_CTRL);
  963. otg_ctrl |= OTG_A_BUSREQ;
  964. otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
  965. & OTG_CTRL_MASK;
  966. omap_writel(otg_ctrl, OTG_CTRL);
  967. break;
  968. case OTG_STATE_B_WAIT_ACON:
  969. isp->otg.state = OTG_STATE_B_HOST;
  970. pr_debug(" --> b_host (acon)\n");
  971. break;
  972. case OTG_STATE_B_HOST:
  973. case OTG_STATE_B_IDLE:
  974. case OTG_STATE_A_IDLE:
  975. break;
  976. default:
  977. pr_debug(" host resume in %s\n",
  978. state_name(isp));
  979. }
  980. host_resume(isp);
  981. // mdelay(10);
  982. put_device(&isp->client->dev);
  983. }
  984. if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
  985. #ifdef VERBOSE
  986. dump_regs(isp, "timer");
  987. if (!stop)
  988. mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
  989. #endif
  990. put_device(&isp->client->dev);
  991. }
  992. if (isp->todo)
  993. dev_vdbg(&isp->client->dev,
  994. "work done, todo = 0x%lx\n",
  995. isp->todo);
  996. if (stop) {
  997. dev_dbg(&isp->client->dev, "stop\n");
  998. break;
  999. }
  1000. } while (isp->todo);
  1001. isp->working = 0;
  1002. }
  1003. static irqreturn_t isp1301_irq(int irq, void *isp)
  1004. {
  1005. isp1301_defer_work(isp, WORK_UPDATE_OTG);
  1006. return IRQ_HANDLED;
  1007. }
  1008. static void isp1301_timer(unsigned long _isp)
  1009. {
  1010. isp1301_defer_work((void *)_isp, WORK_TIMER);
  1011. }
  1012. /*-------------------------------------------------------------------------*/
  1013. static void isp1301_release(struct device *dev)
  1014. {
  1015. struct isp1301 *isp;
  1016. isp = dev_get_drvdata(dev);
  1017. /* FIXME -- not with a "new style" driver, it doesn't!! */
  1018. /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
  1019. if (isp->i2c_release)
  1020. isp->i2c_release(dev);
  1021. kfree (isp);
  1022. }
  1023. static struct isp1301 *the_transceiver;
  1024. static int __exit isp1301_remove(struct i2c_client *i2c)
  1025. {
  1026. struct isp1301 *isp;
  1027. isp = i2c_get_clientdata(i2c);
  1028. isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
  1029. isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
  1030. free_irq(i2c->irq, isp);
  1031. #ifdef CONFIG_USB_OTG
  1032. otg_unbind(isp);
  1033. #endif
  1034. if (machine_is_omap_h2())
  1035. gpio_free(2);
  1036. isp->timer.data = 0;
  1037. set_bit(WORK_STOP, &isp->todo);
  1038. del_timer_sync(&isp->timer);
  1039. flush_work_sync(&isp->work);
  1040. put_device(&i2c->dev);
  1041. the_transceiver = NULL;
  1042. return 0;
  1043. }
  1044. /*-------------------------------------------------------------------------*/
  1045. /* NOTE: three modes are possible here, only one of which
  1046. * will be standards-conformant on any given system:
  1047. *
  1048. * - OTG mode (dual-role), required if there's a Mini-AB connector
  1049. * - HOST mode, for when there's one or more A (host) connectors
  1050. * - DEVICE mode, for when there's a B/Mini-B (device) connector
  1051. *
  1052. * As a rule, you won't have an isp1301 chip unless it's there to
  1053. * support the OTG mode. Other modes help testing USB controllers
  1054. * in isolation from (full) OTG support, or maybe so later board
  1055. * revisions can help to support those feature.
  1056. */
  1057. #ifdef CONFIG_USB_OTG
  1058. static int isp1301_otg_enable(struct isp1301 *isp)
  1059. {
  1060. power_up(isp);
  1061. isp1301_otg_init(isp);
  1062. /* NOTE: since we don't change this, this provides
  1063. * a few more interrupts than are strictly needed.
  1064. */
  1065. isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
  1066. INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
  1067. isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
  1068. INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
  1069. dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
  1070. return 0;
  1071. }
  1072. #endif
  1073. /* add or disable the host device+driver */
  1074. static int
  1075. isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
  1076. {
  1077. struct isp1301 *isp = container_of(otg, struct isp1301, otg);
  1078. if (!otg || isp != the_transceiver)
  1079. return -ENODEV;
  1080. if (!host) {
  1081. omap_writew(0, OTG_IRQ_EN);
  1082. power_down(isp);
  1083. isp->otg.host = NULL;
  1084. return 0;
  1085. }
  1086. #ifdef CONFIG_USB_OTG
  1087. isp->otg.host = host;
  1088. dev_dbg(&isp->client->dev, "registered host\n");
  1089. host_suspend(isp);
  1090. if (isp->otg.gadget)
  1091. return isp1301_otg_enable(isp);
  1092. return 0;
  1093. #elif !defined(CONFIG_USB_GADGET_OMAP)
  1094. // FIXME update its refcount
  1095. isp->otg.host = host;
  1096. power_up(isp);
  1097. if (machine_is_omap_h2())
  1098. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
  1099. dev_info(&isp->client->dev, "A-Host sessions ok\n");
  1100. isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
  1101. INTR_ID_GND);
  1102. isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
  1103. INTR_ID_GND);
  1104. /* If this has a Mini-AB connector, this mode is highly
  1105. * nonstandard ... but can be handy for testing, especially with
  1106. * the Mini-A end of an OTG cable. (Or something nonstandard
  1107. * like MiniB-to-StandardB, maybe built with a gender mender.)
  1108. */
  1109. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
  1110. dump_regs(isp, __func__);
  1111. return 0;
  1112. #else
  1113. dev_dbg(&isp->client->dev, "host sessions not allowed\n");
  1114. return -EINVAL;
  1115. #endif
  1116. }
  1117. static int
  1118. isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
  1119. {
  1120. struct isp1301 *isp = container_of(otg, struct isp1301, otg);
  1121. #ifndef CONFIG_USB_OTG
  1122. u32 l;
  1123. #endif
  1124. if (!otg || isp != the_transceiver)
  1125. return -ENODEV;
  1126. if (!gadget) {
  1127. omap_writew(0, OTG_IRQ_EN);
  1128. if (!isp->otg.default_a)
  1129. enable_vbus_draw(isp, 0);
  1130. usb_gadget_vbus_disconnect(isp->otg.gadget);
  1131. isp->otg.gadget = NULL;
  1132. power_down(isp);
  1133. return 0;
  1134. }
  1135. #ifdef CONFIG_USB_OTG
  1136. isp->otg.gadget = gadget;
  1137. dev_dbg(&isp->client->dev, "registered gadget\n");
  1138. /* gadget driver may be suspended until vbus_connect () */
  1139. if (isp->otg.host)
  1140. return isp1301_otg_enable(isp);
  1141. return 0;
  1142. #elif !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
  1143. isp->otg.gadget = gadget;
  1144. // FIXME update its refcount
  1145. l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
  1146. l &= ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS);
  1147. l |= OTG_ID;
  1148. omap_writel(l, OTG_CTRL);
  1149. power_up(isp);
  1150. isp->otg.state = OTG_STATE_B_IDLE;
  1151. if (machine_is_omap_h2() || machine_is_omap_h3())
  1152. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
  1153. isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
  1154. INTR_SESS_VLD);
  1155. isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
  1156. INTR_VBUS_VLD);
  1157. dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
  1158. dump_regs(isp, __func__);
  1159. /* If this has a Mini-AB connector, this mode is highly
  1160. * nonstandard ... but can be handy for testing, so long
  1161. * as you don't plug a Mini-A cable into the jack.
  1162. */
  1163. if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
  1164. b_peripheral(isp);
  1165. return 0;
  1166. #else
  1167. dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
  1168. return -EINVAL;
  1169. #endif
  1170. }
  1171. /*-------------------------------------------------------------------------*/
  1172. static int
  1173. isp1301_set_power(struct otg_transceiver *dev, unsigned mA)
  1174. {
  1175. if (!the_transceiver)
  1176. return -ENODEV;
  1177. if (dev->state == OTG_STATE_B_PERIPHERAL)
  1178. enable_vbus_draw(the_transceiver, mA);
  1179. return 0;
  1180. }
  1181. static int
  1182. isp1301_start_srp(struct otg_transceiver *dev)
  1183. {
  1184. struct isp1301 *isp = container_of(dev, struct isp1301, otg);
  1185. u32 otg_ctrl;
  1186. if (!dev || isp != the_transceiver
  1187. || isp->otg.state != OTG_STATE_B_IDLE)
  1188. return -ENODEV;
  1189. otg_ctrl = omap_readl(OTG_CTRL);
  1190. if (!(otg_ctrl & OTG_BSESSEND))
  1191. return -EINVAL;
  1192. otg_ctrl |= OTG_B_BUSREQ;
  1193. otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
  1194. omap_writel(otg_ctrl, OTG_CTRL);
  1195. isp->otg.state = OTG_STATE_B_SRP_INIT;
  1196. pr_debug("otg: SRP, %s ... %06x\n", state_name(isp),
  1197. omap_readl(OTG_CTRL));
  1198. #ifdef CONFIG_USB_OTG
  1199. check_state(isp, __func__);
  1200. #endif
  1201. return 0;
  1202. }
  1203. static int
  1204. isp1301_start_hnp(struct otg_transceiver *dev)
  1205. {
  1206. #ifdef CONFIG_USB_OTG
  1207. struct isp1301 *isp = container_of(dev, struct isp1301, otg);
  1208. u32 l;
  1209. if (!dev || isp != the_transceiver)
  1210. return -ENODEV;
  1211. if (isp->otg.default_a && (isp->otg.host == NULL
  1212. || !isp->otg.host->b_hnp_enable))
  1213. return -ENOTCONN;
  1214. if (!isp->otg.default_a && (isp->otg.gadget == NULL
  1215. || !isp->otg.gadget->b_hnp_enable))
  1216. return -ENOTCONN;
  1217. /* We want hardware to manage most HNP protocol timings.
  1218. * So do this part as early as possible...
  1219. */
  1220. switch (isp->otg.state) {
  1221. case OTG_STATE_B_HOST:
  1222. isp->otg.state = OTG_STATE_B_PERIPHERAL;
  1223. /* caller will suspend next */
  1224. break;
  1225. case OTG_STATE_A_HOST:
  1226. #if 0
  1227. /* autoconnect mode avoids irq latency bugs */
  1228. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
  1229. MC1_BDIS_ACON_EN);
  1230. #endif
  1231. /* caller must suspend then clear A_BUSREQ */
  1232. usb_gadget_vbus_connect(isp->otg.gadget);
  1233. l = omap_readl(OTG_CTRL);
  1234. l |= OTG_A_SETB_HNPEN;
  1235. omap_writel(l, OTG_CTRL);
  1236. break;
  1237. case OTG_STATE_A_PERIPHERAL:
  1238. /* initiated by B-Host suspend */
  1239. break;
  1240. default:
  1241. return -EILSEQ;
  1242. }
  1243. pr_debug("otg: HNP %s, %06x ...\n",
  1244. state_name(isp), omap_readl(OTG_CTRL));
  1245. check_state(isp, __func__);
  1246. return 0;
  1247. #else
  1248. /* srp-only */
  1249. return -EINVAL;
  1250. #endif
  1251. }
  1252. /*-------------------------------------------------------------------------*/
  1253. static int __devinit
  1254. isp1301_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  1255. {
  1256. int status;
  1257. struct isp1301 *isp;
  1258. if (the_transceiver)
  1259. return 0;
  1260. isp = kzalloc(sizeof *isp, GFP_KERNEL);
  1261. if (!isp)
  1262. return 0;
  1263. INIT_WORK(&isp->work, isp1301_work);
  1264. init_timer(&isp->timer);
  1265. isp->timer.function = isp1301_timer;
  1266. isp->timer.data = (unsigned long) isp;
  1267. i2c_set_clientdata(i2c, isp);
  1268. isp->client = i2c;
  1269. /* verify the chip (shouldn't be necessary) */
  1270. status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
  1271. if (status != I2C_VENDOR_ID_PHILIPS) {
  1272. dev_dbg(&i2c->dev, "not philips id: %d\n", status);
  1273. goto fail;
  1274. }
  1275. status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
  1276. if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
  1277. dev_dbg(&i2c->dev, "not isp1301, %d\n", status);
  1278. goto fail;
  1279. }
  1280. isp->i2c_release = i2c->dev.release;
  1281. i2c->dev.release = isp1301_release;
  1282. /* initial development used chiprev 2.00 */
  1283. status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
  1284. dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
  1285. status >> 8, status & 0xff);
  1286. /* make like power-on reset */
  1287. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
  1288. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
  1289. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
  1290. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
  1291. OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
  1292. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
  1293. ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
  1294. isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
  1295. isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
  1296. isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
  1297. #ifdef CONFIG_USB_OTG
  1298. status = otg_bind(isp);
  1299. if (status < 0) {
  1300. dev_dbg(&i2c->dev, "can't bind OTG\n");
  1301. goto fail;
  1302. }
  1303. #endif
  1304. if (machine_is_omap_h2()) {
  1305. /* full speed signaling by default */
  1306. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
  1307. MC1_SPEED);
  1308. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
  1309. MC2_SPD_SUSP_CTRL);
  1310. /* IRQ wired at M14 */
  1311. omap_cfg_reg(M14_1510_GPIO2);
  1312. if (gpio_request(2, "isp1301") == 0)
  1313. gpio_direction_input(2);
  1314. isp->irq_type = IRQF_TRIGGER_FALLING;
  1315. }
  1316. isp->irq_type |= IRQF_SAMPLE_RANDOM;
  1317. status = request_irq(i2c->irq, isp1301_irq,
  1318. isp->irq_type, DRIVER_NAME, isp);
  1319. if (status < 0) {
  1320. dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
  1321. i2c->irq, status);
  1322. goto fail;
  1323. }
  1324. isp->otg.dev = &i2c->dev;
  1325. isp->otg.label = DRIVER_NAME;
  1326. isp->otg.set_host = isp1301_set_host,
  1327. isp->otg.set_peripheral = isp1301_set_peripheral,
  1328. isp->otg.set_power = isp1301_set_power,
  1329. isp->otg.start_srp = isp1301_start_srp,
  1330. isp->otg.start_hnp = isp1301_start_hnp,
  1331. enable_vbus_draw(isp, 0);
  1332. power_down(isp);
  1333. the_transceiver = isp;
  1334. #ifdef CONFIG_USB_OTG
  1335. update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
  1336. update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
  1337. #endif
  1338. dump_regs(isp, __func__);
  1339. #ifdef VERBOSE
  1340. mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
  1341. dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
  1342. #endif
  1343. status = otg_set_transceiver(&isp->otg);
  1344. if (status < 0)
  1345. dev_err(&i2c->dev, "can't register transceiver, %d\n",
  1346. status);
  1347. return 0;
  1348. fail:
  1349. kfree(isp);
  1350. return -ENODEV;
  1351. }
  1352. static const struct i2c_device_id isp1301_id[] = {
  1353. { "isp1301_omap", 0 },
  1354. { }
  1355. };
  1356. MODULE_DEVICE_TABLE(i2c, isp1301_id);
  1357. static struct i2c_driver isp1301_driver = {
  1358. .driver = {
  1359. .name = "isp1301_omap",
  1360. },
  1361. .probe = isp1301_probe,
  1362. .remove = __exit_p(isp1301_remove),
  1363. .id_table = isp1301_id,
  1364. };
  1365. /*-------------------------------------------------------------------------*/
  1366. static int __init isp_init(void)
  1367. {
  1368. return i2c_add_driver(&isp1301_driver);
  1369. }
  1370. subsys_initcall(isp_init);
  1371. static void __exit isp_exit(void)
  1372. {
  1373. if (the_transceiver)
  1374. otg_set_transceiver(NULL);
  1375. i2c_del_driver(&isp1301_driver);
  1376. }
  1377. module_exit(isp_exit);