isp1301_omap.c 41 KB

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