mb862xx-i2c.c 3.7 KB

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