Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * isp1301_omap - ISP 1301 USB transceiver, talking to OMAP OTG controller
0004  *
0005  * Copyright (C) 2004 Texas Instruments
0006  * Copyright (C) 2004 David Brownell
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/slab.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/usb/ch9.h>
0017 #include <linux/usb/gadget.h>
0018 #include <linux/usb.h>
0019 #include <linux/usb/otg.h>
0020 #include <linux/i2c.h>
0021 #include <linux/workqueue.h>
0022 
0023 #include <asm/irq.h>
0024 #include <asm/mach-types.h>
0025 
0026 #include <linux/soc/ti/omap1-mux.h>
0027 #include <linux/soc/ti/omap1-usb.h>
0028 #include <linux/soc/ti/omap1-io.h>
0029 
0030 #undef VERBOSE
0031 
0032 
0033 #define DRIVER_VERSION  "24 August 2004"
0034 #define DRIVER_NAME (isp1301_driver.driver.name)
0035 
0036 MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
0037 MODULE_LICENSE("GPL");
0038 
0039 struct isp1301 {
0040     struct usb_phy      phy;
0041     struct i2c_client   *client;
0042     void            (*i2c_release)(struct device *dev);
0043 
0044     int         irq_type;
0045 
0046     u32         last_otg_ctrl;
0047     unsigned        working:1;
0048 
0049     struct timer_list   timer;
0050 
0051     /* use keventd context to change the state for us */
0052     struct work_struct  work;
0053 
0054     unsigned long       todo;
0055 #       define WORK_UPDATE_ISP  0   /* update ISP from OTG */
0056 #       define WORK_UPDATE_OTG  1   /* update OTG from ISP */
0057 #       define WORK_HOST_RESUME 4   /* resume host */
0058 #       define WORK_TIMER   6   /* timer fired */
0059 #       define WORK_STOP    7   /* don't resubmit */
0060 };
0061 
0062 
0063 /* bits in OTG_CTRL */
0064 
0065 #define OTG_XCEIV_OUTPUTS \
0066     (OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
0067 #define OTG_XCEIV_INPUTS \
0068     (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
0069 #define OTG_CTRL_BITS \
0070     (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
0071     /* and OTG_PULLUP is sometimes written */
0072 
0073 #define OTG_CTRL_MASK   (OTG_DRIVER_SEL| \
0074     OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
0075     OTG_CTRL_BITS)
0076 
0077 
0078 /*-------------------------------------------------------------------------*/
0079 
0080 /* board-specific PM hooks */
0081 
0082 #if defined(CONFIG_MACH_OMAP_H2) || defined(CONFIG_MACH_OMAP_H3)
0083 
0084 #if IS_REACHABLE(CONFIG_TPS65010)
0085 
0086 #include <linux/mfd/tps65010.h>
0087 
0088 #else
0089 
0090 static inline int tps65010_set_vbus_draw(unsigned mA)
0091 {
0092     pr_debug("tps65010: draw %d mA (STUB)\n", mA);
0093     return 0;
0094 }
0095 
0096 #endif
0097 
0098 static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
0099 {
0100     int status = tps65010_set_vbus_draw(mA);
0101     if (status < 0)
0102         pr_debug("  VBUS %d mA error %d\n", mA, status);
0103 }
0104 
0105 #else
0106 
0107 static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
0108 {
0109     /* H4 controls this by DIP switch S2.4; no soft control.
0110      * ON means the charger is always enabled.  Leave it OFF
0111      * unless the OTG port is used only in B-peripheral mode.
0112      */
0113 }
0114 
0115 #endif
0116 
0117 static void enable_vbus_source(struct isp1301 *isp)
0118 {
0119     /* this board won't supply more than 8mA vbus power.
0120      * some boards can switch a 100ma "unit load" (or more).
0121      */
0122 }
0123 
0124 
0125 /* products will deliver OTG messages with LEDs, GUI, etc */
0126 static inline void notresponding(struct isp1301 *isp)
0127 {
0128     printk(KERN_NOTICE "OTG device not responding.\n");
0129 }
0130 
0131 
0132 /*-------------------------------------------------------------------------*/
0133 
0134 static struct i2c_driver isp1301_driver;
0135 
0136 /* smbus apis are used for portability */
0137 
0138 static inline u8
0139 isp1301_get_u8(struct isp1301 *isp, u8 reg)
0140 {
0141     return i2c_smbus_read_byte_data(isp->client, reg + 0);
0142 }
0143 
0144 static inline int
0145 isp1301_get_u16(struct isp1301 *isp, u8 reg)
0146 {
0147     return i2c_smbus_read_word_data(isp->client, reg);
0148 }
0149 
0150 static inline int
0151 isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
0152 {
0153     return i2c_smbus_write_byte_data(isp->client, reg + 0, bits);
0154 }
0155 
0156 static inline int
0157 isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
0158 {
0159     return i2c_smbus_write_byte_data(isp->client, reg + 1, bits);
0160 }
0161 
0162 /*-------------------------------------------------------------------------*/
0163 
0164 /* identification */
0165 #define ISP1301_VENDOR_ID       0x00    /* u16 read */
0166 #define ISP1301_PRODUCT_ID      0x02    /* u16 read */
0167 #define ISP1301_BCD_DEVICE      0x14    /* u16 read */
0168 
0169 #define I2C_VENDOR_ID_PHILIPS       0x04cc
0170 #define I2C_PRODUCT_ID_PHILIPS_1301 0x1301
0171 
0172 /* operational registers */
0173 #define ISP1301_MODE_CONTROL_1      0x04    /* u8 read, set, +1 clear */
0174 #   define  MC1_SPEED       (1 << 0)
0175 #   define  MC1_SUSPEND     (1 << 1)
0176 #   define  MC1_DAT_SE0     (1 << 2)
0177 #   define  MC1_TRANSPARENT     (1 << 3)
0178 #   define  MC1_BDIS_ACON_EN    (1 << 4)
0179 #   define  MC1_OE_INT_EN       (1 << 5)
0180 #   define  MC1_UART_EN     (1 << 6)
0181 #   define  MC1_MASK        0x7f
0182 #define ISP1301_MODE_CONTROL_2      0x12    /* u8 read, set, +1 clear */
0183 #   define  MC2_GLOBAL_PWR_DN   (1 << 0)
0184 #   define  MC2_SPD_SUSP_CTRL   (1 << 1)
0185 #   define  MC2_BI_DI       (1 << 2)
0186 #   define  MC2_TRANSP_BDIR0    (1 << 3)
0187 #   define  MC2_TRANSP_BDIR1    (1 << 4)
0188 #   define  MC2_AUDIO_EN        (1 << 5)
0189 #   define  MC2_PSW_EN      (1 << 6)
0190 #   define  MC2_EN2V7       (1 << 7)
0191 #define ISP1301_OTG_CONTROL_1       0x06    /* u8 read, set, +1 clear */
0192 #   define  OTG1_DP_PULLUP      (1 << 0)
0193 #   define  OTG1_DM_PULLUP      (1 << 1)
0194 #   define  OTG1_DP_PULLDOWN    (1 << 2)
0195 #   define  OTG1_DM_PULLDOWN    (1 << 3)
0196 #   define  OTG1_ID_PULLDOWN    (1 << 4)
0197 #   define  OTG1_VBUS_DRV       (1 << 5)
0198 #   define  OTG1_VBUS_DISCHRG   (1 << 6)
0199 #   define  OTG1_VBUS_CHRG      (1 << 7)
0200 #define ISP1301_OTG_STATUS      0x10    /* u8 readonly */
0201 #   define  OTG_B_SESS_END      (1 << 6)
0202 #   define  OTG_B_SESS_VLD      (1 << 7)
0203 
0204 #define ISP1301_INTERRUPT_SOURCE    0x08    /* u8 read */
0205 #define ISP1301_INTERRUPT_LATCH     0x0A    /* u8 read, set, +1 clear */
0206 
0207 #define ISP1301_INTERRUPT_FALLING   0x0C    /* u8 read, set, +1 clear */
0208 #define ISP1301_INTERRUPT_RISING    0x0E    /* u8 read, set, +1 clear */
0209 
0210 /* same bitfields in all interrupt registers */
0211 #   define  INTR_VBUS_VLD       (1 << 0)
0212 #   define  INTR_SESS_VLD       (1 << 1)
0213 #   define  INTR_DP_HI      (1 << 2)
0214 #   define  INTR_ID_GND     (1 << 3)
0215 #   define  INTR_DM_HI      (1 << 4)
0216 #   define  INTR_ID_FLOAT       (1 << 5)
0217 #   define  INTR_BDIS_ACON      (1 << 6)
0218 #   define  INTR_CR_INT     (1 << 7)
0219 
0220 /*-------------------------------------------------------------------------*/
0221 
0222 static inline const char *state_name(struct isp1301 *isp)
0223 {
0224     return usb_otg_state_string(isp->phy.otg->state);
0225 }
0226 
0227 /*-------------------------------------------------------------------------*/
0228 
0229 /* NOTE:  some of this ISP1301 setup is specific to H2 boards;
0230  * not everything is guarded by board-specific checks, or even using
0231  * omap_usb_config data to deduce MC1_DAT_SE0 and MC2_BI_DI.
0232  *
0233  * ALSO:  this currently doesn't use ISP1301 low-power modes
0234  * while OTG is running.
0235  */
0236 
0237 static void power_down(struct isp1301 *isp)
0238 {
0239     isp->phy.otg->state = OTG_STATE_UNDEFINED;
0240 
0241     // isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
0242     isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
0243 
0244     isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_ID_PULLDOWN);
0245     isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
0246 }
0247 
0248 static void __maybe_unused power_up(struct isp1301 *isp)
0249 {
0250     // isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
0251     isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
0252 
0253     /* do this only when cpu is driving transceiver,
0254      * so host won't see a low speed device...
0255      */
0256     isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
0257 }
0258 
0259 #define NO_HOST_SUSPEND
0260 
0261 static int host_suspend(struct isp1301 *isp)
0262 {
0263 #ifdef  NO_HOST_SUSPEND
0264     return 0;
0265 #else
0266     struct device   *dev;
0267 
0268     if (!isp->phy.otg->host)
0269         return -ENODEV;
0270 
0271     /* Currently ASSUMES only the OTG port matters;
0272      * other ports could be active...
0273      */
0274     dev = isp->phy.otg->host->controller;
0275     return dev->driver->suspend(dev, 3, 0);
0276 #endif
0277 }
0278 
0279 static int host_resume(struct isp1301 *isp)
0280 {
0281 #ifdef  NO_HOST_SUSPEND
0282     return 0;
0283 #else
0284     struct device   *dev;
0285 
0286     if (!isp->phy.otg->host)
0287         return -ENODEV;
0288 
0289     dev = isp->phy.otg->host->controller;
0290     return dev->driver->resume(dev, 0);
0291 #endif
0292 }
0293 
0294 static int gadget_suspend(struct isp1301 *isp)
0295 {
0296     isp->phy.otg->gadget->b_hnp_enable = 0;
0297     isp->phy.otg->gadget->a_hnp_support = 0;
0298     isp->phy.otg->gadget->a_alt_hnp_support = 0;
0299     return usb_gadget_vbus_disconnect(isp->phy.otg->gadget);
0300 }
0301 
0302 /*-------------------------------------------------------------------------*/
0303 
0304 #define TIMER_MINUTES   10
0305 #define TIMER_JIFFIES   (TIMER_MINUTES * 60 * HZ)
0306 
0307 /* Almost all our I2C messaging comes from a work queue's task context.
0308  * NOTE: guaranteeing certain response times might mean we shouldn't
0309  * share keventd's work queue; a realtime task might be safest.
0310  */
0311 static void isp1301_defer_work(struct isp1301 *isp, int work)
0312 {
0313     int status;
0314 
0315     if (isp && !test_and_set_bit(work, &isp->todo)) {
0316         (void) get_device(&isp->client->dev);
0317         status = schedule_work(&isp->work);
0318         if (!status && !isp->working)
0319             dev_vdbg(&isp->client->dev,
0320                 "work item %d may be lost\n", work);
0321     }
0322 }
0323 
0324 /* called from irq handlers */
0325 static void a_idle(struct isp1301 *isp, const char *tag)
0326 {
0327     u32 l;
0328 
0329     if (isp->phy.otg->state == OTG_STATE_A_IDLE)
0330         return;
0331 
0332     isp->phy.otg->default_a = 1;
0333     if (isp->phy.otg->host) {
0334         isp->phy.otg->host->is_b_host = 0;
0335         host_suspend(isp);
0336     }
0337     if (isp->phy.otg->gadget) {
0338         isp->phy.otg->gadget->is_a_peripheral = 1;
0339         gadget_suspend(isp);
0340     }
0341     isp->phy.otg->state = OTG_STATE_A_IDLE;
0342     l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
0343     omap_writel(l, OTG_CTRL);
0344     isp->last_otg_ctrl = l;
0345     pr_debug("  --> %s/%s\n", state_name(isp), tag);
0346 }
0347 
0348 /* called from irq handlers */
0349 static void b_idle(struct isp1301 *isp, const char *tag)
0350 {
0351     u32 l;
0352 
0353     if (isp->phy.otg->state == OTG_STATE_B_IDLE)
0354         return;
0355 
0356     isp->phy.otg->default_a = 0;
0357     if (isp->phy.otg->host) {
0358         isp->phy.otg->host->is_b_host = 1;
0359         host_suspend(isp);
0360     }
0361     if (isp->phy.otg->gadget) {
0362         isp->phy.otg->gadget->is_a_peripheral = 0;
0363         gadget_suspend(isp);
0364     }
0365     isp->phy.otg->state = OTG_STATE_B_IDLE;
0366     l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
0367     omap_writel(l, OTG_CTRL);
0368     isp->last_otg_ctrl = l;
0369     pr_debug("  --> %s/%s\n", state_name(isp), tag);
0370 }
0371 
0372 static void
0373 dump_regs(struct isp1301 *isp, const char *label)
0374 {
0375     u8  ctrl = isp1301_get_u8(isp, ISP1301_OTG_CONTROL_1);
0376     u8  status = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
0377     u8  src = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
0378 
0379     pr_debug("otg: %06x, %s %s, otg/%02x stat/%02x.%02x\n",
0380         omap_readl(OTG_CTRL), label, state_name(isp),
0381         ctrl, status, src);
0382     /* mode control and irq enables don't change much */
0383 }
0384 
0385 /*-------------------------------------------------------------------------*/
0386 
0387 #ifdef  CONFIG_USB_OTG
0388 
0389 /*
0390  * The OMAP OTG controller handles most of the OTG state transitions.
0391  *
0392  * We translate isp1301 outputs (mostly voltage comparator status) into
0393  * OTG inputs; OTG outputs (mostly pullup/pulldown controls) and HNP state
0394  * flags into isp1301 inputs ... and infer state transitions.
0395  */
0396 
0397 #ifdef  VERBOSE
0398 
0399 static void check_state(struct isp1301 *isp, const char *tag)
0400 {
0401     enum usb_otg_state  state = OTG_STATE_UNDEFINED;
0402     u8          fsm = omap_readw(OTG_TEST) & 0x0ff;
0403     unsigned        extra = 0;
0404 
0405     switch (fsm) {
0406 
0407     /* default-b */
0408     case 0x0:
0409         state = OTG_STATE_B_IDLE;
0410         break;
0411     case 0x3:
0412     case 0x7:
0413         extra = 1;
0414     case 0x1:
0415         state = OTG_STATE_B_PERIPHERAL;
0416         break;
0417     case 0x11:
0418         state = OTG_STATE_B_SRP_INIT;
0419         break;
0420 
0421     /* extra dual-role default-b states */
0422     case 0x12:
0423     case 0x13:
0424     case 0x16:
0425         extra = 1;
0426     case 0x17:
0427         state = OTG_STATE_B_WAIT_ACON;
0428         break;
0429     case 0x34:
0430         state = OTG_STATE_B_HOST;
0431         break;
0432 
0433     /* default-a */
0434     case 0x36:
0435         state = OTG_STATE_A_IDLE;
0436         break;
0437     case 0x3c:
0438         state = OTG_STATE_A_WAIT_VFALL;
0439         break;
0440     case 0x7d:
0441         state = OTG_STATE_A_VBUS_ERR;
0442         break;
0443     case 0x9e:
0444     case 0x9f:
0445         extra = 1;
0446     case 0x89:
0447         state = OTG_STATE_A_PERIPHERAL;
0448         break;
0449     case 0xb7:
0450         state = OTG_STATE_A_WAIT_VRISE;
0451         break;
0452     case 0xb8:
0453         state = OTG_STATE_A_WAIT_BCON;
0454         break;
0455     case 0xb9:
0456         state = OTG_STATE_A_HOST;
0457         break;
0458     case 0xba:
0459         state = OTG_STATE_A_SUSPEND;
0460         break;
0461     default:
0462         break;
0463     }
0464     if (isp->phy.otg->state == state && !extra)
0465         return;
0466     pr_debug("otg: %s FSM %s/%02x, %s, %06x\n", tag,
0467         usb_otg_state_string(state), fsm, state_name(isp),
0468         omap_readl(OTG_CTRL));
0469 }
0470 
0471 #else
0472 
0473 static inline void check_state(struct isp1301 *isp, const char *tag) { }
0474 
0475 #endif
0476 
0477 /* outputs from ISP1301_INTERRUPT_SOURCE */
0478 static void update_otg1(struct isp1301 *isp, u8 int_src)
0479 {
0480     u32 otg_ctrl;
0481 
0482     otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
0483     otg_ctrl &= ~OTG_XCEIV_INPUTS;
0484     otg_ctrl &= ~(OTG_ID|OTG_ASESSVLD|OTG_VBUSVLD);
0485 
0486     if (int_src & INTR_SESS_VLD)
0487         otg_ctrl |= OTG_ASESSVLD;
0488     else if (isp->phy.otg->state == OTG_STATE_A_WAIT_VFALL) {
0489         a_idle(isp, "vfall");
0490         otg_ctrl &= ~OTG_CTRL_BITS;
0491     }
0492     if (int_src & INTR_VBUS_VLD)
0493         otg_ctrl |= OTG_VBUSVLD;
0494     if (int_src & INTR_ID_GND) {        /* default-A */
0495         if (isp->phy.otg->state == OTG_STATE_B_IDLE
0496                 || isp->phy.otg->state
0497                     == OTG_STATE_UNDEFINED) {
0498             a_idle(isp, "init");
0499             return;
0500         }
0501     } else {                /* default-B */
0502         otg_ctrl |= OTG_ID;
0503         if (isp->phy.otg->state == OTG_STATE_A_IDLE
0504             || isp->phy.otg->state == OTG_STATE_UNDEFINED) {
0505             b_idle(isp, "init");
0506             return;
0507         }
0508     }
0509     omap_writel(otg_ctrl, OTG_CTRL);
0510 }
0511 
0512 /* outputs from ISP1301_OTG_STATUS */
0513 static void update_otg2(struct isp1301 *isp, u8 otg_status)
0514 {
0515     u32 otg_ctrl;
0516 
0517     otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
0518     otg_ctrl &= ~OTG_XCEIV_INPUTS;
0519     otg_ctrl &= ~(OTG_BSESSVLD | OTG_BSESSEND);
0520     if (otg_status & OTG_B_SESS_VLD)
0521         otg_ctrl |= OTG_BSESSVLD;
0522     else if (otg_status & OTG_B_SESS_END)
0523         otg_ctrl |= OTG_BSESSEND;
0524     omap_writel(otg_ctrl, OTG_CTRL);
0525 }
0526 
0527 /* inputs going to ISP1301 */
0528 static void otg_update_isp(struct isp1301 *isp)
0529 {
0530     u32 otg_ctrl, otg_change;
0531     u8  set = OTG1_DM_PULLDOWN, clr = OTG1_DM_PULLUP;
0532 
0533     otg_ctrl = omap_readl(OTG_CTRL);
0534     otg_change = otg_ctrl ^ isp->last_otg_ctrl;
0535     isp->last_otg_ctrl = otg_ctrl;
0536     otg_ctrl = otg_ctrl & OTG_XCEIV_INPUTS;
0537 
0538     switch (isp->phy.otg->state) {
0539     case OTG_STATE_B_IDLE:
0540     case OTG_STATE_B_PERIPHERAL:
0541     case OTG_STATE_B_SRP_INIT:
0542         if (!(otg_ctrl & OTG_PULLUP)) {
0543             // if (otg_ctrl & OTG_B_HNPEN) {
0544             if (isp->phy.otg->gadget->b_hnp_enable) {
0545                 isp->phy.otg->state = OTG_STATE_B_WAIT_ACON;
0546                 pr_debug("  --> b_wait_acon\n");
0547             }
0548             goto pulldown;
0549         }
0550 pullup:
0551         set |= OTG1_DP_PULLUP;
0552         clr |= OTG1_DP_PULLDOWN;
0553         break;
0554     case OTG_STATE_A_SUSPEND:
0555     case OTG_STATE_A_PERIPHERAL:
0556         if (otg_ctrl & OTG_PULLUP)
0557             goto pullup;
0558         fallthrough;
0559     // case OTG_STATE_B_WAIT_ACON:
0560     default:
0561 pulldown:
0562         set |= OTG1_DP_PULLDOWN;
0563         clr |= OTG1_DP_PULLUP;
0564         break;
0565     }
0566 
0567 #   define toggle(OTG,ISP) do { \
0568         if (otg_ctrl & OTG) set |= ISP; \
0569         else clr |= ISP; \
0570         } while (0)
0571 
0572     if (!(isp->phy.otg->host))
0573         otg_ctrl &= ~OTG_DRV_VBUS;
0574 
0575     switch (isp->phy.otg->state) {
0576     case OTG_STATE_A_SUSPEND:
0577         if (otg_ctrl & OTG_DRV_VBUS) {
0578             set |= OTG1_VBUS_DRV;
0579             break;
0580         }
0581         /* HNP failed for some reason (A_AIDL_BDIS timeout) */
0582         notresponding(isp);
0583 
0584         fallthrough;
0585     case OTG_STATE_A_VBUS_ERR:
0586         isp->phy.otg->state = OTG_STATE_A_WAIT_VFALL;
0587         pr_debug("  --> a_wait_vfall\n");
0588         fallthrough;
0589     case OTG_STATE_A_WAIT_VFALL:
0590         /* FIXME usbcore thinks port power is still on ... */
0591         clr |= OTG1_VBUS_DRV;
0592         break;
0593     case OTG_STATE_A_IDLE:
0594         if (otg_ctrl & OTG_DRV_VBUS) {
0595             isp->phy.otg->state = OTG_STATE_A_WAIT_VRISE;
0596             pr_debug("  --> a_wait_vrise\n");
0597         }
0598         fallthrough;
0599     default:
0600         toggle(OTG_DRV_VBUS, OTG1_VBUS_DRV);
0601     }
0602 
0603     toggle(OTG_PU_VBUS, OTG1_VBUS_CHRG);
0604     toggle(OTG_PD_VBUS, OTG1_VBUS_DISCHRG);
0605 
0606 #   undef toggle
0607 
0608     isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, set);
0609     isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, clr);
0610 
0611     /* HNP switch to host or peripheral; and SRP */
0612     if (otg_change & OTG_PULLUP) {
0613         u32 l;
0614 
0615         switch (isp->phy.otg->state) {
0616         case OTG_STATE_B_IDLE:
0617             if (clr & OTG1_DP_PULLUP)
0618                 break;
0619             isp->phy.otg->state = OTG_STATE_B_PERIPHERAL;
0620             pr_debug("  --> b_peripheral\n");
0621             break;
0622         case OTG_STATE_A_SUSPEND:
0623             if (clr & OTG1_DP_PULLUP)
0624                 break;
0625             isp->phy.otg->state = OTG_STATE_A_PERIPHERAL;
0626             pr_debug("  --> a_peripheral\n");
0627             break;
0628         default:
0629             break;
0630         }
0631         l = omap_readl(OTG_CTRL);
0632         l |= OTG_PULLUP;
0633         omap_writel(l, OTG_CTRL);
0634     }
0635 
0636     check_state(isp, __func__);
0637     dump_regs(isp, "otg->isp1301");
0638 }
0639 
0640 static irqreturn_t omap_otg_irq(int irq, void *_isp)
0641 {
0642     u16     otg_irq = omap_readw(OTG_IRQ_SRC);
0643     u32     otg_ctrl;
0644     int     ret = IRQ_NONE;
0645     struct isp1301  *isp = _isp;
0646     struct usb_otg  *otg = isp->phy.otg;
0647 
0648     /* update ISP1301 transceiver from OTG controller */
0649     if (otg_irq & OPRT_CHG) {
0650         omap_writew(OPRT_CHG, OTG_IRQ_SRC);
0651         isp1301_defer_work(isp, WORK_UPDATE_ISP);
0652         ret = IRQ_HANDLED;
0653 
0654     /* SRP to become b_peripheral failed */
0655     } else if (otg_irq & B_SRP_TMROUT) {
0656         pr_debug("otg: B_SRP_TIMEOUT, %06x\n", omap_readl(OTG_CTRL));
0657         notresponding(isp);
0658 
0659         /* gadget drivers that care should monitor all kinds of
0660          * remote wakeup (SRP, normal) using their own timer
0661          * to give "check cable and A-device" messages.
0662          */
0663         if (isp->phy.otg->state == OTG_STATE_B_SRP_INIT)
0664             b_idle(isp, "srp_timeout");
0665 
0666         omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
0667         ret = IRQ_HANDLED;
0668 
0669     /* HNP to become b_host failed */
0670     } else if (otg_irq & B_HNP_FAIL) {
0671         pr_debug("otg: %s B_HNP_FAIL, %06x\n",
0672                 state_name(isp), omap_readl(OTG_CTRL));
0673         notresponding(isp);
0674 
0675         otg_ctrl = omap_readl(OTG_CTRL);
0676         otg_ctrl |= OTG_BUSDROP;
0677         otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
0678         omap_writel(otg_ctrl, OTG_CTRL);
0679 
0680         /* subset of b_peripheral()... */
0681         isp->phy.otg->state = OTG_STATE_B_PERIPHERAL;
0682         pr_debug("  --> b_peripheral\n");
0683 
0684         omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
0685         ret = IRQ_HANDLED;
0686 
0687     /* detect SRP from B-device ... */
0688     } else if (otg_irq & A_SRP_DETECT) {
0689         pr_debug("otg: %s SRP_DETECT, %06x\n",
0690                 state_name(isp), omap_readl(OTG_CTRL));
0691 
0692         isp1301_defer_work(isp, WORK_UPDATE_OTG);
0693         switch (isp->phy.otg->state) {
0694         case OTG_STATE_A_IDLE:
0695             if (!otg->host)
0696                 break;
0697             isp1301_defer_work(isp, WORK_HOST_RESUME);
0698             otg_ctrl = omap_readl(OTG_CTRL);
0699             otg_ctrl |= OTG_A_BUSREQ;
0700             otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
0701                     & ~OTG_XCEIV_INPUTS
0702                     & OTG_CTRL_MASK;
0703             omap_writel(otg_ctrl, OTG_CTRL);
0704             break;
0705         default:
0706             break;
0707         }
0708 
0709         omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
0710         ret = IRQ_HANDLED;
0711 
0712     /* timer expired:  T(a_wait_bcon) and maybe T(a_wait_vrise)
0713      * we don't track them separately
0714      */
0715     } else if (otg_irq & A_REQ_TMROUT) {
0716         otg_ctrl = omap_readl(OTG_CTRL);
0717         pr_info("otg: BCON_TMOUT from %s, %06x\n",
0718                 state_name(isp), otg_ctrl);
0719         notresponding(isp);
0720 
0721         otg_ctrl |= OTG_BUSDROP;
0722         otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
0723         omap_writel(otg_ctrl, OTG_CTRL);
0724         isp->phy.otg->state = OTG_STATE_A_WAIT_VFALL;
0725 
0726         omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
0727         ret = IRQ_HANDLED;
0728 
0729     /* A-supplied voltage fell too low; overcurrent */
0730     } else if (otg_irq & A_VBUS_ERR) {
0731         otg_ctrl = omap_readl(OTG_CTRL);
0732         printk(KERN_ERR "otg: %s, VBUS_ERR %04x ctrl %06x\n",
0733             state_name(isp), otg_irq, otg_ctrl);
0734 
0735         otg_ctrl |= OTG_BUSDROP;
0736         otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
0737         omap_writel(otg_ctrl, OTG_CTRL);
0738         isp->phy.otg->state = OTG_STATE_A_VBUS_ERR;
0739 
0740         omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
0741         ret = IRQ_HANDLED;
0742 
0743     /* switch driver; the transceiver code activates it,
0744      * ungating the udc clock or resuming OHCI.
0745      */
0746     } else if (otg_irq & DRIVER_SWITCH) {
0747         int kick = 0;
0748 
0749         otg_ctrl = omap_readl(OTG_CTRL);
0750         printk(KERN_NOTICE "otg: %s, SWITCH to %s, ctrl %06x\n",
0751                 state_name(isp),
0752                 (otg_ctrl & OTG_DRIVER_SEL)
0753                     ? "gadget" : "host",
0754                 otg_ctrl);
0755         isp1301_defer_work(isp, WORK_UPDATE_ISP);
0756 
0757         /* role is peripheral */
0758         if (otg_ctrl & OTG_DRIVER_SEL) {
0759             switch (isp->phy.otg->state) {
0760             case OTG_STATE_A_IDLE:
0761                 b_idle(isp, __func__);
0762                 break;
0763             default:
0764                 break;
0765             }
0766             isp1301_defer_work(isp, WORK_UPDATE_ISP);
0767 
0768         /* role is host */
0769         } else {
0770             if (!(otg_ctrl & OTG_ID)) {
0771                 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
0772                 omap_writel(otg_ctrl | OTG_A_BUSREQ, OTG_CTRL);
0773             }
0774 
0775             if (otg->host) {
0776                 switch (isp->phy.otg->state) {
0777                 case OTG_STATE_B_WAIT_ACON:
0778                     isp->phy.otg->state = OTG_STATE_B_HOST;
0779                     pr_debug("  --> b_host\n");
0780                     kick = 1;
0781                     break;
0782                 case OTG_STATE_A_WAIT_BCON:
0783                     isp->phy.otg->state = OTG_STATE_A_HOST;
0784                     pr_debug("  --> a_host\n");
0785                     break;
0786                 case OTG_STATE_A_PERIPHERAL:
0787                     isp->phy.otg->state = OTG_STATE_A_WAIT_BCON;
0788                     pr_debug("  --> a_wait_bcon\n");
0789                     break;
0790                 default:
0791                     break;
0792                 }
0793                 isp1301_defer_work(isp, WORK_HOST_RESUME);
0794             }
0795         }
0796 
0797         omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
0798         ret = IRQ_HANDLED;
0799 
0800         if (kick)
0801             usb_bus_start_enum(otg->host, otg->host->otg_port);
0802     }
0803 
0804     check_state(isp, __func__);
0805     return ret;
0806 }
0807 
0808 static struct platform_device *otg_dev;
0809 
0810 static int isp1301_otg_init(struct isp1301 *isp)
0811 {
0812     u32 l;
0813 
0814     if (!otg_dev)
0815         return -ENODEV;
0816 
0817     dump_regs(isp, __func__);
0818     /* some of these values are board-specific... */
0819     l = omap_readl(OTG_SYSCON_2);
0820     l |= OTG_EN
0821         /* for B-device: */
0822         | SRP_GPDATA        /* 9msec Bdev D+ pulse */
0823         | SRP_GPDVBUS       /* discharge after VBUS pulse */
0824         // | (3 << 24)      /* 2msec VBUS pulse */
0825         /* for A-device: */
0826         | (0 << 20)     /* 200ms nominal A_WAIT_VRISE timer */
0827         | SRP_DPW       /* detect 167+ns SRP pulses */
0828         | SRP_DATA | SRP_VBUS   /* accept both kinds of SRP pulse */
0829         ;
0830     omap_writel(l, OTG_SYSCON_2);
0831 
0832     update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
0833     update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
0834 
0835     check_state(isp, __func__);
0836     pr_debug("otg: %s, %s %06x\n",
0837             state_name(isp), __func__, omap_readl(OTG_CTRL));
0838 
0839     omap_writew(DRIVER_SWITCH | OPRT_CHG
0840             | B_SRP_TMROUT | B_HNP_FAIL
0841             | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT, OTG_IRQ_EN);
0842 
0843     l = omap_readl(OTG_SYSCON_2);
0844     l |= OTG_EN;
0845     omap_writel(l, OTG_SYSCON_2);
0846 
0847     return 0;
0848 }
0849 
0850 static int otg_probe(struct platform_device *dev)
0851 {
0852     // struct omap_usb_config *config = dev->platform_data;
0853 
0854     otg_dev = dev;
0855     return 0;
0856 }
0857 
0858 static int otg_remove(struct platform_device *dev)
0859 {
0860     otg_dev = NULL;
0861     return 0;
0862 }
0863 
0864 static struct platform_driver omap_otg_driver = {
0865     .probe      = otg_probe,
0866     .remove     = otg_remove,
0867     .driver     = {
0868         .name   = "omap_otg",
0869     },
0870 };
0871 
0872 static int otg_bind(struct isp1301 *isp)
0873 {
0874     int status;
0875 
0876     if (otg_dev)
0877         return -EBUSY;
0878 
0879     status = platform_driver_register(&omap_otg_driver);
0880     if (status < 0)
0881         return status;
0882 
0883     if (otg_dev)
0884         status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
0885                 0, DRIVER_NAME, isp);
0886     else
0887         status = -ENODEV;
0888 
0889     if (status < 0)
0890         platform_driver_unregister(&omap_otg_driver);
0891     return status;
0892 }
0893 
0894 static void otg_unbind(struct isp1301 *isp)
0895 {
0896     if (!otg_dev)
0897         return;
0898     free_irq(otg_dev->resource[1].start, isp);
0899 }
0900 
0901 #else
0902 
0903 /* OTG controller isn't clocked */
0904 
0905 #endif  /* CONFIG_USB_OTG */
0906 
0907 /*-------------------------------------------------------------------------*/
0908 
0909 static void b_peripheral(struct isp1301 *isp)
0910 {
0911     u32 l;
0912 
0913     l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
0914     omap_writel(l, OTG_CTRL);
0915 
0916     usb_gadget_vbus_connect(isp->phy.otg->gadget);
0917 
0918 #ifdef  CONFIG_USB_OTG
0919     enable_vbus_draw(isp, 8);
0920     otg_update_isp(isp);
0921 #else
0922     enable_vbus_draw(isp, 100);
0923     /* UDC driver just set OTG_BSESSVLD */
0924     isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
0925     isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
0926     isp->phy.otg->state = OTG_STATE_B_PERIPHERAL;
0927     pr_debug("  --> b_peripheral\n");
0928     dump_regs(isp, "2periph");
0929 #endif
0930 }
0931 
0932 static void isp_update_otg(struct isp1301 *isp, u8 stat)
0933 {
0934     struct usb_otg      *otg = isp->phy.otg;
0935     u8          isp_stat, isp_bstat;
0936     enum usb_otg_state  state = isp->phy.otg->state;
0937 
0938     if (stat & INTR_BDIS_ACON)
0939         pr_debug("OTG:  BDIS_ACON, %s\n", state_name(isp));
0940 
0941     /* start certain state transitions right away */
0942     isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
0943     if (isp_stat & INTR_ID_GND) {
0944         if (otg->default_a) {
0945             switch (state) {
0946             case OTG_STATE_B_IDLE:
0947                 a_idle(isp, "idle");
0948                 fallthrough;
0949             case OTG_STATE_A_IDLE:
0950                 enable_vbus_source(isp);
0951                 fallthrough;
0952             case OTG_STATE_A_WAIT_VRISE:
0953                 /* we skip over OTG_STATE_A_WAIT_BCON, since
0954                  * the HC will transition to A_HOST (or
0955                  * A_SUSPEND!) without our noticing except
0956                  * when HNP is used.
0957                  */
0958                 if (isp_stat & INTR_VBUS_VLD)
0959                     isp->phy.otg->state = OTG_STATE_A_HOST;
0960                 break;
0961             case OTG_STATE_A_WAIT_VFALL:
0962                 if (!(isp_stat & INTR_SESS_VLD))
0963                     a_idle(isp, "vfell");
0964                 break;
0965             default:
0966                 if (!(isp_stat & INTR_VBUS_VLD))
0967                     isp->phy.otg->state = OTG_STATE_A_VBUS_ERR;
0968                 break;
0969             }
0970             isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
0971         } else {
0972             switch (state) {
0973             case OTG_STATE_B_PERIPHERAL:
0974             case OTG_STATE_B_HOST:
0975             case OTG_STATE_B_WAIT_ACON:
0976                 usb_gadget_vbus_disconnect(otg->gadget);
0977                 break;
0978             default:
0979                 break;
0980             }
0981             if (state != OTG_STATE_A_IDLE)
0982                 a_idle(isp, "id");
0983             if (otg->host && state == OTG_STATE_A_IDLE)
0984                 isp1301_defer_work(isp, WORK_HOST_RESUME);
0985             isp_bstat = 0;
0986         }
0987     } else {
0988         u32 l;
0989 
0990         /* if user unplugged mini-A end of cable,
0991          * don't bypass A_WAIT_VFALL.
0992          */
0993         if (otg->default_a) {
0994             switch (state) {
0995             default:
0996                 isp->phy.otg->state = OTG_STATE_A_WAIT_VFALL;
0997                 break;
0998             case OTG_STATE_A_WAIT_VFALL:
0999                 state = OTG_STATE_A_IDLE;
1000                 /* hub_wq may take a while to notice and
1001                  * handle this disconnect, so don't go
1002                  * to B_IDLE quite yet.
1003                  */
1004                 break;
1005             case OTG_STATE_A_IDLE:
1006                 host_suspend(isp);
1007                 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
1008                         MC1_BDIS_ACON_EN);
1009                 isp->phy.otg->state = OTG_STATE_B_IDLE;
1010                 l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1011                 l &= ~OTG_CTRL_BITS;
1012                 omap_writel(l, OTG_CTRL);
1013                 break;
1014             case OTG_STATE_B_IDLE:
1015                 break;
1016             }
1017         }
1018         isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
1019 
1020         switch (isp->phy.otg->state) {
1021         case OTG_STATE_B_PERIPHERAL:
1022         case OTG_STATE_B_WAIT_ACON:
1023         case OTG_STATE_B_HOST:
1024             if (likely(isp_bstat & OTG_B_SESS_VLD))
1025                 break;
1026             enable_vbus_draw(isp, 0);
1027 #ifndef CONFIG_USB_OTG
1028             /* UDC driver will clear OTG_BSESSVLD */
1029             isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1030                         OTG1_DP_PULLDOWN);
1031             isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1032                         OTG1_DP_PULLUP);
1033             dump_regs(isp, __func__);
1034 #endif
1035             fallthrough;
1036         case OTG_STATE_B_SRP_INIT:
1037             b_idle(isp, __func__);
1038             l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
1039             omap_writel(l, OTG_CTRL);
1040             fallthrough;
1041         case OTG_STATE_B_IDLE:
1042             if (otg->gadget && (isp_bstat & OTG_B_SESS_VLD)) {
1043 #ifdef  CONFIG_USB_OTG
1044                 update_otg1(isp, isp_stat);
1045                 update_otg2(isp, isp_bstat);
1046 #endif
1047                 b_peripheral(isp);
1048             } else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
1049                 isp_bstat |= OTG_B_SESS_END;
1050             break;
1051         case OTG_STATE_A_WAIT_VFALL:
1052             break;
1053         default:
1054             pr_debug("otg: unsupported b-device %s\n",
1055                 state_name(isp));
1056             break;
1057         }
1058     }
1059 
1060     if (state != isp->phy.otg->state)
1061         pr_debug("  isp, %s -> %s\n",
1062                 usb_otg_state_string(state), state_name(isp));
1063 
1064 #ifdef  CONFIG_USB_OTG
1065     /* update the OTG controller state to match the isp1301; may
1066      * trigger OPRT_CHG irqs for changes going to the isp1301.
1067      */
1068     update_otg1(isp, isp_stat);
1069     update_otg2(isp, isp_bstat);
1070     check_state(isp, __func__);
1071 #endif
1072 
1073     dump_regs(isp, "isp1301->otg");
1074 }
1075 
1076 /*-------------------------------------------------------------------------*/
1077 
1078 static u8 isp1301_clear_latch(struct isp1301 *isp)
1079 {
1080     u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
1081     isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
1082     return latch;
1083 }
1084 
1085 static void
1086 isp1301_work(struct work_struct *work)
1087 {
1088     struct isp1301  *isp = container_of(work, struct isp1301, work);
1089     int     stop;
1090 
1091     /* implicit lock:  we're the only task using this device */
1092     isp->working = 1;
1093     do {
1094         stop = test_bit(WORK_STOP, &isp->todo);
1095 
1096 #ifdef  CONFIG_USB_OTG
1097         /* transfer state from otg engine to isp1301 */
1098         if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
1099             otg_update_isp(isp);
1100             put_device(&isp->client->dev);
1101         }
1102 #endif
1103         /* transfer state from isp1301 to otg engine */
1104         if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
1105             u8      stat = isp1301_clear_latch(isp);
1106 
1107             isp_update_otg(isp, stat);
1108             put_device(&isp->client->dev);
1109         }
1110 
1111         if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
1112             u32 otg_ctrl;
1113 
1114             /*
1115              * skip A_WAIT_VRISE; hc transitions invisibly
1116              * skip A_WAIT_BCON; same.
1117              */
1118             switch (isp->phy.otg->state) {
1119             case OTG_STATE_A_WAIT_BCON:
1120             case OTG_STATE_A_WAIT_VRISE:
1121                 isp->phy.otg->state = OTG_STATE_A_HOST;
1122                 pr_debug("  --> a_host\n");
1123                 otg_ctrl = omap_readl(OTG_CTRL);
1124                 otg_ctrl |= OTG_A_BUSREQ;
1125                 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
1126                         & OTG_CTRL_MASK;
1127                 omap_writel(otg_ctrl, OTG_CTRL);
1128                 break;
1129             case OTG_STATE_B_WAIT_ACON:
1130                 isp->phy.otg->state = OTG_STATE_B_HOST;
1131                 pr_debug("  --> b_host (acon)\n");
1132                 break;
1133             case OTG_STATE_B_HOST:
1134             case OTG_STATE_B_IDLE:
1135             case OTG_STATE_A_IDLE:
1136                 break;
1137             default:
1138                 pr_debug("  host resume in %s\n",
1139                         state_name(isp));
1140             }
1141             host_resume(isp);
1142             // mdelay(10);
1143             put_device(&isp->client->dev);
1144         }
1145 
1146         if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
1147 #ifdef  VERBOSE
1148             dump_regs(isp, "timer");
1149             if (!stop)
1150                 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1151 #endif
1152             put_device(&isp->client->dev);
1153         }
1154 
1155         if (isp->todo)
1156             dev_vdbg(&isp->client->dev,
1157                 "work done, todo = 0x%lx\n",
1158                 isp->todo);
1159         if (stop) {
1160             dev_dbg(&isp->client->dev, "stop\n");
1161             break;
1162         }
1163     } while (isp->todo);
1164     isp->working = 0;
1165 }
1166 
1167 static irqreturn_t isp1301_irq(int irq, void *isp)
1168 {
1169     isp1301_defer_work(isp, WORK_UPDATE_OTG);
1170     return IRQ_HANDLED;
1171 }
1172 
1173 static void isp1301_timer(struct timer_list *t)
1174 {
1175     struct isp1301 *isp = from_timer(isp, t, timer);
1176 
1177     isp1301_defer_work(isp, WORK_TIMER);
1178 }
1179 
1180 /*-------------------------------------------------------------------------*/
1181 
1182 static void isp1301_release(struct device *dev)
1183 {
1184     struct isp1301  *isp;
1185 
1186     isp = dev_get_drvdata(dev);
1187 
1188     /* FIXME -- not with a "new style" driver, it doesn't!! */
1189 
1190     /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
1191     if (isp->i2c_release)
1192         isp->i2c_release(dev);
1193     kfree(isp->phy.otg);
1194     kfree (isp);
1195 }
1196 
1197 static struct isp1301 *the_transceiver;
1198 
1199 static int isp1301_remove(struct i2c_client *i2c)
1200 {
1201     struct isp1301  *isp;
1202 
1203     isp = i2c_get_clientdata(i2c);
1204 
1205     isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1206     isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1207     free_irq(i2c->irq, isp);
1208 #ifdef  CONFIG_USB_OTG
1209     otg_unbind(isp);
1210 #endif
1211     set_bit(WORK_STOP, &isp->todo);
1212     del_timer_sync(&isp->timer);
1213     flush_work(&isp->work);
1214 
1215     put_device(&i2c->dev);
1216     the_transceiver = NULL;
1217 
1218     return 0;
1219 }
1220 
1221 /*-------------------------------------------------------------------------*/
1222 
1223 /* NOTE:  three modes are possible here, only one of which
1224  * will be standards-conformant on any given system:
1225  *
1226  *  - OTG mode (dual-role), required if there's a Mini-AB connector
1227  *  - HOST mode, for when there's one or more A (host) connectors
1228  *  - DEVICE mode, for when there's a B/Mini-B (device) connector
1229  *
1230  * As a rule, you won't have an isp1301 chip unless it's there to
1231  * support the OTG mode.  Other modes help testing USB controllers
1232  * in isolation from (full) OTG support, or maybe so later board
1233  * revisions can help to support those feature.
1234  */
1235 
1236 #ifdef  CONFIG_USB_OTG
1237 
1238 static int isp1301_otg_enable(struct isp1301 *isp)
1239 {
1240     power_up(isp);
1241     isp1301_otg_init(isp);
1242 
1243     /* NOTE:  since we don't change this, this provides
1244      * a few more interrupts than are strictly needed.
1245      */
1246     isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1247         INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1248     isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1249         INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1250 
1251     dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
1252 
1253     return 0;
1254 }
1255 
1256 #endif
1257 
1258 /* add or disable the host device+driver */
1259 static int
1260 isp1301_set_host(struct usb_otg *otg, struct usb_bus *host)
1261 {
1262     struct isp1301  *isp = container_of(otg->usb_phy, struct isp1301, phy);
1263 
1264     if (isp != the_transceiver)
1265         return -ENODEV;
1266 
1267     if (!host) {
1268         omap_writew(0, OTG_IRQ_EN);
1269         power_down(isp);
1270         otg->host = NULL;
1271         return 0;
1272     }
1273 
1274 #ifdef  CONFIG_USB_OTG
1275     otg->host = host;
1276     dev_dbg(&isp->client->dev, "registered host\n");
1277     host_suspend(isp);
1278     if (otg->gadget)
1279         return isp1301_otg_enable(isp);
1280     return 0;
1281 
1282 #elif !IS_ENABLED(CONFIG_USB_OMAP)
1283     // FIXME update its refcount
1284     otg->host = host;
1285 
1286     power_up(isp);
1287 
1288     if (machine_is_omap_h2())
1289         isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1290 
1291     dev_info(&isp->client->dev, "A-Host sessions ok\n");
1292     isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1293         INTR_ID_GND);
1294     isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1295         INTR_ID_GND);
1296 
1297     /* If this has a Mini-AB connector, this mode is highly
1298      * nonstandard ... but can be handy for testing, especially with
1299      * the Mini-A end of an OTG cable.  (Or something nonstandard
1300      * like MiniB-to-StandardB, maybe built with a gender mender.)
1301      */
1302     isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
1303 
1304     dump_regs(isp, __func__);
1305 
1306     return 0;
1307 
1308 #else
1309     dev_dbg(&isp->client->dev, "host sessions not allowed\n");
1310     return -EINVAL;
1311 #endif
1312 
1313 }
1314 
1315 static int
1316 isp1301_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
1317 {
1318     struct isp1301  *isp = container_of(otg->usb_phy, struct isp1301, phy);
1319 
1320     if (isp != the_transceiver)
1321         return -ENODEV;
1322 
1323     if (!gadget) {
1324         omap_writew(0, OTG_IRQ_EN);
1325         if (!otg->default_a)
1326             enable_vbus_draw(isp, 0);
1327         usb_gadget_vbus_disconnect(otg->gadget);
1328         otg->gadget = NULL;
1329         power_down(isp);
1330         return 0;
1331     }
1332 
1333 #ifdef  CONFIG_USB_OTG
1334     otg->gadget = gadget;
1335     dev_dbg(&isp->client->dev, "registered gadget\n");
1336     /* gadget driver may be suspended until vbus_connect () */
1337     if (otg->host)
1338         return isp1301_otg_enable(isp);
1339     return 0;
1340 
1341 #elif   !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
1342     otg->gadget = gadget;
1343     // FIXME update its refcount
1344 
1345     {
1346         u32 l;
1347 
1348         l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1349         l &= ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS);
1350         l |= OTG_ID;
1351         omap_writel(l, OTG_CTRL);
1352     }
1353 
1354     power_up(isp);
1355     isp->phy.otg->state = OTG_STATE_B_IDLE;
1356 
1357     if (machine_is_omap_h2() || machine_is_omap_h3())
1358         isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1359 
1360     isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1361         INTR_SESS_VLD);
1362     isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1363         INTR_VBUS_VLD);
1364     dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
1365     dump_regs(isp, __func__);
1366 
1367     /* If this has a Mini-AB connector, this mode is highly
1368      * nonstandard ... but can be handy for testing, so long
1369      * as you don't plug a Mini-A cable into the jack.
1370      */
1371     if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
1372         b_peripheral(isp);
1373 
1374     return 0;
1375 
1376 #else
1377     dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
1378     return -EINVAL;
1379 #endif
1380 }
1381 
1382 
1383 /*-------------------------------------------------------------------------*/
1384 
1385 static int
1386 isp1301_set_power(struct usb_phy *dev, unsigned mA)
1387 {
1388     if (!the_transceiver)
1389         return -ENODEV;
1390     if (dev->otg->state == OTG_STATE_B_PERIPHERAL)
1391         enable_vbus_draw(the_transceiver, mA);
1392     return 0;
1393 }
1394 
1395 static int
1396 isp1301_start_srp(struct usb_otg *otg)
1397 {
1398     struct isp1301  *isp = container_of(otg->usb_phy, struct isp1301, phy);
1399     u32     otg_ctrl;
1400 
1401     if (isp != the_transceiver || isp->phy.otg->state != OTG_STATE_B_IDLE)
1402         return -ENODEV;
1403 
1404     otg_ctrl = omap_readl(OTG_CTRL);
1405     if (!(otg_ctrl & OTG_BSESSEND))
1406         return -EINVAL;
1407 
1408     otg_ctrl |= OTG_B_BUSREQ;
1409     otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
1410     omap_writel(otg_ctrl, OTG_CTRL);
1411     isp->phy.otg->state = OTG_STATE_B_SRP_INIT;
1412 
1413     pr_debug("otg: SRP, %s ... %06x\n", state_name(isp),
1414             omap_readl(OTG_CTRL));
1415 #ifdef  CONFIG_USB_OTG
1416     check_state(isp, __func__);
1417 #endif
1418     return 0;
1419 }
1420 
1421 static int
1422 isp1301_start_hnp(struct usb_otg *otg)
1423 {
1424 #ifdef  CONFIG_USB_OTG
1425     struct isp1301  *isp = container_of(otg->usb_phy, struct isp1301, phy);
1426     u32 l;
1427 
1428     if (isp != the_transceiver)
1429         return -ENODEV;
1430     if (otg->default_a && (otg->host == NULL || !otg->host->b_hnp_enable))
1431         return -ENOTCONN;
1432     if (!otg->default_a && (otg->gadget == NULL
1433             || !otg->gadget->b_hnp_enable))
1434         return -ENOTCONN;
1435 
1436     /* We want hardware to manage most HNP protocol timings.
1437      * So do this part as early as possible...
1438      */
1439     switch (isp->phy.otg->state) {
1440     case OTG_STATE_B_HOST:
1441         isp->phy.otg->state = OTG_STATE_B_PERIPHERAL;
1442         /* caller will suspend next */
1443         break;
1444     case OTG_STATE_A_HOST:
1445 #if 0
1446         /* autoconnect mode avoids irq latency bugs */
1447         isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1448                 MC1_BDIS_ACON_EN);
1449 #endif
1450         /* caller must suspend then clear A_BUSREQ */
1451         usb_gadget_vbus_connect(otg->gadget);
1452         l = omap_readl(OTG_CTRL);
1453         l |= OTG_A_SETB_HNPEN;
1454         omap_writel(l, OTG_CTRL);
1455 
1456         break;
1457     case OTG_STATE_A_PERIPHERAL:
1458         /* initiated by B-Host suspend */
1459         break;
1460     default:
1461         return -EILSEQ;
1462     }
1463     pr_debug("otg: HNP %s, %06x ...\n",
1464         state_name(isp), omap_readl(OTG_CTRL));
1465     check_state(isp, __func__);
1466     return 0;
1467 #else
1468     /* srp-only */
1469     return -EINVAL;
1470 #endif
1471 }
1472 
1473 /*-------------------------------------------------------------------------*/
1474 
1475 static int
1476 isp1301_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1477 {
1478     int         status;
1479     struct isp1301      *isp;
1480     int irq;
1481 
1482     if (the_transceiver)
1483         return 0;
1484 
1485     isp = kzalloc(sizeof *isp, GFP_KERNEL);
1486     if (!isp)
1487         return 0;
1488 
1489     isp->phy.otg = kzalloc(sizeof *isp->phy.otg, GFP_KERNEL);
1490     if (!isp->phy.otg) {
1491         kfree(isp);
1492         return 0;
1493     }
1494 
1495     INIT_WORK(&isp->work, isp1301_work);
1496     timer_setup(&isp->timer, isp1301_timer, 0);
1497 
1498     i2c_set_clientdata(i2c, isp);
1499     isp->client = i2c;
1500 
1501     /* verify the chip (shouldn't be necessary) */
1502     status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1503     if (status != I2C_VENDOR_ID_PHILIPS) {
1504         dev_dbg(&i2c->dev, "not philips id: %d\n", status);
1505         goto fail;
1506     }
1507     status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1508     if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1509         dev_dbg(&i2c->dev, "not isp1301, %d\n", status);
1510         goto fail;
1511     }
1512     isp->i2c_release = i2c->dev.release;
1513     i2c->dev.release = isp1301_release;
1514 
1515     /* initial development used chiprev 2.00 */
1516     status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
1517     dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
1518         status >> 8, status & 0xff);
1519 
1520     /* make like power-on reset */
1521     isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
1522 
1523     isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
1524     isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
1525 
1526     isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1527                 OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
1528     isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1529                 ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
1530 
1531     isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
1532     isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1533     isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1534 
1535 #ifdef  CONFIG_USB_OTG
1536     status = otg_bind(isp);
1537     if (status < 0) {
1538         dev_dbg(&i2c->dev, "can't bind OTG\n");
1539         goto fail;
1540     }
1541 #endif
1542 
1543     if (machine_is_omap_h2()) {
1544         struct gpio_desc *gpiod;
1545 
1546         /* full speed signaling by default */
1547         isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1548             MC1_SPEED);
1549         isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
1550             MC2_SPD_SUSP_CTRL);
1551 
1552         gpiod = devm_gpiod_get(&i2c->dev, NULL, GPIOD_IN);
1553         if (IS_ERR(gpiod)) {
1554             dev_err(&i2c->dev, "cannot obtain H2 GPIO\n");
1555             goto fail;
1556         }
1557         gpiod_set_consumer_name(gpiod, "isp1301");
1558         irq = gpiod_to_irq(gpiod);
1559         isp->irq_type = IRQF_TRIGGER_FALLING;
1560     } else {
1561         irq = i2c->irq;
1562     }
1563 
1564     status = request_irq(irq, isp1301_irq,
1565             isp->irq_type, DRIVER_NAME, isp);
1566     if (status < 0) {
1567         dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
1568                 i2c->irq, status);
1569         goto fail;
1570     }
1571 
1572     isp->phy.dev = &i2c->dev;
1573     isp->phy.label = DRIVER_NAME;
1574     isp->phy.set_power = isp1301_set_power;
1575 
1576     isp->phy.otg->usb_phy = &isp->phy;
1577     isp->phy.otg->set_host = isp1301_set_host;
1578     isp->phy.otg->set_peripheral = isp1301_set_peripheral;
1579     isp->phy.otg->start_srp = isp1301_start_srp;
1580     isp->phy.otg->start_hnp = isp1301_start_hnp;
1581 
1582     enable_vbus_draw(isp, 0);
1583     power_down(isp);
1584     the_transceiver = isp;
1585 
1586 #ifdef  CONFIG_USB_OTG
1587     update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
1588     update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
1589 #endif
1590 
1591     dump_regs(isp, __func__);
1592 
1593 #ifdef  VERBOSE
1594     mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1595     dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
1596 #endif
1597 
1598     status = usb_add_phy(&isp->phy, USB_PHY_TYPE_USB2);
1599     if (status < 0)
1600         dev_err(&i2c->dev, "can't register transceiver, %d\n",
1601             status);
1602 
1603     return 0;
1604 
1605 fail:
1606     kfree(isp->phy.otg);
1607     kfree(isp);
1608     return -ENODEV;
1609 }
1610 
1611 static const struct i2c_device_id isp1301_id[] = {
1612     { "isp1301_omap", 0 },
1613     { }
1614 };
1615 MODULE_DEVICE_TABLE(i2c, isp1301_id);
1616 
1617 static struct i2c_driver isp1301_driver = {
1618     .driver = {
1619         .name   = "isp1301_omap",
1620     },
1621     .probe      = isp1301_probe,
1622     .remove     = isp1301_remove,
1623     .id_table   = isp1301_id,
1624 };
1625 
1626 /*-------------------------------------------------------------------------*/
1627 
1628 static int __init isp_init(void)
1629 {
1630     return i2c_add_driver(&isp1301_driver);
1631 }
1632 subsys_initcall(isp_init);
1633 
1634 static void __exit isp_exit(void)
1635 {
1636     if (the_transceiver)
1637         usb_remove_phy(&the_transceiver->phy);
1638     i2c_del_driver(&isp1301_driver);
1639 }
1640 module_exit(isp_exit);
1641