isp1301_omap.c 41 KB

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