9 #include <linux/module.h>
12 #include <linux/i2c.h>
19 #include <linux/slab.h>
25 #define I2C_CR (0x00000000)
26 #define I2C_CR_RESET_VALUE (0x00)
27 #define I2C_CR_RESET_UMASK (0x00)
28 #define I2C_CR_DDC1_ENABLE (0x80)
29 #define I2C_CR_TRANS_ENABLE (0x40)
30 #define I2C_CR_PERIPHERAL_ENABLE (0x20)
31 #define I2C_CR_DDC2B_ENABLE (0x10)
32 #define I2C_CR_START_ENABLE (0x08)
33 #define I2C_CR_ACK_ENABLE (0x04)
34 #define I2C_CR_STOP_ENABLE (0x02)
35 #define I2C_CR_INTERRUPT_ENABLE (0x01)
37 #define I2C_SR1 (0x00000004)
38 #define I2C_SR1_RESET_VALUE (0x00)
39 #define I2C_SR1_RESET_UMASK (0x00)
40 #define I2C_SR1_EVF_IND (0x80)
41 #define I2C_SR1_ADD10_IND (0x40)
42 #define I2C_SR1_TRA_IND (0x20)
43 #define I2C_SR1_BUSY_IND (0x10)
44 #define I2C_SR1_BTF_IND (0x08)
45 #define I2C_SR1_ADSL_IND (0x04)
46 #define I2C_SR1_MSL_IND (0x02)
47 #define I2C_SR1_SB_IND (0x01)
49 #define I2C_SR2 (0x00000008)
50 #define I2C_SR2_RESET_VALUE (0x00)
51 #define I2C_SR2_RESET_UMASK (0x40)
52 #define I2C_SR2_MASK (0xBF)
53 #define I2C_SR2_SCLFAL_IND (0x80)
54 #define I2C_SR2_ENDAD_IND (0x20)
55 #define I2C_SR2_AF_IND (0x10)
56 #define I2C_SR2_STOPF_IND (0x08)
57 #define I2C_SR2_ARLO_IND (0x04)
58 #define I2C_SR2_BERR_IND (0x02)
59 #define I2C_SR2_DDC2BF_IND (0x01)
61 #define I2C_CCR (0x0000000C)
62 #define I2C_CCR_RESET_VALUE (0x00)
63 #define I2C_CCR_RESET_UMASK (0x00)
64 #define I2C_CCR_MASK (0xFF)
65 #define I2C_CCR_FMSM (0x80)
66 #define I2C_CCR_CC_MASK (0x7F)
68 #define I2C_OAR1 (0x00000010)
69 #define I2C_OAR1_RESET_VALUE (0x00)
70 #define I2C_OAR1_RESET_UMASK (0x00)
71 #define I2C_OAR1_ADD_MASK (0xFF)
73 #define I2C_OAR2 (0x00000014)
74 #define I2C_OAR2_RESET_VALUE (0x40)
75 #define I2C_OAR2_RESET_UMASK (0x19)
76 #define I2C_OAR2_MASK (0xE6)
77 #define I2C_OAR2_FR_25_10MHZ (0x00)
78 #define I2C_OAR2_FR_10_1667MHZ (0x20)
79 #define I2C_OAR2_FR_1667_2667MHZ (0x40)
80 #define I2C_OAR2_FR_2667_40MHZ (0x60)
81 #define I2C_OAR2_FR_40_5333MHZ (0x80)
82 #define I2C_OAR2_FR_5333_66MHZ (0xA0)
83 #define I2C_OAR2_FR_66_80MHZ (0xC0)
84 #define I2C_OAR2_FR_80_100MHZ (0xE0)
85 #define I2C_OAR2_FR_MASK (0xE0)
86 #define I2C_OAR2_ADD_MASK (0x06)
88 #define I2C_DR (0x00000018)
89 #define I2C_DR_RESET_VALUE (0x00)
90 #define I2C_DR_RESET_UMASK (0xFF)
91 #define I2C_DR_D_MASK (0xFF)
93 #define I2C_ECCR (0x0000001C)
94 #define I2C_ECCR_RESET_VALUE (0x00)
95 #define I2C_ECCR_RESET_UMASK (0xE0)
96 #define I2C_ECCR_MASK (0x1F)
97 #define I2C_ECCR_CC_MASK (0x1F)
126 #define STU300_TIMEOUT (msecs_to_jiffies(1000))
132 #define NUM_ADDR_RESEND_ATTEMPTS 12
135 static unsigned int scl_frequency = 100000;
177 writel((value << 16) | value, address);
185 static inline u32 stu300_r8(
void __iomem *address)
187 return readl(address) & 0x000000FF
U;
238 stu300_irq_disable(dev);
294 "Unhandled interrupt! %d sr1: 0x%x sr2: 0x%x\n",
295 mr_event, status1, status2);
310 res = stu300_event_occurred(dev, dev->
cmd_event);
325 static int stu300_start_and_await_event(
struct stu300_dev *dev,
333 WARN(1,
"irqs are disabled, cannot poll for event\n");
351 "wait_for_completion_interruptible_timeout() "
352 "returned %d waiting for event %04x\n", ret, mr_event);
358 "waiting for event %d, reinit hardware\n", mr_event);
359 (
void) stu300_init_hw(dev);
365 "error %d waiting for event %d, reinit hardware\n",
367 (
void) stu300_init_hw(dev);
378 static int stu300_await_event(
struct stu300_dev *dev,
385 dev_err(&dev->
pdev->dev,
"irqs are disabled on this "
398 stu300_irq_enable(dev);
407 "wait_for_completion_interruptible_timeout()"
408 "returned %d waiting for event %04x\n", ret, mr_event);
415 "timed out waiting for event %d, reinit "
416 "hardware\n", mr_event);
417 (
void) stu300_init_hw(dev);
425 "error (await_event) %d waiting for event %d, "
426 "reinit hardware\n", dev->
cmd_err, mr_event);
427 (
void) stu300_init_hw(dev);
438 #define BUSY_RELEASE_ATTEMPTS 10
439 static int stu300_wait_while_busy(
struct stu300_dev *dev)
456 "waiting for device to be free (not busy). "
457 "Attempt: %d\n", i+1);
462 (
void) stu300_init_hw(dev);
465 dev_err(&dev->
pdev->dev,
"giving up after %d attempts "
466 "to reset the bus.\n", BUSY_RELEASE_ATTEMPTS);
486 { 100000000, 0xFF
U },
490 static int stu300_set_clk(
struct stu300_dev *dev,
unsigned long clkrate)
498 stu300_clktable[i].
rate < clkrate)
501 if (stu300_clktable[i].
setting == 0xFFU) {
502 dev_err(&dev->
pdev->dev,
"too %s clock rate requested "
503 "(%lu Hz).\n", i ?
"high" :
"low", clkrate);
507 stu300_wr8(stu300_clktable[i].
setting,
510 dev_dbg(&dev->
pdev->dev,
"Clock rate %lu Hz, I2C bus speed %d Hz "
513 if (dev->
speed > 100000)
515 val = ((clkrate/dev->
speed) - 9)/3 + 1;
518 val = ((clkrate/dev->
speed) - 7)/2 + 1;
522 dev_err(&dev->
pdev->dev,
"too low clock rate (%lu Hz).\n",
528 if (val & 0xFFFFF000U) {
529 dev_err(&dev->
pdev->dev,
"too high clock rate (%lu Hz).\n",
534 if (dev->
speed > 100000) {
538 dev_dbg(&dev->
pdev->dev,
"set clock divider to 0x%08x, "
539 "Fast Mode I2C\n", val);
545 "0x%08x, Standard Mode I2C\n", val);
549 stu300_wr8(((val >> 7) & 0x1F),
556 static int stu300_init_hw(
struct stu300_dev *dev)
559 unsigned long clkrate;
577 ret = stu300_set_clk(dev, clkrate);
599 static int stu300_send_address(
struct stu300_dev *dev,
607 val = (0xf0 | (((
u32) msg->
addr & 0x300) >> 7)) &
653 static int stu300_xfer_msg(
struct i2c_adapter *adap,
661 struct stu300_dev *dev = i2c_get_adapdata(adap);
667 dev_dbg(&dev->
pdev->dev,
"I2C message to: 0x%04x, len: %d, "
668 "flags: 0x%04x, stop: %d\n",
688 ret = stu300_wait_while_busy(dev);
698 ret = stu300_init_hw(dev);
714 ret = stu300_start_and_await_event(dev, cr,
723 ret = stu300_send_address(dev, msg, attempts != 0);
727 dev_dbg(&dev->
pdev->dev,
"failed sending address, "
728 "retrying. Attempt: %d msg_index: %d/%d\n",
734 if (attempts < NUM_ADDR_RESEND_ATTEMPTS && attempts > 0) {
736 "through after %d attempts\n", attempts);
738 dev_dbg(&dev->
pdev->dev,
"I give up, tried %d times "
739 "to resend address.\n",
747 for (i = 0; i < msg->
len; i++) {
748 if (i == msg->
len-1) {
770 for (i = 0; i < msg->
len; i++) {
772 stu300_wr8(msg->
buf[i],
779 "event 8 (%d)\n", ret);
788 "send returned NAK!\n");
802 ret = stu300_wait_while_busy(dev);
804 dev_err(&dev->
pdev->dev,
"timout waiting for transfer "
827 struct stu300_dev *dev = i2c_get_adapdata(adap);
830 for (i = 0; i < num; i++) {
840 ret = stu300_xfer_msg(adap, &msgs[i], (i == (num - 1)));
858 .master_xfer = stu300_xfer,
859 .functionality = stu300_func,
870 char clk_name[] =
"I2C0";
874 dev_err(&pdev->
dev,
"could not allocate device struct\n");
879 clk_name[3] += (
char)bus_nr;
881 if (IS_ERR(dev->
clk)) {
882 dev_err(&pdev->
dev,
"could not retrieve i2c bus clock\n");
883 return PTR_ERR(dev->
clk);
892 dev_dbg(&pdev->
dev,
"initialize bus device I2C%d on virtual "
893 "base %p\n", bus_nr, dev->
virtbase);
898 ret = devm_request_irq(&pdev->
dev, dev->
irq, stu300_irh, 0,
NAME, dev);
902 dev->
speed = scl_frequency;
904 clk_prepare_enable(dev->
clk);
905 ret = stu300_init_hw(dev);
908 dev_err(&dev->
pdev->dev,
"error initializing hardware.\n");
921 strlcpy(adap->
name,
"ST Microelectronics DDC I2C adapter",
924 adap->
algo = &stu300_algo;
925 adap->
dev.parent = &pdev->
dev;
926 i2c_set_adapdata(adap, dev);
931 dev_err(&pdev->
dev,
"failure adding ST Micro DDC "
936 platform_set_drvdata(pdev, dev);
956 ret = stu300_init_hw(dev);
960 dev_err(device,
"error re-initializing hardware.\n");
965 #define STU300_I2C_PM (&stu300_pm)
967 #define STU300_I2C_PM NULL
973 struct stu300_dev *dev = platform_get_drvdata(pdev);
978 platform_set_drvdata(pdev,
NULL);
992 static int __init stu300_init(
void)
997 static void __exit stu300_exit(
void)