blob: 640b49df2ddde4bf9e07aa2e6a972f68df0321fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tzachi Perelsteina0832792007-11-12 19:38:51 +02002 * Driver for the i2c controller on the Marvell line of host bridges
3 * (e.g, gt642[46]0, mv643[46]0, mv644[46]0, and Orion SoC family).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Author: Mark A. Greer <mgreer@mvista.com>
6 *
7 * 2005 (c) MontaVista, Software, Inc. This file is licensed under
8 * the terms of the GNU General Public License version 2. This program
9 * is licensed "as is" without any warranty of any kind, whether express
10 * or implied.
11 */
12#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/spinlock.h>
16#include <linux/i2c.h>
17#include <linux/interrupt.h>
Tzachi Perelsteina0832792007-11-12 19:38:51 +020018#include <linux/mv643xx_i2c.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010019#include <linux/platform_device.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020020#include <linux/io.h>
Andrew Lunnb61d1572012-07-22 12:51:35 +020021#include <linux/of.h>
22#include <linux/of_irq.h>
23#include <linux/of_i2c.h>
24#include <linux/clk.h>
25#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/* Register defines */
28#define MV64XXX_I2C_REG_SLAVE_ADDR 0x00
29#define MV64XXX_I2C_REG_DATA 0x04
30#define MV64XXX_I2C_REG_CONTROL 0x08
31#define MV64XXX_I2C_REG_STATUS 0x0c
32#define MV64XXX_I2C_REG_BAUD 0x0c
33#define MV64XXX_I2C_REG_EXT_SLAVE_ADDR 0x10
34#define MV64XXX_I2C_REG_SOFT_RESET 0x1c
35
36#define MV64XXX_I2C_REG_CONTROL_ACK 0x00000004
37#define MV64XXX_I2C_REG_CONTROL_IFLG 0x00000008
38#define MV64XXX_I2C_REG_CONTROL_STOP 0x00000010
39#define MV64XXX_I2C_REG_CONTROL_START 0x00000020
40#define MV64XXX_I2C_REG_CONTROL_TWSIEN 0x00000040
41#define MV64XXX_I2C_REG_CONTROL_INTEN 0x00000080
42
43/* Ctlr status values */
44#define MV64XXX_I2C_STATUS_BUS_ERR 0x00
45#define MV64XXX_I2C_STATUS_MAST_START 0x08
46#define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10
47#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18
48#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20
49#define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28
50#define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30
51#define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38
52#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40
53#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48
54#define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50
55#define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58
56#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0
57#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8
58#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0
59#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8
60#define MV64XXX_I2C_STATUS_NO_STATUS 0xf8
61
62/* Driver states */
63enum {
64 MV64XXX_I2C_STATE_INVALID,
65 MV64XXX_I2C_STATE_IDLE,
66 MV64XXX_I2C_STATE_WAITING_FOR_START_COND,
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +010067 MV64XXX_I2C_STATE_WAITING_FOR_RESTART,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK,
69 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK,
70 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK,
71 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
74/* Driver actions */
75enum {
76 MV64XXX_I2C_ACTION_INVALID,
77 MV64XXX_I2C_ACTION_CONTINUE,
78 MV64XXX_I2C_ACTION_SEND_START,
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +010079 MV64XXX_I2C_ACTION_SEND_RESTART,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 MV64XXX_I2C_ACTION_SEND_ADDR_1,
81 MV64XXX_I2C_ACTION_SEND_ADDR_2,
82 MV64XXX_I2C_ACTION_SEND_DATA,
83 MV64XXX_I2C_ACTION_RCV_DATA,
84 MV64XXX_I2C_ACTION_RCV_DATA_STOP,
85 MV64XXX_I2C_ACTION_SEND_STOP,
86};
87
88struct mv64xxx_i2c_data {
89 int irq;
90 u32 state;
91 u32 action;
Mark A. Greere91c0212005-12-18 17:22:01 +010092 u32 aborting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 u32 cntl_bits;
94 void __iomem *reg_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 u32 addr1;
96 u32 addr2;
97 u32 bytes_left;
98 u32 byte_posn;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +010099 u32 send_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 u32 block;
101 int rc;
102 u32 freq_m;
103 u32 freq_n;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200104#if defined(CONFIG_HAVE_CLK)
105 struct clk *clk;
106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 wait_queue_head_t waitq;
108 spinlock_t lock;
109 struct i2c_msg *msg;
110 struct i2c_adapter adapter;
111};
112
113/*
114 *****************************************************************************
115 *
116 * Finite State Machine & Interrupt Routines
117 *
118 *****************************************************************************
119 */
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200120
121/* Reset hardware and initialize FSM */
122static void
123mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
124{
125 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SOFT_RESET);
126 writel((((drv_data->freq_m & 0xf) << 3) | (drv_data->freq_n & 0x7)),
127 drv_data->reg_base + MV64XXX_I2C_REG_BAUD);
128 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SLAVE_ADDR);
129 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_EXT_SLAVE_ADDR);
130 writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
131 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
132 drv_data->state = MV64XXX_I2C_STATE_IDLE;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static void
136mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
137{
138 /*
139 * If state is idle, then this is likely the remnants of an old
140 * operation that driver has given up on or the user has killed.
141 * If so, issue the stop condition and go to idle.
142 */
143 if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
144 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
145 return;
146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* The status from the ctlr [mostly] tells us what to do next */
149 switch (status) {
150 /* Start condition interrupt */
151 case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
152 case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
153 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
154 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
155 break;
156
157 /* Performing a write */
158 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
159 if (drv_data->msg->flags & I2C_M_TEN) {
160 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
161 drv_data->state =
162 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
163 break;
164 }
165 /* FALLTHRU */
166 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
167 case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
Mark A. Greere91c0212005-12-18 17:22:01 +0100168 if ((drv_data->bytes_left == 0)
169 || (drv_data->aborting
170 && (drv_data->byte_posn != 0))) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100171 if (drv_data->send_stop) {
172 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
173 drv_data->state = MV64XXX_I2C_STATE_IDLE;
174 } else {
175 drv_data->action =
176 MV64XXX_I2C_ACTION_SEND_RESTART;
177 drv_data->state =
178 MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
179 }
Mark A. Greere91c0212005-12-18 17:22:01 +0100180 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
182 drv_data->state =
183 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
184 drv_data->bytes_left--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 break;
187
188 /* Performing a read */
189 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
190 if (drv_data->msg->flags & I2C_M_TEN) {
191 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
192 drv_data->state =
193 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
194 break;
195 }
196 /* FALLTHRU */
197 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
198 if (drv_data->bytes_left == 0) {
199 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
200 drv_data->state = MV64XXX_I2C_STATE_IDLE;
201 break;
202 }
203 /* FALLTHRU */
204 case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
205 if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
206 drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
207 else {
208 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
209 drv_data->bytes_left--;
210 }
211 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
212
Mark A. Greere91c0212005-12-18 17:22:01 +0100213 if ((drv_data->bytes_left == 1) || drv_data->aborting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
215 break;
216
217 case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
218 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
219 drv_data->state = MV64XXX_I2C_STATE_IDLE;
220 break;
221
222 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
223 case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
224 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
225 /* Doesn't seem to be a device at other end */
226 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
227 drv_data->state = MV64XXX_I2C_STATE_IDLE;
228 drv_data->rc = -ENODEV;
229 break;
230
231 default:
232 dev_err(&drv_data->adapter.dev,
233 "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
234 "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
235 drv_data->state, status, drv_data->msg->addr,
236 drv_data->msg->flags);
237 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200238 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 drv_data->rc = -EIO;
240 }
241}
242
243static void
244mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
245{
246 switch(drv_data->action) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100247 case MV64XXX_I2C_ACTION_SEND_RESTART:
248 drv_data->cntl_bits |= MV64XXX_I2C_REG_CONTROL_START;
249 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
250 writel(drv_data->cntl_bits,
251 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
252 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000253 wake_up(&drv_data->waitq);
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100254 break;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 case MV64XXX_I2C_ACTION_CONTINUE:
257 writel(drv_data->cntl_bits,
258 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
259 break;
260
261 case MV64XXX_I2C_ACTION_SEND_START:
262 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
263 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
264 break;
265
266 case MV64XXX_I2C_ACTION_SEND_ADDR_1:
267 writel(drv_data->addr1,
268 drv_data->reg_base + MV64XXX_I2C_REG_DATA);
269 writel(drv_data->cntl_bits,
270 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
271 break;
272
273 case MV64XXX_I2C_ACTION_SEND_ADDR_2:
274 writel(drv_data->addr2,
275 drv_data->reg_base + MV64XXX_I2C_REG_DATA);
276 writel(drv_data->cntl_bits,
277 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
278 break;
279
280 case MV64XXX_I2C_ACTION_SEND_DATA:
281 writel(drv_data->msg->buf[drv_data->byte_posn++],
282 drv_data->reg_base + MV64XXX_I2C_REG_DATA);
283 writel(drv_data->cntl_bits,
284 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
285 break;
286
287 case MV64XXX_I2C_ACTION_RCV_DATA:
288 drv_data->msg->buf[drv_data->byte_posn++] =
289 readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA);
290 writel(drv_data->cntl_bits,
291 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
292 break;
293
294 case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
295 drv_data->msg->buf[drv_data->byte_posn++] =
296 readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA);
297 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
298 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
299 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
300 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000301 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303
304 case MV64XXX_I2C_ACTION_INVALID:
305 default:
306 dev_err(&drv_data->adapter.dev,
307 "mv64xxx_i2c_do_action: Invalid action: %d\n",
308 drv_data->action);
309 drv_data->rc = -EIO;
310 /* FALLTHRU */
311 case MV64XXX_I2C_ACTION_SEND_STOP:
312 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
313 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
314 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
315 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000316 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
318 }
319}
320
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200321static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100322mv64xxx_i2c_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct mv64xxx_i2c_data *drv_data = dev_id;
325 unsigned long flags;
326 u32 status;
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200327 irqreturn_t rc = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 spin_lock_irqsave(&drv_data->lock, flags);
330 while (readl(drv_data->reg_base + MV64XXX_I2C_REG_CONTROL) &
331 MV64XXX_I2C_REG_CONTROL_IFLG) {
332 status = readl(drv_data->reg_base + MV64XXX_I2C_REG_STATUS);
333 mv64xxx_i2c_fsm(drv_data, status);
334 mv64xxx_i2c_do_action(drv_data);
335 rc = IRQ_HANDLED;
336 }
337 spin_unlock_irqrestore(&drv_data->lock, flags);
338
339 return rc;
340}
341
342/*
343 *****************************************************************************
344 *
345 * I2C Msg Execution Routines
346 *
347 *****************************************************************************
348 */
349static void
350mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
351 struct i2c_msg *msg)
352{
353 u32 dir = 0;
354
355 drv_data->msg = msg;
356 drv_data->byte_posn = 0;
357 drv_data->bytes_left = msg->len;
Mark A. Greere91c0212005-12-18 17:22:01 +0100358 drv_data->aborting = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 drv_data->rc = 0;
360 drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
361 MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
362
363 if (msg->flags & I2C_M_RD)
364 dir = 1;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (msg->flags & I2C_M_TEN) {
367 drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
368 drv_data->addr2 = (u32)msg->addr & 0xff;
369 } else {
370 drv_data->addr1 = ((u32)msg->addr & 0x7f) << 1 | dir;
371 drv_data->addr2 = 0;
372 }
373}
374
375static void
376mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
377{
378 long time_left;
379 unsigned long flags;
380 char abort = 0;
381
Russell Kingd295a862013-05-16 10:30:59 +0000382 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100383 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 spin_lock_irqsave(&drv_data->lock, flags);
386 if (!time_left) { /* Timed out */
387 drv_data->rc = -ETIMEDOUT;
388 abort = 1;
389 } else if (time_left < 0) { /* Interrupted/Error */
390 drv_data->rc = time_left; /* errno value */
391 abort = 1;
392 }
393
394 if (abort && drv_data->block) {
Mark A. Greere91c0212005-12-18 17:22:01 +0100395 drv_data->aborting = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_unlock_irqrestore(&drv_data->lock, flags);
397
398 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100399 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Mark A. Greere91c0212005-12-18 17:22:01 +0100401 if ((time_left <= 0) && drv_data->block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 drv_data->state = MV64XXX_I2C_STATE_IDLE;
403 dev_err(&drv_data->adapter.dev,
Mark A. Greere91c0212005-12-18 17:22:01 +0100404 "mv64xxx: I2C bus locked, block: %d, "
405 "time_left: %d\n", drv_data->block,
406 (int)time_left);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200407 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 } else
410 spin_unlock_irqrestore(&drv_data->lock, flags);
411}
412
413static int
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100414mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg,
415 int is_first, int is_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 unsigned long flags;
418
419 spin_lock_irqsave(&drv_data->lock, flags);
420 mv64xxx_i2c_prepare_for_io(drv_data, msg);
421
422 if (unlikely(msg->flags & I2C_M_NOSTART)) { /* Skip start/addr phases */
423 if (drv_data->msg->flags & I2C_M_RD) {
424 /* No action to do, wait for slave to send a byte */
425 drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
426 drv_data->state =
427 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
428 } else {
429 drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
430 drv_data->state =
431 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
432 drv_data->bytes_left--;
433 }
434 } else {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100435 if (is_first) {
436 drv_data->action = MV64XXX_I2C_ACTION_SEND_START;
437 drv_data->state =
438 MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
439 } else {
440 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
441 drv_data->state =
442 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100446 drv_data->send_stop = is_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 drv_data->block = 1;
448 mv64xxx_i2c_do_action(drv_data);
449 spin_unlock_irqrestore(&drv_data->lock, flags);
450
451 mv64xxx_i2c_wait_for_completion(drv_data);
452 return drv_data->rc;
453}
454
455/*
456 *****************************************************************************
457 *
458 * I2C Core Support Routines (Interface to higher level I2C code)
459 *
460 *****************************************************************************
461 */
462static u32
463mv64xxx_i2c_functionality(struct i2c_adapter *adap)
464{
465 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
466}
467
468static int
469mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
470{
471 struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
Jean Delvared1b2f0a2005-09-02 21:25:47 +0200472 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100474 for (i = 0; i < num; i++) {
475 rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i],
476 i == 0, i + 1 == num);
477 if (rc < 0)
Jean Delvared1b2f0a2005-09-02 21:25:47 +0200478 return rc;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jean Delvared1b2f0a2005-09-02 21:25:47 +0200481 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Jean Delvare8f9082c2006-09-03 22:39:46 +0200484static const struct i2c_algorithm mv64xxx_i2c_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .master_xfer = mv64xxx_i2c_xfer,
486 .functionality = mv64xxx_i2c_functionality,
487};
488
489/*
490 *****************************************************************************
491 *
492 * Driver Interface & Early Init Routines
493 *
494 *****************************************************************************
495 */
Andrew Lunnb61d1572012-07-22 12:51:35 +0200496#ifdef CONFIG_OF
Bill Pemberton0b255e92012-11-27 15:59:38 -0500497static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200498mv64xxx_calc_freq(const int tclk, const int n, const int m)
499{
500 return tclk / (10 * (m + 1) * (2 << n));
501}
502
Bill Pemberton0b255e92012-11-27 15:59:38 -0500503static bool
Andrew Lunnb61d1572012-07-22 12:51:35 +0200504mv64xxx_find_baud_factors(const u32 req_freq, const u32 tclk, u32 *best_n,
505 u32 *best_m)
506{
507 int freq, delta, best_delta = INT_MAX;
508 int m, n;
509
510 for (n = 0; n <= 7; n++)
511 for (m = 0; m <= 15; m++) {
512 freq = mv64xxx_calc_freq(tclk, n, m);
513 delta = req_freq - freq;
514 if (delta >= 0 && delta < best_delta) {
515 *best_m = m;
516 *best_n = n;
517 best_delta = delta;
518 }
519 if (best_delta == 0)
520 return true;
521 }
522 if (best_delta == INT_MAX)
523 return false;
524 return true;
525}
526
Bill Pemberton0b255e92012-11-27 15:59:38 -0500527static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200528mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
529 struct device_node *np)
530{
531 u32 bus_freq, tclk;
532 int rc = 0;
533
534 /* CLK is mandatory when using DT to describe the i2c bus. We
535 * need to know tclk in order to calculate bus clock
536 * factors.
537 */
538#if !defined(CONFIG_HAVE_CLK)
539 /* Have OF but no CLK */
540 return -ENODEV;
541#else
542 if (IS_ERR(drv_data->clk)) {
543 rc = -ENODEV;
544 goto out;
545 }
546 tclk = clk_get_rate(drv_data->clk);
547 of_property_read_u32(np, "clock-frequency", &bus_freq);
548 if (!mv64xxx_find_baud_factors(bus_freq, tclk,
549 &drv_data->freq_n, &drv_data->freq_m)) {
550 rc = -EINVAL;
551 goto out;
552 }
553 drv_data->irq = irq_of_parse_and_map(np, 0);
554
555 /* Its not yet defined how timeouts will be specified in device tree.
556 * So hard code the value to 1 second.
557 */
558 drv_data->adapter.timeout = HZ;
559out:
560 return rc;
561#endif
562}
563#else /* CONFIG_OF */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500564static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200565mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
566 struct device_node *np)
567{
568 return -ENODEV;
569}
570#endif /* CONFIG_OF */
571
Bill Pemberton0b255e92012-11-27 15:59:38 -0500572static int
Russell King3ae5eae2005-11-09 22:32:44 +0000573mv64xxx_i2c_probe(struct platform_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct mv64xxx_i2c_data *drv_data;
Russell King3ae5eae2005-11-09 22:32:44 +0000576 struct mv64xxx_i2c_pdata *pdata = pd->dev.platform_data;
Russell King16874b02013-05-16 21:33:09 +0100577 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int rc;
579
Andrew Lunnb61d1572012-07-22 12:51:35 +0200580 if ((!pdata && !pd->dev.of_node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return -ENODEV;
582
Russell King2c911102013-05-16 21:35:10 +0100583 drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data),
584 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!drv_data)
586 return -ENOMEM;
587
Russell King16874b02013-05-16 21:33:09 +0100588 r = platform_get_resource(pd, IORESOURCE_MEM, 0);
589 drv_data->reg_base = devm_ioremap_resource(&pd->dev, r);
Russell King2c911102013-05-16 21:35:10 +0100590 if (IS_ERR(drv_data->reg_base))
591 return PTR_ERR(drv_data->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Mark A. Greere91c0212005-12-18 17:22:01 +0100593 strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
David Brownell2096b952007-05-01 23:26:28 +0200594 sizeof(drv_data->adapter.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 init_waitqueue_head(&drv_data->waitq);
597 spin_lock_init(&drv_data->lock);
598
Andrew Lunnb61d1572012-07-22 12:51:35 +0200599#if defined(CONFIG_HAVE_CLK)
600 /* Not all platforms have a clk */
Russell King4c5c95f2013-05-16 21:34:10 +0100601 drv_data->clk = devm_clk_get(&pd->dev, NULL);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200602 if (!IS_ERR(drv_data->clk)) {
603 clk_prepare(drv_data->clk);
604 clk_enable(drv_data->clk);
605 }
606#endif
607 if (pdata) {
608 drv_data->freq_m = pdata->freq_m;
609 drv_data->freq_n = pdata->freq_n;
610 drv_data->irq = platform_get_irq(pd, 0);
611 drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
612 } else if (pd->dev.of_node) {
613 rc = mv64xxx_of_config(drv_data, pd->dev.of_node);
614 if (rc)
Russell King2c911102013-05-16 21:35:10 +0100615 goto exit_clk;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200616 }
David Vrabel48944732006-01-19 17:56:29 +0000617 if (drv_data->irq < 0) {
618 rc = -ENXIO;
Russell King2c911102013-05-16 21:35:10 +0100619 goto exit_clk;
David Vrabel48944732006-01-19 17:56:29 +0000620 }
Andrew Lunnb61d1572012-07-22 12:51:35 +0200621
Jean Delvare12a917f2007-02-13 22:09:03 +0100622 drv_data->adapter.dev.parent = &pd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 drv_data->adapter.algo = &mv64xxx_i2c_algo;
624 drv_data->adapter.owner = THIS_MODULE;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200625 drv_data->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200626 drv_data->adapter.nr = pd->id;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200627 drv_data->adapter.dev.of_node = pd->dev.of_node;
Russell King3ae5eae2005-11-09 22:32:44 +0000628 platform_set_drvdata(pd, drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 i2c_set_adapdata(&drv_data->adapter, drv_data);
630
Maxime Bizon3269bb62007-01-05 17:54:05 +0100631 mv64xxx_i2c_hw_init(drv_data);
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800634 MV64XXX_I2C_CTLR_NAME, drv_data)) {
635 dev_err(&drv_data->adapter.dev,
636 "mv64xxx: Can't register intr handler irq: %d\n",
637 drv_data->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 rc = -EINVAL;
Russell King2c911102013-05-16 21:35:10 +0100639 goto exit_clk;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200640 } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800641 dev_err(&drv_data->adapter.dev,
642 "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto exit_free_irq;
644 }
645
Andrew Lunnb61d1572012-07-22 12:51:35 +0200646 of_i2c_register_devices(&drv_data->adapter);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return 0;
649
Russell King2c911102013-05-16 21:35:10 +0100650exit_free_irq:
651 free_irq(drv_data->irq, drv_data);
652exit_clk:
Andrew Lunnb61d1572012-07-22 12:51:35 +0200653#if defined(CONFIG_HAVE_CLK)
654 /* Not all platforms have a clk */
655 if (!IS_ERR(drv_data->clk)) {
656 clk_disable(drv_data->clk);
657 clk_unprepare(drv_data->clk);
658 }
659#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return rc;
661}
662
Bill Pemberton0b255e92012-11-27 15:59:38 -0500663static int
Russell King3ae5eae2005-11-09 22:32:44 +0000664mv64xxx_i2c_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Russell King3ae5eae2005-11-09 22:32:44 +0000666 struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000668 i2c_del_adapter(&drv_data->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 free_irq(drv_data->irq, drv_data);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200670#if defined(CONFIG_HAVE_CLK)
671 /* Not all platforms have a clk */
672 if (!IS_ERR(drv_data->clk)) {
673 clk_disable(drv_data->clk);
674 clk_unprepare(drv_data->clk);
675 }
676#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Bill Pemberton0b255e92012-11-27 15:59:38 -0500681static const struct of_device_id mv64xxx_i2c_of_match_table[] = {
Andrew Lunnb61d1572012-07-22 12:51:35 +0200682 { .compatible = "marvell,mv64xxx-i2c", },
683 {}
684};
685MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table);
686
Russell King3ae5eae2005-11-09 22:32:44 +0000687static struct platform_driver mv64xxx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 .probe = mv64xxx_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500689 .remove = mv64xxx_i2c_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000690 .driver = {
691 .owner = THIS_MODULE,
692 .name = MV64XXX_I2C_CTLR_NAME,
Andrew Lunnb61d1572012-07-22 12:51:35 +0200693 .of_match_table = of_match_ptr(mv64xxx_i2c_of_match_table),
Russell King3ae5eae2005-11-09 22:32:44 +0000694 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695};
696
Axel Lina3664b52012-01-12 20:32:04 +0100697module_platform_driver(mv64xxx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
700MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
701MODULE_LICENSE("GPL");