Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
[linux/fpc-iii.git] / drivers / i2c / busses / i2c-lpc2k.c
blob586a15205e61ded5757ef01e107d96a0a2915e01
1 /*
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
7 * Copyright (c) 2012
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>
24 #include <linux/io.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/of.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
61 enum {
62 M_BUS_ERROR = 0x00,
63 M_START = 0x08,
64 M_REPSTART = 0x10,
65 MX_ADDR_W_ACK = 0x18,
66 MX_ADDR_W_NACK = 0x20,
67 MX_DATA_W_ACK = 0x28,
68 MX_DATA_W_NACK = 0x30,
69 M_DATA_ARB_LOST = 0x38,
70 MR_ADDR_R_ACK = 0x40,
71 MR_ADDR_R_NACK = 0x48,
72 MR_DATA_R_ACK = 0x50,
73 MR_DATA_R_NACK = 0x58,
74 M_I2C_IDLE = 0xf8,
77 struct lpc2k_i2c {
78 void __iomem *base;
79 struct clk *clk;
80 int irq;
81 wait_queue_head_t wait;
82 struct i2c_adapter adap;
83 struct i2c_msg *msg;
84 int msg_idx;
85 int msg_status;
86 int is_last;
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);
112 return -EBUSY;
115 cpu_relax();
118 return 0;
121 static void i2c_lpc2k_pump_msg(struct lpc2k_i2c *i2c)
123 unsigned char data;
124 u32 status;
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);
132 switch (status) {
133 case M_START:
134 case M_REPSTART:
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);
140 break;
142 case MX_ADDR_W_ACK:
143 case MX_DATA_W_ACK:
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);
155 i2c->msg_status = 0;
156 disable_irq_nosync(i2c->irq);
157 } else {
158 i2c->msg_status = 0;
159 disable_irq_nosync(i2c->irq);
162 i2c->msg_idx++;
163 break;
165 case MR_ADDR_R_ACK:
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);
170 } else {
171 /* Not last byte, return ACK */
172 writel(LPC24XX_AA, i2c->base + LPC24XX_I2CONSET);
175 writel(LPC24XX_STA, i2c->base + LPC24XX_I2CONCLR);
176 break;
178 case MR_DATA_R_NACK:
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.
184 case MR_DATA_R_ACK:
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);
195 i2c->msg_status = 0;
198 /* Message is done */
199 if (i2c->msg_idx >= i2c->msg->len - 1) {
200 i2c->msg_status = 0;
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);
211 } else {
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);
217 i2c->msg_idx++;
218 break;
220 case MX_ADDR_W_NACK:
221 case MX_DATA_W_NACK:
222 case MR_ADDR_R_NACK:
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);
227 break;
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);
236 break;
238 default:
239 /* Unexpected statuses */
240 i2c->msg_status = -EIO;
241 disable_irq_nosync(i2c->irq);
242 break;
245 /* Exit on failure or all bytes transferred */
246 if (i2c->msg_status != -EBUSY)
247 wake_up(&i2c->wait);
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 */
260 if (!msgidx) {
261 writel(LPC24XX_STA, i2c->base + LPC24XX_I2CONSET);
262 } else {
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);
275 i2c->msg_idx++;
277 } else {
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);
292 return -ETIMEDOUT;
295 return i2c->msg_status;
298 static int i2c_lpc2k_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
299 int msg_num)
301 struct lpc2k_i2c *i2c = i2c_get_adapdata(adap);
302 int ret, i;
303 u32 stat;
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 */
315 i2c->msg = &msgs[i];
316 i2c->msg_idx = 0;
317 i2c->msg_status = -EBUSY;
318 i2c->is_last = (i == (msg_num - 1));
320 ret = lpc2k_process_msg(i2c, i);
321 if (ret)
322 return ret;
325 return msg_num;
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);
334 return IRQ_HANDLED;
337 return IRQ_NONE;
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;
355 u32 bus_clk_rate;
356 u32 scl_high;
357 u32 clkrate;
358 int ret;
360 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
361 if (!i2c)
362 return -ENOMEM;
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);
370 if (i2c->irq < 0) {
371 dev_err(&pdev->dev, "can't get interrupt resource\n");
372 return i2c->irq;
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);
384 if (ret) {
385 dev_err(&pdev->dev, "unable to enable clock.\n");
386 return ret;
389 ret = devm_request_irq(&pdev->dev, i2c->irq, i2c_lpc2k_handler, 0,
390 dev_name(&pdev->dev), i2c);
391 if (ret < 0) {
392 dev_err(&pdev->dev, "can't request interrupt.\n");
393 goto fail_clk;
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",
402 &bus_clk_rate);
403 if (ret)
404 bus_clk_rate = 100000; /* 100 kHz default clock rate */
406 clkrate = clk_get_rate(i2c->clk);
407 if (clkrate == 0) {
408 dev_err(&pdev->dev, "can't get I2C base clock\n");
409 ret = -EINVAL;
410 goto fail_clk;
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;
419 else
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);
435 if (ret < 0) {
436 dev_err(&pdev->dev, "failed to add adapter!\n");
437 goto fail_clk;
440 dev_info(&pdev->dev, "LPC2K I2C adapter\n");
442 return 0;
444 fail_clk:
445 clk_disable_unprepare(i2c->clk);
446 return ret;
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);
456 return 0;
459 #ifdef CONFIG_PM
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);
467 return 0;
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);
478 return 0;
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)
487 #else
488 #define I2C_LPC2K_DEV_PM_OPS NULL
489 #endif
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,
500 .driver = {
501 .name = "lpc2k-i2c",
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");