blob: 3e0335f1fc60df8b78ce50c7a4e5284e82ad432f [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>
Mark Brownc62c3ca2012-01-21 13:28:47 +000034#include <linux/pm_runtime.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>
Thomas Abraham5a5f5082011-09-13 09:46:05 +053039#include <linux/of_i2c.h>
40#include <linux/of_gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ben Dooks9498cb72008-10-30 10:14:33 +000044#include <plat/regs-iic.h>
Arnd Bergmann436d42c2012-08-24 15:22:12 +020045#include <linux/platform_data/i2c-s3c2410.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Karol Lewandowski27452492012-04-23 18:24:00 +020047/* Treat S3C2410 as baseline hardware, anything else is supported via quirks */
48#define QUIRK_S3C2440 (1 << 0)
Karol Lewandowskiec39ef82012-04-23 18:24:01 +020049#define QUIRK_HDMIPHY (1 << 1)
50#define QUIRK_NO_GPIO (1 << 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Karol Lewandowski27452492012-04-23 18:24:00 +020052/* i2c controller state */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053enum s3c24xx_i2c_state {
54 STATE_IDLE,
55 STATE_START,
56 STATE_READ,
57 STATE_WRITE,
58 STATE_STOP
59};
60
61struct s3c24xx_i2c {
62 spinlock_t lock;
63 wait_queue_head_t wait;
Karol Lewandowski27452492012-04-23 18:24:00 +020064 unsigned int quirks;
Ben Dooksbe44f012008-10-31 16:10:22 +000065 unsigned int suspended:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 struct i2c_msg *msg;
68 unsigned int msg_num;
69 unsigned int msg_idx;
70 unsigned int msg_ptr;
71
Ben Dookse00a8cd2007-05-01 23:26:35 +020072 unsigned int tx_setup;
Ben Dookse0d1ec92008-10-31 16:10:30 +000073 unsigned int irq;
Ben Dookse00a8cd2007-05-01 23:26:35 +020074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 enum s3c24xx_i2c_state state;
Ben Dooks61c7cff2008-07-28 12:04:07 +010076 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 void __iomem *regs;
79 struct clk *clk;
80 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct resource *ioarea;
82 struct i2c_adapter adap;
Ben Dooks61c7cff2008-07-28 12:04:07 +010083
Thomas Abraham4fd81eb2011-09-13 09:46:04 +053084 struct s3c2410_platform_i2c *pdata;
Thomas Abraham5a5f5082011-09-13 09:46:05 +053085 int gpios[2];
Ben Dooks61c7cff2008-07-28 12:04:07 +010086#ifdef CONFIG_CPU_FREQ
87 struct notifier_block freq_transition;
88#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Karol Lewandowski27452492012-04-23 18:24:00 +020091static struct platform_device_id s3c24xx_driver_ids[] = {
92 {
93 .name = "s3c2410-i2c",
94 .driver_data = 0,
95 }, {
96 .name = "s3c2440-i2c",
97 .driver_data = QUIRK_S3C2440,
Karol Lewandowskiec39ef82012-04-23 18:24:01 +020098 }, {
99 .name = "s3c2440-hdmiphy-i2c",
100 .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
Karol Lewandowski27452492012-04-23 18:24:00 +0200101 }, { },
102};
103MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530105#ifdef CONFIG_OF
Karol Lewandowski27452492012-04-23 18:24:00 +0200106static const struct of_device_id s3c24xx_i2c_match[] = {
107 { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
108 { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200109 { .compatible = "samsung,s3c2440-hdmiphy-i2c",
110 .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
Karol Lewandowski27452492012-04-23 18:24:00 +0200111 {},
112};
113MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530114#endif
115
Karol Lewandowski27452492012-04-23 18:24:00 +0200116/* s3c24xx_get_device_quirks
117 *
118 * Get controller type either from device tree or platform device variant.
119*/
120
121static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
122{
123 if (pdev->dev.of_node) {
124 const struct of_device_id *match;
Karol Lewandowskib900ba42012-05-30 11:43:05 +0200125 match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
Karol Lewandowski27452492012-04-23 18:24:00 +0200126 return (unsigned int)match->data;
127 }
128
129 return platform_get_device_id(pdev)->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/* s3c24xx_i2c_master_complete
133 *
134 * complete the message and wake up the caller, using the given return code,
135 * or zero to mean ok.
136*/
137
138static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
139{
140 dev_dbg(i2c->dev, "master_complete %d\n", ret);
141
142 i2c->msg_ptr = 0;
143 i2c->msg = NULL;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000144 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 i2c->msg_num = 0;
146 if (ret)
147 i2c->msg_idx = ret;
148
149 wake_up(&i2c->wait);
150}
151
152static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
153{
154 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 tmp = readl(i2c->regs + S3C2410_IICCON);
157 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
161{
162 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 tmp = readl(i2c->regs + S3C2410_IICCON);
165 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168/* irq enable/disable functions */
169
170static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
171{
172 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 tmp = readl(i2c->regs + S3C2410_IICCON);
175 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
176}
177
178static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
179{
180 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 tmp = readl(i2c->regs + S3C2410_IICCON);
183 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
184}
185
186
187/* s3c24xx_i2c_message_start
188 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000189 * put the start of a message onto the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190*/
191
Ben Dooks3d0911b2008-10-31 16:10:24 +0000192static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct i2c_msg *msg)
194{
195 unsigned int addr = (msg->addr & 0x7f) << 1;
196 unsigned long stat;
197 unsigned long iiccon;
198
199 stat = 0;
200 stat |= S3C2410_IICSTAT_TXRXEN;
201
202 if (msg->flags & I2C_M_RD) {
203 stat |= S3C2410_IICSTAT_MASTER_RX;
204 addr |= 1;
205 } else
206 stat |= S3C2410_IICSTAT_MASTER_TX;
207
208 if (msg->flags & I2C_M_REV_DIR_ADDR)
209 addr ^= 1;
210
Ben Dooks3d0911b2008-10-31 16:10:24 +0000211 /* todo - check for wether ack wanted or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 s3c24xx_i2c_enable_ack(i2c);
213
214 iiccon = readl(i2c->regs + S3C2410_IICCON);
215 writel(stat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
218 writeb(addr, i2c->regs + S3C2410_IICDS);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000219
Ben Dookse00a8cd2007-05-01 23:26:35 +0200220 /* delay here to ensure the data byte has gotten onto the bus
221 * before the transaction is started */
222
223 ndelay(i2c->tx_setup);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
226 writel(iiccon, i2c->regs + S3C2410_IICCON);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000227
228 stat |= S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 writel(stat, i2c->regs + S3C2410_IICSTAT);
230}
231
232static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
233{
234 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
235
236 dev_dbg(i2c->dev, "STOP\n");
237
238 /* stop the transfer */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000239 iicstat &= ~S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 i2c->state = STATE_STOP;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 s3c24xx_i2c_master_complete(i2c, ret);
245 s3c24xx_i2c_disable_irq(i2c);
246}
247
248/* helper functions to determine the current state in the set of
249 * messages we are sending */
250
251/* is_lastmsg()
252 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000253 * returns TRUE if the current message is the last in the set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254*/
255
256static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
257{
258 return i2c->msg_idx >= (i2c->msg_num - 1);
259}
260
261/* is_msglast
262 *
263 * returns TRUE if we this is the last byte in the current message
264*/
265
266static inline int is_msglast(struct s3c24xx_i2c *i2c)
267{
268 return i2c->msg_ptr == i2c->msg->len-1;
269}
270
271/* is_msgend
272 *
273 * returns TRUE if we reached the end of the current message
274*/
275
276static inline int is_msgend(struct s3c24xx_i2c *i2c)
277{
278 return i2c->msg_ptr >= i2c->msg->len;
279}
280
Huisung Kang19820512011-06-23 21:37:33 +0900281/* i2c_s3c_irq_nextbyte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
283 * process an interrupt and work out what to do
284 */
285
Huisung Kang19820512011-06-23 21:37:33 +0900286static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 unsigned long tmp;
289 unsigned char byte;
290 int ret = 0;
291
292 switch (i2c->state) {
293
294 case STATE_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200295 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 case STATE_STOP:
Harvey Harrison08882d22008-04-22 22:16:47 +0200299 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000300 s3c24xx_i2c_disable_irq(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto out_ack;
302
303 case STATE_START:
304 /* last thing we did was send a start condition on the
305 * bus, or started a new i2c message
306 */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000307
Ben Dooks63f5c282008-07-01 11:59:42 +0100308 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
310 /* ack was not received... */
311
312 dev_dbg(i2c->dev, "ack was not received\n");
Ben Dooks63f5c282008-07-01 11:59:42 +0100313 s3c24xx_i2c_stop(i2c, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 goto out_ack;
315 }
316
317 if (i2c->msg->flags & I2C_M_RD)
318 i2c->state = STATE_READ;
319 else
320 i2c->state = STATE_WRITE;
321
322 /* terminate the transfer if there is nothing to do
Ben Dooks63f5c282008-07-01 11:59:42 +0100323 * as this is used by the i2c probe to find devices. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
326 s3c24xx_i2c_stop(i2c, 0);
327 goto out_ack;
328 }
329
330 if (i2c->state == STATE_READ)
331 goto prepare_read;
332
Ben Dooks3d0911b2008-10-31 16:10:24 +0000333 /* fall through to the write state, as we will need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * send a byte as well */
335
336 case STATE_WRITE:
337 /* we are writing data to the device... check for the
338 * end of the message, and if so, work out what to do
339 */
340
Ben Dooks27097812008-07-01 11:59:41 +0100341 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
342 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
343 dev_dbg(i2c->dev, "WRITE: No Ack\n");
344
345 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
346 goto out_ack;
347 }
348 }
349
Ben Dooks3d0911b2008-10-31 16:10:24 +0000350 retry_write:
Ben Dooks27097812008-07-01 11:59:41 +0100351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!is_msgend(i2c)) {
353 byte = i2c->msg->buf[i2c->msg_ptr++];
354 writeb(byte, i2c->regs + S3C2410_IICDS);
Ben Dookse00a8cd2007-05-01 23:26:35 +0200355
356 /* delay after writing the byte to allow the
357 * data setup time on the bus, as writing the
358 * data to the register causes the first bit
359 * to appear on SDA, and SCL will change as
360 * soon as the interrupt is acknowledged */
361
362 ndelay(i2c->tx_setup);
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 } else if (!is_lastmsg(i2c)) {
365 /* we need to go to the next i2c message */
366
367 dev_dbg(i2c->dev, "WRITE: Next Message\n");
368
369 i2c->msg_ptr = 0;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000370 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 i2c->msg++;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* check to see if we need to do another message */
374 if (i2c->msg->flags & I2C_M_NOSTART) {
375
376 if (i2c->msg->flags & I2C_M_RD) {
377 /* cannot do this, the controller
378 * forces us to send a new START
379 * when we change direction */
380
381 s3c24xx_i2c_stop(i2c, -EINVAL);
382 }
383
384 goto retry_write;
385 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* send the new start */
387 s3c24xx_i2c_message_start(i2c, i2c->msg);
388 i2c->state = STATE_START;
389 }
390
391 } else {
392 /* send stop */
393
394 s3c24xx_i2c_stop(i2c, 0);
395 }
396 break;
397
398 case STATE_READ:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000399 /* we have a byte of data in the data register, do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * something with it, and then work out wether we are
401 * going to do any more read/write
402 */
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 byte = readb(i2c->regs + S3C2410_IICDS);
405 i2c->msg->buf[i2c->msg_ptr++] = byte;
406
Ben Dooks3d0911b2008-10-31 16:10:24 +0000407 prepare_read:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (is_msglast(i2c)) {
409 /* last byte of buffer */
410
411 if (is_lastmsg(i2c))
412 s3c24xx_i2c_disable_ack(i2c);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 } else if (is_msgend(i2c)) {
415 /* ok, we've read the entire buffer, see if there
416 * is anything else we need to do */
417
418 if (is_lastmsg(i2c)) {
419 /* last message, send stop and complete */
420 dev_dbg(i2c->dev, "READ: Send Stop\n");
421
422 s3c24xx_i2c_stop(i2c, 0);
423 } else {
424 /* go to the next transfer */
425 dev_dbg(i2c->dev, "READ: Next Transfer\n");
426
427 i2c->msg_ptr = 0;
428 i2c->msg_idx++;
429 i2c->msg++;
430 }
431 }
432
433 break;
434 }
435
436 /* acknowlegde the IRQ and get back on with the work */
437
438 out_ack:
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 out:
443 return ret;
444}
445
446/* s3c24xx_i2c_irq
447 *
448 * top level IRQ servicing routine
449*/
450
David Howells7d12e782006-10-05 14:55:46 +0100451static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct s3c24xx_i2c *i2c = dev_id;
454 unsigned long status;
455 unsigned long tmp;
456
457 status = readl(i2c->regs + S3C2410_IICSTAT);
458
459 if (status & S3C2410_IICSTAT_ARBITR) {
Ben Dooks3d0911b2008-10-31 16:10:24 +0000460 /* deal with arbitration loss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 dev_err(i2c->dev, "deal with arbitration loss\n");
462 }
463
464 if (i2c->state == STATE_IDLE) {
465 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
466
Ben Dooks3d0911b2008-10-31 16:10:24 +0000467 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 tmp &= ~S3C2410_IICCON_IRQPEND;
469 writel(tmp, i2c->regs + S3C2410_IICCON);
470 goto out;
471 }
Ben Dooks3d0911b2008-10-31 16:10:24 +0000472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* pretty much this leaves us with the fact that we've
474 * transmitted or received whatever byte we last sent */
475
Huisung Kang19820512011-06-23 21:37:33 +0900476 i2c_s3c_irq_nextbyte(i2c, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 out:
479 return IRQ_HANDLED;
480}
481
482
483/* s3c24xx_i2c_set_master
484 *
485 * get the i2c bus for a master transaction
486*/
487
488static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
489{
490 unsigned long iicstat;
491 int timeout = 400;
492
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200493 /* the timeout for HDMIPHY is reduced to 10 ms because
494 * the hangup is expected to happen, so waiting 400 ms
495 * causes only unnecessary system hangup
496 */
497 if (i2c->quirks & QUIRK_HDMIPHY)
498 timeout = 10;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 while (timeout-- > 0) {
501 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
504 return 0;
505
506 msleep(1);
507 }
508
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200509 /* hang-up of bus dedicated for HDMIPHY occurred, resetting */
510 if (i2c->quirks & QUIRK_HDMIPHY) {
511 writel(0, i2c->regs + S3C2410_IICCON);
512 writel(0, i2c->regs + S3C2410_IICSTAT);
513 writel(0, i2c->regs + S3C2410_IICDS);
514
515 return 0;
516 }
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -ETIMEDOUT;
519}
520
521/* s3c24xx_i2c_doxfer
522 *
523 * this starts an i2c transfer
524*/
525
Ben Dooks3d0911b2008-10-31 16:10:24 +0000526static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
527 struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Mark Brown1bc29622010-04-02 14:15:09 +0100529 unsigned long iicstat, timeout;
530 int spins = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int ret;
532
Ben Dooksbe44f012008-10-31 16:10:22 +0000533 if (i2c->suspended)
Ben Dooks61c7cff2008-07-28 12:04:07 +0100534 return -EIO;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ret = s3c24xx_i2c_set_master(i2c);
537 if (ret != 0) {
538 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
539 ret = -EAGAIN;
540 goto out;
541 }
542
543 spin_lock_irq(&i2c->lock);
544
545 i2c->msg = msgs;
546 i2c->msg_num = num;
547 i2c->msg_ptr = 0;
548 i2c->msg_idx = 0;
549 i2c->state = STATE_START;
550
551 s3c24xx_i2c_enable_irq(i2c);
552 s3c24xx_i2c_message_start(i2c, msgs);
553 spin_unlock_irq(&i2c->lock);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
556
557 ret = i2c->msg_idx;
558
Ben Dooks3d0911b2008-10-31 16:10:24 +0000559 /* having these next two as dev_err() makes life very
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * noisy when doing an i2cdetect */
561
562 if (timeout == 0)
563 dev_dbg(i2c->dev, "timeout\n");
564 else if (ret != num)
565 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
566
567 /* ensure the stop has been through the bus */
568
Mark Brown1bc29622010-04-02 14:15:09 +0100569 dev_dbg(i2c->dev, "waiting for bus idle\n");
570
571 /* first, try busy waiting briefly */
572 do {
Mark Brown37de03e2011-12-09 20:55:03 +0800573 cpu_relax();
Mark Brown1bc29622010-04-02 14:15:09 +0100574 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
575 } while ((iicstat & S3C2410_IICSTAT_START) && --spins);
576
577 /* if that timed out sleep */
578 if (!spins) {
579 msleep(1);
580 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
581 }
582
583 if (iicstat & S3C2410_IICSTAT_START)
584 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 out:
587 return ret;
588}
589
590/* s3c24xx_i2c_xfer
591 *
592 * first port of call from the i2c bus code when an message needs
Steven Cole44bbe872005-05-03 18:21:25 -0600593 * transferring across the i2c bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594*/
595
596static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
597 struct i2c_msg *msgs, int num)
598{
599 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
600 int retry;
601 int ret;
602
Mark Brownc62c3ca2012-01-21 13:28:47 +0000603 pm_runtime_get_sync(&adap->dev);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900604 clk_prepare_enable(i2c->clk);
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 for (retry = 0; retry < adap->retries; retry++) {
607
608 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
609
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200610 if (ret != -EAGAIN) {
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900611 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100612 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return ret;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
617
618 udelay(100);
619 }
620
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900621 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100622 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EREMOTEIO;
624}
625
626/* declare our i2c functionality */
627static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
628{
Mark Brown14674e72012-05-30 10:55:34 +0200629 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
630 I2C_FUNC_PROTOCOL_MANGLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633/* i2c bus registration info */
634
Jean Delvare8f9082c2006-09-03 22:39:46 +0200635static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 .master_xfer = s3c24xx_i2c_xfer,
637 .functionality = s3c24xx_i2c_func,
638};
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/* s3c24xx_i2c_calcdivisor
641 *
642 * return the divisor settings for a given frequency
643*/
644
645static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
646 unsigned int *div1, unsigned int *divs)
647{
648 unsigned int calc_divs = clkin / wanted;
649 unsigned int calc_div1;
650
651 if (calc_divs > (16*16))
652 calc_div1 = 512;
653 else
654 calc_div1 = 16;
655
656 calc_divs += calc_div1-1;
657 calc_divs /= calc_div1;
658
659 if (calc_divs == 0)
660 calc_divs = 1;
661 if (calc_divs > 17)
662 calc_divs = 17;
663
664 *divs = calc_divs;
665 *div1 = calc_div1;
666
667 return clkin / (calc_divs * calc_div1);
668}
669
Ben Dooks61c7cff2008-07-28 12:04:07 +0100670/* s3c24xx_i2c_clockrate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *
672 * work out a divisor for the user requested frequency setting,
673 * either by the requested frequency, or scanning the acceptable
674 * range of frequencies until something is found
675*/
676
Ben Dooks61c7cff2008-07-28 12:04:07 +0100677static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530679 struct s3c2410_platform_i2c *pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 unsigned long clkin = clk_get_rate(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 unsigned int divs, div1;
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000682 unsigned long target_frequency;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100683 u32 iiccon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Ben Dooks61c7cff2008-07-28 12:04:07 +0100686 i2c->clkrate = clkin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 clkin /= 1000; /* clkin now in KHz */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000688
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000689 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000691 target_frequency = pdata->frequency ? pdata->frequency : 100000;
692
693 target_frequency /= 1000; /* Target frequency now in KHz */
694
695 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
696
697 if (freq > target_frequency) {
698 dev_err(i2c->dev,
699 "Unable to achieve desired frequency %luKHz." \
700 " Lowest achievable %dKHz\n", target_frequency, freq);
701 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *got = freq;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100705
706 iiccon = readl(i2c->regs + S3C2410_IICCON);
707 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
708 iiccon |= (divs-1);
709
710 if (div1 == 512)
711 iiccon |= S3C2410_IICCON_TXDIV_512;
712
713 writel(iiccon, i2c->regs + S3C2410_IICCON);
714
Karol Lewandowski27452492012-04-23 18:24:00 +0200715 if (i2c->quirks & QUIRK_S3C2440) {
Ben Dooksa192f712009-03-27 10:52:13 +0000716 unsigned long sda_delay;
717
718 if (pdata->sda_delay) {
MyungJoo Ham70313072010-09-30 15:54:46 +0200719 sda_delay = clkin * pdata->sda_delay;
720 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
Ben Dooksa192f712009-03-27 10:52:13 +0000721 sda_delay = DIV_ROUND_UP(sda_delay, 5);
722 if (sda_delay > 3)
723 sda_delay = 3;
724 sda_delay |= S3C2410_IICLC_FILTER_ON;
725 } else
726 sda_delay = 0;
727
728 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
729 writel(sda_delay, i2c->regs + S3C2440_IICLC);
730 }
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 0;
733}
734
Ben Dooks61c7cff2008-07-28 12:04:07 +0100735#ifdef CONFIG_CPU_FREQ
736
737#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
738
739static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
740 unsigned long val, void *data)
741{
742 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
743 unsigned long flags;
744 unsigned int got;
745 int delta_f;
746 int ret;
747
748 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
749
750 /* if we're post-change and the input clock has slowed down
751 * or at pre-change and the clock is about to speed up, then
752 * adjust our clock rate. <0 is slow, >0 speedup.
753 */
754
755 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
756 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
757 spin_lock_irqsave(&i2c->lock, flags);
758 ret = s3c24xx_i2c_clockrate(i2c, &got);
759 spin_unlock_irqrestore(&i2c->lock, flags);
760
761 if (ret < 0)
762 dev_err(i2c->dev, "cannot find frequency\n");
763 else
764 dev_info(i2c->dev, "setting freq %d\n", got);
765 }
766
767 return 0;
768}
769
770static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
771{
772 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
773
774 return cpufreq_register_notifier(&i2c->freq_transition,
775 CPUFREQ_TRANSITION_NOTIFIER);
776}
777
778static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
779{
780 cpufreq_unregister_notifier(&i2c->freq_transition,
781 CPUFREQ_TRANSITION_NOTIFIER);
782}
783
784#else
785static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
786{
787 return 0;
788}
789
790static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
791{
792}
793#endif
794
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530795#ifdef CONFIG_OF
796static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
797{
798 int idx, gpio, ret;
799
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200800 if (i2c->quirks & QUIRK_NO_GPIO)
801 return 0;
802
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530803 for (idx = 0; idx < 2; idx++) {
804 gpio = of_get_gpio(i2c->dev->of_node, idx);
805 if (!gpio_is_valid(gpio)) {
806 dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
807 goto free_gpio;
808 }
809
810 ret = gpio_request(gpio, "i2c-bus");
811 if (ret) {
812 dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
813 goto free_gpio;
814 }
815 }
816 return 0;
817
818free_gpio:
819 while (--idx >= 0)
820 gpio_free(i2c->gpios[idx]);
821 return -EINVAL;
822}
823
824static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
825{
826 unsigned int idx;
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200827
828 if (i2c->quirks & QUIRK_NO_GPIO)
829 return;
830
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530831 for (idx = 0; idx < 2; idx++)
832 gpio_free(i2c->gpios[idx]);
833}
834#else
835static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
836{
Tushar Behera8ebe6612011-12-09 15:33:55 +0530837 return 0;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530838}
839
840static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
841{
842}
843#endif
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/* s3c24xx_i2c_init
846 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000847 * initialise the controller, set the IO lines and frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848*/
849
850static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
851{
852 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
853 struct s3c2410_platform_i2c *pdata;
854 unsigned int freq;
855
856 /* get the plafrom data */
857
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530858 pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* inititalise the gpio */
861
Ben Dooks8be310a2008-10-31 16:10:25 +0000862 if (pdata->cfg_gpio)
863 pdata->cfg_gpio(to_platform_device(i2c->dev));
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530864 else
865 if (s3c24xx_i2c_parse_dt_gpio(i2c))
866 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /* write slave address */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
871
872 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
873
Ben Dooks61c7cff2008-07-28 12:04:07 +0100874 writel(iicon, i2c->regs + S3C2410_IICCON);
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* we need to work out the divisors for the clock... */
877
Ben Dooks61c7cff2008-07-28 12:04:07 +0100878 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
879 writel(0, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 dev_err(i2c->dev, "cannot meet bus frequency required\n");
881 return -EINVAL;
882 }
883
884 /* todo - check that the i2c lines aren't being dragged anywhere */
885
886 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
887 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return 0;
890}
891
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530892#ifdef CONFIG_OF
893/* s3c24xx_i2c_parse_dt
894 *
895 * Parse the device tree node and retreive the platform data.
896*/
897
898static void
899s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
900{
901 struct s3c2410_platform_i2c *pdata = i2c->pdata;
902
903 if (!np)
904 return;
905
906 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
907 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
908 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
909 of_property_read_u32(np, "samsung,i2c-max-bus-freq",
910 (u32 *)&pdata->frequency);
911}
912#else
913static void
914s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
915{
916 return;
917}
918#endif
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/* s3c24xx_i2c_probe
921 *
922 * called by the bus driver when a suitable device is found
923*/
924
Russell King3ae5eae2005-11-09 22:32:44 +0000925static int s3c24xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Ben Dooks692acbd2008-10-31 16:10:28 +0000927 struct s3c24xx_i2c *i2c;
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530928 struct s3c2410_platform_i2c *pdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct resource *res;
930 int ret;
931
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530932 if (!pdev->dev.of_node) {
933 pdata = pdev->dev.platform_data;
934 if (!pdata) {
935 dev_err(&pdev->dev, "no platform data\n");
936 return -EINVAL;
937 }
Ben Dooks6a039ca2008-10-31 16:10:27 +0000938 }
Ben Dooks399dee22008-07-28 12:04:06 +0100939
Mark Brown4ea15572012-01-21 13:28:46 +0000940 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
Ben Dooks692acbd2008-10-31 16:10:28 +0000941 if (!i2c) {
942 dev_err(&pdev->dev, "no memory for state\n");
943 return -ENOMEM;
944 }
945
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530946 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
947 if (!i2c->pdata) {
948 ret = -ENOMEM;
949 goto err_noclk;
950 }
951
Karol Lewandowski27452492012-04-23 18:24:00 +0200952 i2c->quirks = s3c24xx_get_device_quirks(pdev);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530953 if (pdata)
954 memcpy(i2c->pdata, pdata, sizeof(*pdata));
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530955 else
956 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530957
Ben Dooks692acbd2008-10-31 16:10:28 +0000958 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
959 i2c->adap.owner = THIS_MODULE;
960 i2c->adap.algo = &s3c24xx_i2c_algorithm;
961 i2c->adap.retries = 2;
962 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
963 i2c->tx_setup = 50;
964
965 spin_lock_init(&i2c->lock);
966 init_waitqueue_head(&i2c->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /* find the clock and enable it */
969
Russell King3ae5eae2005-11-09 22:32:44 +0000970 i2c->dev = &pdev->dev;
971 i2c->clk = clk_get(&pdev->dev, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (IS_ERR(i2c->clk)) {
Russell King3ae5eae2005-11-09 22:32:44 +0000973 dev_err(&pdev->dev, "cannot get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +0200975 goto err_noclk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
Russell King3ae5eae2005-11-09 22:32:44 +0000978 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900980 clk_prepare_enable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /* map the registers */
983
984 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
985 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000986 dev_err(&pdev->dev, "cannot find IO resource\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +0200988 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
Ben Dooks933a2ae2009-06-14 14:04:20 +0100991 i2c->ioarea = request_mem_region(res->start, resource_size(res),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 pdev->name);
993
994 if (i2c->ioarea == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000995 dev_err(&pdev->dev, "cannot request IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ret = -ENXIO;
Ben Dooks5b687902007-05-01 23:26:35 +0200997 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
Ben Dooks933a2ae2009-06-14 14:04:20 +01001000 i2c->regs = ioremap(res->start, resource_size(res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (i2c->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001003 dev_err(&pdev->dev, "cannot map IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 ret = -ENXIO;
Ben Dooks5b687902007-05-01 23:26:35 +02001005 goto err_ioarea;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
Ben Dooks3d0911b2008-10-31 16:10:24 +00001008 dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
1009 i2c->regs, i2c->ioarea, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /* setup info block for the i2c core */
1012
1013 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001014 i2c->adap.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /* initialise the i2c controller */
1017
1018 ret = s3c24xx_i2c_init(i2c);
1019 if (ret != 0)
Ben Dooks5b687902007-05-01 23:26:35 +02001020 goto err_iomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /* find the IRQ for this unit (note, this relies on the init call to
Ben Dooks3d0911b2008-10-31 16:10:24 +00001023 * ensure no current IRQs pending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
1025
Ben Dookse0d1ec92008-10-31 16:10:30 +00001026 i2c->irq = ret = platform_get_irq(pdev, 0);
1027 if (ret <= 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001028 dev_err(&pdev->dev, "cannot find IRQ\n");
Ben Dooks5b687902007-05-01 23:26:35 +02001029 goto err_iomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
Yong Zhang43110512011-09-21 17:28:33 +08001032 ret = request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
Ben Dookse0d1ec92008-10-31 16:10:30 +00001033 dev_name(&pdev->dev), i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (ret != 0) {
Ben Dookse0d1ec92008-10-31 16:10:30 +00001036 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
Ben Dooks5b687902007-05-01 23:26:35 +02001037 goto err_iomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
Ben Dooks61c7cff2008-07-28 12:04:07 +01001040 ret = s3c24xx_i2c_register_cpufreq(i2c);
1041 if (ret < 0) {
1042 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
1043 goto err_irq;
1044 }
1045
Ben Dooks399dee22008-07-28 12:04:06 +01001046 /* Note, previous versions of the driver used i2c_add_adapter()
1047 * to add the bus at any number. We now pass the bus number via
1048 * the platform data, so if unset it will now default to always
1049 * being bus 0.
1050 */
1051
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301052 i2c->adap.nr = i2c->pdata->bus_num;
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301053 i2c->adap.dev.of_node = pdev->dev.of_node;
Ben Dooks399dee22008-07-28 12:04:06 +01001054
1055 ret = i2c_add_numbered_adapter(&i2c->adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001057 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
Ben Dooks61c7cff2008-07-28 12:04:07 +01001058 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301061 of_i2c_register_devices(&i2c->adap);
Russell King3ae5eae2005-11-09 22:32:44 +00001062 platform_set_drvdata(pdev, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Mark Brownc62c3ca2012-01-21 13:28:47 +00001064 pm_runtime_enable(&pdev->dev);
1065 pm_runtime_enable(&i2c->adap.dev);
1066
Jean Delvare22e965c2009-01-07 14:29:16 +01001067 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001068 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Ben Dooks61c7cff2008-07-28 12:04:07 +01001071 err_cpufreq:
1072 s3c24xx_i2c_deregister_cpufreq(i2c);
1073
Ben Dooks5b687902007-05-01 23:26:35 +02001074 err_irq:
Ben Dookse0d1ec92008-10-31 16:10:30 +00001075 free_irq(i2c->irq, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Ben Dooks5b687902007-05-01 23:26:35 +02001077 err_iomap:
1078 iounmap(i2c->regs);
1079
1080 err_ioarea:
1081 release_resource(i2c->ioarea);
1082 kfree(i2c->ioarea);
1083
1084 err_clk:
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001085 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001086 clk_put(i2c->clk);
1087
1088 err_noclk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return ret;
1090}
1091
1092/* s3c24xx_i2c_remove
1093 *
1094 * called when device is removed from the bus
1095*/
1096
Russell King3ae5eae2005-11-09 22:32:44 +00001097static int s3c24xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Russell King3ae5eae2005-11-09 22:32:44 +00001099 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Ben Dooks5b687902007-05-01 23:26:35 +02001100
Mark Brownc62c3ca2012-01-21 13:28:47 +00001101 pm_runtime_disable(&i2c->adap.dev);
1102 pm_runtime_disable(&pdev->dev);
1103
Ben Dooks61c7cff2008-07-28 12:04:07 +01001104 s3c24xx_i2c_deregister_cpufreq(i2c);
1105
Ben Dooks5b687902007-05-01 23:26:35 +02001106 i2c_del_adapter(&i2c->adap);
Ben Dookse0d1ec92008-10-31 16:10:30 +00001107 free_irq(i2c->irq, i2c);
Ben Dooks5b687902007-05-01 23:26:35 +02001108
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001109 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001110 clk_put(i2c->clk);
1111
1112 iounmap(i2c->regs);
1113
1114 release_resource(i2c->ioarea);
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301115 s3c24xx_i2c_dt_gpio_free(i2c);
Ben Dooks5b687902007-05-01 23:26:35 +02001116 kfree(i2c->ioarea);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 return 0;
1119}
1120
1121#ifdef CONFIG_PM
Magnus Damm6a6c61892009-07-08 13:22:47 +02001122static int s3c24xx_i2c_suspend_noirq(struct device *dev)
Ben Dooksbe44f012008-10-31 16:10:22 +00001123{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001124 struct platform_device *pdev = to_platform_device(dev);
1125 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1126
Ben Dooksbe44f012008-10-31 16:10:22 +00001127 i2c->suspended = 1;
Magnus Damm6a6c61892009-07-08 13:22:47 +02001128
Ben Dooksbe44f012008-10-31 16:10:22 +00001129 return 0;
1130}
1131
Magnus Damm6a6c61892009-07-08 13:22:47 +02001132static int s3c24xx_i2c_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001134 struct platform_device *pdev = to_platform_device(dev);
1135 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Russell King9480e302005-10-28 09:52:56 -07001136
Ben Dooksbe44f012008-10-31 16:10:22 +00001137 i2c->suspended = 0;
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001138 clk_prepare_enable(i2c->clk);
Ben Dooksbe44f012008-10-31 16:10:22 +00001139 s3c24xx_i2c_init(i2c);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001140 clk_disable_unprepare(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 return 0;
1143}
1144
Alexey Dobriyan47145212009-12-14 18:00:08 -08001145static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
Magnus Damm6a6c61892009-07-08 13:22:47 +02001146 .suspend_noirq = s3c24xx_i2c_suspend_noirq,
1147 .resume = s3c24xx_i2c_resume,
1148};
1149
1150#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151#else
Magnus Damm6a6c61892009-07-08 13:22:47 +02001152#define S3C24XX_DEV_PM_OPS NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153#endif
1154
1155/* device driver for platform bus bits */
1156
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001157static struct platform_driver s3c24xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 .probe = s3c24xx_i2c_probe,
1159 .remove = s3c24xx_i2c_remove,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001160 .id_table = s3c24xx_driver_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001161 .driver = {
1162 .owner = THIS_MODULE,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001163 .name = "s3c-i2c",
Magnus Damm6a6c61892009-07-08 13:22:47 +02001164 .pm = S3C24XX_DEV_PM_OPS,
Karol Lewandowski9df7ead2012-03-21 20:11:51 +01001165 .of_match_table = of_match_ptr(s3c24xx_i2c_match),
Russell King3ae5eae2005-11-09 22:32:44 +00001166 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167};
1168
1169static int __init i2c_adap_s3c_init(void)
1170{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001171 return platform_driver_register(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
Mark Brown18dc83a2009-02-26 16:29:22 +00001173subsys_initcall(i2c_adap_s3c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175static void __exit i2c_adap_s3c_exit(void)
1176{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001177 platform_driver_unregister(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179module_exit(i2c_adap_s3c_exit);
1180
1181MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1182MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1183MODULE_LICENSE("GPL");