a8293.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. static int debug;
  23. module_param(debug, int, 0644);
  24. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  25. #define LOG_PREFIX "a8293"
  26. #undef dbg
  27. #define dbg(f, arg...) \
  28. if (debug) \
  29. printk(KERN_INFO LOG_PREFIX": " f "\n" , ## arg)
  30. #undef err
  31. #define err(f, arg...) printk(KERN_ERR LOG_PREFIX": " f "\n" , ## arg)
  32. #undef info
  33. #define info(f, arg...) printk(KERN_INFO LOG_PREFIX": " f "\n" , ## arg)
  34. #undef warn
  35. #define warn(f, arg...) printk(KERN_WARNING LOG_PREFIX": " f "\n" , ## arg)
  36. struct a8293_priv {
  37. struct i2c_adapter *i2c;
  38. const struct a8293_config *cfg;
  39. u8 reg[2];
  40. };
  41. static int a8293_i2c(struct a8293_priv *priv, u8 *val, int len, bool rd)
  42. {
  43. int ret;
  44. struct i2c_msg msg[1] = {
  45. {
  46. .addr = priv->cfg->i2c_addr,
  47. .len = len,
  48. .buf = val,
  49. }
  50. };
  51. if (rd)
  52. msg[0].flags = I2C_M_RD;
  53. else
  54. msg[0].flags = 0;
  55. ret = i2c_transfer(priv->i2c, msg, 1);
  56. if (ret == 1) {
  57. ret = 0;
  58. } else {
  59. warn("i2c failed=%d rd=%d", ret, rd);
  60. ret = -EREMOTEIO;
  61. }
  62. return ret;
  63. }
  64. static int a8293_wr(struct a8293_priv *priv, u8 *val, int len)
  65. {
  66. return a8293_i2c(priv, val, len, 0);
  67. }
  68. static int a8293_rd(struct a8293_priv *priv, u8 *val, int len)
  69. {
  70. return a8293_i2c(priv, val, len, 1);
  71. }
  72. static int a8293_set_voltage(struct dvb_frontend *fe,
  73. fe_sec_voltage_t fe_sec_voltage)
  74. {
  75. struct a8293_priv *priv = fe->sec_priv;
  76. int ret;
  77. dbg("%s: fe_sec_voltage=%d", __func__, fe_sec_voltage);
  78. switch (fe_sec_voltage) {
  79. case SEC_VOLTAGE_OFF:
  80. /* ENB=0 */
  81. priv->reg[0] = 0x10;
  82. break;
  83. case SEC_VOLTAGE_13:
  84. /* VSEL0=1, VSEL1=0, VSEL2=0, VSEL3=0, ENB=1*/
  85. priv->reg[0] = 0x31;
  86. break;
  87. case SEC_VOLTAGE_18:
  88. /* VSEL0=0, VSEL1=0, VSEL2=0, VSEL3=1, ENB=1*/
  89. priv->reg[0] = 0x38;
  90. break;
  91. default:
  92. ret = -EINVAL;
  93. goto err;
  94. };
  95. ret = a8293_wr(priv, &priv->reg[0], 1);
  96. if (ret)
  97. goto err;
  98. return ret;
  99. err:
  100. dbg("%s: failed=%d", __func__, ret);
  101. return ret;
  102. }
  103. static void a8293_release_sec(struct dvb_frontend *fe)
  104. {
  105. dbg("%s:", __func__);
  106. a8293_set_voltage(fe, SEC_VOLTAGE_OFF);
  107. kfree(fe->sec_priv);
  108. fe->sec_priv = NULL;
  109. }
  110. struct dvb_frontend *a8293_attach(struct dvb_frontend *fe,
  111. struct i2c_adapter *i2c, const struct a8293_config *cfg)
  112. {
  113. int ret;
  114. struct a8293_priv *priv = NULL;
  115. u8 buf[2];
  116. /* allocate memory for the internal priv */
  117. priv = kzalloc(sizeof(struct a8293_priv), GFP_KERNEL);
  118. if (priv == NULL) {
  119. ret = -ENOMEM;
  120. goto err;
  121. }
  122. /* setup the priv */
  123. priv->i2c = i2c;
  124. priv->cfg = cfg;
  125. fe->sec_priv = priv;
  126. /* check if the SEC is there */
  127. ret = a8293_rd(priv, buf, 2);
  128. if (ret)
  129. goto err;
  130. /* ENB=0 */
  131. priv->reg[0] = 0x10;
  132. ret = a8293_wr(priv, &priv->reg[1], 1);
  133. if (ret)
  134. goto err;
  135. /* TMODE=0, TGATE=1 */
  136. priv->reg[1] = 0x82;
  137. ret = a8293_wr(priv, &priv->reg[1], 1);
  138. if (ret)
  139. goto err;
  140. info("Allegro A8293 SEC attached.");
  141. fe->ops.release_sec = a8293_release_sec;
  142. /* override frontend ops */
  143. fe->ops.set_voltage = a8293_set_voltage;
  144. return fe;
  145. err:
  146. dbg("%s: failed=%d", __func__, ret);
  147. kfree(priv);
  148. return NULL;
  149. }
  150. EXPORT_SYMBOL(a8293_attach);
  151. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  152. MODULE_DESCRIPTION("Allegro A8293 SEC driver");
  153. MODULE_LICENSE("GPL");