a8293.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Allegro A8293 SEC driver
  3. *
  4. * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "dvb_frontend.h"
  21. #include "a8293.h"
  22. struct a8293_priv {
  23. struct i2c_adapter *i2c;
  24. const struct a8293_config *cfg;
  25. u8 reg[2];
  26. };
  27. static int a8293_i2c(struct a8293_priv *priv, u8 *val, int len, bool rd)
  28. {
  29. int ret;
  30. struct i2c_msg msg[1] = {
  31. {
  32. .addr = priv->cfg->i2c_addr,
  33. .len = len,
  34. .buf = val,
  35. }
  36. };
  37. if (rd)
  38. msg[0].flags = I2C_M_RD;
  39. else
  40. msg[0].flags = 0;
  41. ret = i2c_transfer(priv->i2c, msg, 1);
  42. if (ret == 1) {
  43. ret = 0;
  44. } else {
  45. dev_warn(&priv->i2c->dev, "%s: i2c failed=%d rd=%d\n",
  46. KBUILD_MODNAME, ret, rd);
  47. ret = -EREMOTEIO;
  48. }
  49. return ret;
  50. }
  51. static int a8293_wr(struct a8293_priv *priv, u8 *val, int len)
  52. {
  53. return a8293_i2c(priv, val, len, 0);
  54. }
  55. static int a8293_rd(struct a8293_priv *priv, u8 *val, int len)
  56. {
  57. return a8293_i2c(priv, val, len, 1);
  58. }
  59. static int a8293_set_voltage(struct dvb_frontend *fe,
  60. fe_sec_voltage_t fe_sec_voltage)
  61. {
  62. struct a8293_priv *priv = fe->sec_priv;
  63. int ret;
  64. dev_dbg(&priv->i2c->dev, "%s: fe_sec_voltage=%d\n", __func__,
  65. fe_sec_voltage);
  66. switch (fe_sec_voltage) {
  67. case SEC_VOLTAGE_OFF:
  68. /* ENB=0 */
  69. priv->reg[0] = 0x10;
  70. break;
  71. case SEC_VOLTAGE_13:
  72. /* VSEL0=1, VSEL1=0, VSEL2=0, VSEL3=0, ENB=1*/
  73. priv->reg[0] = 0x31;
  74. break;
  75. case SEC_VOLTAGE_18:
  76. /* VSEL0=0, VSEL1=0, VSEL2=0, VSEL3=1, ENB=1*/
  77. priv->reg[0] = 0x38;
  78. break;
  79. default:
  80. ret = -EINVAL;
  81. goto err;
  82. }
  83. ret = a8293_wr(priv, &priv->reg[0], 1);
  84. if (ret)
  85. goto err;
  86. return ret;
  87. err:
  88. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  89. return ret;
  90. }
  91. static void a8293_release_sec(struct dvb_frontend *fe)
  92. {
  93. a8293_set_voltage(fe, SEC_VOLTAGE_OFF);
  94. kfree(fe->sec_priv);
  95. fe->sec_priv = NULL;
  96. }
  97. struct dvb_frontend *a8293_attach(struct dvb_frontend *fe,
  98. struct i2c_adapter *i2c, const struct a8293_config *cfg)
  99. {
  100. int ret;
  101. struct a8293_priv *priv = NULL;
  102. u8 buf[2];
  103. /* allocate memory for the internal priv */
  104. priv = kzalloc(sizeof(struct a8293_priv), GFP_KERNEL);
  105. if (priv == NULL) {
  106. ret = -ENOMEM;
  107. goto err;
  108. }
  109. /* setup the priv */
  110. priv->i2c = i2c;
  111. priv->cfg = cfg;
  112. fe->sec_priv = priv;
  113. /* check if the SEC is there */
  114. ret = a8293_rd(priv, buf, 2);
  115. if (ret)
  116. goto err;
  117. /* ENB=0 */
  118. priv->reg[0] = 0x10;
  119. ret = a8293_wr(priv, &priv->reg[0], 1);
  120. if (ret)
  121. goto err;
  122. /* TMODE=0, TGATE=1 */
  123. priv->reg[1] = 0x82;
  124. ret = a8293_wr(priv, &priv->reg[1], 1);
  125. if (ret)
  126. goto err;
  127. fe->ops.release_sec = a8293_release_sec;
  128. /* override frontend ops */
  129. fe->ops.set_voltage = a8293_set_voltage;
  130. dev_info(&priv->i2c->dev, "%s: Allegro A8293 SEC attached\n",
  131. KBUILD_MODNAME);
  132. return fe;
  133. err:
  134. dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
  135. kfree(priv);
  136. return NULL;
  137. }
  138. EXPORT_SYMBOL(a8293_attach);
  139. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  140. MODULE_DESCRIPTION("Allegro A8293 SEC driver");
  141. MODULE_LICENSE("GPL");