phy-isp1301-omap.c 40 KB

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