blob: d2fabd9dbf80bba20ccd24e3dc3514262ded09d8 [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>
30#include <linux/err.h>
Magnus Dammf1a3b992009-08-14 10:48:59 +000031#include <linux/pm_runtime.h>
Magnus Dammda672772008-04-22 22:16:49 +020032#include <linux/clk.h>
33#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Magnus Dammda672772008-04-22 22:16:49 +020035
Magnus Damm4eb00c92008-08-27 18:33:56 +090036/* Transmit operation: */
37/* */
38/* 0 byte transmit */
39/* BUS: S A8 ACK P */
40/* IRQ: DTE WAIT */
41/* ICIC: */
42/* ICCR: 0x94 0x90 */
43/* ICDR: A8 */
44/* */
45/* 1 byte transmit */
46/* BUS: S A8 ACK D8(1) ACK P */
47/* IRQ: DTE WAIT WAIT */
48/* ICIC: -DTE */
49/* ICCR: 0x94 0x90 */
50/* ICDR: A8 D8(1) */
51/* */
52/* 2 byte transmit */
53/* BUS: S A8 ACK D8(1) ACK D8(2) ACK P */
54/* IRQ: DTE WAIT WAIT WAIT */
55/* ICIC: -DTE */
56/* ICCR: 0x94 0x90 */
57/* ICDR: A8 D8(1) D8(2) */
58/* */
59/* 3 bytes or more, +---------+ gets repeated */
60/* */
61/* */
62/* Receive operation: */
63/* */
64/* 0 byte receive - not supported since slave may hold SDA low */
65/* */
66/* 1 byte receive [TX] | [RX] */
67/* BUS: S A8 ACK | D8(1) ACK P */
68/* IRQ: DTE WAIT | WAIT DTE */
69/* ICIC: -DTE | +DTE */
70/* ICCR: 0x94 0x81 | 0xc0 */
71/* ICDR: A8 | D8(1) */
72/* */
73/* 2 byte receive [TX]| [RX] */
74/* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P */
75/* IRQ: DTE WAIT | WAIT WAIT DTE */
76/* ICIC: -DTE | +DTE */
77/* ICCR: 0x94 0x81 | 0xc0 */
78/* ICDR: A8 | D8(1) D8(2) */
79/* */
80/* 3 byte receive [TX] | [RX] */
81/* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */
82/* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */
83/* ICIC: -DTE | +DTE */
84/* ICCR: 0x94 0x81 | 0xc0 */
85/* ICDR: A8 | D8(1) D8(2) D8(3) */
86/* */
87/* 4 bytes or more, this part is repeated +---------+ */
88/* */
89/* */
90/* Interrupt order and BUSY flag */
91/* ___ _ */
92/* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */
93/* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */
94/* */
95/* S D7 D6 D5 D4 D3 D2 D1 D0 P */
96/* ___ */
97/* WAIT IRQ ________________________________/ \___________ */
98/* TACK IRQ ____________________________________/ \_______ */
99/* DTE IRQ __________________________________________/ \_ */
100/* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
101/* _______________________________________________ */
102/* BUSY __/ \_ */
103/* */
104
Magnus Dammda672772008-04-22 22:16:49 +0200105enum sh_mobile_i2c_op {
106 OP_START = 0,
Magnus Damm4eb00c92008-08-27 18:33:56 +0900107 OP_TX_FIRST,
108 OP_TX,
Magnus Dammda672772008-04-22 22:16:49 +0200109 OP_TX_STOP,
110 OP_TX_TO_RX,
Magnus Damm4eb00c92008-08-27 18:33:56 +0900111 OP_RX,
Magnus Dammda672772008-04-22 22:16:49 +0200112 OP_RX_STOP,
Magnus Damm4eb00c92008-08-27 18:33:56 +0900113 OP_RX_STOP_DATA,
Magnus Dammda672772008-04-22 22:16:49 +0200114};
115
116struct sh_mobile_i2c_data {
117 struct device *dev;
118 void __iomem *reg;
119 struct i2c_adapter adap;
120
121 struct clk *clk;
122 u_int8_t iccl;
123 u_int8_t icch;
124
125 spinlock_t lock;
126 wait_queue_head_t wait;
127 struct i2c_msg *msg;
128 int pos;
129 int sr;
130};
131
132#define NORMAL_SPEED 100000 /* FAST_SPEED 400000 */
133
134/* Register offsets */
Magnus Damm12a55f22010-03-11 10:05:50 +0000135#define ICDR 0x00
136#define ICCR 0x04
137#define ICSR 0x08
138#define ICIC 0x0c
139#define ICCL 0x10
140#define ICCH 0x14
Magnus Dammda672772008-04-22 22:16:49 +0200141
142/* Register bits */
143#define ICCR_ICE 0x80
144#define ICCR_RACK 0x40
145#define ICCR_TRS 0x10
146#define ICCR_BBSY 0x04
147#define ICCR_SCP 0x01
148
149#define ICSR_SCLM 0x80
150#define ICSR_SDAM 0x40
151#define SW_DONE 0x20
152#define ICSR_BUSY 0x10
153#define ICSR_AL 0x08
154#define ICSR_TACK 0x04
155#define ICSR_WAIT 0x02
156#define ICSR_DTE 0x01
157
158#define ICIC_ALE 0x08
159#define ICIC_TACKE 0x04
160#define ICIC_WAITE 0x02
161#define ICIC_DTEE 0x01
162
Magnus Damm12a55f22010-03-11 10:05:50 +0000163static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
164{
165 iowrite8(data, pd->reg + offs);
166}
167
168static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
169{
170 return ioread8(pd->reg + offs);
171}
172
173static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
174 unsigned char set, unsigned char clr)
175{
176 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
177}
178
Magnus Dammda672772008-04-22 22:16:49 +0200179static void activate_ch(struct sh_mobile_i2c_data *pd)
180{
Magnus Damma5616bd2008-10-31 20:20:55 +0900181 unsigned long i2c_clk;
182 u_int32_t num;
183 u_int32_t denom;
184 u_int32_t tmp;
185
Magnus Dammf1a3b992009-08-14 10:48:59 +0000186 /* Wake up device and enable clock */
187 pm_runtime_get_sync(pd->dev);
Magnus Dammda672772008-04-22 22:16:49 +0200188 clk_enable(pd->clk);
189
Magnus Damma5616bd2008-10-31 20:20:55 +0900190 /* Get clock rate after clock is enabled */
191 i2c_clk = clk_get_rate(pd->clk);
192
193 /* Calculate the value for iccl. From the data sheet:
194 * iccl = (p clock / transfer rate) * (L / (L + H))
195 * where L and H are the SCL low/high ratio (5/4 in this case).
196 * We also round off the result.
197 */
198 num = i2c_clk * 5;
199 denom = NORMAL_SPEED * 9;
200 tmp = num * 10 / denom;
201 if (tmp % 10 >= 5)
202 pd->iccl = (u_int8_t)((num/denom) + 1);
203 else
204 pd->iccl = (u_int8_t)(num/denom);
205
206 /* Calculate the value for icch. From the data sheet:
207 icch = (p clock / transfer rate) * (H / (L + H)) */
208 num = i2c_clk * 4;
209 tmp = num * 10 / denom;
210 if (tmp % 10 >= 5)
211 pd->icch = (u_int8_t)((num/denom) + 1);
212 else
213 pd->icch = (u_int8_t)(num/denom);
214
Magnus Dammda672772008-04-22 22:16:49 +0200215 /* Enable channel and configure rx ack */
Magnus Damm12a55f22010-03-11 10:05:50 +0000216 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200217
218 /* Mask all interrupts */
Magnus Damm12a55f22010-03-11 10:05:50 +0000219 iic_wr(pd, ICIC, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200220
221 /* Set the clock */
Magnus Damm12a55f22010-03-11 10:05:50 +0000222 iic_wr(pd, ICCL, pd->iccl);
223 iic_wr(pd, ICCH, pd->icch);
Magnus Dammda672772008-04-22 22:16:49 +0200224}
225
226static void deactivate_ch(struct sh_mobile_i2c_data *pd)
227{
228 /* Clear/disable interrupts */
Magnus Damm12a55f22010-03-11 10:05:50 +0000229 iic_wr(pd, ICSR, 0);
230 iic_wr(pd, ICIC, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200231
232 /* Disable channel */
Magnus Damm12a55f22010-03-11 10:05:50 +0000233 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
Magnus Dammda672772008-04-22 22:16:49 +0200234
Magnus Dammf1a3b992009-08-14 10:48:59 +0000235 /* Disable clock and mark device as idle */
Magnus Dammda672772008-04-22 22:16:49 +0200236 clk_disable(pd->clk);
Magnus Dammf1a3b992009-08-14 10:48:59 +0000237 pm_runtime_put_sync(pd->dev);
Magnus Dammda672772008-04-22 22:16:49 +0200238}
239
240static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
241 enum sh_mobile_i2c_op op, unsigned char data)
242{
243 unsigned char ret = 0;
244 unsigned long flags;
245
246 dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
247
248 spin_lock_irqsave(&pd->lock, flags);
249
250 switch (op) {
Magnus Damm4eb00c92008-08-27 18:33:56 +0900251 case OP_START: /* issue start and trigger DTE interrupt */
Magnus Damm12a55f22010-03-11 10:05:50 +0000252 iic_wr(pd, ICCR, 0x94);
Magnus Dammda672772008-04-22 22:16:49 +0200253 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900254 case OP_TX_FIRST: /* disable DTE interrupt and write data */
Magnus Damm12a55f22010-03-11 10:05:50 +0000255 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
256 iic_wr(pd, ICDR, data);
Magnus Dammda672772008-04-22 22:16:49 +0200257 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900258 case OP_TX: /* write data */
Magnus Damm12a55f22010-03-11 10:05:50 +0000259 iic_wr(pd, ICDR, data);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900260 break;
261 case OP_TX_STOP: /* write data and issue a stop afterwards */
Magnus Damm12a55f22010-03-11 10:05:50 +0000262 iic_wr(pd, ICDR, data);
263 iic_wr(pd, ICCR, 0x90);
Magnus Dammda672772008-04-22 22:16:49 +0200264 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900265 case OP_TX_TO_RX: /* select read mode */
Magnus Damm12a55f22010-03-11 10:05:50 +0000266 iic_wr(pd, ICCR, 0x81);
Magnus Dammda672772008-04-22 22:16:49 +0200267 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900268 case OP_RX: /* just read data */
Magnus Damm12a55f22010-03-11 10:05:50 +0000269 ret = iic_rd(pd, ICDR);
Magnus Dammda672772008-04-22 22:16:49 +0200270 break;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900271 case OP_RX_STOP: /* enable DTE interrupt, issue stop */
Magnus Damm12a55f22010-03-11 10:05:50 +0000272 iic_wr(pd, ICIC,
273 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
274 iic_wr(pd, ICCR, 0xc0);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900275 break;
276 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
Magnus Damm12a55f22010-03-11 10:05:50 +0000277 iic_wr(pd, ICIC,
278 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
279 ret = iic_rd(pd, ICDR);
280 iic_wr(pd, ICCR, 0xc0);
Magnus Dammda672772008-04-22 22:16:49 +0200281 break;
282 }
283
284 spin_unlock_irqrestore(&pd->lock, flags);
285
286 dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
287 return ret;
288}
289
Magnus Damm4eb00c92008-08-27 18:33:56 +0900290static int sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
291{
292 if (pd->pos == -1)
293 return 1;
294
295 return 0;
296}
297
298static int sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
299{
300 if (pd->pos == (pd->msg->len - 1))
301 return 1;
302
303 return 0;
304}
305
306static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
307 unsigned char *buf)
308{
309 switch (pd->pos) {
310 case -1:
311 *buf = (pd->msg->addr & 0x7f) << 1;
312 *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
313 break;
314 default:
315 *buf = pd->msg->buf[pd->pos];
316 }
317}
318
319static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
320{
321 unsigned char data;
322
323 if (pd->pos == pd->msg->len)
324 return 1;
325
326 sh_mobile_i2c_get_data(pd, &data);
327
328 if (sh_mobile_i2c_is_last_byte(pd))
329 i2c_op(pd, OP_TX_STOP, data);
330 else if (sh_mobile_i2c_is_first_byte(pd))
331 i2c_op(pd, OP_TX_FIRST, data);
332 else
333 i2c_op(pd, OP_TX, data);
334
335 pd->pos++;
336 return 0;
337}
338
339static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
340{
341 unsigned char data;
342 int real_pos;
343
344 do {
345 if (pd->pos <= -1) {
346 sh_mobile_i2c_get_data(pd, &data);
347
348 if (sh_mobile_i2c_is_first_byte(pd))
349 i2c_op(pd, OP_TX_FIRST, data);
350 else
351 i2c_op(pd, OP_TX, data);
352 break;
353 }
354
355 if (pd->pos == 0) {
356 i2c_op(pd, OP_TX_TO_RX, 0);
357 break;
358 }
359
360 real_pos = pd->pos - 2;
361
362 if (pd->pos == pd->msg->len) {
363 if (real_pos < 0) {
364 i2c_op(pd, OP_RX_STOP, 0);
365 break;
366 }
367 data = i2c_op(pd, OP_RX_STOP_DATA, 0);
368 } else
369 data = i2c_op(pd, OP_RX, 0);
370
Magnus Dammbff4056c82008-11-13 15:28:21 +0900371 if (real_pos >= 0)
372 pd->msg->buf[real_pos] = data;
Magnus Damm4eb00c92008-08-27 18:33:56 +0900373 } while (0);
374
375 pd->pos++;
376 return pd->pos == (pd->msg->len + 2);
377}
378
Magnus Dammda672772008-04-22 22:16:49 +0200379static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
380{
381 struct platform_device *dev = dev_id;
382 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900383 unsigned char sr;
384 int wakeup;
Magnus Dammda672772008-04-22 22:16:49 +0200385
Magnus Damm12a55f22010-03-11 10:05:50 +0000386 sr = iic_rd(pd, ICSR);
Magnus Damm4eb00c92008-08-27 18:33:56 +0900387 pd->sr |= sr; /* remember state */
Magnus Dammda672772008-04-22 22:16:49 +0200388
389 dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
Magnus Damm4eb00c92008-08-27 18:33:56 +0900390 (pd->msg->flags & I2C_M_RD) ? "read" : "write",
391 pd->pos, pd->msg->len);
Magnus Dammda672772008-04-22 22:16:49 +0200392
393 if (sr & (ICSR_AL | ICSR_TACK)) {
Magnus Damm4eb00c92008-08-27 18:33:56 +0900394 /* don't interrupt transaction - continue to issue stop */
Magnus Damm12a55f22010-03-11 10:05:50 +0000395 iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
Magnus Damm4eb00c92008-08-27 18:33:56 +0900396 wakeup = 0;
397 } else if (pd->msg->flags & I2C_M_RD)
398 wakeup = sh_mobile_i2c_isr_rx(pd);
399 else
400 wakeup = sh_mobile_i2c_isr_tx(pd);
Magnus Dammda672772008-04-22 22:16:49 +0200401
Magnus Damm4eb00c92008-08-27 18:33:56 +0900402 if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
Magnus Damm12a55f22010-03-11 10:05:50 +0000403 iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
Magnus Dammda672772008-04-22 22:16:49 +0200404
Magnus Dammda672772008-04-22 22:16:49 +0200405 if (wakeup) {
406 pd->sr |= SW_DONE;
407 wake_up(&pd->wait);
408 }
409
410 return IRQ_HANDLED;
411}
412
413static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg)
414{
Magnus Damm4eb00c92008-08-27 18:33:56 +0900415 if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
416 dev_err(pd->dev, "Unsupported zero length i2c read\n");
417 return -EIO;
418 }
419
Magnus Dammda672772008-04-22 22:16:49 +0200420 /* Initialize channel registers */
Magnus Damm12a55f22010-03-11 10:05:50 +0000421 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
Magnus Dammda672772008-04-22 22:16:49 +0200422
423 /* Enable channel and configure rx ack */
Magnus Damm12a55f22010-03-11 10:05:50 +0000424 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
Magnus Dammda672772008-04-22 22:16:49 +0200425
426 /* Set the clock */
Magnus Damm12a55f22010-03-11 10:05:50 +0000427 iic_wr(pd, ICCL, pd->iccl);
428 iic_wr(pd, ICCH, pd->icch);
Magnus Dammda672772008-04-22 22:16:49 +0200429
430 pd->msg = usr_msg;
431 pd->pos = -1;
432 pd->sr = 0;
433
Magnus Damm4eb00c92008-08-27 18:33:56 +0900434 /* Enable all interrupts to begin with */
Magnus Damm12a55f22010-03-11 10:05:50 +0000435 iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
Magnus Dammda672772008-04-22 22:16:49 +0200436 return 0;
437}
438
439static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
440 struct i2c_msg *msgs,
441 int num)
442{
443 struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
444 struct i2c_msg *msg;
445 int err = 0;
446 u_int8_t val;
447 int i, k, retry_count;
448
449 activate_ch(pd);
450
451 /* Process all messages */
452 for (i = 0; i < num; i++) {
453 msg = &msgs[i];
454
455 err = start_ch(pd, msg);
456 if (err)
457 break;
458
459 i2c_op(pd, OP_START, 0);
460
461 /* The interrupt handler takes care of the rest... */
462 k = wait_event_timeout(pd->wait,
463 pd->sr & (ICSR_TACK | SW_DONE),
464 5 * HZ);
465 if (!k)
466 dev_err(pd->dev, "Transfer request timed out\n");
467
Magnus Damm4eb00c92008-08-27 18:33:56 +0900468 retry_count = 1000;
Magnus Dammda672772008-04-22 22:16:49 +0200469again:
Magnus Damm12a55f22010-03-11 10:05:50 +0000470 val = iic_rd(pd, ICSR);
Magnus Dammda672772008-04-22 22:16:49 +0200471
472 dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
473
Magnus Dammda672772008-04-22 22:16:49 +0200474 /* the interrupt handler may wake us up before the
475 * transfer is finished, so poll the hardware
476 * until we're done.
477 */
Magnus Damm4eb00c92008-08-27 18:33:56 +0900478 if (val & ICSR_BUSY) {
479 udelay(10);
Magnus Dammda672772008-04-22 22:16:49 +0200480 if (retry_count--)
481 goto again;
482
483 err = -EIO;
484 dev_err(pd->dev, "Polling timed out\n");
485 break;
486 }
Magnus Damm4eb00c92008-08-27 18:33:56 +0900487
488 /* handle missing acknowledge and arbitration lost */
489 if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) {
490 err = -EIO;
491 break;
492 }
Magnus Dammda672772008-04-22 22:16:49 +0200493 }
494
495 deactivate_ch(pd);
496
497 if (!err)
498 err = num;
499 return err;
500}
501
502static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
503{
504 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
505}
506
507static struct i2c_algorithm sh_mobile_i2c_algorithm = {
508 .functionality = sh_mobile_i2c_func,
509 .master_xfer = sh_mobile_i2c_xfer,
510};
511
Magnus Dammda672772008-04-22 22:16:49 +0200512static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, int hook)
513{
514 struct resource *res;
515 int ret = -ENXIO;
516 int q, m;
517 int k = 0;
518 int n = 0;
519
520 while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
521 for (n = res->start; hook && n <= res->end; n++) {
522 if (request_irq(n, sh_mobile_i2c_isr, IRQF_DISABLED,
Jean Delvare22e965c2009-01-07 14:29:16 +0100523 dev_name(&dev->dev), dev))
Magnus Dammda672772008-04-22 22:16:49 +0200524 goto rollback;
525 }
526 k++;
527 }
528
529 if (hook)
530 return k > 0 ? 0 : -ENOENT;
531
532 k--;
533 ret = 0;
534
535 rollback:
536 for (q = k; k >= 0; k--) {
537 for (m = n; m >= res->start; m--)
538 free_irq(m, dev);
539
540 res = platform_get_resource(dev, IORESOURCE_IRQ, k - 1);
541 m = res->end;
542 }
543
544 return ret;
545}
546
547static int sh_mobile_i2c_probe(struct platform_device *dev)
548{
549 struct sh_mobile_i2c_data *pd;
550 struct i2c_adapter *adap;
551 struct resource *res;
Magnus Damma5616bd2008-10-31 20:20:55 +0900552 char clk_name[8];
Magnus Dammda672772008-04-22 22:16:49 +0200553 int size;
554 int ret;
555
556 pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
557 if (pd == NULL) {
558 dev_err(&dev->dev, "cannot allocate private data\n");
559 return -ENOMEM;
560 }
561
Magnus Damma5616bd2008-10-31 20:20:55 +0900562 snprintf(clk_name, sizeof(clk_name), "i2c%d", dev->id);
563 pd->clk = clk_get(&dev->dev, clk_name);
Magnus Dammda672772008-04-22 22:16:49 +0200564 if (IS_ERR(pd->clk)) {
Magnus Damma5616bd2008-10-31 20:20:55 +0900565 dev_err(&dev->dev, "cannot get clock \"%s\"\n", clk_name);
Magnus Dammda672772008-04-22 22:16:49 +0200566 ret = PTR_ERR(pd->clk);
567 goto err;
568 }
569
570 ret = sh_mobile_i2c_hook_irqs(dev, 1);
571 if (ret) {
572 dev_err(&dev->dev, "cannot request IRQ\n");
573 goto err_clk;
574 }
575
576 pd->dev = &dev->dev;
577 platform_set_drvdata(dev, pd);
578
579 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
580 if (res == NULL) {
581 dev_err(&dev->dev, "cannot find IO resource\n");
582 ret = -ENOENT;
583 goto err_irq;
584 }
585
Julia Lawall59330822009-07-05 08:37:50 +0200586 size = resource_size(res);
Magnus Dammda672772008-04-22 22:16:49 +0200587
588 pd->reg = ioremap(res->start, size);
589 if (pd->reg == NULL) {
590 dev_err(&dev->dev, "cannot map IO\n");
591 ret = -ENXIO;
592 goto err_irq;
593 }
594
Magnus Dammf1a3b992009-08-14 10:48:59 +0000595 /* Enable Runtime PM for this device.
596 *
597 * Also tell the Runtime PM core to ignore children
598 * for this device since it is valid for us to suspend
599 * this I2C master driver even though the slave devices
600 * on the I2C bus may not be suspended.
601 *
602 * The state of the I2C hardware bus is unaffected by
603 * the Runtime PM state.
604 */
605 pm_suspend_ignore_children(&dev->dev, true);
606 pm_runtime_enable(&dev->dev);
607
Magnus Dammda672772008-04-22 22:16:49 +0200608 /* setup the private data */
609 adap = &pd->adap;
610 i2c_set_adapdata(adap, pd);
611
612 adap->owner = THIS_MODULE;
613 adap->algo = &sh_mobile_i2c_algorithm;
614 adap->dev.parent = &dev->dev;
615 adap->retries = 5;
616 adap->nr = dev->id;
617
618 strlcpy(adap->name, dev->name, sizeof(adap->name));
619
Magnus Damma5616bd2008-10-31 20:20:55 +0900620 spin_lock_init(&pd->lock);
621 init_waitqueue_head(&pd->wait);
Magnus Dammda672772008-04-22 22:16:49 +0200622
623 ret = i2c_add_numbered_adapter(adap);
624 if (ret < 0) {
625 dev_err(&dev->dev, "cannot add numbered adapter\n");
626 goto err_all;
627 }
628
629 return 0;
630
631 err_all:
632 iounmap(pd->reg);
633 err_irq:
634 sh_mobile_i2c_hook_irqs(dev, 0);
635 err_clk:
636 clk_put(pd->clk);
637 err:
638 kfree(pd);
639 return ret;
640}
641
642static int sh_mobile_i2c_remove(struct platform_device *dev)
643{
644 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
645
646 i2c_del_adapter(&pd->adap);
647 iounmap(pd->reg);
648 sh_mobile_i2c_hook_irqs(dev, 0);
649 clk_put(pd->clk);
Magnus Dammf1a3b992009-08-14 10:48:59 +0000650 pm_runtime_disable(&dev->dev);
Magnus Dammda672772008-04-22 22:16:49 +0200651 kfree(pd);
652 return 0;
653}
654
Magnus Dammf1a3b992009-08-14 10:48:59 +0000655static int sh_mobile_i2c_runtime_nop(struct device *dev)
656{
657 /* Runtime PM callback shared between ->runtime_suspend()
658 * and ->runtime_resume(). Simply returns success.
659 *
660 * This driver re-initializes all registers after
661 * pm_runtime_get_sync() anyway so there is no need
662 * to save and restore registers here.
663 */
664 return 0;
665}
666
Alexey Dobriyan47145212009-12-14 18:00:08 -0800667static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
Magnus Dammf1a3b992009-08-14 10:48:59 +0000668 .runtime_suspend = sh_mobile_i2c_runtime_nop,
669 .runtime_resume = sh_mobile_i2c_runtime_nop,
670};
671
Magnus Dammda672772008-04-22 22:16:49 +0200672static struct platform_driver sh_mobile_i2c_driver = {
673 .driver = {
674 .name = "i2c-sh_mobile",
675 .owner = THIS_MODULE,
Magnus Dammf1a3b992009-08-14 10:48:59 +0000676 .pm = &sh_mobile_i2c_dev_pm_ops,
Magnus Dammda672772008-04-22 22:16:49 +0200677 },
678 .probe = sh_mobile_i2c_probe,
679 .remove = sh_mobile_i2c_remove,
680};
681
682static int __init sh_mobile_i2c_adap_init(void)
683{
684 return platform_driver_register(&sh_mobile_i2c_driver);
685}
686
687static void __exit sh_mobile_i2c_adap_exit(void)
688{
689 platform_driver_unregister(&sh_mobile_i2c_driver);
690}
691
Magnus Dammccb3bc12009-07-22 23:58:39 +0900692subsys_initcall(sh_mobile_i2c_adap_init);
Magnus Dammda672772008-04-22 22:16:49 +0200693module_exit(sh_mobile_i2c_adap_exit);
694
695MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
696MODULE_AUTHOR("Magnus Damm");
697MODULE_LICENSE("GPL v2");