blob: 4dc0cc3611c2b3f1a3913a9d1f9955fab3b335f1 [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 Damm962b6032010-03-11 10:06:02 +0000125 u_int8_t flags;
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900126 u_int16_t iccl;
127 u_int16_t icch;
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
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900138#define STANDARD_MODE 100000
139#define FAST_MODE 400000
Magnus Dammda672772008-04-22 22:16:49 +0200140
141/* Register offsets */
Magnus Damm12a55f22010-03-11 10:05:50 +0000142#define ICDR 0x00
143#define ICCR 0x04
144#define ICSR 0x08
145#define ICIC 0x0c
146#define ICCL 0x10
147#define ICCH 0x14
Magnus Dammda672772008-04-22 22:16:49 +0200148
149/* Register bits */
150#define ICCR_ICE 0x80
151#define ICCR_RACK 0x40
152#define ICCR_TRS 0x10
153#define ICCR_BBSY 0x04
154#define ICCR_SCP 0x01
155
156#define ICSR_SCLM 0x80
157#define ICSR_SDAM 0x40
158#define SW_DONE 0x20
159#define ICSR_BUSY 0x10
160#define ICSR_AL 0x08
161#define ICSR_TACK 0x04
162#define ICSR_WAIT 0x02
163#define ICSR_DTE 0x01
164
Magnus Damm962b6032010-03-11 10:06:02 +0000165#define ICIC_ICCLB8 0x80
166#define ICIC_ICCHB8 0x40
Magnus Dammda672772008-04-22 22:16:49 +0200167#define ICIC_ALE 0x08
168#define ICIC_TACKE 0x04
169#define ICIC_WAITE 0x02
170#define ICIC_DTEE 0x01
171
Magnus Damm12a55f22010-03-11 10:05:50 +0000172static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
173{
Magnus Damm962b6032010-03-11 10:06:02 +0000174 if (offs == ICIC)
175 data |= pd->icic;
176
Magnus Damm12a55f22010-03-11 10:05:50 +0000177 iowrite8(data, pd->reg + offs);
178}
179
180static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
181{
182 return ioread8(pd->reg + offs);
183}
184
185static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
186 unsigned char set, unsigned char clr)
187{
188 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
189}
190
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900191static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf, int offset)
192{
193 /*
194 * Conditional expression:
195 * ICCL >= COUNT_CLK * (tLOW + tf)
196 *
197 * SH-Mobile IIC hardware starts counting the LOW period of
198 * the SCL signal (tLOW) as soon as it pulls the SCL line.
199 * In order to meet the tLOW timing spec, we need to take into
200 * account the fall time of SCL signal (tf). Default tf value
201 * should be 0.3 us, for safety.
202 */
203 return (((count_khz * (tLOW + tf)) + 5000) / 10000) + offset;
204}
205
206static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf, int offset)
207{
208 /*
209 * Conditional expression:
210 * ICCH >= COUNT_CLK * (tHIGH + tf)
211 *
212 * SH-Mobile IIC hardware is aware of SCL transition period 'tr',
213 * and can ignore it. SH-Mobile IIC controller starts counting
214 * the HIGH period of the SCL signal (tHIGH) after the SCL input
215 * voltage increases at VIH.
216 *
217 * Afterward it turned out calculating ICCH using only tHIGH spec
218 * will result in violation of the tHD;STA timing spec. We need
219 * to take into account the fall time of SDA signal (tf) at START
220 * condition, in order to meet both tHIGH and tHD;STA specs.
221 */
222 return (((count_khz * (tHIGH + tf)) + 5000) / 10000) + offset;
223}
224
Shinya Kuribayashi7b0e6292012-10-24 19:56:51 +0900225static void sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
Magnus Dammda672772008-04-22 22:16:49 +0200226{
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900227 unsigned long i2c_clk_khz;
228 u32 tHIGH, tLOW, tf;
229 int offset;
Magnus Damma5616bd2008-10-31 20:20:55 +0900230
Magnus Damma5616bd2008-10-31 20:20:55 +0900231 /* Get clock rate after clock is enabled */
Shinya Kuribayashi7b0e6292012-10-24 19:56:51 +0900232 clk_enable(pd->clk);
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900233 i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
Magnus Damma5616bd2008-10-31 20:20:55 +0900234
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900235 if (pd->bus_speed == STANDARD_MODE) {
236 tLOW = 47; /* tLOW = 4.7 us */
237 tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */
238 tf = 3; /* tf = 0.3 us */
239 offset = 0; /* No offset */
240 } else if (pd->bus_speed == FAST_MODE) {
241 tLOW = 13; /* tLOW = 1.3 us */
242 tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */
243 tf = 3; /* tf = 0.3 us */
244 offset = 0; /* No offset */
245 } else {
246 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n",
247 pd->bus_speed);
248 goto out;
249 }
Magnus Damma5616bd2008-10-31 20:20:55 +0900250
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900251 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf, offset);
Magnus Damm962b6032010-03-11 10:06:02 +0000252 /* one more bit of ICCL in ICIC */
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900253 if ((pd->iccl > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67))
254 pd->icic |= ICIC_ICCLB8;
Magnus Damma5616bd2008-10-31 20:20:55 +0900255 else
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900256 pd->icic &= ~ICIC_ICCLB8;
Magnus Damma5616bd2008-10-31 20:20:55 +0900257
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900258 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf, offset);
Magnus Damm962b6032010-03-11 10:06:02 +0000259 /* one more bit of ICCH in ICIC */
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900260 if ((pd->icch > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67))
261 pd->icic |= ICIC_ICCHB8;
262 else
263 pd->icic &= ~ICIC_ICCHB8;
Magnus Damm962b6032010-03-11 10:06:02 +0000264
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900265out:
Shinya Kuribayashi7b0e6292012-10-24 19:56:51 +0900266 clk_disable(pd->clk);
267}
268
269static void activate_ch(struct sh_mobile_i2c_data *pd)
270{
271 /* Wake up device and enable clock */
272 pm_runtime_get_sync(pd->dev);
273 clk_enable(pd->clk);
274
Magnus Dammda672772008-04-22 22:16:49 +0200275 /* Enable channel and configure rx ack */
Magnus Damm12a55f22010-03-11 10:05:50 +0000276 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200277
278 /* Mask all interrupts */
Magnus Damm12a55f22010-03-11 10:05:50 +0000279 iic_wr(pd, ICIC, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200280
281 /* Set the clock */
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900282 iic_wr(pd, ICCL, pd->iccl & 0xff);
283 iic_wr(pd, ICCH, pd->icch & 0xff);
Magnus Dammda672772008-04-22 22:16:49 +0200284}
285
286static void deactivate_ch(struct sh_mobile_i2c_data *pd)
287{
288 /* Clear/disable interrupts */
Magnus Damm12a55f22010-03-11 10:05:50 +0000289 iic_wr(pd, ICSR, 0);
290 iic_wr(pd, ICIC, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200291
292 /* Disable channel */
Magnus Damm12a55f22010-03-11 10:05:50 +0000293 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
Magnus Dammda672772008-04-22 22:16:49 +0200294
Magnus Dammf1a3b992009-08-14 10:48:59 +0000295 /* Disable clock and mark device as idle */
Magnus Dammda672772008-04-22 22:16:49 +0200296 clk_disable(pd->clk);
Magnus Dammf1a3b992009-08-14 10:48:59 +0000297 pm_runtime_put_sync(pd->dev);
Magnus Dammda672772008-04-22 22:16:49 +0200298}
299
300static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
301 enum sh_mobile_i2c_op op, unsigned char data)
302{
303 unsigned char ret = 0;
304 unsigned long flags;
305
306 dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
307
308 spin_lock_irqsave(&pd->lock, flags);
309
310 switch (op) {
Magnus Damm4eb00c92008-08-27 18:33:56 +0900311 case OP_START: /* issue start and trigger DTE interrupt */
Magnus Damm12a55f22010-03-11 10:05:50 +0000312 iic_wr(pd, ICCR, 0x94);
Magnus Dammda672772008-04-22 22:16:49 +0200313 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900314 case OP_TX_FIRST: /* disable DTE interrupt and write data */
Magnus Damm12a55f22010-03-11 10:05:50 +0000315 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
316 iic_wr(pd, ICDR, data);
Magnus Dammda672772008-04-22 22:16:49 +0200317 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900318 case OP_TX: /* write data */
Magnus Damm12a55f22010-03-11 10:05:50 +0000319 iic_wr(pd, ICDR, data);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900320 break;
321 case OP_TX_STOP: /* write data and issue a stop afterwards */
Magnus Damm12a55f22010-03-11 10:05:50 +0000322 iic_wr(pd, ICDR, data);
323 iic_wr(pd, ICCR, 0x90);
Magnus Dammda672772008-04-22 22:16:49 +0200324 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900325 case OP_TX_TO_RX: /* select read mode */
Magnus Damm12a55f22010-03-11 10:05:50 +0000326 iic_wr(pd, ICCR, 0x81);
Magnus Dammda672772008-04-22 22:16:49 +0200327 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900328 case OP_RX: /* just read data */
Magnus Damm12a55f22010-03-11 10:05:50 +0000329 ret = iic_rd(pd, ICDR);
Magnus Dammda672772008-04-22 22:16:49 +0200330 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900331 case OP_RX_STOP: /* enable DTE interrupt, issue stop */
Magnus Damm12a55f22010-03-11 10:05:50 +0000332 iic_wr(pd, ICIC,
333 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
334 iic_wr(pd, ICCR, 0xc0);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900335 break;
336 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
Magnus Damm12a55f22010-03-11 10:05:50 +0000337 iic_wr(pd, ICIC,
338 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
339 ret = iic_rd(pd, ICDR);
340 iic_wr(pd, ICCR, 0xc0);
Magnus Dammda672772008-04-22 22:16:49 +0200341 break;
342 }
343
344 spin_unlock_irqrestore(&pd->lock, flags);
345
346 dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
347 return ret;
348}
349
Magnus Damm4eb00c92008-08-27 18:33:56 +0900350static int sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
351{
352 if (pd->pos == -1)
353 return 1;
354
355 return 0;
356}
357
358static int sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
359{
360 if (pd->pos == (pd->msg->len - 1))
361 return 1;
362
363 return 0;
364}
365
366static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
367 unsigned char *buf)
368{
369 switch (pd->pos) {
370 case -1:
371 *buf = (pd->msg->addr & 0x7f) << 1;
372 *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
373 break;
374 default:
375 *buf = pd->msg->buf[pd->pos];
376 }
377}
378
379static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
380{
381 unsigned char data;
382
383 if (pd->pos == pd->msg->len)
384 return 1;
385
386 sh_mobile_i2c_get_data(pd, &data);
387
388 if (sh_mobile_i2c_is_last_byte(pd))
389 i2c_op(pd, OP_TX_STOP, data);
390 else if (sh_mobile_i2c_is_first_byte(pd))
391 i2c_op(pd, OP_TX_FIRST, data);
392 else
393 i2c_op(pd, OP_TX, data);
394
395 pd->pos++;
396 return 0;
397}
398
399static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
400{
401 unsigned char data;
402 int real_pos;
403
404 do {
405 if (pd->pos <= -1) {
406 sh_mobile_i2c_get_data(pd, &data);
407
408 if (sh_mobile_i2c_is_first_byte(pd))
409 i2c_op(pd, OP_TX_FIRST, data);
410 else
411 i2c_op(pd, OP_TX, data);
412 break;
413 }
414
415 if (pd->pos == 0) {
416 i2c_op(pd, OP_TX_TO_RX, 0);
417 break;
418 }
419
420 real_pos = pd->pos - 2;
421
422 if (pd->pos == pd->msg->len) {
423 if (real_pos < 0) {
424 i2c_op(pd, OP_RX_STOP, 0);
425 break;
426 }
427 data = i2c_op(pd, OP_RX_STOP_DATA, 0);
428 } else
429 data = i2c_op(pd, OP_RX, 0);
430
Magnus Dammbff4056c82008-11-13 15:28:21 +0900431 if (real_pos >= 0)
432 pd->msg->buf[real_pos] = data;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900433 } while (0);
434
435 pd->pos++;
436 return pd->pos == (pd->msg->len + 2);
437}
438
Magnus Dammda672772008-04-22 22:16:49 +0200439static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
440{
441 struct platform_device *dev = dev_id;
442 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900443 unsigned char sr;
444 int wakeup;
Magnus Dammda672772008-04-22 22:16:49 +0200445
Magnus Damm12a55f22010-03-11 10:05:50 +0000446 sr = iic_rd(pd, ICSR);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900447 pd->sr |= sr; /* remember state */
Magnus Dammda672772008-04-22 22:16:49 +0200448
449 dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
Magnus Damm4eb00c92008-08-27 18:33:56 +0900450 (pd->msg->flags & I2C_M_RD) ? "read" : "write",
451 pd->pos, pd->msg->len);
Magnus Dammda672772008-04-22 22:16:49 +0200452
453 if (sr & (ICSR_AL | ICSR_TACK)) {
Magnus Damm4eb00c92008-08-27 18:33:56 +0900454 /* don't interrupt transaction - continue to issue stop */
Magnus Damm12a55f22010-03-11 10:05:50 +0000455 iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
Magnus Damm4eb00c92008-08-27 18:33:56 +0900456 wakeup = 0;
457 } else if (pd->msg->flags & I2C_M_RD)
458 wakeup = sh_mobile_i2c_isr_rx(pd);
459 else
460 wakeup = sh_mobile_i2c_isr_tx(pd);
Magnus Dammda672772008-04-22 22:16:49 +0200461
Magnus Damm4eb00c92008-08-27 18:33:56 +0900462 if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
Magnus Damm12a55f22010-03-11 10:05:50 +0000463 iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
Magnus Dammda672772008-04-22 22:16:49 +0200464
Magnus Dammda672772008-04-22 22:16:49 +0200465 if (wakeup) {
466 pd->sr |= SW_DONE;
467 wake_up(&pd->wait);
468 }
469
470 return IRQ_HANDLED;
471}
472
473static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg)
474{
Magnus Damm4eb00c92008-08-27 18:33:56 +0900475 if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
476 dev_err(pd->dev, "Unsupported zero length i2c read\n");
477 return -EIO;
478 }
479
Magnus Dammda672772008-04-22 22:16:49 +0200480 /* Initialize channel registers */
Magnus Damm12a55f22010-03-11 10:05:50 +0000481 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
Magnus Dammda672772008-04-22 22:16:49 +0200482
483 /* Enable channel and configure rx ack */
Magnus Damm12a55f22010-03-11 10:05:50 +0000484 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200485
486 /* Set the clock */
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900487 iic_wr(pd, ICCL, pd->iccl & 0xff);
488 iic_wr(pd, ICCH, pd->icch & 0xff);
Magnus Dammda672772008-04-22 22:16:49 +0200489
490 pd->msg = usr_msg;
491 pd->pos = -1;
492 pd->sr = 0;
493
Magnus Damm4eb00c92008-08-27 18:33:56 +0900494 /* Enable all interrupts to begin with */
Magnus Damm12a55f22010-03-11 10:05:50 +0000495 iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
Magnus Dammda672772008-04-22 22:16:49 +0200496 return 0;
497}
498
499static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
500 struct i2c_msg *msgs,
501 int num)
502{
503 struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
504 struct i2c_msg *msg;
505 int err = 0;
506 u_int8_t val;
507 int i, k, retry_count;
508
509 activate_ch(pd);
510
511 /* Process all messages */
512 for (i = 0; i < num; i++) {
513 msg = &msgs[i];
514
515 err = start_ch(pd, msg);
516 if (err)
517 break;
518
519 i2c_op(pd, OP_START, 0);
520
521 /* The interrupt handler takes care of the rest... */
522 k = wait_event_timeout(pd->wait,
523 pd->sr & (ICSR_TACK | SW_DONE),
524 5 * HZ);
525 if (!k)
526 dev_err(pd->dev, "Transfer request timed out\n");
527
Magnus Damm4eb00c92008-08-27 18:33:56 +0900528 retry_count = 1000;
Magnus Dammda672772008-04-22 22:16:49 +0200529again:
Magnus Damm12a55f22010-03-11 10:05:50 +0000530 val = iic_rd(pd, ICSR);
Magnus Dammda672772008-04-22 22:16:49 +0200531
532 dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
533
Magnus Dammda672772008-04-22 22:16:49 +0200534 /* the interrupt handler may wake us up before the
535 * transfer is finished, so poll the hardware
536 * until we're done.
537 */
Magnus Damm4eb00c92008-08-27 18:33:56 +0900538 if (val & ICSR_BUSY) {
539 udelay(10);
Magnus Dammda672772008-04-22 22:16:49 +0200540 if (retry_count--)
541 goto again;
542
543 err = -EIO;
544 dev_err(pd->dev, "Polling timed out\n");
545 break;
546 }
Magnus Damm4eb00c92008-08-27 18:33:56 +0900547
548 /* handle missing acknowledge and arbitration lost */
549 if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) {
550 err = -EIO;
551 break;
552 }
Magnus Dammda672772008-04-22 22:16:49 +0200553 }
554
555 deactivate_ch(pd);
556
557 if (!err)
558 err = num;
559 return err;
560}
561
562static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
563{
564 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
565}
566
567static struct i2c_algorithm sh_mobile_i2c_algorithm = {
568 .functionality = sh_mobile_i2c_func,
569 .master_xfer = sh_mobile_i2c_xfer,
570};
571
Magnus Dammda672772008-04-22 22:16:49 +0200572static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, int hook)
573{
574 struct resource *res;
575 int ret = -ENXIO;
Magnus Damm82b20d82010-08-02 07:16:37 +0000576 int n, k = 0;
Magnus Dammda672772008-04-22 22:16:49 +0200577
578 while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
579 for (n = res->start; hook && n <= res->end; n++) {
Yong Zhang43110512011-09-21 17:28:33 +0800580 if (request_irq(n, sh_mobile_i2c_isr, 0,
Magnus Damm82b20d82010-08-02 07:16:37 +0000581 dev_name(&dev->dev), dev)) {
582 for (n--; n >= res->start; n--)
583 free_irq(n, dev);
584
Magnus Dammda672772008-04-22 22:16:49 +0200585 goto rollback;
Magnus Damm82b20d82010-08-02 07:16:37 +0000586 }
Magnus Dammda672772008-04-22 22:16:49 +0200587 }
588 k++;
589 }
590
591 if (hook)
592 return k > 0 ? 0 : -ENOENT;
593
Magnus Dammda672772008-04-22 22:16:49 +0200594 ret = 0;
595
596 rollback:
Magnus Damm82b20d82010-08-02 07:16:37 +0000597 k--;
Magnus Dammda672772008-04-22 22:16:49 +0200598
Magnus Damm82b20d82010-08-02 07:16:37 +0000599 while (k >= 0) {
600 res = platform_get_resource(dev, IORESOURCE_IRQ, k);
601 for (n = res->start; n <= res->end; n++)
602 free_irq(n, dev);
603
604 k--;
Magnus Dammda672772008-04-22 22:16:49 +0200605 }
606
607 return ret;
608}
609
610static int sh_mobile_i2c_probe(struct platform_device *dev)
611{
Magnus Damm81f81152011-04-28 13:25:36 +0900612 struct i2c_sh_mobile_platform_data *pdata = dev->dev.platform_data;
Magnus Dammda672772008-04-22 22:16:49 +0200613 struct sh_mobile_i2c_data *pd;
614 struct i2c_adapter *adap;
615 struct resource *res;
616 int size;
617 int ret;
618
619 pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
620 if (pd == NULL) {
621 dev_err(&dev->dev, "cannot allocate private data\n");
622 return -ENOMEM;
623 }
624
Magnus Damm1082d5d2011-04-21 22:20:39 +0900625 pd->clk = clk_get(&dev->dev, NULL);
Magnus Dammda672772008-04-22 22:16:49 +0200626 if (IS_ERR(pd->clk)) {
Magnus Damm1082d5d2011-04-21 22:20:39 +0900627 dev_err(&dev->dev, "cannot get clock\n");
Magnus Dammda672772008-04-22 22:16:49 +0200628 ret = PTR_ERR(pd->clk);
629 goto err;
630 }
631
632 ret = sh_mobile_i2c_hook_irqs(dev, 1);
633 if (ret) {
634 dev_err(&dev->dev, "cannot request IRQ\n");
635 goto err_clk;
636 }
637
638 pd->dev = &dev->dev;
639 platform_set_drvdata(dev, pd);
640
641 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
642 if (res == NULL) {
643 dev_err(&dev->dev, "cannot find IO resource\n");
644 ret = -ENOENT;
645 goto err_irq;
646 }
647
Julia Lawall59330822009-07-05 08:37:50 +0200648 size = resource_size(res);
Magnus Dammda672772008-04-22 22:16:49 +0200649
650 pd->reg = ioremap(res->start, size);
651 if (pd->reg == NULL) {
652 dev_err(&dev->dev, "cannot map IO\n");
653 ret = -ENXIO;
654 goto err_irq;
655 }
656
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900657 /* Use platform data bus speed or STANDARD_MODE */
658 pd->bus_speed = STANDARD_MODE;
Magnus Damm81f81152011-04-28 13:25:36 +0900659 if (pdata && pdata->bus_speed)
660 pd->bus_speed = pdata->bus_speed;
661
Magnus Damm962b6032010-03-11 10:06:02 +0000662 /* The IIC blocks on SH-Mobile ARM processors
663 * come with two new bits in ICIC.
664 */
665 if (size > 0x17)
666 pd->flags |= IIC_FLAG_HAS_ICIC67;
667
Shinya Kuribayashi7b0e6292012-10-24 19:56:51 +0900668 sh_mobile_i2c_init(pd);
669
Magnus Dammf1a3b992009-08-14 10:48:59 +0000670 /* Enable Runtime PM for this device.
671 *
672 * Also tell the Runtime PM core to ignore children
673 * for this device since it is valid for us to suspend
674 * this I2C master driver even though the slave devices
675 * on the I2C bus may not be suspended.
676 *
677 * The state of the I2C hardware bus is unaffected by
678 * the Runtime PM state.
679 */
680 pm_suspend_ignore_children(&dev->dev, true);
681 pm_runtime_enable(&dev->dev);
682
Magnus Dammda672772008-04-22 22:16:49 +0200683 /* setup the private data */
684 adap = &pd->adap;
685 i2c_set_adapdata(adap, pd);
686
687 adap->owner = THIS_MODULE;
688 adap->algo = &sh_mobile_i2c_algorithm;
689 adap->dev.parent = &dev->dev;
690 adap->retries = 5;
691 adap->nr = dev->id;
Magnus Dammad337072012-03-30 17:44:02 +0900692 adap->dev.of_node = dev->dev.of_node;
Magnus Dammda672772008-04-22 22:16:49 +0200693
694 strlcpy(adap->name, dev->name, sizeof(adap->name));
695
Magnus Damma5616bd2008-10-31 20:20:55 +0900696 spin_lock_init(&pd->lock);
697 init_waitqueue_head(&pd->wait);
Magnus Dammda672772008-04-22 22:16:49 +0200698
699 ret = i2c_add_numbered_adapter(adap);
700 if (ret < 0) {
701 dev_err(&dev->dev, "cannot add numbered adapter\n");
702 goto err_all;
703 }
704
Shinya Kuribayashi23a61292012-10-24 19:57:27 +0900705 dev_info(&dev->dev,
706 "I2C adapter %d with bus speed %lu Hz (L/H=%x/%x)\n",
707 adap->nr, pd->bus_speed, pd->iccl, pd->icch);
Magnus Dammad337072012-03-30 17:44:02 +0900708
709 of_i2c_register_devices(adap);
Magnus Dammda672772008-04-22 22:16:49 +0200710 return 0;
711
712 err_all:
713 iounmap(pd->reg);
714 err_irq:
715 sh_mobile_i2c_hook_irqs(dev, 0);
716 err_clk:
717 clk_put(pd->clk);
718 err:
719 kfree(pd);
720 return ret;
721}
722
723static int sh_mobile_i2c_remove(struct platform_device *dev)
724{
725 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
726
727 i2c_del_adapter(&pd->adap);
728 iounmap(pd->reg);
729 sh_mobile_i2c_hook_irqs(dev, 0);
730 clk_put(pd->clk);
Magnus Dammf1a3b992009-08-14 10:48:59 +0000731 pm_runtime_disable(&dev->dev);
Magnus Dammda672772008-04-22 22:16:49 +0200732 kfree(pd);
733 return 0;
734}
735
Magnus Dammf1a3b992009-08-14 10:48:59 +0000736static int sh_mobile_i2c_runtime_nop(struct device *dev)
737{
738 /* Runtime PM callback shared between ->runtime_suspend()
739 * and ->runtime_resume(). Simply returns success.
740 *
741 * This driver re-initializes all registers after
742 * pm_runtime_get_sync() anyway so there is no need
743 * to save and restore registers here.
744 */
745 return 0;
746}
747
Alexey Dobriyan47145212009-12-14 18:00:08 -0800748static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
Magnus Dammf1a3b992009-08-14 10:48:59 +0000749 .runtime_suspend = sh_mobile_i2c_runtime_nop,
750 .runtime_resume = sh_mobile_i2c_runtime_nop,
751};
752
Magnus Dammad337072012-03-30 17:44:02 +0900753static const struct of_device_id sh_mobile_i2c_dt_ids[] __devinitconst = {
754 { .compatible = "renesas,rmobile-iic", },
755 {},
756};
757MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
758
Magnus Dammda672772008-04-22 22:16:49 +0200759static struct platform_driver sh_mobile_i2c_driver = {
760 .driver = {
761 .name = "i2c-sh_mobile",
762 .owner = THIS_MODULE,
Magnus Dammf1a3b992009-08-14 10:48:59 +0000763 .pm = &sh_mobile_i2c_dev_pm_ops,
Magnus Dammad337072012-03-30 17:44:02 +0900764 .of_match_table = sh_mobile_i2c_dt_ids,
Magnus Dammda672772008-04-22 22:16:49 +0200765 },
766 .probe = sh_mobile_i2c_probe,
767 .remove = sh_mobile_i2c_remove,
768};
769
770static int __init sh_mobile_i2c_adap_init(void)
771{
772 return platform_driver_register(&sh_mobile_i2c_driver);
773}
774
775static void __exit sh_mobile_i2c_adap_exit(void)
776{
777 platform_driver_unregister(&sh_mobile_i2c_driver);
778}
779
Magnus Dammccb3bc12009-07-22 23:58:39 +0900780subsys_initcall(sh_mobile_i2c_adap_init);
Magnus Dammda672772008-04-22 22:16:49 +0200781module_exit(sh_mobile_i2c_adap_exit);
782
783MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
784MODULE_AUTHOR("Magnus Damm");
785MODULE_LICENSE("GPL v2");
Guennadi Liakhovetski7ef0c122011-04-15 20:18:57 +0200786MODULE_ALIAS("platform:i2c-sh_mobile");