via_i2c.c 7.1 KB

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