blob: 785f7f7a344a30653eaf3e4b3ba36e64e4d1abe7 [file] [log] [blame]
Colin Crossdb811ca2011-02-20 17:14:21 -08001/*
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>
John Bonesio5c470f32011-06-22 09:16:56 -070029#include <linux/of_i2c.h>
Paul Gortmaker93cf5d72011-07-29 21:14:30 -070030#include <linux/module.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080031
32#include <asm/unaligned.h>
33
34#include <mach/clk.h>
35
36#define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
37#define BYTES_PER_FIFO_WORD 4
38
39#define I2C_CNFG 0x000
Jay Cheng40abcf72011-04-25 15:32:27 -060040#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
Colin Crossdb811ca2011-02-20 17:14:21 -080041#define I2C_CNFG_PACKET_MODE_EN (1<<10)
42#define I2C_CNFG_NEW_MASTER_FSM (1<<11)
Todd Poynorcb63c622011-04-25 15:32:25 -060043#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080044#define I2C_SL_CNFG 0x020
Stephen Warren5afa9d32011-06-06 11:25:19 -060045#define I2C_SL_CNFG_NACK (1<<1)
Colin Crossdb811ca2011-02-20 17:14:21 -080046#define I2C_SL_CNFG_NEWSL (1<<2)
47#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060048#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080049#define I2C_TX_FIFO 0x050
50#define I2C_RX_FIFO 0x054
51#define I2C_PACKET_TRANSFER_STATUS 0x058
52#define I2C_FIFO_CONTROL 0x05c
53#define I2C_FIFO_CONTROL_TX_FLUSH (1<<1)
54#define I2C_FIFO_CONTROL_RX_FLUSH (1<<0)
55#define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
56#define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
57#define I2C_FIFO_STATUS 0x060
58#define I2C_FIFO_STATUS_TX_MASK 0xF0
59#define I2C_FIFO_STATUS_TX_SHIFT 4
60#define I2C_FIFO_STATUS_RX_MASK 0x0F
61#define I2C_FIFO_STATUS_RX_SHIFT 0
62#define I2C_INT_MASK 0x064
63#define I2C_INT_STATUS 0x068
64#define I2C_INT_PACKET_XFER_COMPLETE (1<<7)
65#define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6)
66#define I2C_INT_TX_FIFO_OVERFLOW (1<<5)
67#define I2C_INT_RX_FIFO_UNDERFLOW (1<<4)
68#define I2C_INT_NO_ACK (1<<3)
69#define I2C_INT_ARBITRATION_LOST (1<<2)
70#define I2C_INT_TX_FIFO_DATA_REQ (1<<1)
71#define I2C_INT_RX_FIFO_DATA_REQ (1<<0)
72#define I2C_CLK_DIVISOR 0x06c
73
74#define DVC_CTRL_REG1 0x000
75#define DVC_CTRL_REG1_INTR_EN (1<<10)
76#define DVC_CTRL_REG2 0x004
77#define DVC_CTRL_REG3 0x008
78#define DVC_CTRL_REG3_SW_PROG (1<<26)
79#define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30)
80#define DVC_STATUS 0x00c
81#define DVC_STATUS_I2C_DONE_INTR (1<<30)
82
83#define I2C_ERR_NONE 0x00
84#define I2C_ERR_NO_ACK 0x01
85#define I2C_ERR_ARBITRATION_LOST 0x02
Todd Poynorcb63c622011-04-25 15:32:25 -060086#define I2C_ERR_UNKNOWN_INTERRUPT 0x04
Colin Crossdb811ca2011-02-20 17:14:21 -080087
88#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
89#define PACKET_HEADER0_PACKET_ID_SHIFT 16
90#define PACKET_HEADER0_CONT_ID_SHIFT 12
91#define PACKET_HEADER0_PROTOCOL_I2C (1<<4)
92
93#define I2C_HEADER_HIGHSPEED_MODE (1<<22)
94#define I2C_HEADER_CONT_ON_NAK (1<<21)
95#define I2C_HEADER_SEND_START_BYTE (1<<20)
96#define I2C_HEADER_READ (1<<19)
97#define I2C_HEADER_10BIT_ADDR (1<<18)
98#define I2C_HEADER_IE_ENABLE (1<<17)
99#define I2C_HEADER_REPEAT_START (1<<16)
100#define I2C_HEADER_MASTER_ADDR_SHIFT 12
101#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
102
103/**
104 * struct tegra_i2c_dev - per device i2c context
105 * @dev: device reference for power management
106 * @adapter: core i2c layer adapter information
107 * @clk: clock reference for i2c controller
108 * @i2c_clk: clock reference for i2c bus
109 * @iomem: memory resource for registers
110 * @base: ioremapped registers cookie
111 * @cont_id: i2c controller id, used for for packet header
112 * @irq: irq number of transfer complete interrupt
113 * @is_dvc: identifies the DVC i2c controller, has a different register layout
114 * @msg_complete: transfer completion notifier
115 * @msg_err: error code for completed message
116 * @msg_buf: pointer to current message data
117 * @msg_buf_remaining: size of unsent data in the message buffer
118 * @msg_read: identifies read transfers
119 * @bus_clk_rate: current i2c bus clock rate
120 * @is_suspended: prevents i2c controller accesses after suspend is called
121 */
122struct tegra_i2c_dev {
123 struct device *dev;
124 struct i2c_adapter adapter;
125 struct clk *clk;
126 struct clk *i2c_clk;
127 struct resource *iomem;
128 void __iomem *base;
129 int cont_id;
130 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600131 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800132 int is_dvc;
133 struct completion msg_complete;
134 int msg_err;
135 u8 *msg_buf;
136 size_t msg_buf_remaining;
137 int msg_read;
138 unsigned long bus_clk_rate;
139 bool is_suspended;
140};
141
142static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
143{
144 writel(val, i2c_dev->base + reg);
145}
146
147static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
148{
149 return readl(i2c_dev->base + reg);
150}
151
152/*
153 * i2c_writel and i2c_readl will offset the register if necessary to talk
154 * to the I2C block inside the DVC block
155 */
156static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
157 unsigned long reg)
158{
159 if (i2c_dev->is_dvc)
160 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
161 return reg;
162}
163
164static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
165 unsigned long reg)
166{
167 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530168
169 /* Read back register to make sure that register writes completed */
170 if (reg != I2C_TX_FIFO)
171 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800172}
173
174static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
175{
176 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
177}
178
179static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
180 unsigned long reg, int len)
181{
182 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
183}
184
185static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
186 unsigned long reg, int len)
187{
188 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
189}
190
191static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
192{
193 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
194 int_mask &= ~mask;
195 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
196}
197
198static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
199{
200 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
201 int_mask |= mask;
202 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
203}
204
205static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
206{
207 unsigned long timeout = jiffies + HZ;
208 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
209 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
210 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
211
212 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
213 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
214 if (time_after(jiffies, timeout)) {
215 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
216 return -ETIMEDOUT;
217 }
218 msleep(1);
219 }
220 return 0;
221}
222
223static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
224{
225 u32 val;
226 int rx_fifo_avail;
227 u8 *buf = i2c_dev->msg_buf;
228 size_t buf_remaining = i2c_dev->msg_buf_remaining;
229 int words_to_transfer;
230
231 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
232 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
233 I2C_FIFO_STATUS_RX_SHIFT;
234
235 /* Rounds down to not include partial word at the end of buf */
236 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
237 if (words_to_transfer > rx_fifo_avail)
238 words_to_transfer = rx_fifo_avail;
239
240 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
241
242 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
243 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
244 rx_fifo_avail -= words_to_transfer;
245
246 /*
247 * If there is a partial word at the end of buf, handle it manually to
248 * prevent overwriting past the end of buf
249 */
250 if (rx_fifo_avail > 0 && buf_remaining > 0) {
251 BUG_ON(buf_remaining > 3);
252 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
253 memcpy(buf, &val, buf_remaining);
254 buf_remaining = 0;
255 rx_fifo_avail--;
256 }
257
258 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
259 i2c_dev->msg_buf_remaining = buf_remaining;
260 i2c_dev->msg_buf = buf;
261 return 0;
262}
263
264static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
265{
266 u32 val;
267 int tx_fifo_avail;
268 u8 *buf = i2c_dev->msg_buf;
269 size_t buf_remaining = i2c_dev->msg_buf_remaining;
270 int words_to_transfer;
271
272 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
273 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
274 I2C_FIFO_STATUS_TX_SHIFT;
275
276 /* Rounds down to not include partial word at the end of buf */
277 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800278
Doug Anderson96219c32011-08-30 11:46:10 -0600279 /* It's very common to have < 4 bytes, so optimize that case. */
280 if (words_to_transfer) {
281 if (words_to_transfer > tx_fifo_avail)
282 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800283
Doug Anderson96219c32011-08-30 11:46:10 -0600284 /*
285 * Update state before writing to FIFO. If this casues us
286 * to finish writing all bytes (AKA buf_remaining goes to 0) we
287 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
288 * not maskable). We need to make sure that the isr sees
289 * buf_remaining as 0 and doesn't call us back re-entrantly.
290 */
291 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
292 tx_fifo_avail -= words_to_transfer;
293 i2c_dev->msg_buf_remaining = buf_remaining;
294 i2c_dev->msg_buf = buf +
295 words_to_transfer * BYTES_PER_FIFO_WORD;
296 barrier();
297
298 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
299
300 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
301 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800302
303 /*
304 * If there is a partial word at the end of buf, handle it manually to
305 * prevent reading past the end of buf, which could cross a page
306 * boundary and fault.
307 */
308 if (tx_fifo_avail > 0 && buf_remaining > 0) {
309 BUG_ON(buf_remaining > 3);
310 memcpy(&val, buf, buf_remaining);
Doug Anderson96219c32011-08-30 11:46:10 -0600311
312 /* Again update before writing to FIFO to make sure isr sees. */
313 i2c_dev->msg_buf_remaining = 0;
314 i2c_dev->msg_buf = NULL;
315 barrier();
316
Colin Crossdb811ca2011-02-20 17:14:21 -0800317 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800318 }
319
Colin Crossdb811ca2011-02-20 17:14:21 -0800320 return 0;
321}
322
323/*
324 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
325 * block. This block is identical to the rest of the I2C blocks, except that
326 * it only supports master mode, it has registers moved around, and it needs
327 * some extra init to get it into I2C mode. The register moves are handled
328 * by i2c_readl and i2c_writel
329 */
330static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
331{
332 u32 val = 0;
333 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
334 val |= DVC_CTRL_REG3_SW_PROG;
335 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
336 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
337
338 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
339 val |= DVC_CTRL_REG1_INTR_EN;
340 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
341}
342
343static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
344{
345 u32 val;
346 int err = 0;
347
348 clk_enable(i2c_dev->clk);
349
350 tegra_periph_reset_assert(i2c_dev->clk);
351 udelay(2);
352 tegra_periph_reset_deassert(i2c_dev->clk);
353
354 if (i2c_dev->is_dvc)
355 tegra_dvc_init(i2c_dev);
356
Jay Cheng40abcf72011-04-25 15:32:27 -0600357 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
358 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800359 i2c_writel(i2c_dev, val, I2C_CNFG);
360 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
361 clk_set_rate(i2c_dev->clk, i2c_dev->bus_clk_rate * 8);
362
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600363 if (!i2c_dev->is_dvc) {
364 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Stephen Warren5afa9d32011-06-06 11:25:19 -0600365 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
366 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
367 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
368 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
369
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600370 }
371
Colin Crossdb811ca2011-02-20 17:14:21 -0800372 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
373 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
374 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
375
376 if (tegra_i2c_flush_fifos(i2c_dev))
377 err = -ETIMEDOUT;
378
379 clk_disable(i2c_dev->clk);
Todd Poynorcb63c622011-04-25 15:32:25 -0600380
381 if (i2c_dev->irq_disabled) {
382 i2c_dev->irq_disabled = 0;
383 enable_irq(i2c_dev->irq);
384 }
385
Colin Crossdb811ca2011-02-20 17:14:21 -0800386 return err;
387}
388
389static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
390{
391 u32 status;
392 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
393 struct tegra_i2c_dev *i2c_dev = dev_id;
394
395 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
396
397 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600398 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
399 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
400 i2c_readl(i2c_dev, I2C_STATUS),
401 i2c_readl(i2c_dev, I2C_CNFG));
402 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
403
404 if (!i2c_dev->irq_disabled) {
405 disable_irq_nosync(i2c_dev->irq);
406 i2c_dev->irq_disabled = 1;
407 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600408 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800409 }
410
411 if (unlikely(status & status_err)) {
412 if (status & I2C_INT_NO_ACK)
413 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
414 if (status & I2C_INT_ARBITRATION_LOST)
415 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800416 goto err;
417 }
418
419 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
420 if (i2c_dev->msg_buf_remaining)
421 tegra_i2c_empty_rx_fifo(i2c_dev);
422 else
423 BUG();
424 }
425
426 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
427 if (i2c_dev->msg_buf_remaining)
428 tegra_i2c_fill_tx_fifo(i2c_dev);
429 else
430 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
431 }
432
Laxman Dewanganc889e912012-05-07 12:16:19 +0530433 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
434 if (i2c_dev->is_dvc)
435 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
436
Doug Anderson96219c32011-08-30 11:46:10 -0600437 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
438 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800439 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600440 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800441 return IRQ_HANDLED;
442err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300443 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800444 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
445 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
446 I2C_INT_RX_FIFO_DATA_REQ);
447 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600448 if (i2c_dev->is_dvc)
449 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530450
451 complete(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800452 return IRQ_HANDLED;
453}
454
455static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
456 struct i2c_msg *msg, int stop)
457{
458 u32 packet_header;
459 u32 int_mask;
460 int ret;
461
462 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800463
464 if (msg->len == 0)
465 return -EINVAL;
466
467 i2c_dev->msg_buf = msg->buf;
468 i2c_dev->msg_buf_remaining = msg->len;
469 i2c_dev->msg_err = I2C_ERR_NONE;
470 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
471 INIT_COMPLETION(i2c_dev->msg_complete);
472
473 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
474 PACKET_HEADER0_PROTOCOL_I2C |
475 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
476 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
477 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
478
479 packet_header = msg->len - 1;
480 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
481
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530482 packet_header = I2C_HEADER_IE_ENABLE;
Erik Gilling2078cf32011-04-25 15:32:26 -0600483 if (!stop)
484 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530485 if (msg->flags & I2C_M_TEN) {
486 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800487 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530488 } else {
489 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
490 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800491 if (msg->flags & I2C_M_IGNORE_NAK)
492 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800493 if (msg->flags & I2C_M_RD)
494 packet_header |= I2C_HEADER_READ;
495 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
496
497 if (!(msg->flags & I2C_M_RD))
498 tegra_i2c_fill_tx_fifo(i2c_dev);
499
500 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
501 if (msg->flags & I2C_M_RD)
502 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
503 else if (i2c_dev->msg_buf_remaining)
504 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
505 tegra_i2c_unmask_irq(i2c_dev, int_mask);
506 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
507 i2c_readl(i2c_dev, I2C_INT_MASK));
508
509 ret = wait_for_completion_timeout(&i2c_dev->msg_complete, TEGRA_I2C_TIMEOUT);
510 tegra_i2c_mask_irq(i2c_dev, int_mask);
511
512 if (WARN_ON(ret == 0)) {
513 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
514
515 tegra_i2c_init(i2c_dev);
516 return -ETIMEDOUT;
517 }
518
519 dev_dbg(i2c_dev->dev, "transfer complete: %d %d %d\n",
520 ret, completion_done(&i2c_dev->msg_complete), i2c_dev->msg_err);
521
522 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
523 return 0;
524
Alok Chauhanf70893d02012-04-02 11:23:02 +0530525 /*
526 * NACK interrupt is generated before the I2C controller generates the
527 * STOP condition on the bus. So wait for 2 clock periods before resetting
528 * the controller so that STOP condition has been delivered properly.
529 */
530 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
531 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
532
Colin Crossdb811ca2011-02-20 17:14:21 -0800533 tegra_i2c_init(i2c_dev);
534 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
535 if (msg->flags & I2C_M_IGNORE_NAK)
536 return 0;
537 return -EREMOTEIO;
538 }
539
540 return -EIO;
541}
542
543static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
544 int num)
545{
546 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
547 int i;
548 int ret = 0;
549
550 if (i2c_dev->is_suspended)
551 return -EBUSY;
552
553 clk_enable(i2c_dev->clk);
554 for (i = 0; i < num; i++) {
555 int stop = (i == (num - 1)) ? 1 : 0;
556 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], stop);
557 if (ret)
558 break;
559 }
560 clk_disable(i2c_dev->clk);
561 return ret ?: i;
562}
563
564static u32 tegra_i2c_func(struct i2c_adapter *adap)
565{
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530566 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
Colin Crossdb811ca2011-02-20 17:14:21 -0800567}
568
569static const struct i2c_algorithm tegra_i2c_algo = {
570 .master_xfer = tegra_i2c_xfer,
571 .functionality = tegra_i2c_func,
572};
573
Stephen Warren92891da12011-12-17 23:29:29 -0700574static int __devinit tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800575{
576 struct tegra_i2c_dev *i2c_dev;
577 struct tegra_i2c_platform_data *pdata = pdev->dev.platform_data;
578 struct resource *res;
579 struct resource *iomem;
580 struct clk *clk;
581 struct clk *i2c_clk;
John Bonesio5c470f32011-06-22 09:16:56 -0700582 const unsigned int *prop;
Olof Johanssonf533c612011-10-12 17:33:00 -0700583 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800584 int irq;
585 int ret = 0;
586
587 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
588 if (!res) {
589 dev_err(&pdev->dev, "no mem resource\n");
590 return -EINVAL;
591 }
592 iomem = request_mem_region(res->start, resource_size(res), pdev->name);
593 if (!iomem) {
594 dev_err(&pdev->dev, "I2C region already claimed\n");
595 return -EBUSY;
596 }
597
598 base = ioremap(iomem->start, resource_size(iomem));
599 if (!base) {
600 dev_err(&pdev->dev, "Cannot ioremap I2C region\n");
601 return -ENOMEM;
602 }
603
604 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
605 if (!res) {
606 dev_err(&pdev->dev, "no irq resource\n");
607 ret = -EINVAL;
608 goto err_iounmap;
609 }
610 irq = res->start;
611
612 clk = clk_get(&pdev->dev, NULL);
613 if (IS_ERR(clk)) {
614 dev_err(&pdev->dev, "missing controller clock");
615 ret = PTR_ERR(clk);
616 goto err_release_region;
617 }
618
619 i2c_clk = clk_get(&pdev->dev, "i2c");
620 if (IS_ERR(i2c_clk)) {
621 dev_err(&pdev->dev, "missing bus clock");
622 ret = PTR_ERR(i2c_clk);
623 goto err_clk_put;
624 }
625
626 i2c_dev = kzalloc(sizeof(struct tegra_i2c_dev), GFP_KERNEL);
627 if (!i2c_dev) {
628 ret = -ENOMEM;
629 goto err_i2c_clk_put;
630 }
631
632 i2c_dev->base = base;
633 i2c_dev->clk = clk;
634 i2c_dev->i2c_clk = i2c_clk;
635 i2c_dev->iomem = iomem;
636 i2c_dev->adapter.algo = &tegra_i2c_algo;
637 i2c_dev->irq = irq;
638 i2c_dev->cont_id = pdev->id;
639 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700640
641 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
642 if (pdata) {
643 i2c_dev->bus_clk_rate = pdata->bus_clk_rate;
644
645 } else if (i2c_dev->dev->of_node) { /* if there is a device tree node ... */
646 prop = of_get_property(i2c_dev->dev->of_node,
647 "clock-frequency", NULL);
648 if (prop)
649 i2c_dev->bus_clk_rate = be32_to_cpup(prop);
650 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800651
Stephen Warren68fb6692011-12-17 23:29:30 -0700652 if (pdev->dev.of_node)
653 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
654 "nvidia,tegra20-i2c-dvc");
655 else if (pdev->id == 3)
Colin Crossdb811ca2011-02-20 17:14:21 -0800656 i2c_dev->is_dvc = 1;
657 init_completion(&i2c_dev->msg_complete);
658
659 platform_set_drvdata(pdev, i2c_dev);
660
661 ret = tegra_i2c_init(i2c_dev);
662 if (ret) {
663 dev_err(&pdev->dev, "Failed to initialize i2c controller");
664 goto err_free;
665 }
666
667 ret = request_irq(i2c_dev->irq, tegra_i2c_isr, 0, pdev->name, i2c_dev);
668 if (ret) {
669 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
670 goto err_free;
671 }
672
673 clk_enable(i2c_dev->i2c_clk);
674
675 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
676 i2c_dev->adapter.owner = THIS_MODULE;
677 i2c_dev->adapter.class = I2C_CLASS_HWMON;
678 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
679 sizeof(i2c_dev->adapter.name));
680 i2c_dev->adapter.algo = &tegra_i2c_algo;
681 i2c_dev->adapter.dev.parent = &pdev->dev;
682 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700683 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800684
685 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
686 if (ret) {
687 dev_err(&pdev->dev, "Failed to add I2C adapter\n");
688 goto err_free_irq;
689 }
690
John Bonesio5c470f32011-06-22 09:16:56 -0700691 of_i2c_register_devices(&i2c_dev->adapter);
692
Colin Crossdb811ca2011-02-20 17:14:21 -0800693 return 0;
694err_free_irq:
695 free_irq(i2c_dev->irq, i2c_dev);
696err_free:
697 kfree(i2c_dev);
698err_i2c_clk_put:
699 clk_put(i2c_clk);
700err_clk_put:
701 clk_put(clk);
702err_release_region:
703 release_mem_region(iomem->start, resource_size(iomem));
704err_iounmap:
705 iounmap(base);
706 return ret;
707}
708
Stephen Warren92891da12011-12-17 23:29:29 -0700709static int __devexit tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800710{
711 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
712 i2c_del_adapter(&i2c_dev->adapter);
713 free_irq(i2c_dev->irq, i2c_dev);
714 clk_put(i2c_dev->i2c_clk);
715 clk_put(i2c_dev->clk);
716 release_mem_region(i2c_dev->iomem->start,
717 resource_size(i2c_dev->iomem));
718 iounmap(i2c_dev->base);
719 kfree(i2c_dev);
720 return 0;
721}
722
723#ifdef CONFIG_PM
724static int tegra_i2c_suspend(struct platform_device *pdev, pm_message_t state)
725{
726 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
727
728 i2c_lock_adapter(&i2c_dev->adapter);
729 i2c_dev->is_suspended = true;
730 i2c_unlock_adapter(&i2c_dev->adapter);
731
732 return 0;
733}
734
735static int tegra_i2c_resume(struct platform_device *pdev)
736{
737 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
738 int ret;
739
740 i2c_lock_adapter(&i2c_dev->adapter);
741
742 ret = tegra_i2c_init(i2c_dev);
743
744 if (ret) {
745 i2c_unlock_adapter(&i2c_dev->adapter);
746 return ret;
747 }
748
749 i2c_dev->is_suspended = false;
750
751 i2c_unlock_adapter(&i2c_dev->adapter);
752
753 return 0;
754}
755#endif
756
John Bonesio406bd182011-08-30 11:46:08 -0600757#if defined(CONFIG_OF)
758/* Match table for of_platform binding */
759static const struct of_device_id tegra_i2c_of_match[] __devinitconst = {
760 { .compatible = "nvidia,tegra20-i2c", },
Stephen Warren68fb6692011-12-17 23:29:30 -0700761 { .compatible = "nvidia,tegra20-i2c-dvc", },
John Bonesio406bd182011-08-30 11:46:08 -0600762 {},
763};
764MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
765#else
766#define tegra_i2c_of_match NULL
767#endif
768
Colin Crossdb811ca2011-02-20 17:14:21 -0800769static struct platform_driver tegra_i2c_driver = {
770 .probe = tegra_i2c_probe,
Shubhrajyoti Datta218d06d2011-12-20 11:45:08 +0530771 .remove = __devexit_p(tegra_i2c_remove),
Colin Crossdb811ca2011-02-20 17:14:21 -0800772#ifdef CONFIG_PM
773 .suspend = tegra_i2c_suspend,
774 .resume = tegra_i2c_resume,
775#endif
776 .driver = {
777 .name = "tegra-i2c",
778 .owner = THIS_MODULE,
John Bonesio406bd182011-08-30 11:46:08 -0600779 .of_match_table = tegra_i2c_of_match,
Colin Crossdb811ca2011-02-20 17:14:21 -0800780 },
781};
782
783static int __init tegra_i2c_init_driver(void)
784{
785 return platform_driver_register(&tegra_i2c_driver);
786}
787
788static void __exit tegra_i2c_exit_driver(void)
789{
790 platform_driver_unregister(&tegra_i2c_driver);
791}
792
793subsys_initcall(tegra_i2c_init_driver);
794module_exit(tegra_i2c_exit_driver);
795
796MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
797MODULE_AUTHOR("Colin Cross");
798MODULE_LICENSE("GPL v2");