Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * extcon-fsa9480.c - Fairchild Semiconductor FSA9480 extcon driver
0004  *
0005  * Copyright (c) 2019 Tomasz Figa <tomasz.figa@gmail.com>
0006  *
0007  * Loosely based on old fsa9480 misc-device driver.
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/types.h>
0013 #include <linux/i2c.h>
0014 #include <linux/slab.h>
0015 #include <linux/bitops.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/err.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/kobject.h>
0020 #include <linux/extcon-provider.h>
0021 #include <linux/irqdomain.h>
0022 #include <linux/regmap.h>
0023 
0024 /* FSA9480 I2C registers */
0025 #define FSA9480_REG_DEVID               0x01
0026 #define FSA9480_REG_CTRL                0x02
0027 #define FSA9480_REG_INT1                0x03
0028 #define FSA9480_REG_INT2                0x04
0029 #define FSA9480_REG_INT1_MASK           0x05
0030 #define FSA9480_REG_INT2_MASK           0x06
0031 #define FSA9480_REG_ADC                 0x07
0032 #define FSA9480_REG_TIMING1             0x08
0033 #define FSA9480_REG_TIMING2             0x09
0034 #define FSA9480_REG_DEV_T1              0x0a
0035 #define FSA9480_REG_DEV_T2              0x0b
0036 #define FSA9480_REG_BTN1                0x0c
0037 #define FSA9480_REG_BTN2                0x0d
0038 #define FSA9480_REG_CK                  0x0e
0039 #define FSA9480_REG_CK_INT1             0x0f
0040 #define FSA9480_REG_CK_INT2             0x10
0041 #define FSA9480_REG_CK_INTMASK1         0x11
0042 #define FSA9480_REG_CK_INTMASK2         0x12
0043 #define FSA9480_REG_MANSW1              0x13
0044 #define FSA9480_REG_MANSW2              0x14
0045 #define FSA9480_REG_END                 0x15
0046 
0047 /* Control */
0048 #define CON_SWITCH_OPEN         (1 << 4)
0049 #define CON_RAW_DATA            (1 << 3)
0050 #define CON_MANUAL_SW           (1 << 2)
0051 #define CON_WAIT                (1 << 1)
0052 #define CON_INT_MASK            (1 << 0)
0053 #define CON_MASK                (CON_SWITCH_OPEN | CON_RAW_DATA | \
0054                  CON_MANUAL_SW | CON_WAIT)
0055 
0056 /* Device Type 1 */
0057 #define DEV_USB_OTG             7
0058 #define DEV_DEDICATED_CHG       6
0059 #define DEV_USB_CHG             5
0060 #define DEV_CAR_KIT             4
0061 #define DEV_UART                3
0062 #define DEV_USB                 2
0063 #define DEV_AUDIO_2             1
0064 #define DEV_AUDIO_1             0
0065 
0066 #define DEV_T1_USB_MASK         (DEV_USB_OTG | DEV_USB)
0067 #define DEV_T1_UART_MASK        (DEV_UART)
0068 #define DEV_T1_CHARGER_MASK     (DEV_DEDICATED_CHG | DEV_USB_CHG)
0069 
0070 /* Device Type 2 */
0071 #define DEV_AV                  14
0072 #define DEV_TTY                 13
0073 #define DEV_PPD                 12
0074 #define DEV_JIG_UART_OFF        11
0075 #define DEV_JIG_UART_ON         10
0076 #define DEV_JIG_USB_OFF         9
0077 #define DEV_JIG_USB_ON          8
0078 
0079 #define DEV_T2_USB_MASK         (DEV_JIG_USB_OFF | DEV_JIG_USB_ON)
0080 #define DEV_T2_UART_MASK        (DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
0081 #define DEV_T2_JIG_MASK         (DEV_JIG_USB_OFF | DEV_JIG_USB_ON | \
0082                  DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
0083 
0084 /*
0085  * Manual Switch
0086  * D- [7:5] / D+ [4:2]
0087  * 000: Open all / 001: USB / 010: AUDIO / 011: UART / 100: V_AUDIO
0088  */
0089 #define SW_VAUDIO               ((4 << 5) | (4 << 2))
0090 #define SW_UART                 ((3 << 5) | (3 << 2))
0091 #define SW_AUDIO                ((2 << 5) | (2 << 2))
0092 #define SW_DHOST                ((1 << 5) | (1 << 2))
0093 #define SW_AUTO                 ((0 << 5) | (0 << 2))
0094 
0095 /* Interrupt 1 */
0096 #define INT1_MASK               (0xff << 0)
0097 #define INT_DETACH              (1 << 1)
0098 #define INT_ATTACH              (1 << 0)
0099 
0100 /* Interrupt 2 mask */
0101 #define INT2_MASK               (0x1f << 0)
0102 
0103 /* Timing Set 1 */
0104 #define TIMING1_ADC_500MS       (0x6 << 0)
0105 
0106 struct fsa9480_usbsw {
0107     struct device *dev;
0108     struct regmap *regmap;
0109     struct extcon_dev *edev;
0110     u16 cable;
0111 };
0112 
0113 static const unsigned int fsa9480_extcon_cable[] = {
0114     EXTCON_USB_HOST,
0115     EXTCON_USB,
0116     EXTCON_CHG_USB_DCP,
0117     EXTCON_CHG_USB_SDP,
0118     EXTCON_CHG_USB_ACA,
0119     EXTCON_JACK_LINE_OUT,
0120     EXTCON_JACK_VIDEO_OUT,
0121     EXTCON_JIG,
0122 
0123     EXTCON_NONE,
0124 };
0125 
0126 static const u64 cable_types[] = {
0127     [DEV_USB_OTG] = BIT_ULL(EXTCON_USB_HOST),
0128     [DEV_DEDICATED_CHG] = BIT_ULL(EXTCON_USB) | BIT_ULL(EXTCON_CHG_USB_DCP),
0129     [DEV_USB_CHG] = BIT_ULL(EXTCON_USB) | BIT_ULL(EXTCON_CHG_USB_SDP),
0130     [DEV_CAR_KIT] = BIT_ULL(EXTCON_USB) | BIT_ULL(EXTCON_CHG_USB_SDP)
0131             | BIT_ULL(EXTCON_JACK_LINE_OUT),
0132     [DEV_UART] = BIT_ULL(EXTCON_JIG),
0133     [DEV_USB] = BIT_ULL(EXTCON_USB) | BIT_ULL(EXTCON_CHG_USB_SDP),
0134     [DEV_AUDIO_2] = BIT_ULL(EXTCON_JACK_LINE_OUT),
0135     [DEV_AUDIO_1] = BIT_ULL(EXTCON_JACK_LINE_OUT),
0136     [DEV_AV] = BIT_ULL(EXTCON_JACK_LINE_OUT)
0137            | BIT_ULL(EXTCON_JACK_VIDEO_OUT),
0138     [DEV_TTY] = BIT_ULL(EXTCON_JIG),
0139     [DEV_PPD] = BIT_ULL(EXTCON_JACK_LINE_OUT) | BIT_ULL(EXTCON_CHG_USB_ACA),
0140     [DEV_JIG_UART_OFF] = BIT_ULL(EXTCON_JIG),
0141     [DEV_JIG_UART_ON] = BIT_ULL(EXTCON_JIG),
0142     [DEV_JIG_USB_OFF] = BIT_ULL(EXTCON_USB) | BIT_ULL(EXTCON_JIG),
0143     [DEV_JIG_USB_ON] = BIT_ULL(EXTCON_USB) | BIT_ULL(EXTCON_JIG),
0144 };
0145 
0146 /* Define regmap configuration of FSA9480 for I2C communication  */
0147 static bool fsa9480_volatile_reg(struct device *dev, unsigned int reg)
0148 {
0149     switch (reg) {
0150     case FSA9480_REG_INT1_MASK:
0151         return true;
0152     default:
0153         break;
0154     }
0155     return false;
0156 }
0157 
0158 static const struct regmap_config fsa9480_regmap_config = {
0159     .reg_bits   = 8,
0160     .val_bits   = 8,
0161     .volatile_reg   = fsa9480_volatile_reg,
0162     .max_register   = FSA9480_REG_END,
0163 };
0164 
0165 static int fsa9480_write_reg(struct fsa9480_usbsw *usbsw, int reg, int value)
0166 {
0167     int ret;
0168 
0169     ret = regmap_write(usbsw->regmap, reg, value);
0170     if (ret < 0)
0171         dev_err(usbsw->dev, "%s: err %d\n", __func__, ret);
0172 
0173     return ret;
0174 }
0175 
0176 static int fsa9480_read_reg(struct fsa9480_usbsw *usbsw, int reg)
0177 {
0178     int ret, val;
0179 
0180     ret = regmap_read(usbsw->regmap, reg, &val);
0181     if (ret < 0) {
0182         dev_err(usbsw->dev, "%s: err %d\n", __func__, ret);
0183         return ret;
0184     }
0185 
0186     return val;
0187 }
0188 
0189 static int fsa9480_read_irq(struct fsa9480_usbsw *usbsw, int *value)
0190 {
0191     u8 regs[2];
0192     int ret;
0193 
0194     ret = regmap_bulk_read(usbsw->regmap, FSA9480_REG_INT1, regs, 2);
0195     if (ret < 0)
0196         dev_err(usbsw->dev, "%s: err %d\n", __func__, ret);
0197 
0198     *value = regs[1] << 8 | regs[0];
0199     return ret;
0200 }
0201 
0202 static void fsa9480_handle_change(struct fsa9480_usbsw *usbsw,
0203                   u16 mask, bool attached)
0204 {
0205     while (mask) {
0206         int dev = fls64(mask) - 1;
0207         u64 cables = cable_types[dev];
0208 
0209         while (cables) {
0210             int cable = fls64(cables) - 1;
0211 
0212             extcon_set_state_sync(usbsw->edev, cable, attached);
0213             cables &= ~BIT_ULL(cable);
0214         }
0215 
0216         mask &= ~BIT_ULL(dev);
0217     }
0218 }
0219 
0220 static void fsa9480_detect_dev(struct fsa9480_usbsw *usbsw)
0221 {
0222     int val1, val2;
0223     u16 val;
0224 
0225     val1 = fsa9480_read_reg(usbsw, FSA9480_REG_DEV_T1);
0226     val2 = fsa9480_read_reg(usbsw, FSA9480_REG_DEV_T2);
0227     if (val1 < 0 || val2 < 0) {
0228         dev_err(usbsw->dev, "%s: failed to read registers", __func__);
0229         return;
0230     }
0231     val = val2 << 8 | val1;
0232 
0233     dev_info(usbsw->dev, "dev1: 0x%x, dev2: 0x%x\n", val1, val2);
0234 
0235     /* handle detached cables first */
0236     fsa9480_handle_change(usbsw, usbsw->cable & ~val, false);
0237 
0238     /* then handle attached ones */
0239     fsa9480_handle_change(usbsw, val & ~usbsw->cable, true);
0240 
0241     usbsw->cable = val;
0242 }
0243 
0244 static irqreturn_t fsa9480_irq_handler(int irq, void *data)
0245 {
0246     struct fsa9480_usbsw *usbsw = data;
0247     int intr = 0;
0248 
0249     /* clear interrupt */
0250     fsa9480_read_irq(usbsw, &intr);
0251     if (!intr)
0252         return IRQ_NONE;
0253 
0254     /* device detection */
0255     fsa9480_detect_dev(usbsw);
0256 
0257     return IRQ_HANDLED;
0258 }
0259 
0260 static int fsa9480_probe(struct i2c_client *client,
0261              const struct i2c_device_id *id)
0262 {
0263     struct fsa9480_usbsw *info;
0264     int ret;
0265 
0266     if (!client->irq) {
0267         dev_err(&client->dev, "no interrupt provided\n");
0268         return -EINVAL;
0269     }
0270 
0271     info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
0272     if (!info)
0273         return -ENOMEM;
0274     info->dev = &client->dev;
0275 
0276     i2c_set_clientdata(client, info);
0277 
0278     /* External connector */
0279     info->edev = devm_extcon_dev_allocate(info->dev,
0280                           fsa9480_extcon_cable);
0281     if (IS_ERR(info->edev)) {
0282         dev_err(info->dev, "failed to allocate memory for extcon\n");
0283         ret = -ENOMEM;
0284         return ret;
0285     }
0286 
0287     ret = devm_extcon_dev_register(info->dev, info->edev);
0288     if (ret) {
0289         dev_err(info->dev, "failed to register extcon device\n");
0290         return ret;
0291     }
0292 
0293     info->regmap = devm_regmap_init_i2c(client, &fsa9480_regmap_config);
0294     if (IS_ERR(info->regmap)) {
0295         ret = PTR_ERR(info->regmap);
0296         dev_err(info->dev, "failed to allocate register map: %d\n",
0297             ret);
0298         return ret;
0299     }
0300 
0301     /* ADC Detect Time: 500ms */
0302     fsa9480_write_reg(info, FSA9480_REG_TIMING1, TIMING1_ADC_500MS);
0303 
0304     /* configure automatic switching */
0305     fsa9480_write_reg(info, FSA9480_REG_CTRL, CON_MASK);
0306 
0307     /* unmask interrupt (attach/detach only) */
0308     fsa9480_write_reg(info, FSA9480_REG_INT1_MASK,
0309               INT1_MASK & ~(INT_ATTACH | INT_DETACH));
0310     fsa9480_write_reg(info, FSA9480_REG_INT2_MASK, INT2_MASK);
0311 
0312     ret = devm_request_threaded_irq(info->dev, client->irq, NULL,
0313                     fsa9480_irq_handler,
0314                     IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
0315                     "fsa9480", info);
0316     if (ret) {
0317         dev_err(info->dev, "failed to request IRQ\n");
0318         return ret;
0319     }
0320 
0321     device_init_wakeup(info->dev, true);
0322     fsa9480_detect_dev(info);
0323 
0324     return 0;
0325 }
0326 
0327 #ifdef CONFIG_PM_SLEEP
0328 static int fsa9480_suspend(struct device *dev)
0329 {
0330     struct i2c_client *client = to_i2c_client(dev);
0331 
0332     if (device_may_wakeup(&client->dev) && client->irq)
0333         enable_irq_wake(client->irq);
0334 
0335     return 0;
0336 }
0337 
0338 static int fsa9480_resume(struct device *dev)
0339 {
0340     struct i2c_client *client = to_i2c_client(dev);
0341 
0342     if (device_may_wakeup(&client->dev) && client->irq)
0343         disable_irq_wake(client->irq);
0344 
0345     return 0;
0346 }
0347 #endif
0348 
0349 static const struct dev_pm_ops fsa9480_pm_ops = {
0350     SET_SYSTEM_SLEEP_PM_OPS(fsa9480_suspend, fsa9480_resume)
0351 };
0352 
0353 static const struct i2c_device_id fsa9480_id[] = {
0354     { "fsa9480", 0 },
0355     {}
0356 };
0357 MODULE_DEVICE_TABLE(i2c, fsa9480_id);
0358 
0359 static const struct of_device_id fsa9480_of_match[] = {
0360     { .compatible = "fcs,fsa9480", },
0361     { .compatible = "fcs,fsa880", },
0362     { .compatible = "ti,tsu6111", },
0363     { },
0364 };
0365 MODULE_DEVICE_TABLE(of, fsa9480_of_match);
0366 
0367 static struct i2c_driver fsa9480_i2c_driver = {
0368     .driver         = {
0369         .name       = "fsa9480",
0370         .pm     = &fsa9480_pm_ops,
0371         .of_match_table = fsa9480_of_match,
0372     },
0373     .probe          = fsa9480_probe,
0374     .id_table       = fsa9480_id,
0375 };
0376 
0377 static int __init fsa9480_module_init(void)
0378 {
0379     return i2c_add_driver(&fsa9480_i2c_driver);
0380 }
0381 subsys_initcall(fsa9480_module_init);
0382 
0383 static void __exit fsa9480_module_exit(void)
0384 {
0385     i2c_del_driver(&fsa9480_i2c_driver);
0386 }
0387 module_exit(fsa9480_module_exit);
0388 
0389 MODULE_DESCRIPTION("Fairchild Semiconductor FSA9480 extcon driver");
0390 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
0391 MODULE_LICENSE("GPL");