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/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>
28 #include <linux/of_device.h>
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
;
43 /* Read/Write functions for different register alignments */
45 static int i2c_pca_pf_readbyte8(void *pd
, int reg
)
47 struct i2c_pca_pf_data
*i2c
= pd
;
48 return ioread8(i2c
->reg_base
+ reg
);
51 static int i2c_pca_pf_readbyte16(void *pd
, int reg
)
53 struct i2c_pca_pf_data
*i2c
= pd
;
54 return ioread8(i2c
->reg_base
+ reg
* 2);
57 static int i2c_pca_pf_readbyte32(void *pd
, int reg
)
59 struct i2c_pca_pf_data
*i2c
= pd
;
60 return ioread8(i2c
->reg_base
+ reg
* 4);
63 static void i2c_pca_pf_writebyte8(void *pd
, int reg
, int val
)
65 struct i2c_pca_pf_data
*i2c
= pd
;
66 iowrite8(val
, i2c
->reg_base
+ reg
);
69 static void i2c_pca_pf_writebyte16(void *pd
, int reg
, int val
)
71 struct i2c_pca_pf_data
*i2c
= pd
;
72 iowrite8(val
, i2c
->reg_base
+ reg
* 2);
75 static void i2c_pca_pf_writebyte32(void *pd
, int reg
, int val
)
77 struct i2c_pca_pf_data
*i2c
= pd
;
78 iowrite8(val
, i2c
->reg_base
+ reg
* 4);
82 static int i2c_pca_pf_waitforcompletion(void *pd
)
84 struct i2c_pca_pf_data
*i2c
= pd
;
85 unsigned long timeout
;
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
);
94 timeout
= jiffies
+ i2c
->adap
.timeout
;
96 ret
= time_before(jiffies
, timeout
);
97 if (i2c
->algo_data
.read_byte(i2c
, I2C_PCA_CON
)
107 static void i2c_pca_pf_dummyreset(void *pd
)
109 struct i2c_pca_pf_data
*i2c
= pd
;
111 dev_warn(&i2c
->adap
.dev
, "No reset-pin found. Chip may get stuck!\n");
114 static void i2c_pca_pf_resetchip(void *pd
)
116 struct i2c_pca_pf_data
*i2c
= pd
;
118 gpiod_set_value(i2c
->gpio
, 1);
120 gpiod_set_value(i2c
->gpio
, 0);
123 static irqreturn_t
i2c_pca_pf_handler(int this_irq
, void *dev_id
)
125 struct i2c_pca_pf_data
*i2c
= dev_id
;
127 if ((i2c
->algo_data
.read_byte(i2c
, I2C_PCA_CON
) & I2C_PCA_CON_SI
) == 0)
136 static int i2c_pca_pf_probe(struct platform_device
*pdev
)
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
;
146 irq
= platform_get_irq(pdev
, 0);
147 /* If irq is 0, we do polling. */
151 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
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
);
161 init_waitqueue_head(&i2c
->wait
);
163 i2c
->io_base
= res
->start
;
164 i2c
->io_size
= resource_size(res
);
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
;
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
,
185 i2c
->gpio
= gpio_to_desc(platform_data
->gpio
);
186 gpiod_direction_output(i2c
->gpio
, 0);
188 dev_warn(&pdev
->dev
, "Registering gpio failed!\n");
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
);
200 i2c
->adap
.timeout
= HZ
;
201 i2c
->algo_data
.i2c_clock
= 59000;
205 i2c
->algo_data
.data
= i2c
;
206 i2c
->algo_data
.wait_for_completion
= i2c_pca_pf_waitforcompletion
;
208 i2c
->algo_data
.reset_chip
= i2c_pca_pf_resetchip
;
210 i2c
->algo_data
.reset_chip
= i2c_pca_pf_dummyreset
;
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
;
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
;
221 case IORESOURCE_MEM_8BIT
:
223 i2c
->algo_data
.write_byte
= i2c_pca_pf_writebyte8
;
224 i2c
->algo_data
.read_byte
= i2c_pca_pf_readbyte8
;
229 ret
= devm_request_irq(&pdev
->dev
, irq
, i2c_pca_pf_handler
,
230 IRQF_TRIGGER_FALLING
, pdev
->name
, i2c
);
235 ret
= i2c_pca_add_numbered_bus(&i2c
->adap
);
239 platform_set_drvdata(pdev
, i2c
);
241 dev_info(&pdev
->dev
, "registered.\n");
246 static int i2c_pca_pf_remove(struct platform_device
*pdev
)
248 struct i2c_pca_pf_data
*i2c
= platform_get_drvdata(pdev
);
250 i2c_del_adapter(&i2c
->adap
);
256 static const struct of_device_id i2c_pca_of_match_table
[] = {
257 { .compatible
= "nxp,pca9564" },
258 { .compatible
= "nxp,pca9665" },
261 MODULE_DEVICE_TABLE(of
, i2c_pca_of_match_table
);
264 static struct platform_driver i2c_pca_pf_driver
= {
265 .probe
= i2c_pca_pf_probe
,
266 .remove
= i2c_pca_pf_remove
,
268 .name
= "i2c-pca-platform",
269 .of_match_table
= of_match_ptr(i2c_pca_of_match_table
),
273 module_platform_driver(i2c_pca_pf_driver
);
275 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
276 MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver");
277 MODULE_LICENSE("GPL");