Lines Matching refs:i2c
88 static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_is2440() argument
90 struct platform_device *pdev = to_platform_device(i2c->dev); in s3c24xx_i2c_is2440()
101 static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret) in s3c24xx_i2c_master_complete() argument
103 dev_dbg(i2c->dev, "master_complete %d\n", ret); in s3c24xx_i2c_master_complete()
105 i2c->msg_ptr = 0; in s3c24xx_i2c_master_complete()
106 i2c->msg = NULL; in s3c24xx_i2c_master_complete()
107 i2c->msg_idx++; in s3c24xx_i2c_master_complete()
108 i2c->msg_num = 0; in s3c24xx_i2c_master_complete()
110 i2c->msg_idx = ret; in s3c24xx_i2c_master_complete()
112 wake_up(&i2c->wait); in s3c24xx_i2c_master_complete()
115 static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_disable_ack() argument
119 tmp = readl(i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_disable_ack()
120 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_disable_ack()
123 static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_enable_ack() argument
127 tmp = readl(i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_enable_ack()
128 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_enable_ack()
133 static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_disable_irq() argument
137 tmp = readl(i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_disable_irq()
138 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_disable_irq()
141 static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_enable_irq() argument
145 tmp = readl(i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_enable_irq()
146 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_enable_irq()
155 static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, in s3c24xx_i2c_message_start() argument
175 s3c24xx_i2c_enable_ack(i2c); in s3c24xx_i2c_message_start()
177 iiccon = readl(i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_message_start()
178 writel(stat, i2c->regs + S3C2410_IICSTAT); in s3c24xx_i2c_message_start()
180 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); in s3c24xx_i2c_message_start()
181 writeb(addr, i2c->regs + S3C2410_IICDS); in s3c24xx_i2c_message_start()
186 ndelay(i2c->tx_setup); in s3c24xx_i2c_message_start()
188 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); in s3c24xx_i2c_message_start()
189 writel(iiccon, i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_message_start()
192 writel(stat, i2c->regs + S3C2410_IICSTAT); in s3c24xx_i2c_message_start()
195 static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret) in s3c24xx_i2c_stop() argument
197 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT); in s3c24xx_i2c_stop()
199 dev_dbg(i2c->dev, "STOP\n"); in s3c24xx_i2c_stop()
203 writel(iicstat, i2c->regs + S3C2410_IICSTAT); in s3c24xx_i2c_stop()
205 i2c->state = STATE_STOP; in s3c24xx_i2c_stop()
207 s3c24xx_i2c_master_complete(i2c, ret); in s3c24xx_i2c_stop()
208 s3c24xx_i2c_disable_irq(i2c); in s3c24xx_i2c_stop()
219 static inline int is_lastmsg(struct s3c24xx_i2c *i2c) in is_lastmsg() argument
221 return i2c->msg_idx >= (i2c->msg_num - 1); in is_lastmsg()
229 static inline int is_msglast(struct s3c24xx_i2c *i2c) in is_msglast() argument
231 return i2c->msg_ptr == i2c->msg->len-1; in is_msglast()
239 static inline int is_msgend(struct s3c24xx_i2c *i2c) in is_msgend() argument
241 return i2c->msg_ptr >= i2c->msg->len; in is_msgend()
249 static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) in i2s_s3c_irq_nextbyte() argument
255 switch (i2c->state) { in i2s_s3c_irq_nextbyte()
258 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__); in i2s_s3c_irq_nextbyte()
263 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); in i2s_s3c_irq_nextbyte()
264 s3c24xx_i2c_disable_irq(i2c); in i2s_s3c_irq_nextbyte()
273 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { in i2s_s3c_irq_nextbyte()
276 dev_dbg(i2c->dev, "ack was not received\n"); in i2s_s3c_irq_nextbyte()
277 s3c24xx_i2c_stop(i2c, -ENXIO); in i2s_s3c_irq_nextbyte()
281 if (i2c->msg->flags & I2C_M_RD) in i2s_s3c_irq_nextbyte()
282 i2c->state = STATE_READ; in i2s_s3c_irq_nextbyte()
284 i2c->state = STATE_WRITE; in i2s_s3c_irq_nextbyte()
289 if (is_lastmsg(i2c) && i2c->msg->len == 0) { in i2s_s3c_irq_nextbyte()
290 s3c24xx_i2c_stop(i2c, 0); in i2s_s3c_irq_nextbyte()
294 if (i2c->state == STATE_READ) in i2s_s3c_irq_nextbyte()
305 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { in i2s_s3c_irq_nextbyte()
307 dev_dbg(i2c->dev, "WRITE: No Ack\n"); in i2s_s3c_irq_nextbyte()
309 s3c24xx_i2c_stop(i2c, -ECONNREFUSED); in i2s_s3c_irq_nextbyte()
316 if (!is_msgend(i2c)) { in i2s_s3c_irq_nextbyte()
317 byte = i2c->msg->buf[i2c->msg_ptr++]; in i2s_s3c_irq_nextbyte()
318 writeb(byte, i2c->regs + S3C2410_IICDS); in i2s_s3c_irq_nextbyte()
326 ndelay(i2c->tx_setup); in i2s_s3c_irq_nextbyte()
328 } else if (!is_lastmsg(i2c)) { in i2s_s3c_irq_nextbyte()
331 dev_dbg(i2c->dev, "WRITE: Next Message\n"); in i2s_s3c_irq_nextbyte()
333 i2c->msg_ptr = 0; in i2s_s3c_irq_nextbyte()
334 i2c->msg_idx++; in i2s_s3c_irq_nextbyte()
335 i2c->msg++; in i2s_s3c_irq_nextbyte()
338 if (i2c->msg->flags & I2C_M_NOSTART) { in i2s_s3c_irq_nextbyte()
340 if (i2c->msg->flags & I2C_M_RD) { in i2s_s3c_irq_nextbyte()
345 s3c24xx_i2c_stop(i2c, -EINVAL); in i2s_s3c_irq_nextbyte()
351 s3c24xx_i2c_message_start(i2c, i2c->msg); in i2s_s3c_irq_nextbyte()
352 i2c->state = STATE_START; in i2s_s3c_irq_nextbyte()
358 s3c24xx_i2c_stop(i2c, 0); in i2s_s3c_irq_nextbyte()
368 byte = readb(i2c->regs + S3C2410_IICDS); in i2s_s3c_irq_nextbyte()
369 i2c->msg->buf[i2c->msg_ptr++] = byte; in i2s_s3c_irq_nextbyte()
372 if (is_msglast(i2c)) { in i2s_s3c_irq_nextbyte()
375 if (is_lastmsg(i2c)) in i2s_s3c_irq_nextbyte()
376 s3c24xx_i2c_disable_ack(i2c); in i2s_s3c_irq_nextbyte()
378 } else if (is_msgend(i2c)) { in i2s_s3c_irq_nextbyte()
382 if (is_lastmsg(i2c)) { in i2s_s3c_irq_nextbyte()
384 dev_dbg(i2c->dev, "READ: Send Stop\n"); in i2s_s3c_irq_nextbyte()
386 s3c24xx_i2c_stop(i2c, 0); in i2s_s3c_irq_nextbyte()
389 dev_dbg(i2c->dev, "READ: Next Transfer\n"); in i2s_s3c_irq_nextbyte()
391 i2c->msg_ptr = 0; in i2s_s3c_irq_nextbyte()
392 i2c->msg_idx++; in i2s_s3c_irq_nextbyte()
393 i2c->msg++; in i2s_s3c_irq_nextbyte()
403 tmp = readl(i2c->regs + S3C2410_IICCON); in i2s_s3c_irq_nextbyte()
405 writel(tmp, i2c->regs + S3C2410_IICCON); in i2s_s3c_irq_nextbyte()
417 struct s3c24xx_i2c *i2c = dev_id; in s3c24xx_i2c_irq() local
421 status = readl(i2c->regs + S3C2410_IICSTAT); in s3c24xx_i2c_irq()
425 dev_err(i2c->dev, "deal with arbitration loss\n"); in s3c24xx_i2c_irq()
428 if (i2c->state == STATE_IDLE) { in s3c24xx_i2c_irq()
429 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); in s3c24xx_i2c_irq()
431 tmp = readl(i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_irq()
433 writel(tmp, i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_irq()
440 i2s_s3c_irq_nextbyte(i2c, status); in s3c24xx_i2c_irq()
452 static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_set_master() argument
458 iicstat = readl(i2c->regs + S3C2410_IICSTAT); in s3c24xx_i2c_set_master()
474 static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, in s3c24xx_i2c_doxfer() argument
480 if (i2c->suspended) in s3c24xx_i2c_doxfer()
483 ret = s3c24xx_i2c_set_master(i2c); in s3c24xx_i2c_doxfer()
485 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); in s3c24xx_i2c_doxfer()
490 spin_lock_irq(&i2c->lock); in s3c24xx_i2c_doxfer()
492 i2c->msg = msgs; in s3c24xx_i2c_doxfer()
493 i2c->msg_num = num; in s3c24xx_i2c_doxfer()
494 i2c->msg_ptr = 0; in s3c24xx_i2c_doxfer()
495 i2c->msg_idx = 0; in s3c24xx_i2c_doxfer()
496 i2c->state = STATE_START; in s3c24xx_i2c_doxfer()
498 s3c24xx_i2c_enable_irq(i2c); in s3c24xx_i2c_doxfer()
499 s3c24xx_i2c_message_start(i2c, msgs); in s3c24xx_i2c_doxfer()
500 spin_unlock_irq(&i2c->lock); in s3c24xx_i2c_doxfer()
502 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); in s3c24xx_i2c_doxfer()
504 ret = i2c->msg_idx; in s3c24xx_i2c_doxfer()
510 dev_dbg(i2c->dev, "timeout\n"); in s3c24xx_i2c_doxfer()
512 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); in s3c24xx_i2c_doxfer()
531 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; in s3c24xx_i2c_xfer() local
537 ret = s3c24xx_i2c_doxfer(i2c, msgs, num); in s3c24xx_i2c_xfer()
542 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); in s3c24xx_i2c_xfer()
612 static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) in s3c24xx_i2c_clockrate() argument
614 struct s3c2410_platform_i2c *pdata = i2c->dev->platform_data; in s3c24xx_i2c_clockrate()
615 unsigned long clkin = clk_get_rate(i2c->clk); in s3c24xx_i2c_clockrate()
621 i2c->clkrate = clkin; in s3c24xx_i2c_clockrate()
624 dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n", in s3c24xx_i2c_clockrate()
657 iiccon = readl(i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_clockrate()
664 writel(iiccon, i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_clockrate()
676 struct s3c24xx_i2c *i2c = freq_to_i2c(nb); in s3c24xx_i2c_cpufreq_transition() local
682 delta_f = clk_get_rate(i2c->clk) - i2c->clkrate; in s3c24xx_i2c_cpufreq_transition()
691 spin_lock_irqsave(&i2c->lock, flags); in s3c24xx_i2c_cpufreq_transition()
692 ret = s3c24xx_i2c_clockrate(i2c, &got); in s3c24xx_i2c_cpufreq_transition()
693 spin_unlock_irqrestore(&i2c->lock, flags); in s3c24xx_i2c_cpufreq_transition()
696 dev_err(i2c->dev, "cannot find frequency\n"); in s3c24xx_i2c_cpufreq_transition()
698 dev_info(i2c->dev, "setting freq %d\n", got); in s3c24xx_i2c_cpufreq_transition()
704 static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_register_cpufreq() argument
706 i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition; in s3c24xx_i2c_register_cpufreq()
708 return cpufreq_register_notifier(&i2c->freq_transition, in s3c24xx_i2c_register_cpufreq()
712 static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_deregister_cpufreq() argument
714 cpufreq_unregister_notifier(&i2c->freq_transition, in s3c24xx_i2c_deregister_cpufreq()
719 static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_register_cpufreq() argument
724 static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_deregister_cpufreq() argument
734 static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) in s3c24xx_i2c_init() argument
742 pdata = i2c->dev->platform_data; in s3c24xx_i2c_init()
747 pdata->cfg_gpio(to_platform_device(i2c->dev)); in s3c24xx_i2c_init()
751 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); in s3c24xx_i2c_init()
753 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); in s3c24xx_i2c_init()
755 writel(iicon, i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_init()
759 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { in s3c24xx_i2c_init()
760 writel(0, i2c->regs + S3C2410_IICCON); in s3c24xx_i2c_init()
761 dev_err(i2c->dev, "cannot meet bus frequency required\n"); in s3c24xx_i2c_init()
767 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); in s3c24xx_i2c_init()
768 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon); in s3c24xx_i2c_init()
772 if (s3c24xx_i2c_is2440(i2c)) { in s3c24xx_i2c_init()
773 dev_dbg(i2c->dev, "S3C2440_IICLC=%08x\n", pdata->sda_delay); in s3c24xx_i2c_init()
775 writel(pdata->sda_delay, i2c->regs + S3C2440_IICLC); in s3c24xx_i2c_init()
788 struct s3c24xx_i2c *i2c; in s3c24xx_i2c_probe() local
799 i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL); in s3c24xx_i2c_probe()
800 if (!i2c) { in s3c24xx_i2c_probe()
805 strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); in s3c24xx_i2c_probe()
806 i2c->adap.owner = THIS_MODULE; in s3c24xx_i2c_probe()
807 i2c->adap.algo = &s3c24xx_i2c_algorithm; in s3c24xx_i2c_probe()
808 i2c->adap.retries = 2; in s3c24xx_i2c_probe()
809 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; in s3c24xx_i2c_probe()
810 i2c->tx_setup = 50; in s3c24xx_i2c_probe()
812 spin_lock_init(&i2c->lock); in s3c24xx_i2c_probe()
813 init_waitqueue_head(&i2c->wait); in s3c24xx_i2c_probe()
817 i2c->dev = &pdev->dev; in s3c24xx_i2c_probe()
818 i2c->clk = clk_get(&pdev->dev, "i2c"); in s3c24xx_i2c_probe()
819 if (IS_ERR(i2c->clk)) { in s3c24xx_i2c_probe()
825 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk); in s3c24xx_i2c_probe()
827 clk_enable(i2c->clk); in s3c24xx_i2c_probe()
838 i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1, in s3c24xx_i2c_probe()
841 if (i2c->ioarea == NULL) { in s3c24xx_i2c_probe()
847 i2c->regs = ioremap(res->start, (res->end-res->start)+1); in s3c24xx_i2c_probe()
849 if (i2c->regs == NULL) { in s3c24xx_i2c_probe()
856 i2c->regs, i2c->ioarea, res); in s3c24xx_i2c_probe()
860 i2c->adap.algo_data = i2c; in s3c24xx_i2c_probe()
861 i2c->adap.dev.parent = &pdev->dev; in s3c24xx_i2c_probe()
865 ret = s3c24xx_i2c_init(i2c); in s3c24xx_i2c_probe()
873 i2c->irq = ret = platform_get_irq(pdev, 0); in s3c24xx_i2c_probe()
879 ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED, in s3c24xx_i2c_probe()
880 dev_name(&pdev->dev), i2c); in s3c24xx_i2c_probe()
883 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); in s3c24xx_i2c_probe()
887 ret = s3c24xx_i2c_register_cpufreq(i2c); in s3c24xx_i2c_probe()
899 i2c->adap.nr = pdata->bus_num; in s3c24xx_i2c_probe()
901 ret = i2c_add_numbered_adapter(&i2c->adap); in s3c24xx_i2c_probe()
907 platform_set_drvdata(pdev, i2c); in s3c24xx_i2c_probe()
909 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev)); in s3c24xx_i2c_probe()
913 s3c24xx_i2c_deregister_cpufreq(i2c); in s3c24xx_i2c_probe()
916 free_irq(i2c->irq, i2c); in s3c24xx_i2c_probe()
919 iounmap(i2c->regs); in s3c24xx_i2c_probe()
922 release_resource(i2c->ioarea); in s3c24xx_i2c_probe()
923 kfree(i2c->ioarea); in s3c24xx_i2c_probe()
926 clk_disable(i2c->clk); in s3c24xx_i2c_probe()
927 clk_put(i2c->clk); in s3c24xx_i2c_probe()
930 kfree(i2c); in s3c24xx_i2c_probe()
941 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); in s3c24xx_i2c_remove() local
943 s3c24xx_i2c_deregister_cpufreq(i2c); in s3c24xx_i2c_remove()
945 i2c_del_adapter(&i2c->adap); in s3c24xx_i2c_remove()
946 free_irq(i2c->irq, i2c); in s3c24xx_i2c_remove()
948 clk_disable(i2c->clk); in s3c24xx_i2c_remove()
949 clk_put(i2c->clk); in s3c24xx_i2c_remove()
951 iounmap(i2c->regs); in s3c24xx_i2c_remove()
953 release_resource(i2c->ioarea); in s3c24xx_i2c_remove()
954 kfree(i2c->ioarea); in s3c24xx_i2c_remove()
955 kfree(i2c); in s3c24xx_i2c_remove()
964 struct s3c24xx_i2c *i2c = platform_get_drvdata(dev); in s3c24xx_i2c_suspend_late() local
965 i2c->suspended = 1; in s3c24xx_i2c_suspend_late()
971 struct s3c24xx_i2c *i2c = platform_get_drvdata(dev); in s3c24xx_i2c_resume() local
973 i2c->suspended = 0; in s3c24xx_i2c_resume()
974 s3c24xx_i2c_init(i2c); in s3c24xx_i2c_resume()