stv6110x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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, pCalcOpt = 1000;
  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)(pCalcOpt - pVal))))
  101. rDivOpt = rDiv;
  102. pCalcOpt = (REFCLOCK_kHz / 100) / R_DIV(rDivOpt);
  103. }
  104. divider = (frequency * R_DIV(rDivOpt) * pVal) / REFCLOCK_kHz;
  105. divider = (divider + 5) / 10;
  106. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_R_DIV, rDivOpt);
  107. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_N_DIV_11_8, MSB(divider));
  108. STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG0], TNG0_N_DIV_7_0, LSB(divider));
  109. /* VCO Auto calibration */
  110. STV6110x_SETFIELD(stv6110x_regs[STV6110x_STAT1], STAT1_CALVCO_STRT, 1);
  111. stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x_regs[STV6110x_CTRL1]);
  112. stv6110x_write_reg(stv6110x, STV6110x_TNG1, stv6110x_regs[STV6110x_TNG1]);
  113. stv6110x_write_reg(stv6110x, STV6110x_TNG0, stv6110x_regs[STV6110x_TNG0]);
  114. stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x_regs[STV6110x_STAT1]);
  115. for (i = 0; i < TRIALS; i++) {
  116. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
  117. if (!STV6110x_GETFIELD(STAT1_CALVCO_STRT, stv6110x_regs[STV6110x_STAT1]))
  118. break;
  119. msleep(1);
  120. }
  121. return 0;
  122. }
  123. static int stv6110x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  124. {
  125. struct stv6110x_state *stv6110x = fe->tuner_priv;
  126. stv6110x_read_reg(stv6110x, STV6110x_TNG1, &stv6110x_regs[STV6110x_TNG1]);
  127. stv6110x_read_reg(stv6110x, STV6110x_TNG0, &stv6110x_regs[STV6110x_TNG0]);
  128. *frequency = (MAKEWORD16(STV6110x_GETFIELD(TNG1_N_DIV_11_8, stv6110x_regs[STV6110x_TNG1]),
  129. STV6110x_GETFIELD(TNG0_N_DIV_7_0, stv6110x_regs[STV6110x_TNG0]))) * REFCLOCK_kHz;
  130. *frequency /= (1 << (STV6110x_GETFIELD(TNG1_R_DIV, stv6110x_regs[STV6110x_TNG1]) +
  131. STV6110x_GETFIELD(TNG1_DIV4SEL, stv6110x_regs[STV6110x_TNG1])));
  132. *frequency >>= 2;
  133. return 0;
  134. }
  135. static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
  136. {
  137. struct stv6110x_state *stv6110x = fe->tuner_priv;
  138. u32 halfbw;
  139. u8 i;
  140. halfbw = bandwidth >> 1;
  141. if (halfbw > 36000000)
  142. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, 31); /* LPF */
  143. else if (halfbw < 5000000)
  144. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, 0); /* LPF */
  145. else
  146. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, ((halfbw / 1000000) - 5)); /* LPF */
  147. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x0); /* cal. clk activated */
  148. STV6110x_SETFIELD(stv6110x_regs[STV6110x_STAT1], STAT1_CALRC_STRT, 0x1); /* LPF auto cal */
  149. stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x_regs[STV6110x_CTRL3]);
  150. stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x_regs[STV6110x_STAT1]);
  151. for (i = 0; i < TRIALS; i++) {
  152. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
  153. if (!STV6110x_GETFIELD(STAT1_CALRC_STRT, stv6110x_regs[STV6110x_STAT1]))
  154. break;
  155. msleep(1);
  156. }
  157. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */
  158. stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x_regs[STV6110x_CTRL3]);
  159. return 0;
  160. }
  161. static int stv6110x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  162. {
  163. struct stv6110x_state *stv6110x = fe->tuner_priv;
  164. stv6110x_read_reg(stv6110x, STV6110x_CTRL3, &stv6110x_regs[STV6110x_CTRL3]);
  165. *bandwidth = (STV6110x_GETFIELD(CTRL3_CF, stv6110x_regs[STV6110x_CTRL3]) + 5) * 2000000;
  166. return 0;
  167. }
  168. static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock)
  169. {
  170. struct stv6110x_state *stv6110x = fe->tuner_priv;
  171. /* setup divider */
  172. switch (refclock) {
  173. default:
  174. case 1:
  175. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
  176. break;
  177. case 2:
  178. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
  179. break;
  180. case 4:
  181. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
  182. break;
  183. case 8:
  184. case 0:
  185. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
  186. break;
  187. }
  188. stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x_regs[STV6110x_CTRL2]);
  189. return 0;
  190. }
  191. static int stv6110x_get_bbgain(struct dvb_frontend *fe, u32 *gain)
  192. {
  193. struct stv6110x_state *stv6110x = fe->tuner_priv;
  194. stv6110x_read_reg(stv6110x, STV6110x_CTRL2, &stv6110x_regs[STV6110x_CTRL2]);
  195. *gain = 2 * STV6110x_GETFIELD(CTRL2_BBGAIN, stv6110x_regs[STV6110x_CTRL2]);
  196. return 0;
  197. }
  198. static int stv6110x_set_bbgain(struct dvb_frontend *fe, u32 gain)
  199. {
  200. struct stv6110x_state *stv6110x = fe->tuner_priv;
  201. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_BBGAIN, gain / 2);
  202. stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x_regs[STV6110x_CTRL2]);
  203. return 0;
  204. }
  205. static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode)
  206. {
  207. struct stv6110x_state *stv6110x = fe->tuner_priv;
  208. int ret;
  209. switch (mode) {
  210. case TUNER_SLEEP:
  211. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_SYN, 0);
  212. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_RX, 0);
  213. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_LPT, 0);
  214. break;
  215. case TUNER_WAKE:
  216. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_SYN, 1);
  217. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_RX, 1);
  218. STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_LPT, 1);
  219. break;
  220. }
  221. ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x_regs[STV6110x_CTRL1]);
  222. if (ret < 0) {
  223. dprintk(FE_ERROR, 1, "I/O Error");
  224. return -EIO;
  225. }
  226. return 0;
  227. }
  228. static int stv6110x_sleep(struct dvb_frontend *fe)
  229. {
  230. return stv6110x_set_mode(fe, TUNER_SLEEP);
  231. }
  232. static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status)
  233. {
  234. struct stv6110x_state *stv6110x = fe->tuner_priv;
  235. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
  236. if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x_regs[STV6110x_STAT1]))
  237. *status = TUNER_PHASELOCKED;
  238. else
  239. *status = 0;
  240. return 0;
  241. }
  242. static int stv6110x_release(struct dvb_frontend *fe)
  243. {
  244. struct stv6110x_state *stv6110x = fe->tuner_priv;
  245. fe->tuner_priv = NULL;
  246. kfree(stv6110x);
  247. return 0;
  248. }
  249. static struct dvb_tuner_ops stv6110x_ops = {
  250. .info = {
  251. .name = "STV6110(A) Silicon Tuner",
  252. .frequency_min = 950000,
  253. .frequency_max = 2150000,
  254. .frequency_step = 0,
  255. },
  256. .init = stv6110x_init,
  257. .sleep = stv6110x_sleep,
  258. .release = stv6110x_release
  259. };
  260. static struct stv6110x_devctl stv6110x_ctl = {
  261. .tuner_init = stv6110x_init,
  262. .tuner_set_mode = stv6110x_set_mode,
  263. .tuner_set_frequency = stv6110x_set_frequency,
  264. .tuner_get_frequency = stv6110x_get_frequency,
  265. .tuner_set_bandwidth = stv6110x_set_bandwidth,
  266. .tuner_get_bandwidth = stv6110x_get_bandwidth,
  267. .tuner_set_bbgain = stv6110x_set_bbgain,
  268. .tuner_get_bbgain = stv6110x_get_bbgain,
  269. .tuner_set_refclk = stv6110x_set_refclock,
  270. .tuner_get_status = stv6110x_get_status,
  271. };
  272. struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
  273. const struct stv6110x_config *config,
  274. struct i2c_adapter *i2c)
  275. {
  276. struct stv6110x_state *stv6110x;
  277. stv6110x = kzalloc(sizeof (struct stv6110x_state), GFP_KERNEL);
  278. if (stv6110x == NULL)
  279. goto error;
  280. stv6110x->i2c = i2c;
  281. stv6110x->config = config;
  282. stv6110x->devctl = &stv6110x_ctl;
  283. fe->tuner_priv = stv6110x;
  284. fe->ops.tuner_ops = stv6110x_ops;
  285. printk("%s: Attaching STV6110x \n", __func__);
  286. return stv6110x->devctl;
  287. error:
  288. kfree(stv6110x);
  289. return NULL;
  290. }
  291. EXPORT_SYMBOL(stv6110x_attach);
  292. MODULE_AUTHOR("Manu Abraham");
  293. MODULE_DESCRIPTION("STV6110x Silicon tuner");
  294. MODULE_LICENSE("GPL");