2 * Copyright (C) 2007-2012 ST-Ericsson AB
3 * License terms: GNU General Public License (GPL) version 2
4 * ST DDC I2C master mode driver, used in e.g. U300 series platforms.
5 * Author: Linus Walleij <linus.walleij@stericsson.com>
6 * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <linux/spinlock.h>
14 #include <linux/completion.h>
15 #include <linux/err.h>
16 #include <linux/interrupt.h>
17 #include <linux/clk.h>
19 #include <linux/slab.h>
20 #include <linux/of_i2c.h>
22 /* the name of this kernel module */
25 /* CR (Control Register) 8bit (R/W) */
26 #define I2C_CR (0x00000000)
27 #define I2C_CR_RESET_VALUE (0x00)
28 #define I2C_CR_RESET_UMASK (0x00)
29 #define I2C_CR_DDC1_ENABLE (0x80)
30 #define I2C_CR_TRANS_ENABLE (0x40)
31 #define I2C_CR_PERIPHERAL_ENABLE (0x20)
32 #define I2C_CR_DDC2B_ENABLE (0x10)
33 #define I2C_CR_START_ENABLE (0x08)
34 #define I2C_CR_ACK_ENABLE (0x04)
35 #define I2C_CR_STOP_ENABLE (0x02)
36 #define I2C_CR_INTERRUPT_ENABLE (0x01)
37 /* SR1 (Status Register 1) 8bit (R/-) */
38 #define I2C_SR1 (0x00000004)
39 #define I2C_SR1_RESET_VALUE (0x00)
40 #define I2C_SR1_RESET_UMASK (0x00)
41 #define I2C_SR1_EVF_IND (0x80)
42 #define I2C_SR1_ADD10_IND (0x40)
43 #define I2C_SR1_TRA_IND (0x20)
44 #define I2C_SR1_BUSY_IND (0x10)
45 #define I2C_SR1_BTF_IND (0x08)
46 #define I2C_SR1_ADSL_IND (0x04)
47 #define I2C_SR1_MSL_IND (0x02)
48 #define I2C_SR1_SB_IND (0x01)
49 /* SR2 (Status Register 2) 8bit (R/-) */
50 #define I2C_SR2 (0x00000008)
51 #define I2C_SR2_RESET_VALUE (0x00)
52 #define I2C_SR2_RESET_UMASK (0x40)
53 #define I2C_SR2_MASK (0xBF)
54 #define I2C_SR2_SCLFAL_IND (0x80)
55 #define I2C_SR2_ENDAD_IND (0x20)
56 #define I2C_SR2_AF_IND (0x10)
57 #define I2C_SR2_STOPF_IND (0x08)
58 #define I2C_SR2_ARLO_IND (0x04)
59 #define I2C_SR2_BERR_IND (0x02)
60 #define I2C_SR2_DDC2BF_IND (0x01)
61 /* CCR (Clock Control Register) 8bit (R/W) */
62 #define I2C_CCR (0x0000000C)
63 #define I2C_CCR_RESET_VALUE (0x00)
64 #define I2C_CCR_RESET_UMASK (0x00)
65 #define I2C_CCR_MASK (0xFF)
66 #define I2C_CCR_FMSM (0x80)
67 #define I2C_CCR_CC_MASK (0x7F)
68 /* OAR1 (Own Address Register 1) 8bit (R/W) */
69 #define I2C_OAR1 (0x00000010)
70 #define I2C_OAR1_RESET_VALUE (0x00)
71 #define I2C_OAR1_RESET_UMASK (0x00)
72 #define I2C_OAR1_ADD_MASK (0xFF)
73 /* OAR2 (Own Address Register 2) 8bit (R/W) */
74 #define I2C_OAR2 (0x00000014)
75 #define I2C_OAR2_RESET_VALUE (0x40)
76 #define I2C_OAR2_RESET_UMASK (0x19)
77 #define I2C_OAR2_MASK (0xE6)
78 #define I2C_OAR2_FR_25_10MHZ (0x00)
79 #define I2C_OAR2_FR_10_1667MHZ (0x20)
80 #define I2C_OAR2_FR_1667_2667MHZ (0x40)
81 #define I2C_OAR2_FR_2667_40MHZ (0x60)
82 #define I2C_OAR2_FR_40_5333MHZ (0x80)
83 #define I2C_OAR2_FR_5333_66MHZ (0xA0)
84 #define I2C_OAR2_FR_66_80MHZ (0xC0)
85 #define I2C_OAR2_FR_80_100MHZ (0xE0)
86 #define I2C_OAR2_FR_MASK (0xE0)
87 #define I2C_OAR2_ADD_MASK (0x06)
88 /* DR (Data Register) 8bit (R/W) */
89 #define I2C_DR (0x00000018)
90 #define I2C_DR_RESET_VALUE (0x00)
91 #define I2C_DR_RESET_UMASK (0xFF)
92 #define I2C_DR_D_MASK (0xFF)
93 /* ECCR (Extended Clock Control Register) 8bit (R/W) */
94 #define I2C_ECCR (0x0000001C)
95 #define I2C_ECCR_RESET_VALUE (0x00)
96 #define I2C_ECCR_RESET_UMASK (0xE0)
97 #define I2C_ECCR_MASK (0x1F)
98 #define I2C_ECCR_CC_MASK (0x1F)
101 * These events are more or less responses to commands
102 * sent into the hardware, presumably reflecting the state
103 * of an internal state machine.
106 STU300_EVENT_NONE = 0,
119 STU300_ERROR_NONE = 0,
120 STU300_ERROR_ACKNOWLEDGE_FAILURE,
121 STU300_ERROR_BUS_ERROR,
122 STU300_ERROR_ARBITRATION_LOST,
126 /* timeout waiting for the controller to respond */
127 #define STU300_TIMEOUT (msecs_to_jiffies(1000))
130 * The number of address send athemps tried before giving up.
131 * If the first one failes it seems like 5 to 8 attempts are required.
133 #define NUM_ADDR_RESEND_ATTEMPTS 12
135 /* I2C clock speed, in Hz 0-400kHz*/
136 static unsigned int scl_frequency = 100000;
137 module_param(scl_frequency, uint, 0644);
140 * struct stu300_dev - the stu300 driver state holder
141 * @pdev: parent platform device
142 * @adapter: corresponding I2C adapter
143 * @clk: hardware block clock
144 * @irq: assigned interrupt line
145 * @cmd_issue_lock: this locks the following cmd_ variables
146 * @cmd_complete: acknowledge completion for an I2C command
147 * @cmd_event: expected event coming in as a response to a command
148 * @cmd_err: error code as response to a command
149 * @speed: current bus speed in Hz
150 * @msg_index: index of current message
151 * @msg_len: length of current message
155 struct platform_device *pdev;
156 struct i2c_adapter adapter;
157 void __iomem *virtbase;
160 spinlock_t cmd_issue_lock;
161 struct completion cmd_complete;
162 enum stu300_event cmd_event;
163 enum stu300_error cmd_err;
169 /* Local forward function declarations */
170 static int stu300_init_hw(struct stu300_dev *dev);
173 * The block needs writes in both MSW and LSW in order
174 * for all data lines to reach their destination.
176 static inline void stu300_wr8(u32 value, void __iomem *address)
178 writel((value << 16) | value, address);
182 * This merely masks off the duplicates which appear
183 * in bytes 1-3. You _MUST_ use 32-bit bus access on this
184 * device, else it will not work.
186 static inline u32 stu300_r8(void __iomem *address)
188 return readl(address) & 0x000000FFU;
191 static void stu300_irq_enable(struct stu300_dev *dev)
194 val = stu300_r8(dev->virtbase + I2C_CR);
195 val |= I2C_CR_INTERRUPT_ENABLE;
196 /* Twice paranoia (possible HW glitch) */
197 stu300_wr8(val, dev->virtbase + I2C_CR);
198 stu300_wr8(val, dev->virtbase + I2C_CR);
201 static void stu300_irq_disable(struct stu300_dev *dev)
204 val = stu300_r8(dev->virtbase + I2C_CR);
205 val &= ~I2C_CR_INTERRUPT_ENABLE;
206 /* Twice paranoia (possible HW glitch) */
207 stu300_wr8(val, dev->virtbase + I2C_CR);
208 stu300_wr8(val, dev->virtbase + I2C_CR);
213 * Tells whether a certain event or events occurred in
214 * response to a command. The events represent states in
215 * the internal state machine of the hardware. The events
216 * are not very well described in the hardware
217 * documentation and can only be treated as abstract state
220 * @ret 0 = event has not occurred or unknown error, any
221 * other value means the correct event occurred or an error.
224 static int stu300_event_occurred(struct stu300_dev *dev,
225 enum stu300_event mr_event) {
229 /* What event happened? */
230 status1 = stu300_r8(dev->virtbase + I2C_SR1);
232 if (!(status1 & I2C_SR1_EVF_IND))
233 /* No event at all */
236 status2 = stu300_r8(dev->virtbase + I2C_SR2);
238 /* Block any multiple interrupts */
239 stu300_irq_disable(dev);
241 /* Check for errors first */
242 if (status2 & I2C_SR2_AF_IND) {
243 dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE;
245 } else if (status2 & I2C_SR2_BERR_IND) {
246 dev->cmd_err = STU300_ERROR_BUS_ERROR;
248 } else if (status2 & I2C_SR2_ARLO_IND) {
249 dev->cmd_err = STU300_ERROR_ARBITRATION_LOST;
255 if (status1 & I2C_SR1_ADSL_IND)
262 if (status1 & I2C_SR1_BTF_IND) {
267 if (status2 & I2C_SR2_STOPF_IND)
271 if (status1 & I2C_SR1_SB_IND)
272 /* Clear start bit */
276 if (status2 & I2C_SR2_ENDAD_IND) {
277 /* First check for any errors */
282 if (status1 & I2C_SR1_ADD10_IND)
288 /* If we get here, we're on thin ice.
289 * Here we are in a status where we have
290 * gotten a response that does not match
293 dev->cmd_err = STU300_ERROR_UNKNOWN;
294 dev_err(&dev->pdev->dev,
295 "Unhandled interrupt! %d sr1: 0x%x sr2: 0x%x\n",
296 mr_event, status1, status2);
300 static irqreturn_t stu300_irh(int irq, void *data)
302 struct stu300_dev *dev = data;
305 /* Just make sure that the block is clocked */
306 clk_enable(dev->clk);
308 /* See if this was what we were waiting for */
309 spin_lock(&dev->cmd_issue_lock);
311 res = stu300_event_occurred(dev, dev->cmd_event);
312 if (res || dev->cmd_err != STU300_ERROR_NONE)
313 complete(&dev->cmd_complete);
315 spin_unlock(&dev->cmd_issue_lock);
317 clk_disable(dev->clk);
323 * Sends a command and then waits for the bits masked by *flagmask*
324 * to go high or low by IRQ awaiting.
326 static int stu300_start_and_await_event(struct stu300_dev *dev,
328 enum stu300_event mr_event)
332 if (unlikely(irqs_disabled())) {
333 /* TODO: implement polling for this case if need be. */
334 WARN(1, "irqs are disabled, cannot poll for event\n");
338 /* Lock command issue, fill in an event we wait for */
339 spin_lock_irq(&dev->cmd_issue_lock);
340 init_completion(&dev->cmd_complete);
341 dev->cmd_err = STU300_ERROR_NONE;
342 dev->cmd_event = mr_event;
343 spin_unlock_irq(&dev->cmd_issue_lock);
345 /* Turn on interrupt, send command and wait. */
346 cr_value |= I2C_CR_INTERRUPT_ENABLE;
347 stu300_wr8(cr_value, dev->virtbase + I2C_CR);
348 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
351 dev_err(&dev->pdev->dev,
352 "wait_for_completion_interruptible_timeout() "
353 "returned %d waiting for event %04x\n", ret, mr_event);
358 dev_err(&dev->pdev->dev, "controller timed out "
359 "waiting for event %d, reinit hardware\n", mr_event);
360 (void) stu300_init_hw(dev);
364 if (dev->cmd_err != STU300_ERROR_NONE) {
365 dev_err(&dev->pdev->dev, "controller (start) "
366 "error %d waiting for event %d, reinit hardware\n",
367 dev->cmd_err, mr_event);
368 (void) stu300_init_hw(dev);
376 * This waits for a flag to be set, if it is not set on entry, an interrupt is
377 * configured to wait for the flag using a completion.
379 static int stu300_await_event(struct stu300_dev *dev,
380 enum stu300_event mr_event)
384 if (unlikely(irqs_disabled())) {
385 /* TODO: implement polling for this case if need be. */
386 dev_err(&dev->pdev->dev, "irqs are disabled on this "
391 /* Is it already here? */
392 spin_lock_irq(&dev->cmd_issue_lock);
393 dev->cmd_err = STU300_ERROR_NONE;
394 dev->cmd_event = mr_event;
396 init_completion(&dev->cmd_complete);
398 /* Turn on the I2C interrupt for current operation */
399 stu300_irq_enable(dev);
401 /* Unlock the command block and wait for the event to occur */
402 spin_unlock_irq(&dev->cmd_issue_lock);
404 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
407 dev_err(&dev->pdev->dev,
408 "wait_for_completion_interruptible_timeout()"
409 "returned %d waiting for event %04x\n", ret, mr_event);
414 if (mr_event != STU300_EVENT_6) {
415 dev_err(&dev->pdev->dev, "controller "
416 "timed out waiting for event %d, reinit "
417 "hardware\n", mr_event);
418 (void) stu300_init_hw(dev);
423 if (dev->cmd_err != STU300_ERROR_NONE) {
424 if (mr_event != STU300_EVENT_6) {
425 dev_err(&dev->pdev->dev, "controller "
426 "error (await_event) %d waiting for event %d, "
427 "reinit hardware\n", dev->cmd_err, mr_event);
428 (void) stu300_init_hw(dev);
437 * Waits for the busy bit to go low by repeated polling.
439 #define BUSY_RELEASE_ATTEMPTS 10
440 static int stu300_wait_while_busy(struct stu300_dev *dev)
442 unsigned long timeout;
445 for (i = 0; i < BUSY_RELEASE_ATTEMPTS; i++) {
446 timeout = jiffies + STU300_TIMEOUT;
448 while (!time_after(jiffies, timeout)) {
450 if ((stu300_r8(dev->virtbase + I2C_SR1) &
451 I2C_SR1_BUSY_IND) == 0)
456 dev_err(&dev->pdev->dev, "transaction timed out "
457 "waiting for device to be free (not busy). "
458 "Attempt: %d\n", i+1);
460 dev_err(&dev->pdev->dev, "base address = "
461 "0x%08x, reinit hardware\n", (u32) dev->virtbase);
463 (void) stu300_init_hw(dev);
466 dev_err(&dev->pdev->dev, "giving up after %d attempts "
467 "to reset the bus.\n", BUSY_RELEASE_ATTEMPTS);
472 struct stu300_clkset {
477 static const struct stu300_clkset stu300_clktable[] = {
479 { 2500000, I2C_OAR2_FR_25_10MHZ },
480 { 10000000, I2C_OAR2_FR_10_1667MHZ },
481 { 16670000, I2C_OAR2_FR_1667_2667MHZ },
482 { 26670000, I2C_OAR2_FR_2667_40MHZ },
483 { 40000000, I2C_OAR2_FR_40_5333MHZ },
484 { 53330000, I2C_OAR2_FR_5333_66MHZ },
485 { 66000000, I2C_OAR2_FR_66_80MHZ },
486 { 80000000, I2C_OAR2_FR_80_100MHZ },
487 { 100000000, 0xFFU },
491 static int stu300_set_clk(struct stu300_dev *dev, unsigned long clkrate)
497 /* Locate the appropriate clock setting */
498 while (i < ARRAY_SIZE(stu300_clktable) - 1 &&
499 stu300_clktable[i].rate < clkrate)
502 if (stu300_clktable[i].setting == 0xFFU) {
503 dev_err(&dev->pdev->dev, "too %s clock rate requested "
504 "(%lu Hz).\n", i ? "high" : "low", clkrate);
508 stu300_wr8(stu300_clktable[i].setting,
509 dev->virtbase + I2C_OAR2);
511 dev_dbg(&dev->pdev->dev, "Clock rate %lu Hz, I2C bus speed %d Hz "
512 "virtbase %p\n", clkrate, dev->speed, dev->virtbase);
514 if (dev->speed > 100000)
516 val = ((clkrate/dev->speed) - 9)/3 + 1;
518 /* Standard Mode I2C */
519 val = ((clkrate/dev->speed) - 7)/2 + 1;
521 /* According to spec the divider must be > 2 */
523 dev_err(&dev->pdev->dev, "too low clock rate (%lu Hz).\n",
528 /* We have 12 bits clock divider only! */
529 if (val & 0xFFFFF000U) {
530 dev_err(&dev->pdev->dev, "too high clock rate (%lu Hz).\n",
535 if (dev->speed > 100000) {
537 stu300_wr8((val & I2C_CCR_CC_MASK) | I2C_CCR_FMSM,
538 dev->virtbase + I2C_CCR);
539 dev_dbg(&dev->pdev->dev, "set clock divider to 0x%08x, "
540 "Fast Mode I2C\n", val);
543 stu300_wr8((val & I2C_CCR_CC_MASK),
544 dev->virtbase + I2C_CCR);
545 dev_dbg(&dev->pdev->dev, "set clock divider to "
546 "0x%08x, Standard Mode I2C\n", val);
550 stu300_wr8(((val >> 7) & 0x1F),
551 dev->virtbase + I2C_ECCR);
557 static int stu300_init_hw(struct stu300_dev *dev)
560 unsigned long clkrate;
563 /* Disable controller */
564 stu300_wr8(0x00, dev->virtbase + I2C_CR);
566 * Set own address to some default value (0x00).
567 * We do not support slave mode anyway.
569 stu300_wr8(0x00, dev->virtbase + I2C_OAR1);
571 * The I2C controller only operates properly in 26 MHz but we
572 * program this driver as if we didn't know. This will also set the two
573 * high bits of the own address to zero as well.
574 * There is no known hardware issue with running in 13 MHz
575 * However, speeds over 200 kHz are not used.
577 clkrate = clk_get_rate(dev->clk);
578 ret = stu300_set_clk(dev, clkrate);
583 * Enable block, do it TWICE (hardware glitch)
584 * Setting bit 7 can enable DDC mode. (Not used currently.)
586 stu300_wr8(I2C_CR_PERIPHERAL_ENABLE,
587 dev->virtbase + I2C_CR);
588 stu300_wr8(I2C_CR_PERIPHERAL_ENABLE,
589 dev->virtbase + I2C_CR);
590 /* Make a dummy read of the status register SR1 & SR2 */
591 dummy = stu300_r8(dev->virtbase + I2C_SR2);
592 dummy = stu300_r8(dev->virtbase + I2C_SR1);
599 /* Send slave address. */
600 static int stu300_send_address(struct stu300_dev *dev,
601 struct i2c_msg *msg, int resend)
606 if (msg->flags & I2C_M_TEN)
607 /* This is probably how 10 bit addresses look */
608 val = (0xf0 | (((u32) msg->addr & 0x300) >> 7)) &
611 val = ((msg->addr << 1) & I2C_DR_D_MASK);
613 if (msg->flags & I2C_M_RD) {
614 /* This is the direction bit */
617 dev_dbg(&dev->pdev->dev, "read resend\n");
619 dev_dbg(&dev->pdev->dev, "write resend\n");
620 stu300_wr8(val, dev->virtbase + I2C_DR);
622 /* For 10bit addressing, await 10bit request (EVENT 9) */
623 if (msg->flags & I2C_M_TEN) {
624 ret = stu300_await_event(dev, STU300_EVENT_9);
626 * The slave device wants a 10bit address, send the rest
627 * of the bits (the LSBits)
629 val = msg->addr & I2C_DR_D_MASK;
630 /* This clears "event 9" */
631 stu300_wr8(val, dev->virtbase + I2C_DR);
635 /* FIXME: Why no else here? two events for 10bit?
636 * Await event 6 (normal) or event 9 (10bit)
640 dev_dbg(&dev->pdev->dev, "await event 6\n");
641 ret = stu300_await_event(dev, STU300_EVENT_6);
644 * Clear any pending EVENT 6 no matter what happened during
647 val = stu300_r8(dev->virtbase + I2C_CR);
648 val |= I2C_CR_PERIPHERAL_ENABLE;
649 stu300_wr8(val, dev->virtbase + I2C_CR);
654 static int stu300_xfer_msg(struct i2c_adapter *adap,
655 struct i2c_msg *msg, int stop)
662 struct stu300_dev *dev = i2c_get_adapdata(adap);
664 clk_enable(dev->clk);
666 /* Remove this if (0) to trace each and every message. */
668 dev_dbg(&dev->pdev->dev, "I2C message to: 0x%04x, len: %d, "
669 "flags: 0x%04x, stop: %d\n",
670 msg->addr, msg->len, msg->flags, stop);
673 /* Zero-length messages are not supported by this hardware */
680 * For some reason, sending the address sometimes fails when running
681 * on the 13 MHz clock. No interrupt arrives. This is a work around,
682 * which tries to restart and send the address up to 10 times before
683 * really giving up. Usually 5 to 8 attempts are enough.
687 dev_dbg(&dev->pdev->dev, "wait while busy\n");
688 /* Check that the bus is free, or wait until some timeout */
689 ret = stu300_wait_while_busy(dev);
694 dev_dbg(&dev->pdev->dev, "re-int hw\n");
696 * According to ST, there is no problem if the clock is
697 * changed between 13 and 26 MHz during a transfer.
699 ret = stu300_init_hw(dev);
703 /* Send a start condition */
704 cr = I2C_CR_PERIPHERAL_ENABLE;
705 /* Setting the START bit puts the block in master mode */
706 if (!(msg->flags & I2C_M_NOSTART))
707 cr |= I2C_CR_START_ENABLE;
708 if ((msg->flags & I2C_M_RD) && (msg->len > 1))
709 /* On read more than 1 byte, we need ack. */
710 cr |= I2C_CR_ACK_ENABLE;
711 /* Check that it gets through */
712 if (!(msg->flags & I2C_M_NOSTART)) {
714 dev_dbg(&dev->pdev->dev, "send start event\n");
715 ret = stu300_start_and_await_event(dev, cr,
720 dev_dbg(&dev->pdev->dev, "send address\n");
724 ret = stu300_send_address(dev, msg, attempts != 0);
728 dev_dbg(&dev->pdev->dev, "failed sending address, "
729 "retrying. Attempt: %d msg_index: %d/%d\n",
730 attempts, dev->msg_index, dev->msg_len);
733 } while (ret != 0 && attempts < NUM_ADDR_RESEND_ATTEMPTS);
735 if (attempts < NUM_ADDR_RESEND_ATTEMPTS && attempts > 0) {
736 dev_dbg(&dev->pdev->dev, "managed to get address "
737 "through after %d attempts\n", attempts);
738 } else if (attempts == NUM_ADDR_RESEND_ATTEMPTS) {
739 dev_dbg(&dev->pdev->dev, "I give up, tried %d times "
740 "to resend address.\n",
741 NUM_ADDR_RESEND_ATTEMPTS);
746 if (msg->flags & I2C_M_RD) {
747 /* READ: we read the actual bytes one at a time */
748 for (i = 0; i < msg->len; i++) {
749 if (i == msg->len-1) {
751 * Disable ACK and set STOP condition before
754 val = I2C_CR_PERIPHERAL_ENABLE;
757 val |= I2C_CR_STOP_ENABLE;
760 dev->virtbase + I2C_CR);
762 /* Wait for this byte... */
763 ret = stu300_await_event(dev, STU300_EVENT_7);
766 /* This clears event 7 */
767 msg->buf[i] = (u8) stu300_r8(dev->virtbase + I2C_DR);
770 /* WRITE: we send the actual bytes one at a time */
771 for (i = 0; i < msg->len; i++) {
773 stu300_wr8(msg->buf[i],
774 dev->virtbase + I2C_DR);
776 ret = stu300_await_event(dev, STU300_EVENT_8);
777 /* Next write to DR will clear event 8 */
779 dev_err(&dev->pdev->dev, "error awaiting "
780 "event 8 (%d)\n", ret);
785 if (!(msg->flags & I2C_M_IGNORE_NAK)) {
786 if (stu300_r8(dev->virtbase + I2C_SR2) &
788 dev_err(&dev->pdev->dev, "I2C payload "
789 "send returned NAK!\n");
795 /* Send stop condition */
796 val = I2C_CR_PERIPHERAL_ENABLE;
797 val |= I2C_CR_STOP_ENABLE;
798 stu300_wr8(val, dev->virtbase + I2C_CR);
802 /* Check that the bus is free, or wait until some timeout occurs */
803 ret = stu300_wait_while_busy(dev);
805 dev_err(&dev->pdev->dev, "timout waiting for transfer "
810 /* Dummy read status registers */
811 val = stu300_r8(dev->virtbase + I2C_SR2);
812 val = stu300_r8(dev->virtbase + I2C_SR1);
816 /* Disable controller */
817 stu300_wr8(0x00, dev->virtbase + I2C_CR);
818 clk_disable(dev->clk);
822 static int stu300_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
828 struct stu300_dev *dev = i2c_get_adapdata(adap);
831 for (i = 0; i < num; i++) {
833 * Another driver appears to send stop for each message,
834 * here we only do that for the last message. Possibly some
835 * peripherals require this behaviour, then their drivers
836 * have to send single messages in order to get "stop" for
841 ret = stu300_xfer_msg(adap, &msgs[i], (i == (num - 1)));
852 static u32 stu300_func(struct i2c_adapter *adap)
854 /* This is the simplest thing you can think of... */
855 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
858 static const struct i2c_algorithm stu300_algo = {
859 .master_xfer = stu300_xfer,
860 .functionality = stu300_func,
864 stu300_probe(struct platform_device *pdev)
866 struct stu300_dev *dev;
867 struct i2c_adapter *adap;
868 struct resource *res;
872 dev = devm_kzalloc(&pdev->dev, sizeof(struct stu300_dev), GFP_KERNEL);
874 dev_err(&pdev->dev, "could not allocate device struct\n");
879 dev->clk = devm_clk_get(&pdev->dev, NULL);
880 if (IS_ERR(dev->clk)) {
881 dev_err(&pdev->dev, "could not retrieve i2c bus clock\n");
882 return PTR_ERR(dev->clk);
886 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
890 dev->virtbase = devm_ioremap_resource(&pdev->dev, res);
891 dev_dbg(&pdev->dev, "initialize bus device I2C%d on virtual "
892 "base %p\n", bus_nr, dev->virtbase);
893 if (IS_ERR(dev->virtbase))
894 return PTR_ERR(dev->virtbase);
896 dev->irq = platform_get_irq(pdev, 0);
897 ret = devm_request_irq(&pdev->dev, dev->irq, stu300_irh, 0, NAME, dev);
901 dev->speed = scl_frequency;
903 clk_prepare_enable(dev->clk);
904 ret = stu300_init_hw(dev);
905 clk_disable(dev->clk);
907 dev_err(&dev->pdev->dev, "error initializing hardware.\n");
911 /* IRQ event handling initialization */
912 spin_lock_init(&dev->cmd_issue_lock);
913 dev->cmd_event = STU300_EVENT_NONE;
914 dev->cmd_err = STU300_ERROR_NONE;
916 adap = &dev->adapter;
917 adap->owner = THIS_MODULE;
918 /* DDC class but actually often used for more generic I2C */
919 adap->class = I2C_CLASS_DDC;
920 strlcpy(adap->name, "ST Microelectronics DDC I2C adapter",
923 adap->algo = &stu300_algo;
924 adap->dev.parent = &pdev->dev;
925 adap->dev.of_node = pdev->dev.of_node;
926 i2c_set_adapdata(adap, dev);
928 /* i2c device drivers may be active on return from add_adapter() */
929 ret = i2c_add_numbered_adapter(adap);
931 dev_err(&pdev->dev, "failure adding ST Micro DDC "
936 platform_set_drvdata(pdev, dev);
937 dev_info(&pdev->dev, "ST DDC I2C @ %p, irq %d\n",
938 dev->virtbase, dev->irq);
939 of_i2c_register_devices(adap);
945 static int stu300_suspend(struct device *device)
947 struct stu300_dev *dev = dev_get_drvdata(device);
949 /* Turn off everything */
950 stu300_wr8(0x00, dev->virtbase + I2C_CR);
954 static int stu300_resume(struct device *device)
957 struct stu300_dev *dev = dev_get_drvdata(device);
959 clk_enable(dev->clk);
960 ret = stu300_init_hw(dev);
961 clk_disable(dev->clk);
964 dev_err(device, "error re-initializing hardware.\n");
968 static SIMPLE_DEV_PM_OPS(stu300_pm, stu300_suspend, stu300_resume);
969 #define STU300_I2C_PM (&stu300_pm)
971 #define STU300_I2C_PM NULL
975 stu300_remove(struct platform_device *pdev)
977 struct stu300_dev *dev = platform_get_drvdata(pdev);
979 i2c_del_adapter(&dev->adapter);
980 /* Turn off everything */
981 stu300_wr8(0x00, dev->virtbase + I2C_CR);
985 static const struct of_device_id stu300_dt_match[] = {
986 { .compatible = "st,ddci2c" },
990 static struct platform_driver stu300_i2c_driver = {
993 .owner = THIS_MODULE,
995 .of_match_table = stu300_dt_match,
997 .remove = __exit_p(stu300_remove),
1001 static int __init stu300_init(void)
1003 return platform_driver_probe(&stu300_i2c_driver, stu300_probe);
1006 static void __exit stu300_exit(void)
1008 platform_driver_unregister(&stu300_i2c_driver);
1012 * The systems using this bus often have very basic devices such
1013 * as regulators on the I2C bus, so this needs to be loaded early.
1014 * Therefore it is registered in the subsys_initcall().
1016 subsys_initcall(stu300_init);
1017 module_exit(stu300_exit);
1019 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
1020 MODULE_DESCRIPTION("ST Micro DDC I2C adapter (" NAME ")");
1021 MODULE_LICENSE("GPL");
1022 MODULE_ALIAS("platform:" NAME);