a8293.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. usleep_range(1500, 50000);
  87. return ret;
  88. err:
  89. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  90. return ret;
  91. }
  92. static void a8293_release_sec(struct dvb_frontend *fe)
  93. {
  94. a8293_set_voltage(fe, SEC_VOLTAGE_OFF);
  95. kfree(fe->sec_priv);
  96. fe->sec_priv = NULL;
  97. }
  98. struct dvb_frontend *a8293_attach(struct dvb_frontend *fe,
  99. struct i2c_adapter *i2c, const struct a8293_config *cfg)
  100. {
  101. int ret;
  102. struct a8293_priv *priv = NULL;
  103. u8 buf[2];
  104. /* allocate memory for the internal priv */
  105. priv = kzalloc(sizeof(struct a8293_priv), GFP_KERNEL);
  106. if (priv == NULL) {
  107. ret = -ENOMEM;
  108. goto err;
  109. }
  110. /* setup the priv */
  111. priv->i2c = i2c;
  112. priv->cfg = cfg;
  113. fe->sec_priv = priv;
  114. /* check if the SEC is there */
  115. ret = a8293_rd(priv, buf, 2);
  116. if (ret)
  117. goto err;
  118. /* ENB=0 */
  119. priv->reg[0] = 0x10;
  120. ret = a8293_wr(priv, &priv->reg[0], 1);
  121. if (ret)
  122. goto err;
  123. /* TMODE=0, TGATE=1 */
  124. priv->reg[1] = 0x82;
  125. ret = a8293_wr(priv, &priv->reg[1], 1);
  126. if (ret)
  127. goto err;
  128. fe->ops.release_sec = a8293_release_sec;
  129. /* override frontend ops */
  130. fe->ops.set_voltage = a8293_set_voltage;
  131. dev_info(&priv->i2c->dev, "%s: Allegro A8293 SEC attached\n",
  132. KBUILD_MODNAME);
  133. return fe;
  134. err:
  135. dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
  136. kfree(priv);
  137. return NULL;
  138. }
  139. EXPORT_SYMBOL(a8293_attach);
  140. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  141. MODULE_DESCRIPTION("Allegro A8293 SEC driver");
  142. MODULE_LICENSE("GPL");