aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2011-03-17 20:10:19 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-03-17 20:10:19 -0400
commit02e4c627d862427653fc088ce299746ea7d85600 (patch)
treee756f329f8f5e4546c9f62c3493903895227f5b6 /drivers/i2c
parent4b0e976c663e808822adf51274f948e8a4986f06 (diff)
parentead9a638c8f7a4f6471a130c854afafeccbb0824 (diff)
Merge branch 'for-linus/2639/i2c-1' of git://git.fluff.org/bjdooks/linux
* 'for-linus/2639/i2c-1' of git://git.fluff.org/bjdooks/linux: i2c-mpc: Add support for 64bit system i2c: add driver for Freescale i.MX28 i2c: tegra: Add i2c support
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/busses/Kconfig19
-rw-r--r--drivers/i2c/busses/Makefile2
-rw-r--r--drivers/i2c/busses/i2c-mxs.c412
-rw-r--r--drivers/i2c/busses/i2c-tegra.c700
4 files changed, 1132 insertions, 1 deletions
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index cbfcf6fb4a61..230601e8853f 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -433,7 +433,7 @@ config I2C_IXP2000
433 433
434config I2C_MPC 434config I2C_MPC
435 tristate "MPC107/824x/85xx/512x/52xx/83xx/86xx" 435 tristate "MPC107/824x/85xx/512x/52xx/83xx/86xx"
436 depends on PPC32 436 depends on PPC
437 help 437 help
438 If you say yes to this option, support will be included for the 438 If you say yes to this option, support will be included for the
439 built-in I2C interface on the MPC107, Tsi107, MPC512x, MPC52xx, 439 built-in I2C interface on the MPC107, Tsi107, MPC512x, MPC52xx,
@@ -452,6 +452,16 @@ config I2C_MV64XXX
452 This driver can also be built as a module. If so, the module 452 This driver can also be built as a module. If so, the module
453 will be called i2c-mv64xxx. 453 will be called i2c-mv64xxx.
454 454
455config I2C_MXS
456 tristate "Freescale i.MX28 I2C interface"
457 depends on SOC_IMX28
458 help
459 Say Y here if you want to use the I2C bus controller on
460 the Freescale i.MX28 processors.
461
462 This driver can also be built as a module. If so, the module
463 will be called i2c-mxs.
464
455config I2C_NOMADIK 465config I2C_NOMADIK
456 tristate "ST-Ericsson Nomadik/Ux500 I2C Controller" 466 tristate "ST-Ericsson Nomadik/Ux500 I2C Controller"
457 depends on PLAT_NOMADIK 467 depends on PLAT_NOMADIK
@@ -618,6 +628,13 @@ config I2C_STU300
618 This driver can also be built as a module. If so, the module 628 This driver can also be built as a module. If so, the module
619 will be called i2c-stu300. 629 will be called i2c-stu300.
620 630
631config I2C_TEGRA
632 tristate "NVIDIA Tegra internal I2C controller"
633 depends on ARCH_TEGRA
634 help
635 If you say yes to this option, support will be included for the
636 I2C controller embedded in NVIDIA Tegra SOCs
637
621config I2C_VERSATILE 638config I2C_VERSATILE
622 tristate "ARM Versatile/Realview I2C bus support" 639 tristate "ARM Versatile/Realview I2C bus support"
623 depends on ARCH_VERSATILE || ARCH_REALVIEW || ARCH_VEXPRESS 640 depends on ARCH_VERSATILE || ARCH_REALVIEW || ARCH_VEXPRESS
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index a83966acc5ab..3878c959d4fa 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -43,6 +43,7 @@ obj-$(CONFIG_I2C_IOP3XX) += i2c-iop3xx.o
43obj-$(CONFIG_I2C_IXP2000) += i2c-ixp2000.o 43obj-$(CONFIG_I2C_IXP2000) += i2c-ixp2000.o
44obj-$(CONFIG_I2C_MPC) += i2c-mpc.o 44obj-$(CONFIG_I2C_MPC) += i2c-mpc.o
45obj-$(CONFIG_I2C_MV64XXX) += i2c-mv64xxx.o 45obj-$(CONFIG_I2C_MV64XXX) += i2c-mv64xxx.o
46obj-$(CONFIG_I2C_MXS) += i2c-mxs.o
46obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o 47obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o
47obj-$(CONFIG_I2C_NUC900) += i2c-nuc900.o 48obj-$(CONFIG_I2C_NUC900) += i2c-nuc900.o
48obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o 49obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o
@@ -59,6 +60,7 @@ obj-$(CONFIG_I2C_SH7760) += i2c-sh7760.o
59obj-$(CONFIG_I2C_SH_MOBILE) += i2c-sh_mobile.o 60obj-$(CONFIG_I2C_SH_MOBILE) += i2c-sh_mobile.o
60obj-$(CONFIG_I2C_SIMTEC) += i2c-simtec.o 61obj-$(CONFIG_I2C_SIMTEC) += i2c-simtec.o
61obj-$(CONFIG_I2C_STU300) += i2c-stu300.o 62obj-$(CONFIG_I2C_STU300) += i2c-stu300.o
63obj-$(CONFIG_I2C_TEGRA) += i2c-tegra.o
62obj-$(CONFIG_I2C_VERSATILE) += i2c-versatile.o 64obj-$(CONFIG_I2C_VERSATILE) += i2c-versatile.o
63obj-$(CONFIG_I2C_OCTEON) += i2c-octeon.o 65obj-$(CONFIG_I2C_OCTEON) += i2c-octeon.o
64obj-$(CONFIG_I2C_XILINX) += i2c-xiic.o 66obj-$(CONFIG_I2C_XILINX) += i2c-xiic.o
diff --git a/drivers/i2c/busses/i2c-mxs.c b/drivers/i2c/busses/i2c-mxs.c
new file mode 100644
index 000000000000..8022e2390a5a
--- /dev/null
+++ b/drivers/i2c/busses/i2c-mxs.c
@@ -0,0 +1,412 @@
1/*
2 * Freescale MXS I2C bus driver
3 *
4 * Copyright (C) 2011 Wolfram Sang, Pengutronix e.K.
5 *
6 * based on a (non-working) driver which was:
7 *
8 * Copyright (C) 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
9 *
10 * TODO: add dma-support if platform-support for it is available
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 */
18
19#include <linux/slab.h>
20#include <linux/device.h>
21#include <linux/module.h>
22#include <linux/i2c.h>
23#include <linux/err.h>
24#include <linux/interrupt.h>
25#include <linux/completion.h>
26#include <linux/platform_device.h>
27#include <linux/jiffies.h>
28#include <linux/io.h>
29
30#include <mach/common.h>
31
32#define DRIVER_NAME "mxs-i2c"
33
34#define MXS_I2C_CTRL0 (0x00)
35#define MXS_I2C_CTRL0_SET (0x04)
36
37#define MXS_I2C_CTRL0_SFTRST 0x80000000
38#define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
39#define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
40#define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
41#define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000
42#define MXS_I2C_CTRL0_MASTER_MODE 0x00020000
43#define MXS_I2C_CTRL0_DIRECTION 0x00010000
44#define MXS_I2C_CTRL0_XFER_COUNT(v) ((v) & 0x0000FFFF)
45
46#define MXS_I2C_CTRL1 (0x40)
47#define MXS_I2C_CTRL1_SET (0x44)
48#define MXS_I2C_CTRL1_CLR (0x48)
49
50#define MXS_I2C_CTRL1_BUS_FREE_IRQ 0x80
51#define MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ 0x40
52#define MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ 0x20
53#define MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ 0x10
54#define MXS_I2C_CTRL1_EARLY_TERM_IRQ 0x08
55#define MXS_I2C_CTRL1_MASTER_LOSS_IRQ 0x04
56#define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02
57#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
58
59#define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \
60 MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \
61 MXS_I2C_CTRL1_EARLY_TERM_IRQ | \
62 MXS_I2C_CTRL1_MASTER_LOSS_IRQ | \
63 MXS_I2C_CTRL1_SLAVE_STOP_IRQ | \
64 MXS_I2C_CTRL1_SLAVE_IRQ)
65
66#define MXS_I2C_QUEUECTRL (0x60)
67#define MXS_I2C_QUEUECTRL_SET (0x64)
68#define MXS_I2C_QUEUECTRL_CLR (0x68)
69
70#define MXS_I2C_QUEUECTRL_QUEUE_RUN 0x20
71#define MXS_I2C_QUEUECTRL_PIO_QUEUE_MODE 0x04
72
73#define MXS_I2C_QUEUESTAT (0x70)
74#define MXS_I2C_QUEUESTAT_RD_QUEUE_EMPTY 0x00002000
75
76#define MXS_I2C_QUEUECMD (0x80)
77
78#define MXS_I2C_QUEUEDATA (0x90)
79
80#define MXS_I2C_DATA (0xa0)
81
82
83#define MXS_CMD_I2C_SELECT (MXS_I2C_CTRL0_RETAIN_CLOCK | \
84 MXS_I2C_CTRL0_PRE_SEND_START | \
85 MXS_I2C_CTRL0_MASTER_MODE | \
86 MXS_I2C_CTRL0_DIRECTION | \
87 MXS_I2C_CTRL0_XFER_COUNT(1))
88
89#define MXS_CMD_I2C_WRITE (MXS_I2C_CTRL0_PRE_SEND_START | \
90 MXS_I2C_CTRL0_MASTER_MODE | \
91 MXS_I2C_CTRL0_DIRECTION)
92
93#define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
94 MXS_I2C_CTRL0_MASTER_MODE)
95
96/**
97 * struct mxs_i2c_dev - per device, private MXS-I2C data
98 *
99 * @dev: driver model device node
100 * @regs: IO registers pointer
101 * @cmd_complete: completion object for transaction wait
102 * @cmd_err: error code for last transaction
103 * @adapter: i2c subsystem adapter node
104 */
105struct mxs_i2c_dev {
106 struct device *dev;
107 void __iomem *regs;
108 struct completion cmd_complete;
109 u32 cmd_err;
110 struct i2c_adapter adapter;
111};
112
113/*
114 * TODO: check if calls to here are really needed. If not, we could get rid of
115 * mxs_reset_block and the mach-dependency. Needs an I2C analyzer, probably.
116 */
117static void mxs_i2c_reset(struct mxs_i2c_dev *i2c)
118{
119 mxs_reset_block(i2c->regs);
120 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
121}
122
123static void mxs_i2c_pioq_setup_read(struct mxs_i2c_dev *i2c, u8 addr, int len,
124 int flags)
125{
126 u32 data;
127
128 writel(MXS_CMD_I2C_SELECT, i2c->regs + MXS_I2C_QUEUECMD);
129
130 data = (addr << 1) | I2C_SMBUS_READ;
131 writel(data, i2c->regs + MXS_I2C_DATA);
132
133 data = MXS_CMD_I2C_READ | MXS_I2C_CTRL0_XFER_COUNT(len) | flags;
134 writel(data, i2c->regs + MXS_I2C_QUEUECMD);
135}
136
137static void mxs_i2c_pioq_setup_write(struct mxs_i2c_dev *i2c,
138 u8 addr, u8 *buf, int len, int flags)
139{
140 u32 data;
141 int i, shifts_left;
142
143 data = MXS_CMD_I2C_WRITE | MXS_I2C_CTRL0_XFER_COUNT(len + 1) | flags;
144 writel(data, i2c->regs + MXS_I2C_QUEUECMD);
145
146 /*
147 * We have to copy the slave address (u8) and buffer (arbitrary number
148 * of u8) into the data register (u32). To achieve that, the u8 are put
149 * into the MSBs of 'data' which is then shifted for the next u8. When
150 * apropriate, 'data' is written to MXS_I2C_DATA. So, the first u32
151 * looks like this:
152 *
153 * 3 2 1 0
154 * 10987654|32109876|54321098|76543210
155 * --------+--------+--------+--------
156 * buffer+2|buffer+1|buffer+0|slave_addr
157 */
158
159 data = ((addr << 1) | I2C_SMBUS_WRITE) << 24;
160
161 for (i = 0; i < len; i++) {
162 data >>= 8;
163 data |= buf[i] << 24;
164 if ((i & 3) == 2)
165 writel(data, i2c->regs + MXS_I2C_DATA);
166 }
167
168 /* Write out the remaining bytes if any */
169 shifts_left = 24 - (i & 3) * 8;
170 if (shifts_left)
171 writel(data >> shifts_left, i2c->regs + MXS_I2C_DATA);
172}
173
174/*
175 * TODO: should be replaceable with a waitqueue and RD_QUEUE_IRQ (setting the
176 * rd_threshold to 1). Couldn't get this to work, though.
177 */
178static int mxs_i2c_wait_for_data(struct mxs_i2c_dev *i2c)
179{
180 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
181
182 while (readl(i2c->regs + MXS_I2C_QUEUESTAT)
183 & MXS_I2C_QUEUESTAT_RD_QUEUE_EMPTY) {
184 if (time_after(jiffies, timeout))
185 return -ETIMEDOUT;
186 cond_resched();
187 }
188
189 return 0;
190}
191
192static int mxs_i2c_finish_read(struct mxs_i2c_dev *i2c, u8 *buf, int len)
193{
194 u32 data;
195 int i;
196
197 for (i = 0; i < len; i++) {
198 if ((i & 3) == 0) {
199 if (mxs_i2c_wait_for_data(i2c))
200 return -ETIMEDOUT;
201 data = readl(i2c->regs + MXS_I2C_QUEUEDATA);
202 }
203 buf[i] = data & 0xff;
204 data >>= 8;
205 }
206
207 return 0;
208}
209
210/*
211 * Low level master read/write transaction.
212 */
213static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
214 int stop)
215{
216 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
217 int ret;
218 int flags;
219
220 init_completion(&i2c->cmd_complete);
221
222 dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
223 msg->addr, msg->len, msg->flags, stop);
224
225 if (msg->len == 0)
226 return -EINVAL;
227
228 flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
229
230 if (msg->flags & I2C_M_RD)
231 mxs_i2c_pioq_setup_read(i2c, msg->addr, msg->len, flags);
232 else
233 mxs_i2c_pioq_setup_write(i2c, msg->addr, msg->buf, msg->len,
234 flags);
235
236 writel(MXS_I2C_QUEUECTRL_QUEUE_RUN,
237 i2c->regs + MXS_I2C_QUEUECTRL_SET);
238
239 ret = wait_for_completion_timeout(&i2c->cmd_complete,
240 msecs_to_jiffies(1000));
241 if (ret == 0)
242 goto timeout;
243
244 if ((!i2c->cmd_err) && (msg->flags & I2C_M_RD)) {
245 ret = mxs_i2c_finish_read(i2c, msg->buf, msg->len);
246 if (ret)
247 goto timeout;
248 }
249
250 if (i2c->cmd_err == -ENXIO)
251 mxs_i2c_reset(i2c);
252
253 dev_dbg(i2c->dev, "Done with err=%d\n", i2c->cmd_err);
254
255 return i2c->cmd_err;
256
257timeout:
258 dev_dbg(i2c->dev, "Timeout!\n");
259 mxs_i2c_reset(i2c);
260 return -ETIMEDOUT;
261}
262
263static int mxs_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
264 int num)
265{
266 int i;
267 int err;
268
269 for (i = 0; i < num; i++) {
270 err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1));
271 if (err)
272 return err;
273 }
274
275 return num;
276}
277
278static u32 mxs_i2c_func(struct i2c_adapter *adap)
279{
280 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
281}
282
283static irqreturn_t mxs_i2c_isr(int this_irq, void *dev_id)
284{
285 struct mxs_i2c_dev *i2c = dev_id;
286 u32 stat = readl(i2c->regs + MXS_I2C_CTRL1) & MXS_I2C_IRQ_MASK;
287
288 if (!stat)
289 return IRQ_NONE;
290
291 if (stat & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
292 i2c->cmd_err = -ENXIO;
293 else if (stat & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
294 MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
295 MXS_I2C_CTRL1_SLAVE_STOP_IRQ | MXS_I2C_CTRL1_SLAVE_IRQ))
296 /* MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ is only for slaves */
297 i2c->cmd_err = -EIO;
298 else
299 i2c->cmd_err = 0;
300
301 complete(&i2c->cmd_complete);
302
303 writel(stat, i2c->regs + MXS_I2C_CTRL1_CLR);
304 return IRQ_HANDLED;
305}
306
307static const struct i2c_algorithm mxs_i2c_algo = {
308 .master_xfer = mxs_i2c_xfer,
309 .functionality = mxs_i2c_func,
310};
311
312static int __devinit mxs_i2c_probe(struct platform_device *pdev)
313{
314 struct device *dev = &pdev->dev;
315 struct mxs_i2c_dev *i2c;
316 struct i2c_adapter *adap;
317 struct resource *res;
318 resource_size_t res_size;
319 int err, irq;
320
321 i2c = devm_kzalloc(dev, sizeof(struct mxs_i2c_dev), GFP_KERNEL);
322 if (!i2c)
323 return -ENOMEM;
324
325 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
326 if (!res)
327 return -ENOENT;
328
329 res_size = resource_size(res);
330 if (!devm_request_mem_region(dev, res->start, res_size, res->name))
331 return -EBUSY;
332
333 i2c->regs = devm_ioremap_nocache(dev, res->start, res_size);
334 if (!i2c->regs)
335 return -EBUSY;
336
337 irq = platform_get_irq(pdev, 0);
338 if (irq < 0)
339 return irq;
340
341 err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c);
342 if (err)
343 return err;
344
345 i2c->dev = dev;
346 platform_set_drvdata(pdev, i2c);
347
348 /* Do reset to enforce correct startup after pinmuxing */
349 mxs_i2c_reset(i2c);
350 writel(MXS_I2C_QUEUECTRL_PIO_QUEUE_MODE,
351 i2c->regs + MXS_I2C_QUEUECTRL_SET);
352
353 adap = &i2c->adapter;
354 strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name));
355 adap->owner = THIS_MODULE;
356 adap->algo = &mxs_i2c_algo;
357 adap->dev.parent = dev;
358 adap->nr = pdev->id;
359 i2c_set_adapdata(adap, i2c);
360 err = i2c_add_numbered_adapter(adap);
361 if (err) {
362 dev_err(dev, "Failed to add adapter (%d)\n", err);
363 writel(MXS_I2C_CTRL0_SFTRST,
364 i2c->regs + MXS_I2C_CTRL0_SET);
365 return err;
366 }
367
368 return 0;
369}
370
371static int __devexit mxs_i2c_remove(struct platform_device *pdev)
372{
373 struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev);
374 int ret;
375
376 ret = i2c_del_adapter(&i2c->adapter);
377 if (ret)
378 return -EBUSY;
379
380 writel(MXS_I2C_QUEUECTRL_QUEUE_RUN,
381 i2c->regs + MXS_I2C_QUEUECTRL_CLR);
382 writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
383
384 platform_set_drvdata(pdev, NULL);
385
386 return 0;
387}
388
389static struct platform_driver mxs_i2c_driver = {
390 .driver = {
391 .name = DRIVER_NAME,
392 .owner = THIS_MODULE,
393 },
394 .remove = __devexit_p(mxs_i2c_remove),
395};
396
397static int __init mxs_i2c_init(void)
398{
399 return platform_driver_probe(&mxs_i2c_driver, mxs_i2c_probe);
400}
401subsys_initcall(mxs_i2c_init);
402
403static void __exit mxs_i2c_exit(void)
404{
405 platform_driver_unregister(&mxs_i2c_driver);
406}
407module_exit(mxs_i2c_exit);
408
409MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
410MODULE_DESCRIPTION("MXS I2C Bus Driver");
411MODULE_LICENSE("GPL");
412MODULE_ALIAS("platform:" DRIVER_NAME);
diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
new file mode 100644
index 000000000000..3921f664c9c3
--- /dev/null
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -0,0 +1,700 @@
1/*
2 * drivers/i2c/busses/i2c-tegra.c
3 *
4 * Copyright (C) 2010 Google, Inc.
5 * Author: Colin Cross <ccross@android.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/platform_device.h>
21#include <linux/clk.h>
22#include <linux/err.h>
23#include <linux/i2c.h>
24#include <linux/io.h>
25#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28#include <linux/i2c-tegra.h>
29
30#include <asm/unaligned.h>
31
32#include <mach/clk.h>
33
34#define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
35#define BYTES_PER_FIFO_WORD 4
36
37#define I2C_CNFG 0x000
38#define I2C_CNFG_PACKET_MODE_EN (1<<10)
39#define I2C_CNFG_NEW_MASTER_FSM (1<<11)
40#define I2C_SL_CNFG 0x020
41#define I2C_SL_CNFG_NEWSL (1<<2)
42#define I2C_SL_ADDR1 0x02c
43#define I2C_TX_FIFO 0x050
44#define I2C_RX_FIFO 0x054
45#define I2C_PACKET_TRANSFER_STATUS 0x058
46#define I2C_FIFO_CONTROL 0x05c
47#define I2C_FIFO_CONTROL_TX_FLUSH (1<<1)
48#define I2C_FIFO_CONTROL_RX_FLUSH (1<<0)
49#define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
50#define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
51#define I2C_FIFO_STATUS 0x060
52#define I2C_FIFO_STATUS_TX_MASK 0xF0
53#define I2C_FIFO_STATUS_TX_SHIFT 4
54#define I2C_FIFO_STATUS_RX_MASK 0x0F
55#define I2C_FIFO_STATUS_RX_SHIFT 0
56#define I2C_INT_MASK 0x064
57#define I2C_INT_STATUS 0x068
58#define I2C_INT_PACKET_XFER_COMPLETE (1<<7)
59#define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6)
60#define I2C_INT_TX_FIFO_OVERFLOW (1<<5)
61#define I2C_INT_RX_FIFO_UNDERFLOW (1<<4)
62#define I2C_INT_NO_ACK (1<<3)
63#define I2C_INT_ARBITRATION_LOST (1<<2)
64#define I2C_INT_TX_FIFO_DATA_REQ (1<<1)
65#define I2C_INT_RX_FIFO_DATA_REQ (1<<0)
66#define I2C_CLK_DIVISOR 0x06c
67
68#define DVC_CTRL_REG1 0x000
69#define DVC_CTRL_REG1_INTR_EN (1<<10)
70#define DVC_CTRL_REG2 0x004
71#define DVC_CTRL_REG3 0x008
72#define DVC_CTRL_REG3_SW_PROG (1<<26)
73#define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30)
74#define DVC_STATUS 0x00c
75#define DVC_STATUS_I2C_DONE_INTR (1<<30)
76
77#define I2C_ERR_NONE 0x00
78#define I2C_ERR_NO_ACK 0x01
79#define I2C_ERR_ARBITRATION_LOST 0x02
80
81#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
82#define PACKET_HEADER0_PACKET_ID_SHIFT 16
83#define PACKET_HEADER0_CONT_ID_SHIFT 12
84#define PACKET_HEADER0_PROTOCOL_I2C (1<<4)
85
86#define I2C_HEADER_HIGHSPEED_MODE (1<<22)
87#define I2C_HEADER_CONT_ON_NAK (1<<21)
88#define I2C_HEADER_SEND_START_BYTE (1<<20)
89#define I2C_HEADER_READ (1<<19)
90#define I2C_HEADER_10BIT_ADDR (1<<18)
91#define I2C_HEADER_IE_ENABLE (1<<17)
92#define I2C_HEADER_REPEAT_START (1<<16)
93#define I2C_HEADER_MASTER_ADDR_SHIFT 12
94#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
95
96/**
97 * struct tegra_i2c_dev - per device i2c context
98 * @dev: device reference for power management
99 * @adapter: core i2c layer adapter information
100 * @clk: clock reference for i2c controller
101 * @i2c_clk: clock reference for i2c bus
102 * @iomem: memory resource for registers
103 * @base: ioremapped registers cookie
104 * @cont_id: i2c controller id, used for for packet header
105 * @irq: irq number of transfer complete interrupt
106 * @is_dvc: identifies the DVC i2c controller, has a different register layout
107 * @msg_complete: transfer completion notifier
108 * @msg_err: error code for completed message
109 * @msg_buf: pointer to current message data
110 * @msg_buf_remaining: size of unsent data in the message buffer
111 * @msg_read: identifies read transfers
112 * @bus_clk_rate: current i2c bus clock rate
113 * @is_suspended: prevents i2c controller accesses after suspend is called
114 */
115struct tegra_i2c_dev {
116 struct device *dev;
117 struct i2c_adapter adapter;
118 struct clk *clk;
119 struct clk *i2c_clk;
120 struct resource *iomem;
121 void __iomem *base;
122 int cont_id;
123 int irq;
124 int is_dvc;
125 struct completion msg_complete;
126 int msg_err;
127 u8 *msg_buf;
128 size_t msg_buf_remaining;
129 int msg_read;
130 unsigned long bus_clk_rate;
131 bool is_suspended;
132};
133
134static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
135{
136 writel(val, i2c_dev->base + reg);
137}
138
139static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
140{
141 return readl(i2c_dev->base + reg);
142}
143
144/*
145 * i2c_writel and i2c_readl will offset the register if necessary to talk
146 * to the I2C block inside the DVC block
147 */
148static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
149 unsigned long reg)
150{
151 if (i2c_dev->is_dvc)
152 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
153 return reg;
154}
155
156static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
157 unsigned long reg)
158{
159 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
160}
161
162static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
163{
164 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
165}
166
167static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
168 unsigned long reg, int len)
169{
170 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
171}
172
173static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
174 unsigned long reg, int len)
175{
176 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
177}
178
179static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
180{
181 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
182 int_mask &= ~mask;
183 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
184}
185
186static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
187{
188 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
189 int_mask |= mask;
190 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
191}
192
193static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
194{
195 unsigned long timeout = jiffies + HZ;
196 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
197 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
198 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
199
200 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
201 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
202 if (time_after(jiffies, timeout)) {
203 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
204 return -ETIMEDOUT;
205 }
206 msleep(1);
207 }
208 return 0;
209}
210
211static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
212{
213 u32 val;
214 int rx_fifo_avail;
215 u8 *buf = i2c_dev->msg_buf;
216 size_t buf_remaining = i2c_dev->msg_buf_remaining;
217 int words_to_transfer;
218
219 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
220 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
221 I2C_FIFO_STATUS_RX_SHIFT;
222
223 /* Rounds down to not include partial word at the end of buf */
224 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
225 if (words_to_transfer > rx_fifo_avail)
226 words_to_transfer = rx_fifo_avail;
227
228 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
229
230 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
231 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
232 rx_fifo_avail -= words_to_transfer;
233
234 /*
235 * If there is a partial word at the end of buf, handle it manually to
236 * prevent overwriting past the end of buf
237 */
238 if (rx_fifo_avail > 0 && buf_remaining > 0) {
239 BUG_ON(buf_remaining > 3);
240 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
241 memcpy(buf, &val, buf_remaining);
242 buf_remaining = 0;
243 rx_fifo_avail--;
244 }
245
246 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
247 i2c_dev->msg_buf_remaining = buf_remaining;
248 i2c_dev->msg_buf = buf;
249 return 0;
250}
251
252static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
253{
254 u32 val;
255 int tx_fifo_avail;
256 u8 *buf = i2c_dev->msg_buf;
257 size_t buf_remaining = i2c_dev->msg_buf_remaining;
258 int words_to_transfer;
259
260 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
261 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
262 I2C_FIFO_STATUS_TX_SHIFT;
263
264 /* Rounds down to not include partial word at the end of buf */
265 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
266 if (words_to_transfer > tx_fifo_avail)
267 words_to_transfer = tx_fifo_avail;
268
269 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
270
271 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
272 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
273 tx_fifo_avail -= words_to_transfer;
274
275 /*
276 * If there is a partial word at the end of buf, handle it manually to
277 * prevent reading past the end of buf, which could cross a page
278 * boundary and fault.
279 */
280 if (tx_fifo_avail > 0 && buf_remaining > 0) {
281 BUG_ON(buf_remaining > 3);
282 memcpy(&val, buf, buf_remaining);
283 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
284 buf_remaining = 0;
285 tx_fifo_avail--;
286 }
287
288 BUG_ON(tx_fifo_avail > 0 && buf_remaining > 0);
289 i2c_dev->msg_buf_remaining = buf_remaining;
290 i2c_dev->msg_buf = buf;
291 return 0;
292}
293
294/*
295 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
296 * block. This block is identical to the rest of the I2C blocks, except that
297 * it only supports master mode, it has registers moved around, and it needs
298 * some extra init to get it into I2C mode. The register moves are handled
299 * by i2c_readl and i2c_writel
300 */
301static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
302{
303 u32 val = 0;
304 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
305 val |= DVC_CTRL_REG3_SW_PROG;
306 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
307 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
308
309 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
310 val |= DVC_CTRL_REG1_INTR_EN;
311 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
312}
313
314static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
315{
316 u32 val;
317 int err = 0;
318
319 clk_enable(i2c_dev->clk);
320
321 tegra_periph_reset_assert(i2c_dev->clk);
322 udelay(2);
323 tegra_periph_reset_deassert(i2c_dev->clk);
324
325 if (i2c_dev->is_dvc)
326 tegra_dvc_init(i2c_dev);
327
328 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN;
329 i2c_writel(i2c_dev, val, I2C_CNFG);
330 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
331 clk_set_rate(i2c_dev->clk, i2c_dev->bus_clk_rate * 8);
332
333 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
334 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
335 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
336
337 if (tegra_i2c_flush_fifos(i2c_dev))
338 err = -ETIMEDOUT;
339
340 clk_disable(i2c_dev->clk);
341 return err;
342}
343
344static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
345{
346 u32 status;
347 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
348 struct tegra_i2c_dev *i2c_dev = dev_id;
349
350 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
351
352 if (status == 0) {
353 dev_warn(i2c_dev->dev, "interrupt with no status\n");
354 return IRQ_NONE;
355 }
356
357 if (unlikely(status & status_err)) {
358 if (status & I2C_INT_NO_ACK)
359 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
360 if (status & I2C_INT_ARBITRATION_LOST)
361 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
362 complete(&i2c_dev->msg_complete);
363 goto err;
364 }
365
366 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
367 if (i2c_dev->msg_buf_remaining)
368 tegra_i2c_empty_rx_fifo(i2c_dev);
369 else
370 BUG();
371 }
372
373 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
374 if (i2c_dev->msg_buf_remaining)
375 tegra_i2c_fill_tx_fifo(i2c_dev);
376 else
377 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
378 }
379
380 if ((status & I2C_INT_PACKET_XFER_COMPLETE) &&
381 !i2c_dev->msg_buf_remaining)
382 complete(&i2c_dev->msg_complete);
383
384 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
385 if (i2c_dev->is_dvc)
386 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
387 return IRQ_HANDLED;
388err:
389 /* An error occured, mask all interrupts */
390 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
391 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
392 I2C_INT_RX_FIFO_DATA_REQ);
393 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
394 return IRQ_HANDLED;
395}
396
397static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
398 struct i2c_msg *msg, int stop)
399{
400 u32 packet_header;
401 u32 int_mask;
402 int ret;
403
404 tegra_i2c_flush_fifos(i2c_dev);
405 i2c_writel(i2c_dev, 0xFF, I2C_INT_STATUS);
406
407 if (msg->len == 0)
408 return -EINVAL;
409
410 i2c_dev->msg_buf = msg->buf;
411 i2c_dev->msg_buf_remaining = msg->len;
412 i2c_dev->msg_err = I2C_ERR_NONE;
413 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
414 INIT_COMPLETION(i2c_dev->msg_complete);
415
416 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
417 PACKET_HEADER0_PROTOCOL_I2C |
418 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
419 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
420 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
421
422 packet_header = msg->len - 1;
423 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
424
425 packet_header = msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
426 packet_header |= I2C_HEADER_IE_ENABLE;
427 if (msg->flags & I2C_M_TEN)
428 packet_header |= I2C_HEADER_10BIT_ADDR;
429 if (msg->flags & I2C_M_IGNORE_NAK)
430 packet_header |= I2C_HEADER_CONT_ON_NAK;
431 if (msg->flags & I2C_M_NOSTART)
432 packet_header |= I2C_HEADER_REPEAT_START;
433 if (msg->flags & I2C_M_RD)
434 packet_header |= I2C_HEADER_READ;
435 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
436
437 if (!(msg->flags & I2C_M_RD))
438 tegra_i2c_fill_tx_fifo(i2c_dev);
439
440 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
441 if (msg->flags & I2C_M_RD)
442 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
443 else if (i2c_dev->msg_buf_remaining)
444 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
445 tegra_i2c_unmask_irq(i2c_dev, int_mask);
446 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
447 i2c_readl(i2c_dev, I2C_INT_MASK));
448
449 ret = wait_for_completion_timeout(&i2c_dev->msg_complete, TEGRA_I2C_TIMEOUT);
450 tegra_i2c_mask_irq(i2c_dev, int_mask);
451
452 if (WARN_ON(ret == 0)) {
453 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
454
455 tegra_i2c_init(i2c_dev);
456 return -ETIMEDOUT;
457 }
458
459 dev_dbg(i2c_dev->dev, "transfer complete: %d %d %d\n",
460 ret, completion_done(&i2c_dev->msg_complete), i2c_dev->msg_err);
461
462 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
463 return 0;
464
465 tegra_i2c_init(i2c_dev);
466 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
467 if (msg->flags & I2C_M_IGNORE_NAK)
468 return 0;
469 return -EREMOTEIO;
470 }
471
472 return -EIO;
473}
474
475static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
476 int num)
477{
478 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
479 int i;
480 int ret = 0;
481
482 if (i2c_dev->is_suspended)
483 return -EBUSY;
484
485 clk_enable(i2c_dev->clk);
486 for (i = 0; i < num; i++) {
487 int stop = (i == (num - 1)) ? 1 : 0;
488 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], stop);
489 if (ret)
490 break;
491 }
492 clk_disable(i2c_dev->clk);
493 return ret ?: i;
494}
495
496static u32 tegra_i2c_func(struct i2c_adapter *adap)
497{
498 return I2C_FUNC_I2C;
499}
500
501static const struct i2c_algorithm tegra_i2c_algo = {
502 .master_xfer = tegra_i2c_xfer,
503 .functionality = tegra_i2c_func,
504};
505
506static int tegra_i2c_probe(struct platform_device *pdev)
507{
508 struct tegra_i2c_dev *i2c_dev;
509 struct tegra_i2c_platform_data *pdata = pdev->dev.platform_data;
510 struct resource *res;
511 struct resource *iomem;
512 struct clk *clk;
513 struct clk *i2c_clk;
514 void *base;
515 int irq;
516 int ret = 0;
517
518 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
519 if (!res) {
520 dev_err(&pdev->dev, "no mem resource\n");
521 return -EINVAL;
522 }
523 iomem = request_mem_region(res->start, resource_size(res), pdev->name);
524 if (!iomem) {
525 dev_err(&pdev->dev, "I2C region already claimed\n");
526 return -EBUSY;
527 }
528
529 base = ioremap(iomem->start, resource_size(iomem));
530 if (!base) {
531 dev_err(&pdev->dev, "Cannot ioremap I2C region\n");
532 return -ENOMEM;
533 }
534
535 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
536 if (!res) {
537 dev_err(&pdev->dev, "no irq resource\n");
538 ret = -EINVAL;
539 goto err_iounmap;
540 }
541 irq = res->start;
542
543 clk = clk_get(&pdev->dev, NULL);
544 if (IS_ERR(clk)) {
545 dev_err(&pdev->dev, "missing controller clock");
546 ret = PTR_ERR(clk);
547 goto err_release_region;
548 }
549
550 i2c_clk = clk_get(&pdev->dev, "i2c");
551 if (IS_ERR(i2c_clk)) {
552 dev_err(&pdev->dev, "missing bus clock");
553 ret = PTR_ERR(i2c_clk);
554 goto err_clk_put;
555 }
556
557 i2c_dev = kzalloc(sizeof(struct tegra_i2c_dev), GFP_KERNEL);
558 if (!i2c_dev) {
559 ret = -ENOMEM;
560 goto err_i2c_clk_put;
561 }
562
563 i2c_dev->base = base;
564 i2c_dev->clk = clk;
565 i2c_dev->i2c_clk = i2c_clk;
566 i2c_dev->iomem = iomem;
567 i2c_dev->adapter.algo = &tegra_i2c_algo;
568 i2c_dev->irq = irq;
569 i2c_dev->cont_id = pdev->id;
570 i2c_dev->dev = &pdev->dev;
571 i2c_dev->bus_clk_rate = pdata ? pdata->bus_clk_rate : 100000;
572
573 if (pdev->id == 3)
574 i2c_dev->is_dvc = 1;
575 init_completion(&i2c_dev->msg_complete);
576
577 platform_set_drvdata(pdev, i2c_dev);
578
579 ret = tegra_i2c_init(i2c_dev);
580 if (ret) {
581 dev_err(&pdev->dev, "Failed to initialize i2c controller");
582 goto err_free;
583 }
584
585 ret = request_irq(i2c_dev->irq, tegra_i2c_isr, 0, pdev->name, i2c_dev);
586 if (ret) {
587 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
588 goto err_free;
589 }
590
591 clk_enable(i2c_dev->i2c_clk);
592
593 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
594 i2c_dev->adapter.owner = THIS_MODULE;
595 i2c_dev->adapter.class = I2C_CLASS_HWMON;
596 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
597 sizeof(i2c_dev->adapter.name));
598 i2c_dev->adapter.algo = &tegra_i2c_algo;
599 i2c_dev->adapter.dev.parent = &pdev->dev;
600 i2c_dev->adapter.nr = pdev->id;
601
602 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
603 if (ret) {
604 dev_err(&pdev->dev, "Failed to add I2C adapter\n");
605 goto err_free_irq;
606 }
607
608 return 0;
609err_free_irq:
610 free_irq(i2c_dev->irq, i2c_dev);
611err_free:
612 kfree(i2c_dev);
613err_i2c_clk_put:
614 clk_put(i2c_clk);
615err_clk_put:
616 clk_put(clk);
617err_release_region:
618 release_mem_region(iomem->start, resource_size(iomem));
619err_iounmap:
620 iounmap(base);
621 return ret;
622}
623
624static int tegra_i2c_remove(struct platform_device *pdev)
625{
626 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
627 i2c_del_adapter(&i2c_dev->adapter);
628 free_irq(i2c_dev->irq, i2c_dev);
629 clk_put(i2c_dev->i2c_clk);
630 clk_put(i2c_dev->clk);
631 release_mem_region(i2c_dev->iomem->start,
632 resource_size(i2c_dev->iomem));
633 iounmap(i2c_dev->base);
634 kfree(i2c_dev);
635 return 0;
636}
637
638#ifdef CONFIG_PM
639static int tegra_i2c_suspend(struct platform_device *pdev, pm_message_t state)
640{
641 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
642
643 i2c_lock_adapter(&i2c_dev->adapter);
644 i2c_dev->is_suspended = true;
645 i2c_unlock_adapter(&i2c_dev->adapter);
646
647 return 0;
648}
649
650static int tegra_i2c_resume(struct platform_device *pdev)
651{
652 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
653 int ret;
654
655 i2c_lock_adapter(&i2c_dev->adapter);
656
657 ret = tegra_i2c_init(i2c_dev);
658
659 if (ret) {
660 i2c_unlock_adapter(&i2c_dev->adapter);
661 return ret;
662 }
663
664 i2c_dev->is_suspended = false;
665
666 i2c_unlock_adapter(&i2c_dev->adapter);
667
668 return 0;
669}
670#endif
671
672static struct platform_driver tegra_i2c_driver = {
673 .probe = tegra_i2c_probe,
674 .remove = tegra_i2c_remove,
675#ifdef CONFIG_PM
676 .suspend = tegra_i2c_suspend,
677 .resume = tegra_i2c_resume,
678#endif
679 .driver = {
680 .name = "tegra-i2c",
681 .owner = THIS_MODULE,
682 },
683};
684
685static int __init tegra_i2c_init_driver(void)
686{
687 return platform_driver_register(&tegra_i2c_driver);
688}
689
690static void __exit tegra_i2c_exit_driver(void)
691{
692 platform_driver_unregister(&tegra_i2c_driver);
693}
694
695subsys_initcall(tegra_i2c_init_driver);
696module_exit(tegra_i2c_exit_driver);
697
698MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
699MODULE_AUTHOR("Colin Cross");
700MODULE_LICENSE("GPL v2");