via_i2c.c 6.4 KB

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