Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * USB transceiver driver for AB8500 family chips
0004  *
0005  * Copyright (C) 2010-2013 ST-Ericsson AB
0006  * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
0007  * Avinash Kumar <avinash.kumar@stericsson.com>
0008  * Thirupathi Chippakurthy <thirupathi.chippakurthy@stericsson.com>
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/usb/otg.h>
0014 #include <linux/slab.h>
0015 #include <linux/notifier.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/delay.h>
0018 #include <linux/clk.h>
0019 #include <linux/err.h>
0020 #include <linux/mfd/abx500.h>
0021 #include <linux/mfd/abx500/ab8500.h>
0022 #include <linux/usb/musb-ux500.h>
0023 #include <linux/regulator/consumer.h>
0024 #include <linux/pinctrl/consumer.h>
0025 
0026 /* Bank AB8500_SYS_CTRL2_BLOCK */
0027 #define AB8500_MAIN_WD_CTRL_REG 0x01
0028 
0029 /* Bank AB8500_USB */
0030 #define AB8500_USB_LINE_STAT_REG 0x80
0031 #define AB8505_USB_LINE_STAT_REG 0x94
0032 #define AB8500_USB_PHY_CTRL_REG 0x8A
0033 
0034 /* Bank AB8500_DEVELOPMENT */
0035 #define AB8500_BANK12_ACCESS 0x00
0036 
0037 /* Bank AB8500_DEBUG */
0038 #define AB8500_USB_PHY_TUNE1 0x05
0039 #define AB8500_USB_PHY_TUNE2 0x06
0040 #define AB8500_USB_PHY_TUNE3 0x07
0041 
0042 /* Bank AB8500_INTERRUPT */
0043 #define AB8500_IT_SOURCE2_REG 0x01
0044 
0045 #define AB8500_BIT_OTG_STAT_ID (1 << 0)
0046 #define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
0047 #define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
0048 #define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
0049 #define AB8500_BIT_WD_CTRL_KICK (1 << 1)
0050 #define AB8500_BIT_SOURCE2_VBUSDET (1 << 7)
0051 
0052 #define AB8500_WD_KICK_DELAY_US 100 /* usec */
0053 #define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
0054 #define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
0055 
0056 /* Usb line status register */
0057 enum ab8500_usb_link_status {
0058     USB_LINK_NOT_CONFIGURED_8500 = 0,
0059     USB_LINK_STD_HOST_NC_8500,
0060     USB_LINK_STD_HOST_C_NS_8500,
0061     USB_LINK_STD_HOST_C_S_8500,
0062     USB_LINK_HOST_CHG_NM_8500,
0063     USB_LINK_HOST_CHG_HS_8500,
0064     USB_LINK_HOST_CHG_HS_CHIRP_8500,
0065     USB_LINK_DEDICATED_CHG_8500,
0066     USB_LINK_ACA_RID_A_8500,
0067     USB_LINK_ACA_RID_B_8500,
0068     USB_LINK_ACA_RID_C_NM_8500,
0069     USB_LINK_ACA_RID_C_HS_8500,
0070     USB_LINK_ACA_RID_C_HS_CHIRP_8500,
0071     USB_LINK_HM_IDGND_8500,
0072     USB_LINK_RESERVED_8500,
0073     USB_LINK_NOT_VALID_LINK_8500,
0074 };
0075 
0076 enum ab8505_usb_link_status {
0077     USB_LINK_NOT_CONFIGURED_8505 = 0,
0078     USB_LINK_STD_HOST_NC_8505,
0079     USB_LINK_STD_HOST_C_NS_8505,
0080     USB_LINK_STD_HOST_C_S_8505,
0081     USB_LINK_CDP_8505,
0082     USB_LINK_RESERVED0_8505,
0083     USB_LINK_RESERVED1_8505,
0084     USB_LINK_DEDICATED_CHG_8505,
0085     USB_LINK_ACA_RID_A_8505,
0086     USB_LINK_ACA_RID_B_8505,
0087     USB_LINK_ACA_RID_C_NM_8505,
0088     USB_LINK_RESERVED2_8505,
0089     USB_LINK_RESERVED3_8505,
0090     USB_LINK_HM_IDGND_8505,
0091     USB_LINK_CHARGERPORT_NOT_OK_8505,
0092     USB_LINK_CHARGER_DM_HIGH_8505,
0093     USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
0094     USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
0095     USB_LINK_STD_UPSTREAM_8505,
0096     USB_LINK_CHARGER_SE1_8505,
0097     USB_LINK_CARKIT_CHGR_1_8505,
0098     USB_LINK_CARKIT_CHGR_2_8505,
0099     USB_LINK_ACA_DOCK_CHGR_8505,
0100     USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
0101     USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
0102     USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
0103     USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
0104     USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
0105 };
0106 
0107 enum ab8500_usb_mode {
0108     USB_IDLE = 0,
0109     USB_PERIPHERAL,
0110     USB_HOST,
0111     USB_DEDICATED_CHG,
0112     USB_UART
0113 };
0114 
0115 /* Register USB_LINK_STATUS interrupt */
0116 #define AB8500_USB_FLAG_USE_LINK_STATUS_IRQ (1 << 0)
0117 /* Register ID_WAKEUP_F interrupt */
0118 #define AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ   (1 << 1)
0119 /* Register VBUS_DET_F interrupt */
0120 #define AB8500_USB_FLAG_USE_VBUS_DET_IRQ    (1 << 2)
0121 /* Driver is using the ab-iddet driver*/
0122 #define AB8500_USB_FLAG_USE_AB_IDDET        (1 << 3)
0123 /* Enable setting regulators voltage */
0124 #define AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE   (1 << 4)
0125 
0126 struct ab8500_usb {
0127     struct usb_phy phy;
0128     struct device *dev;
0129     struct ab8500 *ab8500;
0130     unsigned vbus_draw;
0131     struct work_struct phy_dis_work;
0132     enum ab8500_usb_mode mode;
0133     struct clk *sysclk;
0134     struct regulator *v_ape;
0135     struct regulator *v_musb;
0136     struct regulator *v_ulpi;
0137     int saved_v_ulpi;
0138     int previous_link_status_state;
0139     struct pinctrl *pinctrl;
0140     struct pinctrl_state *pins_sleep;
0141     bool enabled_charging_detection;
0142     unsigned int flags;
0143 };
0144 
0145 static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
0146 {
0147     return container_of(x, struct ab8500_usb, phy);
0148 }
0149 
0150 static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
0151 {
0152     abx500_set_register_interruptible(ab->dev,
0153         AB8500_SYS_CTRL2_BLOCK,
0154         AB8500_MAIN_WD_CTRL_REG,
0155         AB8500_BIT_WD_CTRL_ENABLE);
0156 
0157     udelay(AB8500_WD_KICK_DELAY_US);
0158 
0159     abx500_set_register_interruptible(ab->dev,
0160         AB8500_SYS_CTRL2_BLOCK,
0161         AB8500_MAIN_WD_CTRL_REG,
0162         (AB8500_BIT_WD_CTRL_ENABLE
0163         | AB8500_BIT_WD_CTRL_KICK));
0164 
0165     udelay(AB8500_WD_V11_DISABLE_DELAY_US);
0166 
0167     abx500_set_register_interruptible(ab->dev,
0168         AB8500_SYS_CTRL2_BLOCK,
0169         AB8500_MAIN_WD_CTRL_REG,
0170         0);
0171 }
0172 
0173 static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
0174 {
0175     int ret, volt;
0176 
0177     ret = regulator_enable(ab->v_ape);
0178     if (ret)
0179         dev_err(ab->dev, "Failed to enable v-ape\n");
0180 
0181     if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
0182         ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
0183         if (ab->saved_v_ulpi < 0)
0184             dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
0185 
0186         ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
0187         if (ret < 0)
0188             dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
0189                     ret);
0190 
0191         ret = regulator_set_load(ab->v_ulpi, 28000);
0192         if (ret < 0)
0193             dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
0194                     ret);
0195     }
0196 
0197     ret = regulator_enable(ab->v_ulpi);
0198     if (ret)
0199         dev_err(ab->dev, "Failed to enable vddulpivio18\n");
0200 
0201     if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
0202         volt = regulator_get_voltage(ab->v_ulpi);
0203         if ((volt != 1300000) && (volt != 1350000))
0204             dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
0205                     volt);
0206     }
0207 
0208     ret = regulator_enable(ab->v_musb);
0209     if (ret)
0210         dev_err(ab->dev, "Failed to enable musb_1v8\n");
0211 }
0212 
0213 static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
0214 {
0215     int ret;
0216 
0217     regulator_disable(ab->v_musb);
0218 
0219     regulator_disable(ab->v_ulpi);
0220 
0221     /* USB is not the only consumer of Vintcore, restore old settings */
0222     if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
0223         if (ab->saved_v_ulpi > 0) {
0224             ret = regulator_set_voltage(ab->v_ulpi,
0225                     ab->saved_v_ulpi, ab->saved_v_ulpi);
0226             if (ret < 0)
0227                 dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
0228                         ab->saved_v_ulpi, ret);
0229         }
0230 
0231         ret = regulator_set_load(ab->v_ulpi, 0);
0232         if (ret < 0)
0233             dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
0234                     ret);
0235     }
0236 
0237     regulator_disable(ab->v_ape);
0238 }
0239 
0240 static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
0241 {
0242     /* Workaround for v2.0 bug # 31952 */
0243     if (is_ab8500_2p0(ab->ab8500)) {
0244         abx500_mask_and_set_register_interruptible(ab->dev,
0245                 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
0246                 bit, bit);
0247         udelay(AB8500_V20_31952_DISABLE_DELAY_US);
0248     }
0249 }
0250 
0251 static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
0252 {
0253     u8 bit;
0254     bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
0255         AB8500_BIT_PHY_CTRL_DEVICE_EN;
0256 
0257     /* mux and configure USB pins to DEFAULT state */
0258     ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
0259     if (IS_ERR(ab->pinctrl))
0260         dev_err(ab->dev, "could not get/set default pinstate\n");
0261 
0262     if (clk_prepare_enable(ab->sysclk))
0263         dev_err(ab->dev, "can't prepare/enable clock\n");
0264 
0265     ab8500_usb_regulator_enable(ab);
0266 
0267     abx500_mask_and_set_register_interruptible(ab->dev,
0268             AB8500_USB, AB8500_USB_PHY_CTRL_REG,
0269             bit, bit);
0270 }
0271 
0272 static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
0273 {
0274     u8 bit;
0275     bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
0276         AB8500_BIT_PHY_CTRL_DEVICE_EN;
0277 
0278     ab8500_usb_wd_linkstatus(ab, bit);
0279 
0280     abx500_mask_and_set_register_interruptible(ab->dev,
0281             AB8500_USB, AB8500_USB_PHY_CTRL_REG,
0282             bit, 0);
0283 
0284     /* Needed to disable the phy.*/
0285     ab8500_usb_wd_workaround(ab);
0286 
0287     clk_disable_unprepare(ab->sysclk);
0288 
0289     ab8500_usb_regulator_disable(ab);
0290 
0291     if (!IS_ERR(ab->pinctrl)) {
0292         /* configure USB pins to SLEEP state */
0293         ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
0294                 PINCTRL_STATE_SLEEP);
0295 
0296         if (IS_ERR(ab->pins_sleep))
0297             dev_dbg(ab->dev, "could not get sleep pinstate\n");
0298         else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
0299             dev_err(ab->dev, "could not set pins to sleep state\n");
0300 
0301         /*
0302          * as USB pins are shared with iddet, release them to allow
0303          * iddet to request them
0304          */
0305         pinctrl_put(ab->pinctrl);
0306     }
0307 }
0308 
0309 #define ab8500_usb_host_phy_en(ab)  ab8500_usb_phy_enable(ab, true)
0310 #define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
0311 #define ab8500_usb_peri_phy_en(ab)  ab8500_usb_phy_enable(ab, false)
0312 #define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
0313 
0314 static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
0315         enum ab8505_usb_link_status lsts)
0316 {
0317     enum ux500_musb_vbus_id_status event = 0;
0318 
0319     dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
0320 
0321     /*
0322      * Spurious link_status interrupts are seen at the time of
0323      * disconnection of a device in RIDA state
0324      */
0325     if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
0326             (lsts == USB_LINK_STD_HOST_NC_8505))
0327         return 0;
0328 
0329     ab->previous_link_status_state = lsts;
0330 
0331     switch (lsts) {
0332     case USB_LINK_ACA_RID_B_8505:
0333         event = UX500_MUSB_RIDB;
0334         fallthrough;
0335     case USB_LINK_NOT_CONFIGURED_8505:
0336     case USB_LINK_RESERVED0_8505:
0337     case USB_LINK_RESERVED1_8505:
0338     case USB_LINK_RESERVED2_8505:
0339     case USB_LINK_RESERVED3_8505:
0340         ab->mode = USB_IDLE;
0341         ab->phy.otg->default_a = false;
0342         ab->vbus_draw = 0;
0343         if (event != UX500_MUSB_RIDB)
0344             event = UX500_MUSB_NONE;
0345         /*
0346          * Fallback to default B_IDLE as nothing
0347          * is connected
0348          */
0349         ab->phy.otg->state = OTG_STATE_B_IDLE;
0350         usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
0351         break;
0352 
0353     case USB_LINK_ACA_RID_C_NM_8505:
0354         event = UX500_MUSB_RIDC;
0355         fallthrough;
0356     case USB_LINK_STD_HOST_NC_8505:
0357     case USB_LINK_STD_HOST_C_NS_8505:
0358     case USB_LINK_STD_HOST_C_S_8505:
0359     case USB_LINK_CDP_8505:
0360         if (ab->mode == USB_IDLE) {
0361             ab->mode = USB_PERIPHERAL;
0362             ab8500_usb_peri_phy_en(ab);
0363             atomic_notifier_call_chain(&ab->phy.notifier,
0364                     UX500_MUSB_PREPARE, &ab->vbus_draw);
0365             usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
0366         }
0367         if (event != UX500_MUSB_RIDC)
0368             event = UX500_MUSB_VBUS;
0369         break;
0370 
0371     case USB_LINK_ACA_RID_A_8505:
0372     case USB_LINK_ACA_DOCK_CHGR_8505:
0373         event = UX500_MUSB_RIDA;
0374         fallthrough;
0375     case USB_LINK_HM_IDGND_8505:
0376         if (ab->mode == USB_IDLE) {
0377             ab->mode = USB_HOST;
0378             ab8500_usb_host_phy_en(ab);
0379             atomic_notifier_call_chain(&ab->phy.notifier,
0380                     UX500_MUSB_PREPARE, &ab->vbus_draw);
0381         }
0382         ab->phy.otg->default_a = true;
0383         if (event != UX500_MUSB_RIDA)
0384             event = UX500_MUSB_ID;
0385         atomic_notifier_call_chain(&ab->phy.notifier,
0386                 event, &ab->vbus_draw);
0387         break;
0388 
0389     case USB_LINK_DEDICATED_CHG_8505:
0390         ab->mode = USB_DEDICATED_CHG;
0391         event = UX500_MUSB_CHARGER;
0392         atomic_notifier_call_chain(&ab->phy.notifier,
0393                 event, &ab->vbus_draw);
0394         usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
0395         break;
0396 
0397     /*
0398      * FIXME: For now we rely on the boot firmware to set up the necessary
0399      * PHY/pin configuration for UART mode.
0400      *
0401      * AB8505 does not seem to report any status change for UART cables,
0402      * possibly because it cannot detect them autonomously.
0403      * We may need to measure the ID resistance manually to reliably
0404      * detect UART cables after bootup.
0405      */
0406     case USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505:
0407     case USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505:
0408         if (ab->mode == USB_IDLE) {
0409             ab->mode = USB_UART;
0410             ab8500_usb_peri_phy_en(ab);
0411         }
0412 
0413         break;
0414 
0415     default:
0416         break;
0417     }
0418 
0419     return 0;
0420 }
0421 
0422 static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
0423         enum ab8500_usb_link_status lsts)
0424 {
0425     enum ux500_musb_vbus_id_status event = 0;
0426 
0427     dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
0428 
0429     /*
0430      * Spurious link_status interrupts are seen in case of a
0431      * disconnection of a device in IDGND and RIDA stage
0432      */
0433     if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
0434             (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
0435              lsts == USB_LINK_STD_HOST_NC_8500))
0436         return 0;
0437 
0438     if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
0439             lsts == USB_LINK_STD_HOST_NC_8500)
0440         return 0;
0441 
0442     ab->previous_link_status_state = lsts;
0443 
0444     switch (lsts) {
0445     case USB_LINK_ACA_RID_B_8500:
0446         event = UX500_MUSB_RIDB;
0447         fallthrough;
0448     case USB_LINK_NOT_CONFIGURED_8500:
0449     case USB_LINK_NOT_VALID_LINK_8500:
0450         ab->mode = USB_IDLE;
0451         ab->phy.otg->default_a = false;
0452         ab->vbus_draw = 0;
0453         if (event != UX500_MUSB_RIDB)
0454             event = UX500_MUSB_NONE;
0455         /* Fallback to default B_IDLE as nothing is connected */
0456         ab->phy.otg->state = OTG_STATE_B_IDLE;
0457         usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
0458         break;
0459 
0460     case USB_LINK_ACA_RID_C_NM_8500:
0461     case USB_LINK_ACA_RID_C_HS_8500:
0462     case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
0463         event = UX500_MUSB_RIDC;
0464         fallthrough;
0465     case USB_LINK_STD_HOST_NC_8500:
0466     case USB_LINK_STD_HOST_C_NS_8500:
0467     case USB_LINK_STD_HOST_C_S_8500:
0468     case USB_LINK_HOST_CHG_NM_8500:
0469     case USB_LINK_HOST_CHG_HS_8500:
0470     case USB_LINK_HOST_CHG_HS_CHIRP_8500:
0471         if (ab->mode == USB_IDLE) {
0472             ab->mode = USB_PERIPHERAL;
0473             ab8500_usb_peri_phy_en(ab);
0474             atomic_notifier_call_chain(&ab->phy.notifier,
0475                     UX500_MUSB_PREPARE, &ab->vbus_draw);
0476             usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
0477         }
0478         if (event != UX500_MUSB_RIDC)
0479             event = UX500_MUSB_VBUS;
0480         break;
0481 
0482     case USB_LINK_ACA_RID_A_8500:
0483         event = UX500_MUSB_RIDA;
0484         fallthrough;
0485     case USB_LINK_HM_IDGND_8500:
0486         if (ab->mode == USB_IDLE) {
0487             ab->mode = USB_HOST;
0488             ab8500_usb_host_phy_en(ab);
0489             atomic_notifier_call_chain(&ab->phy.notifier,
0490                     UX500_MUSB_PREPARE, &ab->vbus_draw);
0491         }
0492         ab->phy.otg->default_a = true;
0493         if (event != UX500_MUSB_RIDA)
0494             event = UX500_MUSB_ID;
0495         atomic_notifier_call_chain(&ab->phy.notifier,
0496                 event, &ab->vbus_draw);
0497         break;
0498 
0499     case USB_LINK_DEDICATED_CHG_8500:
0500         ab->mode = USB_DEDICATED_CHG;
0501         event = UX500_MUSB_CHARGER;
0502         atomic_notifier_call_chain(&ab->phy.notifier,
0503                 event, &ab->vbus_draw);
0504         usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
0505         break;
0506 
0507     case USB_LINK_RESERVED_8500:
0508         break;
0509     }
0510 
0511     return 0;
0512 }
0513 
0514 /*
0515  * Connection Sequence:
0516  *   1. Link Status Interrupt
0517  *   2. Enable AB clock
0518  *   3. Enable AB regulators
0519  *   4. Enable USB phy
0520  *   5. Reset the musb controller
0521  *   6. Switch the ULPI GPIO pins to function mode
0522  *   7. Enable the musb Peripheral5 clock
0523  *   8. Restore MUSB context
0524  */
0525 static int abx500_usb_link_status_update(struct ab8500_usb *ab)
0526 {
0527     u8 reg;
0528     int ret = 0;
0529 
0530     if (is_ab8500(ab->ab8500)) {
0531         enum ab8500_usb_link_status lsts;
0532 
0533         ret = abx500_get_register_interruptible(ab->dev,
0534                 AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
0535         if (ret < 0)
0536             return ret;
0537         lsts = (reg >> 3) & 0x0F;
0538         ret = ab8500_usb_link_status_update(ab, lsts);
0539     } else if (is_ab8505(ab->ab8500)) {
0540         enum ab8505_usb_link_status lsts;
0541 
0542         ret = abx500_get_register_interruptible(ab->dev,
0543                 AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
0544         if (ret < 0)
0545             return ret;
0546         lsts = (reg >> 3) & 0x1F;
0547         ret = ab8505_usb_link_status_update(ab, lsts);
0548     }
0549 
0550     return ret;
0551 }
0552 
0553 /*
0554  * Disconnection Sequence:
0555  *   1. Disconnect Interrupt
0556  *   2. Disable regulators
0557  *   3. Disable AB clock
0558  *   4. Disable the Phy
0559  *   5. Link Status Interrupt
0560  *   6. Disable Musb Clock
0561  */
0562 static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
0563 {
0564     struct ab8500_usb *ab = (struct ab8500_usb *) data;
0565     enum usb_phy_events event = USB_EVENT_NONE;
0566 
0567     /* Link status will not be updated till phy is disabled. */
0568     if (ab->mode == USB_HOST) {
0569         ab->phy.otg->default_a = false;
0570         ab->vbus_draw = 0;
0571         atomic_notifier_call_chain(&ab->phy.notifier,
0572                 event, &ab->vbus_draw);
0573         ab8500_usb_host_phy_dis(ab);
0574         ab->mode = USB_IDLE;
0575     }
0576 
0577     if (ab->mode == USB_PERIPHERAL) {
0578         atomic_notifier_call_chain(&ab->phy.notifier,
0579                 event, &ab->vbus_draw);
0580         ab8500_usb_peri_phy_dis(ab);
0581         atomic_notifier_call_chain(&ab->phy.notifier,
0582                 UX500_MUSB_CLEAN, &ab->vbus_draw);
0583         ab->mode = USB_IDLE;
0584         ab->phy.otg->default_a = false;
0585         ab->vbus_draw = 0;
0586     }
0587 
0588     if (ab->mode == USB_UART) {
0589         ab8500_usb_peri_phy_dis(ab);
0590         ab->mode = USB_IDLE;
0591     }
0592 
0593     if (is_ab8500_2p0(ab->ab8500)) {
0594         if (ab->mode == USB_DEDICATED_CHG) {
0595             ab8500_usb_wd_linkstatus(ab,
0596                     AB8500_BIT_PHY_CTRL_DEVICE_EN);
0597             abx500_mask_and_set_register_interruptible(ab->dev,
0598                     AB8500_USB, AB8500_USB_PHY_CTRL_REG,
0599                     AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
0600         }
0601     }
0602 
0603     return IRQ_HANDLED;
0604 }
0605 
0606 static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
0607 {
0608     struct ab8500_usb *ab = (struct ab8500_usb *)data;
0609 
0610     abx500_usb_link_status_update(ab);
0611 
0612     return IRQ_HANDLED;
0613 }
0614 
0615 static void ab8500_usb_phy_disable_work(struct work_struct *work)
0616 {
0617     struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
0618                         phy_dis_work);
0619 
0620     if (!ab->phy.otg->host)
0621         ab8500_usb_host_phy_dis(ab);
0622 
0623     if (!ab->phy.otg->gadget)
0624         ab8500_usb_peri_phy_dis(ab);
0625 }
0626 
0627 static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
0628 {
0629     /* TODO */
0630     return 0;
0631 }
0632 
0633 static int ab8500_usb_set_peripheral(struct usb_otg *otg,
0634                     struct usb_gadget *gadget)
0635 {
0636     struct ab8500_usb *ab;
0637 
0638     if (!otg)
0639         return -ENODEV;
0640 
0641     ab = phy_to_ab(otg->usb_phy);
0642 
0643     ab->phy.otg->gadget = gadget;
0644 
0645     /* Some drivers call this function in atomic context.
0646      * Do not update ab8500 registers directly till this
0647      * is fixed.
0648      */
0649 
0650     if ((ab->mode != USB_IDLE) && !gadget) {
0651         ab->mode = USB_IDLE;
0652         schedule_work(&ab->phy_dis_work);
0653     }
0654 
0655     return 0;
0656 }
0657 
0658 static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
0659 {
0660     struct ab8500_usb *ab;
0661 
0662     if (!otg)
0663         return -ENODEV;
0664 
0665     ab = phy_to_ab(otg->usb_phy);
0666 
0667     ab->phy.otg->host = host;
0668 
0669     /* Some drivers call this function in atomic context.
0670      * Do not update ab8500 registers directly till this
0671      * is fixed.
0672      */
0673 
0674     if ((ab->mode != USB_IDLE) && !host) {
0675         ab->mode = USB_IDLE;
0676         schedule_work(&ab->phy_dis_work);
0677     }
0678 
0679     return 0;
0680 }
0681 
0682 static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
0683 {
0684     abx500_mask_and_set_register_interruptible(ab->dev,
0685             AB8500_USB, AB8500_USB_PHY_CTRL_REG,
0686             AB8500_BIT_PHY_CTRL_DEVICE_EN,
0687             AB8500_BIT_PHY_CTRL_DEVICE_EN);
0688 
0689     udelay(100);
0690 
0691     abx500_mask_and_set_register_interruptible(ab->dev,
0692             AB8500_USB, AB8500_USB_PHY_CTRL_REG,
0693             AB8500_BIT_PHY_CTRL_DEVICE_EN,
0694             0);
0695 
0696     abx500_mask_and_set_register_interruptible(ab->dev,
0697             AB8500_USB, AB8500_USB_PHY_CTRL_REG,
0698             AB8500_BIT_PHY_CTRL_HOST_EN,
0699             AB8500_BIT_PHY_CTRL_HOST_EN);
0700 
0701     udelay(100);
0702 
0703     abx500_mask_and_set_register_interruptible(ab->dev,
0704             AB8500_USB, AB8500_USB_PHY_CTRL_REG,
0705             AB8500_BIT_PHY_CTRL_HOST_EN,
0706             0);
0707 }
0708 
0709 static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
0710 {
0711     int err;
0712 
0713     ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
0714     if (IS_ERR(ab->v_ape)) {
0715         dev_err(ab->dev, "Could not get v-ape supply\n");
0716         err = PTR_ERR(ab->v_ape);
0717         return err;
0718     }
0719 
0720     ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
0721     if (IS_ERR(ab->v_ulpi)) {
0722         dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
0723         err = PTR_ERR(ab->v_ulpi);
0724         return err;
0725     }
0726 
0727     ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
0728     if (IS_ERR(ab->v_musb)) {
0729         dev_err(ab->dev, "Could not get musb_1v8 supply\n");
0730         err = PTR_ERR(ab->v_musb);
0731         return err;
0732     }
0733 
0734     return 0;
0735 }
0736 
0737 static int ab8500_usb_irq_setup(struct platform_device *pdev,
0738         struct ab8500_usb *ab)
0739 {
0740     int err;
0741     int irq;
0742 
0743     if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
0744         irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
0745         if (irq < 0)
0746             return irq;
0747         err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
0748                 ab8500_usb_link_status_irq,
0749                 IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
0750                 "usb-link-status", ab);
0751         if (err < 0) {
0752             dev_err(ab->dev, "request_irq failed for link status irq\n");
0753             return err;
0754         }
0755     }
0756 
0757     if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
0758         irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
0759         if (irq < 0)
0760             return irq;
0761         err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
0762                 ab8500_usb_disconnect_irq,
0763                 IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
0764                 "usb-id-fall", ab);
0765         if (err < 0) {
0766             dev_err(ab->dev, "request_irq failed for ID fall irq\n");
0767             return err;
0768         }
0769     }
0770 
0771     if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
0772         irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
0773         if (irq < 0)
0774             return irq;
0775         err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
0776                 ab8500_usb_disconnect_irq,
0777                 IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
0778                 "usb-vbus-fall", ab);
0779         if (err < 0) {
0780             dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
0781             return err;
0782         }
0783     }
0784 
0785     return 0;
0786 }
0787 
0788 static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
0789 {
0790     int err;
0791 
0792     /* Enable the PBT/Bank 0x12 access */
0793     err = abx500_set_register_interruptible(ab->dev,
0794             AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
0795     if (err < 0)
0796         dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
0797                 err);
0798 
0799     err = abx500_set_register_interruptible(ab->dev,
0800             AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
0801     if (err < 0)
0802         dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
0803                 err);
0804 
0805     err = abx500_set_register_interruptible(ab->dev,
0806             AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
0807     if (err < 0)
0808         dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
0809                 err);
0810 
0811     err = abx500_set_register_interruptible(ab->dev,
0812             AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
0813     if (err < 0)
0814         dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
0815                 err);
0816 
0817     /* Switch to normal mode/disable Bank 0x12 access */
0818     err = abx500_set_register_interruptible(ab->dev,
0819             AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
0820     if (err < 0)
0821         dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
0822                 err);
0823 }
0824 
0825 static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
0826 {
0827     int err;
0828 
0829     /* Enable the PBT/Bank 0x12 access */
0830     err = abx500_mask_and_set_register_interruptible(ab->dev,
0831             AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
0832             0x01, 0x01);
0833     if (err < 0)
0834         dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
0835                 err);
0836 
0837     err = abx500_mask_and_set_register_interruptible(ab->dev,
0838             AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
0839             0xC8, 0xC8);
0840     if (err < 0)
0841         dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
0842                 err);
0843 
0844     err = abx500_mask_and_set_register_interruptible(ab->dev,
0845             AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
0846             0x60, 0x60);
0847     if (err < 0)
0848         dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
0849                 err);
0850 
0851     err = abx500_mask_and_set_register_interruptible(ab->dev,
0852             AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
0853             0xFC, 0x80);
0854 
0855     if (err < 0)
0856         dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
0857                 err);
0858 
0859     /* Switch to normal mode/disable Bank 0x12 access */
0860     err = abx500_mask_and_set_register_interruptible(ab->dev,
0861             AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
0862             0x00, 0x00);
0863     if (err < 0)
0864         dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
0865                 err);
0866 }
0867 
0868 static int ab8500_usb_probe(struct platform_device *pdev)
0869 {
0870     struct ab8500_usb   *ab;
0871     struct ab8500       *ab8500;
0872     struct usb_otg      *otg;
0873     int err;
0874     int rev;
0875 
0876     ab8500 = dev_get_drvdata(pdev->dev.parent);
0877     rev = abx500_get_chip_id(&pdev->dev);
0878 
0879     if (is_ab8500_1p1_or_earlier(ab8500)) {
0880         dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
0881         return -ENODEV;
0882     }
0883 
0884     ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
0885     if (!ab)
0886         return -ENOMEM;
0887 
0888     otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
0889     if (!otg)
0890         return -ENOMEM;
0891 
0892     ab->dev         = &pdev->dev;
0893     ab->ab8500      = ab8500;
0894     ab->phy.dev     = ab->dev;
0895     ab->phy.otg     = otg;
0896     ab->phy.label       = "ab8500";
0897     ab->phy.set_suspend = ab8500_usb_set_suspend;
0898     ab->phy.otg->state  = OTG_STATE_UNDEFINED;
0899 
0900     otg->usb_phy        = &ab->phy;
0901     otg->set_host       = ab8500_usb_set_host;
0902     otg->set_peripheral = ab8500_usb_set_peripheral;
0903 
0904     if (is_ab8500(ab->ab8500)) {
0905         ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
0906             AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
0907             AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
0908             AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
0909     } else if (is_ab8505(ab->ab8500)) {
0910         ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
0911             AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
0912             AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
0913             AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
0914     }
0915 
0916     /* Disable regulator voltage setting for AB8500 <= v2.0 */
0917     if (is_ab8500_2p0_or_earlier(ab->ab8500))
0918         ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
0919 
0920     platform_set_drvdata(pdev, ab);
0921 
0922     /* all: Disable phy when called from set_host and set_peripheral */
0923     INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
0924 
0925     err = ab8500_usb_regulator_get(ab);
0926     if (err)
0927         return err;
0928 
0929     ab->sysclk = devm_clk_get(ab->dev, "sysclk");
0930     if (IS_ERR(ab->sysclk)) {
0931         dev_err(ab->dev, "Could not get sysclk.\n");
0932         return PTR_ERR(ab->sysclk);
0933     }
0934 
0935     err = ab8500_usb_irq_setup(pdev, ab);
0936     if (err < 0)
0937         return err;
0938 
0939     err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
0940     if (err) {
0941         dev_err(&pdev->dev, "Can't register transceiver\n");
0942         return err;
0943     }
0944 
0945     if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
0946         /* Phy tuning values for AB8500 > v2.0 */
0947         ab8500_usb_set_ab8500_tuning_values(ab);
0948     else if (is_ab8505(ab->ab8500))
0949         /* Phy tuning values for AB8505 */
0950         ab8500_usb_set_ab8505_tuning_values(ab);
0951 
0952     /* Needed to enable ID detection. */
0953     ab8500_usb_wd_workaround(ab);
0954 
0955     /*
0956      * This is required for usb-link-status to work properly when a
0957      * cable is connected at boot time.
0958      */
0959     ab8500_usb_restart_phy(ab);
0960 
0961     abx500_usb_link_status_update(ab);
0962 
0963     dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
0964 
0965     return 0;
0966 }
0967 
0968 static int ab8500_usb_remove(struct platform_device *pdev)
0969 {
0970     struct ab8500_usb *ab = platform_get_drvdata(pdev);
0971 
0972     cancel_work_sync(&ab->phy_dis_work);
0973 
0974     usb_remove_phy(&ab->phy);
0975 
0976     if (ab->mode == USB_HOST)
0977         ab8500_usb_host_phy_dis(ab);
0978     else if (ab->mode == USB_PERIPHERAL)
0979         ab8500_usb_peri_phy_dis(ab);
0980 
0981     return 0;
0982 }
0983 
0984 static const struct platform_device_id ab8500_usb_devtype[] = {
0985     { .name = "ab8500-usb", },
0986     { /* sentinel */ }
0987 };
0988 MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
0989 
0990 static struct platform_driver ab8500_usb_driver = {
0991     .probe      = ab8500_usb_probe,
0992     .remove     = ab8500_usb_remove,
0993     .id_table   = ab8500_usb_devtype,
0994     .driver     = {
0995         .name   = "abx5x0-usb",
0996     },
0997 };
0998 
0999 static int __init ab8500_usb_init(void)
1000 {
1001     return platform_driver_register(&ab8500_usb_driver);
1002 }
1003 subsys_initcall(ab8500_usb_init);
1004 
1005 static void __exit ab8500_usb_exit(void)
1006 {
1007     platform_driver_unregister(&ab8500_usb_driver);
1008 }
1009 module_exit(ab8500_usb_exit);
1010 
1011 MODULE_AUTHOR("ST-Ericsson AB");
1012 MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
1013 MODULE_LICENSE("GPL");