ngene-i2c.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. * To obtain the license, point your browser to
  23. * http://www.gnu.org/copyleft/gpl.html
  24. */
  25. /* FIXME - some of these can probably be removed */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/poll.h>
  31. #include <linux/io.h>
  32. #include <asm/div64.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci_ids.h>
  35. #include <linux/timer.h>
  36. #include <linux/byteorder/generic.h>
  37. #include <linux/firmware.h>
  38. #include <linux/vmalloc.h>
  39. #include "ngene.h"
  40. /* Firmware command for i2c operations */
  41. static int ngene_command_i2c_read(struct ngene *dev, u8 adr,
  42. u8 *out, u8 outlen, u8 *in, u8 inlen, int flag)
  43. {
  44. struct ngene_command com;
  45. com.cmd.hdr.Opcode = CMD_I2C_READ;
  46. com.cmd.hdr.Length = outlen + 3;
  47. com.cmd.I2CRead.Device = adr << 1;
  48. memcpy(com.cmd.I2CRead.Data, out, outlen);
  49. com.cmd.I2CRead.Data[outlen] = inlen;
  50. com.cmd.I2CRead.Data[outlen + 1] = 0;
  51. com.in_len = outlen + 3;
  52. com.out_len = inlen + 1;
  53. if (ngene_command(dev, &com) < 0)
  54. return -EIO;
  55. if ((com.cmd.raw8[0] >> 1) != adr)
  56. return -EIO;
  57. if (flag)
  58. memcpy(in, com.cmd.raw8, inlen + 1);
  59. else
  60. memcpy(in, com.cmd.raw8 + 1, inlen);
  61. return 0;
  62. }
  63. static int ngene_command_i2c_write(struct ngene *dev, u8 adr,
  64. u8 *out, u8 outlen)
  65. {
  66. struct ngene_command com;
  67. com.cmd.hdr.Opcode = CMD_I2C_WRITE;
  68. com.cmd.hdr.Length = outlen + 1;
  69. com.cmd.I2CRead.Device = adr << 1;
  70. memcpy(com.cmd.I2CRead.Data, out, outlen);
  71. com.in_len = outlen + 1;
  72. com.out_len = 1;
  73. if (ngene_command(dev, &com) < 0)
  74. return -EIO;
  75. if (com.cmd.raw8[0] == 1)
  76. return -EIO;
  77. return 0;
  78. }
  79. static void ngene_i2c_set_bus(struct ngene *dev, int bus)
  80. {
  81. if (!(dev->card_info->i2c_access & 2))
  82. return;
  83. if (dev->i2c_current_bus == bus)
  84. return;
  85. switch (bus) {
  86. case 0:
  87. ngene_command_gpio_set(dev, 3, 0);
  88. ngene_command_gpio_set(dev, 2, 1);
  89. break;
  90. case 1:
  91. ngene_command_gpio_set(dev, 2, 0);
  92. ngene_command_gpio_set(dev, 3, 1);
  93. break;
  94. }
  95. dev->i2c_current_bus = bus;
  96. }
  97. static int ngene_i2c_master_xfer(struct i2c_adapter *adapter,
  98. struct i2c_msg msg[], int num)
  99. {
  100. struct ngene_channel *chan =
  101. (struct ngene_channel *)i2c_get_adapdata(adapter);
  102. struct ngene *dev = chan->dev;
  103. mutex_lock(&dev->i2c_switch_mutex);
  104. ngene_i2c_set_bus(dev, chan->number);
  105. if (num == 2 && msg[1].flags & I2C_M_RD && !(msg[0].flags & I2C_M_RD))
  106. if (!ngene_command_i2c_read(dev, msg[0].addr,
  107. msg[0].buf, msg[0].len,
  108. msg[1].buf, msg[1].len, 0))
  109. goto done;
  110. if (num == 1 && !(msg[0].flags & I2C_M_RD))
  111. if (!ngene_command_i2c_write(dev, msg[0].addr,
  112. msg[0].buf, msg[0].len))
  113. goto done;
  114. if (num == 1 && (msg[0].flags & I2C_M_RD))
  115. if (!ngene_command_i2c_read(dev, msg[0].addr, NULL, 0,
  116. msg[0].buf, msg[0].len, 0))
  117. goto done;
  118. mutex_unlock(&dev->i2c_switch_mutex);
  119. return -EIO;
  120. done:
  121. mutex_unlock(&dev->i2c_switch_mutex);
  122. return num;
  123. }
  124. static u32 ngene_i2c_functionality(struct i2c_adapter *adap)
  125. {
  126. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  127. }
  128. static const struct i2c_algorithm ngene_i2c_algo = {
  129. .master_xfer = ngene_i2c_master_xfer,
  130. .functionality = ngene_i2c_functionality,
  131. };
  132. int ngene_i2c_init(struct ngene *dev, int dev_nr)
  133. {
  134. struct i2c_adapter *adap = &(dev->channel[dev_nr].i2c_adapter);
  135. i2c_set_adapdata(adap, &(dev->channel[dev_nr]));
  136. strcpy(adap->name, "nGene");
  137. adap->algo = &ngene_i2c_algo;
  138. adap->algo_data = (void *)&(dev->channel[dev_nr]);
  139. adap->dev.parent = &dev->pci_dev->dev;
  140. return i2c_add_adapter(adap);
  141. }