tda18212.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * NXP TDA18212HN silicon tuner 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 "tda18212.h"
  21. #include <linux/regmap.h>
  22. struct tda18212_dev {
  23. struct tda18212_config cfg;
  24. struct i2c_client *client;
  25. struct regmap *regmap;
  26. u32 if_frequency;
  27. };
  28. static int tda18212_set_params(struct dvb_frontend *fe)
  29. {
  30. struct tda18212_dev *dev = fe->tuner_priv;
  31. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  32. int ret, i;
  33. u32 if_khz;
  34. u8 buf[9];
  35. #define DVBT_6 0
  36. #define DVBT_7 1
  37. #define DVBT_8 2
  38. #define DVBT2_6 3
  39. #define DVBT2_7 4
  40. #define DVBT2_8 5
  41. #define DVBC_6 6
  42. #define DVBC_8 7
  43. #define ATSC_VSB 8
  44. #define ATSC_QAM 9
  45. static const u8 bw_params[][3] = {
  46. /* reg: 0f 13 23 */
  47. [DVBT_6] = { 0xb3, 0x20, 0x03 },
  48. [DVBT_7] = { 0xb3, 0x31, 0x01 },
  49. [DVBT_8] = { 0xb3, 0x22, 0x01 },
  50. [DVBT2_6] = { 0xbc, 0x20, 0x03 },
  51. [DVBT2_7] = { 0xbc, 0x72, 0x03 },
  52. [DVBT2_8] = { 0xbc, 0x22, 0x01 },
  53. [DVBC_6] = { 0x92, 0x50, 0x03 },
  54. [DVBC_8] = { 0x92, 0x53, 0x03 },
  55. [ATSC_VSB] = { 0x7d, 0x20, 0x63 },
  56. [ATSC_QAM] = { 0x7d, 0x20, 0x63 },
  57. };
  58. dev_dbg(&dev->client->dev,
  59. "delivery_system=%d frequency=%d bandwidth_hz=%d\n",
  60. c->delivery_system, c->frequency,
  61. c->bandwidth_hz);
  62. if (fe->ops.i2c_gate_ctrl)
  63. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  64. switch (c->delivery_system) {
  65. case SYS_ATSC:
  66. if_khz = dev->cfg.if_atsc_vsb;
  67. i = ATSC_VSB;
  68. break;
  69. case SYS_DVBC_ANNEX_B:
  70. if_khz = dev->cfg.if_atsc_qam;
  71. i = ATSC_QAM;
  72. break;
  73. case SYS_DVBT:
  74. switch (c->bandwidth_hz) {
  75. case 6000000:
  76. if_khz = dev->cfg.if_dvbt_6;
  77. i = DVBT_6;
  78. break;
  79. case 7000000:
  80. if_khz = dev->cfg.if_dvbt_7;
  81. i = DVBT_7;
  82. break;
  83. case 8000000:
  84. if_khz = dev->cfg.if_dvbt_8;
  85. i = DVBT_8;
  86. break;
  87. default:
  88. ret = -EINVAL;
  89. goto error;
  90. }
  91. break;
  92. case SYS_DVBT2:
  93. switch (c->bandwidth_hz) {
  94. case 6000000:
  95. if_khz = dev->cfg.if_dvbt2_6;
  96. i = DVBT2_6;
  97. break;
  98. case 7000000:
  99. if_khz = dev->cfg.if_dvbt2_7;
  100. i = DVBT2_7;
  101. break;
  102. case 8000000:
  103. if_khz = dev->cfg.if_dvbt2_8;
  104. i = DVBT2_8;
  105. break;
  106. default:
  107. ret = -EINVAL;
  108. goto error;
  109. }
  110. break;
  111. case SYS_DVBC_ANNEX_A:
  112. case SYS_DVBC_ANNEX_C:
  113. if_khz = dev->cfg.if_dvbc;
  114. i = DVBC_8;
  115. break;
  116. default:
  117. ret = -EINVAL;
  118. goto error;
  119. }
  120. ret = regmap_write(dev->regmap, 0x23, bw_params[i][2]);
  121. if (ret)
  122. goto error;
  123. ret = regmap_write(dev->regmap, 0x06, 0x00);
  124. if (ret)
  125. goto error;
  126. ret = regmap_write(dev->regmap, 0x0f, bw_params[i][0]);
  127. if (ret)
  128. goto error;
  129. buf[0] = 0x02;
  130. buf[1] = bw_params[i][1];
  131. buf[2] = 0x03; /* default value */
  132. buf[3] = DIV_ROUND_CLOSEST(if_khz, 50);
  133. buf[4] = ((c->frequency / 1000) >> 16) & 0xff;
  134. buf[5] = ((c->frequency / 1000) >> 8) & 0xff;
  135. buf[6] = ((c->frequency / 1000) >> 0) & 0xff;
  136. buf[7] = 0xc1;
  137. buf[8] = 0x01;
  138. ret = regmap_bulk_write(dev->regmap, 0x12, buf, sizeof(buf));
  139. if (ret)
  140. goto error;
  141. /* actual IF rounded as it is on register */
  142. dev->if_frequency = buf[3] * 50 * 1000;
  143. exit:
  144. if (fe->ops.i2c_gate_ctrl)
  145. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  146. return ret;
  147. error:
  148. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  149. goto exit;
  150. }
  151. static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  152. {
  153. struct tda18212_dev *dev = fe->tuner_priv;
  154. *frequency = dev->if_frequency;
  155. return 0;
  156. }
  157. static const struct dvb_tuner_ops tda18212_tuner_ops = {
  158. .info = {
  159. .name = "NXP TDA18212",
  160. .frequency_min = 48000000,
  161. .frequency_max = 864000000,
  162. .frequency_step = 1000,
  163. },
  164. .set_params = tda18212_set_params,
  165. .get_if_frequency = tda18212_get_if_frequency,
  166. };
  167. static int tda18212_probe(struct i2c_client *client,
  168. const struct i2c_device_id *id)
  169. {
  170. struct tda18212_config *cfg = client->dev.platform_data;
  171. struct dvb_frontend *fe = cfg->fe;
  172. struct tda18212_dev *dev;
  173. int ret;
  174. unsigned int chip_id;
  175. char *version;
  176. static const struct regmap_config regmap_config = {
  177. .reg_bits = 8,
  178. .val_bits = 8,
  179. };
  180. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  181. if (dev == NULL) {
  182. ret = -ENOMEM;
  183. dev_err(&client->dev, "kzalloc() failed\n");
  184. goto err;
  185. }
  186. memcpy(&dev->cfg, cfg, sizeof(struct tda18212_config));
  187. dev->client = client;
  188. dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
  189. if (IS_ERR(dev->regmap)) {
  190. ret = PTR_ERR(dev->regmap);
  191. goto err;
  192. }
  193. /* check if the tuner is there */
  194. if (fe->ops.i2c_gate_ctrl)
  195. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  196. ret = regmap_read(dev->regmap, 0x00, &chip_id);
  197. dev_dbg(&dev->client->dev, "chip_id=%02x\n", chip_id);
  198. if (fe->ops.i2c_gate_ctrl)
  199. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  200. if (ret)
  201. goto err;
  202. switch (chip_id) {
  203. case 0xc7:
  204. version = "M"; /* master */
  205. break;
  206. case 0x47:
  207. version = "S"; /* slave */
  208. break;
  209. default:
  210. ret = -ENODEV;
  211. goto err;
  212. }
  213. dev_info(&dev->client->dev,
  214. "NXP TDA18212HN/%s successfully identified\n", version);
  215. fe->tuner_priv = dev;
  216. memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
  217. sizeof(struct dvb_tuner_ops));
  218. i2c_set_clientdata(client, dev);
  219. return 0;
  220. err:
  221. dev_dbg(&client->dev, "failed=%d\n", ret);
  222. kfree(dev);
  223. return ret;
  224. }
  225. static int tda18212_remove(struct i2c_client *client)
  226. {
  227. struct tda18212_dev *dev = i2c_get_clientdata(client);
  228. struct dvb_frontend *fe = dev->cfg.fe;
  229. dev_dbg(&client->dev, "\n");
  230. memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
  231. fe->tuner_priv = NULL;
  232. kfree(dev);
  233. return 0;
  234. }
  235. static const struct i2c_device_id tda18212_id[] = {
  236. {"tda18212", 0},
  237. {}
  238. };
  239. MODULE_DEVICE_TABLE(i2c, tda18212_id);
  240. static struct i2c_driver tda18212_driver = {
  241. .driver = {
  242. .name = "tda18212",
  243. },
  244. .probe = tda18212_probe,
  245. .remove = tda18212_remove,
  246. .id_table = tda18212_id,
  247. };
  248. module_i2c_driver(tda18212_driver);
  249. MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
  250. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  251. MODULE_LICENSE("GPL");