/drivers/i2c/algos/i2c-algo-sibyte.c

https://bitbucket.org/evzijst/gittest · C · 222 lines · 148 code · 38 blank · 36 comment · 23 complexity · b72b1c8b8b1e84e2f2773d54dd86cb8e MD5 · raw file

  1. /* ------------------------------------------------------------------------- */
  2. /* i2c-algo-sibyte.c i2c driver algorithms for bit-shift adapters */
  3. /* ------------------------------------------------------------------------- */
  4. /* Copyright (C) 2001,2002,2003 Broadcom Corporation
  5. Copyright (C) 1995-2000 Simon G. Vogl
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  17. /* ------------------------------------------------------------------------- */
  18. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
  19. Frodo Looijaard <frodol@dds.nl>. */
  20. /* Ported for SiByte SOCs by Broadcom Corporation. */
  21. #include <linux/config.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <asm/io.h>
  26. #include <asm/sibyte/sb1250_regs.h>
  27. #include <asm/sibyte/sb1250_smbus.h>
  28. #include <linux/i2c.h>
  29. #include <linux/i2c-algo-sibyte.h>
  30. /* ----- global defines ----------------------------------------------- */
  31. #define SMB_CSR(a,r) ((long)(a->reg_base + r))
  32. /* ----- global variables --------------------------------------------- */
  33. /* module parameters:
  34. */
  35. static int bit_scan=0; /* have a look at what's hanging 'round */
  36. static int smbus_xfer(struct i2c_adapter *i2c_adap, u16 addr,
  37. unsigned short flags, char read_write,
  38. u8 command, int size, union i2c_smbus_data * data)
  39. {
  40. struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
  41. int data_bytes = 0;
  42. int error;
  43. while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
  44. ;
  45. switch (size) {
  46. case I2C_SMBUS_QUICK:
  47. csr_out32((V_SMB_ADDR(addr) | (read_write == I2C_SMBUS_READ ? M_SMB_QDATA : 0) |
  48. V_SMB_TT_QUICKCMD), SMB_CSR(adap, R_SMB_START));
  49. break;
  50. case I2C_SMBUS_BYTE:
  51. if (read_write == I2C_SMBUS_READ) {
  52. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_RD1BYTE),
  53. SMB_CSR(adap, R_SMB_START));
  54. data_bytes = 1;
  55. } else {
  56. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  57. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR1BYTE),
  58. SMB_CSR(adap, R_SMB_START));
  59. }
  60. break;
  61. case I2C_SMBUS_BYTE_DATA:
  62. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  63. if (read_write == I2C_SMBUS_READ) {
  64. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD1BYTE),
  65. SMB_CSR(adap, R_SMB_START));
  66. data_bytes = 1;
  67. } else {
  68. csr_out32(V_SMB_LB(data->byte), SMB_CSR(adap, R_SMB_DATA));
  69. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
  70. SMB_CSR(adap, R_SMB_START));
  71. }
  72. break;
  73. case I2C_SMBUS_WORD_DATA:
  74. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  75. if (read_write == I2C_SMBUS_READ) {
  76. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD2BYTE),
  77. SMB_CSR(adap, R_SMB_START));
  78. data_bytes = 2;
  79. } else {
  80. csr_out32(V_SMB_LB(data->word & 0xff), SMB_CSR(adap, R_SMB_DATA));
  81. csr_out32(V_SMB_MB(data->word >> 8), SMB_CSR(adap, R_SMB_DATA));
  82. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
  83. SMB_CSR(adap, R_SMB_START));
  84. }
  85. break;
  86. default:
  87. return -1; /* XXXKW better error code? */
  88. }
  89. while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
  90. ;
  91. error = csr_in32(SMB_CSR(adap, R_SMB_STATUS));
  92. if (error & M_SMB_ERROR) {
  93. /* Clear error bit by writing a 1 */
  94. csr_out32(M_SMB_ERROR, SMB_CSR(adap, R_SMB_STATUS));
  95. return -1; /* XXXKW better error code? */
  96. }
  97. if (data_bytes == 1)
  98. data->byte = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xff;
  99. if (data_bytes == 2)
  100. data->word = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xffff;
  101. return 0;
  102. }
  103. static int algo_control(struct i2c_adapter *adapter,
  104. unsigned int cmd, unsigned long arg)
  105. {
  106. return 0;
  107. }
  108. static u32 bit_func(struct i2c_adapter *adap)
  109. {
  110. return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  111. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA);
  112. }
  113. /* -----exported algorithm data: ------------------------------------- */
  114. static struct i2c_algorithm i2c_sibyte_algo = {
  115. .name = "SiByte algorithm",
  116. .id = I2C_ALGO_SIBYTE,
  117. .smbus_xfer = smbus_xfer,
  118. .algo_control = algo_control, /* ioctl */
  119. .functionality = bit_func,
  120. };
  121. /*
  122. * registering functions to load algorithms at runtime
  123. */
  124. int i2c_sibyte_add_bus(struct i2c_adapter *i2c_adap, int speed)
  125. {
  126. int i;
  127. struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
  128. /* register new adapter to i2c module... */
  129. i2c_adap->id |= i2c_sibyte_algo.id;
  130. i2c_adap->algo = &i2c_sibyte_algo;
  131. /* Set the frequency to 100 kHz */
  132. csr_out32(speed, SMB_CSR(adap,R_SMB_FREQ));
  133. csr_out32(0, SMB_CSR(adap,R_SMB_CONTROL));
  134. /* scan bus */
  135. if (bit_scan) {
  136. union i2c_smbus_data data;
  137. int rc;
  138. printk(KERN_INFO " i2c-algo-sibyte.o: scanning bus %s.\n",
  139. i2c_adap->name);
  140. for (i = 0x00; i < 0x7f; i++) {
  141. /* XXXKW is this a realistic probe? */
  142. rc = smbus_xfer(i2c_adap, i, 0, I2C_SMBUS_READ, 0,
  143. I2C_SMBUS_BYTE_DATA, &data);
  144. if (!rc) {
  145. printk("(%02x)",i);
  146. } else
  147. printk(".");
  148. }
  149. printk("\n");
  150. }
  151. i2c_add_adapter(i2c_adap);
  152. return 0;
  153. }
  154. int i2c_sibyte_del_bus(struct i2c_adapter *adap)
  155. {
  156. int res;
  157. if ((res = i2c_del_adapter(adap)) < 0)
  158. return res;
  159. return 0;
  160. }
  161. int __init i2c_algo_sibyte_init (void)
  162. {
  163. printk("i2c-algo-sibyte.o: i2c SiByte algorithm module\n");
  164. return 0;
  165. }
  166. EXPORT_SYMBOL(i2c_sibyte_add_bus);
  167. EXPORT_SYMBOL(i2c_sibyte_del_bus);
  168. #ifdef MODULE
  169. MODULE_AUTHOR("Kip Walker, Broadcom Corp.");
  170. MODULE_DESCRIPTION("SiByte I2C-Bus algorithm");
  171. MODULE_PARM(bit_scan, "i");
  172. MODULE_PARM_DESC(bit_scan, "Scan for active chips on the bus");
  173. MODULE_LICENSE("GPL");
  174. int init_module(void)
  175. {
  176. return i2c_algo_sibyte_init();
  177. }
  178. void cleanup_module(void)
  179. {
  180. }
  181. #endif