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