blob: 40e2d40bbdb5b72283a3fc21a176a3fda4977945 [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>
Tomasz Figa2693ac62012-11-13 11:33:40 +010041#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Ben Dooks9498cb72008-10-30 10:14:33 +000045#include <plat/regs-iic.h>
Arnd Bergmann436d42c2012-08-24 15:22:12 +020046#include <linux/platform_data/i2c-s3c2410.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Karol Lewandowski27452492012-04-23 18:24:00 +020048/* Treat S3C2410 as baseline hardware, anything else is supported via quirks */
49#define QUIRK_S3C2440 (1 << 0)
Karol Lewandowskiec39ef82012-04-23 18:24:01 +020050#define QUIRK_HDMIPHY (1 << 1)
51#define QUIRK_NO_GPIO (1 << 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Karol Lewandowski27452492012-04-23 18:24:00 +020053/* i2c controller state */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054enum s3c24xx_i2c_state {
55 STATE_IDLE,
56 STATE_START,
57 STATE_READ,
58 STATE_WRITE,
59 STATE_STOP
60};
61
62struct s3c24xx_i2c {
63 spinlock_t lock;
64 wait_queue_head_t wait;
Karol Lewandowski27452492012-04-23 18:24:00 +020065 unsigned int quirks;
Ben Dooksbe44f012008-10-31 16:10:22 +000066 unsigned int suspended:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 struct i2c_msg *msg;
69 unsigned int msg_num;
70 unsigned int msg_idx;
71 unsigned int msg_ptr;
72
Ben Dookse00a8cd2007-05-01 23:26:35 +020073 unsigned int tx_setup;
Ben Dookse0d1ec92008-10-31 16:10:30 +000074 unsigned int irq;
Ben Dookse00a8cd2007-05-01 23:26:35 +020075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 enum s3c24xx_i2c_state state;
Ben Dooks61c7cff2008-07-28 12:04:07 +010077 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 void __iomem *regs;
80 struct clk *clk;
81 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 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];
Tomasz Figa2693ac62012-11-13 11:33:40 +010086 struct pinctrl *pctrl;
Ben Dooks61c7cff2008-07-28 12:04:07 +010087#ifdef CONFIG_CPU_FREQ
88 struct notifier_block freq_transition;
89#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Karol Lewandowski27452492012-04-23 18:24:00 +020092static struct platform_device_id s3c24xx_driver_ids[] = {
93 {
94 .name = "s3c2410-i2c",
95 .driver_data = 0,
96 }, {
97 .name = "s3c2440-i2c",
98 .driver_data = QUIRK_S3C2440,
Karol Lewandowskiec39ef82012-04-23 18:24:01 +020099 }, {
100 .name = "s3c2440-hdmiphy-i2c",
101 .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
Karol Lewandowski27452492012-04-23 18:24:00 +0200102 }, { },
103};
104MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530106#ifdef CONFIG_OF
Karol Lewandowski27452492012-04-23 18:24:00 +0200107static const struct of_device_id s3c24xx_i2c_match[] = {
108 { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
109 { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200110 { .compatible = "samsung,s3c2440-hdmiphy-i2c",
111 .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
Karol Lewandowski27452492012-04-23 18:24:00 +0200112 {},
113};
114MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530115#endif
116
Karol Lewandowski27452492012-04-23 18:24:00 +0200117/* s3c24xx_get_device_quirks
118 *
119 * Get controller type either from device tree or platform device variant.
120*/
121
122static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
123{
124 if (pdev->dev.of_node) {
125 const struct of_device_id *match;
Karol Lewandowskib900ba42012-05-30 11:43:05 +0200126 match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
Karol Lewandowski27452492012-04-23 18:24:00 +0200127 return (unsigned int)match->data;
128 }
129
130 return platform_get_device_id(pdev)->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/* s3c24xx_i2c_master_complete
134 *
135 * complete the message and wake up the caller, using the given return code,
136 * or zero to mean ok.
137*/
138
139static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
140{
141 dev_dbg(i2c->dev, "master_complete %d\n", ret);
142
143 i2c->msg_ptr = 0;
144 i2c->msg = NULL;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000145 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 i2c->msg_num = 0;
147 if (ret)
148 i2c->msg_idx = ret;
149
150 wake_up(&i2c->wait);
151}
152
153static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
154{
155 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 tmp = readl(i2c->regs + S3C2410_IICCON);
158 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
162{
163 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 tmp = readl(i2c->regs + S3C2410_IICCON);
166 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/* irq enable/disable functions */
170
171static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
172{
173 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 tmp = readl(i2c->regs + S3C2410_IICCON);
176 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
177}
178
179static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
180{
181 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 tmp = readl(i2c->regs + S3C2410_IICCON);
184 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
185}
186
187
188/* s3c24xx_i2c_message_start
189 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000190 * put the start of a message onto the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191*/
192
Ben Dooks3d0911b2008-10-31 16:10:24 +0000193static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct i2c_msg *msg)
195{
196 unsigned int addr = (msg->addr & 0x7f) << 1;
197 unsigned long stat;
198 unsigned long iiccon;
199
200 stat = 0;
201 stat |= S3C2410_IICSTAT_TXRXEN;
202
203 if (msg->flags & I2C_M_RD) {
204 stat |= S3C2410_IICSTAT_MASTER_RX;
205 addr |= 1;
206 } else
207 stat |= S3C2410_IICSTAT_MASTER_TX;
208
209 if (msg->flags & I2C_M_REV_DIR_ADDR)
210 addr ^= 1;
211
Ben Dooks3d0911b2008-10-31 16:10:24 +0000212 /* todo - check for wether ack wanted or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 s3c24xx_i2c_enable_ack(i2c);
214
215 iiccon = readl(i2c->regs + S3C2410_IICCON);
216 writel(stat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
219 writeb(addr, i2c->regs + S3C2410_IICDS);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000220
Ben Dookse00a8cd2007-05-01 23:26:35 +0200221 /* delay here to ensure the data byte has gotten onto the bus
222 * before the transaction is started */
223
224 ndelay(i2c->tx_setup);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
227 writel(iiccon, i2c->regs + S3C2410_IICCON);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000228
229 stat |= S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 writel(stat, i2c->regs + S3C2410_IICSTAT);
231}
232
233static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
234{
235 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
236
237 dev_dbg(i2c->dev, "STOP\n");
238
239 /* stop the transfer */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000240 iicstat &= ~S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 i2c->state = STATE_STOP;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 s3c24xx_i2c_master_complete(i2c, ret);
246 s3c24xx_i2c_disable_irq(i2c);
247}
248
249/* helper functions to determine the current state in the set of
250 * messages we are sending */
251
252/* is_lastmsg()
253 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000254 * returns TRUE if the current message is the last in the set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255*/
256
257static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
258{
259 return i2c->msg_idx >= (i2c->msg_num - 1);
260}
261
262/* is_msglast
263 *
264 * returns TRUE if we this is the last byte in the current message
265*/
266
267static inline int is_msglast(struct s3c24xx_i2c *i2c)
268{
269 return i2c->msg_ptr == i2c->msg->len-1;
270}
271
272/* is_msgend
273 *
274 * returns TRUE if we reached the end of the current message
275*/
276
277static inline int is_msgend(struct s3c24xx_i2c *i2c)
278{
279 return i2c->msg_ptr >= i2c->msg->len;
280}
281
Huisung Kang19820512011-06-23 21:37:33 +0900282/* i2c_s3c_irq_nextbyte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
284 * process an interrupt and work out what to do
285 */
286
Huisung Kang19820512011-06-23 21:37:33 +0900287static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 unsigned long tmp;
290 unsigned char byte;
291 int ret = 0;
292
293 switch (i2c->state) {
294
295 case STATE_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200296 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 case STATE_STOP:
Harvey Harrison08882d22008-04-22 22:16:47 +0200300 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000301 s3c24xx_i2c_disable_irq(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 goto out_ack;
303
304 case STATE_START:
305 /* last thing we did was send a start condition on the
306 * bus, or started a new i2c message
307 */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000308
Ben Dooks63f5c282008-07-01 11:59:42 +0100309 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
311 /* ack was not received... */
312
313 dev_dbg(i2c->dev, "ack was not received\n");
Ben Dooks63f5c282008-07-01 11:59:42 +0100314 s3c24xx_i2c_stop(i2c, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto out_ack;
316 }
317
318 if (i2c->msg->flags & I2C_M_RD)
319 i2c->state = STATE_READ;
320 else
321 i2c->state = STATE_WRITE;
322
323 /* terminate the transfer if there is nothing to do
Ben Dooks63f5c282008-07-01 11:59:42 +0100324 * as this is used by the i2c probe to find devices. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
327 s3c24xx_i2c_stop(i2c, 0);
328 goto out_ack;
329 }
330
331 if (i2c->state == STATE_READ)
332 goto prepare_read;
333
Ben Dooks3d0911b2008-10-31 16:10:24 +0000334 /* fall through to the write state, as we will need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * send a byte as well */
336
337 case STATE_WRITE:
338 /* we are writing data to the device... check for the
339 * end of the message, and if so, work out what to do
340 */
341
Ben Dooks27097812008-07-01 11:59:41 +0100342 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
343 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
344 dev_dbg(i2c->dev, "WRITE: No Ack\n");
345
346 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
347 goto out_ack;
348 }
349 }
350
Ben Dooks3d0911b2008-10-31 16:10:24 +0000351 retry_write:
Ben Dooks27097812008-07-01 11:59:41 +0100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!is_msgend(i2c)) {
354 byte = i2c->msg->buf[i2c->msg_ptr++];
355 writeb(byte, i2c->regs + S3C2410_IICDS);
Ben Dookse00a8cd2007-05-01 23:26:35 +0200356
357 /* delay after writing the byte to allow the
358 * data setup time on the bus, as writing the
359 * data to the register causes the first bit
360 * to appear on SDA, and SCL will change as
361 * soon as the interrupt is acknowledged */
362
363 ndelay(i2c->tx_setup);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 } else if (!is_lastmsg(i2c)) {
366 /* we need to go to the next i2c message */
367
368 dev_dbg(i2c->dev, "WRITE: Next Message\n");
369
370 i2c->msg_ptr = 0;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000371 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 i2c->msg++;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* check to see if we need to do another message */
375 if (i2c->msg->flags & I2C_M_NOSTART) {
376
377 if (i2c->msg->flags & I2C_M_RD) {
378 /* cannot do this, the controller
379 * forces us to send a new START
380 * when we change direction */
381
382 s3c24xx_i2c_stop(i2c, -EINVAL);
383 }
384
385 goto retry_write;
386 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* send the new start */
388 s3c24xx_i2c_message_start(i2c, i2c->msg);
389 i2c->state = STATE_START;
390 }
391
392 } else {
393 /* send stop */
394
395 s3c24xx_i2c_stop(i2c, 0);
396 }
397 break;
398
399 case STATE_READ:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000400 /* we have a byte of data in the data register, do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * something with it, and then work out wether we are
402 * going to do any more read/write
403 */
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 byte = readb(i2c->regs + S3C2410_IICDS);
406 i2c->msg->buf[i2c->msg_ptr++] = byte;
407
Ben Dooks3d0911b2008-10-31 16:10:24 +0000408 prepare_read:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (is_msglast(i2c)) {
410 /* last byte of buffer */
411
412 if (is_lastmsg(i2c))
413 s3c24xx_i2c_disable_ack(i2c);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 } else if (is_msgend(i2c)) {
416 /* ok, we've read the entire buffer, see if there
417 * is anything else we need to do */
418
419 if (is_lastmsg(i2c)) {
420 /* last message, send stop and complete */
421 dev_dbg(i2c->dev, "READ: Send Stop\n");
422
423 s3c24xx_i2c_stop(i2c, 0);
424 } else {
425 /* go to the next transfer */
426 dev_dbg(i2c->dev, "READ: Next Transfer\n");
427
428 i2c->msg_ptr = 0;
429 i2c->msg_idx++;
430 i2c->msg++;
431 }
432 }
433
434 break;
435 }
436
437 /* acknowlegde the IRQ and get back on with the work */
438
439 out_ack:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000440 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 tmp &= ~S3C2410_IICCON_IRQPEND;
442 writel(tmp, i2c->regs + S3C2410_IICCON);
443 out:
444 return ret;
445}
446
447/* s3c24xx_i2c_irq
448 *
449 * top level IRQ servicing routine
450*/
451
David Howells7d12e782006-10-05 14:55:46 +0100452static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct s3c24xx_i2c *i2c = dev_id;
455 unsigned long status;
456 unsigned long tmp;
457
458 status = readl(i2c->regs + S3C2410_IICSTAT);
459
460 if (status & S3C2410_IICSTAT_ARBITR) {
Ben Dooks3d0911b2008-10-31 16:10:24 +0000461 /* deal with arbitration loss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 dev_err(i2c->dev, "deal with arbitration loss\n");
463 }
464
465 if (i2c->state == STATE_IDLE) {
466 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
467
Ben Dooks3d0911b2008-10-31 16:10:24 +0000468 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 tmp &= ~S3C2410_IICCON_IRQPEND;
470 writel(tmp, i2c->regs + S3C2410_IICCON);
471 goto out;
472 }
Ben Dooks3d0911b2008-10-31 16:10:24 +0000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* pretty much this leaves us with the fact that we've
475 * transmitted or received whatever byte we last sent */
476
Huisung Kang19820512011-06-23 21:37:33 +0900477 i2c_s3c_irq_nextbyte(i2c, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 out:
480 return IRQ_HANDLED;
481}
482
483
484/* s3c24xx_i2c_set_master
485 *
486 * get the i2c bus for a master transaction
487*/
488
489static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
490{
491 unsigned long iicstat;
492 int timeout = 400;
493
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200494 /* the timeout for HDMIPHY is reduced to 10 ms because
495 * the hangup is expected to happen, so waiting 400 ms
496 * causes only unnecessary system hangup
497 */
498 if (i2c->quirks & QUIRK_HDMIPHY)
499 timeout = 10;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 while (timeout-- > 0) {
502 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
505 return 0;
506
507 msleep(1);
508 }
509
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200510 /* hang-up of bus dedicated for HDMIPHY occurred, resetting */
511 if (i2c->quirks & QUIRK_HDMIPHY) {
512 writel(0, i2c->regs + S3C2410_IICCON);
513 writel(0, i2c->regs + S3C2410_IICSTAT);
514 writel(0, i2c->regs + S3C2410_IICDS);
515
516 return 0;
517 }
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -ETIMEDOUT;
520}
521
522/* s3c24xx_i2c_doxfer
523 *
524 * this starts an i2c transfer
525*/
526
Ben Dooks3d0911b2008-10-31 16:10:24 +0000527static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
528 struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Mark Brown1bc29622010-04-02 14:15:09 +0100530 unsigned long iicstat, timeout;
531 int spins = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int ret;
533
Ben Dooksbe44f012008-10-31 16:10:22 +0000534 if (i2c->suspended)
Ben Dooks61c7cff2008-07-28 12:04:07 +0100535 return -EIO;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ret = s3c24xx_i2c_set_master(i2c);
538 if (ret != 0) {
539 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
540 ret = -EAGAIN;
541 goto out;
542 }
543
544 spin_lock_irq(&i2c->lock);
545
546 i2c->msg = msgs;
547 i2c->msg_num = num;
548 i2c->msg_ptr = 0;
549 i2c->msg_idx = 0;
550 i2c->state = STATE_START;
551
552 s3c24xx_i2c_enable_irq(i2c);
553 s3c24xx_i2c_message_start(i2c, msgs);
554 spin_unlock_irq(&i2c->lock);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
557
558 ret = i2c->msg_idx;
559
Ben Dooks3d0911b2008-10-31 16:10:24 +0000560 /* having these next two as dev_err() makes life very
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * noisy when doing an i2cdetect */
562
563 if (timeout == 0)
564 dev_dbg(i2c->dev, "timeout\n");
565 else if (ret != num)
566 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
567
568 /* ensure the stop has been through the bus */
569
Mark Brown1bc29622010-04-02 14:15:09 +0100570 dev_dbg(i2c->dev, "waiting for bus idle\n");
571
572 /* first, try busy waiting briefly */
573 do {
Mark Brown37de03e2011-12-09 20:55:03 +0800574 cpu_relax();
Mark Brown1bc29622010-04-02 14:15:09 +0100575 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
576 } while ((iicstat & S3C2410_IICSTAT_START) && --spins);
577
578 /* if that timed out sleep */
579 if (!spins) {
580 msleep(1);
581 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
582 }
583
584 if (iicstat & S3C2410_IICSTAT_START)
585 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 out:
588 return ret;
589}
590
591/* s3c24xx_i2c_xfer
592 *
593 * first port of call from the i2c bus code when an message needs
Steven Cole44bbe872005-05-03 18:21:25 -0600594 * transferring across the i2c bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595*/
596
597static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
598 struct i2c_msg *msgs, int num)
599{
600 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
601 int retry;
602 int ret;
603
Mark Brownc62c3ca2012-01-21 13:28:47 +0000604 pm_runtime_get_sync(&adap->dev);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900605 clk_prepare_enable(i2c->clk);
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 for (retry = 0; retry < adap->retries; retry++) {
608
609 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
610
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200611 if (ret != -EAGAIN) {
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900612 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100613 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return ret;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
618
619 udelay(100);
620 }
621
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900622 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100623 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return -EREMOTEIO;
625}
626
627/* declare our i2c functionality */
628static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
629{
Mark Brown14674e72012-05-30 10:55:34 +0200630 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
631 I2C_FUNC_PROTOCOL_MANGLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634/* i2c bus registration info */
635
Jean Delvare8f9082c2006-09-03 22:39:46 +0200636static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .master_xfer = s3c24xx_i2c_xfer,
638 .functionality = s3c24xx_i2c_func,
639};
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/* s3c24xx_i2c_calcdivisor
642 *
643 * return the divisor settings for a given frequency
644*/
645
646static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
647 unsigned int *div1, unsigned int *divs)
648{
649 unsigned int calc_divs = clkin / wanted;
650 unsigned int calc_div1;
651
652 if (calc_divs > (16*16))
653 calc_div1 = 512;
654 else
655 calc_div1 = 16;
656
657 calc_divs += calc_div1-1;
658 calc_divs /= calc_div1;
659
660 if (calc_divs == 0)
661 calc_divs = 1;
662 if (calc_divs > 17)
663 calc_divs = 17;
664
665 *divs = calc_divs;
666 *div1 = calc_div1;
667
668 return clkin / (calc_divs * calc_div1);
669}
670
Ben Dooks61c7cff2008-07-28 12:04:07 +0100671/* s3c24xx_i2c_clockrate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 *
673 * work out a divisor for the user requested frequency setting,
674 * either by the requested frequency, or scanning the acceptable
675 * range of frequencies until something is found
676*/
677
Ben Dooks61c7cff2008-07-28 12:04:07 +0100678static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530680 struct s3c2410_platform_i2c *pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 unsigned long clkin = clk_get_rate(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 unsigned int divs, div1;
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000683 unsigned long target_frequency;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100684 u32 iiccon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Ben Dooks61c7cff2008-07-28 12:04:07 +0100687 i2c->clkrate = clkin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 clkin /= 1000; /* clkin now in KHz */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000689
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000690 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000692 target_frequency = pdata->frequency ? pdata->frequency : 100000;
693
694 target_frequency /= 1000; /* Target frequency now in KHz */
695
696 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
697
698 if (freq > target_frequency) {
699 dev_err(i2c->dev,
700 "Unable to achieve desired frequency %luKHz." \
701 " Lowest achievable %dKHz\n", target_frequency, freq);
702 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 *got = freq;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100706
707 iiccon = readl(i2c->regs + S3C2410_IICCON);
708 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
709 iiccon |= (divs-1);
710
711 if (div1 == 512)
712 iiccon |= S3C2410_IICCON_TXDIV_512;
713
714 writel(iiccon, i2c->regs + S3C2410_IICCON);
715
Karol Lewandowski27452492012-04-23 18:24:00 +0200716 if (i2c->quirks & QUIRK_S3C2440) {
Ben Dooksa192f712009-03-27 10:52:13 +0000717 unsigned long sda_delay;
718
719 if (pdata->sda_delay) {
MyungJoo Ham70313072010-09-30 15:54:46 +0200720 sda_delay = clkin * pdata->sda_delay;
721 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
Ben Dooksa192f712009-03-27 10:52:13 +0000722 sda_delay = DIV_ROUND_UP(sda_delay, 5);
723 if (sda_delay > 3)
724 sda_delay = 3;
725 sda_delay |= S3C2410_IICLC_FILTER_ON;
726 } else
727 sda_delay = 0;
728
729 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
730 writel(sda_delay, i2c->regs + S3C2440_IICLC);
731 }
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734}
735
Ben Dooks61c7cff2008-07-28 12:04:07 +0100736#ifdef CONFIG_CPU_FREQ
737
738#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
739
740static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
741 unsigned long val, void *data)
742{
743 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
744 unsigned long flags;
745 unsigned int got;
746 int delta_f;
747 int ret;
748
749 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
750
751 /* if we're post-change and the input clock has slowed down
752 * or at pre-change and the clock is about to speed up, then
753 * adjust our clock rate. <0 is slow, >0 speedup.
754 */
755
756 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
757 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
758 spin_lock_irqsave(&i2c->lock, flags);
759 ret = s3c24xx_i2c_clockrate(i2c, &got);
760 spin_unlock_irqrestore(&i2c->lock, flags);
761
762 if (ret < 0)
763 dev_err(i2c->dev, "cannot find frequency\n");
764 else
765 dev_info(i2c->dev, "setting freq %d\n", got);
766 }
767
768 return 0;
769}
770
771static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
772{
773 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
774
775 return cpufreq_register_notifier(&i2c->freq_transition,
776 CPUFREQ_TRANSITION_NOTIFIER);
777}
778
779static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
780{
781 cpufreq_unregister_notifier(&i2c->freq_transition,
782 CPUFREQ_TRANSITION_NOTIFIER);
783}
784
785#else
786static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
787{
788 return 0;
789}
790
791static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
792{
793}
794#endif
795
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530796#ifdef CONFIG_OF
797static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
798{
799 int idx, gpio, ret;
800
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200801 if (i2c->quirks & QUIRK_NO_GPIO)
802 return 0;
803
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530804 for (idx = 0; idx < 2; idx++) {
805 gpio = of_get_gpio(i2c->dev->of_node, idx);
806 if (!gpio_is_valid(gpio)) {
807 dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
808 goto free_gpio;
809 }
810
811 ret = gpio_request(gpio, "i2c-bus");
812 if (ret) {
813 dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
814 goto free_gpio;
815 }
816 }
817 return 0;
818
819free_gpio:
820 while (--idx >= 0)
821 gpio_free(i2c->gpios[idx]);
822 return -EINVAL;
823}
824
825static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
826{
827 unsigned int idx;
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200828
829 if (i2c->quirks & QUIRK_NO_GPIO)
830 return;
831
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530832 for (idx = 0; idx < 2; idx++)
833 gpio_free(i2c->gpios[idx]);
834}
835#else
836static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
837{
Tushar Behera8ebe6612011-12-09 15:33:55 +0530838 return 0;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530839}
840
841static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
842{
843}
844#endif
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846/* s3c24xx_i2c_init
847 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000848 * initialise the controller, set the IO lines and frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849*/
850
851static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
852{
853 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
854 struct s3c2410_platform_i2c *pdata;
855 unsigned int freq;
856
857 /* get the plafrom data */
858
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530859 pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /* inititalise the gpio */
862
Ben Dooks8be310a2008-10-31 16:10:25 +0000863 if (pdata->cfg_gpio)
864 pdata->cfg_gpio(to_platform_device(i2c->dev));
Tomasz Figa2693ac62012-11-13 11:33:40 +0100865 else if (IS_ERR(i2c->pctrl) && 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
Mark Browna72ad452012-11-05 09:33:39 +0100991 i2c->regs = devm_request_and_ioremap(&pdev->dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (i2c->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000994 dev_err(&pdev->dev, "cannot map IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 ret = -ENXIO;
Mark Browna72ad452012-11-05 09:33:39 +0100996 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998
Mark Browna72ad452012-11-05 09:33:39 +0100999 dev_dbg(&pdev->dev, "registers %p (%p)\n",
1000 i2c->regs, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* setup info block for the i2c core */
1003
1004 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001005 i2c->adap.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Tomasz Figa2693ac62012-11-13 11:33:40 +01001007 i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* initialise the i2c controller */
1010
1011 ret = s3c24xx_i2c_init(i2c);
1012 if (ret != 0)
Mark Browna72ad452012-11-05 09:33:39 +01001013 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* find the IRQ for this unit (note, this relies on the init call to
Ben Dooks3d0911b2008-10-31 16:10:24 +00001016 * ensure no current IRQs pending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
1018
Ben Dookse0d1ec92008-10-31 16:10:30 +00001019 i2c->irq = ret = platform_get_irq(pdev, 0);
1020 if (ret <= 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001021 dev_err(&pdev->dev, "cannot find IRQ\n");
Mark Browna72ad452012-11-05 09:33:39 +01001022 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024
Yong Zhang43110512011-09-21 17:28:33 +08001025 ret = request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
Ben Dookse0d1ec92008-10-31 16:10:30 +00001026 dev_name(&pdev->dev), i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (ret != 0) {
Ben Dookse0d1ec92008-10-31 16:10:30 +00001029 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
Mark Browna72ad452012-11-05 09:33:39 +01001030 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
Ben Dooks61c7cff2008-07-28 12:04:07 +01001033 ret = s3c24xx_i2c_register_cpufreq(i2c);
1034 if (ret < 0) {
1035 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
1036 goto err_irq;
1037 }
1038
Ben Dooks399dee22008-07-28 12:04:06 +01001039 /* Note, previous versions of the driver used i2c_add_adapter()
1040 * to add the bus at any number. We now pass the bus number via
1041 * the platform data, so if unset it will now default to always
1042 * being bus 0.
1043 */
1044
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301045 i2c->adap.nr = i2c->pdata->bus_num;
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301046 i2c->adap.dev.of_node = pdev->dev.of_node;
Ben Dooks399dee22008-07-28 12:04:06 +01001047
1048 ret = i2c_add_numbered_adapter(&i2c->adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001050 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
Ben Dooks61c7cff2008-07-28 12:04:07 +01001051 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301054 of_i2c_register_devices(&i2c->adap);
Russell King3ae5eae2005-11-09 22:32:44 +00001055 platform_set_drvdata(pdev, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Mark Brownc62c3ca2012-01-21 13:28:47 +00001057 pm_runtime_enable(&pdev->dev);
1058 pm_runtime_enable(&i2c->adap.dev);
1059
Jean Delvare22e965c2009-01-07 14:29:16 +01001060 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001061 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Ben Dooks61c7cff2008-07-28 12:04:07 +01001064 err_cpufreq:
1065 s3c24xx_i2c_deregister_cpufreq(i2c);
1066
Ben Dooks5b687902007-05-01 23:26:35 +02001067 err_irq:
Ben Dookse0d1ec92008-10-31 16:10:30 +00001068 free_irq(i2c->irq, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Ben Dooks5b687902007-05-01 23:26:35 +02001070 err_clk:
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001071 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001072 clk_put(i2c->clk);
1073
1074 err_noclk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return ret;
1076}
1077
1078/* s3c24xx_i2c_remove
1079 *
1080 * called when device is removed from the bus
1081*/
1082
Russell King3ae5eae2005-11-09 22:32:44 +00001083static int s3c24xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Russell King3ae5eae2005-11-09 22:32:44 +00001085 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Ben Dooks5b687902007-05-01 23:26:35 +02001086
Mark Brownc62c3ca2012-01-21 13:28:47 +00001087 pm_runtime_disable(&i2c->adap.dev);
1088 pm_runtime_disable(&pdev->dev);
1089
Ben Dooks61c7cff2008-07-28 12:04:07 +01001090 s3c24xx_i2c_deregister_cpufreq(i2c);
1091
Ben Dooks5b687902007-05-01 23:26:35 +02001092 i2c_del_adapter(&i2c->adap);
Ben Dookse0d1ec92008-10-31 16:10:30 +00001093 free_irq(i2c->irq, i2c);
Ben Dooks5b687902007-05-01 23:26:35 +02001094
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001095 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001096 clk_put(i2c->clk);
1097
Tomasz Figa2693ac62012-11-13 11:33:40 +01001098 if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
1099 s3c24xx_i2c_dt_gpio_free(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 return 0;
1102}
1103
Mark Brown2935e0e2012-11-05 09:33:38 +01001104#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001105static int s3c24xx_i2c_suspend_noirq(struct device *dev)
Ben Dooksbe44f012008-10-31 16:10:22 +00001106{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001107 struct platform_device *pdev = to_platform_device(dev);
1108 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1109
Ben Dooksbe44f012008-10-31 16:10:22 +00001110 i2c->suspended = 1;
Magnus Damm6a6c61892009-07-08 13:22:47 +02001111
Ben Dooksbe44f012008-10-31 16:10:22 +00001112 return 0;
1113}
1114
Magnus Damm6a6c61892009-07-08 13:22:47 +02001115static int s3c24xx_i2c_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001117 struct platform_device *pdev = to_platform_device(dev);
1118 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Russell King9480e302005-10-28 09:52:56 -07001119
Ben Dooksbe44f012008-10-31 16:10:22 +00001120 i2c->suspended = 0;
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001121 clk_prepare_enable(i2c->clk);
Ben Dooksbe44f012008-10-31 16:10:22 +00001122 s3c24xx_i2c_init(i2c);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001123 clk_disable_unprepare(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 return 0;
1126}
Mark Brown2935e0e2012-11-05 09:33:38 +01001127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Mark Brown2935e0e2012-11-05 09:33:38 +01001129#ifdef CONFIG_PM
Alexey Dobriyan47145212009-12-14 18:00:08 -08001130static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
Mark Brown2935e0e2012-11-05 09:33:38 +01001131#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001132 .suspend_noirq = s3c24xx_i2c_suspend_noirq,
1133 .resume = s3c24xx_i2c_resume,
Mark Brown2935e0e2012-11-05 09:33:38 +01001134#endif
Magnus Damm6a6c61892009-07-08 13:22:47 +02001135};
1136
1137#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138#else
Magnus Damm6a6c61892009-07-08 13:22:47 +02001139#define S3C24XX_DEV_PM_OPS NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140#endif
1141
1142/* device driver for platform bus bits */
1143
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001144static struct platform_driver s3c24xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 .probe = s3c24xx_i2c_probe,
1146 .remove = s3c24xx_i2c_remove,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001147 .id_table = s3c24xx_driver_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001148 .driver = {
1149 .owner = THIS_MODULE,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001150 .name = "s3c-i2c",
Magnus Damm6a6c61892009-07-08 13:22:47 +02001151 .pm = S3C24XX_DEV_PM_OPS,
Karol Lewandowski9df7ead2012-03-21 20:11:51 +01001152 .of_match_table = of_match_ptr(s3c24xx_i2c_match),
Russell King3ae5eae2005-11-09 22:32:44 +00001153 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154};
1155
1156static int __init i2c_adap_s3c_init(void)
1157{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001158 return platform_driver_register(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
Mark Brown18dc83a2009-02-26 16:29:22 +00001160subsys_initcall(i2c_adap_s3c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162static void __exit i2c_adap_s3c_exit(void)
1163{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001164 platform_driver_unregister(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166module_exit(i2c_adap_s3c_exit);
1167
1168MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1169MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1170MODULE_LICENSE("GPL");