isp1301_omap.c 40 KB

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