isp1301_omap.c 40 KB

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