blob: 7ba9bac18478f1c59bc17510465437bd20d98c0e [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>
Maxime Ripard004e8ed2013-06-12 18:53:31 +020022#include <linux/of_device.h>
Andrew Lunnb61d1572012-07-22 12:51:35 +020023#include <linux/of_irq.h>
24#include <linux/of_i2c.h>
25#include <linux/clk.h>
26#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Maxime Ripard683e69b2013-06-12 18:53:30 +020028#define MV64XXX_I2C_ADDR_ADDR(val) ((val & 0x7f) << 1)
29#define MV64XXX_I2C_BAUD_DIV_N(val) (val & 0x7)
30#define MV64XXX_I2C_BAUD_DIV_M(val) ((val & 0xf) << 3)
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define MV64XXX_I2C_REG_CONTROL_ACK 0x00000004
33#define MV64XXX_I2C_REG_CONTROL_IFLG 0x00000008
34#define MV64XXX_I2C_REG_CONTROL_STOP 0x00000010
35#define MV64XXX_I2C_REG_CONTROL_START 0x00000020
36#define MV64XXX_I2C_REG_CONTROL_TWSIEN 0x00000040
37#define MV64XXX_I2C_REG_CONTROL_INTEN 0x00000080
38
39/* Ctlr status values */
40#define MV64XXX_I2C_STATUS_BUS_ERR 0x00
41#define MV64XXX_I2C_STATUS_MAST_START 0x08
42#define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10
43#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18
44#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20
45#define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28
46#define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30
47#define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38
48#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40
49#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48
50#define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50
51#define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58
52#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0
53#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8
54#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0
55#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8
56#define MV64XXX_I2C_STATUS_NO_STATUS 0xf8
57
58/* Driver states */
59enum {
60 MV64XXX_I2C_STATE_INVALID,
61 MV64XXX_I2C_STATE_IDLE,
62 MV64XXX_I2C_STATE_WAITING_FOR_START_COND,
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +010063 MV64XXX_I2C_STATE_WAITING_FOR_RESTART,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK,
65 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK,
66 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK,
67 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70/* Driver actions */
71enum {
72 MV64XXX_I2C_ACTION_INVALID,
73 MV64XXX_I2C_ACTION_CONTINUE,
74 MV64XXX_I2C_ACTION_SEND_START,
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +010075 MV64XXX_I2C_ACTION_SEND_RESTART,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 MV64XXX_I2C_ACTION_SEND_ADDR_1,
77 MV64XXX_I2C_ACTION_SEND_ADDR_2,
78 MV64XXX_I2C_ACTION_SEND_DATA,
79 MV64XXX_I2C_ACTION_RCV_DATA,
80 MV64XXX_I2C_ACTION_RCV_DATA_STOP,
81 MV64XXX_I2C_ACTION_SEND_STOP,
82};
83
Maxime Ripard004e8ed2013-06-12 18:53:31 +020084struct mv64xxx_i2c_regs {
85 u8 addr;
86 u8 ext_addr;
87 u8 data;
88 u8 control;
89 u8 status;
90 u8 clock;
91 u8 soft_reset;
92};
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct mv64xxx_i2c_data {
Russell King4243fa02013-05-16 21:39:12 +010095 struct i2c_msg *msgs;
96 int num_msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int irq;
98 u32 state;
99 u32 action;
Mark A. Greere91c0212005-12-18 17:22:01 +0100100 u32 aborting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 u32 cntl_bits;
102 void __iomem *reg_base;
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200103 struct mv64xxx_i2c_regs reg_offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 u32 addr1;
105 u32 addr2;
106 u32 bytes_left;
107 u32 byte_posn;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100108 u32 send_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 u32 block;
110 int rc;
111 u32 freq_m;
112 u32 freq_n;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200113#if defined(CONFIG_HAVE_CLK)
114 struct clk *clk;
115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 wait_queue_head_t waitq;
117 spinlock_t lock;
118 struct i2c_msg *msg;
119 struct i2c_adapter adapter;
120};
121
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200122static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_mv64xxx = {
123 .addr = 0x00,
124 .ext_addr = 0x10,
125 .data = 0x04,
126 .control = 0x08,
127 .status = 0x0c,
128 .clock = 0x0c,
129 .soft_reset = 0x1c,
130};
131
Russell King3420afb2013-05-16 21:38:11 +0100132static void
133mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
134 struct i2c_msg *msg)
135{
136 u32 dir = 0;
137
138 drv_data->msg = msg;
139 drv_data->byte_posn = 0;
140 drv_data->bytes_left = msg->len;
141 drv_data->aborting = 0;
142 drv_data->rc = 0;
143 drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
144 MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
145
146 if (msg->flags & I2C_M_RD)
147 dir = 1;
148
149 if (msg->flags & I2C_M_TEN) {
150 drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
151 drv_data->addr2 = (u32)msg->addr & 0xff;
152 } else {
Maxime Ripard683e69b2013-06-12 18:53:30 +0200153 drv_data->addr1 = MV64XXX_I2C_ADDR_ADDR((u32)msg->addr) | dir;
Russell King3420afb2013-05-16 21:38:11 +0100154 drv_data->addr2 = 0;
155 }
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
159 *****************************************************************************
160 *
161 * Finite State Machine & Interrupt Routines
162 *
163 *****************************************************************************
164 */
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200165
166/* Reset hardware and initialize FSM */
167static void
168mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
169{
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200170 writel(0, drv_data->reg_base + drv_data->reg_offsets.soft_reset);
Maxime Ripard683e69b2013-06-12 18:53:30 +0200171 writel(MV64XXX_I2C_BAUD_DIV_M(drv_data->freq_m) | MV64XXX_I2C_BAUD_DIV_N(drv_data->freq_n),
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200172 drv_data->reg_base + drv_data->reg_offsets.clock);
173 writel(0, drv_data->reg_base + drv_data->reg_offsets.addr);
174 writel(0, drv_data->reg_base + drv_data->reg_offsets.ext_addr);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200175 writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200176 drv_data->reg_base + drv_data->reg_offsets.control);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200177 drv_data->state = MV64XXX_I2C_STATE_IDLE;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void
181mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
182{
183 /*
184 * If state is idle, then this is likely the remnants of an old
185 * operation that driver has given up on or the user has killed.
186 * If so, issue the stop condition and go to idle.
187 */
188 if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
189 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
190 return;
191 }
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* The status from the ctlr [mostly] tells us what to do next */
194 switch (status) {
195 /* Start condition interrupt */
196 case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
197 case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
198 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
199 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
200 break;
201
202 /* Performing a write */
203 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
204 if (drv_data->msg->flags & I2C_M_TEN) {
205 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
206 drv_data->state =
207 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
208 break;
209 }
210 /* FALLTHRU */
211 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
212 case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
Mark A. Greere91c0212005-12-18 17:22:01 +0100213 if ((drv_data->bytes_left == 0)
214 || (drv_data->aborting
215 && (drv_data->byte_posn != 0))) {
Russell King4243fa02013-05-16 21:39:12 +0100216 if (drv_data->send_stop || drv_data->aborting) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100217 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
218 drv_data->state = MV64XXX_I2C_STATE_IDLE;
219 } else {
220 drv_data->action =
221 MV64XXX_I2C_ACTION_SEND_RESTART;
222 drv_data->state =
223 MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
224 }
Mark A. Greere91c0212005-12-18 17:22:01 +0100225 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
227 drv_data->state =
228 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
229 drv_data->bytes_left--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 break;
232
233 /* Performing a read */
234 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
235 if (drv_data->msg->flags & I2C_M_TEN) {
236 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
237 drv_data->state =
238 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
239 break;
240 }
241 /* FALLTHRU */
242 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
243 if (drv_data->bytes_left == 0) {
244 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
245 drv_data->state = MV64XXX_I2C_STATE_IDLE;
246 break;
247 }
248 /* FALLTHRU */
249 case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
250 if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
251 drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
252 else {
253 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
254 drv_data->bytes_left--;
255 }
256 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
257
Mark A. Greere91c0212005-12-18 17:22:01 +0100258 if ((drv_data->bytes_left == 1) || drv_data->aborting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
260 break;
261
262 case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
263 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
264 drv_data->state = MV64XXX_I2C_STATE_IDLE;
265 break;
266
267 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
268 case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
269 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
270 /* Doesn't seem to be a device at other end */
271 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
272 drv_data->state = MV64XXX_I2C_STATE_IDLE;
273 drv_data->rc = -ENODEV;
274 break;
275
276 default:
277 dev_err(&drv_data->adapter.dev,
278 "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
279 "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
280 drv_data->state, status, drv_data->msg->addr,
281 drv_data->msg->flags);
282 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200283 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 drv_data->rc = -EIO;
285 }
286}
287
288static void
289mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
290{
291 switch(drv_data->action) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100292 case MV64XXX_I2C_ACTION_SEND_RESTART:
Russell King4243fa02013-05-16 21:39:12 +0100293 /* We should only get here if we have further messages */
294 BUG_ON(drv_data->num_msgs == 0);
295
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100296 drv_data->cntl_bits |= MV64XXX_I2C_REG_CONTROL_START;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100297 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200298 drv_data->reg_base + drv_data->reg_offsets.control);
Russell King4243fa02013-05-16 21:39:12 +0100299
300 drv_data->msgs++;
301 drv_data->num_msgs--;
302
303 /* Setup for the next message */
304 mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs);
305
306 /*
307 * We're never at the start of the message here, and by this
308 * time it's already too late to do any protocol mangling.
309 * Thankfully, do not advertise support for that feature.
310 */
311 drv_data->send_stop = drv_data->num_msgs == 1;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100312 break;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case MV64XXX_I2C_ACTION_CONTINUE:
315 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200316 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
318
319 case MV64XXX_I2C_ACTION_SEND_START:
320 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200321 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
323
324 case MV64XXX_I2C_ACTION_SEND_ADDR_1:
325 writel(drv_data->addr1,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200326 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200328 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
330
331 case MV64XXX_I2C_ACTION_SEND_ADDR_2:
332 writel(drv_data->addr2,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200333 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200335 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
337
338 case MV64XXX_I2C_ACTION_SEND_DATA:
339 writel(drv_data->msg->buf[drv_data->byte_posn++],
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200340 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200342 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 break;
344
345 case MV64XXX_I2C_ACTION_RCV_DATA:
346 drv_data->msg->buf[drv_data->byte_posn++] =
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200347 readl(drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200349 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351
352 case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
353 drv_data->msg->buf[drv_data->byte_posn++] =
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200354 readl(drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
356 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200357 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000359 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361
362 case MV64XXX_I2C_ACTION_INVALID:
363 default:
364 dev_err(&drv_data->adapter.dev,
365 "mv64xxx_i2c_do_action: Invalid action: %d\n",
366 drv_data->action);
367 drv_data->rc = -EIO;
368 /* FALLTHRU */
369 case MV64XXX_I2C_ACTION_SEND_STOP:
370 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
371 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200372 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000374 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 break;
376 }
377}
378
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200379static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100380mv64xxx_i2c_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct mv64xxx_i2c_data *drv_data = dev_id;
383 unsigned long flags;
384 u32 status;
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200385 irqreturn_t rc = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 spin_lock_irqsave(&drv_data->lock, flags);
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200388 while (readl(drv_data->reg_base + drv_data->reg_offsets.control) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 MV64XXX_I2C_REG_CONTROL_IFLG) {
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200390 status = readl(drv_data->reg_base + drv_data->reg_offsets.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 mv64xxx_i2c_fsm(drv_data, status);
392 mv64xxx_i2c_do_action(drv_data);
393 rc = IRQ_HANDLED;
394 }
395 spin_unlock_irqrestore(&drv_data->lock, flags);
396
397 return rc;
398}
399
400/*
401 *****************************************************************************
402 *
403 * I2C Msg Execution Routines
404 *
405 *****************************************************************************
406 */
407static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
409{
410 long time_left;
411 unsigned long flags;
412 char abort = 0;
413
Russell Kingd295a862013-05-16 10:30:59 +0000414 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100415 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 spin_lock_irqsave(&drv_data->lock, flags);
418 if (!time_left) { /* Timed out */
419 drv_data->rc = -ETIMEDOUT;
420 abort = 1;
421 } else if (time_left < 0) { /* Interrupted/Error */
422 drv_data->rc = time_left; /* errno value */
423 abort = 1;
424 }
425
426 if (abort && drv_data->block) {
Mark A. Greere91c0212005-12-18 17:22:01 +0100427 drv_data->aborting = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 spin_unlock_irqrestore(&drv_data->lock, flags);
429
430 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100431 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Mark A. Greere91c0212005-12-18 17:22:01 +0100433 if ((time_left <= 0) && drv_data->block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 drv_data->state = MV64XXX_I2C_STATE_IDLE;
435 dev_err(&drv_data->adapter.dev,
Mark A. Greere91c0212005-12-18 17:22:01 +0100436 "mv64xxx: I2C bus locked, block: %d, "
437 "time_left: %d\n", drv_data->block,
438 (int)time_left);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200439 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 } else
442 spin_unlock_irqrestore(&drv_data->lock, flags);
443}
444
445static int
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100446mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg,
Russell King4243fa02013-05-16 21:39:12 +0100447 int is_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 unsigned long flags;
450
451 spin_lock_irqsave(&drv_data->lock, flags);
452 mv64xxx_i2c_prepare_for_io(drv_data, msg);
453
Russell King4243fa02013-05-16 21:39:12 +0100454 drv_data->action = MV64XXX_I2C_ACTION_SEND_START;
455 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100457 drv_data->send_stop = is_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 drv_data->block = 1;
459 mv64xxx_i2c_do_action(drv_data);
460 spin_unlock_irqrestore(&drv_data->lock, flags);
461
462 mv64xxx_i2c_wait_for_completion(drv_data);
463 return drv_data->rc;
464}
465
466/*
467 *****************************************************************************
468 *
469 * I2C Core Support Routines (Interface to higher level I2C code)
470 *
471 *****************************************************************************
472 */
473static u32
474mv64xxx_i2c_functionality(struct i2c_adapter *adap)
475{
476 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
477}
478
479static int
480mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
481{
482 struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
Russell King4243fa02013-05-16 21:39:12 +0100483 int rc, ret = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Russell King4243fa02013-05-16 21:39:12 +0100485 BUG_ON(drv_data->msgs != NULL);
486 drv_data->msgs = msgs;
487 drv_data->num_msgs = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Russell King4243fa02013-05-16 21:39:12 +0100489 rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1);
490 if (rc < 0)
491 ret = rc;
492
493 drv_data->num_msgs = 0;
494 drv_data->msgs = NULL;
495
496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Jean Delvare8f9082c2006-09-03 22:39:46 +0200499static const struct i2c_algorithm mv64xxx_i2c_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 .master_xfer = mv64xxx_i2c_xfer,
501 .functionality = mv64xxx_i2c_functionality,
502};
503
504/*
505 *****************************************************************************
506 *
507 * Driver Interface & Early Init Routines
508 *
509 *****************************************************************************
510 */
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200511static const struct of_device_id mv64xxx_i2c_of_match_table[] = {
512 { .compatible = "marvell,mv64xxx-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
513 {}
514};
515MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table);
516
Andrew Lunnb61d1572012-07-22 12:51:35 +0200517#ifdef CONFIG_OF
Bill Pemberton0b255e92012-11-27 15:59:38 -0500518static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200519mv64xxx_calc_freq(const int tclk, const int n, const int m)
520{
521 return tclk / (10 * (m + 1) * (2 << n));
522}
523
Bill Pemberton0b255e92012-11-27 15:59:38 -0500524static bool
Andrew Lunnb61d1572012-07-22 12:51:35 +0200525mv64xxx_find_baud_factors(const u32 req_freq, const u32 tclk, u32 *best_n,
526 u32 *best_m)
527{
528 int freq, delta, best_delta = INT_MAX;
529 int m, n;
530
531 for (n = 0; n <= 7; n++)
532 for (m = 0; m <= 15; m++) {
533 freq = mv64xxx_calc_freq(tclk, n, m);
534 delta = req_freq - freq;
535 if (delta >= 0 && delta < best_delta) {
536 *best_m = m;
537 *best_n = n;
538 best_delta = delta;
539 }
540 if (best_delta == 0)
541 return true;
542 }
543 if (best_delta == INT_MAX)
544 return false;
545 return true;
546}
547
Bill Pemberton0b255e92012-11-27 15:59:38 -0500548static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200549mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200550 struct device *dev)
Andrew Lunnb61d1572012-07-22 12:51:35 +0200551{
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200552 const struct of_device_id *device;
553 struct device_node *np = dev->of_node;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200554 u32 bus_freq, tclk;
555 int rc = 0;
556
557 /* CLK is mandatory when using DT to describe the i2c bus. We
558 * need to know tclk in order to calculate bus clock
559 * factors.
560 */
561#if !defined(CONFIG_HAVE_CLK)
562 /* Have OF but no CLK */
563 return -ENODEV;
564#else
565 if (IS_ERR(drv_data->clk)) {
566 rc = -ENODEV;
567 goto out;
568 }
569 tclk = clk_get_rate(drv_data->clk);
570 of_property_read_u32(np, "clock-frequency", &bus_freq);
571 if (!mv64xxx_find_baud_factors(bus_freq, tclk,
572 &drv_data->freq_n, &drv_data->freq_m)) {
573 rc = -EINVAL;
574 goto out;
575 }
576 drv_data->irq = irq_of_parse_and_map(np, 0);
577
578 /* Its not yet defined how timeouts will be specified in device tree.
579 * So hard code the value to 1 second.
580 */
581 drv_data->adapter.timeout = HZ;
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200582
583 device = of_match_device(mv64xxx_i2c_of_match_table, dev);
584 if (!device)
585 return -ENODEV;
586
587 memcpy(&drv_data->reg_offsets, device->data, sizeof(drv_data->reg_offsets));
588
Andrew Lunnb61d1572012-07-22 12:51:35 +0200589out:
590 return rc;
591#endif
592}
593#else /* CONFIG_OF */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500594static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200595mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200596 struct device *dev)
Andrew Lunnb61d1572012-07-22 12:51:35 +0200597{
598 return -ENODEV;
599}
600#endif /* CONFIG_OF */
601
Bill Pemberton0b255e92012-11-27 15:59:38 -0500602static int
Russell King3ae5eae2005-11-09 22:32:44 +0000603mv64xxx_i2c_probe(struct platform_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct mv64xxx_i2c_data *drv_data;
Russell King3ae5eae2005-11-09 22:32:44 +0000606 struct mv64xxx_i2c_pdata *pdata = pd->dev.platform_data;
Russell King16874b02013-05-16 21:33:09 +0100607 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int rc;
609
Andrew Lunnb61d1572012-07-22 12:51:35 +0200610 if ((!pdata && !pd->dev.of_node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return -ENODEV;
612
Russell King2c911102013-05-16 21:35:10 +0100613 drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data),
614 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!drv_data)
616 return -ENOMEM;
617
Russell King16874b02013-05-16 21:33:09 +0100618 r = platform_get_resource(pd, IORESOURCE_MEM, 0);
619 drv_data->reg_base = devm_ioremap_resource(&pd->dev, r);
Russell King2c911102013-05-16 21:35:10 +0100620 if (IS_ERR(drv_data->reg_base))
621 return PTR_ERR(drv_data->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Mark A. Greere91c0212005-12-18 17:22:01 +0100623 strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
David Brownell2096b952007-05-01 23:26:28 +0200624 sizeof(drv_data->adapter.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 init_waitqueue_head(&drv_data->waitq);
627 spin_lock_init(&drv_data->lock);
628
Andrew Lunnb61d1572012-07-22 12:51:35 +0200629#if defined(CONFIG_HAVE_CLK)
630 /* Not all platforms have a clk */
Russell King4c5c95f2013-05-16 21:34:10 +0100631 drv_data->clk = devm_clk_get(&pd->dev, NULL);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200632 if (!IS_ERR(drv_data->clk)) {
633 clk_prepare(drv_data->clk);
634 clk_enable(drv_data->clk);
635 }
636#endif
637 if (pdata) {
638 drv_data->freq_m = pdata->freq_m;
639 drv_data->freq_n = pdata->freq_n;
640 drv_data->irq = platform_get_irq(pd, 0);
641 drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200642 memcpy(&drv_data->reg_offsets, &mv64xxx_i2c_regs_mv64xxx, sizeof(drv_data->reg_offsets));
Andrew Lunnb61d1572012-07-22 12:51:35 +0200643 } else if (pd->dev.of_node) {
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200644 rc = mv64xxx_of_config(drv_data, &pd->dev);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200645 if (rc)
Russell King2c911102013-05-16 21:35:10 +0100646 goto exit_clk;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200647 }
David Vrabel48944732006-01-19 17:56:29 +0000648 if (drv_data->irq < 0) {
649 rc = -ENXIO;
Russell King2c911102013-05-16 21:35:10 +0100650 goto exit_clk;
David Vrabel48944732006-01-19 17:56:29 +0000651 }
Andrew Lunnb61d1572012-07-22 12:51:35 +0200652
Jean Delvare12a917f2007-02-13 22:09:03 +0100653 drv_data->adapter.dev.parent = &pd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 drv_data->adapter.algo = &mv64xxx_i2c_algo;
655 drv_data->adapter.owner = THIS_MODULE;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200656 drv_data->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200657 drv_data->adapter.nr = pd->id;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200658 drv_data->adapter.dev.of_node = pd->dev.of_node;
Russell King3ae5eae2005-11-09 22:32:44 +0000659 platform_set_drvdata(pd, drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 i2c_set_adapdata(&drv_data->adapter, drv_data);
661
Maxime Bizon3269bb62007-01-05 17:54:05 +0100662 mv64xxx_i2c_hw_init(drv_data);
663
Russell King0c195af2013-05-16 21:36:11 +0100664 rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
665 MV64XXX_I2C_CTLR_NAME, drv_data);
666 if (rc) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800667 dev_err(&drv_data->adapter.dev,
Russell King0c195af2013-05-16 21:36:11 +0100668 "mv64xxx: Can't register intr handler irq%d: %d\n",
669 drv_data->irq, rc);
Russell King2c911102013-05-16 21:35:10 +0100670 goto exit_clk;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200671 } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800672 dev_err(&drv_data->adapter.dev,
673 "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto exit_free_irq;
675 }
676
Andrew Lunnb61d1572012-07-22 12:51:35 +0200677 of_i2c_register_devices(&drv_data->adapter);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return 0;
680
Russell King2c911102013-05-16 21:35:10 +0100681exit_free_irq:
682 free_irq(drv_data->irq, drv_data);
683exit_clk:
Andrew Lunnb61d1572012-07-22 12:51:35 +0200684#if defined(CONFIG_HAVE_CLK)
685 /* Not all platforms have a clk */
686 if (!IS_ERR(drv_data->clk)) {
687 clk_disable(drv_data->clk);
688 clk_unprepare(drv_data->clk);
689 }
690#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return rc;
692}
693
Bill Pemberton0b255e92012-11-27 15:59:38 -0500694static int
Russell King3ae5eae2005-11-09 22:32:44 +0000695mv64xxx_i2c_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Russell King3ae5eae2005-11-09 22:32:44 +0000697 struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000699 i2c_del_adapter(&drv_data->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 free_irq(drv_data->irq, drv_data);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200701#if defined(CONFIG_HAVE_CLK)
702 /* Not all platforms have a clk */
703 if (!IS_ERR(drv_data->clk)) {
704 clk_disable(drv_data->clk);
705 clk_unprepare(drv_data->clk);
706 }
707#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Russell King3ae5eae2005-11-09 22:32:44 +0000712static struct platform_driver mv64xxx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 .probe = mv64xxx_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500714 .remove = mv64xxx_i2c_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000715 .driver = {
716 .owner = THIS_MODULE,
717 .name = MV64XXX_I2C_CTLR_NAME,
Andrew Lunnb61d1572012-07-22 12:51:35 +0200718 .of_match_table = of_match_ptr(mv64xxx_i2c_of_match_table),
Russell King3ae5eae2005-11-09 22:32:44 +0000719 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720};
721
Axel Lina3664b52012-01-12 20:32:04 +0100722module_platform_driver(mv64xxx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
725MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
726MODULE_LICENSE("GPL");