]>
Commit | Line | Data |
---|---|---|
244fbbb8 WS |
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/init.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/delay.h> | |
2378bc09 | 18 | #include <linux/jiffies.h> |
244fbbb8 WS |
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> | |
26 | ||
27 | #include <asm/irq.h> | |
28 | #include <asm/io.h> | |
29 | ||
30 | #define res_len(r) ((r)->end - (r)->start + 1) | |
31 | ||
32 | struct i2c_pca_pf_data { | |
33 | void __iomem *reg_base; | |
34 | int irq; /* if 0, use polling */ | |
35 | int 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; | |
2378bc09 WS |
85 | long ret = ~0; |
86 | unsigned long timeout; | |
244fbbb8 WS |
87 | |
88 | if (i2c->irq) { | |
2378bc09 | 89 | ret = wait_event_interruptible_timeout(i2c->wait, |
244fbbb8 | 90 | i2c->algo_data.read_byte(i2c, I2C_PCA_CON) |
2378bc09 | 91 | & I2C_PCA_CON_SI, i2c->adap.timeout); |
244fbbb8 | 92 | } else { |
2378bc09 WS |
93 | /* Do polling */ |
94 | timeout = jiffies + i2c->adap.timeout; | |
95 | while (((i2c->algo_data.read_byte(i2c, I2C_PCA_CON) | |
244fbbb8 | 96 | & I2C_PCA_CON_SI) == 0) |
2378bc09 | 97 | && (ret = time_before(jiffies, timeout))) |
244fbbb8 WS |
98 | udelay(100); |
99 | } | |
100 | ||
2378bc09 | 101 | return ret > 0; |
244fbbb8 WS |
102 | } |
103 | ||
104 | static void i2c_pca_pf_dummyreset(void *pd) | |
105 | { | |
106 | struct i2c_pca_pf_data *i2c = pd; | |
107 | printk(KERN_WARNING "%s: No reset-pin found. Chip may get stuck!\n", | |
108 | i2c->adap.name); | |
109 | } | |
110 | ||
111 | static void i2c_pca_pf_resetchip(void *pd) | |
112 | { | |
113 | struct i2c_pca_pf_data *i2c = pd; | |
114 | ||
115 | gpio_set_value(i2c->gpio, 0); | |
116 | ndelay(100); | |
117 | gpio_set_value(i2c->gpio, 1); | |
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_interruptible(&i2c->wait); | |
128 | ||
129 | return IRQ_HANDLED; | |
130 | } | |
131 | ||
132 | ||
133 | static int __devinit 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 | pdev->dev.platform_data; | |
139 | int ret = 0; | |
140 | int irq; | |
141 | ||
142 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
143 | irq = platform_get_irq(pdev, 0); | |
144 | /* If irq is 0, we do polling. */ | |
145 | ||
146 | if (res == NULL) { | |
147 | ret = -ENODEV; | |
148 | goto e_print; | |
149 | } | |
150 | ||
151 | if (!request_mem_region(res->start, res_len(res), res->name)) { | |
152 | ret = -ENOMEM; | |
153 | goto e_print; | |
154 | } | |
155 | ||
156 | i2c = kzalloc(sizeof(struct i2c_pca_pf_data), GFP_KERNEL); | |
157 | if (!i2c) { | |
158 | ret = -ENOMEM; | |
159 | goto e_alloc; | |
160 | } | |
161 | ||
162 | init_waitqueue_head(&i2c->wait); | |
163 | ||
164 | i2c->reg_base = ioremap(res->start, res_len(res)); | |
165 | if (!i2c->reg_base) { | |
7650da02 | 166 | ret = -ENOMEM; |
244fbbb8 WS |
167 | goto e_remap; |
168 | } | |
169 | i2c->io_base = res->start; | |
170 | i2c->io_size = res_len(res); | |
171 | i2c->irq = irq; | |
172 | ||
173 | i2c->adap.nr = pdev->id >= 0 ? pdev->id : 0; | |
174 | i2c->adap.owner = THIS_MODULE; | |
eff9ec95 MAC |
175 | snprintf(i2c->adap.name, sizeof(i2c->adap.name), |
176 | "PCA9564/PCA9665 at 0x%08lx", | |
177 | (unsigned long) res->start); | |
244fbbb8 WS |
178 | i2c->adap.algo_data = &i2c->algo_data; |
179 | i2c->adap.dev.parent = &pdev->dev; | |
180 | i2c->adap.timeout = platform_data->timeout; | |
181 | ||
182 | i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed; | |
183 | i2c->algo_data.data = i2c; | |
184 | ||
185 | switch (res->flags & IORESOURCE_MEM_TYPE_MASK) { | |
186 | case IORESOURCE_MEM_32BIT: | |
187 | i2c->algo_data.write_byte = i2c_pca_pf_writebyte32; | |
188 | i2c->algo_data.read_byte = i2c_pca_pf_readbyte32; | |
189 | break; | |
190 | case IORESOURCE_MEM_16BIT: | |
191 | i2c->algo_data.write_byte = i2c_pca_pf_writebyte16; | |
192 | i2c->algo_data.read_byte = i2c_pca_pf_readbyte16; | |
193 | break; | |
194 | case IORESOURCE_MEM_8BIT: | |
195 | default: | |
196 | i2c->algo_data.write_byte = i2c_pca_pf_writebyte8; | |
197 | i2c->algo_data.read_byte = i2c_pca_pf_readbyte8; | |
198 | break; | |
199 | } | |
200 | ||
201 | i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion; | |
202 | ||
203 | i2c->gpio = platform_data->gpio; | |
204 | i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset; | |
205 | ||
206 | /* Use gpio_is_valid() when in mainline */ | |
207 | if (i2c->gpio > -1) { | |
208 | ret = gpio_request(i2c->gpio, i2c->adap.name); | |
209 | if (ret == 0) { | |
210 | gpio_direction_output(i2c->gpio, 1); | |
211 | i2c->algo_data.reset_chip = i2c_pca_pf_resetchip; | |
212 | } else { | |
213 | printk(KERN_WARNING "%s: Registering gpio failed!\n", | |
214 | i2c->adap.name); | |
215 | i2c->gpio = ret; | |
216 | } | |
217 | } | |
218 | ||
219 | if (irq) { | |
220 | ret = request_irq(irq, i2c_pca_pf_handler, | |
221 | IRQF_TRIGGER_FALLING, i2c->adap.name, i2c); | |
222 | if (ret) | |
223 | goto e_reqirq; | |
224 | } | |
225 | ||
226 | if (i2c_pca_add_numbered_bus(&i2c->adap) < 0) { | |
227 | ret = -ENODEV; | |
228 | goto e_adapt; | |
229 | } | |
230 | ||
231 | platform_set_drvdata(pdev, i2c); | |
232 | ||
233 | printk(KERN_INFO "%s registered.\n", i2c->adap.name); | |
234 | ||
235 | return 0; | |
236 | ||
237 | e_adapt: | |
238 | if (irq) | |
239 | free_irq(irq, i2c); | |
240 | e_reqirq: | |
241 | if (i2c->gpio > -1) | |
242 | gpio_free(i2c->gpio); | |
243 | ||
244 | iounmap(i2c->reg_base); | |
245 | e_remap: | |
246 | kfree(i2c); | |
247 | e_alloc: | |
248 | release_mem_region(res->start, res_len(res)); | |
249 | e_print: | |
eff9ec95 | 250 | printk(KERN_ERR "Registering PCA9564/PCA9665 FAILED! (%d)\n", ret); |
244fbbb8 WS |
251 | return ret; |
252 | } | |
253 | ||
254 | static int __devexit i2c_pca_pf_remove(struct platform_device *pdev) | |
255 | { | |
256 | struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev); | |
257 | platform_set_drvdata(pdev, NULL); | |
258 | ||
259 | i2c_del_adapter(&i2c->adap); | |
260 | ||
261 | if (i2c->irq) | |
262 | free_irq(i2c->irq, i2c); | |
263 | ||
264 | if (i2c->gpio > -1) | |
265 | gpio_free(i2c->gpio); | |
266 | ||
267 | iounmap(i2c->reg_base); | |
268 | release_mem_region(i2c->io_base, i2c->io_size); | |
269 | kfree(i2c); | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
274 | static struct platform_driver i2c_pca_pf_driver = { | |
275 | .probe = i2c_pca_pf_probe, | |
276 | .remove = __devexit_p(i2c_pca_pf_remove), | |
277 | .driver = { | |
278 | .name = "i2c-pca-platform", | |
279 | .owner = THIS_MODULE, | |
280 | }, | |
281 | }; | |
282 | ||
283 | static int __init i2c_pca_pf_init(void) | |
284 | { | |
285 | return platform_driver_register(&i2c_pca_pf_driver); | |
286 | } | |
287 | ||
288 | static void __exit i2c_pca_pf_exit(void) | |
289 | { | |
290 | platform_driver_unregister(&i2c_pca_pf_driver); | |
291 | } | |
292 | ||
293 | MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>"); | |
eff9ec95 | 294 | MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver"); |
244fbbb8 WS |
295 | MODULE_LICENSE("GPL"); |
296 | ||
297 | module_init(i2c_pca_pf_init); | |
298 | module_exit(i2c_pca_pf_exit); | |
299 |