Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * GPIO Chip driver for Analog Devices
0004  * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
0005  *
0006  * Copyright 2009-2010 Analog Devices Inc.
0007  */
0008 
0009 #include <linux/gpio/driver.h>
0010 #include <linux/i2c.h>
0011 #include <linux/init.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/irq.h>
0014 #include <linux/kernel.h>
0015 #include <linux/mod_devicetable.h>
0016 #include <linux/module.h>
0017 #include <linux/slab.h>
0018 
0019 #include <linux/platform_data/adp5588.h>
0020 
0021 /*
0022  * Early pre 4.0 Silicon required to delay readout by at least 25ms,
0023  * since the Event Counter Register updated 25ms after the interrupt
0024  * asserted.
0025  */
0026 #define WA_DELAYED_READOUT_REVID(rev)   ((rev) < 4)
0027 
0028 struct adp5588_gpio {
0029     struct i2c_client *client;
0030     struct gpio_chip gpio_chip;
0031     struct mutex lock;  /* protect cached dir, dat_out */
0032     /* protect serialized access to the interrupt controller bus */
0033     struct mutex irq_lock;
0034     uint8_t dat_out[3];
0035     uint8_t dir[3];
0036     uint8_t int_lvl_low[3];
0037     uint8_t int_lvl_high[3];
0038     uint8_t int_en[3];
0039     uint8_t irq_mask[3];
0040     uint8_t int_input_en[3];
0041 };
0042 
0043 static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
0044 {
0045     int ret = i2c_smbus_read_byte_data(client, reg);
0046 
0047     if (ret < 0)
0048         dev_err(&client->dev, "Read Error\n");
0049 
0050     return ret;
0051 }
0052 
0053 static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
0054 {
0055     int ret = i2c_smbus_write_byte_data(client, reg, val);
0056 
0057     if (ret < 0)
0058         dev_err(&client->dev, "Write Error\n");
0059 
0060     return ret;
0061 }
0062 
0063 static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
0064 {
0065     struct adp5588_gpio *dev = gpiochip_get_data(chip);
0066     unsigned bank = ADP5588_BANK(off);
0067     unsigned bit = ADP5588_BIT(off);
0068     int val;
0069 
0070     mutex_lock(&dev->lock);
0071 
0072     if (dev->dir[bank] & bit)
0073         val = dev->dat_out[bank];
0074     else
0075         val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank);
0076 
0077     mutex_unlock(&dev->lock);
0078 
0079     return !!(val & bit);
0080 }
0081 
0082 static void adp5588_gpio_set_value(struct gpio_chip *chip,
0083                    unsigned off, int val)
0084 {
0085     unsigned bank, bit;
0086     struct adp5588_gpio *dev = gpiochip_get_data(chip);
0087 
0088     bank = ADP5588_BANK(off);
0089     bit = ADP5588_BIT(off);
0090 
0091     mutex_lock(&dev->lock);
0092     if (val)
0093         dev->dat_out[bank] |= bit;
0094     else
0095         dev->dat_out[bank] &= ~bit;
0096 
0097     adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
0098                dev->dat_out[bank]);
0099     mutex_unlock(&dev->lock);
0100 }
0101 
0102 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
0103 {
0104     int ret;
0105     unsigned bank;
0106     struct adp5588_gpio *dev = gpiochip_get_data(chip);
0107 
0108     bank = ADP5588_BANK(off);
0109 
0110     mutex_lock(&dev->lock);
0111     dev->dir[bank] &= ~ADP5588_BIT(off);
0112     ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
0113     mutex_unlock(&dev->lock);
0114 
0115     return ret;
0116 }
0117 
0118 static int adp5588_gpio_direction_output(struct gpio_chip *chip,
0119                      unsigned off, int val)
0120 {
0121     int ret;
0122     unsigned bank, bit;
0123     struct adp5588_gpio *dev = gpiochip_get_data(chip);
0124 
0125     bank = ADP5588_BANK(off);
0126     bit = ADP5588_BIT(off);
0127 
0128     mutex_lock(&dev->lock);
0129     dev->dir[bank] |= bit;
0130 
0131     if (val)
0132         dev->dat_out[bank] |= bit;
0133     else
0134         dev->dat_out[bank] &= ~bit;
0135 
0136     ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
0137                  dev->dat_out[bank]);
0138     ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
0139                  dev->dir[bank]);
0140     mutex_unlock(&dev->lock);
0141 
0142     return ret;
0143 }
0144 
0145 #ifdef CONFIG_GPIO_ADP5588_IRQ
0146 
0147 static void adp5588_irq_bus_lock(struct irq_data *d)
0148 {
0149     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0150     struct adp5588_gpio *dev = gpiochip_get_data(gc);
0151 
0152     mutex_lock(&dev->irq_lock);
0153 }
0154 
0155  /*
0156   * genirq core code can issue chip->mask/unmask from atomic context.
0157   * This doesn't work for slow busses where an access needs to sleep.
0158   * bus_sync_unlock() is therefore called outside the atomic context,
0159   * syncs the current irq mask state with the slow external controller
0160   * and unlocks the bus.
0161   */
0162 
0163 static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
0164 {
0165     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0166     struct adp5588_gpio *dev = gpiochip_get_data(gc);
0167     int i;
0168 
0169     for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
0170         if (dev->int_input_en[i]) {
0171             mutex_lock(&dev->lock);
0172             dev->dir[i] &= ~dev->int_input_en[i];
0173             dev->int_input_en[i] = 0;
0174             adp5588_gpio_write(dev->client, GPIO_DIR1 + i,
0175                        dev->dir[i]);
0176             mutex_unlock(&dev->lock);
0177         }
0178 
0179         if (dev->int_en[i] ^ dev->irq_mask[i]) {
0180             dev->int_en[i] = dev->irq_mask[i];
0181             adp5588_gpio_write(dev->client, GPI_EM1 + i,
0182                        dev->int_en[i]);
0183         }
0184     }
0185 
0186     mutex_unlock(&dev->irq_lock);
0187 }
0188 
0189 static void adp5588_irq_mask(struct irq_data *d)
0190 {
0191     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0192     struct adp5588_gpio *dev = gpiochip_get_data(gc);
0193 
0194     dev->irq_mask[ADP5588_BANK(d->hwirq)] &= ~ADP5588_BIT(d->hwirq);
0195 }
0196 
0197 static void adp5588_irq_unmask(struct irq_data *d)
0198 {
0199     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0200     struct adp5588_gpio *dev = gpiochip_get_data(gc);
0201 
0202     dev->irq_mask[ADP5588_BANK(d->hwirq)] |= ADP5588_BIT(d->hwirq);
0203 }
0204 
0205 static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
0206 {
0207     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0208     struct adp5588_gpio *dev = gpiochip_get_data(gc);
0209     uint16_t gpio = d->hwirq;
0210     unsigned bank, bit;
0211 
0212     bank = ADP5588_BANK(gpio);
0213     bit = ADP5588_BIT(gpio);
0214 
0215     dev->int_lvl_low[bank] &= ~bit;
0216     dev->int_lvl_high[bank] &= ~bit;
0217 
0218     if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_HIGH)
0219         dev->int_lvl_high[bank] |= bit;
0220 
0221     if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_LOW)
0222         dev->int_lvl_low[bank] |= bit;
0223 
0224     dev->int_input_en[bank] |= bit;
0225 
0226     return 0;
0227 }
0228 
0229 static struct irq_chip adp5588_irq_chip = {
0230     .name           = "adp5588",
0231     .irq_mask       = adp5588_irq_mask,
0232     .irq_unmask     = adp5588_irq_unmask,
0233     .irq_bus_lock       = adp5588_irq_bus_lock,
0234     .irq_bus_sync_unlock    = adp5588_irq_bus_sync_unlock,
0235     .irq_set_type       = adp5588_irq_set_type,
0236 };
0237 
0238 static irqreturn_t adp5588_irq_handler(int irq, void *devid)
0239 {
0240     struct adp5588_gpio *dev = devid;
0241     int status = adp5588_gpio_read(dev->client, INT_STAT);
0242 
0243     if (status & ADP5588_KE_INT) {
0244         int ev_cnt = adp5588_gpio_read(dev->client, KEY_LCK_EC_STAT);
0245 
0246         if (ev_cnt > 0) {
0247             int i;
0248 
0249             for (i = 0; i < (ev_cnt & ADP5588_KEC); i++) {
0250                 int key = adp5588_gpio_read(dev->client,
0251                                 Key_EVENTA + i);
0252                 /* GPIN events begin at 97,
0253                  * bit 7 indicates logic level
0254                  */
0255                 int gpio = (key & 0x7f) - 97;
0256                 int lvl = key & (1 << 7);
0257                 int bank = ADP5588_BANK(gpio);
0258                 int bit = ADP5588_BIT(gpio);
0259 
0260                 if ((lvl && dev->int_lvl_high[bank] & bit) ||
0261                     (!lvl && dev->int_lvl_low[bank] & bit))
0262                     handle_nested_irq(irq_find_mapping(
0263                           dev->gpio_chip.irq.domain, gpio));
0264             }
0265         }
0266     }
0267 
0268     adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
0269 
0270     return IRQ_HANDLED;
0271 }
0272 
0273 
0274 static int adp5588_irq_init_hw(struct gpio_chip *gc)
0275 {
0276     struct adp5588_gpio *dev = gpiochip_get_data(gc);
0277     /* Enable IRQs after registering chip */
0278     adp5588_gpio_write(dev->client, CFG,
0279                ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_KE_IEN);
0280 
0281     return 0;
0282 }
0283 
0284 static int adp5588_irq_setup(struct adp5588_gpio *dev)
0285 {
0286     struct i2c_client *client = dev->client;
0287     int ret;
0288     struct adp5588_gpio_platform_data *pdata =
0289             dev_get_platdata(&client->dev);
0290     struct gpio_irq_chip *girq;
0291 
0292     adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
0293     adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
0294 
0295     mutex_init(&dev->irq_lock);
0296 
0297     ret = devm_request_threaded_irq(&client->dev, client->irq,
0298                     NULL, adp5588_irq_handler, IRQF_ONESHOT
0299                     | IRQF_TRIGGER_FALLING | IRQF_SHARED,
0300                     dev_name(&client->dev), dev);
0301     if (ret) {
0302         dev_err(&client->dev, "failed to request irq %d\n",
0303             client->irq);
0304         return ret;
0305     }
0306 
0307     /* This will be registered in the call to devm_gpiochip_add_data() */
0308     girq = &dev->gpio_chip.irq;
0309     girq->chip = &adp5588_irq_chip;
0310     /* This will let us handle the parent IRQ in the driver */
0311     girq->parent_handler = NULL;
0312     girq->num_parents = 0;
0313     girq->parents = NULL;
0314     girq->first = pdata ? pdata->irq_base : 0;
0315     girq->default_type = IRQ_TYPE_NONE;
0316     girq->handler = handle_simple_irq;
0317     girq->init_hw = adp5588_irq_init_hw;
0318     girq->threaded = true;
0319 
0320     return 0;
0321 }
0322 
0323 #else
0324 static int adp5588_irq_setup(struct adp5588_gpio *dev)
0325 {
0326     struct i2c_client *client = dev->client;
0327     dev_warn(&client->dev, "interrupt support not compiled in\n");
0328 
0329     return 0;
0330 }
0331 
0332 #endif /* CONFIG_GPIO_ADP5588_IRQ */
0333 
0334 static int adp5588_gpio_probe(struct i2c_client *client)
0335 {
0336     struct adp5588_gpio_platform_data *pdata =
0337             dev_get_platdata(&client->dev);
0338     struct adp5588_gpio *dev;
0339     struct gpio_chip *gc;
0340     int ret, i, revid;
0341     unsigned int pullup_dis_mask = 0;
0342 
0343     if (!i2c_check_functionality(client->adapter,
0344                     I2C_FUNC_SMBUS_BYTE_DATA)) {
0345         dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
0346         return -EIO;
0347     }
0348 
0349     dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
0350     if (!dev)
0351         return -ENOMEM;
0352 
0353     dev->client = client;
0354 
0355     gc = &dev->gpio_chip;
0356     gc->direction_input = adp5588_gpio_direction_input;
0357     gc->direction_output = adp5588_gpio_direction_output;
0358     gc->get = adp5588_gpio_get_value;
0359     gc->set = adp5588_gpio_set_value;
0360     gc->can_sleep = true;
0361     gc->base = -1;
0362     gc->parent = &client->dev;
0363 
0364     if (pdata) {
0365         gc->base = pdata->gpio_start;
0366         gc->names = pdata->names;
0367         pullup_dis_mask = pdata->pullup_dis_mask;
0368     }
0369 
0370     gc->ngpio = ADP5588_MAXGPIO;
0371     gc->label = client->name;
0372     gc->owner = THIS_MODULE;
0373 
0374     mutex_init(&dev->lock);
0375 
0376     ret = adp5588_gpio_read(dev->client, DEV_ID);
0377     if (ret < 0)
0378         return ret;
0379 
0380     revid = ret & ADP5588_DEVICE_ID_MASK;
0381 
0382     for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
0383         dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
0384         dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
0385         ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
0386         ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
0387                 (pullup_dis_mask >> (8 * i)) & 0xFF);
0388         ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
0389         if (ret)
0390             return ret;
0391     }
0392 
0393     if (client->irq) {
0394         if (WA_DELAYED_READOUT_REVID(revid)) {
0395             dev_warn(&client->dev, "GPIO int not supported\n");
0396         } else {
0397             ret = adp5588_irq_setup(dev);
0398             if (ret)
0399                 return ret;
0400         }
0401     }
0402 
0403     ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev);
0404     if (ret)
0405         return ret;
0406 
0407     i2c_set_clientdata(client, dev);
0408 
0409     return 0;
0410 }
0411 
0412 static int adp5588_gpio_remove(struct i2c_client *client)
0413 {
0414     struct adp5588_gpio *dev = i2c_get_clientdata(client);
0415 
0416     if (dev->client->irq)
0417         free_irq(dev->client->irq, dev);
0418 
0419     return 0;
0420 }
0421 
0422 static const struct i2c_device_id adp5588_gpio_id[] = {
0423     { "adp5588-gpio" },
0424     {}
0425 };
0426 MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
0427 
0428 static const struct of_device_id adp5588_gpio_of_id[] = {
0429     { .compatible = "adi,adp5588-gpio" },
0430     {}
0431 };
0432 MODULE_DEVICE_TABLE(of, adp5588_gpio_of_id);
0433 
0434 static struct i2c_driver adp5588_gpio_driver = {
0435     .driver = {
0436         .name = "adp5588-gpio",
0437         .of_match_table = adp5588_gpio_of_id,
0438     },
0439     .probe_new = adp5588_gpio_probe,
0440     .remove = adp5588_gpio_remove,
0441     .id_table = adp5588_gpio_id,
0442 };
0443 
0444 module_i2c_driver(adp5588_gpio_driver);
0445 
0446 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
0447 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
0448 MODULE_LICENSE("GPL");