stv6110x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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/slab.h>
  21. #include <linux/string.h>
  22. #include "dvb_frontend.h"
  23. #include "stv6110x_reg.h"
  24. #include "stv6110x.h"
  25. #include "stv6110x_priv.h"
  26. static unsigned int verbose;
  27. module_param(verbose, int, 0644);
  28. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  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_regs(struct stv6110x_state *stv6110x, int start, u8 data[], int len)
  48. {
  49. int ret;
  50. const struct stv6110x_config *config = stv6110x->config;
  51. u8 buf[len + 1];
  52. struct i2c_msg msg = {
  53. .addr = config->addr,
  54. .flags = 0,
  55. .buf = buf,
  56. .len = len + 1
  57. };
  58. if (start + len > 8)
  59. return -EINVAL;
  60. buf[0] = start;
  61. memcpy(&buf[1], data, len);
  62. ret = i2c_transfer(stv6110x->i2c, &msg, 1);
  63. if (ret != 1) {
  64. dprintk(FE_ERROR, 1, "I/O Error");
  65. return -EREMOTEIO;
  66. }
  67. return 0;
  68. }
  69. static int stv6110x_write_reg(struct stv6110x_state *stv6110x, u8 reg, u8 data)
  70. {
  71. return stv6110x_write_regs(stv6110x, reg, &data, 1);
  72. }
  73. static int stv6110x_init(struct dvb_frontend *fe)
  74. {
  75. struct stv6110x_state *stv6110x = fe->tuner_priv;
  76. int ret;
  77. ret = stv6110x_write_regs(stv6110x, 0, stv6110x->regs,
  78. ARRAY_SIZE(stv6110x->regs));
  79. if (ret < 0) {
  80. dprintk(FE_ERROR, 1, "Initialization failed");
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. static int stv6110x_set_frequency(struct dvb_frontend *fe, u32 frequency)
  86. {
  87. struct stv6110x_state *stv6110x = fe->tuner_priv;
  88. u32 rDiv, divider;
  89. s32 pVal, pCalc, rDivOpt = 0, pCalcOpt = 1000;
  90. u8 i;
  91. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_K, (REFCLOCK_MHz - 16));
  92. if (frequency <= 1023000) {
  93. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
  94. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
  95. pVal = 40;
  96. } else if (frequency <= 1300000) {
  97. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
  98. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
  99. pVal = 40;
  100. } else if (frequency <= 2046000) {
  101. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
  102. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
  103. pVal = 20;
  104. } else {
  105. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
  106. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
  107. pVal = 20;
  108. }
  109. for (rDiv = 0; rDiv <= 3; rDiv++) {
  110. pCalc = (REFCLOCK_kHz / 100) / R_DIV(rDiv);
  111. if ((abs((s32)(pCalc - pVal))) < (abs((s32)(pCalcOpt - pVal))))
  112. rDivOpt = rDiv;
  113. pCalcOpt = (REFCLOCK_kHz / 100) / R_DIV(rDivOpt);
  114. }
  115. divider = (frequency * R_DIV(rDivOpt) * pVal) / REFCLOCK_kHz;
  116. divider = (divider + 5) / 10;
  117. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_R_DIV, rDivOpt);
  118. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_N_DIV_11_8, MSB(divider));
  119. STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG0], TNG0_N_DIV_7_0, LSB(divider));
  120. /* VCO Auto calibration */
  121. STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALVCO_STRT, 1);
  122. stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]);
  123. stv6110x_write_reg(stv6110x, STV6110x_TNG1, stv6110x->regs[STV6110x_TNG1]);
  124. stv6110x_write_reg(stv6110x, STV6110x_TNG0, stv6110x->regs[STV6110x_TNG0]);
  125. stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]);
  126. for (i = 0; i < TRIALS; i++) {
  127. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
  128. if (!STV6110x_GETFIELD(STAT1_CALVCO_STRT, stv6110x->regs[STV6110x_STAT1]))
  129. break;
  130. msleep(1);
  131. }
  132. return 0;
  133. }
  134. static int stv6110x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  135. {
  136. struct stv6110x_state *stv6110x = fe->tuner_priv;
  137. stv6110x_read_reg(stv6110x, STV6110x_TNG1, &stv6110x->regs[STV6110x_TNG1]);
  138. stv6110x_read_reg(stv6110x, STV6110x_TNG0, &stv6110x->regs[STV6110x_TNG0]);
  139. *frequency = (MAKEWORD16(STV6110x_GETFIELD(TNG1_N_DIV_11_8, stv6110x->regs[STV6110x_TNG1]),
  140. STV6110x_GETFIELD(TNG0_N_DIV_7_0, stv6110x->regs[STV6110x_TNG0]))) * REFCLOCK_kHz;
  141. *frequency /= (1 << (STV6110x_GETFIELD(TNG1_R_DIV, stv6110x->regs[STV6110x_TNG1]) +
  142. STV6110x_GETFIELD(TNG1_DIV4SEL, stv6110x->regs[STV6110x_TNG1])));
  143. *frequency >>= 2;
  144. return 0;
  145. }
  146. static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
  147. {
  148. struct stv6110x_state *stv6110x = fe->tuner_priv;
  149. u32 halfbw;
  150. u8 i;
  151. halfbw = bandwidth >> 1;
  152. if (halfbw > 36000000)
  153. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 31); /* LPF */
  154. else if (halfbw < 5000000)
  155. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 0); /* LPF */
  156. else
  157. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, ((halfbw / 1000000) - 5)); /* LPF */
  158. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x0); /* cal. clk activated */
  159. STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALRC_STRT, 0x1); /* LPF auto cal */
  160. stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]);
  161. stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]);
  162. for (i = 0; i < TRIALS; i++) {
  163. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
  164. if (!STV6110x_GETFIELD(STAT1_CALRC_STRT, stv6110x->regs[STV6110x_STAT1]))
  165. break;
  166. msleep(1);
  167. }
  168. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */
  169. stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]);
  170. return 0;
  171. }
  172. static int stv6110x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  173. {
  174. struct stv6110x_state *stv6110x = fe->tuner_priv;
  175. stv6110x_read_reg(stv6110x, STV6110x_CTRL3, &stv6110x->regs[STV6110x_CTRL3]);
  176. *bandwidth = (STV6110x_GETFIELD(CTRL3_CF, stv6110x->regs[STV6110x_CTRL3]) + 5) * 2000000;
  177. return 0;
  178. }
  179. static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock)
  180. {
  181. struct stv6110x_state *stv6110x = fe->tuner_priv;
  182. /* setup divider */
  183. switch (refclock) {
  184. default:
  185. case 1:
  186. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
  187. break;
  188. case 2:
  189. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
  190. break;
  191. case 4:
  192. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
  193. break;
  194. case 8:
  195. case 0:
  196. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
  197. break;
  198. }
  199. stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]);
  200. return 0;
  201. }
  202. static int stv6110x_get_bbgain(struct dvb_frontend *fe, u32 *gain)
  203. {
  204. struct stv6110x_state *stv6110x = fe->tuner_priv;
  205. stv6110x_read_reg(stv6110x, STV6110x_CTRL2, &stv6110x->regs[STV6110x_CTRL2]);
  206. *gain = 2 * STV6110x_GETFIELD(CTRL2_BBGAIN, stv6110x->regs[STV6110x_CTRL2]);
  207. return 0;
  208. }
  209. static int stv6110x_set_bbgain(struct dvb_frontend *fe, u32 gain)
  210. {
  211. struct stv6110x_state *stv6110x = fe->tuner_priv;
  212. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_BBGAIN, gain / 2);
  213. stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]);
  214. return 0;
  215. }
  216. static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode)
  217. {
  218. struct stv6110x_state *stv6110x = fe->tuner_priv;
  219. int ret;
  220. switch (mode) {
  221. case TUNER_SLEEP:
  222. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 0);
  223. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 0);
  224. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 0);
  225. break;
  226. case TUNER_WAKE:
  227. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 1);
  228. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 1);
  229. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 1);
  230. break;
  231. }
  232. ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]);
  233. if (ret < 0) {
  234. dprintk(FE_ERROR, 1, "I/O Error");
  235. return -EIO;
  236. }
  237. return 0;
  238. }
  239. static int stv6110x_sleep(struct dvb_frontend *fe)
  240. {
  241. if (fe->tuner_priv)
  242. return stv6110x_set_mode(fe, TUNER_SLEEP);
  243. return 0;
  244. }
  245. static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status)
  246. {
  247. struct stv6110x_state *stv6110x = fe->tuner_priv;
  248. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
  249. if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x->regs[STV6110x_STAT1]))
  250. *status = TUNER_PHASELOCKED;
  251. else
  252. *status = 0;
  253. return 0;
  254. }
  255. static int stv6110x_release(struct dvb_frontend *fe)
  256. {
  257. struct stv6110x_state *stv6110x = fe->tuner_priv;
  258. fe->tuner_priv = NULL;
  259. kfree(stv6110x);
  260. return 0;
  261. }
  262. static struct dvb_tuner_ops stv6110x_ops = {
  263. .info = {
  264. .name = "STV6110(A) Silicon Tuner",
  265. .frequency_min = 950000,
  266. .frequency_max = 2150000,
  267. .frequency_step = 0,
  268. },
  269. .release = stv6110x_release
  270. };
  271. static struct stv6110x_devctl stv6110x_ctl = {
  272. .tuner_init = stv6110x_init,
  273. .tuner_sleep = stv6110x_sleep,
  274. .tuner_set_mode = stv6110x_set_mode,
  275. .tuner_set_frequency = stv6110x_set_frequency,
  276. .tuner_get_frequency = stv6110x_get_frequency,
  277. .tuner_set_bandwidth = stv6110x_set_bandwidth,
  278. .tuner_get_bandwidth = stv6110x_get_bandwidth,
  279. .tuner_set_bbgain = stv6110x_set_bbgain,
  280. .tuner_get_bbgain = stv6110x_get_bbgain,
  281. .tuner_set_refclk = stv6110x_set_refclock,
  282. .tuner_get_status = stv6110x_get_status,
  283. };
  284. struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
  285. const struct stv6110x_config *config,
  286. struct i2c_adapter *i2c)
  287. {
  288. struct stv6110x_state *stv6110x;
  289. u8 default_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e};
  290. stv6110x = kzalloc(sizeof (struct stv6110x_state), GFP_KERNEL);
  291. if (!stv6110x)
  292. return NULL;
  293. stv6110x->i2c = i2c;
  294. stv6110x->config = config;
  295. stv6110x->devctl = &stv6110x_ctl;
  296. memcpy(stv6110x->regs, default_regs, 8);
  297. /* setup divider */
  298. switch (stv6110x->config->clk_div) {
  299. default:
  300. case 1:
  301. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
  302. break;
  303. case 2:
  304. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
  305. break;
  306. case 4:
  307. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
  308. break;
  309. case 8:
  310. case 0:
  311. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
  312. break;
  313. }
  314. fe->tuner_priv = stv6110x;
  315. fe->ops.tuner_ops = stv6110x_ops;
  316. printk(KERN_INFO "%s: Attaching STV6110x\n", __func__);
  317. return stv6110x->devctl;
  318. }
  319. EXPORT_SYMBOL(stv6110x_attach);
  320. MODULE_AUTHOR("Manu Abraham");
  321. MODULE_DESCRIPTION("STV6110x Silicon tuner");
  322. MODULE_LICENSE("GPL");