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