tua9001.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Infineon TUA 9001 silicon tuner driver
  3. *
  4. * Copyright (C) 2009 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 "tua9001.h"
  21. #include "tua9001_priv.h"
  22. /* write register */
  23. static int tua9001_wr_reg(struct tua9001_priv *priv, u8 reg, u16 val)
  24. {
  25. int ret;
  26. u8 buf[3] = { reg, (val >> 8) & 0xff, (val >> 0) & 0xff };
  27. struct i2c_msg msg[1] = {
  28. {
  29. .addr = priv->cfg->i2c_addr,
  30. .flags = 0,
  31. .len = sizeof(buf),
  32. .buf = buf,
  33. }
  34. };
  35. ret = i2c_transfer(priv->i2c, msg, 1);
  36. if (ret == 1) {
  37. ret = 0;
  38. } else {
  39. dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x\n",
  40. KBUILD_MODNAME, ret, reg);
  41. ret = -EREMOTEIO;
  42. }
  43. return ret;
  44. }
  45. static int tua9001_release(struct dvb_frontend *fe)
  46. {
  47. struct tua9001_priv *priv = fe->tuner_priv;
  48. int ret = 0;
  49. dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
  50. if (fe->callback)
  51. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  52. TUA9001_CMD_CEN, 0);
  53. kfree(fe->tuner_priv);
  54. fe->tuner_priv = NULL;
  55. return ret;
  56. }
  57. static int tua9001_init(struct dvb_frontend *fe)
  58. {
  59. struct tua9001_priv *priv = fe->tuner_priv;
  60. int ret = 0;
  61. u8 i;
  62. struct reg_val data[] = {
  63. { 0x1e, 0x6512 },
  64. { 0x25, 0xb888 },
  65. { 0x39, 0x5460 },
  66. { 0x3b, 0x00c0 },
  67. { 0x3a, 0xf000 },
  68. { 0x08, 0x0000 },
  69. { 0x32, 0x0030 },
  70. { 0x41, 0x703a },
  71. { 0x40, 0x1c78 },
  72. { 0x2c, 0x1c00 },
  73. { 0x36, 0xc013 },
  74. { 0x37, 0x6f18 },
  75. { 0x27, 0x0008 },
  76. { 0x2a, 0x0001 },
  77. { 0x34, 0x0a40 },
  78. };
  79. dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
  80. if (fe->callback) {
  81. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  82. TUA9001_CMD_RESETN, 0);
  83. if (ret < 0)
  84. goto err;
  85. }
  86. if (fe->ops.i2c_gate_ctrl)
  87. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
  88. for (i = 0; i < ARRAY_SIZE(data); i++) {
  89. ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
  90. if (ret < 0)
  91. goto err_i2c_gate_ctrl;
  92. }
  93. err_i2c_gate_ctrl:
  94. if (fe->ops.i2c_gate_ctrl)
  95. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
  96. err:
  97. if (ret < 0)
  98. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  99. return ret;
  100. }
  101. static int tua9001_sleep(struct dvb_frontend *fe)
  102. {
  103. struct tua9001_priv *priv = fe->tuner_priv;
  104. int ret = 0;
  105. dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
  106. if (fe->callback)
  107. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  108. TUA9001_CMD_RESETN, 1);
  109. if (ret < 0)
  110. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  111. return ret;
  112. }
  113. static int tua9001_set_params(struct dvb_frontend *fe)
  114. {
  115. struct tua9001_priv *priv = fe->tuner_priv;
  116. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  117. int ret = 0, i;
  118. u16 val;
  119. u32 frequency;
  120. struct reg_val data[2];
  121. dev_dbg(&priv->i2c->dev, "%s: delivery_system=%d frequency=%d " \
  122. "bandwidth_hz=%d\n", __func__,
  123. c->delivery_system, c->frequency, c->bandwidth_hz);
  124. switch (c->delivery_system) {
  125. case SYS_DVBT:
  126. switch (c->bandwidth_hz) {
  127. case 8000000:
  128. val = 0x0000;
  129. break;
  130. case 7000000:
  131. val = 0x1000;
  132. break;
  133. case 6000000:
  134. val = 0x2000;
  135. break;
  136. case 5000000:
  137. val = 0x3000;
  138. break;
  139. default:
  140. ret = -EINVAL;
  141. goto err;
  142. }
  143. break;
  144. default:
  145. ret = -EINVAL;
  146. goto err;
  147. }
  148. data[0].reg = 0x04;
  149. data[0].val = val;
  150. frequency = (c->frequency - 150000000);
  151. frequency /= 100;
  152. frequency *= 48;
  153. frequency /= 10000;
  154. data[1].reg = 0x1f;
  155. data[1].val = frequency;
  156. if (fe->ops.i2c_gate_ctrl)
  157. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
  158. if (fe->callback) {
  159. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  160. TUA9001_CMD_RXEN, 0);
  161. if (ret < 0)
  162. goto err_i2c_gate_ctrl;
  163. }
  164. for (i = 0; i < ARRAY_SIZE(data); i++) {
  165. ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
  166. if (ret < 0)
  167. goto err_i2c_gate_ctrl;
  168. }
  169. if (fe->callback) {
  170. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  171. TUA9001_CMD_RXEN, 1);
  172. if (ret < 0)
  173. goto err_i2c_gate_ctrl;
  174. }
  175. err_i2c_gate_ctrl:
  176. if (fe->ops.i2c_gate_ctrl)
  177. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
  178. err:
  179. if (ret < 0)
  180. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  181. return ret;
  182. }
  183. static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  184. {
  185. struct tua9001_priv *priv = fe->tuner_priv;
  186. dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
  187. *frequency = 0; /* Zero-IF */
  188. return 0;
  189. }
  190. static const struct dvb_tuner_ops tua9001_tuner_ops = {
  191. .info = {
  192. .name = "Infineon TUA 9001",
  193. .frequency_min = 170000000,
  194. .frequency_max = 862000000,
  195. .frequency_step = 0,
  196. },
  197. .release = tua9001_release,
  198. .init = tua9001_init,
  199. .sleep = tua9001_sleep,
  200. .set_params = tua9001_set_params,
  201. .get_if_frequency = tua9001_get_if_frequency,
  202. };
  203. struct dvb_frontend *tua9001_attach(struct dvb_frontend *fe,
  204. struct i2c_adapter *i2c, struct tua9001_config *cfg)
  205. {
  206. struct tua9001_priv *priv = NULL;
  207. int ret;
  208. priv = kzalloc(sizeof(struct tua9001_priv), GFP_KERNEL);
  209. if (priv == NULL)
  210. return NULL;
  211. priv->cfg = cfg;
  212. priv->i2c = i2c;
  213. if (fe->callback) {
  214. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  215. TUA9001_CMD_CEN, 1);
  216. if (ret < 0)
  217. goto err;
  218. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  219. TUA9001_CMD_RXEN, 0);
  220. if (ret < 0)
  221. goto err;
  222. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  223. TUA9001_CMD_RESETN, 1);
  224. if (ret < 0)
  225. goto err;
  226. }
  227. dev_info(&priv->i2c->dev,
  228. "%s: Infineon TUA 9001 successfully attached\n",
  229. KBUILD_MODNAME);
  230. memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
  231. sizeof(struct dvb_tuner_ops));
  232. fe->tuner_priv = priv;
  233. return fe;
  234. err:
  235. dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
  236. kfree(priv);
  237. return NULL;
  238. }
  239. EXPORT_SYMBOL(tua9001_attach);
  240. MODULE_DESCRIPTION("Infineon TUA 9001 silicon tuner driver");
  241. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  242. MODULE_LICENSE("GPL");