phy-isp1301-omap.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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)
  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.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.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.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.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.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.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.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.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.state == OTG_STATE_B_IDLE
  416. || isp->phy.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.state == OTG_STATE_A_IDLE
  424. || isp->phy.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.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.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.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.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.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.state) {
  522. case OTG_STATE_B_IDLE:
  523. if (clr & OTG1_DP_PULLUP)
  524. break;
  525. isp->phy.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.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.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.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.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.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.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.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.state) {
  659. case OTG_STATE_B_WAIT_ACON:
  660. isp->phy.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.state = OTG_STATE_A_HOST;
  666. pr_debug(" --> a_host\n");
  667. break;
  668. case OTG_STATE_A_PERIPHERAL:
  669. isp->phy.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. .owner = THIS_MODULE,
  735. .name = "omap_otg",
  736. },
  737. };
  738. static int otg_bind(struct isp1301 *isp)
  739. {
  740. int status;
  741. if (otg_dev)
  742. return -EBUSY;
  743. status = platform_driver_register(&omap_otg_driver);
  744. if (status < 0)
  745. return status;
  746. if (otg_dev)
  747. status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
  748. 0, DRIVER_NAME, isp);
  749. else
  750. status = -ENODEV;
  751. if (status < 0)
  752. platform_driver_unregister(&omap_otg_driver);
  753. return status;
  754. }
  755. static void otg_unbind(struct isp1301 *isp)
  756. {
  757. if (!otg_dev)
  758. return;
  759. free_irq(otg_dev->resource[1].start, isp);
  760. }
  761. #else
  762. /* OTG controller isn't clocked */
  763. #endif /* CONFIG_USB_OTG */
  764. /*-------------------------------------------------------------------------*/
  765. static void b_peripheral(struct isp1301 *isp)
  766. {
  767. u32 l;
  768. l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
  769. omap_writel(l, OTG_CTRL);
  770. usb_gadget_vbus_connect(isp->phy.otg->gadget);
  771. #ifdef CONFIG_USB_OTG
  772. enable_vbus_draw(isp, 8);
  773. otg_update_isp(isp);
  774. #else
  775. enable_vbus_draw(isp, 100);
  776. /* UDC driver just set OTG_BSESSVLD */
  777. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
  778. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
  779. isp->phy.state = OTG_STATE_B_PERIPHERAL;
  780. pr_debug(" --> b_peripheral\n");
  781. dump_regs(isp, "2periph");
  782. #endif
  783. }
  784. static void isp_update_otg(struct isp1301 *isp, u8 stat)
  785. {
  786. struct usb_otg *otg = isp->phy.otg;
  787. u8 isp_stat, isp_bstat;
  788. enum usb_otg_state state = isp->phy.state;
  789. if (stat & INTR_BDIS_ACON)
  790. pr_debug("OTG: BDIS_ACON, %s\n", state_name(isp));
  791. /* start certain state transitions right away */
  792. isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
  793. if (isp_stat & INTR_ID_GND) {
  794. if (otg->default_a) {
  795. switch (state) {
  796. case OTG_STATE_B_IDLE:
  797. a_idle(isp, "idle");
  798. /* FALLTHROUGH */
  799. case OTG_STATE_A_IDLE:
  800. enable_vbus_source(isp);
  801. /* FALLTHROUGH */
  802. case OTG_STATE_A_WAIT_VRISE:
  803. /* we skip over OTG_STATE_A_WAIT_BCON, since
  804. * the HC will transition to A_HOST (or
  805. * A_SUSPEND!) without our noticing except
  806. * when HNP is used.
  807. */
  808. if (isp_stat & INTR_VBUS_VLD)
  809. isp->phy.state = OTG_STATE_A_HOST;
  810. break;
  811. case OTG_STATE_A_WAIT_VFALL:
  812. if (!(isp_stat & INTR_SESS_VLD))
  813. a_idle(isp, "vfell");
  814. break;
  815. default:
  816. if (!(isp_stat & INTR_VBUS_VLD))
  817. isp->phy.state = OTG_STATE_A_VBUS_ERR;
  818. break;
  819. }
  820. isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
  821. } else {
  822. switch (state) {
  823. case OTG_STATE_B_PERIPHERAL:
  824. case OTG_STATE_B_HOST:
  825. case OTG_STATE_B_WAIT_ACON:
  826. usb_gadget_vbus_disconnect(otg->gadget);
  827. break;
  828. default:
  829. break;
  830. }
  831. if (state != OTG_STATE_A_IDLE)
  832. a_idle(isp, "id");
  833. if (otg->host && state == OTG_STATE_A_IDLE)
  834. isp1301_defer_work(isp, WORK_HOST_RESUME);
  835. isp_bstat = 0;
  836. }
  837. } else {
  838. u32 l;
  839. /* if user unplugged mini-A end of cable,
  840. * don't bypass A_WAIT_VFALL.
  841. */
  842. if (otg->default_a) {
  843. switch (state) {
  844. default:
  845. isp->phy.state = OTG_STATE_A_WAIT_VFALL;
  846. break;
  847. case OTG_STATE_A_WAIT_VFALL:
  848. state = OTG_STATE_A_IDLE;
  849. /* khubd may take a while to notice and
  850. * handle this disconnect, so don't go
  851. * to B_IDLE quite yet.
  852. */
  853. break;
  854. case OTG_STATE_A_IDLE:
  855. host_suspend(isp);
  856. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
  857. MC1_BDIS_ACON_EN);
  858. isp->phy.state = OTG_STATE_B_IDLE;
  859. l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
  860. l &= ~OTG_CTRL_BITS;
  861. omap_writel(l, OTG_CTRL);
  862. break;
  863. case OTG_STATE_B_IDLE:
  864. break;
  865. }
  866. }
  867. isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
  868. switch (isp->phy.state) {
  869. case OTG_STATE_B_PERIPHERAL:
  870. case OTG_STATE_B_WAIT_ACON:
  871. case OTG_STATE_B_HOST:
  872. if (likely(isp_bstat & OTG_B_SESS_VLD))
  873. break;
  874. enable_vbus_draw(isp, 0);
  875. #ifndef CONFIG_USB_OTG
  876. /* UDC driver will clear OTG_BSESSVLD */
  877. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
  878. OTG1_DP_PULLDOWN);
  879. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
  880. OTG1_DP_PULLUP);
  881. dump_regs(isp, __func__);
  882. #endif
  883. /* FALLTHROUGH */
  884. case OTG_STATE_B_SRP_INIT:
  885. b_idle(isp, __func__);
  886. l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
  887. omap_writel(l, OTG_CTRL);
  888. /* FALLTHROUGH */
  889. case OTG_STATE_B_IDLE:
  890. if (otg->gadget && (isp_bstat & OTG_B_SESS_VLD)) {
  891. #ifdef CONFIG_USB_OTG
  892. update_otg1(isp, isp_stat);
  893. update_otg2(isp, isp_bstat);
  894. #endif
  895. b_peripheral(isp);
  896. } else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
  897. isp_bstat |= OTG_B_SESS_END;
  898. break;
  899. case OTG_STATE_A_WAIT_VFALL:
  900. break;
  901. default:
  902. pr_debug("otg: unsupported b-device %s\n",
  903. state_name(isp));
  904. break;
  905. }
  906. }
  907. if (state != isp->phy.state)
  908. pr_debug(" isp, %s -> %s\n",
  909. usb_otg_state_string(state), state_name(isp));
  910. #ifdef CONFIG_USB_OTG
  911. /* update the OTG controller state to match the isp1301; may
  912. * trigger OPRT_CHG irqs for changes going to the isp1301.
  913. */
  914. update_otg1(isp, isp_stat);
  915. update_otg2(isp, isp_bstat);
  916. check_state(isp, __func__);
  917. #endif
  918. dump_regs(isp, "isp1301->otg");
  919. }
  920. /*-------------------------------------------------------------------------*/
  921. static u8 isp1301_clear_latch(struct isp1301 *isp)
  922. {
  923. u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
  924. isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
  925. return latch;
  926. }
  927. static void
  928. isp1301_work(struct work_struct *work)
  929. {
  930. struct isp1301 *isp = container_of(work, struct isp1301, work);
  931. int stop;
  932. /* implicit lock: we're the only task using this device */
  933. isp->working = 1;
  934. do {
  935. stop = test_bit(WORK_STOP, &isp->todo);
  936. #ifdef CONFIG_USB_OTG
  937. /* transfer state from otg engine to isp1301 */
  938. if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
  939. otg_update_isp(isp);
  940. put_device(&isp->client->dev);
  941. }
  942. #endif
  943. /* transfer state from isp1301 to otg engine */
  944. if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
  945. u8 stat = isp1301_clear_latch(isp);
  946. isp_update_otg(isp, stat);
  947. put_device(&isp->client->dev);
  948. }
  949. if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
  950. u32 otg_ctrl;
  951. /*
  952. * skip A_WAIT_VRISE; hc transitions invisibly
  953. * skip A_WAIT_BCON; same.
  954. */
  955. switch (isp->phy.state) {
  956. case OTG_STATE_A_WAIT_BCON:
  957. case OTG_STATE_A_WAIT_VRISE:
  958. isp->phy.state = OTG_STATE_A_HOST;
  959. pr_debug(" --> a_host\n");
  960. otg_ctrl = omap_readl(OTG_CTRL);
  961. otg_ctrl |= OTG_A_BUSREQ;
  962. otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
  963. & OTG_CTRL_MASK;
  964. omap_writel(otg_ctrl, OTG_CTRL);
  965. break;
  966. case OTG_STATE_B_WAIT_ACON:
  967. isp->phy.state = OTG_STATE_B_HOST;
  968. pr_debug(" --> b_host (acon)\n");
  969. break;
  970. case OTG_STATE_B_HOST:
  971. case OTG_STATE_B_IDLE:
  972. case OTG_STATE_A_IDLE:
  973. break;
  974. default:
  975. pr_debug(" host resume in %s\n",
  976. state_name(isp));
  977. }
  978. host_resume(isp);
  979. // mdelay(10);
  980. put_device(&isp->client->dev);
  981. }
  982. if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
  983. #ifdef VERBOSE
  984. dump_regs(isp, "timer");
  985. if (!stop)
  986. mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
  987. #endif
  988. put_device(&isp->client->dev);
  989. }
  990. if (isp->todo)
  991. dev_vdbg(&isp->client->dev,
  992. "work done, todo = 0x%lx\n",
  993. isp->todo);
  994. if (stop) {
  995. dev_dbg(&isp->client->dev, "stop\n");
  996. break;
  997. }
  998. } while (isp->todo);
  999. isp->working = 0;
  1000. }
  1001. static irqreturn_t isp1301_irq(int irq, void *isp)
  1002. {
  1003. isp1301_defer_work(isp, WORK_UPDATE_OTG);
  1004. return IRQ_HANDLED;
  1005. }
  1006. static void isp1301_timer(unsigned long _isp)
  1007. {
  1008. isp1301_defer_work((void *)_isp, WORK_TIMER);
  1009. }
  1010. /*-------------------------------------------------------------------------*/
  1011. static void isp1301_release(struct device *dev)
  1012. {
  1013. struct isp1301 *isp;
  1014. isp = dev_get_drvdata(dev);
  1015. /* FIXME -- not with a "new style" driver, it doesn't!! */
  1016. /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
  1017. if (isp->i2c_release)
  1018. isp->i2c_release(dev);
  1019. kfree(isp->phy.otg);
  1020. kfree (isp);
  1021. }
  1022. static struct isp1301 *the_transceiver;
  1023. static int isp1301_remove(struct i2c_client *i2c)
  1024. {
  1025. struct isp1301 *isp;
  1026. isp = i2c_get_clientdata(i2c);
  1027. isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
  1028. isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
  1029. free_irq(i2c->irq, isp);
  1030. #ifdef CONFIG_USB_OTG
  1031. otg_unbind(isp);
  1032. #endif
  1033. if (machine_is_omap_h2())
  1034. gpio_free(2);
  1035. isp->timer.data = 0;
  1036. set_bit(WORK_STOP, &isp->todo);
  1037. del_timer_sync(&isp->timer);
  1038. flush_work(&isp->work);
  1039. put_device(&i2c->dev);
  1040. the_transceiver = NULL;
  1041. return 0;
  1042. }
  1043. /*-------------------------------------------------------------------------*/
  1044. /* NOTE: three modes are possible here, only one of which
  1045. * will be standards-conformant on any given system:
  1046. *
  1047. * - OTG mode (dual-role), required if there's a Mini-AB connector
  1048. * - HOST mode, for when there's one or more A (host) connectors
  1049. * - DEVICE mode, for when there's a B/Mini-B (device) connector
  1050. *
  1051. * As a rule, you won't have an isp1301 chip unless it's there to
  1052. * support the OTG mode. Other modes help testing USB controllers
  1053. * in isolation from (full) OTG support, or maybe so later board
  1054. * revisions can help to support those feature.
  1055. */
  1056. #ifdef CONFIG_USB_OTG
  1057. static int isp1301_otg_enable(struct isp1301 *isp)
  1058. {
  1059. power_up(isp);
  1060. isp1301_otg_init(isp);
  1061. /* NOTE: since we don't change this, this provides
  1062. * a few more interrupts than are strictly needed.
  1063. */
  1064. isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
  1065. INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
  1066. isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
  1067. INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
  1068. dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
  1069. return 0;
  1070. }
  1071. #endif
  1072. /* add or disable the host device+driver */
  1073. static int
  1074. isp1301_set_host(struct usb_otg *otg, struct usb_bus *host)
  1075. {
  1076. struct isp1301 *isp = container_of(otg->phy, struct isp1301, phy);
  1077. if (!otg || isp != the_transceiver)
  1078. return -ENODEV;
  1079. if (!host) {
  1080. omap_writew(0, OTG_IRQ_EN);
  1081. power_down(isp);
  1082. otg->host = NULL;
  1083. return 0;
  1084. }
  1085. #ifdef CONFIG_USB_OTG
  1086. otg->host = host;
  1087. dev_dbg(&isp->client->dev, "registered host\n");
  1088. host_suspend(isp);
  1089. if (otg->gadget)
  1090. return isp1301_otg_enable(isp);
  1091. return 0;
  1092. #elif !defined(CONFIG_USB_GADGET_OMAP)
  1093. // FIXME update its refcount
  1094. otg->host = host;
  1095. power_up(isp);
  1096. if (machine_is_omap_h2())
  1097. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
  1098. dev_info(&isp->client->dev, "A-Host sessions ok\n");
  1099. isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
  1100. INTR_ID_GND);
  1101. isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
  1102. INTR_ID_GND);
  1103. /* If this has a Mini-AB connector, this mode is highly
  1104. * nonstandard ... but can be handy for testing, especially with
  1105. * the Mini-A end of an OTG cable. (Or something nonstandard
  1106. * like MiniB-to-StandardB, maybe built with a gender mender.)
  1107. */
  1108. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
  1109. dump_regs(isp, __func__);
  1110. return 0;
  1111. #else
  1112. dev_dbg(&isp->client->dev, "host sessions not allowed\n");
  1113. return -EINVAL;
  1114. #endif
  1115. }
  1116. static int
  1117. isp1301_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
  1118. {
  1119. struct isp1301 *isp = container_of(otg->phy, struct isp1301, phy);
  1120. if (!otg || isp != the_transceiver)
  1121. return -ENODEV;
  1122. if (!gadget) {
  1123. omap_writew(0, OTG_IRQ_EN);
  1124. if (!otg->default_a)
  1125. enable_vbus_draw(isp, 0);
  1126. usb_gadget_vbus_disconnect(otg->gadget);
  1127. otg->gadget = NULL;
  1128. power_down(isp);
  1129. return 0;
  1130. }
  1131. #ifdef CONFIG_USB_OTG
  1132. otg->gadget = gadget;
  1133. dev_dbg(&isp->client->dev, "registered gadget\n");
  1134. /* gadget driver may be suspended until vbus_connect () */
  1135. if (otg->host)
  1136. return isp1301_otg_enable(isp);
  1137. return 0;
  1138. #elif !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
  1139. otg->gadget = gadget;
  1140. // FIXME update its refcount
  1141. {
  1142. u32 l;
  1143. l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
  1144. l &= ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS);
  1145. l |= OTG_ID;
  1146. omap_writel(l, OTG_CTRL);
  1147. }
  1148. power_up(isp);
  1149. isp->phy.state = OTG_STATE_B_IDLE;
  1150. if (machine_is_omap_h2() || machine_is_omap_h3())
  1151. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
  1152. isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
  1153. INTR_SESS_VLD);
  1154. isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
  1155. INTR_VBUS_VLD);
  1156. dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
  1157. dump_regs(isp, __func__);
  1158. /* If this has a Mini-AB connector, this mode is highly
  1159. * nonstandard ... but can be handy for testing, so long
  1160. * as you don't plug a Mini-A cable into the jack.
  1161. */
  1162. if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
  1163. b_peripheral(isp);
  1164. return 0;
  1165. #else
  1166. dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
  1167. return -EINVAL;
  1168. #endif
  1169. }
  1170. /*-------------------------------------------------------------------------*/
  1171. static int
  1172. isp1301_set_power(struct usb_phy *dev, unsigned mA)
  1173. {
  1174. if (!the_transceiver)
  1175. return -ENODEV;
  1176. if (dev->state == OTG_STATE_B_PERIPHERAL)
  1177. enable_vbus_draw(the_transceiver, mA);
  1178. return 0;
  1179. }
  1180. static int
  1181. isp1301_start_srp(struct usb_otg *otg)
  1182. {
  1183. struct isp1301 *isp = container_of(otg->phy, struct isp1301, phy);
  1184. u32 otg_ctrl;
  1185. if (!otg || isp != the_transceiver
  1186. || isp->phy.state != OTG_STATE_B_IDLE)
  1187. return -ENODEV;
  1188. otg_ctrl = omap_readl(OTG_CTRL);
  1189. if (!(otg_ctrl & OTG_BSESSEND))
  1190. return -EINVAL;
  1191. otg_ctrl |= OTG_B_BUSREQ;
  1192. otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
  1193. omap_writel(otg_ctrl, OTG_CTRL);
  1194. isp->phy.state = OTG_STATE_B_SRP_INIT;
  1195. pr_debug("otg: SRP, %s ... %06x\n", state_name(isp),
  1196. omap_readl(OTG_CTRL));
  1197. #ifdef CONFIG_USB_OTG
  1198. check_state(isp, __func__);
  1199. #endif
  1200. return 0;
  1201. }
  1202. static int
  1203. isp1301_start_hnp(struct usb_otg *otg)
  1204. {
  1205. #ifdef CONFIG_USB_OTG
  1206. struct isp1301 *isp = container_of(otg->phy, struct isp1301, phy);
  1207. u32 l;
  1208. if (!otg || isp != the_transceiver)
  1209. return -ENODEV;
  1210. if (otg->default_a && (otg->host == NULL || !otg->host->b_hnp_enable))
  1211. return -ENOTCONN;
  1212. if (!otg->default_a && (otg->gadget == NULL
  1213. || !otg->gadget->b_hnp_enable))
  1214. return -ENOTCONN;
  1215. /* We want hardware to manage most HNP protocol timings.
  1216. * So do this part as early as possible...
  1217. */
  1218. switch (isp->phy.state) {
  1219. case OTG_STATE_B_HOST:
  1220. isp->phy.state = OTG_STATE_B_PERIPHERAL;
  1221. /* caller will suspend next */
  1222. break;
  1223. case OTG_STATE_A_HOST:
  1224. #if 0
  1225. /* autoconnect mode avoids irq latency bugs */
  1226. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
  1227. MC1_BDIS_ACON_EN);
  1228. #endif
  1229. /* caller must suspend then clear A_BUSREQ */
  1230. usb_gadget_vbus_connect(otg->gadget);
  1231. l = omap_readl(OTG_CTRL);
  1232. l |= OTG_A_SETB_HNPEN;
  1233. omap_writel(l, OTG_CTRL);
  1234. break;
  1235. case OTG_STATE_A_PERIPHERAL:
  1236. /* initiated by B-Host suspend */
  1237. break;
  1238. default:
  1239. return -EILSEQ;
  1240. }
  1241. pr_debug("otg: HNP %s, %06x ...\n",
  1242. state_name(isp), omap_readl(OTG_CTRL));
  1243. check_state(isp, __func__);
  1244. return 0;
  1245. #else
  1246. /* srp-only */
  1247. return -EINVAL;
  1248. #endif
  1249. }
  1250. /*-------------------------------------------------------------------------*/
  1251. static int
  1252. isp1301_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  1253. {
  1254. int status;
  1255. struct isp1301 *isp;
  1256. if (the_transceiver)
  1257. return 0;
  1258. isp = kzalloc(sizeof *isp, GFP_KERNEL);
  1259. if (!isp)
  1260. return 0;
  1261. isp->phy.otg = kzalloc(sizeof *isp->phy.otg, GFP_KERNEL);
  1262. if (!isp->phy.otg) {
  1263. kfree(isp);
  1264. return 0;
  1265. }
  1266. INIT_WORK(&isp->work, isp1301_work);
  1267. init_timer(&isp->timer);
  1268. isp->timer.function = isp1301_timer;
  1269. isp->timer.data = (unsigned long) isp;
  1270. i2c_set_clientdata(i2c, isp);
  1271. isp->client = i2c;
  1272. /* verify the chip (shouldn't be necessary) */
  1273. status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
  1274. if (status != I2C_VENDOR_ID_PHILIPS) {
  1275. dev_dbg(&i2c->dev, "not philips id: %d\n", status);
  1276. goto fail;
  1277. }
  1278. status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
  1279. if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
  1280. dev_dbg(&i2c->dev, "not isp1301, %d\n", status);
  1281. goto fail;
  1282. }
  1283. isp->i2c_release = i2c->dev.release;
  1284. i2c->dev.release = isp1301_release;
  1285. /* initial development used chiprev 2.00 */
  1286. status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
  1287. dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
  1288. status >> 8, status & 0xff);
  1289. /* make like power-on reset */
  1290. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
  1291. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
  1292. isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
  1293. isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
  1294. OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
  1295. isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
  1296. ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
  1297. isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
  1298. isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
  1299. isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
  1300. #ifdef CONFIG_USB_OTG
  1301. status = otg_bind(isp);
  1302. if (status < 0) {
  1303. dev_dbg(&i2c->dev, "can't bind OTG\n");
  1304. goto fail;
  1305. }
  1306. #endif
  1307. if (machine_is_omap_h2()) {
  1308. /* full speed signaling by default */
  1309. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
  1310. MC1_SPEED);
  1311. isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
  1312. MC2_SPD_SUSP_CTRL);
  1313. /* IRQ wired at M14 */
  1314. omap_cfg_reg(M14_1510_GPIO2);
  1315. if (gpio_request(2, "isp1301") == 0)
  1316. gpio_direction_input(2);
  1317. isp->irq_type = IRQF_TRIGGER_FALLING;
  1318. }
  1319. status = request_irq(i2c->irq, isp1301_irq,
  1320. isp->irq_type, DRIVER_NAME, isp);
  1321. if (status < 0) {
  1322. dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
  1323. i2c->irq, status);
  1324. goto fail;
  1325. }
  1326. isp->phy.dev = &i2c->dev;
  1327. isp->phy.label = DRIVER_NAME;
  1328. isp->phy.set_power = isp1301_set_power,
  1329. isp->phy.otg->phy = &isp->phy;
  1330. isp->phy.otg->set_host = isp1301_set_host,
  1331. isp->phy.otg->set_peripheral = isp1301_set_peripheral,
  1332. isp->phy.otg->start_srp = isp1301_start_srp,
  1333. isp->phy.otg->start_hnp = isp1301_start_hnp,
  1334. enable_vbus_draw(isp, 0);
  1335. power_down(isp);
  1336. the_transceiver = isp;
  1337. #ifdef CONFIG_USB_OTG
  1338. update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
  1339. update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
  1340. #endif
  1341. dump_regs(isp, __func__);
  1342. #ifdef VERBOSE
  1343. mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
  1344. dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
  1345. #endif
  1346. status = usb_add_phy(&isp->phy, USB_PHY_TYPE_USB2);
  1347. if (status < 0)
  1348. dev_err(&i2c->dev, "can't register transceiver, %d\n",
  1349. status);
  1350. return 0;
  1351. fail:
  1352. kfree(isp->phy.otg);
  1353. kfree(isp);
  1354. return -ENODEV;
  1355. }
  1356. static const struct i2c_device_id isp1301_id[] = {
  1357. { "isp1301_omap", 0 },
  1358. { }
  1359. };
  1360. MODULE_DEVICE_TABLE(i2c, isp1301_id);
  1361. static struct i2c_driver isp1301_driver = {
  1362. .driver = {
  1363. .name = "isp1301_omap",
  1364. },
  1365. .probe = isp1301_probe,
  1366. .remove = isp1301_remove,
  1367. .id_table = isp1301_id,
  1368. };
  1369. /*-------------------------------------------------------------------------*/
  1370. static int __init isp_init(void)
  1371. {
  1372. return i2c_add_driver(&isp1301_driver);
  1373. }
  1374. subsys_initcall(isp_init);
  1375. static void __exit isp_exit(void)
  1376. {
  1377. if (the_transceiver)
  1378. usb_remove_phy(&the_transceiver->phy);
  1379. i2c_del_driver(&isp1301_driver);
  1380. }
  1381. module_exit(isp_exit);