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
6 Signed-off-by: John Crispin <blogic@openwrt.org>
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
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
21 +I2C for Ralink platforms
23 +Required properties :
24 +- compatible : Must be "link,rt3052-i2c"
25 +- reg: physical base address of the controller and length of memory mapped
27 +- #address-cells = <1>;
31 +- Child nodes conforming to i2c bus binding
37 + compatible = "link,rt3052-i2c";
38 + reg = <0x900 0x100>;
39 + #address-cells = <1>;
43 + compatible = "national,lm92";
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
53 This driver can also be built as a module. If so, the module
54 will be called i2c-riic.
57 + tristate "Ralink I2C Controller"
60 config HAVE_S3C2410_I2C
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
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
81 + * drivers/i2c/busses/i2c-ralink.c
83 + * Copyright (C) 2013 Steven Liu <steven_liu@mediatek.com>
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.
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.
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>
109 +#include <asm/mach-ralink/ralink_regs.h>
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
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)
127 +#define I2C_DEVADLEN_7 (6 << 2)
128 +#define I2C_ADDRDIS BIT(1)
130 +#define I2C_RETRY 0x400
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).
135 +#define READ_CMD 0x01
136 +#define WRITE_CMD 0x00
137 +#define READ_BLOCK 64
139 +static void __iomem *membase;
140 +static struct i2c_adapter *adapter;
142 +static void rt_i2c_w32(u32 val, unsigned reg)
144 + iowrite32(val, membase + reg);
147 +static u32 rt_i2c_r32(unsigned reg)
149 + return ioread32(membase + reg);
152 +static inline int rt_i2c_wait_rx_done(void)
154 + int retries = I2C_RETRY;
159 + } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
161 + return (retries < 0);
164 +static inline int rt_i2c_wait_idle(void)
166 + int retries = I2C_RETRY;
171 + } while(rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY);
173 + return (retries < 0);
176 +static inline int rt_i2c_wait_tx_done(void)
178 + int retries = I2C_RETRY;
183 + } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
185 + return (retries < 0);
188 +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
190 + int i = 0, j = 0, pos = 0;
191 + int nblock = msg->len / READ_BLOCK;
192 + int rem = msg->len % READ_BLOCK;
194 + if (msg->flags & I2C_M_TEN) {
195 + printk("10 bits addr not supported\n");
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())
207 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
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())
217 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
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())
233 +static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
238 + if (rt_i2c_wait_idle()) {
239 + printk("i2c transfer failed\n");
243 + device_reset(a->dev.parent);
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);
249 + for (i = 0; i < n && !ret; i++)
250 + ret = rt_i2c_handle_msg(a, &m[i]);
253 + printk("i2c transfer failed\n");
260 +static u32 rt_i2c_func(struct i2c_adapter *a)
262 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
265 +static const struct i2c_algorithm rt_i2c_algo = {
266 + .master_xfer = rt_i2c_master_xfer,
267 + .functionality = rt_i2c_func,
270 +static int rt_i2c_probe(struct platform_device *pdev)
272 + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
276 + dev_err(&pdev->dev, "no memory resource found\n");
280 + adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
282 + dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
286 + membase = devm_request_and_ioremap(&pdev->dev, res);
287 + if (IS_ERR(membase))
288 + return PTR_ERR(membase);
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;
299 + ret = i2c_add_numbered_adapter(adapter);
303 + platform_set_drvdata(pdev, adapter);
305 + dev_info(&pdev->dev, "loaded\n");
310 +static int rt_i2c_remove(struct platform_device *pdev)
312 + platform_set_drvdata(pdev, NULL);
317 +static const struct of_device_id i2c_rt_dt_ids[] = {
318 + { .compatible = "ralink,rt2880-i2c", },
322 +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
324 +static struct platform_driver rt_i2c_driver = {
325 + .probe = rt_i2c_probe,
326 + .remove = rt_i2c_remove,
328 + .owner = THIS_MODULE,
329 + .name = "i2c-ralink",
330 + .of_match_table = i2c_rt_dt_ids,
334 +static int __init i2c_rt_init (void)
336 + return platform_driver_register(&rt_i2c_driver);
338 +subsys_initcall(i2c_rt_init);
340 +static void __exit i2c_rt_exit (void)
342 + platform_driver_unregister(&rt_i2c_driver);
345 +module_exit (i2c_rt_exit);
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");