ngene-i2c.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/smp_lock.h>
  40. #include <linux/timer.h>
  41. #include <linux/version.h>
  42. #include <linux/byteorder/generic.h>
  43. #include <linux/firmware.h>
  44. #include <linux/vmalloc.h>
  45. #include "ngene.h"
  46. /* Firmware command for i2c operations */
  47. static int ngene_command_i2c_read(struct ngene *dev, u8 adr,
  48. u8 *out, u8 outlen, u8 *in, u8 inlen, int flag)
  49. {
  50. struct ngene_command com;
  51. com.cmd.hdr.Opcode = CMD_I2C_READ;
  52. com.cmd.hdr.Length = outlen + 3;
  53. com.cmd.I2CRead.Device = adr << 1;
  54. memcpy(com.cmd.I2CRead.Data, out, outlen);
  55. com.cmd.I2CRead.Data[outlen] = inlen;
  56. com.cmd.I2CRead.Data[outlen + 1] = 0;
  57. com.in_len = outlen + 3;
  58. com.out_len = inlen + 1;
  59. if (ngene_command(dev, &com) < 0)
  60. return -EIO;
  61. if ((com.cmd.raw8[0] >> 1) != adr)
  62. return -EIO;
  63. if (flag)
  64. memcpy(in, com.cmd.raw8, inlen + 1);
  65. else
  66. memcpy(in, com.cmd.raw8 + 1, inlen);
  67. return 0;
  68. }
  69. static int ngene_command_i2c_write(struct ngene *dev, u8 adr,
  70. u8 *out, u8 outlen)
  71. {
  72. struct ngene_command com;
  73. com.cmd.hdr.Opcode = CMD_I2C_WRITE;
  74. com.cmd.hdr.Length = outlen + 1;
  75. com.cmd.I2CRead.Device = adr << 1;
  76. memcpy(com.cmd.I2CRead.Data, out, outlen);
  77. com.in_len = outlen + 1;
  78. com.out_len = 1;
  79. if (ngene_command(dev, &com) < 0)
  80. return -EIO;
  81. if (com.cmd.raw8[0] == 1)
  82. return -EIO;
  83. return 0;
  84. }
  85. static void ngene_i2c_set_bus(struct ngene *dev, int bus)
  86. {
  87. if (!(dev->card_info->i2c_access & 2))
  88. return;
  89. if (dev->i2c_current_bus == bus)
  90. return;
  91. switch (bus) {
  92. case 0:
  93. ngene_command_gpio_set(dev, 3, 0);
  94. ngene_command_gpio_set(dev, 2, 1);
  95. break;
  96. case 1:
  97. ngene_command_gpio_set(dev, 2, 0);
  98. ngene_command_gpio_set(dev, 3, 1);
  99. break;
  100. }
  101. dev->i2c_current_bus = bus;
  102. }
  103. static int ngene_i2c_master_xfer(struct i2c_adapter *adapter,
  104. struct i2c_msg msg[], int num)
  105. {
  106. struct ngene_channel *chan =
  107. (struct ngene_channel *)i2c_get_adapdata(adapter);
  108. struct ngene *dev = chan->dev;
  109. down(&dev->i2c_switch_mutex);
  110. ngene_i2c_set_bus(dev, chan->number);
  111. if (num == 2 && msg[1].flags & I2C_M_RD && !(msg[0].flags & I2C_M_RD))
  112. if (!ngene_command_i2c_read(dev, msg[0].addr,
  113. msg[0].buf, msg[0].len,
  114. msg[1].buf, msg[1].len, 0))
  115. goto done;
  116. if (num == 1 && !(msg[0].flags & I2C_M_RD))
  117. if (!ngene_command_i2c_write(dev, msg[0].addr,
  118. msg[0].buf, msg[0].len))
  119. goto done;
  120. if (num == 1 && (msg[0].flags & I2C_M_RD))
  121. if (!ngene_command_i2c_read(dev, msg[0].addr, NULL, 0,
  122. msg[0].buf, msg[0].len, 0))
  123. goto done;
  124. up(&dev->i2c_switch_mutex);
  125. return -EIO;
  126. done:
  127. up(&dev->i2c_switch_mutex);
  128. return num;
  129. }
  130. static u32 ngene_i2c_functionality(struct i2c_adapter *adap)
  131. {
  132. return I2C_FUNC_SMBUS_EMUL;
  133. }
  134. static struct i2c_algorithm ngene_i2c_algo = {
  135. .master_xfer = ngene_i2c_master_xfer,
  136. .functionality = ngene_i2c_functionality,
  137. };
  138. int ngene_i2c_init(struct ngene *dev, int dev_nr)
  139. {
  140. struct i2c_adapter *adap = &(dev->channel[dev_nr].i2c_adapter);
  141. i2c_set_adapdata(adap, &(dev->channel[dev_nr]));
  142. adap->class = I2C_CLASS_TV_DIGITAL | I2C_CLASS_TV_ANALOG;
  143. strcpy(adap->name, "nGene");
  144. adap->algo = &ngene_i2c_algo;
  145. adap->algo_data = (void *)&(dev->channel[dev_nr]);
  146. adap->dev.parent = &dev->pci_dev->dev;
  147. return i2c_add_adapter(adap);
  148. }