1 /* linux/drivers/i2c/busses/i2c-s3c2410.c
3 * Copyright (C) 2004,2005 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C2410 I2C Controller
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/kernel.h>
24 #include <linux/module.h>
26 #include <linux/i2c.h>
27 #include <linux/i2c-id.h>
28 #include <linux/init.h>
29 #include <linux/time.h>
30 #include <linux/interrupt.h>
31 #include <linux/sched.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/err.h>
35 #include <linux/platform_device.h>
36 #include <linux/clk.h>
38 #include <asm/hardware.h>
42 #include <asm/arch/regs-gpio.h>
43 #include <asm/arch/regs-iic.h>
44 #include <asm/arch/iic.h>
46 /* i2c controller state */
48 enum s3c24xx_i2c_state
{
58 wait_queue_head_t wait
;
65 enum s3c24xx_i2c_state state
;
71 struct resource
*ioarea
;
72 struct i2c_adapter adap
;
75 /* default platform data to use if not supplied in the platform_device
78 static struct s3c2410_platform_i2c s3c24xx_i2c_default_platform
= {
83 .sda_delay
= S3C2410_IICLC_SDA_DELAY5
| S3C2410_IICLC_FILTER_ON
,
86 /* s3c24xx_i2c_is2440()
88 * return true is this is an s3c2440
91 static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c
*i2c
)
93 struct platform_device
*pdev
= to_platform_device(i2c
->dev
);
95 return !strcmp(pdev
->name
, "s3c2440-i2c");
99 /* s3c24xx_i2c_get_platformdata
101 * get the platform data associated with the given device, or return
102 * the default if there is none
105 static inline struct s3c2410_platform_i2c
*s3c24xx_i2c_get_platformdata(struct device
*dev
)
107 if (dev
->platform_data
!= NULL
)
108 return (struct s3c2410_platform_i2c
*)dev
->platform_data
;
110 return &s3c24xx_i2c_default_platform
;
113 /* s3c24xx_i2c_master_complete
115 * complete the message and wake up the caller, using the given return code,
116 * or zero to mean ok.
119 static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c
*i2c
, int ret
)
121 dev_dbg(i2c
->dev
, "master_complete %d\n", ret
);
133 static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c
*i2c
)
137 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
138 writel(tmp
& ~S3C2410_IICCON_ACKEN
, i2c
->regs
+ S3C2410_IICCON
);
142 static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c
*i2c
)
146 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
147 writel(tmp
| S3C2410_IICCON_ACKEN
, i2c
->regs
+ S3C2410_IICCON
);
151 /* irq enable/disable functions */
153 static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c
*i2c
)
157 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
158 writel(tmp
& ~S3C2410_IICCON_IRQEN
, i2c
->regs
+ S3C2410_IICCON
);
161 static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c
*i2c
)
165 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
166 writel(tmp
| S3C2410_IICCON_IRQEN
, i2c
->regs
+ S3C2410_IICCON
);
170 /* s3c24xx_i2c_message_start
172 * put the start of a message onto the bus
175 static void s3c24xx_i2c_message_start(struct s3c24xx_i2c
*i2c
,
178 unsigned int addr
= (msg
->addr
& 0x7f) << 1;
180 unsigned long iiccon
;
183 stat
|= S3C2410_IICSTAT_TXRXEN
;
185 if (msg
->flags
& I2C_M_RD
) {
186 stat
|= S3C2410_IICSTAT_MASTER_RX
;
189 stat
|= S3C2410_IICSTAT_MASTER_TX
;
191 if (msg
->flags
& I2C_M_REV_DIR_ADDR
)
194 // todo - check for wether ack wanted or not
195 s3c24xx_i2c_enable_ack(i2c
);
197 iiccon
= readl(i2c
->regs
+ S3C2410_IICCON
);
198 writel(stat
, i2c
->regs
+ S3C2410_IICSTAT
);
200 dev_dbg(i2c
->dev
, "START: %08lx to IICSTAT, %02x to DS\n", stat
, addr
);
201 writeb(addr
, i2c
->regs
+ S3C2410_IICDS
);
203 // delay a bit and reset iiccon before setting start (per samsung)
205 dev_dbg(i2c
->dev
, "iiccon, %08lx\n", iiccon
);
206 writel(iiccon
, i2c
->regs
+ S3C2410_IICCON
);
208 stat
|= S3C2410_IICSTAT_START
;
209 writel(stat
, i2c
->regs
+ S3C2410_IICSTAT
);
212 static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c
*i2c
, int ret
)
214 unsigned long iicstat
= readl(i2c
->regs
+ S3C2410_IICSTAT
);
216 dev_dbg(i2c
->dev
, "STOP\n");
218 /* stop the transfer */
219 iicstat
&= ~ S3C2410_IICSTAT_START
;
220 writel(iicstat
, i2c
->regs
+ S3C2410_IICSTAT
);
222 i2c
->state
= STATE_STOP
;
224 s3c24xx_i2c_master_complete(i2c
, ret
);
225 s3c24xx_i2c_disable_irq(i2c
);
228 /* helper functions to determine the current state in the set of
229 * messages we are sending */
233 * returns TRUE if the current message is the last in the set
236 static inline int is_lastmsg(struct s3c24xx_i2c
*i2c
)
238 return i2c
->msg_idx
>= (i2c
->msg_num
- 1);
243 * returns TRUE if we this is the last byte in the current message
246 static inline int is_msglast(struct s3c24xx_i2c
*i2c
)
248 return i2c
->msg_ptr
== i2c
->msg
->len
-1;
253 * returns TRUE if we reached the end of the current message
256 static inline int is_msgend(struct s3c24xx_i2c
*i2c
)
258 return i2c
->msg_ptr
>= i2c
->msg
->len
;
261 /* i2s_s3c_irq_nextbyte
263 * process an interrupt and work out what to do
266 static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c
*i2c
, unsigned long iicstat
)
272 switch (i2c
->state
) {
275 dev_err(i2c
->dev
, "%s: called in STATE_IDLE\n", __FUNCTION__
);
280 dev_err(i2c
->dev
, "%s: called in STATE_STOP\n", __FUNCTION__
);
281 s3c24xx_i2c_disable_irq(i2c
);
285 /* last thing we did was send a start condition on the
286 * bus, or started a new i2c message
289 if (iicstat
& S3C2410_IICSTAT_LASTBIT
&&
290 !(i2c
->msg
->flags
& I2C_M_IGNORE_NAK
)) {
291 /* ack was not received... */
293 dev_dbg(i2c
->dev
, "ack was not received\n");
294 s3c24xx_i2c_stop(i2c
, -EREMOTEIO
);
298 if (i2c
->msg
->flags
& I2C_M_RD
)
299 i2c
->state
= STATE_READ
;
301 i2c
->state
= STATE_WRITE
;
303 /* terminate the transfer if there is nothing to do
304 * (used by the i2c probe to find devices */
306 if (is_lastmsg(i2c
) && i2c
->msg
->len
== 0) {
307 s3c24xx_i2c_stop(i2c
, 0);
311 if (i2c
->state
== STATE_READ
)
314 /* fall through to the write state, as we will need to
315 * send a byte as well */
318 /* we are writing data to the device... check for the
319 * end of the message, and if so, work out what to do
323 if (!is_msgend(i2c
)) {
324 byte
= i2c
->msg
->buf
[i2c
->msg_ptr
++];
325 writeb(byte
, i2c
->regs
+ S3C2410_IICDS
);
327 } else if (!is_lastmsg(i2c
)) {
328 /* we need to go to the next i2c message */
330 dev_dbg(i2c
->dev
, "WRITE: Next Message\n");
336 /* check to see if we need to do another message */
337 if (i2c
->msg
->flags
& I2C_M_NOSTART
) {
339 if (i2c
->msg
->flags
& I2C_M_RD
) {
340 /* cannot do this, the controller
341 * forces us to send a new START
342 * when we change direction */
344 s3c24xx_i2c_stop(i2c
, -EINVAL
);
350 /* send the new start */
351 s3c24xx_i2c_message_start(i2c
, i2c
->msg
);
352 i2c
->state
= STATE_START
;
358 s3c24xx_i2c_stop(i2c
, 0);
363 /* we have a byte of data in the data register, do
364 * something with it, and then work out wether we are
365 * going to do any more read/write
368 if (!(i2c
->msg
->flags
& I2C_M_IGNORE_NAK
) &&
369 !(is_msglast(i2c
) && is_lastmsg(i2c
))) {
371 if (iicstat
& S3C2410_IICSTAT_LASTBIT
) {
372 dev_dbg(i2c
->dev
, "READ: No Ack\n");
374 s3c24xx_i2c_stop(i2c
, -ECONNREFUSED
);
379 byte
= readb(i2c
->regs
+ S3C2410_IICDS
);
380 i2c
->msg
->buf
[i2c
->msg_ptr
++] = byte
;
383 if (is_msglast(i2c
)) {
384 /* last byte of buffer */
387 s3c24xx_i2c_disable_ack(i2c
);
389 } else if (is_msgend(i2c
)) {
390 /* ok, we've read the entire buffer, see if there
391 * is anything else we need to do */
393 if (is_lastmsg(i2c
)) {
394 /* last message, send stop and complete */
395 dev_dbg(i2c
->dev
, "READ: Send Stop\n");
397 s3c24xx_i2c_stop(i2c
, 0);
399 /* go to the next transfer */
400 dev_dbg(i2c
->dev
, "READ: Next Transfer\n");
411 /* acknowlegde the IRQ and get back on with the work */
414 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
415 tmp
&= ~S3C2410_IICCON_IRQPEND
;
416 writel(tmp
, i2c
->regs
+ S3C2410_IICCON
);
423 * top level IRQ servicing routine
426 static irqreturn_t
s3c24xx_i2c_irq(int irqno
, void *dev_id
)
428 struct s3c24xx_i2c
*i2c
= dev_id
;
429 unsigned long status
;
432 status
= readl(i2c
->regs
+ S3C2410_IICSTAT
);
434 if (status
& S3C2410_IICSTAT_ARBITR
) {
435 // deal with arbitration loss
436 dev_err(i2c
->dev
, "deal with arbitration loss\n");
439 if (i2c
->state
== STATE_IDLE
) {
440 dev_dbg(i2c
->dev
, "IRQ: error i2c->state == IDLE\n");
442 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
443 tmp
&= ~S3C2410_IICCON_IRQPEND
;
444 writel(tmp
, i2c
->regs
+ S3C2410_IICCON
);
448 /* pretty much this leaves us with the fact that we've
449 * transmitted or received whatever byte we last sent */
451 i2s_s3c_irq_nextbyte(i2c
, status
);
458 /* s3c24xx_i2c_set_master
460 * get the i2c bus for a master transaction
463 static int s3c24xx_i2c_set_master(struct s3c24xx_i2c
*i2c
)
465 unsigned long iicstat
;
468 while (timeout
-- > 0) {
469 iicstat
= readl(i2c
->regs
+ S3C2410_IICSTAT
);
471 if (!(iicstat
& S3C2410_IICSTAT_BUSBUSY
))
477 dev_dbg(i2c
->dev
, "timeout: GPEDAT is %08x\n",
478 __raw_readl(S3C2410_GPEDAT
));
483 /* s3c24xx_i2c_doxfer
485 * this starts an i2c transfer
488 static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c
*i2c
, struct i2c_msg
*msgs
, int num
)
490 unsigned long timeout
;
493 ret
= s3c24xx_i2c_set_master(i2c
);
495 dev_err(i2c
->dev
, "cannot get bus (error %d)\n", ret
);
500 spin_lock_irq(&i2c
->lock
);
506 i2c
->state
= STATE_START
;
508 s3c24xx_i2c_enable_irq(i2c
);
509 s3c24xx_i2c_message_start(i2c
, msgs
);
510 spin_unlock_irq(&i2c
->lock
);
512 timeout
= wait_event_timeout(i2c
->wait
, i2c
->msg_num
== 0, HZ
* 5);
516 /* having these next two as dev_err() makes life very
517 * noisy when doing an i2cdetect */
520 dev_dbg(i2c
->dev
, "timeout\n");
522 dev_dbg(i2c
->dev
, "incomplete xfer (%d)\n", ret
);
524 /* ensure the stop has been through the bus */
534 * first port of call from the i2c bus code when an message needs
535 * transferring across the i2c bus.
538 static int s3c24xx_i2c_xfer(struct i2c_adapter
*adap
,
539 struct i2c_msg
*msgs
, int num
)
541 struct s3c24xx_i2c
*i2c
= (struct s3c24xx_i2c
*)adap
->algo_data
;
545 for (retry
= 0; retry
< adap
->retries
; retry
++) {
547 ret
= s3c24xx_i2c_doxfer(i2c
, msgs
, num
);
552 dev_dbg(i2c
->dev
, "Retrying transmission (%d)\n", retry
);
560 /* declare our i2c functionality */
561 static u32
s3c24xx_i2c_func(struct i2c_adapter
*adap
)
563 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_PROTOCOL_MANGLING
;
566 /* i2c bus registration info */
568 static const struct i2c_algorithm s3c24xx_i2c_algorithm
= {
569 .master_xfer
= s3c24xx_i2c_xfer
,
570 .functionality
= s3c24xx_i2c_func
,
573 static struct s3c24xx_i2c s3c24xx_i2c
= {
574 .lock
= SPIN_LOCK_UNLOCKED
,
575 .wait
= __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c
.wait
),
577 .name
= "s3c2410-i2c",
578 .owner
= THIS_MODULE
,
579 .algo
= &s3c24xx_i2c_algorithm
,
581 .class = I2C_CLASS_HWMON
,
585 /* s3c24xx_i2c_calcdivisor
587 * return the divisor settings for a given frequency
590 static int s3c24xx_i2c_calcdivisor(unsigned long clkin
, unsigned int wanted
,
591 unsigned int *div1
, unsigned int *divs
)
593 unsigned int calc_divs
= clkin
/ wanted
;
594 unsigned int calc_div1
;
596 if (calc_divs
> (16*16))
601 calc_divs
+= calc_div1
-1;
602 calc_divs
/= calc_div1
;
612 return clkin
/ (calc_divs
* calc_div1
);
617 * test wether a frequency is within the acceptable range of error
620 static inline int freq_acceptable(unsigned int freq
, unsigned int wanted
)
622 int diff
= freq
- wanted
;
624 return (diff
>= -2 && diff
<= 2);
627 /* s3c24xx_i2c_getdivisor
629 * work out a divisor for the user requested frequency setting,
630 * either by the requested frequency, or scanning the acceptable
631 * range of frequencies until something is found
634 static int s3c24xx_i2c_getdivisor(struct s3c24xx_i2c
*i2c
,
635 struct s3c2410_platform_i2c
*pdata
,
636 unsigned long *iicon
,
639 unsigned long clkin
= clk_get_rate(i2c
->clk
);
641 unsigned int divs
, div1
;
645 clkin
/= 1000; /* clkin now in KHz */
647 dev_dbg(i2c
->dev
, "pdata %p, freq %lu %lu..%lu\n",
648 pdata
, pdata
->bus_freq
, pdata
->min_freq
, pdata
->max_freq
);
650 if (pdata
->bus_freq
!= 0) {
651 freq
= s3c24xx_i2c_calcdivisor(clkin
, pdata
->bus_freq
/1000,
653 if (freq_acceptable(freq
, pdata
->bus_freq
/1000))
657 /* ok, we may have to search for something suitable... */
659 start
= (pdata
->max_freq
== 0) ? pdata
->bus_freq
: pdata
->max_freq
;
660 end
= pdata
->min_freq
;
667 for (; start
> end
; start
--) {
668 freq
= s3c24xx_i2c_calcdivisor(clkin
, start
, &div1
, &divs
);
669 if (freq_acceptable(freq
, start
))
673 /* cannot find frequency spec */
680 *iicon
|= (div1
== 512) ? S3C2410_IICCON_TXDIV_512
: 0;
686 * initialise the controller, set the IO lines and frequency
689 static int s3c24xx_i2c_init(struct s3c24xx_i2c
*i2c
)
691 unsigned long iicon
= S3C2410_IICCON_IRQEN
| S3C2410_IICCON_ACKEN
;
692 struct s3c2410_platform_i2c
*pdata
;
695 /* get the plafrom data */
697 pdata
= s3c24xx_i2c_get_platformdata(i2c
->adap
.dev
.parent
);
699 /* inititalise the gpio */
701 s3c2410_gpio_cfgpin(S3C2410_GPE15
, S3C2410_GPE15_IICSDA
);
702 s3c2410_gpio_cfgpin(S3C2410_GPE14
, S3C2410_GPE14_IICSCL
);
704 /* write slave address */
706 writeb(pdata
->slave_addr
, i2c
->regs
+ S3C2410_IICADD
);
708 dev_info(i2c
->dev
, "slave address 0x%02x\n", pdata
->slave_addr
);
710 /* we need to work out the divisors for the clock... */
712 if (s3c24xx_i2c_getdivisor(i2c
, pdata
, &iicon
, &freq
) != 0) {
713 dev_err(i2c
->dev
, "cannot meet bus frequency required\n");
717 /* todo - check that the i2c lines aren't being dragged anywhere */
719 dev_info(i2c
->dev
, "bus frequency set to %d KHz\n", freq
);
720 dev_dbg(i2c
->dev
, "S3C2410_IICCON=0x%02lx\n", iicon
);
722 writel(iicon
, i2c
->regs
+ S3C2410_IICCON
);
724 /* check for s3c2440 i2c controller */
726 if (s3c24xx_i2c_is2440(i2c
)) {
727 dev_dbg(i2c
->dev
, "S3C2440_IICLC=%08x\n", pdata
->sda_delay
);
729 writel(pdata
->sda_delay
, i2c
->regs
+ S3C2440_IICLC
);
735 static void s3c24xx_i2c_free(struct s3c24xx_i2c
*i2c
)
737 if (i2c
->clk
!= NULL
&& !IS_ERR(i2c
->clk
)) {
738 clk_disable(i2c
->clk
);
743 if (i2c
->regs
!= NULL
) {
748 if (i2c
->ioarea
!= NULL
) {
749 release_resource(i2c
->ioarea
);
757 * called by the bus driver when a suitable device is found
760 static int s3c24xx_i2c_probe(struct platform_device
*pdev
)
762 struct s3c24xx_i2c
*i2c
= &s3c24xx_i2c
;
763 struct resource
*res
;
766 /* find the clock and enable it */
768 i2c
->dev
= &pdev
->dev
;
769 i2c
->clk
= clk_get(&pdev
->dev
, "i2c");
770 if (IS_ERR(i2c
->clk
)) {
771 dev_err(&pdev
->dev
, "cannot get clock\n");
776 dev_dbg(&pdev
->dev
, "clock source %p\n", i2c
->clk
);
778 clk_enable(i2c
->clk
);
780 /* map the registers */
782 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
784 dev_err(&pdev
->dev
, "cannot find IO resource\n");
789 i2c
->ioarea
= request_mem_region(res
->start
, (res
->end
-res
->start
)+1,
792 if (i2c
->ioarea
== NULL
) {
793 dev_err(&pdev
->dev
, "cannot request IO\n");
798 i2c
->regs
= ioremap(res
->start
, (res
->end
-res
->start
)+1);
800 if (i2c
->regs
== NULL
) {
801 dev_err(&pdev
->dev
, "cannot map IO\n");
806 dev_dbg(&pdev
->dev
, "registers %p (%p, %p)\n", i2c
->regs
, i2c
->ioarea
, res
);
808 /* setup info block for the i2c core */
810 i2c
->adap
.algo_data
= i2c
;
811 i2c
->adap
.dev
.parent
= &pdev
->dev
;
813 /* initialise the i2c controller */
815 ret
= s3c24xx_i2c_init(i2c
);
819 /* find the IRQ for this unit (note, this relies on the init call to
820 * ensure no current IRQs pending
823 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
825 dev_err(&pdev
->dev
, "cannot find IRQ\n");
830 ret
= request_irq(res
->start
, s3c24xx_i2c_irq
, IRQF_DISABLED
,
834 dev_err(&pdev
->dev
, "cannot claim IRQ\n");
840 dev_dbg(&pdev
->dev
, "irq resource %p (%ld)\n", res
, res
->start
);
842 ret
= i2c_add_adapter(&i2c
->adap
);
844 dev_err(&pdev
->dev
, "failed to add bus to i2c core\n");
848 platform_set_drvdata(pdev
, i2c
);
850 dev_info(&pdev
->dev
, "%s: S3C I2C adapter\n", i2c
->adap
.dev
.bus_id
);
854 s3c24xx_i2c_free(i2c
);
859 /* s3c24xx_i2c_remove
861 * called when device is removed from the bus
864 static int s3c24xx_i2c_remove(struct platform_device
*pdev
)
866 struct s3c24xx_i2c
*i2c
= platform_get_drvdata(pdev
);
869 s3c24xx_i2c_free(i2c
);
870 platform_set_drvdata(pdev
, NULL
);
877 static int s3c24xx_i2c_resume(struct platform_device
*dev
)
879 struct s3c24xx_i2c
*i2c
= platform_get_drvdata(dev
);
882 s3c24xx_i2c_init(i2c
);
888 #define s3c24xx_i2c_resume NULL
891 /* device driver for platform bus bits */
893 static struct platform_driver s3c2410_i2c_driver
= {
894 .probe
= s3c24xx_i2c_probe
,
895 .remove
= s3c24xx_i2c_remove
,
896 .resume
= s3c24xx_i2c_resume
,
898 .owner
= THIS_MODULE
,
899 .name
= "s3c2410-i2c",
903 static struct platform_driver s3c2440_i2c_driver
= {
904 .probe
= s3c24xx_i2c_probe
,
905 .remove
= s3c24xx_i2c_remove
,
906 .resume
= s3c24xx_i2c_resume
,
908 .owner
= THIS_MODULE
,
909 .name
= "s3c2440-i2c",
913 static int __init
i2c_adap_s3c_init(void)
917 ret
= platform_driver_register(&s3c2410_i2c_driver
);
919 ret
= platform_driver_register(&s3c2440_i2c_driver
);
921 platform_driver_unregister(&s3c2410_i2c_driver
);
927 static void __exit
i2c_adap_s3c_exit(void)
929 platform_driver_unregister(&s3c2410_i2c_driver
);
930 platform_driver_unregister(&s3c2440_i2c_driver
);
933 module_init(i2c_adap_s3c_init
);
934 module_exit(i2c_adap_s3c_exit
);
936 MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
937 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
938 MODULE_LICENSE("GPL");