phy-isp1301-omap.c 40 KB

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