kernel: move CONFIG_NET_IP_TUNNEL to generic
[openwrt.git] / target / linux / ramips / patches-3.9 / 0153-i2c-MIPS-adds-ralink-I2C-driver.patch
1 From e5a800fb1dc440ee1e4ca656b2ed3f6e2debecca Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 29 Apr 2013 14:40:43 +0200
4 Subject: [PATCH 153/164] 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.9.8/Documentation/devicetree/bindings/i2c/i2c-ralink.txt
17 ===================================================================
18 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
19 +++ linux-3.9.8/Documentation/devicetree/bindings/i2c/i2c-ralink.txt    2013-07-02 11:36:42.944155612 +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.9.8/drivers/i2c/busses/Kconfig
49 ===================================================================
50 --- linux-3.9.8.orig/drivers/i2c/busses/Kconfig 2013-06-27 19:41:32.000000000 +0200
51 +++ linux-3.9.8/drivers/i2c/busses/Kconfig      2013-07-02 11:36:42.944155612 +0200
52 @@ -628,6 +628,10 @@
53           is necessary for systems where the PXA may be a target on the
54           I2C bus.
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.9.8/drivers/i2c/busses/Makefile
64 ===================================================================
65 --- linux-3.9.8.orig/drivers/i2c/busses/Makefile        2013-06-27 19:41:32.000000000 +0200
66 +++ linux-3.9.8/drivers/i2c/busses/Makefile     2013-07-02 11:36:42.944155612 +0200
67 @@ -62,6 +62,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_S3C2410)      += i2c-s3c2410.o
73  obj-$(CONFIG_I2C_S6000)                += i2c-s6000.o
74  obj-$(CONFIG_I2C_SH7760)       += i2c-sh7760.o
75 Index: linux-3.9.8/drivers/i2c/busses/i2c-ralink.c
76 ===================================================================
77 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
78 +++ linux-3.9.8/drivers/i2c/busses/i2c-ralink.c 2013-07-08 10:56:00.068287905 +0200
79 @@ -0,0 +1,274 @@
80 +/*
81 + * drivers/i2c/busses/i2c-ralink.c
82 + *
83 + * Copyright (C) 2013 Steven Liu <steven_liu@mediatek.com>
84 + *
85 + * This software is licensed under the terms of the GNU General Public
86 + * License version 2, as published by the Free Software Foundation, and
87 + * may be copied, distributed, and modified under those terms.
88 + *
89 + * This program is distributed in the hope that it will be useful,
90 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
91 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
92 + * GNU General Public License for more details.
93 + *
94 + */
95 +
96 +#include <linux/interrupt.h>
97 +#include <linux/kernel.h>
98 +#include <linux/module.h>
99 +#include <linux/reset.h>
100 +#include <linux/delay.h>
101 +#include <linux/slab.h>
102 +#include <linux/init.h>
103 +#include <linux/errno.h>
104 +#include <linux/platform_device.h>
105 +#include <linux/i2c.h>
106 +#include <linux/io.h>
107 +#include <linux/of_i2c.h>
108 +#include <linux/err.h>
109 +
110 +#include <asm/mach-ralink/ralink_regs.h>
111 +
112 +#define REG_CONFIG_REG         0x00
113 +#define REG_CLKDIV_REG         0x04
114 +#define REG_DEVADDR_REG                0x08
115 +#define REG_ADDR_REG           0x0C
116 +#define REG_DATAOUT_REG                0x10
117 +#define REG_DATAIN_REG         0x14
118 +#define REG_STATUS_REG         0x18
119 +#define REG_STARTXFR_REG       0x1C
120 +#define REG_BYTECNT_REG                0x20
121 +
122 +#define I2C_STARTERR           BIT(4)
123 +#define I2C_ACKERR             BIT(3)
124 +#define I2C_DATARDY            BIT(2)
125 +#define I2C_SDOEMPTY           BIT(1)
126 +#define I2C_BUSY               BIT(0)
127 +
128 +#define I2C_DEVADLEN_7         (6 << 2)
129 +#define I2C_ADDRDIS            BIT(1)
130 +
131 +#define I2C_RETRY              0x400
132 +
133 +#define CLKDIV_VALUE           200 // clock rate is 40M, 40M / (200*2) = 100k (standard i2c bus rate).
134 +//#define CLKDIV_VALUE         50 // clock rate is 40M, 40M / (50*2) = 400k (fast i2c bus rate).
135 +
136 +#define READ_CMD               0x01
137 +#define WRITE_CMD              0x00
138 +#define READ_BLOCK              64
139 +
140 +static void __iomem *membase;
141 +static struct i2c_adapter *adapter;
142 +
143 +static void rt_i2c_w32(u32 val, unsigned reg)
144 +{
145 +       iowrite32(val, membase + reg);
146 +}
147 +
148 +static u32 rt_i2c_r32(unsigned reg)
149 +{
150 +       return ioread32(membase + reg);
151 +}
152 +
153 +static inline int rt_i2c_wait_rx_done(void)
154 +{
155 +       int retries = I2C_RETRY;
156 +
157 +       do {
158 +               if (!retries--)
159 +                       break;
160 +       } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
161 +
162 +       return (retries < 0);
163 +}
164 +
165 +static inline int rt_i2c_wait_idle(void)
166 +{
167 +       int retries = I2C_RETRY;
168 +
169 +       do {
170 +               if (!retries--)
171 +                       break;
172 +       } while(rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY);
173 +
174 +       return (retries < 0);
175 +}
176 +
177 +static inline int rt_i2c_wait_tx_done(void)
178 +{
179 +       int retries = I2C_RETRY;
180 +
181 +       do {
182 +               if (!retries--)
183 +                       break;
184 +       } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
185 +
186 +       return (retries < 0);
187 +}
188 +
189 +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
190 +{
191 +       int i = 0, j = 0, pos = 0;
192 +       int nblock = msg->len / READ_BLOCK;
193 +        int rem = msg->len % READ_BLOCK;
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 +                       rt_i2c_wait_idle();
203 +                       rt_i2c_w32(READ_BLOCK - 1, REG_BYTECNT_REG);
204 +                       rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
205 +                       for (j = 0; j < READ_BLOCK; j++) {
206 +                               if (rt_i2c_wait_rx_done())
207 +                                       return -1;
208 +                               msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
209 +                       }
210 +               }
211 +
212 +               rt_i2c_wait_idle();
213 +               rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
214 +               rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
215 +               for (i = 0; i < rem; i++) {
216 +                       if (rt_i2c_wait_rx_done())
217 +                               return -1;
218 +                       msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
219 +               }
220 +       } else {
221 +               rt_i2c_wait_idle();
222 +               rt_i2c_w32(msg->len - 1, REG_BYTECNT_REG);
223 +               for (i = 0; i < msg->len; i++) {
224 +                       rt_i2c_w32(msg->buf[i], REG_DATAOUT_REG);
225 +                       rt_i2c_w32(WRITE_CMD, REG_STARTXFR_REG);
226 +                       if (rt_i2c_wait_tx_done())
227 +                               return -1;
228 +               }
229 +       }
230 +
231 +       return 0;
232 +}
233 +
234 +static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
235 +{
236 +       int i = 0;
237 +       int ret = 0;
238 +
239 +       if (rt_i2c_wait_idle()) {
240 +               printk("i2c transfer failed\n");
241 +               return 0;
242 +       }
243 +
244 +       device_reset(a->dev.parent);
245 +
246 +       rt_i2c_w32(m->addr, REG_DEVADDR_REG);
247 +       rt_i2c_w32(I2C_DEVADLEN_7 | I2C_ADDRDIS, REG_CONFIG_REG);
248 +       rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
249 +
250 +       for (i = 0; i < n && !ret; i++)
251 +               ret = rt_i2c_handle_msg(a, &m[i]);
252 +
253 +       if (ret) {
254 +               printk("i2c transfer failed\n");
255 +               return 0;
256 +       }
257 +
258 +       return n;
259 +}
260 +
261 +static u32 rt_i2c_func(struct i2c_adapter *a)
262 +{
263 +       return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
264 +}
265 +
266 +static const struct i2c_algorithm rt_i2c_algo = {
267 +       .master_xfer    = rt_i2c_master_xfer,
268 +       .functionality  = rt_i2c_func,
269 +};
270 +
271 +static int rt_i2c_probe(struct platform_device *pdev)
272 +{
273 +       struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
274 +       int ret;
275 +
276 +       if (!res) {
277 +               dev_err(&pdev->dev, "no memory resource found\n");
278 +               return -ENODEV;
279 +       }
280 +
281 +       adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
282 +       if (!adapter) {
283 +               dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
284 +               return -ENOMEM;
285 +       }
286 +
287 +       membase = devm_request_and_ioremap(&pdev->dev, res);
288 +       if (IS_ERR(membase))
289 +               return PTR_ERR(membase);
290 +
291 +       strlcpy(adapter->name, dev_name(&pdev->dev), sizeof(adapter->name));
292 +       adapter->owner = THIS_MODULE;
293 +       adapter->nr = pdev->id;
294 +       adapter->timeout = HZ;
295 +       adapter->algo = &rt_i2c_algo;
296 +       adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
297 +       adapter->dev.parent = &pdev->dev;
298 +       adapter->dev.of_node = pdev->dev.of_node;
299 +
300 +       ret = i2c_add_numbered_adapter(adapter);
301 +       if (ret)
302 +               return ret;
303 +
304 +       of_i2c_register_devices(adapter);
305 +
306 +       platform_set_drvdata(pdev, adapter);
307 +
308 +       dev_info(&pdev->dev, "loaded\n");
309 +
310 +       return 0;
311 +}
312 +
313 +static int rt_i2c_remove(struct platform_device *pdev)
314 +{
315 +       platform_set_drvdata(pdev, NULL);
316 +
317 +       return 0;
318 +}
319 +
320 +static const struct of_device_id i2c_rt_dt_ids[] = {
321 +       { .compatible = "ralink,rt2880-i2c", },
322 +       { /* sentinel */ }
323 +};
324 +
325 +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
326 +
327 +static struct platform_driver rt_i2c_driver = {
328 +       .probe          = rt_i2c_probe,
329 +       .remove         = rt_i2c_remove,
330 +       .driver         = {
331 +               .owner  = THIS_MODULE,
332 +               .name   = "i2c-ralink",
333 +               .of_match_table = i2c_rt_dt_ids,
334 +       },
335 +};
336 +
337 +static int __init i2c_rt_init (void)
338 +{
339 +       return platform_driver_register(&rt_i2c_driver);
340 +}
341 +subsys_initcall(i2c_rt_init);
342 +
343 +static void __exit i2c_rt_exit (void)
344 +{
345 +       platform_driver_unregister(&rt_i2c_driver);
346 +}
347 +
348 +module_exit (i2c_rt_exit);
349 +
350 +MODULE_AUTHOR("Steven Liu <steven_liu@mediatek.com>");
351 +MODULE_DESCRIPTION("Ralink I2c host driver");
352 +MODULE_LICENSE("GPL");
353 +MODULE_ALIAS("platform:Ralink-I2C");