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.18/Documentation/devicetree/bindings/i2c/i2c-ralink.txt
17 ===================================================================
18 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
19 +++ linux-3.14.18/Documentation/devicetree/bindings/i2c/i2c-ralink.txt 2014-10-06 14:28:14.296590159 +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.18/drivers/i2c/busses/Kconfig
49 ===================================================================
50 --- linux-3.14.18.orig/drivers/i2c/busses/Kconfig 2014-09-06 01:34:59.000000000 +0200
51 +++ linux-3.14.18/drivers/i2c/busses/Kconfig 2014-10-06 14:28:14.296590159 +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.18/drivers/i2c/busses/Makefile
64 ===================================================================
65 --- linux-3.14.18.orig/drivers/i2c/busses/Makefile 2014-09-06 01:34:59.000000000 +0200
66 +++ linux-3.14.18/drivers/i2c/busses/Makefile 2014-10-06 14:28:14.296590159 +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.18/drivers/i2c/busses/i2c-ralink.c
76 ===================================================================
77 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
78 +++ linux-3.14.18/drivers/i2c/busses/i2c-ralink.c 2014-10-06 15:53:32.416500362 +0200
81 + * drivers/i2c/busses/i2c-ralink.c
83 + * Copyright (C) 2013 Steven Liu <steven_liu@mediatek.com>
85 + * Improve driver for i2cdetect from i2c-tools to detect i2c devices on the bus.
86 + * (C) 2014 Sittisak <sittisaks@hotmail.com>
88 + * This software is licensed under the terms of the GNU General Public
89 + * License version 2, as published by the Free Software Foundation, and
90 + * may be copied, distributed, and modified under those terms.
92 + * This program is distributed in the hope that it will be useful,
93 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
94 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95 + * GNU General Public License for more details.
99 +#include <linux/interrupt.h>
100 +#include <linux/kernel.h>
101 +#include <linux/module.h>
102 +#include <linux/reset.h>
103 +#include <linux/delay.h>
104 +#include <linux/slab.h>
105 +#include <linux/init.h>
106 +#include <linux/errno.h>
107 +#include <linux/platform_device.h>
108 +#include <linux/i2c.h>
109 +#include <linux/io.h>
110 +#include <linux/of_i2c.h>
111 +#include <linux/err.h>
113 +#include <asm/mach-ralink/ralink_regs.h>
115 +#define REG_CONFIG_REG 0x00
116 +#define REG_CLKDIV_REG 0x04
117 +#define REG_DEVADDR_REG 0x08
118 +#define REG_ADDR_REG 0x0C
119 +#define REG_DATAOUT_REG 0x10
120 +#define REG_DATAIN_REG 0x14
121 +#define REG_STATUS_REG 0x18
122 +#define REG_STARTXFR_REG 0x1C
123 +#define REG_BYTECNT_REG 0x20
125 +#define I2C_STARTERR BIT(4)
126 +#define I2C_ACKERR BIT(3)
127 +#define I2C_DATARDY BIT(2)
128 +#define I2C_SDOEMPTY BIT(1)
129 +#define I2C_BUSY BIT(0)
131 +#define I2C_DEVADLEN_7 (6 << 2)
132 +#define I2C_ADDRDIS BIT(1)
134 +#define I2C_RETRY 0x1000
136 +#define CLKDIV_VALUE 200 // clock rate is 40M, 40M / (200*2) = 100k (standard i2c bus rate).
137 +//#define CLKDIV_VALUE 50 // clock rate is 40M, 40M / (50*2) = 400k (fast i2c bus rate).
139 +#define READ_CMD 0x01
140 +#define WRITE_CMD 0x00
141 +#define READ_BLOCK 64
143 +static void __iomem *membase;
144 +static struct i2c_adapter *adapter;
146 +static void rt_i2c_w32(u32 val, unsigned reg)
148 + iowrite32(val, membase + reg);
151 +static u32 rt_i2c_r32(unsigned reg)
153 + return ioread32(membase + reg);
156 +static inline int rt_i2c_get_ack(void)
158 + return (rt_i2c_r32(REG_STATUS_REG) & I2C_ACKERR) ? -EIO : 0;
161 +static inline int rt_i2c_wait_rx_done(void)
163 + int retries = I2C_RETRY;
168 + } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
170 + return (retries < 0) ? -ETIMEDOUT : 0;
173 +static inline int rt_i2c_wait_idle(void)
175 + int retries = I2C_RETRY;
180 + } while(rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY);
182 + return (retries < 0);
185 +static inline int rt_i2c_wait_tx_done(void)
187 + int retries = I2C_RETRY;
192 + } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
194 + return (retries < 0) ? -ETIMEDOUT : 0;
197 +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
199 + int i = 0, j = 0, pos = 0;
200 + int nblock = msg->len / READ_BLOCK;
201 + int rem = msg->len % READ_BLOCK;
203 + /*printk("i2c handle msg len:%d\n",msg->len);*/
204 + if (msg->flags & I2C_M_TEN) {
205 + printk("10 bits addr not supported\n");
209 + if (msg->flags & I2C_M_RD) {
210 + for (i = 0; i < nblock; i++) {
211 + if (rt_i2c_wait_idle()) {
212 + printk("i2c-read line busy\n");
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) ret = rt_i2c_wait_rx_done();
219 + if (rt_i2c_get_ack()<0) ret = rt_i2c_get_ack();
220 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
224 + if (rt_i2c_wait_idle()) {
225 + printk("i2c-read line busy\n");
229 + rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
230 + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
232 + for (i = 0; i < rem; i++) {
233 + if (rt_i2c_wait_rx_done()<0) ret = rt_i2c_wait_rx_done();
234 + if (rt_i2c_get_ack() <0) ret = rt_i2c_get_ack();
236 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
239 + if (rt_i2c_wait_idle()) {
240 + printk("i2c-write line busy\n");
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);
248 + if (rt_i2c_wait_tx_done()<0) ret = rt_i2c_wait_tx_done();
249 + if (rt_i2c_get_ack()<0) ret = rt_i2c_get_ack();
258 +static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
263 + if (rt_i2c_wait_idle()) {
264 + printk("i2c-master_xfer line busy\n");
268 + device_reset(a->dev.parent);
270 + rt_i2c_w32(m->addr, REG_DEVADDR_REG);
271 + rt_i2c_w32(I2C_DEVADLEN_7 | I2C_ADDRDIS, REG_CONFIG_REG);
272 + rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
274 + for (i = 0; i < n && !ret; i++) {
275 + ret = rt_i2c_handle_msg(a, &m[i]);
285 +static u32 rt_i2c_func(struct i2c_adapter *a)
287 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
290 +static const struct i2c_algorithm rt_i2c_algo = {
291 + .master_xfer = rt_i2c_master_xfer,
292 + .functionality = rt_i2c_func,
295 +static int rt_i2c_probe(struct platform_device *pdev)
297 + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
301 + dev_err(&pdev->dev, "no memory resource found\n");
305 + adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
307 + dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
311 + membase = devm_request_and_ioremap(&pdev->dev, res);
312 + if (IS_ERR(membase))
313 + return PTR_ERR(membase);
315 + strlcpy(adapter->name, dev_name(&pdev->dev), sizeof(adapter->name));
316 + adapter->owner = THIS_MODULE;
317 + adapter->nr = pdev->id;
318 + adapter->timeout = HZ;
319 + adapter->algo = &rt_i2c_algo;
320 + adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
321 + adapter->dev.parent = &pdev->dev;
322 + adapter->dev.of_node = pdev->dev.of_node;
324 + ret = i2c_add_numbered_adapter(adapter);
328 + of_i2c_register_devices(adapter);
330 + platform_set_drvdata(pdev, adapter);
332 + dev_info(&pdev->dev, "loaded\n");
337 +static int rt_i2c_remove(struct platform_device *pdev)
339 + platform_set_drvdata(pdev, NULL);
344 +static const struct of_device_id i2c_rt_dt_ids[] = {
345 + { .compatible = "ralink,rt2880-i2c", },
349 +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
351 +static struct platform_driver rt_i2c_driver = {
352 + .probe = rt_i2c_probe,
353 + .remove = rt_i2c_remove,
355 + .owner = THIS_MODULE,
356 + .name = "i2c-ralink",
357 + .of_match_table = i2c_rt_dt_ids,
361 +static int __init i2c_rt_init (void)
363 + return platform_driver_register(&rt_i2c_driver);
365 +subsys_initcall(i2c_rt_init);
367 +static void __exit i2c_rt_exit (void)
369 + platform_driver_unregister(&rt_i2c_driver);
372 +module_exit (i2c_rt_exit);
374 +MODULE_AUTHOR("Steven Liu <steven_liu@mediatek.com>");
375 +MODULE_DESCRIPTION("Ralink I2c host driver");
376 +MODULE_LICENSE("GPL");
377 +MODULE_ALIAS("platform:Ralink-I2C");