tua9001.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Infineon TUA9001 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. #include "tua9001_priv.h"
  17. static int tua9001_init(struct dvb_frontend *fe)
  18. {
  19. struct tua9001_dev *dev = fe->tuner_priv;
  20. struct i2c_client *client = dev->client;
  21. int ret, i;
  22. static const struct tua9001_reg_val data[] = {
  23. {0x1e, 0x6512},
  24. {0x25, 0xb888},
  25. {0x39, 0x5460},
  26. {0x3b, 0x00c0},
  27. {0x3a, 0xf000},
  28. {0x08, 0x0000},
  29. {0x32, 0x0030},
  30. {0x41, 0x703a},
  31. {0x40, 0x1c78},
  32. {0x2c, 0x1c00},
  33. {0x36, 0xc013},
  34. {0x37, 0x6f18},
  35. {0x27, 0x0008},
  36. {0x2a, 0x0001},
  37. {0x34, 0x0a40},
  38. };
  39. dev_dbg(&client->dev, "\n");
  40. if (fe->callback) {
  41. ret = fe->callback(client->adapter,
  42. DVB_FRONTEND_COMPONENT_TUNER,
  43. TUA9001_CMD_RESETN, 0);
  44. if (ret)
  45. goto err;
  46. }
  47. for (i = 0; i < ARRAY_SIZE(data); i++) {
  48. ret = regmap_write(dev->regmap, data[i].reg, data[i].val);
  49. if (ret)
  50. goto err;
  51. }
  52. return 0;
  53. err:
  54. dev_dbg(&client->dev, "failed=%d\n", ret);
  55. return ret;
  56. }
  57. static int tua9001_sleep(struct dvb_frontend *fe)
  58. {
  59. struct tua9001_dev *dev = fe->tuner_priv;
  60. struct i2c_client *client = dev->client;
  61. int ret;
  62. dev_dbg(&client->dev, "\n");
  63. if (fe->callback) {
  64. ret = fe->callback(client->adapter,
  65. DVB_FRONTEND_COMPONENT_TUNER,
  66. TUA9001_CMD_RESETN, 1);
  67. if (ret)
  68. goto err;
  69. }
  70. return 0;
  71. err:
  72. dev_dbg(&client->dev, "failed=%d\n", ret);
  73. return ret;
  74. }
  75. static int tua9001_set_params(struct dvb_frontend *fe)
  76. {
  77. struct tua9001_dev *dev = fe->tuner_priv;
  78. struct i2c_client *client = dev->client;
  79. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  80. int ret, i;
  81. u16 val;
  82. struct tua9001_reg_val data[2];
  83. dev_dbg(&client->dev,
  84. "delivery_system=%u frequency=%u bandwidth_hz=%u\n",
  85. c->delivery_system, c->frequency, c->bandwidth_hz);
  86. switch (c->delivery_system) {
  87. case SYS_DVBT:
  88. switch (c->bandwidth_hz) {
  89. case 8000000:
  90. val = 0x0000;
  91. break;
  92. case 7000000:
  93. val = 0x1000;
  94. break;
  95. case 6000000:
  96. val = 0x2000;
  97. break;
  98. case 5000000:
  99. val = 0x3000;
  100. break;
  101. default:
  102. ret = -EINVAL;
  103. goto err;
  104. }
  105. break;
  106. default:
  107. ret = -EINVAL;
  108. goto err;
  109. }
  110. data[0].reg = 0x04;
  111. data[0].val = val;
  112. data[1].reg = 0x1f;
  113. data[1].val = div_u64((u64) (c->frequency - 150000000) * 48, 1000000);
  114. if (fe->callback) {
  115. ret = fe->callback(client->adapter,
  116. DVB_FRONTEND_COMPONENT_TUNER,
  117. TUA9001_CMD_RXEN, 0);
  118. if (ret)
  119. goto err;
  120. }
  121. for (i = 0; i < ARRAY_SIZE(data); i++) {
  122. ret = regmap_write(dev->regmap, data[i].reg, data[i].val);
  123. if (ret)
  124. goto err;
  125. }
  126. if (fe->callback) {
  127. ret = fe->callback(client->adapter,
  128. DVB_FRONTEND_COMPONENT_TUNER,
  129. TUA9001_CMD_RXEN, 1);
  130. if (ret)
  131. goto err;
  132. }
  133. return 0;
  134. err:
  135. dev_dbg(&client->dev, "failed=%d\n", ret);
  136. return ret;
  137. }
  138. static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  139. {
  140. struct tua9001_dev *dev = fe->tuner_priv;
  141. struct i2c_client *client = dev->client;
  142. dev_dbg(&client->dev, "\n");
  143. *frequency = 0; /* Zero-IF */
  144. return 0;
  145. }
  146. static const struct dvb_tuner_ops tua9001_tuner_ops = {
  147. .info = {
  148. .name = "Infineon TUA9001",
  149. .frequency_min = 170000000,
  150. .frequency_max = 862000000,
  151. },
  152. .init = tua9001_init,
  153. .sleep = tua9001_sleep,
  154. .set_params = tua9001_set_params,
  155. .get_if_frequency = tua9001_get_if_frequency,
  156. };
  157. static int tua9001_probe(struct i2c_client *client,
  158. const struct i2c_device_id *id)
  159. {
  160. struct tua9001_dev *dev;
  161. struct tua9001_platform_data *pdata = client->dev.platform_data;
  162. struct dvb_frontend *fe = pdata->dvb_frontend;
  163. int ret;
  164. static const struct regmap_config regmap_config = {
  165. .reg_bits = 8,
  166. .val_bits = 16,
  167. };
  168. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  169. if (!dev) {
  170. ret = -ENOMEM;
  171. goto err;
  172. }
  173. dev->fe = pdata->dvb_frontend;
  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_kfree;
  179. }
  180. if (fe->callback) {
  181. ret = fe->callback(client->adapter,
  182. DVB_FRONTEND_COMPONENT_TUNER,
  183. TUA9001_CMD_CEN, 1);
  184. if (ret)
  185. goto err_kfree;
  186. ret = fe->callback(client->adapter,
  187. DVB_FRONTEND_COMPONENT_TUNER,
  188. TUA9001_CMD_RXEN, 0);
  189. if (ret)
  190. goto err_kfree;
  191. ret = fe->callback(client->adapter,
  192. DVB_FRONTEND_COMPONENT_TUNER,
  193. TUA9001_CMD_RESETN, 1);
  194. if (ret)
  195. goto err_kfree;
  196. }
  197. fe->tuner_priv = dev;
  198. memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
  199. sizeof(struct dvb_tuner_ops));
  200. i2c_set_clientdata(client, dev);
  201. dev_info(&client->dev, "Infineon TUA9001 successfully attached\n");
  202. return 0;
  203. err_kfree:
  204. kfree(dev);
  205. err:
  206. dev_dbg(&client->dev, "failed=%d\n", ret);
  207. return ret;
  208. }
  209. static int tua9001_remove(struct i2c_client *client)
  210. {
  211. struct tua9001_dev *dev = i2c_get_clientdata(client);
  212. struct dvb_frontend *fe = dev->fe;
  213. int ret;
  214. dev_dbg(&client->dev, "\n");
  215. if (fe->callback) {
  216. ret = fe->callback(client->adapter,
  217. DVB_FRONTEND_COMPONENT_TUNER,
  218. TUA9001_CMD_CEN, 0);
  219. if (ret)
  220. goto err_kfree;
  221. }
  222. kfree(dev);
  223. return 0;
  224. err_kfree:
  225. kfree(dev);
  226. dev_dbg(&client->dev, "failed=%d\n", ret);
  227. return ret;
  228. }
  229. static const struct i2c_device_id tua9001_id_table[] = {
  230. {"tua9001", 0},
  231. {}
  232. };
  233. MODULE_DEVICE_TABLE(i2c, tua9001_id_table);
  234. static struct i2c_driver tua9001_driver = {
  235. .driver = {
  236. .name = "tua9001",
  237. .suppress_bind_attrs = true,
  238. },
  239. .probe = tua9001_probe,
  240. .remove = tua9001_remove,
  241. .id_table = tua9001_id_table,
  242. };
  243. module_i2c_driver(tua9001_driver);
  244. MODULE_DESCRIPTION("Infineon TUA9001 silicon tuner driver");
  245. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  246. MODULE_LICENSE("GPL");