i2c-at91-slave.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * i2c slave support for Atmel's AT91 Two-Wire Interface (TWI)
  4. *
  5. * Copyright (C) 2017 Juergen Fitschen <[email protected]>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/i2c.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pm_runtime.h>
  11. #include "i2c-at91.h"
  12. static irqreturn_t atmel_twi_interrupt_slave(int irq, void *dev_id)
  13. {
  14. struct at91_twi_dev *dev = dev_id;
  15. const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
  16. const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
  17. u8 value;
  18. if (!irqstatus)
  19. return IRQ_NONE;
  20. /* slave address has been detected on I2C bus */
  21. if (irqstatus & AT91_TWI_SVACC) {
  22. if (status & AT91_TWI_SVREAD) {
  23. i2c_slave_event(dev->slave,
  24. I2C_SLAVE_READ_REQUESTED, &value);
  25. writeb_relaxed(value, dev->base + AT91_TWI_THR);
  26. at91_twi_write(dev, AT91_TWI_IER,
  27. AT91_TWI_TXRDY | AT91_TWI_EOSACC);
  28. } else {
  29. i2c_slave_event(dev->slave,
  30. I2C_SLAVE_WRITE_REQUESTED, &value);
  31. at91_twi_write(dev, AT91_TWI_IER,
  32. AT91_TWI_RXRDY | AT91_TWI_EOSACC);
  33. }
  34. at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_SVACC);
  35. }
  36. /* byte transmitted to remote master */
  37. if (irqstatus & AT91_TWI_TXRDY) {
  38. i2c_slave_event(dev->slave, I2C_SLAVE_READ_PROCESSED, &value);
  39. writeb_relaxed(value, dev->base + AT91_TWI_THR);
  40. }
  41. /* byte received from remote master */
  42. if (irqstatus & AT91_TWI_RXRDY) {
  43. value = readb_relaxed(dev->base + AT91_TWI_RHR);
  44. i2c_slave_event(dev->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
  45. }
  46. /* master sent stop */
  47. if (irqstatus & AT91_TWI_EOSACC) {
  48. at91_twi_write(dev, AT91_TWI_IDR,
  49. AT91_TWI_TXRDY | AT91_TWI_RXRDY | AT91_TWI_EOSACC);
  50. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_SVACC);
  51. i2c_slave_event(dev->slave, I2C_SLAVE_STOP, &value);
  52. }
  53. return IRQ_HANDLED;
  54. }
  55. static int at91_reg_slave(struct i2c_client *slave)
  56. {
  57. struct at91_twi_dev *dev = i2c_get_adapdata(slave->adapter);
  58. if (dev->slave)
  59. return -EBUSY;
  60. if (slave->flags & I2C_CLIENT_TEN)
  61. return -EAFNOSUPPORT;
  62. /* Make sure twi_clk doesn't get turned off! */
  63. pm_runtime_get_sync(dev->dev);
  64. dev->slave = slave;
  65. dev->smr = AT91_TWI_SMR_SADR(slave->addr);
  66. at91_init_twi_bus(dev);
  67. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_SVACC);
  68. dev_info(dev->dev, "entered slave mode (ADR=%d)\n", slave->addr);
  69. return 0;
  70. }
  71. static int at91_unreg_slave(struct i2c_client *slave)
  72. {
  73. struct at91_twi_dev *dev = i2c_get_adapdata(slave->adapter);
  74. WARN_ON(!dev->slave);
  75. dev_info(dev->dev, "leaving slave mode\n");
  76. dev->slave = NULL;
  77. dev->smr = 0;
  78. at91_init_twi_bus(dev);
  79. pm_runtime_put(dev->dev);
  80. return 0;
  81. }
  82. static u32 at91_twi_func(struct i2c_adapter *adapter)
  83. {
  84. return I2C_FUNC_SLAVE | I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  85. | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  86. }
  87. static const struct i2c_algorithm at91_twi_algorithm_slave = {
  88. .reg_slave = at91_reg_slave,
  89. .unreg_slave = at91_unreg_slave,
  90. .functionality = at91_twi_func,
  91. };
  92. int at91_twi_probe_slave(struct platform_device *pdev,
  93. u32 phy_addr, struct at91_twi_dev *dev)
  94. {
  95. int rc;
  96. rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt_slave,
  97. 0, dev_name(dev->dev), dev);
  98. if (rc) {
  99. dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
  100. return rc;
  101. }
  102. dev->adapter.algo = &at91_twi_algorithm_slave;
  103. return 0;
  104. }
  105. void at91_init_twi_bus_slave(struct at91_twi_dev *dev)
  106. {
  107. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSDIS);
  108. if (dev->slave_detected && dev->smr) {
  109. at91_twi_write(dev, AT91_TWI_SMR, dev->smr);
  110. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVEN);
  111. }
  112. }