via_i2c.c 5.8 KB

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