2 * i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
4 * Copyright (C) 2011 Weinmann Medical GmbH
5 * Author: Nikolaus Voss <n.voss@weinmann.de>
7 * Evolved from original work by:
8 * Copyright (C) 2004 Rick Bronson
9 * Converted to 2.6 by Andrew Victor <andrew@sanpeople.com>
11 * Borrowed heavily from original work by:
12 * Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
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.
20 #include <linux/clk.h>
21 #include <linux/completion.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/dmaengine.h>
24 #include <linux/err.h>
25 #include <linux/i2c.h>
26 #include <linux/interrupt.h>
28 #include <linux/module.h>
30 #include <linux/of_device.h>
31 #include <linux/of_i2c.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <linux/platform_data/dma-atmel.h>
36 #define TWI_CLK_HZ 100000 /* max 400 Kbits/s */
37 #define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */
38 #define AT91_I2C_DMA_THRESHOLD 8 /* enable DMA if transfer size is bigger than this threshold */
40 /* AT91 TWI register definitions */
41 #define AT91_TWI_CR 0x0000 /* Control Register */
42 #define AT91_TWI_START 0x0001 /* Send a Start Condition */
43 #define AT91_TWI_STOP 0x0002 /* Send a Stop Condition */
44 #define AT91_TWI_MSEN 0x0004 /* Master Transfer Enable */
45 #define AT91_TWI_SVDIS 0x0020 /* Slave Transfer Disable */
46 #define AT91_TWI_QUICK 0x0040 /* SMBus quick command */
47 #define AT91_TWI_SWRST 0x0080 /* Software Reset */
49 #define AT91_TWI_MMR 0x0004 /* Master Mode Register */
50 #define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */
51 #define AT91_TWI_MREAD 0x1000 /* Master Read Direction */
53 #define AT91_TWI_IADR 0x000c /* Internal Address Register */
55 #define AT91_TWI_CWGR 0x0010 /* Clock Waveform Generator Reg */
57 #define AT91_TWI_SR 0x0020 /* Status Register */
58 #define AT91_TWI_TXCOMP 0x0001 /* Transmission Complete */
59 #define AT91_TWI_RXRDY 0x0002 /* Receive Holding Register Ready */
60 #define AT91_TWI_TXRDY 0x0004 /* Transmit Holding Register Ready */
62 #define AT91_TWI_OVRE 0x0040 /* Overrun Error */
63 #define AT91_TWI_UNRE 0x0080 /* Underrun Error */
64 #define AT91_TWI_NACK 0x0100 /* Not Acknowledged */
66 #define AT91_TWI_IER 0x0024 /* Interrupt Enable Register */
67 #define AT91_TWI_IDR 0x0028 /* Interrupt Disable Register */
68 #define AT91_TWI_IMR 0x002c /* Interrupt Mask Register */
69 #define AT91_TWI_RHR 0x0030 /* Receive Holding Register */
70 #define AT91_TWI_THR 0x0034 /* Transmit Holding Register */
72 struct at91_twi_pdata {
77 struct at_dma_slave dma_slave;
81 struct dma_chan *chan_rx;
82 struct dma_chan *chan_tx;
83 struct scatterlist sg;
84 struct dma_async_tx_descriptor *data_desc;
85 enum dma_data_direction direction;
87 bool xfer_in_progress;
93 struct completion cmd_complete;
100 unsigned transfer_status;
101 struct i2c_adapter adapter;
102 unsigned twi_cwgr_reg;
103 struct at91_twi_pdata *pdata;
106 struct at91_twi_dma dma;
109 static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg)
111 return readl_relaxed(dev->base + reg);
114 static void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val)
116 writel_relaxed(val, dev->base + reg);
119 static void at91_disable_twi_interrupts(struct at91_twi_dev *dev)
121 at91_twi_write(dev, AT91_TWI_IDR,
122 AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY);
125 static void at91_twi_irq_save(struct at91_twi_dev *dev)
127 dev->imr = at91_twi_read(dev, AT91_TWI_IMR) & 0x7;
128 at91_disable_twi_interrupts(dev);
131 static void at91_twi_irq_restore(struct at91_twi_dev *dev)
133 at91_twi_write(dev, AT91_TWI_IER, dev->imr);
136 static void at91_init_twi_bus(struct at91_twi_dev *dev)
138 at91_disable_twi_interrupts(dev);
139 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
140 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
141 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
142 at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
146 * Calculate symmetric clock as stated in datasheet:
147 * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
149 static void at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk)
151 int ckdiv, cdiv, div;
152 struct at91_twi_pdata *pdata = dev->pdata;
153 int offset = pdata->clk_offset;
154 int max_ckdiv = pdata->clk_max_div;
156 div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
157 2 * twi_clk) - offset);
158 ckdiv = fls(div >> 8);
161 if (ckdiv > max_ckdiv) {
162 dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
168 dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv;
169 dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv);
172 static void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
174 struct at91_twi_dma *dma = &dev->dma;
176 at91_twi_irq_save(dev);
178 if (dma->xfer_in_progress) {
179 if (dma->direction == DMA_FROM_DEVICE)
180 dmaengine_terminate_all(dma->chan_rx);
182 dmaengine_terminate_all(dma->chan_tx);
183 dma->xfer_in_progress = false;
185 if (dma->buf_mapped) {
186 dma_unmap_single(dev->dev, sg_dma_address(&dma->sg),
187 dev->buf_len, dma->direction);
188 dma->buf_mapped = false;
191 at91_twi_irq_restore(dev);
194 static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
196 if (dev->buf_len <= 0)
199 at91_twi_write(dev, AT91_TWI_THR, *dev->buf);
201 /* send stop when last byte has been written */
202 if (--dev->buf_len == 0)
203 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
205 dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len);
210 static void at91_twi_write_data_dma_callback(void *data)
212 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
214 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg),
215 dev->buf_len, DMA_TO_DEVICE);
217 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
220 static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
223 struct dma_async_tx_descriptor *txdesc;
224 struct at91_twi_dma *dma = &dev->dma;
225 struct dma_chan *chan_tx = dma->chan_tx;
227 if (dev->buf_len <= 0)
230 dma->direction = DMA_TO_DEVICE;
232 at91_twi_irq_save(dev);
233 dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
235 if (dma_mapping_error(dev->dev, dma_addr)) {
236 dev_err(dev->dev, "dma map failed\n");
239 dma->buf_mapped = true;
240 at91_twi_irq_restore(dev);
241 sg_dma_len(&dma->sg) = dev->buf_len;
242 sg_dma_address(&dma->sg) = dma_addr;
244 txdesc = dmaengine_prep_slave_sg(chan_tx, &dma->sg, 1, DMA_MEM_TO_DEV,
245 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
247 dev_err(dev->dev, "dma prep slave sg failed\n");
251 txdesc->callback = at91_twi_write_data_dma_callback;
252 txdesc->callback_param = dev;
254 dma->xfer_in_progress = true;
255 dmaengine_submit(txdesc);
256 dma_async_issue_pending(chan_tx);
261 at91_twi_dma_cleanup(dev);
264 static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
266 if (dev->buf_len <= 0)
269 *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff;
272 /* return if aborting, we only needed to read RHR to clear RXRDY*/
273 if (dev->recv_len_abort)
276 /* handle I2C_SMBUS_BLOCK_DATA */
277 if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
278 /* ensure length byte is a valid value */
279 if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
280 dev->msg->flags &= ~I2C_M_RECV_LEN;
281 dev->buf_len += *dev->buf;
282 dev->msg->len = dev->buf_len + 1;
283 dev_dbg(dev->dev, "received block length %d\n",
286 /* abort and send the stop by reading one more byte */
287 dev->recv_len_abort = true;
292 /* send stop if second but last byte has been read */
293 if (dev->buf_len == 1)
294 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
296 dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len);
301 static void at91_twi_read_data_dma_callback(void *data)
303 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
305 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg),
306 dev->buf_len, DMA_FROM_DEVICE);
308 /* The last two bytes have to be read without using dma */
309 dev->buf += dev->buf_len - 2;
311 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_RXRDY);
314 static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
317 struct dma_async_tx_descriptor *rxdesc;
318 struct at91_twi_dma *dma = &dev->dma;
319 struct dma_chan *chan_rx = dma->chan_rx;
321 dma->direction = DMA_FROM_DEVICE;
323 /* Keep in mind that we won't use dma to read the last two bytes */
324 at91_twi_irq_save(dev);
325 dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len - 2,
327 if (dma_mapping_error(dev->dev, dma_addr)) {
328 dev_err(dev->dev, "dma map failed\n");
331 dma->buf_mapped = true;
332 at91_twi_irq_restore(dev);
333 dma->sg.dma_address = dma_addr;
334 sg_dma_len(&dma->sg) = dev->buf_len - 2;
336 rxdesc = dmaengine_prep_slave_sg(chan_rx, &dma->sg, 1, DMA_DEV_TO_MEM,
337 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
339 dev_err(dev->dev, "dma prep slave sg failed\n");
343 rxdesc->callback = at91_twi_read_data_dma_callback;
344 rxdesc->callback_param = dev;
346 dma->xfer_in_progress = true;
347 dmaengine_submit(rxdesc);
348 dma_async_issue_pending(dma->chan_rx);
353 at91_twi_dma_cleanup(dev);
356 static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
358 struct at91_twi_dev *dev = dev_id;
359 const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
360 const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
364 else if (irqstatus & AT91_TWI_RXRDY)
365 at91_twi_read_next_byte(dev);
366 else if (irqstatus & AT91_TWI_TXRDY)
367 at91_twi_write_next_byte(dev);
369 /* catch error flags */
370 dev->transfer_status |= status;
372 if (irqstatus & AT91_TWI_TXCOMP) {
373 at91_disable_twi_interrupts(dev);
374 complete(&dev->cmd_complete);
380 static int at91_do_twi_transfer(struct at91_twi_dev *dev)
383 bool has_unre_flag = dev->pdata->has_unre_flag;
385 dev_dbg(dev->dev, "transfer: %s %d bytes.\n",
386 (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
388 INIT_COMPLETION(dev->cmd_complete);
389 dev->transfer_status = 0;
392 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
393 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
394 } else if (dev->msg->flags & I2C_M_RD) {
395 unsigned start_flags = AT91_TWI_START;
397 if (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY) {
398 dev_err(dev->dev, "RXRDY still set!");
399 at91_twi_read(dev, AT91_TWI_RHR);
402 /* if only one byte is to be read, immediately stop transfer */
403 if (dev->buf_len <= 1 && !(dev->msg->flags & I2C_M_RECV_LEN))
404 start_flags |= AT91_TWI_STOP;
405 at91_twi_write(dev, AT91_TWI_CR, start_flags);
407 * When using dma, the last byte has to be read manually in
408 * order to not send the stop command too late and then
409 * to receive extra data. In practice, there are some issues
410 * if you use the dma to read n-1 bytes because of latency.
411 * Reading n-2 bytes with dma and the two last ones manually
412 * seems to be the best solution.
414 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
415 at91_twi_read_data_dma(dev);
417 * It is important to enable TXCOMP irq here because
418 * doing it only when transferring the last two bytes
419 * will mask NACK errors since TXCOMP is set when a
422 at91_twi_write(dev, AT91_TWI_IER,
425 at91_twi_write(dev, AT91_TWI_IER,
426 AT91_TWI_TXCOMP | AT91_TWI_RXRDY);
428 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
429 at91_twi_write_data_dma(dev);
430 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
432 at91_twi_write_next_byte(dev);
433 at91_twi_write(dev, AT91_TWI_IER,
434 AT91_TWI_TXCOMP | AT91_TWI_TXRDY);
438 ret = wait_for_completion_timeout(&dev->cmd_complete,
439 dev->adapter.timeout);
441 dev_err(dev->dev, "controller timed out\n");
442 at91_init_twi_bus(dev);
446 if (dev->transfer_status & AT91_TWI_NACK) {
447 dev_dbg(dev->dev, "received nack\n");
451 if (dev->transfer_status & AT91_TWI_OVRE) {
452 dev_err(dev->dev, "overrun while reading\n");
456 if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
457 dev_err(dev->dev, "underrun while writing\n");
461 if (dev->recv_len_abort) {
462 dev_err(dev->dev, "invalid smbus block length recvd\n");
467 dev_dbg(dev->dev, "transfer complete\n");
472 at91_twi_dma_cleanup(dev);
476 static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
478 struct at91_twi_dev *dev = i2c_get_adapdata(adap);
480 unsigned int_addr_flag = 0;
481 struct i2c_msg *m_start = msg;
483 dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
486 * The hardware can handle at most two messages concatenated by a
487 * repeated start via it's internal address feature.
491 "cannot handle more than two concatenated messages.\n");
493 } else if (num == 2) {
494 int internal_address = 0;
497 if (msg->flags & I2C_M_RD) {
498 dev_err(dev->dev, "first transfer must be write.\n");
502 dev_err(dev->dev, "first message size must be <= 3.\n");
506 /* 1st msg is put into the internal address, start with 2nd */
508 for (i = 0; i < msg->len; ++i) {
509 const unsigned addr = msg->buf[msg->len - 1 - i];
511 internal_address |= addr << (8 * i);
512 int_addr_flag += AT91_TWI_IADRSZ_1;
514 at91_twi_write(dev, AT91_TWI_IADR, internal_address);
517 at91_twi_write(dev, AT91_TWI_MMR, (m_start->addr << 16) | int_addr_flag
518 | ((m_start->flags & I2C_M_RD) ? AT91_TWI_MREAD : 0));
520 dev->buf_len = m_start->len;
521 dev->buf = m_start->buf;
523 dev->recv_len_abort = false;
525 ret = at91_do_twi_transfer(dev);
527 return (ret < 0) ? ret : num;
530 static u32 at91_twi_func(struct i2c_adapter *adapter)
532 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
533 | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
536 static struct i2c_algorithm at91_twi_algorithm = {
537 .master_xfer = at91_twi_xfer,
538 .functionality = at91_twi_func,
541 static struct at91_twi_pdata at91rm9200_config = {
544 .has_unre_flag = true,
545 .has_dma_support = false,
548 static struct at91_twi_pdata at91sam9261_config = {
551 .has_unre_flag = false,
552 .has_dma_support = false,
555 static struct at91_twi_pdata at91sam9260_config = {
558 .has_unre_flag = false,
559 .has_dma_support = false,
562 static struct at91_twi_pdata at91sam9g20_config = {
565 .has_unre_flag = false,
566 .has_dma_support = false,
569 static struct at91_twi_pdata at91sam9g10_config = {
572 .has_unre_flag = false,
573 .has_dma_support = false,
576 static const struct platform_device_id at91_twi_devtypes[] = {
578 .name = "i2c-at91rm9200",
579 .driver_data = (unsigned long) &at91rm9200_config,
581 .name = "i2c-at91sam9261",
582 .driver_data = (unsigned long) &at91sam9261_config,
584 .name = "i2c-at91sam9260",
585 .driver_data = (unsigned long) &at91sam9260_config,
587 .name = "i2c-at91sam9g20",
588 .driver_data = (unsigned long) &at91sam9g20_config,
590 .name = "i2c-at91sam9g10",
591 .driver_data = (unsigned long) &at91sam9g10_config,
597 #if defined(CONFIG_OF)
598 static struct at91_twi_pdata at91sam9x5_config = {
601 .has_unre_flag = false,
602 .has_dma_support = true,
605 static const struct of_device_id atmel_twi_dt_ids[] = {
607 .compatible = "atmel,at91rm9200-i2c",
608 .data = &at91rm9200_config,
610 .compatible = "atmel,at91sam9260-i2c",
611 .data = &at91sam9260_config,
613 .compatible = "atmel,at91sam9g20-i2c",
614 .data = &at91sam9g20_config,
616 .compatible = "atmel,at91sam9g10-i2c",
617 .data = &at91sam9g10_config,
619 .compatible = "atmel,at91sam9x5-i2c",
620 .data = &at91sam9x5_config,
625 MODULE_DEVICE_TABLE(of, atmel_twi_dt_ids);
628 static bool filter(struct dma_chan *chan, void *pdata)
630 struct at91_twi_pdata *sl_pdata = pdata;
631 struct at_dma_slave *sl;
636 sl = &sl_pdata->dma_slave;
637 if (sl && (sl->dma_dev == chan->device->dev)) {
645 static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
648 struct at91_twi_pdata *pdata = dev->pdata;
649 struct dma_slave_config slave_config;
650 struct at91_twi_dma *dma = &dev->dma;
653 memset(&slave_config, 0, sizeof(slave_config));
654 slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
655 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
656 slave_config.src_maxburst = 1;
657 slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
658 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
659 slave_config.dst_maxburst = 1;
660 slave_config.device_fc = false;
663 dma_cap_set(DMA_SLAVE, mask);
665 dma->chan_tx = dma_request_slave_channel_compat(mask, filter, pdata,
668 dev_err(dev->dev, "can't get a DMA channel for tx\n");
673 dma->chan_rx = dma_request_slave_channel_compat(mask, filter, pdata,
676 dev_err(dev->dev, "can't get a DMA channel for rx\n");
681 slave_config.direction = DMA_MEM_TO_DEV;
682 if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
683 dev_err(dev->dev, "failed to configure tx channel\n");
688 slave_config.direction = DMA_DEV_TO_MEM;
689 if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
690 dev_err(dev->dev, "failed to configure rx channel\n");
695 sg_init_table(&dma->sg, 1);
696 dma->buf_mapped = false;
697 dma->xfer_in_progress = false;
699 dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
700 dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
705 dev_info(dev->dev, "can't use DMA\n");
707 dma_release_channel(dma->chan_rx);
709 dma_release_channel(dma->chan_tx);
713 static struct at91_twi_pdata *at91_twi_get_driver_data(
714 struct platform_device *pdev)
716 if (pdev->dev.of_node) {
717 const struct of_device_id *match;
718 match = of_match_node(atmel_twi_dt_ids, pdev->dev.of_node);
721 return (struct at91_twi_pdata *)match->data;
723 return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data;
726 static int at91_twi_probe(struct platform_device *pdev)
728 struct at91_twi_dev *dev;
729 struct resource *mem;
733 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
736 init_completion(&dev->cmd_complete);
737 dev->dev = &pdev->dev;
739 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
742 phy_addr = mem->start;
744 dev->pdata = at91_twi_get_driver_data(pdev);
748 dev->base = devm_ioremap_resource(&pdev->dev, mem);
749 if (IS_ERR(dev->base))
750 return PTR_ERR(dev->base);
752 dev->irq = platform_get_irq(pdev, 0);
756 rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
757 dev_name(dev->dev), dev);
759 dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
763 platform_set_drvdata(pdev, dev);
765 dev->clk = devm_clk_get(dev->dev, NULL);
766 if (IS_ERR(dev->clk)) {
767 dev_err(dev->dev, "no clock defined\n");
770 clk_prepare_enable(dev->clk);
772 if (dev->pdata->has_dma_support) {
773 if (at91_twi_configure_dma(dev, phy_addr) == 0)
777 at91_calc_twi_clock(dev, TWI_CLK_HZ);
778 at91_init_twi_bus(dev);
780 snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91");
781 i2c_set_adapdata(&dev->adapter, dev);
782 dev->adapter.owner = THIS_MODULE;
783 dev->adapter.class = I2C_CLASS_HWMON;
784 dev->adapter.algo = &at91_twi_algorithm;
785 dev->adapter.dev.parent = dev->dev;
786 dev->adapter.nr = pdev->id;
787 dev->adapter.timeout = AT91_I2C_TIMEOUT;
788 dev->adapter.dev.of_node = pdev->dev.of_node;
790 rc = i2c_add_numbered_adapter(&dev->adapter);
792 dev_err(dev->dev, "Adapter %s registration failed\n",
794 clk_disable_unprepare(dev->clk);
798 of_i2c_register_devices(&dev->adapter);
800 dev_info(dev->dev, "AT91 i2c bus driver.\n");
804 static int at91_twi_remove(struct platform_device *pdev)
806 struct at91_twi_dev *dev = platform_get_drvdata(pdev);
808 i2c_del_adapter(&dev->adapter);
809 clk_disable_unprepare(dev->clk);
816 static int at91_twi_runtime_suspend(struct device *dev)
818 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
820 clk_disable(twi_dev->clk);
825 static int at91_twi_runtime_resume(struct device *dev)
827 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
829 return clk_enable(twi_dev->clk);
832 static const struct dev_pm_ops at91_twi_pm = {
833 .runtime_suspend = at91_twi_runtime_suspend,
834 .runtime_resume = at91_twi_runtime_resume,
837 #define at91_twi_pm_ops (&at91_twi_pm)
839 #define at91_twi_pm_ops NULL
842 static struct platform_driver at91_twi_driver = {
843 .probe = at91_twi_probe,
844 .remove = at91_twi_remove,
845 .id_table = at91_twi_devtypes,
848 .owner = THIS_MODULE,
849 .of_match_table = of_match_ptr(atmel_twi_dt_ids),
850 .pm = at91_twi_pm_ops,
854 static int __init at91_twi_init(void)
856 return platform_driver_register(&at91_twi_driver);
859 static void __exit at91_twi_exit(void)
861 platform_driver_unregister(&at91_twi_driver);
864 subsys_initcall(at91_twi_init);
865 module_exit(at91_twi_exit);
867 MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
868 MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
869 MODULE_LICENSE("GPL");
870 MODULE_ALIAS("platform:at91_i2c");