isp1301_omap.c 41 KB

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