a3b03116e2be25c2db059a87925d864d56d028d4
[openwrt/staging/chunkeey.git] / target / linux / ramips / patches-3.14 / 0052-i2c-MIPS-adds-ralink-I2C-driver.patch
1 From 225f36695bb07dad9510f9affd79e63f1a44a195 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 27 Jul 2014 09:52:56 +0100
4 Subject: [PATCH 52/57] i2c: MIPS: adds ralink I2C driver
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
8 .../devicetree/bindings/i2c/i2c-ralink.txt | 27 ++
9 drivers/i2c/busses/Kconfig | 4 +
10 drivers/i2c/busses/Makefile | 1 +
11 drivers/i2c/busses/i2c-ralink.c | 274 ++++++++++++++++++++
12 4 files changed, 306 insertions(+)
13 create mode 100644 Documentation/devicetree/bindings/i2c/i2c-ralink.txt
14 create mode 100644 drivers/i2c/busses/i2c-ralink.c
15
16 Index: linux-3.14.18/Documentation/devicetree/bindings/i2c/i2c-ralink.txt
17 ===================================================================
18 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
19 +++ linux-3.14.18/Documentation/devicetree/bindings/i2c/i2c-ralink.txt 2014-10-06 14:28:14.296590159 +0200
20 @@ -0,0 +1,27 @@
21 +I2C for Ralink platforms
22 +
23 +Required properties :
24 +- compatible : Must be "link,rt3052-i2c"
25 +- reg: physical base address of the controller and length of memory mapped
26 + region.
27 +- #address-cells = <1>;
28 +- #size-cells = <0>;
29 +
30 +Optional properties:
31 +- Child nodes conforming to i2c bus binding
32 +
33 +Example :
34 +
35 +palmbus@10000000 {
36 + i2c@900 {
37 + compatible = "link,rt3052-i2c";
38 + reg = <0x900 0x100>;
39 + #address-cells = <1>;
40 + #size-cells = <0>;
41 +
42 + hwmon@4b {
43 + compatible = "national,lm92";
44 + reg = <0x4b>;
45 + };
46 + };
47 +};
48 Index: linux-3.14.18/drivers/i2c/busses/Kconfig
49 ===================================================================
50 --- linux-3.14.18.orig/drivers/i2c/busses/Kconfig 2014-09-06 01:34:59.000000000 +0200
51 +++ linux-3.14.18/drivers/i2c/busses/Kconfig 2014-10-06 14:28:14.296590159 +0200
52 @@ -659,6 +659,10 @@
53 This driver can also be built as a module. If so, the module
54 will be called i2c-riic.
55
56 +config I2C_RALINK
57 + tristate "Ralink I2C Controller"
58 + select OF_I2C
59 +
60 config HAVE_S3C2410_I2C
61 bool
62 help
63 Index: linux-3.14.18/drivers/i2c/busses/Makefile
64 ===================================================================
65 --- linux-3.14.18.orig/drivers/i2c/busses/Makefile 2014-09-06 01:34:59.000000000 +0200
66 +++ linux-3.14.18/drivers/i2c/busses/Makefile 2014-10-06 14:28:14.296590159 +0200
67 @@ -63,6 +63,7 @@
68 obj-$(CONFIG_I2C_PUV3) += i2c-puv3.o
69 obj-$(CONFIG_I2C_PXA) += i2c-pxa.o
70 obj-$(CONFIG_I2C_PXA_PCI) += i2c-pxa-pci.o
71 +obj-$(CONFIG_I2C_RALINK) += i2c-ralink.o
72 obj-$(CONFIG_I2C_RIIC) += i2c-riic.o
73 obj-$(CONFIG_I2C_S3C2410) += i2c-s3c2410.o
74 obj-$(CONFIG_I2C_S6000) += i2c-s6000.o
75 Index: linux-3.14.18/drivers/i2c/busses/i2c-ralink.c
76 ===================================================================
77 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
78 +++ linux-3.14.18/drivers/i2c/busses/i2c-ralink.c 2014-10-06 15:53:32.416500362 +0200
79 @@ -0,0 +1,298 @@
80 +/*
81 + * drivers/i2c/busses/i2c-ralink.c
82 + *
83 + * Copyright (C) 2013 Steven Liu <steven_liu@mediatek.com>
84 + *
85 + * Improve driver for i2cdetect from i2c-tools to detect i2c devices on the bus.
86 + * (C) 2014 Sittisak <sittisaks@hotmail.com>
87 + *
88 + * This software is licensed under the terms of the GNU General Public
89 + * License version 2, as published by the Free Software Foundation, and
90 + * may be copied, distributed, and modified under those terms.
91 + *
92 + * This program is distributed in the hope that it will be useful,
93 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
94 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95 + * GNU General Public License for more details.
96 + *
97 + */
98 +
99 +#include <linux/interrupt.h>
100 +#include <linux/kernel.h>
101 +#include <linux/module.h>
102 +#include <linux/reset.h>
103 +#include <linux/delay.h>
104 +#include <linux/slab.h>
105 +#include <linux/init.h>
106 +#include <linux/errno.h>
107 +#include <linux/platform_device.h>
108 +#include <linux/i2c.h>
109 +#include <linux/io.h>
110 +#include <linux/of_i2c.h>
111 +#include <linux/err.h>
112 +
113 +#include <asm/mach-ralink/ralink_regs.h>
114 +
115 +#define REG_CONFIG_REG 0x00
116 +#define REG_CLKDIV_REG 0x04
117 +#define REG_DEVADDR_REG 0x08
118 +#define REG_ADDR_REG 0x0C
119 +#define REG_DATAOUT_REG 0x10
120 +#define REG_DATAIN_REG 0x14
121 +#define REG_STATUS_REG 0x18
122 +#define REG_STARTXFR_REG 0x1C
123 +#define REG_BYTECNT_REG 0x20
124 +
125 +#define I2C_STARTERR BIT(4)
126 +#define I2C_ACKERR BIT(3)
127 +#define I2C_DATARDY BIT(2)
128 +#define I2C_SDOEMPTY BIT(1)
129 +#define I2C_BUSY BIT(0)
130 +
131 +#define I2C_DEVADLEN_7 (6 << 2)
132 +#define I2C_ADDRDIS BIT(1)
133 +
134 +#define I2C_RETRY 0x1000
135 +
136 +#define CLKDIV_VALUE 200 // clock rate is 40M, 40M / (200*2) = 100k (standard i2c bus rate).
137 +//#define CLKDIV_VALUE 50 // clock rate is 40M, 40M / (50*2) = 400k (fast i2c bus rate).
138 +
139 +#define READ_CMD 0x01
140 +#define WRITE_CMD 0x00
141 +#define READ_BLOCK 64
142 +
143 +static void __iomem *membase;
144 +static struct i2c_adapter *adapter;
145 +
146 +static void rt_i2c_w32(u32 val, unsigned reg)
147 +{
148 + iowrite32(val, membase + reg);
149 +}
150 +
151 +static u32 rt_i2c_r32(unsigned reg)
152 +{
153 + return ioread32(membase + reg);
154 +}
155 +
156 +static inline int rt_i2c_get_ack(void)
157 +{
158 + return (rt_i2c_r32(REG_STATUS_REG) & I2C_ACKERR) ? -EIO : 0;
159 +}
160 +
161 +static inline int rt_i2c_wait_rx_done(void)
162 +{
163 + int retries = I2C_RETRY;
164 +
165 + do {
166 + if (!retries--)
167 + break;
168 + } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
169 +
170 + return (retries < 0) ? -ETIMEDOUT : 0;
171 +}
172 +
173 +static inline int rt_i2c_wait_idle(void)
174 +{
175 + int retries = I2C_RETRY;
176 +
177 + do {
178 + if (!retries--)
179 + break;
180 + } while(rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY);
181 +
182 + return (retries < 0);
183 +}
184 +
185 +static inline int rt_i2c_wait_tx_done(void)
186 +{
187 + int retries = I2C_RETRY;
188 +
189 + do {
190 + if (!retries--)
191 + break;
192 + } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
193 +
194 + return (retries < 0) ? -ETIMEDOUT : 0;
195 +}
196 +
197 +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
198 +{
199 + int i = 0, j = 0, pos = 0;
200 + int nblock = msg->len / READ_BLOCK;
201 + int rem = msg->len % READ_BLOCK;
202 + int ret = 0;
203 + /*printk("i2c handle msg len:%d\n",msg->len);*/
204 + if (msg->flags & I2C_M_TEN) {
205 + printk("10 bits addr not supported\n");
206 + return -EINVAL;
207 + }
208 +
209 + if (msg->flags & I2C_M_RD) {
210 + for (i = 0; i < nblock; i++) {
211 + if (rt_i2c_wait_idle()) {
212 + printk("i2c-read line busy\n");
213 + return -ETIMEDOUT;
214 + }
215 + rt_i2c_w32(READ_BLOCK - 1, REG_BYTECNT_REG);
216 + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
217 + for (j = 0; j < READ_BLOCK; j++) {
218 + if (rt_i2c_wait_rx_done()<0) ret = rt_i2c_wait_rx_done();
219 + if (rt_i2c_get_ack()<0) ret = rt_i2c_get_ack();
220 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
221 + }
222 + }
223 +
224 + if (rt_i2c_wait_idle()) {
225 + printk("i2c-read line busy\n");
226 + return -ETIMEDOUT;
227 + }
228 + if (rem){
229 + rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
230 + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
231 + }
232 + for (i = 0; i < rem; i++) {
233 + if (rt_i2c_wait_rx_done()<0) ret = rt_i2c_wait_rx_done();
234 + if (rt_i2c_get_ack() <0) ret = rt_i2c_get_ack();
235 +
236 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
237 + }
238 + } else {
239 + if (rt_i2c_wait_idle()) {
240 + printk("i2c-write line busy\n");
241 + return -ETIMEDOUT;
242 + }
243 + rt_i2c_w32(msg->len - 1, REG_BYTECNT_REG);
244 + for (i = 0; i < msg->len; i++) {
245 + rt_i2c_w32(msg->buf[i], REG_DATAOUT_REG);
246 + rt_i2c_w32(WRITE_CMD, REG_STARTXFR_REG);
247 +
248 + if (rt_i2c_wait_tx_done()<0) ret = rt_i2c_wait_tx_done();
249 + if (rt_i2c_get_ack()<0) ret = rt_i2c_get_ack();
250 +
251 +
252 + }
253 + }
254 +
255 + return ret;
256 +}
257 +
258 +static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
259 +{
260 + int i = 0;
261 + int ret = 0;
262 +
263 + if (rt_i2c_wait_idle()) {
264 + printk("i2c-master_xfer line busy\n");
265 + return -ETIMEDOUT;
266 + }
267 +
268 + device_reset(a->dev.parent);
269 +
270 + rt_i2c_w32(m->addr, REG_DEVADDR_REG);
271 + rt_i2c_w32(I2C_DEVADLEN_7 | I2C_ADDRDIS, REG_CONFIG_REG);
272 + rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
273 +
274 + for (i = 0; i < n && !ret; i++) {
275 + ret = rt_i2c_handle_msg(a, &m[i]);
276 +
277 + if (ret < 0) {
278 + return ret;
279 + }
280 + }
281 +
282 + return n;
283 +}
284 +
285 +static u32 rt_i2c_func(struct i2c_adapter *a)
286 +{
287 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
288 +}
289 +
290 +static const struct i2c_algorithm rt_i2c_algo = {
291 + .master_xfer = rt_i2c_master_xfer,
292 + .functionality = rt_i2c_func,
293 +};
294 +
295 +static int rt_i2c_probe(struct platform_device *pdev)
296 +{
297 + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
298 + int ret;
299 +
300 + if (!res) {
301 + dev_err(&pdev->dev, "no memory resource found\n");
302 + return -ENODEV;
303 + }
304 +
305 + adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
306 + if (!adapter) {
307 + dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
308 + return -ENOMEM;
309 + }
310 +
311 + membase = devm_request_and_ioremap(&pdev->dev, res);
312 + if (IS_ERR(membase))
313 + return PTR_ERR(membase);
314 +
315 + strlcpy(adapter->name, dev_name(&pdev->dev), sizeof(adapter->name));
316 + adapter->owner = THIS_MODULE;
317 + adapter->nr = pdev->id;
318 + adapter->timeout = HZ;
319 + adapter->algo = &rt_i2c_algo;
320 + adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
321 + adapter->dev.parent = &pdev->dev;
322 + adapter->dev.of_node = pdev->dev.of_node;
323 +
324 + ret = i2c_add_numbered_adapter(adapter);
325 + if (ret)
326 + return ret;
327 +
328 + of_i2c_register_devices(adapter);
329 +
330 + platform_set_drvdata(pdev, adapter);
331 +
332 + dev_info(&pdev->dev, "loaded\n");
333 +
334 + return 0;
335 +}
336 +
337 +static int rt_i2c_remove(struct platform_device *pdev)
338 +{
339 + platform_set_drvdata(pdev, NULL);
340 +
341 + return 0;
342 +}
343 +
344 +static const struct of_device_id i2c_rt_dt_ids[] = {
345 + { .compatible = "ralink,rt2880-i2c", },
346 + { /* sentinel */ }
347 +};
348 +
349 +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
350 +
351 +static struct platform_driver rt_i2c_driver = {
352 + .probe = rt_i2c_probe,
353 + .remove = rt_i2c_remove,
354 + .driver = {
355 + .owner = THIS_MODULE,
356 + .name = "i2c-ralink",
357 + .of_match_table = i2c_rt_dt_ids,
358 + },
359 +};
360 +
361 +static int __init i2c_rt_init (void)
362 +{
363 + return platform_driver_register(&rt_i2c_driver);
364 +}
365 +subsys_initcall(i2c_rt_init);
366 +
367 +static void __exit i2c_rt_exit (void)
368 +{
369 + platform_driver_unregister(&rt_i2c_driver);
370 +}
371 +
372 +module_exit (i2c_rt_exit);
373 +
374 +MODULE_AUTHOR("Steven Liu <steven_liu@mediatek.com>");
375 +MODULE_DESCRIPTION("Ralink I2c host driver");
376 +MODULE_LICENSE("GPL");
377 +MODULE_ALIAS("platform:Ralink-I2C");