stv6110x.c 12 KB

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