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