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