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
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 #if defined(CONFIG_ARM_S3C24XX_CPUFREQ)
120 struct notifier_block freq_transition;
122 struct regmap *sysreg;
123 unsigned int sys_i2c_cfg;
126 static const struct platform_device_id s3c24xx_driver_ids[] = {
128 .name = "s3c2410-i2c",
131 .name = "s3c2440-i2c",
132 .driver_data = QUIRK_S3C2440,
134 .name = "s3c2440-hdmiphy-i2c",
135 .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
138 MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
140 static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat);
143 static const struct of_device_id s3c24xx_i2c_match[] = {
144 { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
145 { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
146 { .compatible = "samsung,s3c2440-hdmiphy-i2c",
147 .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
148 { .compatible = "samsung,exynos5-sata-phy-i2c",
149 .data = (void *)(QUIRK_S3C2440 | QUIRK_POLL | QUIRK_NO_GPIO) },
152 MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
156 * Get controller type either from device tree or platform device variant.
158 static inline kernel_ulong_t s3c24xx_get_device_quirks(struct platform_device *pdev)
160 if (pdev->dev.of_node)
161 return (kernel_ulong_t)of_device_get_match_data(&pdev->dev);
163 return platform_get_device_id(pdev)->driver_data;
167 * Complete the message and wake up the caller, using the given return code,
168 * or zero to mean ok.
170 static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
172 dev_dbg(i2c->dev, "master_complete %d\n", ret);
181 if (!(i2c->quirks & QUIRK_POLL))
185 static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
189 tmp = readl(i2c->regs + S3C2410_IICCON);
190 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
193 static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
197 tmp = readl(i2c->regs + S3C2410_IICCON);
198 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
201 /* irq enable/disable functions */
202 static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
206 tmp = readl(i2c->regs + S3C2410_IICCON);
207 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
210 static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
214 tmp = readl(i2c->regs + S3C2410_IICCON);
215 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
218 static bool is_ack(struct s3c24xx_i2c *i2c)
222 for (tries = 50; tries; --tries) {
223 if (readl(i2c->regs + S3C2410_IICCON)
224 & S3C2410_IICCON_IRQPEND) {
225 if (!(readl(i2c->regs + S3C2410_IICSTAT)
226 & S3C2410_IICSTAT_LASTBIT))
229 usleep_range(1000, 2000);
231 dev_err(i2c->dev, "ack was not received\n");
236 * put the start of a message onto the bus
238 static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
241 unsigned int addr = (msg->addr & 0x7f) << 1;
243 unsigned long iiccon;
246 stat |= S3C2410_IICSTAT_TXRXEN;
248 if (msg->flags & I2C_M_RD) {
249 stat |= S3C2410_IICSTAT_MASTER_RX;
252 stat |= S3C2410_IICSTAT_MASTER_TX;
254 if (msg->flags & I2C_M_REV_DIR_ADDR)
257 /* todo - check for whether ack wanted or not */
258 s3c24xx_i2c_enable_ack(i2c);
260 iiccon = readl(i2c->regs + S3C2410_IICCON);
261 writel(stat, i2c->regs + S3C2410_IICSTAT);
263 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
264 writeb(addr, i2c->regs + S3C2410_IICDS);
267 * delay here to ensure the data byte has gotten onto the bus
268 * before the transaction is started
270 ndelay(i2c->tx_setup);
272 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
273 writel(iiccon, i2c->regs + S3C2410_IICCON);
275 stat |= S3C2410_IICSTAT_START;
276 writel(stat, i2c->regs + S3C2410_IICSTAT);
278 if (i2c->quirks & QUIRK_POLL) {
279 while ((i2c->msg_num != 0) && is_ack(i2c)) {
280 i2c_s3c_irq_nextbyte(i2c, stat);
281 stat = readl(i2c->regs + S3C2410_IICSTAT);
283 if (stat & S3C2410_IICSTAT_ARBITR)
284 dev_err(i2c->dev, "deal with arbitration loss\n");
289 static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
291 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
293 dev_dbg(i2c->dev, "STOP\n");
296 * The datasheet says that the STOP sequence should be:
297 * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP')
298 * 2) I2CCON.4 = 0 - Clear IRQPEND
299 * 3) Wait until the stop condition takes effect.
300 * 4*) I2CSTAT.4 = 0 - Clear TXRXEN
302 * Where, step "4*" is only for buses with the "HDMIPHY" quirk.
304 * However, after much experimentation, it appears that:
305 * a) normal buses automatically clear BUSY and transition from
306 * Master->Slave when they complete generating a STOP condition.
307 * Therefore, step (3) can be done in doxfer() by polling I2CCON.4
308 * after starting the STOP generation here.
309 * b) HDMIPHY bus does neither, so there is no way to do step 3.
310 * There is no indication when this bus has finished generating
313 * In fact, we have found that as soon as the IRQPEND bit is cleared in
314 * step 2, the HDMIPHY bus generates the STOP condition, and then
315 * immediately starts transferring another data byte, even though the
316 * bus is supposedly stopped. This is presumably because the bus is
317 * still in "Master" mode, and its BUSY bit is still set.
319 * To avoid these extra post-STOP transactions on HDMI phy devices, we
320 * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly,
321 * instead of first generating a proper STOP condition. This should
322 * float SDA & SCK terminating the transfer. Subsequent transfers
323 * start with a proper START condition, and proceed normally.
325 * The HDMIPHY bus is an internal bus that always has exactly two
326 * devices, the host as Master and the HDMIPHY device as the slave.
327 * Skipping the STOP condition has been tested on this bus and works.
329 if (i2c->quirks & QUIRK_HDMIPHY) {
330 /* Stop driving the I2C pins */
331 iicstat &= ~S3C2410_IICSTAT_TXRXEN;
333 /* stop the transfer */
334 iicstat &= ~S3C2410_IICSTAT_START;
336 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
338 i2c->state = STATE_STOP;
340 s3c24xx_i2c_master_complete(i2c, ret);
341 s3c24xx_i2c_disable_irq(i2c);
345 * helper functions to determine the current state in the set of
346 * messages we are sending
350 * returns TRUE if the current message is the last in the set
352 static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
354 return i2c->msg_idx >= (i2c->msg_num - 1);
358 * returns TRUE if we this is the last byte in the current message
360 static inline int is_msglast(struct s3c24xx_i2c *i2c)
363 * msg->len is always 1 for the first byte of smbus block read.
364 * Actual length will be read from slave. More bytes will be
365 * read according to the length then.
367 if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1)
370 return i2c->msg_ptr == i2c->msg->len-1;
374 * returns TRUE if we reached the end of the current message
376 static inline int is_msgend(struct s3c24xx_i2c *i2c)
378 return i2c->msg_ptr >= i2c->msg->len;
382 * process an interrupt and work out what to do
384 static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
390 switch (i2c->state) {
393 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
397 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
398 s3c24xx_i2c_disable_irq(i2c);
403 * last thing we did was send a start condition on the
404 * bus, or started a new i2c message
406 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
407 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
408 /* ack was not received... */
409 dev_dbg(i2c->dev, "ack was not received\n");
410 s3c24xx_i2c_stop(i2c, -ENXIO);
414 if (i2c->msg->flags & I2C_M_RD)
415 i2c->state = STATE_READ;
417 i2c->state = STATE_WRITE;
420 * Terminate the transfer if there is nothing to do
421 * as this is used by the i2c probe to find devices.
423 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
424 s3c24xx_i2c_stop(i2c, 0);
428 if (i2c->state == STATE_READ)
432 * fall through to the write state, as we will need to
433 * send a byte as well
438 * we are writing data to the device... check for the
439 * end of the message, and if so, work out what to do
441 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
442 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
443 dev_dbg(i2c->dev, "WRITE: No Ack\n");
445 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
452 if (!is_msgend(i2c)) {
453 byte = i2c->msg->buf[i2c->msg_ptr++];
454 writeb(byte, i2c->regs + S3C2410_IICDS);
457 * delay after writing the byte to allow the
458 * data setup time on the bus, as writing the
459 * data to the register causes the first bit
460 * to appear on SDA, and SCL will change as
461 * soon as the interrupt is acknowledged
463 ndelay(i2c->tx_setup);
465 } else if (!is_lastmsg(i2c)) {
466 /* we need to go to the next i2c message */
468 dev_dbg(i2c->dev, "WRITE: Next Message\n");
474 /* check to see if we need to do another message */
475 if (i2c->msg->flags & I2C_M_NOSTART) {
477 if (i2c->msg->flags & I2C_M_RD) {
479 * cannot do this, the controller
480 * forces us to send a new START
481 * when we change direction
483 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);
885 #if defined(CONFIG_ARM_S3C24XX_CPUFREQ)
887 #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
889 static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
890 unsigned long val, void *data)
892 struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
897 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
899 /* if we're post-change and the input clock has slowed down
900 * or at pre-change and the clock is about to speed up, then
901 * adjust our clock rate. <0 is slow, >0 speedup.
904 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
905 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
906 i2c_lock_bus(&i2c->adap, I2C_LOCK_ROOT_ADAPTER);
907 ret = s3c24xx_i2c_clockrate(i2c, &got);
908 i2c_unlock_bus(&i2c->adap, I2C_LOCK_ROOT_ADAPTER);
911 dev_err(i2c->dev, "cannot find frequency (%d)\n", ret);
913 dev_info(i2c->dev, "setting freq %d\n", got);
919 static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
921 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
923 return cpufreq_register_notifier(&i2c->freq_transition,
924 CPUFREQ_TRANSITION_NOTIFIER);
927 static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
929 cpufreq_unregister_notifier(&i2c->freq_transition,
930 CPUFREQ_TRANSITION_NOTIFIER);
934 static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
939 static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
945 static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
949 if (i2c->quirks & QUIRK_NO_GPIO)
952 for (i = 0; i < 2; i++) {
953 i2c->gpios[i] = devm_gpiod_get_index(i2c->dev, NULL,
955 if (IS_ERR(i2c->gpios[i])) {
956 dev_err(i2c->dev, "i2c gpio invalid at index %d\n", i);
964 static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
971 * initialise the controller, set the IO lines and frequency
973 static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
975 struct s3c2410_platform_i2c *pdata;
978 /* get the plafrom data */
982 /* write slave address */
984 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
986 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
988 writel(0, i2c->regs + S3C2410_IICCON);
989 writel(0, i2c->regs + S3C2410_IICSTAT);
991 /* we need to work out the divisors for the clock... */
993 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
994 dev_err(i2c->dev, "cannot meet bus frequency required\n");
998 /* todo - check that the i2c lines aren't being dragged anywhere */
1000 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
1001 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02x\n",
1002 readl(i2c->regs + S3C2410_IICCON));
1009 * Parse the device tree node and retreive the platform data.
1012 s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
1014 struct s3c2410_platform_i2c *pdata = i2c->pdata;
1020 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
1021 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
1022 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
1023 of_property_read_u32(np, "samsung,i2c-max-bus-freq",
1024 (u32 *)&pdata->frequency);
1026 * Exynos5's legacy i2c controller and new high speed i2c
1027 * controller have muxed interrupt sources. By default the
1028 * interrupts for 4-channel HS-I2C controller are enabled.
1029 * If nodes for first four channels of legacy i2c controller
1030 * are available then re-configure the interrupts via the
1033 id = of_alias_get_id(np, "i2c");
1034 i2c->sysreg = syscon_regmap_lookup_by_phandle(np,
1035 "samsung,sysreg-phandle");
1036 if (IS_ERR(i2c->sysreg))
1039 regmap_update_bits(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, BIT(id), 0);
1043 s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) { }
1046 static int s3c24xx_i2c_probe(struct platform_device *pdev)
1048 struct s3c24xx_i2c *i2c;
1049 struct s3c2410_platform_i2c *pdata = NULL;
1050 struct resource *res;
1053 if (!pdev->dev.of_node) {
1054 pdata = dev_get_platdata(&pdev->dev);
1056 dev_err(&pdev->dev, "no platform data\n");
1061 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
1065 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1069 i2c->quirks = s3c24xx_get_device_quirks(pdev);
1070 i2c->sysreg = ERR_PTR(-ENOENT);
1072 memcpy(i2c->pdata, pdata, sizeof(*pdata));
1074 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
1076 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
1077 i2c->adap.owner = THIS_MODULE;
1078 i2c->adap.algo = &s3c24xx_i2c_algorithm;
1079 i2c->adap.retries = 2;
1080 i2c->adap.class = I2C_CLASS_DEPRECATED;
1083 init_waitqueue_head(&i2c->wait);
1085 /* find the clock and enable it */
1086 i2c->dev = &pdev->dev;
1087 i2c->clk = devm_clk_get(&pdev->dev, "i2c");
1088 if (IS_ERR(i2c->clk)) {
1089 dev_err(&pdev->dev, "cannot get clock\n");
1093 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
1095 /* map the registers */
1096 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1097 i2c->regs = devm_ioremap_resource(&pdev->dev, res);
1099 if (IS_ERR(i2c->regs))
1100 return PTR_ERR(i2c->regs);
1102 dev_dbg(&pdev->dev, "registers %p (%p)\n",
1105 /* setup info block for the i2c core */
1106 i2c->adap.algo_data = i2c;
1107 i2c->adap.dev.parent = &pdev->dev;
1108 i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
1110 /* inititalise the i2c gpio lines */
1111 if (i2c->pdata->cfg_gpio)
1112 i2c->pdata->cfg_gpio(to_platform_device(i2c->dev));
1113 else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c))
1116 /* initialise the i2c controller */
1117 ret = clk_prepare_enable(i2c->clk);
1119 dev_err(&pdev->dev, "I2C clock enable failed\n");
1123 ret = s3c24xx_i2c_init(i2c);
1124 clk_disable(i2c->clk);
1126 dev_err(&pdev->dev, "I2C controller init failed\n");
1127 clk_unprepare(i2c->clk);
1132 * find the IRQ for this unit (note, this relies on the init call to
1133 * ensure no current IRQs pending
1135 if (!(i2c->quirks & QUIRK_POLL)) {
1136 i2c->irq = ret = platform_get_irq(pdev, 0);
1138 dev_err(&pdev->dev, "cannot find IRQ\n");
1139 clk_unprepare(i2c->clk);
1143 ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq,
1144 0, dev_name(&pdev->dev), i2c);
1146 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
1147 clk_unprepare(i2c->clk);
1152 ret = s3c24xx_i2c_register_cpufreq(i2c);
1154 dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
1155 clk_unprepare(i2c->clk);
1160 * Note, previous versions of the driver used i2c_add_adapter()
1161 * to add the bus at any number. We now pass the bus number via
1162 * the platform data, so if unset it will now default to always
1165 i2c->adap.nr = i2c->pdata->bus_num;
1166 i2c->adap.dev.of_node = pdev->dev.of_node;
1168 platform_set_drvdata(pdev, i2c);
1170 pm_runtime_enable(&pdev->dev);
1172 ret = i2c_add_numbered_adapter(&i2c->adap);
1174 pm_runtime_disable(&pdev->dev);
1175 s3c24xx_i2c_deregister_cpufreq(i2c);
1176 clk_unprepare(i2c->clk);
1180 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
1184 static int s3c24xx_i2c_remove(struct platform_device *pdev)
1186 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1188 clk_unprepare(i2c->clk);
1190 pm_runtime_disable(&pdev->dev);
1192 s3c24xx_i2c_deregister_cpufreq(i2c);
1194 i2c_del_adapter(&i2c->adap);
1199 #ifdef CONFIG_PM_SLEEP
1200 static int s3c24xx_i2c_suspend_noirq(struct device *dev)
1202 struct s3c24xx_i2c *i2c = dev_get_drvdata(dev);
1204 i2c_mark_adapter_suspended(&i2c->adap);
1206 if (!IS_ERR(i2c->sysreg))
1207 regmap_read(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, &i2c->sys_i2c_cfg);
1212 static int s3c24xx_i2c_resume_noirq(struct device *dev)
1214 struct s3c24xx_i2c *i2c = dev_get_drvdata(dev);
1217 if (!IS_ERR(i2c->sysreg))
1218 regmap_write(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, i2c->sys_i2c_cfg);
1220 ret = clk_enable(i2c->clk);
1223 s3c24xx_i2c_init(i2c);
1224 clk_disable(i2c->clk);
1225 i2c_mark_adapter_resumed(&i2c->adap);
1232 static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
1233 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(s3c24xx_i2c_suspend_noirq,
1234 s3c24xx_i2c_resume_noirq)
1237 #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
1239 #define S3C24XX_DEV_PM_OPS NULL
1242 static struct platform_driver s3c24xx_i2c_driver = {
1243 .probe = s3c24xx_i2c_probe,
1244 .remove = s3c24xx_i2c_remove,
1245 .id_table = s3c24xx_driver_ids,
1248 .pm = S3C24XX_DEV_PM_OPS,
1249 .of_match_table = of_match_ptr(s3c24xx_i2c_match),
1253 static int __init i2c_adap_s3c_init(void)
1255 return platform_driver_register(&s3c24xx_i2c_driver);
1257 subsys_initcall(i2c_adap_s3c_init);
1259 static void __exit i2c_adap_s3c_exit(void)
1261 platform_driver_unregister(&s3c24xx_i2c_driver);
1263 module_exit(i2c_adap_s3c_exit);
1265 MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1267 MODULE_LICENSE("GPL");