ramips: unbreak i2c for 3.14
[openwrt/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.16/Documentation/devicetree/bindings/i2c/i2c-ralink.txt
17 ===================================================================
18 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
19 +++ linux-3.14.16/Documentation/devicetree/bindings/i2c/i2c-ralink.txt 2014-08-24 16:03:20.586671002 +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.16/drivers/i2c/busses/Kconfig
49 ===================================================================
50 --- linux-3.14.16.orig/drivers/i2c/busses/Kconfig 2014-08-08 01:50:59.000000000 +0200
51 +++ linux-3.14.16/drivers/i2c/busses/Kconfig 2014-08-24 16:03:20.586671002 +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.16/drivers/i2c/busses/Makefile
64 ===================================================================
65 --- linux-3.14.16.orig/drivers/i2c/busses/Makefile 2014-08-08 01:50:59.000000000 +0200
66 +++ linux-3.14.16/drivers/i2c/busses/Makefile 2014-08-24 16:03:20.586671002 +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.16/drivers/i2c/busses/i2c-ralink.c
76 ===================================================================
77 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
78 +++ linux-3.14.16/drivers/i2c/busses/i2c-ralink.c 2014-08-27 23:07:01.318731261 +0200
79 @@ -0,0 +1,271 @@
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/err.h>
108 +
109 +#include <asm/mach-ralink/ralink_regs.h>
110 +
111 +#define REG_CONFIG_REG 0x00
112 +#define REG_CLKDIV_REG 0x04
113 +#define REG_DEVADDR_REG 0x08
114 +#define REG_ADDR_REG 0x0C
115 +#define REG_DATAOUT_REG 0x10
116 +#define REG_DATAIN_REG 0x14
117 +#define REG_STATUS_REG 0x18
118 +#define REG_STARTXFR_REG 0x1C
119 +#define REG_BYTECNT_REG 0x20
120 +
121 +#define I2C_STARTERR BIT(4)
122 +#define I2C_ACKERR BIT(3)
123 +#define I2C_DATARDY BIT(2)
124 +#define I2C_SDOEMPTY BIT(1)
125 +#define I2C_BUSY BIT(0)
126 +
127 +#define I2C_DEVADLEN_7 (6 << 2)
128 +#define I2C_ADDRDIS BIT(1)
129 +
130 +#define I2C_RETRY 0x400
131 +
132 +#define CLKDIV_VALUE 200 // clock rate is 40M, 40M / (200*2) = 100k (standard i2c bus rate).
133 +//#define CLKDIV_VALUE 50 // clock rate is 40M, 40M / (50*2) = 400k (fast i2c bus rate).
134 +
135 +#define READ_CMD 0x01
136 +#define WRITE_CMD 0x00
137 +#define READ_BLOCK 64
138 +
139 +static void __iomem *membase;
140 +static struct i2c_adapter *adapter;
141 +
142 +static void rt_i2c_w32(u32 val, unsigned reg)
143 +{
144 + iowrite32(val, membase + reg);
145 +}
146 +
147 +static u32 rt_i2c_r32(unsigned reg)
148 +{
149 + return ioread32(membase + reg);
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);
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);
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 +
194 + if (msg->flags & I2C_M_TEN) {
195 + printk("10 bits addr not supported\n");
196 + return -EINVAL;
197 + }
198 +
199 + if (msg->flags & I2C_M_RD) {
200 + for (i = 0; i < nblock; i++) {
201 + rt_i2c_wait_idle();
202 + rt_i2c_w32(READ_BLOCK - 1, REG_BYTECNT_REG);
203 + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
204 + for (j = 0; j < READ_BLOCK; j++) {
205 + if (rt_i2c_wait_rx_done())
206 + return -1;
207 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
208 + }
209 + }
210 +
211 + rt_i2c_wait_idle();
212 + rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
213 + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
214 + for (i = 0; i < rem; i++) {
215 + if (rt_i2c_wait_rx_done())
216 + return -1;
217 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
218 + }
219 + } else {
220 + rt_i2c_wait_idle();
221 + rt_i2c_w32(msg->len - 1, REG_BYTECNT_REG);
222 + for (i = 0; i < msg->len; i++) {
223 + rt_i2c_w32(msg->buf[i], REG_DATAOUT_REG);
224 + rt_i2c_w32(WRITE_CMD, REG_STARTXFR_REG);
225 + if (rt_i2c_wait_tx_done())
226 + return -1;
227 + }
228 + }
229 +
230 + return 0;
231 +}
232 +
233 +static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
234 +{
235 + int i = 0;
236 + int ret = 0;
237 +
238 + if (rt_i2c_wait_idle()) {
239 + printk("i2c transfer failed\n");
240 + return 0;
241 + }
242 +
243 + device_reset(a->dev.parent);
244 +
245 + rt_i2c_w32(m->addr, REG_DEVADDR_REG);
246 + rt_i2c_w32(I2C_DEVADLEN_7 | I2C_ADDRDIS, REG_CONFIG_REG);
247 + rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
248 +
249 + for (i = 0; i < n && !ret; i++)
250 + ret = rt_i2c_handle_msg(a, &m[i]);
251 +
252 + if (ret) {
253 + printk("i2c transfer failed\n");
254 + return 0;
255 + }
256 +
257 + return n;
258 +}
259 +
260 +static u32 rt_i2c_func(struct i2c_adapter *a)
261 +{
262 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
263 +}
264 +
265 +static const struct i2c_algorithm rt_i2c_algo = {
266 + .master_xfer = rt_i2c_master_xfer,
267 + .functionality = rt_i2c_func,
268 +};
269 +
270 +static int rt_i2c_probe(struct platform_device *pdev)
271 +{
272 + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
273 + int ret;
274 +
275 + if (!res) {
276 + dev_err(&pdev->dev, "no memory resource found\n");
277 + return -ENODEV;
278 + }
279 +
280 + adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
281 + if (!adapter) {
282 + dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
283 + return -ENOMEM;
284 + }
285 +
286 + membase = devm_request_and_ioremap(&pdev->dev, res);
287 + if (IS_ERR(membase))
288 + return PTR_ERR(membase);
289 +
290 + strlcpy(adapter->name, dev_name(&pdev->dev), sizeof(adapter->name));
291 + adapter->owner = THIS_MODULE;
292 + adapter->nr = pdev->id;
293 + adapter->timeout = HZ;
294 + adapter->algo = &rt_i2c_algo;
295 + adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
296 + adapter->dev.parent = &pdev->dev;
297 + adapter->dev.of_node = pdev->dev.of_node;
298 +
299 + ret = i2c_add_numbered_adapter(adapter);
300 + if (ret)
301 + return ret;
302 +
303 + platform_set_drvdata(pdev, adapter);
304 +
305 + dev_info(&pdev->dev, "loaded\n");
306 +
307 + return 0;
308 +}
309 +
310 +static int rt_i2c_remove(struct platform_device *pdev)
311 +{
312 + platform_set_drvdata(pdev, NULL);
313 +
314 + return 0;
315 +}
316 +
317 +static const struct of_device_id i2c_rt_dt_ids[] = {
318 + { .compatible = "ralink,rt2880-i2c", },
319 + { /* sentinel */ }
320 +};
321 +
322 +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
323 +
324 +static struct platform_driver rt_i2c_driver = {
325 + .probe = rt_i2c_probe,
326 + .remove = rt_i2c_remove,
327 + .driver = {
328 + .owner = THIS_MODULE,
329 + .name = "i2c-ralink",
330 + .of_match_table = i2c_rt_dt_ids,
331 + },
332 +};
333 +
334 +static int __init i2c_rt_init (void)
335 +{
336 + return platform_driver_register(&rt_i2c_driver);
337 +}
338 +subsys_initcall(i2c_rt_init);
339 +
340 +static void __exit i2c_rt_exit (void)
341 +{
342 + platform_driver_unregister(&rt_i2c_driver);
343 +}
344 +
345 +module_exit (i2c_rt_exit);
346 +
347 +MODULE_AUTHOR("Steven Liu <steven_liu@mediatek.com>");
348 +MODULE_DESCRIPTION("Ralink I2c host driver");
349 +MODULE_LICENSE("GPL");
350 +MODULE_ALIAS("platform:Ralink-I2C");