1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* linux/drivers/i2c/busses/i2c-s3c2410.c
4 * Copyright (C) 2004,2005,2009 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>
7 * S3C2410 I2C Controller
10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/time.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/clk.h>
23 #include <linux/cpufreq.h>
24 #include <linux/slab.h>
27 #include <linux/of_device.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/pinctrl/consumer.h>
30 #include <linux/mfd/syscon.h>
31 #include <linux/regmap.h>
35 #include <linux/platform_data/i2c-s3c2410.h>
37 /* see s3c2410x user guide, v1.1, section 9 (p447) for more info */
39 #define S3C2410_IICCON 0x00
40 #define S3C2410_IICSTAT 0x04
41 #define S3C2410_IICADD 0x08
42 #define S3C2410_IICDS 0x0C
43 #define S3C2440_IICLC 0x10
45 #define S3C2410_IICCON_ACKEN (1 << 7)
46 #define S3C2410_IICCON_TXDIV_16 (0 << 6)
47 #define S3C2410_IICCON_TXDIV_512 (1 << 6)
48 #define S3C2410_IICCON_IRQEN (1 << 5)
49 #define S3C2410_IICCON_IRQPEND (1 << 4)
50 #define S3C2410_IICCON_SCALE(x) ((x) & 0xf)
51 #define S3C2410_IICCON_SCALEMASK (0xf)
53 #define S3C2410_IICSTAT_MASTER_RX (2 << 6)
54 #define S3C2410_IICSTAT_MASTER_TX (3 << 6)
55 #define S3C2410_IICSTAT_SLAVE_RX (0 << 6)
56 #define S3C2410_IICSTAT_SLAVE_TX (1 << 6)
57 #define S3C2410_IICSTAT_MODEMASK (3 << 6)
59 #define S3C2410_IICSTAT_START (1 << 5)
60 #define S3C2410_IICSTAT_BUSBUSY (1 << 5)
61 #define S3C2410_IICSTAT_TXRXEN (1 << 4)
62 #define S3C2410_IICSTAT_ARBITR (1 << 3)
63 #define S3C2410_IICSTAT_ASSLAVE (1 << 2)
64 #define S3C2410_IICSTAT_ADDR0 (1 << 1)
65 #define S3C2410_IICSTAT_LASTBIT (1 << 0)
67 #define S3C2410_IICLC_SDA_DELAY0 (0 << 0)
68 #define S3C2410_IICLC_SDA_DELAY5 (1 << 0)
69 #define S3C2410_IICLC_SDA_DELAY10 (2 << 0)
70 #define S3C2410_IICLC_SDA_DELAY15 (3 << 0)
71 #define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0)
73 #define S3C2410_IICLC_FILTER_ON (1 << 2)
75 /* Treat S3C2410 as baseline hardware, anything else is supported via quirks */
76 #define QUIRK_S3C2440 (1 << 0)
77 #define QUIRK_HDMIPHY (1 << 1)
78 #define QUIRK_NO_GPIO (1 << 2)
79 #define QUIRK_POLL (1 << 3)
81 /* Max time to wait for bus to become idle after a xfer (in us) */
82 #define S3C2410_IDLE_TIMEOUT 5000
84 /* Exynos5 Sysreg offset */
85 #define EXYNOS5_SYS_I2C_CFG 0x0234
87 /* i2c controller state */
88 enum s3c24xx_i2c_state {
97 wait_queue_head_t wait;
98 kernel_ulong_t quirks;
101 unsigned int msg_num;
102 unsigned int msg_idx;
103 unsigned int msg_ptr;
105 unsigned int tx_setup;
108 enum s3c24xx_i2c_state state;
109 unsigned long clkrate;
114 struct i2c_adapter adap;
116 struct s3c2410_platform_i2c *pdata;
117 struct gpio_desc *gpios[2];
118 struct pinctrl *pctrl;
119 struct regmap *sysreg;
120 unsigned int sys_i2c_cfg;
123 static const struct platform_device_id s3c24xx_driver_ids[] = {
125 .name = "s3c2410-i2c",
128 .name = "s3c2440-i2c",
129 .driver_data = QUIRK_S3C2440,
131 .name = "s3c2440-hdmiphy-i2c",
132 .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
135 MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
137 static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat);
140 static const struct of_device_id s3c24xx_i2c_match[] = {
141 { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
142 { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
143 { .compatible = "samsung,s3c2440-hdmiphy-i2c",
144 .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
145 { .compatible = "samsung,exynos5-sata-phy-i2c",
146 .data = (void *)(QUIRK_S3C2440 | QUIRK_POLL | QUIRK_NO_GPIO) },
149 MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
153 * Get controller type either from device tree or platform device variant.
155 static inline kernel_ulong_t s3c24xx_get_device_quirks(struct platform_device *pdev)
157 if (pdev->dev.of_node)
158 return (kernel_ulong_t)of_device_get_match_data(&pdev->dev);
160 return platform_get_device_id(pdev)->driver_data;
164 * Complete the message and wake up the caller, using the given return code,
165 * or zero to mean ok.
167 static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
169 dev_dbg(i2c->dev, "master_complete %d\n", ret);
178 if (!(i2c->quirks & QUIRK_POLL))
182 static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
186 tmp = readl(i2c->regs + S3C2410_IICCON);
187 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
190 static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
194 tmp = readl(i2c->regs + S3C2410_IICCON);
195 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
198 /* irq enable/disable functions */
199 static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
203 tmp = readl(i2c->regs + S3C2410_IICCON);
204 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
207 static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
211 tmp = readl(i2c->regs + S3C2410_IICCON);
212 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
215 static bool is_ack(struct s3c24xx_i2c *i2c)
219 for (tries = 50; tries; --tries) {
220 if (readl(i2c->regs + S3C2410_IICCON)
221 & S3C2410_IICCON_IRQPEND) {
222 if (!(readl(i2c->regs + S3C2410_IICSTAT)
223 & S3C2410_IICSTAT_LASTBIT))
226 usleep_range(1000, 2000);
228 dev_err(i2c->dev, "ack was not received\n");
233 * put the start of a message onto the bus
235 static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
238 unsigned int addr = (msg->addr & 0x7f) << 1;
240 unsigned long iiccon;
243 stat |= S3C2410_IICSTAT_TXRXEN;
245 if (msg->flags & I2C_M_RD) {
246 stat |= S3C2410_IICSTAT_MASTER_RX;
249 stat |= S3C2410_IICSTAT_MASTER_TX;
251 if (msg->flags & I2C_M_REV_DIR_ADDR)
254 /* todo - check for whether ack wanted or not */
255 s3c24xx_i2c_enable_ack(i2c);
257 iiccon = readl(i2c->regs + S3C2410_IICCON);
258 writel(stat, i2c->regs + S3C2410_IICSTAT);
260 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
261 writeb(addr, i2c->regs + S3C2410_IICDS);
264 * delay here to ensure the data byte has gotten onto the bus
265 * before the transaction is started
267 ndelay(i2c->tx_setup);
269 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
270 writel(iiccon, i2c->regs + S3C2410_IICCON);
272 stat |= S3C2410_IICSTAT_START;
273 writel(stat, i2c->regs + S3C2410_IICSTAT);
275 if (i2c->quirks & QUIRK_POLL) {
276 while ((i2c->msg_num != 0) && is_ack(i2c)) {
277 i2c_s3c_irq_nextbyte(i2c, stat);
278 stat = readl(i2c->regs + S3C2410_IICSTAT);
280 if (stat & S3C2410_IICSTAT_ARBITR)
281 dev_err(i2c->dev, "deal with arbitration loss\n");
286 static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
288 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
290 dev_dbg(i2c->dev, "STOP\n");
293 * The datasheet says that the STOP sequence should be:
294 * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP')
295 * 2) I2CCON.4 = 0 - Clear IRQPEND
296 * 3) Wait until the stop condition takes effect.
297 * 4*) I2CSTAT.4 = 0 - Clear TXRXEN
299 * Where, step "4*" is only for buses with the "HDMIPHY" quirk.
301 * However, after much experimentation, it appears that:
302 * a) normal buses automatically clear BUSY and transition from
303 * Master->Slave when they complete generating a STOP condition.
304 * Therefore, step (3) can be done in doxfer() by polling I2CCON.4
305 * after starting the STOP generation here.
306 * b) HDMIPHY bus does neither, so there is no way to do step 3.
307 * There is no indication when this bus has finished generating
310 * In fact, we have found that as soon as the IRQPEND bit is cleared in
311 * step 2, the HDMIPHY bus generates the STOP condition, and then
312 * immediately starts transferring another data byte, even though the
313 * bus is supposedly stopped. This is presumably because the bus is
314 * still in "Master" mode, and its BUSY bit is still set.
316 * To avoid these extra post-STOP transactions on HDMI phy devices, we
317 * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly,
318 * instead of first generating a proper STOP condition. This should
319 * float SDA & SCK terminating the transfer. Subsequent transfers
320 * start with a proper START condition, and proceed normally.
322 * The HDMIPHY bus is an internal bus that always has exactly two
323 * devices, the host as Master and the HDMIPHY device as the slave.
324 * Skipping the STOP condition has been tested on this bus and works.
326 if (i2c->quirks & QUIRK_HDMIPHY) {
327 /* Stop driving the I2C pins */
328 iicstat &= ~S3C2410_IICSTAT_TXRXEN;
330 /* stop the transfer */
331 iicstat &= ~S3C2410_IICSTAT_START;
333 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
335 i2c->state = STATE_STOP;
337 s3c24xx_i2c_master_complete(i2c, ret);
338 s3c24xx_i2c_disable_irq(i2c);
342 * helper functions to determine the current state in the set of
343 * messages we are sending
347 * returns TRUE if the current message is the last in the set
349 static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
351 return i2c->msg_idx >= (i2c->msg_num - 1);
355 * returns TRUE if we this is the last byte in the current message
357 static inline int is_msglast(struct s3c24xx_i2c *i2c)
360 * msg->len is always 1 for the first byte of smbus block read.
361 * Actual length will be read from slave. More bytes will be
362 * read according to the length then.
364 if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1)
367 return i2c->msg_ptr == i2c->msg->len-1;
371 * returns TRUE if we reached the end of the current message
373 static inline int is_msgend(struct s3c24xx_i2c *i2c)
375 return i2c->msg_ptr >= i2c->msg->len;
379 * process an interrupt and work out what to do
381 static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
387 switch (i2c->state) {
390 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
394 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
395 s3c24xx_i2c_disable_irq(i2c);
400 * last thing we did was send a start condition on the
401 * bus, or started a new i2c message
403 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
404 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
405 /* ack was not received... */
406 dev_dbg(i2c->dev, "ack was not received\n");
407 s3c24xx_i2c_stop(i2c, -ENXIO);
411 if (i2c->msg->flags & I2C_M_RD)
412 i2c->state = STATE_READ;
414 i2c->state = STATE_WRITE;
417 * Terminate the transfer if there is nothing to do
418 * as this is used by the i2c probe to find devices.
420 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
421 s3c24xx_i2c_stop(i2c, 0);
425 if (i2c->state == STATE_READ)
429 * fall through to the write state, as we will need to
430 * send a byte as well
435 * we are writing data to the device... check for the
436 * end of the message, and if so, work out what to do
438 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
439 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
440 dev_dbg(i2c->dev, "WRITE: No Ack\n");
442 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
449 if (!is_msgend(i2c)) {
450 byte = i2c->msg->buf[i2c->msg_ptr++];
451 writeb(byte, i2c->regs + S3C2410_IICDS);
454 * delay after writing the byte to allow the
455 * data setup time on the bus, as writing the
456 * data to the register causes the first bit
457 * to appear on SDA, and SCL will change as
458 * soon as the interrupt is acknowledged
460 ndelay(i2c->tx_setup);
462 } else if (!is_lastmsg(i2c)) {
463 /* we need to go to the next i2c message */
465 dev_dbg(i2c->dev, "WRITE: Next Message\n");
471 /* check to see if we need to do another message */
472 if (i2c->msg->flags & I2C_M_NOSTART) {
474 if (i2c->msg->flags & I2C_M_RD) {
476 * cannot do this, the controller
477 * forces us to send a new START
478 * when we change direction
481 "missing START before write->read\n");
482 s3c24xx_i2c_stop(i2c, -EINVAL);
488 /* send the new start */
489 s3c24xx_i2c_message_start(i2c, i2c->msg);
490 i2c->state = STATE_START;
495 s3c24xx_i2c_stop(i2c, 0);
501 * we have a byte of data in the data register, do
502 * something with it, and then work out whether we are
503 * going to do any more read/write
505 byte = readb(i2c->regs + S3C2410_IICDS);
506 i2c->msg->buf[i2c->msg_ptr++] = byte;
508 /* Add actual length to read for smbus block read */
509 if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1)
510 i2c->msg->len += byte;
512 if (is_msglast(i2c)) {
513 /* last byte of buffer */
516 s3c24xx_i2c_disable_ack(i2c);
518 } else if (is_msgend(i2c)) {
520 * ok, we've read the entire buffer, see if there
521 * is anything else we need to do
523 if (is_lastmsg(i2c)) {
524 /* last message, send stop and complete */
525 dev_dbg(i2c->dev, "READ: Send Stop\n");
527 s3c24xx_i2c_stop(i2c, 0);
529 /* go to the next transfer */
530 dev_dbg(i2c->dev, "READ: Next Transfer\n");
541 /* acknowlegde the IRQ and get back on with the work */
544 tmp = readl(i2c->regs + S3C2410_IICCON);
545 tmp &= ~S3C2410_IICCON_IRQPEND;
546 writel(tmp, i2c->regs + S3C2410_IICCON);
552 * top level IRQ servicing routine
554 static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
556 struct s3c24xx_i2c *i2c = dev_id;
557 unsigned long status;
560 status = readl(i2c->regs + S3C2410_IICSTAT);
562 if (status & S3C2410_IICSTAT_ARBITR) {
563 /* deal with arbitration loss */
564 dev_err(i2c->dev, "deal with arbitration loss\n");
567 if (i2c->state == STATE_IDLE) {
568 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
570 tmp = readl(i2c->regs + S3C2410_IICCON);
571 tmp &= ~S3C2410_IICCON_IRQPEND;
572 writel(tmp, i2c->regs + S3C2410_IICCON);
577 * pretty much this leaves us with the fact that we've
578 * transmitted or received whatever byte we last sent
580 i2c_s3c_irq_nextbyte(i2c, status);
587 * Disable the bus so that we won't get any interrupts from now on, or try
588 * to drive any lines. This is the default state when we don't have
589 * anything to send/receive.
591 * If there is an event on the bus, or we have a pre-existing event at
592 * kernel boot time, we may not notice the event and the I2C controller
593 * will lock the bus with the I2C clock line low indefinitely.
595 static inline void s3c24xx_i2c_disable_bus(struct s3c24xx_i2c *i2c)
599 /* Stop driving the I2C pins */
600 tmp = readl(i2c->regs + S3C2410_IICSTAT);
601 tmp &= ~S3C2410_IICSTAT_TXRXEN;
602 writel(tmp, i2c->regs + S3C2410_IICSTAT);
604 /* We don't expect any interrupts now, and don't want send acks */
605 tmp = readl(i2c->regs + S3C2410_IICCON);
606 tmp &= ~(S3C2410_IICCON_IRQEN | S3C2410_IICCON_IRQPEND |
607 S3C2410_IICCON_ACKEN);
608 writel(tmp, i2c->regs + S3C2410_IICCON);
613 * get the i2c bus for a master transaction
615 static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
617 unsigned long iicstat;
620 while (timeout-- > 0) {
621 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
623 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
633 * wait for the i2c bus to become idle.
635 static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
637 unsigned long iicstat;
642 /* ensure the stop has been through the bus */
644 dev_dbg(i2c->dev, "waiting for bus idle\n");
646 start = now = ktime_get();
649 * Most of the time, the bus is already idle within a few usec of the
650 * end of a transaction. However, really slow i2c devices can stretch
651 * the clock, delaying STOP generation.
653 * On slower SoCs this typically happens within a very small number of
654 * instructions so busy wait briefly to avoid scheduling overhead.
657 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
658 while ((iicstat & S3C2410_IICSTAT_START) && --spins) {
660 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
664 * If we do get an appreciable delay as a compromise between idle
665 * detection latency for the normal, fast case, and system load in the
666 * slow device case, use an exponential back off in the polling loop,
667 * up to 1/10th of the total timeout, then continue to poll at a
668 * constant rate up to the timeout.
671 while ((iicstat & S3C2410_IICSTAT_START) &&
672 ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) {
673 usleep_range(delay, 2 * delay);
674 if (delay < S3C2410_IDLE_TIMEOUT / 10)
677 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
680 if (iicstat & S3C2410_IICSTAT_START)
681 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
685 * this starts an i2c transfer
687 static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
688 struct i2c_msg *msgs, int num)
690 unsigned long timeout;
693 ret = s3c24xx_i2c_set_master(i2c);
695 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
704 i2c->state = STATE_START;
706 s3c24xx_i2c_enable_irq(i2c);
707 s3c24xx_i2c_message_start(i2c, msgs);
709 if (i2c->quirks & QUIRK_POLL) {
713 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
718 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
723 * Having these next two as dev_err() makes life very
724 * noisy when doing an i2cdetect
727 dev_dbg(i2c->dev, "timeout\n");
729 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
731 /* For QUIRK_HDMIPHY, bus is already disabled */
732 if (i2c->quirks & QUIRK_HDMIPHY)
735 s3c24xx_i2c_wait_idle(i2c);
737 s3c24xx_i2c_disable_bus(i2c);
740 i2c->state = STATE_IDLE;
746 * first port of call from the i2c bus code when an message needs
747 * transferring across the i2c bus.
749 static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
750 struct i2c_msg *msgs, int num)
752 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
756 ret = clk_enable(i2c->clk);
760 for (retry = 0; retry < adap->retries; retry++) {
762 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
764 if (ret != -EAGAIN) {
765 clk_disable(i2c->clk);
769 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
774 clk_disable(i2c->clk);
778 /* declare our i2c functionality */
779 static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
781 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL_ALL | I2C_FUNC_NOSTART |
782 I2C_FUNC_PROTOCOL_MANGLING;
785 /* i2c bus registration info */
786 static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
787 .master_xfer = s3c24xx_i2c_xfer,
788 .functionality = s3c24xx_i2c_func,
792 * return the divisor settings for a given frequency
794 static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
795 unsigned int *div1, unsigned int *divs)
797 unsigned int calc_divs = clkin / wanted;
798 unsigned int calc_div1;
800 if (calc_divs > (16*16))
805 calc_divs += calc_div1-1;
806 calc_divs /= calc_div1;
816 return clkin / (calc_divs * calc_div1);
820 * work out a divisor for the user requested frequency setting,
821 * either by the requested frequency, or scanning the acceptable
822 * range of frequencies until something is found
824 static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
826 struct s3c2410_platform_i2c *pdata = i2c->pdata;
827 unsigned long clkin = clk_get_rate(i2c->clk);
828 unsigned int divs, div1;
829 unsigned long target_frequency;
833 i2c->clkrate = clkin;
834 clkin /= 1000; /* clkin now in KHz */
836 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
838 target_frequency = pdata->frequency ?: I2C_MAX_STANDARD_MODE_FREQ;
840 target_frequency /= 1000; /* Target frequency now in KHz */
842 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
844 if (freq > target_frequency) {
846 "Unable to achieve desired frequency %luKHz." \
847 " Lowest achievable %dKHz\n", target_frequency, freq);
853 iiccon = readl(i2c->regs + S3C2410_IICCON);
854 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
858 iiccon |= S3C2410_IICCON_TXDIV_512;
860 if (i2c->quirks & QUIRK_POLL)
861 iiccon |= S3C2410_IICCON_SCALE(2);
863 writel(iiccon, i2c->regs + S3C2410_IICCON);
865 if (i2c->quirks & QUIRK_S3C2440) {
866 unsigned long sda_delay;
868 if (pdata->sda_delay) {
869 sda_delay = clkin * pdata->sda_delay;
870 sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
871 sda_delay = DIV_ROUND_UP(sda_delay, 5);
874 sda_delay |= S3C2410_IICLC_FILTER_ON;
878 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
879 writel(sda_delay, i2c->regs + S3C2440_IICLC);
886 static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
890 if (i2c->quirks & QUIRK_NO_GPIO)
893 for (i = 0; i < 2; i++) {
894 i2c->gpios[i] = devm_gpiod_get_index(i2c->dev, NULL,
896 if (IS_ERR(i2c->gpios[i])) {
897 dev_err(i2c->dev, "i2c gpio invalid at index %d\n", i);
905 static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
912 * initialise the controller, set the IO lines and frequency
914 static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
916 struct s3c2410_platform_i2c *pdata;
919 /* get the plafrom data */
923 /* write slave address */
925 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
927 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
929 writel(0, i2c->regs + S3C2410_IICCON);
930 writel(0, i2c->regs + S3C2410_IICSTAT);
932 /* we need to work out the divisors for the clock... */
934 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
935 dev_err(i2c->dev, "cannot meet bus frequency required\n");
939 /* todo - check that the i2c lines aren't being dragged anywhere */
941 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
942 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02x\n",
943 readl(i2c->regs + S3C2410_IICCON));
950 * Parse the device tree node and retreive the platform data.
953 s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
955 struct s3c2410_platform_i2c *pdata = i2c->pdata;
961 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
962 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
963 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
964 of_property_read_u32(np, "samsung,i2c-max-bus-freq",
965 (u32 *)&pdata->frequency);
967 * Exynos5's legacy i2c controller and new high speed i2c
968 * controller have muxed interrupt sources. By default the
969 * interrupts for 4-channel HS-I2C controller are enabled.
970 * If nodes for first four channels of legacy i2c controller
971 * are available then re-configure the interrupts via the
974 id = of_alias_get_id(np, "i2c");
975 i2c->sysreg = syscon_regmap_lookup_by_phandle(np,
976 "samsung,sysreg-phandle");
977 if (IS_ERR(i2c->sysreg))
980 regmap_update_bits(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, BIT(id), 0);
984 s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) { }
987 static int s3c24xx_i2c_probe(struct platform_device *pdev)
989 struct s3c24xx_i2c *i2c;
990 struct s3c2410_platform_i2c *pdata = NULL;
991 struct resource *res;
994 if (!pdev->dev.of_node) {
995 pdata = dev_get_platdata(&pdev->dev);
997 dev_err(&pdev->dev, "no platform data\n");
1002 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
1006 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1010 i2c->quirks = s3c24xx_get_device_quirks(pdev);
1011 i2c->sysreg = ERR_PTR(-ENOENT);
1013 memcpy(i2c->pdata, pdata, sizeof(*pdata));
1015 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
1017 strscpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
1018 i2c->adap.owner = THIS_MODULE;
1019 i2c->adap.algo = &s3c24xx_i2c_algorithm;
1020 i2c->adap.retries = 2;
1021 i2c->adap.class = I2C_CLASS_DEPRECATED;
1024 init_waitqueue_head(&i2c->wait);
1026 /* find the clock and enable it */
1027 i2c->dev = &pdev->dev;
1028 i2c->clk = devm_clk_get(&pdev->dev, "i2c");
1029 if (IS_ERR(i2c->clk)) {
1030 dev_err(&pdev->dev, "cannot get clock\n");
1034 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
1036 /* map the registers */
1037 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1038 i2c->regs = devm_ioremap_resource(&pdev->dev, res);
1040 if (IS_ERR(i2c->regs))
1041 return PTR_ERR(i2c->regs);
1043 dev_dbg(&pdev->dev, "registers %p (%p)\n",
1046 /* setup info block for the i2c core */
1047 i2c->adap.algo_data = i2c;
1048 i2c->adap.dev.parent = &pdev->dev;
1049 i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
1051 /* inititalise the i2c gpio lines */
1052 if (i2c->pdata->cfg_gpio)
1053 i2c->pdata->cfg_gpio(to_platform_device(i2c->dev));
1054 else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c))
1057 /* initialise the i2c controller */
1058 ret = clk_prepare_enable(i2c->clk);
1060 dev_err(&pdev->dev, "I2C clock enable failed\n");
1064 ret = s3c24xx_i2c_init(i2c);
1065 clk_disable(i2c->clk);
1067 dev_err(&pdev->dev, "I2C controller init failed\n");
1068 clk_unprepare(i2c->clk);
1073 * find the IRQ for this unit (note, this relies on the init call to
1074 * ensure no current IRQs pending
1076 if (!(i2c->quirks & QUIRK_POLL)) {
1077 i2c->irq = ret = platform_get_irq(pdev, 0);
1079 dev_err(&pdev->dev, "cannot find IRQ\n");
1080 clk_unprepare(i2c->clk);
1084 ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq,
1085 0, dev_name(&pdev->dev), i2c);
1087 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
1088 clk_unprepare(i2c->clk);
1094 * Note, previous versions of the driver used i2c_add_adapter()
1095 * to add the bus at any number. We now pass the bus number via
1096 * the platform data, so if unset it will now default to always
1099 i2c->adap.nr = i2c->pdata->bus_num;
1100 i2c->adap.dev.of_node = pdev->dev.of_node;
1102 platform_set_drvdata(pdev, i2c);
1104 pm_runtime_enable(&pdev->dev);
1106 ret = i2c_add_numbered_adapter(&i2c->adap);
1108 pm_runtime_disable(&pdev->dev);
1109 clk_unprepare(i2c->clk);
1113 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
1117 static void s3c24xx_i2c_remove(struct platform_device *pdev)
1119 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1121 clk_unprepare(i2c->clk);
1123 pm_runtime_disable(&pdev->dev);
1125 i2c_del_adapter(&i2c->adap);
1128 #ifdef CONFIG_PM_SLEEP
1129 static int s3c24xx_i2c_suspend_noirq(struct device *dev)
1131 struct s3c24xx_i2c *i2c = dev_get_drvdata(dev);
1133 i2c_mark_adapter_suspended(&i2c->adap);
1135 if (!IS_ERR(i2c->sysreg))
1136 regmap_read(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, &i2c->sys_i2c_cfg);
1141 static int s3c24xx_i2c_resume_noirq(struct device *dev)
1143 struct s3c24xx_i2c *i2c = dev_get_drvdata(dev);
1146 if (!IS_ERR(i2c->sysreg))
1147 regmap_write(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, i2c->sys_i2c_cfg);
1149 ret = clk_enable(i2c->clk);
1152 s3c24xx_i2c_init(i2c);
1153 clk_disable(i2c->clk);
1154 i2c_mark_adapter_resumed(&i2c->adap);
1161 static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
1162 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(s3c24xx_i2c_suspend_noirq,
1163 s3c24xx_i2c_resume_noirq)
1166 #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
1168 #define S3C24XX_DEV_PM_OPS NULL
1171 static struct platform_driver s3c24xx_i2c_driver = {
1172 .probe = s3c24xx_i2c_probe,
1173 .remove_new = s3c24xx_i2c_remove,
1174 .id_table = s3c24xx_driver_ids,
1177 .pm = S3C24XX_DEV_PM_OPS,
1178 .of_match_table = of_match_ptr(s3c24xx_i2c_match),
1182 static int __init i2c_adap_s3c_init(void)
1184 return platform_driver_register(&s3c24xx_i2c_driver);
1186 subsys_initcall(i2c_adap_s3c_init);
1188 static void __exit i2c_adap_s3c_exit(void)
1190 platform_driver_unregister(&s3c24xx_i2c_driver);
1192 module_exit(i2c_adap_s3c_exit);
1194 MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1195 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1196 MODULE_LICENSE("GPL");