2 * TI DAVINCI I2C adapter driver.
4 * Copyright (C) 2006 Texas Instruments.
5 * Copyright (C) 2007 MontaVista Software Inc.
7 * Updated by Vinod & Sudhakar Feb 2005
9 * ----------------------------------------------------------------------------
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * ----------------------------------------------------------------------------
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/delay.h>
26 #include <linux/i2c.h>
27 #include <linux/clk.h>
28 #include <linux/errno.h>
29 #include <linux/sched.h>
30 #include <linux/err.h>
31 #include <linux/interrupt.h>
32 #include <linux/platform_device.h>
34 #include <linux/slab.h>
35 #include <linux/cpufreq.h>
36 #include <linux/gpio/consumer.h>
37 #include <linux/of_device.h>
38 #include <linux/platform_data/i2c-davinci.h>
39 #include <linux/pm_runtime.h>
41 /* ----- global defines ----------------------------------------------- */
43 #define DAVINCI_I2C_TIMEOUT (1*HZ)
44 #define DAVINCI_I2C_MAX_TRIES 2
45 #define DAVINCI_I2C_OWN_ADDRESS 0x08
46 #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_SCD | \
47 DAVINCI_I2C_IMR_ARDY | \
48 DAVINCI_I2C_IMR_NACK | \
51 #define DAVINCI_I2C_OAR_REG 0x00
52 #define DAVINCI_I2C_IMR_REG 0x04
53 #define DAVINCI_I2C_STR_REG 0x08
54 #define DAVINCI_I2C_CLKL_REG 0x0c
55 #define DAVINCI_I2C_CLKH_REG 0x10
56 #define DAVINCI_I2C_CNT_REG 0x14
57 #define DAVINCI_I2C_DRR_REG 0x18
58 #define DAVINCI_I2C_SAR_REG 0x1c
59 #define DAVINCI_I2C_DXR_REG 0x20
60 #define DAVINCI_I2C_MDR_REG 0x24
61 #define DAVINCI_I2C_IVR_REG 0x28
62 #define DAVINCI_I2C_EMDR_REG 0x2c
63 #define DAVINCI_I2C_PSC_REG 0x30
64 #define DAVINCI_I2C_FUNC_REG 0x48
65 #define DAVINCI_I2C_DIR_REG 0x4c
66 #define DAVINCI_I2C_DIN_REG 0x50
67 #define DAVINCI_I2C_DOUT_REG 0x54
68 #define DAVINCI_I2C_DSET_REG 0x58
69 #define DAVINCI_I2C_DCLR_REG 0x5c
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)
103 /* set SDA and SCL as GPIO */
104 #define DAVINCI_I2C_FUNC_PFUNC0 BIT(0)
106 /* set SCL as output when used as GPIO*/
107 #define DAVINCI_I2C_DIR_PDIR0 BIT(0)
108 /* set SDA as output when used as GPIO*/
109 #define DAVINCI_I2C_DIR_PDIR1 BIT(1)
111 /* read SCL GPIO level */
112 #define DAVINCI_I2C_DIN_PDIN0 BIT(0)
113 /* read SDA GPIO level */
114 #define DAVINCI_I2C_DIN_PDIN1 BIT(1)
116 /*set the SCL GPIO high */
117 #define DAVINCI_I2C_DSET_PDSET0 BIT(0)
118 /*set the SDA GPIO high */
119 #define DAVINCI_I2C_DSET_PDSET1 BIT(1)
121 /* set the SCL GPIO low */
122 #define DAVINCI_I2C_DCLR_PDCLR0 BIT(0)
123 /* set the SDA GPIO low */
124 #define DAVINCI_I2C_DCLR_PDCLR1 BIT(1)
126 /* timeout for pm runtime autosuspend */
127 #define DAVINCI_I2C_PM_TIMEOUT 1000 /* ms */
129 struct davinci_i2c_dev
{
132 struct completion cmd_complete
;
140 struct i2c_adapter adapter
;
141 #ifdef CONFIG_CPU_FREQ
142 struct notifier_block freq_transition
;
144 struct davinci_i2c_platform_data
*pdata
;
147 /* default platform data to use if not supplied in the platform_device */
148 static struct davinci_i2c_platform_data davinci_i2c_platform_data_default
= {
153 static inline void davinci_i2c_write_reg(struct davinci_i2c_dev
*i2c_dev
,
156 writew_relaxed(val
, i2c_dev
->base
+ reg
);
159 static inline u16
davinci_i2c_read_reg(struct davinci_i2c_dev
*i2c_dev
, int reg
)
161 return readw_relaxed(i2c_dev
->base
+ reg
);
164 static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev
*i2c_dev
,
169 w
= davinci_i2c_read_reg(i2c_dev
, DAVINCI_I2C_MDR_REG
);
170 if (!val
) /* put I2C into reset */
171 w
&= ~DAVINCI_I2C_MDR_IRS
;
172 else /* take I2C out of reset */
173 w
|= DAVINCI_I2C_MDR_IRS
;
175 davinci_i2c_write_reg(i2c_dev
, DAVINCI_I2C_MDR_REG
, w
);
178 static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev
*dev
)
180 struct davinci_i2c_platform_data
*pdata
= dev
->pdata
;
186 u32 input_clock
= clk_get_rate(dev
->clk
);
187 struct device_node
*of_node
= dev
->dev
->of_node
;
189 /* NOTE: I2C Clock divider programming info
190 * As per I2C specs the following formulas provide prescaler
191 * and low/high divider values
192 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
195 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
198 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
200 * where if PSC == 0, d = 7,
205 * d is always 6 on Keystone I2C controller
209 * Both Davinci and current Keystone User Guides recommend a value
210 * between 7MHz and 12MHz. In reality 7MHz module clock doesn't
211 * always produce enough margin between SDA and SCL transitions.
212 * Measurements show that the higher the module clock is, the
213 * bigger is the margin, providing more reliable communication.
214 * So we better target for 12MHz.
216 psc
= (input_clock
/ 12000000) - 1;
217 if ((input_clock
/ (psc
+ 1)) > 12000000)
218 psc
++; /* better to run under spec than over */
219 d
= (psc
>= 2) ? 5 : 7 - psc
;
221 if (of_node
&& of_device_is_compatible(of_node
, "ti,keystone-i2c"))
224 clk
= ((input_clock
/ (psc
+ 1)) / (pdata
->bus_freq
* 1000));
225 /* Avoid driving the bus too fast because of rounding errors above */
226 if (input_clock
/ (psc
+ 1) / clk
> pdata
->bus_freq
* 1000)
229 * According to I2C-BUS Spec 2.1, in FAST-MODE LOW period should be at
230 * least 1.3uS, which is not the case with 50% duty cycle. Driving HIGH
231 * to LOW ratio as 1 to 2 is more safe.
233 if (pdata
->bus_freq
> 100)
234 clkl
= (clk
<< 1) / 3;
238 * It's not always possible to have 1 to 2 ratio when d=7, so fall back
239 * to minimal possible clkh in this case.
242 * CLKH is not allowed to be 0, in this case I2C clock is not generated
245 if (clk
> clkl
+ d
) {
246 clkh
= clk
- clkl
- d
;
250 clkl
= clk
- (d
<< 1);
253 davinci_i2c_write_reg(dev
, DAVINCI_I2C_PSC_REG
, psc
);
254 davinci_i2c_write_reg(dev
, DAVINCI_I2C_CLKH_REG
, clkh
);
255 davinci_i2c_write_reg(dev
, DAVINCI_I2C_CLKL_REG
, clkl
);
257 dev_dbg(dev
->dev
, "input_clock = %d, CLK = %d\n", input_clock
, clk
);
261 * This function configures I2C and brings I2C out of reset.
262 * This function is called during I2C init function. This function
263 * also gets called if I2C encounters any errors.
265 static int i2c_davinci_init(struct davinci_i2c_dev
*dev
)
267 struct davinci_i2c_platform_data
*pdata
= dev
->pdata
;
269 /* put I2C into reset */
270 davinci_i2c_reset_ctrl(dev
, 0);
272 /* compute clock dividers */
273 i2c_davinci_calc_clk_dividers(dev
);
275 /* Respond at reserved "SMBus Host" slave address" (and zero);
276 * we seem to have no option to not respond...
278 davinci_i2c_write_reg(dev
, DAVINCI_I2C_OAR_REG
, DAVINCI_I2C_OWN_ADDRESS
);
280 dev_dbg(dev
->dev
, "PSC = %d\n",
281 davinci_i2c_read_reg(dev
, DAVINCI_I2C_PSC_REG
));
282 dev_dbg(dev
->dev
, "CLKL = %d\n",
283 davinci_i2c_read_reg(dev
, DAVINCI_I2C_CLKL_REG
));
284 dev_dbg(dev
->dev
, "CLKH = %d\n",
285 davinci_i2c_read_reg(dev
, DAVINCI_I2C_CLKH_REG
));
286 dev_dbg(dev
->dev
, "bus_freq = %dkHz, bus_delay = %d\n",
287 pdata
->bus_freq
, pdata
->bus_delay
);
290 /* Take the I2C module out of reset: */
291 davinci_i2c_reset_ctrl(dev
, 1);
293 /* Enable interrupts */
294 davinci_i2c_write_reg(dev
, DAVINCI_I2C_IMR_REG
, I2C_DAVINCI_INTR_ALL
);
300 * This routine does i2c bus recovery by using i2c_generic_scl_recovery
301 * which is provided by I2C Bus recovery infrastructure.
303 static void davinci_i2c_prepare_recovery(struct i2c_adapter
*adap
)
305 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
307 /* Disable interrupts */
308 davinci_i2c_write_reg(dev
, DAVINCI_I2C_IMR_REG
, 0);
310 /* put I2C into reset */
311 davinci_i2c_reset_ctrl(dev
, 0);
314 static void davinci_i2c_unprepare_recovery(struct i2c_adapter
*adap
)
316 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
318 i2c_davinci_init(dev
);
321 static struct i2c_bus_recovery_info davinci_i2c_gpio_recovery_info
= {
322 .recover_bus
= i2c_generic_scl_recovery
,
323 .prepare_recovery
= davinci_i2c_prepare_recovery
,
324 .unprepare_recovery
= davinci_i2c_unprepare_recovery
,
327 static void davinci_i2c_set_scl(struct i2c_adapter
*adap
, int val
)
329 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
332 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DSET_REG
,
333 DAVINCI_I2C_DSET_PDSET0
);
335 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DCLR_REG
,
336 DAVINCI_I2C_DCLR_PDCLR0
);
339 static int davinci_i2c_get_scl(struct i2c_adapter
*adap
)
341 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
344 /* read the state of SCL */
345 val
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_DIN_REG
);
346 return val
& DAVINCI_I2C_DIN_PDIN0
;
349 static int davinci_i2c_get_sda(struct i2c_adapter
*adap
)
351 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
354 /* read the state of SDA */
355 val
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_DIN_REG
);
356 return val
& DAVINCI_I2C_DIN_PDIN1
;
359 static void davinci_i2c_scl_prepare_recovery(struct i2c_adapter
*adap
)
361 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
363 davinci_i2c_prepare_recovery(adap
);
365 /* SCL output, SDA input */
366 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DIR_REG
, DAVINCI_I2C_DIR_PDIR0
);
368 /* change to GPIO mode */
369 davinci_i2c_write_reg(dev
, DAVINCI_I2C_FUNC_REG
,
370 DAVINCI_I2C_FUNC_PFUNC0
);
373 static void davinci_i2c_scl_unprepare_recovery(struct i2c_adapter
*adap
)
375 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
377 /* change back to I2C mode */
378 davinci_i2c_write_reg(dev
, DAVINCI_I2C_FUNC_REG
, 0);
380 davinci_i2c_unprepare_recovery(adap
);
383 static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info
= {
384 .recover_bus
= i2c_generic_scl_recovery
,
385 .set_scl
= davinci_i2c_set_scl
,
386 .get_scl
= davinci_i2c_get_scl
,
387 .get_sda
= davinci_i2c_get_sda
,
388 .prepare_recovery
= davinci_i2c_scl_prepare_recovery
,
389 .unprepare_recovery
= davinci_i2c_scl_unprepare_recovery
,
393 * Waiting for bus not busy
395 static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev
*dev
)
397 unsigned long timeout
= jiffies
+ dev
->adapter
.timeout
;
400 if (!(davinci_i2c_read_reg(dev
, DAVINCI_I2C_STR_REG
) & DAVINCI_I2C_STR_BB
))
402 schedule_timeout_uninterruptible(1);
403 } while (time_before_eq(jiffies
, timeout
));
405 dev_warn(dev
->dev
, "timeout waiting for bus ready\n");
406 i2c_recover_bus(&dev
->adapter
);
409 * if bus is still "busy" here, it's most probably a HW problem like
412 if (davinci_i2c_read_reg(dev
, DAVINCI_I2C_STR_REG
) & DAVINCI_I2C_STR_BB
)
419 * Low level master read/write transaction. This function is called
420 * from i2c_davinci_xfer.
423 i2c_davinci_xfer_msg(struct i2c_adapter
*adap
, struct i2c_msg
*msg
, int stop
)
425 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
426 struct davinci_i2c_platform_data
*pdata
= dev
->pdata
;
429 unsigned long time_left
;
431 if (msg
->addr
== DAVINCI_I2C_OWN_ADDRESS
) {
432 dev_warn(dev
->dev
, "transfer to own address aborted\n");
433 return -EADDRNOTAVAIL
;
436 /* Introduce a delay, required for some boards (e.g Davinci EVM) */
437 if (pdata
->bus_delay
)
438 udelay(pdata
->bus_delay
);
440 /* set the slave address */
441 davinci_i2c_write_reg(dev
, DAVINCI_I2C_SAR_REG
, msg
->addr
);
444 dev
->buf_len
= msg
->len
;
447 davinci_i2c_write_reg(dev
, DAVINCI_I2C_CNT_REG
, dev
->buf_len
);
449 reinit_completion(&dev
->cmd_complete
);
452 /* Take I2C out of reset and configure it as master */
453 flag
= DAVINCI_I2C_MDR_IRS
| DAVINCI_I2C_MDR_MST
;
455 /* if the slave address is ten bit address, enable XA bit */
456 if (msg
->flags
& I2C_M_TEN
)
457 flag
|= DAVINCI_I2C_MDR_XA
;
458 if (!(msg
->flags
& I2C_M_RD
))
459 flag
|= DAVINCI_I2C_MDR_TRX
;
461 flag
|= DAVINCI_I2C_MDR_RM
;
463 /* Enable receive or transmit interrupts */
464 w
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_IMR_REG
);
465 if (msg
->flags
& I2C_M_RD
)
466 w
|= DAVINCI_I2C_IMR_RRDY
;
468 w
|= DAVINCI_I2C_IMR_XRDY
;
469 davinci_i2c_write_reg(dev
, DAVINCI_I2C_IMR_REG
, w
);
474 * Write mode register first as needed for correct behaviour
475 * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
476 * occurring before we have loaded DXR
478 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, flag
);
481 * First byte should be set here, not after interrupt,
482 * because transmit-data-ready interrupt can come before
483 * NACK-interrupt during sending of previous message and
484 * ICDXR may have wrong data
485 * It also saves us one interrupt, slightly faster
487 if ((!(msg
->flags
& I2C_M_RD
)) && dev
->buf_len
) {
488 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DXR_REG
, *dev
->buf
++);
492 /* Set STT to begin transmit now DXR is loaded */
493 flag
|= DAVINCI_I2C_MDR_STT
;
494 if (stop
&& msg
->len
!= 0)
495 flag
|= DAVINCI_I2C_MDR_STP
;
496 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, flag
);
498 time_left
= wait_for_completion_timeout(&dev
->cmd_complete
,
499 dev
->adapter
.timeout
);
501 dev_err(dev
->dev
, "controller timed out\n");
502 i2c_recover_bus(adap
);
507 /* This should be 0 if all bytes were transferred
508 * or dev->cmd_err denotes an error.
510 dev_err(dev
->dev
, "abnormal termination buf_len=%zu\n",
519 if (likely(!dev
->cmd_err
))
522 /* We have an error */
523 if (dev
->cmd_err
& DAVINCI_I2C_STR_AL
) {
524 i2c_davinci_init(dev
);
528 if (dev
->cmd_err
& DAVINCI_I2C_STR_NACK
) {
529 if (msg
->flags
& I2C_M_IGNORE_NAK
)
531 w
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_MDR_REG
);
532 w
|= DAVINCI_I2C_MDR_STP
;
533 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, w
);
540 * Prepare controller for a transaction and call i2c_davinci_xfer_msg
543 i2c_davinci_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
545 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
549 dev_dbg(dev
->dev
, "%s: msgs: %d\n", __func__
, num
);
551 ret
= pm_runtime_get_sync(dev
->dev
);
553 dev_err(dev
->dev
, "Failed to runtime_get device: %d\n", ret
);
554 pm_runtime_put_noidle(dev
->dev
);
558 ret
= i2c_davinci_wait_bus_not_busy(dev
);
560 dev_warn(dev
->dev
, "timeout waiting for bus ready\n");
564 for (i
= 0; i
< num
; i
++) {
565 ret
= i2c_davinci_xfer_msg(adap
, &msgs
[i
], (i
== (num
- 1)));
566 dev_dbg(dev
->dev
, "%s [%d/%d] ret: %d\n", __func__
, i
+ 1, num
,
575 pm_runtime_mark_last_busy(dev
->dev
);
576 pm_runtime_put_autosuspend(dev
->dev
);
581 static u32
i2c_davinci_func(struct i2c_adapter
*adap
)
583 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
586 static void terminate_read(struct davinci_i2c_dev
*dev
)
588 u16 w
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_MDR_REG
);
589 w
|= DAVINCI_I2C_MDR_NACK
;
590 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, w
);
592 /* Throw away data */
593 davinci_i2c_read_reg(dev
, DAVINCI_I2C_DRR_REG
);
595 dev_err(dev
->dev
, "RDR IRQ while no data requested\n");
597 static void terminate_write(struct davinci_i2c_dev
*dev
)
599 u16 w
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_MDR_REG
);
600 w
|= DAVINCI_I2C_MDR_RM
| DAVINCI_I2C_MDR_STP
;
601 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, w
);
604 dev_dbg(dev
->dev
, "TDR IRQ while no data to send\n");
608 * Interrupt service routine. This gets called whenever an I2C interrupt
611 static irqreturn_t
i2c_davinci_isr(int this_irq
, void *dev_id
)
613 struct davinci_i2c_dev
*dev
= dev_id
;
618 if (pm_runtime_suspended(dev
->dev
))
621 while ((stat
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_IVR_REG
))) {
622 dev_dbg(dev
->dev
, "%s: stat=0x%x\n", __func__
, stat
);
623 if (count
++ == 100) {
624 dev_warn(dev
->dev
, "Too much work in one IRQ\n");
629 case DAVINCI_I2C_IVR_AL
:
630 /* Arbitration lost, must retry */
631 dev
->cmd_err
|= DAVINCI_I2C_STR_AL
;
633 complete(&dev
->cmd_complete
);
636 case DAVINCI_I2C_IVR_NACK
:
637 dev
->cmd_err
|= DAVINCI_I2C_STR_NACK
;
639 complete(&dev
->cmd_complete
);
642 case DAVINCI_I2C_IVR_ARDY
:
643 davinci_i2c_write_reg(dev
,
644 DAVINCI_I2C_STR_REG
, DAVINCI_I2C_STR_ARDY
);
645 if (((dev
->buf_len
== 0) && (dev
->stop
!= 0)) ||
646 (dev
->cmd_err
& DAVINCI_I2C_STR_NACK
)) {
647 w
= davinci_i2c_read_reg(dev
,
648 DAVINCI_I2C_MDR_REG
);
649 w
|= DAVINCI_I2C_MDR_STP
;
650 davinci_i2c_write_reg(dev
,
651 DAVINCI_I2C_MDR_REG
, w
);
653 complete(&dev
->cmd_complete
);
656 case DAVINCI_I2C_IVR_RDR
:
659 davinci_i2c_read_reg(dev
,
660 DAVINCI_I2C_DRR_REG
);
665 davinci_i2c_write_reg(dev
,
667 DAVINCI_I2C_IMR_RRDY
);
669 /* signal can terminate transfer */
674 case DAVINCI_I2C_IVR_XRDY
:
676 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DXR_REG
,
682 w
= davinci_i2c_read_reg(dev
,
683 DAVINCI_I2C_IMR_REG
);
684 w
&= ~DAVINCI_I2C_IMR_XRDY
;
685 davinci_i2c_write_reg(dev
,
689 /* signal can terminate transfer */
690 terminate_write(dev
);
694 case DAVINCI_I2C_IVR_SCD
:
695 davinci_i2c_write_reg(dev
,
696 DAVINCI_I2C_STR_REG
, DAVINCI_I2C_STR_SCD
);
697 complete(&dev
->cmd_complete
);
700 case DAVINCI_I2C_IVR_AAS
:
701 dev_dbg(dev
->dev
, "Address as slave interrupt\n");
705 dev_warn(dev
->dev
, "Unrecognized irq stat %d\n", stat
);
710 return count
? IRQ_HANDLED
: IRQ_NONE
;
713 #ifdef CONFIG_CPU_FREQ
714 static int i2c_davinci_cpufreq_transition(struct notifier_block
*nb
,
715 unsigned long val
, void *data
)
717 struct davinci_i2c_dev
*dev
;
719 dev
= container_of(nb
, struct davinci_i2c_dev
, freq_transition
);
721 i2c_lock_bus(&dev
->adapter
, I2C_LOCK_ROOT_ADAPTER
);
722 if (val
== CPUFREQ_PRECHANGE
) {
723 davinci_i2c_reset_ctrl(dev
, 0);
724 } else if (val
== CPUFREQ_POSTCHANGE
) {
725 i2c_davinci_calc_clk_dividers(dev
);
726 davinci_i2c_reset_ctrl(dev
, 1);
728 i2c_unlock_bus(&dev
->adapter
, I2C_LOCK_ROOT_ADAPTER
);
733 static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev
*dev
)
735 dev
->freq_transition
.notifier_call
= i2c_davinci_cpufreq_transition
;
737 return cpufreq_register_notifier(&dev
->freq_transition
,
738 CPUFREQ_TRANSITION_NOTIFIER
);
741 static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev
*dev
)
743 cpufreq_unregister_notifier(&dev
->freq_transition
,
744 CPUFREQ_TRANSITION_NOTIFIER
);
747 static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev
*dev
)
752 static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev
*dev
)
757 static const struct i2c_algorithm i2c_davinci_algo
= {
758 .master_xfer
= i2c_davinci_xfer
,
759 .functionality
= i2c_davinci_func
,
762 static const struct of_device_id davinci_i2c_of_match
[] = {
763 {.compatible
= "ti,davinci-i2c", },
764 {.compatible
= "ti,keystone-i2c", },
767 MODULE_DEVICE_TABLE(of
, davinci_i2c_of_match
);
769 static int davinci_i2c_probe(struct platform_device
*pdev
)
771 struct davinci_i2c_dev
*dev
;
772 struct i2c_adapter
*adap
;
773 struct resource
*mem
;
774 struct i2c_bus_recovery_info
*rinfo
;
777 irq
= platform_get_irq(pdev
, 0);
781 if (irq
!= -EPROBE_DEFER
)
783 "can't get irq resource ret=%d\n", irq
);
787 dev
= devm_kzalloc(&pdev
->dev
, sizeof(struct davinci_i2c_dev
),
790 dev_err(&pdev
->dev
, "Memory allocation failed\n");
794 init_completion(&dev
->cmd_complete
);
796 dev
->dev
= &pdev
->dev
;
798 dev
->pdata
= dev_get_platdata(&pdev
->dev
);
799 platform_set_drvdata(pdev
, dev
);
801 if (!dev
->pdata
&& pdev
->dev
.of_node
) {
804 dev
->pdata
= devm_kzalloc(&pdev
->dev
,
805 sizeof(struct davinci_i2c_platform_data
), GFP_KERNEL
);
809 memcpy(dev
->pdata
, &davinci_i2c_platform_data_default
,
810 sizeof(struct davinci_i2c_platform_data
));
811 if (!of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
813 dev
->pdata
->bus_freq
= prop
/ 1000;
815 dev
->pdata
->has_pfunc
=
816 of_property_read_bool(pdev
->dev
.of_node
,
818 } else if (!dev
->pdata
) {
819 dev
->pdata
= &davinci_i2c_platform_data_default
;
822 dev
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
823 if (IS_ERR(dev
->clk
))
824 return PTR_ERR(dev
->clk
);
826 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
827 dev
->base
= devm_ioremap_resource(&pdev
->dev
, mem
);
828 if (IS_ERR(dev
->base
)) {
829 return PTR_ERR(dev
->base
);
832 pm_runtime_set_autosuspend_delay(dev
->dev
,
833 DAVINCI_I2C_PM_TIMEOUT
);
834 pm_runtime_use_autosuspend(dev
->dev
);
836 pm_runtime_enable(dev
->dev
);
838 r
= pm_runtime_get_sync(dev
->dev
);
840 dev_err(dev
->dev
, "failed to runtime_get device: %d\n", r
);
841 pm_runtime_put_noidle(dev
->dev
);
845 i2c_davinci_init(dev
);
847 r
= devm_request_irq(&pdev
->dev
, dev
->irq
, i2c_davinci_isr
, 0,
850 dev_err(&pdev
->dev
, "failure requesting irq %i\n", dev
->irq
);
851 goto err_unuse_clocks
;
854 r
= i2c_davinci_cpufreq_register(dev
);
856 dev_err(&pdev
->dev
, "failed to register cpufreq\n");
857 goto err_unuse_clocks
;
860 adap
= &dev
->adapter
;
861 i2c_set_adapdata(adap
, dev
);
862 adap
->owner
= THIS_MODULE
;
863 adap
->class = I2C_CLASS_DEPRECATED
;
864 strlcpy(adap
->name
, "DaVinci I2C adapter", sizeof(adap
->name
));
865 adap
->algo
= &i2c_davinci_algo
;
866 adap
->dev
.parent
= &pdev
->dev
;
867 adap
->timeout
= DAVINCI_I2C_TIMEOUT
;
868 adap
->dev
.of_node
= pdev
->dev
.of_node
;
870 if (dev
->pdata
->has_pfunc
)
871 adap
->bus_recovery_info
= &davinci_i2c_scl_recovery_info
;
872 else if (dev
->pdata
->gpio_recovery
) {
873 rinfo
= &davinci_i2c_gpio_recovery_info
;
874 adap
->bus_recovery_info
= rinfo
;
875 rinfo
->scl_gpiod
= devm_gpiod_get(&pdev
->dev
, "scl",
876 GPIOD_OUT_HIGH_OPEN_DRAIN
);
877 if (IS_ERR(rinfo
->scl_gpiod
)) {
878 r
= PTR_ERR(rinfo
->scl_gpiod
);
879 goto err_unuse_clocks
;
881 rinfo
->sda_gpiod
= devm_gpiod_get(&pdev
->dev
, "sda", GPIOD_IN
);
882 if (IS_ERR(rinfo
->sda_gpiod
)) {
883 r
= PTR_ERR(rinfo
->sda_gpiod
);
884 goto err_unuse_clocks
;
889 r
= i2c_add_numbered_adapter(adap
);
891 goto err_unuse_clocks
;
893 pm_runtime_mark_last_busy(dev
->dev
);
894 pm_runtime_put_autosuspend(dev
->dev
);
899 pm_runtime_dont_use_autosuspend(dev
->dev
);
900 pm_runtime_put_sync(dev
->dev
);
901 pm_runtime_disable(dev
->dev
);
906 static int davinci_i2c_remove(struct platform_device
*pdev
)
908 struct davinci_i2c_dev
*dev
= platform_get_drvdata(pdev
);
911 i2c_davinci_cpufreq_deregister(dev
);
913 i2c_del_adapter(&dev
->adapter
);
915 ret
= pm_runtime_get_sync(&pdev
->dev
);
917 pm_runtime_put_noidle(&pdev
->dev
);
921 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, 0);
923 pm_runtime_dont_use_autosuspend(dev
->dev
);
924 pm_runtime_put_sync(dev
->dev
);
925 pm_runtime_disable(dev
->dev
);
931 static int davinci_i2c_suspend(struct device
*dev
)
933 struct davinci_i2c_dev
*i2c_dev
= dev_get_drvdata(dev
);
935 /* put I2C into reset */
936 davinci_i2c_reset_ctrl(i2c_dev
, 0);
941 static int davinci_i2c_resume(struct device
*dev
)
943 struct davinci_i2c_dev
*i2c_dev
= dev_get_drvdata(dev
);
945 /* take I2C out of reset */
946 davinci_i2c_reset_ctrl(i2c_dev
, 1);
951 static const struct dev_pm_ops davinci_i2c_pm
= {
952 .suspend
= davinci_i2c_suspend
,
953 .resume
= davinci_i2c_resume
,
954 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
955 pm_runtime_force_resume
)
958 #define davinci_i2c_pm_ops (&davinci_i2c_pm)
960 #define davinci_i2c_pm_ops NULL
963 /* work with hotplug and coldplug */
964 MODULE_ALIAS("platform:i2c_davinci");
966 static struct platform_driver davinci_i2c_driver
= {
967 .probe
= davinci_i2c_probe
,
968 .remove
= davinci_i2c_remove
,
970 .name
= "i2c_davinci",
971 .pm
= davinci_i2c_pm_ops
,
972 .of_match_table
= davinci_i2c_of_match
,
976 /* I2C may be needed to bring up other drivers */
977 static int __init
davinci_i2c_init_driver(void)
979 return platform_driver_register(&davinci_i2c_driver
);
981 subsys_initcall(davinci_i2c_init_driver
);
983 static void __exit
davinci_i2c_exit_driver(void)
985 platform_driver_unregister(&davinci_i2c_driver
);
987 module_exit(davinci_i2c_exit_driver
);
989 MODULE_AUTHOR("Texas Instruments India");
990 MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
991 MODULE_LICENSE("GPL");