]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/i2c/busses/i2c-pca-platform.c
i2c: pca-platform: Fix GPIO lookup code
[thirdparty/linux.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>
4cc7229d 24#include <linux/gpio/consumer.h>
21782180 25#include <linux/io.h>
0e8ce93b
CP
26#include <linux/of.h>
27#include <linux/of_device.h>
244fbbb8
WS
28
29#include <asm/irq.h>
244fbbb8 30
244fbbb8
WS
31struct i2c_pca_pf_data {
32 void __iomem *reg_base;
33 int irq; /* if 0, use polling */
4cc7229d 34 struct gpio_desc *gpio;
244fbbb8
WS
35 wait_queue_head_t wait;
36 struct i2c_adapter adap;
37 struct i2c_algo_pca_data algo_data;
38 unsigned long io_base;
39 unsigned long io_size;
40};
41
42/* Read/Write functions for different register alignments */
43
44static int i2c_pca_pf_readbyte8(void *pd, int reg)
45{
46 struct i2c_pca_pf_data *i2c = pd;
47 return ioread8(i2c->reg_base + reg);
48}
49
50static int i2c_pca_pf_readbyte16(void *pd, int reg)
51{
52 struct i2c_pca_pf_data *i2c = pd;
53 return ioread8(i2c->reg_base + reg * 2);
54}
55
56static int i2c_pca_pf_readbyte32(void *pd, int reg)
57{
58 struct i2c_pca_pf_data *i2c = pd;
59 return ioread8(i2c->reg_base + reg * 4);
60}
61
62static void i2c_pca_pf_writebyte8(void *pd, int reg, int val)
63{
64 struct i2c_pca_pf_data *i2c = pd;
65 iowrite8(val, i2c->reg_base + reg);
66}
67
68static void i2c_pca_pf_writebyte16(void *pd, int reg, int val)
69{
70 struct i2c_pca_pf_data *i2c = pd;
71 iowrite8(val, i2c->reg_base + reg * 2);
72}
73
74static void i2c_pca_pf_writebyte32(void *pd, int reg, int val)
75{
76 struct i2c_pca_pf_data *i2c = pd;
77 iowrite8(val, i2c->reg_base + reg * 4);
78}
79
80
81static int i2c_pca_pf_waitforcompletion(void *pd)
82{
83 struct i2c_pca_pf_data *i2c = pd;
2378bc09 84 unsigned long timeout;
6abb930a 85 long ret;
244fbbb8
WS
86
87 if (i2c->irq) {
22f8b269 88 ret = wait_event_timeout(i2c->wait,
244fbbb8 89 i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
2378bc09 90 & I2C_PCA_CON_SI, i2c->adap.timeout);
244fbbb8 91 } else {
2378bc09
WS
92 /* Do polling */
93 timeout = jiffies + i2c->adap.timeout;
6abb930a
YY
94 do {
95 ret = time_before(jiffies, timeout);
96 if (i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
97 & I2C_PCA_CON_SI)
98 break;
244fbbb8 99 udelay(100);
6abb930a 100 } while (ret);
244fbbb8
WS
101 }
102
2378bc09 103 return ret > 0;
244fbbb8
WS
104}
105
106static void i2c_pca_pf_dummyreset(void *pd)
107{
108 struct i2c_pca_pf_data *i2c = pd;
df40f247
CP
109
110 dev_warn(&i2c->adap.dev, "No reset-pin found. Chip may get stuck!\n");
244fbbb8
WS
111}
112
113static void i2c_pca_pf_resetchip(void *pd)
114{
115 struct i2c_pca_pf_data *i2c = pd;
116
4cc7229d 117 gpiod_set_value(i2c->gpio, 1);
244fbbb8 118 ndelay(100);
4cc7229d 119 gpiod_set_value(i2c->gpio, 0);
244fbbb8
WS
120}
121
122static irqreturn_t i2c_pca_pf_handler(int this_irq, void *dev_id)
123{
124 struct i2c_pca_pf_data *i2c = dev_id;
125
126 if ((i2c->algo_data.read_byte(i2c, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
127 return IRQ_NONE;
128
22f8b269 129 wake_up(&i2c->wait);
244fbbb8
WS
130
131 return IRQ_HANDLED;
132}
133
134
0b255e92 135static int i2c_pca_pf_probe(struct platform_device *pdev)
244fbbb8
WS
136{
137 struct i2c_pca_pf_data *i2c;
138 struct resource *res;
139 struct i2c_pca9564_pf_platform_data *platform_data =
6d4028c6 140 dev_get_platdata(&pdev->dev);
0e8ce93b 141 struct device_node *np = pdev->dev.of_node;
244fbbb8
WS
142 int ret = 0;
143 int irq;
144
244fbbb8
WS
145 irq = platform_get_irq(pdev, 0);
146 /* If irq is 0, we do polling. */
0e8ce93b
CP
147 if (irq < 0)
148 irq = 0;
244fbbb8 149
fa70ca7c
CP
150 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
151 if (!i2c)
152 return -ENOMEM;
244fbbb8 153
fa70ca7c
CP
154 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
155 i2c->reg_base = devm_ioremap_resource(&pdev->dev, res);
156 if (IS_ERR(i2c->reg_base))
157 return PTR_ERR(i2c->reg_base);
244fbbb8 158
244fbbb8
WS
159
160 init_waitqueue_head(&i2c->wait);
161
244fbbb8 162 i2c->io_base = res->start;
c6ffddea 163 i2c->io_size = resource_size(res);
244fbbb8
WS
164 i2c->irq = irq;
165
44454baa 166 i2c->adap.nr = pdev->id;
244fbbb8 167 i2c->adap.owner = THIS_MODULE;
eff9ec95
MAC
168 snprintf(i2c->adap.name, sizeof(i2c->adap.name),
169 "PCA9564/PCA9665 at 0x%08lx",
170 (unsigned long) res->start);
244fbbb8
WS
171 i2c->adap.algo_data = &i2c->algo_data;
172 i2c->adap.dev.parent = &pdev->dev;
0e8ce93b 173 i2c->adap.dev.of_node = np;
244fbbb8 174
a0cac264 175 i2c->gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
06783261
CP
176 if (IS_ERR(i2c->gpio))
177 return PTR_ERR(i2c->gpio);
178
7562dee2
CP
179 i2c->adap.timeout = HZ;
180 ret = device_property_read_u32(&pdev->dev, "clock-frequency",
181 &i2c->algo_data.i2c_clock);
182 if (ret)
183 i2c->algo_data.i2c_clock = 59000;
184
6b110d13
WS
185 if (platform_data) {
186 i2c->adap.timeout = platform_data->timeout;
187 i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed;
6b110d13
WS
188 }
189
244fbbb8 190 i2c->algo_data.data = i2c;
6b110d13 191 i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion;
2ec4d883
CP
192 if (i2c->gpio)
193 i2c->algo_data.reset_chip = i2c_pca_pf_resetchip;
194 else
195 i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset;
244fbbb8
WS
196
197 switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
198 case IORESOURCE_MEM_32BIT:
199 i2c->algo_data.write_byte = i2c_pca_pf_writebyte32;
200 i2c->algo_data.read_byte = i2c_pca_pf_readbyte32;
201 break;
202 case IORESOURCE_MEM_16BIT:
203 i2c->algo_data.write_byte = i2c_pca_pf_writebyte16;
204 i2c->algo_data.read_byte = i2c_pca_pf_readbyte16;
205 break;
206 case IORESOURCE_MEM_8BIT:
207 default:
208 i2c->algo_data.write_byte = i2c_pca_pf_writebyte8;
209 i2c->algo_data.read_byte = i2c_pca_pf_readbyte8;
210 break;
211 }
212
244fbbb8 213 if (irq) {
fa70ca7c 214 ret = devm_request_irq(&pdev->dev, irq, i2c_pca_pf_handler,
32358443 215 IRQF_TRIGGER_FALLING, pdev->name, i2c);
244fbbb8 216 if (ret)
fa70ca7c 217 return ret;
244fbbb8
WS
218 }
219
78e6c5ab
CP
220 ret = i2c_pca_add_numbered_bus(&i2c->adap);
221 if (ret)
222 return ret;
244fbbb8
WS
223
224 platform_set_drvdata(pdev, i2c);
225
df40f247 226 dev_info(&pdev->dev, "registered.\n");
244fbbb8
WS
227
228 return 0;
244fbbb8
WS
229}
230
0b255e92 231static int i2c_pca_pf_remove(struct platform_device *pdev)
244fbbb8
WS
232{
233 struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev);
244fbbb8
WS
234
235 i2c_del_adapter(&i2c->adap);
236
244fbbb8
WS
237 return 0;
238}
239
0e8ce93b
CP
240#ifdef CONFIG_OF
241static const struct of_device_id i2c_pca_of_match_table[] = {
242 { .compatible = "nxp,pca9564" },
243 { .compatible = "nxp,pca9665" },
244 {},
245};
246MODULE_DEVICE_TABLE(of, i2c_pca_of_match_table);
247#endif
248
244fbbb8
WS
249static struct platform_driver i2c_pca_pf_driver = {
250 .probe = i2c_pca_pf_probe,
0b255e92 251 .remove = i2c_pca_pf_remove,
244fbbb8
WS
252 .driver = {
253 .name = "i2c-pca-platform",
0e8ce93b 254 .of_match_table = of_match_ptr(i2c_pca_of_match_table),
244fbbb8
WS
255 },
256};
257
a3664b51 258module_platform_driver(i2c_pca_pf_driver);
244fbbb8 259
9ef8a0be 260MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
eff9ec95 261MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver");
244fbbb8 262MODULE_LICENSE("GPL");