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/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/platform_data/i2c-pca-platform.h>
24 #include <linux/gpio.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/io.h>
27 #include <linux/of.h>
28 #include <linux/of_device.h>
29 
30 #include <asm/irq.h>
31 
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;
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;
85 	unsigned long timeout;
86 	long ret;
87 
88 	if (i2c->irq) {
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);
92 	} else {
93 		/* Do polling */
94 		timeout = jiffies + i2c->adap.timeout;
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;
100 			udelay(100);
101 		} while (ret);
102 	}
103 
104 	return ret > 0;
105 }
106 
107 static void i2c_pca_pf_dummyreset(void *pd)
108 {
109 	struct i2c_pca_pf_data *i2c = pd;
110 
111 	dev_warn(&i2c->adap.dev, "No reset-pin found. Chip may get stuck!\n");
112 }
113 
114 static void i2c_pca_pf_resetchip(void *pd)
115 {
116 	struct i2c_pca_pf_data *i2c = pd;
117 
118 	gpiod_set_value(i2c->gpio, 1);
119 	ndelay(100);
120 	gpiod_set_value(i2c->gpio, 0);
121 }
122 
123 static 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 
130 	wake_up(&i2c->wait);
131 
132 	return IRQ_HANDLED;
133 }
134 
135 
136 static int i2c_pca_pf_probe(struct platform_device *pdev)
137 {
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;
143 	int ret = 0;
144 	int irq;
145 
146 	irq = platform_get_irq(pdev, 0);
147 	/* If irq is 0, we do polling. */
148 	if (irq < 0)
149 		irq = 0;
150 
151 	i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
152 	if (!i2c)
153 		return -ENOMEM;
154 
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);
159 
160 
161 	init_waitqueue_head(&i2c->wait);
162 
163 	i2c->io_base = res->start;
164 	i2c->io_size = resource_size(res);
165 	i2c->irq = irq;
166 
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;
175 
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 
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 
186 	if (platform_data) {
187 		i2c->adap.timeout = platform_data->timeout;
188 		i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed;
189 	}
190 
191 	i2c->algo_data.data = i2c;
192 	i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion;
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;
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 
214 	if (irq) {
215 		ret = devm_request_irq(&pdev->dev, irq, i2c_pca_pf_handler,
216 			IRQF_TRIGGER_FALLING, pdev->name, i2c);
217 		if (ret)
218 			return ret;
219 	}
220 
221 	ret = i2c_pca_add_numbered_bus(&i2c->adap);
222 	if (ret)
223 		return ret;
224 
225 	platform_set_drvdata(pdev, i2c);
226 
227 	dev_info(&pdev->dev, "registered.\n");
228 
229 	return 0;
230 }
231 
232 static int i2c_pca_pf_remove(struct platform_device *pdev)
233 {
234 	struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev);
235 
236 	i2c_del_adapter(&i2c->adap);
237 
238 	return 0;
239 }
240 
241 #ifdef CONFIG_OF
242 static const struct of_device_id i2c_pca_of_match_table[] = {
243 	{ .compatible = "nxp,pca9564" },
244 	{ .compatible = "nxp,pca9665" },
245 	{},
246 };
247 MODULE_DEVICE_TABLE(of, i2c_pca_of_match_table);
248 #endif
249 
250 static struct platform_driver i2c_pca_pf_driver = {
251 	.probe = i2c_pca_pf_probe,
252 	.remove = i2c_pca_pf_remove,
253 	.driver = {
254 		.name = "i2c-pca-platform",
255 		.of_match_table = of_match_ptr(i2c_pca_of_match_table),
256 	},
257 };
258 
259 module_platform_driver(i2c_pca_pf_driver);
260 
261 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
262 MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver");
263 MODULE_LICENSE("GPL");
264