via_i2c.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
  3. * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License as published by the Free Software Foundation;
  7. * either version 2, or (at your option) any later version.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTIES OR REPRESENTATIONS; without even
  10. * the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. * A PARTICULAR PURPOSE.See the GNU General Public License
  12. * for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc.,
  16. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "global.h"
  19. /*
  20. * There can only be one set of these, so there's no point in having
  21. * them be dynamically allocated...
  22. */
  23. #define VIAFB_NUM_I2C 5
  24. static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
  25. static void via_i2c_setscl(void *data, int state)
  26. {
  27. u8 val;
  28. struct via_port_cfg *adap_data = data;
  29. val = viafb_read_reg(adap_data->io_port,
  30. adap_data->ioport_index) & 0xF0;
  31. if (state)
  32. val |= 0x20;
  33. else
  34. val &= ~0x20;
  35. switch (adap_data->type) {
  36. case VIA_PORT_I2C:
  37. val |= 0x01;
  38. break;
  39. case VIA_PORT_GPIO:
  40. val |= 0x80;
  41. break;
  42. default:
  43. DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
  44. }
  45. viafb_write_reg(adap_data->ioport_index,
  46. adap_data->io_port, val);
  47. }
  48. static int via_i2c_getscl(void *data)
  49. {
  50. struct via_port_cfg *adap_data = data;
  51. if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
  52. return 1;
  53. return 0;
  54. }
  55. static int via_i2c_getsda(void *data)
  56. {
  57. struct via_port_cfg *adap_data = data;
  58. if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
  59. return 1;
  60. return 0;
  61. }
  62. static void via_i2c_setsda(void *data, int state)
  63. {
  64. u8 val;
  65. struct via_port_cfg *adap_data = data;
  66. val = viafb_read_reg(adap_data->io_port,
  67. adap_data->ioport_index) & 0xF0;
  68. if (state)
  69. val |= 0x10;
  70. else
  71. val &= ~0x10;
  72. switch (adap_data->type) {
  73. case VIA_PORT_I2C:
  74. val |= 0x01;
  75. break;
  76. case VIA_PORT_GPIO:
  77. val |= 0x40;
  78. break;
  79. default:
  80. DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
  81. }
  82. viafb_write_reg(adap_data->ioport_index,
  83. adap_data->io_port, val);
  84. }
  85. int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
  86. {
  87. u8 mm1[] = {0x00};
  88. struct i2c_msg msgs[2];
  89. *pdata = 0;
  90. msgs[0].flags = 0;
  91. msgs[1].flags = I2C_M_RD;
  92. msgs[0].addr = msgs[1].addr = slave_addr / 2;
  93. mm1[0] = index;
  94. msgs[0].len = 1; msgs[1].len = 1;
  95. msgs[0].buf = mm1; msgs[1].buf = pdata;
  96. return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
  97. }
  98. int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
  99. {
  100. u8 msg[2] = { index, data };
  101. struct i2c_msg msgs;
  102. msgs.flags = 0;
  103. msgs.addr = slave_addr / 2;
  104. msgs.len = 2;
  105. msgs.buf = msg;
  106. return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
  107. }
  108. int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
  109. {
  110. u8 mm1[] = {0x00};
  111. struct i2c_msg msgs[2];
  112. msgs[0].flags = 0;
  113. msgs[1].flags = I2C_M_RD;
  114. msgs[0].addr = msgs[1].addr = slave_addr / 2;
  115. mm1[0] = index;
  116. msgs[0].len = 1; msgs[1].len = buff_len;
  117. msgs[0].buf = mm1; msgs[1].buf = buff;
  118. return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
  119. }
  120. static int create_i2c_bus(struct i2c_adapter *adapter,
  121. struct i2c_algo_bit_data *algo,
  122. struct via_port_cfg *adap_cfg,
  123. struct pci_dev *pdev)
  124. {
  125. DEBUG_MSG(KERN_DEBUG "viafb: creating bus adap=0x%p, algo_bit_data=0x%p, adap_cfg=0x%p\n", adapter, algo, adap_cfg);
  126. algo->setsda = via_i2c_setsda;
  127. algo->setscl = via_i2c_setscl;
  128. algo->getsda = via_i2c_getsda;
  129. algo->getscl = via_i2c_getscl;
  130. algo->udelay = 40;
  131. algo->timeout = 20;
  132. algo->data = adap_cfg;
  133. sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
  134. adap_cfg->ioport_index);
  135. adapter->owner = THIS_MODULE;
  136. adapter->id = 0x01FFFF;
  137. adapter->class = I2C_CLASS_DDC;
  138. adapter->algo_data = algo;
  139. if (pdev)
  140. adapter->dev.parent = &pdev->dev;
  141. else
  142. adapter->dev.parent = NULL;
  143. /* i2c_set_adapdata(adapter, adap_cfg); */
  144. /* Raise SCL and SDA */
  145. via_i2c_setsda(adap_cfg, 1);
  146. via_i2c_setscl(adap_cfg, 1);
  147. udelay(20);
  148. return i2c_bit_add_bus(adapter);
  149. }
  150. int viafb_create_i2c_busses(struct via_port_cfg *configs)
  151. {
  152. int i, ret;
  153. for (i = 0; i < VIAFB_NUM_PORTS; i++) {
  154. struct via_port_cfg *adap_cfg = configs++;
  155. struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
  156. if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
  157. continue;
  158. ret = create_i2c_bus(&i2c_stuff->adapter,
  159. &i2c_stuff->algo, adap_cfg,
  160. NULL); /* FIXME: PCIDEV */
  161. if (ret < 0) {
  162. printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
  163. i, ret);
  164. /* FIXME: properly release previous busses */
  165. return ret;
  166. }
  167. }
  168. return 0;
  169. }
  170. void viafb_delete_i2c_busses(void)
  171. {
  172. int i;
  173. for (i = 0; i < VIAFB_NUM_PORTS; i++) {
  174. struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
  175. /*
  176. * Only remove those entries in the array that we've
  177. * actually used (and thus initialized algo_data)
  178. */
  179. if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
  180. i2c_del_adapter(&i2c_stuff->adapter);
  181. }
  182. }