isp1301_omap.c 41 KB

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