27 #include <linux/kernel.h>
28 #include <linux/module.h>
30 #include <linux/i2c.h>
32 #include <linux/errno.h>
33 #include <linux/sched.h>
38 #include <linux/slab.h>
44 #include <mach/hardware.h>
49 #define DAVINCI_I2C_TIMEOUT (1*HZ)
50 #define DAVINCI_I2C_MAX_TRIES 2
51 #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \
52 DAVINCI_I2C_IMR_SCD | \
53 DAVINCI_I2C_IMR_ARDY | \
54 DAVINCI_I2C_IMR_NACK | \
57 #define DAVINCI_I2C_OAR_REG 0x00
58 #define DAVINCI_I2C_IMR_REG 0x04
59 #define DAVINCI_I2C_STR_REG 0x08
60 #define DAVINCI_I2C_CLKL_REG 0x0c
61 #define DAVINCI_I2C_CLKH_REG 0x10
62 #define DAVINCI_I2C_CNT_REG 0x14
63 #define DAVINCI_I2C_DRR_REG 0x18
64 #define DAVINCI_I2C_SAR_REG 0x1c
65 #define DAVINCI_I2C_DXR_REG 0x20
66 #define DAVINCI_I2C_MDR_REG 0x24
67 #define DAVINCI_I2C_IVR_REG 0x28
68 #define DAVINCI_I2C_EMDR_REG 0x2c
69 #define DAVINCI_I2C_PSC_REG 0x30
71 #define DAVINCI_I2C_IVR_AAS 0x07
72 #define DAVINCI_I2C_IVR_SCD 0x06
73 #define DAVINCI_I2C_IVR_XRDY 0x05
74 #define DAVINCI_I2C_IVR_RDR 0x04
75 #define DAVINCI_I2C_IVR_ARDY 0x03
76 #define DAVINCI_I2C_IVR_NACK 0x02
77 #define DAVINCI_I2C_IVR_AL 0x01
79 #define DAVINCI_I2C_STR_BB BIT(12)
80 #define DAVINCI_I2C_STR_RSFULL BIT(11)
81 #define DAVINCI_I2C_STR_SCD BIT(5)
82 #define DAVINCI_I2C_STR_ARDY BIT(2)
83 #define DAVINCI_I2C_STR_NACK BIT(1)
84 #define DAVINCI_I2C_STR_AL BIT(0)
86 #define DAVINCI_I2C_MDR_NACK BIT(15)
87 #define DAVINCI_I2C_MDR_STT BIT(13)
88 #define DAVINCI_I2C_MDR_STP BIT(11)
89 #define DAVINCI_I2C_MDR_MST BIT(10)
90 #define DAVINCI_I2C_MDR_TRX BIT(9)
91 #define DAVINCI_I2C_MDR_XA BIT(8)
92 #define DAVINCI_I2C_MDR_RM BIT(7)
93 #define DAVINCI_I2C_MDR_IRS BIT(5)
95 #define DAVINCI_I2C_IMR_AAS BIT(6)
96 #define DAVINCI_I2C_IMR_SCD BIT(5)
97 #define DAVINCI_I2C_IMR_XRDY BIT(4)
98 #define DAVINCI_I2C_IMR_RRDY BIT(3)
99 #define DAVINCI_I2C_IMR_ARDY BIT(2)
100 #define DAVINCI_I2C_IMR_NACK BIT(1)
101 #define DAVINCI_I2C_IMR_AL BIT(0)
115 #ifdef CONFIG_CPU_FREQ
140 static void generic_i2c_clock_pulse(
unsigned int scl_pin)
146 for (i = 0; i < 9; i++) {
163 dev_err(dev->
dev,
"initiating i2c bus recovery\n");
169 generic_i2c_clock_pulse(pdata->
scl_pin);
217 psc = (input_clock / 7000000) - 1;
218 if ((input_clock / (psc + 1)) > 12000000)
220 d = (psc >= 2) ? 5 : 7 - psc;
222 clk = ((input_clock / (psc + 1)) / (pdata->
bus_freq * 1000)) - (d << 1);
230 dev_dbg(dev->
dev,
"input_clock = %d, CLK = %d\n", input_clock, clk);
243 davinci_i2c_reset_ctrl(dev, 0);
246 i2c_davinci_calc_clk_dividers(dev);
259 dev_dbg(dev->
dev,
"bus_freq = %dkHz, bus_delay = %d\n",
264 davinci_i2c_reset_ctrl(dev, 1);
287 "timeout waiting for bus ready\n");
292 i2c_recover_bus(dev);
293 i2c_davinci_init(dev);
374 if (stop && msg->
len != 0)
382 i2c_recover_bus(dev);
383 i2c_davinci_init(dev);
393 dev_err(dev->
dev,
"abnormal termination buf_len=%i\n",
410 i2c_davinci_init(dev);
437 dev_dbg(dev->
dev,
"%s: msgs: %d\n", __func__, num);
439 ret = i2c_davinci_wait_bus_not_busy(dev, 1);
441 dev_warn(dev->
dev,
"timeout waiting for bus ready\n");
445 for (i = 0; i < num; i++) {
446 ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
447 dev_dbg(dev->
dev,
"%s [%d/%d] ret: %d\n", __func__, i + 1, num,
453 #ifdef CONFIG_CPU_FREQ
474 dev_err(dev->
dev,
"RDR IRQ while no data requested\n");
483 dev_dbg(dev->
dev,
"TDR IRQ while no data to send\n");
498 dev_dbg(dev->
dev,
"%s: stat=0x%x\n", __func__, stat);
499 if (count++ == 100) {
519 davinci_i2c_write_reg(dev,
523 w = davinci_i2c_read_reg(dev,
526 davinci_i2c_write_reg(dev,
535 davinci_i2c_read_reg(dev,
541 davinci_i2c_write_reg(dev,
558 w = davinci_i2c_read_reg(dev,
561 davinci_i2c_write_reg(dev,
566 terminate_write(dev);
571 davinci_i2c_write_reg(dev,
577 dev_dbg(dev->
dev,
"Address as slave interrupt\n");
581 dev_warn(dev->
dev,
"Unrecognized irq stat %d\n", stat);
589 #ifdef CONFIG_CPU_FREQ
590 static int i2c_davinci_cpufreq_transition(
struct notifier_block *nb,
591 unsigned long val,
void *
data)
598 davinci_i2c_reset_ctrl(dev, 0);
600 i2c_davinci_calc_clk_dividers(dev);
601 davinci_i2c_reset_ctrl(dev, 1);
607 static inline int i2c_davinci_cpufreq_register(
struct davinci_i2c_dev *dev)
609 dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
615 static inline void i2c_davinci_cpufreq_deregister(
struct davinci_i2c_dev *dev)
621 static inline int i2c_davinci_cpufreq_register(
struct davinci_i2c_dev *dev)
626 static inline void i2c_davinci_cpufreq_deregister(
struct davinci_i2c_dev *dev)
632 .master_xfer = i2c_davinci_xfer,
633 .functionality = i2c_davinci_func,
636 static const struct of_device_id davinci_i2c_of_match[] = {
637 {.compatible =
"ti,davinci-i2c", },
665 dev_err(&pdev->
dev,
"I2C region already claimed\n");
672 goto err_release_region;
676 #ifdef CONFIG_CPU_FREQ
677 init_completion(&dev->xfr_complete);
681 dev->
pdata = dev->
dev->platform_data;
682 platform_set_drvdata(pdev, dev);
684 if (!dev->
pdata && pdev->
dev.of_node) {
693 memcpy(dev->
pdata, &davinci_i2c_platform_data_default,
695 if (!of_property_read_u32(pdev->
dev.of_node,
"clock-frequency",
697 dev->
pdata->bus_freq = prop / 1000;
698 }
else if (!dev->
pdata) {
699 dev->
pdata = &davinci_i2c_platform_data_default;
703 if (IS_ERR(dev->
clk)) {
707 clk_prepare_enable(dev->
clk);
712 goto err_mem_ioremap;
715 i2c_davinci_init(dev);
719 dev_err(&pdev->
dev,
"failure requesting irq %i\n", dev->
irq);
720 goto err_unuse_clocks;
723 r = i2c_davinci_cpufreq_register(dev);
725 dev_err(&pdev->
dev,
"failed to register cpufreq\n");
730 i2c_set_adapdata(adap, dev);
734 adap->
algo = &i2c_davinci_algo;
735 adap->
dev.parent = &pdev->
dev;
737 adap->
dev.of_node = pdev->
dev.of_node;
742 dev_err(&pdev->
dev,
"failure adding adapter\n");
754 clk_disable_unprepare(dev->
clk);
758 platform_set_drvdata(pdev,
NULL);
772 i2c_davinci_cpufreq_deregister(dev);
774 platform_set_drvdata(pdev,
NULL);
778 clk_disable_unprepare(dev->
clk);
793 static int davinci_i2c_suspend(
struct device *dev)
799 davinci_i2c_reset_ctrl(i2c_dev, 0);
800 clk_disable_unprepare(i2c_dev->
clk);
805 static int davinci_i2c_resume(
struct device *dev)
810 clk_prepare_enable(i2c_dev->
clk);
812 davinci_i2c_reset_ctrl(i2c_dev, 1);
817 static const struct dev_pm_ops davinci_i2c_pm = {
818 .
suspend = davinci_i2c_suspend,
819 .resume = davinci_i2c_resume,
822 #define davinci_i2c_pm_ops (&davinci_i2c_pm)
824 #define davinci_i2c_pm_ops NULL
831 .probe = davinci_i2c_probe,
832 .remove = davinci_i2c_remove,
834 .name =
"i2c_davinci",
842 static int __init davinci_i2c_init_driver(
void)
848 static void __exit davinci_i2c_exit_driver(
void)