23 #include <linux/i2c.h>
26 #include <linux/module.h>
31 #include <linux/slab.h>
33 #define TWI_CLK_HZ 100000
34 #define AT91_I2C_TIMEOUT msecs_to_jiffies(100)
37 #define AT91_TWI_CR 0x0000
38 #define AT91_TWI_START 0x0001
39 #define AT91_TWI_STOP 0x0002
40 #define AT91_TWI_MSEN 0x0004
41 #define AT91_TWI_SVDIS 0x0020
42 #define AT91_TWI_QUICK 0x0040
43 #define AT91_TWI_SWRST 0x0080
45 #define AT91_TWI_MMR 0x0004
46 #define AT91_TWI_IADRSZ_1 0x0100
47 #define AT91_TWI_MREAD 0x1000
49 #define AT91_TWI_IADR 0x000c
51 #define AT91_TWI_CWGR 0x0010
53 #define AT91_TWI_SR 0x0020
54 #define AT91_TWI_TXCOMP 0x0001
55 #define AT91_TWI_RXRDY 0x0002
56 #define AT91_TWI_TXRDY 0x0004
58 #define AT91_TWI_OVRE 0x0040
59 #define AT91_TWI_UNRE 0x0080
60 #define AT91_TWI_NACK 0x0100
62 #define AT91_TWI_IER 0x0024
63 #define AT91_TWI_IDR 0x0028
64 #define AT91_TWI_IMR 0x002c
65 #define AT91_TWI_RHR 0x0030
66 #define AT91_TWI_THR 0x0034
96 writel_relaxed(val, dev->
base + reg);
107 at91_disable_twi_interrupts(dev);
120 int ckdiv, cdiv,
div;
126 2 * twi_clk) - offset);
127 ckdiv = fls(div >> 8);
130 if (ckdiv > max_ckdiv) {
131 dev_warn(dev->
dev,
"%d exceeds ckdiv max value which is %d.\n",
138 dev_dbg(dev->
dev,
"cdiv %d ckdiv %d\n", cdiv, ckdiv);
141 static void at91_twi_write_next_byte(
struct at91_twi_dev *dev)
157 static void at91_twi_read_next_byte(
struct at91_twi_dev *dev)
186 const unsigned irqstatus = status & at91_twi_read(dev,
AT91_TWI_IMR);
191 at91_twi_read_next_byte(dev);
193 at91_twi_write_next_byte(dev);
199 at91_disable_twi_interrupts(dev);
206 static int at91_do_twi_transfer(
struct at91_twi_dev *dev)
209 bool has_unre_flag = dev->
pdata->has_unre_flag;
223 if (at91_twi_read(dev,
AT91_TWI_SR) & AT91_TWI_RXRDY) {
235 at91_twi_write_next_byte(dev);
244 at91_init_twi_bus(dev);
256 dev_err(dev->
dev,
"underrun while writing\n");
268 unsigned int_addr_flag = 0;
271 dev_dbg(&adap->
dev,
"at91_xfer: processing %d messages:\n", num);
279 "cannot handle more than two concatenated messages.\n");
281 }
else if (num == 2) {
282 int internal_address = 0;
286 dev_err(dev->
dev,
"first transfer must be write.\n");
290 dev_err(dev->
dev,
"first message size must be <= 3.\n");
296 for (i = 0; i < msg->
len; ++
i) {
299 internal_address |= addr << (8 *
i);
312 ret = at91_do_twi_transfer(dev);
314 return (ret < 0) ? ret : num;
324 .master_xfer = at91_twi_xfer,
325 .functionality = at91_twi_func,
331 .has_unre_flag =
true,
337 .has_unre_flag =
false,
343 .has_unre_flag =
false,
349 .has_unre_flag =
false,
355 .has_unre_flag =
false,
361 .has_unre_flag =
false,
366 .name =
"i2c-at91rm9200",
367 .driver_data = (
unsigned long) &at91rm9200_config,
369 .name =
"i2c-at91sam9261",
370 .driver_data = (
unsigned long) &at91sam9261_config,
372 .name =
"i2c-at91sam9260",
373 .driver_data = (
unsigned long) &at91sam9260_config,
375 .name =
"i2c-at91sam9g20",
376 .driver_data = (
unsigned long) &at91sam9g20_config,
378 .name =
"i2c-at91sam9g10",
379 .driver_data = (
unsigned long) &at91sam9g10_config,
385 #if defined(CONFIG_OF)
388 .compatible =
"atmel,at91sam9260-i2c",
389 .data = &at91sam9260_config,
391 .compatible =
"atmel,at91sam9g20-i2c",
392 .data = &at91sam9g20_config,
394 .compatible =
"atmel,at91sam9g10-i2c",
395 .data = &at91sam9g10_config,
397 .compatible =
"atmel,at91sam9x5-i2c",
398 .data = &at91sam9x5_config,
405 #define atmel_twi_dt_ids NULL
411 if (pdev->
dev.of_node) {
437 dev->
pdata = at91_twi_get_driver_data(pdev);
449 rc = devm_request_irq(&pdev->
dev, dev->
irq, atmel_twi_interrupt, 0,
450 dev_name(dev->
dev), dev);
456 platform_set_drvdata(pdev, dev);
459 if (IS_ERR(dev->
clk)) {
463 clk_prepare_enable(dev->
clk);
466 at91_init_twi_bus(dev);
469 i2c_set_adapdata(&dev->
adapter, dev);
472 dev->
adapter.algo = &at91_twi_algorithm;
480 dev_err(dev->
dev,
"Adapter %s registration failed\n",
482 clk_disable_unprepare(dev->
clk);
498 clk_disable_unprepare(dev->
clk);
505 static int at91_twi_runtime_suspend(
struct device *dev)
514 static int at91_twi_runtime_resume(
struct device *dev)
521 static const struct dev_pm_ops at91_twi_pm = {
523 .runtime_resume = at91_twi_runtime_resume,
526 #define at91_twi_pm_ops (&at91_twi_pm)
528 #define at91_twi_pm_ops NULL
532 .probe = at91_twi_probe,
534 .id_table = at91_twi_devtypes,
543 static int __init at91_twi_init(
void)
548 static void __exit at91_twi_exit(
void)