blob: 9c00ad1f50a2bda012fa9b6ea1be6929b1fa7ff4 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/time.h>
29#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
31#include <linux/errno.h>
32#include <linux/err.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010033#include <linux/platform_device.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000034#include <linux/clk.h>
Ben Dooks61c7cff2008-07-28 12:04:07 +010035#include <linux/cpufreq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020037#include <linux/io.h>
Thomas Abraham5a5f5082011-09-13 09:46:05 +053038#include <linux/of_i2c.h>
39#include <linux/of_gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Ben Dooks9498cb72008-10-30 10:14:33 +000043#include <plat/regs-iic.h>
44#include <plat/iic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* i2c controller state */
47
48enum s3c24xx_i2c_state {
49 STATE_IDLE,
50 STATE_START,
51 STATE_READ,
52 STATE_WRITE,
53 STATE_STOP
54};
55
Ben Dooks7d85ccd2009-06-12 10:45:29 +010056enum s3c24xx_i2c_type {
57 TYPE_S3C2410,
58 TYPE_S3C2440,
59};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061struct s3c24xx_i2c {
62 spinlock_t lock;
63 wait_queue_head_t wait;
Ben Dooksbe44f012008-10-31 16:10:22 +000064 unsigned int suspended:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 struct i2c_msg *msg;
67 unsigned int msg_num;
68 unsigned int msg_idx;
69 unsigned int msg_ptr;
70
Ben Dookse00a8cd2007-05-01 23:26:35 +020071 unsigned int tx_setup;
Ben Dookse0d1ec92008-10-31 16:10:30 +000072 unsigned int irq;
Ben Dookse00a8cd2007-05-01 23:26:35 +020073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 enum s3c24xx_i2c_state state;
Ben Dooks61c7cff2008-07-28 12:04:07 +010075 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 void __iomem *regs;
78 struct clk *clk;
79 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct resource *ioarea;
81 struct i2c_adapter adap;
Ben Dooks61c7cff2008-07-28 12:04:07 +010082
Thomas Abraham4fd81eb2011-09-13 09:46:04 +053083 struct s3c2410_platform_i2c *pdata;
Thomas Abraham5a5f5082011-09-13 09:46:05 +053084 int gpios[2];
Ben Dooks61c7cff2008-07-28 12:04:07 +010085#ifdef CONFIG_CPU_FREQ
86 struct notifier_block freq_transition;
87#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Ben Dooks6a039ca2008-10-31 16:10:27 +000090/* default platform data removed, dev should always carry data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* s3c24xx_i2c_is2440()
93 *
94 * return true is this is an s3c2440
95*/
96
97static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c)
98{
99 struct platform_device *pdev = to_platform_device(i2c->dev);
Ben Dooks7d85ccd2009-06-12 10:45:29 +0100100 enum s3c24xx_i2c_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530102#ifdef CONFIG_OF
103 if (i2c->dev->of_node)
104 return of_device_is_compatible(i2c->dev->of_node,
105 "samsung,s3c2440-i2c");
106#endif
107
Ben Dooks7d85ccd2009-06-12 10:45:29 +0100108 type = platform_get_device_id(pdev)->driver_data;
109 return type == TYPE_S3C2440;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* s3c24xx_i2c_master_complete
113 *
114 * complete the message and wake up the caller, using the given return code,
115 * or zero to mean ok.
116*/
117
118static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
119{
120 dev_dbg(i2c->dev, "master_complete %d\n", ret);
121
122 i2c->msg_ptr = 0;
123 i2c->msg = NULL;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000124 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 i2c->msg_num = 0;
126 if (ret)
127 i2c->msg_idx = ret;
128
129 wake_up(&i2c->wait);
130}
131
132static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
133{
134 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 tmp = readl(i2c->regs + S3C2410_IICCON);
137 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
141{
142 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 tmp = readl(i2c->regs + S3C2410_IICCON);
145 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/* irq enable/disable functions */
149
150static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
151{
152 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 tmp = readl(i2c->regs + S3C2410_IICCON);
155 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
156}
157
158static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
159{
160 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 tmp = readl(i2c->regs + S3C2410_IICCON);
163 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
164}
165
166
167/* s3c24xx_i2c_message_start
168 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000169 * put the start of a message onto the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170*/
171
Ben Dooks3d0911b2008-10-31 16:10:24 +0000172static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct i2c_msg *msg)
174{
175 unsigned int addr = (msg->addr & 0x7f) << 1;
176 unsigned long stat;
177 unsigned long iiccon;
178
179 stat = 0;
180 stat |= S3C2410_IICSTAT_TXRXEN;
181
182 if (msg->flags & I2C_M_RD) {
183 stat |= S3C2410_IICSTAT_MASTER_RX;
184 addr |= 1;
185 } else
186 stat |= S3C2410_IICSTAT_MASTER_TX;
187
188 if (msg->flags & I2C_M_REV_DIR_ADDR)
189 addr ^= 1;
190
Ben Dooks3d0911b2008-10-31 16:10:24 +0000191 /* todo - check for wether ack wanted or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 s3c24xx_i2c_enable_ack(i2c);
193
194 iiccon = readl(i2c->regs + S3C2410_IICCON);
195 writel(stat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
198 writeb(addr, i2c->regs + S3C2410_IICDS);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000199
Ben Dookse00a8cd2007-05-01 23:26:35 +0200200 /* delay here to ensure the data byte has gotten onto the bus
201 * before the transaction is started */
202
203 ndelay(i2c->tx_setup);
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
206 writel(iiccon, i2c->regs + S3C2410_IICCON);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000207
208 stat |= S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 writel(stat, i2c->regs + S3C2410_IICSTAT);
210}
211
212static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
213{
214 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
215
216 dev_dbg(i2c->dev, "STOP\n");
217
218 /* stop the transfer */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000219 iicstat &= ~S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 i2c->state = STATE_STOP;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 s3c24xx_i2c_master_complete(i2c, ret);
225 s3c24xx_i2c_disable_irq(i2c);
226}
227
228/* helper functions to determine the current state in the set of
229 * messages we are sending */
230
231/* is_lastmsg()
232 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000233 * returns TRUE if the current message is the last in the set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234*/
235
236static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
237{
238 return i2c->msg_idx >= (i2c->msg_num - 1);
239}
240
241/* is_msglast
242 *
243 * returns TRUE if we this is the last byte in the current message
244*/
245
246static inline int is_msglast(struct s3c24xx_i2c *i2c)
247{
248 return i2c->msg_ptr == i2c->msg->len-1;
249}
250
251/* is_msgend
252 *
253 * returns TRUE if we reached the end of the current message
254*/
255
256static inline int is_msgend(struct s3c24xx_i2c *i2c)
257{
258 return i2c->msg_ptr >= i2c->msg->len;
259}
260
Huisung Kang19820512011-06-23 21:37:33 +0900261/* i2c_s3c_irq_nextbyte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
263 * process an interrupt and work out what to do
264 */
265
Huisung Kang19820512011-06-23 21:37:33 +0900266static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 unsigned long tmp;
269 unsigned char byte;
270 int ret = 0;
271
272 switch (i2c->state) {
273
274 case STATE_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200275 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 case STATE_STOP:
Harvey Harrison08882d22008-04-22 22:16:47 +0200279 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000280 s3c24xx_i2c_disable_irq(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 goto out_ack;
282
283 case STATE_START:
284 /* last thing we did was send a start condition on the
285 * bus, or started a new i2c message
286 */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000287
Ben Dooks63f5c282008-07-01 11:59:42 +0100288 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
290 /* ack was not received... */
291
292 dev_dbg(i2c->dev, "ack was not received\n");
Ben Dooks63f5c282008-07-01 11:59:42 +0100293 s3c24xx_i2c_stop(i2c, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 goto out_ack;
295 }
296
297 if (i2c->msg->flags & I2C_M_RD)
298 i2c->state = STATE_READ;
299 else
300 i2c->state = STATE_WRITE;
301
302 /* terminate the transfer if there is nothing to do
Ben Dooks63f5c282008-07-01 11:59:42 +0100303 * as this is used by the i2c probe to find devices. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
306 s3c24xx_i2c_stop(i2c, 0);
307 goto out_ack;
308 }
309
310 if (i2c->state == STATE_READ)
311 goto prepare_read;
312
Ben Dooks3d0911b2008-10-31 16:10:24 +0000313 /* fall through to the write state, as we will need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * send a byte as well */
315
316 case STATE_WRITE:
317 /* we are writing data to the device... check for the
318 * end of the message, and if so, work out what to do
319 */
320
Ben Dooks27097812008-07-01 11:59:41 +0100321 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
322 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
323 dev_dbg(i2c->dev, "WRITE: No Ack\n");
324
325 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
326 goto out_ack;
327 }
328 }
329
Ben Dooks3d0911b2008-10-31 16:10:24 +0000330 retry_write:
Ben Dooks27097812008-07-01 11:59:41 +0100331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!is_msgend(i2c)) {
333 byte = i2c->msg->buf[i2c->msg_ptr++];
334 writeb(byte, i2c->regs + S3C2410_IICDS);
Ben Dookse00a8cd2007-05-01 23:26:35 +0200335
336 /* delay after writing the byte to allow the
337 * data setup time on the bus, as writing the
338 * data to the register causes the first bit
339 * to appear on SDA, and SCL will change as
340 * soon as the interrupt is acknowledged */
341
342 ndelay(i2c->tx_setup);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else if (!is_lastmsg(i2c)) {
345 /* we need to go to the next i2c message */
346
347 dev_dbg(i2c->dev, "WRITE: Next Message\n");
348
349 i2c->msg_ptr = 0;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000350 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 i2c->msg++;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* check to see if we need to do another message */
354 if (i2c->msg->flags & I2C_M_NOSTART) {
355
356 if (i2c->msg->flags & I2C_M_RD) {
357 /* cannot do this, the controller
358 * forces us to send a new START
359 * when we change direction */
360
361 s3c24xx_i2c_stop(i2c, -EINVAL);
362 }
363
364 goto retry_write;
365 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* send the new start */
367 s3c24xx_i2c_message_start(i2c, i2c->msg);
368 i2c->state = STATE_START;
369 }
370
371 } else {
372 /* send stop */
373
374 s3c24xx_i2c_stop(i2c, 0);
375 }
376 break;
377
378 case STATE_READ:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000379 /* we have a byte of data in the data register, do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * something with it, and then work out wether we are
381 * going to do any more read/write
382 */
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 byte = readb(i2c->regs + S3C2410_IICDS);
385 i2c->msg->buf[i2c->msg_ptr++] = byte;
386
Ben Dooks3d0911b2008-10-31 16:10:24 +0000387 prepare_read:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (is_msglast(i2c)) {
389 /* last byte of buffer */
390
391 if (is_lastmsg(i2c))
392 s3c24xx_i2c_disable_ack(i2c);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 } else if (is_msgend(i2c)) {
395 /* ok, we've read the entire buffer, see if there
396 * is anything else we need to do */
397
398 if (is_lastmsg(i2c)) {
399 /* last message, send stop and complete */
400 dev_dbg(i2c->dev, "READ: Send Stop\n");
401
402 s3c24xx_i2c_stop(i2c, 0);
403 } else {
404 /* go to the next transfer */
405 dev_dbg(i2c->dev, "READ: Next Transfer\n");
406
407 i2c->msg_ptr = 0;
408 i2c->msg_idx++;
409 i2c->msg++;
410 }
411 }
412
413 break;
414 }
415
416 /* acknowlegde the IRQ and get back on with the work */
417
418 out_ack:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000419 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 tmp &= ~S3C2410_IICCON_IRQPEND;
421 writel(tmp, i2c->regs + S3C2410_IICCON);
422 out:
423 return ret;
424}
425
426/* s3c24xx_i2c_irq
427 *
428 * top level IRQ servicing routine
429*/
430
David Howells7d12e782006-10-05 14:55:46 +0100431static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct s3c24xx_i2c *i2c = dev_id;
434 unsigned long status;
435 unsigned long tmp;
436
437 status = readl(i2c->regs + S3C2410_IICSTAT);
438
439 if (status & S3C2410_IICSTAT_ARBITR) {
Ben Dooks3d0911b2008-10-31 16:10:24 +0000440 /* deal with arbitration loss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 dev_err(i2c->dev, "deal with arbitration loss\n");
442 }
443
444 if (i2c->state == STATE_IDLE) {
445 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
446
Ben Dooks3d0911b2008-10-31 16:10:24 +0000447 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 tmp &= ~S3C2410_IICCON_IRQPEND;
449 writel(tmp, i2c->regs + S3C2410_IICCON);
450 goto out;
451 }
Ben Dooks3d0911b2008-10-31 16:10:24 +0000452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* pretty much this leaves us with the fact that we've
454 * transmitted or received whatever byte we last sent */
455
Huisung Kang19820512011-06-23 21:37:33 +0900456 i2c_s3c_irq_nextbyte(i2c, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 out:
459 return IRQ_HANDLED;
460}
461
462
463/* s3c24xx_i2c_set_master
464 *
465 * get the i2c bus for a master transaction
466*/
467
468static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
469{
470 unsigned long iicstat;
471 int timeout = 400;
472
473 while (timeout-- > 0) {
474 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
477 return 0;
478
479 msleep(1);
480 }
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -ETIMEDOUT;
483}
484
485/* s3c24xx_i2c_doxfer
486 *
487 * this starts an i2c transfer
488*/
489
Ben Dooks3d0911b2008-10-31 16:10:24 +0000490static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
491 struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Mark Brown1bc29622010-04-02 14:15:09 +0100493 unsigned long iicstat, timeout;
494 int spins = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int ret;
496
Ben Dooksbe44f012008-10-31 16:10:22 +0000497 if (i2c->suspended)
Ben Dooks61c7cff2008-07-28 12:04:07 +0100498 return -EIO;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ret = s3c24xx_i2c_set_master(i2c);
501 if (ret != 0) {
502 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
503 ret = -EAGAIN;
504 goto out;
505 }
506
507 spin_lock_irq(&i2c->lock);
508
509 i2c->msg = msgs;
510 i2c->msg_num = num;
511 i2c->msg_ptr = 0;
512 i2c->msg_idx = 0;
513 i2c->state = STATE_START;
514
515 s3c24xx_i2c_enable_irq(i2c);
516 s3c24xx_i2c_message_start(i2c, msgs);
517 spin_unlock_irq(&i2c->lock);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
520
521 ret = i2c->msg_idx;
522
Ben Dooks3d0911b2008-10-31 16:10:24 +0000523 /* having these next two as dev_err() makes life very
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * noisy when doing an i2cdetect */
525
526 if (timeout == 0)
527 dev_dbg(i2c->dev, "timeout\n");
528 else if (ret != num)
529 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
530
531 /* ensure the stop has been through the bus */
532
Mark Brown1bc29622010-04-02 14:15:09 +0100533 dev_dbg(i2c->dev, "waiting for bus idle\n");
534
535 /* first, try busy waiting briefly */
536 do {
537 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
538 } while ((iicstat & S3C2410_IICSTAT_START) && --spins);
539
540 /* if that timed out sleep */
541 if (!spins) {
542 msleep(1);
543 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
544 }
545
546 if (iicstat & S3C2410_IICSTAT_START)
547 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 out:
550 return ret;
551}
552
553/* s3c24xx_i2c_xfer
554 *
555 * first port of call from the i2c bus code when an message needs
Steven Cole44bbe872005-05-03 18:21:25 -0600556 * transferring across the i2c bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557*/
558
559static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
560 struct i2c_msg *msgs, int num)
561{
562 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
563 int retry;
564 int ret;
565
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200566 clk_enable(i2c->clk);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 for (retry = 0; retry < adap->retries; retry++) {
569
570 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
571
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200572 if (ret != -EAGAIN) {
573 clk_disable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return ret;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
578
579 udelay(100);
580 }
581
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200582 clk_disable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EREMOTEIO;
584}
585
586/* declare our i2c functionality */
587static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
588{
589 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
590}
591
592/* i2c bus registration info */
593
Jean Delvare8f9082c2006-09-03 22:39:46 +0200594static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 .master_xfer = s3c24xx_i2c_xfer,
596 .functionality = s3c24xx_i2c_func,
597};
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/* s3c24xx_i2c_calcdivisor
600 *
601 * return the divisor settings for a given frequency
602*/
603
604static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
605 unsigned int *div1, unsigned int *divs)
606{
607 unsigned int calc_divs = clkin / wanted;
608 unsigned int calc_div1;
609
610 if (calc_divs > (16*16))
611 calc_div1 = 512;
612 else
613 calc_div1 = 16;
614
615 calc_divs += calc_div1-1;
616 calc_divs /= calc_div1;
617
618 if (calc_divs == 0)
619 calc_divs = 1;
620 if (calc_divs > 17)
621 calc_divs = 17;
622
623 *divs = calc_divs;
624 *div1 = calc_div1;
625
626 return clkin / (calc_divs * calc_div1);
627}
628
Ben Dooks61c7cff2008-07-28 12:04:07 +0100629/* s3c24xx_i2c_clockrate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
631 * work out a divisor for the user requested frequency setting,
632 * either by the requested frequency, or scanning the acceptable
633 * range of frequencies until something is found
634*/
635
Ben Dooks61c7cff2008-07-28 12:04:07 +0100636static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530638 struct s3c2410_platform_i2c *pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 unsigned long clkin = clk_get_rate(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 unsigned int divs, div1;
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000641 unsigned long target_frequency;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100642 u32 iiccon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Ben Dooks61c7cff2008-07-28 12:04:07 +0100645 i2c->clkrate = clkin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 clkin /= 1000; /* clkin now in KHz */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000647
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000648 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000650 target_frequency = pdata->frequency ? pdata->frequency : 100000;
651
652 target_frequency /= 1000; /* Target frequency now in KHz */
653
654 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
655
656 if (freq > target_frequency) {
657 dev_err(i2c->dev,
658 "Unable to achieve desired frequency %luKHz." \
659 " Lowest achievable %dKHz\n", target_frequency, freq);
660 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 *got = freq;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100664
665 iiccon = readl(i2c->regs + S3C2410_IICCON);
666 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
667 iiccon |= (divs-1);
668
669 if (div1 == 512)
670 iiccon |= S3C2410_IICCON_TXDIV_512;
671
672 writel(iiccon, i2c->regs + S3C2410_IICCON);
673
Ben Dooksa192f712009-03-27 10:52:13 +0000674 if (s3c24xx_i2c_is2440(i2c)) {
675 unsigned long sda_delay;
676
677 if (pdata->sda_delay) {
MyungJoo Ham70313072010-09-30 15:54:46 +0200678 sda_delay = clkin * pdata->sda_delay;
679 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
Ben Dooksa192f712009-03-27 10:52:13 +0000680 sda_delay = DIV_ROUND_UP(sda_delay, 5);
681 if (sda_delay > 3)
682 sda_delay = 3;
683 sda_delay |= S3C2410_IICLC_FILTER_ON;
684 } else
685 sda_delay = 0;
686
687 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
688 writel(sda_delay, i2c->regs + S3C2440_IICLC);
689 }
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692}
693
Ben Dooks61c7cff2008-07-28 12:04:07 +0100694#ifdef CONFIG_CPU_FREQ
695
696#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
697
698static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
699 unsigned long val, void *data)
700{
701 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
702 unsigned long flags;
703 unsigned int got;
704 int delta_f;
705 int ret;
706
707 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
708
709 /* if we're post-change and the input clock has slowed down
710 * or at pre-change and the clock is about to speed up, then
711 * adjust our clock rate. <0 is slow, >0 speedup.
712 */
713
714 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
715 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
716 spin_lock_irqsave(&i2c->lock, flags);
717 ret = s3c24xx_i2c_clockrate(i2c, &got);
718 spin_unlock_irqrestore(&i2c->lock, flags);
719
720 if (ret < 0)
721 dev_err(i2c->dev, "cannot find frequency\n");
722 else
723 dev_info(i2c->dev, "setting freq %d\n", got);
724 }
725
726 return 0;
727}
728
729static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
730{
731 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
732
733 return cpufreq_register_notifier(&i2c->freq_transition,
734 CPUFREQ_TRANSITION_NOTIFIER);
735}
736
737static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
738{
739 cpufreq_unregister_notifier(&i2c->freq_transition,
740 CPUFREQ_TRANSITION_NOTIFIER);
741}
742
743#else
744static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
745{
746 return 0;
747}
748
749static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
750{
751}
752#endif
753
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530754#ifdef CONFIG_OF
755static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
756{
757 int idx, gpio, ret;
758
759 for (idx = 0; idx < 2; idx++) {
760 gpio = of_get_gpio(i2c->dev->of_node, idx);
761 if (!gpio_is_valid(gpio)) {
762 dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
763 goto free_gpio;
764 }
765
766 ret = gpio_request(gpio, "i2c-bus");
767 if (ret) {
768 dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
769 goto free_gpio;
770 }
771 }
772 return 0;
773
774free_gpio:
775 while (--idx >= 0)
776 gpio_free(i2c->gpios[idx]);
777 return -EINVAL;
778}
779
780static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
781{
782 unsigned int idx;
783 for (idx = 0; idx < 2; idx++)
784 gpio_free(i2c->gpios[idx]);
785}
786#else
787static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
788{
789 return -EINVAL;
790}
791
792static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
793{
794}
795#endif
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/* s3c24xx_i2c_init
798 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000799 * initialise the controller, set the IO lines and frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800*/
801
802static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
803{
804 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
805 struct s3c2410_platform_i2c *pdata;
806 unsigned int freq;
807
808 /* get the plafrom data */
809
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530810 pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* inititalise the gpio */
813
Ben Dooks8be310a2008-10-31 16:10:25 +0000814 if (pdata->cfg_gpio)
815 pdata->cfg_gpio(to_platform_device(i2c->dev));
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530816 else
817 if (s3c24xx_i2c_parse_dt_gpio(i2c))
818 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* write slave address */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
823
824 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
825
Ben Dooks61c7cff2008-07-28 12:04:07 +0100826 writel(iicon, i2c->regs + S3C2410_IICCON);
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* we need to work out the divisors for the clock... */
829
Ben Dooks61c7cff2008-07-28 12:04:07 +0100830 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
831 writel(0, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 dev_err(i2c->dev, "cannot meet bus frequency required\n");
833 return -EINVAL;
834 }
835
836 /* todo - check that the i2c lines aren't being dragged anywhere */
837
838 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
839 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
842}
843
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530844#ifdef CONFIG_OF
845/* s3c24xx_i2c_parse_dt
846 *
847 * Parse the device tree node and retreive the platform data.
848*/
849
850static void
851s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
852{
853 struct s3c2410_platform_i2c *pdata = i2c->pdata;
854
855 if (!np)
856 return;
857
858 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
859 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
860 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
861 of_property_read_u32(np, "samsung,i2c-max-bus-freq",
862 (u32 *)&pdata->frequency);
863}
864#else
865static void
866s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
867{
868 return;
869}
870#endif
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/* s3c24xx_i2c_probe
873 *
874 * called by the bus driver when a suitable device is found
875*/
876
Russell King3ae5eae2005-11-09 22:32:44 +0000877static int s3c24xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Ben Dooks692acbd2008-10-31 16:10:28 +0000879 struct s3c24xx_i2c *i2c;
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530880 struct s3c2410_platform_i2c *pdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct resource *res;
882 int ret;
883
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530884 if (!pdev->dev.of_node) {
885 pdata = pdev->dev.platform_data;
886 if (!pdata) {
887 dev_err(&pdev->dev, "no platform data\n");
888 return -EINVAL;
889 }
Ben Dooks6a039ca2008-10-31 16:10:27 +0000890 }
Ben Dooks399dee22008-07-28 12:04:06 +0100891
Ben Dooks692acbd2008-10-31 16:10:28 +0000892 i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
893 if (!i2c) {
894 dev_err(&pdev->dev, "no memory for state\n");
895 return -ENOMEM;
896 }
897
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530898 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
899 if (!i2c->pdata) {
900 ret = -ENOMEM;
901 goto err_noclk;
902 }
903
904 if (pdata)
905 memcpy(i2c->pdata, pdata, sizeof(*pdata));
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530906 else
907 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530908
Ben Dooks692acbd2008-10-31 16:10:28 +0000909 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
910 i2c->adap.owner = THIS_MODULE;
911 i2c->adap.algo = &s3c24xx_i2c_algorithm;
912 i2c->adap.retries = 2;
913 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
914 i2c->tx_setup = 50;
915
916 spin_lock_init(&i2c->lock);
917 init_waitqueue_head(&i2c->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /* find the clock and enable it */
920
Russell King3ae5eae2005-11-09 22:32:44 +0000921 i2c->dev = &pdev->dev;
922 i2c->clk = clk_get(&pdev->dev, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (IS_ERR(i2c->clk)) {
Russell King3ae5eae2005-11-09 22:32:44 +0000924 dev_err(&pdev->dev, "cannot get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +0200926 goto err_noclk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
Russell King3ae5eae2005-11-09 22:32:44 +0000929 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 clk_enable(i2c->clk);
932
933 /* map the registers */
934
935 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
936 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000937 dev_err(&pdev->dev, "cannot find IO resource\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +0200939 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
Ben Dooks933a2ae2009-06-14 14:04:20 +0100942 i2c->ioarea = request_mem_region(res->start, resource_size(res),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 pdev->name);
944
945 if (i2c->ioarea == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000946 dev_err(&pdev->dev, "cannot request IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 ret = -ENXIO;
Ben Dooks5b687902007-05-01 23:26:35 +0200948 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
Ben Dooks933a2ae2009-06-14 14:04:20 +0100951 i2c->regs = ioremap(res->start, resource_size(res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 if (i2c->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000954 dev_err(&pdev->dev, "cannot map IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 ret = -ENXIO;
Ben Dooks5b687902007-05-01 23:26:35 +0200956 goto err_ioarea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
Ben Dooks3d0911b2008-10-31 16:10:24 +0000959 dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
960 i2c->regs, i2c->ioarea, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 /* setup info block for the i2c core */
963
964 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +0000965 i2c->adap.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /* initialise the i2c controller */
968
969 ret = s3c24xx_i2c_init(i2c);
970 if (ret != 0)
Ben Dooks5b687902007-05-01 23:26:35 +0200971 goto err_iomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /* find the IRQ for this unit (note, this relies on the init call to
Ben Dooks3d0911b2008-10-31 16:10:24 +0000974 * ensure no current IRQs pending
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 */
976
Ben Dookse0d1ec92008-10-31 16:10:30 +0000977 i2c->irq = ret = platform_get_irq(pdev, 0);
978 if (ret <= 0) {
Russell King3ae5eae2005-11-09 22:32:44 +0000979 dev_err(&pdev->dev, "cannot find IRQ\n");
Ben Dooks5b687902007-05-01 23:26:35 +0200980 goto err_iomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
Ben Dookse0d1ec92008-10-31 16:10:30 +0000983 ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,
984 dev_name(&pdev->dev), i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (ret != 0) {
Ben Dookse0d1ec92008-10-31 16:10:30 +0000987 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
Ben Dooks5b687902007-05-01 23:26:35 +0200988 goto err_iomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
Ben Dooks61c7cff2008-07-28 12:04:07 +0100991 ret = s3c24xx_i2c_register_cpufreq(i2c);
992 if (ret < 0) {
993 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
994 goto err_irq;
995 }
996
Ben Dooks399dee22008-07-28 12:04:06 +0100997 /* Note, previous versions of the driver used i2c_add_adapter()
998 * to add the bus at any number. We now pass the bus number via
999 * the platform data, so if unset it will now default to always
1000 * being bus 0.
1001 */
1002
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301003 i2c->adap.nr = i2c->pdata->bus_num;
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301004 i2c->adap.dev.of_node = pdev->dev.of_node;
Ben Dooks399dee22008-07-28 12:04:06 +01001005
1006 ret = i2c_add_numbered_adapter(&i2c->adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001008 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
Ben Dooks61c7cff2008-07-28 12:04:07 +01001009 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301012 of_i2c_register_devices(&i2c->adap);
Russell King3ae5eae2005-11-09 22:32:44 +00001013 platform_set_drvdata(pdev, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Jean Delvare22e965c2009-01-07 14:29:16 +01001015 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +02001016 clk_disable(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Ben Dooks61c7cff2008-07-28 12:04:07 +01001019 err_cpufreq:
1020 s3c24xx_i2c_deregister_cpufreq(i2c);
1021
Ben Dooks5b687902007-05-01 23:26:35 +02001022 err_irq:
Ben Dookse0d1ec92008-10-31 16:10:30 +00001023 free_irq(i2c->irq, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Ben Dooks5b687902007-05-01 23:26:35 +02001025 err_iomap:
1026 iounmap(i2c->regs);
1027
1028 err_ioarea:
1029 release_resource(i2c->ioarea);
1030 kfree(i2c->ioarea);
1031
1032 err_clk:
1033 clk_disable(i2c->clk);
1034 clk_put(i2c->clk);
1035
1036 err_noclk:
Ben Dooks692acbd2008-10-31 16:10:28 +00001037 kfree(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return ret;
1039}
1040
1041/* s3c24xx_i2c_remove
1042 *
1043 * called when device is removed from the bus
1044*/
1045
Russell King3ae5eae2005-11-09 22:32:44 +00001046static int s3c24xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Russell King3ae5eae2005-11-09 22:32:44 +00001048 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Ben Dooks5b687902007-05-01 23:26:35 +02001049
Ben Dooks61c7cff2008-07-28 12:04:07 +01001050 s3c24xx_i2c_deregister_cpufreq(i2c);
1051
Ben Dooks5b687902007-05-01 23:26:35 +02001052 i2c_del_adapter(&i2c->adap);
Ben Dookse0d1ec92008-10-31 16:10:30 +00001053 free_irq(i2c->irq, i2c);
Ben Dooks5b687902007-05-01 23:26:35 +02001054
1055 clk_disable(i2c->clk);
1056 clk_put(i2c->clk);
1057
1058 iounmap(i2c->regs);
1059
1060 release_resource(i2c->ioarea);
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301061 s3c24xx_i2c_dt_gpio_free(i2c);
Ben Dooks5b687902007-05-01 23:26:35 +02001062 kfree(i2c->ioarea);
Ben Dooks692acbd2008-10-31 16:10:28 +00001063 kfree(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 return 0;
1066}
1067
1068#ifdef CONFIG_PM
Magnus Damm6a6c61892009-07-08 13:22:47 +02001069static int s3c24xx_i2c_suspend_noirq(struct device *dev)
Ben Dooksbe44f012008-10-31 16:10:22 +00001070{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001071 struct platform_device *pdev = to_platform_device(dev);
1072 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1073
Ben Dooksbe44f012008-10-31 16:10:22 +00001074 i2c->suspended = 1;
Magnus Damm6a6c61892009-07-08 13:22:47 +02001075
Ben Dooksbe44f012008-10-31 16:10:22 +00001076 return 0;
1077}
1078
Magnus Damm6a6c61892009-07-08 13:22:47 +02001079static int s3c24xx_i2c_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001081 struct platform_device *pdev = to_platform_device(dev);
1082 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Russell King9480e302005-10-28 09:52:56 -07001083
Ben Dooksbe44f012008-10-31 16:10:22 +00001084 i2c->suspended = 0;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +02001085 clk_enable(i2c->clk);
Ben Dooksbe44f012008-10-31 16:10:22 +00001086 s3c24xx_i2c_init(i2c);
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +02001087 clk_disable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 return 0;
1090}
1091
Alexey Dobriyan47145212009-12-14 18:00:08 -08001092static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
Magnus Damm6a6c61892009-07-08 13:22:47 +02001093 .suspend_noirq = s3c24xx_i2c_suspend_noirq,
1094 .resume = s3c24xx_i2c_resume,
1095};
1096
1097#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098#else
Magnus Damm6a6c61892009-07-08 13:22:47 +02001099#define S3C24XX_DEV_PM_OPS NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100#endif
1101
1102/* device driver for platform bus bits */
1103
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001104static struct platform_device_id s3c24xx_driver_ids[] = {
1105 {
1106 .name = "s3c2410-i2c",
1107 .driver_data = TYPE_S3C2410,
1108 }, {
1109 .name = "s3c2440-i2c",
1110 .driver_data = TYPE_S3C2440,
1111 }, { },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112};
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001113MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301115#ifdef CONFIG_OF
1116static const struct of_device_id s3c24xx_i2c_match[] = {
1117 { .compatible = "samsung,s3c2410-i2c" },
1118 { .compatible = "samsung,s3c2440-i2c" },
1119 {},
1120};
1121MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
1122#else
1123#define s3c24xx_i2c_match NULL
1124#endif
1125
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001126static struct platform_driver s3c24xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 .probe = s3c24xx_i2c_probe,
1128 .remove = s3c24xx_i2c_remove,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001129 .id_table = s3c24xx_driver_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001130 .driver = {
1131 .owner = THIS_MODULE,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001132 .name = "s3c-i2c",
Magnus Damm6a6c61892009-07-08 13:22:47 +02001133 .pm = S3C24XX_DEV_PM_OPS,
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301134 .of_match_table = s3c24xx_i2c_match,
Russell King3ae5eae2005-11-09 22:32:44 +00001135 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136};
1137
1138static int __init i2c_adap_s3c_init(void)
1139{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001140 return platform_driver_register(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
Mark Brown18dc83a2009-02-26 16:29:22 +00001142subsys_initcall(i2c_adap_s3c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144static void __exit i2c_adap_s3c_exit(void)
1145{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001146 platform_driver_unregister(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148module_exit(i2c_adap_s3c_exit);
1149
1150MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1151MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1152MODULE_LICENSE("GPL");