1 Index: linux-3.14.30/drivers/i2c/busses/Kconfig
2 ===================================================================
3 --- linux-3.14.30.orig/drivers/i2c/busses/Kconfig 2015-03-11 16:46:46.963996212 +0800
4 +++ linux-3.14.30/drivers/i2c/busses/Kconfig 2015-03-12 10:40:34.849574512 +0800
6 tristate "Ralink I2C Controller"
10 + tristate "Mt7621 I2C Controller"
13 config HAVE_S3C2410_I2C
16 Index: linux-3.14.30/drivers/i2c/busses/Makefile
17 ===================================================================
18 --- linux-3.14.30.orig/drivers/i2c/busses/Makefile 2015-03-11 16:46:46.967996212 +0800
19 +++ linux-3.14.30/drivers/i2c/busses/Makefile 2015-03-11 17:52:43.008121712 +0800
21 obj-$(CONFIG_I2C_PXA) += i2c-pxa.o
22 obj-$(CONFIG_I2C_PXA_PCI) += i2c-pxa-pci.o
23 obj-$(CONFIG_I2C_RALINK) += i2c-ralink.o
24 +obj-$(CONFIG_I2C_MT7621) += i2c-mt7621.o
25 obj-$(CONFIG_I2C_RIIC) += i2c-riic.o
26 obj-$(CONFIG_I2C_S3C2410) += i2c-s3c2410.o
27 obj-$(CONFIG_I2C_S6000) += i2c-s6000.o
28 Index: linux-3.14.30/drivers/i2c/busses/i2c-mt7621.c
29 ===================================================================
30 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
31 +++ linux-3.14.30/drivers/i2c/busses/i2c-mt7621.c 2015-03-12 11:45:00.449759139 +0800
34 + * drivers/i2c/busses/i2c-mt7621.c
36 + * Copyright (C) 2013 Steven Liu <steven_liu@mediatek.com>
38 + * Improve driver for i2cdetect from i2c-tools to detect i2c devices on the bus.
39 + * (C) 2014 Sittisak <sittisaks@hotmail.com>
41 + * This software is licensed under the terms of the GNU General Public
42 + * License version 2, as published by the Free Software Foundation, and
43 + * may be copied, distributed, and modified under those terms.
45 + * This program is distributed in the hope that it will be useful,
46 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 + * GNU General Public License for more details.
52 +#include <linux/interrupt.h>
53 +#include <linux/kernel.h>
54 +#include <linux/module.h>
55 +#include <linux/reset.h>
56 +#include <linux/delay.h>
57 +#include <linux/slab.h>
58 +#include <linux/init.h>
59 +#include <linux/errno.h>
60 +#include <linux/platform_device.h>
61 +#include <linux/i2c.h>
62 +#include <linux/io.h>
63 +#include <linux/err.h>
65 +#include <asm/mach-ralink/ralink_regs.h>
67 +#define REG_CONFIG_REG 0x00
68 +#define REG_CLKDIV_REG 0x04
69 +#define REG_DEVADDR_REG 0x08
70 +#define REG_ADDR_REG 0x0C
71 +#define REG_DATAOUT_REG 0x10
72 +#define REG_DATAIN_REG 0x14
73 +#define REG_STATUS_REG 0x18
74 +#define REG_STARTXFR_REG 0x1C
75 +#define REG_BYTECNT_REG 0x20
76 +#define REG_SM0_IS_AUTOMODE 0x28
77 +#define REG_SM0CTL0 0x40
80 +#define I2C_STARTERR 0x10
81 +#define I2C_ACKERR 0x08
82 +#define I2C_DATARDY 0x04
83 +#define I2C_SDOEMPTY 0x02
84 +#define I2C_BUSY 0x01
86 +/* I2C_CFG register bit field */
87 +#define I2C_CFG_ADDRLEN_8 (7<<5) /* 8 bits */
88 +#define I2C_CFG_DEVADLEN_7 (6<<2)
89 +#define I2C_CFG_ADDRDIS (1<<1)
90 +#define I2C_CFG_DEVADDIS (1<<0)
92 +#define I2C_CFG_DEFAULT (I2C_CFG_ADDRLEN_8 | \
93 + I2C_CFG_DEVADLEN_7 | \
96 +#define I2C_RETRY 0x1000
98 +#define CLKDIV_VALUE 333
99 +#define i2c_busy_loop (CLKDIV_VALUE*30)
101 +#define READ_CMD 0x01
102 +#define WRITE_CMD 0x00
103 +#define READ_BLOCK 16
105 +#define I2C_OFFSET 0x900
106 +#define RSTCTRL_OFFSET 0x34
108 +#define MT7621_REG(x) (*((volatile u32 *)(x)))
110 +struct i2c_algo_mt7621_data{
112 + wait_queue_head_t waitq;
116 +/***********************************************************/
118 +static unsigned long clkdiv_value = CLKDIV_VALUE;
120 +static void __iomem *memsysctlbase;
121 +static void __iomem *membase;
122 +static struct i2c_adapter *adapter;
125 +static void rt_i2c_w32(u32 val, unsigned reg)
127 + iowrite32(val, membase + reg);
130 +static u32 rt_i2c_r32(unsigned reg)
132 + return ioread32(membase + reg);
135 +static void mt7621_i2c_reset(void)
138 + val = MT7621_REG(RSTCTRL_OFFSET+memsysctlbase);
139 + val = val | (1<<16);
140 + MT7621_REG(RSTCTRL_OFFSET+memsysctlbase) = val;
141 + val = val & ~(1<<16);
142 + MT7621_REG(RSTCTRL_OFFSET+memsysctlbase) = val;
145 +static void mt7621_i2c_enable(struct i2c_msg *msg)
147 + rt_i2c_w32(msg->addr,REG_DEVADDR_REG);
148 + rt_i2c_w32(0,REG_ADDR_REG);
151 +static void i2c_master_init(void)
154 + /*set mt7621 i2c mode*/
155 + (*((volatile u32*)(memsysctlbase+0x60))) &=~0x4;
157 + mt7621_i2c_reset();
158 + rt_i2c_w32(I2C_CFG_DEFAULT,REG_CONFIG_REG);
162 + value |= clkdiv_value <<16;
165 + rt_i2c_w32(value,REG_SM0CTL0);
166 + rt_i2c_w32(1,REG_SM0_IS_AUTOMODE);//auto mode
170 +static inline int rt_i2c_wait_rx_done(void)
173 + while((!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY)) && (i<i2c_busy_loop))
175 + if(i>=i2c_busy_loop){
176 + pr_err("err,wait for idle timeout");
182 +static inline int rt_i2c_wait_idle(void)
185 + while((rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY) && (i<i2c_busy_loop))
187 + if(i>=i2c_busy_loop){
188 + pr_err("err,wait for idle timeout");
194 +static inline int rt_i2c_wait_tx_done(void)
197 + while((!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY)) && (i<i2c_busy_loop))
199 + if(i>=i2c_busy_loop){
200 + pr_err("err,wait for idle timeout");
206 +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
208 + int i = 0, j = 0, pos = 0;
209 + int nblock = msg->len / READ_BLOCK;
210 + int rem = msg->len % READ_BLOCK;
212 + if (msg->flags & I2C_M_TEN) {
213 + printk("10 bits addr not supported\n");
217 + if (msg->flags & I2C_M_RD) {
218 + for (i = 0; i < nblock; i++) {
219 + if (rt_i2c_wait_idle())
221 + rt_i2c_w32(READ_BLOCK - 1, REG_BYTECNT_REG);
222 + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
223 + for (j = 0; j < READ_BLOCK; j++) {
224 + if (rt_i2c_wait_rx_done())
226 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
230 + if (rt_i2c_wait_idle()) {
234 + rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
235 + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
237 + for (i = 0; i < rem; i++) {
238 + if (rt_i2c_wait_rx_done())
240 + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
243 + if (rt_i2c_wait_idle()) {
246 + rt_i2c_w32(msg->len - 1, REG_BYTECNT_REG);
247 + for (i = 0; i < msg->len; i++) {
248 + rt_i2c_w32(msg->buf[i], REG_DATAOUT_REG);
250 + rt_i2c_w32(WRITE_CMD, REG_STARTXFR_REG);
252 + if (rt_i2c_wait_tx_done())
262 +static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
267 + mt7621_i2c_enable(m);
269 + for (i = 0; i != n && ret==0; i++) {
270 + ret = rt_i2c_handle_msg(a, &m[i]);
280 +static u32 rt_i2c_func(struct i2c_adapter *a)
282 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
285 +static const struct i2c_algorithm rt_i2c_algo = {
286 + .master_xfer = rt_i2c_master_xfer,
287 + .functionality = rt_i2c_func,
290 +static int rt_i2c_probe(struct platform_device *pdev)
292 + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
294 + struct i2c_algo_mt7621_data *adapter_data;
296 + adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
298 + dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
302 + adapter_data = kzalloc(sizeof(struct i2c_algo_mt7621_data), GFP_KERNEL);
303 + if (!adapter_data) {
308 + membase = devm_request_and_ioremap(&pdev->dev, res);
309 + if (IS_ERR(membase)){
313 + memsysctlbase = membase - I2C_OFFSET;
315 + adapter_data->id = i2c_id++;
316 + strlcpy(adapter->name, dev_name(&pdev->dev), sizeof(adapter->name));
318 + adapter->owner = THIS_MODULE;
319 + adapter->nr = pdev->id;
320 + adapter->timeout = HZ;
321 + adapter->algo = &rt_i2c_algo;
322 + adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
323 + adapter->dev.parent = &pdev->dev;
324 + adapter->dev.of_node = pdev->dev.of_node;
326 + init_waitqueue_head(&adapter_data->waitq);
327 + spin_lock_init(&adapter_data->lock);
329 + platform_set_drvdata(pdev, adapter);
330 + adapter->algo_data = adapter_data;
332 + ret = i2c_add_numbered_adapter(adapter);
336 + printk("MT7621A i2c add adapter is ok\n");
340 + kfree(adapter_data);
347 +static int rt_i2c_remove(struct platform_device *pdev)
349 + struct i2c_algo_mt7621_data *adapter_data = (struct i2c_algo_mt7621_data *)adapter->algo_data;
350 + release_mem_region((resource_size_t)membase,0x100);
351 + kfree(adapter_data);
353 + platform_set_drvdata(pdev, NULL);
358 +static const struct of_device_id i2c_rt_dt_ids[] = {
359 + { .compatible = "ralink,i2c-mt7621", },
363 +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
365 +static struct platform_driver rt_i2c_driver = {
366 + .probe = rt_i2c_probe,
367 + .remove = rt_i2c_remove,
369 + .owner = THIS_MODULE,
370 + .name = "i2c-mt7621",
371 + .of_match_table = i2c_rt_dt_ids,
375 +static int __init i2c_rt_init (void)
377 + return platform_driver_register(&rt_i2c_driver);
380 +static void __exit i2c_rt_exit (void)
382 + platform_driver_unregister(&rt_i2c_driver);
384 +module_init (i2c_rt_init);
385 +module_exit (i2c_rt_exit);
387 +MODULE_AUTHOR("Steven Liu <steven_liu@mediatek.com>");
388 +MODULE_DESCRIPTION("MT7621 I2c host driver");
389 +MODULE_LICENSE("GPL");
390 +MODULE_ALIAS("platform:MT7621-I2C");