blob: 309d0d5928904dd5c95acc6af39820b0bcad71e6 [file] [log] [blame]
Magnus Dammda672772008-04-22 22:16:49 +02001/*
2 * SuperH Mobile I2C Controller
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
6 * Portions of the code based on out-of-tree driver i2c-sh7343.c
7 * Copyright (c) 2006 Carlos Munoz <carlos@kenati.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/platform_device.h>
28#include <linux/interrupt.h>
29#include <linux/i2c.h>
Magnus Dammad337072012-03-30 17:44:02 +090030#include <linux/of_i2c.h>
Magnus Dammda672772008-04-22 22:16:49 +020031#include <linux/err.h>
Magnus Dammf1a3b992009-08-14 10:48:59 +000032#include <linux/pm_runtime.h>
Magnus Dammda672772008-04-22 22:16:49 +020033#include <linux/clk.h>
34#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Magnus Damm81f81152011-04-28 13:25:36 +090036#include <linux/i2c/i2c-sh_mobile.h>
Magnus Dammda672772008-04-22 22:16:49 +020037
Magnus Damm4eb00c92008-08-27 18:33:56 +090038/* Transmit operation: */
39/* */
40/* 0 byte transmit */
41/* BUS: S A8 ACK P */
42/* IRQ: DTE WAIT */
43/* ICIC: */
44/* ICCR: 0x94 0x90 */
45/* ICDR: A8 */
46/* */
47/* 1 byte transmit */
48/* BUS: S A8 ACK D8(1) ACK P */
49/* IRQ: DTE WAIT WAIT */
50/* ICIC: -DTE */
51/* ICCR: 0x94 0x90 */
52/* ICDR: A8 D8(1) */
53/* */
54/* 2 byte transmit */
55/* BUS: S A8 ACK D8(1) ACK D8(2) ACK P */
56/* IRQ: DTE WAIT WAIT WAIT */
57/* ICIC: -DTE */
58/* ICCR: 0x94 0x90 */
59/* ICDR: A8 D8(1) D8(2) */
60/* */
61/* 3 bytes or more, +---------+ gets repeated */
62/* */
63/* */
64/* Receive operation: */
65/* */
66/* 0 byte receive - not supported since slave may hold SDA low */
67/* */
68/* 1 byte receive [TX] | [RX] */
69/* BUS: S A8 ACK | D8(1) ACK P */
70/* IRQ: DTE WAIT | WAIT DTE */
71/* ICIC: -DTE | +DTE */
72/* ICCR: 0x94 0x81 | 0xc0 */
73/* ICDR: A8 | D8(1) */
74/* */
75/* 2 byte receive [TX]| [RX] */
76/* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P */
77/* IRQ: DTE WAIT | WAIT WAIT DTE */
78/* ICIC: -DTE | +DTE */
79/* ICCR: 0x94 0x81 | 0xc0 */
80/* ICDR: A8 | D8(1) D8(2) */
81/* */
82/* 3 byte receive [TX] | [RX] */
83/* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */
84/* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */
85/* ICIC: -DTE | +DTE */
86/* ICCR: 0x94 0x81 | 0xc0 */
87/* ICDR: A8 | D8(1) D8(2) D8(3) */
88/* */
89/* 4 bytes or more, this part is repeated +---------+ */
90/* */
91/* */
92/* Interrupt order and BUSY flag */
93/* ___ _ */
94/* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */
95/* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */
96/* */
97/* S D7 D6 D5 D4 D3 D2 D1 D0 P */
98/* ___ */
99/* WAIT IRQ ________________________________/ \___________ */
100/* TACK IRQ ____________________________________/ \_______ */
101/* DTE IRQ __________________________________________/ \_ */
102/* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
103/* _______________________________________________ */
104/* BUSY __/ \_ */
105/* */
106
Magnus Dammda672772008-04-22 22:16:49 +0200107enum sh_mobile_i2c_op {
108 OP_START = 0,
Magnus Damm4eb00c92008-08-27 18:33:56 +0900109 OP_TX_FIRST,
110 OP_TX,
Magnus Dammda672772008-04-22 22:16:49 +0200111 OP_TX_STOP,
112 OP_TX_TO_RX,
Magnus Damm4eb00c92008-08-27 18:33:56 +0900113 OP_RX,
Magnus Dammda672772008-04-22 22:16:49 +0200114 OP_RX_STOP,
Magnus Damm4eb00c92008-08-27 18:33:56 +0900115 OP_RX_STOP_DATA,
Magnus Dammda672772008-04-22 22:16:49 +0200116};
117
118struct sh_mobile_i2c_data {
119 struct device *dev;
120 void __iomem *reg;
121 struct i2c_adapter adap;
Magnus Damm81f81152011-04-28 13:25:36 +0900122 unsigned long bus_speed;
Magnus Dammda672772008-04-22 22:16:49 +0200123 struct clk *clk;
Magnus Damm962b6032010-03-11 10:06:02 +0000124 u_int8_t icic;
Magnus Dammda672772008-04-22 22:16:49 +0200125 u_int8_t iccl;
126 u_int8_t icch;
Magnus Damm962b6032010-03-11 10:06:02 +0000127 u_int8_t flags;
Magnus Dammda672772008-04-22 22:16:49 +0200128
129 spinlock_t lock;
130 wait_queue_head_t wait;
131 struct i2c_msg *msg;
132 int pos;
133 int sr;
134};
135
Magnus Damm962b6032010-03-11 10:06:02 +0000136#define IIC_FLAG_HAS_ICIC67 (1 << 0)
137
Magnus Dammda672772008-04-22 22:16:49 +0200138#define NORMAL_SPEED 100000 /* FAST_SPEED 400000 */
139
140/* Register offsets */
Magnus Damm12a55f22010-03-11 10:05:50 +0000141#define ICDR 0x00
142#define ICCR 0x04
143#define ICSR 0x08
144#define ICIC 0x0c
145#define ICCL 0x10
146#define ICCH 0x14
Magnus Dammda672772008-04-22 22:16:49 +0200147
148/* Register bits */
149#define ICCR_ICE 0x80
150#define ICCR_RACK 0x40
151#define ICCR_TRS 0x10
152#define ICCR_BBSY 0x04
153#define ICCR_SCP 0x01
154
155#define ICSR_SCLM 0x80
156#define ICSR_SDAM 0x40
157#define SW_DONE 0x20
158#define ICSR_BUSY 0x10
159#define ICSR_AL 0x08
160#define ICSR_TACK 0x04
161#define ICSR_WAIT 0x02
162#define ICSR_DTE 0x01
163
Magnus Damm962b6032010-03-11 10:06:02 +0000164#define ICIC_ICCLB8 0x80
165#define ICIC_ICCHB8 0x40
Magnus Dammda672772008-04-22 22:16:49 +0200166#define ICIC_ALE 0x08
167#define ICIC_TACKE 0x04
168#define ICIC_WAITE 0x02
169#define ICIC_DTEE 0x01
170
Magnus Damm12a55f22010-03-11 10:05:50 +0000171static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
172{
Magnus Damm962b6032010-03-11 10:06:02 +0000173 if (offs == ICIC)
174 data |= pd->icic;
175
Magnus Damm12a55f22010-03-11 10:05:50 +0000176 iowrite8(data, pd->reg + offs);
177}
178
179static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
180{
181 return ioread8(pd->reg + offs);
182}
183
184static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
185 unsigned char set, unsigned char clr)
186{
187 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
188}
189
Shinya Kuribayashi7b0e6292012-10-24 19:56:51 +0900190static void sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
Magnus Dammda672772008-04-22 22:16:49 +0200191{
Magnus Damma5616bd2008-10-31 20:20:55 +0900192 unsigned long i2c_clk;
193 u_int32_t num;
194 u_int32_t denom;
195 u_int32_t tmp;
196
Magnus Damma5616bd2008-10-31 20:20:55 +0900197 /* Get clock rate after clock is enabled */
Shinya Kuribayashi7b0e6292012-10-24 19:56:51 +0900198 clk_enable(pd->clk);
Magnus Damma5616bd2008-10-31 20:20:55 +0900199 i2c_clk = clk_get_rate(pd->clk);
200
201 /* Calculate the value for iccl. From the data sheet:
202 * iccl = (p clock / transfer rate) * (L / (L + H))
203 * where L and H are the SCL low/high ratio (5/4 in this case).
204 * We also round off the result.
205 */
206 num = i2c_clk * 5;
Magnus Damm81f81152011-04-28 13:25:36 +0900207 denom = pd->bus_speed * 9;
Magnus Damma5616bd2008-10-31 20:20:55 +0900208 tmp = num * 10 / denom;
209 if (tmp % 10 >= 5)
210 pd->iccl = (u_int8_t)((num/denom) + 1);
211 else
212 pd->iccl = (u_int8_t)(num/denom);
213
Magnus Damm962b6032010-03-11 10:06:02 +0000214 /* one more bit of ICCL in ICIC */
215 if (pd->flags & IIC_FLAG_HAS_ICIC67) {
216 if ((num/denom) > 0xff)
217 pd->icic |= ICIC_ICCLB8;
218 else
219 pd->icic &= ~ICIC_ICCLB8;
220 }
221
Magnus Damma5616bd2008-10-31 20:20:55 +0900222 /* Calculate the value for icch. From the data sheet:
223 icch = (p clock / transfer rate) * (H / (L + H)) */
224 num = i2c_clk * 4;
225 tmp = num * 10 / denom;
226 if (tmp % 10 >= 5)
227 pd->icch = (u_int8_t)((num/denom) + 1);
228 else
229 pd->icch = (u_int8_t)(num/denom);
230
Magnus Damm962b6032010-03-11 10:06:02 +0000231 /* one more bit of ICCH in ICIC */
232 if (pd->flags & IIC_FLAG_HAS_ICIC67) {
233 if ((num/denom) > 0xff)
234 pd->icic |= ICIC_ICCHB8;
235 else
236 pd->icic &= ~ICIC_ICCHB8;
237 }
238
Shinya Kuribayashi7b0e6292012-10-24 19:56:51 +0900239 clk_disable(pd->clk);
240}
241
242static void activate_ch(struct sh_mobile_i2c_data *pd)
243{
244 /* Wake up device and enable clock */
245 pm_runtime_get_sync(pd->dev);
246 clk_enable(pd->clk);
247
Magnus Dammda672772008-04-22 22:16:49 +0200248 /* Enable channel and configure rx ack */
Magnus Damm12a55f22010-03-11 10:05:50 +0000249 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200250
251 /* Mask all interrupts */
Magnus Damm12a55f22010-03-11 10:05:50 +0000252 iic_wr(pd, ICIC, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200253
254 /* Set the clock */
Magnus Damm12a55f22010-03-11 10:05:50 +0000255 iic_wr(pd, ICCL, pd->iccl);
256 iic_wr(pd, ICCH, pd->icch);
Magnus Dammda672772008-04-22 22:16:49 +0200257}
258
259static void deactivate_ch(struct sh_mobile_i2c_data *pd)
260{
261 /* Clear/disable interrupts */
Magnus Damm12a55f22010-03-11 10:05:50 +0000262 iic_wr(pd, ICSR, 0);
263 iic_wr(pd, ICIC, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200264
265 /* Disable channel */
Magnus Damm12a55f22010-03-11 10:05:50 +0000266 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
Magnus Dammda672772008-04-22 22:16:49 +0200267
Magnus Dammf1a3b992009-08-14 10:48:59 +0000268 /* Disable clock and mark device as idle */
Magnus Dammda672772008-04-22 22:16:49 +0200269 clk_disable(pd->clk);
Magnus Dammf1a3b992009-08-14 10:48:59 +0000270 pm_runtime_put_sync(pd->dev);
Magnus Dammda672772008-04-22 22:16:49 +0200271}
272
273static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
274 enum sh_mobile_i2c_op op, unsigned char data)
275{
276 unsigned char ret = 0;
277 unsigned long flags;
278
279 dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
280
281 spin_lock_irqsave(&pd->lock, flags);
282
283 switch (op) {
Magnus Damm4eb00c92008-08-27 18:33:56 +0900284 case OP_START: /* issue start and trigger DTE interrupt */
Magnus Damm12a55f22010-03-11 10:05:50 +0000285 iic_wr(pd, ICCR, 0x94);
Magnus Dammda672772008-04-22 22:16:49 +0200286 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900287 case OP_TX_FIRST: /* disable DTE interrupt and write data */
Magnus Damm12a55f22010-03-11 10:05:50 +0000288 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
289 iic_wr(pd, ICDR, data);
Magnus Dammda672772008-04-22 22:16:49 +0200290 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900291 case OP_TX: /* write data */
Magnus Damm12a55f22010-03-11 10:05:50 +0000292 iic_wr(pd, ICDR, data);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900293 break;
294 case OP_TX_STOP: /* write data and issue a stop afterwards */
Magnus Damm12a55f22010-03-11 10:05:50 +0000295 iic_wr(pd, ICDR, data);
296 iic_wr(pd, ICCR, 0x90);
Magnus Dammda672772008-04-22 22:16:49 +0200297 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900298 case OP_TX_TO_RX: /* select read mode */
Magnus Damm12a55f22010-03-11 10:05:50 +0000299 iic_wr(pd, ICCR, 0x81);
Magnus Dammda672772008-04-22 22:16:49 +0200300 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900301 case OP_RX: /* just read data */
Magnus Damm12a55f22010-03-11 10:05:50 +0000302 ret = iic_rd(pd, ICDR);
Magnus Dammda672772008-04-22 22:16:49 +0200303 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900304 case OP_RX_STOP: /* enable DTE interrupt, issue stop */
Magnus Damm12a55f22010-03-11 10:05:50 +0000305 iic_wr(pd, ICIC,
306 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
307 iic_wr(pd, ICCR, 0xc0);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900308 break;
309 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
Magnus Damm12a55f22010-03-11 10:05:50 +0000310 iic_wr(pd, ICIC,
311 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
312 ret = iic_rd(pd, ICDR);
313 iic_wr(pd, ICCR, 0xc0);
Magnus Dammda672772008-04-22 22:16:49 +0200314 break;
315 }
316
317 spin_unlock_irqrestore(&pd->lock, flags);
318
319 dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
320 return ret;
321}
322
Magnus Damm4eb00c92008-08-27 18:33:56 +0900323static int sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
324{
325 if (pd->pos == -1)
326 return 1;
327
328 return 0;
329}
330
331static int sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
332{
333 if (pd->pos == (pd->msg->len - 1))
334 return 1;
335
336 return 0;
337}
338
339static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
340 unsigned char *buf)
341{
342 switch (pd->pos) {
343 case -1:
344 *buf = (pd->msg->addr & 0x7f) << 1;
345 *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
346 break;
347 default:
348 *buf = pd->msg->buf[pd->pos];
349 }
350}
351
352static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
353{
354 unsigned char data;
355
356 if (pd->pos == pd->msg->len)
357 return 1;
358
359 sh_mobile_i2c_get_data(pd, &data);
360
361 if (sh_mobile_i2c_is_last_byte(pd))
362 i2c_op(pd, OP_TX_STOP, data);
363 else if (sh_mobile_i2c_is_first_byte(pd))
364 i2c_op(pd, OP_TX_FIRST, data);
365 else
366 i2c_op(pd, OP_TX, data);
367
368 pd->pos++;
369 return 0;
370}
371
372static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
373{
374 unsigned char data;
375 int real_pos;
376
377 do {
378 if (pd->pos <= -1) {
379 sh_mobile_i2c_get_data(pd, &data);
380
381 if (sh_mobile_i2c_is_first_byte(pd))
382 i2c_op(pd, OP_TX_FIRST, data);
383 else
384 i2c_op(pd, OP_TX, data);
385 break;
386 }
387
388 if (pd->pos == 0) {
389 i2c_op(pd, OP_TX_TO_RX, 0);
390 break;
391 }
392
393 real_pos = pd->pos - 2;
394
395 if (pd->pos == pd->msg->len) {
396 if (real_pos < 0) {
397 i2c_op(pd, OP_RX_STOP, 0);
398 break;
399 }
400 data = i2c_op(pd, OP_RX_STOP_DATA, 0);
401 } else
402 data = i2c_op(pd, OP_RX, 0);
403
Magnus Dammbff4056c82008-11-13 15:28:21 +0900404 if (real_pos >= 0)
405 pd->msg->buf[real_pos] = data;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900406 } while (0);
407
408 pd->pos++;
409 return pd->pos == (pd->msg->len + 2);
410}
411
Magnus Dammda672772008-04-22 22:16:49 +0200412static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
413{
414 struct platform_device *dev = dev_id;
415 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900416 unsigned char sr;
417 int wakeup;
Magnus Dammda672772008-04-22 22:16:49 +0200418
Magnus Damm12a55f22010-03-11 10:05:50 +0000419 sr = iic_rd(pd, ICSR);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900420 pd->sr |= sr; /* remember state */
Magnus Dammda672772008-04-22 22:16:49 +0200421
422 dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
Magnus Damm4eb00c92008-08-27 18:33:56 +0900423 (pd->msg->flags & I2C_M_RD) ? "read" : "write",
424 pd->pos, pd->msg->len);
Magnus Dammda672772008-04-22 22:16:49 +0200425
426 if (sr & (ICSR_AL | ICSR_TACK)) {
Magnus Damm4eb00c92008-08-27 18:33:56 +0900427 /* don't interrupt transaction - continue to issue stop */
Magnus Damm12a55f22010-03-11 10:05:50 +0000428 iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
Magnus Damm4eb00c92008-08-27 18:33:56 +0900429 wakeup = 0;
430 } else if (pd->msg->flags & I2C_M_RD)
431 wakeup = sh_mobile_i2c_isr_rx(pd);
432 else
433 wakeup = sh_mobile_i2c_isr_tx(pd);
Magnus Dammda672772008-04-22 22:16:49 +0200434
Magnus Damm4eb00c92008-08-27 18:33:56 +0900435 if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
Magnus Damm12a55f22010-03-11 10:05:50 +0000436 iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
Magnus Dammda672772008-04-22 22:16:49 +0200437
Magnus Dammda672772008-04-22 22:16:49 +0200438 if (wakeup) {
439 pd->sr |= SW_DONE;
440 wake_up(&pd->wait);
441 }
442
443 return IRQ_HANDLED;
444}
445
446static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg)
447{
Magnus Damm4eb00c92008-08-27 18:33:56 +0900448 if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
449 dev_err(pd->dev, "Unsupported zero length i2c read\n");
450 return -EIO;
451 }
452
Magnus Dammda672772008-04-22 22:16:49 +0200453 /* Initialize channel registers */
Magnus Damm12a55f22010-03-11 10:05:50 +0000454 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
Magnus Dammda672772008-04-22 22:16:49 +0200455
456 /* Enable channel and configure rx ack */
Magnus Damm12a55f22010-03-11 10:05:50 +0000457 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200458
459 /* Set the clock */
Magnus Damm12a55f22010-03-11 10:05:50 +0000460 iic_wr(pd, ICCL, pd->iccl);
461 iic_wr(pd, ICCH, pd->icch);
Magnus Dammda672772008-04-22 22:16:49 +0200462
463 pd->msg = usr_msg;
464 pd->pos = -1;
465 pd->sr = 0;
466
Magnus Damm4eb00c92008-08-27 18:33:56 +0900467 /* Enable all interrupts to begin with */
Magnus Damm12a55f22010-03-11 10:05:50 +0000468 iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
Magnus Dammda672772008-04-22 22:16:49 +0200469 return 0;
470}
471
472static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
473 struct i2c_msg *msgs,
474 int num)
475{
476 struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
477 struct i2c_msg *msg;
478 int err = 0;
479 u_int8_t val;
480 int i, k, retry_count;
481
482 activate_ch(pd);
483
484 /* Process all messages */
485 for (i = 0; i < num; i++) {
486 msg = &msgs[i];
487
488 err = start_ch(pd, msg);
489 if (err)
490 break;
491
492 i2c_op(pd, OP_START, 0);
493
494 /* The interrupt handler takes care of the rest... */
495 k = wait_event_timeout(pd->wait,
496 pd->sr & (ICSR_TACK | SW_DONE),
497 5 * HZ);
498 if (!k)
499 dev_err(pd->dev, "Transfer request timed out\n");
500
Magnus Damm4eb00c92008-08-27 18:33:56 +0900501 retry_count = 1000;
Magnus Dammda672772008-04-22 22:16:49 +0200502again:
Magnus Damm12a55f22010-03-11 10:05:50 +0000503 val = iic_rd(pd, ICSR);
Magnus Dammda672772008-04-22 22:16:49 +0200504
505 dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
506
Magnus Dammda672772008-04-22 22:16:49 +0200507 /* the interrupt handler may wake us up before the
508 * transfer is finished, so poll the hardware
509 * until we're done.
510 */
Magnus Damm4eb00c92008-08-27 18:33:56 +0900511 if (val & ICSR_BUSY) {
512 udelay(10);
Magnus Dammda672772008-04-22 22:16:49 +0200513 if (retry_count--)
514 goto again;
515
516 err = -EIO;
517 dev_err(pd->dev, "Polling timed out\n");
518 break;
519 }
Magnus Damm4eb00c92008-08-27 18:33:56 +0900520
521 /* handle missing acknowledge and arbitration lost */
522 if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) {
523 err = -EIO;
524 break;
525 }
Magnus Dammda672772008-04-22 22:16:49 +0200526 }
527
528 deactivate_ch(pd);
529
530 if (!err)
531 err = num;
532 return err;
533}
534
535static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
536{
537 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
538}
539
540static struct i2c_algorithm sh_mobile_i2c_algorithm = {
541 .functionality = sh_mobile_i2c_func,
542 .master_xfer = sh_mobile_i2c_xfer,
543};
544
Magnus Dammda672772008-04-22 22:16:49 +0200545static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, int hook)
546{
547 struct resource *res;
548 int ret = -ENXIO;
Magnus Damm82b20d82010-08-02 07:16:37 +0000549 int n, k = 0;
Magnus Dammda672772008-04-22 22:16:49 +0200550
551 while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
552 for (n = res->start; hook && n <= res->end; n++) {
Yong Zhang43110512011-09-21 17:28:33 +0800553 if (request_irq(n, sh_mobile_i2c_isr, 0,
Magnus Damm82b20d82010-08-02 07:16:37 +0000554 dev_name(&dev->dev), dev)) {
555 for (n--; n >= res->start; n--)
556 free_irq(n, dev);
557
Magnus Dammda672772008-04-22 22:16:49 +0200558 goto rollback;
Magnus Damm82b20d82010-08-02 07:16:37 +0000559 }
Magnus Dammda672772008-04-22 22:16:49 +0200560 }
561 k++;
562 }
563
564 if (hook)
565 return k > 0 ? 0 : -ENOENT;
566
Magnus Dammda672772008-04-22 22:16:49 +0200567 ret = 0;
568
569 rollback:
Magnus Damm82b20d82010-08-02 07:16:37 +0000570 k--;
Magnus Dammda672772008-04-22 22:16:49 +0200571
Magnus Damm82b20d82010-08-02 07:16:37 +0000572 while (k >= 0) {
573 res = platform_get_resource(dev, IORESOURCE_IRQ, k);
574 for (n = res->start; n <= res->end; n++)
575 free_irq(n, dev);
576
577 k--;
Magnus Dammda672772008-04-22 22:16:49 +0200578 }
579
580 return ret;
581}
582
583static int sh_mobile_i2c_probe(struct platform_device *dev)
584{
Magnus Damm81f81152011-04-28 13:25:36 +0900585 struct i2c_sh_mobile_platform_data *pdata = dev->dev.platform_data;
Magnus Dammda672772008-04-22 22:16:49 +0200586 struct sh_mobile_i2c_data *pd;
587 struct i2c_adapter *adap;
588 struct resource *res;
589 int size;
590 int ret;
591
592 pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
593 if (pd == NULL) {
594 dev_err(&dev->dev, "cannot allocate private data\n");
595 return -ENOMEM;
596 }
597
Magnus Damm1082d5d2011-04-21 22:20:39 +0900598 pd->clk = clk_get(&dev->dev, NULL);
Magnus Dammda672772008-04-22 22:16:49 +0200599 if (IS_ERR(pd->clk)) {
Magnus Damm1082d5d2011-04-21 22:20:39 +0900600 dev_err(&dev->dev, "cannot get clock\n");
Magnus Dammda672772008-04-22 22:16:49 +0200601 ret = PTR_ERR(pd->clk);
602 goto err;
603 }
604
605 ret = sh_mobile_i2c_hook_irqs(dev, 1);
606 if (ret) {
607 dev_err(&dev->dev, "cannot request IRQ\n");
608 goto err_clk;
609 }
610
611 pd->dev = &dev->dev;
612 platform_set_drvdata(dev, pd);
613
614 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
615 if (res == NULL) {
616 dev_err(&dev->dev, "cannot find IO resource\n");
617 ret = -ENOENT;
618 goto err_irq;
619 }
620
Julia Lawall59330822009-07-05 08:37:50 +0200621 size = resource_size(res);
Magnus Dammda672772008-04-22 22:16:49 +0200622
623 pd->reg = ioremap(res->start, size);
624 if (pd->reg == NULL) {
625 dev_err(&dev->dev, "cannot map IO\n");
626 ret = -ENXIO;
627 goto err_irq;
628 }
629
Magnus Damm81f81152011-04-28 13:25:36 +0900630 /* Use platformd data bus speed or NORMAL_SPEED */
631 pd->bus_speed = NORMAL_SPEED;
632 if (pdata && pdata->bus_speed)
633 pd->bus_speed = pdata->bus_speed;
634
Magnus Damm962b6032010-03-11 10:06:02 +0000635 /* The IIC blocks on SH-Mobile ARM processors
636 * come with two new bits in ICIC.
637 */
638 if (size > 0x17)
639 pd->flags |= IIC_FLAG_HAS_ICIC67;
640
Shinya Kuribayashi7b0e6292012-10-24 19:56:51 +0900641 sh_mobile_i2c_init(pd);
642
Magnus Dammf1a3b992009-08-14 10:48:59 +0000643 /* Enable Runtime PM for this device.
644 *
645 * Also tell the Runtime PM core to ignore children
646 * for this device since it is valid for us to suspend
647 * this I2C master driver even though the slave devices
648 * on the I2C bus may not be suspended.
649 *
650 * The state of the I2C hardware bus is unaffected by
651 * the Runtime PM state.
652 */
653 pm_suspend_ignore_children(&dev->dev, true);
654 pm_runtime_enable(&dev->dev);
655
Magnus Dammda672772008-04-22 22:16:49 +0200656 /* setup the private data */
657 adap = &pd->adap;
658 i2c_set_adapdata(adap, pd);
659
660 adap->owner = THIS_MODULE;
661 adap->algo = &sh_mobile_i2c_algorithm;
662 adap->dev.parent = &dev->dev;
663 adap->retries = 5;
664 adap->nr = dev->id;
Magnus Dammad337072012-03-30 17:44:02 +0900665 adap->dev.of_node = dev->dev.of_node;
Magnus Dammda672772008-04-22 22:16:49 +0200666
667 strlcpy(adap->name, dev->name, sizeof(adap->name));
668
Magnus Damma5616bd2008-10-31 20:20:55 +0900669 spin_lock_init(&pd->lock);
670 init_waitqueue_head(&pd->wait);
Magnus Dammda672772008-04-22 22:16:49 +0200671
672 ret = i2c_add_numbered_adapter(adap);
673 if (ret < 0) {
674 dev_err(&dev->dev, "cannot add numbered adapter\n");
675 goto err_all;
676 }
677
Magnus Damm81f81152011-04-28 13:25:36 +0900678 dev_info(&dev->dev, "I2C adapter %d with bus speed %lu Hz\n",
679 adap->nr, pd->bus_speed);
Magnus Dammad337072012-03-30 17:44:02 +0900680
681 of_i2c_register_devices(adap);
Magnus Dammda672772008-04-22 22:16:49 +0200682 return 0;
683
684 err_all:
685 iounmap(pd->reg);
686 err_irq:
687 sh_mobile_i2c_hook_irqs(dev, 0);
688 err_clk:
689 clk_put(pd->clk);
690 err:
691 kfree(pd);
692 return ret;
693}
694
695static int sh_mobile_i2c_remove(struct platform_device *dev)
696{
697 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
698
699 i2c_del_adapter(&pd->adap);
700 iounmap(pd->reg);
701 sh_mobile_i2c_hook_irqs(dev, 0);
702 clk_put(pd->clk);
Magnus Dammf1a3b992009-08-14 10:48:59 +0000703 pm_runtime_disable(&dev->dev);
Magnus Dammda672772008-04-22 22:16:49 +0200704 kfree(pd);
705 return 0;
706}
707
Magnus Dammf1a3b992009-08-14 10:48:59 +0000708static int sh_mobile_i2c_runtime_nop(struct device *dev)
709{
710 /* Runtime PM callback shared between ->runtime_suspend()
711 * and ->runtime_resume(). Simply returns success.
712 *
713 * This driver re-initializes all registers after
714 * pm_runtime_get_sync() anyway so there is no need
715 * to save and restore registers here.
716 */
717 return 0;
718}
719
Alexey Dobriyan47145212009-12-14 18:00:08 -0800720static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
Magnus Dammf1a3b992009-08-14 10:48:59 +0000721 .runtime_suspend = sh_mobile_i2c_runtime_nop,
722 .runtime_resume = sh_mobile_i2c_runtime_nop,
723};
724
Magnus Dammad337072012-03-30 17:44:02 +0900725static const struct of_device_id sh_mobile_i2c_dt_ids[] __devinitconst = {
726 { .compatible = "renesas,rmobile-iic", },
727 {},
728};
729MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
730
Magnus Dammda672772008-04-22 22:16:49 +0200731static struct platform_driver sh_mobile_i2c_driver = {
732 .driver = {
733 .name = "i2c-sh_mobile",
734 .owner = THIS_MODULE,
Magnus Dammf1a3b992009-08-14 10:48:59 +0000735 .pm = &sh_mobile_i2c_dev_pm_ops,
Magnus Dammad337072012-03-30 17:44:02 +0900736 .of_match_table = sh_mobile_i2c_dt_ids,
Magnus Dammda672772008-04-22 22:16:49 +0200737 },
738 .probe = sh_mobile_i2c_probe,
739 .remove = sh_mobile_i2c_remove,
740};
741
742static int __init sh_mobile_i2c_adap_init(void)
743{
744 return platform_driver_register(&sh_mobile_i2c_driver);
745}
746
747static void __exit sh_mobile_i2c_adap_exit(void)
748{
749 platform_driver_unregister(&sh_mobile_i2c_driver);
750}
751
Magnus Dammccb3bc12009-07-22 23:58:39 +0900752subsys_initcall(sh_mobile_i2c_adap_init);
Magnus Dammda672772008-04-22 22:16:49 +0200753module_exit(sh_mobile_i2c_adap_exit);
754
755MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
756MODULE_AUTHOR("Magnus Damm");
757MODULE_LICENSE("GPL v2");
Guennadi Liakhovetski7ef0c122011-04-15 20:18:57 +0200758MODULE_ALIAS("platform:i2c-sh_mobile");