stv6110x.c 12 KB

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