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.
241 if (clk
>= clkl
+ d
) {
242 clkh
= clk
- clkl
- d
;
246 clkl
= clk
- (d
<< 1);
249 davinci_i2c_write_reg(dev
, DAVINCI_I2C_PSC_REG
, psc
);
250 davinci_i2c_write_reg(dev
, DAVINCI_I2C_CLKH_REG
, clkh
);
251 davinci_i2c_write_reg(dev
, DAVINCI_I2C_CLKL_REG
, clkl
);
253 dev_dbg(dev
->dev
, "input_clock = %d, CLK = %d\n", input_clock
, clk
);
257 * This function configures I2C and brings I2C out of reset.
258 * This function is called during I2C init function. This function
259 * also gets called if I2C encounters any errors.
261 static int i2c_davinci_init(struct davinci_i2c_dev
*dev
)
263 struct davinci_i2c_platform_data
*pdata
= dev
->pdata
;
265 /* put I2C into reset */
266 davinci_i2c_reset_ctrl(dev
, 0);
268 /* compute clock dividers */
269 i2c_davinci_calc_clk_dividers(dev
);
271 /* Respond at reserved "SMBus Host" slave address" (and zero);
272 * we seem to have no option to not respond...
274 davinci_i2c_write_reg(dev
, DAVINCI_I2C_OAR_REG
, DAVINCI_I2C_OWN_ADDRESS
);
276 dev_dbg(dev
->dev
, "PSC = %d\n",
277 davinci_i2c_read_reg(dev
, DAVINCI_I2C_PSC_REG
));
278 dev_dbg(dev
->dev
, "CLKL = %d\n",
279 davinci_i2c_read_reg(dev
, DAVINCI_I2C_CLKL_REG
));
280 dev_dbg(dev
->dev
, "CLKH = %d\n",
281 davinci_i2c_read_reg(dev
, DAVINCI_I2C_CLKH_REG
));
282 dev_dbg(dev
->dev
, "bus_freq = %dkHz, bus_delay = %d\n",
283 pdata
->bus_freq
, pdata
->bus_delay
);
286 /* Take the I2C module out of reset: */
287 davinci_i2c_reset_ctrl(dev
, 1);
289 /* Enable interrupts */
290 davinci_i2c_write_reg(dev
, DAVINCI_I2C_IMR_REG
, I2C_DAVINCI_INTR_ALL
);
296 * This routine does i2c bus recovery by using i2c_generic_scl_recovery
297 * which is provided by I2C Bus recovery infrastructure.
299 static void davinci_i2c_prepare_recovery(struct i2c_adapter
*adap
)
301 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
303 /* Disable interrupts */
304 davinci_i2c_write_reg(dev
, DAVINCI_I2C_IMR_REG
, 0);
306 /* put I2C into reset */
307 davinci_i2c_reset_ctrl(dev
, 0);
310 static void davinci_i2c_unprepare_recovery(struct i2c_adapter
*adap
)
312 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
314 i2c_davinci_init(dev
);
317 static struct i2c_bus_recovery_info davinci_i2c_gpio_recovery_info
= {
318 .recover_bus
= i2c_generic_scl_recovery
,
319 .prepare_recovery
= davinci_i2c_prepare_recovery
,
320 .unprepare_recovery
= davinci_i2c_unprepare_recovery
,
323 static void davinci_i2c_set_scl(struct i2c_adapter
*adap
, int val
)
325 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
328 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DSET_REG
,
329 DAVINCI_I2C_DSET_PDSET0
);
331 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DCLR_REG
,
332 DAVINCI_I2C_DCLR_PDCLR0
);
335 static int davinci_i2c_get_scl(struct i2c_adapter
*adap
)
337 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
340 /* read the state of SCL */
341 val
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_DIN_REG
);
342 return val
& DAVINCI_I2C_DIN_PDIN0
;
345 static int davinci_i2c_get_sda(struct i2c_adapter
*adap
)
347 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
350 /* read the state of SDA */
351 val
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_DIN_REG
);
352 return val
& DAVINCI_I2C_DIN_PDIN1
;
355 static void davinci_i2c_scl_prepare_recovery(struct i2c_adapter
*adap
)
357 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
359 davinci_i2c_prepare_recovery(adap
);
361 /* SCL output, SDA input */
362 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DIR_REG
, DAVINCI_I2C_DIR_PDIR0
);
364 /* change to GPIO mode */
365 davinci_i2c_write_reg(dev
, DAVINCI_I2C_FUNC_REG
,
366 DAVINCI_I2C_FUNC_PFUNC0
);
369 static void davinci_i2c_scl_unprepare_recovery(struct i2c_adapter
*adap
)
371 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
373 /* change back to I2C mode */
374 davinci_i2c_write_reg(dev
, DAVINCI_I2C_FUNC_REG
, 0);
376 davinci_i2c_unprepare_recovery(adap
);
379 static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info
= {
380 .recover_bus
= i2c_generic_scl_recovery
,
381 .set_scl
= davinci_i2c_set_scl
,
382 .get_scl
= davinci_i2c_get_scl
,
383 .get_sda
= davinci_i2c_get_sda
,
384 .prepare_recovery
= davinci_i2c_scl_prepare_recovery
,
385 .unprepare_recovery
= davinci_i2c_scl_unprepare_recovery
,
389 * Waiting for bus not busy
391 static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev
*dev
)
393 unsigned long timeout
= jiffies
+ dev
->adapter
.timeout
;
396 if (!(davinci_i2c_read_reg(dev
, DAVINCI_I2C_STR_REG
) & DAVINCI_I2C_STR_BB
))
398 schedule_timeout_uninterruptible(1);
399 } while (time_before_eq(jiffies
, timeout
));
401 dev_warn(dev
->dev
, "timeout waiting for bus ready\n");
402 i2c_recover_bus(&dev
->adapter
);
405 * if bus is still "busy" here, it's most probably a HW problem like
408 if (davinci_i2c_read_reg(dev
, DAVINCI_I2C_STR_REG
) & DAVINCI_I2C_STR_BB
)
415 * Low level master read/write transaction. This function is called
416 * from i2c_davinci_xfer.
419 i2c_davinci_xfer_msg(struct i2c_adapter
*adap
, struct i2c_msg
*msg
, int stop
)
421 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
422 struct davinci_i2c_platform_data
*pdata
= dev
->pdata
;
425 unsigned long time_left
;
427 if (msg
->addr
== DAVINCI_I2C_OWN_ADDRESS
) {
428 dev_warn(dev
->dev
, "transfer to own address aborted\n");
429 return -EADDRNOTAVAIL
;
432 /* Introduce a delay, required for some boards (e.g Davinci EVM) */
433 if (pdata
->bus_delay
)
434 udelay(pdata
->bus_delay
);
436 /* set the slave address */
437 davinci_i2c_write_reg(dev
, DAVINCI_I2C_SAR_REG
, msg
->addr
);
440 dev
->buf_len
= msg
->len
;
443 davinci_i2c_write_reg(dev
, DAVINCI_I2C_CNT_REG
, dev
->buf_len
);
445 reinit_completion(&dev
->cmd_complete
);
448 /* Take I2C out of reset and configure it as master */
449 flag
= DAVINCI_I2C_MDR_IRS
| DAVINCI_I2C_MDR_MST
;
451 /* if the slave address is ten bit address, enable XA bit */
452 if (msg
->flags
& I2C_M_TEN
)
453 flag
|= DAVINCI_I2C_MDR_XA
;
454 if (!(msg
->flags
& I2C_M_RD
))
455 flag
|= DAVINCI_I2C_MDR_TRX
;
457 flag
|= DAVINCI_I2C_MDR_RM
;
459 /* Enable receive or transmit interrupts */
460 w
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_IMR_REG
);
461 if (msg
->flags
& I2C_M_RD
)
462 w
|= DAVINCI_I2C_IMR_RRDY
;
464 w
|= DAVINCI_I2C_IMR_XRDY
;
465 davinci_i2c_write_reg(dev
, DAVINCI_I2C_IMR_REG
, w
);
470 * Write mode register first as needed for correct behaviour
471 * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
472 * occurring before we have loaded DXR
474 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, flag
);
477 * First byte should be set here, not after interrupt,
478 * because transmit-data-ready interrupt can come before
479 * NACK-interrupt during sending of previous message and
480 * ICDXR may have wrong data
481 * It also saves us one interrupt, slightly faster
483 if ((!(msg
->flags
& I2C_M_RD
)) && dev
->buf_len
) {
484 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DXR_REG
, *dev
->buf
++);
488 /* Set STT to begin transmit now DXR is loaded */
489 flag
|= DAVINCI_I2C_MDR_STT
;
490 if (stop
&& msg
->len
!= 0)
491 flag
|= DAVINCI_I2C_MDR_STP
;
492 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, flag
);
494 time_left
= wait_for_completion_timeout(&dev
->cmd_complete
,
495 dev
->adapter
.timeout
);
497 dev_err(dev
->dev
, "controller timed out\n");
498 i2c_recover_bus(adap
);
503 /* This should be 0 if all bytes were transferred
504 * or dev->cmd_err denotes an error.
506 dev_err(dev
->dev
, "abnormal termination buf_len=%zu\n",
515 if (likely(!dev
->cmd_err
))
518 /* We have an error */
519 if (dev
->cmd_err
& DAVINCI_I2C_STR_AL
) {
520 i2c_davinci_init(dev
);
524 if (dev
->cmd_err
& DAVINCI_I2C_STR_NACK
) {
525 if (msg
->flags
& I2C_M_IGNORE_NAK
)
527 w
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_MDR_REG
);
528 w
|= DAVINCI_I2C_MDR_STP
;
529 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, w
);
536 * Prepare controller for a transaction and call i2c_davinci_xfer_msg
539 i2c_davinci_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
541 struct davinci_i2c_dev
*dev
= i2c_get_adapdata(adap
);
545 dev_dbg(dev
->dev
, "%s: msgs: %d\n", __func__
, num
);
547 ret
= pm_runtime_get_sync(dev
->dev
);
549 dev_err(dev
->dev
, "Failed to runtime_get device: %d\n", ret
);
550 pm_runtime_put_noidle(dev
->dev
);
554 ret
= i2c_davinci_wait_bus_not_busy(dev
);
556 dev_warn(dev
->dev
, "timeout waiting for bus ready\n");
560 for (i
= 0; i
< num
; i
++) {
561 ret
= i2c_davinci_xfer_msg(adap
, &msgs
[i
], (i
== (num
- 1)));
562 dev_dbg(dev
->dev
, "%s [%d/%d] ret: %d\n", __func__
, i
+ 1, num
,
571 pm_runtime_mark_last_busy(dev
->dev
);
572 pm_runtime_put_autosuspend(dev
->dev
);
577 static u32
i2c_davinci_func(struct i2c_adapter
*adap
)
579 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
582 static void terminate_read(struct davinci_i2c_dev
*dev
)
584 u16 w
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_MDR_REG
);
585 w
|= DAVINCI_I2C_MDR_NACK
;
586 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, w
);
588 /* Throw away data */
589 davinci_i2c_read_reg(dev
, DAVINCI_I2C_DRR_REG
);
591 dev_err(dev
->dev
, "RDR IRQ while no data requested\n");
593 static void terminate_write(struct davinci_i2c_dev
*dev
)
595 u16 w
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_MDR_REG
);
596 w
|= DAVINCI_I2C_MDR_RM
| DAVINCI_I2C_MDR_STP
;
597 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, w
);
600 dev_dbg(dev
->dev
, "TDR IRQ while no data to send\n");
604 * Interrupt service routine. This gets called whenever an I2C interrupt
607 static irqreturn_t
i2c_davinci_isr(int this_irq
, void *dev_id
)
609 struct davinci_i2c_dev
*dev
= dev_id
;
614 if (pm_runtime_suspended(dev
->dev
))
617 while ((stat
= davinci_i2c_read_reg(dev
, DAVINCI_I2C_IVR_REG
))) {
618 dev_dbg(dev
->dev
, "%s: stat=0x%x\n", __func__
, stat
);
619 if (count
++ == 100) {
620 dev_warn(dev
->dev
, "Too much work in one IRQ\n");
625 case DAVINCI_I2C_IVR_AL
:
626 /* Arbitration lost, must retry */
627 dev
->cmd_err
|= DAVINCI_I2C_STR_AL
;
629 complete(&dev
->cmd_complete
);
632 case DAVINCI_I2C_IVR_NACK
:
633 dev
->cmd_err
|= DAVINCI_I2C_STR_NACK
;
635 complete(&dev
->cmd_complete
);
638 case DAVINCI_I2C_IVR_ARDY
:
639 davinci_i2c_write_reg(dev
,
640 DAVINCI_I2C_STR_REG
, DAVINCI_I2C_STR_ARDY
);
641 if (((dev
->buf_len
== 0) && (dev
->stop
!= 0)) ||
642 (dev
->cmd_err
& DAVINCI_I2C_STR_NACK
)) {
643 w
= davinci_i2c_read_reg(dev
,
644 DAVINCI_I2C_MDR_REG
);
645 w
|= DAVINCI_I2C_MDR_STP
;
646 davinci_i2c_write_reg(dev
,
647 DAVINCI_I2C_MDR_REG
, w
);
649 complete(&dev
->cmd_complete
);
652 case DAVINCI_I2C_IVR_RDR
:
655 davinci_i2c_read_reg(dev
,
656 DAVINCI_I2C_DRR_REG
);
661 davinci_i2c_write_reg(dev
,
663 DAVINCI_I2C_IMR_RRDY
);
665 /* signal can terminate transfer */
670 case DAVINCI_I2C_IVR_XRDY
:
672 davinci_i2c_write_reg(dev
, DAVINCI_I2C_DXR_REG
,
678 w
= davinci_i2c_read_reg(dev
,
679 DAVINCI_I2C_IMR_REG
);
680 w
&= ~DAVINCI_I2C_IMR_XRDY
;
681 davinci_i2c_write_reg(dev
,
685 /* signal can terminate transfer */
686 terminate_write(dev
);
690 case DAVINCI_I2C_IVR_SCD
:
691 davinci_i2c_write_reg(dev
,
692 DAVINCI_I2C_STR_REG
, DAVINCI_I2C_STR_SCD
);
693 complete(&dev
->cmd_complete
);
696 case DAVINCI_I2C_IVR_AAS
:
697 dev_dbg(dev
->dev
, "Address as slave interrupt\n");
701 dev_warn(dev
->dev
, "Unrecognized irq stat %d\n", stat
);
706 return count
? IRQ_HANDLED
: IRQ_NONE
;
709 #ifdef CONFIG_CPU_FREQ
710 static int i2c_davinci_cpufreq_transition(struct notifier_block
*nb
,
711 unsigned long val
, void *data
)
713 struct davinci_i2c_dev
*dev
;
715 dev
= container_of(nb
, struct davinci_i2c_dev
, freq_transition
);
717 i2c_lock_adapter(&dev
->adapter
);
718 if (val
== CPUFREQ_PRECHANGE
) {
719 davinci_i2c_reset_ctrl(dev
, 0);
720 } else if (val
== CPUFREQ_POSTCHANGE
) {
721 i2c_davinci_calc_clk_dividers(dev
);
722 davinci_i2c_reset_ctrl(dev
, 1);
724 i2c_unlock_adapter(&dev
->adapter
);
729 static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev
*dev
)
731 dev
->freq_transition
.notifier_call
= i2c_davinci_cpufreq_transition
;
733 return cpufreq_register_notifier(&dev
->freq_transition
,
734 CPUFREQ_TRANSITION_NOTIFIER
);
737 static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev
*dev
)
739 cpufreq_unregister_notifier(&dev
->freq_transition
,
740 CPUFREQ_TRANSITION_NOTIFIER
);
743 static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev
*dev
)
748 static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev
*dev
)
753 static const struct i2c_algorithm i2c_davinci_algo
= {
754 .master_xfer
= i2c_davinci_xfer
,
755 .functionality
= i2c_davinci_func
,
758 static const struct of_device_id davinci_i2c_of_match
[] = {
759 {.compatible
= "ti,davinci-i2c", },
760 {.compatible
= "ti,keystone-i2c", },
763 MODULE_DEVICE_TABLE(of
, davinci_i2c_of_match
);
765 static int davinci_i2c_probe(struct platform_device
*pdev
)
767 struct davinci_i2c_dev
*dev
;
768 struct i2c_adapter
*adap
;
769 struct resource
*mem
;
770 struct i2c_bus_recovery_info
*rinfo
;
773 irq
= platform_get_irq(pdev
, 0);
777 if (irq
!= -EPROBE_DEFER
)
779 "can't get irq resource ret=%d\n", irq
);
783 dev
= devm_kzalloc(&pdev
->dev
, sizeof(struct davinci_i2c_dev
),
786 dev_err(&pdev
->dev
, "Memory allocation failed\n");
790 init_completion(&dev
->cmd_complete
);
792 dev
->dev
= &pdev
->dev
;
794 dev
->pdata
= dev_get_platdata(&pdev
->dev
);
795 platform_set_drvdata(pdev
, dev
);
797 if (!dev
->pdata
&& pdev
->dev
.of_node
) {
800 dev
->pdata
= devm_kzalloc(&pdev
->dev
,
801 sizeof(struct davinci_i2c_platform_data
), GFP_KERNEL
);
805 memcpy(dev
->pdata
, &davinci_i2c_platform_data_default
,
806 sizeof(struct davinci_i2c_platform_data
));
807 if (!of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
809 dev
->pdata
->bus_freq
= prop
/ 1000;
811 dev
->pdata
->has_pfunc
=
812 of_property_read_bool(pdev
->dev
.of_node
,
814 } else if (!dev
->pdata
) {
815 dev
->pdata
= &davinci_i2c_platform_data_default
;
818 dev
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
819 if (IS_ERR(dev
->clk
))
820 return PTR_ERR(dev
->clk
);
822 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
823 dev
->base
= devm_ioremap_resource(&pdev
->dev
, mem
);
824 if (IS_ERR(dev
->base
)) {
825 return PTR_ERR(dev
->base
);
828 pm_runtime_set_autosuspend_delay(dev
->dev
,
829 DAVINCI_I2C_PM_TIMEOUT
);
830 pm_runtime_use_autosuspend(dev
->dev
);
832 pm_runtime_enable(dev
->dev
);
834 r
= pm_runtime_get_sync(dev
->dev
);
836 dev_err(dev
->dev
, "failed to runtime_get device: %d\n", r
);
837 pm_runtime_put_noidle(dev
->dev
);
841 i2c_davinci_init(dev
);
843 r
= devm_request_irq(&pdev
->dev
, dev
->irq
, i2c_davinci_isr
, 0,
846 dev_err(&pdev
->dev
, "failure requesting irq %i\n", dev
->irq
);
847 goto err_unuse_clocks
;
850 r
= i2c_davinci_cpufreq_register(dev
);
852 dev_err(&pdev
->dev
, "failed to register cpufreq\n");
853 goto err_unuse_clocks
;
856 adap
= &dev
->adapter
;
857 i2c_set_adapdata(adap
, dev
);
858 adap
->owner
= THIS_MODULE
;
859 adap
->class = I2C_CLASS_DEPRECATED
;
860 strlcpy(adap
->name
, "DaVinci I2C adapter", sizeof(adap
->name
));
861 adap
->algo
= &i2c_davinci_algo
;
862 adap
->dev
.parent
= &pdev
->dev
;
863 adap
->timeout
= DAVINCI_I2C_TIMEOUT
;
864 adap
->dev
.of_node
= pdev
->dev
.of_node
;
866 if (dev
->pdata
->has_pfunc
)
867 adap
->bus_recovery_info
= &davinci_i2c_scl_recovery_info
;
868 else if (dev
->pdata
->gpio_recovery
) {
869 rinfo
= &davinci_i2c_gpio_recovery_info
;
870 adap
->bus_recovery_info
= rinfo
;
871 rinfo
->scl_gpiod
= devm_gpiod_get(&pdev
->dev
, "scl",
872 GPIOD_OUT_HIGH_OPEN_DRAIN
);
873 if (IS_ERR(rinfo
->scl_gpiod
)) {
874 r
= PTR_ERR(rinfo
->scl_gpiod
);
875 goto err_unuse_clocks
;
877 rinfo
->sda_gpiod
= devm_gpiod_get(&pdev
->dev
, "sda", GPIOD_IN
);
878 if (IS_ERR(rinfo
->sda_gpiod
)) {
879 r
= PTR_ERR(rinfo
->sda_gpiod
);
880 goto err_unuse_clocks
;
885 r
= i2c_add_numbered_adapter(adap
);
887 goto err_unuse_clocks
;
889 pm_runtime_mark_last_busy(dev
->dev
);
890 pm_runtime_put_autosuspend(dev
->dev
);
895 pm_runtime_dont_use_autosuspend(dev
->dev
);
896 pm_runtime_put_sync(dev
->dev
);
897 pm_runtime_disable(dev
->dev
);
902 static int davinci_i2c_remove(struct platform_device
*pdev
)
904 struct davinci_i2c_dev
*dev
= platform_get_drvdata(pdev
);
907 i2c_davinci_cpufreq_deregister(dev
);
909 i2c_del_adapter(&dev
->adapter
);
911 ret
= pm_runtime_get_sync(&pdev
->dev
);
913 pm_runtime_put_noidle(&pdev
->dev
);
917 davinci_i2c_write_reg(dev
, DAVINCI_I2C_MDR_REG
, 0);
919 pm_runtime_dont_use_autosuspend(dev
->dev
);
920 pm_runtime_put_sync(dev
->dev
);
921 pm_runtime_disable(dev
->dev
);
927 static int davinci_i2c_suspend(struct device
*dev
)
929 struct davinci_i2c_dev
*i2c_dev
= dev_get_drvdata(dev
);
931 /* put I2C into reset */
932 davinci_i2c_reset_ctrl(i2c_dev
, 0);
937 static int davinci_i2c_resume(struct device
*dev
)
939 struct davinci_i2c_dev
*i2c_dev
= dev_get_drvdata(dev
);
941 /* take I2C out of reset */
942 davinci_i2c_reset_ctrl(i2c_dev
, 1);
947 static const struct dev_pm_ops davinci_i2c_pm
= {
948 .suspend
= davinci_i2c_suspend
,
949 .resume
= davinci_i2c_resume
,
950 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
951 pm_runtime_force_resume
)
954 #define davinci_i2c_pm_ops (&davinci_i2c_pm)
956 #define davinci_i2c_pm_ops NULL
959 /* work with hotplug and coldplug */
960 MODULE_ALIAS("platform:i2c_davinci");
962 static struct platform_driver davinci_i2c_driver
= {
963 .probe
= davinci_i2c_probe
,
964 .remove
= davinci_i2c_remove
,
966 .name
= "i2c_davinci",
967 .pm
= davinci_i2c_pm_ops
,
968 .of_match_table
= davinci_i2c_of_match
,
972 /* I2C may be needed to bring up other drivers */
973 static int __init
davinci_i2c_init_driver(void)
975 return platform_driver_register(&davinci_i2c_driver
);
977 subsys_initcall(davinci_i2c_init_driver
);
979 static void __exit
davinci_i2c_exit_driver(void)
981 platform_driver_unregister(&davinci_i2c_driver
);
983 module_exit(davinci_i2c_exit_driver
);
985 MODULE_AUTHOR("Texas Instruments India");
986 MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
987 MODULE_LICENSE("GPL");