i2c-algo-sgi.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * i2c-algo-sgi.c: i2c driver algorithms for SGI adapters.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License version 2 as published by the Free Software Foundation.
  6. *
  7. * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/i2c-algo-sgi.h>
  16. #define SGI_I2C_FORCE_IDLE (0 << 0)
  17. #define SGI_I2C_NOT_IDLE (1 << 0)
  18. #define SGI_I2C_WRITE (0 << 1)
  19. #define SGI_I2C_READ (1 << 1)
  20. #define SGI_I2C_RELEASE_BUS (0 << 2)
  21. #define SGI_I2C_HOLD_BUS (1 << 2)
  22. #define SGI_I2C_XFER_DONE (0 << 4)
  23. #define SGI_I2C_XFER_BUSY (1 << 4)
  24. #define SGI_I2C_ACK (0 << 5)
  25. #define SGI_I2C_NACK (1 << 5)
  26. #define SGI_I2C_BUS_OK (0 << 7)
  27. #define SGI_I2C_BUS_ERR (1 << 7)
  28. #define get_control() adap->getctrl(adap->data)
  29. #define set_control(val) adap->setctrl(adap->data, val)
  30. #define read_data() adap->rdata(adap->data)
  31. #define write_data(val) adap->wdata(adap->data, val)
  32. static int wait_xfer_done(struct i2c_algo_sgi_data *adap)
  33. {
  34. int i;
  35. for (i = 0; i < adap->xfer_timeout; i++) {
  36. if ((get_control() & SGI_I2C_XFER_BUSY) == 0)
  37. return 0;
  38. udelay(1);
  39. }
  40. return -ETIMEDOUT;
  41. }
  42. static int wait_ack(struct i2c_algo_sgi_data *adap)
  43. {
  44. int i;
  45. if (wait_xfer_done(adap))
  46. return -ETIMEDOUT;
  47. for (i = 0; i < adap->ack_timeout; i++) {
  48. if ((get_control() & SGI_I2C_NACK) == 0)
  49. return 0;
  50. udelay(1);
  51. }
  52. return -ETIMEDOUT;
  53. }
  54. static int force_idle(struct i2c_algo_sgi_data *adap)
  55. {
  56. int i;
  57. set_control(SGI_I2C_FORCE_IDLE);
  58. for (i = 0; i < adap->xfer_timeout; i++) {
  59. if ((get_control() & SGI_I2C_NOT_IDLE) == 0)
  60. goto out;
  61. udelay(1);
  62. }
  63. return -ETIMEDOUT;
  64. out:
  65. if (get_control() & SGI_I2C_BUS_ERR)
  66. return -EIO;
  67. return 0;
  68. }
  69. static int do_address(struct i2c_algo_sgi_data *adap, unsigned int addr,
  70. int rd)
  71. {
  72. if (rd)
  73. set_control(SGI_I2C_NOT_IDLE);
  74. /* Check if bus is idle, eventually force it to do so */
  75. if (get_control() & SGI_I2C_NOT_IDLE)
  76. if (force_idle(adap))
  77. return -EIO;
  78. /* Write out the i2c chip address and specify operation */
  79. set_control(SGI_I2C_HOLD_BUS | SGI_I2C_WRITE | SGI_I2C_NOT_IDLE);
  80. if (rd)
  81. addr |= 1;
  82. write_data(addr);
  83. if (wait_ack(adap))
  84. return -EIO;
  85. return 0;
  86. }
  87. static int i2c_read(struct i2c_algo_sgi_data *adap, unsigned char *buf,
  88. unsigned int len)
  89. {
  90. int i;
  91. set_control(SGI_I2C_HOLD_BUS | SGI_I2C_READ | SGI_I2C_NOT_IDLE);
  92. for (i = 0; i < len; i++) {
  93. if (wait_xfer_done(adap))
  94. return -EIO;
  95. buf[i] = read_data();
  96. }
  97. set_control(SGI_I2C_RELEASE_BUS | SGI_I2C_FORCE_IDLE);
  98. return 0;
  99. }
  100. static int i2c_write(struct i2c_algo_sgi_data *adap, unsigned char *buf,
  101. unsigned int len)
  102. {
  103. int i;
  104. /* We are already in write state */
  105. for (i = 0; i < len; i++) {
  106. write_data(buf[i]);
  107. if (wait_ack(adap))
  108. return -EIO;
  109. }
  110. return 0;
  111. }
  112. static int sgi_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
  113. int num)
  114. {
  115. struct i2c_algo_sgi_data *adap = i2c_adap->algo_data;
  116. struct i2c_msg *p;
  117. int i, err = 0;
  118. for (i = 0; !err && i < num; i++) {
  119. p = &msgs[i];
  120. err = do_address(adap, p->addr, p->flags & I2C_M_RD);
  121. if (err || !p->len)
  122. continue;
  123. if (p->flags & I2C_M_RD)
  124. err = i2c_read(adap, p->buf, p->len);
  125. else
  126. err = i2c_write(adap, p->buf, p->len);
  127. }
  128. return (err < 0) ? err : i;
  129. }
  130. static u32 sgi_func(struct i2c_adapter *adap)
  131. {
  132. return I2C_FUNC_SMBUS_EMUL;
  133. }
  134. static const struct i2c_algorithm sgi_algo = {
  135. .master_xfer = sgi_xfer,
  136. .functionality = sgi_func,
  137. };
  138. /*
  139. * registering functions to load algorithms at runtime
  140. */
  141. int i2c_sgi_add_bus(struct i2c_adapter *adap)
  142. {
  143. adap->algo = &sgi_algo;
  144. return i2c_add_adapter(adap);
  145. }
  146. int i2c_sgi_del_bus(struct i2c_adapter *adap)
  147. {
  148. return i2c_del_adapter(adap);
  149. }
  150. EXPORT_SYMBOL(i2c_sgi_add_bus);
  151. EXPORT_SYMBOL(i2c_sgi_del_bus);
  152. MODULE_AUTHOR("Ladislav Michl <ladis@linux-mips.org>");
  153. MODULE_DESCRIPTION("I2C-Bus SGI algorithm");
  154. MODULE_LICENSE("GPL");