blob: 3dc1eb9f8088c3482644282ad9cfa2ad29b124af [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* linux/drivers/i2c/busses/i2c-s3c2410.c
2 *
Daniel Silverstonec564e6a2009-03-13 13:53:46 +00003 * Copyright (C) 2004,2005,2009 Simtec Electronics
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Ben Dooks <ben@simtec.co.uk>
5 *
6 * S3C2410 I2C Controller
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/module.h>
25
26#include <linux/i2c.h>
27#include <linux/i2c-id.h>
28#include <linux/init.h>
29#include <linux/time.h>
30#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
32#include <linux/errno.h>
33#include <linux/err.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010034#include <linux/platform_device.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000035#include <linux/clk.h>
Ben Dooks61c7cff2008-07-28 12:04:07 +010036#include <linux/cpufreq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020038#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ben Dooks9498cb72008-10-30 10:14:33 +000042#include <plat/regs-iic.h>
43#include <plat/iic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* i2c controller state */
46
47enum s3c24xx_i2c_state {
48 STATE_IDLE,
49 STATE_START,
50 STATE_READ,
51 STATE_WRITE,
52 STATE_STOP
53};
54
Ben Dooks7d85ccd2009-06-12 10:45:29 +010055enum s3c24xx_i2c_type {
56 TYPE_S3C2410,
57 TYPE_S3C2440,
58};
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct s3c24xx_i2c {
61 spinlock_t lock;
62 wait_queue_head_t wait;
Ben Dooksbe44f012008-10-31 16:10:22 +000063 unsigned int suspended:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 struct i2c_msg *msg;
66 unsigned int msg_num;
67 unsigned int msg_idx;
68 unsigned int msg_ptr;
69
Ben Dookse00a8cd2007-05-01 23:26:35 +020070 unsigned int tx_setup;
Ben Dookse0d1ec92008-10-31 16:10:30 +000071 unsigned int irq;
Ben Dookse00a8cd2007-05-01 23:26:35 +020072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 enum s3c24xx_i2c_state state;
Ben Dooks61c7cff2008-07-28 12:04:07 +010074 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 void __iomem *regs;
77 struct clk *clk;
78 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct resource *ioarea;
80 struct i2c_adapter adap;
Ben Dooks61c7cff2008-07-28 12:04:07 +010081
82#ifdef CONFIG_CPU_FREQ
83 struct notifier_block freq_transition;
84#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Ben Dooks6a039ca2008-10-31 16:10:27 +000087/* default platform data removed, dev should always carry data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* s3c24xx_i2c_is2440()
90 *
91 * return true is this is an s3c2440
92*/
93
94static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c)
95{
96 struct platform_device *pdev = to_platform_device(i2c->dev);
Ben Dooks7d85ccd2009-06-12 10:45:29 +010097 enum s3c24xx_i2c_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Ben Dooks7d85ccd2009-06-12 10:45:29 +010099 type = platform_get_device_id(pdev)->driver_data;
100 return type == TYPE_S3C2440;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* s3c24xx_i2c_master_complete
104 *
105 * complete the message and wake up the caller, using the given return code,
106 * or zero to mean ok.
107*/
108
109static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
110{
111 dev_dbg(i2c->dev, "master_complete %d\n", ret);
112
113 i2c->msg_ptr = 0;
114 i2c->msg = NULL;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000115 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 i2c->msg_num = 0;
117 if (ret)
118 i2c->msg_idx = ret;
119
120 wake_up(&i2c->wait);
121}
122
123static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
124{
125 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 tmp = readl(i2c->regs + S3C2410_IICCON);
128 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
132{
133 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 tmp = readl(i2c->regs + S3C2410_IICCON);
136 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/* irq enable/disable functions */
140
141static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
142{
143 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 tmp = readl(i2c->regs + S3C2410_IICCON);
146 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
147}
148
149static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
150{
151 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 tmp = readl(i2c->regs + S3C2410_IICCON);
154 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
155}
156
157
158/* s3c24xx_i2c_message_start
159 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000160 * put the start of a message onto the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161*/
162
Ben Dooks3d0911b2008-10-31 16:10:24 +0000163static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct i2c_msg *msg)
165{
166 unsigned int addr = (msg->addr & 0x7f) << 1;
167 unsigned long stat;
168 unsigned long iiccon;
169
170 stat = 0;
171 stat |= S3C2410_IICSTAT_TXRXEN;
172
173 if (msg->flags & I2C_M_RD) {
174 stat |= S3C2410_IICSTAT_MASTER_RX;
175 addr |= 1;
176 } else
177 stat |= S3C2410_IICSTAT_MASTER_TX;
178
179 if (msg->flags & I2C_M_REV_DIR_ADDR)
180 addr ^= 1;
181
Ben Dooks3d0911b2008-10-31 16:10:24 +0000182 /* todo - check for wether ack wanted or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 s3c24xx_i2c_enable_ack(i2c);
184
185 iiccon = readl(i2c->regs + S3C2410_IICCON);
186 writel(stat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
189 writeb(addr, i2c->regs + S3C2410_IICDS);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000190
Ben Dookse00a8cd2007-05-01 23:26:35 +0200191 /* delay here to ensure the data byte has gotten onto the bus
192 * before the transaction is started */
193
194 ndelay(i2c->tx_setup);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
197 writel(iiccon, i2c->regs + S3C2410_IICCON);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000198
199 stat |= S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 writel(stat, i2c->regs + S3C2410_IICSTAT);
201}
202
203static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
204{
205 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
206
207 dev_dbg(i2c->dev, "STOP\n");
208
209 /* stop the transfer */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000210 iicstat &= ~S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 i2c->state = STATE_STOP;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 s3c24xx_i2c_master_complete(i2c, ret);
216 s3c24xx_i2c_disable_irq(i2c);
217}
218
219/* helper functions to determine the current state in the set of
220 * messages we are sending */
221
222/* is_lastmsg()
223 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000224 * returns TRUE if the current message is the last in the set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225*/
226
227static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
228{
229 return i2c->msg_idx >= (i2c->msg_num - 1);
230}
231
232/* is_msglast
233 *
234 * returns TRUE if we this is the last byte in the current message
235*/
236
237static inline int is_msglast(struct s3c24xx_i2c *i2c)
238{
239 return i2c->msg_ptr == i2c->msg->len-1;
240}
241
242/* is_msgend
243 *
244 * returns TRUE if we reached the end of the current message
245*/
246
247static inline int is_msgend(struct s3c24xx_i2c *i2c)
248{
249 return i2c->msg_ptr >= i2c->msg->len;
250}
251
252/* i2s_s3c_irq_nextbyte
253 *
254 * process an interrupt and work out what to do
255 */
256
257static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
258{
259 unsigned long tmp;
260 unsigned char byte;
261 int ret = 0;
262
263 switch (i2c->state) {
264
265 case STATE_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200266 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 goto out;
268 break;
269
270 case STATE_STOP:
Harvey Harrison08882d22008-04-22 22:16:47 +0200271 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000272 s3c24xx_i2c_disable_irq(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 goto out_ack;
274
275 case STATE_START:
276 /* last thing we did was send a start condition on the
277 * bus, or started a new i2c message
278 */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000279
Ben Dooks63f5c282008-07-01 11:59:42 +0100280 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
282 /* ack was not received... */
283
284 dev_dbg(i2c->dev, "ack was not received\n");
Ben Dooks63f5c282008-07-01 11:59:42 +0100285 s3c24xx_i2c_stop(i2c, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto out_ack;
287 }
288
289 if (i2c->msg->flags & I2C_M_RD)
290 i2c->state = STATE_READ;
291 else
292 i2c->state = STATE_WRITE;
293
294 /* terminate the transfer if there is nothing to do
Ben Dooks63f5c282008-07-01 11:59:42 +0100295 * as this is used by the i2c probe to find devices. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
298 s3c24xx_i2c_stop(i2c, 0);
299 goto out_ack;
300 }
301
302 if (i2c->state == STATE_READ)
303 goto prepare_read;
304
Ben Dooks3d0911b2008-10-31 16:10:24 +0000305 /* fall through to the write state, as we will need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * send a byte as well */
307
308 case STATE_WRITE:
309 /* we are writing data to the device... check for the
310 * end of the message, and if so, work out what to do
311 */
312
Ben Dooks27097812008-07-01 11:59:41 +0100313 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
314 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
315 dev_dbg(i2c->dev, "WRITE: No Ack\n");
316
317 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
318 goto out_ack;
319 }
320 }
321
Ben Dooks3d0911b2008-10-31 16:10:24 +0000322 retry_write:
Ben Dooks27097812008-07-01 11:59:41 +0100323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!is_msgend(i2c)) {
325 byte = i2c->msg->buf[i2c->msg_ptr++];
326 writeb(byte, i2c->regs + S3C2410_IICDS);
Ben Dookse00a8cd2007-05-01 23:26:35 +0200327
328 /* delay after writing the byte to allow the
329 * data setup time on the bus, as writing the
330 * data to the register causes the first bit
331 * to appear on SDA, and SCL will change as
332 * soon as the interrupt is acknowledged */
333
334 ndelay(i2c->tx_setup);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 } else if (!is_lastmsg(i2c)) {
337 /* we need to go to the next i2c message */
338
339 dev_dbg(i2c->dev, "WRITE: Next Message\n");
340
341 i2c->msg_ptr = 0;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000342 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 i2c->msg++;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* check to see if we need to do another message */
346 if (i2c->msg->flags & I2C_M_NOSTART) {
347
348 if (i2c->msg->flags & I2C_M_RD) {
349 /* cannot do this, the controller
350 * forces us to send a new START
351 * when we change direction */
352
353 s3c24xx_i2c_stop(i2c, -EINVAL);
354 }
355
356 goto retry_write;
357 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* send the new start */
359 s3c24xx_i2c_message_start(i2c, i2c->msg);
360 i2c->state = STATE_START;
361 }
362
363 } else {
364 /* send stop */
365
366 s3c24xx_i2c_stop(i2c, 0);
367 }
368 break;
369
370 case STATE_READ:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000371 /* we have a byte of data in the data register, do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * something with it, and then work out wether we are
373 * going to do any more read/write
374 */
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 byte = readb(i2c->regs + S3C2410_IICDS);
377 i2c->msg->buf[i2c->msg_ptr++] = byte;
378
Ben Dooks3d0911b2008-10-31 16:10:24 +0000379 prepare_read:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (is_msglast(i2c)) {
381 /* last byte of buffer */
382
383 if (is_lastmsg(i2c))
384 s3c24xx_i2c_disable_ack(i2c);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 } else if (is_msgend(i2c)) {
387 /* ok, we've read the entire buffer, see if there
388 * is anything else we need to do */
389
390 if (is_lastmsg(i2c)) {
391 /* last message, send stop and complete */
392 dev_dbg(i2c->dev, "READ: Send Stop\n");
393
394 s3c24xx_i2c_stop(i2c, 0);
395 } else {
396 /* go to the next transfer */
397 dev_dbg(i2c->dev, "READ: Next Transfer\n");
398
399 i2c->msg_ptr = 0;
400 i2c->msg_idx++;
401 i2c->msg++;
402 }
403 }
404
405 break;
406 }
407
408 /* acknowlegde the IRQ and get back on with the work */
409
410 out_ack:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000411 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 tmp &= ~S3C2410_IICCON_IRQPEND;
413 writel(tmp, i2c->regs + S3C2410_IICCON);
414 out:
415 return ret;
416}
417
418/* s3c24xx_i2c_irq
419 *
420 * top level IRQ servicing routine
421*/
422
David Howells7d12e782006-10-05 14:55:46 +0100423static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct s3c24xx_i2c *i2c = dev_id;
426 unsigned long status;
427 unsigned long tmp;
428
429 status = readl(i2c->regs + S3C2410_IICSTAT);
430
431 if (status & S3C2410_IICSTAT_ARBITR) {
Ben Dooks3d0911b2008-10-31 16:10:24 +0000432 /* deal with arbitration loss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 dev_err(i2c->dev, "deal with arbitration loss\n");
434 }
435
436 if (i2c->state == STATE_IDLE) {
437 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
438
Ben Dooks3d0911b2008-10-31 16:10:24 +0000439 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 tmp &= ~S3C2410_IICCON_IRQPEND;
441 writel(tmp, i2c->regs + S3C2410_IICCON);
442 goto out;
443 }
Ben Dooks3d0911b2008-10-31 16:10:24 +0000444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* pretty much this leaves us with the fact that we've
446 * transmitted or received whatever byte we last sent */
447
448 i2s_s3c_irq_nextbyte(i2c, status);
449
450 out:
451 return IRQ_HANDLED;
452}
453
454
455/* s3c24xx_i2c_set_master
456 *
457 * get the i2c bus for a master transaction
458*/
459
460static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
461{
462 unsigned long iicstat;
463 int timeout = 400;
464
465 while (timeout-- > 0) {
466 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
469 return 0;
470
471 msleep(1);
472 }
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -ETIMEDOUT;
475}
476
477/* s3c24xx_i2c_doxfer
478 *
479 * this starts an i2c transfer
480*/
481
Ben Dooks3d0911b2008-10-31 16:10:24 +0000482static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
483 struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Mark Brown1bc29622010-04-02 14:15:09 +0100485 unsigned long iicstat, timeout;
486 int spins = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int ret;
488
Ben Dooksbe44f012008-10-31 16:10:22 +0000489 if (i2c->suspended)
Ben Dooks61c7cff2008-07-28 12:04:07 +0100490 return -EIO;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 ret = s3c24xx_i2c_set_master(i2c);
493 if (ret != 0) {
494 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
495 ret = -EAGAIN;
496 goto out;
497 }
498
499 spin_lock_irq(&i2c->lock);
500
501 i2c->msg = msgs;
502 i2c->msg_num = num;
503 i2c->msg_ptr = 0;
504 i2c->msg_idx = 0;
505 i2c->state = STATE_START;
506
507 s3c24xx_i2c_enable_irq(i2c);
508 s3c24xx_i2c_message_start(i2c, msgs);
509 spin_unlock_irq(&i2c->lock);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
512
513 ret = i2c->msg_idx;
514
Ben Dooks3d0911b2008-10-31 16:10:24 +0000515 /* having these next two as dev_err() makes life very
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * noisy when doing an i2cdetect */
517
518 if (timeout == 0)
519 dev_dbg(i2c->dev, "timeout\n");
520 else if (ret != num)
521 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
522
523 /* ensure the stop has been through the bus */
524
Mark Brown1bc29622010-04-02 14:15:09 +0100525 dev_dbg(i2c->dev, "waiting for bus idle\n");
526
527 /* first, try busy waiting briefly */
528 do {
529 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
530 } while ((iicstat & S3C2410_IICSTAT_START) && --spins);
531
532 /* if that timed out sleep */
533 if (!spins) {
534 msleep(1);
535 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
536 }
537
538 if (iicstat & S3C2410_IICSTAT_START)
539 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 out:
542 return ret;
543}
544
545/* s3c24xx_i2c_xfer
546 *
547 * first port of call from the i2c bus code when an message needs
Steven Cole44bbe872005-05-03 18:21:25 -0600548 * transferring across the i2c bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549*/
550
551static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
552 struct i2c_msg *msgs, int num)
553{
554 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
555 int retry;
556 int ret;
557
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200558 clk_enable(i2c->clk);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 for (retry = 0; retry < adap->retries; retry++) {
561
562 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
563
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200564 if (ret != -EAGAIN) {
565 clk_disable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return ret;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
570
571 udelay(100);
572 }
573
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200574 clk_disable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -EREMOTEIO;
576}
577
578/* declare our i2c functionality */
579static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
580{
581 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
582}
583
584/* i2c bus registration info */
585
Jean Delvare8f9082c2006-09-03 22:39:46 +0200586static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .master_xfer = s3c24xx_i2c_xfer,
588 .functionality = s3c24xx_i2c_func,
589};
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/* s3c24xx_i2c_calcdivisor
592 *
593 * return the divisor settings for a given frequency
594*/
595
596static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
597 unsigned int *div1, unsigned int *divs)
598{
599 unsigned int calc_divs = clkin / wanted;
600 unsigned int calc_div1;
601
602 if (calc_divs > (16*16))
603 calc_div1 = 512;
604 else
605 calc_div1 = 16;
606
607 calc_divs += calc_div1-1;
608 calc_divs /= calc_div1;
609
610 if (calc_divs == 0)
611 calc_divs = 1;
612 if (calc_divs > 17)
613 calc_divs = 17;
614
615 *divs = calc_divs;
616 *div1 = calc_div1;
617
618 return clkin / (calc_divs * calc_div1);
619}
620
Ben Dooks61c7cff2008-07-28 12:04:07 +0100621/* s3c24xx_i2c_clockrate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *
623 * work out a divisor for the user requested frequency setting,
624 * either by the requested frequency, or scanning the acceptable
625 * range of frequencies until something is found
626*/
627
Ben Dooks61c7cff2008-07-28 12:04:07 +0100628static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Ben Dooks6a039ca2008-10-31 16:10:27 +0000630 struct s3c2410_platform_i2c *pdata = i2c->dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 unsigned long clkin = clk_get_rate(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 unsigned int divs, div1;
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000633 unsigned long target_frequency;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100634 u32 iiccon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Ben Dooks61c7cff2008-07-28 12:04:07 +0100637 i2c->clkrate = clkin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 clkin /= 1000; /* clkin now in KHz */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000639
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000640 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000642 target_frequency = pdata->frequency ? pdata->frequency : 100000;
643
644 target_frequency /= 1000; /* Target frequency now in KHz */
645
646 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
647
648 if (freq > target_frequency) {
649 dev_err(i2c->dev,
650 "Unable to achieve desired frequency %luKHz." \
651 " Lowest achievable %dKHz\n", target_frequency, freq);
652 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *got = freq;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100656
657 iiccon = readl(i2c->regs + S3C2410_IICCON);
658 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
659 iiccon |= (divs-1);
660
661 if (div1 == 512)
662 iiccon |= S3C2410_IICCON_TXDIV_512;
663
664 writel(iiccon, i2c->regs + S3C2410_IICCON);
665
Ben Dooksa192f712009-03-27 10:52:13 +0000666 if (s3c24xx_i2c_is2440(i2c)) {
667 unsigned long sda_delay;
668
669 if (pdata->sda_delay) {
MyungJoo Ham7031307a2010-09-30 15:54:46 +0200670 sda_delay = clkin * pdata->sda_delay;
671 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
Ben Dooksa192f712009-03-27 10:52:13 +0000672 sda_delay = DIV_ROUND_UP(sda_delay, 5);
673 if (sda_delay > 3)
674 sda_delay = 3;
675 sda_delay |= S3C2410_IICLC_FILTER_ON;
676 } else
677 sda_delay = 0;
678
679 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
680 writel(sda_delay, i2c->regs + S3C2440_IICLC);
681 }
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685
Ben Dooks61c7cff2008-07-28 12:04:07 +0100686#ifdef CONFIG_CPU_FREQ
687
688#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
689
690static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
691 unsigned long val, void *data)
692{
693 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
694 unsigned long flags;
695 unsigned int got;
696 int delta_f;
697 int ret;
698
699 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
700
701 /* if we're post-change and the input clock has slowed down
702 * or at pre-change and the clock is about to speed up, then
703 * adjust our clock rate. <0 is slow, >0 speedup.
704 */
705
706 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
707 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
708 spin_lock_irqsave(&i2c->lock, flags);
709 ret = s3c24xx_i2c_clockrate(i2c, &got);
710 spin_unlock_irqrestore(&i2c->lock, flags);
711
712 if (ret < 0)
713 dev_err(i2c->dev, "cannot find frequency\n");
714 else
715 dev_info(i2c->dev, "setting freq %d\n", got);
716 }
717
718 return 0;
719}
720
721static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
722{
723 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
724
725 return cpufreq_register_notifier(&i2c->freq_transition,
726 CPUFREQ_TRANSITION_NOTIFIER);
727}
728
729static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
730{
731 cpufreq_unregister_notifier(&i2c->freq_transition,
732 CPUFREQ_TRANSITION_NOTIFIER);
733}
734
735#else
736static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
737{
738 return 0;
739}
740
741static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
742{
743}
744#endif
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746/* s3c24xx_i2c_init
747 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000748 * initialise the controller, set the IO lines and frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749*/
750
751static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
752{
753 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
754 struct s3c2410_platform_i2c *pdata;
755 unsigned int freq;
756
757 /* get the plafrom data */
758
Ben Dooks6a039ca2008-10-31 16:10:27 +0000759 pdata = i2c->dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /* inititalise the gpio */
762
Ben Dooks8be310a2008-10-31 16:10:25 +0000763 if (pdata->cfg_gpio)
764 pdata->cfg_gpio(to_platform_device(i2c->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* write slave address */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
769
770 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
771
Ben Dooks61c7cff2008-07-28 12:04:07 +0100772 writel(iicon, i2c->regs + S3C2410_IICCON);
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* we need to work out the divisors for the clock... */
775
Ben Dooks61c7cff2008-07-28 12:04:07 +0100776 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
777 writel(0, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 dev_err(i2c->dev, "cannot meet bus frequency required\n");
779 return -EINVAL;
780 }
781
782 /* todo - check that the i2c lines aren't being dragged anywhere */
783
784 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
785 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return 0;
788}
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790/* s3c24xx_i2c_probe
791 *
792 * called by the bus driver when a suitable device is found
793*/
794
Russell King3ae5eae2005-11-09 22:32:44 +0000795static int s3c24xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Ben Dooks692acbd2008-10-31 16:10:28 +0000797 struct s3c24xx_i2c *i2c;
Ben Dooks399dee22008-07-28 12:04:06 +0100798 struct s3c2410_platform_i2c *pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct resource *res;
800 int ret;
801
Ben Dooks6a039ca2008-10-31 16:10:27 +0000802 pdata = pdev->dev.platform_data;
803 if (!pdata) {
804 dev_err(&pdev->dev, "no platform data\n");
805 return -EINVAL;
806 }
Ben Dooks399dee22008-07-28 12:04:06 +0100807
Ben Dooks692acbd2008-10-31 16:10:28 +0000808 i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
809 if (!i2c) {
810 dev_err(&pdev->dev, "no memory for state\n");
811 return -ENOMEM;
812 }
813
814 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
815 i2c->adap.owner = THIS_MODULE;
816 i2c->adap.algo = &s3c24xx_i2c_algorithm;
817 i2c->adap.retries = 2;
818 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
819 i2c->tx_setup = 50;
820
821 spin_lock_init(&i2c->lock);
822 init_waitqueue_head(&i2c->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /* find the clock and enable it */
825
Russell King3ae5eae2005-11-09 22:32:44 +0000826 i2c->dev = &pdev->dev;
827 i2c->clk = clk_get(&pdev->dev, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (IS_ERR(i2c->clk)) {
Russell King3ae5eae2005-11-09 22:32:44 +0000829 dev_err(&pdev->dev, "cannot get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +0200831 goto err_noclk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Russell King3ae5eae2005-11-09 22:32:44 +0000834 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 clk_enable(i2c->clk);
837
838 /* map the registers */
839
840 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
841 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000842 dev_err(&pdev->dev, "cannot find IO resource\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +0200844 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
Ben Dooks933a2ae2009-06-14 14:04:20 +0100847 i2c->ioarea = request_mem_region(res->start, resource_size(res),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 pdev->name);
849
850 if (i2c->ioarea == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000851 dev_err(&pdev->dev, "cannot request IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ret = -ENXIO;
Ben Dooks5b687902007-05-01 23:26:35 +0200853 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
Ben Dooks933a2ae2009-06-14 14:04:20 +0100856 i2c->regs = ioremap(res->start, resource_size(res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (i2c->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000859 dev_err(&pdev->dev, "cannot map IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 ret = -ENXIO;
Ben Dooks5b687902007-05-01 23:26:35 +0200861 goto err_ioarea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
Ben Dooks3d0911b2008-10-31 16:10:24 +0000864 dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
865 i2c->regs, i2c->ioarea, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /* setup info block for the i2c core */
868
869 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +0000870 i2c->adap.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /* initialise the i2c controller */
873
874 ret = s3c24xx_i2c_init(i2c);
875 if (ret != 0)
Ben Dooks5b687902007-05-01 23:26:35 +0200876 goto err_iomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /* find the IRQ for this unit (note, this relies on the init call to
Ben Dooks3d0911b2008-10-31 16:10:24 +0000879 * ensure no current IRQs pending
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
881
Ben Dookse0d1ec92008-10-31 16:10:30 +0000882 i2c->irq = ret = platform_get_irq(pdev, 0);
883 if (ret <= 0) {
Russell King3ae5eae2005-11-09 22:32:44 +0000884 dev_err(&pdev->dev, "cannot find IRQ\n");
Ben Dooks5b687902007-05-01 23:26:35 +0200885 goto err_iomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
Ben Dookse0d1ec92008-10-31 16:10:30 +0000888 ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,
889 dev_name(&pdev->dev), i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 if (ret != 0) {
Ben Dookse0d1ec92008-10-31 16:10:30 +0000892 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
Ben Dooks5b687902007-05-01 23:26:35 +0200893 goto err_iomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
Ben Dooks61c7cff2008-07-28 12:04:07 +0100896 ret = s3c24xx_i2c_register_cpufreq(i2c);
897 if (ret < 0) {
898 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
899 goto err_irq;
900 }
901
Ben Dooks399dee22008-07-28 12:04:06 +0100902 /* Note, previous versions of the driver used i2c_add_adapter()
903 * to add the bus at any number. We now pass the bus number via
904 * the platform data, so if unset it will now default to always
905 * being bus 0.
906 */
907
908 i2c->adap.nr = pdata->bus_num;
909
910 ret = i2c_add_numbered_adapter(&i2c->adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +0000912 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
Ben Dooks61c7cff2008-07-28 12:04:07 +0100913 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
Russell King3ae5eae2005-11-09 22:32:44 +0000916 platform_set_drvdata(pdev, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Jean Delvare22e965c2009-01-07 14:29:16 +0100918 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200919 clk_disable(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +0200920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Ben Dooks61c7cff2008-07-28 12:04:07 +0100922 err_cpufreq:
923 s3c24xx_i2c_deregister_cpufreq(i2c);
924
Ben Dooks5b687902007-05-01 23:26:35 +0200925 err_irq:
Ben Dookse0d1ec92008-10-31 16:10:30 +0000926 free_irq(i2c->irq, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Ben Dooks5b687902007-05-01 23:26:35 +0200928 err_iomap:
929 iounmap(i2c->regs);
930
931 err_ioarea:
932 release_resource(i2c->ioarea);
933 kfree(i2c->ioarea);
934
935 err_clk:
936 clk_disable(i2c->clk);
937 clk_put(i2c->clk);
938
939 err_noclk:
Ben Dooks692acbd2008-10-31 16:10:28 +0000940 kfree(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return ret;
942}
943
944/* s3c24xx_i2c_remove
945 *
946 * called when device is removed from the bus
947*/
948
Russell King3ae5eae2005-11-09 22:32:44 +0000949static int s3c24xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Russell King3ae5eae2005-11-09 22:32:44 +0000951 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Ben Dooks5b687902007-05-01 23:26:35 +0200952
Ben Dooks61c7cff2008-07-28 12:04:07 +0100953 s3c24xx_i2c_deregister_cpufreq(i2c);
954
Ben Dooks5b687902007-05-01 23:26:35 +0200955 i2c_del_adapter(&i2c->adap);
Ben Dookse0d1ec92008-10-31 16:10:30 +0000956 free_irq(i2c->irq, i2c);
Ben Dooks5b687902007-05-01 23:26:35 +0200957
958 clk_disable(i2c->clk);
959 clk_put(i2c->clk);
960
961 iounmap(i2c->regs);
962
963 release_resource(i2c->ioarea);
964 kfree(i2c->ioarea);
Ben Dooks692acbd2008-10-31 16:10:28 +0000965 kfree(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 return 0;
968}
969
970#ifdef CONFIG_PM
Magnus Damm6a6c61892009-07-08 13:22:47 +0200971static int s3c24xx_i2c_suspend_noirq(struct device *dev)
Ben Dooksbe44f012008-10-31 16:10:22 +0000972{
Magnus Damm6a6c61892009-07-08 13:22:47 +0200973 struct platform_device *pdev = to_platform_device(dev);
974 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
975
Ben Dooksbe44f012008-10-31 16:10:22 +0000976 i2c->suspended = 1;
Magnus Damm6a6c61892009-07-08 13:22:47 +0200977
Ben Dooksbe44f012008-10-31 16:10:22 +0000978 return 0;
979}
980
Magnus Damm6a6c61892009-07-08 13:22:47 +0200981static int s3c24xx_i2c_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Magnus Damm6a6c61892009-07-08 13:22:47 +0200983 struct platform_device *pdev = to_platform_device(dev);
984 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Russell King9480e302005-10-28 09:52:56 -0700985
Ben Dooksbe44f012008-10-31 16:10:22 +0000986 i2c->suspended = 0;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200987 clk_enable(i2c->clk);
Ben Dooksbe44f012008-10-31 16:10:22 +0000988 s3c24xx_i2c_init(i2c);
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200989 clk_disable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 return 0;
992}
993
Alexey Dobriyan47145212009-12-14 18:00:08 -0800994static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
Magnus Damm6a6c61892009-07-08 13:22:47 +0200995 .suspend_noirq = s3c24xx_i2c_suspend_noirq,
996 .resume = s3c24xx_i2c_resume,
997};
998
999#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000#else
Magnus Damm6a6c61892009-07-08 13:22:47 +02001001#define S3C24XX_DEV_PM_OPS NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002#endif
1003
1004/* device driver for platform bus bits */
1005
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001006static struct platform_device_id s3c24xx_driver_ids[] = {
1007 {
1008 .name = "s3c2410-i2c",
1009 .driver_data = TYPE_S3C2410,
1010 }, {
1011 .name = "s3c2440-i2c",
1012 .driver_data = TYPE_S3C2440,
1013 }, { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014};
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001015MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001017static struct platform_driver s3c24xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 .probe = s3c24xx_i2c_probe,
1019 .remove = s3c24xx_i2c_remove,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001020 .id_table = s3c24xx_driver_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001021 .driver = {
1022 .owner = THIS_MODULE,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001023 .name = "s3c-i2c",
Magnus Damm6a6c61892009-07-08 13:22:47 +02001024 .pm = S3C24XX_DEV_PM_OPS,
Russell King3ae5eae2005-11-09 22:32:44 +00001025 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026};
1027
1028static int __init i2c_adap_s3c_init(void)
1029{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001030 return platform_driver_register(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
Mark Brown18dc83a2009-02-26 16:29:22 +00001032subsys_initcall(i2c_adap_s3c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034static void __exit i2c_adap_s3c_exit(void)
1035{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001036 platform_driver_unregister(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038module_exit(i2c_adap_s3c_exit);
1039
1040MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1041MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1042MODULE_LICENSE("GPL");