ngene-i2c.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ngene-i2c.c: nGene PCIe bridge driver i2c functions
  3. *
  4. * Copyright (C) 2005-2007 Micronas
  5. *
  6. * Copyright (C) 2008-2009 Ralph Metzler <rjkm@metzlerbros.de>
  7. * Modifications for new nGene firmware,
  8. * support for EEPROM-copying,
  9. * support for new dual DVB-S2 card prototype
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2 only, as published by the Free Software Foundation.
  15. *
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  26. * 02110-1301, USA
  27. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  28. */
  29. /* FIXME - some of these can probably be removed */
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/delay.h>
  33. #include <linux/slab.h>
  34. #include <linux/poll.h>
  35. #include <linux/io.h>
  36. #include <asm/div64.h>
  37. #include <linux/pci.h>
  38. #include <linux/pci_ids.h>
  39. #include <linux/timer.h>
  40. #include <linux/byteorder/generic.h>
  41. #include <linux/firmware.h>
  42. #include <linux/vmalloc.h>
  43. #include "ngene.h"
  44. /* Firmware command for i2c operations */
  45. static int ngene_command_i2c_read(struct ngene *dev, u8 adr,
  46. u8 *out, u8 outlen, u8 *in, u8 inlen, int flag)
  47. {
  48. struct ngene_command com;
  49. com.cmd.hdr.Opcode = CMD_I2C_READ;
  50. com.cmd.hdr.Length = outlen + 3;
  51. com.cmd.I2CRead.Device = adr << 1;
  52. memcpy(com.cmd.I2CRead.Data, out, outlen);
  53. com.cmd.I2CRead.Data[outlen] = inlen;
  54. com.cmd.I2CRead.Data[outlen + 1] = 0;
  55. com.in_len = outlen + 3;
  56. com.out_len = inlen + 1;
  57. if (ngene_command(dev, &com) < 0)
  58. return -EIO;
  59. if ((com.cmd.raw8[0] >> 1) != adr)
  60. return -EIO;
  61. if (flag)
  62. memcpy(in, com.cmd.raw8, inlen + 1);
  63. else
  64. memcpy(in, com.cmd.raw8 + 1, inlen);
  65. return 0;
  66. }
  67. static int ngene_command_i2c_write(struct ngene *dev, u8 adr,
  68. u8 *out, u8 outlen)
  69. {
  70. struct ngene_command com;
  71. com.cmd.hdr.Opcode = CMD_I2C_WRITE;
  72. com.cmd.hdr.Length = outlen + 1;
  73. com.cmd.I2CRead.Device = adr << 1;
  74. memcpy(com.cmd.I2CRead.Data, out, outlen);
  75. com.in_len = outlen + 1;
  76. com.out_len = 1;
  77. if (ngene_command(dev, &com) < 0)
  78. return -EIO;
  79. if (com.cmd.raw8[0] == 1)
  80. return -EIO;
  81. return 0;
  82. }
  83. static void ngene_i2c_set_bus(struct ngene *dev, int bus)
  84. {
  85. if (!(dev->card_info->i2c_access & 2))
  86. return;
  87. if (dev->i2c_current_bus == bus)
  88. return;
  89. switch (bus) {
  90. case 0:
  91. ngene_command_gpio_set(dev, 3, 0);
  92. ngene_command_gpio_set(dev, 2, 1);
  93. break;
  94. case 1:
  95. ngene_command_gpio_set(dev, 2, 0);
  96. ngene_command_gpio_set(dev, 3, 1);
  97. break;
  98. }
  99. dev->i2c_current_bus = bus;
  100. }
  101. static int ngene_i2c_master_xfer(struct i2c_adapter *adapter,
  102. struct i2c_msg msg[], int num)
  103. {
  104. struct ngene_channel *chan =
  105. (struct ngene_channel *)i2c_get_adapdata(adapter);
  106. struct ngene *dev = chan->dev;
  107. down(&dev->i2c_switch_mutex);
  108. ngene_i2c_set_bus(dev, chan->number);
  109. if (num == 2 && msg[1].flags & I2C_M_RD && !(msg[0].flags & I2C_M_RD))
  110. if (!ngene_command_i2c_read(dev, msg[0].addr,
  111. msg[0].buf, msg[0].len,
  112. msg[1].buf, msg[1].len, 0))
  113. goto done;
  114. if (num == 1 && !(msg[0].flags & I2C_M_RD))
  115. if (!ngene_command_i2c_write(dev, msg[0].addr,
  116. msg[0].buf, msg[0].len))
  117. goto done;
  118. if (num == 1 && (msg[0].flags & I2C_M_RD))
  119. if (!ngene_command_i2c_read(dev, msg[0].addr, NULL, 0,
  120. msg[0].buf, msg[0].len, 0))
  121. goto done;
  122. up(&dev->i2c_switch_mutex);
  123. return -EIO;
  124. done:
  125. up(&dev->i2c_switch_mutex);
  126. return num;
  127. }
  128. static u32 ngene_i2c_functionality(struct i2c_adapter *adap)
  129. {
  130. return I2C_FUNC_SMBUS_EMUL;
  131. }
  132. static struct i2c_algorithm ngene_i2c_algo = {
  133. .master_xfer = ngene_i2c_master_xfer,
  134. .functionality = ngene_i2c_functionality,
  135. };
  136. int ngene_i2c_init(struct ngene *dev, int dev_nr)
  137. {
  138. struct i2c_adapter *adap = &(dev->channel[dev_nr].i2c_adapter);
  139. i2c_set_adapdata(adap, &(dev->channel[dev_nr]));
  140. strcpy(adap->name, "nGene");
  141. adap->algo = &ngene_i2c_algo;
  142. adap->algo_data = (void *)&(dev->channel[dev_nr]);
  143. adap->dev.parent = &dev->pci_dev->dev;
  144. return i2c_add_adapter(adap);
  145. }