Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-block.git] / drivers / i2c / busses / i2c-pca-platform.c
CommitLineData
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>
244fbbb8
WS
15#include <linux/slab.h>
16#include <linux/delay.h>
2378bc09 17#include <linux/jiffies.h>
244fbbb8
WS
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>
e5c71377 23#include <linux/platform_data/i2c-pca-platform.h>
244fbbb8 24#include <linux/gpio.h>
4cc7229d 25#include <linux/gpio/consumer.h>
21782180 26#include <linux/io.h>
0e8ce93b
CP
27#include <linux/of.h>
28#include <linux/of_device.h>
244fbbb8
WS
29
30#include <asm/irq.h>
244fbbb8 31
244fbbb8
WS
32struct i2c_pca_pf_data {
33 void __iomem *reg_base;
34 int irq; /* if 0, use polling */
4cc7229d 35 struct gpio_desc *gpio;
244fbbb8
WS
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
45static 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
51static 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
57static 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
63static 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
69static 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
75static 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
82static int i2c_pca_pf_waitforcompletion(void *pd)
83{
84 struct i2c_pca_pf_data *i2c = pd;
2378bc09 85 unsigned long timeout;
6abb930a 86 long ret;
244fbbb8
WS
87
88 if (i2c->irq) {
22f8b269 89 ret = wait_event_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;
6abb930a
YY
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;
244fbbb8 100 udelay(100);
6abb930a 101 } while (ret);
244fbbb8
WS
102 }
103
2378bc09 104 return ret > 0;
244fbbb8
WS
105}
106
107static void i2c_pca_pf_dummyreset(void *pd)
108{
109 struct i2c_pca_pf_data *i2c = pd;
df40f247
CP
110
111 dev_warn(&i2c->adap.dev, "No reset-pin found. Chip may get stuck!\n");
244fbbb8
WS
112}
113
114static void i2c_pca_pf_resetchip(void *pd)
115{
116 struct i2c_pca_pf_data *i2c = pd;
117
4cc7229d 118 gpiod_set_value(i2c->gpio, 1);
244fbbb8 119 ndelay(100);
4cc7229d 120 gpiod_set_value(i2c->gpio, 0);
244fbbb8
WS
121}
122
123static 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
22f8b269 130 wake_up(&i2c->wait);
244fbbb8
WS
131
132 return IRQ_HANDLED;
133}
134
135
0b255e92 136static int i2c_pca_pf_probe(struct platform_device *pdev)
244fbbb8
WS
137{
138 struct i2c_pca_pf_data *i2c;
139 struct resource *res;
140 struct i2c_pca9564_pf_platform_data *platform_data =
6d4028c6 141 dev_get_platdata(&pdev->dev);
0e8ce93b 142 struct device_node *np = pdev->dev.of_node;
244fbbb8
WS
143 int ret = 0;
144 int irq;
145
244fbbb8
WS
146 irq = platform_get_irq(pdev, 0);
147 /* If irq is 0, we do polling. */
0e8ce93b
CP
148 if (irq < 0)
149 irq = 0;
244fbbb8 150
fa70ca7c
CP
151 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
152 if (!i2c)
153 return -ENOMEM;
244fbbb8 154
fa70ca7c
CP
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);
244fbbb8 159
244fbbb8
WS
160
161 init_waitqueue_head(&i2c->wait);
162
244fbbb8 163 i2c->io_base = res->start;
c6ffddea 164 i2c->io_size = resource_size(res);
244fbbb8
WS
165 i2c->irq = irq;
166
44454baa 167 i2c->adap.nr = pdev->id;
244fbbb8 168 i2c->adap.owner = THIS_MODULE;
eff9ec95
MAC
169 snprintf(i2c->adap.name, sizeof(i2c->adap.name),
170 "PCA9564/PCA9665 at 0x%08lx",
171 (unsigned long) res->start);
244fbbb8
WS
172 i2c->adap.algo_data = &i2c->algo_data;
173 i2c->adap.dev.parent = &pdev->dev;
0e8ce93b 174 i2c->adap.dev.of_node = np;
244fbbb8 175
06783261
CP
176 i2c->gpio = devm_gpiod_get_optional(&pdev->dev, "reset-gpios", GPIOD_OUT_LOW);
177 if (IS_ERR(i2c->gpio))
178 return PTR_ERR(i2c->gpio);
179
7562dee2
CP
180 i2c->adap.timeout = HZ;
181 ret = device_property_read_u32(&pdev->dev, "clock-frequency",
182 &i2c->algo_data.i2c_clock);
183 if (ret)
184 i2c->algo_data.i2c_clock = 59000;
185
6b110d13
WS
186 if (platform_data) {
187 i2c->adap.timeout = platform_data->timeout;
188 i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed;
6b110d13
WS
189 }
190
244fbbb8 191 i2c->algo_data.data = i2c;
6b110d13 192 i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion;
2ec4d883
CP
193 if (i2c->gpio)
194 i2c->algo_data.reset_chip = i2c_pca_pf_resetchip;
195 else
196 i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset;
244fbbb8
WS
197
198 switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
199 case IORESOURCE_MEM_32BIT:
200 i2c->algo_data.write_byte = i2c_pca_pf_writebyte32;
201 i2c->algo_data.read_byte = i2c_pca_pf_readbyte32;
202 break;
203 case IORESOURCE_MEM_16BIT:
204 i2c->algo_data.write_byte = i2c_pca_pf_writebyte16;
205 i2c->algo_data.read_byte = i2c_pca_pf_readbyte16;
206 break;
207 case IORESOURCE_MEM_8BIT:
208 default:
209 i2c->algo_data.write_byte = i2c_pca_pf_writebyte8;
210 i2c->algo_data.read_byte = i2c_pca_pf_readbyte8;
211 break;
212 }
213
244fbbb8 214 if (irq) {
fa70ca7c 215 ret = devm_request_irq(&pdev->dev, irq, i2c_pca_pf_handler,
32358443 216 IRQF_TRIGGER_FALLING, pdev->name, i2c);
244fbbb8 217 if (ret)
fa70ca7c 218 return ret;
244fbbb8
WS
219 }
220
78e6c5ab
CP
221 ret = i2c_pca_add_numbered_bus(&i2c->adap);
222 if (ret)
223 return ret;
244fbbb8
WS
224
225 platform_set_drvdata(pdev, i2c);
226
df40f247 227 dev_info(&pdev->dev, "registered.\n");
244fbbb8
WS
228
229 return 0;
244fbbb8
WS
230}
231
0b255e92 232static int i2c_pca_pf_remove(struct platform_device *pdev)
244fbbb8
WS
233{
234 struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev);
244fbbb8
WS
235
236 i2c_del_adapter(&i2c->adap);
237
244fbbb8
WS
238 return 0;
239}
240
0e8ce93b
CP
241#ifdef CONFIG_OF
242static const struct of_device_id i2c_pca_of_match_table[] = {
243 { .compatible = "nxp,pca9564" },
244 { .compatible = "nxp,pca9665" },
245 {},
246};
247MODULE_DEVICE_TABLE(of, i2c_pca_of_match_table);
248#endif
249
244fbbb8
WS
250static struct platform_driver i2c_pca_pf_driver = {
251 .probe = i2c_pca_pf_probe,
0b255e92 252 .remove = i2c_pca_pf_remove,
244fbbb8
WS
253 .driver = {
254 .name = "i2c-pca-platform",
0e8ce93b 255 .of_match_table = of_match_ptr(i2c_pca_of_match_table),
244fbbb8
WS
256 },
257};
258
a3664b51 259module_platform_driver(i2c_pca_pf_driver);
244fbbb8 260
9ef8a0be 261MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
eff9ec95 262MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver");
244fbbb8 263MODULE_LICENSE("GPL");