blob: d70a2fda4a915671ed226dbf4cbb9b0b037a8436 [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
Maxime Ripard683e69b2013-06-12 18:53:30 +020036#define MV64XXX_I2C_ADDR_ADDR(val) ((val & 0x7f) << 1)
37#define MV64XXX_I2C_BAUD_DIV_N(val) (val & 0x7)
38#define MV64XXX_I2C_BAUD_DIV_M(val) ((val & 0xf) << 3)
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define MV64XXX_I2C_REG_CONTROL_ACK 0x00000004
41#define MV64XXX_I2C_REG_CONTROL_IFLG 0x00000008
42#define MV64XXX_I2C_REG_CONTROL_STOP 0x00000010
43#define MV64XXX_I2C_REG_CONTROL_START 0x00000020
44#define MV64XXX_I2C_REG_CONTROL_TWSIEN 0x00000040
45#define MV64XXX_I2C_REG_CONTROL_INTEN 0x00000080
46
47/* Ctlr status values */
48#define MV64XXX_I2C_STATUS_BUS_ERR 0x00
49#define MV64XXX_I2C_STATUS_MAST_START 0x08
50#define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10
51#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18
52#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20
53#define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28
54#define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30
55#define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38
56#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40
57#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48
58#define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50
59#define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58
60#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0
61#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8
62#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0
63#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8
64#define MV64XXX_I2C_STATUS_NO_STATUS 0xf8
65
66/* Driver states */
67enum {
68 MV64XXX_I2C_STATE_INVALID,
69 MV64XXX_I2C_STATE_IDLE,
70 MV64XXX_I2C_STATE_WAITING_FOR_START_COND,
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +010071 MV64XXX_I2C_STATE_WAITING_FOR_RESTART,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK,
73 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK,
74 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK,
75 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
78/* Driver actions */
79enum {
80 MV64XXX_I2C_ACTION_INVALID,
81 MV64XXX_I2C_ACTION_CONTINUE,
82 MV64XXX_I2C_ACTION_SEND_START,
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +010083 MV64XXX_I2C_ACTION_SEND_RESTART,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 MV64XXX_I2C_ACTION_SEND_ADDR_1,
85 MV64XXX_I2C_ACTION_SEND_ADDR_2,
86 MV64XXX_I2C_ACTION_SEND_DATA,
87 MV64XXX_I2C_ACTION_RCV_DATA,
88 MV64XXX_I2C_ACTION_RCV_DATA_STOP,
89 MV64XXX_I2C_ACTION_SEND_STOP,
90};
91
92struct mv64xxx_i2c_data {
Russell King4243fa02013-05-16 21:39:12 +010093 struct i2c_msg *msgs;
94 int num_msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int irq;
96 u32 state;
97 u32 action;
Mark A. Greere91c0212005-12-18 17:22:01 +010098 u32 aborting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 u32 cntl_bits;
100 void __iomem *reg_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 u32 addr1;
102 u32 addr2;
103 u32 bytes_left;
104 u32 byte_posn;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100105 u32 send_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 u32 block;
107 int rc;
108 u32 freq_m;
109 u32 freq_n;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200110#if defined(CONFIG_HAVE_CLK)
111 struct clk *clk;
112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 wait_queue_head_t waitq;
114 spinlock_t lock;
115 struct i2c_msg *msg;
116 struct i2c_adapter adapter;
117};
118
Russell King3420afb2013-05-16 21:38:11 +0100119static void
120mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
121 struct i2c_msg *msg)
122{
123 u32 dir = 0;
124
125 drv_data->msg = msg;
126 drv_data->byte_posn = 0;
127 drv_data->bytes_left = msg->len;
128 drv_data->aborting = 0;
129 drv_data->rc = 0;
130 drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
131 MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
132
133 if (msg->flags & I2C_M_RD)
134 dir = 1;
135
136 if (msg->flags & I2C_M_TEN) {
137 drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
138 drv_data->addr2 = (u32)msg->addr & 0xff;
139 } else {
Maxime Ripard683e69b2013-06-12 18:53:30 +0200140 drv_data->addr1 = MV64XXX_I2C_ADDR_ADDR((u32)msg->addr) | dir;
Russell King3420afb2013-05-16 21:38:11 +0100141 drv_data->addr2 = 0;
142 }
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146 *****************************************************************************
147 *
148 * Finite State Machine & Interrupt Routines
149 *
150 *****************************************************************************
151 */
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200152
153/* Reset hardware and initialize FSM */
154static void
155mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
156{
157 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SOFT_RESET);
Maxime Ripard683e69b2013-06-12 18:53:30 +0200158 writel(MV64XXX_I2C_BAUD_DIV_M(drv_data->freq_m) | MV64XXX_I2C_BAUD_DIV_N(drv_data->freq_n),
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200159 drv_data->reg_base + MV64XXX_I2C_REG_BAUD);
160 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SLAVE_ADDR);
161 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_EXT_SLAVE_ADDR);
162 writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
163 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
164 drv_data->state = MV64XXX_I2C_STATE_IDLE;
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void
168mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
169{
170 /*
171 * If state is idle, then this is likely the remnants of an old
172 * operation that driver has given up on or the user has killed.
173 * If so, issue the stop condition and go to idle.
174 */
175 if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
176 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
177 return;
178 }
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* The status from the ctlr [mostly] tells us what to do next */
181 switch (status) {
182 /* Start condition interrupt */
183 case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
184 case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
185 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
186 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
187 break;
188
189 /* Performing a write */
190 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
191 if (drv_data->msg->flags & I2C_M_TEN) {
192 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
193 drv_data->state =
194 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
195 break;
196 }
197 /* FALLTHRU */
198 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
199 case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
Mark A. Greere91c0212005-12-18 17:22:01 +0100200 if ((drv_data->bytes_left == 0)
201 || (drv_data->aborting
202 && (drv_data->byte_posn != 0))) {
Russell King4243fa02013-05-16 21:39:12 +0100203 if (drv_data->send_stop || drv_data->aborting) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100204 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
205 drv_data->state = MV64XXX_I2C_STATE_IDLE;
206 } else {
207 drv_data->action =
208 MV64XXX_I2C_ACTION_SEND_RESTART;
209 drv_data->state =
210 MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
211 }
Mark A. Greere91c0212005-12-18 17:22:01 +0100212 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
214 drv_data->state =
215 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
216 drv_data->bytes_left--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 break;
219
220 /* Performing a read */
221 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
222 if (drv_data->msg->flags & I2C_M_TEN) {
223 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
224 drv_data->state =
225 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
226 break;
227 }
228 /* FALLTHRU */
229 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
230 if (drv_data->bytes_left == 0) {
231 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
232 drv_data->state = MV64XXX_I2C_STATE_IDLE;
233 break;
234 }
235 /* FALLTHRU */
236 case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
237 if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
238 drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
239 else {
240 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
241 drv_data->bytes_left--;
242 }
243 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
244
Mark A. Greere91c0212005-12-18 17:22:01 +0100245 if ((drv_data->bytes_left == 1) || drv_data->aborting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
247 break;
248
249 case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
250 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
251 drv_data->state = MV64XXX_I2C_STATE_IDLE;
252 break;
253
254 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
255 case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
256 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
257 /* Doesn't seem to be a device at other end */
258 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
259 drv_data->state = MV64XXX_I2C_STATE_IDLE;
260 drv_data->rc = -ENODEV;
261 break;
262
263 default:
264 dev_err(&drv_data->adapter.dev,
265 "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
266 "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
267 drv_data->state, status, drv_data->msg->addr,
268 drv_data->msg->flags);
269 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200270 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 drv_data->rc = -EIO;
272 }
273}
274
275static void
276mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
277{
278 switch(drv_data->action) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100279 case MV64XXX_I2C_ACTION_SEND_RESTART:
Russell King4243fa02013-05-16 21:39:12 +0100280 /* We should only get here if we have further messages */
281 BUG_ON(drv_data->num_msgs == 0);
282
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100283 drv_data->cntl_bits |= MV64XXX_I2C_REG_CONTROL_START;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100284 writel(drv_data->cntl_bits,
285 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
Russell King4243fa02013-05-16 21:39:12 +0100286
287 drv_data->msgs++;
288 drv_data->num_msgs--;
289
290 /* Setup for the next message */
291 mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs);
292
293 /*
294 * We're never at the start of the message here, and by this
295 * time it's already too late to do any protocol mangling.
296 * Thankfully, do not advertise support for that feature.
297 */
298 drv_data->send_stop = drv_data->num_msgs == 1;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100299 break;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 case MV64XXX_I2C_ACTION_CONTINUE:
302 writel(drv_data->cntl_bits,
303 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
304 break;
305
306 case MV64XXX_I2C_ACTION_SEND_START:
307 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
308 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
309 break;
310
311 case MV64XXX_I2C_ACTION_SEND_ADDR_1:
312 writel(drv_data->addr1,
313 drv_data->reg_base + MV64XXX_I2C_REG_DATA);
314 writel(drv_data->cntl_bits,
315 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
316 break;
317
318 case MV64XXX_I2C_ACTION_SEND_ADDR_2:
319 writel(drv_data->addr2,
320 drv_data->reg_base + MV64XXX_I2C_REG_DATA);
321 writel(drv_data->cntl_bits,
322 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
323 break;
324
325 case MV64XXX_I2C_ACTION_SEND_DATA:
326 writel(drv_data->msg->buf[drv_data->byte_posn++],
327 drv_data->reg_base + MV64XXX_I2C_REG_DATA);
328 writel(drv_data->cntl_bits,
329 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
330 break;
331
332 case MV64XXX_I2C_ACTION_RCV_DATA:
333 drv_data->msg->buf[drv_data->byte_posn++] =
334 readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA);
335 writel(drv_data->cntl_bits,
336 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
337 break;
338
339 case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
340 drv_data->msg->buf[drv_data->byte_posn++] =
341 readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA);
342 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
343 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
344 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
345 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000346 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 break;
348
349 case MV64XXX_I2C_ACTION_INVALID:
350 default:
351 dev_err(&drv_data->adapter.dev,
352 "mv64xxx_i2c_do_action: Invalid action: %d\n",
353 drv_data->action);
354 drv_data->rc = -EIO;
355 /* FALLTHRU */
356 case MV64XXX_I2C_ACTION_SEND_STOP:
357 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
358 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
359 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
360 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000361 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 break;
363 }
364}
365
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200366static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100367mv64xxx_i2c_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct mv64xxx_i2c_data *drv_data = dev_id;
370 unsigned long flags;
371 u32 status;
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200372 irqreturn_t rc = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 spin_lock_irqsave(&drv_data->lock, flags);
375 while (readl(drv_data->reg_base + MV64XXX_I2C_REG_CONTROL) &
376 MV64XXX_I2C_REG_CONTROL_IFLG) {
377 status = readl(drv_data->reg_base + MV64XXX_I2C_REG_STATUS);
378 mv64xxx_i2c_fsm(drv_data, status);
379 mv64xxx_i2c_do_action(drv_data);
380 rc = IRQ_HANDLED;
381 }
382 spin_unlock_irqrestore(&drv_data->lock, flags);
383
384 return rc;
385}
386
387/*
388 *****************************************************************************
389 *
390 * I2C Msg Execution Routines
391 *
392 *****************************************************************************
393 */
394static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
396{
397 long time_left;
398 unsigned long flags;
399 char abort = 0;
400
Russell Kingd295a862013-05-16 10:30:59 +0000401 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100402 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 spin_lock_irqsave(&drv_data->lock, flags);
405 if (!time_left) { /* Timed out */
406 drv_data->rc = -ETIMEDOUT;
407 abort = 1;
408 } else if (time_left < 0) { /* Interrupted/Error */
409 drv_data->rc = time_left; /* errno value */
410 abort = 1;
411 }
412
413 if (abort && drv_data->block) {
Mark A. Greere91c0212005-12-18 17:22:01 +0100414 drv_data->aborting = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 spin_unlock_irqrestore(&drv_data->lock, flags);
416
417 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100418 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Mark A. Greere91c0212005-12-18 17:22:01 +0100420 if ((time_left <= 0) && drv_data->block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 drv_data->state = MV64XXX_I2C_STATE_IDLE;
422 dev_err(&drv_data->adapter.dev,
Mark A. Greere91c0212005-12-18 17:22:01 +0100423 "mv64xxx: I2C bus locked, block: %d, "
424 "time_left: %d\n", drv_data->block,
425 (int)time_left);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200426 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 } else
429 spin_unlock_irqrestore(&drv_data->lock, flags);
430}
431
432static int
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100433mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg,
Russell King4243fa02013-05-16 21:39:12 +0100434 int is_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 unsigned long flags;
437
438 spin_lock_irqsave(&drv_data->lock, flags);
439 mv64xxx_i2c_prepare_for_io(drv_data, msg);
440
Russell King4243fa02013-05-16 21:39:12 +0100441 drv_data->action = MV64XXX_I2C_ACTION_SEND_START;
442 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100444 drv_data->send_stop = is_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 drv_data->block = 1;
446 mv64xxx_i2c_do_action(drv_data);
447 spin_unlock_irqrestore(&drv_data->lock, flags);
448
449 mv64xxx_i2c_wait_for_completion(drv_data);
450 return drv_data->rc;
451}
452
453/*
454 *****************************************************************************
455 *
456 * I2C Core Support Routines (Interface to higher level I2C code)
457 *
458 *****************************************************************************
459 */
460static u32
461mv64xxx_i2c_functionality(struct i2c_adapter *adap)
462{
463 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
464}
465
466static int
467mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
468{
469 struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
Russell King4243fa02013-05-16 21:39:12 +0100470 int rc, ret = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Russell King4243fa02013-05-16 21:39:12 +0100472 BUG_ON(drv_data->msgs != NULL);
473 drv_data->msgs = msgs;
474 drv_data->num_msgs = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Russell King4243fa02013-05-16 21:39:12 +0100476 rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1);
477 if (rc < 0)
478 ret = rc;
479
480 drv_data->num_msgs = 0;
481 drv_data->msgs = NULL;
482
483 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Jean Delvare8f9082c2006-09-03 22:39:46 +0200486static const struct i2c_algorithm mv64xxx_i2c_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 .master_xfer = mv64xxx_i2c_xfer,
488 .functionality = mv64xxx_i2c_functionality,
489};
490
491/*
492 *****************************************************************************
493 *
494 * Driver Interface & Early Init Routines
495 *
496 *****************************************************************************
497 */
Andrew Lunnb61d1572012-07-22 12:51:35 +0200498#ifdef CONFIG_OF
Bill Pemberton0b255e92012-11-27 15:59:38 -0500499static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200500mv64xxx_calc_freq(const int tclk, const int n, const int m)
501{
502 return tclk / (10 * (m + 1) * (2 << n));
503}
504
Bill Pemberton0b255e92012-11-27 15:59:38 -0500505static bool
Andrew Lunnb61d1572012-07-22 12:51:35 +0200506mv64xxx_find_baud_factors(const u32 req_freq, const u32 tclk, u32 *best_n,
507 u32 *best_m)
508{
509 int freq, delta, best_delta = INT_MAX;
510 int m, n;
511
512 for (n = 0; n <= 7; n++)
513 for (m = 0; m <= 15; m++) {
514 freq = mv64xxx_calc_freq(tclk, n, m);
515 delta = req_freq - freq;
516 if (delta >= 0 && delta < best_delta) {
517 *best_m = m;
518 *best_n = n;
519 best_delta = delta;
520 }
521 if (best_delta == 0)
522 return true;
523 }
524 if (best_delta == INT_MAX)
525 return false;
526 return true;
527}
528
Bill Pemberton0b255e92012-11-27 15:59:38 -0500529static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200530mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
531 struct device_node *np)
532{
533 u32 bus_freq, tclk;
534 int rc = 0;
535
536 /* CLK is mandatory when using DT to describe the i2c bus. We
537 * need to know tclk in order to calculate bus clock
538 * factors.
539 */
540#if !defined(CONFIG_HAVE_CLK)
541 /* Have OF but no CLK */
542 return -ENODEV;
543#else
544 if (IS_ERR(drv_data->clk)) {
545 rc = -ENODEV;
546 goto out;
547 }
548 tclk = clk_get_rate(drv_data->clk);
549 of_property_read_u32(np, "clock-frequency", &bus_freq);
550 if (!mv64xxx_find_baud_factors(bus_freq, tclk,
551 &drv_data->freq_n, &drv_data->freq_m)) {
552 rc = -EINVAL;
553 goto out;
554 }
555 drv_data->irq = irq_of_parse_and_map(np, 0);
556
557 /* Its not yet defined how timeouts will be specified in device tree.
558 * So hard code the value to 1 second.
559 */
560 drv_data->adapter.timeout = HZ;
561out:
562 return rc;
563#endif
564}
565#else /* CONFIG_OF */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500566static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200567mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
568 struct device_node *np)
569{
570 return -ENODEV;
571}
572#endif /* CONFIG_OF */
573
Bill Pemberton0b255e92012-11-27 15:59:38 -0500574static int
Russell King3ae5eae2005-11-09 22:32:44 +0000575mv64xxx_i2c_probe(struct platform_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct mv64xxx_i2c_data *drv_data;
Russell King3ae5eae2005-11-09 22:32:44 +0000578 struct mv64xxx_i2c_pdata *pdata = pd->dev.platform_data;
Russell King16874b02013-05-16 21:33:09 +0100579 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int rc;
581
Andrew Lunnb61d1572012-07-22 12:51:35 +0200582 if ((!pdata && !pd->dev.of_node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -ENODEV;
584
Russell King2c911102013-05-16 21:35:10 +0100585 drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data),
586 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!drv_data)
588 return -ENOMEM;
589
Russell King16874b02013-05-16 21:33:09 +0100590 r = platform_get_resource(pd, IORESOURCE_MEM, 0);
591 drv_data->reg_base = devm_ioremap_resource(&pd->dev, r);
Russell King2c911102013-05-16 21:35:10 +0100592 if (IS_ERR(drv_data->reg_base))
593 return PTR_ERR(drv_data->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Mark A. Greere91c0212005-12-18 17:22:01 +0100595 strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
David Brownell2096b952007-05-01 23:26:28 +0200596 sizeof(drv_data->adapter.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 init_waitqueue_head(&drv_data->waitq);
599 spin_lock_init(&drv_data->lock);
600
Andrew Lunnb61d1572012-07-22 12:51:35 +0200601#if defined(CONFIG_HAVE_CLK)
602 /* Not all platforms have a clk */
Russell King4c5c95f2013-05-16 21:34:10 +0100603 drv_data->clk = devm_clk_get(&pd->dev, NULL);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200604 if (!IS_ERR(drv_data->clk)) {
605 clk_prepare(drv_data->clk);
606 clk_enable(drv_data->clk);
607 }
608#endif
609 if (pdata) {
610 drv_data->freq_m = pdata->freq_m;
611 drv_data->freq_n = pdata->freq_n;
612 drv_data->irq = platform_get_irq(pd, 0);
613 drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
614 } else if (pd->dev.of_node) {
615 rc = mv64xxx_of_config(drv_data, pd->dev.of_node);
616 if (rc)
Russell King2c911102013-05-16 21:35:10 +0100617 goto exit_clk;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200618 }
David Vrabel48944732006-01-19 17:56:29 +0000619 if (drv_data->irq < 0) {
620 rc = -ENXIO;
Russell King2c911102013-05-16 21:35:10 +0100621 goto exit_clk;
David Vrabel48944732006-01-19 17:56:29 +0000622 }
Andrew Lunnb61d1572012-07-22 12:51:35 +0200623
Jean Delvare12a917f2007-02-13 22:09:03 +0100624 drv_data->adapter.dev.parent = &pd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 drv_data->adapter.algo = &mv64xxx_i2c_algo;
626 drv_data->adapter.owner = THIS_MODULE;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200627 drv_data->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200628 drv_data->adapter.nr = pd->id;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200629 drv_data->adapter.dev.of_node = pd->dev.of_node;
Russell King3ae5eae2005-11-09 22:32:44 +0000630 platform_set_drvdata(pd, drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 i2c_set_adapdata(&drv_data->adapter, drv_data);
632
Maxime Bizon3269bb62007-01-05 17:54:05 +0100633 mv64xxx_i2c_hw_init(drv_data);
634
Russell King0c195af2013-05-16 21:36:11 +0100635 rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
636 MV64XXX_I2C_CTLR_NAME, drv_data);
637 if (rc) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800638 dev_err(&drv_data->adapter.dev,
Russell King0c195af2013-05-16 21:36:11 +0100639 "mv64xxx: Can't register intr handler irq%d: %d\n",
640 drv_data->irq, rc);
Russell King2c911102013-05-16 21:35:10 +0100641 goto exit_clk;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200642 } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800643 dev_err(&drv_data->adapter.dev,
644 "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto exit_free_irq;
646 }
647
Andrew Lunnb61d1572012-07-22 12:51:35 +0200648 of_i2c_register_devices(&drv_data->adapter);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
651
Russell King2c911102013-05-16 21:35:10 +0100652exit_free_irq:
653 free_irq(drv_data->irq, drv_data);
654exit_clk:
Andrew Lunnb61d1572012-07-22 12:51:35 +0200655#if defined(CONFIG_HAVE_CLK)
656 /* Not all platforms have a clk */
657 if (!IS_ERR(drv_data->clk)) {
658 clk_disable(drv_data->clk);
659 clk_unprepare(drv_data->clk);
660 }
661#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return rc;
663}
664
Bill Pemberton0b255e92012-11-27 15:59:38 -0500665static int
Russell King3ae5eae2005-11-09 22:32:44 +0000666mv64xxx_i2c_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Russell King3ae5eae2005-11-09 22:32:44 +0000668 struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000670 i2c_del_adapter(&drv_data->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 free_irq(drv_data->irq, drv_data);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200672#if defined(CONFIG_HAVE_CLK)
673 /* Not all platforms have a clk */
674 if (!IS_ERR(drv_data->clk)) {
675 clk_disable(drv_data->clk);
676 clk_unprepare(drv_data->clk);
677 }
678#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Bill Pemberton0b255e92012-11-27 15:59:38 -0500683static const struct of_device_id mv64xxx_i2c_of_match_table[] = {
Andrew Lunnb61d1572012-07-22 12:51:35 +0200684 { .compatible = "marvell,mv64xxx-i2c", },
685 {}
686};
687MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table);
688
Russell King3ae5eae2005-11-09 22:32:44 +0000689static struct platform_driver mv64xxx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 .probe = mv64xxx_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500691 .remove = mv64xxx_i2c_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000692 .driver = {
693 .owner = THIS_MODULE,
694 .name = MV64XXX_I2C_CTLR_NAME,
Andrew Lunnb61d1572012-07-22 12:51:35 +0200695 .of_match_table = of_match_ptr(mv64xxx_i2c_of_match_table),
Russell King3ae5eae2005-11-09 22:32:44 +0000696 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697};
698
Axel Lina3664b52012-01-12 20:32:04 +0100699module_platform_driver(mv64xxx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
702MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
703MODULE_LICENSE("GPL");