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