via_i2c.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "via-core.h"
  19. #include "via_i2c.h"
  20. #include "global.h"
  21. /*
  22. * There can only be one set of these, so there's no point in having
  23. * them be dynamically allocated...
  24. */
  25. #define VIAFB_NUM_I2C 5
  26. static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
  27. static void via_i2c_setscl(void *data, int state)
  28. {
  29. u8 val;
  30. struct via_port_cfg *adap_data = data;
  31. val = viafb_read_reg(adap_data->io_port,
  32. adap_data->ioport_index) & 0xF0;
  33. if (state)
  34. val |= 0x20;
  35. else
  36. val &= ~0x20;
  37. switch (adap_data->type) {
  38. case VIA_PORT_I2C:
  39. val |= 0x01;
  40. break;
  41. case VIA_PORT_GPIO:
  42. val |= 0x80;
  43. break;
  44. default:
  45. DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
  46. }
  47. viafb_write_reg(adap_data->ioport_index,
  48. adap_data->io_port, val);
  49. }
  50. static int via_i2c_getscl(void *data)
  51. {
  52. struct via_port_cfg *adap_data = data;
  53. if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
  54. return 1;
  55. return 0;
  56. }
  57. static int via_i2c_getsda(void *data)
  58. {
  59. struct via_port_cfg *adap_data = data;
  60. if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
  61. return 1;
  62. return 0;
  63. }
  64. static void via_i2c_setsda(void *data, int state)
  65. {
  66. u8 val;
  67. struct via_port_cfg *adap_data = data;
  68. val = viafb_read_reg(adap_data->io_port,
  69. adap_data->ioport_index) & 0xF0;
  70. if (state)
  71. val |= 0x10;
  72. else
  73. val &= ~0x10;
  74. switch (adap_data->type) {
  75. case VIA_PORT_I2C:
  76. val |= 0x01;
  77. break;
  78. case VIA_PORT_GPIO:
  79. val |= 0x40;
  80. break;
  81. default:
  82. DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
  83. }
  84. viafb_write_reg(adap_data->ioport_index,
  85. adap_data->io_port, val);
  86. }
  87. int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
  88. {
  89. u8 mm1[] = {0x00};
  90. struct i2c_msg msgs[2];
  91. *pdata = 0;
  92. msgs[0].flags = 0;
  93. msgs[1].flags = I2C_M_RD;
  94. msgs[0].addr = msgs[1].addr = slave_addr / 2;
  95. mm1[0] = index;
  96. msgs[0].len = 1; msgs[1].len = 1;
  97. msgs[0].buf = mm1; msgs[1].buf = pdata;
  98. return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
  99. }
  100. int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
  101. {
  102. u8 msg[2] = { index, data };
  103. struct i2c_msg msgs;
  104. msgs.flags = 0;
  105. msgs.addr = slave_addr / 2;
  106. msgs.len = 2;
  107. msgs.buf = msg;
  108. return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
  109. }
  110. int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
  111. {
  112. u8 mm1[] = {0x00};
  113. struct i2c_msg msgs[2];
  114. msgs[0].flags = 0;
  115. msgs[1].flags = I2C_M_RD;
  116. msgs[0].addr = msgs[1].addr = slave_addr / 2;
  117. mm1[0] = index;
  118. msgs[0].len = 1; msgs[1].len = buff_len;
  119. msgs[0].buf = mm1; msgs[1].buf = buff;
  120. return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
  121. }
  122. static int create_i2c_bus(struct i2c_adapter *adapter,
  123. struct i2c_algo_bit_data *algo,
  124. struct via_port_cfg *adap_cfg,
  125. struct pci_dev *pdev)
  126. {
  127. DEBUG_MSG(KERN_DEBUG "viafb: creating bus adap=0x%p, algo_bit_data=0x%p, adap_cfg=0x%p\n", adapter, algo, adap_cfg);
  128. algo->setsda = via_i2c_setsda;
  129. algo->setscl = via_i2c_setscl;
  130. algo->getsda = via_i2c_getsda;
  131. algo->getscl = via_i2c_getscl;
  132. algo->udelay = 40;
  133. algo->timeout = 20;
  134. algo->data = adap_cfg;
  135. sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
  136. adap_cfg->ioport_index);
  137. adapter->owner = THIS_MODULE;
  138. adapter->id = 0x01FFFF;
  139. adapter->class = I2C_CLASS_DDC;
  140. adapter->algo_data = algo;
  141. if (pdev)
  142. adapter->dev.parent = &pdev->dev;
  143. else
  144. adapter->dev.parent = NULL;
  145. /* i2c_set_adapdata(adapter, adap_cfg); */
  146. /* Raise SCL and SDA */
  147. via_i2c_setsda(adap_cfg, 1);
  148. via_i2c_setscl(adap_cfg, 1);
  149. udelay(20);
  150. return i2c_bit_add_bus(adapter);
  151. }
  152. int viafb_create_i2c_busses(struct via_port_cfg *configs)
  153. {
  154. int i, ret;
  155. for (i = 0; i < VIAFB_NUM_PORTS; i++) {
  156. struct via_port_cfg *adap_cfg = configs++;
  157. struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
  158. if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
  159. continue;
  160. ret = create_i2c_bus(&i2c_stuff->adapter,
  161. &i2c_stuff->algo, adap_cfg,
  162. NULL); /* FIXME: PCIDEV */
  163. if (ret < 0) {
  164. printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
  165. i, ret);
  166. /* FIXME: properly release previous busses */
  167. return ret;
  168. }
  169. }
  170. return 0;
  171. }
  172. void viafb_delete_i2c_busses(void)
  173. {
  174. int i;
  175. for (i = 0; i < VIAFB_NUM_PORTS; i++) {
  176. struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
  177. /*
  178. * Only remove those entries in the array that we've
  179. * actually used (and thus initialized algo_data)
  180. */
  181. if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
  182. i2c_del_adapter(&i2c_stuff->adapter);
  183. }
  184. }