blob: f6b880ba1932e9058a55ef8eca542629d0ea4ffd [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) },
Giridhar Maruthyfaf93ff2013-01-24 11:27:51 -0800114 { .compatible = "samsung,exynos5440-i2c",
115 .data = (void *)(QUIRK_S3C2440 | QUIRK_NO_GPIO) },
Karol Lewandowski27452492012-04-23 18:24:00 +0200116 {},
117};
118MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530119#endif
120
Karol Lewandowski27452492012-04-23 18:24:00 +0200121/* s3c24xx_get_device_quirks
122 *
123 * Get controller type either from device tree or platform device variant.
124*/
125
126static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
127{
128 if (pdev->dev.of_node) {
129 const struct of_device_id *match;
Karol Lewandowskib900ba42012-05-30 11:43:05 +0200130 match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
Karol Lewandowski27452492012-04-23 18:24:00 +0200131 return (unsigned int)match->data;
132 }
133
134 return platform_get_device_id(pdev)->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* s3c24xx_i2c_master_complete
138 *
139 * complete the message and wake up the caller, using the given return code,
140 * or zero to mean ok.
141*/
142
143static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
144{
145 dev_dbg(i2c->dev, "master_complete %d\n", ret);
146
147 i2c->msg_ptr = 0;
148 i2c->msg = NULL;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000149 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 i2c->msg_num = 0;
151 if (ret)
152 i2c->msg_idx = ret;
153
154 wake_up(&i2c->wait);
155}
156
157static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
158{
159 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 tmp = readl(i2c->regs + S3C2410_IICCON);
162 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
166{
167 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 tmp = readl(i2c->regs + S3C2410_IICCON);
170 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/* irq enable/disable functions */
174
175static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
176{
177 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 tmp = readl(i2c->regs + S3C2410_IICCON);
180 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
181}
182
183static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
184{
185 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 tmp = readl(i2c->regs + S3C2410_IICCON);
188 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
189}
190
191
192/* s3c24xx_i2c_message_start
193 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000194 * put the start of a message onto the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195*/
196
Ben Dooks3d0911b2008-10-31 16:10:24 +0000197static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct i2c_msg *msg)
199{
200 unsigned int addr = (msg->addr & 0x7f) << 1;
201 unsigned long stat;
202 unsigned long iiccon;
203
204 stat = 0;
205 stat |= S3C2410_IICSTAT_TXRXEN;
206
207 if (msg->flags & I2C_M_RD) {
208 stat |= S3C2410_IICSTAT_MASTER_RX;
209 addr |= 1;
210 } else
211 stat |= S3C2410_IICSTAT_MASTER_TX;
212
213 if (msg->flags & I2C_M_REV_DIR_ADDR)
214 addr ^= 1;
215
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400216 /* todo - check for whether ack wanted or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 s3c24xx_i2c_enable_ack(i2c);
218
219 iiccon = readl(i2c->regs + S3C2410_IICCON);
220 writel(stat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
223 writeb(addr, i2c->regs + S3C2410_IICDS);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000224
Ben Dookse00a8cd2007-05-01 23:26:35 +0200225 /* delay here to ensure the data byte has gotten onto the bus
226 * before the transaction is started */
227
228 ndelay(i2c->tx_setup);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
231 writel(iiccon, i2c->regs + S3C2410_IICCON);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000232
233 stat |= S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 writel(stat, i2c->regs + S3C2410_IICSTAT);
235}
236
237static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
238{
239 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
240
241 dev_dbg(i2c->dev, "STOP\n");
242
Daniel Kurtz0da2e772012-11-15 17:43:31 +0530243 /*
244 * The datasheet says that the STOP sequence should be:
245 * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP')
246 * 2) I2CCON.4 = 0 - Clear IRQPEND
247 * 3) Wait until the stop condition takes effect.
248 * 4*) I2CSTAT.4 = 0 - Clear TXRXEN
249 *
250 * Where, step "4*" is only for buses with the "HDMIPHY" quirk.
251 *
252 * However, after much experimentation, it appears that:
253 * a) normal buses automatically clear BUSY and transition from
254 * Master->Slave when they complete generating a STOP condition.
255 * Therefore, step (3) can be done in doxfer() by polling I2CCON.4
256 * after starting the STOP generation here.
257 * b) HDMIPHY bus does neither, so there is no way to do step 3.
258 * There is no indication when this bus has finished generating
259 * STOP.
260 *
261 * In fact, we have found that as soon as the IRQPEND bit is cleared in
262 * step 2, the HDMIPHY bus generates the STOP condition, and then
263 * immediately starts transferring another data byte, even though the
264 * bus is supposedly stopped. This is presumably because the bus is
265 * still in "Master" mode, and its BUSY bit is still set.
266 *
267 * To avoid these extra post-STOP transactions on HDMI phy devices, we
268 * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly,
269 * instead of first generating a proper STOP condition. This should
270 * float SDA & SCK terminating the transfer. Subsequent transfers
271 * start with a proper START condition, and proceed normally.
272 *
273 * The HDMIPHY bus is an internal bus that always has exactly two
274 * devices, the host as Master and the HDMIPHY device as the slave.
275 * Skipping the STOP condition has been tested on this bus and works.
276 */
277 if (i2c->quirks & QUIRK_HDMIPHY) {
278 /* Stop driving the I2C pins */
279 iicstat &= ~S3C2410_IICSTAT_TXRXEN;
280 } else {
281 /* stop the transfer */
282 iicstat &= ~S3C2410_IICSTAT_START;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 i2c->state = STATE_STOP;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 s3c24xx_i2c_master_complete(i2c, ret);
289 s3c24xx_i2c_disable_irq(i2c);
290}
291
292/* helper functions to determine the current state in the set of
293 * messages we are sending */
294
295/* is_lastmsg()
296 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000297 * returns TRUE if the current message is the last in the set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298*/
299
300static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
301{
302 return i2c->msg_idx >= (i2c->msg_num - 1);
303}
304
305/* is_msglast
306 *
307 * returns TRUE if we this is the last byte in the current message
308*/
309
310static inline int is_msglast(struct s3c24xx_i2c *i2c)
311{
312 return i2c->msg_ptr == i2c->msg->len-1;
313}
314
315/* is_msgend
316 *
317 * returns TRUE if we reached the end of the current message
318*/
319
320static inline int is_msgend(struct s3c24xx_i2c *i2c)
321{
322 return i2c->msg_ptr >= i2c->msg->len;
323}
324
Huisung Kang19820512011-06-23 21:37:33 +0900325/* i2c_s3c_irq_nextbyte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *
327 * process an interrupt and work out what to do
328 */
329
Huisung Kang19820512011-06-23 21:37:33 +0900330static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 unsigned long tmp;
333 unsigned char byte;
334 int ret = 0;
335
336 switch (i2c->state) {
337
338 case STATE_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200339 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 case STATE_STOP:
Harvey Harrison08882d22008-04-22 22:16:47 +0200343 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000344 s3c24xx_i2c_disable_irq(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto out_ack;
346
347 case STATE_START:
348 /* last thing we did was send a start condition on the
349 * bus, or started a new i2c message
350 */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000351
Ben Dooks63f5c282008-07-01 11:59:42 +0100352 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
354 /* ack was not received... */
355
356 dev_dbg(i2c->dev, "ack was not received\n");
Ben Dooks63f5c282008-07-01 11:59:42 +0100357 s3c24xx_i2c_stop(i2c, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto out_ack;
359 }
360
361 if (i2c->msg->flags & I2C_M_RD)
362 i2c->state = STATE_READ;
363 else
364 i2c->state = STATE_WRITE;
365
366 /* terminate the transfer if there is nothing to do
Ben Dooks63f5c282008-07-01 11:59:42 +0100367 * as this is used by the i2c probe to find devices. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
370 s3c24xx_i2c_stop(i2c, 0);
371 goto out_ack;
372 }
373
374 if (i2c->state == STATE_READ)
375 goto prepare_read;
376
Ben Dooks3d0911b2008-10-31 16:10:24 +0000377 /* fall through to the write state, as we will need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * send a byte as well */
379
380 case STATE_WRITE:
381 /* we are writing data to the device... check for the
382 * end of the message, and if so, work out what to do
383 */
384
Ben Dooks27097812008-07-01 11:59:41 +0100385 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
386 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
387 dev_dbg(i2c->dev, "WRITE: No Ack\n");
388
389 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
390 goto out_ack;
391 }
392 }
393
Ben Dooks3d0911b2008-10-31 16:10:24 +0000394 retry_write:
Ben Dooks27097812008-07-01 11:59:41 +0100395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!is_msgend(i2c)) {
397 byte = i2c->msg->buf[i2c->msg_ptr++];
398 writeb(byte, i2c->regs + S3C2410_IICDS);
Ben Dookse00a8cd2007-05-01 23:26:35 +0200399
400 /* delay after writing the byte to allow the
401 * data setup time on the bus, as writing the
402 * data to the register causes the first bit
403 * to appear on SDA, and SCL will change as
404 * soon as the interrupt is acknowledged */
405
406 ndelay(i2c->tx_setup);
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 } else if (!is_lastmsg(i2c)) {
409 /* we need to go to the next i2c message */
410
411 dev_dbg(i2c->dev, "WRITE: Next Message\n");
412
413 i2c->msg_ptr = 0;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000414 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 i2c->msg++;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* check to see if we need to do another message */
418 if (i2c->msg->flags & I2C_M_NOSTART) {
419
420 if (i2c->msg->flags & I2C_M_RD) {
421 /* cannot do this, the controller
422 * forces us to send a new START
423 * when we change direction */
424
425 s3c24xx_i2c_stop(i2c, -EINVAL);
426 }
427
428 goto retry_write;
429 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* send the new start */
431 s3c24xx_i2c_message_start(i2c, i2c->msg);
432 i2c->state = STATE_START;
433 }
434
435 } else {
436 /* send stop */
437
438 s3c24xx_i2c_stop(i2c, 0);
439 }
440 break;
441
442 case STATE_READ:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000443 /* we have a byte of data in the data register, do
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400444 * something with it, and then work out whether we are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * going to do any more read/write
446 */
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 byte = readb(i2c->regs + S3C2410_IICDS);
449 i2c->msg->buf[i2c->msg_ptr++] = byte;
450
Ben Dooks3d0911b2008-10-31 16:10:24 +0000451 prepare_read:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (is_msglast(i2c)) {
453 /* last byte of buffer */
454
455 if (is_lastmsg(i2c))
456 s3c24xx_i2c_disable_ack(i2c);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 } else if (is_msgend(i2c)) {
459 /* ok, we've read the entire buffer, see if there
460 * is anything else we need to do */
461
462 if (is_lastmsg(i2c)) {
463 /* last message, send stop and complete */
464 dev_dbg(i2c->dev, "READ: Send Stop\n");
465
466 s3c24xx_i2c_stop(i2c, 0);
467 } else {
468 /* go to the next transfer */
469 dev_dbg(i2c->dev, "READ: Next Transfer\n");
470
471 i2c->msg_ptr = 0;
472 i2c->msg_idx++;
473 i2c->msg++;
474 }
475 }
476
477 break;
478 }
479
480 /* acknowlegde the IRQ and get back on with the work */
481
482 out_ack:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000483 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 tmp &= ~S3C2410_IICCON_IRQPEND;
485 writel(tmp, i2c->regs + S3C2410_IICCON);
486 out:
487 return ret;
488}
489
490/* s3c24xx_i2c_irq
491 *
492 * top level IRQ servicing routine
493*/
494
David Howells7d12e782006-10-05 14:55:46 +0100495static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct s3c24xx_i2c *i2c = dev_id;
498 unsigned long status;
499 unsigned long tmp;
500
501 status = readl(i2c->regs + S3C2410_IICSTAT);
502
503 if (status & S3C2410_IICSTAT_ARBITR) {
Ben Dooks3d0911b2008-10-31 16:10:24 +0000504 /* deal with arbitration loss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 dev_err(i2c->dev, "deal with arbitration loss\n");
506 }
507
508 if (i2c->state == STATE_IDLE) {
509 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
510
Ben Dooks3d0911b2008-10-31 16:10:24 +0000511 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 tmp &= ~S3C2410_IICCON_IRQPEND;
513 writel(tmp, i2c->regs + S3C2410_IICCON);
514 goto out;
515 }
Ben Dooks3d0911b2008-10-31 16:10:24 +0000516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* pretty much this leaves us with the fact that we've
518 * transmitted or received whatever byte we last sent */
519
Huisung Kang19820512011-06-23 21:37:33 +0900520 i2c_s3c_irq_nextbyte(i2c, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 out:
523 return IRQ_HANDLED;
524}
525
526
527/* s3c24xx_i2c_set_master
528 *
529 * get the i2c bus for a master transaction
530*/
531
532static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
533{
534 unsigned long iicstat;
535 int timeout = 400;
536
537 while (timeout-- > 0) {
538 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
541 return 0;
542
543 msleep(1);
544 }
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -ETIMEDOUT;
547}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530549/* s3c24xx_i2c_wait_idle
550 *
551 * wait for the i2c bus to become idle.
552*/
553
554static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
555{
556 unsigned long iicstat;
557 ktime_t start, now;
558 unsigned long delay;
Mark Brown31f313d2012-11-21 13:12:11 +0900559 int spins;
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530560
561 /* ensure the stop has been through the bus */
562
563 dev_dbg(i2c->dev, "waiting for bus idle\n");
564
565 start = now = ktime_get();
566
567 /*
568 * Most of the time, the bus is already idle within a few usec of the
569 * end of a transaction. However, really slow i2c devices can stretch
570 * the clock, delaying STOP generation.
571 *
Mark Brown31f313d2012-11-21 13:12:11 +0900572 * On slower SoCs this typically happens within a very small number of
573 * instructions so busy wait briefly to avoid scheduling overhead.
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530574 */
Mark Brown31f313d2012-11-21 13:12:11 +0900575 spins = 3;
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530576 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Mark Brown31f313d2012-11-21 13:12:11 +0900577 while ((iicstat & S3C2410_IICSTAT_START) && --spins) {
578 cpu_relax();
579 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
Mark Brown31f313d2012-11-21 13:12:11 +0900582 /*
583 * If we do get an appreciable delay as a compromise between idle
584 * detection latency for the normal, fast case, and system load in the
585 * slow device case, use an exponential back off in the polling loop,
586 * up to 1/10th of the total timeout, then continue to poll at a
587 * constant rate up to the timeout.
588 */
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530589 delay = 1;
590 while ((iicstat & S3C2410_IICSTAT_START) &&
591 ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) {
592 usleep_range(delay, 2 * delay);
593 if (delay < S3C2410_IDLE_TIMEOUT / 10)
594 delay <<= 1;
595 now = ktime_get();
596 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
597 }
598
599 if (iicstat & S3C2410_IICSTAT_START)
600 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/* s3c24xx_i2c_doxfer
604 *
605 * this starts an i2c transfer
606*/
607
Ben Dooks3d0911b2008-10-31 16:10:24 +0000608static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
609 struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530611 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int ret;
613
Ben Dooksbe44f012008-10-31 16:10:22 +0000614 if (i2c->suspended)
Ben Dooks61c7cff2008-07-28 12:04:07 +0100615 return -EIO;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ret = s3c24xx_i2c_set_master(i2c);
618 if (ret != 0) {
619 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
620 ret = -EAGAIN;
621 goto out;
622 }
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 i2c->msg = msgs;
625 i2c->msg_num = num;
626 i2c->msg_ptr = 0;
627 i2c->msg_idx = 0;
628 i2c->state = STATE_START;
629
630 s3c24xx_i2c_enable_irq(i2c);
631 s3c24xx_i2c_message_start(i2c, msgs);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
634
635 ret = i2c->msg_idx;
636
Ben Dooks3d0911b2008-10-31 16:10:24 +0000637 /* having these next two as dev_err() makes life very
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * noisy when doing an i2cdetect */
639
640 if (timeout == 0)
641 dev_dbg(i2c->dev, "timeout\n");
642 else if (ret != num)
643 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
644
Daniel Kurtz0da2e772012-11-15 17:43:31 +0530645 /* For QUIRK_HDMIPHY, bus is already disabled */
646 if (i2c->quirks & QUIRK_HDMIPHY)
647 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530649 s3c24xx_i2c_wait_idle(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 out:
652 return ret;
653}
654
655/* s3c24xx_i2c_xfer
656 *
657 * first port of call from the i2c bus code when an message needs
Steven Cole44bbe872005-05-03 18:21:25 -0600658 * transferring across the i2c bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659*/
660
661static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
662 struct i2c_msg *msgs, int num)
663{
664 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
665 int retry;
666 int ret;
667
Mark Brownc62c3ca2012-01-21 13:28:47 +0000668 pm_runtime_get_sync(&adap->dev);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900669 clk_prepare_enable(i2c->clk);
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 for (retry = 0; retry < adap->retries; retry++) {
672
673 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
674
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200675 if (ret != -EAGAIN) {
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900676 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100677 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return ret;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
682
683 udelay(100);
684 }
685
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900686 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100687 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return -EREMOTEIO;
689}
690
691/* declare our i2c functionality */
692static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
693{
Mark Brown14674e72012-05-30 10:55:34 +0200694 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
695 I2C_FUNC_PROTOCOL_MANGLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698/* i2c bus registration info */
699
Jean Delvare8f9082c2006-09-03 22:39:46 +0200700static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 .master_xfer = s3c24xx_i2c_xfer,
702 .functionality = s3c24xx_i2c_func,
703};
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/* s3c24xx_i2c_calcdivisor
706 *
707 * return the divisor settings for a given frequency
708*/
709
710static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
711 unsigned int *div1, unsigned int *divs)
712{
713 unsigned int calc_divs = clkin / wanted;
714 unsigned int calc_div1;
715
716 if (calc_divs > (16*16))
717 calc_div1 = 512;
718 else
719 calc_div1 = 16;
720
721 calc_divs += calc_div1-1;
722 calc_divs /= calc_div1;
723
724 if (calc_divs == 0)
725 calc_divs = 1;
726 if (calc_divs > 17)
727 calc_divs = 17;
728
729 *divs = calc_divs;
730 *div1 = calc_div1;
731
732 return clkin / (calc_divs * calc_div1);
733}
734
Ben Dooks61c7cff2008-07-28 12:04:07 +0100735/* s3c24xx_i2c_clockrate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 *
737 * work out a divisor for the user requested frequency setting,
738 * either by the requested frequency, or scanning the acceptable
739 * range of frequencies until something is found
740*/
741
Ben Dooks61c7cff2008-07-28 12:04:07 +0100742static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530744 struct s3c2410_platform_i2c *pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 unsigned long clkin = clk_get_rate(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 unsigned int divs, div1;
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000747 unsigned long target_frequency;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100748 u32 iiccon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Ben Dooks61c7cff2008-07-28 12:04:07 +0100751 i2c->clkrate = clkin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 clkin /= 1000; /* clkin now in KHz */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000753
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000754 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000756 target_frequency = pdata->frequency ? pdata->frequency : 100000;
757
758 target_frequency /= 1000; /* Target frequency now in KHz */
759
760 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
761
762 if (freq > target_frequency) {
763 dev_err(i2c->dev,
764 "Unable to achieve desired frequency %luKHz." \
765 " Lowest achievable %dKHz\n", target_frequency, freq);
766 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 *got = freq;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100770
771 iiccon = readl(i2c->regs + S3C2410_IICCON);
772 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
773 iiccon |= (divs-1);
774
775 if (div1 == 512)
776 iiccon |= S3C2410_IICCON_TXDIV_512;
777
778 writel(iiccon, i2c->regs + S3C2410_IICCON);
779
Karol Lewandowski27452492012-04-23 18:24:00 +0200780 if (i2c->quirks & QUIRK_S3C2440) {
Ben Dooksa192f712009-03-27 10:52:13 +0000781 unsigned long sda_delay;
782
783 if (pdata->sda_delay) {
MyungJoo Ham70313072010-09-30 15:54:46 +0200784 sda_delay = clkin * pdata->sda_delay;
785 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
Ben Dooksa192f712009-03-27 10:52:13 +0000786 sda_delay = DIV_ROUND_UP(sda_delay, 5);
787 if (sda_delay > 3)
788 sda_delay = 3;
789 sda_delay |= S3C2410_IICLC_FILTER_ON;
790 } else
791 sda_delay = 0;
792
793 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
794 writel(sda_delay, i2c->regs + S3C2440_IICLC);
795 }
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798}
799
Ben Dooks61c7cff2008-07-28 12:04:07 +0100800#ifdef CONFIG_CPU_FREQ
801
802#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
803
804static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
805 unsigned long val, void *data)
806{
807 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100808 unsigned int got;
809 int delta_f;
810 int ret;
811
812 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
813
814 /* if we're post-change and the input clock has slowed down
815 * or at pre-change and the clock is about to speed up, then
816 * adjust our clock rate. <0 is slow, >0 speedup.
817 */
818
819 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
820 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
Daniel Kurtz9bcd04b2012-11-15 17:43:30 +0530821 i2c_lock_adapter(&i2c->adap);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100822 ret = s3c24xx_i2c_clockrate(i2c, &got);
Daniel Kurtz9bcd04b2012-11-15 17:43:30 +0530823 i2c_unlock_adapter(&i2c->adap);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100824
825 if (ret < 0)
826 dev_err(i2c->dev, "cannot find frequency\n");
827 else
828 dev_info(i2c->dev, "setting freq %d\n", got);
829 }
830
831 return 0;
832}
833
834static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
835{
836 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
837
838 return cpufreq_register_notifier(&i2c->freq_transition,
839 CPUFREQ_TRANSITION_NOTIFIER);
840}
841
842static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
843{
844 cpufreq_unregister_notifier(&i2c->freq_transition,
845 CPUFREQ_TRANSITION_NOTIFIER);
846}
847
848#else
849static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
850{
851 return 0;
852}
853
854static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
855{
856}
857#endif
858
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530859#ifdef CONFIG_OF
860static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
861{
862 int idx, gpio, ret;
863
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200864 if (i2c->quirks & QUIRK_NO_GPIO)
865 return 0;
866
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530867 for (idx = 0; idx < 2; idx++) {
868 gpio = of_get_gpio(i2c->dev->of_node, idx);
869 if (!gpio_is_valid(gpio)) {
870 dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
871 goto free_gpio;
872 }
Abhilash Kesavan963f2072012-11-19 15:48:46 +0530873 i2c->gpios[idx] = gpio;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530874
875 ret = gpio_request(gpio, "i2c-bus");
876 if (ret) {
877 dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
878 goto free_gpio;
879 }
880 }
881 return 0;
882
883free_gpio:
884 while (--idx >= 0)
885 gpio_free(i2c->gpios[idx]);
886 return -EINVAL;
887}
888
889static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
890{
891 unsigned int idx;
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200892
893 if (i2c->quirks & QUIRK_NO_GPIO)
894 return;
895
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530896 for (idx = 0; idx < 2; idx++)
897 gpio_free(i2c->gpios[idx]);
898}
899#else
900static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
901{
Tushar Behera8ebe6612011-12-09 15:33:55 +0530902 return 0;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530903}
904
905static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
906{
907}
908#endif
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/* s3c24xx_i2c_init
911 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000912 * initialise the controller, set the IO lines and frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913*/
914
915static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
916{
917 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
918 struct s3c2410_platform_i2c *pdata;
919 unsigned int freq;
920
921 /* get the plafrom data */
922
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530923 pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* write slave address */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
928
929 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
930
Ben Dooks61c7cff2008-07-28 12:04:07 +0100931 writel(iicon, i2c->regs + S3C2410_IICCON);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* we need to work out the divisors for the clock... */
934
Ben Dooks61c7cff2008-07-28 12:04:07 +0100935 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
936 writel(0, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 dev_err(i2c->dev, "cannot meet bus frequency required\n");
938 return -EINVAL;
939 }
940
941 /* todo - check that the i2c lines aren't being dragged anywhere */
942
943 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
944 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947}
948
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530949#ifdef CONFIG_OF
950/* s3c24xx_i2c_parse_dt
951 *
952 * Parse the device tree node and retreive the platform data.
953*/
954
955static void
956s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
957{
958 struct s3c2410_platform_i2c *pdata = i2c->pdata;
959
960 if (!np)
961 return;
962
963 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
964 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
965 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
966 of_property_read_u32(np, "samsung,i2c-max-bus-freq",
967 (u32 *)&pdata->frequency);
968}
969#else
970static void
971s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
972{
973 return;
974}
975#endif
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/* s3c24xx_i2c_probe
978 *
979 * called by the bus driver when a suitable device is found
980*/
981
Russell King3ae5eae2005-11-09 22:32:44 +0000982static int s3c24xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Ben Dooks692acbd2008-10-31 16:10:28 +0000984 struct s3c24xx_i2c *i2c;
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530985 struct s3c2410_platform_i2c *pdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct resource *res;
987 int ret;
988
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530989 if (!pdev->dev.of_node) {
990 pdata = pdev->dev.platform_data;
991 if (!pdata) {
992 dev_err(&pdev->dev, "no platform data\n");
993 return -EINVAL;
994 }
Ben Dooks6a039ca2008-10-31 16:10:27 +0000995 }
Ben Dooks399dee22008-07-28 12:04:06 +0100996
Mark Brown4ea15572012-01-21 13:28:46 +0000997 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
Ben Dooks692acbd2008-10-31 16:10:28 +0000998 if (!i2c) {
999 dev_err(&pdev->dev, "no memory for state\n");
1000 return -ENOMEM;
1001 }
1002
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301003 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1004 if (!i2c->pdata) {
Tushar Behera669da302013-01-24 15:41:06 +05301005 dev_err(&pdev->dev, "no memory for platform data\n");
1006 return -ENOMEM;
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301007 }
1008
Karol Lewandowski27452492012-04-23 18:24:00 +02001009 i2c->quirks = s3c24xx_get_device_quirks(pdev);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301010 if (pdata)
1011 memcpy(i2c->pdata, pdata, sizeof(*pdata));
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301012 else
1013 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301014
Ben Dooks692acbd2008-10-31 16:10:28 +00001015 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
1016 i2c->adap.owner = THIS_MODULE;
1017 i2c->adap.algo = &s3c24xx_i2c_algorithm;
1018 i2c->adap.retries = 2;
1019 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1020 i2c->tx_setup = 50;
1021
Ben Dooks692acbd2008-10-31 16:10:28 +00001022 init_waitqueue_head(&i2c->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* find the clock and enable it */
1025
Russell King3ae5eae2005-11-09 22:32:44 +00001026 i2c->dev = &pdev->dev;
Tushar Behera2b255b92013-01-24 15:41:07 +05301027 i2c->clk = devm_clk_get(&pdev->dev, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (IS_ERR(i2c->clk)) {
Russell King3ae5eae2005-11-09 22:32:44 +00001029 dev_err(&pdev->dev, "cannot get clock\n");
Tushar Behera669da302013-01-24 15:41:06 +05301030 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
Russell King3ae5eae2005-11-09 22:32:44 +00001033 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* map the registers */
1037
1038 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1039 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001040 dev_err(&pdev->dev, "cannot find IO resource\n");
Tushar Beherad16933b2013-01-24 15:41:08 +05301041 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Thierry Reding84dbf802013-01-21 11:09:03 +01001044 i2c->regs = devm_ioremap_resource(&pdev->dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Linus Torvalds52caa592013-02-26 09:41:53 -08001046 if (IS_ERR(i2c->regs))
1047 return PTR_ERR(i2c->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Mark Browna72ad452012-11-05 09:33:39 +01001049 dev_dbg(&pdev->dev, "registers %p (%p)\n",
1050 i2c->regs, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /* setup info block for the i2c core */
1053
1054 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001055 i2c->adap.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Tomasz Figa2693ac62012-11-13 11:33:40 +01001057 i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
1058
Abhilash Kesavan658122f2012-11-19 15:47:17 +05301059 /* inititalise the i2c gpio lines */
1060
1061 if (i2c->pdata->cfg_gpio) {
1062 i2c->pdata->cfg_gpio(to_platform_device(i2c->dev));
1063 } else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) {
Tushar Beherad16933b2013-01-24 15:41:08 +05301064 return -EINVAL;
Abhilash Kesavan658122f2012-11-19 15:47:17 +05301065 }
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* initialise the i2c controller */
1068
Tushar Beherad16933b2013-01-24 15:41:08 +05301069 clk_prepare_enable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ret = s3c24xx_i2c_init(i2c);
Tushar Beherad16933b2013-01-24 15:41:08 +05301071 clk_disable_unprepare(i2c->clk);
1072 if (ret != 0) {
1073 dev_err(&pdev->dev, "I2C controller init failed\n");
1074 return ret;
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /* find the IRQ for this unit (note, this relies on the init call to
Ben Dooks3d0911b2008-10-31 16:10:24 +00001077 * ensure no current IRQs pending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 */
1079
Ben Dookse0d1ec92008-10-31 16:10:30 +00001080 i2c->irq = ret = platform_get_irq(pdev, 0);
1081 if (ret <= 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001082 dev_err(&pdev->dev, "cannot find IRQ\n");
Tushar Beherad16933b2013-01-24 15:41:08 +05301083 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
Tushar Behera2b255b92013-01-24 15:41:07 +05301086 ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 0,
1087 dev_name(&pdev->dev), i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (ret != 0) {
Ben Dookse0d1ec92008-10-31 16:10:30 +00001090 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
Tushar Beherad16933b2013-01-24 15:41:08 +05301091 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
Ben Dooks61c7cff2008-07-28 12:04:07 +01001094 ret = s3c24xx_i2c_register_cpufreq(i2c);
1095 if (ret < 0) {
1096 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
Tushar Beherad16933b2013-01-24 15:41:08 +05301097 return ret;
Ben Dooks61c7cff2008-07-28 12:04:07 +01001098 }
1099
Ben Dooks399dee22008-07-28 12:04:06 +01001100 /* Note, previous versions of the driver used i2c_add_adapter()
1101 * to add the bus at any number. We now pass the bus number via
1102 * the platform data, so if unset it will now default to always
1103 * being bus 0.
1104 */
1105
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301106 i2c->adap.nr = i2c->pdata->bus_num;
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301107 i2c->adap.dev.of_node = pdev->dev.of_node;
Ben Dooks399dee22008-07-28 12:04:06 +01001108
1109 ret = i2c_add_numbered_adapter(&i2c->adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001111 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
Tushar Beheradc6fea42013-01-24 15:41:09 +05301112 s3c24xx_i2c_deregister_cpufreq(i2c);
1113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301116 of_i2c_register_devices(&i2c->adap);
Russell King3ae5eae2005-11-09 22:32:44 +00001117 platform_set_drvdata(pdev, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Mark Brownc62c3ca2012-01-21 13:28:47 +00001119 pm_runtime_enable(&pdev->dev);
1120 pm_runtime_enable(&i2c->adap.dev);
1121
Jean Delvare22e965c2009-01-07 14:29:16 +01001122 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
Ben Dooks5b687902007-05-01 23:26:35 +02001123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126/* s3c24xx_i2c_remove
1127 *
1128 * called when device is removed from the bus
1129*/
1130
Russell King3ae5eae2005-11-09 22:32:44 +00001131static int s3c24xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Russell King3ae5eae2005-11-09 22:32:44 +00001133 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Ben Dooks5b687902007-05-01 23:26:35 +02001134
Mark Brownc62c3ca2012-01-21 13:28:47 +00001135 pm_runtime_disable(&i2c->adap.dev);
1136 pm_runtime_disable(&pdev->dev);
1137
Ben Dooks61c7cff2008-07-28 12:04:07 +01001138 s3c24xx_i2c_deregister_cpufreq(i2c);
1139
Ben Dooks5b687902007-05-01 23:26:35 +02001140 i2c_del_adapter(&i2c->adap);
Ben Dooks5b687902007-05-01 23:26:35 +02001141
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001142 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001143
Tomasz Figa2693ac62012-11-13 11:33:40 +01001144 if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
1145 s3c24xx_i2c_dt_gpio_free(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 return 0;
1148}
1149
Mark Brown2935e0e2012-11-05 09:33:38 +01001150#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001151static int s3c24xx_i2c_suspend_noirq(struct device *dev)
Ben Dooksbe44f012008-10-31 16:10:22 +00001152{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001153 struct platform_device *pdev = to_platform_device(dev);
1154 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1155
Ben Dooksbe44f012008-10-31 16:10:22 +00001156 i2c->suspended = 1;
Magnus Damm6a6c61892009-07-08 13:22:47 +02001157
Ben Dooksbe44f012008-10-31 16:10:22 +00001158 return 0;
1159}
1160
Magnus Damm6a6c61892009-07-08 13:22:47 +02001161static int s3c24xx_i2c_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001163 struct platform_device *pdev = to_platform_device(dev);
1164 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Russell King9480e302005-10-28 09:52:56 -07001165
Ben Dooksbe44f012008-10-31 16:10:22 +00001166 i2c->suspended = 0;
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001167 clk_prepare_enable(i2c->clk);
Ben Dooksbe44f012008-10-31 16:10:22 +00001168 s3c24xx_i2c_init(i2c);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001169 clk_disable_unprepare(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 return 0;
1172}
Mark Brown2935e0e2012-11-05 09:33:38 +01001173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Mark Brown2935e0e2012-11-05 09:33:38 +01001175#ifdef CONFIG_PM
Alexey Dobriyan47145212009-12-14 18:00:08 -08001176static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
Mark Brown2935e0e2012-11-05 09:33:38 +01001177#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001178 .suspend_noirq = s3c24xx_i2c_suspend_noirq,
1179 .resume = s3c24xx_i2c_resume,
Mark Brown2935e0e2012-11-05 09:33:38 +01001180#endif
Magnus Damm6a6c61892009-07-08 13:22:47 +02001181};
1182
1183#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184#else
Magnus Damm6a6c61892009-07-08 13:22:47 +02001185#define S3C24XX_DEV_PM_OPS NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186#endif
1187
1188/* device driver for platform bus bits */
1189
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001190static struct platform_driver s3c24xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 .probe = s3c24xx_i2c_probe,
1192 .remove = s3c24xx_i2c_remove,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001193 .id_table = s3c24xx_driver_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001194 .driver = {
1195 .owner = THIS_MODULE,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001196 .name = "s3c-i2c",
Magnus Damm6a6c61892009-07-08 13:22:47 +02001197 .pm = S3C24XX_DEV_PM_OPS,
Karol Lewandowski9df7ead2012-03-21 20:11:51 +01001198 .of_match_table = of_match_ptr(s3c24xx_i2c_match),
Russell King3ae5eae2005-11-09 22:32:44 +00001199 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200};
1201
1202static int __init i2c_adap_s3c_init(void)
1203{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001204 return platform_driver_register(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
Mark Brown18dc83a2009-02-26 16:29:22 +00001206subsys_initcall(i2c_adap_s3c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208static void __exit i2c_adap_s3c_exit(void)
1209{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001210 platform_driver_unregister(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212module_exit(i2c_adap_s3c_exit);
1213
1214MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1215MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1216MODULE_LICENSE("GPL");