Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
0004  */
0005 
0006 #include <linux/bitops.h>
0007 #include <linux/err.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/io.h>
0010 #include <linux/iopoll.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/of.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/slab.h>
0016 #include <linux/sysfs.h>
0017 #include <linux/usb/role.h>
0018 
0019 #define EUD_REG_INT1_EN_MASK    0x0024
0020 #define EUD_REG_INT_STATUS_1    0x0044
0021 #define EUD_REG_CTL_OUT_1   0x0074
0022 #define EUD_REG_VBUS_INT_CLR    0x0080
0023 #define EUD_REG_CSR_EUD_EN  0x1014
0024 #define EUD_REG_SW_ATTACH_DET   0x1018
0025 #define EUD_REG_EUD_EN2        0x0000
0026 
0027 #define EUD_ENABLE      BIT(0)
0028 #define EUD_INT_PET_EUD BIT(0)
0029 #define EUD_INT_VBUS        BIT(2)
0030 #define EUD_INT_SAFE_MODE   BIT(4)
0031 #define EUD_INT_ALL     (EUD_INT_VBUS | EUD_INT_SAFE_MODE)
0032 
0033 struct eud_chip {
0034     struct device           *dev;
0035     struct usb_role_switch      *role_sw;
0036     void __iomem            *base;
0037     void __iomem            *mode_mgr;
0038     unsigned int            int_status;
0039     int             irq;
0040     bool                enabled;
0041     bool                usb_attached;
0042 };
0043 
0044 static int enable_eud(struct eud_chip *priv)
0045 {
0046     writel(EUD_ENABLE, priv->base + EUD_REG_CSR_EUD_EN);
0047     writel(EUD_INT_VBUS | EUD_INT_SAFE_MODE,
0048             priv->base + EUD_REG_INT1_EN_MASK);
0049     writel(1, priv->mode_mgr + EUD_REG_EUD_EN2);
0050 
0051     return usb_role_switch_set_role(priv->role_sw, USB_ROLE_DEVICE);
0052 }
0053 
0054 static void disable_eud(struct eud_chip *priv)
0055 {
0056     writel(0, priv->base + EUD_REG_CSR_EUD_EN);
0057     writel(0, priv->mode_mgr + EUD_REG_EUD_EN2);
0058 }
0059 
0060 static ssize_t enable_show(struct device *dev,
0061         struct device_attribute *attr, char *buf)
0062 {
0063     struct eud_chip *chip = dev_get_drvdata(dev);
0064 
0065     return sysfs_emit(buf, "%d\n", chip->enabled);
0066 }
0067 
0068 static ssize_t enable_store(struct device *dev,
0069         struct device_attribute *attr,
0070         const char *buf, size_t count)
0071 {
0072     struct eud_chip *chip = dev_get_drvdata(dev);
0073     bool enable;
0074     int ret;
0075 
0076     if (kstrtobool(buf, &enable))
0077         return -EINVAL;
0078 
0079     if (enable) {
0080         ret = enable_eud(chip);
0081         if (!ret)
0082             chip->enabled = enable;
0083         else
0084             disable_eud(chip);
0085     } else {
0086         disable_eud(chip);
0087     }
0088 
0089     return count;
0090 }
0091 
0092 static DEVICE_ATTR_RW(enable);
0093 
0094 static struct attribute *eud_attrs[] = {
0095     &dev_attr_enable.attr,
0096     NULL,
0097 };
0098 ATTRIBUTE_GROUPS(eud);
0099 
0100 static void usb_attach_detach(struct eud_chip *chip)
0101 {
0102     u32 reg;
0103 
0104     /* read ctl_out_1[4] to find USB attach or detach event */
0105     reg = readl(chip->base + EUD_REG_CTL_OUT_1);
0106     chip->usb_attached = reg & EUD_INT_SAFE_MODE;
0107 }
0108 
0109 static void pet_eud(struct eud_chip *chip)
0110 {
0111     u32 reg;
0112     int ret;
0113 
0114     /* When the EUD_INT_PET_EUD in SW_ATTACH_DET is set, the cable has been
0115      * disconnected and we need to detach the pet to check if EUD is in safe
0116      * mode before attaching again.
0117      */
0118     reg = readl(chip->base + EUD_REG_SW_ATTACH_DET);
0119     if (reg & EUD_INT_PET_EUD) {
0120         /* Detach & Attach pet for EUD */
0121         writel(0, chip->base + EUD_REG_SW_ATTACH_DET);
0122         /* Delay to make sure detach pet is done before attach pet */
0123         ret = readl_poll_timeout(chip->base + EUD_REG_SW_ATTACH_DET,
0124                     reg, (reg == 0), 1, 100);
0125         if (ret) {
0126             dev_err(chip->dev, "Detach pet failed\n");
0127             return;
0128         }
0129     }
0130     /* Attach pet for EUD */
0131     writel(EUD_INT_PET_EUD, chip->base + EUD_REG_SW_ATTACH_DET);
0132 }
0133 
0134 static irqreturn_t handle_eud_irq(int irq, void *data)
0135 {
0136     struct eud_chip *chip = data;
0137     u32 reg;
0138 
0139     reg = readl(chip->base + EUD_REG_INT_STATUS_1);
0140     switch (reg & EUD_INT_ALL) {
0141     case EUD_INT_VBUS:
0142         usb_attach_detach(chip);
0143         return IRQ_WAKE_THREAD;
0144     case EUD_INT_SAFE_MODE:
0145         pet_eud(chip);
0146         return IRQ_HANDLED;
0147     default:
0148         return IRQ_NONE;
0149     }
0150 }
0151 
0152 static irqreturn_t handle_eud_irq_thread(int irq, void *data)
0153 {
0154     struct eud_chip *chip = data;
0155     int ret;
0156 
0157     if (chip->usb_attached)
0158         ret = usb_role_switch_set_role(chip->role_sw, USB_ROLE_DEVICE);
0159     else
0160         ret = usb_role_switch_set_role(chip->role_sw, USB_ROLE_HOST);
0161     if (ret)
0162         dev_err(chip->dev, "failed to set role switch\n");
0163 
0164     /* set and clear vbus_int_clr[0] to clear interrupt */
0165     writel(BIT(0), chip->base + EUD_REG_VBUS_INT_CLR);
0166     writel(0, chip->base + EUD_REG_VBUS_INT_CLR);
0167 
0168     return IRQ_HANDLED;
0169 }
0170 
0171 static void eud_role_switch_release(void *data)
0172 {
0173     struct eud_chip *chip = data;
0174 
0175     usb_role_switch_put(chip->role_sw);
0176 }
0177 
0178 static int eud_probe(struct platform_device *pdev)
0179 {
0180     struct eud_chip *chip;
0181     int ret;
0182 
0183     chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
0184     if (!chip)
0185         return -ENOMEM;
0186 
0187     chip->dev = &pdev->dev;
0188 
0189     chip->role_sw = usb_role_switch_get(&pdev->dev);
0190     if (IS_ERR(chip->role_sw))
0191         return dev_err_probe(chip->dev, PTR_ERR(chip->role_sw),
0192                     "failed to get role switch\n");
0193 
0194     ret = devm_add_action_or_reset(chip->dev, eud_role_switch_release, chip);
0195     if (ret)
0196         return dev_err_probe(chip->dev, ret,
0197                 "failed to add role switch release action\n");
0198 
0199     chip->base = devm_platform_ioremap_resource(pdev, 0);
0200     if (IS_ERR(chip->base))
0201         return PTR_ERR(chip->base);
0202 
0203     chip->mode_mgr = devm_platform_ioremap_resource(pdev, 1);
0204     if (IS_ERR(chip->mode_mgr))
0205         return PTR_ERR(chip->mode_mgr);
0206 
0207     chip->irq = platform_get_irq(pdev, 0);
0208     ret = devm_request_threaded_irq(&pdev->dev, chip->irq, handle_eud_irq,
0209             handle_eud_irq_thread, IRQF_ONESHOT, NULL, chip);
0210     if (ret)
0211         return dev_err_probe(chip->dev, ret, "failed to allocate irq\n");
0212 
0213     enable_irq_wake(chip->irq);
0214 
0215     platform_set_drvdata(pdev, chip);
0216 
0217     return 0;
0218 }
0219 
0220 static int eud_remove(struct platform_device *pdev)
0221 {
0222     struct eud_chip *chip = platform_get_drvdata(pdev);
0223 
0224     if (chip->enabled)
0225         disable_eud(chip);
0226 
0227     device_init_wakeup(&pdev->dev, false);
0228     disable_irq_wake(chip->irq);
0229 
0230     return 0;
0231 }
0232 
0233 static const struct of_device_id eud_dt_match[] = {
0234     { .compatible = "qcom,sc7280-eud" },
0235     { }
0236 };
0237 MODULE_DEVICE_TABLE(of, eud_dt_match);
0238 
0239 static struct platform_driver eud_driver = {
0240     .probe  = eud_probe,
0241     .remove = eud_remove,
0242     .driver = {
0243         .name = "qcom_eud",
0244         .dev_groups = eud_groups,
0245         .of_match_table = eud_dt_match,
0246     },
0247 };
0248 module_platform_driver(eud_driver);
0249 
0250 MODULE_DESCRIPTION("QTI EUD driver");
0251 MODULE_LICENSE("GPL v2");