blob: 9cc361d1994104a3c88dcf54e84512705536156a [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
Maxime Ripard3d66ac72013-06-12 18:53:32 +0200132static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_sun4i = {
133 .addr = 0x00,
134 .ext_addr = 0x04,
135 .data = 0x08,
136 .control = 0x0c,
137 .status = 0x10,
138 .clock = 0x14,
139 .soft_reset = 0x18,
140};
141
Russell King3420afb2013-05-16 21:38:11 +0100142static void
143mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
144 struct i2c_msg *msg)
145{
146 u32 dir = 0;
147
148 drv_data->msg = msg;
149 drv_data->byte_posn = 0;
150 drv_data->bytes_left = msg->len;
151 drv_data->aborting = 0;
152 drv_data->rc = 0;
153 drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
154 MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
155
156 if (msg->flags & I2C_M_RD)
157 dir = 1;
158
159 if (msg->flags & I2C_M_TEN) {
160 drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
161 drv_data->addr2 = (u32)msg->addr & 0xff;
162 } else {
Maxime Ripard683e69b2013-06-12 18:53:30 +0200163 drv_data->addr1 = MV64XXX_I2C_ADDR_ADDR((u32)msg->addr) | dir;
Russell King3420afb2013-05-16 21:38:11 +0100164 drv_data->addr2 = 0;
165 }
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
169 *****************************************************************************
170 *
171 * Finite State Machine & Interrupt Routines
172 *
173 *****************************************************************************
174 */
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200175
176/* Reset hardware and initialize FSM */
177static void
178mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
179{
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200180 writel(0, drv_data->reg_base + drv_data->reg_offsets.soft_reset);
Maxime Ripard683e69b2013-06-12 18:53:30 +0200181 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 +0200182 drv_data->reg_base + drv_data->reg_offsets.clock);
183 writel(0, drv_data->reg_base + drv_data->reg_offsets.addr);
184 writel(0, drv_data->reg_base + drv_data->reg_offsets.ext_addr);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200185 writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200186 drv_data->reg_base + drv_data->reg_offsets.control);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200187 drv_data->state = MV64XXX_I2C_STATE_IDLE;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static void
191mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
192{
193 /*
194 * If state is idle, then this is likely the remnants of an old
195 * operation that driver has given up on or the user has killed.
196 * If so, issue the stop condition and go to idle.
197 */
198 if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
199 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
200 return;
201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* The status from the ctlr [mostly] tells us what to do next */
204 switch (status) {
205 /* Start condition interrupt */
206 case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
207 case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
208 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
209 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
210 break;
211
212 /* Performing a write */
213 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
214 if (drv_data->msg->flags & I2C_M_TEN) {
215 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
216 drv_data->state =
217 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
218 break;
219 }
220 /* FALLTHRU */
221 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
222 case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
Mark A. Greere91c0212005-12-18 17:22:01 +0100223 if ((drv_data->bytes_left == 0)
224 || (drv_data->aborting
225 && (drv_data->byte_posn != 0))) {
Russell King4243fa02013-05-16 21:39:12 +0100226 if (drv_data->send_stop || drv_data->aborting) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100227 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
228 drv_data->state = MV64XXX_I2C_STATE_IDLE;
229 } else {
230 drv_data->action =
231 MV64XXX_I2C_ACTION_SEND_RESTART;
232 drv_data->state =
233 MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
234 }
Mark A. Greere91c0212005-12-18 17:22:01 +0100235 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
237 drv_data->state =
238 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
239 drv_data->bytes_left--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 break;
242
243 /* Performing a read */
244 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
245 if (drv_data->msg->flags & I2C_M_TEN) {
246 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
247 drv_data->state =
248 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
249 break;
250 }
251 /* FALLTHRU */
252 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
253 if (drv_data->bytes_left == 0) {
254 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
255 drv_data->state = MV64XXX_I2C_STATE_IDLE;
256 break;
257 }
258 /* FALLTHRU */
259 case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
260 if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
261 drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
262 else {
263 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
264 drv_data->bytes_left--;
265 }
266 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
267
Mark A. Greere91c0212005-12-18 17:22:01 +0100268 if ((drv_data->bytes_left == 1) || drv_data->aborting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
270 break;
271
272 case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
273 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
274 drv_data->state = MV64XXX_I2C_STATE_IDLE;
275 break;
276
277 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
278 case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
279 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
280 /* Doesn't seem to be a device at other end */
281 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
282 drv_data->state = MV64XXX_I2C_STATE_IDLE;
Guenter Roeck6faa3532013-06-19 14:53:52 -0700283 drv_data->rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285
286 default:
287 dev_err(&drv_data->adapter.dev,
288 "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
289 "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
290 drv_data->state, status, drv_data->msg->addr,
291 drv_data->msg->flags);
292 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200293 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 drv_data->rc = -EIO;
295 }
296}
297
298static void
299mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
300{
301 switch(drv_data->action) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100302 case MV64XXX_I2C_ACTION_SEND_RESTART:
Russell King4243fa02013-05-16 21:39:12 +0100303 /* We should only get here if we have further messages */
304 BUG_ON(drv_data->num_msgs == 0);
305
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100306 drv_data->cntl_bits |= MV64XXX_I2C_REG_CONTROL_START;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100307 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200308 drv_data->reg_base + drv_data->reg_offsets.control);
Russell King4243fa02013-05-16 21:39:12 +0100309
310 drv_data->msgs++;
311 drv_data->num_msgs--;
312
313 /* Setup for the next message */
314 mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs);
315
316 /*
317 * We're never at the start of the message here, and by this
318 * time it's already too late to do any protocol mangling.
319 * Thankfully, do not advertise support for that feature.
320 */
321 drv_data->send_stop = drv_data->num_msgs == 1;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100322 break;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 case MV64XXX_I2C_ACTION_CONTINUE:
325 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200326 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328
329 case MV64XXX_I2C_ACTION_SEND_START:
330 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200331 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 break;
333
334 case MV64XXX_I2C_ACTION_SEND_ADDR_1:
335 writel(drv_data->addr1,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200336 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200338 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 break;
340
341 case MV64XXX_I2C_ACTION_SEND_ADDR_2:
342 writel(drv_data->addr2,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200343 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200345 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 break;
347
348 case MV64XXX_I2C_ACTION_SEND_DATA:
349 writel(drv_data->msg->buf[drv_data->byte_posn++],
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200350 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200352 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354
355 case MV64XXX_I2C_ACTION_RCV_DATA:
356 drv_data->msg->buf[drv_data->byte_posn++] =
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200357 readl(drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200359 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361
362 case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
363 drv_data->msg->buf[drv_data->byte_posn++] =
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200364 readl(drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
366 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200367 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000369 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 break;
371
372 case MV64XXX_I2C_ACTION_INVALID:
373 default:
374 dev_err(&drv_data->adapter.dev,
375 "mv64xxx_i2c_do_action: Invalid action: %d\n",
376 drv_data->action);
377 drv_data->rc = -EIO;
378 /* FALLTHRU */
379 case MV64XXX_I2C_ACTION_SEND_STOP:
380 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
381 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200382 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000384 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386 }
387}
388
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200389static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100390mv64xxx_i2c_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct mv64xxx_i2c_data *drv_data = dev_id;
393 unsigned long flags;
394 u32 status;
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200395 irqreturn_t rc = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 spin_lock_irqsave(&drv_data->lock, flags);
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200398 while (readl(drv_data->reg_base + drv_data->reg_offsets.control) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 MV64XXX_I2C_REG_CONTROL_IFLG) {
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200400 status = readl(drv_data->reg_base + drv_data->reg_offsets.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 mv64xxx_i2c_fsm(drv_data, status);
402 mv64xxx_i2c_do_action(drv_data);
403 rc = IRQ_HANDLED;
404 }
405 spin_unlock_irqrestore(&drv_data->lock, flags);
406
407 return rc;
408}
409
410/*
411 *****************************************************************************
412 *
413 * I2C Msg Execution Routines
414 *
415 *****************************************************************************
416 */
417static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
419{
420 long time_left;
421 unsigned long flags;
422 char abort = 0;
423
Russell Kingd295a862013-05-16 10:30:59 +0000424 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100425 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 spin_lock_irqsave(&drv_data->lock, flags);
428 if (!time_left) { /* Timed out */
429 drv_data->rc = -ETIMEDOUT;
430 abort = 1;
431 } else if (time_left < 0) { /* Interrupted/Error */
432 drv_data->rc = time_left; /* errno value */
433 abort = 1;
434 }
435
436 if (abort && drv_data->block) {
Mark A. Greere91c0212005-12-18 17:22:01 +0100437 drv_data->aborting = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 spin_unlock_irqrestore(&drv_data->lock, flags);
439
440 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100441 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Mark A. Greere91c0212005-12-18 17:22:01 +0100443 if ((time_left <= 0) && drv_data->block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 drv_data->state = MV64XXX_I2C_STATE_IDLE;
445 dev_err(&drv_data->adapter.dev,
Mark A. Greere91c0212005-12-18 17:22:01 +0100446 "mv64xxx: I2C bus locked, block: %d, "
447 "time_left: %d\n", drv_data->block,
448 (int)time_left);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200449 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 } else
452 spin_unlock_irqrestore(&drv_data->lock, flags);
453}
454
455static int
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100456mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg,
Russell King4243fa02013-05-16 21:39:12 +0100457 int is_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 unsigned long flags;
460
461 spin_lock_irqsave(&drv_data->lock, flags);
462 mv64xxx_i2c_prepare_for_io(drv_data, msg);
463
Russell King4243fa02013-05-16 21:39:12 +0100464 drv_data->action = MV64XXX_I2C_ACTION_SEND_START;
465 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100467 drv_data->send_stop = is_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 drv_data->block = 1;
469 mv64xxx_i2c_do_action(drv_data);
470 spin_unlock_irqrestore(&drv_data->lock, flags);
471
472 mv64xxx_i2c_wait_for_completion(drv_data);
473 return drv_data->rc;
474}
475
476/*
477 *****************************************************************************
478 *
479 * I2C Core Support Routines (Interface to higher level I2C code)
480 *
481 *****************************************************************************
482 */
483static u32
484mv64xxx_i2c_functionality(struct i2c_adapter *adap)
485{
486 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
487}
488
489static int
490mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
491{
492 struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
Russell King4243fa02013-05-16 21:39:12 +0100493 int rc, ret = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Russell King4243fa02013-05-16 21:39:12 +0100495 BUG_ON(drv_data->msgs != NULL);
496 drv_data->msgs = msgs;
497 drv_data->num_msgs = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Russell King4243fa02013-05-16 21:39:12 +0100499 rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1);
500 if (rc < 0)
501 ret = rc;
502
503 drv_data->num_msgs = 0;
504 drv_data->msgs = NULL;
505
506 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Jean Delvare8f9082c2006-09-03 22:39:46 +0200509static const struct i2c_algorithm mv64xxx_i2c_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .master_xfer = mv64xxx_i2c_xfer,
511 .functionality = mv64xxx_i2c_functionality,
512};
513
514/*
515 *****************************************************************************
516 *
517 * Driver Interface & Early Init Routines
518 *
519 *****************************************************************************
520 */
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200521static const struct of_device_id mv64xxx_i2c_of_match_table[] = {
Maxime Ripard3d66ac72013-06-12 18:53:32 +0200522 { .compatible = "allwinner,sun4i-i2c", .data = &mv64xxx_i2c_regs_sun4i},
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200523 { .compatible = "marvell,mv64xxx-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
524 {}
525};
526MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table);
527
Andrew Lunnb61d1572012-07-22 12:51:35 +0200528#ifdef CONFIG_OF
Bill Pemberton0b255e92012-11-27 15:59:38 -0500529static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200530mv64xxx_calc_freq(const int tclk, const int n, const int m)
531{
532 return tclk / (10 * (m + 1) * (2 << n));
533}
534
Bill Pemberton0b255e92012-11-27 15:59:38 -0500535static bool
Andrew Lunnb61d1572012-07-22 12:51:35 +0200536mv64xxx_find_baud_factors(const u32 req_freq, const u32 tclk, u32 *best_n,
537 u32 *best_m)
538{
539 int freq, delta, best_delta = INT_MAX;
540 int m, n;
541
542 for (n = 0; n <= 7; n++)
543 for (m = 0; m <= 15; m++) {
544 freq = mv64xxx_calc_freq(tclk, n, m);
545 delta = req_freq - freq;
546 if (delta >= 0 && delta < best_delta) {
547 *best_m = m;
548 *best_n = n;
549 best_delta = delta;
550 }
551 if (best_delta == 0)
552 return true;
553 }
554 if (best_delta == INT_MAX)
555 return false;
556 return true;
557}
558
Bill Pemberton0b255e92012-11-27 15:59:38 -0500559static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200560mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200561 struct device *dev)
Andrew Lunnb61d1572012-07-22 12:51:35 +0200562{
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200563 const struct of_device_id *device;
564 struct device_node *np = dev->of_node;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200565 u32 bus_freq, tclk;
566 int rc = 0;
567
568 /* CLK is mandatory when using DT to describe the i2c bus. We
569 * need to know tclk in order to calculate bus clock
570 * factors.
571 */
572#if !defined(CONFIG_HAVE_CLK)
573 /* Have OF but no CLK */
574 return -ENODEV;
575#else
576 if (IS_ERR(drv_data->clk)) {
577 rc = -ENODEV;
578 goto out;
579 }
580 tclk = clk_get_rate(drv_data->clk);
Gregory CLEMENT4c730a02013-06-21 15:32:06 +0200581
582 rc = of_property_read_u32(np, "clock-frequency", &bus_freq);
583 if (rc)
584 bus_freq = 100000; /* 100kHz by default */
585
Andrew Lunnb61d1572012-07-22 12:51:35 +0200586 if (!mv64xxx_find_baud_factors(bus_freq, tclk,
587 &drv_data->freq_n, &drv_data->freq_m)) {
588 rc = -EINVAL;
589 goto out;
590 }
591 drv_data->irq = irq_of_parse_and_map(np, 0);
592
593 /* Its not yet defined how timeouts will be specified in device tree.
594 * So hard code the value to 1 second.
595 */
596 drv_data->adapter.timeout = HZ;
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200597
598 device = of_match_device(mv64xxx_i2c_of_match_table, dev);
599 if (!device)
600 return -ENODEV;
601
602 memcpy(&drv_data->reg_offsets, device->data, sizeof(drv_data->reg_offsets));
603
Andrew Lunnb61d1572012-07-22 12:51:35 +0200604out:
605 return rc;
606#endif
607}
608#else /* CONFIG_OF */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500609static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200610mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200611 struct device *dev)
Andrew Lunnb61d1572012-07-22 12:51:35 +0200612{
613 return -ENODEV;
614}
615#endif /* CONFIG_OF */
616
Bill Pemberton0b255e92012-11-27 15:59:38 -0500617static int
Russell King3ae5eae2005-11-09 22:32:44 +0000618mv64xxx_i2c_probe(struct platform_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct mv64xxx_i2c_data *drv_data;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900621 struct mv64xxx_i2c_pdata *pdata = dev_get_platdata(&pd->dev);
Russell King16874b02013-05-16 21:33:09 +0100622 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int rc;
624
Andrew Lunnb61d1572012-07-22 12:51:35 +0200625 if ((!pdata && !pd->dev.of_node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -ENODEV;
627
Russell King2c911102013-05-16 21:35:10 +0100628 drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data),
629 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!drv_data)
631 return -ENOMEM;
632
Russell King16874b02013-05-16 21:33:09 +0100633 r = platform_get_resource(pd, IORESOURCE_MEM, 0);
634 drv_data->reg_base = devm_ioremap_resource(&pd->dev, r);
Russell King2c911102013-05-16 21:35:10 +0100635 if (IS_ERR(drv_data->reg_base))
636 return PTR_ERR(drv_data->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Mark A. Greere91c0212005-12-18 17:22:01 +0100638 strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
David Brownell2096b952007-05-01 23:26:28 +0200639 sizeof(drv_data->adapter.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 init_waitqueue_head(&drv_data->waitq);
642 spin_lock_init(&drv_data->lock);
643
Andrew Lunnb61d1572012-07-22 12:51:35 +0200644#if defined(CONFIG_HAVE_CLK)
645 /* Not all platforms have a clk */
Russell King4c5c95f2013-05-16 21:34:10 +0100646 drv_data->clk = devm_clk_get(&pd->dev, NULL);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200647 if (!IS_ERR(drv_data->clk)) {
648 clk_prepare(drv_data->clk);
649 clk_enable(drv_data->clk);
650 }
651#endif
652 if (pdata) {
653 drv_data->freq_m = pdata->freq_m;
654 drv_data->freq_n = pdata->freq_n;
655 drv_data->irq = platform_get_irq(pd, 0);
656 drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200657 memcpy(&drv_data->reg_offsets, &mv64xxx_i2c_regs_mv64xxx, sizeof(drv_data->reg_offsets));
Andrew Lunnb61d1572012-07-22 12:51:35 +0200658 } else if (pd->dev.of_node) {
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200659 rc = mv64xxx_of_config(drv_data, &pd->dev);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200660 if (rc)
Russell King2c911102013-05-16 21:35:10 +0100661 goto exit_clk;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200662 }
David Vrabel48944732006-01-19 17:56:29 +0000663 if (drv_data->irq < 0) {
664 rc = -ENXIO;
Russell King2c911102013-05-16 21:35:10 +0100665 goto exit_clk;
David Vrabel48944732006-01-19 17:56:29 +0000666 }
Andrew Lunnb61d1572012-07-22 12:51:35 +0200667
Jean Delvare12a917f2007-02-13 22:09:03 +0100668 drv_data->adapter.dev.parent = &pd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 drv_data->adapter.algo = &mv64xxx_i2c_algo;
670 drv_data->adapter.owner = THIS_MODULE;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200671 drv_data->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200672 drv_data->adapter.nr = pd->id;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200673 drv_data->adapter.dev.of_node = pd->dev.of_node;
Russell King3ae5eae2005-11-09 22:32:44 +0000674 platform_set_drvdata(pd, drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 i2c_set_adapdata(&drv_data->adapter, drv_data);
676
Maxime Bizon3269bb62007-01-05 17:54:05 +0100677 mv64xxx_i2c_hw_init(drv_data);
678
Russell King0c195af2013-05-16 21:36:11 +0100679 rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
680 MV64XXX_I2C_CTLR_NAME, drv_data);
681 if (rc) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800682 dev_err(&drv_data->adapter.dev,
Russell King0c195af2013-05-16 21:36:11 +0100683 "mv64xxx: Can't register intr handler irq%d: %d\n",
684 drv_data->irq, rc);
Russell King2c911102013-05-16 21:35:10 +0100685 goto exit_clk;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200686 } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800687 dev_err(&drv_data->adapter.dev,
688 "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto exit_free_irq;
690 }
691
Andrew Lunnb61d1572012-07-22 12:51:35 +0200692 of_i2c_register_devices(&drv_data->adapter);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 0;
695
Russell King2c911102013-05-16 21:35:10 +0100696exit_free_irq:
697 free_irq(drv_data->irq, drv_data);
698exit_clk:
Andrew Lunnb61d1572012-07-22 12:51:35 +0200699#if defined(CONFIG_HAVE_CLK)
700 /* Not all platforms have a clk */
701 if (!IS_ERR(drv_data->clk)) {
702 clk_disable(drv_data->clk);
703 clk_unprepare(drv_data->clk);
704 }
705#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return rc;
707}
708
Bill Pemberton0b255e92012-11-27 15:59:38 -0500709static int
Russell King3ae5eae2005-11-09 22:32:44 +0000710mv64xxx_i2c_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Russell King3ae5eae2005-11-09 22:32:44 +0000712 struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000714 i2c_del_adapter(&drv_data->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 free_irq(drv_data->irq, drv_data);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200716#if defined(CONFIG_HAVE_CLK)
717 /* Not all platforms have a clk */
718 if (!IS_ERR(drv_data->clk)) {
719 clk_disable(drv_data->clk);
720 clk_unprepare(drv_data->clk);
721 }
722#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Russell King3ae5eae2005-11-09 22:32:44 +0000727static struct platform_driver mv64xxx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 .probe = mv64xxx_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500729 .remove = mv64xxx_i2c_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000730 .driver = {
731 .owner = THIS_MODULE,
732 .name = MV64XXX_I2C_CTLR_NAME,
Andrew Lunnb61d1572012-07-22 12:51:35 +0200733 .of_match_table = of_match_ptr(mv64xxx_i2c_of_match_table),
Russell King3ae5eae2005-11-09 22:32:44 +0000734 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736
Axel Lina3664b52012-01-12 20:32:04 +0100737module_platform_driver(mv64xxx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
740MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
741MODULE_LICENSE("GPL");