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