1 /* linux/drivers/i2c/busses/i2c-s3c2410.c
3 * Copyright (C) 2004,2005,2009 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/init.h>
28 #include <linux/time.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
32 #include <linux/err.h>
33 #include <linux/platform_device.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/clk.h>
36 #include <linux/cpufreq.h>
37 #include <linux/slab.h>
39 #include <linux/of_i2c.h>
40 #include <linux/of_gpio.h>
44 #include <plat/regs-iic.h>
47 /* i2c controller state */
49 enum s3c24xx_i2c_state
{
57 enum s3c24xx_i2c_type
{
64 wait_queue_head_t wait
;
65 unsigned int suspended
:1;
72 unsigned int tx_setup
;
75 enum s3c24xx_i2c_state state
;
76 unsigned long clkrate
;
81 struct resource
*ioarea
;
82 struct i2c_adapter adap
;
84 struct s3c2410_platform_i2c
*pdata
;
86 #ifdef CONFIG_CPU_FREQ
87 struct notifier_block freq_transition
;
91 /* default platform data removed, dev should always carry data. */
93 /* s3c24xx_i2c_is2440()
95 * return true is this is an s3c2440
98 static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c
*i2c
)
100 struct platform_device
*pdev
= to_platform_device(i2c
->dev
);
101 enum s3c24xx_i2c_type type
;
104 if (i2c
->dev
->of_node
)
105 return of_device_is_compatible(i2c
->dev
->of_node
,
106 "samsung,s3c2440-i2c");
109 type
= platform_get_device_id(pdev
)->driver_data
;
110 return type
== TYPE_S3C2440
;
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
);
141 static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c
*i2c
)
145 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
146 writel(tmp
| S3C2410_IICCON_ACKEN
, i2c
->regs
+ S3C2410_IICCON
);
149 /* irq enable/disable functions */
151 static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c
*i2c
)
155 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
156 writel(tmp
& ~S3C2410_IICCON_IRQEN
, i2c
->regs
+ S3C2410_IICCON
);
159 static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c
*i2c
)
163 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
164 writel(tmp
| S3C2410_IICCON_IRQEN
, i2c
->regs
+ S3C2410_IICCON
);
168 /* s3c24xx_i2c_message_start
170 * put the start of a message onto the bus
173 static void s3c24xx_i2c_message_start(struct s3c24xx_i2c
*i2c
,
176 unsigned int addr
= (msg
->addr
& 0x7f) << 1;
178 unsigned long iiccon
;
181 stat
|= S3C2410_IICSTAT_TXRXEN
;
183 if (msg
->flags
& I2C_M_RD
) {
184 stat
|= S3C2410_IICSTAT_MASTER_RX
;
187 stat
|= S3C2410_IICSTAT_MASTER_TX
;
189 if (msg
->flags
& I2C_M_REV_DIR_ADDR
)
192 /* todo - check for wether ack wanted or not */
193 s3c24xx_i2c_enable_ack(i2c
);
195 iiccon
= readl(i2c
->regs
+ S3C2410_IICCON
);
196 writel(stat
, i2c
->regs
+ S3C2410_IICSTAT
);
198 dev_dbg(i2c
->dev
, "START: %08lx to IICSTAT, %02x to DS\n", stat
, addr
);
199 writeb(addr
, i2c
->regs
+ S3C2410_IICDS
);
201 /* delay here to ensure the data byte has gotten onto the bus
202 * before the transaction is started */
204 ndelay(i2c
->tx_setup
);
206 dev_dbg(i2c
->dev
, "iiccon, %08lx\n", iiccon
);
207 writel(iiccon
, i2c
->regs
+ S3C2410_IICCON
);
209 stat
|= S3C2410_IICSTAT_START
;
210 writel(stat
, i2c
->regs
+ S3C2410_IICSTAT
);
213 static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c
*i2c
, int ret
)
215 unsigned long iicstat
= readl(i2c
->regs
+ S3C2410_IICSTAT
);
217 dev_dbg(i2c
->dev
, "STOP\n");
219 /* stop the transfer */
220 iicstat
&= ~S3C2410_IICSTAT_START
;
221 writel(iicstat
, i2c
->regs
+ S3C2410_IICSTAT
);
223 i2c
->state
= STATE_STOP
;
225 s3c24xx_i2c_master_complete(i2c
, ret
);
226 s3c24xx_i2c_disable_irq(i2c
);
229 /* helper functions to determine the current state in the set of
230 * messages we are sending */
234 * returns TRUE if the current message is the last in the set
237 static inline int is_lastmsg(struct s3c24xx_i2c
*i2c
)
239 return i2c
->msg_idx
>= (i2c
->msg_num
- 1);
244 * returns TRUE if we this is the last byte in the current message
247 static inline int is_msglast(struct s3c24xx_i2c
*i2c
)
249 return i2c
->msg_ptr
== i2c
->msg
->len
-1;
254 * returns TRUE if we reached the end of the current message
257 static inline int is_msgend(struct s3c24xx_i2c
*i2c
)
259 return i2c
->msg_ptr
>= i2c
->msg
->len
;
262 /* i2c_s3c_irq_nextbyte
264 * process an interrupt and work out what to do
267 static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c
*i2c
, unsigned long iicstat
)
273 switch (i2c
->state
) {
276 dev_err(i2c
->dev
, "%s: called in STATE_IDLE\n", __func__
);
280 dev_err(i2c
->dev
, "%s: called in STATE_STOP\n", __func__
);
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
, -ENXIO
);
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 * as this is 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
322 if (!(i2c
->msg
->flags
& I2C_M_IGNORE_NAK
)) {
323 if (iicstat
& S3C2410_IICSTAT_LASTBIT
) {
324 dev_dbg(i2c
->dev
, "WRITE: No Ack\n");
326 s3c24xx_i2c_stop(i2c
, -ECONNREFUSED
);
333 if (!is_msgend(i2c
)) {
334 byte
= i2c
->msg
->buf
[i2c
->msg_ptr
++];
335 writeb(byte
, i2c
->regs
+ S3C2410_IICDS
);
337 /* delay after writing the byte to allow the
338 * data setup time on the bus, as writing the
339 * data to the register causes the first bit
340 * to appear on SDA, and SCL will change as
341 * soon as the interrupt is acknowledged */
343 ndelay(i2c
->tx_setup
);
345 } else if (!is_lastmsg(i2c
)) {
346 /* we need to go to the next i2c message */
348 dev_dbg(i2c
->dev
, "WRITE: Next Message\n");
354 /* check to see if we need to do another message */
355 if (i2c
->msg
->flags
& I2C_M_NOSTART
) {
357 if (i2c
->msg
->flags
& I2C_M_RD
) {
358 /* cannot do this, the controller
359 * forces us to send a new START
360 * when we change direction */
362 s3c24xx_i2c_stop(i2c
, -EINVAL
);
367 /* send the new start */
368 s3c24xx_i2c_message_start(i2c
, i2c
->msg
);
369 i2c
->state
= STATE_START
;
375 s3c24xx_i2c_stop(i2c
, 0);
380 /* we have a byte of data in the data register, do
381 * something with it, and then work out wether we are
382 * going to do any more read/write
385 byte
= readb(i2c
->regs
+ S3C2410_IICDS
);
386 i2c
->msg
->buf
[i2c
->msg_ptr
++] = byte
;
389 if (is_msglast(i2c
)) {
390 /* last byte of buffer */
393 s3c24xx_i2c_disable_ack(i2c
);
395 } else if (is_msgend(i2c
)) {
396 /* ok, we've read the entire buffer, see if there
397 * is anything else we need to do */
399 if (is_lastmsg(i2c
)) {
400 /* last message, send stop and complete */
401 dev_dbg(i2c
->dev
, "READ: Send Stop\n");
403 s3c24xx_i2c_stop(i2c
, 0);
405 /* go to the next transfer */
406 dev_dbg(i2c
->dev
, "READ: Next Transfer\n");
417 /* acknowlegde the IRQ and get back on with the work */
420 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
421 tmp
&= ~S3C2410_IICCON_IRQPEND
;
422 writel(tmp
, i2c
->regs
+ S3C2410_IICCON
);
429 * top level IRQ servicing routine
432 static irqreturn_t
s3c24xx_i2c_irq(int irqno
, void *dev_id
)
434 struct s3c24xx_i2c
*i2c
= dev_id
;
435 unsigned long status
;
438 status
= readl(i2c
->regs
+ S3C2410_IICSTAT
);
440 if (status
& S3C2410_IICSTAT_ARBITR
) {
441 /* deal with arbitration loss */
442 dev_err(i2c
->dev
, "deal with arbitration loss\n");
445 if (i2c
->state
== STATE_IDLE
) {
446 dev_dbg(i2c
->dev
, "IRQ: error i2c->state == IDLE\n");
448 tmp
= readl(i2c
->regs
+ S3C2410_IICCON
);
449 tmp
&= ~S3C2410_IICCON_IRQPEND
;
450 writel(tmp
, i2c
->regs
+ S3C2410_IICCON
);
454 /* pretty much this leaves us with the fact that we've
455 * transmitted or received whatever byte we last sent */
457 i2c_s3c_irq_nextbyte(i2c
, status
);
464 /* s3c24xx_i2c_set_master
466 * get the i2c bus for a master transaction
469 static int s3c24xx_i2c_set_master(struct s3c24xx_i2c
*i2c
)
471 unsigned long iicstat
;
474 while (timeout
-- > 0) {
475 iicstat
= readl(i2c
->regs
+ S3C2410_IICSTAT
);
477 if (!(iicstat
& S3C2410_IICSTAT_BUSBUSY
))
486 /* s3c24xx_i2c_doxfer
488 * this starts an i2c transfer
491 static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c
*i2c
,
492 struct i2c_msg
*msgs
, int num
)
494 unsigned long iicstat
, timeout
;
501 ret
= s3c24xx_i2c_set_master(i2c
);
503 dev_err(i2c
->dev
, "cannot get bus (error %d)\n", ret
);
508 spin_lock_irq(&i2c
->lock
);
514 i2c
->state
= STATE_START
;
516 s3c24xx_i2c_enable_irq(i2c
);
517 s3c24xx_i2c_message_start(i2c
, msgs
);
518 spin_unlock_irq(&i2c
->lock
);
520 timeout
= wait_event_timeout(i2c
->wait
, i2c
->msg_num
== 0, HZ
* 5);
524 /* having these next two as dev_err() makes life very
525 * noisy when doing an i2cdetect */
528 dev_dbg(i2c
->dev
, "timeout\n");
530 dev_dbg(i2c
->dev
, "incomplete xfer (%d)\n", ret
);
532 /* ensure the stop has been through the bus */
534 dev_dbg(i2c
->dev
, "waiting for bus idle\n");
536 /* first, try busy waiting briefly */
539 iicstat
= readl(i2c
->regs
+ S3C2410_IICSTAT
);
540 } while ((iicstat
& S3C2410_IICSTAT_START
) && --spins
);
542 /* if that timed out sleep */
545 iicstat
= readl(i2c
->regs
+ S3C2410_IICSTAT
);
548 if (iicstat
& S3C2410_IICSTAT_START
)
549 dev_warn(i2c
->dev
, "timeout waiting for bus idle\n");
557 * first port of call from the i2c bus code when an message needs
558 * transferring across the i2c bus.
561 static int s3c24xx_i2c_xfer(struct i2c_adapter
*adap
,
562 struct i2c_msg
*msgs
, int num
)
564 struct s3c24xx_i2c
*i2c
= (struct s3c24xx_i2c
*)adap
->algo_data
;
568 pm_runtime_get_sync(&adap
->dev
);
569 clk_enable(i2c
->clk
);
571 for (retry
= 0; retry
< adap
->retries
; retry
++) {
573 ret
= s3c24xx_i2c_doxfer(i2c
, msgs
, num
);
575 if (ret
!= -EAGAIN
) {
576 clk_disable(i2c
->clk
);
577 pm_runtime_put_sync(&adap
->dev
);
581 dev_dbg(i2c
->dev
, "Retrying transmission (%d)\n", retry
);
586 clk_disable(i2c
->clk
);
587 pm_runtime_put_sync(&adap
->dev
);
591 /* declare our i2c functionality */
592 static u32
s3c24xx_i2c_func(struct i2c_adapter
*adap
)
594 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_PROTOCOL_MANGLING
;
597 /* i2c bus registration info */
599 static const struct i2c_algorithm s3c24xx_i2c_algorithm
= {
600 .master_xfer
= s3c24xx_i2c_xfer
,
601 .functionality
= s3c24xx_i2c_func
,
604 /* s3c24xx_i2c_calcdivisor
606 * return the divisor settings for a given frequency
609 static int s3c24xx_i2c_calcdivisor(unsigned long clkin
, unsigned int wanted
,
610 unsigned int *div1
, unsigned int *divs
)
612 unsigned int calc_divs
= clkin
/ wanted
;
613 unsigned int calc_div1
;
615 if (calc_divs
> (16*16))
620 calc_divs
+= calc_div1
-1;
621 calc_divs
/= calc_div1
;
631 return clkin
/ (calc_divs
* calc_div1
);
634 /* s3c24xx_i2c_clockrate
636 * work out a divisor for the user requested frequency setting,
637 * either by the requested frequency, or scanning the acceptable
638 * range of frequencies until something is found
641 static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c
*i2c
, unsigned int *got
)
643 struct s3c2410_platform_i2c
*pdata
= i2c
->pdata
;
644 unsigned long clkin
= clk_get_rate(i2c
->clk
);
645 unsigned int divs
, div1
;
646 unsigned long target_frequency
;
650 i2c
->clkrate
= clkin
;
651 clkin
/= 1000; /* clkin now in KHz */
653 dev_dbg(i2c
->dev
, "pdata desired frequency %lu\n", pdata
->frequency
);
655 target_frequency
= pdata
->frequency
? pdata
->frequency
: 100000;
657 target_frequency
/= 1000; /* Target frequency now in KHz */
659 freq
= s3c24xx_i2c_calcdivisor(clkin
, target_frequency
, &div1
, &divs
);
661 if (freq
> target_frequency
) {
663 "Unable to achieve desired frequency %luKHz." \
664 " Lowest achievable %dKHz\n", target_frequency
, freq
);
670 iiccon
= readl(i2c
->regs
+ S3C2410_IICCON
);
671 iiccon
&= ~(S3C2410_IICCON_SCALEMASK
| S3C2410_IICCON_TXDIV_512
);
675 iiccon
|= S3C2410_IICCON_TXDIV_512
;
677 writel(iiccon
, i2c
->regs
+ S3C2410_IICCON
);
679 if (s3c24xx_i2c_is2440(i2c
)) {
680 unsigned long sda_delay
;
682 if (pdata
->sda_delay
) {
683 sda_delay
= clkin
* pdata
->sda_delay
;
684 sda_delay
= DIV_ROUND_UP(sda_delay
, 1000000);
685 sda_delay
= DIV_ROUND_UP(sda_delay
, 5);
688 sda_delay
|= S3C2410_IICLC_FILTER_ON
;
692 dev_dbg(i2c
->dev
, "IICLC=%08lx\n", sda_delay
);
693 writel(sda_delay
, i2c
->regs
+ S3C2440_IICLC
);
699 #ifdef CONFIG_CPU_FREQ
701 #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
703 static int s3c24xx_i2c_cpufreq_transition(struct notifier_block
*nb
,
704 unsigned long val
, void *data
)
706 struct s3c24xx_i2c
*i2c
= freq_to_i2c(nb
);
712 delta_f
= clk_get_rate(i2c
->clk
) - i2c
->clkrate
;
714 /* if we're post-change and the input clock has slowed down
715 * or at pre-change and the clock is about to speed up, then
716 * adjust our clock rate. <0 is slow, >0 speedup.
719 if ((val
== CPUFREQ_POSTCHANGE
&& delta_f
< 0) ||
720 (val
== CPUFREQ_PRECHANGE
&& delta_f
> 0)) {
721 spin_lock_irqsave(&i2c
->lock
, flags
);
722 ret
= s3c24xx_i2c_clockrate(i2c
, &got
);
723 spin_unlock_irqrestore(&i2c
->lock
, flags
);
726 dev_err(i2c
->dev
, "cannot find frequency\n");
728 dev_info(i2c
->dev
, "setting freq %d\n", got
);
734 static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c
*i2c
)
736 i2c
->freq_transition
.notifier_call
= s3c24xx_i2c_cpufreq_transition
;
738 return cpufreq_register_notifier(&i2c
->freq_transition
,
739 CPUFREQ_TRANSITION_NOTIFIER
);
742 static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c
*i2c
)
744 cpufreq_unregister_notifier(&i2c
->freq_transition
,
745 CPUFREQ_TRANSITION_NOTIFIER
);
749 static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c
*i2c
)
754 static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c
*i2c
)
760 static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c
*i2c
)
764 for (idx
= 0; idx
< 2; idx
++) {
765 gpio
= of_get_gpio(i2c
->dev
->of_node
, idx
);
766 if (!gpio_is_valid(gpio
)) {
767 dev_err(i2c
->dev
, "invalid gpio[%d]: %d\n", idx
, gpio
);
771 ret
= gpio_request(gpio
, "i2c-bus");
773 dev_err(i2c
->dev
, "gpio [%d] request failed\n", gpio
);
781 gpio_free(i2c
->gpios
[idx
]);
785 static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c
*i2c
)
788 for (idx
= 0; idx
< 2; idx
++)
789 gpio_free(i2c
->gpios
[idx
]);
792 static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c
*i2c
)
797 static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c
*i2c
)
804 * initialise the controller, set the IO lines and frequency
807 static int s3c24xx_i2c_init(struct s3c24xx_i2c
*i2c
)
809 unsigned long iicon
= S3C2410_IICCON_IRQEN
| S3C2410_IICCON_ACKEN
;
810 struct s3c2410_platform_i2c
*pdata
;
813 /* get the plafrom data */
817 /* inititalise the gpio */
820 pdata
->cfg_gpio(to_platform_device(i2c
->dev
));
822 if (s3c24xx_i2c_parse_dt_gpio(i2c
))
825 /* write slave address */
827 writeb(pdata
->slave_addr
, i2c
->regs
+ S3C2410_IICADD
);
829 dev_info(i2c
->dev
, "slave address 0x%02x\n", pdata
->slave_addr
);
831 writel(iicon
, i2c
->regs
+ S3C2410_IICCON
);
833 /* we need to work out the divisors for the clock... */
835 if (s3c24xx_i2c_clockrate(i2c
, &freq
) != 0) {
836 writel(0, i2c
->regs
+ S3C2410_IICCON
);
837 dev_err(i2c
->dev
, "cannot meet bus frequency required\n");
841 /* todo - check that the i2c lines aren't being dragged anywhere */
843 dev_info(i2c
->dev
, "bus frequency set to %d KHz\n", freq
);
844 dev_dbg(i2c
->dev
, "S3C2410_IICCON=0x%02lx\n", iicon
);
850 /* s3c24xx_i2c_parse_dt
852 * Parse the device tree node and retreive the platform data.
856 s3c24xx_i2c_parse_dt(struct device_node
*np
, struct s3c24xx_i2c
*i2c
)
858 struct s3c2410_platform_i2c
*pdata
= i2c
->pdata
;
863 pdata
->bus_num
= -1; /* i2c bus number is dynamically assigned */
864 of_property_read_u32(np
, "samsung,i2c-sda-delay", &pdata
->sda_delay
);
865 of_property_read_u32(np
, "samsung,i2c-slave-addr", &pdata
->slave_addr
);
866 of_property_read_u32(np
, "samsung,i2c-max-bus-freq",
867 (u32
*)&pdata
->frequency
);
871 s3c24xx_i2c_parse_dt(struct device_node
*np
, struct s3c24xx_i2c
*i2c
)
879 * called by the bus driver when a suitable device is found
882 static int s3c24xx_i2c_probe(struct platform_device
*pdev
)
884 struct s3c24xx_i2c
*i2c
;
885 struct s3c2410_platform_i2c
*pdata
= NULL
;
886 struct resource
*res
;
889 if (!pdev
->dev
.of_node
) {
890 pdata
= pdev
->dev
.platform_data
;
892 dev_err(&pdev
->dev
, "no platform data\n");
897 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(struct s3c24xx_i2c
), GFP_KERNEL
);
899 dev_err(&pdev
->dev
, "no memory for state\n");
903 i2c
->pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
910 memcpy(i2c
->pdata
, pdata
, sizeof(*pdata
));
912 s3c24xx_i2c_parse_dt(pdev
->dev
.of_node
, i2c
);
914 strlcpy(i2c
->adap
.name
, "s3c2410-i2c", sizeof(i2c
->adap
.name
));
915 i2c
->adap
.owner
= THIS_MODULE
;
916 i2c
->adap
.algo
= &s3c24xx_i2c_algorithm
;
917 i2c
->adap
.retries
= 2;
918 i2c
->adap
.class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
921 spin_lock_init(&i2c
->lock
);
922 init_waitqueue_head(&i2c
->wait
);
924 /* find the clock and enable it */
926 i2c
->dev
= &pdev
->dev
;
927 i2c
->clk
= clk_get(&pdev
->dev
, "i2c");
928 if (IS_ERR(i2c
->clk
)) {
929 dev_err(&pdev
->dev
, "cannot get clock\n");
934 dev_dbg(&pdev
->dev
, "clock source %p\n", i2c
->clk
);
936 clk_enable(i2c
->clk
);
938 /* map the registers */
940 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
942 dev_err(&pdev
->dev
, "cannot find IO resource\n");
947 i2c
->ioarea
= request_mem_region(res
->start
, resource_size(res
),
950 if (i2c
->ioarea
== NULL
) {
951 dev_err(&pdev
->dev
, "cannot request IO\n");
956 i2c
->regs
= ioremap(res
->start
, resource_size(res
));
958 if (i2c
->regs
== NULL
) {
959 dev_err(&pdev
->dev
, "cannot map IO\n");
964 dev_dbg(&pdev
->dev
, "registers %p (%p, %p)\n",
965 i2c
->regs
, i2c
->ioarea
, res
);
967 /* setup info block for the i2c core */
969 i2c
->adap
.algo_data
= i2c
;
970 i2c
->adap
.dev
.parent
= &pdev
->dev
;
972 /* initialise the i2c controller */
974 ret
= s3c24xx_i2c_init(i2c
);
978 /* find the IRQ for this unit (note, this relies on the init call to
979 * ensure no current IRQs pending
982 i2c
->irq
= ret
= platform_get_irq(pdev
, 0);
984 dev_err(&pdev
->dev
, "cannot find IRQ\n");
988 ret
= request_irq(i2c
->irq
, s3c24xx_i2c_irq
, 0,
989 dev_name(&pdev
->dev
), i2c
);
992 dev_err(&pdev
->dev
, "cannot claim IRQ %d\n", i2c
->irq
);
996 ret
= s3c24xx_i2c_register_cpufreq(i2c
);
998 dev_err(&pdev
->dev
, "failed to register cpufreq notifier\n");
1002 /* Note, previous versions of the driver used i2c_add_adapter()
1003 * to add the bus at any number. We now pass the bus number via
1004 * the platform data, so if unset it will now default to always
1008 i2c
->adap
.nr
= i2c
->pdata
->bus_num
;
1009 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
1011 ret
= i2c_add_numbered_adapter(&i2c
->adap
);
1013 dev_err(&pdev
->dev
, "failed to add bus to i2c core\n");
1017 of_i2c_register_devices(&i2c
->adap
);
1018 platform_set_drvdata(pdev
, i2c
);
1020 pm_runtime_enable(&pdev
->dev
);
1021 pm_runtime_enable(&i2c
->adap
.dev
);
1023 dev_info(&pdev
->dev
, "%s: S3C I2C adapter\n", dev_name(&i2c
->adap
.dev
));
1024 clk_disable(i2c
->clk
);
1028 s3c24xx_i2c_deregister_cpufreq(i2c
);
1031 free_irq(i2c
->irq
, i2c
);
1037 release_resource(i2c
->ioarea
);
1041 clk_disable(i2c
->clk
);
1048 /* s3c24xx_i2c_remove
1050 * called when device is removed from the bus
1053 static int s3c24xx_i2c_remove(struct platform_device
*pdev
)
1055 struct s3c24xx_i2c
*i2c
= platform_get_drvdata(pdev
);
1057 pm_runtime_disable(&i2c
->adap
.dev
);
1058 pm_runtime_disable(&pdev
->dev
);
1060 s3c24xx_i2c_deregister_cpufreq(i2c
);
1062 i2c_del_adapter(&i2c
->adap
);
1063 free_irq(i2c
->irq
, i2c
);
1065 clk_disable(i2c
->clk
);
1070 release_resource(i2c
->ioarea
);
1071 s3c24xx_i2c_dt_gpio_free(i2c
);
1078 static int s3c24xx_i2c_suspend_noirq(struct device
*dev
)
1080 struct platform_device
*pdev
= to_platform_device(dev
);
1081 struct s3c24xx_i2c
*i2c
= platform_get_drvdata(pdev
);
1088 static int s3c24xx_i2c_resume(struct device
*dev
)
1090 struct platform_device
*pdev
= to_platform_device(dev
);
1091 struct s3c24xx_i2c
*i2c
= platform_get_drvdata(pdev
);
1094 clk_enable(i2c
->clk
);
1095 s3c24xx_i2c_init(i2c
);
1096 clk_disable(i2c
->clk
);
1101 static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops
= {
1102 .suspend_noirq
= s3c24xx_i2c_suspend_noirq
,
1103 .resume
= s3c24xx_i2c_resume
,
1106 #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
1108 #define S3C24XX_DEV_PM_OPS NULL
1111 /* device driver for platform bus bits */
1113 static struct platform_device_id s3c24xx_driver_ids
[] = {
1115 .name
= "s3c2410-i2c",
1116 .driver_data
= TYPE_S3C2410
,
1118 .name
= "s3c2440-i2c",
1119 .driver_data
= TYPE_S3C2440
,
1122 MODULE_DEVICE_TABLE(platform
, s3c24xx_driver_ids
);
1125 static const struct of_device_id s3c24xx_i2c_match
[] = {
1126 { .compatible
= "samsung,s3c2410-i2c" },
1127 { .compatible
= "samsung,s3c2440-i2c" },
1130 MODULE_DEVICE_TABLE(of
, s3c24xx_i2c_match
);
1132 #define s3c24xx_i2c_match NULL
1135 static struct platform_driver s3c24xx_i2c_driver
= {
1136 .probe
= s3c24xx_i2c_probe
,
1137 .remove
= s3c24xx_i2c_remove
,
1138 .id_table
= s3c24xx_driver_ids
,
1140 .owner
= THIS_MODULE
,
1142 .pm
= S3C24XX_DEV_PM_OPS
,
1143 .of_match_table
= s3c24xx_i2c_match
,
1147 static int __init
i2c_adap_s3c_init(void)
1149 return platform_driver_register(&s3c24xx_i2c_driver
);
1151 subsys_initcall(i2c_adap_s3c_init
);
1153 static void __exit
i2c_adap_s3c_exit(void)
1155 platform_driver_unregister(&s3c24xx_i2c_driver
);
1157 module_exit(i2c_adap_s3c_exit
);
1159 MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1160 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1161 MODULE_LICENSE("GPL");