4 * Platform driver for the PCA9564 I2C controller.
6 * Copyright (C) 2008 Pengutronix
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.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/jiffies.h>
19 #include <linux/errno.h>
20 #include <linux/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
23 #include <linux/i2c-algo-pca.h>
24 #include <linux/i2c-pca-platform.h>
25 #include <linux/gpio.h>
30 struct i2c_pca_pf_data
{
31 void __iomem
*reg_base
;
32 int irq
; /* if 0, use polling */
34 wait_queue_head_t wait
;
35 struct i2c_adapter adap
;
36 struct i2c_algo_pca_data algo_data
;
37 unsigned long io_base
;
38 unsigned long io_size
;
41 /* Read/Write functions for different register alignments */
43 static int i2c_pca_pf_readbyte8(void *pd
, int reg
)
45 struct i2c_pca_pf_data
*i2c
= pd
;
46 return ioread8(i2c
->reg_base
+ reg
);
49 static int i2c_pca_pf_readbyte16(void *pd
, int reg
)
51 struct i2c_pca_pf_data
*i2c
= pd
;
52 return ioread8(i2c
->reg_base
+ reg
* 2);
55 static int i2c_pca_pf_readbyte32(void *pd
, int reg
)
57 struct i2c_pca_pf_data
*i2c
= pd
;
58 return ioread8(i2c
->reg_base
+ reg
* 4);
61 static void i2c_pca_pf_writebyte8(void *pd
, int reg
, int val
)
63 struct i2c_pca_pf_data
*i2c
= pd
;
64 iowrite8(val
, i2c
->reg_base
+ reg
);
67 static void i2c_pca_pf_writebyte16(void *pd
, int reg
, int val
)
69 struct i2c_pca_pf_data
*i2c
= pd
;
70 iowrite8(val
, i2c
->reg_base
+ reg
* 2);
73 static void i2c_pca_pf_writebyte32(void *pd
, int reg
, int val
)
75 struct i2c_pca_pf_data
*i2c
= pd
;
76 iowrite8(val
, i2c
->reg_base
+ reg
* 4);
80 static int i2c_pca_pf_waitforcompletion(void *pd
)
82 struct i2c_pca_pf_data
*i2c
= pd
;
83 unsigned long timeout
;
87 ret
= wait_event_timeout(i2c
->wait
,
88 i2c
->algo_data
.read_byte(i2c
, I2C_PCA_CON
)
89 & I2C_PCA_CON_SI
, i2c
->adap
.timeout
);
92 timeout
= jiffies
+ i2c
->adap
.timeout
;
94 ret
= time_before(jiffies
, timeout
);
95 if (i2c
->algo_data
.read_byte(i2c
, I2C_PCA_CON
)
105 static void i2c_pca_pf_dummyreset(void *pd
)
107 struct i2c_pca_pf_data
*i2c
= pd
;
108 printk(KERN_WARNING
"%s: No reset-pin found. Chip may get stuck!\n",
112 static void i2c_pca_pf_resetchip(void *pd
)
114 struct i2c_pca_pf_data
*i2c
= pd
;
116 gpio_set_value(i2c
->gpio
, 0);
118 gpio_set_value(i2c
->gpio
, 1);
121 static irqreturn_t
i2c_pca_pf_handler(int this_irq
, void *dev_id
)
123 struct i2c_pca_pf_data
*i2c
= dev_id
;
125 if ((i2c
->algo_data
.read_byte(i2c
, I2C_PCA_CON
) & I2C_PCA_CON_SI
) == 0)
134 static int i2c_pca_pf_probe(struct platform_device
*pdev
)
136 struct i2c_pca_pf_data
*i2c
;
137 struct resource
*res
;
138 struct i2c_pca9564_pf_platform_data
*platform_data
=
139 dev_get_platdata(&pdev
->dev
);
143 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
144 irq
= platform_get_irq(pdev
, 0);
145 /* If irq is 0, we do polling. */
152 if (!request_mem_region(res
->start
, resource_size(res
), res
->name
)) {
157 i2c
= kzalloc(sizeof(struct i2c_pca_pf_data
), GFP_KERNEL
);
163 init_waitqueue_head(&i2c
->wait
);
165 i2c
->reg_base
= ioremap(res
->start
, resource_size(res
));
166 if (!i2c
->reg_base
) {
170 i2c
->io_base
= res
->start
;
171 i2c
->io_size
= resource_size(res
);
174 i2c
->adap
.nr
= pdev
->id
;
175 i2c
->adap
.owner
= THIS_MODULE
;
176 snprintf(i2c
->adap
.name
, sizeof(i2c
->adap
.name
),
177 "PCA9564/PCA9665 at 0x%08lx",
178 (unsigned long) res
->start
);
179 i2c
->adap
.algo_data
= &i2c
->algo_data
;
180 i2c
->adap
.dev
.parent
= &pdev
->dev
;
183 i2c
->adap
.timeout
= platform_data
->timeout
;
184 i2c
->algo_data
.i2c_clock
= platform_data
->i2c_clock_speed
;
185 i2c
->gpio
= platform_data
->gpio
;
187 i2c
->adap
.timeout
= HZ
;
188 i2c
->algo_data
.i2c_clock
= 59000;
192 i2c
->algo_data
.data
= i2c
;
193 i2c
->algo_data
.wait_for_completion
= i2c_pca_pf_waitforcompletion
;
194 i2c
->algo_data
.reset_chip
= i2c_pca_pf_dummyreset
;
196 switch (res
->flags
& IORESOURCE_MEM_TYPE_MASK
) {
197 case IORESOURCE_MEM_32BIT
:
198 i2c
->algo_data
.write_byte
= i2c_pca_pf_writebyte32
;
199 i2c
->algo_data
.read_byte
= i2c_pca_pf_readbyte32
;
201 case IORESOURCE_MEM_16BIT
:
202 i2c
->algo_data
.write_byte
= i2c_pca_pf_writebyte16
;
203 i2c
->algo_data
.read_byte
= i2c_pca_pf_readbyte16
;
205 case IORESOURCE_MEM_8BIT
:
207 i2c
->algo_data
.write_byte
= i2c_pca_pf_writebyte8
;
208 i2c
->algo_data
.read_byte
= i2c_pca_pf_readbyte8
;
212 /* Use gpio_is_valid() when in mainline */
213 if (i2c
->gpio
> -1) {
214 ret
= gpio_request(i2c
->gpio
, i2c
->adap
.name
);
216 gpio_direction_output(i2c
->gpio
, 1);
217 i2c
->algo_data
.reset_chip
= i2c_pca_pf_resetchip
;
219 printk(KERN_WARNING
"%s: Registering gpio failed!\n",
226 ret
= request_irq(irq
, i2c_pca_pf_handler
,
227 IRQF_TRIGGER_FALLING
, pdev
->name
, i2c
);
232 if (i2c_pca_add_numbered_bus(&i2c
->adap
) < 0) {
237 platform_set_drvdata(pdev
, i2c
);
239 printk(KERN_INFO
"%s registered.\n", i2c
->adap
.name
);
248 gpio_free(i2c
->gpio
);
250 iounmap(i2c
->reg_base
);
254 release_mem_region(res
->start
, resource_size(res
));
256 printk(KERN_ERR
"Registering PCA9564/PCA9665 FAILED! (%d)\n", ret
);
260 static int i2c_pca_pf_remove(struct platform_device
*pdev
)
262 struct i2c_pca_pf_data
*i2c
= platform_get_drvdata(pdev
);
264 i2c_del_adapter(&i2c
->adap
);
267 free_irq(i2c
->irq
, i2c
);
270 gpio_free(i2c
->gpio
);
272 iounmap(i2c
->reg_base
);
273 release_mem_region(i2c
->io_base
, i2c
->io_size
);
279 static struct platform_driver i2c_pca_pf_driver
= {
280 .probe
= i2c_pca_pf_probe
,
281 .remove
= i2c_pca_pf_remove
,
283 .name
= "i2c-pca-platform",
284 .owner
= THIS_MODULE
,
288 module_platform_driver(i2c_pca_pf_driver
);
290 MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
291 MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver");
292 MODULE_LICENSE("GPL");