mb862xx-i2c.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Coral-P(A)/Lime I2C adapter driver
  3. *
  4. * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/fb.h>
  12. #include <linux/i2c.h>
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include "mb862xxfb.h"
  16. #include "mb862xx_reg.h"
  17. static int mb862xx_i2c_wait_event(struct i2c_adapter *adap)
  18. {
  19. struct mb862xxfb_par *par = adap->algo_data;
  20. u32 reg;
  21. do {
  22. udelay(1);
  23. reg = inreg(i2c, GC_I2C_BCR);
  24. if (reg & (I2C_INT | I2C_BER))
  25. break;
  26. } while (1);
  27. return (reg & I2C_BER) ? 0 : 1;
  28. }
  29. static int mb862xx_i2c_do_address(struct i2c_adapter *adap, int addr)
  30. {
  31. struct mb862xxfb_par *par = adap->algo_data;
  32. outreg(i2c, GC_I2C_DAR, addr);
  33. outreg(i2c, GC_I2C_CCR, I2C_CLOCK_AND_ENABLE);
  34. outreg(i2c, GC_I2C_BCR, par->i2c_rs ? I2C_REPEATED_START : I2C_START);
  35. if (!mb862xx_i2c_wait_event(adap))
  36. return -EIO;
  37. par->i2c_rs = !(inreg(i2c, GC_I2C_BSR) & I2C_LRB);
  38. return par->i2c_rs;
  39. }
  40. static int mb862xx_i2c_write_byte(struct i2c_adapter *adap, u8 byte)
  41. {
  42. struct mb862xxfb_par *par = adap->algo_data;
  43. outreg(i2c, GC_I2C_DAR, byte);
  44. outreg(i2c, GC_I2C_BCR, I2C_START);
  45. if (!mb862xx_i2c_wait_event(adap))
  46. return -EIO;
  47. return !(inreg(i2c, GC_I2C_BSR) & I2C_LRB);
  48. }
  49. static int mb862xx_i2c_read_byte(struct i2c_adapter *adap, u8 *byte, int last)
  50. {
  51. struct mb862xxfb_par *par = adap->algo_data;
  52. outreg(i2c, GC_I2C_BCR, I2C_START | (last ? 0 : I2C_ACK));
  53. if (!mb862xx_i2c_wait_event(adap))
  54. return 0;
  55. *byte = inreg(i2c, GC_I2C_DAR);
  56. return 1;
  57. }
  58. void mb862xx_i2c_stop(struct i2c_adapter *adap)
  59. {
  60. struct mb862xxfb_par *par = adap->algo_data;
  61. outreg(i2c, GC_I2C_BCR, I2C_STOP);
  62. outreg(i2c, GC_I2C_CCR, I2C_DISABLE);
  63. par->i2c_rs = 0;
  64. }
  65. static int mb862xx_i2c_read(struct i2c_adapter *adap, struct i2c_msg *m)
  66. {
  67. int i, ret = 0;
  68. int last = m->len - 1;
  69. for (i = 0; i < m->len; i++) {
  70. if (!mb862xx_i2c_read_byte(adap, &m->buf[i], i == last)) {
  71. ret = -EIO;
  72. break;
  73. }
  74. }
  75. return ret;
  76. }
  77. static int mb862xx_i2c_write(struct i2c_adapter *adap, struct i2c_msg *m)
  78. {
  79. int i, ret = 0;
  80. for (i = 0; i < m->len; i++) {
  81. if (!mb862xx_i2c_write_byte(adap, m->buf[i])) {
  82. ret = -EIO;
  83. break;
  84. }
  85. }
  86. return ret;
  87. }
  88. static int mb862xx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  89. int num)
  90. {
  91. struct mb862xxfb_par *par = adap->algo_data;
  92. struct i2c_msg *m;
  93. int addr;
  94. int i = 0, err = 0;
  95. dev_dbg(par->dev, "%s: %d msgs\n", __func__, num);
  96. for (i = 0; i < num; i++) {
  97. m = &msgs[i];
  98. if (!m->len) {
  99. dev_dbg(par->dev, "%s: null msgs\n", __func__);
  100. continue;
  101. }
  102. addr = m->addr;
  103. if (m->flags & I2C_M_RD)
  104. addr |= 1;
  105. err = mb862xx_i2c_do_address(adap, addr);
  106. if (err < 0)
  107. break;
  108. if (m->flags & I2C_M_RD)
  109. err = mb862xx_i2c_read(adap, m);
  110. else
  111. err = mb862xx_i2c_write(adap, m);
  112. }
  113. if (i)
  114. mb862xx_i2c_stop(adap);
  115. return (err < 0) ? err : i;
  116. }
  117. static u32 mb862xx_func(struct i2c_adapter *adap)
  118. {
  119. return I2C_FUNC_SMBUS_BYTE_DATA;
  120. }
  121. static const struct i2c_algorithm mb862xx_algo = {
  122. .master_xfer = mb862xx_xfer,
  123. .functionality = mb862xx_func,
  124. };
  125. static struct i2c_adapter mb862xx_i2c_adapter = {
  126. .name = "MB862xx I2C adapter",
  127. .algo = &mb862xx_algo,
  128. .owner = THIS_MODULE,
  129. };
  130. int mb862xx_i2c_init(struct mb862xxfb_par *par)
  131. {
  132. int ret;
  133. mb862xx_i2c_adapter.algo_data = par;
  134. par->adap = &mb862xx_i2c_adapter;
  135. ret = i2c_add_adapter(par->adap);
  136. if (ret < 0) {
  137. dev_err(par->dev, "failed to add %s\n",
  138. mb862xx_i2c_adapter.name);
  139. }
  140. return ret;
  141. }
  142. void mb862xx_i2c_exit(struct mb862xxfb_par *par)
  143. {
  144. if (par->adap) {
  145. i2c_del_adapter(par->adap);
  146. par->adap = NULL;
  147. }
  148. }