isp1301_omap.c 41 KB

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