blob: 78bcad031d6a118acc7a87f50a4101f632aa797e [file] [log] [blame]
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001/*
2 * i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
3 *
4 * Copyright (C) 2011 Weinmann Medical GmbH
5 * Author: Nikolaus Voss <n.voss@weinmann.de>
6 *
7 * Evolved from original work by:
8 * Copyright (C) 2004 Rick Bronson
9 * Converted to 2.6 by Andrew Victor <andrew@sanpeople.com>
10 *
11 * Borrowed heavily from original work by:
12 * Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 */
19
20#include <linux/clk.h>
21#include <linux/completion.h>
22#include <linux/err.h>
23#include <linux/i2c.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/module.h>
27#include <linux/platform_device.h>
28#include <linux/slab.h>
29
30#define TWI_CLK_HZ 100000 /* max 400 Kbits/s */
31#define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */
32
33/* AT91 TWI register definitions */
34#define AT91_TWI_CR 0x0000 /* Control Register */
35#define AT91_TWI_START 0x0001 /* Send a Start Condition */
36#define AT91_TWI_STOP 0x0002 /* Send a Stop Condition */
37#define AT91_TWI_MSEN 0x0004 /* Master Transfer Enable */
38#define AT91_TWI_SVDIS 0x0020 /* Slave Transfer Disable */
39#define AT91_TWI_SWRST 0x0080 /* Software Reset */
40
41#define AT91_TWI_MMR 0x0004 /* Master Mode Register */
42#define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */
43#define AT91_TWI_MREAD 0x1000 /* Master Read Direction */
44
45#define AT91_TWI_IADR 0x000c /* Internal Address Register */
46
47#define AT91_TWI_CWGR 0x0010 /* Clock Waveform Generator Reg */
48
49#define AT91_TWI_SR 0x0020 /* Status Register */
50#define AT91_TWI_TXCOMP 0x0001 /* Transmission Complete */
51#define AT91_TWI_RXRDY 0x0002 /* Receive Holding Register Ready */
52#define AT91_TWI_TXRDY 0x0004 /* Transmit Holding Register Ready */
53
54#define AT91_TWI_OVRE 0x0040 /* Overrun Error */
55#define AT91_TWI_UNRE 0x0080 /* Underrun Error */
56#define AT91_TWI_NACK 0x0100 /* Not Acknowledged */
57
58#define AT91_TWI_IER 0x0024 /* Interrupt Enable Register */
59#define AT91_TWI_IDR 0x0028 /* Interrupt Disable Register */
60#define AT91_TWI_IMR 0x002c /* Interrupt Mask Register */
61#define AT91_TWI_RHR 0x0030 /* Receive Holding Register */
62#define AT91_TWI_THR 0x0034 /* Transmit Holding Register */
63
64struct at91_twi_pdata {
65 unsigned clk_max_div;
66 unsigned clk_offset;
67 bool has_unre_flag;
68};
69
70struct at91_twi_dev {
71 struct device *dev;
72 void __iomem *base;
73 struct completion cmd_complete;
74 struct clk *clk;
75 u8 *buf;
76 size_t buf_len;
77 struct i2c_msg *msg;
78 int irq;
79 unsigned transfer_status;
80 struct i2c_adapter adapter;
81 unsigned twi_cwgr_reg;
82 struct at91_twi_pdata *pdata;
83};
84
85static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg)
86{
87 return readl_relaxed(dev->base + reg);
88}
89
90static void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val)
91{
92 writel_relaxed(val, dev->base + reg);
93}
94
95static void at91_disable_twi_interrupts(struct at91_twi_dev *dev)
96{
97 at91_twi_write(dev, AT91_TWI_IDR,
98 AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY);
99}
100
101static void at91_init_twi_bus(struct at91_twi_dev *dev)
102{
103 at91_disable_twi_interrupts(dev);
104 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
105 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
106 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
107 at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
108}
109
110/*
111 * Calculate symmetric clock as stated in datasheet:
112 * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
113 */
114static void __devinit at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk)
115{
116 int ckdiv, cdiv, div;
117 struct at91_twi_pdata *pdata = dev->pdata;
118 int offset = pdata->clk_offset;
119 int max_ckdiv = pdata->clk_max_div;
120
121 div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
122 2 * twi_clk) - offset);
123 ckdiv = fls(div >> 8);
124 cdiv = div >> ckdiv;
125
126 if (ckdiv > max_ckdiv) {
127 dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
128 ckdiv, max_ckdiv);
129 ckdiv = max_ckdiv;
130 cdiv = 255;
131 }
132
133 dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv;
134 dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv);
135}
136
137static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
138{
139 if (dev->buf_len <= 0)
140 return;
141
142 at91_twi_write(dev, AT91_TWI_THR, *dev->buf);
143
144 /* send stop when last byte has been written */
145 if (--dev->buf_len == 0)
146 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
147
148 dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len);
149
150 ++dev->buf;
151}
152
153static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
154{
155 if (dev->buf_len <= 0)
156 return;
157
158 *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff;
159 --dev->buf_len;
160
161 /* handle I2C_SMBUS_BLOCK_DATA */
162 if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
163 dev->msg->flags &= ~I2C_M_RECV_LEN;
164 dev->buf_len += *dev->buf;
165 dev->msg->len = dev->buf_len + 1;
166 dev_dbg(dev->dev, "received block length %d\n", dev->buf_len);
167 }
168
169 /* send stop if second but last byte has been read */
170 if (dev->buf_len == 1)
171 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
172
173 dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len);
174
175 ++dev->buf;
176}
177
178static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
179{
180 struct at91_twi_dev *dev = dev_id;
181 const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
182 const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
183
184 if (!irqstatus)
185 return IRQ_NONE;
186 else if (irqstatus & AT91_TWI_RXRDY)
187 at91_twi_read_next_byte(dev);
188 else if (irqstatus & AT91_TWI_TXRDY)
189 at91_twi_write_next_byte(dev);
190
191 /* catch error flags */
192 dev->transfer_status |= status;
193
194 if (irqstatus & AT91_TWI_TXCOMP) {
195 at91_disable_twi_interrupts(dev);
196 complete(&dev->cmd_complete);
197 }
198
199 return IRQ_HANDLED;
200}
201
202static int at91_do_twi_transfer(struct at91_twi_dev *dev)
203{
204 int ret;
205 bool has_unre_flag = dev->pdata->has_unre_flag;
206
207 dev_dbg(dev->dev, "transfer: %s %d bytes.\n",
208 (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
209
210 INIT_COMPLETION(dev->cmd_complete);
211 dev->transfer_status = 0;
212 if (dev->msg->flags & I2C_M_RD) {
213 unsigned start_flags = AT91_TWI_START;
214
215 if (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY) {
216 dev_err(dev->dev, "RXRDY still set!");
217 at91_twi_read(dev, AT91_TWI_RHR);
218 }
219
220 /* if only one byte is to be read, immediately stop transfer */
221 if (dev->buf_len <= 1 && !(dev->msg->flags & I2C_M_RECV_LEN))
222 start_flags |= AT91_TWI_STOP;
223 at91_twi_write(dev, AT91_TWI_CR, start_flags);
224 at91_twi_write(dev, AT91_TWI_IER,
225 AT91_TWI_TXCOMP | AT91_TWI_RXRDY);
226 } else {
227 at91_twi_write_next_byte(dev);
228 at91_twi_write(dev, AT91_TWI_IER,
229 AT91_TWI_TXCOMP | AT91_TWI_TXRDY);
230 }
231
232 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
233 dev->adapter.timeout);
234 if (ret == 0) {
235 dev_err(dev->dev, "controller timed out\n");
236 at91_init_twi_bus(dev);
237 return -ETIMEDOUT;
238 }
239 if (dev->transfer_status & AT91_TWI_NACK) {
240 dev_dbg(dev->dev, "received nack\n");
241 return -EREMOTEIO;
242 }
243 if (dev->transfer_status & AT91_TWI_OVRE) {
244 dev_err(dev->dev, "overrun while reading\n");
245 return -EIO;
246 }
247 if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
248 dev_err(dev->dev, "underrun while writing\n");
249 return -EIO;
250 }
251 dev_dbg(dev->dev, "transfer complete\n");
252
253 return 0;
254}
255
256static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
257{
258 struct at91_twi_dev *dev = i2c_get_adapdata(adap);
259 int ret;
260 unsigned int_addr_flag = 0;
261 struct i2c_msg *m_start = msg;
262
263 dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
264
265 /*
266 * The hardware can handle at most two messages concatenated by a
267 * repeated start via it's internal address feature.
268 */
269 if (num > 2) {
270 dev_err(dev->dev,
271 "cannot handle more than two concatenated messages.\n");
272 return 0;
273 } else if (num == 2) {
274 int internal_address = 0;
275 int i;
276
277 if (msg->flags & I2C_M_RD) {
278 dev_err(dev->dev, "first transfer must be write.\n");
279 return -EINVAL;
280 }
281 if (msg->len > 3) {
282 dev_err(dev->dev, "first message size must be <= 3.\n");
283 return -EINVAL;
284 }
285
286 /* 1st msg is put into the internal address, start with 2nd */
287 m_start = &msg[1];
288 for (i = 0; i < msg->len; ++i) {
289 const unsigned addr = msg->buf[msg->len - 1 - i];
290
291 internal_address |= addr << (8 * i);
292 int_addr_flag += AT91_TWI_IADRSZ_1;
293 }
294 at91_twi_write(dev, AT91_TWI_IADR, internal_address);
295 }
296
297 at91_twi_write(dev, AT91_TWI_MMR, (m_start->addr << 16) | int_addr_flag
298 | ((m_start->flags & I2C_M_RD) ? AT91_TWI_MREAD : 0));
299
300 dev->buf_len = m_start->len;
301 dev->buf = m_start->buf;
302 dev->msg = m_start;
303
304 ret = at91_do_twi_transfer(dev);
305
306 return (ret < 0) ? ret : num;
307}
308
309static u32 at91_twi_func(struct i2c_adapter *adapter)
310{
311 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
312 | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
313}
314
315static struct i2c_algorithm at91_twi_algorithm = {
316 .master_xfer = at91_twi_xfer,
317 .functionality = at91_twi_func,
318};
319
320static struct at91_twi_pdata at91rm9200_config = {
321 .clk_max_div = 5,
322 .clk_offset = 3,
323 .has_unre_flag = true,
324};
325
326static struct at91_twi_pdata at91sam9261_config = {
327 .clk_max_div = 5,
328 .clk_offset = 4,
329 .has_unre_flag = false,
330};
331
332static struct at91_twi_pdata at91sam9260_config = {
333 .clk_max_div = 7,
334 .clk_offset = 4,
335 .has_unre_flag = false,
336};
337
338static struct at91_twi_pdata at91sam9g20_config = {
339 .clk_max_div = 7,
340 .clk_offset = 4,
341 .has_unre_flag = false,
342};
343
344static struct at91_twi_pdata at91sam9g10_config = {
345 .clk_max_div = 7,
346 .clk_offset = 4,
347 .has_unre_flag = false,
348};
349
350static const struct platform_device_id at91_twi_devtypes[] = {
351 {
352 .name = "i2c-at91rm9200",
353 .driver_data = (unsigned long) &at91rm9200_config,
354 }, {
355 .name = "i2c-at91sam9261",
356 .driver_data = (unsigned long) &at91sam9261_config,
357 }, {
358 .name = "i2c-at91sam9260",
359 .driver_data = (unsigned long) &at91sam9260_config,
360 }, {
361 .name = "i2c-at91sam9g20",
362 .driver_data = (unsigned long) &at91sam9g20_config,
363 }, {
364 .name = "i2c-at91sam9g10",
365 .driver_data = (unsigned long) &at91sam9g10_config,
366 }, {
367 /* sentinel */
368 }
369};
370
371static int __devinit at91_twi_probe(struct platform_device *pdev)
372{
373 struct at91_twi_dev *dev;
374 struct resource *mem;
375 int rc;
376
377 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
378 if (!dev)
379 return -ENOMEM;
380 init_completion(&dev->cmd_complete);
381 dev->dev = &pdev->dev;
382
383 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
384 if (!mem)
385 return -ENODEV;
386
387 dev->pdata = at91_twi_get_driver_data(pdev);
388 if (!dev->pdata)
389 return -ENODEV;
390
391 dev->base = devm_request_and_ioremap(&pdev->dev, mem);
392 if (!dev->base)
393 return -EBUSY;
394
395 dev->irq = platform_get_irq(pdev, 0);
396 if (dev->irq < 0)
397 return dev->irq;
398
399 rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
400 dev_name(dev->dev), dev);
401 if (rc) {
402 dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
403 return rc;
404 }
405
406 platform_set_drvdata(pdev, dev);
407
408 dev->clk = devm_clk_get(dev->dev, NULL);
409 if (IS_ERR(dev->clk)) {
410 dev_err(dev->dev, "no clock defined\n");
411 return -ENODEV;
412 }
413 clk_prepare_enable(dev->clk);
414
415 at91_calc_twi_clock(dev, TWI_CLK_HZ);
416 at91_init_twi_bus(dev);
417
418 snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91");
419 i2c_set_adapdata(&dev->adapter, dev);
420 dev->adapter.owner = THIS_MODULE;
421 dev->adapter.class = I2C_CLASS_HWMON;
422 dev->adapter.algo = &at91_twi_algorithm;
423 dev->adapter.dev.parent = dev->dev;
424 dev->adapter.nr = pdev->id;
425 dev->adapter.timeout = AT91_I2C_TIMEOUT;
426
427 rc = i2c_add_numbered_adapter(&dev->adapter);
428 if (rc) {
429 dev_err(dev->dev, "Adapter %s registration failed\n",
430 dev->adapter.name);
431 clk_disable_unprepare(dev->clk);
432 return rc;
433 }
434
435 dev_info(dev->dev, "AT91 i2c bus driver.\n");
436 return 0;
437}
438
439static int __devexit at91_twi_remove(struct platform_device *pdev)
440{
441 struct at91_twi_dev *dev = platform_get_drvdata(pdev);
442 int rc;
443
444 rc = i2c_del_adapter(&dev->adapter);
445 clk_disable_unprepare(dev->clk);
446
447 return rc;
448}
449
450#ifdef CONFIG_PM
451
452static int at91_twi_runtime_suspend(struct device *dev)
453{
454 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
455
456 clk_disable(twi_dev->clk);
457
458 return 0;
459}
460
461static int at91_twi_runtime_resume(struct device *dev)
462{
463 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
464
465 return clk_enable(twi_dev->clk);
466}
467
468static const struct dev_pm_ops at91_twi_pm = {
469 .runtime_suspend = at91_twi_runtime_suspend,
470 .runtime_resume = at91_twi_runtime_resume,
471};
472
473#define at91_twi_pm_ops (&at91_twi_pm)
474#else
475#define at91_twi_pm_ops NULL
476#endif
477
478static struct platform_driver at91_twi_driver = {
479 .probe = at91_twi_probe,
480 .remove = __devexit_p(at91_twi_remove),
481 .id_table = at91_twi_devtypes,
482 .driver = {
483 .name = "at91_i2c",
484 .owner = THIS_MODULE,
485 .pm = at91_twi_pm_ops,
486 },
487};
488
489static int __init at91_twi_init(void)
490{
491 return platform_driver_register(&at91_twi_driver);
492}
493
494static void __exit at91_twi_exit(void)
495{
496 platform_driver_unregister(&at91_twi_driver);
497}
498
499subsys_initcall(at91_twi_init);
500module_exit(at91_twi_exit);
501
502MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
503MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
504MODULE_LICENSE("GPL");
505MODULE_ALIAS("platform:at91_i2c");