ngene-i2c.c 4.5 KB

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