23 #include <linux/kernel.h>
24 #include <linux/module.h>
26 #include <linux/i2c.h>
28 #include <linux/time.h>
31 #include <linux/errno.h>
37 #include <linux/slab.h>
48 #define QUIRK_S3C2440 (1 << 0)
49 #define QUIRK_HDMIPHY (1 << 1)
50 #define QUIRK_NO_GPIO (1 << 2)
86 #ifdef CONFIG_CPU_FREQ
93 .name =
"s3c2410-i2c",
96 .name =
"s3c2440-i2c",
99 .name =
"s3c2440-hdmiphy-i2c",
106 static const struct of_device_id s3c24xx_i2c_match[] = {
107 { .
compatible =
"samsung,s3c2410-i2c", .data = (
void *)0 },
108 { .compatible =
"samsung,s3c2440-i2c", .data = (
void *)
QUIRK_S3C2440 },
109 { .compatible =
"samsung,s3c2440-hdmiphy-i2c",
121 static inline unsigned int s3c24xx_get_device_quirks(
struct platform_device *pdev)
123 if (pdev->
dev.of_node) {
126 return (
unsigned int)match->
data;
138 static inline void s3c24xx_i2c_master_complete(
struct s3c24xx_i2c *i2c,
int ret)
140 dev_dbg(i2c->
dev,
"master_complete %d\n", ret);
152 static inline void s3c24xx_i2c_disable_ack(
struct s3c24xx_i2c *i2c)
160 static inline void s3c24xx_i2c_enable_ack(
struct s3c24xx_i2c *i2c)
170 static inline void s3c24xx_i2c_disable_irq(
struct s3c24xx_i2c *i2c)
178 static inline void s3c24xx_i2c_enable_irq(
struct s3c24xx_i2c *i2c)
192 static void s3c24xx_i2c_message_start(
struct s3c24xx_i2c *i2c,
195 unsigned int addr = (msg->
addr & 0x7f) << 1;
197 unsigned long iiccon;
212 s3c24xx_i2c_enable_ack(i2c);
217 dev_dbg(i2c->
dev,
"START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
232 static inline void s3c24xx_i2c_stop(
struct s3c24xx_i2c *i2c,
int ret)
244 s3c24xx_i2c_master_complete(i2c, ret);
245 s3c24xx_i2c_disable_irq(i2c);
256 static inline int is_lastmsg(
struct s3c24xx_i2c *i2c)
266 static inline int is_msglast(
struct s3c24xx_i2c *i2c)
276 static inline int is_msgend(
struct s3c24xx_i2c *i2c)
286 static int i2c_s3c_irq_nextbyte(
struct s3c24xx_i2c *i2c,
unsigned long iicstat)
292 switch (i2c->
state) {
295 dev_err(i2c->
dev,
"%s: called in STATE_IDLE\n", __func__);
299 dev_err(i2c->
dev,
"%s: called in STATE_STOP\n", __func__);
300 s3c24xx_i2c_disable_irq(i2c);
313 s3c24xx_i2c_stop(i2c, -
ENXIO);
325 if (is_lastmsg(i2c) && i2c->
msg->len == 0) {
326 s3c24xx_i2c_stop(i2c, 0);
352 if (!is_msgend(i2c)) {
364 }
else if (!is_lastmsg(i2c)) {
381 s3c24xx_i2c_stop(i2c, -
EINVAL);
387 s3c24xx_i2c_message_start(i2c, i2c->
msg);
394 s3c24xx_i2c_stop(i2c, 0);
408 if (is_msglast(i2c)) {
412 s3c24xx_i2c_disable_ack(i2c);
414 }
else if (is_msgend(i2c)) {
418 if (is_lastmsg(i2c)) {
422 s3c24xx_i2c_stop(i2c, 0);
461 dev_err(i2c->
dev,
"deal with arbitration loss\n");
465 dev_dbg(i2c->
dev,
"IRQ: error i2c->state == IDLE\n");
476 i2c_s3c_irq_nextbyte(i2c, status);
488 static int s3c24xx_i2c_set_master(
struct s3c24xx_i2c *i2c)
490 unsigned long iicstat;
500 while (timeout-- > 0) {
526 static int s3c24xx_i2c_doxfer(
struct s3c24xx_i2c *i2c,
529 unsigned long iicstat, timeout;
536 ret = s3c24xx_i2c_set_master(i2c);
538 dev_err(i2c->
dev,
"cannot get bus (error %d)\n", ret);
543 spin_lock_irq(&i2c->
lock);
551 s3c24xx_i2c_enable_irq(i2c);
552 s3c24xx_i2c_message_start(i2c, msgs);
553 spin_unlock_irq(&i2c->
lock);
565 dev_dbg(i2c->
dev,
"incomplete xfer (%d)\n", ret);
583 if (iicstat & S3C2410_IICSTAT_START)
584 dev_warn(i2c->
dev,
"timeout waiting for bus idle\n");
603 pm_runtime_get_sync(&adap->
dev);
604 clk_prepare_enable(i2c->
clk);
608 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
611 clk_disable_unprepare(i2c->
clk);
612 pm_runtime_put(&adap->
dev);
621 clk_disable_unprepare(i2c->
clk);
622 pm_runtime_put(&adap->
dev);
636 .master_xfer = s3c24xx_i2c_xfer,
637 .functionality = s3c24xx_i2c_func,
645 static int s3c24xx_i2c_calcdivisor(
unsigned long clkin,
unsigned int wanted,
646 unsigned int *div1,
unsigned int *divs)
648 unsigned int calc_divs = clkin /
wanted;
649 unsigned int calc_div1;
651 if (calc_divs > (16*16))
656 calc_divs += calc_div1-1;
657 calc_divs /= calc_div1;
667 return clkin / (calc_divs * calc_div1);
677 static int s3c24xx_i2c_clockrate(
struct s3c24xx_i2c *i2c,
unsigned int *got)
681 unsigned int divs, div1;
682 unsigned long target_frequency;
693 target_frequency /= 1000;
695 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
697 if (freq > target_frequency) {
699 "Unable to achieve desired frequency %luKHz." \
700 " Lowest achievable %dKHz\n", target_frequency, freq);
728 dev_dbg(i2c->
dev,
"IICLC=%08lx\n", sda_delay);
735 #ifdef CONFIG_CPU_FREQ
737 #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
739 static int s3c24xx_i2c_cpufreq_transition(
struct notifier_block *nb,
758 ret = s3c24xx_i2c_clockrate(i2c, &got);
759 spin_unlock_irqrestore(&i2c->
lock, flags);
770 static inline int s3c24xx_i2c_register_cpufreq(
struct s3c24xx_i2c *i2c)
772 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
778 static inline void s3c24xx_i2c_deregister_cpufreq(
struct s3c24xx_i2c *i2c)
785 static inline int s3c24xx_i2c_register_cpufreq(
struct s3c24xx_i2c *i2c)
790 static inline void s3c24xx_i2c_deregister_cpufreq(
struct s3c24xx_i2c *i2c)
796 static int s3c24xx_i2c_parse_dt_gpio(
struct s3c24xx_i2c *i2c)
803 for (idx = 0; idx < 2; idx++) {
804 gpio = of_get_gpio(i2c->
dev->of_node, idx);
805 if (!gpio_is_valid(gpio)) {
806 dev_err(i2c->
dev,
"invalid gpio[%d]: %d\n", idx, gpio);
813 dev_err(i2c->
dev,
"gpio [%d] request failed\n", gpio);
825 static void s3c24xx_i2c_dt_gpio_free(
struct s3c24xx_i2c *i2c)
832 for (idx = 0; idx < 2; idx++)
836 static int s3c24xx_i2c_parse_dt_gpio(
struct s3c24xx_i2c *i2c)
841 static void s3c24xx_i2c_dt_gpio_free(
struct s3c24xx_i2c *i2c)
851 static int s3c24xx_i2c_init(
struct s3c24xx_i2c *i2c)
866 if (s3c24xx_i2c_parse_dt_gpio(i2c))
879 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
881 dev_err(i2c->
dev,
"cannot meet bus frequency required\n");
887 dev_info(i2c->
dev,
"bus frequency set to %d KHz\n", freq);
888 dev_dbg(i2c->
dev,
"S3C2410_IICCON=0x%02lx\n", iicon);
908 of_property_read_u32(np,
"samsung,i2c-sda-delay", &pdata->
sda_delay);
909 of_property_read_u32(np,
"samsung,i2c-slave-addr", &pdata->
slave_addr);
910 of_property_read_u32(np,
"samsung,i2c-max-bus-freq",
933 if (!pdev->
dev.of_node) {
934 pdata = pdev->
dev.platform_data;
953 i2c->
quirks = s3c24xx_get_device_quirks(pdev);
957 s3c24xx_i2c_parse_dt(pdev->
dev.of_node, i2c);
961 i2c->
adap.algo = &s3c24xx_i2c_algorithm;
962 i2c->
adap.retries = 2;
973 if (IS_ERR(i2c->
clk)) {
981 clk_prepare_enable(i2c->
clk);
987 dev_err(&pdev->
dev,
"cannot find IO resource\n");
1009 dev_dbg(&pdev->
dev,
"registers %p (%p, %p)\n",
1014 i2c->
adap.algo_data = i2c;
1015 i2c->
adap.dev.parent = &pdev->
dev;
1019 ret = s3c24xx_i2c_init(i2c);
1034 dev_name(&pdev->
dev), i2c);
1041 ret = s3c24xx_i2c_register_cpufreq(i2c);
1043 dev_err(&pdev->
dev,
"failed to register cpufreq notifier\n");
1054 i2c->
adap.dev.of_node = pdev->
dev.of_node;
1058 dev_err(&pdev->
dev,
"failed to add bus to i2c core\n");
1063 platform_set_drvdata(pdev, i2c);
1068 dev_info(&pdev->
dev,
"%s: S3C I2C adapter\n", dev_name(&i2c->
adap.dev));
1069 clk_disable_unprepare(i2c->
clk);
1073 s3c24xx_i2c_deregister_cpufreq(i2c);
1086 clk_disable_unprepare(i2c->
clk);
1100 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1102 pm_runtime_disable(&i2c->
adap.dev);
1103 pm_runtime_disable(&pdev->
dev);
1105 s3c24xx_i2c_deregister_cpufreq(i2c);
1110 clk_disable_unprepare(i2c->
clk);
1116 s3c24xx_i2c_dt_gpio_free(i2c);
1123 static int s3c24xx_i2c_suspend_noirq(
struct device *
dev)
1126 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1133 static int s3c24xx_i2c_resume(
struct device *
dev)
1136 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
1139 clk_prepare_enable(i2c->
clk);
1140 s3c24xx_i2c_init(i2c);
1141 clk_disable_unprepare(i2c->
clk);
1146 static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
1148 .resume = s3c24xx_i2c_resume,
1151 #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
1153 #define S3C24XX_DEV_PM_OPS NULL
1159 .probe = s3c24xx_i2c_probe,
1160 .remove = s3c24xx_i2c_remove,
1161 .id_table = s3c24xx_driver_ids,
1170 static int __init i2c_adap_s3c_init(
void)
1176 static void __exit i2c_adap_s3c_exit(
void)