blob: f82d11f9b8f2ceb30e79e0a148e64431f36c31ac [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 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
Karol Lewandowski27452492012-04-23 18:24:00 +020090static struct platform_device_id s3c24xx_driver_ids[] = {
91 {
92 .name = "s3c2410-i2c",
93 .driver_data = 0,
94 }, {
95 .name = "s3c2440-i2c",
96 .driver_data = QUIRK_S3C2440,
Karol Lewandowskiec39ef82012-04-23 18:24:01 +020097 }, {
98 .name = "s3c2440-hdmiphy-i2c",
99 .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
Karol Lewandowski27452492012-04-23 18:24:00 +0200100 }, { },
101};
102MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530104#ifdef CONFIG_OF
Karol Lewandowski27452492012-04-23 18:24:00 +0200105static const struct of_device_id s3c24xx_i2c_match[] = {
106 { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
107 { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200108 { .compatible = "samsung,s3c2440-hdmiphy-i2c",
109 .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
Karol Lewandowski27452492012-04-23 18:24:00 +0200110 {},
111};
112MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530113#endif
114
Karol Lewandowski27452492012-04-23 18:24:00 +0200115/* s3c24xx_get_device_quirks
116 *
117 * Get controller type either from device tree or platform device variant.
118*/
119
120static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
121{
122 if (pdev->dev.of_node) {
123 const struct of_device_id *match;
Karol Lewandowskib900ba42012-05-30 11:43:05 +0200124 match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
Karol Lewandowski27452492012-04-23 18:24:00 +0200125 return (unsigned int)match->data;
126 }
127
128 return platform_get_device_id(pdev)->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* s3c24xx_i2c_master_complete
132 *
133 * complete the message and wake up the caller, using the given return code,
134 * or zero to mean ok.
135*/
136
137static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
138{
139 dev_dbg(i2c->dev, "master_complete %d\n", ret);
140
141 i2c->msg_ptr = 0;
142 i2c->msg = NULL;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000143 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 i2c->msg_num = 0;
145 if (ret)
146 i2c->msg_idx = ret;
147
148 wake_up(&i2c->wait);
149}
150
151static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
152{
153 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 tmp = readl(i2c->regs + S3C2410_IICCON);
156 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
160{
161 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 tmp = readl(i2c->regs + S3C2410_IICCON);
164 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167/* irq enable/disable functions */
168
169static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
170{
171 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 tmp = readl(i2c->regs + S3C2410_IICCON);
174 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
175}
176
177static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
178{
179 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 tmp = readl(i2c->regs + S3C2410_IICCON);
182 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
183}
184
185
186/* s3c24xx_i2c_message_start
187 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000188 * put the start of a message onto the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189*/
190
Ben Dooks3d0911b2008-10-31 16:10:24 +0000191static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct i2c_msg *msg)
193{
194 unsigned int addr = (msg->addr & 0x7f) << 1;
195 unsigned long stat;
196 unsigned long iiccon;
197
198 stat = 0;
199 stat |= S3C2410_IICSTAT_TXRXEN;
200
201 if (msg->flags & I2C_M_RD) {
202 stat |= S3C2410_IICSTAT_MASTER_RX;
203 addr |= 1;
204 } else
205 stat |= S3C2410_IICSTAT_MASTER_TX;
206
207 if (msg->flags & I2C_M_REV_DIR_ADDR)
208 addr ^= 1;
209
Ben Dooks3d0911b2008-10-31 16:10:24 +0000210 /* todo - check for wether ack wanted or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 s3c24xx_i2c_enable_ack(i2c);
212
213 iiccon = readl(i2c->regs + S3C2410_IICCON);
214 writel(stat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
217 writeb(addr, i2c->regs + S3C2410_IICDS);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000218
Ben Dookse00a8cd2007-05-01 23:26:35 +0200219 /* delay here to ensure the data byte has gotten onto the bus
220 * before the transaction is started */
221
222 ndelay(i2c->tx_setup);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
225 writel(iiccon, i2c->regs + S3C2410_IICCON);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000226
227 stat |= S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 writel(stat, i2c->regs + S3C2410_IICSTAT);
229}
230
231static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
232{
233 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
234
235 dev_dbg(i2c->dev, "STOP\n");
236
237 /* stop the transfer */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000238 iicstat &= ~S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 i2c->state = STATE_STOP;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 s3c24xx_i2c_master_complete(i2c, ret);
244 s3c24xx_i2c_disable_irq(i2c);
245}
246
247/* helper functions to determine the current state in the set of
248 * messages we are sending */
249
250/* is_lastmsg()
251 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000252 * returns TRUE if the current message is the last in the set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253*/
254
255static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
256{
257 return i2c->msg_idx >= (i2c->msg_num - 1);
258}
259
260/* is_msglast
261 *
262 * returns TRUE if we this is the last byte in the current message
263*/
264
265static inline int is_msglast(struct s3c24xx_i2c *i2c)
266{
267 return i2c->msg_ptr == i2c->msg->len-1;
268}
269
270/* is_msgend
271 *
272 * returns TRUE if we reached the end of the current message
273*/
274
275static inline int is_msgend(struct s3c24xx_i2c *i2c)
276{
277 return i2c->msg_ptr >= i2c->msg->len;
278}
279
Huisung Kang19820512011-06-23 21:37:33 +0900280/* i2c_s3c_irq_nextbyte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 *
282 * process an interrupt and work out what to do
283 */
284
Huisung Kang19820512011-06-23 21:37:33 +0900285static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 unsigned long tmp;
288 unsigned char byte;
289 int ret = 0;
290
291 switch (i2c->state) {
292
293 case STATE_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200294 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 case STATE_STOP:
Harvey Harrison08882d22008-04-22 22:16:47 +0200298 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000299 s3c24xx_i2c_disable_irq(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto out_ack;
301
302 case STATE_START:
303 /* last thing we did was send a start condition on the
304 * bus, or started a new i2c message
305 */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000306
Ben Dooks63f5c282008-07-01 11:59:42 +0100307 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
309 /* ack was not received... */
310
311 dev_dbg(i2c->dev, "ack was not received\n");
Ben Dooks63f5c282008-07-01 11:59:42 +0100312 s3c24xx_i2c_stop(i2c, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto out_ack;
314 }
315
316 if (i2c->msg->flags & I2C_M_RD)
317 i2c->state = STATE_READ;
318 else
319 i2c->state = STATE_WRITE;
320
321 /* terminate the transfer if there is nothing to do
Ben Dooks63f5c282008-07-01 11:59:42 +0100322 * as this is used by the i2c probe to find devices. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
325 s3c24xx_i2c_stop(i2c, 0);
326 goto out_ack;
327 }
328
329 if (i2c->state == STATE_READ)
330 goto prepare_read;
331
Ben Dooks3d0911b2008-10-31 16:10:24 +0000332 /* fall through to the write state, as we will need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * send a byte as well */
334
335 case STATE_WRITE:
336 /* we are writing data to the device... check for the
337 * end of the message, and if so, work out what to do
338 */
339
Ben Dooks27097812008-07-01 11:59:41 +0100340 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
341 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
342 dev_dbg(i2c->dev, "WRITE: No Ack\n");
343
344 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
345 goto out_ack;
346 }
347 }
348
Ben Dooks3d0911b2008-10-31 16:10:24 +0000349 retry_write:
Ben Dooks27097812008-07-01 11:59:41 +0100350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!is_msgend(i2c)) {
352 byte = i2c->msg->buf[i2c->msg_ptr++];
353 writeb(byte, i2c->regs + S3C2410_IICDS);
Ben Dookse00a8cd2007-05-01 23:26:35 +0200354
355 /* delay after writing the byte to allow the
356 * data setup time on the bus, as writing the
357 * data to the register causes the first bit
358 * to appear on SDA, and SCL will change as
359 * soon as the interrupt is acknowledged */
360
361 ndelay(i2c->tx_setup);
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 } else if (!is_lastmsg(i2c)) {
364 /* we need to go to the next i2c message */
365
366 dev_dbg(i2c->dev, "WRITE: Next Message\n");
367
368 i2c->msg_ptr = 0;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000369 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 i2c->msg++;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* check to see if we need to do another message */
373 if (i2c->msg->flags & I2C_M_NOSTART) {
374
375 if (i2c->msg->flags & I2C_M_RD) {
376 /* cannot do this, the controller
377 * forces us to send a new START
378 * when we change direction */
379
380 s3c24xx_i2c_stop(i2c, -EINVAL);
381 }
382
383 goto retry_write;
384 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* send the new start */
386 s3c24xx_i2c_message_start(i2c, i2c->msg);
387 i2c->state = STATE_START;
388 }
389
390 } else {
391 /* send stop */
392
393 s3c24xx_i2c_stop(i2c, 0);
394 }
395 break;
396
397 case STATE_READ:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000398 /* we have a byte of data in the data register, do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * something with it, and then work out wether we are
400 * going to do any more read/write
401 */
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 byte = readb(i2c->regs + S3C2410_IICDS);
404 i2c->msg->buf[i2c->msg_ptr++] = byte;
405
Ben Dooks3d0911b2008-10-31 16:10:24 +0000406 prepare_read:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (is_msglast(i2c)) {
408 /* last byte of buffer */
409
410 if (is_lastmsg(i2c))
411 s3c24xx_i2c_disable_ack(i2c);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 } else if (is_msgend(i2c)) {
414 /* ok, we've read the entire buffer, see if there
415 * is anything else we need to do */
416
417 if (is_lastmsg(i2c)) {
418 /* last message, send stop and complete */
419 dev_dbg(i2c->dev, "READ: Send Stop\n");
420
421 s3c24xx_i2c_stop(i2c, 0);
422 } else {
423 /* go to the next transfer */
424 dev_dbg(i2c->dev, "READ: Next Transfer\n");
425
426 i2c->msg_ptr = 0;
427 i2c->msg_idx++;
428 i2c->msg++;
429 }
430 }
431
432 break;
433 }
434
435 /* acknowlegde the IRQ and get back on with the work */
436
437 out_ack:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000438 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 tmp &= ~S3C2410_IICCON_IRQPEND;
440 writel(tmp, i2c->regs + S3C2410_IICCON);
441 out:
442 return ret;
443}
444
445/* s3c24xx_i2c_irq
446 *
447 * top level IRQ servicing routine
448*/
449
David Howells7d12e782006-10-05 14:55:46 +0100450static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct s3c24xx_i2c *i2c = dev_id;
453 unsigned long status;
454 unsigned long tmp;
455
456 status = readl(i2c->regs + S3C2410_IICSTAT);
457
458 if (status & S3C2410_IICSTAT_ARBITR) {
Ben Dooks3d0911b2008-10-31 16:10:24 +0000459 /* deal with arbitration loss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 dev_err(i2c->dev, "deal with arbitration loss\n");
461 }
462
463 if (i2c->state == STATE_IDLE) {
464 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
465
Ben Dooks3d0911b2008-10-31 16:10:24 +0000466 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 tmp &= ~S3C2410_IICCON_IRQPEND;
468 writel(tmp, i2c->regs + S3C2410_IICCON);
469 goto out;
470 }
Ben Dooks3d0911b2008-10-31 16:10:24 +0000471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* pretty much this leaves us with the fact that we've
473 * transmitted or received whatever byte we last sent */
474
Huisung Kang19820512011-06-23 21:37:33 +0900475 i2c_s3c_irq_nextbyte(i2c, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 out:
478 return IRQ_HANDLED;
479}
480
481
482/* s3c24xx_i2c_set_master
483 *
484 * get the i2c bus for a master transaction
485*/
486
487static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
488{
489 unsigned long iicstat;
490 int timeout = 400;
491
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200492 /* the timeout for HDMIPHY is reduced to 10 ms because
493 * the hangup is expected to happen, so waiting 400 ms
494 * causes only unnecessary system hangup
495 */
496 if (i2c->quirks & QUIRK_HDMIPHY)
497 timeout = 10;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 while (timeout-- > 0) {
500 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
503 return 0;
504
505 msleep(1);
506 }
507
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200508 /* hang-up of bus dedicated for HDMIPHY occurred, resetting */
509 if (i2c->quirks & QUIRK_HDMIPHY) {
510 writel(0, i2c->regs + S3C2410_IICCON);
511 writel(0, i2c->regs + S3C2410_IICSTAT);
512 writel(0, i2c->regs + S3C2410_IICDS);
513
514 return 0;
515 }
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -ETIMEDOUT;
518}
519
520/* s3c24xx_i2c_doxfer
521 *
522 * this starts an i2c transfer
523*/
524
Ben Dooks3d0911b2008-10-31 16:10:24 +0000525static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
526 struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Mark Brown1bc29622010-04-02 14:15:09 +0100528 unsigned long iicstat, timeout;
529 int spins = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int ret;
531
Ben Dooksbe44f012008-10-31 16:10:22 +0000532 if (i2c->suspended)
Ben Dooks61c7cff2008-07-28 12:04:07 +0100533 return -EIO;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ret = s3c24xx_i2c_set_master(i2c);
536 if (ret != 0) {
537 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
538 ret = -EAGAIN;
539 goto out;
540 }
541
542 spin_lock_irq(&i2c->lock);
543
544 i2c->msg = msgs;
545 i2c->msg_num = num;
546 i2c->msg_ptr = 0;
547 i2c->msg_idx = 0;
548 i2c->state = STATE_START;
549
550 s3c24xx_i2c_enable_irq(i2c);
551 s3c24xx_i2c_message_start(i2c, msgs);
552 spin_unlock_irq(&i2c->lock);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
555
556 ret = i2c->msg_idx;
557
Ben Dooks3d0911b2008-10-31 16:10:24 +0000558 /* having these next two as dev_err() makes life very
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * noisy when doing an i2cdetect */
560
561 if (timeout == 0)
562 dev_dbg(i2c->dev, "timeout\n");
563 else if (ret != num)
564 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
565
566 /* ensure the stop has been through the bus */
567
Mark Brown1bc29622010-04-02 14:15:09 +0100568 dev_dbg(i2c->dev, "waiting for bus idle\n");
569
570 /* first, try busy waiting briefly */
571 do {
Mark Brown37de03e2011-12-09 20:55:03 +0800572 cpu_relax();
Mark Brown1bc29622010-04-02 14:15:09 +0100573 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
574 } while ((iicstat & S3C2410_IICSTAT_START) && --spins);
575
576 /* if that timed out sleep */
577 if (!spins) {
578 msleep(1);
579 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
580 }
581
582 if (iicstat & S3C2410_IICSTAT_START)
583 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 out:
586 return ret;
587}
588
589/* s3c24xx_i2c_xfer
590 *
591 * first port of call from the i2c bus code when an message needs
Steven Cole44bbe872005-05-03 18:21:25 -0600592 * transferring across the i2c bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593*/
594
595static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
596 struct i2c_msg *msgs, int num)
597{
598 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
599 int retry;
600 int ret;
601
Mark Brownc62c3ca2012-01-21 13:28:47 +0000602 pm_runtime_get_sync(&adap->dev);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900603 clk_prepare_enable(i2c->clk);
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 for (retry = 0; retry < adap->retries; retry++) {
606
607 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
608
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200609 if (ret != -EAGAIN) {
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900610 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100611 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return ret;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
616
617 udelay(100);
618 }
619
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900620 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100621 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -EREMOTEIO;
623}
624
625/* declare our i2c functionality */
626static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
627{
Mark Brown14674e72012-05-30 10:55:34 +0200628 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
629 I2C_FUNC_PROTOCOL_MANGLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632/* i2c bus registration info */
633
Jean Delvare8f9082c2006-09-03 22:39:46 +0200634static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 .master_xfer = s3c24xx_i2c_xfer,
636 .functionality = s3c24xx_i2c_func,
637};
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/* s3c24xx_i2c_calcdivisor
640 *
641 * return the divisor settings for a given frequency
642*/
643
644static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
645 unsigned int *div1, unsigned int *divs)
646{
647 unsigned int calc_divs = clkin / wanted;
648 unsigned int calc_div1;
649
650 if (calc_divs > (16*16))
651 calc_div1 = 512;
652 else
653 calc_div1 = 16;
654
655 calc_divs += calc_div1-1;
656 calc_divs /= calc_div1;
657
658 if (calc_divs == 0)
659 calc_divs = 1;
660 if (calc_divs > 17)
661 calc_divs = 17;
662
663 *divs = calc_divs;
664 *div1 = calc_div1;
665
666 return clkin / (calc_divs * calc_div1);
667}
668
Ben Dooks61c7cff2008-07-28 12:04:07 +0100669/* s3c24xx_i2c_clockrate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 *
671 * work out a divisor for the user requested frequency setting,
672 * either by the requested frequency, or scanning the acceptable
673 * range of frequencies until something is found
674*/
675
Ben Dooks61c7cff2008-07-28 12:04:07 +0100676static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530678 struct s3c2410_platform_i2c *pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned long clkin = clk_get_rate(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 unsigned int divs, div1;
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000681 unsigned long target_frequency;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100682 u32 iiccon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Ben Dooks61c7cff2008-07-28 12:04:07 +0100685 i2c->clkrate = clkin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 clkin /= 1000; /* clkin now in KHz */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000687
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000688 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000690 target_frequency = pdata->frequency ? pdata->frequency : 100000;
691
692 target_frequency /= 1000; /* Target frequency now in KHz */
693
694 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
695
696 if (freq > target_frequency) {
697 dev_err(i2c->dev,
698 "Unable to achieve desired frequency %luKHz." \
699 " Lowest achievable %dKHz\n", target_frequency, freq);
700 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *got = freq;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100704
705 iiccon = readl(i2c->regs + S3C2410_IICCON);
706 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
707 iiccon |= (divs-1);
708
709 if (div1 == 512)
710 iiccon |= S3C2410_IICCON_TXDIV_512;
711
712 writel(iiccon, i2c->regs + S3C2410_IICCON);
713
Karol Lewandowski27452492012-04-23 18:24:00 +0200714 if (i2c->quirks & QUIRK_S3C2440) {
Ben Dooksa192f712009-03-27 10:52:13 +0000715 unsigned long sda_delay;
716
717 if (pdata->sda_delay) {
MyungJoo Ham7031307a2010-09-30 15:54:46 +0200718 sda_delay = clkin * pdata->sda_delay;
719 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
Ben Dooksa192f712009-03-27 10:52:13 +0000720 sda_delay = DIV_ROUND_UP(sda_delay, 5);
721 if (sda_delay > 3)
722 sda_delay = 3;
723 sda_delay |= S3C2410_IICLC_FILTER_ON;
724 } else
725 sda_delay = 0;
726
727 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
728 writel(sda_delay, i2c->regs + S3C2440_IICLC);
729 }
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return 0;
732}
733
Ben Dooks61c7cff2008-07-28 12:04:07 +0100734#ifdef CONFIG_CPU_FREQ
735
736#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
737
738static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
739 unsigned long val, void *data)
740{
741 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
742 unsigned long flags;
743 unsigned int got;
744 int delta_f;
745 int ret;
746
747 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
748
749 /* if we're post-change and the input clock has slowed down
750 * or at pre-change and the clock is about to speed up, then
751 * adjust our clock rate. <0 is slow, >0 speedup.
752 */
753
754 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
755 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
756 spin_lock_irqsave(&i2c->lock, flags);
757 ret = s3c24xx_i2c_clockrate(i2c, &got);
758 spin_unlock_irqrestore(&i2c->lock, flags);
759
760 if (ret < 0)
761 dev_err(i2c->dev, "cannot find frequency\n");
762 else
763 dev_info(i2c->dev, "setting freq %d\n", got);
764 }
765
766 return 0;
767}
768
769static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
770{
771 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
772
773 return cpufreq_register_notifier(&i2c->freq_transition,
774 CPUFREQ_TRANSITION_NOTIFIER);
775}
776
777static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
778{
779 cpufreq_unregister_notifier(&i2c->freq_transition,
780 CPUFREQ_TRANSITION_NOTIFIER);
781}
782
783#else
784static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
785{
786 return 0;
787}
788
789static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
790{
791}
792#endif
793
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530794#ifdef CONFIG_OF
795static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
796{
797 int idx, gpio, ret;
798
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200799 if (i2c->quirks & QUIRK_NO_GPIO)
800 return 0;
801
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530802 for (idx = 0; idx < 2; idx++) {
803 gpio = of_get_gpio(i2c->dev->of_node, idx);
804 if (!gpio_is_valid(gpio)) {
805 dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
806 goto free_gpio;
807 }
808
809 ret = gpio_request(gpio, "i2c-bus");
810 if (ret) {
811 dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
812 goto free_gpio;
813 }
814 }
815 return 0;
816
817free_gpio:
818 while (--idx >= 0)
819 gpio_free(i2c->gpios[idx]);
820 return -EINVAL;
821}
822
823static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
824{
825 unsigned int idx;
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200826
827 if (i2c->quirks & QUIRK_NO_GPIO)
828 return;
829
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530830 for (idx = 0; idx < 2; idx++)
831 gpio_free(i2c->gpios[idx]);
832}
833#else
834static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
835{
Tushar Behera8ebe6612011-12-09 15:33:55 +0530836 return 0;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530837}
838
839static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
840{
841}
842#endif
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/* s3c24xx_i2c_init
845 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000846 * initialise the controller, set the IO lines and frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847*/
848
849static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
850{
851 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
852 struct s3c2410_platform_i2c *pdata;
853 unsigned int freq;
854
855 /* get the plafrom data */
856
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530857 pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /* inititalise the gpio */
860
Ben Dooks8be310a2008-10-31 16:10:25 +0000861 if (pdata->cfg_gpio)
862 pdata->cfg_gpio(to_platform_device(i2c->dev));
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530863 else
864 if (s3c24xx_i2c_parse_dt_gpio(i2c))
865 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /* write slave address */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
870
871 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
872
Ben Dooks61c7cff2008-07-28 12:04:07 +0100873 writel(iicon, i2c->regs + S3C2410_IICCON);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* we need to work out the divisors for the clock... */
876
Ben Dooks61c7cff2008-07-28 12:04:07 +0100877 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
878 writel(0, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 dev_err(i2c->dev, "cannot meet bus frequency required\n");
880 return -EINVAL;
881 }
882
883 /* todo - check that the i2c lines aren't being dragged anywhere */
884
885 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
886 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 0;
889}
890
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530891#ifdef CONFIG_OF
892/* s3c24xx_i2c_parse_dt
893 *
894 * Parse the device tree node and retreive the platform data.
895*/
896
897static void
898s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
899{
900 struct s3c2410_platform_i2c *pdata = i2c->pdata;
901
902 if (!np)
903 return;
904
905 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
906 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
907 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
908 of_property_read_u32(np, "samsung,i2c-max-bus-freq",
909 (u32 *)&pdata->frequency);
910}
911#else
912static void
913s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
914{
915 return;
916}
917#endif
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/* s3c24xx_i2c_probe
920 *
921 * called by the bus driver when a suitable device is found
922*/
923
Russell King3ae5eae2005-11-09 22:32:44 +0000924static int s3c24xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Ben Dooks692acbd2008-10-31 16:10:28 +0000926 struct s3c24xx_i2c *i2c;
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530927 struct s3c2410_platform_i2c *pdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct resource *res;
929 int ret;
930
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530931 if (!pdev->dev.of_node) {
932 pdata = pdev->dev.platform_data;
933 if (!pdata) {
934 dev_err(&pdev->dev, "no platform data\n");
935 return -EINVAL;
936 }
Ben Dooks6a039ca2008-10-31 16:10:27 +0000937 }
Ben Dooks399dee22008-07-28 12:04:06 +0100938
Mark Brown4ea15572012-01-21 13:28:46 +0000939 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
Ben Dooks692acbd2008-10-31 16:10:28 +0000940 if (!i2c) {
941 dev_err(&pdev->dev, "no memory for state\n");
942 return -ENOMEM;
943 }
944
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530945 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
946 if (!i2c->pdata) {
947 ret = -ENOMEM;
948 goto err_noclk;
949 }
950
Karol Lewandowski27452492012-04-23 18:24:00 +0200951 i2c->quirks = s3c24xx_get_device_quirks(pdev);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530952 if (pdata)
953 memcpy(i2c->pdata, pdata, sizeof(*pdata));
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530954 else
955 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530956
Ben Dooks692acbd2008-10-31 16:10:28 +0000957 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
958 i2c->adap.owner = THIS_MODULE;
959 i2c->adap.algo = &s3c24xx_i2c_algorithm;
960 i2c->adap.retries = 2;
961 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
962 i2c->tx_setup = 50;
963
964 spin_lock_init(&i2c->lock);
965 init_waitqueue_head(&i2c->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /* find the clock and enable it */
968
Russell King3ae5eae2005-11-09 22:32:44 +0000969 i2c->dev = &pdev->dev;
970 i2c->clk = clk_get(&pdev->dev, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (IS_ERR(i2c->clk)) {
Russell King3ae5eae2005-11-09 22:32:44 +0000972 dev_err(&pdev->dev, "cannot get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +0200974 goto err_noclk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
Russell King3ae5eae2005-11-09 22:32:44 +0000977 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900979 clk_prepare_enable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 /* map the registers */
982
983 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
984 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000985 dev_err(&pdev->dev, "cannot find IO resource\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +0200987 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
Mark Browna72ad452012-11-05 09:33:39 +0100990 i2c->regs = devm_request_and_ioremap(&pdev->dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (i2c->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000993 dev_err(&pdev->dev, "cannot map IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 ret = -ENXIO;
Mark Browna72ad452012-11-05 09:33:39 +0100995 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
Mark Browna72ad452012-11-05 09:33:39 +0100998 dev_dbg(&pdev->dev, "registers %p (%p)\n",
999 i2c->regs, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /* setup info block for the i2c core */
1002
1003 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001004 i2c->adap.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /* initialise the i2c controller */
1007
1008 ret = s3c24xx_i2c_init(i2c);
1009 if (ret != 0)
Mark Browna72ad452012-11-05 09:33:39 +01001010 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 /* find the IRQ for this unit (note, this relies on the init call to
Ben Dooks3d0911b2008-10-31 16:10:24 +00001013 * ensure no current IRQs pending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
1015
Ben Dookse0d1ec92008-10-31 16:10:30 +00001016 i2c->irq = ret = platform_get_irq(pdev, 0);
1017 if (ret <= 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001018 dev_err(&pdev->dev, "cannot find IRQ\n");
Mark Browna72ad452012-11-05 09:33:39 +01001019 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Yong Zhang43110512011-09-21 17:28:33 +08001022 ret = request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
Ben Dookse0d1ec92008-10-31 16:10:30 +00001023 dev_name(&pdev->dev), i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (ret != 0) {
Ben Dookse0d1ec92008-10-31 16:10:30 +00001026 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
Mark Browna72ad452012-11-05 09:33:39 +01001027 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
Ben Dooks61c7cff2008-07-28 12:04:07 +01001030 ret = s3c24xx_i2c_register_cpufreq(i2c);
1031 if (ret < 0) {
1032 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
1033 goto err_irq;
1034 }
1035
Ben Dooks399dee22008-07-28 12:04:06 +01001036 /* Note, previous versions of the driver used i2c_add_adapter()
1037 * to add the bus at any number. We now pass the bus number via
1038 * the platform data, so if unset it will now default to always
1039 * being bus 0.
1040 */
1041
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301042 i2c->adap.nr = i2c->pdata->bus_num;
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301043 i2c->adap.dev.of_node = pdev->dev.of_node;
Ben Dooks399dee22008-07-28 12:04:06 +01001044
1045 ret = i2c_add_numbered_adapter(&i2c->adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001047 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
Ben Dooks61c7cff2008-07-28 12:04:07 +01001048 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301051 of_i2c_register_devices(&i2c->adap);
Russell King3ae5eae2005-11-09 22:32:44 +00001052 platform_set_drvdata(pdev, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Mark Brownc62c3ca2012-01-21 13:28:47 +00001054 pm_runtime_enable(&pdev->dev);
1055 pm_runtime_enable(&i2c->adap.dev);
1056
Jean Delvare22e965c2009-01-07 14:29:16 +01001057 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001058 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Ben Dooks61c7cff2008-07-28 12:04:07 +01001061 err_cpufreq:
1062 s3c24xx_i2c_deregister_cpufreq(i2c);
1063
Ben Dooks5b687902007-05-01 23:26:35 +02001064 err_irq:
Ben Dookse0d1ec92008-10-31 16:10:30 +00001065 free_irq(i2c->irq, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Ben Dooks5b687902007-05-01 23:26:35 +02001067 err_clk:
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001068 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001069 clk_put(i2c->clk);
1070
1071 err_noclk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return ret;
1073}
1074
1075/* s3c24xx_i2c_remove
1076 *
1077 * called when device is removed from the bus
1078*/
1079
Russell King3ae5eae2005-11-09 22:32:44 +00001080static int s3c24xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Russell King3ae5eae2005-11-09 22:32:44 +00001082 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Ben Dooks5b687902007-05-01 23:26:35 +02001083
Mark Brownc62c3ca2012-01-21 13:28:47 +00001084 pm_runtime_disable(&i2c->adap.dev);
1085 pm_runtime_disable(&pdev->dev);
1086
Ben Dooks61c7cff2008-07-28 12:04:07 +01001087 s3c24xx_i2c_deregister_cpufreq(i2c);
1088
Ben Dooks5b687902007-05-01 23:26:35 +02001089 i2c_del_adapter(&i2c->adap);
Ben Dookse0d1ec92008-10-31 16:10:30 +00001090 free_irq(i2c->irq, i2c);
Ben Dooks5b687902007-05-01 23:26:35 +02001091
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001092 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001093 clk_put(i2c->clk);
1094
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301095 s3c24xx_i2c_dt_gpio_free(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 return 0;
1098}
1099
Mark Brown2935e0e2012-11-05 09:33:38 +01001100#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001101static int s3c24xx_i2c_suspend_noirq(struct device *dev)
Ben Dooksbe44f012008-10-31 16:10:22 +00001102{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001103 struct platform_device *pdev = to_platform_device(dev);
1104 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1105
Ben Dooksbe44f012008-10-31 16:10:22 +00001106 i2c->suspended = 1;
Magnus Damm6a6c61892009-07-08 13:22:47 +02001107
Ben Dooksbe44f012008-10-31 16:10:22 +00001108 return 0;
1109}
1110
Magnus Damm6a6c61892009-07-08 13:22:47 +02001111static int s3c24xx_i2c_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001113 struct platform_device *pdev = to_platform_device(dev);
1114 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Russell King9480e302005-10-28 09:52:56 -07001115
Ben Dooksbe44f012008-10-31 16:10:22 +00001116 i2c->suspended = 0;
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001117 clk_prepare_enable(i2c->clk);
Ben Dooksbe44f012008-10-31 16:10:22 +00001118 s3c24xx_i2c_init(i2c);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001119 clk_disable_unprepare(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 return 0;
1122}
Mark Brown2935e0e2012-11-05 09:33:38 +01001123#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Mark Brown2935e0e2012-11-05 09:33:38 +01001125#ifdef CONFIG_PM
Alexey Dobriyan47145212009-12-14 18:00:08 -08001126static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
Mark Brown2935e0e2012-11-05 09:33:38 +01001127#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001128 .suspend_noirq = s3c24xx_i2c_suspend_noirq,
1129 .resume = s3c24xx_i2c_resume,
Mark Brown2935e0e2012-11-05 09:33:38 +01001130#endif
Magnus Damm6a6c61892009-07-08 13:22:47 +02001131};
1132
1133#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134#else
Magnus Damm6a6c61892009-07-08 13:22:47 +02001135#define S3C24XX_DEV_PM_OPS NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136#endif
1137
1138/* device driver for platform bus bits */
1139
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001140static struct platform_driver s3c24xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 .probe = s3c24xx_i2c_probe,
1142 .remove = s3c24xx_i2c_remove,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001143 .id_table = s3c24xx_driver_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001144 .driver = {
1145 .owner = THIS_MODULE,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001146 .name = "s3c-i2c",
Magnus Damm6a6c61892009-07-08 13:22:47 +02001147 .pm = S3C24XX_DEV_PM_OPS,
Karol Lewandowski9df7ead2012-03-21 20:11:51 +01001148 .of_match_table = of_match_ptr(s3c24xx_i2c_match),
Russell King3ae5eae2005-11-09 22:32:44 +00001149 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150};
1151
1152static int __init i2c_adap_s3c_init(void)
1153{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001154 return platform_driver_register(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
Mark Brown18dc83a2009-02-26 16:29:22 +00001156subsys_initcall(i2c_adap_s3c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158static void __exit i2c_adap_s3c_exit(void)
1159{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001160 platform_driver_unregister(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162module_exit(i2c_adap_s3c_exit);
1163
1164MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1165MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1166MODULE_LICENSE("GPL");