2 * Copyright (C) 2011 NXP Semiconductors
4 * Code portions referenced from the i2x-pxa and i2c-pnx drivers
6 * Make SMBus byte and word transactions work on LPC178x/7x
8 * Alexander Potashev, Emcraft Systems, aspotashev@emcraft.com
9 * Anton Protopopov, Emcraft Systems, antonp@emcraft.com
11 * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
20 #include <linux/clk.h>
21 #include <linux/errno.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
28 #include <linux/of_device.h>
29 #include <linux/platform_device.h>
30 #include <linux/sched.h>
31 #include <linux/time.h>
33 /* LPC24xx register offsets and bits */
34 #define LPC24XX_I2CONSET 0x00
35 #define LPC24XX_I2STAT 0x04
36 #define LPC24XX_I2DAT 0x08
37 #define LPC24XX_I2ADDR 0x0c
38 #define LPC24XX_I2SCLH 0x10
39 #define LPC24XX_I2SCLL 0x14
40 #define LPC24XX_I2CONCLR 0x18
42 #define LPC24XX_AA BIT(2)
43 #define LPC24XX_SI BIT(3)
44 #define LPC24XX_STO BIT(4)
45 #define LPC24XX_STA BIT(5)
46 #define LPC24XX_I2EN BIT(6)
48 #define LPC24XX_STO_AA (LPC24XX_STO | LPC24XX_AA)
49 #define LPC24XX_CLEAR_ALL (LPC24XX_AA | LPC24XX_SI | LPC24XX_STO | \
50 LPC24XX_STA | LPC24XX_I2EN)
52 /* I2C SCL clock has different duty cycle depending on mode */
53 #define I2C_STD_MODE_DUTY 46
54 #define I2C_FAST_MODE_DUTY 36
55 #define I2C_FAST_MODE_PLUS_DUTY 38
58 * 26 possible I2C status codes, but codes applicable only
59 * to master are listed here and used in this driver
66 MX_ADDR_W_NACK
= 0x20,
68 MX_DATA_W_NACK
= 0x30,
69 M_DATA_ARB_LOST
= 0x38,
71 MR_ADDR_R_NACK
= 0x48,
73 MR_DATA_R_NACK
= 0x58,
81 wait_queue_head_t wait
;
82 struct i2c_adapter adap
;
89 static void i2c_lpc2k_reset(struct lpc2k_i2c
*i2c
)
91 /* Will force clear all statuses */
92 writel(LPC24XX_CLEAR_ALL
, i2c
->base
+ LPC24XX_I2CONCLR
);
93 writel(0, i2c
->base
+ LPC24XX_I2ADDR
);
94 writel(LPC24XX_I2EN
, i2c
->base
+ LPC24XX_I2CONSET
);
97 static int i2c_lpc2k_clear_arb(struct lpc2k_i2c
*i2c
)
99 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1000);
102 * If the transfer needs to abort for some reason, we'll try to
103 * force a stop condition to clear any pending bus conditions
105 writel(LPC24XX_STO
, i2c
->base
+ LPC24XX_I2CONSET
);
107 /* Wait for status change */
108 while (readl(i2c
->base
+ LPC24XX_I2STAT
) != M_I2C_IDLE
) {
109 if (time_after(jiffies
, timeout
)) {
110 /* Bus was not idle, try to reset adapter */
111 i2c_lpc2k_reset(i2c
);
121 static void i2c_lpc2k_pump_msg(struct lpc2k_i2c
*i2c
)
127 * I2C in the LPC2xxx series is basically a state machine.
128 * Just run through the steps based on the current status.
130 status
= readl(i2c
->base
+ LPC24XX_I2STAT
);
135 /* Start bit was just sent out, send out addr and dir */
136 data
= i2c_8bit_addr_from_msg(i2c
->msg
);
138 writel(data
, i2c
->base
+ LPC24XX_I2DAT
);
139 writel(LPC24XX_STA
, i2c
->base
+ LPC24XX_I2CONCLR
);
145 * Address or data was sent out with an ACK. If there is more
146 * data to send, send it now
148 if (i2c
->msg_idx
< i2c
->msg
->len
) {
149 writel(i2c
->msg
->buf
[i2c
->msg_idx
],
150 i2c
->base
+ LPC24XX_I2DAT
);
151 } else if (i2c
->is_last
) {
152 /* Last message, send stop */
153 writel(LPC24XX_STO_AA
, i2c
->base
+ LPC24XX_I2CONSET
);
154 writel(LPC24XX_SI
, i2c
->base
+ LPC24XX_I2CONCLR
);
156 disable_irq_nosync(i2c
->irq
);
159 disable_irq_nosync(i2c
->irq
);
166 /* Receive first byte from slave */
167 if (i2c
->msg
->len
== 1) {
168 /* Last byte, return NACK */
169 writel(LPC24XX_AA
, i2c
->base
+ LPC24XX_I2CONCLR
);
171 /* Not last byte, return ACK */
172 writel(LPC24XX_AA
, i2c
->base
+ LPC24XX_I2CONSET
);
175 writel(LPC24XX_STA
, i2c
->base
+ LPC24XX_I2CONCLR
);
180 * The I2C shows NACK status on reads, so we need to accept
181 * the NACK as an ACK here. This should be ok, as the real
182 * BACK would of been caught on the address write.
185 /* Data was received */
186 if (i2c
->msg_idx
< i2c
->msg
->len
) {
187 i2c
->msg
->buf
[i2c
->msg_idx
] =
188 readl(i2c
->base
+ LPC24XX_I2DAT
);
191 /* If transfer is done, send STOP */
192 if (i2c
->msg_idx
>= i2c
->msg
->len
- 1 && i2c
->is_last
) {
193 writel(LPC24XX_STO_AA
, i2c
->base
+ LPC24XX_I2CONSET
);
194 writel(LPC24XX_SI
, i2c
->base
+ LPC24XX_I2CONCLR
);
198 /* Message is done */
199 if (i2c
->msg_idx
>= i2c
->msg
->len
- 1) {
201 disable_irq_nosync(i2c
->irq
);
205 * One pre-last data input, send NACK to tell the slave that
206 * this is going to be the last data byte to be transferred.
208 if (i2c
->msg_idx
>= i2c
->msg
->len
- 2) {
209 /* One byte left to receive - NACK */
210 writel(LPC24XX_AA
, i2c
->base
+ LPC24XX_I2CONCLR
);
212 /* More than one byte left to receive - ACK */
213 writel(LPC24XX_AA
, i2c
->base
+ LPC24XX_I2CONSET
);
216 writel(LPC24XX_STA
, i2c
->base
+ LPC24XX_I2CONCLR
);
223 /* NACK processing is done */
224 writel(LPC24XX_STO_AA
, i2c
->base
+ LPC24XX_I2CONSET
);
225 i2c
->msg_status
= -ENXIO
;
226 disable_irq_nosync(i2c
->irq
);
229 case M_DATA_ARB_LOST
:
230 /* Arbitration lost */
231 i2c
->msg_status
= -EAGAIN
;
233 /* Release the I2C bus */
234 writel(LPC24XX_STA
| LPC24XX_STO
, i2c
->base
+ LPC24XX_I2CONCLR
);
235 disable_irq_nosync(i2c
->irq
);
239 /* Unexpected statuses */
240 i2c
->msg_status
= -EIO
;
241 disable_irq_nosync(i2c
->irq
);
245 /* Exit on failure or all bytes transferred */
246 if (i2c
->msg_status
!= -EBUSY
)
250 * If `msg_status` is zero, then `lpc2k_process_msg()`
251 * is responsible for clearing the SI flag.
253 if (i2c
->msg_status
!= 0)
254 writel(LPC24XX_SI
, i2c
->base
+ LPC24XX_I2CONCLR
);
257 static int lpc2k_process_msg(struct lpc2k_i2c
*i2c
, int msgidx
)
259 /* A new transfer is kicked off by initiating a start condition */
261 writel(LPC24XX_STA
, i2c
->base
+ LPC24XX_I2CONSET
);
264 * A multi-message I2C transfer continues where the
265 * previous I2C transfer left off and uses the
266 * current condition of the I2C adapter.
268 if (unlikely(i2c
->msg
->flags
& I2C_M_NOSTART
)) {
269 WARN_ON(i2c
->msg
->len
== 0);
271 if (!(i2c
->msg
->flags
& I2C_M_RD
)) {
272 /* Start transmit of data */
273 writel(i2c
->msg
->buf
[0],
274 i2c
->base
+ LPC24XX_I2DAT
);
278 /* Start or repeated start */
279 writel(LPC24XX_STA
, i2c
->base
+ LPC24XX_I2CONSET
);
282 writel(LPC24XX_SI
, i2c
->base
+ LPC24XX_I2CONCLR
);
285 enable_irq(i2c
->irq
);
287 /* Wait for transfer completion */
288 if (wait_event_timeout(i2c
->wait
, i2c
->msg_status
!= -EBUSY
,
289 msecs_to_jiffies(1000)) == 0) {
290 disable_irq_nosync(i2c
->irq
);
295 return i2c
->msg_status
;
298 static int i2c_lpc2k_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
301 struct lpc2k_i2c
*i2c
= i2c_get_adapdata(adap
);
305 /* Check for bus idle condition */
306 stat
= readl(i2c
->base
+ LPC24XX_I2STAT
);
307 if (stat
!= M_I2C_IDLE
) {
308 /* Something is holding the bus, try to clear it */
309 return i2c_lpc2k_clear_arb(i2c
);
312 /* Process a single message at a time */
313 for (i
= 0; i
< msg_num
; i
++) {
314 /* Save message pointer and current message data index */
317 i2c
->msg_status
= -EBUSY
;
318 i2c
->is_last
= (i
== (msg_num
- 1));
320 ret
= lpc2k_process_msg(i2c
, i
);
328 static irqreturn_t
i2c_lpc2k_handler(int irq
, void *dev_id
)
330 struct lpc2k_i2c
*i2c
= dev_id
;
332 if (readl(i2c
->base
+ LPC24XX_I2CONSET
) & LPC24XX_SI
) {
333 i2c_lpc2k_pump_msg(i2c
);
340 static u32
i2c_lpc2k_functionality(struct i2c_adapter
*adap
)
342 /* Only emulated SMBus for now */
343 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
346 static const struct i2c_algorithm i2c_lpc2k_algorithm
= {
347 .master_xfer
= i2c_lpc2k_xfer
,
348 .functionality
= i2c_lpc2k_functionality
,
351 static int i2c_lpc2k_probe(struct platform_device
*pdev
)
353 struct lpc2k_i2c
*i2c
;
354 struct resource
*res
;
360 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
364 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
365 i2c
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
366 if (IS_ERR(i2c
->base
))
367 return PTR_ERR(i2c
->base
);
369 i2c
->irq
= platform_get_irq(pdev
, 0);
371 dev_err(&pdev
->dev
, "can't get interrupt resource\n");
375 init_waitqueue_head(&i2c
->wait
);
377 i2c
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
378 if (IS_ERR(i2c
->clk
)) {
379 dev_err(&pdev
->dev
, "error getting clock\n");
380 return PTR_ERR(i2c
->clk
);
383 ret
= clk_prepare_enable(i2c
->clk
);
385 dev_err(&pdev
->dev
, "unable to enable clock.\n");
389 ret
= devm_request_irq(&pdev
->dev
, i2c
->irq
, i2c_lpc2k_handler
, 0,
390 dev_name(&pdev
->dev
), i2c
);
392 dev_err(&pdev
->dev
, "can't request interrupt.\n");
396 disable_irq_nosync(i2c
->irq
);
398 /* Place controller is a known state */
399 i2c_lpc2k_reset(i2c
);
401 ret
= of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
404 bus_clk_rate
= 100000; /* 100 kHz default clock rate */
406 clkrate
= clk_get_rate(i2c
->clk
);
408 dev_err(&pdev
->dev
, "can't get I2C base clock\n");
413 /* Setup I2C dividers to generate clock with proper duty cycle */
414 clkrate
= clkrate
/ bus_clk_rate
;
415 if (bus_clk_rate
<= 100000)
416 scl_high
= (clkrate
* I2C_STD_MODE_DUTY
) / 100;
417 else if (bus_clk_rate
<= 400000)
418 scl_high
= (clkrate
* I2C_FAST_MODE_DUTY
) / 100;
420 scl_high
= (clkrate
* I2C_FAST_MODE_PLUS_DUTY
) / 100;
422 writel(scl_high
, i2c
->base
+ LPC24XX_I2SCLH
);
423 writel(clkrate
- scl_high
, i2c
->base
+ LPC24XX_I2SCLL
);
425 platform_set_drvdata(pdev
, i2c
);
427 i2c_set_adapdata(&i2c
->adap
, i2c
);
428 i2c
->adap
.owner
= THIS_MODULE
;
429 strlcpy(i2c
->adap
.name
, "LPC2K I2C adapter", sizeof(i2c
->adap
.name
));
430 i2c
->adap
.algo
= &i2c_lpc2k_algorithm
;
431 i2c
->adap
.dev
.parent
= &pdev
->dev
;
432 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
434 ret
= i2c_add_adapter(&i2c
->adap
);
436 dev_err(&pdev
->dev
, "failed to add adapter!\n");
440 dev_info(&pdev
->dev
, "LPC2K I2C adapter\n");
445 clk_disable_unprepare(i2c
->clk
);
449 static int i2c_lpc2k_remove(struct platform_device
*dev
)
451 struct lpc2k_i2c
*i2c
= platform_get_drvdata(dev
);
453 i2c_del_adapter(&i2c
->adap
);
454 clk_disable_unprepare(i2c
->clk
);
460 static int i2c_lpc2k_suspend(struct device
*dev
)
462 struct platform_device
*pdev
= to_platform_device(dev
);
463 struct lpc2k_i2c
*i2c
= platform_get_drvdata(pdev
);
465 clk_disable(i2c
->clk
);
470 static int i2c_lpc2k_resume(struct device
*dev
)
472 struct platform_device
*pdev
= to_platform_device(dev
);
473 struct lpc2k_i2c
*i2c
= platform_get_drvdata(pdev
);
475 clk_enable(i2c
->clk
);
476 i2c_lpc2k_reset(i2c
);
481 static const struct dev_pm_ops i2c_lpc2k_dev_pm_ops
= {
482 .suspend_noirq
= i2c_lpc2k_suspend
,
483 .resume_noirq
= i2c_lpc2k_resume
,
486 #define I2C_LPC2K_DEV_PM_OPS (&i2c_lpc2k_dev_pm_ops)
488 #define I2C_LPC2K_DEV_PM_OPS NULL
491 static const struct of_device_id lpc2k_i2c_match
[] = {
492 { .compatible
= "nxp,lpc1788-i2c" },
495 MODULE_DEVICE_TABLE(of
, lpc2k_i2c_match
);
497 static struct platform_driver i2c_lpc2k_driver
= {
498 .probe
= i2c_lpc2k_probe
,
499 .remove
= i2c_lpc2k_remove
,
502 .pm
= I2C_LPC2K_DEV_PM_OPS
,
503 .of_match_table
= lpc2k_i2c_match
,
506 module_platform_driver(i2c_lpc2k_driver
);
508 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
509 MODULE_DESCRIPTION("I2C driver for LPC2xxx devices");
510 MODULE_LICENSE("GPL");
511 MODULE_ALIAS("platform:lpc2k-i2c");