via_i2c.c 5.5 KB

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