1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * i2c_pca_platform.c 4 * 5 * Platform driver for the PCA9564 I2C controller. 6 * 7 * Copyright (C) 2008 Pengutronix 8 * 9 10 */ 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/slab.h> 14 #include <linux/delay.h> 15 #include <linux/jiffies.h> 16 #include <linux/errno.h> 17 #include <linux/i2c.h> 18 #include <linux/interrupt.h> 19 #include <linux/platform_device.h> 20 #include <linux/i2c-algo-pca.h> 21 #include <linux/platform_data/i2c-pca-platform.h> 22 #include <linux/gpio/consumer.h> 23 #include <linux/io.h> 24 #include <linux/of.h> 25 #include <linux/of_device.h> 26 27 #include <asm/irq.h> 28 29 struct i2c_pca_pf_data { 30 void __iomem *reg_base; 31 int irq; /* if 0, use polling */ 32 struct gpio_desc *gpio; 33 wait_queue_head_t wait; 34 struct i2c_adapter adap; 35 struct i2c_algo_pca_data algo_data; 36 unsigned long io_base; 37 unsigned long io_size; 38 }; 39 40 /* Read/Write functions for different register alignments */ 41 42 static int i2c_pca_pf_readbyte8(void *pd, int reg) 43 { 44 struct i2c_pca_pf_data *i2c = pd; 45 return ioread8(i2c->reg_base + reg); 46 } 47 48 static int i2c_pca_pf_readbyte16(void *pd, int reg) 49 { 50 struct i2c_pca_pf_data *i2c = pd; 51 return ioread8(i2c->reg_base + reg * 2); 52 } 53 54 static int i2c_pca_pf_readbyte32(void *pd, int reg) 55 { 56 struct i2c_pca_pf_data *i2c = pd; 57 return ioread8(i2c->reg_base + reg * 4); 58 } 59 60 static void i2c_pca_pf_writebyte8(void *pd, int reg, int val) 61 { 62 struct i2c_pca_pf_data *i2c = pd; 63 iowrite8(val, i2c->reg_base + reg); 64 } 65 66 static void i2c_pca_pf_writebyte16(void *pd, int reg, int val) 67 { 68 struct i2c_pca_pf_data *i2c = pd; 69 iowrite8(val, i2c->reg_base + reg * 2); 70 } 71 72 static void i2c_pca_pf_writebyte32(void *pd, int reg, int val) 73 { 74 struct i2c_pca_pf_data *i2c = pd; 75 iowrite8(val, i2c->reg_base + reg * 4); 76 } 77 78 79 static int i2c_pca_pf_waitforcompletion(void *pd) 80 { 81 struct i2c_pca_pf_data *i2c = pd; 82 unsigned long timeout; 83 long ret; 84 85 if (i2c->irq) { 86 ret = wait_event_timeout(i2c->wait, 87 i2c->algo_data.read_byte(i2c, I2C_PCA_CON) 88 & I2C_PCA_CON_SI, i2c->adap.timeout); 89 } else { 90 /* Do polling */ 91 timeout = jiffies + i2c->adap.timeout; 92 do { 93 ret = time_before(jiffies, timeout); 94 if (i2c->algo_data.read_byte(i2c, I2C_PCA_CON) 95 & I2C_PCA_CON_SI) 96 break; 97 udelay(100); 98 } while (ret); 99 } 100 101 return ret > 0; 102 } 103 104 static void i2c_pca_pf_dummyreset(void *pd) 105 { 106 struct i2c_pca_pf_data *i2c = pd; 107 108 dev_warn(&i2c->adap.dev, "No reset-pin found. Chip may get stuck!\n"); 109 } 110 111 static void i2c_pca_pf_resetchip(void *pd) 112 { 113 struct i2c_pca_pf_data *i2c = pd; 114 115 gpiod_set_value(i2c->gpio, 1); 116 ndelay(100); 117 gpiod_set_value(i2c->gpio, 0); 118 } 119 120 static irqreturn_t i2c_pca_pf_handler(int this_irq, void *dev_id) 121 { 122 struct i2c_pca_pf_data *i2c = dev_id; 123 124 if ((i2c->algo_data.read_byte(i2c, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0) 125 return IRQ_NONE; 126 127 wake_up(&i2c->wait); 128 129 return IRQ_HANDLED; 130 } 131 132 133 static int i2c_pca_pf_probe(struct platform_device *pdev) 134 { 135 struct i2c_pca_pf_data *i2c; 136 struct resource *res; 137 struct i2c_pca9564_pf_platform_data *platform_data = 138 dev_get_platdata(&pdev->dev); 139 struct device_node *np = pdev->dev.of_node; 140 int ret = 0; 141 int irq; 142 143 irq = platform_get_irq(pdev, 0); 144 /* If irq is 0, we do polling. */ 145 if (irq < 0) 146 irq = 0; 147 148 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 149 if (!i2c) 150 return -ENOMEM; 151 152 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 153 i2c->reg_base = devm_ioremap_resource(&pdev->dev, res); 154 if (IS_ERR(i2c->reg_base)) 155 return PTR_ERR(i2c->reg_base); 156 157 158 init_waitqueue_head(&i2c->wait); 159 160 i2c->io_base = res->start; 161 i2c->io_size = resource_size(res); 162 i2c->irq = irq; 163 164 i2c->adap.nr = pdev->id; 165 i2c->adap.owner = THIS_MODULE; 166 snprintf(i2c->adap.name, sizeof(i2c->adap.name), 167 "PCA9564/PCA9665 at 0x%08lx", 168 (unsigned long) res->start); 169 i2c->adap.algo_data = &i2c->algo_data; 170 i2c->adap.dev.parent = &pdev->dev; 171 i2c->adap.dev.of_node = np; 172 173 i2c->gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW); 174 if (IS_ERR(i2c->gpio)) 175 return PTR_ERR(i2c->gpio); 176 177 i2c->adap.timeout = HZ; 178 ret = device_property_read_u32(&pdev->dev, "clock-frequency", 179 &i2c->algo_data.i2c_clock); 180 if (ret) 181 i2c->algo_data.i2c_clock = 59000; 182 183 if (platform_data) { 184 i2c->adap.timeout = platform_data->timeout; 185 i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed; 186 } 187 188 i2c->algo_data.data = i2c; 189 i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion; 190 if (i2c->gpio) 191 i2c->algo_data.reset_chip = i2c_pca_pf_resetchip; 192 else 193 i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset; 194 195 switch (res->flags & IORESOURCE_MEM_TYPE_MASK) { 196 case IORESOURCE_MEM_32BIT: 197 i2c->algo_data.write_byte = i2c_pca_pf_writebyte32; 198 i2c->algo_data.read_byte = i2c_pca_pf_readbyte32; 199 break; 200 case IORESOURCE_MEM_16BIT: 201 i2c->algo_data.write_byte = i2c_pca_pf_writebyte16; 202 i2c->algo_data.read_byte = i2c_pca_pf_readbyte16; 203 break; 204 case IORESOURCE_MEM_8BIT: 205 default: 206 i2c->algo_data.write_byte = i2c_pca_pf_writebyte8; 207 i2c->algo_data.read_byte = i2c_pca_pf_readbyte8; 208 break; 209 } 210 211 if (irq) { 212 ret = devm_request_irq(&pdev->dev, irq, i2c_pca_pf_handler, 213 IRQF_TRIGGER_FALLING, pdev->name, i2c); 214 if (ret) 215 return ret; 216 } 217 218 ret = i2c_pca_add_numbered_bus(&i2c->adap); 219 if (ret) 220 return ret; 221 222 platform_set_drvdata(pdev, i2c); 223 224 dev_info(&pdev->dev, "registered.\n"); 225 226 return 0; 227 } 228 229 static int i2c_pca_pf_remove(struct platform_device *pdev) 230 { 231 struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev); 232 233 i2c_del_adapter(&i2c->adap); 234 235 return 0; 236 } 237 238 #ifdef CONFIG_OF 239 static const struct of_device_id i2c_pca_of_match_table[] = { 240 { .compatible = "nxp,pca9564" }, 241 { .compatible = "nxp,pca9665" }, 242 {}, 243 }; 244 MODULE_DEVICE_TABLE(of, i2c_pca_of_match_table); 245 #endif 246 247 static struct platform_driver i2c_pca_pf_driver = { 248 .probe = i2c_pca_pf_probe, 249 .remove = i2c_pca_pf_remove, 250 .driver = { 251 .name = "i2c-pca-platform", 252 .of_match_table = of_match_ptr(i2c_pca_of_match_table), 253 }, 254 }; 255 256 module_platform_driver(i2c_pca_pf_driver); 257 258 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>"); 259 MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver"); 260 MODULE_LICENSE("GPL"); 261