Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  i2c_pca_platform.c
0004  *
0005  *  Platform driver for the PCA9564 I2C controller.
0006  *
0007  *  Copyright (C) 2008 Pengutronix
0008  *
0009 
0010  */
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/delay.h>
0015 #include <linux/jiffies.h>
0016 #include <linux/errno.h>
0017 #include <linux/i2c.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/i2c-algo-pca.h>
0021 #include <linux/platform_data/i2c-pca-platform.h>
0022 #include <linux/gpio/consumer.h>
0023 #include <linux/io.h>
0024 #include <linux/of.h>
0025 #include <linux/of_device.h>
0026 
0027 #include <asm/irq.h>
0028 
0029 struct i2c_pca_pf_data {
0030     void __iomem            *reg_base;
0031     int             irq;    /* if 0, use polling */
0032     struct gpio_desc        *gpio;
0033     wait_queue_head_t       wait;
0034     struct i2c_adapter      adap;
0035     struct i2c_algo_pca_data    algo_data;
0036 };
0037 
0038 /* Read/Write functions for different register alignments */
0039 
0040 static int i2c_pca_pf_readbyte8(void *pd, int reg)
0041 {
0042     struct i2c_pca_pf_data *i2c = pd;
0043     return ioread8(i2c->reg_base + reg);
0044 }
0045 
0046 static int i2c_pca_pf_readbyte16(void *pd, int reg)
0047 {
0048     struct i2c_pca_pf_data *i2c = pd;
0049     return ioread8(i2c->reg_base + reg * 2);
0050 }
0051 
0052 static int i2c_pca_pf_readbyte32(void *pd, int reg)
0053 {
0054     struct i2c_pca_pf_data *i2c = pd;
0055     return ioread8(i2c->reg_base + reg * 4);
0056 }
0057 
0058 static void i2c_pca_pf_writebyte8(void *pd, int reg, int val)
0059 {
0060     struct i2c_pca_pf_data *i2c = pd;
0061     iowrite8(val, i2c->reg_base + reg);
0062 }
0063 
0064 static void i2c_pca_pf_writebyte16(void *pd, int reg, int val)
0065 {
0066     struct i2c_pca_pf_data *i2c = pd;
0067     iowrite8(val, i2c->reg_base + reg * 2);
0068 }
0069 
0070 static void i2c_pca_pf_writebyte32(void *pd, int reg, int val)
0071 {
0072     struct i2c_pca_pf_data *i2c = pd;
0073     iowrite8(val, i2c->reg_base + reg * 4);
0074 }
0075 
0076 
0077 static int i2c_pca_pf_waitforcompletion(void *pd)
0078 {
0079     struct i2c_pca_pf_data *i2c = pd;
0080     unsigned long timeout;
0081     long ret;
0082 
0083     if (i2c->irq) {
0084         ret = wait_event_timeout(i2c->wait,
0085             i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
0086             & I2C_PCA_CON_SI, i2c->adap.timeout);
0087     } else {
0088         /* Do polling */
0089         timeout = jiffies + i2c->adap.timeout;
0090         do {
0091             ret = time_before(jiffies, timeout);
0092             if (i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
0093                     & I2C_PCA_CON_SI)
0094                 break;
0095             udelay(100);
0096         } while (ret);
0097     }
0098 
0099     return ret > 0;
0100 }
0101 
0102 static void i2c_pca_pf_dummyreset(void *pd)
0103 {
0104     struct i2c_pca_pf_data *i2c = pd;
0105 
0106     dev_warn(&i2c->adap.dev, "No reset-pin found. Chip may get stuck!\n");
0107 }
0108 
0109 static void i2c_pca_pf_resetchip(void *pd)
0110 {
0111     struct i2c_pca_pf_data *i2c = pd;
0112 
0113     gpiod_set_value(i2c->gpio, 1);
0114     ndelay(100);
0115     gpiod_set_value(i2c->gpio, 0);
0116 }
0117 
0118 static irqreturn_t i2c_pca_pf_handler(int this_irq, void *dev_id)
0119 {
0120     struct i2c_pca_pf_data *i2c = dev_id;
0121 
0122     if ((i2c->algo_data.read_byte(i2c, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
0123         return IRQ_NONE;
0124 
0125     wake_up(&i2c->wait);
0126 
0127     return IRQ_HANDLED;
0128 }
0129 
0130 
0131 static int i2c_pca_pf_probe(struct platform_device *pdev)
0132 {
0133     struct i2c_pca_pf_data *i2c;
0134     struct resource *res;
0135     struct i2c_pca9564_pf_platform_data *platform_data =
0136                 dev_get_platdata(&pdev->dev);
0137     struct device_node *np = pdev->dev.of_node;
0138     int ret = 0;
0139     int irq;
0140 
0141     irq = platform_get_irq_optional(pdev, 0);
0142     /* If irq is 0, we do polling. */
0143     if (irq < 0)
0144         irq = 0;
0145 
0146     i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
0147     if (!i2c)
0148         return -ENOMEM;
0149 
0150     i2c->reg_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
0151     if (IS_ERR(i2c->reg_base))
0152         return PTR_ERR(i2c->reg_base);
0153 
0154 
0155     init_waitqueue_head(&i2c->wait);
0156 
0157     i2c->irq = irq;
0158 
0159     i2c->adap.nr = pdev->id;
0160     i2c->adap.owner = THIS_MODULE;
0161     snprintf(i2c->adap.name, sizeof(i2c->adap.name),
0162          "PCA9564/PCA9665 at 0x%08lx",
0163          (unsigned long) res->start);
0164     i2c->adap.algo_data = &i2c->algo_data;
0165     i2c->adap.dev.parent = &pdev->dev;
0166     i2c->adap.dev.of_node = np;
0167 
0168     i2c->gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
0169     if (IS_ERR(i2c->gpio))
0170         return PTR_ERR(i2c->gpio);
0171 
0172     i2c->adap.timeout = HZ;
0173     ret = device_property_read_u32(&pdev->dev, "clock-frequency",
0174                        &i2c->algo_data.i2c_clock);
0175     if (ret)
0176         i2c->algo_data.i2c_clock = 59000;
0177 
0178     if (platform_data) {
0179         i2c->adap.timeout = platform_data->timeout;
0180         i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed;
0181     }
0182 
0183     i2c->algo_data.data = i2c;
0184     i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion;
0185     if (i2c->gpio)
0186         i2c->algo_data.reset_chip = i2c_pca_pf_resetchip;
0187     else
0188         i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset;
0189 
0190     switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
0191     case IORESOURCE_MEM_32BIT:
0192         i2c->algo_data.write_byte = i2c_pca_pf_writebyte32;
0193         i2c->algo_data.read_byte = i2c_pca_pf_readbyte32;
0194         break;
0195     case IORESOURCE_MEM_16BIT:
0196         i2c->algo_data.write_byte = i2c_pca_pf_writebyte16;
0197         i2c->algo_data.read_byte = i2c_pca_pf_readbyte16;
0198         break;
0199     case IORESOURCE_MEM_8BIT:
0200     default:
0201         i2c->algo_data.write_byte = i2c_pca_pf_writebyte8;
0202         i2c->algo_data.read_byte = i2c_pca_pf_readbyte8;
0203         break;
0204     }
0205 
0206     if (irq) {
0207         ret = devm_request_irq(&pdev->dev, irq, i2c_pca_pf_handler,
0208             IRQF_TRIGGER_FALLING, pdev->name, i2c);
0209         if (ret)
0210             return ret;
0211     }
0212 
0213     ret = i2c_pca_add_numbered_bus(&i2c->adap);
0214     if (ret)
0215         return ret;
0216 
0217     platform_set_drvdata(pdev, i2c);
0218 
0219     dev_info(&pdev->dev, "registered.\n");
0220 
0221     return 0;
0222 }
0223 
0224 static int i2c_pca_pf_remove(struct platform_device *pdev)
0225 {
0226     struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev);
0227 
0228     i2c_del_adapter(&i2c->adap);
0229 
0230     return 0;
0231 }
0232 
0233 #ifdef CONFIG_OF
0234 static const struct of_device_id i2c_pca_of_match_table[] = {
0235     { .compatible = "nxp,pca9564" },
0236     { .compatible = "nxp,pca9665" },
0237     {},
0238 };
0239 MODULE_DEVICE_TABLE(of, i2c_pca_of_match_table);
0240 #endif
0241 
0242 static struct platform_driver i2c_pca_pf_driver = {
0243     .probe = i2c_pca_pf_probe,
0244     .remove = i2c_pca_pf_remove,
0245     .driver = {
0246         .name = "i2c-pca-platform",
0247         .of_match_table = of_match_ptr(i2c_pca_of_match_table),
0248     },
0249 };
0250 
0251 module_platform_driver(i2c_pca_pf_driver);
0252 
0253 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
0254 MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver");
0255 MODULE_LICENSE("GPL");