stv6110x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. STV6110(A) Silicon tuner driver
  3. Copyright (C) Manu Abraham <abraham.manu@gmail.com>
  4. Copyright (C) ST Microelectronics
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. #include "dvb_frontend.h"
  22. #include "stv6110x_reg.h"
  23. #include "stv6110x.h"
  24. #include "stv6110x_priv.h"
  25. static unsigned int verbose;
  26. module_param(verbose, int, 0644);
  27. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  28. static u8 stv6110x_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e};
  29. static int stv6110x_read_reg(struct stv6110x_state *stv6110x, u8 reg, u8 *data)
  30. {
  31. int ret;
  32. const struct stv6110x_config *config = stv6110x->config;
  33. u8 b0[] = { reg };
  34. u8 b1[] = { 0 };
  35. struct i2c_msg msg[] = {
  36. { .addr = config->addr, .flags = 0, .buf = b0, .len = 1 },
  37. { .addr = config->addr, .flags = I2C_M_RD, .buf = b1, .len = 1 }
  38. };
  39. ret = i2c_transfer(stv6110x->i2c, msg, 2);
  40. if (ret != 2) {
  41. dprintk(FE_ERROR, 1, "I/O Error");
  42. return -EREMOTEIO;
  43. }
  44. *data = b1[0];
  45. return 0;
  46. }
  47. static int stv6110x_write_reg(struct stv6110x_state *stv6110x, u8 reg, u8 data)
  48. {
  49. int ret;
  50. const struct stv6110x_config *config = stv6110x->config;
  51. u8 buf[] = { reg, data };
  52. struct i2c_msg msg = { .addr = config->addr, .flags = 0, . buf = buf, .len = 2 };
  53. ret = i2c_transfer(stv6110x->i2c, &msg, 1);
  54. if (ret != 1) {
  55. dprintk(FE_ERROR, 1, "I/O Error");
  56. return -EREMOTEIO;
  57. }
  58. return 0;
  59. }
  60. static int stv6110x_init(struct dvb_frontend *fe)
  61. {
  62. struct stv6110x_state *stv6110x = fe->tuner_priv;
  63. int ret;
  64. u8 i;
  65. for (i = 0; i < ARRAY_SIZE(stv6110x_regs); i++) {
  66. ret = stv6110x_write_reg(stv6110x, i, stv6110x_regs[i]);
  67. if (ret < 0) {
  68. dprintk(FE_ERROR, 1, "Initialization failed");
  69. return -1;
  70. }
  71. }
  72. return 0;
  73. }
  74. static int stv6110x_set_frequency(struct dvb_frontend *fe, u32 frequency)
  75. {
  76. struct stv6110x_state *stv6110x = fe->tuner_priv;
  77. u32 rDiv, divider;
  78. s32 pVal, pCalc, rDivOpt = 0;
  79. u8 i;
  80. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_K, (REFCLOCK_MHz - 16));
  81. if (frequency <= 1023000) {
  82. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
  83. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
  84. pVal = 40;
  85. } else if (frequency <= 1300000) {
  86. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
  87. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
  88. pVal = 40;
  89. } else if (frequency <= 2046000) {
  90. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
  91. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
  92. pVal = 20;
  93. } else {
  94. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
  95. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
  96. pVal = 20;
  97. }
  98. for (rDiv = 0; rDiv <= 3; rDiv++) {
  99. pCalc = (REFCLOCK_kHz / 100) / R_DIV(rDiv);
  100. if ((abs((s32)(pCalc - pVal))) < (abs((s32)(1000 - pVal))))
  101. rDivOpt = rDiv;
  102. }
  103. divider = (frequency * R_DIV(rDivOpt) * pVal) / REFCLOCK_kHz;
  104. divider = (divider + 5) / 10;
  105. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_R_DIV, rDivOpt);
  106. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_N_DIV_11_8, MSB(divider));
  107. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG0], TNG0_N_DIV_7_0, LSB(divider));
  108. /* VCO Auto calibration */
  109. STV6110x_SETFIELD(stv6110x_regs[STV6110x_STAT1], STAT1_CALVCO_STRT, 1);
  110. stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x_regs[STV6110x_CTRL1]);
  111. stv6110x_write_reg(stv6110x, STV6110x_TNG1, stv6110x_regs[STV6110x_TNG1]);
  112. stv6110x_write_reg(stv6110x, STV6110x_TNG0, stv6110x_regs[STV6110x_TNG0]);
  113. stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x_regs[STV6110x_STAT1]);
  114. for (i = 0; i < TRIALS; i++) {
  115. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
  116. if (!STV6110x_GETFIELD(STAT1_CALVCO_STRT, stv6110x_regs[STV6110x_STAT1]))
  117. break;
  118. msleep(1);
  119. }
  120. return 0;
  121. }
  122. static int stv6110x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  123. {
  124. struct stv6110x_state *stv6110x = fe->tuner_priv;
  125. stv6110x_read_reg(stv6110x, STV6110x_TNG1, &stv6110x_regs[STV6110x_TNG1]);
  126. stv6110x_read_reg(stv6110x, STV6110x_TNG0, &stv6110x_regs[STV6110x_TNG0]);
  127. *frequency = (MAKEWORD16(STV6110x_GETFIELD(TNG1_N_DIV_11_8, stv6110x_regs[STV6110x_TNG1]),
  128. STV6110x_GETFIELD(TNG0_N_DIV_7_0, stv6110x_regs[STV6110x_TNG0]))) * REFCLOCK_kHz;
  129. *frequency /= (1 << (STV6110x_GETFIELD(TNG1_R_DIV, stv6110x_regs[STV6110x_TNG1]) +
  130. STV6110x_GETFIELD(TNG1_DIV4SEL, stv6110x_regs[STV6110x_TNG1])));
  131. *frequency >>= 2;
  132. return 0;
  133. }
  134. static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
  135. {
  136. struct stv6110x_state *stv6110x = fe->tuner_priv;
  137. u32 halfbw;
  138. u8 i;
  139. halfbw = bandwidth >> 1;
  140. if (halfbw > 36000000)
  141. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, 31); /* LPF */
  142. else if (halfbw < 5000000)
  143. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, 0); /* LPF */
  144. else
  145. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, ((halfbw / 1000000) - 5)); /* LPF */
  146. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x0); /* cal. clk activated */
  147. STV6110x_SETFIELD(stv6110x_regs[STV6110x_STAT1], STAT1_CALRC_STRT, 0x1); /* LPF auto cal */
  148. stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x_regs[STV6110x_CTRL3]);
  149. stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x_regs[STV6110x_STAT1]);
  150. for (i = 0; i < TRIALS; i++) {
  151. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
  152. if (!STV6110x_GETFIELD(STAT1_CALRC_STRT, stv6110x_regs[STV6110x_STAT1]))
  153. break;
  154. msleep(1);
  155. }
  156. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */
  157. stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x_regs[STV6110x_CTRL3]);
  158. return 0;
  159. }
  160. static int stv6110x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  161. {
  162. struct stv6110x_state *stv6110x = fe->tuner_priv;
  163. stv6110x_read_reg(stv6110x, STV6110x_CTRL3, &stv6110x_regs[STV6110x_CTRL3]);
  164. *bandwidth = (STV6110x_GETFIELD(CTRL3_CF, stv6110x_regs[STV6110x_CTRL3]) + 5) * 2000000;
  165. return 0;
  166. }
  167. static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock)
  168. {
  169. struct stv6110x_state *stv6110x = fe->tuner_priv;
  170. /* setup divider */
  171. switch (refclock) {
  172. default:
  173. case 1:
  174. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
  175. break;
  176. case 2:
  177. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
  178. break;
  179. case 4:
  180. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
  181. break;
  182. case 8:
  183. case 0:
  184. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
  185. break;
  186. }
  187. stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x_regs[STV6110x_CTRL2]);
  188. return 0;
  189. }
  190. static int stv6110x_get_bbgain(struct dvb_frontend *fe, u32 *gain)
  191. {
  192. struct stv6110x_state *stv6110x = fe->tuner_priv;
  193. stv6110x_read_reg(stv6110x, STV6110x_CTRL2, &stv6110x_regs[STV6110x_CTRL2]);
  194. *gain = 2 * STV6110x_GETFIELD(CTRL2_BBGAIN, stv6110x_regs[STV6110x_CTRL2]);
  195. return 0;
  196. }
  197. static int stv6110x_set_bbgain(struct dvb_frontend *fe, u32 gain)
  198. {
  199. struct stv6110x_state *stv6110x = fe->tuner_priv;
  200. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_BBGAIN, gain / 2);
  201. stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x_regs[STV6110x_CTRL2]);
  202. return 0;
  203. }
  204. static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode)
  205. {
  206. struct stv6110x_state *stv6110x = fe->tuner_priv;
  207. int ret;
  208. switch (mode) {
  209. case TUNER_SLEEP:
  210. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_SYN, 0);
  211. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_RX, 0);
  212. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_LPT, 0);
  213. break;
  214. case TUNER_WAKE:
  215. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_SYN, 1);
  216. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_RX, 1);
  217. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_LPT, 1);
  218. break;
  219. }
  220. ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x_regs[STV6110x_CTRL1]);
  221. if (ret < 0) {
  222. dprintk(FE_ERROR, 1, "I/O Error");
  223. return -EIO;
  224. }
  225. return 0;
  226. }
  227. static int stv6110x_sleep(struct dvb_frontend *fe)
  228. {
  229. return stv6110x_set_mode(fe, TUNER_SLEEP);
  230. }
  231. static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status)
  232. {
  233. struct stv6110x_state *stv6110x = fe->tuner_priv;
  234. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
  235. if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x_regs[STV6110x_STAT1]))
  236. *status = TUNER_PHASELOCKED;
  237. else
  238. *status = 0;
  239. return 0;
  240. }
  241. static int stv6110x_release(struct dvb_frontend *fe)
  242. {
  243. struct stv6110x_state *stv6110x = fe->tuner_priv;
  244. fe->tuner_priv = NULL;
  245. kfree(stv6110x);
  246. return 0;
  247. }
  248. static struct dvb_tuner_ops stv6110x_ops = {
  249. .info = {
  250. .name = "STV6110(A) Silicon Tuner",
  251. .frequency_min = 950000,
  252. .frequency_max = 2150000,
  253. .frequency_step = 0,
  254. },
  255. .init = stv6110x_init,
  256. .sleep = stv6110x_sleep,
  257. .release = stv6110x_release
  258. };
  259. static struct stv6110x_devctl stv6110x_ctl = {
  260. .tuner_init = stv6110x_init,
  261. .tuner_set_mode = stv6110x_set_mode,
  262. .tuner_set_frequency = stv6110x_set_frequency,
  263. .tuner_get_frequency = stv6110x_get_frequency,
  264. .tuner_set_bandwidth = stv6110x_set_bandwidth,
  265. .tuner_get_bandwidth = stv6110x_get_bandwidth,
  266. .tuner_set_bbgain = stv6110x_set_bbgain,
  267. .tuner_get_bbgain = stv6110x_get_bbgain,
  268. .tuner_set_refclk = stv6110x_set_refclock,
  269. .tuner_get_status = stv6110x_get_status,
  270. };
  271. struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
  272. const struct stv6110x_config *config,
  273. struct i2c_adapter *i2c)
  274. {
  275. struct stv6110x_state *stv6110x;
  276. stv6110x = kzalloc(sizeof (struct stv6110x_state), GFP_KERNEL);
  277. if (stv6110x == NULL)
  278. goto error;
  279. stv6110x->i2c = i2c;
  280. stv6110x->config = config;
  281. stv6110x->devctl = &stv6110x_ctl;
  282. fe->tuner_priv = stv6110x;
  283. fe->ops.tuner_ops = stv6110x_ops;
  284. printk("%s: Attaching STV6110x \n", __func__);
  285. return stv6110x->devctl;
  286. error:
  287. kfree(stv6110x);
  288. return NULL;
  289. }
  290. EXPORT_SYMBOL(stv6110x_attach);
  291. MODULE_AUTHOR("Manu Abraham");
  292. MODULE_DESCRIPTION("STV6110x Silicon tuner");
  293. MODULE_LICENSE("GPL");