tda18212.c 5.9 KB

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