29cb52a776ebb4117348b606ff1c1ba5e541a7ee
[openwrt.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-09 19:38:29.242416182 +0200
79 @@ -0,0 +1,299 @@
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/err.h>
111 +
112 +#include <asm/mach-ralink/ralink_regs.h>
113 +
114 +#define REG_CONFIG_REG         0x00
115 +#define REG_CLKDIV_REG         0x04
116 +#define REG_DEVADDR_REG                0x08
117 +#define REG_ADDR_REG           0x0C
118 +#define REG_DATAOUT_REG                0x10
119 +#define REG_DATAIN_REG         0x14
120 +#define REG_STATUS_REG         0x18
121 +#define REG_STARTXFR_REG       0x1C
122 +#define REG_BYTECNT_REG                0x20
123 +
124 +#define I2C_STARTERR           BIT(4)
125 +#define I2C_ACKERR             BIT(3)
126 +#define I2C_DATARDY            BIT(2)
127 +#define I2C_SDOEMPTY           BIT(1)
128 +#define I2C_BUSY               BIT(0)
129 +
130 +#define I2C_DEVADLEN_7         (6 << 2)
131 +#define I2C_ADDRDIS            BIT(1)
132 +
133 +#define I2C_RETRY              0x1000
134 +
135 +#define CLKDIV_VALUE           200 // clock rate is 40M, 40M / (200*2) = 100k (standard i2c bus rate).
136 +//#define CLKDIV_VALUE         50 // clock rate is 40M, 40M / (50*2) = 400k (fast i2c bus rate).
137 +
138 +#define READ_CMD               0x01
139 +#define WRITE_CMD              0x00
140 +#define READ_BLOCK              64
141 +
142 +static void __iomem *membase;
143 +static struct i2c_adapter *adapter;
144 +
145 +static void rt_i2c_w32(u32 val, unsigned reg)
146 +{
147 +       iowrite32(val, membase + reg);
148 +}
149 +
150 +static u32 rt_i2c_r32(unsigned reg)
151 +{
152 +       return ioread32(membase + reg);
153 +}
154 +
155 +static inline int rt_i2c_get_ack(void)
156 +{
157 +        return (rt_i2c_r32(REG_STATUS_REG) & I2C_ACKERR) ? -EIO : 0;
158 +}
159 +
160 +static inline int rt_i2c_wait_rx_done(void)
161 +{
162 +       int retries = I2C_RETRY;
163 +
164 +       do {
165 +               if (!retries--)
166 +                       break;
167 +       } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
168 +
169 +       return (retries < 0) ? -ETIMEDOUT : 0;
170 +}
171 +
172 +static inline int rt_i2c_wait_idle(void)
173 +{
174 +       int retries = I2C_RETRY;
175 +
176 +       do {
177 +               if (!retries--)
178 +                       break;
179 +       } while(rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY);
180 +
181 +       return (retries < 0);
182 +}
183 +
184 +static inline int rt_i2c_wait_tx_done(void)
185 +{
186 +       int retries = I2C_RETRY;
187 +
188 +       do {
189 +               if (!retries--)
190 +                       break;
191 +       } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
192 +
193 +       return (retries < 0) ? -ETIMEDOUT : 0;
194 +}
195 +
196 +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
197 +{
198 +       int i = 0, j = 0, pos = 0;
199 +       int nblock = msg->len / READ_BLOCK;
200 +        int rem = msg->len % READ_BLOCK;
201 +       int ret = 0;
202 +
203 +       if (msg->flags & I2C_M_TEN) {
204 +               printk("10 bits addr not supported\n");
205 +               return -EINVAL;
206 +       }
207 +
208 +       if (msg->flags & I2C_M_RD) {
209 +               for (i = 0; i < nblock; i++) {
210 +                       if (rt_i2c_wait_idle()) {
211 +                           printk("i2c-read line busy\n");
212 +                               return -ETIMEDOUT;
213 +                       }
214 +                       rt_i2c_w32(READ_BLOCK - 1, REG_BYTECNT_REG);
215 +                       rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
216 +                       for (j = 0; j < READ_BLOCK; j++) {
217 +                               if (rt_i2c_wait_rx_done() < 0)
218 +                                       ret = rt_i2c_wait_rx_done();
219 +                                if (rt_i2c_get_ack() < 0)
220 +                                       ret = rt_i2c_get_ack();
221 +                               msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
222 +                       }
223 +               }
224 +
225 +               if (rt_i2c_wait_idle()) {
226 +                       printk("i2c-read line busy\n");
227 +                       return -ETIMEDOUT;
228 +               }
229 +               if (rem) {
230 +                       rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
231 +                       rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
232 +               }
233 +               for (i = 0; i < rem; i++) {
234 +                       if (rt_i2c_wait_rx_done() < 0)
235 +                               ret = rt_i2c_wait_rx_done();
236 +                        if (rt_i2c_get_ack() < 0)
237 +                               ret = rt_i2c_get_ack();
238 +
239 +                       msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
240 +               }
241 +       } else {
242 +               if (rt_i2c_wait_idle()) {
243 +                       printk("i2c-write line busy\n");
244 +                       return -ETIMEDOUT;
245 +               }
246 +               rt_i2c_w32(msg->len - 1, REG_BYTECNT_REG);
247 +               for (i = 0; i < msg->len; i++) {
248 +                       rt_i2c_w32(msg->buf[i], REG_DATAOUT_REG);
249 +                       rt_i2c_w32(WRITE_CMD, REG_STARTXFR_REG);
250 +
251 +                       if (rt_i2c_wait_tx_done() < 0)
252 +                               ret = rt_i2c_wait_tx_done();
253 +                        if (rt_i2c_get_ack() < 0)
254 +                               ret = rt_i2c_get_ack();
255 +               }
256 +       }
257 +
258 +       return ret;
259 +}
260 +
261 +static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
262 +{
263 +       int i = 0;
264 +       int ret = 0;
265 +
266 +       if (rt_i2c_wait_idle()) {
267 +               printk("i2c-master_xfer line busy\n");
268 +               return -ETIMEDOUT;
269 +       }
270 +
271 +       device_reset(a->dev.parent);
272 +
273 +       rt_i2c_w32(m->addr, REG_DEVADDR_REG);
274 +       rt_i2c_w32(I2C_DEVADLEN_7 | I2C_ADDRDIS, REG_CONFIG_REG);
275 +       rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
276 +
277 +       for (i = 0; i < n && !ret; i++) {
278 +               ret = rt_i2c_handle_msg(a, &m[i]);
279 +
280 +               if (ret < 0) {
281 +                       return ret;
282 +               }
283 +       }
284 +
285 +       return n;
286 +}
287 +
288 +static u32 rt_i2c_func(struct i2c_adapter *a)
289 +{
290 +       return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
291 +}
292 +
293 +static const struct i2c_algorithm rt_i2c_algo = {
294 +       .master_xfer    = rt_i2c_master_xfer,
295 +       .functionality  = rt_i2c_func,
296 +};
297 +
298 +static int rt_i2c_probe(struct platform_device *pdev)
299 +{
300 +       struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
301 +       int ret;
302 +
303 +       if (!res) {
304 +               dev_err(&pdev->dev, "no memory resource found\n");
305 +               return -ENODEV;
306 +       }
307 +
308 +       adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
309 +       if (!adapter) {
310 +               dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
311 +               return -ENOMEM;
312 +       }
313 +
314 +       membase = devm_request_and_ioremap(&pdev->dev, res);
315 +       if (IS_ERR(membase))
316 +               return PTR_ERR(membase);
317 +
318 +       strlcpy(adapter->name, dev_name(&pdev->dev), sizeof(adapter->name));
319 +       adapter->owner = THIS_MODULE;
320 +       adapter->nr = pdev->id;
321 +       adapter->timeout = HZ;
322 +       adapter->algo = &rt_i2c_algo;
323 +       adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
324 +       adapter->dev.parent = &pdev->dev;
325 +       adapter->dev.of_node = pdev->dev.of_node;
326 +
327 +       ret = i2c_add_numbered_adapter(adapter);
328 +       if (ret)
329 +               return ret;
330 +
331 +       platform_set_drvdata(pdev, adapter);
332 +
333 +       dev_info(&pdev->dev, "loaded\n");
334 +
335 +       return 0;
336 +}
337 +
338 +static int rt_i2c_remove(struct platform_device *pdev)
339 +{
340 +       platform_set_drvdata(pdev, NULL);
341 +
342 +       return 0;
343 +}
344 +
345 +static const struct of_device_id i2c_rt_dt_ids[] = {
346 +       { .compatible = "ralink,rt2880-i2c", },
347 +       { /* sentinel */ }
348 +};
349 +
350 +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
351 +
352 +static struct platform_driver rt_i2c_driver = {
353 +       .probe          = rt_i2c_probe,
354 +       .remove         = rt_i2c_remove,
355 +       .driver         = {
356 +               .owner  = THIS_MODULE,
357 +               .name   = "i2c-ralink",
358 +               .of_match_table = i2c_rt_dt_ids,
359 +       },
360 +};
361 +
362 +static int __init i2c_rt_init (void)
363 +{
364 +       return platform_driver_register(&rt_i2c_driver);
365 +}
366 +subsys_initcall(i2c_rt_init);
367 +
368 +static void __exit i2c_rt_exit (void)
369 +{
370 +       platform_driver_unregister(&rt_i2c_driver);
371 +}
372 +
373 +module_exit (i2c_rt_exit);
374 +
375 +MODULE_AUTHOR("Steven Liu <steven_liu@mediatek.com>");
376 +MODULE_DESCRIPTION("Ralink I2c host driver");
377 +MODULE_LICENSE("GPL");
378 +MODULE_ALIAS("platform:Ralink-I2C");