stv6110x.c 12 KB

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