blob: 216fea12e2e91b21d53bc533cefd8af09a56ae03 [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
Arnd Bergmann436d42c2012-08-24 15:22:12 +020045#include <linux/platform_data/i2c-s3c2410.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Heiko Stübnere6366022013-03-21 04:09:25 +000047/* see s3c2410x user guide, v1.1, section 9 (p447) for more info */
48
49#define S3C2410_IICREG(x) (x)
50
51#define S3C2410_IICCON S3C2410_IICREG(0x00)
52#define S3C2410_IICSTAT S3C2410_IICREG(0x04)
53#define S3C2410_IICADD S3C2410_IICREG(0x08)
54#define S3C2410_IICDS S3C2410_IICREG(0x0C)
55#define S3C2440_IICLC S3C2410_IICREG(0x10)
56
57#define S3C2410_IICCON_ACKEN (1<<7)
58#define S3C2410_IICCON_TXDIV_16 (0<<6)
59#define S3C2410_IICCON_TXDIV_512 (1<<6)
60#define S3C2410_IICCON_IRQEN (1<<5)
61#define S3C2410_IICCON_IRQPEND (1<<4)
62#define S3C2410_IICCON_SCALE(x) ((x)&15)
63#define S3C2410_IICCON_SCALEMASK (0xf)
64
65#define S3C2410_IICSTAT_MASTER_RX (2<<6)
66#define S3C2410_IICSTAT_MASTER_TX (3<<6)
67#define S3C2410_IICSTAT_SLAVE_RX (0<<6)
68#define S3C2410_IICSTAT_SLAVE_TX (1<<6)
69#define S3C2410_IICSTAT_MODEMASK (3<<6)
70
71#define S3C2410_IICSTAT_START (1<<5)
72#define S3C2410_IICSTAT_BUSBUSY (1<<5)
73#define S3C2410_IICSTAT_TXRXEN (1<<4)
74#define S3C2410_IICSTAT_ARBITR (1<<3)
75#define S3C2410_IICSTAT_ASSLAVE (1<<2)
76#define S3C2410_IICSTAT_ADDR0 (1<<1)
77#define S3C2410_IICSTAT_LASTBIT (1<<0)
78
79#define S3C2410_IICLC_SDA_DELAY0 (0 << 0)
80#define S3C2410_IICLC_SDA_DELAY5 (1 << 0)
81#define S3C2410_IICLC_SDA_DELAY10 (2 << 0)
82#define S3C2410_IICLC_SDA_DELAY15 (3 << 0)
83#define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0)
84
85#define S3C2410_IICLC_FILTER_ON (1<<2)
86
Karol Lewandowski27452492012-04-23 18:24:00 +020087/* Treat S3C2410 as baseline hardware, anything else is supported via quirks */
88#define QUIRK_S3C2440 (1 << 0)
Karol Lewandowskiec39ef82012-04-23 18:24:01 +020089#define QUIRK_HDMIPHY (1 << 1)
90#define QUIRK_NO_GPIO (1 << 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Daniel Kurtzfe724bf2012-11-15 17:43:32 +053092/* Max time to wait for bus to become idle after a xfer (in us) */
93#define S3C2410_IDLE_TIMEOUT 5000
94
Karol Lewandowski27452492012-04-23 18:24:00 +020095/* i2c controller state */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096enum s3c24xx_i2c_state {
97 STATE_IDLE,
98 STATE_START,
99 STATE_READ,
100 STATE_WRITE,
101 STATE_STOP
102};
103
104struct s3c24xx_i2c {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 wait_queue_head_t wait;
Karol Lewandowski27452492012-04-23 18:24:00 +0200106 unsigned int quirks;
Ben Dooksbe44f012008-10-31 16:10:22 +0000107 unsigned int suspended:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 struct i2c_msg *msg;
110 unsigned int msg_num;
111 unsigned int msg_idx;
112 unsigned int msg_ptr;
113
Ben Dookse00a8cd2007-05-01 23:26:35 +0200114 unsigned int tx_setup;
Ben Dookse0d1ec92008-10-31 16:10:30 +0000115 unsigned int irq;
Ben Dookse00a8cd2007-05-01 23:26:35 +0200116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 enum s3c24xx_i2c_state state;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100118 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 void __iomem *regs;
121 struct clk *clk;
122 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct i2c_adapter adap;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100124
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530125 struct s3c2410_platform_i2c *pdata;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530126 int gpios[2];
Tomasz Figa2693ac62012-11-13 11:33:40 +0100127 struct pinctrl *pctrl;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100128#ifdef CONFIG_CPU_FREQ
129 struct notifier_block freq_transition;
130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Karol Lewandowski27452492012-04-23 18:24:00 +0200133static struct platform_device_id s3c24xx_driver_ids[] = {
134 {
135 .name = "s3c2410-i2c",
136 .driver_data = 0,
137 }, {
138 .name = "s3c2440-i2c",
139 .driver_data = QUIRK_S3C2440,
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200140 }, {
141 .name = "s3c2440-hdmiphy-i2c",
142 .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
Karol Lewandowski27452492012-04-23 18:24:00 +0200143 }, { },
144};
145MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530147#ifdef CONFIG_OF
Karol Lewandowski27452492012-04-23 18:24:00 +0200148static const struct of_device_id s3c24xx_i2c_match[] = {
149 { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
150 { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200151 { .compatible = "samsung,s3c2440-hdmiphy-i2c",
152 .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
Giridhar Maruthyfaf93ff2013-01-24 11:27:51 -0800153 { .compatible = "samsung,exynos5440-i2c",
154 .data = (void *)(QUIRK_S3C2440 | QUIRK_NO_GPIO) },
Karol Lewandowski27452492012-04-23 18:24:00 +0200155 {},
156};
157MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530158#endif
159
Karol Lewandowski27452492012-04-23 18:24:00 +0200160/* s3c24xx_get_device_quirks
161 *
162 * Get controller type either from device tree or platform device variant.
163*/
164
165static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
166{
167 if (pdev->dev.of_node) {
168 const struct of_device_id *match;
Karol Lewandowskib900ba42012-05-30 11:43:05 +0200169 match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
Karol Lewandowski27452492012-04-23 18:24:00 +0200170 return (unsigned int)match->data;
171 }
172
173 return platform_get_device_id(pdev)->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/* s3c24xx_i2c_master_complete
177 *
178 * complete the message and wake up the caller, using the given return code,
179 * or zero to mean ok.
180*/
181
182static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
183{
184 dev_dbg(i2c->dev, "master_complete %d\n", ret);
185
186 i2c->msg_ptr = 0;
187 i2c->msg = NULL;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000188 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 i2c->msg_num = 0;
190 if (ret)
191 i2c->msg_idx = ret;
192
193 wake_up(&i2c->wait);
194}
195
196static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
197{
198 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 tmp = readl(i2c->regs + S3C2410_IICCON);
201 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
205{
206 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 tmp = readl(i2c->regs + S3C2410_IICCON);
209 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/* irq enable/disable functions */
213
214static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
215{
216 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 tmp = readl(i2c->regs + S3C2410_IICCON);
219 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
220}
221
222static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
223{
224 unsigned long tmp;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 tmp = readl(i2c->regs + S3C2410_IICCON);
227 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
228}
229
230
231/* s3c24xx_i2c_message_start
232 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000233 * put the start of a message onto the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234*/
235
Ben Dooks3d0911b2008-10-31 16:10:24 +0000236static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct i2c_msg *msg)
238{
239 unsigned int addr = (msg->addr & 0x7f) << 1;
240 unsigned long stat;
241 unsigned long iiccon;
242
243 stat = 0;
244 stat |= S3C2410_IICSTAT_TXRXEN;
245
246 if (msg->flags & I2C_M_RD) {
247 stat |= S3C2410_IICSTAT_MASTER_RX;
248 addr |= 1;
249 } else
250 stat |= S3C2410_IICSTAT_MASTER_TX;
251
252 if (msg->flags & I2C_M_REV_DIR_ADDR)
253 addr ^= 1;
254
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400255 /* todo - check for whether ack wanted or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 s3c24xx_i2c_enable_ack(i2c);
257
258 iiccon = readl(i2c->regs + S3C2410_IICCON);
259 writel(stat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
262 writeb(addr, i2c->regs + S3C2410_IICDS);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000263
Ben Dookse00a8cd2007-05-01 23:26:35 +0200264 /* delay here to ensure the data byte has gotten onto the bus
265 * before the transaction is started */
266
267 ndelay(i2c->tx_setup);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
270 writel(iiccon, i2c->regs + S3C2410_IICCON);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000271
272 stat |= S3C2410_IICSTAT_START;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 writel(stat, i2c->regs + S3C2410_IICSTAT);
274}
275
276static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
277{
278 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
279
280 dev_dbg(i2c->dev, "STOP\n");
281
Daniel Kurtz0da2e772012-11-15 17:43:31 +0530282 /*
283 * The datasheet says that the STOP sequence should be:
284 * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP')
285 * 2) I2CCON.4 = 0 - Clear IRQPEND
286 * 3) Wait until the stop condition takes effect.
287 * 4*) I2CSTAT.4 = 0 - Clear TXRXEN
288 *
289 * Where, step "4*" is only for buses with the "HDMIPHY" quirk.
290 *
291 * However, after much experimentation, it appears that:
292 * a) normal buses automatically clear BUSY and transition from
293 * Master->Slave when they complete generating a STOP condition.
294 * Therefore, step (3) can be done in doxfer() by polling I2CCON.4
295 * after starting the STOP generation here.
296 * b) HDMIPHY bus does neither, so there is no way to do step 3.
297 * There is no indication when this bus has finished generating
298 * STOP.
299 *
300 * In fact, we have found that as soon as the IRQPEND bit is cleared in
301 * step 2, the HDMIPHY bus generates the STOP condition, and then
302 * immediately starts transferring another data byte, even though the
303 * bus is supposedly stopped. This is presumably because the bus is
304 * still in "Master" mode, and its BUSY bit is still set.
305 *
306 * To avoid these extra post-STOP transactions on HDMI phy devices, we
307 * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly,
308 * instead of first generating a proper STOP condition. This should
309 * float SDA & SCK terminating the transfer. Subsequent transfers
310 * start with a proper START condition, and proceed normally.
311 *
312 * The HDMIPHY bus is an internal bus that always has exactly two
313 * devices, the host as Master and the HDMIPHY device as the slave.
314 * Skipping the STOP condition has been tested on this bus and works.
315 */
316 if (i2c->quirks & QUIRK_HDMIPHY) {
317 /* Stop driving the I2C pins */
318 iicstat &= ~S3C2410_IICSTAT_TXRXEN;
319 } else {
320 /* stop the transfer */
321 iicstat &= ~S3C2410_IICSTAT_START;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 i2c->state = STATE_STOP;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 s3c24xx_i2c_master_complete(i2c, ret);
328 s3c24xx_i2c_disable_irq(i2c);
329}
330
331/* helper functions to determine the current state in the set of
332 * messages we are sending */
333
334/* is_lastmsg()
335 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000336 * returns TRUE if the current message is the last in the set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337*/
338
339static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
340{
341 return i2c->msg_idx >= (i2c->msg_num - 1);
342}
343
344/* is_msglast
345 *
346 * returns TRUE if we this is the last byte in the current message
347*/
348
349static inline int is_msglast(struct s3c24xx_i2c *i2c)
350{
351 return i2c->msg_ptr == i2c->msg->len-1;
352}
353
354/* is_msgend
355 *
356 * returns TRUE if we reached the end of the current message
357*/
358
359static inline int is_msgend(struct s3c24xx_i2c *i2c)
360{
361 return i2c->msg_ptr >= i2c->msg->len;
362}
363
Huisung Kang19820512011-06-23 21:37:33 +0900364/* i2c_s3c_irq_nextbyte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
366 * process an interrupt and work out what to do
367 */
368
Huisung Kang19820512011-06-23 21:37:33 +0900369static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 unsigned long tmp;
372 unsigned char byte;
373 int ret = 0;
374
375 switch (i2c->state) {
376
377 case STATE_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200378 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 case STATE_STOP:
Harvey Harrison08882d22008-04-22 22:16:47 +0200382 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000383 s3c24xx_i2c_disable_irq(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 goto out_ack;
385
386 case STATE_START:
387 /* last thing we did was send a start condition on the
388 * bus, or started a new i2c message
389 */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000390
Ben Dooks63f5c282008-07-01 11:59:42 +0100391 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
393 /* ack was not received... */
394
395 dev_dbg(i2c->dev, "ack was not received\n");
Ben Dooks63f5c282008-07-01 11:59:42 +0100396 s3c24xx_i2c_stop(i2c, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto out_ack;
398 }
399
400 if (i2c->msg->flags & I2C_M_RD)
401 i2c->state = STATE_READ;
402 else
403 i2c->state = STATE_WRITE;
404
405 /* terminate the transfer if there is nothing to do
Ben Dooks63f5c282008-07-01 11:59:42 +0100406 * as this is used by the i2c probe to find devices. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
409 s3c24xx_i2c_stop(i2c, 0);
410 goto out_ack;
411 }
412
413 if (i2c->state == STATE_READ)
414 goto prepare_read;
415
Ben Dooks3d0911b2008-10-31 16:10:24 +0000416 /* fall through to the write state, as we will need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * send a byte as well */
418
419 case STATE_WRITE:
420 /* we are writing data to the device... check for the
421 * end of the message, and if so, work out what to do
422 */
423
Ben Dooks27097812008-07-01 11:59:41 +0100424 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
425 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
426 dev_dbg(i2c->dev, "WRITE: No Ack\n");
427
428 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
429 goto out_ack;
430 }
431 }
432
Ben Dooks3d0911b2008-10-31 16:10:24 +0000433 retry_write:
Ben Dooks27097812008-07-01 11:59:41 +0100434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!is_msgend(i2c)) {
436 byte = i2c->msg->buf[i2c->msg_ptr++];
437 writeb(byte, i2c->regs + S3C2410_IICDS);
Ben Dookse00a8cd2007-05-01 23:26:35 +0200438
439 /* delay after writing the byte to allow the
440 * data setup time on the bus, as writing the
441 * data to the register causes the first bit
442 * to appear on SDA, and SCL will change as
443 * soon as the interrupt is acknowledged */
444
445 ndelay(i2c->tx_setup);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 } else if (!is_lastmsg(i2c)) {
448 /* we need to go to the next i2c message */
449
450 dev_dbg(i2c->dev, "WRITE: Next Message\n");
451
452 i2c->msg_ptr = 0;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000453 i2c->msg_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 i2c->msg++;
Ben Dooks3d0911b2008-10-31 16:10:24 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* check to see if we need to do another message */
457 if (i2c->msg->flags & I2C_M_NOSTART) {
458
459 if (i2c->msg->flags & I2C_M_RD) {
460 /* cannot do this, the controller
461 * forces us to send a new START
462 * when we change direction */
463
464 s3c24xx_i2c_stop(i2c, -EINVAL);
465 }
466
467 goto retry_write;
468 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* send the new start */
470 s3c24xx_i2c_message_start(i2c, i2c->msg);
471 i2c->state = STATE_START;
472 }
473
474 } else {
475 /* send stop */
476
477 s3c24xx_i2c_stop(i2c, 0);
478 }
479 break;
480
481 case STATE_READ:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000482 /* we have a byte of data in the data register, do
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400483 * something with it, and then work out whether we are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * going to do any more read/write
485 */
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 byte = readb(i2c->regs + S3C2410_IICDS);
488 i2c->msg->buf[i2c->msg_ptr++] = byte;
489
Ben Dooks3d0911b2008-10-31 16:10:24 +0000490 prepare_read:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (is_msglast(i2c)) {
492 /* last byte of buffer */
493
494 if (is_lastmsg(i2c))
495 s3c24xx_i2c_disable_ack(i2c);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else if (is_msgend(i2c)) {
498 /* ok, we've read the entire buffer, see if there
499 * is anything else we need to do */
500
501 if (is_lastmsg(i2c)) {
502 /* last message, send stop and complete */
503 dev_dbg(i2c->dev, "READ: Send Stop\n");
504
505 s3c24xx_i2c_stop(i2c, 0);
506 } else {
507 /* go to the next transfer */
508 dev_dbg(i2c->dev, "READ: Next Transfer\n");
509
510 i2c->msg_ptr = 0;
511 i2c->msg_idx++;
512 i2c->msg++;
513 }
514 }
515
516 break;
517 }
518
519 /* acknowlegde the IRQ and get back on with the work */
520
521 out_ack:
Ben Dooks3d0911b2008-10-31 16:10:24 +0000522 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 tmp &= ~S3C2410_IICCON_IRQPEND;
524 writel(tmp, i2c->regs + S3C2410_IICCON);
525 out:
526 return ret;
527}
528
529/* s3c24xx_i2c_irq
530 *
531 * top level IRQ servicing routine
532*/
533
David Howells7d12e782006-10-05 14:55:46 +0100534static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct s3c24xx_i2c *i2c = dev_id;
537 unsigned long status;
538 unsigned long tmp;
539
540 status = readl(i2c->regs + S3C2410_IICSTAT);
541
542 if (status & S3C2410_IICSTAT_ARBITR) {
Ben Dooks3d0911b2008-10-31 16:10:24 +0000543 /* deal with arbitration loss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 dev_err(i2c->dev, "deal with arbitration loss\n");
545 }
546
547 if (i2c->state == STATE_IDLE) {
548 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
549
Ben Dooks3d0911b2008-10-31 16:10:24 +0000550 tmp = readl(i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 tmp &= ~S3C2410_IICCON_IRQPEND;
552 writel(tmp, i2c->regs + S3C2410_IICCON);
553 goto out;
554 }
Ben Dooks3d0911b2008-10-31 16:10:24 +0000555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* pretty much this leaves us with the fact that we've
557 * transmitted or received whatever byte we last sent */
558
Huisung Kang19820512011-06-23 21:37:33 +0900559 i2c_s3c_irq_nextbyte(i2c, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 out:
562 return IRQ_HANDLED;
563}
564
565
566/* s3c24xx_i2c_set_master
567 *
568 * get the i2c bus for a master transaction
569*/
570
571static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
572{
573 unsigned long iicstat;
574 int timeout = 400;
575
576 while (timeout-- > 0) {
577 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
580 return 0;
581
582 msleep(1);
583 }
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -ETIMEDOUT;
586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530588/* s3c24xx_i2c_wait_idle
589 *
590 * wait for the i2c bus to become idle.
591*/
592
593static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
594{
595 unsigned long iicstat;
596 ktime_t start, now;
597 unsigned long delay;
Mark Brown31f313d2012-11-21 13:12:11 +0900598 int spins;
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530599
600 /* ensure the stop has been through the bus */
601
602 dev_dbg(i2c->dev, "waiting for bus idle\n");
603
604 start = now = ktime_get();
605
606 /*
607 * Most of the time, the bus is already idle within a few usec of the
608 * end of a transaction. However, really slow i2c devices can stretch
609 * the clock, delaying STOP generation.
610 *
Mark Brown31f313d2012-11-21 13:12:11 +0900611 * On slower SoCs this typically happens within a very small number of
612 * instructions so busy wait briefly to avoid scheduling overhead.
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530613 */
Mark Brown31f313d2012-11-21 13:12:11 +0900614 spins = 3;
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530615 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Mark Brown31f313d2012-11-21 13:12:11 +0900616 while ((iicstat & S3C2410_IICSTAT_START) && --spins) {
617 cpu_relax();
618 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Mark Brown31f313d2012-11-21 13:12:11 +0900621 /*
622 * If we do get an appreciable delay as a compromise between idle
623 * detection latency for the normal, fast case, and system load in the
624 * slow device case, use an exponential back off in the polling loop,
625 * up to 1/10th of the total timeout, then continue to poll at a
626 * constant rate up to the timeout.
627 */
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530628 delay = 1;
629 while ((iicstat & S3C2410_IICSTAT_START) &&
630 ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) {
631 usleep_range(delay, 2 * delay);
632 if (delay < S3C2410_IDLE_TIMEOUT / 10)
633 delay <<= 1;
634 now = ktime_get();
635 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
636 }
637
638 if (iicstat & S3C2410_IICSTAT_START)
639 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/* s3c24xx_i2c_doxfer
643 *
644 * this starts an i2c transfer
645*/
646
Ben Dooks3d0911b2008-10-31 16:10:24 +0000647static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
648 struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530650 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int ret;
652
Ben Dooksbe44f012008-10-31 16:10:22 +0000653 if (i2c->suspended)
Ben Dooks61c7cff2008-07-28 12:04:07 +0100654 return -EIO;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 ret = s3c24xx_i2c_set_master(i2c);
657 if (ret != 0) {
658 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
659 ret = -EAGAIN;
660 goto out;
661 }
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 i2c->msg = msgs;
664 i2c->msg_num = num;
665 i2c->msg_ptr = 0;
666 i2c->msg_idx = 0;
667 i2c->state = STATE_START;
668
669 s3c24xx_i2c_enable_irq(i2c);
670 s3c24xx_i2c_message_start(i2c, msgs);
Ben Dooks3d0911b2008-10-31 16:10:24 +0000671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
673
674 ret = i2c->msg_idx;
675
Ben Dooks3d0911b2008-10-31 16:10:24 +0000676 /* having these next two as dev_err() makes life very
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * noisy when doing an i2cdetect */
678
679 if (timeout == 0)
680 dev_dbg(i2c->dev, "timeout\n");
681 else if (ret != num)
682 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
683
Daniel Kurtz0da2e772012-11-15 17:43:31 +0530684 /* For QUIRK_HDMIPHY, bus is already disabled */
685 if (i2c->quirks & QUIRK_HDMIPHY)
686 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Daniel Kurtzfe724bf2012-11-15 17:43:32 +0530688 s3c24xx_i2c_wait_idle(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 out:
691 return ret;
692}
693
694/* s3c24xx_i2c_xfer
695 *
696 * first port of call from the i2c bus code when an message needs
Steven Cole44bbe872005-05-03 18:21:25 -0600697 * transferring across the i2c bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698*/
699
700static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
701 struct i2c_msg *msgs, int num)
702{
703 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
704 int retry;
705 int ret;
706
Mark Brownc62c3ca2012-01-21 13:28:47 +0000707 pm_runtime_get_sync(&adap->dev);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900708 clk_prepare_enable(i2c->clk);
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 for (retry = 0; retry < adap->retries; retry++) {
711
712 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
713
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200714 if (ret != -EAGAIN) {
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900715 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100716 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return ret;
Arnaud Patard (Rtp)d2360b82010-07-15 15:06:14 +0200718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
721
722 udelay(100);
723 }
724
Thomas Abrahamd3b64c52012-10-03 08:26:39 +0900725 clk_disable_unprepare(i2c->clk);
Mark Browna86ae9f2012-06-28 14:08:26 +0100726 pm_runtime_put(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -EREMOTEIO;
728}
729
730/* declare our i2c functionality */
731static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
732{
Mark Brown14674e72012-05-30 10:55:34 +0200733 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
734 I2C_FUNC_PROTOCOL_MANGLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737/* i2c bus registration info */
738
Jean Delvare8f9082c2006-09-03 22:39:46 +0200739static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 .master_xfer = s3c24xx_i2c_xfer,
741 .functionality = s3c24xx_i2c_func,
742};
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/* s3c24xx_i2c_calcdivisor
745 *
746 * return the divisor settings for a given frequency
747*/
748
749static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
750 unsigned int *div1, unsigned int *divs)
751{
752 unsigned int calc_divs = clkin / wanted;
753 unsigned int calc_div1;
754
755 if (calc_divs > (16*16))
756 calc_div1 = 512;
757 else
758 calc_div1 = 16;
759
760 calc_divs += calc_div1-1;
761 calc_divs /= calc_div1;
762
763 if (calc_divs == 0)
764 calc_divs = 1;
765 if (calc_divs > 17)
766 calc_divs = 17;
767
768 *divs = calc_divs;
769 *div1 = calc_div1;
770
771 return clkin / (calc_divs * calc_div1);
772}
773
Ben Dooks61c7cff2008-07-28 12:04:07 +0100774/* s3c24xx_i2c_clockrate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 *
776 * work out a divisor for the user requested frequency setting,
777 * either by the requested frequency, or scanning the acceptable
778 * range of frequencies until something is found
779*/
780
Ben Dooks61c7cff2008-07-28 12:04:07 +0100781static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530783 struct s3c2410_platform_i2c *pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 unsigned long clkin = clk_get_rate(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 unsigned int divs, div1;
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000786 unsigned long target_frequency;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100787 u32 iiccon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Ben Dooks61c7cff2008-07-28 12:04:07 +0100790 i2c->clkrate = clkin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 clkin /= 1000; /* clkin now in KHz */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000792
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000793 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Daniel Silverstonec564e6a2009-03-13 13:53:46 +0000795 target_frequency = pdata->frequency ? pdata->frequency : 100000;
796
797 target_frequency /= 1000; /* Target frequency now in KHz */
798
799 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
800
801 if (freq > target_frequency) {
802 dev_err(i2c->dev,
803 "Unable to achieve desired frequency %luKHz." \
804 " Lowest achievable %dKHz\n", target_frequency, freq);
805 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 *got = freq;
Ben Dooks61c7cff2008-07-28 12:04:07 +0100809
810 iiccon = readl(i2c->regs + S3C2410_IICCON);
811 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
812 iiccon |= (divs-1);
813
814 if (div1 == 512)
815 iiccon |= S3C2410_IICCON_TXDIV_512;
816
817 writel(iiccon, i2c->regs + S3C2410_IICCON);
818
Karol Lewandowski27452492012-04-23 18:24:00 +0200819 if (i2c->quirks & QUIRK_S3C2440) {
Ben Dooksa192f712009-03-27 10:52:13 +0000820 unsigned long sda_delay;
821
822 if (pdata->sda_delay) {
MyungJoo Ham70313072010-09-30 15:54:46 +0200823 sda_delay = clkin * pdata->sda_delay;
824 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
Ben Dooksa192f712009-03-27 10:52:13 +0000825 sda_delay = DIV_ROUND_UP(sda_delay, 5);
826 if (sda_delay > 3)
827 sda_delay = 3;
828 sda_delay |= S3C2410_IICLC_FILTER_ON;
829 } else
830 sda_delay = 0;
831
832 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
833 writel(sda_delay, i2c->regs + S3C2440_IICLC);
834 }
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 0;
837}
838
Ben Dooks61c7cff2008-07-28 12:04:07 +0100839#ifdef CONFIG_CPU_FREQ
840
841#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
842
843static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
844 unsigned long val, void *data)
845{
846 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100847 unsigned int got;
848 int delta_f;
849 int ret;
850
851 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
852
853 /* if we're post-change and the input clock has slowed down
854 * or at pre-change and the clock is about to speed up, then
855 * adjust our clock rate. <0 is slow, >0 speedup.
856 */
857
858 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
859 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
Daniel Kurtz9bcd04b2012-11-15 17:43:30 +0530860 i2c_lock_adapter(&i2c->adap);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100861 ret = s3c24xx_i2c_clockrate(i2c, &got);
Daniel Kurtz9bcd04b2012-11-15 17:43:30 +0530862 i2c_unlock_adapter(&i2c->adap);
Ben Dooks61c7cff2008-07-28 12:04:07 +0100863
864 if (ret < 0)
865 dev_err(i2c->dev, "cannot find frequency\n");
866 else
867 dev_info(i2c->dev, "setting freq %d\n", got);
868 }
869
870 return 0;
871}
872
873static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
874{
875 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
876
877 return cpufreq_register_notifier(&i2c->freq_transition,
878 CPUFREQ_TRANSITION_NOTIFIER);
879}
880
881static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
882{
883 cpufreq_unregister_notifier(&i2c->freq_transition,
884 CPUFREQ_TRANSITION_NOTIFIER);
885}
886
887#else
888static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
889{
890 return 0;
891}
892
893static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
894{
895}
896#endif
897
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530898#ifdef CONFIG_OF
899static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
900{
901 int idx, gpio, ret;
902
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200903 if (i2c->quirks & QUIRK_NO_GPIO)
904 return 0;
905
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530906 for (idx = 0; idx < 2; idx++) {
907 gpio = of_get_gpio(i2c->dev->of_node, idx);
908 if (!gpio_is_valid(gpio)) {
909 dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
910 goto free_gpio;
911 }
Abhilash Kesavan963f2072012-11-19 15:48:46 +0530912 i2c->gpios[idx] = gpio;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530913
914 ret = gpio_request(gpio, "i2c-bus");
915 if (ret) {
916 dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
917 goto free_gpio;
918 }
919 }
920 return 0;
921
922free_gpio:
923 while (--idx >= 0)
924 gpio_free(i2c->gpios[idx]);
925 return -EINVAL;
926}
927
928static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
929{
930 unsigned int idx;
Karol Lewandowskiec39ef82012-04-23 18:24:01 +0200931
932 if (i2c->quirks & QUIRK_NO_GPIO)
933 return;
934
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530935 for (idx = 0; idx < 2; idx++)
936 gpio_free(i2c->gpios[idx]);
937}
938#else
939static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
940{
Tushar Behera8ebe6612011-12-09 15:33:55 +0530941 return 0;
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530942}
943
944static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
945{
946}
947#endif
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/* s3c24xx_i2c_init
950 *
Ben Dooks3d0911b2008-10-31 16:10:24 +0000951 * initialise the controller, set the IO lines and frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952*/
953
954static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
955{
956 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
957 struct s3c2410_platform_i2c *pdata;
958 unsigned int freq;
959
960 /* get the plafrom data */
961
Thomas Abraham4fd81eb2011-09-13 09:46:04 +0530962 pdata = i2c->pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /* write slave address */
Ben Dooks3d0911b2008-10-31 16:10:24 +0000965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
967
968 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
969
Ben Dooks61c7cff2008-07-28 12:04:07 +0100970 writel(iicon, i2c->regs + S3C2410_IICCON);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* we need to work out the divisors for the clock... */
973
Ben Dooks61c7cff2008-07-28 12:04:07 +0100974 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
975 writel(0, i2c->regs + S3C2410_IICCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 dev_err(i2c->dev, "cannot meet bus frequency required\n");
977 return -EINVAL;
978 }
979
980 /* todo - check that the i2c lines aren't being dragged anywhere */
981
982 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
983 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 0;
986}
987
Thomas Abraham5a5f5082011-09-13 09:46:05 +0530988#ifdef CONFIG_OF
989/* s3c24xx_i2c_parse_dt
990 *
991 * Parse the device tree node and retreive the platform data.
992*/
993
994static void
995s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
996{
997 struct s3c2410_platform_i2c *pdata = i2c->pdata;
998
999 if (!np)
1000 return;
1001
1002 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
1003 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
1004 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
1005 of_property_read_u32(np, "samsung,i2c-max-bus-freq",
1006 (u32 *)&pdata->frequency);
1007}
1008#else
1009static void
1010s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
1011{
1012 return;
1013}
1014#endif
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/* s3c24xx_i2c_probe
1017 *
1018 * called by the bus driver when a suitable device is found
1019*/
1020
Russell King3ae5eae2005-11-09 22:32:44 +00001021static int s3c24xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Ben Dooks692acbd2008-10-31 16:10:28 +00001023 struct s3c24xx_i2c *i2c;
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301024 struct s3c2410_platform_i2c *pdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 struct resource *res;
1026 int ret;
1027
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301028 if (!pdev->dev.of_node) {
1029 pdata = pdev->dev.platform_data;
1030 if (!pdata) {
1031 dev_err(&pdev->dev, "no platform data\n");
1032 return -EINVAL;
1033 }
Ben Dooks6a039ca2008-10-31 16:10:27 +00001034 }
Ben Dooks399dee22008-07-28 12:04:06 +01001035
Mark Brown4ea15572012-01-21 13:28:46 +00001036 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
Ben Dooks692acbd2008-10-31 16:10:28 +00001037 if (!i2c) {
1038 dev_err(&pdev->dev, "no memory for state\n");
1039 return -ENOMEM;
1040 }
1041
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301042 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1043 if (!i2c->pdata) {
Tushar Behera669da302013-01-24 15:41:06 +05301044 dev_err(&pdev->dev, "no memory for platform data\n");
1045 return -ENOMEM;
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301046 }
1047
Karol Lewandowski27452492012-04-23 18:24:00 +02001048 i2c->quirks = s3c24xx_get_device_quirks(pdev);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301049 if (pdata)
1050 memcpy(i2c->pdata, pdata, sizeof(*pdata));
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301051 else
1052 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301053
Ben Dooks692acbd2008-10-31 16:10:28 +00001054 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
1055 i2c->adap.owner = THIS_MODULE;
1056 i2c->adap.algo = &s3c24xx_i2c_algorithm;
1057 i2c->adap.retries = 2;
1058 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1059 i2c->tx_setup = 50;
1060
Ben Dooks692acbd2008-10-31 16:10:28 +00001061 init_waitqueue_head(&i2c->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /* find the clock and enable it */
1064
Russell King3ae5eae2005-11-09 22:32:44 +00001065 i2c->dev = &pdev->dev;
Tushar Behera2b255b92013-01-24 15:41:07 +05301066 i2c->clk = devm_clk_get(&pdev->dev, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (IS_ERR(i2c->clk)) {
Russell King3ae5eae2005-11-09 22:32:44 +00001068 dev_err(&pdev->dev, "cannot get clock\n");
Tushar Behera669da302013-01-24 15:41:06 +05301069 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
Russell King3ae5eae2005-11-09 22:32:44 +00001072 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 /* map the registers */
1076
1077 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1078 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001079 dev_err(&pdev->dev, "cannot find IO resource\n");
Tushar Beherad16933b2013-01-24 15:41:08 +05301080 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
Thierry Reding84dbf802013-01-21 11:09:03 +01001083 i2c->regs = devm_ioremap_resource(&pdev->dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Linus Torvalds52caa592013-02-26 09:41:53 -08001085 if (IS_ERR(i2c->regs))
1086 return PTR_ERR(i2c->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Mark Browna72ad452012-11-05 09:33:39 +01001088 dev_dbg(&pdev->dev, "registers %p (%p)\n",
1089 i2c->regs, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /* setup info block for the i2c core */
1092
1093 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001094 i2c->adap.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Tomasz Figa2693ac62012-11-13 11:33:40 +01001096 i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
1097
Abhilash Kesavan658122f2012-11-19 15:47:17 +05301098 /* inititalise the i2c gpio lines */
1099
1100 if (i2c->pdata->cfg_gpio) {
1101 i2c->pdata->cfg_gpio(to_platform_device(i2c->dev));
1102 } else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) {
Tushar Beherad16933b2013-01-24 15:41:08 +05301103 return -EINVAL;
Abhilash Kesavan658122f2012-11-19 15:47:17 +05301104 }
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* initialise the i2c controller */
1107
Tushar Beherad16933b2013-01-24 15:41:08 +05301108 clk_prepare_enable(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 ret = s3c24xx_i2c_init(i2c);
Tushar Beherad16933b2013-01-24 15:41:08 +05301110 clk_disable_unprepare(i2c->clk);
1111 if (ret != 0) {
1112 dev_err(&pdev->dev, "I2C controller init failed\n");
1113 return ret;
1114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /* find the IRQ for this unit (note, this relies on the init call to
Ben Dooks3d0911b2008-10-31 16:10:24 +00001116 * ensure no current IRQs pending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
1118
Ben Dookse0d1ec92008-10-31 16:10:30 +00001119 i2c->irq = ret = platform_get_irq(pdev, 0);
1120 if (ret <= 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001121 dev_err(&pdev->dev, "cannot find IRQ\n");
Tushar Beherad16933b2013-01-24 15:41:08 +05301122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
Tushar Behera2b255b92013-01-24 15:41:07 +05301125 ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 0,
1126 dev_name(&pdev->dev), i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (ret != 0) {
Ben Dookse0d1ec92008-10-31 16:10:30 +00001129 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
Tushar Beherad16933b2013-01-24 15:41:08 +05301130 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
Ben Dooks61c7cff2008-07-28 12:04:07 +01001133 ret = s3c24xx_i2c_register_cpufreq(i2c);
1134 if (ret < 0) {
1135 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
Tushar Beherad16933b2013-01-24 15:41:08 +05301136 return ret;
Ben Dooks61c7cff2008-07-28 12:04:07 +01001137 }
1138
Ben Dooks399dee22008-07-28 12:04:06 +01001139 /* Note, previous versions of the driver used i2c_add_adapter()
1140 * to add the bus at any number. We now pass the bus number via
1141 * the platform data, so if unset it will now default to always
1142 * being bus 0.
1143 */
1144
Thomas Abraham4fd81eb2011-09-13 09:46:04 +05301145 i2c->adap.nr = i2c->pdata->bus_num;
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301146 i2c->adap.dev.of_node = pdev->dev.of_node;
Ben Dooks399dee22008-07-28 12:04:06 +01001147
1148 ret = i2c_add_numbered_adapter(&i2c->adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001150 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
Tushar Beheradc6fea42013-01-24 15:41:09 +05301151 s3c24xx_i2c_deregister_cpufreq(i2c);
1152 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
Thomas Abraham5a5f5082011-09-13 09:46:05 +05301155 of_i2c_register_devices(&i2c->adap);
Russell King3ae5eae2005-11-09 22:32:44 +00001156 platform_set_drvdata(pdev, i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Mark Brownc62c3ca2012-01-21 13:28:47 +00001158 pm_runtime_enable(&pdev->dev);
1159 pm_runtime_enable(&i2c->adap.dev);
1160
Jean Delvare22e965c2009-01-07 14:29:16 +01001161 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
Ben Dooks5b687902007-05-01 23:26:35 +02001162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165/* s3c24xx_i2c_remove
1166 *
1167 * called when device is removed from the bus
1168*/
1169
Russell King3ae5eae2005-11-09 22:32:44 +00001170static int s3c24xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Russell King3ae5eae2005-11-09 22:32:44 +00001172 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Ben Dooks5b687902007-05-01 23:26:35 +02001173
Mark Brownc62c3ca2012-01-21 13:28:47 +00001174 pm_runtime_disable(&i2c->adap.dev);
1175 pm_runtime_disable(&pdev->dev);
1176
Ben Dooks61c7cff2008-07-28 12:04:07 +01001177 s3c24xx_i2c_deregister_cpufreq(i2c);
1178
Ben Dooks5b687902007-05-01 23:26:35 +02001179 i2c_del_adapter(&i2c->adap);
Ben Dooks5b687902007-05-01 23:26:35 +02001180
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001181 clk_disable_unprepare(i2c->clk);
Ben Dooks5b687902007-05-01 23:26:35 +02001182
Tomasz Figa2693ac62012-11-13 11:33:40 +01001183 if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
1184 s3c24xx_i2c_dt_gpio_free(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 return 0;
1187}
1188
Mark Brown2935e0e2012-11-05 09:33:38 +01001189#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001190static int s3c24xx_i2c_suspend_noirq(struct device *dev)
Ben Dooksbe44f012008-10-31 16:10:22 +00001191{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001192 struct platform_device *pdev = to_platform_device(dev);
1193 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1194
Ben Dooksbe44f012008-10-31 16:10:22 +00001195 i2c->suspended = 1;
Magnus Damm6a6c61892009-07-08 13:22:47 +02001196
Ben Dooksbe44f012008-10-31 16:10:22 +00001197 return 0;
1198}
1199
Magnus Damm6a6c61892009-07-08 13:22:47 +02001200static int s3c24xx_i2c_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Magnus Damm6a6c61892009-07-08 13:22:47 +02001202 struct platform_device *pdev = to_platform_device(dev);
1203 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
Russell King9480e302005-10-28 09:52:56 -07001204
Ben Dooksbe44f012008-10-31 16:10:22 +00001205 i2c->suspended = 0;
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001206 clk_prepare_enable(i2c->clk);
Ben Dooksbe44f012008-10-31 16:10:22 +00001207 s3c24xx_i2c_init(i2c);
Thomas Abrahamd3b64c52012-10-03 08:26:39 +09001208 clk_disable_unprepare(i2c->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 return 0;
1211}
Mark Brown2935e0e2012-11-05 09:33:38 +01001212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Mark Brown2935e0e2012-11-05 09:33:38 +01001214#ifdef CONFIG_PM
Alexey Dobriyan47145212009-12-14 18:00:08 -08001215static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
Mark Brown2935e0e2012-11-05 09:33:38 +01001216#ifdef CONFIG_PM_SLEEP
Magnus Damm6a6c61892009-07-08 13:22:47 +02001217 .suspend_noirq = s3c24xx_i2c_suspend_noirq,
1218 .resume = s3c24xx_i2c_resume,
Mark Brown2935e0e2012-11-05 09:33:38 +01001219#endif
Magnus Damm6a6c61892009-07-08 13:22:47 +02001220};
1221
1222#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223#else
Magnus Damm6a6c61892009-07-08 13:22:47 +02001224#define S3C24XX_DEV_PM_OPS NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225#endif
1226
1227/* device driver for platform bus bits */
1228
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001229static struct platform_driver s3c24xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 .probe = s3c24xx_i2c_probe,
1231 .remove = s3c24xx_i2c_remove,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001232 .id_table = s3c24xx_driver_ids,
Russell King3ae5eae2005-11-09 22:32:44 +00001233 .driver = {
1234 .owner = THIS_MODULE,
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001235 .name = "s3c-i2c",
Magnus Damm6a6c61892009-07-08 13:22:47 +02001236 .pm = S3C24XX_DEV_PM_OPS,
Karol Lewandowski9df7ead2012-03-21 20:11:51 +01001237 .of_match_table = of_match_ptr(s3c24xx_i2c_match),
Russell King3ae5eae2005-11-09 22:32:44 +00001238 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239};
1240
1241static int __init i2c_adap_s3c_init(void)
1242{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001243 return platform_driver_register(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
Mark Brown18dc83a2009-02-26 16:29:22 +00001245subsys_initcall(i2c_adap_s3c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247static void __exit i2c_adap_s3c_exit(void)
1248{
Ben Dooks7d85ccd2009-06-12 10:45:29 +01001249 platform_driver_unregister(&s3c24xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251module_exit(i2c_adap_s3c_exit);
1252
1253MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1254MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1255MODULE_LICENSE("GPL");