isp1301_omap.c 40 KB

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