blob: a44e2130d3e09376a5d21d36440312ae782d9531 [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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 wait_queue_head_t wait;
Karol Lewandowski27452492012-04-23 18:24:00 +020064 unsigned int quirks;
Ben Dooksbe44f012008-10-31 16:10:22 +000065 unsigned int suspended:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 struct i2c_msg *msg;
68 unsigned int msg_num;
69 unsigned int msg_idx;
70 unsigned int msg_ptr;
71
Ben Dookse00a8cd2007-05-01 23:26:35 +020072 unsigned int tx_setup;
Ben Dookse0d1ec92008-10-31 16:10:30 +000073 unsigned int irq;
Ben Dookse00a8cd2007-05-01 23:26:35 +020074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 enum s3c24xx_i2c_state state;
Ben Dooks61c7cff2008-07-28 12:04:07 +010076 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 void __iomem *regs;
79 struct clk *clk;
80 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct i2c_adapter adap;
Ben Dooks61c7cff2008-07-28 12:04:07 +010082
Thomas Abraham4fd81eb2011-09-13 09:46:04 +053083 struct s3c2410_platform_i2c *pdata;
Thomas Abraham5a5f5082011-09-13 09:46:05 +053084 int gpios[2];
Tomasz Figa2693ac62012-11-13 11:33:40 +010085 struct pinctrl *pctrl;
Ben Dooks61c7cff2008-07-28 12:04:07 +010086#ifdef CONFIG_CPU_FREQ
87 struct notifier_block freq_transition;
88#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Karol Lewandowski27452492012-04-23 18:24:00 +020091static struct platform_device_id s3c24xx_driver_ids[] = {
92 {
93 .name = "s3c2410-i2c",
94 .driver_data = 0,
95 }, {
96 .name = "s3c2440-i2c",
97 .driver_data = QUIRK_S3C2440,
Karol Lewandowskiec39ef82012-04-23 18:24:01 +020098 }, {
99 .name = "s3c2440-hdmiphy-i2c",
100 .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
Karol Lewandowski27452492012-04-23 18:24:00 +0200101 }, { },
102};
103MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530105#ifdef CONFIG_OF
Karol Lewandowski27452492012-04-23 18:24:00 +0200106static const struct of_device_id s3c24xx_i2c_match[] = {
107 { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
108 { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200109 { .compatible = "samsung,s3c2440-hdmiphy-i2c",
110 .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
Karol Lewandowski27452492012-04-23 18:24:00 +0200111 {},
112};
113MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530114#endif
115
Karol Lewandowski27452492012-04-23 18:24:00 +0200116/* s3c24xx_get_device_quirks
117 *
118 * Get controller type either from device tree or platform device variant.
119*/
120
121static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
122{
123 if (pdev->dev.of_node) {
124 const struct of_device_id *match;
Karol Lewandowskib900ba42012-05-30 11:43:05 +0200125 match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
Karol Lewandowski27452492012-04-23 18:24:00 +0200126 return (unsigned int)match->data;
127 }
128
129 return platform_get_device_id(pdev)->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/* s3c24xx_i2c_master_complete
133 *
134 * complete the message and wake up the caller, using the given return code,
135 * or zero to mean ok.
136*/
137
138static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
139{
140 dev_dbg(i2c->dev, "master_complete %d\n", ret);
141
142 i2c->msg_ptr = 0;
143 i2c->msg = NULL;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000144 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 i2c->msg_num = 0;
146 if (ret)
147 i2c->msg_idx = ret;
148
149 wake_up(&i2c->wait);
150}
151
152static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
153{
154 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 tmp = readl(i2c->regs + S3C2410_IICCON);
157 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
161{
162 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 tmp = readl(i2c->regs + S3C2410_IICCON);
165 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168/* irq enable/disable functions */
169
170static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
171{
172 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 tmp = readl(i2c->regs + S3C2410_IICCON);
175 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
176}
177
178static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
179{
180 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 tmp = readl(i2c->regs + S3C2410_IICCON);
183 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
184}
185
186
187/* s3c24xx_i2c_message_start
188 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000189 * put the start of a message onto the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190*/
191
Ben Dooks3d0911b2008-10-31 16:10:24 +0000192static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct i2c_msg *msg)
194{
195 unsigned int addr = (msg->addr & 0x7f) << 1;
196 unsigned long stat;
197 unsigned long iiccon;
198
199 stat = 0;
200 stat |= S3C2410_IICSTAT_TXRXEN;
201
202 if (msg->flags & I2C_M_RD) {
203 stat |= S3C2410_IICSTAT_MASTER_RX;
204 addr |= 1;
205 } else
206 stat |= S3C2410_IICSTAT_MASTER_TX;
207
208 if (msg->flags & I2C_M_REV_DIR_ADDR)
209 addr ^= 1;
210
Ben Dooks3d0911b2008-10-31 16:10:24 +0000211 /* todo - check for wether ack wanted or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 s3c24xx_i2c_enable_ack(i2c);
213
214 iiccon = readl(i2c->regs + S3C2410_IICCON);
215 writel(stat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
218 writeb(addr, i2c->regs + S3C2410_IICDS);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000219
Ben Dookse00a8cd2007-05-01 23:26:35 +0200220 /* delay here to ensure the data byte has gotten onto the bus
221 * before the transaction is started */
222
223 ndelay(i2c->tx_setup);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
226 writel(iiccon, i2c->regs + S3C2410_IICCON);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000227
228 stat |= S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 writel(stat, i2c->regs + S3C2410_IICSTAT);
230}
231
232static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
233{
234 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
235
236 dev_dbg(i2c->dev, "STOP\n");
237
Daniel Kurtz0da2e772012-11-15 17:43:31 +0530238 /*
239 * The datasheet says that the STOP sequence should be:
240 * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP')
241 * 2) I2CCON.4 = 0 - Clear IRQPEND
242 * 3) Wait until the stop condition takes effect.
243 * 4*) I2CSTAT.4 = 0 - Clear TXRXEN
244 *
245 * Where, step "4*" is only for buses with the "HDMIPHY" quirk.
246 *
247 * However, after much experimentation, it appears that:
248 * a) normal buses automatically clear BUSY and transition from
249 * Master->Slave when they complete generating a STOP condition.
250 * Therefore, step (3) can be done in doxfer() by polling I2CCON.4
251 * after starting the STOP generation here.
252 * b) HDMIPHY bus does neither, so there is no way to do step 3.
253 * There is no indication when this bus has finished generating
254 * STOP.
255 *
256 * In fact, we have found that as soon as the IRQPEND bit is cleared in
257 * step 2, the HDMIPHY bus generates the STOP condition, and then
258 * immediately starts transferring another data byte, even though the
259 * bus is supposedly stopped. This is presumably because the bus is
260 * still in "Master" mode, and its BUSY bit is still set.
261 *
262 * To avoid these extra post-STOP transactions on HDMI phy devices, we
263 * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly,
264 * instead of first generating a proper STOP condition. This should
265 * float SDA & SCK terminating the transfer. Subsequent transfers
266 * start with a proper START condition, and proceed normally.
267 *
268 * The HDMIPHY bus is an internal bus that always has exactly two
269 * devices, the host as Master and the HDMIPHY device as the slave.
270 * Skipping the STOP condition has been tested on this bus and works.
271 */
272 if (i2c->quirks & QUIRK_HDMIPHY) {
273 /* Stop driving the I2C pins */
274 iicstat &= ~S3C2410_IICSTAT_TXRXEN;
275 } else {
276 /* stop the transfer */
277 iicstat &= ~S3C2410_IICSTAT_START;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 i2c->state = STATE_STOP;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 s3c24xx_i2c_master_complete(i2c, ret);
284 s3c24xx_i2c_disable_irq(i2c);
285}
286
287/* helper functions to determine the current state in the set of
288 * messages we are sending */
289
290/* is_lastmsg()
291 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000292 * returns TRUE if the current message is the last in the set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293*/
294
295static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
296{
297 return i2c->msg_idx >= (i2c->msg_num - 1);
298}
299
300/* is_msglast
301 *
302 * returns TRUE if we this is the last byte in the current message
303*/
304
305static inline int is_msglast(struct s3c24xx_i2c *i2c)
306{
307 return i2c->msg_ptr == i2c->msg->len-1;
308}
309
310/* is_msgend
311 *
312 * returns TRUE if we reached the end of the current message
313*/
314
315static inline int is_msgend(struct s3c24xx_i2c *i2c)
316{
317 return i2c->msg_ptr >= i2c->msg->len;
318}
319
Huisung Kang19820512011-06-23 21:37:33 +0900320/* i2c_s3c_irq_nextbyte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 *
322 * process an interrupt and work out what to do
323 */
324
Huisung Kang19820512011-06-23 21:37:33 +0900325static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned long tmp;
328 unsigned char byte;
329 int ret = 0;
330
331 switch (i2c->state) {
332
333 case STATE_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200334 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 case STATE_STOP:
Harvey Harrison08882d22008-04-22 22:16:47 +0200338 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000339 s3c24xx_i2c_disable_irq(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto out_ack;
341
342 case STATE_START:
343 /* last thing we did was send a start condition on the
344 * bus, or started a new i2c message
345 */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000346
Ben Dooks63f5c282008-07-01 11:59:42 +0100347 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
349 /* ack was not received... */
350
351 dev_dbg(i2c->dev, "ack was not received\n");
Ben Dooks63f5c282008-07-01 11:59:42 +0100352 s3c24xx_i2c_stop(i2c, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto out_ack;
354 }
355
356 if (i2c->msg->flags & I2C_M_RD)
357 i2c->state = STATE_READ;
358 else
359 i2c->state = STATE_WRITE;
360
361 /* terminate the transfer if there is nothing to do
Ben Dooks63f5c282008-07-01 11:59:42 +0100362 * as this is used by the i2c probe to find devices. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
365 s3c24xx_i2c_stop(i2c, 0);
366 goto out_ack;
367 }
368
369 if (i2c->state == STATE_READ)
370 goto prepare_read;
371
Ben Dooks3d0911b2008-10-31 16:10:24 +0000372 /* fall through to the write state, as we will need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * send a byte as well */
374
375 case STATE_WRITE:
376 /* we are writing data to the device... check for the
377 * end of the message, and if so, work out what to do
378 */
379
Ben Dooks27097812008-07-01 11:59:41 +0100380 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
381 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
382 dev_dbg(i2c->dev, "WRITE: No Ack\n");
383
384 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
385 goto out_ack;
386 }
387 }
388
Ben Dooks3d0911b2008-10-31 16:10:24 +0000389 retry_write:
Ben Dooks27097812008-07-01 11:59:41 +0100390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (!is_msgend(i2c)) {
392 byte = i2c->msg->buf[i2c->msg_ptr++];
393 writeb(byte, i2c->regs + S3C2410_IICDS);
Ben Dookse00a8cd2007-05-01 23:26:35 +0200394
395 /* delay after writing the byte to allow the
396 * data setup time on the bus, as writing the
397 * data to the register causes the first bit
398 * to appear on SDA, and SCL will change as
399 * soon as the interrupt is acknowledged */
400
401 ndelay(i2c->tx_setup);
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 } else if (!is_lastmsg(i2c)) {
404 /* we need to go to the next i2c message */
405
406 dev_dbg(i2c->dev, "WRITE: Next Message\n");
407
408 i2c->msg_ptr = 0;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000409 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 i2c->msg++;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* check to see if we need to do another message */
413 if (i2c->msg->flags & I2C_M_NOSTART) {
414
415 if (i2c->msg->flags & I2C_M_RD) {
416 /* cannot do this, the controller
417 * forces us to send a new START
418 * when we change direction */
419
420 s3c24xx_i2c_stop(i2c, -EINVAL);
421 }
422
423 goto retry_write;
424 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* send the new start */
426 s3c24xx_i2c_message_start(i2c, i2c->msg);
427 i2c->state = STATE_START;
428 }
429
430 } else {
431 /* send stop */
432
433 s3c24xx_i2c_stop(i2c, 0);
434 }
435 break;
436
437 case STATE_READ:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000438 /* we have a byte of data in the data register, do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * something with it, and then work out wether we are
440 * going to do any more read/write
441 */
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 byte = readb(i2c->regs + S3C2410_IICDS);
444 i2c->msg->buf[i2c->msg_ptr++] = byte;
445
Ben Dooks3d0911b2008-10-31 16:10:24 +0000446 prepare_read:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (is_msglast(i2c)) {
448 /* last byte of buffer */
449
450 if (is_lastmsg(i2c))
451 s3c24xx_i2c_disable_ack(i2c);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 } else if (is_msgend(i2c)) {
454 /* ok, we've read the entire buffer, see if there
455 * is anything else we need to do */
456
457 if (is_lastmsg(i2c)) {
458 /* last message, send stop and complete */
459 dev_dbg(i2c->dev, "READ: Send Stop\n");
460
461 s3c24xx_i2c_stop(i2c, 0);
462 } else {
463 /* go to the next transfer */
464 dev_dbg(i2c->dev, "READ: Next Transfer\n");
465
466 i2c->msg_ptr = 0;
467 i2c->msg_idx++;
468 i2c->msg++;
469 }
470 }
471
472 break;
473 }
474
475 /* acknowlegde the IRQ and get back on with the work */
476
477 out_ack:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000478 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 tmp &= ~S3C2410_IICCON_IRQPEND;
480 writel(tmp, i2c->regs + S3C2410_IICCON);
481 out:
482 return ret;
483}
484
485/* s3c24xx_i2c_irq
486 *
487 * top level IRQ servicing routine
488*/
489
David Howells7d12e782006-10-05 14:55:46 +0100490static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct s3c24xx_i2c *i2c = dev_id;
493 unsigned long status;
494 unsigned long tmp;
495
496 status = readl(i2c->regs + S3C2410_IICSTAT);
497
498 if (status & S3C2410_IICSTAT_ARBITR) {
Ben Dooks3d0911b2008-10-31 16:10:24 +0000499 /* deal with arbitration loss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 dev_err(i2c->dev, "deal with arbitration loss\n");
501 }
502
503 if (i2c->state == STATE_IDLE) {
504 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
505
Ben Dooks3d0911b2008-10-31 16:10:24 +0000506 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 tmp &= ~S3C2410_IICCON_IRQPEND;
508 writel(tmp, i2c->regs + S3C2410_IICCON);
509 goto out;
510 }
Ben Dooks3d0911b2008-10-31 16:10:24 +0000511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* pretty much this leaves us with the fact that we've
513 * transmitted or received whatever byte we last sent */
514
Huisung Kang19820512011-06-23 21:37:33 +0900515 i2c_s3c_irq_nextbyte(i2c, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 out:
518 return IRQ_HANDLED;
519}
520
521
522/* s3c24xx_i2c_set_master
523 *
524 * get the i2c bus for a master transaction
525*/
526
527static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
528{
529 unsigned long iicstat;
530 int timeout = 400;
531
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200532 /* the timeout for HDMIPHY is reduced to 10 ms because
533 * the hangup is expected to happen, so waiting 400 ms
534 * causes only unnecessary system hangup
535 */
536 if (i2c->quirks & QUIRK_HDMIPHY)
537 timeout = 10;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 while (timeout-- > 0) {
540 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
543 return 0;
544
545 msleep(1);
546 }
547
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200548 /* hang-up of bus dedicated for HDMIPHY occurred, resetting */
549 if (i2c->quirks & QUIRK_HDMIPHY) {
550 writel(0, i2c->regs + S3C2410_IICCON);
551 writel(0, i2c->regs + S3C2410_IICSTAT);
552 writel(0, i2c->regs + S3C2410_IICDS);
553
554 return 0;
555 }
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -ETIMEDOUT;
558}
559
560/* s3c24xx_i2c_doxfer
561 *
562 * this starts an i2c transfer
563*/
564
Ben Dooks3d0911b2008-10-31 16:10:24 +0000565static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
566 struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Mark Brown1bc29622010-04-02 14:15:09 +0100568 unsigned long iicstat, timeout;
569 int spins = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int ret;
571
Ben Dooksbe44f012008-10-31 16:10:22 +0000572 if (i2c->suspended)
Ben Dooks61c7cff2008-07-28 12:04:07 +0100573 return -EIO;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ret = s3c24xx_i2c_set_master(i2c);
576 if (ret != 0) {
577 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
578 ret = -EAGAIN;
579 goto out;
580 }
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 i2c->msg = msgs;
583 i2c->msg_num = num;
584 i2c->msg_ptr = 0;
585 i2c->msg_idx = 0;
586 i2c->state = STATE_START;
587
588 s3c24xx_i2c_enable_irq(i2c);
589 s3c24xx_i2c_message_start(i2c, msgs);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
592
593 ret = i2c->msg_idx;
594
Ben Dooks3d0911b2008-10-31 16:10:24 +0000595 /* having these next two as dev_err() makes life very
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * noisy when doing an i2cdetect */
597
598 if (timeout == 0)
599 dev_dbg(i2c->dev, "timeout\n");
600 else if (ret != num)
601 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
602
Daniel Kurtz0da2e772012-11-15 17:43:31 +0530603 /* For QUIRK_HDMIPHY, bus is already disabled */
604 if (i2c->quirks & QUIRK_HDMIPHY)
605 goto out;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* ensure the stop has been through the bus */
608
Mark Brown1bc29622010-04-02 14:15:09 +0100609 dev_dbg(i2c->dev, "waiting for bus idle\n");
610
611 /* first, try busy waiting briefly */
612 do {
Mark Brown37de03e2011-12-09 20:55:03 +0800613 cpu_relax();
Mark Brown1bc29622010-04-02 14:15:09 +0100614 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
615 } while ((iicstat & S3C2410_IICSTAT_START) && --spins);
616
617 /* if that timed out sleep */
618 if (!spins) {
619 msleep(1);
620 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
621 }
622
623 if (iicstat & S3C2410_IICSTAT_START)
624 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 out:
627 return ret;
628}
629
630/* s3c24xx_i2c_xfer
631 *
632 * first port of call from the i2c bus code when an message needs
Steven Cole44bbe872005-05-03 18:21:25 -0600633 * transferring across the i2c bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634*/
635
636static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
637 struct i2c_msg *msgs, int num)
638{
639 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
640 int retry;
641 int ret;
642
Mark Brownc62c3ca2012-01-21 13:28:47 +0000643 pm_runtime_get_sync(&adap->dev);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900644 clk_prepare_enable(i2c->clk);
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 for (retry = 0; retry < adap->retries; retry++) {
647
648 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
649
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200650 if (ret != -EAGAIN) {
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900651 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100652 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return ret;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
657
658 udelay(100);
659 }
660
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900661 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100662 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -EREMOTEIO;
664}
665
666/* declare our i2c functionality */
667static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
668{
Mark Brown14674e72012-05-30 10:55:34 +0200669 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
670 I2C_FUNC_PROTOCOL_MANGLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673/* i2c bus registration info */
674
Jean Delvare8f9082c2006-09-03 22:39:46 +0200675static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 .master_xfer = s3c24xx_i2c_xfer,
677 .functionality = s3c24xx_i2c_func,
678};
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/* s3c24xx_i2c_calcdivisor
681 *
682 * return the divisor settings for a given frequency
683*/
684
685static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
686 unsigned int *div1, unsigned int *divs)
687{
688 unsigned int calc_divs = clkin / wanted;
689 unsigned int calc_div1;
690
691 if (calc_divs > (16*16))
692 calc_div1 = 512;
693 else
694 calc_div1 = 16;
695
696 calc_divs += calc_div1-1;
697 calc_divs /= calc_div1;
698
699 if (calc_divs == 0)
700 calc_divs = 1;
701 if (calc_divs > 17)
702 calc_divs = 17;
703
704 *divs = calc_divs;
705 *div1 = calc_div1;
706
707 return clkin / (calc_divs * calc_div1);
708}
709
Ben Dooks61c7cff2008-07-28 12:04:07 +0100710/* s3c24xx_i2c_clockrate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 *
712 * work out a divisor for the user requested frequency setting,
713 * either by the requested frequency, or scanning the acceptable
714 * range of frequencies until something is found
715*/
716
Ben Dooks61c7cff2008-07-28 12:04:07 +0100717static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530719 struct s3c2410_platform_i2c *pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unsigned long clkin = clk_get_rate(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned int divs, div1;
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000722 unsigned long target_frequency;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100723 u32 iiccon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Ben Dooks61c7cff2008-07-28 12:04:07 +0100726 i2c->clkrate = clkin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 clkin /= 1000; /* clkin now in KHz */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000728
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000729 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000731 target_frequency = pdata->frequency ? pdata->frequency : 100000;
732
733 target_frequency /= 1000; /* Target frequency now in KHz */
734
735 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
736
737 if (freq > target_frequency) {
738 dev_err(i2c->dev,
739 "Unable to achieve desired frequency %luKHz." \
740 " Lowest achievable %dKHz\n", target_frequency, freq);
741 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 *got = freq;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100745
746 iiccon = readl(i2c->regs + S3C2410_IICCON);
747 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
748 iiccon |= (divs-1);
749
750 if (div1 == 512)
751 iiccon |= S3C2410_IICCON_TXDIV_512;
752
753 writel(iiccon, i2c->regs + S3C2410_IICCON);
754
Karol Lewandowski27452492012-04-23 18:24:00 +0200755 if (i2c->quirks & QUIRK_S3C2440) {
Ben Dooksa192f712009-03-27 10:52:13 +0000756 unsigned long sda_delay;
757
758 if (pdata->sda_delay) {
MyungJoo Ham70313072010-09-30 15:54:46 +0200759 sda_delay = clkin * pdata->sda_delay;
760 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
Ben Dooksa192f712009-03-27 10:52:13 +0000761 sda_delay = DIV_ROUND_UP(sda_delay, 5);
762 if (sda_delay > 3)
763 sda_delay = 3;
764 sda_delay |= S3C2410_IICLC_FILTER_ON;
765 } else
766 sda_delay = 0;
767
768 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
769 writel(sda_delay, i2c->regs + S3C2440_IICLC);
770 }
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return 0;
773}
774
Ben Dooks61c7cff2008-07-28 12:04:07 +0100775#ifdef CONFIG_CPU_FREQ
776
777#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
778
779static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
780 unsigned long val, void *data)
781{
782 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100783 unsigned int got;
784 int delta_f;
785 int ret;
786
787 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
788
789 /* if we're post-change and the input clock has slowed down
790 * or at pre-change and the clock is about to speed up, then
791 * adjust our clock rate. <0 is slow, >0 speedup.
792 */
793
794 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
795 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
Daniel Kurtz9bcd04b2012-11-15 17:43:30 +0530796 i2c_lock_adapter(&i2c->adap);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100797 ret = s3c24xx_i2c_clockrate(i2c, &got);
Daniel Kurtz9bcd04b2012-11-15 17:43:30 +0530798 i2c_unlock_adapter(&i2c->adap);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100799
800 if (ret < 0)
801 dev_err(i2c->dev, "cannot find frequency\n");
802 else
803 dev_info(i2c->dev, "setting freq %d\n", got);
804 }
805
806 return 0;
807}
808
809static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
810{
811 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
812
813 return cpufreq_register_notifier(&i2c->freq_transition,
814 CPUFREQ_TRANSITION_NOTIFIER);
815}
816
817static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
818{
819 cpufreq_unregister_notifier(&i2c->freq_transition,
820 CPUFREQ_TRANSITION_NOTIFIER);
821}
822
823#else
824static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
825{
826 return 0;
827}
828
829static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
830{
831}
832#endif
833
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530834#ifdef CONFIG_OF
835static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
836{
837 int idx, gpio, ret;
838
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200839 if (i2c->quirks & QUIRK_NO_GPIO)
840 return 0;
841
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530842 for (idx = 0; idx < 2; idx++) {
843 gpio = of_get_gpio(i2c->dev->of_node, idx);
844 if (!gpio_is_valid(gpio)) {
845 dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
846 goto free_gpio;
847 }
848
849 ret = gpio_request(gpio, "i2c-bus");
850 if (ret) {
851 dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
852 goto free_gpio;
853 }
854 }
855 return 0;
856
857free_gpio:
858 while (--idx >= 0)
859 gpio_free(i2c->gpios[idx]);
860 return -EINVAL;
861}
862
863static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
864{
865 unsigned int idx;
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200866
867 if (i2c->quirks & QUIRK_NO_GPIO)
868 return;
869
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530870 for (idx = 0; idx < 2; idx++)
871 gpio_free(i2c->gpios[idx]);
872}
873#else
874static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
875{
Tushar Behera8ebe6612011-12-09 15:33:55 +0530876 return 0;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530877}
878
879static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
880{
881}
882#endif
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/* s3c24xx_i2c_init
885 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000886 * initialise the controller, set the IO lines and frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887*/
888
889static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
890{
891 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
892 struct s3c2410_platform_i2c *pdata;
893 unsigned int freq;
894
895 /* get the plafrom data */
896
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530897 pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /* inititalise the gpio */
900
Ben Dooks8be310a2008-10-31 16:10:25 +0000901 if (pdata->cfg_gpio)
902 pdata->cfg_gpio(to_platform_device(i2c->dev));
Tomasz Figa2693ac62012-11-13 11:33:40 +0100903 else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c))
904 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* write slave address */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
909
910 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
911
Ben Dooks61c7cff2008-07-28 12:04:07 +0100912 writel(iicon, i2c->regs + S3C2410_IICCON);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* we need to work out the divisors for the clock... */
915
Ben Dooks61c7cff2008-07-28 12:04:07 +0100916 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
917 writel(0, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dev_err(i2c->dev, "cannot meet bus frequency required\n");
919 return -EINVAL;
920 }
921
922 /* todo - check that the i2c lines aren't being dragged anywhere */
923
924 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
925 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return 0;
928}
929
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530930#ifdef CONFIG_OF
931/* s3c24xx_i2c_parse_dt
932 *
933 * Parse the device tree node and retreive the platform data.
934*/
935
936static void
937s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
938{
939 struct s3c2410_platform_i2c *pdata = i2c->pdata;
940
941 if (!np)
942 return;
943
944 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
945 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
946 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
947 of_property_read_u32(np, "samsung,i2c-max-bus-freq",
948 (u32 *)&pdata->frequency);
949}
950#else
951static void
952s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
953{
954 return;
955}
956#endif
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/* s3c24xx_i2c_probe
959 *
960 * called by the bus driver when a suitable device is found
961*/
962
Russell King3ae5eae2005-11-09 22:32:44 +0000963static int s3c24xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Ben Dooks692acbd2008-10-31 16:10:28 +0000965 struct s3c24xx_i2c *i2c;
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530966 struct s3c2410_platform_i2c *pdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 struct resource *res;
968 int ret;
969
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530970 if (!pdev->dev.of_node) {
971 pdata = pdev->dev.platform_data;
972 if (!pdata) {
973 dev_err(&pdev->dev, "no platform data\n");
974 return -EINVAL;
975 }
Ben Dooks6a039ca2008-10-31 16:10:27 +0000976 }
Ben Dooks399dee22008-07-28 12:04:06 +0100977
Mark Brown4ea15572012-01-21 13:28:46 +0000978 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
Ben Dooks692acbd2008-10-31 16:10:28 +0000979 if (!i2c) {
980 dev_err(&pdev->dev, "no memory for state\n");
981 return -ENOMEM;
982 }
983
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530984 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
985 if (!i2c->pdata) {
986 ret = -ENOMEM;
987 goto err_noclk;
988 }
989
Karol Lewandowski27452492012-04-23 18:24:00 +0200990 i2c->quirks = s3c24xx_get_device_quirks(pdev);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530991 if (pdata)
992 memcpy(i2c->pdata, pdata, sizeof(*pdata));
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530993 else
994 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530995
Ben Dooks692acbd2008-10-31 16:10:28 +0000996 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
997 i2c->adap.owner = THIS_MODULE;
998 i2c->adap.algo = &s3c24xx_i2c_algorithm;
999 i2c->adap.retries = 2;
1000 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1001 i2c->tx_setup = 50;
1002
Ben Dooks692acbd2008-10-31 16:10:28 +00001003 init_waitqueue_head(&i2c->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 /* find the clock and enable it */
1006
Russell King3ae5eae2005-11-09 22:32:44 +00001007 i2c->dev = &pdev->dev;
1008 i2c->clk = clk_get(&pdev->dev, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (IS_ERR(i2c->clk)) {
Russell King3ae5eae2005-11-09 22:32:44 +00001010 dev_err(&pdev->dev, "cannot get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +02001012 goto err_noclk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
Russell King3ae5eae2005-11-09 22:32:44 +00001015 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001017 clk_prepare_enable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /* map the registers */
1020
1021 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1022 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001023 dev_err(&pdev->dev, "cannot find IO resource\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 ret = -ENOENT;
Ben Dooks5b687902007-05-01 23:26:35 +02001025 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
Mark Browna72ad452012-11-05 09:33:39 +01001028 i2c->regs = devm_request_and_ioremap(&pdev->dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 if (i2c->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001031 dev_err(&pdev->dev, "cannot map IO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ret = -ENXIO;
Mark Browna72ad452012-11-05 09:33:39 +01001033 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
Mark Browna72ad452012-11-05 09:33:39 +01001036 dev_dbg(&pdev->dev, "registers %p (%p)\n",
1037 i2c->regs, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /* setup info block for the i2c core */
1040
1041 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001042 i2c->adap.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Tomasz Figa2693ac62012-11-13 11:33:40 +01001044 i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /* initialise the i2c controller */
1047
1048 ret = s3c24xx_i2c_init(i2c);
1049 if (ret != 0)
Mark Browna72ad452012-11-05 09:33:39 +01001050 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /* find the IRQ for this unit (note, this relies on the init call to
Ben Dooks3d0911b2008-10-31 16:10:24 +00001053 * ensure no current IRQs pending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
1055
Ben Dookse0d1ec92008-10-31 16:10:30 +00001056 i2c->irq = ret = platform_get_irq(pdev, 0);
1057 if (ret <= 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001058 dev_err(&pdev->dev, "cannot find IRQ\n");
Mark Browna72ad452012-11-05 09:33:39 +01001059 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061
Yong Zhang43110512011-09-21 17:28:33 +08001062 ret = request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
Ben Dookse0d1ec92008-10-31 16:10:30 +00001063 dev_name(&pdev->dev), i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (ret != 0) {
Ben Dookse0d1ec92008-10-31 16:10:30 +00001066 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
Mark Browna72ad452012-11-05 09:33:39 +01001067 goto err_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
Ben Dooks61c7cff2008-07-28 12:04:07 +01001070 ret = s3c24xx_i2c_register_cpufreq(i2c);
1071 if (ret < 0) {
1072 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
1073 goto err_irq;
1074 }
1075
Ben Dooks399dee22008-07-28 12:04:06 +01001076 /* Note, previous versions of the driver used i2c_add_adapter()
1077 * to add the bus at any number. We now pass the bus number via
1078 * the platform data, so if unset it will now default to always
1079 * being bus 0.
1080 */
1081
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301082 i2c->adap.nr = i2c->pdata->bus_num;
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301083 i2c->adap.dev.of_node = pdev->dev.of_node;
Ben Dooks399dee22008-07-28 12:04:06 +01001084
1085 ret = i2c_add_numbered_adapter(&i2c->adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001087 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
Ben Dooks61c7cff2008-07-28 12:04:07 +01001088 goto err_cpufreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301091 of_i2c_register_devices(&i2c->adap);
Russell King3ae5eae2005-11-09 22:32:44 +00001092 platform_set_drvdata(pdev, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Mark Brownc62c3ca2012-01-21 13:28:47 +00001094 pm_runtime_enable(&pdev->dev);
1095 pm_runtime_enable(&i2c->adap.dev);
1096
Jean Delvare22e965c2009-01-07 14:29:16 +01001097 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001098 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Ben Dooks61c7cff2008-07-28 12:04:07 +01001101 err_cpufreq:
1102 s3c24xx_i2c_deregister_cpufreq(i2c);
1103
Ben Dooks5b687902007-05-01 23:26:35 +02001104 err_irq:
Ben Dookse0d1ec92008-10-31 16:10:30 +00001105 free_irq(i2c->irq, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Ben Dooks5b687902007-05-01 23:26:35 +02001107 err_clk:
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001108 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001109 clk_put(i2c->clk);
1110
1111 err_noclk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return ret;
1113}
1114
1115/* s3c24xx_i2c_remove
1116 *
1117 * called when device is removed from the bus
1118*/
1119
Russell King3ae5eae2005-11-09 22:32:44 +00001120static int s3c24xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Russell King3ae5eae2005-11-09 22:32:44 +00001122 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Ben Dooks5b687902007-05-01 23:26:35 +02001123
Mark Brownc62c3ca2012-01-21 13:28:47 +00001124 pm_runtime_disable(&i2c->adap.dev);
1125 pm_runtime_disable(&pdev->dev);
1126
Ben Dooks61c7cff2008-07-28 12:04:07 +01001127 s3c24xx_i2c_deregister_cpufreq(i2c);
1128
Ben Dooks5b687902007-05-01 23:26:35 +02001129 i2c_del_adapter(&i2c->adap);
Ben Dookse0d1ec92008-10-31 16:10:30 +00001130 free_irq(i2c->irq, i2c);
Ben Dooks5b687902007-05-01 23:26:35 +02001131
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001132 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001133 clk_put(i2c->clk);
1134
Tomasz Figa2693ac62012-11-13 11:33:40 +01001135 if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
1136 s3c24xx_i2c_dt_gpio_free(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 return 0;
1139}
1140
Mark Brown2935e0e2012-11-05 09:33:38 +01001141#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001142static int s3c24xx_i2c_suspend_noirq(struct device *dev)
Ben Dooksbe44f012008-10-31 16:10:22 +00001143{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001144 struct platform_device *pdev = to_platform_device(dev);
1145 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1146
Ben Dooksbe44f012008-10-31 16:10:22 +00001147 i2c->suspended = 1;
Magnus Damm6a6c61892009-07-08 13:22:47 +02001148
Ben Dooksbe44f012008-10-31 16:10:22 +00001149 return 0;
1150}
1151
Magnus Damm6a6c61892009-07-08 13:22:47 +02001152static int s3c24xx_i2c_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001154 struct platform_device *pdev = to_platform_device(dev);
1155 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Russell King9480e302005-10-28 09:52:56 -07001156
Ben Dooksbe44f012008-10-31 16:10:22 +00001157 i2c->suspended = 0;
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001158 clk_prepare_enable(i2c->clk);
Ben Dooksbe44f012008-10-31 16:10:22 +00001159 s3c24xx_i2c_init(i2c);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001160 clk_disable_unprepare(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 return 0;
1163}
Mark Brown2935e0e2012-11-05 09:33:38 +01001164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Mark Brown2935e0e2012-11-05 09:33:38 +01001166#ifdef CONFIG_PM
Alexey Dobriyan47145212009-12-14 18:00:08 -08001167static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
Mark Brown2935e0e2012-11-05 09:33:38 +01001168#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001169 .suspend_noirq = s3c24xx_i2c_suspend_noirq,
1170 .resume = s3c24xx_i2c_resume,
Mark Brown2935e0e2012-11-05 09:33:38 +01001171#endif
Magnus Damm6a6c61892009-07-08 13:22:47 +02001172};
1173
1174#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175#else
Magnus Damm6a6c61892009-07-08 13:22:47 +02001176#define S3C24XX_DEV_PM_OPS NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177#endif
1178
1179/* device driver for platform bus bits */
1180
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001181static struct platform_driver s3c24xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 .probe = s3c24xx_i2c_probe,
1183 .remove = s3c24xx_i2c_remove,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001184 .id_table = s3c24xx_driver_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001185 .driver = {
1186 .owner = THIS_MODULE,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001187 .name = "s3c-i2c",
Magnus Damm6a6c61892009-07-08 13:22:47 +02001188 .pm = S3C24XX_DEV_PM_OPS,
Karol Lewandowski9df7ead2012-03-21 20:11:51 +01001189 .of_match_table = of_match_ptr(s3c24xx_i2c_match),
Russell King3ae5eae2005-11-09 22:32:44 +00001190 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191};
1192
1193static int __init i2c_adap_s3c_init(void)
1194{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001195 return platform_driver_register(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
Mark Brown18dc83a2009-02-26 16:29:22 +00001197subsys_initcall(i2c_adap_s3c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199static void __exit i2c_adap_s3c_exit(void)
1200{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001201 platform_driver_unregister(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203module_exit(i2c_adap_s3c_exit);
1204
1205MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1206MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1207MODULE_LICENSE("GPL");