blob: 98de78fd27ebb177ea3e6fc142f0388d6325a200 [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>
Maxime Ripard370136b2014-03-04 17:28:37 +010020#include <linux/reset.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020021#include <linux/io.h>
Andrew Lunnb61d1572012-07-22 12:51:35 +020022#include <linux/of.h>
Maxime Ripard004e8ed2013-06-12 18:53:31 +020023#include <linux/of_device.h>
Andrew Lunnb61d1572012-07-22 12:51:35 +020024#include <linux/of_irq.h>
Andrew Lunnb61d1572012-07-22 12:51:35 +020025#include <linux/clk.h>
26#include <linux/err.h>
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +020027#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Maxime Ripard683e69b2013-06-12 18:53:30 +020029#define MV64XXX_I2C_ADDR_ADDR(val) ((val & 0x7f) << 1)
30#define MV64XXX_I2C_BAUD_DIV_N(val) (val & 0x7)
31#define MV64XXX_I2C_BAUD_DIV_M(val) ((val & 0xf) << 3)
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define MV64XXX_I2C_REG_CONTROL_ACK 0x00000004
34#define MV64XXX_I2C_REG_CONTROL_IFLG 0x00000008
35#define MV64XXX_I2C_REG_CONTROL_STOP 0x00000010
36#define MV64XXX_I2C_REG_CONTROL_START 0x00000020
37#define MV64XXX_I2C_REG_CONTROL_TWSIEN 0x00000040
38#define MV64XXX_I2C_REG_CONTROL_INTEN 0x00000080
39
40/* Ctlr status values */
41#define MV64XXX_I2C_STATUS_BUS_ERR 0x00
42#define MV64XXX_I2C_STATUS_MAST_START 0x08
43#define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10
44#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18
45#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20
46#define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28
47#define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30
48#define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38
49#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40
50#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48
51#define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50
52#define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58
53#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0
54#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8
55#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0
56#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8
57#define MV64XXX_I2C_STATUS_NO_STATUS 0xf8
58
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +020059/* Register defines (I2C bridge) */
60#define MV64XXX_I2C_REG_TX_DATA_LO 0xc0
61#define MV64XXX_I2C_REG_TX_DATA_HI 0xc4
62#define MV64XXX_I2C_REG_RX_DATA_LO 0xc8
63#define MV64XXX_I2C_REG_RX_DATA_HI 0xcc
64#define MV64XXX_I2C_REG_BRIDGE_CONTROL 0xd0
65#define MV64XXX_I2C_REG_BRIDGE_STATUS 0xd4
66#define MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE 0xd8
67#define MV64XXX_I2C_REG_BRIDGE_INTR_MASK 0xdC
68#define MV64XXX_I2C_REG_BRIDGE_TIMING 0xe0
69
70/* Bridge Control values */
71#define MV64XXX_I2C_BRIDGE_CONTROL_WR 0x00000001
72#define MV64XXX_I2C_BRIDGE_CONTROL_RD 0x00000002
73#define MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT 2
74#define MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT 0x00001000
75#define MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT 13
76#define MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT 16
77#define MV64XXX_I2C_BRIDGE_CONTROL_ENABLE 0x00080000
78
79/* Bridge Status values */
80#define MV64XXX_I2C_BRIDGE_STATUS_ERROR 0x00000001
81#define MV64XXX_I2C_STATUS_OFFLOAD_ERROR 0xf0000001
82#define MV64XXX_I2C_STATUS_OFFLOAD_OK 0xf0000000
83
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Driver states */
86enum {
87 MV64XXX_I2C_STATE_INVALID,
88 MV64XXX_I2C_STATE_IDLE,
89 MV64XXX_I2C_STATE_WAITING_FOR_START_COND,
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +010090 MV64XXX_I2C_STATE_WAITING_FOR_RESTART,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK,
92 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK,
93 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK,
94 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97/* Driver actions */
98enum {
99 MV64XXX_I2C_ACTION_INVALID,
100 MV64XXX_I2C_ACTION_CONTINUE,
101 MV64XXX_I2C_ACTION_SEND_START,
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100102 MV64XXX_I2C_ACTION_SEND_RESTART,
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200103 MV64XXX_I2C_ACTION_OFFLOAD_RESTART,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 MV64XXX_I2C_ACTION_SEND_ADDR_1,
105 MV64XXX_I2C_ACTION_SEND_ADDR_2,
106 MV64XXX_I2C_ACTION_SEND_DATA,
107 MV64XXX_I2C_ACTION_RCV_DATA,
108 MV64XXX_I2C_ACTION_RCV_DATA_STOP,
109 MV64XXX_I2C_ACTION_SEND_STOP,
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200110 MV64XXX_I2C_ACTION_OFFLOAD_SEND_STOP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200113struct mv64xxx_i2c_regs {
114 u8 addr;
115 u8 ext_addr;
116 u8 data;
117 u8 control;
118 u8 status;
119 u8 clock;
120 u8 soft_reset;
121};
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123struct mv64xxx_i2c_data {
Russell King4243fa02013-05-16 21:39:12 +0100124 struct i2c_msg *msgs;
125 int num_msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int irq;
127 u32 state;
128 u32 action;
Mark A. Greere91c0212005-12-18 17:22:01 +0100129 u32 aborting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 u32 cntl_bits;
131 void __iomem *reg_base;
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200132 struct mv64xxx_i2c_regs reg_offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 u32 addr1;
134 u32 addr2;
135 u32 bytes_left;
136 u32 byte_posn;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100137 u32 send_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 u32 block;
139 int rc;
140 u32 freq_m;
141 u32 freq_n;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200142#if defined(CONFIG_HAVE_CLK)
143 struct clk *clk;
144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 wait_queue_head_t waitq;
146 spinlock_t lock;
147 struct i2c_msg *msg;
148 struct i2c_adapter adapter;
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200149 bool offload_enabled;
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200150/* 5us delay in order to avoid repeated start timing violation */
151 bool errata_delay;
Maxime Ripard370136b2014-03-04 17:28:37 +0100152 struct reset_control *rstc;
Maxime Ripardc7dcb1f2014-03-04 17:28:38 +0100153 bool irq_clear_inverted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200156static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_mv64xxx = {
157 .addr = 0x00,
158 .ext_addr = 0x10,
159 .data = 0x04,
160 .control = 0x08,
161 .status = 0x0c,
162 .clock = 0x0c,
163 .soft_reset = 0x1c,
164};
165
Maxime Ripard3d66ac72013-06-12 18:53:32 +0200166static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_sun4i = {
167 .addr = 0x00,
168 .ext_addr = 0x04,
169 .data = 0x08,
170 .control = 0x0c,
171 .status = 0x10,
172 .clock = 0x14,
173 .soft_reset = 0x18,
174};
175
Russell King3420afb2013-05-16 21:38:11 +0100176static void
177mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
178 struct i2c_msg *msg)
179{
180 u32 dir = 0;
181
182 drv_data->msg = msg;
183 drv_data->byte_posn = 0;
184 drv_data->bytes_left = msg->len;
185 drv_data->aborting = 0;
186 drv_data->rc = 0;
187 drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
188 MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
189
190 if (msg->flags & I2C_M_RD)
191 dir = 1;
192
193 if (msg->flags & I2C_M_TEN) {
194 drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
195 drv_data->addr2 = (u32)msg->addr & 0xff;
196 } else {
Maxime Ripard683e69b2013-06-12 18:53:30 +0200197 drv_data->addr1 = MV64XXX_I2C_ADDR_ADDR((u32)msg->addr) | dir;
Russell King3420afb2013-05-16 21:38:11 +0100198 drv_data->addr2 = 0;
199 }
200}
201
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200202static int mv64xxx_i2c_offload_msg(struct mv64xxx_i2c_data *drv_data)
203{
204 unsigned long data_reg_hi = 0;
205 unsigned long data_reg_lo = 0;
206 unsigned long ctrl_reg;
207 struct i2c_msg *msg = drv_data->msgs;
208
Wolfram Sang79970db2014-02-13 21:36:29 +0100209 if (!drv_data->offload_enabled)
210 return -EOPNOTSUPP;
211
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200212 drv_data->msg = msg;
213 drv_data->byte_posn = 0;
214 drv_data->bytes_left = msg->len;
215 drv_data->aborting = 0;
216 drv_data->rc = 0;
217 /* Only regular transactions can be offloaded */
218 if ((msg->flags & ~(I2C_M_TEN | I2C_M_RD)) != 0)
219 return -EINVAL;
220
221 /* Only 1-8 byte transfers can be offloaded */
222 if (msg->len < 1 || msg->len > 8)
223 return -EINVAL;
224
225 /* Build transaction */
226 ctrl_reg = MV64XXX_I2C_BRIDGE_CONTROL_ENABLE |
227 (msg->addr << MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT);
228
229 if ((msg->flags & I2C_M_TEN) != 0)
230 ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT;
231
232 if ((msg->flags & I2C_M_RD) == 0) {
233 u8 local_buf[8] = { 0 };
234
235 memcpy(local_buf, msg->buf, msg->len);
236 data_reg_lo = cpu_to_le32(*((u32 *)local_buf));
237 data_reg_hi = cpu_to_le32(*((u32 *)(local_buf+4)));
238
239 ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_WR |
240 (msg->len - 1) << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT;
241
Thierry Reding85b3a932013-09-18 14:51:40 +0200242 writel(data_reg_lo,
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200243 drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_LO);
Thierry Reding85b3a932013-09-18 14:51:40 +0200244 writel(data_reg_hi,
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200245 drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_HI);
246
247 } else {
248 ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_RD |
249 (msg->len - 1) << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT;
250 }
251
252 /* Execute transaction */
253 writel(ctrl_reg, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
254
255 return 0;
256}
257
258static void
259mv64xxx_i2c_update_offload_data(struct mv64xxx_i2c_data *drv_data)
260{
261 struct i2c_msg *msg = drv_data->msg;
262
263 if (msg->flags & I2C_M_RD) {
264 u32 data_reg_lo = readl(drv_data->reg_base +
265 MV64XXX_I2C_REG_RX_DATA_LO);
266 u32 data_reg_hi = readl(drv_data->reg_base +
267 MV64XXX_I2C_REG_RX_DATA_HI);
268 u8 local_buf[8] = { 0 };
269
270 *((u32 *)local_buf) = le32_to_cpu(data_reg_lo);
271 *((u32 *)(local_buf+4)) = le32_to_cpu(data_reg_hi);
272 memcpy(msg->buf, local_buf, msg->len);
273 }
274
275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 *****************************************************************************
278 *
279 * Finite State Machine & Interrupt Routines
280 *
281 *****************************************************************************
282 */
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200283
284/* Reset hardware and initialize FSM */
285static void
286mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
287{
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200288 if (drv_data->offload_enabled) {
289 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
290 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_TIMING);
291 writel(0, drv_data->reg_base +
292 MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
293 writel(0, drv_data->reg_base +
294 MV64XXX_I2C_REG_BRIDGE_INTR_MASK);
295 }
296
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200297 writel(0, drv_data->reg_base + drv_data->reg_offsets.soft_reset);
Maxime Ripard683e69b2013-06-12 18:53:30 +0200298 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 +0200299 drv_data->reg_base + drv_data->reg_offsets.clock);
300 writel(0, drv_data->reg_base + drv_data->reg_offsets.addr);
301 writel(0, drv_data->reg_base + drv_data->reg_offsets.ext_addr);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200302 writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200303 drv_data->reg_base + drv_data->reg_offsets.control);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200304 drv_data->state = MV64XXX_I2C_STATE_IDLE;
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static void
308mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
309{
310 /*
311 * If state is idle, then this is likely the remnants of an old
312 * operation that driver has given up on or the user has killed.
313 * If so, issue the stop condition and go to idle.
314 */
315 if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
316 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
317 return;
318 }
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* The status from the ctlr [mostly] tells us what to do next */
321 switch (status) {
322 /* Start condition interrupt */
323 case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
324 case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
325 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
326 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
327 break;
328
329 /* Performing a write */
330 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
331 if (drv_data->msg->flags & I2C_M_TEN) {
332 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
333 drv_data->state =
334 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
335 break;
336 }
337 /* FALLTHRU */
338 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
339 case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
Mark A. Greere91c0212005-12-18 17:22:01 +0100340 if ((drv_data->bytes_left == 0)
341 || (drv_data->aborting
342 && (drv_data->byte_posn != 0))) {
Russell King4243fa02013-05-16 21:39:12 +0100343 if (drv_data->send_stop || drv_data->aborting) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100344 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
345 drv_data->state = MV64XXX_I2C_STATE_IDLE;
346 } else {
347 drv_data->action =
348 MV64XXX_I2C_ACTION_SEND_RESTART;
349 drv_data->state =
350 MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
351 }
Mark A. Greere91c0212005-12-18 17:22:01 +0100352 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
354 drv_data->state =
355 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
356 drv_data->bytes_left--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 break;
359
360 /* Performing a read */
361 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
362 if (drv_data->msg->flags & I2C_M_TEN) {
363 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
364 drv_data->state =
365 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
366 break;
367 }
368 /* FALLTHRU */
369 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
370 if (drv_data->bytes_left == 0) {
371 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
372 drv_data->state = MV64XXX_I2C_STATE_IDLE;
373 break;
374 }
375 /* FALLTHRU */
376 case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
377 if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
378 drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
379 else {
380 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
381 drv_data->bytes_left--;
382 }
383 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
384
Mark A. Greere91c0212005-12-18 17:22:01 +0100385 if ((drv_data->bytes_left == 1) || drv_data->aborting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
387 break;
388
389 case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
390 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
391 drv_data->state = MV64XXX_I2C_STATE_IDLE;
392 break;
393
394 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
395 case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
396 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
397 /* Doesn't seem to be a device at other end */
398 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
399 drv_data->state = MV64XXX_I2C_STATE_IDLE;
Guenter Roeck6faa3532013-06-19 14:53:52 -0700400 drv_data->rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
402
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200403 case MV64XXX_I2C_STATUS_OFFLOAD_OK:
404 if (drv_data->send_stop || drv_data->aborting) {
405 drv_data->action = MV64XXX_I2C_ACTION_OFFLOAD_SEND_STOP;
406 drv_data->state = MV64XXX_I2C_STATE_IDLE;
407 } else {
408 drv_data->action = MV64XXX_I2C_ACTION_OFFLOAD_RESTART;
409 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
410 }
411 break;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 default:
414 dev_err(&drv_data->adapter.dev,
415 "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
416 "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
417 drv_data->state, status, drv_data->msg->addr,
418 drv_data->msg->flags);
419 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200420 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 drv_data->rc = -EIO;
422 }
423}
424
Wolfram Sang4c5b38e2014-02-13 21:36:31 +0100425static void mv64xxx_i2c_send_start(struct mv64xxx_i2c_data *drv_data)
426{
427 /* Can we offload this msg ? */
428 if (mv64xxx_i2c_offload_msg(drv_data) < 0) {
429 /* No, switch to standard path */
430 mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs);
431 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
432 drv_data->reg_base + drv_data->reg_offsets.control);
433 }
434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void
437mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
438{
439 switch(drv_data->action) {
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200440 case MV64XXX_I2C_ACTION_OFFLOAD_RESTART:
441 mv64xxx_i2c_update_offload_data(drv_data);
442 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
443 writel(0, drv_data->reg_base +
444 MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
445 /* FALLTHRU */
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100446 case MV64XXX_I2C_ACTION_SEND_RESTART:
Russell King4243fa02013-05-16 21:39:12 +0100447 /* We should only get here if we have further messages */
448 BUG_ON(drv_data->num_msgs == 0);
449
Russell King4243fa02013-05-16 21:39:12 +0100450 drv_data->msgs++;
451 drv_data->num_msgs--;
Wolfram Sang4c5b38e2014-02-13 21:36:31 +0100452 mv64xxx_i2c_send_start(drv_data);
Russell King4243fa02013-05-16 21:39:12 +0100453
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200454 if (drv_data->errata_delay)
455 udelay(5);
456
Russell King4243fa02013-05-16 21:39:12 +0100457 /*
458 * We're never at the start of the message here, and by this
459 * time it's already too late to do any protocol mangling.
460 * Thankfully, do not advertise support for that feature.
461 */
462 drv_data->send_stop = drv_data->num_msgs == 1;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100463 break;
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 case MV64XXX_I2C_ACTION_CONTINUE:
466 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200467 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469
470 case MV64XXX_I2C_ACTION_SEND_START:
Wolfram Sang4c5b38e2014-02-13 21:36:31 +0100471 mv64xxx_i2c_send_start(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473
474 case MV64XXX_I2C_ACTION_SEND_ADDR_1:
475 writel(drv_data->addr1,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200476 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200478 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480
481 case MV64XXX_I2C_ACTION_SEND_ADDR_2:
482 writel(drv_data->addr2,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200483 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200485 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487
488 case MV64XXX_I2C_ACTION_SEND_DATA:
489 writel(drv_data->msg->buf[drv_data->byte_posn++],
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200490 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200492 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494
495 case MV64XXX_I2C_ACTION_RCV_DATA:
496 drv_data->msg->buf[drv_data->byte_posn++] =
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200497 readl(drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200499 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 break;
501
502 case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
503 drv_data->msg->buf[drv_data->byte_posn++] =
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200504 readl(drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
506 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200507 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 drv_data->block = 0;
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200509 if (drv_data->errata_delay)
510 udelay(5);
511
Russell Kingd295a862013-05-16 10:30:59 +0000512 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 break;
514
515 case MV64XXX_I2C_ACTION_INVALID:
516 default:
517 dev_err(&drv_data->adapter.dev,
518 "mv64xxx_i2c_do_action: Invalid action: %d\n",
519 drv_data->action);
520 drv_data->rc = -EIO;
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* FALLTHRU */
523 case MV64XXX_I2C_ACTION_SEND_STOP:
524 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
525 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200526 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000528 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200530
531 case MV64XXX_I2C_ACTION_OFFLOAD_SEND_STOP:
532 mv64xxx_i2c_update_offload_data(drv_data);
533 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
534 writel(0, drv_data->reg_base +
535 MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
536 drv_data->block = 0;
537 wake_up(&drv_data->waitq);
538 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540}
541
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200542static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100543mv64xxx_i2c_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct mv64xxx_i2c_data *drv_data = dev_id;
546 unsigned long flags;
547 u32 status;
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200548 irqreturn_t rc = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 spin_lock_irqsave(&drv_data->lock, flags);
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200551
552 if (drv_data->offload_enabled) {
553 while (readl(drv_data->reg_base +
554 MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE)) {
555 int reg_status = readl(drv_data->reg_base +
556 MV64XXX_I2C_REG_BRIDGE_STATUS);
557 if (reg_status & MV64XXX_I2C_BRIDGE_STATUS_ERROR)
558 status = MV64XXX_I2C_STATUS_OFFLOAD_ERROR;
559 else
560 status = MV64XXX_I2C_STATUS_OFFLOAD_OK;
561 mv64xxx_i2c_fsm(drv_data, status);
562 mv64xxx_i2c_do_action(drv_data);
563 rc = IRQ_HANDLED;
564 }
565 }
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200566 while (readl(drv_data->reg_base + drv_data->reg_offsets.control) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 MV64XXX_I2C_REG_CONTROL_IFLG) {
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200568 status = readl(drv_data->reg_base + drv_data->reg_offsets.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 mv64xxx_i2c_fsm(drv_data, status);
570 mv64xxx_i2c_do_action(drv_data);
Maxime Ripardc7dcb1f2014-03-04 17:28:38 +0100571
572 if (drv_data->irq_clear_inverted)
573 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_IFLG,
574 drv_data->reg_base + drv_data->reg_offsets.control);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 rc = IRQ_HANDLED;
577 }
578 spin_unlock_irqrestore(&drv_data->lock, flags);
579
580 return rc;
581}
582
583/*
584 *****************************************************************************
585 *
586 * I2C Msg Execution Routines
587 *
588 *****************************************************************************
589 */
590static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
592{
593 long time_left;
594 unsigned long flags;
595 char abort = 0;
596
Russell Kingd295a862013-05-16 10:30:59 +0000597 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100598 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 spin_lock_irqsave(&drv_data->lock, flags);
601 if (!time_left) { /* Timed out */
602 drv_data->rc = -ETIMEDOUT;
603 abort = 1;
604 } else if (time_left < 0) { /* Interrupted/Error */
605 drv_data->rc = time_left; /* errno value */
606 abort = 1;
607 }
608
609 if (abort && drv_data->block) {
Mark A. Greere91c0212005-12-18 17:22:01 +0100610 drv_data->aborting = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 spin_unlock_irqrestore(&drv_data->lock, flags);
612
613 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100614 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Mark A. Greere91c0212005-12-18 17:22:01 +0100616 if ((time_left <= 0) && drv_data->block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 drv_data->state = MV64XXX_I2C_STATE_IDLE;
618 dev_err(&drv_data->adapter.dev,
Mark A. Greere91c0212005-12-18 17:22:01 +0100619 "mv64xxx: I2C bus locked, block: %d, "
620 "time_left: %d\n", drv_data->block,
621 (int)time_left);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200622 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 } else
625 spin_unlock_irqrestore(&drv_data->lock, flags);
626}
627
628static int
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100629mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg,
Russell King4243fa02013-05-16 21:39:12 +0100630 int is_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 unsigned long flags;
633
634 spin_lock_irqsave(&drv_data->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Wolfram Sang79970db2014-02-13 21:36:29 +0100636 drv_data->action = MV64XXX_I2C_ACTION_SEND_START;
637 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
638
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100639 drv_data->send_stop = is_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 drv_data->block = 1;
641 mv64xxx_i2c_do_action(drv_data);
642 spin_unlock_irqrestore(&drv_data->lock, flags);
643
644 mv64xxx_i2c_wait_for_completion(drv_data);
645 return drv_data->rc;
646}
647
648/*
649 *****************************************************************************
650 *
651 * I2C Core Support Routines (Interface to higher level I2C code)
652 *
653 *****************************************************************************
654 */
655static u32
656mv64xxx_i2c_functionality(struct i2c_adapter *adap)
657{
658 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
659}
660
661static int
662mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
663{
664 struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
Russell King4243fa02013-05-16 21:39:12 +0100665 int rc, ret = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Russell King4243fa02013-05-16 21:39:12 +0100667 BUG_ON(drv_data->msgs != NULL);
668 drv_data->msgs = msgs;
669 drv_data->num_msgs = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Russell King4243fa02013-05-16 21:39:12 +0100671 rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1);
672 if (rc < 0)
673 ret = rc;
674
675 drv_data->num_msgs = 0;
676 drv_data->msgs = NULL;
677
678 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Jean Delvare8f9082c2006-09-03 22:39:46 +0200681static const struct i2c_algorithm mv64xxx_i2c_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 .master_xfer = mv64xxx_i2c_xfer,
683 .functionality = mv64xxx_i2c_functionality,
684};
685
686/*
687 *****************************************************************************
688 *
689 * Driver Interface & Early Init Routines
690 *
691 *****************************************************************************
692 */
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200693static const struct of_device_id mv64xxx_i2c_of_match_table[] = {
Maxime Ripard3d66ac72013-06-12 18:53:32 +0200694 { .compatible = "allwinner,sun4i-i2c", .data = &mv64xxx_i2c_regs_sun4i},
Maxime Ripardc7dcb1f2014-03-04 17:28:38 +0100695 { .compatible = "allwinner,sun6i-a31-i2c", .data = &mv64xxx_i2c_regs_sun4i},
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200696 { .compatible = "marvell,mv64xxx-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200697 { .compatible = "marvell,mv78230-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
Gregory CLEMENT6cf70ae2013-12-31 16:59:33 +0100698 { .compatible = "marvell,mv78230-a0-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200699 {}
700};
701MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table);
702
Andrew Lunnb61d1572012-07-22 12:51:35 +0200703#ifdef CONFIG_OF
Thierry Redingc1a99462013-09-18 14:50:52 +0200704#ifdef CONFIG_HAVE_CLK
Bill Pemberton0b255e92012-11-27 15:59:38 -0500705static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200706mv64xxx_calc_freq(const int tclk, const int n, const int m)
707{
708 return tclk / (10 * (m + 1) * (2 << n));
709}
710
Bill Pemberton0b255e92012-11-27 15:59:38 -0500711static bool
Andrew Lunnb61d1572012-07-22 12:51:35 +0200712mv64xxx_find_baud_factors(const u32 req_freq, const u32 tclk, u32 *best_n,
713 u32 *best_m)
714{
715 int freq, delta, best_delta = INT_MAX;
716 int m, n;
717
718 for (n = 0; n <= 7; n++)
719 for (m = 0; m <= 15; m++) {
720 freq = mv64xxx_calc_freq(tclk, n, m);
721 delta = req_freq - freq;
722 if (delta >= 0 && delta < best_delta) {
723 *best_m = m;
724 *best_n = n;
725 best_delta = delta;
726 }
727 if (best_delta == 0)
728 return true;
729 }
730 if (best_delta == INT_MAX)
731 return false;
732 return true;
733}
Thierry Redingc1a99462013-09-18 14:50:52 +0200734#endif /* CONFIG_HAVE_CLK */
Andrew Lunnb61d1572012-07-22 12:51:35 +0200735
Bill Pemberton0b255e92012-11-27 15:59:38 -0500736static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200737mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200738 struct device *dev)
Andrew Lunnb61d1572012-07-22 12:51:35 +0200739{
Andrew Lunnb61d1572012-07-22 12:51:35 +0200740 /* CLK is mandatory when using DT to describe the i2c bus. We
741 * need to know tclk in order to calculate bus clock
742 * factors.
743 */
744#if !defined(CONFIG_HAVE_CLK)
745 /* Have OF but no CLK */
746 return -ENODEV;
747#else
Thierry Redingc1a99462013-09-18 14:50:52 +0200748 const struct of_device_id *device;
749 struct device_node *np = dev->of_node;
750 u32 bus_freq, tclk;
751 int rc = 0;
752
Andrew Lunnb61d1572012-07-22 12:51:35 +0200753 if (IS_ERR(drv_data->clk)) {
754 rc = -ENODEV;
755 goto out;
756 }
757 tclk = clk_get_rate(drv_data->clk);
Gregory CLEMENT4c730a02013-06-21 15:32:06 +0200758
759 rc = of_property_read_u32(np, "clock-frequency", &bus_freq);
760 if (rc)
761 bus_freq = 100000; /* 100kHz by default */
762
Andrew Lunnb61d1572012-07-22 12:51:35 +0200763 if (!mv64xxx_find_baud_factors(bus_freq, tclk,
764 &drv_data->freq_n, &drv_data->freq_m)) {
765 rc = -EINVAL;
766 goto out;
767 }
768 drv_data->irq = irq_of_parse_and_map(np, 0);
769
Maxime Ripard370136b2014-03-04 17:28:37 +0100770 drv_data->rstc = devm_reset_control_get(dev, NULL);
771 if (IS_ERR(drv_data->rstc)) {
772 if (PTR_ERR(drv_data->rstc) == -EPROBE_DEFER) {
773 rc = -EPROBE_DEFER;
774 goto out;
775 }
776 } else {
777 reset_control_deassert(drv_data->rstc);
778 }
779
Andrew Lunnb61d1572012-07-22 12:51:35 +0200780 /* Its not yet defined how timeouts will be specified in device tree.
781 * So hard code the value to 1 second.
782 */
783 drv_data->adapter.timeout = HZ;
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200784
785 device = of_match_device(mv64xxx_i2c_of_match_table, dev);
786 if (!device)
787 return -ENODEV;
788
789 memcpy(&drv_data->reg_offsets, device->data, sizeof(drv_data->reg_offsets));
790
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200791 /*
792 * For controllers embedded in new SoCs activate the
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200793 * Transaction Generator support and the errata fix.
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200794 */
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200795 if (of_device_is_compatible(np, "marvell,mv78230-i2c")) {
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200796 drv_data->offload_enabled = true;
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200797 drv_data->errata_delay = true;
798 }
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200799
Gregory CLEMENT6cf70ae2013-12-31 16:59:33 +0100800 if (of_device_is_compatible(np, "marvell,mv78230-a0-i2c")) {
801 drv_data->offload_enabled = false;
802 drv_data->errata_delay = true;
803 }
Maxime Ripardc7dcb1f2014-03-04 17:28:38 +0100804
805 if (of_device_is_compatible(np, "allwinner,sun6i-a31-i2c"))
806 drv_data->irq_clear_inverted = true;
807
Andrew Lunnb61d1572012-07-22 12:51:35 +0200808out:
809 return rc;
810#endif
811}
812#else /* CONFIG_OF */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500813static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200814mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200815 struct device *dev)
Andrew Lunnb61d1572012-07-22 12:51:35 +0200816{
817 return -ENODEV;
818}
819#endif /* CONFIG_OF */
820
Bill Pemberton0b255e92012-11-27 15:59:38 -0500821static int
Russell King3ae5eae2005-11-09 22:32:44 +0000822mv64xxx_i2c_probe(struct platform_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct mv64xxx_i2c_data *drv_data;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900825 struct mv64xxx_i2c_pdata *pdata = dev_get_platdata(&pd->dev);
Russell King16874b02013-05-16 21:33:09 +0100826 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int rc;
828
Andrew Lunnb61d1572012-07-22 12:51:35 +0200829 if ((!pdata && !pd->dev.of_node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -ENODEV;
831
Russell King2c911102013-05-16 21:35:10 +0100832 drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data),
833 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (!drv_data)
835 return -ENOMEM;
836
Russell King16874b02013-05-16 21:33:09 +0100837 r = platform_get_resource(pd, IORESOURCE_MEM, 0);
838 drv_data->reg_base = devm_ioremap_resource(&pd->dev, r);
Russell King2c911102013-05-16 21:35:10 +0100839 if (IS_ERR(drv_data->reg_base))
840 return PTR_ERR(drv_data->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Mark A. Greere91c0212005-12-18 17:22:01 +0100842 strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
David Brownell2096b952007-05-01 23:26:28 +0200843 sizeof(drv_data->adapter.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 init_waitqueue_head(&drv_data->waitq);
846 spin_lock_init(&drv_data->lock);
847
Andrew Lunnb61d1572012-07-22 12:51:35 +0200848#if defined(CONFIG_HAVE_CLK)
849 /* Not all platforms have a clk */
Russell King4c5c95f2013-05-16 21:34:10 +0100850 drv_data->clk = devm_clk_get(&pd->dev, NULL);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200851 if (!IS_ERR(drv_data->clk)) {
852 clk_prepare(drv_data->clk);
853 clk_enable(drv_data->clk);
854 }
855#endif
856 if (pdata) {
857 drv_data->freq_m = pdata->freq_m;
858 drv_data->freq_n = pdata->freq_n;
859 drv_data->irq = platform_get_irq(pd, 0);
860 drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200861 drv_data->offload_enabled = false;
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200862 memcpy(&drv_data->reg_offsets, &mv64xxx_i2c_regs_mv64xxx, sizeof(drv_data->reg_offsets));
Andrew Lunnb61d1572012-07-22 12:51:35 +0200863 } else if (pd->dev.of_node) {
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200864 rc = mv64xxx_of_config(drv_data, &pd->dev);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200865 if (rc)
Russell King2c911102013-05-16 21:35:10 +0100866 goto exit_clk;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200867 }
David Vrabel48944732006-01-19 17:56:29 +0000868 if (drv_data->irq < 0) {
869 rc = -ENXIO;
Maxime Ripard370136b2014-03-04 17:28:37 +0100870 goto exit_reset;
David Vrabel48944732006-01-19 17:56:29 +0000871 }
Andrew Lunnb61d1572012-07-22 12:51:35 +0200872
Jean Delvare12a917f2007-02-13 22:09:03 +0100873 drv_data->adapter.dev.parent = &pd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 drv_data->adapter.algo = &mv64xxx_i2c_algo;
875 drv_data->adapter.owner = THIS_MODULE;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200876 drv_data->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200877 drv_data->adapter.nr = pd->id;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200878 drv_data->adapter.dev.of_node = pd->dev.of_node;
Russell King3ae5eae2005-11-09 22:32:44 +0000879 platform_set_drvdata(pd, drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 i2c_set_adapdata(&drv_data->adapter, drv_data);
881
Maxime Bizon3269bb62007-01-05 17:54:05 +0100882 mv64xxx_i2c_hw_init(drv_data);
883
Russell King0c195af2013-05-16 21:36:11 +0100884 rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
885 MV64XXX_I2C_CTLR_NAME, drv_data);
886 if (rc) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800887 dev_err(&drv_data->adapter.dev,
Russell King0c195af2013-05-16 21:36:11 +0100888 "mv64xxx: Can't register intr handler irq%d: %d\n",
889 drv_data->irq, rc);
Maxime Ripard370136b2014-03-04 17:28:37 +0100890 goto exit_reset;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200891 } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800892 dev_err(&drv_data->adapter.dev,
893 "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto exit_free_irq;
895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
898
Russell King2c911102013-05-16 21:35:10 +0100899exit_free_irq:
900 free_irq(drv_data->irq, drv_data);
Maxime Ripard370136b2014-03-04 17:28:37 +0100901exit_reset:
902 if (pd->dev.of_node && !IS_ERR(drv_data->rstc))
903 reset_control_assert(drv_data->rstc);
Russell King2c911102013-05-16 21:35:10 +0100904exit_clk:
Andrew Lunnb61d1572012-07-22 12:51:35 +0200905#if defined(CONFIG_HAVE_CLK)
906 /* Not all platforms have a clk */
907 if (!IS_ERR(drv_data->clk)) {
908 clk_disable(drv_data->clk);
909 clk_unprepare(drv_data->clk);
910 }
911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return rc;
913}
914
Bill Pemberton0b255e92012-11-27 15:59:38 -0500915static int
Russell King3ae5eae2005-11-09 22:32:44 +0000916mv64xxx_i2c_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Russell King3ae5eae2005-11-09 22:32:44 +0000918 struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000920 i2c_del_adapter(&drv_data->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 free_irq(drv_data->irq, drv_data);
Maxime Ripard370136b2014-03-04 17:28:37 +0100922 if (dev->dev.of_node && !IS_ERR(drv_data->rstc))
923 reset_control_assert(drv_data->rstc);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200924#if defined(CONFIG_HAVE_CLK)
925 /* Not all platforms have a clk */
926 if (!IS_ERR(drv_data->clk)) {
927 clk_disable(drv_data->clk);
928 clk_unprepare(drv_data->clk);
929 }
930#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Russell King3ae5eae2005-11-09 22:32:44 +0000935static struct platform_driver mv64xxx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 .probe = mv64xxx_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500937 .remove = mv64xxx_i2c_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000938 .driver = {
939 .owner = THIS_MODULE,
940 .name = MV64XXX_I2C_CTLR_NAME,
Sachin Kamat4e905322013-09-30 09:04:25 +0530941 .of_match_table = mv64xxx_i2c_of_match_table,
Russell King3ae5eae2005-11-09 22:32:44 +0000942 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943};
944
Axel Lina3664b52012-01-12 20:32:04 +0100945module_platform_driver(mv64xxx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
948MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
949MODULE_LICENSE("GPL");