kernel: update 3.18 to 3.18.21
[openwrt.git] / target / linux / ramips / patches-3.18 / 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 @@ -711,6 +711,10 @@ config I2C_RK3X
49           This driver can also be built as a module. If so, the module will
50           be called i2c-rk3x.
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 @@ -66,6 +66,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_QUP)          += i2c-qup.o
67  obj-$(CONFIG_I2C_RIIC)         += i2c-riic.o
68  obj-$(CONFIG_I2C_RK3X)         += i2c-rk3x.o
69 --- /dev/null
70 +++ b/drivers/i2c/busses/i2c-ralink.c
71 @@ -0,0 +1,302 @@
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 CLKDIV_VALUE           200 // clock rate is 40M, 40M / (200*2) = 100k (standard i2c bus rate).
126 +//#define CLKDIV_VALUE         50 // clock rate is 40M, 40M / (50*2) = 400k (fast i2c bus rate).
127 +
128 +#define READ_CMD               0x01
129 +#define WRITE_CMD              0x00
130 +#define READ_BLOCK              64
131 +
132 +/* timeout waiting for I2C devices to respond (clock streching) */
133 +#define RT_I2C_TIMEOUT (msecs_to_jiffies(1000))
134 +
135 +static void __iomem *membase;
136 +static struct i2c_adapter *adapter;
137 +
138 +static void rt_i2c_w32(u32 val, unsigned reg)
139 +{
140 +       iowrite32(val, membase + reg);
141 +}
142 +
143 +static u32 rt_i2c_r32(unsigned reg)
144 +{
145 +       return ioread32(membase + reg);
146 +}
147 +
148 +static inline int rt_i2c_get_ack(void)
149 +{
150 +        return (rt_i2c_r32(REG_STATUS_REG) & I2C_ACKERR) ? -EIO : 0;
151 +}
152 +
153 +static inline int rt_i2c_wait_rx_done(void)
154 +{
155 +       unsigned long timeout;
156 +
157 +       timeout = jiffies + RT_I2C_TIMEOUT;
158 +
159 +       do {
160 +               if (time_after(jiffies, timeout))
161 +                       return (-ETIMEDOUT);
162 +
163 +       } while (!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
164 +
165 +       return 0;
166 +}
167 +
168 +static inline int rt_i2c_wait_idle(void)
169 +{
170 +       unsigned long timeout;
171 +
172 +       timeout = jiffies + RT_I2C_TIMEOUT;
173 +
174 +       do {
175 +               if (time_after(jiffies, timeout)) {
176 +                       printk("i2c-read line busy\n");
177 +                       return 1;
178 +               }
179 +       } while (rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY);
180 +
181 +       return 0;
182 +}
183 +
184 +static inline int rt_i2c_wait_tx_done(void)
185 +{
186 +       unsigned long timeout;
187 +
188 +       timeout = jiffies + RT_I2C_TIMEOUT;
189 +
190 +       do {
191 +               if (time_after(jiffies, timeout))
192 +                       return (-ETIMEDOUT);
193 +
194 +       } while (!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
195 +
196 +       return 0;
197 +}
198 +
199 +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
200 +{
201 +       int i = 0, j = 0, pos = 0;
202 +       int nblock = msg->len / READ_BLOCK;
203 +        int rem = msg->len % READ_BLOCK;
204 +       int ret = 0;
205 +
206 +       if (msg->flags & I2C_M_TEN) {
207 +               printk("10 bits addr not supported\n");
208 +               return -EINVAL;
209 +       }
210 +
211 +       if (msg->flags & I2C_M_RD) {
212 +               for (i = 0; i < nblock; i++) {
213 +                       if (rt_i2c_wait_idle())
214 +                               return -ETIMEDOUT;
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)
219 +                                       ret = rt_i2c_wait_rx_done();
220 +                                if (rt_i2c_get_ack() < 0)
221 +                                       ret = rt_i2c_get_ack();
222 +                               msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
223 +                       }
224 +               }
225 +
226 +               if (rt_i2c_wait_idle())
227 +                       return -ETIMEDOUT;
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)
234 +                               ret = rt_i2c_wait_rx_done();
235 +                        if (rt_i2c_get_ack() < 0)
236 +                               ret = rt_i2c_get_ack();
237 +
238 +                       msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
239 +               }
240 +       } else {
241 +               if (rt_i2c_wait_idle())
242 +                       return -ETIMEDOUT;
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)
249 +                               ret = rt_i2c_wait_tx_done();
250 +                        if (rt_i2c_get_ack() < 0)
251 +                               ret = rt_i2c_get_ack();
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 +               return -ETIMEDOUT;
265 +
266 +       device_reset(a->dev.parent);
267 +
268 +       rt_i2c_w32(m->addr, REG_DEVADDR_REG);
269 +       rt_i2c_w32(I2C_DEVADLEN_7 | I2C_ADDRDIS, REG_CONFIG_REG);
270 +       rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
271 +
272 +       for (i = 0; i < n && !ret; i++) {
273 +               ret = rt_i2c_handle_msg(a, &m[i]);
274 +
275 +               if (ret < 0) {
276 +                       return ret;
277 +               }
278 +       }
279 +
280 +       return n;
281 +}
282 +
283 +static u32 rt_i2c_func(struct i2c_adapter *a)
284 +{
285 +       return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
286 +}
287 +
288 +static const struct i2c_algorithm rt_i2c_algo = {
289 +       .master_xfer    = rt_i2c_master_xfer,
290 +       .functionality  = rt_i2c_func,
291 +};
292 +
293 +static int rt_i2c_probe(struct platform_device *pdev)
294 +{
295 +       struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
296 +       int ret;
297 +
298 +       if (!res) {
299 +               dev_err(&pdev->dev, "no memory resource found\n");
300 +               return -ENODEV;
301 +       }
302 +
303 +       adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
304 +       if (!adapter) {
305 +               dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
306 +               return -ENOMEM;
307 +       }
308 +
309 +       membase = devm_ioremap_resource(&pdev->dev, res);
310 +       if (IS_ERR(membase))
311 +               return PTR_ERR(membase);
312 +
313 +       strlcpy(adapter->name, dev_name(&pdev->dev), sizeof(adapter->name));
314 +       adapter->owner = THIS_MODULE;
315 +       adapter->nr = pdev->id;
316 +       adapter->timeout = HZ;
317 +       adapter->algo = &rt_i2c_algo;
318 +       adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
319 +       adapter->dev.parent = &pdev->dev;
320 +       adapter->dev.of_node = pdev->dev.of_node;
321 +
322 +       ret = i2c_add_numbered_adapter(adapter);
323 +       if (ret)
324 +               return ret;
325 +
326 +       platform_set_drvdata(pdev, adapter);
327 +
328 +       dev_info(&pdev->dev, "loaded\n");
329 +
330 +       return 0;
331 +}
332 +
333 +static int rt_i2c_remove(struct platform_device *pdev)
334 +{
335 +       platform_set_drvdata(pdev, NULL);
336 +
337 +       return 0;
338 +}
339 +
340 +static const struct of_device_id i2c_rt_dt_ids[] = {
341 +       { .compatible = "ralink,rt2880-i2c", },
342 +       { /* sentinel */ }
343 +};
344 +
345 +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
346 +
347 +static struct platform_driver rt_i2c_driver = {
348 +       .probe          = rt_i2c_probe,
349 +       .remove         = rt_i2c_remove,
350 +       .driver         = {
351 +               .owner  = THIS_MODULE,
352 +               .name   = "i2c-ralink",
353 +               .of_match_table = i2c_rt_dt_ids,
354 +       },
355 +};
356 +
357 +static int __init i2c_rt_init (void)
358 +{
359 +       return platform_driver_register(&rt_i2c_driver);
360 +}
361 +subsys_initcall(i2c_rt_init);
362 +
363 +static void __exit i2c_rt_exit (void)
364 +{
365 +       platform_driver_unregister(&rt_i2c_driver);
366 +}
367 +
368 +module_exit (i2c_rt_exit);
369 +
370 +MODULE_AUTHOR("Steven Liu <steven_liu@mediatek.com>");
371 +MODULE_DESCRIPTION("Ralink I2c host driver");
372 +MODULE_LICENSE("GPL");
373 +MODULE_ALIAS("platform:Ralink-I2C");