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/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 device *dev)
  736. {
  737. // struct omap_usb_config *config = dev->platform_data;
  738. otg_dev = to_platform_device(dev);
  739. return 0;
  740. }
  741. static int otg_remove(struct device *dev)
  742. {
  743. otg_dev = 0;
  744. return 0;
  745. }
  746. struct device_driver omap_otg_driver = {
  747. .name = "omap_otg",
  748. .bus = &platform_bus_type,
  749. .probe = otg_probe,
  750. .remove = otg_remove,
  751. };
  752. static int otg_bind(struct isp1301 *isp)
  753. {
  754. int status;
  755. if (otg_dev)
  756. return -EBUSY;
  757. status = driver_register(&omap_otg_driver);
  758. if (status < 0)
  759. return status;
  760. if (otg_dev)
  761. status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
  762. SA_INTERRUPT, DRIVER_NAME, isp);
  763. else
  764. status = -ENODEV;
  765. if (status < 0)
  766. driver_unregister(&omap_otg_driver);
  767. return status;
  768. }
  769. static void otg_unbind(struct isp1301 *isp)
  770. {
  771. if (!otg_dev)
  772. return;
  773. free_irq(otg_dev->resource[1].start, isp);
  774. }
  775. #else
  776. /* OTG controller isn't clocked */
  777. #endif /* CONFIG_USB_OTG */
  778. /*-------------------------------------------------------------------------*/
  779. static void b_peripheral(struct isp1301 *isp)
  780. {
  781. OTG_CTRL_REG = OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
  782. usb_gadget_vbus_connect(isp->otg.gadget);
  783. #ifdef CONFIG_USB_OTG
  784. enable_vbus_draw(isp, 8);
  785. otg_update_isp(isp);
  786. #else
  787. enable_vbus_draw(isp, 100);
  788. /* UDC driver just set OTG_BSESSVLD */
  789. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
  790. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
  791. isp->otg.state = OTG_STATE_B_PERIPHERAL;
  792. pr_debug(" --> b_peripheral\n");
  793. dump_regs(isp, "2periph");
  794. #endif
  795. }
  796. static void isp_update_otg(struct isp1301 *isp, u8 stat)
  797. {
  798. u8 isp_stat, isp_bstat;
  799. enum usb_otg_state state = isp->otg.state;
  800. if (stat & INTR_BDIS_ACON)
  801. pr_debug("OTG: BDIS_ACON, %s\n", state_name(isp));
  802. /* start certain state transitions right away */
  803. isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
  804. if (isp_stat & INTR_ID_GND) {
  805. if (isp->otg.default_a) {
  806. switch (state) {
  807. case OTG_STATE_B_IDLE:
  808. a_idle(isp, "idle");
  809. /* FALLTHROUGH */
  810. case OTG_STATE_A_IDLE:
  811. enable_vbus_source(isp);
  812. /* FALLTHROUGH */
  813. case OTG_STATE_A_WAIT_VRISE:
  814. /* we skip over OTG_STATE_A_WAIT_BCON, since
  815. * the HC will transition to A_HOST (or
  816. * A_SUSPEND!) without our noticing except
  817. * when HNP is used.
  818. */
  819. if (isp_stat & INTR_VBUS_VLD)
  820. isp->otg.state = OTG_STATE_A_HOST;
  821. break;
  822. case OTG_STATE_A_WAIT_VFALL:
  823. if (!(isp_stat & INTR_SESS_VLD))
  824. a_idle(isp, "vfell");
  825. break;
  826. default:
  827. if (!(isp_stat & INTR_VBUS_VLD))
  828. isp->otg.state = OTG_STATE_A_VBUS_ERR;
  829. break;
  830. }
  831. isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
  832. } else {
  833. switch (state) {
  834. case OTG_STATE_B_PERIPHERAL:
  835. case OTG_STATE_B_HOST:
  836. case OTG_STATE_B_WAIT_ACON:
  837. usb_gadget_vbus_disconnect(isp->otg.gadget);
  838. break;
  839. default:
  840. break;
  841. }
  842. if (state != OTG_STATE_A_IDLE)
  843. a_idle(isp, "id");
  844. if (isp->otg.host && state == OTG_STATE_A_IDLE)
  845. isp1301_defer_work(isp, WORK_HOST_RESUME);
  846. isp_bstat = 0;
  847. }
  848. } else {
  849. /* if user unplugged mini-A end of cable,
  850. * don't bypass A_WAIT_VFALL.
  851. */
  852. if (isp->otg.default_a) {
  853. switch (state) {
  854. default:
  855. isp->otg.state = OTG_STATE_A_WAIT_VFALL;
  856. break;
  857. case OTG_STATE_A_WAIT_VFALL:
  858. state = OTG_STATE_A_IDLE;
  859. /* khubd may take a while to notice and
  860. * handle this disconnect, so don't go
  861. * to B_IDLE quite yet.
  862. */
  863. break;
  864. case OTG_STATE_A_IDLE:
  865. host_suspend(isp);
  866. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
  867. MC1_BDIS_ACON_EN);
  868. isp->otg.state = OTG_STATE_B_IDLE;
  869. OTG_CTRL_REG &= OTG_CTRL_REG & OTG_CTRL_MASK
  870. & ~OTG_CTRL_BITS;
  871. break;
  872. case OTG_STATE_B_IDLE:
  873. break;
  874. }
  875. }
  876. isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
  877. switch (isp->otg.state) {
  878. case OTG_STATE_B_PERIPHERAL:
  879. case OTG_STATE_B_WAIT_ACON:
  880. case OTG_STATE_B_HOST:
  881. if (likely(isp_bstat & OTG_B_SESS_VLD))
  882. break;
  883. enable_vbus_draw(isp, 0);
  884. #ifndef CONFIG_USB_OTG
  885. /* UDC driver will clear OTG_BSESSVLD */
  886. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
  887. OTG1_DP_PULLDOWN);
  888. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
  889. OTG1_DP_PULLUP);
  890. dump_regs(isp, __FUNCTION__);
  891. #endif
  892. /* FALLTHROUGH */
  893. case OTG_STATE_B_SRP_INIT:
  894. b_idle(isp, __FUNCTION__);
  895. OTG_CTRL_REG &= OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
  896. /* FALLTHROUGH */
  897. case OTG_STATE_B_IDLE:
  898. if (isp->otg.gadget && (isp_bstat & OTG_B_SESS_VLD)) {
  899. #ifdef CONFIG_USB_OTG
  900. update_otg1(isp, isp_stat);
  901. update_otg2(isp, isp_bstat);
  902. #endif
  903. b_peripheral(isp);
  904. } else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
  905. isp_bstat |= OTG_B_SESS_END;
  906. break;
  907. case OTG_STATE_A_WAIT_VFALL:
  908. break;
  909. default:
  910. pr_debug("otg: unsupported b-device %s\n",
  911. state_name(isp));
  912. break;
  913. }
  914. }
  915. if (state != isp->otg.state)
  916. pr_debug(" isp, %s -> %s\n",
  917. state_string(state), state_name(isp));
  918. #ifdef CONFIG_USB_OTG
  919. /* update the OTG controller state to match the isp1301; may
  920. * trigger OPRT_CHG irqs for changes going to the isp1301.
  921. */
  922. update_otg1(isp, isp_stat);
  923. update_otg2(isp, isp_bstat);
  924. check_state(isp, __FUNCTION__);
  925. #endif
  926. dump_regs(isp, "isp1301->otg");
  927. }
  928. /*-------------------------------------------------------------------------*/
  929. static u8 isp1301_clear_latch(struct isp1301 *isp)
  930. {
  931. u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
  932. isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
  933. return latch;
  934. }
  935. static void
  936. isp1301_work(void *data)
  937. {
  938. struct isp1301 *isp = data;
  939. int stop;
  940. /* implicit lock: we're the only task using this device */
  941. isp->working = 1;
  942. do {
  943. stop = test_bit(WORK_STOP, &isp->todo);
  944. #ifdef CONFIG_USB_OTG
  945. /* transfer state from otg engine to isp1301 */
  946. if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
  947. otg_update_isp(isp);
  948. put_device(&isp->client.dev);
  949. }
  950. #endif
  951. /* transfer state from isp1301 to otg engine */
  952. if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
  953. u8 stat = isp1301_clear_latch(isp);
  954. isp_update_otg(isp, stat);
  955. put_device(&isp->client.dev);
  956. }
  957. if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
  958. u32 otg_ctrl;
  959. /*
  960. * skip A_WAIT_VRISE; hc transitions invisibly
  961. * skip A_WAIT_BCON; same.
  962. */
  963. switch (isp->otg.state) {
  964. case OTG_STATE_A_WAIT_BCON:
  965. case OTG_STATE_A_WAIT_VRISE:
  966. isp->otg.state = OTG_STATE_A_HOST;
  967. pr_debug(" --> a_host\n");
  968. otg_ctrl = OTG_CTRL_REG;
  969. otg_ctrl |= OTG_A_BUSREQ;
  970. otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
  971. & OTG_CTRL_MASK;
  972. OTG_CTRL_REG = otg_ctrl;
  973. break;
  974. case OTG_STATE_B_WAIT_ACON:
  975. isp->otg.state = OTG_STATE_B_HOST;
  976. pr_debug(" --> b_host (acon)\n");
  977. break;
  978. case OTG_STATE_B_HOST:
  979. case OTG_STATE_B_IDLE:
  980. case OTG_STATE_A_IDLE:
  981. break;
  982. default:
  983. pr_debug(" host resume in %s\n",
  984. state_name(isp));
  985. }
  986. host_resume(isp);
  987. // mdelay(10);
  988. put_device(&isp->client.dev);
  989. }
  990. if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
  991. #ifdef VERBOSE
  992. dump_regs(isp, "timer");
  993. if (!stop)
  994. mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
  995. #endif
  996. put_device(&isp->client.dev);
  997. }
  998. if (isp->todo)
  999. dev_vdbg(&isp->client.dev,
  1000. "work done, todo = 0x%lx\n",
  1001. isp->todo);
  1002. if (stop) {
  1003. dev_dbg(&isp->client.dev, "stop\n");
  1004. break;
  1005. }
  1006. } while (isp->todo);
  1007. isp->working = 0;
  1008. }
  1009. static irqreturn_t isp1301_irq(int irq, void *isp, struct pt_regs *regs)
  1010. {
  1011. isp1301_defer_work(isp, WORK_UPDATE_OTG);
  1012. return IRQ_HANDLED;
  1013. }
  1014. static void isp1301_timer(unsigned long _isp)
  1015. {
  1016. isp1301_defer_work((void *)_isp, WORK_TIMER);
  1017. }
  1018. /*-------------------------------------------------------------------------*/
  1019. static void isp1301_release(struct device *dev)
  1020. {
  1021. struct isp1301 *isp;
  1022. isp = container_of(dev, struct isp1301, client.dev);
  1023. /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
  1024. if (isp->i2c_release)
  1025. isp->i2c_release(dev);
  1026. kfree (isp);
  1027. }
  1028. static struct isp1301 *the_transceiver;
  1029. static int isp1301_detach_client(struct i2c_client *i2c)
  1030. {
  1031. struct isp1301 *isp;
  1032. isp = container_of(i2c, struct isp1301, client);
  1033. isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
  1034. isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
  1035. free_irq(isp->irq, isp);
  1036. #ifdef CONFIG_USB_OTG
  1037. otg_unbind(isp);
  1038. #endif
  1039. if (machine_is_omap_h2())
  1040. omap_free_gpio(2);
  1041. isp->timer.data = 0;
  1042. set_bit(WORK_STOP, &isp->todo);
  1043. del_timer_sync(&isp->timer);
  1044. flush_scheduled_work();
  1045. put_device(&i2c->dev);
  1046. the_transceiver = 0;
  1047. return i2c_detach_client(i2c);
  1048. }
  1049. /*-------------------------------------------------------------------------*/
  1050. /* NOTE: three modes are possible here, only one of which
  1051. * will be standards-conformant on any given system:
  1052. *
  1053. * - OTG mode (dual-role), required if there's a Mini-AB connector
  1054. * - HOST mode, for when there's one or more A (host) connectors
  1055. * - DEVICE mode, for when there's a B/Mini-B (device) connector
  1056. *
  1057. * As a rule, you won't have an isp1301 chip unless it's there to
  1058. * support the OTG mode. Other modes help testing USB controllers
  1059. * in isolation from (full) OTG support, or maybe so later board
  1060. * revisions can help to support those feature.
  1061. */
  1062. #ifdef CONFIG_USB_OTG
  1063. static int isp1301_otg_enable(struct isp1301 *isp)
  1064. {
  1065. power_up(isp);
  1066. otg_init(isp);
  1067. /* NOTE: since we don't change this, this provides
  1068. * a few more interrupts than are strictly needed.
  1069. */
  1070. isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
  1071. INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
  1072. isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
  1073. INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
  1074. dev_info(&isp->client.dev, "ready for dual-role USB ...\n");
  1075. return 0;
  1076. }
  1077. #endif
  1078. /* add or disable the host device+driver */
  1079. static int
  1080. isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
  1081. {
  1082. struct isp1301 *isp = container_of(otg, struct isp1301, otg);
  1083. if (!otg || isp != the_transceiver)
  1084. return -ENODEV;
  1085. if (!host) {
  1086. OTG_IRQ_EN_REG = 0;
  1087. power_down(isp);
  1088. isp->otg.host = 0;
  1089. return 0;
  1090. }
  1091. #ifdef CONFIG_USB_OTG
  1092. isp->otg.host = host;
  1093. dev_dbg(&isp->client.dev, "registered host\n");
  1094. host_suspend(isp);
  1095. if (isp->otg.gadget)
  1096. return isp1301_otg_enable(isp);
  1097. return 0;
  1098. #elif !defined(CONFIG_USB_GADGET_OMAP)
  1099. // FIXME update its refcount
  1100. isp->otg.host = host;
  1101. power_up(isp);
  1102. if (machine_is_omap_h2())
  1103. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
  1104. dev_info(&isp->client.dev, "A-Host sessions ok\n");
  1105. isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
  1106. INTR_ID_GND);
  1107. isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
  1108. INTR_ID_GND);
  1109. /* If this has a Mini-AB connector, this mode is highly
  1110. * nonstandard ... but can be handy for testing, especially with
  1111. * the Mini-A end of an OTG cable. (Or something nonstandard
  1112. * like MiniB-to-StandardB, maybe built with a gender mender.)
  1113. */
  1114. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
  1115. dump_regs(isp, __FUNCTION__);
  1116. return 0;
  1117. #else
  1118. dev_dbg(&isp->client.dev, "host sessions not allowed\n");
  1119. return -EINVAL;
  1120. #endif
  1121. }
  1122. static int
  1123. isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
  1124. {
  1125. struct isp1301 *isp = container_of(otg, struct isp1301, otg);
  1126. if (!otg || isp != the_transceiver)
  1127. return -ENODEV;
  1128. if (!gadget) {
  1129. OTG_IRQ_EN_REG = 0;
  1130. if (!isp->otg.default_a)
  1131. enable_vbus_draw(isp, 0);
  1132. usb_gadget_vbus_disconnect(isp->otg.gadget);
  1133. isp->otg.gadget = 0;
  1134. power_down(isp);
  1135. return 0;
  1136. }
  1137. #ifdef CONFIG_USB_OTG
  1138. isp->otg.gadget = gadget;
  1139. dev_dbg(&isp->client.dev, "registered gadget\n");
  1140. /* gadget driver may be suspended until vbus_connect () */
  1141. if (isp->otg.host)
  1142. return isp1301_otg_enable(isp);
  1143. return 0;
  1144. #elif !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
  1145. isp->otg.gadget = gadget;
  1146. // FIXME update its refcount
  1147. OTG_CTRL_REG = (OTG_CTRL_REG & OTG_CTRL_MASK
  1148. & ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS))
  1149. | OTG_ID;
  1150. power_up(isp);
  1151. isp->otg.state = OTG_STATE_B_IDLE;
  1152. if (machine_is_omap_h2())
  1153. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
  1154. isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
  1155. INTR_SESS_VLD);
  1156. isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
  1157. INTR_VBUS_VLD);
  1158. dev_info(&isp->client.dev, "B-Peripheral sessions ok\n");
  1159. dump_regs(isp, __FUNCTION__);
  1160. /* If this has a Mini-AB connector, this mode is highly
  1161. * nonstandard ... but can be handy for testing, so long
  1162. * as you don't plug a Mini-A cable into the jack.
  1163. */
  1164. if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
  1165. b_peripheral(isp);
  1166. return 0;
  1167. #else
  1168. dev_dbg(&isp->client.dev, "peripheral sessions not allowed\n");
  1169. return -EINVAL;
  1170. #endif
  1171. }
  1172. /*-------------------------------------------------------------------------*/
  1173. static int
  1174. isp1301_set_power(struct otg_transceiver *dev, unsigned mA)
  1175. {
  1176. if (!the_transceiver)
  1177. return -ENODEV;
  1178. if (dev->state == OTG_STATE_B_PERIPHERAL)
  1179. enable_vbus_draw(the_transceiver, mA);
  1180. return 0;
  1181. }
  1182. static int
  1183. isp1301_start_srp(struct otg_transceiver *dev)
  1184. {
  1185. struct isp1301 *isp = container_of(dev, struct isp1301, otg);
  1186. u32 otg_ctrl;
  1187. if (!dev || isp != the_transceiver
  1188. || isp->otg.state != OTG_STATE_B_IDLE)
  1189. return -ENODEV;
  1190. otg_ctrl = OTG_CTRL_REG;
  1191. if (!(otg_ctrl & OTG_BSESSEND))
  1192. return -EINVAL;
  1193. otg_ctrl |= OTG_B_BUSREQ;
  1194. otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
  1195. OTG_CTRL_REG = otg_ctrl;
  1196. isp->otg.state = OTG_STATE_B_SRP_INIT;
  1197. pr_debug("otg: SRP, %s ... %06x\n", state_name(isp), OTG_CTRL_REG);
  1198. #ifdef CONFIG_USB_OTG
  1199. check_state(isp, __FUNCTION__);
  1200. #endif
  1201. return 0;
  1202. }
  1203. static int
  1204. isp1301_start_hnp(struct otg_transceiver *dev)
  1205. {
  1206. #ifdef CONFIG_USB_OTG
  1207. struct isp1301 *isp = container_of(dev, struct isp1301, otg);
  1208. if (!dev || isp != the_transceiver)
  1209. return -ENODEV;
  1210. if (isp->otg.default_a && (isp->otg.host == NULL
  1211. || !isp->otg.host->b_hnp_enable))
  1212. return -ENOTCONN;
  1213. if (!isp->otg.default_a && (isp->otg.gadget == NULL
  1214. || !isp->otg.gadget->b_hnp_enable))
  1215. return -ENOTCONN;
  1216. /* We want hardware to manage most HNP protocol timings.
  1217. * So do this part as early as possible...
  1218. */
  1219. switch (isp->otg.state) {
  1220. case OTG_STATE_B_HOST:
  1221. isp->otg.state = OTG_STATE_B_PERIPHERAL;
  1222. /* caller will suspend next */
  1223. break;
  1224. case OTG_STATE_A_HOST:
  1225. #if 0
  1226. /* autoconnect mode avoids irq latency bugs */
  1227. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
  1228. MC1_BDIS_ACON_EN);
  1229. #endif
  1230. /* caller must suspend then clear A_BUSREQ */
  1231. usb_gadget_vbus_connect(isp->otg.gadget);
  1232. OTG_CTRL_REG |= OTG_A_SETB_HNPEN;
  1233. break;
  1234. case OTG_STATE_A_PERIPHERAL:
  1235. /* initiated by B-Host suspend */
  1236. break;
  1237. default:
  1238. return -EILSEQ;
  1239. }
  1240. pr_debug("otg: HNP %s, %06x ...\n",
  1241. state_name(isp), OTG_CTRL_REG);
  1242. check_state(isp, __FUNCTION__);
  1243. return 0;
  1244. #else
  1245. /* srp-only */
  1246. return -EINVAL;
  1247. #endif
  1248. }
  1249. /*-------------------------------------------------------------------------*/
  1250. /* no error returns, they'd just make bus scanning stop */
  1251. static int isp1301_probe(struct i2c_adapter *bus, int address, int kind)
  1252. {
  1253. int status;
  1254. struct isp1301 *isp;
  1255. struct i2c_client *i2c;
  1256. if (the_transceiver)
  1257. return 0;
  1258. isp = kcalloc(1, sizeof *isp, GFP_KERNEL);
  1259. if (!isp)
  1260. return 0;
  1261. INIT_WORK(&isp->work, isp1301_work, isp);
  1262. init_timer(&isp->timer);
  1263. isp->timer.function = isp1301_timer;
  1264. isp->timer.data = (unsigned long) isp;
  1265. isp->irq = -1;
  1266. isp->client.addr = address;
  1267. i2c_set_clientdata(&isp->client, isp);
  1268. isp->client.adapter = bus;
  1269. isp->client.driver = &isp1301_driver;
  1270. strlcpy(isp->client.name, DRIVER_NAME, I2C_NAME_SIZE);
  1271. i2c = &isp->client;
  1272. /* if this is a true probe, verify the chip ... */
  1273. if (kind < 0) {
  1274. status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
  1275. if (status != I2C_VENDOR_ID_PHILIPS) {
  1276. dev_dbg(&bus->dev, "addr %d not philips id: %d\n",
  1277. address, status);
  1278. goto fail1;
  1279. }
  1280. status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
  1281. if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
  1282. dev_dbg(&bus->dev, "%d not isp1301, %d\n",
  1283. address, status);
  1284. goto fail1;
  1285. }
  1286. }
  1287. status = i2c_attach_client(i2c);
  1288. if (status < 0) {
  1289. dev_dbg(&bus->dev, "can't attach %s to device %d, err %d\n",
  1290. DRIVER_NAME, address, status);
  1291. fail1:
  1292. kfree(isp);
  1293. return 0;
  1294. }
  1295. isp->i2c_release = i2c->dev.release;
  1296. i2c->dev.release = isp1301_release;
  1297. /* initial development used chiprev 2.00 */
  1298. status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
  1299. dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
  1300. status >> 8, status & 0xff);
  1301. /* make like power-on reset */
  1302. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
  1303. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
  1304. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
  1305. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
  1306. OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
  1307. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
  1308. ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
  1309. isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
  1310. isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
  1311. isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
  1312. #ifdef CONFIG_USB_OTG
  1313. status = otg_bind(isp);
  1314. if (status < 0) {
  1315. dev_dbg(&i2c->dev, "can't bind OTG\n");
  1316. goto fail2;
  1317. }
  1318. #endif
  1319. if (machine_is_omap_h2()) {
  1320. /* full speed signaling by default */
  1321. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
  1322. MC1_SPEED_REG);
  1323. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
  1324. MC2_SPD_SUSP_CTRL);
  1325. /* IRQ wired at M14 */
  1326. omap_cfg_reg(M14_1510_GPIO2);
  1327. isp->irq = OMAP_GPIO_IRQ(2);
  1328. omap_request_gpio(2);
  1329. omap_set_gpio_direction(2, 1);
  1330. omap_set_gpio_edge_ctrl(2, OMAP_GPIO_FALLING_EDGE);
  1331. }
  1332. status = request_irq(isp->irq, isp1301_irq,
  1333. SA_SAMPLE_RANDOM, DRIVER_NAME, isp);
  1334. if (status < 0) {
  1335. dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
  1336. isp->irq, status);
  1337. #ifdef CONFIG_USB_OTG
  1338. fail2:
  1339. #endif
  1340. i2c_detach_client(i2c);
  1341. goto fail1;
  1342. }
  1343. isp->otg.dev = &isp->client.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, __FUNCTION__);
  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. }
  1368. static int isp1301_scan_bus(struct i2c_adapter *bus)
  1369. {
  1370. if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA
  1371. | I2C_FUNC_SMBUS_READ_WORD_DATA))
  1372. return -EINVAL;
  1373. return i2c_probe(bus, &addr_data, isp1301_probe);
  1374. }
  1375. static struct i2c_driver isp1301_driver = {
  1376. .owner = THIS_MODULE,
  1377. .name = "isp1301_omap",
  1378. .id = 1301, /* FIXME "official", i2c-ids.h */
  1379. .class = I2C_CLASS_HWMON,
  1380. .flags = I2C_DF_NOTIFY,
  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);