blob: dd93d3d6510a96c12c2aa7a51176f53efcc842a8 [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
Daniel Kurtzfe724bf2012-11-15 17:43:32 +053053/* Max time to wait for bus to become idle after a xfer (in us) */
54#define S3C2410_IDLE_TIMEOUT 5000
55
Karol Lewandowski27452492012-04-23 18:24:00 +020056/* i2c controller state */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057enum s3c24xx_i2c_state {
58 STATE_IDLE,
59 STATE_START,
60 STATE_READ,
61 STATE_WRITE,
62 STATE_STOP
63};
64
65struct s3c24xx_i2c {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 wait_queue_head_t wait;
Karol Lewandowski27452492012-04-23 18:24:00 +020067 unsigned int quirks;
Ben Dooksbe44f012008-10-31 16:10:22 +000068 unsigned int suspended:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 struct i2c_msg *msg;
71 unsigned int msg_num;
72 unsigned int msg_idx;
73 unsigned int msg_ptr;
74
Ben Dookse00a8cd2007-05-01 23:26:35 +020075 unsigned int tx_setup;
Ben Dookse0d1ec92008-10-31 16:10:30 +000076 unsigned int irq;
Ben Dookse00a8cd2007-05-01 23:26:35 +020077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 enum s3c24xx_i2c_state state;
Ben Dooks61c7cff2008-07-28 12:04:07 +010079 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 void __iomem *regs;
82 struct clk *clk;
83 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct i2c_adapter adap;
Ben Dooks61c7cff2008-07-28 12:04:07 +010085
Thomas Abraham4fd81eb2011-09-13 09:46:04 +053086 struct s3c2410_platform_i2c *pdata;
Thomas Abraham5a5f5082011-09-13 09:46:05 +053087 int gpios[2];
Tomasz Figa2693ac62012-11-13 11:33:40 +010088 struct pinctrl *pctrl;
Ben Dooks61c7cff2008-07-28 12:04:07 +010089#ifdef CONFIG_CPU_FREQ
90 struct notifier_block freq_transition;
91#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
Karol Lewandowski27452492012-04-23 18:24:00 +020094static struct platform_device_id s3c24xx_driver_ids[] = {
95 {
96 .name = "s3c2410-i2c",
97 .driver_data = 0,
98 }, {
99 .name = "s3c2440-i2c",
100 .driver_data = QUIRK_S3C2440,
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200101 }, {
102 .name = "s3c2440-hdmiphy-i2c",
103 .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
Karol Lewandowski27452492012-04-23 18:24:00 +0200104 }, { },
105};
106MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530108#ifdef CONFIG_OF
Karol Lewandowski27452492012-04-23 18:24:00 +0200109static const struct of_device_id s3c24xx_i2c_match[] = {
110 { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
111 { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200112 { .compatible = "samsung,s3c2440-hdmiphy-i2c",
113 .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
Karol Lewandowski27452492012-04-23 18:24:00 +0200114 {},
115};
116MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530117#endif
118
Karol Lewandowski27452492012-04-23 18:24:00 +0200119/* s3c24xx_get_device_quirks
120 *
121 * Get controller type either from device tree or platform device variant.
122*/
123
124static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
125{
126 if (pdev->dev.of_node) {
127 const struct of_device_id *match;
Karol Lewandowskib900ba42012-05-30 11:43:05 +0200128 match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
Karol Lewandowski27452492012-04-23 18:24:00 +0200129 return (unsigned int)match->data;
130 }
131
132 return platform_get_device_id(pdev)->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* s3c24xx_i2c_master_complete
136 *
137 * complete the message and wake up the caller, using the given return code,
138 * or zero to mean ok.
139*/
140
141static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
142{
143 dev_dbg(i2c->dev, "master_complete %d\n", ret);
144
145 i2c->msg_ptr = 0;
146 i2c->msg = NULL;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000147 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 i2c->msg_num = 0;
149 if (ret)
150 i2c->msg_idx = ret;
151
152 wake_up(&i2c->wait);
153}
154
155static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
156{
157 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 tmp = readl(i2c->regs + S3C2410_IICCON);
160 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
164{
165 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 tmp = readl(i2c->regs + S3C2410_IICCON);
168 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/* irq enable/disable functions */
172
173static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
174{
175 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 tmp = readl(i2c->regs + S3C2410_IICCON);
178 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
179}
180
181static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
182{
183 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 tmp = readl(i2c->regs + S3C2410_IICCON);
186 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
187}
188
189
190/* s3c24xx_i2c_message_start
191 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000192 * put the start of a message onto the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193*/
194
Ben Dooks3d0911b2008-10-31 16:10:24 +0000195static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct i2c_msg *msg)
197{
198 unsigned int addr = (msg->addr & 0x7f) << 1;
199 unsigned long stat;
200 unsigned long iiccon;
201
202 stat = 0;
203 stat |= S3C2410_IICSTAT_TXRXEN;
204
205 if (msg->flags & I2C_M_RD) {
206 stat |= S3C2410_IICSTAT_MASTER_RX;
207 addr |= 1;
208 } else
209 stat |= S3C2410_IICSTAT_MASTER_TX;
210
211 if (msg->flags & I2C_M_REV_DIR_ADDR)
212 addr ^= 1;
213
Ben Dooks3d0911b2008-10-31 16:10:24 +0000214 /* todo - check for wether ack wanted or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 s3c24xx_i2c_enable_ack(i2c);
216
217 iiccon = readl(i2c->regs + S3C2410_IICCON);
218 writel(stat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
221 writeb(addr, i2c->regs + S3C2410_IICDS);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000222
Ben Dookse00a8cd2007-05-01 23:26:35 +0200223 /* delay here to ensure the data byte has gotten onto the bus
224 * before the transaction is started */
225
226 ndelay(i2c->tx_setup);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
229 writel(iiccon, i2c->regs + S3C2410_IICCON);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000230
231 stat |= S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 writel(stat, i2c->regs + S3C2410_IICSTAT);
233}
234
235static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
236{
237 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
238
239 dev_dbg(i2c->dev, "STOP\n");
240
Daniel Kurtz0da2e772012-11-15 17:43:31 +0530241 /*
242 * The datasheet says that the STOP sequence should be:
243 * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP')
244 * 2) I2CCON.4 = 0 - Clear IRQPEND
245 * 3) Wait until the stop condition takes effect.
246 * 4*) I2CSTAT.4 = 0 - Clear TXRXEN
247 *
248 * Where, step "4*" is only for buses with the "HDMIPHY" quirk.
249 *
250 * However, after much experimentation, it appears that:
251 * a) normal buses automatically clear BUSY and transition from
252 * Master->Slave when they complete generating a STOP condition.
253 * Therefore, step (3) can be done in doxfer() by polling I2CCON.4
254 * after starting the STOP generation here.
255 * b) HDMIPHY bus does neither, so there is no way to do step 3.
256 * There is no indication when this bus has finished generating
257 * STOP.
258 *
259 * In fact, we have found that as soon as the IRQPEND bit is cleared in
260 * step 2, the HDMIPHY bus generates the STOP condition, and then
261 * immediately starts transferring another data byte, even though the
262 * bus is supposedly stopped. This is presumably because the bus is
263 * still in "Master" mode, and its BUSY bit is still set.
264 *
265 * To avoid these extra post-STOP transactions on HDMI phy devices, we
266 * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly,
267 * instead of first generating a proper STOP condition. This should
268 * float SDA & SCK terminating the transfer. Subsequent transfers
269 * start with a proper START condition, and proceed normally.
270 *
271 * The HDMIPHY bus is an internal bus that always has exactly two
272 * devices, the host as Master and the HDMIPHY device as the slave.
273 * Skipping the STOP condition has been tested on this bus and works.
274 */
275 if (i2c->quirks & QUIRK_HDMIPHY) {
276 /* Stop driving the I2C pins */
277 iicstat &= ~S3C2410_IICSTAT_TXRXEN;
278 } else {
279 /* stop the transfer */
280 iicstat &= ~S3C2410_IICSTAT_START;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 i2c->state = STATE_STOP;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 s3c24xx_i2c_master_complete(i2c, ret);
287 s3c24xx_i2c_disable_irq(i2c);
288}
289
290/* helper functions to determine the current state in the set of
291 * messages we are sending */
292
293/* is_lastmsg()
294 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000295 * returns TRUE if the current message is the last in the set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296*/
297
298static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
299{
300 return i2c->msg_idx >= (i2c->msg_num - 1);
301}
302
303/* is_msglast
304 *
305 * returns TRUE if we this is the last byte in the current message
306*/
307
308static inline int is_msglast(struct s3c24xx_i2c *i2c)
309{
310 return i2c->msg_ptr == i2c->msg->len-1;
311}
312
313/* is_msgend
314 *
315 * returns TRUE if we reached the end of the current message
316*/
317
318static inline int is_msgend(struct s3c24xx_i2c *i2c)
319{
320 return i2c->msg_ptr >= i2c->msg->len;
321}
322
Huisung Kang19820512011-06-23 21:37:33 +0900323/* i2c_s3c_irq_nextbyte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *
325 * process an interrupt and work out what to do
326 */
327
Huisung Kang19820512011-06-23 21:37:33 +0900328static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 unsigned long tmp;
331 unsigned char byte;
332 int ret = 0;
333
334 switch (i2c->state) {
335
336 case STATE_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200337 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 case STATE_STOP:
Harvey Harrison08882d22008-04-22 22:16:47 +0200341 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000342 s3c24xx_i2c_disable_irq(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto out_ack;
344
345 case STATE_START:
346 /* last thing we did was send a start condition on the
347 * bus, or started a new i2c message
348 */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000349
Ben Dooks63f5c282008-07-01 11:59:42 +0100350 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
352 /* ack was not received... */
353
354 dev_dbg(i2c->dev, "ack was not received\n");
Ben Dooks63f5c282008-07-01 11:59:42 +0100355 s3c24xx_i2c_stop(i2c, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto out_ack;
357 }
358
359 if (i2c->msg->flags & I2C_M_RD)
360 i2c->state = STATE_READ;
361 else
362 i2c->state = STATE_WRITE;
363
364 /* terminate the transfer if there is nothing to do
Ben Dooks63f5c282008-07-01 11:59:42 +0100365 * as this is used by the i2c probe to find devices. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
368 s3c24xx_i2c_stop(i2c, 0);
369 goto out_ack;
370 }
371
372 if (i2c->state == STATE_READ)
373 goto prepare_read;
374
Ben Dooks3d0911b2008-10-31 16:10:24 +0000375 /* fall through to the write state, as we will need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * send a byte as well */
377
378 case STATE_WRITE:
379 /* we are writing data to the device... check for the
380 * end of the message, and if so, work out what to do
381 */
382
Ben Dooks27097812008-07-01 11:59:41 +0100383 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
384 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
385 dev_dbg(i2c->dev, "WRITE: No Ack\n");
386
387 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
388 goto out_ack;
389 }
390 }
391
Ben Dooks3d0911b2008-10-31 16:10:24 +0000392 retry_write:
Ben Dooks27097812008-07-01 11:59:41 +0100393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!is_msgend(i2c)) {
395 byte = i2c->msg->buf[i2c->msg_ptr++];
396 writeb(byte, i2c->regs + S3C2410_IICDS);
Ben Dookse00a8cd2007-05-01 23:26:35 +0200397
398 /* delay after writing the byte to allow the
399 * data setup time on the bus, as writing the
400 * data to the register causes the first bit
401 * to appear on SDA, and SCL will change as
402 * soon as the interrupt is acknowledged */
403
404 ndelay(i2c->tx_setup);
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 } else if (!is_lastmsg(i2c)) {
407 /* we need to go to the next i2c message */
408
409 dev_dbg(i2c->dev, "WRITE: Next Message\n");
410
411 i2c->msg_ptr = 0;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000412 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 i2c->msg++;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* check to see if we need to do another message */
416 if (i2c->msg->flags & I2C_M_NOSTART) {
417
418 if (i2c->msg->flags & I2C_M_RD) {
419 /* cannot do this, the controller
420 * forces us to send a new START
421 * when we change direction */
422
423 s3c24xx_i2c_stop(i2c, -EINVAL);
424 }
425
426 goto retry_write;
427 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* send the new start */
429 s3c24xx_i2c_message_start(i2c, i2c->msg);
430 i2c->state = STATE_START;
431 }
432
433 } else {
434 /* send stop */
435
436 s3c24xx_i2c_stop(i2c, 0);
437 }
438 break;
439
440 case STATE_READ:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000441 /* we have a byte of data in the data register, do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * something with it, and then work out wether we are
443 * going to do any more read/write
444 */
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 byte = readb(i2c->regs + S3C2410_IICDS);
447 i2c->msg->buf[i2c->msg_ptr++] = byte;
448
Ben Dooks3d0911b2008-10-31 16:10:24 +0000449 prepare_read:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (is_msglast(i2c)) {
451 /* last byte of buffer */
452
453 if (is_lastmsg(i2c))
454 s3c24xx_i2c_disable_ack(i2c);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 } else if (is_msgend(i2c)) {
457 /* ok, we've read the entire buffer, see if there
458 * is anything else we need to do */
459
460 if (is_lastmsg(i2c)) {
461 /* last message, send stop and complete */
462 dev_dbg(i2c->dev, "READ: Send Stop\n");
463
464 s3c24xx_i2c_stop(i2c, 0);
465 } else {
466 /* go to the next transfer */
467 dev_dbg(i2c->dev, "READ: Next Transfer\n");
468
469 i2c->msg_ptr = 0;
470 i2c->msg_idx++;
471 i2c->msg++;
472 }
473 }
474
475 break;
476 }
477
478 /* acknowlegde the IRQ and get back on with the work */
479
480 out_ack:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000481 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 tmp &= ~S3C2410_IICCON_IRQPEND;
483 writel(tmp, i2c->regs + S3C2410_IICCON);
484 out:
485 return ret;
486}
487
488/* s3c24xx_i2c_irq
489 *
490 * top level IRQ servicing routine
491*/
492
David Howells7d12e782006-10-05 14:55:46 +0100493static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct s3c24xx_i2c *i2c = dev_id;
496 unsigned long status;
497 unsigned long tmp;
498
499 status = readl(i2c->regs + S3C2410_IICSTAT);
500
501 if (status & S3C2410_IICSTAT_ARBITR) {
Ben Dooks3d0911b2008-10-31 16:10:24 +0000502 /* deal with arbitration loss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 dev_err(i2c->dev, "deal with arbitration loss\n");
504 }
505
506 if (i2c->state == STATE_IDLE) {
507 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
508
Ben Dooks3d0911b2008-10-31 16:10:24 +0000509 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 tmp &= ~S3C2410_IICCON_IRQPEND;
511 writel(tmp, i2c->regs + S3C2410_IICCON);
512 goto out;
513 }
Ben Dooks3d0911b2008-10-31 16:10:24 +0000514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* pretty much this leaves us with the fact that we've
516 * transmitted or received whatever byte we last sent */
517
Huisung Kang19820512011-06-23 21:37:33 +0900518 i2c_s3c_irq_nextbyte(i2c, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 out:
521 return IRQ_HANDLED;
522}
523
524
525/* s3c24xx_i2c_set_master
526 *
527 * get the i2c bus for a master transaction
528*/
529
530static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
531{
532 unsigned long iicstat;
533 int timeout = 400;
534
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200535 /* the timeout for HDMIPHY is reduced to 10 ms because
536 * the hangup is expected to happen, so waiting 400 ms
537 * causes only unnecessary system hangup
538 */
539 if (i2c->quirks & QUIRK_HDMIPHY)
540 timeout = 10;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 while (timeout-- > 0) {
543 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
546 return 0;
547
548 msleep(1);
549 }
550
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200551 /* hang-up of bus dedicated for HDMIPHY occurred, resetting */
552 if (i2c->quirks & QUIRK_HDMIPHY) {
553 writel(0, i2c->regs + S3C2410_IICCON);
554 writel(0, i2c->regs + S3C2410_IICSTAT);
555 writel(0, i2c->regs + S3C2410_IICDS);
556
557 return 0;
558 }
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return -ETIMEDOUT;
561}
562
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530563/* s3c24xx_i2c_wait_idle
564 *
565 * wait for the i2c bus to become idle.
566*/
567
568static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
569{
570 unsigned long iicstat;
571 ktime_t start, now;
572 unsigned long delay;
573
574 /* ensure the stop has been through the bus */
575
576 dev_dbg(i2c->dev, "waiting for bus idle\n");
577
578 start = now = ktime_get();
579
580 /*
581 * Most of the time, the bus is already idle within a few usec of the
582 * end of a transaction. However, really slow i2c devices can stretch
583 * the clock, delaying STOP generation.
584 *
585 * As a compromise between idle detection latency for the normal, fast
586 * case, and system load in the slow device case, use an exponential
587 * back off in the polling loop, up to 1/10th of the total timeout,
588 * then continue to poll at a constant rate up to the timeout.
589 */
590 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
591 delay = 1;
592 while ((iicstat & S3C2410_IICSTAT_START) &&
593 ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) {
594 usleep_range(delay, 2 * delay);
595 if (delay < S3C2410_IDLE_TIMEOUT / 10)
596 delay <<= 1;
597 now = ktime_get();
598 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
599 }
600
601 if (iicstat & S3C2410_IICSTAT_START)
602 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
603}
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/* s3c24xx_i2c_doxfer
606 *
607 * this starts an i2c transfer
608*/
609
Ben Dooks3d0911b2008-10-31 16:10:24 +0000610static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
611 struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530613 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int ret;
615
Ben Dooksbe44f012008-10-31 16:10:22 +0000616 if (i2c->suspended)
Ben Dooks61c7cff2008-07-28 12:04:07 +0100617 return -EIO;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ret = s3c24xx_i2c_set_master(i2c);
620 if (ret != 0) {
621 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
622 ret = -EAGAIN;
623 goto out;
624 }
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 i2c->msg = msgs;
627 i2c->msg_num = num;
628 i2c->msg_ptr = 0;
629 i2c->msg_idx = 0;
630 i2c->state = STATE_START;
631
632 s3c24xx_i2c_enable_irq(i2c);
633 s3c24xx_i2c_message_start(i2c, msgs);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
636
637 ret = i2c->msg_idx;
638
Ben Dooks3d0911b2008-10-31 16:10:24 +0000639 /* having these next two as dev_err() makes life very
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * noisy when doing an i2cdetect */
641
642 if (timeout == 0)
643 dev_dbg(i2c->dev, "timeout\n");
644 else if (ret != num)
645 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
646
Daniel Kurtz0da2e772012-11-15 17:43:31 +0530647 /* For QUIRK_HDMIPHY, bus is already disabled */
648 if (i2c->quirks & QUIRK_HDMIPHY)
649 goto out;
650
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530651 s3c24xx_i2c_wait_idle(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 out:
654 return ret;
655}
656
657/* s3c24xx_i2c_xfer
658 *
659 * first port of call from the i2c bus code when an message needs
Steven Cole44bbe872005-05-03 18:21:25 -0600660 * transferring across the i2c bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661*/
662
663static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
664 struct i2c_msg *msgs, int num)
665{
666 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
667 int retry;
668 int ret;
669
Mark Brownc62c3ca2012-01-21 13:28:47 +0000670 pm_runtime_get_sync(&adap->dev);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900671 clk_prepare_enable(i2c->clk);
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 for (retry = 0; retry < adap->retries; retry++) {
674
675 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
676
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200677 if (ret != -EAGAIN) {
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900678 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100679 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return ret;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
684
685 udelay(100);
686 }
687
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900688 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100689 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -EREMOTEIO;
691}
692
693/* declare our i2c functionality */
694static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
695{
Mark Brown14674e72012-05-30 10:55:34 +0200696 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
697 I2C_FUNC_PROTOCOL_MANGLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/* i2c bus registration info */
701
Jean Delvare8f9082c2006-09-03 22:39:46 +0200702static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 .master_xfer = s3c24xx_i2c_xfer,
704 .functionality = s3c24xx_i2c_func,
705};
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707/* s3c24xx_i2c_calcdivisor
708 *
709 * return the divisor settings for a given frequency
710*/
711
712static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
713 unsigned int *div1, unsigned int *divs)
714{
715 unsigned int calc_divs = clkin / wanted;
716 unsigned int calc_div1;
717
718 if (calc_divs > (16*16))
719 calc_div1 = 512;
720 else
721 calc_div1 = 16;
722
723 calc_divs += calc_div1-1;
724 calc_divs /= calc_div1;
725
726 if (calc_divs == 0)
727 calc_divs = 1;
728 if (calc_divs > 17)
729 calc_divs = 17;
730
731 *divs = calc_divs;
732 *div1 = calc_div1;
733
734 return clkin / (calc_divs * calc_div1);
735}
736
Ben Dooks61c7cff2008-07-28 12:04:07 +0100737/* s3c24xx_i2c_clockrate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 *
739 * work out a divisor for the user requested frequency setting,
740 * either by the requested frequency, or scanning the acceptable
741 * range of frequencies until something is found
742*/
743
Ben Dooks61c7cff2008-07-28 12:04:07 +0100744static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530746 struct s3c2410_platform_i2c *pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 unsigned long clkin = clk_get_rate(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 unsigned int divs, div1;
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000749 unsigned long target_frequency;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100750 u32 iiccon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Ben Dooks61c7cff2008-07-28 12:04:07 +0100753 i2c->clkrate = clkin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 clkin /= 1000; /* clkin now in KHz */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000755
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000756 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000758 target_frequency = pdata->frequency ? pdata->frequency : 100000;
759
760 target_frequency /= 1000; /* Target frequency now in KHz */
761
762 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
763
764 if (freq > target_frequency) {
765 dev_err(i2c->dev,
766 "Unable to achieve desired frequency %luKHz." \
767 " Lowest achievable %dKHz\n", target_frequency, freq);
768 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 *got = freq;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100772
773 iiccon = readl(i2c->regs + S3C2410_IICCON);
774 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
775 iiccon |= (divs-1);
776
777 if (div1 == 512)
778 iiccon |= S3C2410_IICCON_TXDIV_512;
779
780 writel(iiccon, i2c->regs + S3C2410_IICCON);
781
Karol Lewandowski27452492012-04-23 18:24:00 +0200782 if (i2c->quirks & QUIRK_S3C2440) {
Ben Dooksa192f712009-03-27 10:52:13 +0000783 unsigned long sda_delay;
784
785 if (pdata->sda_delay) {
MyungJoo Ham70313072010-09-30 15:54:46 +0200786 sda_delay = clkin * pdata->sda_delay;
787 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
Ben Dooksa192f712009-03-27 10:52:13 +0000788 sda_delay = DIV_ROUND_UP(sda_delay, 5);
789 if (sda_delay > 3)
790 sda_delay = 3;
791 sda_delay |= S3C2410_IICLC_FILTER_ON;
792 } else
793 sda_delay = 0;
794
795 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
796 writel(sda_delay, i2c->regs + S3C2440_IICLC);
797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 0;
800}
801
Ben Dooks61c7cff2008-07-28 12:04:07 +0100802#ifdef CONFIG_CPU_FREQ
803
804#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
805
806static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
807 unsigned long val, void *data)
808{
809 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100810 unsigned int got;
811 int delta_f;
812 int ret;
813
814 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
815
816 /* if we're post-change and the input clock has slowed down
817 * or at pre-change and the clock is about to speed up, then
818 * adjust our clock rate. <0 is slow, >0 speedup.
819 */
820
821 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
822 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
Daniel Kurtz9bcd04b2012-11-15 17:43:30 +0530823 i2c_lock_adapter(&i2c->adap);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100824 ret = s3c24xx_i2c_clockrate(i2c, &got);
Daniel Kurtz9bcd04b2012-11-15 17:43:30 +0530825 i2c_unlock_adapter(&i2c->adap);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100826
827 if (ret < 0)
828 dev_err(i2c->dev, "cannot find frequency\n");
829 else
830 dev_info(i2c->dev, "setting freq %d\n", got);
831 }
832
833 return 0;
834}
835
836static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
837{
838 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
839
840 return cpufreq_register_notifier(&i2c->freq_transition,
841 CPUFREQ_TRANSITION_NOTIFIER);
842}
843
844static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
845{
846 cpufreq_unregister_notifier(&i2c->freq_transition,
847 CPUFREQ_TRANSITION_NOTIFIER);
848}
849
850#else
851static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
852{
853 return 0;
854}
855
856static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
857{
858}
859#endif
860
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530861#ifdef CONFIG_OF
862static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
863{
864 int idx, gpio, ret;
865
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200866 if (i2c->quirks & QUIRK_NO_GPIO)
867 return 0;
868
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530869 for (idx = 0; idx < 2; idx++) {
870 gpio = of_get_gpio(i2c->dev->of_node, idx);
871 if (!gpio_is_valid(gpio)) {
872 dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
873 goto free_gpio;
874 }
875
876 ret = gpio_request(gpio, "i2c-bus");
877 if (ret) {
878 dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
879 goto free_gpio;
880 }
881 }
882 return 0;
883
884free_gpio:
885 while (--idx >= 0)
886 gpio_free(i2c->gpios[idx]);
887 return -EINVAL;
888}
889
890static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
891{
892 unsigned int idx;
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200893
894 if (i2c->quirks & QUIRK_NO_GPIO)
895 return;
896
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530897 for (idx = 0; idx < 2; idx++)
898 gpio_free(i2c->gpios[idx]);
899}
900#else
901static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
902{
Tushar Behera8ebe6612011-12-09 15:33:55 +0530903 return 0;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530904}
905
906static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
907{
908}
909#endif
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/* s3c24xx_i2c_init
912 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000913 * initialise the controller, set the IO lines and frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914*/
915
916static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
917{
918 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
919 struct s3c2410_platform_i2c *pdata;
920 unsigned int freq;
921
922 /* get the plafrom data */
923
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530924 pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* inititalise the gpio */
927
Ben Dooks8be310a2008-10-31 16:10:25 +0000928 if (pdata->cfg_gpio)
929 pdata->cfg_gpio(to_platform_device(i2c->dev));
Tomasz Figa2693ac62012-11-13 11:33:40 +0100930 else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c))
931 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* write slave address */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
936
937 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
938
Ben Dooks61c7cff2008-07-28 12:04:07 +0100939 writel(iicon, i2c->regs + S3C2410_IICCON);
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* we need to work out the divisors for the clock... */
942
Ben Dooks61c7cff2008-07-28 12:04:07 +0100943 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
944 writel(0, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 dev_err(i2c->dev, "cannot meet bus frequency required\n");
946 return -EINVAL;
947 }
948
949 /* todo - check that the i2c lines aren't being dragged anywhere */
950
951 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
952 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955}
956
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530957#ifdef CONFIG_OF
958/* s3c24xx_i2c_parse_dt
959 *
960 * Parse the device tree node and retreive the platform data.
961*/
962
963static void
964s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
965{
966 struct s3c2410_platform_i2c *pdata = i2c->pdata;
967
968 if (!np)
969 return;
970
971 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
972 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
973 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
974 of_property_read_u32(np, "samsung,i2c-max-bus-freq",
975 (u32 *)&pdata->frequency);
976}
977#else
978static void
979s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
980{
981 return;
982}
983#endif
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/* s3c24xx_i2c_probe
986 *
987 * called by the bus driver when a suitable device is found
988*/
989
Russell King3ae5eae2005-11-09 22:32:44 +0000990static int s3c24xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Ben Dooks692acbd2008-10-31 16:10:28 +0000992 struct s3c24xx_i2c *i2c;
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530993 struct s3c2410_platform_i2c *pdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct resource *res;
995 int ret;
996
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530997 if (!pdev->dev.of_node) {
998 pdata = pdev->dev.platform_data;
999 if (!pdata) {
1000 dev_err(&pdev->dev, "no platform data\n");
1001 return -EINVAL;
1002 }
Ben Dooks6a039ca2008-10-31 16:10:27 +00001003 }
Ben Dooks399dee22008-07-28 12:04:06 +01001004
Mark Brown4ea15572012-01-21 13:28:46 +00001005 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
Ben Dooks692acbd2008-10-31 16:10:28 +00001006 if (!i2c) {
1007 dev_err(&pdev->dev, "no memory for state\n");
1008 return -ENOMEM;
1009 }
1010
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301011 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1012 if (!i2c->pdata) {
1013 ret = -ENOMEM;
1014 goto err_noclk;
1015 }
1016
Karol Lewandowski27452492012-04-23 18:24:00 +02001017 i2c->quirks = s3c24xx_get_device_quirks(pdev);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301018 if (pdata)
1019 memcpy(i2c->pdata, pdata, sizeof(*pdata));
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301020 else
1021 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301022
Ben Dooks692acbd2008-10-31 16:10:28 +00001023 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
1024 i2c->adap.owner = THIS_MODULE;
1025 i2c->adap.algo = &s3c24xx_i2c_algorithm;
1026 i2c->adap.retries = 2;
1027 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1028 i2c->tx_setup = 50;
1029
Ben Dooks692acbd2008-10-31 16:10:28 +00001030 init_waitqueue_head(&i2c->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 /* find the clock and enable it */
1033
Russell King3ae5eae2005-11-09 22:32:44 +00001034 i2c->dev = &pdev->dev;
1035 i2c->clk = clk_get(&pdev->dev, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (IS_ERR(i2c->clk)) {
Russell King3ae5eae2005-11-09 22:32:44 +00001037 dev_err(&pdev->dev, "cannot get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +02001039 goto err_noclk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041
Russell King3ae5eae2005-11-09 22:32:44 +00001042 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001044 clk_prepare_enable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 /* map the registers */
1047
1048 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1049 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001050 dev_err(&pdev->dev, "cannot find IO resource\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +02001052 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Mark Browna72ad452012-11-05 09:33:39 +01001055 i2c->regs = devm_request_and_ioremap(&pdev->dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (i2c->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001058 dev_err(&pdev->dev, "cannot map IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ret = -ENXIO;
Mark Browna72ad452012-11-05 09:33:39 +01001060 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
Mark Browna72ad452012-11-05 09:33:39 +01001063 dev_dbg(&pdev->dev, "registers %p (%p)\n",
1064 i2c->regs, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /* setup info block for the i2c core */
1067
1068 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001069 i2c->adap.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Tomasz Figa2693ac62012-11-13 11:33:40 +01001071 i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* initialise the i2c controller */
1074
1075 ret = s3c24xx_i2c_init(i2c);
1076 if (ret != 0)
Mark Browna72ad452012-11-05 09:33:39 +01001077 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /* find the IRQ for this unit (note, this relies on the init call to
Ben Dooks3d0911b2008-10-31 16:10:24 +00001080 * ensure no current IRQs pending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
1082
Ben Dookse0d1ec92008-10-31 16:10:30 +00001083 i2c->irq = ret = platform_get_irq(pdev, 0);
1084 if (ret <= 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001085 dev_err(&pdev->dev, "cannot find IRQ\n");
Mark Browna72ad452012-11-05 09:33:39 +01001086 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
Yong Zhang43110512011-09-21 17:28:33 +08001089 ret = request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
Ben Dookse0d1ec92008-10-31 16:10:30 +00001090 dev_name(&pdev->dev), i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (ret != 0) {
Ben Dookse0d1ec92008-10-31 16:10:30 +00001093 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
Mark Browna72ad452012-11-05 09:33:39 +01001094 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
Ben Dooks61c7cff2008-07-28 12:04:07 +01001097 ret = s3c24xx_i2c_register_cpufreq(i2c);
1098 if (ret < 0) {
1099 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
1100 goto err_irq;
1101 }
1102
Ben Dooks399dee22008-07-28 12:04:06 +01001103 /* Note, previous versions of the driver used i2c_add_adapter()
1104 * to add the bus at any number. We now pass the bus number via
1105 * the platform data, so if unset it will now default to always
1106 * being bus 0.
1107 */
1108
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301109 i2c->adap.nr = i2c->pdata->bus_num;
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301110 i2c->adap.dev.of_node = pdev->dev.of_node;
Ben Dooks399dee22008-07-28 12:04:06 +01001111
1112 ret = i2c_add_numbered_adapter(&i2c->adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001114 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
Ben Dooks61c7cff2008-07-28 12:04:07 +01001115 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301118 of_i2c_register_devices(&i2c->adap);
Russell King3ae5eae2005-11-09 22:32:44 +00001119 platform_set_drvdata(pdev, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Mark Brownc62c3ca2012-01-21 13:28:47 +00001121 pm_runtime_enable(&pdev->dev);
1122 pm_runtime_enable(&i2c->adap.dev);
1123
Jean Delvare22e965c2009-01-07 14:29:16 +01001124 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001125 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Ben Dooks61c7cff2008-07-28 12:04:07 +01001128 err_cpufreq:
1129 s3c24xx_i2c_deregister_cpufreq(i2c);
1130
Ben Dooks5b687902007-05-01 23:26:35 +02001131 err_irq:
Ben Dookse0d1ec92008-10-31 16:10:30 +00001132 free_irq(i2c->irq, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Ben Dooks5b687902007-05-01 23:26:35 +02001134 err_clk:
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001135 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001136 clk_put(i2c->clk);
1137
1138 err_noclk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return ret;
1140}
1141
1142/* s3c24xx_i2c_remove
1143 *
1144 * called when device is removed from the bus
1145*/
1146
Russell King3ae5eae2005-11-09 22:32:44 +00001147static int s3c24xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Russell King3ae5eae2005-11-09 22:32:44 +00001149 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Ben Dooks5b687902007-05-01 23:26:35 +02001150
Mark Brownc62c3ca2012-01-21 13:28:47 +00001151 pm_runtime_disable(&i2c->adap.dev);
1152 pm_runtime_disable(&pdev->dev);
1153
Ben Dooks61c7cff2008-07-28 12:04:07 +01001154 s3c24xx_i2c_deregister_cpufreq(i2c);
1155
Ben Dooks5b687902007-05-01 23:26:35 +02001156 i2c_del_adapter(&i2c->adap);
Ben Dookse0d1ec92008-10-31 16:10:30 +00001157 free_irq(i2c->irq, i2c);
Ben Dooks5b687902007-05-01 23:26:35 +02001158
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001159 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001160 clk_put(i2c->clk);
1161
Tomasz Figa2693ac62012-11-13 11:33:40 +01001162 if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
1163 s3c24xx_i2c_dt_gpio_free(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 return 0;
1166}
1167
Mark Brown2935e0e2012-11-05 09:33:38 +01001168#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001169static int s3c24xx_i2c_suspend_noirq(struct device *dev)
Ben Dooksbe44f012008-10-31 16:10:22 +00001170{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001171 struct platform_device *pdev = to_platform_device(dev);
1172 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1173
Ben Dooksbe44f012008-10-31 16:10:22 +00001174 i2c->suspended = 1;
Magnus Damm6a6c61892009-07-08 13:22:47 +02001175
Ben Dooksbe44f012008-10-31 16:10:22 +00001176 return 0;
1177}
1178
Magnus Damm6a6c61892009-07-08 13:22:47 +02001179static int s3c24xx_i2c_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001181 struct platform_device *pdev = to_platform_device(dev);
1182 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Russell King9480e302005-10-28 09:52:56 -07001183
Ben Dooksbe44f012008-10-31 16:10:22 +00001184 i2c->suspended = 0;
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001185 clk_prepare_enable(i2c->clk);
Ben Dooksbe44f012008-10-31 16:10:22 +00001186 s3c24xx_i2c_init(i2c);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001187 clk_disable_unprepare(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 return 0;
1190}
Mark Brown2935e0e2012-11-05 09:33:38 +01001191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Mark Brown2935e0e2012-11-05 09:33:38 +01001193#ifdef CONFIG_PM
Alexey Dobriyan47145212009-12-14 18:00:08 -08001194static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
Mark Brown2935e0e2012-11-05 09:33:38 +01001195#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001196 .suspend_noirq = s3c24xx_i2c_suspend_noirq,
1197 .resume = s3c24xx_i2c_resume,
Mark Brown2935e0e2012-11-05 09:33:38 +01001198#endif
Magnus Damm6a6c61892009-07-08 13:22:47 +02001199};
1200
1201#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202#else
Magnus Damm6a6c61892009-07-08 13:22:47 +02001203#define S3C24XX_DEV_PM_OPS NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204#endif
1205
1206/* device driver for platform bus bits */
1207
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001208static struct platform_driver s3c24xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 .probe = s3c24xx_i2c_probe,
1210 .remove = s3c24xx_i2c_remove,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001211 .id_table = s3c24xx_driver_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001212 .driver = {
1213 .owner = THIS_MODULE,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001214 .name = "s3c-i2c",
Magnus Damm6a6c61892009-07-08 13:22:47 +02001215 .pm = S3C24XX_DEV_PM_OPS,
Karol Lewandowski9df7ead2012-03-21 20:11:51 +01001216 .of_match_table = of_match_ptr(s3c24xx_i2c_match),
Russell King3ae5eae2005-11-09 22:32:44 +00001217 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218};
1219
1220static int __init i2c_adap_s3c_init(void)
1221{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001222 return platform_driver_register(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
Mark Brown18dc83a2009-02-26 16:29:22 +00001224subsys_initcall(i2c_adap_s3c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226static void __exit i2c_adap_s3c_exit(void)
1227{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001228 platform_driver_unregister(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230module_exit(i2c_adap_s3c_exit);
1231
1232MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1233MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1234MODULE_LICENSE("GPL");