blob: 6cb5d2f93d59d6e8352b985e3cc7376b801113db [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,
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100101 MV64XXX_I2C_ACTION_SEND_RESTART,
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200102 MV64XXX_I2C_ACTION_OFFLOAD_RESTART,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 MV64XXX_I2C_ACTION_SEND_ADDR_1,
104 MV64XXX_I2C_ACTION_SEND_ADDR_2,
105 MV64XXX_I2C_ACTION_SEND_DATA,
106 MV64XXX_I2C_ACTION_RCV_DATA,
107 MV64XXX_I2C_ACTION_RCV_DATA_STOP,
108 MV64XXX_I2C_ACTION_SEND_STOP,
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200109 MV64XXX_I2C_ACTION_OFFLOAD_SEND_STOP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200112struct mv64xxx_i2c_regs {
113 u8 addr;
114 u8 ext_addr;
115 u8 data;
116 u8 control;
117 u8 status;
118 u8 clock;
119 u8 soft_reset;
120};
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122struct mv64xxx_i2c_data {
Russell King4243fa02013-05-16 21:39:12 +0100123 struct i2c_msg *msgs;
124 int num_msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int irq;
126 u32 state;
127 u32 action;
Mark A. Greere91c0212005-12-18 17:22:01 +0100128 u32 aborting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 u32 cntl_bits;
130 void __iomem *reg_base;
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200131 struct mv64xxx_i2c_regs reg_offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 u32 addr1;
133 u32 addr2;
134 u32 bytes_left;
135 u32 byte_posn;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100136 u32 send_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 u32 block;
138 int rc;
139 u32 freq_m;
140 u32 freq_n;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200141#if defined(CONFIG_HAVE_CLK)
142 struct clk *clk;
143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 wait_queue_head_t waitq;
145 spinlock_t lock;
146 struct i2c_msg *msg;
147 struct i2c_adapter adapter;
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200148 bool offload_enabled;
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200149/* 5us delay in order to avoid repeated start timing violation */
150 bool errata_delay;
Maxime Ripard370136b2014-03-04 17:28:37 +0100151 struct reset_control *rstc;
Maxime Ripardc7dcb1f2014-03-04 17:28:38 +0100152 bool irq_clear_inverted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200155static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_mv64xxx = {
156 .addr = 0x00,
157 .ext_addr = 0x10,
158 .data = 0x04,
159 .control = 0x08,
160 .status = 0x0c,
161 .clock = 0x0c,
162 .soft_reset = 0x1c,
163};
164
Maxime Ripard3d66ac72013-06-12 18:53:32 +0200165static struct mv64xxx_i2c_regs mv64xxx_i2c_regs_sun4i = {
166 .addr = 0x00,
167 .ext_addr = 0x04,
168 .data = 0x08,
169 .control = 0x0c,
170 .status = 0x10,
171 .clock = 0x14,
172 .soft_reset = 0x18,
173};
174
Russell King3420afb2013-05-16 21:38:11 +0100175static void
176mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
177 struct i2c_msg *msg)
178{
179 u32 dir = 0;
180
181 drv_data->msg = msg;
182 drv_data->byte_posn = 0;
183 drv_data->bytes_left = msg->len;
184 drv_data->aborting = 0;
185 drv_data->rc = 0;
186 drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
187 MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
188
189 if (msg->flags & I2C_M_RD)
190 dir = 1;
191
192 if (msg->flags & I2C_M_TEN) {
193 drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
194 drv_data->addr2 = (u32)msg->addr & 0xff;
195 } else {
Maxime Ripard683e69b2013-06-12 18:53:30 +0200196 drv_data->addr1 = MV64XXX_I2C_ADDR_ADDR((u32)msg->addr) | dir;
Russell King3420afb2013-05-16 21:38:11 +0100197 drv_data->addr2 = 0;
198 }
199}
200
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200201static int mv64xxx_i2c_offload_msg(struct mv64xxx_i2c_data *drv_data)
202{
203 unsigned long data_reg_hi = 0;
204 unsigned long data_reg_lo = 0;
205 unsigned long ctrl_reg;
206 struct i2c_msg *msg = drv_data->msgs;
207
Wolfram Sang79970db2014-02-13 21:36:29 +0100208 if (!drv_data->offload_enabled)
209 return -EOPNOTSUPP;
210
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200211 drv_data->msg = msg;
212 drv_data->byte_posn = 0;
213 drv_data->bytes_left = msg->len;
214 drv_data->aborting = 0;
215 drv_data->rc = 0;
216 /* Only regular transactions can be offloaded */
217 if ((msg->flags & ~(I2C_M_TEN | I2C_M_RD)) != 0)
218 return -EINVAL;
219
220 /* Only 1-8 byte transfers can be offloaded */
221 if (msg->len < 1 || msg->len > 8)
222 return -EINVAL;
223
224 /* Build transaction */
225 ctrl_reg = MV64XXX_I2C_BRIDGE_CONTROL_ENABLE |
226 (msg->addr << MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT);
227
228 if ((msg->flags & I2C_M_TEN) != 0)
229 ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT;
230
231 if ((msg->flags & I2C_M_RD) == 0) {
232 u8 local_buf[8] = { 0 };
233
234 memcpy(local_buf, msg->buf, msg->len);
235 data_reg_lo = cpu_to_le32(*((u32 *)local_buf));
236 data_reg_hi = cpu_to_le32(*((u32 *)(local_buf+4)));
237
238 ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_WR |
239 (msg->len - 1) << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT;
240
Thierry Reding85b3a932013-09-18 14:51:40 +0200241 writel(data_reg_lo,
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200242 drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_LO);
Thierry Reding85b3a932013-09-18 14:51:40 +0200243 writel(data_reg_hi,
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200244 drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_HI);
245
246 } else {
247 ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_RD |
248 (msg->len - 1) << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT;
249 }
250
251 /* Execute transaction */
252 writel(ctrl_reg, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
253
254 return 0;
255}
256
257static void
258mv64xxx_i2c_update_offload_data(struct mv64xxx_i2c_data *drv_data)
259{
260 struct i2c_msg *msg = drv_data->msg;
261
262 if (msg->flags & I2C_M_RD) {
263 u32 data_reg_lo = readl(drv_data->reg_base +
264 MV64XXX_I2C_REG_RX_DATA_LO);
265 u32 data_reg_hi = readl(drv_data->reg_base +
266 MV64XXX_I2C_REG_RX_DATA_HI);
267 u8 local_buf[8] = { 0 };
268
269 *((u32 *)local_buf) = le32_to_cpu(data_reg_lo);
270 *((u32 *)(local_buf+4)) = le32_to_cpu(data_reg_hi);
271 memcpy(msg->buf, local_buf, msg->len);
272 }
273
274}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/*
276 *****************************************************************************
277 *
278 * Finite State Machine & Interrupt Routines
279 *
280 *****************************************************************************
281 */
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200282
283/* Reset hardware and initialize FSM */
284static void
285mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
286{
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200287 if (drv_data->offload_enabled) {
288 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
289 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_TIMING);
290 writel(0, drv_data->reg_base +
291 MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
292 writel(0, drv_data->reg_base +
293 MV64XXX_I2C_REG_BRIDGE_INTR_MASK);
294 }
295
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200296 writel(0, drv_data->reg_base + drv_data->reg_offsets.soft_reset);
Maxime Ripard683e69b2013-06-12 18:53:30 +0200297 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 +0200298 drv_data->reg_base + drv_data->reg_offsets.clock);
299 writel(0, drv_data->reg_base + drv_data->reg_offsets.addr);
300 writel(0, drv_data->reg_base + drv_data->reg_offsets.ext_addr);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200301 writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200302 drv_data->reg_base + drv_data->reg_offsets.control);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200303 drv_data->state = MV64XXX_I2C_STATE_IDLE;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static void
307mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
308{
309 /*
310 * If state is idle, then this is likely the remnants of an old
311 * operation that driver has given up on or the user has killed.
312 * If so, issue the stop condition and go to idle.
313 */
314 if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
315 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
316 return;
317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* The status from the ctlr [mostly] tells us what to do next */
320 switch (status) {
321 /* Start condition interrupt */
322 case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
323 case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
324 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
325 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
326 break;
327
328 /* Performing a write */
329 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
330 if (drv_data->msg->flags & I2C_M_TEN) {
331 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
332 drv_data->state =
333 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
334 break;
335 }
336 /* FALLTHRU */
337 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
338 case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
Mark A. Greere91c0212005-12-18 17:22:01 +0100339 if ((drv_data->bytes_left == 0)
340 || (drv_data->aborting
341 && (drv_data->byte_posn != 0))) {
Russell King4243fa02013-05-16 21:39:12 +0100342 if (drv_data->send_stop || drv_data->aborting) {
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100343 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
344 drv_data->state = MV64XXX_I2C_STATE_IDLE;
345 } else {
346 drv_data->action =
347 MV64XXX_I2C_ACTION_SEND_RESTART;
348 drv_data->state =
349 MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
350 }
Mark A. Greere91c0212005-12-18 17:22:01 +0100351 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
353 drv_data->state =
354 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
355 drv_data->bytes_left--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 break;
358
359 /* Performing a read */
360 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
361 if (drv_data->msg->flags & I2C_M_TEN) {
362 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
363 drv_data->state =
364 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
365 break;
366 }
367 /* FALLTHRU */
368 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
369 if (drv_data->bytes_left == 0) {
370 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
371 drv_data->state = MV64XXX_I2C_STATE_IDLE;
372 break;
373 }
374 /* FALLTHRU */
375 case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
376 if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
377 drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
378 else {
379 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
380 drv_data->bytes_left--;
381 }
382 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
383
Mark A. Greere91c0212005-12-18 17:22:01 +0100384 if ((drv_data->bytes_left == 1) || drv_data->aborting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
386 break;
387
388 case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
389 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
390 drv_data->state = MV64XXX_I2C_STATE_IDLE;
391 break;
392
393 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
394 case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
395 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
396 /* Doesn't seem to be a device at other end */
397 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
398 drv_data->state = MV64XXX_I2C_STATE_IDLE;
Guenter Roeck6faa3532013-06-19 14:53:52 -0700399 drv_data->rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
401
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200402 case MV64XXX_I2C_STATUS_OFFLOAD_OK:
403 if (drv_data->send_stop || drv_data->aborting) {
404 drv_data->action = MV64XXX_I2C_ACTION_OFFLOAD_SEND_STOP;
405 drv_data->state = MV64XXX_I2C_STATE_IDLE;
406 } else {
407 drv_data->action = MV64XXX_I2C_ACTION_OFFLOAD_RESTART;
408 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
409 }
410 break;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 default:
413 dev_err(&drv_data->adapter.dev,
414 "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
415 "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
416 drv_data->state, status, drv_data->msg->addr,
417 drv_data->msg->flags);
418 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200419 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 drv_data->rc = -EIO;
421 }
422}
423
Wolfram Sang4c5b38e2014-02-13 21:36:31 +0100424static void mv64xxx_i2c_send_start(struct mv64xxx_i2c_data *drv_data)
425{
426 /* Can we offload this msg ? */
427 if (mv64xxx_i2c_offload_msg(drv_data) < 0) {
428 /* No, switch to standard path */
429 mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs);
430 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
431 drv_data->reg_base + drv_data->reg_offsets.control);
432 }
433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static void
436mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
437{
438 switch(drv_data->action) {
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200439 case MV64XXX_I2C_ACTION_OFFLOAD_RESTART:
440 mv64xxx_i2c_update_offload_data(drv_data);
441 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
442 writel(0, drv_data->reg_base +
443 MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
444 /* FALLTHRU */
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100445 case MV64XXX_I2C_ACTION_SEND_RESTART:
Russell King4243fa02013-05-16 21:39:12 +0100446 /* We should only get here if we have further messages */
447 BUG_ON(drv_data->num_msgs == 0);
448
Russell King4243fa02013-05-16 21:39:12 +0100449 drv_data->msgs++;
450 drv_data->num_msgs--;
Wolfram Sang4c5b38e2014-02-13 21:36:31 +0100451 mv64xxx_i2c_send_start(drv_data);
Russell King4243fa02013-05-16 21:39:12 +0100452
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200453 if (drv_data->errata_delay)
454 udelay(5);
455
Russell King4243fa02013-05-16 21:39:12 +0100456 /*
457 * We're never at the start of the message here, and by this
458 * time it's already too late to do any protocol mangling.
459 * Thankfully, do not advertise support for that feature.
460 */
461 drv_data->send_stop = drv_data->num_msgs == 1;
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100462 break;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 case MV64XXX_I2C_ACTION_CONTINUE:
465 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200466 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case MV64XXX_I2C_ACTION_SEND_ADDR_1:
470 writel(drv_data->addr1,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200471 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200473 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475
476 case MV64XXX_I2C_ACTION_SEND_ADDR_2:
477 writel(drv_data->addr2,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200478 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200480 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 break;
482
483 case MV64XXX_I2C_ACTION_SEND_DATA:
484 writel(drv_data->msg->buf[drv_data->byte_posn++],
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200485 drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200487 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489
490 case MV64XXX_I2C_ACTION_RCV_DATA:
491 drv_data->msg->buf[drv_data->byte_posn++] =
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200492 readl(drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 writel(drv_data->cntl_bits,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200494 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496
497 case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
498 drv_data->msg->buf[drv_data->byte_posn++] =
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200499 readl(drv_data->reg_base + drv_data->reg_offsets.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
501 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200502 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 drv_data->block = 0;
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200504 if (drv_data->errata_delay)
505 udelay(5);
506
Russell Kingd295a862013-05-16 10:30:59 +0000507 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
509
510 case MV64XXX_I2C_ACTION_INVALID:
511 default:
512 dev_err(&drv_data->adapter.dev,
513 "mv64xxx_i2c_do_action: Invalid action: %d\n",
514 drv_data->action);
515 drv_data->rc = -EIO;
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* FALLTHRU */
518 case MV64XXX_I2C_ACTION_SEND_STOP:
519 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
520 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200521 drv_data->reg_base + drv_data->reg_offsets.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 drv_data->block = 0;
Russell Kingd295a862013-05-16 10:30:59 +0000523 wake_up(&drv_data->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200525
526 case MV64XXX_I2C_ACTION_OFFLOAD_SEND_STOP:
527 mv64xxx_i2c_update_offload_data(drv_data);
528 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
529 writel(0, drv_data->reg_base +
530 MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
531 drv_data->block = 0;
532 wake_up(&drv_data->waitq);
533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535}
536
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200537static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100538mv64xxx_i2c_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct mv64xxx_i2c_data *drv_data = dev_id;
541 unsigned long flags;
542 u32 status;
Mikael Petterssonb0999cc2009-09-07 12:00:13 +0200543 irqreturn_t rc = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 spin_lock_irqsave(&drv_data->lock, flags);
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200546
547 if (drv_data->offload_enabled) {
548 while (readl(drv_data->reg_base +
549 MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE)) {
550 int reg_status = readl(drv_data->reg_base +
551 MV64XXX_I2C_REG_BRIDGE_STATUS);
552 if (reg_status & MV64XXX_I2C_BRIDGE_STATUS_ERROR)
553 status = MV64XXX_I2C_STATUS_OFFLOAD_ERROR;
554 else
555 status = MV64XXX_I2C_STATUS_OFFLOAD_OK;
556 mv64xxx_i2c_fsm(drv_data, status);
557 mv64xxx_i2c_do_action(drv_data);
558 rc = IRQ_HANDLED;
559 }
560 }
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200561 while (readl(drv_data->reg_base + drv_data->reg_offsets.control) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 MV64XXX_I2C_REG_CONTROL_IFLG) {
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200563 status = readl(drv_data->reg_base + drv_data->reg_offsets.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 mv64xxx_i2c_fsm(drv_data, status);
565 mv64xxx_i2c_do_action(drv_data);
Maxime Ripardc7dcb1f2014-03-04 17:28:38 +0100566
567 if (drv_data->irq_clear_inverted)
568 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_IFLG,
569 drv_data->reg_base + drv_data->reg_offsets.control);
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 rc = IRQ_HANDLED;
572 }
573 spin_unlock_irqrestore(&drv_data->lock, flags);
574
575 return rc;
576}
577
578/*
579 *****************************************************************************
580 *
581 * I2C Msg Execution Routines
582 *
583 *****************************************************************************
584 */
585static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
587{
588 long time_left;
589 unsigned long flags;
590 char abort = 0;
591
Russell Kingd295a862013-05-16 10:30:59 +0000592 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100593 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 spin_lock_irqsave(&drv_data->lock, flags);
596 if (!time_left) { /* Timed out */
597 drv_data->rc = -ETIMEDOUT;
598 abort = 1;
599 } else if (time_left < 0) { /* Interrupted/Error */
600 drv_data->rc = time_left; /* errno value */
601 abort = 1;
602 }
603
604 if (abort && drv_data->block) {
Mark A. Greere91c0212005-12-18 17:22:01 +0100605 drv_data->aborting = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 spin_unlock_irqrestore(&drv_data->lock, flags);
607
608 time_left = wait_event_timeout(drv_data->waitq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100609 !drv_data->block, drv_data->adapter.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Mark A. Greere91c0212005-12-18 17:22:01 +0100611 if ((time_left <= 0) && drv_data->block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 drv_data->state = MV64XXX_I2C_STATE_IDLE;
613 dev_err(&drv_data->adapter.dev,
Mark A. Greere91c0212005-12-18 17:22:01 +0100614 "mv64xxx: I2C bus locked, block: %d, "
615 "time_left: %d\n", drv_data->block,
616 (int)time_left);
Dale Farnswortha07ad1c2007-08-14 18:37:14 +0200617 mv64xxx_i2c_hw_init(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 } else
620 spin_unlock_irqrestore(&drv_data->lock, flags);
621}
622
623static int
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100624mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg,
Russell King4243fa02013-05-16 21:39:12 +0100625 int is_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 unsigned long flags;
628
629 spin_lock_irqsave(&drv_data->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Wolfram Sang79970db2014-02-13 21:36:29 +0100631 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
632
Rodolfo Giomettieda6bee2010-11-26 17:06:56 +0100633 drv_data->send_stop = is_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 drv_data->block = 1;
Wolfram Sangb0200ab2014-02-13 21:36:32 +0100635 mv64xxx_i2c_send_start(drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 spin_unlock_irqrestore(&drv_data->lock, flags);
637
638 mv64xxx_i2c_wait_for_completion(drv_data);
639 return drv_data->rc;
640}
641
642/*
643 *****************************************************************************
644 *
645 * I2C Core Support Routines (Interface to higher level I2C code)
646 *
647 *****************************************************************************
648 */
649static u32
650mv64xxx_i2c_functionality(struct i2c_adapter *adap)
651{
652 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
653}
654
655static int
656mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
657{
658 struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
Russell King4243fa02013-05-16 21:39:12 +0100659 int rc, ret = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Russell King4243fa02013-05-16 21:39:12 +0100661 BUG_ON(drv_data->msgs != NULL);
662 drv_data->msgs = msgs;
663 drv_data->num_msgs = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Russell King4243fa02013-05-16 21:39:12 +0100665 rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1);
666 if (rc < 0)
667 ret = rc;
668
669 drv_data->num_msgs = 0;
670 drv_data->msgs = NULL;
671
672 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Jean Delvare8f9082c2006-09-03 22:39:46 +0200675static const struct i2c_algorithm mv64xxx_i2c_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 .master_xfer = mv64xxx_i2c_xfer,
677 .functionality = mv64xxx_i2c_functionality,
678};
679
680/*
681 *****************************************************************************
682 *
683 * Driver Interface & Early Init Routines
684 *
685 *****************************************************************************
686 */
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200687static const struct of_device_id mv64xxx_i2c_of_match_table[] = {
Maxime Ripard3d66ac72013-06-12 18:53:32 +0200688 { .compatible = "allwinner,sun4i-i2c", .data = &mv64xxx_i2c_regs_sun4i},
Maxime Ripardc7dcb1f2014-03-04 17:28:38 +0100689 { .compatible = "allwinner,sun6i-a31-i2c", .data = &mv64xxx_i2c_regs_sun4i},
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200690 { .compatible = "marvell,mv64xxx-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200691 { .compatible = "marvell,mv78230-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
Gregory CLEMENT6cf70ae2013-12-31 16:59:33 +0100692 { .compatible = "marvell,mv78230-a0-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200693 {}
694};
695MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table);
696
Andrew Lunnb61d1572012-07-22 12:51:35 +0200697#ifdef CONFIG_OF
Thierry Redingc1a99462013-09-18 14:50:52 +0200698#ifdef CONFIG_HAVE_CLK
Bill Pemberton0b255e92012-11-27 15:59:38 -0500699static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200700mv64xxx_calc_freq(const int tclk, const int n, const int m)
701{
702 return tclk / (10 * (m + 1) * (2 << n));
703}
704
Bill Pemberton0b255e92012-11-27 15:59:38 -0500705static bool
Andrew Lunnb61d1572012-07-22 12:51:35 +0200706mv64xxx_find_baud_factors(const u32 req_freq, const u32 tclk, u32 *best_n,
707 u32 *best_m)
708{
709 int freq, delta, best_delta = INT_MAX;
710 int m, n;
711
712 for (n = 0; n <= 7; n++)
713 for (m = 0; m <= 15; m++) {
714 freq = mv64xxx_calc_freq(tclk, n, m);
715 delta = req_freq - freq;
716 if (delta >= 0 && delta < best_delta) {
717 *best_m = m;
718 *best_n = n;
719 best_delta = delta;
720 }
721 if (best_delta == 0)
722 return true;
723 }
724 if (best_delta == INT_MAX)
725 return false;
726 return true;
727}
Thierry Redingc1a99462013-09-18 14:50:52 +0200728#endif /* CONFIG_HAVE_CLK */
Andrew Lunnb61d1572012-07-22 12:51:35 +0200729
Bill Pemberton0b255e92012-11-27 15:59:38 -0500730static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200731mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200732 struct device *dev)
Andrew Lunnb61d1572012-07-22 12:51:35 +0200733{
Andrew Lunnb61d1572012-07-22 12:51:35 +0200734 /* CLK is mandatory when using DT to describe the i2c bus. We
735 * need to know tclk in order to calculate bus clock
736 * factors.
737 */
738#if !defined(CONFIG_HAVE_CLK)
739 /* Have OF but no CLK */
740 return -ENODEV;
741#else
Thierry Redingc1a99462013-09-18 14:50:52 +0200742 const struct of_device_id *device;
743 struct device_node *np = dev->of_node;
744 u32 bus_freq, tclk;
745 int rc = 0;
746
Andrew Lunnb61d1572012-07-22 12:51:35 +0200747 if (IS_ERR(drv_data->clk)) {
748 rc = -ENODEV;
749 goto out;
750 }
751 tclk = clk_get_rate(drv_data->clk);
Gregory CLEMENT4c730a02013-06-21 15:32:06 +0200752
753 rc = of_property_read_u32(np, "clock-frequency", &bus_freq);
754 if (rc)
755 bus_freq = 100000; /* 100kHz by default */
756
Andrew Lunnb61d1572012-07-22 12:51:35 +0200757 if (!mv64xxx_find_baud_factors(bus_freq, tclk,
758 &drv_data->freq_n, &drv_data->freq_m)) {
759 rc = -EINVAL;
760 goto out;
761 }
762 drv_data->irq = irq_of_parse_and_map(np, 0);
763
Maxime Ripard370136b2014-03-04 17:28:37 +0100764 drv_data->rstc = devm_reset_control_get(dev, NULL);
765 if (IS_ERR(drv_data->rstc)) {
766 if (PTR_ERR(drv_data->rstc) == -EPROBE_DEFER) {
767 rc = -EPROBE_DEFER;
768 goto out;
769 }
770 } else {
771 reset_control_deassert(drv_data->rstc);
772 }
773
Andrew Lunnb61d1572012-07-22 12:51:35 +0200774 /* Its not yet defined how timeouts will be specified in device tree.
775 * So hard code the value to 1 second.
776 */
777 drv_data->adapter.timeout = HZ;
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200778
779 device = of_match_device(mv64xxx_i2c_of_match_table, dev);
780 if (!device)
781 return -ENODEV;
782
783 memcpy(&drv_data->reg_offsets, device->data, sizeof(drv_data->reg_offsets));
784
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200785 /*
786 * For controllers embedded in new SoCs activate the
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200787 * Transaction Generator support and the errata fix.
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200788 */
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200789 if (of_device_is_compatible(np, "marvell,mv78230-i2c")) {
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200790 drv_data->offload_enabled = true;
Gregory CLEMENTc1d15b62013-08-22 16:19:06 +0200791 drv_data->errata_delay = true;
792 }
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200793
Gregory CLEMENT6cf70ae2013-12-31 16:59:33 +0100794 if (of_device_is_compatible(np, "marvell,mv78230-a0-i2c")) {
795 drv_data->offload_enabled = false;
796 drv_data->errata_delay = true;
797 }
Maxime Ripardc7dcb1f2014-03-04 17:28:38 +0100798
799 if (of_device_is_compatible(np, "allwinner,sun6i-a31-i2c"))
800 drv_data->irq_clear_inverted = true;
801
Andrew Lunnb61d1572012-07-22 12:51:35 +0200802out:
803 return rc;
804#endif
805}
806#else /* CONFIG_OF */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500807static int
Andrew Lunnb61d1572012-07-22 12:51:35 +0200808mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200809 struct device *dev)
Andrew Lunnb61d1572012-07-22 12:51:35 +0200810{
811 return -ENODEV;
812}
813#endif /* CONFIG_OF */
814
Bill Pemberton0b255e92012-11-27 15:59:38 -0500815static int
Russell King3ae5eae2005-11-09 22:32:44 +0000816mv64xxx_i2c_probe(struct platform_device *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct mv64xxx_i2c_data *drv_data;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900819 struct mv64xxx_i2c_pdata *pdata = dev_get_platdata(&pd->dev);
Russell King16874b02013-05-16 21:33:09 +0100820 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 int rc;
822
Andrew Lunnb61d1572012-07-22 12:51:35 +0200823 if ((!pdata && !pd->dev.of_node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -ENODEV;
825
Russell King2c911102013-05-16 21:35:10 +0100826 drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data),
827 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!drv_data)
829 return -ENOMEM;
830
Russell King16874b02013-05-16 21:33:09 +0100831 r = platform_get_resource(pd, IORESOURCE_MEM, 0);
832 drv_data->reg_base = devm_ioremap_resource(&pd->dev, r);
Russell King2c911102013-05-16 21:35:10 +0100833 if (IS_ERR(drv_data->reg_base))
834 return PTR_ERR(drv_data->reg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Mark A. Greere91c0212005-12-18 17:22:01 +0100836 strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
David Brownell2096b952007-05-01 23:26:28 +0200837 sizeof(drv_data->adapter.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 init_waitqueue_head(&drv_data->waitq);
840 spin_lock_init(&drv_data->lock);
841
Andrew Lunnb61d1572012-07-22 12:51:35 +0200842#if defined(CONFIG_HAVE_CLK)
843 /* Not all platforms have a clk */
Russell King4c5c95f2013-05-16 21:34:10 +0100844 drv_data->clk = devm_clk_get(&pd->dev, NULL);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200845 if (!IS_ERR(drv_data->clk)) {
846 clk_prepare(drv_data->clk);
847 clk_enable(drv_data->clk);
848 }
849#endif
850 if (pdata) {
851 drv_data->freq_m = pdata->freq_m;
852 drv_data->freq_n = pdata->freq_n;
853 drv_data->irq = platform_get_irq(pd, 0);
854 drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
Gregory CLEMENT930ab3d2013-08-22 16:19:05 +0200855 drv_data->offload_enabled = false;
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200856 memcpy(&drv_data->reg_offsets, &mv64xxx_i2c_regs_mv64xxx, sizeof(drv_data->reg_offsets));
Andrew Lunnb61d1572012-07-22 12:51:35 +0200857 } else if (pd->dev.of_node) {
Maxime Ripard004e8ed2013-06-12 18:53:31 +0200858 rc = mv64xxx_of_config(drv_data, &pd->dev);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200859 if (rc)
Russell King2c911102013-05-16 21:35:10 +0100860 goto exit_clk;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200861 }
David Vrabel48944732006-01-19 17:56:29 +0000862 if (drv_data->irq < 0) {
863 rc = -ENXIO;
Maxime Ripard370136b2014-03-04 17:28:37 +0100864 goto exit_reset;
David Vrabel48944732006-01-19 17:56:29 +0000865 }
Andrew Lunnb61d1572012-07-22 12:51:35 +0200866
Jean Delvare12a917f2007-02-13 22:09:03 +0100867 drv_data->adapter.dev.parent = &pd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 drv_data->adapter.algo = &mv64xxx_i2c_algo;
869 drv_data->adapter.owner = THIS_MODULE;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200870 drv_data->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200871 drv_data->adapter.nr = pd->id;
Andrew Lunnb61d1572012-07-22 12:51:35 +0200872 drv_data->adapter.dev.of_node = pd->dev.of_node;
Russell King3ae5eae2005-11-09 22:32:44 +0000873 platform_set_drvdata(pd, drv_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 i2c_set_adapdata(&drv_data->adapter, drv_data);
875
Maxime Bizon3269bb62007-01-05 17:54:05 +0100876 mv64xxx_i2c_hw_init(drv_data);
877
Russell King0c195af2013-05-16 21:36:11 +0100878 rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
879 MV64XXX_I2C_CTLR_NAME, drv_data);
880 if (rc) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800881 dev_err(&drv_data->adapter.dev,
Russell King0c195af2013-05-16 21:36:11 +0100882 "mv64xxx: Can't register intr handler irq%d: %d\n",
883 drv_data->irq, rc);
Maxime Ripard370136b2014-03-04 17:28:37 +0100884 goto exit_reset;
Dale Farnsworth65b22ad2007-07-12 14:12:29 +0200885 } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
Mark A. Greerdfded4a2005-12-16 11:08:43 -0800886 dev_err(&drv_data->adapter.dev,
887 "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto exit_free_irq;
889 }
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return 0;
892
Russell King2c911102013-05-16 21:35:10 +0100893exit_free_irq:
894 free_irq(drv_data->irq, drv_data);
Maxime Ripard370136b2014-03-04 17:28:37 +0100895exit_reset:
896 if (pd->dev.of_node && !IS_ERR(drv_data->rstc))
897 reset_control_assert(drv_data->rstc);
Russell King2c911102013-05-16 21:35:10 +0100898exit_clk:
Andrew Lunnb61d1572012-07-22 12:51:35 +0200899#if defined(CONFIG_HAVE_CLK)
900 /* Not all platforms have a clk */
901 if (!IS_ERR(drv_data->clk)) {
902 clk_disable(drv_data->clk);
903 clk_unprepare(drv_data->clk);
904 }
905#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return rc;
907}
908
Bill Pemberton0b255e92012-11-27 15:59:38 -0500909static int
Russell King3ae5eae2005-11-09 22:32:44 +0000910mv64xxx_i2c_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Russell King3ae5eae2005-11-09 22:32:44 +0000912 struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000914 i2c_del_adapter(&drv_data->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 free_irq(drv_data->irq, drv_data);
Maxime Ripard370136b2014-03-04 17:28:37 +0100916 if (dev->dev.of_node && !IS_ERR(drv_data->rstc))
917 reset_control_assert(drv_data->rstc);
Andrew Lunnb61d1572012-07-22 12:51:35 +0200918#if defined(CONFIG_HAVE_CLK)
919 /* Not all platforms have a clk */
920 if (!IS_ERR(drv_data->clk)) {
921 clk_disable(drv_data->clk);
922 clk_unprepare(drv_data->clk);
923 }
924#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Russell King3ae5eae2005-11-09 22:32:44 +0000929static struct platform_driver mv64xxx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 .probe = mv64xxx_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500931 .remove = mv64xxx_i2c_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000932 .driver = {
933 .owner = THIS_MODULE,
934 .name = MV64XXX_I2C_CTLR_NAME,
Sachin Kamat4e905322013-09-30 09:04:25 +0530935 .of_match_table = mv64xxx_i2c_of_match_table,
Russell King3ae5eae2005-11-09 22:32:44 +0000936 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937};
938
Axel Lina3664b52012-01-12 20:32:04 +0100939module_platform_driver(mv64xxx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
942MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
943MODULE_LICENSE("GPL");