blob: c4593a24331c8284d41df2184e059a75f89f0fd3 [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)
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530100#define I2C_HEADER_CONTINUE_XFER (1<<15)
Colin Crossdb811ca2011-02-20 17:14:21 -0800101#define I2C_HEADER_MASTER_ADDR_SHIFT 12
102#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530103/*
104 * msg_end_type: The bus control which need to be send at end of transfer.
105 * @MSG_END_STOP: Send stop pulse at end of transfer.
106 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
107 * @MSG_END_CONTINUE: The following on message is coming and so do not send
108 * stop or repeat start.
109 */
110enum msg_end_type {
111 MSG_END_STOP,
112 MSG_END_REPEAT_START,
113 MSG_END_CONTINUE,
114};
Colin Crossdb811ca2011-02-20 17:14:21 -0800115
116/**
117 * struct tegra_i2c_dev - per device i2c context
118 * @dev: device reference for power management
119 * @adapter: core i2c layer adapter information
120 * @clk: clock reference for i2c controller
121 * @i2c_clk: clock reference for i2c bus
122 * @iomem: memory resource for registers
123 * @base: ioremapped registers cookie
124 * @cont_id: i2c controller id, used for for packet header
125 * @irq: irq number of transfer complete interrupt
126 * @is_dvc: identifies the DVC i2c controller, has a different register layout
127 * @msg_complete: transfer completion notifier
128 * @msg_err: error code for completed message
129 * @msg_buf: pointer to current message data
130 * @msg_buf_remaining: size of unsent data in the message buffer
131 * @msg_read: identifies read transfers
132 * @bus_clk_rate: current i2c bus clock rate
133 * @is_suspended: prevents i2c controller accesses after suspend is called
134 */
135struct tegra_i2c_dev {
136 struct device *dev;
137 struct i2c_adapter adapter;
138 struct clk *clk;
139 struct clk *i2c_clk;
140 struct resource *iomem;
141 void __iomem *base;
142 int cont_id;
143 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600144 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800145 int is_dvc;
146 struct completion msg_complete;
147 int msg_err;
148 u8 *msg_buf;
149 size_t msg_buf_remaining;
150 int msg_read;
151 unsigned long bus_clk_rate;
152 bool is_suspended;
153};
154
155static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
156{
157 writel(val, i2c_dev->base + reg);
158}
159
160static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
161{
162 return readl(i2c_dev->base + reg);
163}
164
165/*
166 * i2c_writel and i2c_readl will offset the register if necessary to talk
167 * to the I2C block inside the DVC block
168 */
169static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
170 unsigned long reg)
171{
172 if (i2c_dev->is_dvc)
173 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
174 return reg;
175}
176
177static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
178 unsigned long reg)
179{
180 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530181
182 /* Read back register to make sure that register writes completed */
183 if (reg != I2C_TX_FIFO)
184 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800185}
186
187static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
188{
189 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
190}
191
192static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
193 unsigned long reg, int len)
194{
195 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
196}
197
198static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
199 unsigned long reg, int len)
200{
201 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
202}
203
204static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
205{
206 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
207 int_mask &= ~mask;
208 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
209}
210
211static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
212{
213 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
214 int_mask |= mask;
215 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
216}
217
218static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
219{
220 unsigned long timeout = jiffies + HZ;
221 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
222 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
223 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
224
225 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
226 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
227 if (time_after(jiffies, timeout)) {
228 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
229 return -ETIMEDOUT;
230 }
231 msleep(1);
232 }
233 return 0;
234}
235
236static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
237{
238 u32 val;
239 int rx_fifo_avail;
240 u8 *buf = i2c_dev->msg_buf;
241 size_t buf_remaining = i2c_dev->msg_buf_remaining;
242 int words_to_transfer;
243
244 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
245 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
246 I2C_FIFO_STATUS_RX_SHIFT;
247
248 /* Rounds down to not include partial word at the end of buf */
249 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
250 if (words_to_transfer > rx_fifo_avail)
251 words_to_transfer = rx_fifo_avail;
252
253 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
254
255 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
256 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
257 rx_fifo_avail -= words_to_transfer;
258
259 /*
260 * If there is a partial word at the end of buf, handle it manually to
261 * prevent overwriting past the end of buf
262 */
263 if (rx_fifo_avail > 0 && buf_remaining > 0) {
264 BUG_ON(buf_remaining > 3);
265 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
266 memcpy(buf, &val, buf_remaining);
267 buf_remaining = 0;
268 rx_fifo_avail--;
269 }
270
271 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
272 i2c_dev->msg_buf_remaining = buf_remaining;
273 i2c_dev->msg_buf = buf;
274 return 0;
275}
276
277static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
278{
279 u32 val;
280 int tx_fifo_avail;
281 u8 *buf = i2c_dev->msg_buf;
282 size_t buf_remaining = i2c_dev->msg_buf_remaining;
283 int words_to_transfer;
284
285 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
286 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
287 I2C_FIFO_STATUS_TX_SHIFT;
288
289 /* Rounds down to not include partial word at the end of buf */
290 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800291
Doug Anderson96219c32011-08-30 11:46:10 -0600292 /* It's very common to have < 4 bytes, so optimize that case. */
293 if (words_to_transfer) {
294 if (words_to_transfer > tx_fifo_avail)
295 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800296
Doug Anderson96219c32011-08-30 11:46:10 -0600297 /*
298 * Update state before writing to FIFO. If this casues us
299 * to finish writing all bytes (AKA buf_remaining goes to 0) we
300 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
301 * not maskable). We need to make sure that the isr sees
302 * buf_remaining as 0 and doesn't call us back re-entrantly.
303 */
304 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
305 tx_fifo_avail -= words_to_transfer;
306 i2c_dev->msg_buf_remaining = buf_remaining;
307 i2c_dev->msg_buf = buf +
308 words_to_transfer * BYTES_PER_FIFO_WORD;
309 barrier();
310
311 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
312
313 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
314 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800315
316 /*
317 * If there is a partial word at the end of buf, handle it manually to
318 * prevent reading past the end of buf, which could cross a page
319 * boundary and fault.
320 */
321 if (tx_fifo_avail > 0 && buf_remaining > 0) {
322 BUG_ON(buf_remaining > 3);
323 memcpy(&val, buf, buf_remaining);
Doug Anderson96219c32011-08-30 11:46:10 -0600324
325 /* Again update before writing to FIFO to make sure isr sees. */
326 i2c_dev->msg_buf_remaining = 0;
327 i2c_dev->msg_buf = NULL;
328 barrier();
329
Colin Crossdb811ca2011-02-20 17:14:21 -0800330 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800331 }
332
Colin Crossdb811ca2011-02-20 17:14:21 -0800333 return 0;
334}
335
336/*
337 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
338 * block. This block is identical to the rest of the I2C blocks, except that
339 * it only supports master mode, it has registers moved around, and it needs
340 * some extra init to get it into I2C mode. The register moves are handled
341 * by i2c_readl and i2c_writel
342 */
343static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
344{
345 u32 val = 0;
346 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
347 val |= DVC_CTRL_REG3_SW_PROG;
348 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
349 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
350
351 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
352 val |= DVC_CTRL_REG1_INTR_EN;
353 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
354}
355
356static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
357{
358 u32 val;
359 int err = 0;
360
361 clk_enable(i2c_dev->clk);
362
363 tegra_periph_reset_assert(i2c_dev->clk);
364 udelay(2);
365 tegra_periph_reset_deassert(i2c_dev->clk);
366
367 if (i2c_dev->is_dvc)
368 tegra_dvc_init(i2c_dev);
369
Jay Cheng40abcf72011-04-25 15:32:27 -0600370 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
371 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800372 i2c_writel(i2c_dev, val, I2C_CNFG);
373 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
374 clk_set_rate(i2c_dev->clk, i2c_dev->bus_clk_rate * 8);
375
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600376 if (!i2c_dev->is_dvc) {
377 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Stephen Warren5afa9d32011-06-06 11:25:19 -0600378 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
379 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
380 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
381 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
382
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600383 }
384
Colin Crossdb811ca2011-02-20 17:14:21 -0800385 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
386 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
387 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
388
389 if (tegra_i2c_flush_fifos(i2c_dev))
390 err = -ETIMEDOUT;
391
392 clk_disable(i2c_dev->clk);
Todd Poynorcb63c622011-04-25 15:32:25 -0600393
394 if (i2c_dev->irq_disabled) {
395 i2c_dev->irq_disabled = 0;
396 enable_irq(i2c_dev->irq);
397 }
398
Colin Crossdb811ca2011-02-20 17:14:21 -0800399 return err;
400}
401
402static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
403{
404 u32 status;
405 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
406 struct tegra_i2c_dev *i2c_dev = dev_id;
407
408 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
409
410 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600411 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
412 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
413 i2c_readl(i2c_dev, I2C_STATUS),
414 i2c_readl(i2c_dev, I2C_CNFG));
415 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
416
417 if (!i2c_dev->irq_disabled) {
418 disable_irq_nosync(i2c_dev->irq);
419 i2c_dev->irq_disabled = 1;
420 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600421 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800422 }
423
424 if (unlikely(status & status_err)) {
425 if (status & I2C_INT_NO_ACK)
426 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
427 if (status & I2C_INT_ARBITRATION_LOST)
428 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800429 goto err;
430 }
431
432 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
433 if (i2c_dev->msg_buf_remaining)
434 tegra_i2c_empty_rx_fifo(i2c_dev);
435 else
436 BUG();
437 }
438
439 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
440 if (i2c_dev->msg_buf_remaining)
441 tegra_i2c_fill_tx_fifo(i2c_dev);
442 else
443 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
444 }
445
Laxman Dewanganc889e912012-05-07 12:16:19 +0530446 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
447 if (i2c_dev->is_dvc)
448 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
449
Doug Anderson96219c32011-08-30 11:46:10 -0600450 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
451 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800452 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600453 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800454 return IRQ_HANDLED;
455err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300456 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800457 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
458 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
459 I2C_INT_RX_FIFO_DATA_REQ);
460 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600461 if (i2c_dev->is_dvc)
462 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530463
464 complete(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800465 return IRQ_HANDLED;
466}
467
468static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530469 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -0800470{
471 u32 packet_header;
472 u32 int_mask;
473 int ret;
474
475 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800476
477 if (msg->len == 0)
478 return -EINVAL;
479
480 i2c_dev->msg_buf = msg->buf;
481 i2c_dev->msg_buf_remaining = msg->len;
482 i2c_dev->msg_err = I2C_ERR_NONE;
483 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
484 INIT_COMPLETION(i2c_dev->msg_complete);
485
486 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
487 PACKET_HEADER0_PROTOCOL_I2C |
488 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
489 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
490 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
491
492 packet_header = msg->len - 1;
493 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
494
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530495 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530496 if (end_state == MSG_END_CONTINUE)
497 packet_header |= I2C_HEADER_CONTINUE_XFER;
498 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -0600499 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530500 if (msg->flags & I2C_M_TEN) {
501 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800502 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530503 } else {
504 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
505 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800506 if (msg->flags & I2C_M_IGNORE_NAK)
507 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800508 if (msg->flags & I2C_M_RD)
509 packet_header |= I2C_HEADER_READ;
510 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
511
512 if (!(msg->flags & I2C_M_RD))
513 tegra_i2c_fill_tx_fifo(i2c_dev);
514
515 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
516 if (msg->flags & I2C_M_RD)
517 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
518 else if (i2c_dev->msg_buf_remaining)
519 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
520 tegra_i2c_unmask_irq(i2c_dev, int_mask);
521 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
522 i2c_readl(i2c_dev, I2C_INT_MASK));
523
524 ret = wait_for_completion_timeout(&i2c_dev->msg_complete, TEGRA_I2C_TIMEOUT);
525 tegra_i2c_mask_irq(i2c_dev, int_mask);
526
527 if (WARN_ON(ret == 0)) {
528 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
529
530 tegra_i2c_init(i2c_dev);
531 return -ETIMEDOUT;
532 }
533
534 dev_dbg(i2c_dev->dev, "transfer complete: %d %d %d\n",
535 ret, completion_done(&i2c_dev->msg_complete), i2c_dev->msg_err);
536
537 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
538 return 0;
539
Alok Chauhanf70893d02012-04-02 11:23:02 +0530540 /*
541 * NACK interrupt is generated before the I2C controller generates the
542 * STOP condition on the bus. So wait for 2 clock periods before resetting
543 * the controller so that STOP condition has been delivered properly.
544 */
545 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
546 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
547
Colin Crossdb811ca2011-02-20 17:14:21 -0800548 tegra_i2c_init(i2c_dev);
549 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
550 if (msg->flags & I2C_M_IGNORE_NAK)
551 return 0;
552 return -EREMOTEIO;
553 }
554
555 return -EIO;
556}
557
558static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
559 int num)
560{
561 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
562 int i;
563 int ret = 0;
564
565 if (i2c_dev->is_suspended)
566 return -EBUSY;
567
568 clk_enable(i2c_dev->clk);
569 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530570 enum msg_end_type end_type = MSG_END_STOP;
571 if (i < (num - 1)) {
572 if (msgs[i + 1].flags & I2C_M_NOSTART)
573 end_type = MSG_END_CONTINUE;
574 else
575 end_type = MSG_END_REPEAT_START;
576 }
577 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800578 if (ret)
579 break;
580 }
581 clk_disable(i2c_dev->clk);
582 return ret ?: i;
583}
584
585static u32 tegra_i2c_func(struct i2c_adapter *adap)
586{
Laxman Dewangana7018102012-06-13 15:42:37 +0530587 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530588 I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART;
Colin Crossdb811ca2011-02-20 17:14:21 -0800589}
590
591static const struct i2c_algorithm tegra_i2c_algo = {
592 .master_xfer = tegra_i2c_xfer,
593 .functionality = tegra_i2c_func,
594};
595
Stephen Warren92891da12011-12-17 23:29:29 -0700596static int __devinit tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800597{
598 struct tegra_i2c_dev *i2c_dev;
599 struct tegra_i2c_platform_data *pdata = pdev->dev.platform_data;
600 struct resource *res;
601 struct resource *iomem;
602 struct clk *clk;
603 struct clk *i2c_clk;
John Bonesio5c470f32011-06-22 09:16:56 -0700604 const unsigned int *prop;
Olof Johanssonf533c612011-10-12 17:33:00 -0700605 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800606 int irq;
607 int ret = 0;
608
609 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
610 if (!res) {
611 dev_err(&pdev->dev, "no mem resource\n");
612 return -EINVAL;
613 }
614 iomem = request_mem_region(res->start, resource_size(res), pdev->name);
615 if (!iomem) {
616 dev_err(&pdev->dev, "I2C region already claimed\n");
617 return -EBUSY;
618 }
619
620 base = ioremap(iomem->start, resource_size(iomem));
621 if (!base) {
622 dev_err(&pdev->dev, "Cannot ioremap I2C region\n");
623 return -ENOMEM;
624 }
625
626 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
627 if (!res) {
628 dev_err(&pdev->dev, "no irq resource\n");
629 ret = -EINVAL;
630 goto err_iounmap;
631 }
632 irq = res->start;
633
634 clk = clk_get(&pdev->dev, NULL);
635 if (IS_ERR(clk)) {
636 dev_err(&pdev->dev, "missing controller clock");
637 ret = PTR_ERR(clk);
638 goto err_release_region;
639 }
640
641 i2c_clk = clk_get(&pdev->dev, "i2c");
642 if (IS_ERR(i2c_clk)) {
643 dev_err(&pdev->dev, "missing bus clock");
644 ret = PTR_ERR(i2c_clk);
645 goto err_clk_put;
646 }
647
648 i2c_dev = kzalloc(sizeof(struct tegra_i2c_dev), GFP_KERNEL);
649 if (!i2c_dev) {
650 ret = -ENOMEM;
651 goto err_i2c_clk_put;
652 }
653
654 i2c_dev->base = base;
655 i2c_dev->clk = clk;
656 i2c_dev->i2c_clk = i2c_clk;
657 i2c_dev->iomem = iomem;
658 i2c_dev->adapter.algo = &tegra_i2c_algo;
659 i2c_dev->irq = irq;
660 i2c_dev->cont_id = pdev->id;
661 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700662
663 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
664 if (pdata) {
665 i2c_dev->bus_clk_rate = pdata->bus_clk_rate;
666
667 } else if (i2c_dev->dev->of_node) { /* if there is a device tree node ... */
668 prop = of_get_property(i2c_dev->dev->of_node,
669 "clock-frequency", NULL);
670 if (prop)
671 i2c_dev->bus_clk_rate = be32_to_cpup(prop);
672 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800673
Stephen Warren68fb6692011-12-17 23:29:30 -0700674 if (pdev->dev.of_node)
675 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
676 "nvidia,tegra20-i2c-dvc");
677 else if (pdev->id == 3)
Colin Crossdb811ca2011-02-20 17:14:21 -0800678 i2c_dev->is_dvc = 1;
679 init_completion(&i2c_dev->msg_complete);
680
681 platform_set_drvdata(pdev, i2c_dev);
682
683 ret = tegra_i2c_init(i2c_dev);
684 if (ret) {
685 dev_err(&pdev->dev, "Failed to initialize i2c controller");
686 goto err_free;
687 }
688
689 ret = request_irq(i2c_dev->irq, tegra_i2c_isr, 0, pdev->name, i2c_dev);
690 if (ret) {
691 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
692 goto err_free;
693 }
694
695 clk_enable(i2c_dev->i2c_clk);
696
697 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
698 i2c_dev->adapter.owner = THIS_MODULE;
699 i2c_dev->adapter.class = I2C_CLASS_HWMON;
700 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
701 sizeof(i2c_dev->adapter.name));
702 i2c_dev->adapter.algo = &tegra_i2c_algo;
703 i2c_dev->adapter.dev.parent = &pdev->dev;
704 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700705 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800706
707 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
708 if (ret) {
709 dev_err(&pdev->dev, "Failed to add I2C adapter\n");
710 goto err_free_irq;
711 }
712
John Bonesio5c470f32011-06-22 09:16:56 -0700713 of_i2c_register_devices(&i2c_dev->adapter);
714
Colin Crossdb811ca2011-02-20 17:14:21 -0800715 return 0;
716err_free_irq:
717 free_irq(i2c_dev->irq, i2c_dev);
718err_free:
719 kfree(i2c_dev);
720err_i2c_clk_put:
721 clk_put(i2c_clk);
722err_clk_put:
723 clk_put(clk);
724err_release_region:
725 release_mem_region(iomem->start, resource_size(iomem));
726err_iounmap:
727 iounmap(base);
728 return ret;
729}
730
Stephen Warren92891da12011-12-17 23:29:29 -0700731static int __devexit tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800732{
733 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
734 i2c_del_adapter(&i2c_dev->adapter);
735 free_irq(i2c_dev->irq, i2c_dev);
736 clk_put(i2c_dev->i2c_clk);
737 clk_put(i2c_dev->clk);
738 release_mem_region(i2c_dev->iomem->start,
739 resource_size(i2c_dev->iomem));
740 iounmap(i2c_dev->base);
741 kfree(i2c_dev);
742 return 0;
743}
744
745#ifdef CONFIG_PM
746static int tegra_i2c_suspend(struct platform_device *pdev, pm_message_t state)
747{
748 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
749
750 i2c_lock_adapter(&i2c_dev->adapter);
751 i2c_dev->is_suspended = true;
752 i2c_unlock_adapter(&i2c_dev->adapter);
753
754 return 0;
755}
756
757static int tegra_i2c_resume(struct platform_device *pdev)
758{
759 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
760 int ret;
761
762 i2c_lock_adapter(&i2c_dev->adapter);
763
764 ret = tegra_i2c_init(i2c_dev);
765
766 if (ret) {
767 i2c_unlock_adapter(&i2c_dev->adapter);
768 return ret;
769 }
770
771 i2c_dev->is_suspended = false;
772
773 i2c_unlock_adapter(&i2c_dev->adapter);
774
775 return 0;
776}
777#endif
778
John Bonesio406bd182011-08-30 11:46:08 -0600779#if defined(CONFIG_OF)
780/* Match table for of_platform binding */
781static const struct of_device_id tegra_i2c_of_match[] __devinitconst = {
782 { .compatible = "nvidia,tegra20-i2c", },
Stephen Warren68fb6692011-12-17 23:29:30 -0700783 { .compatible = "nvidia,tegra20-i2c-dvc", },
John Bonesio406bd182011-08-30 11:46:08 -0600784 {},
785};
786MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
787#else
788#define tegra_i2c_of_match NULL
789#endif
790
Colin Crossdb811ca2011-02-20 17:14:21 -0800791static struct platform_driver tegra_i2c_driver = {
792 .probe = tegra_i2c_probe,
Shubhrajyoti Datta218d06d2011-12-20 11:45:08 +0530793 .remove = __devexit_p(tegra_i2c_remove),
Colin Crossdb811ca2011-02-20 17:14:21 -0800794#ifdef CONFIG_PM
795 .suspend = tegra_i2c_suspend,
796 .resume = tegra_i2c_resume,
797#endif
798 .driver = {
799 .name = "tegra-i2c",
800 .owner = THIS_MODULE,
John Bonesio406bd182011-08-30 11:46:08 -0600801 .of_match_table = tegra_i2c_of_match,
Colin Crossdb811ca2011-02-20 17:14:21 -0800802 },
803};
804
805static int __init tegra_i2c_init_driver(void)
806{
807 return platform_driver_register(&tegra_i2c_driver);
808}
809
810static void __exit tegra_i2c_exit_driver(void)
811{
812 platform_driver_unregister(&tegra_i2c_driver);
813}
814
815subsys_initcall(tegra_i2c_init_driver);
816module_exit(tegra_i2c_exit_driver);
817
818MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
819MODULE_AUTHOR("Colin Cross");
820MODULE_LICENSE("GPL v2");