stv6110x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. STV6110(A) Silicon tuner driver
  4. Copyright (C) Manu Abraham <abraham.manu@gmail.com>
  5. Copyright (C) ST Microelectronics
  6. */
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/string.h>
  12. #include <media/dvb_frontend.h>
  13. #include "stv6110x_reg.h"
  14. #include "stv6110x.h"
  15. #include "stv6110x_priv.h"
  16. /* Max transfer size done by I2C transfer functions */
  17. #define MAX_XFER_SIZE 64
  18. static unsigned int verbose;
  19. module_param(verbose, int, 0644);
  20. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  21. static int stv6110x_read_reg(struct stv6110x_state *stv6110x, u8 reg, u8 *data)
  22. {
  23. int ret;
  24. const struct stv6110x_config *config = stv6110x->config;
  25. u8 b0[] = { reg };
  26. u8 b1[] = { 0 };
  27. struct i2c_msg msg[] = {
  28. { .addr = config->addr, .flags = 0, .buf = b0, .len = 1 },
  29. { .addr = config->addr, .flags = I2C_M_RD, .buf = b1, .len = 1 }
  30. };
  31. ret = i2c_transfer(stv6110x->i2c, msg, 2);
  32. if (ret != 2) {
  33. dprintk(FE_ERROR, 1, "I/O Error");
  34. return -EREMOTEIO;
  35. }
  36. *data = b1[0];
  37. return 0;
  38. }
  39. static int stv6110x_write_regs(struct stv6110x_state *stv6110x, int start, u8 data[], int len)
  40. {
  41. int ret;
  42. const struct stv6110x_config *config = stv6110x->config;
  43. u8 buf[MAX_XFER_SIZE];
  44. struct i2c_msg msg = {
  45. .addr = config->addr,
  46. .flags = 0,
  47. .buf = buf,
  48. .len = len + 1
  49. };
  50. if (1 + len > sizeof(buf)) {
  51. printk(KERN_WARNING
  52. "%s: i2c wr: len=%d is too big!\n",
  53. KBUILD_MODNAME, len);
  54. return -EINVAL;
  55. }
  56. if (start + len > 8)
  57. return -EINVAL;
  58. buf[0] = start;
  59. memcpy(&buf[1], data, len);
  60. ret = i2c_transfer(stv6110x->i2c, &msg, 1);
  61. if (ret != 1) {
  62. dprintk(FE_ERROR, 1, "I/O Error");
  63. return -EREMOTEIO;
  64. }
  65. return 0;
  66. }
  67. static int stv6110x_write_reg(struct stv6110x_state *stv6110x, u8 reg, u8 data)
  68. {
  69. u8 tmp = data; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
  70. return stv6110x_write_regs(stv6110x, reg, &tmp, 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. if (fe->tuner_priv)
  241. return stv6110x_set_mode(fe, TUNER_SLEEP);
  242. return 0;
  243. }
  244. static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status)
  245. {
  246. struct stv6110x_state *stv6110x = fe->tuner_priv;
  247. stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
  248. if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x->regs[STV6110x_STAT1]))
  249. *status = TUNER_PHASELOCKED;
  250. else
  251. *status = 0;
  252. return 0;
  253. }
  254. static void stv6110x_release(struct dvb_frontend *fe)
  255. {
  256. struct stv6110x_state *stv6110x = fe->tuner_priv;
  257. fe->tuner_priv = NULL;
  258. kfree(stv6110x);
  259. }
  260. static void st6110x_init_regs(struct stv6110x_state *stv6110x)
  261. {
  262. u8 default_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e};
  263. memcpy(stv6110x->regs, default_regs, 8);
  264. }
  265. static void stv6110x_setup_divider(struct stv6110x_state *stv6110x)
  266. {
  267. switch (stv6110x->config->clk_div) {
  268. default:
  269. case 1:
  270. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
  271. CTRL2_CO_DIV,
  272. 0);
  273. break;
  274. case 2:
  275. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
  276. CTRL2_CO_DIV,
  277. 1);
  278. break;
  279. case 4:
  280. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
  281. CTRL2_CO_DIV,
  282. 2);
  283. break;
  284. case 8:
  285. case 0:
  286. STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
  287. CTRL2_CO_DIV,
  288. 3);
  289. break;
  290. }
  291. }
  292. static const struct dvb_tuner_ops stv6110x_ops = {
  293. .info = {
  294. .name = "STV6110(A) Silicon Tuner",
  295. .frequency_min_hz = 950 * MHz,
  296. .frequency_max_hz = 2150 * MHz,
  297. },
  298. .release = stv6110x_release
  299. };
  300. static struct stv6110x_devctl stv6110x_ctl = {
  301. .tuner_init = stv6110x_init,
  302. .tuner_sleep = stv6110x_sleep,
  303. .tuner_set_mode = stv6110x_set_mode,
  304. .tuner_set_frequency = stv6110x_set_frequency,
  305. .tuner_get_frequency = stv6110x_get_frequency,
  306. .tuner_set_bandwidth = stv6110x_set_bandwidth,
  307. .tuner_get_bandwidth = stv6110x_get_bandwidth,
  308. .tuner_set_bbgain = stv6110x_set_bbgain,
  309. .tuner_get_bbgain = stv6110x_get_bbgain,
  310. .tuner_set_refclk = stv6110x_set_refclock,
  311. .tuner_get_status = stv6110x_get_status,
  312. };
  313. static void stv6110x_set_frontend_opts(struct stv6110x_state *stv6110x)
  314. {
  315. stv6110x->frontend->tuner_priv = stv6110x;
  316. stv6110x->frontend->ops.tuner_ops = stv6110x_ops;
  317. }
  318. static struct stv6110x_devctl *stv6110x_get_devctl(struct i2c_client *client)
  319. {
  320. struct stv6110x_state *stv6110x = i2c_get_clientdata(client);
  321. dev_dbg(&client->dev, "\n");
  322. return stv6110x->devctl;
  323. }
  324. static int stv6110x_probe(struct i2c_client *client,
  325. const struct i2c_device_id *id)
  326. {
  327. struct stv6110x_config *config = client->dev.platform_data;
  328. struct stv6110x_state *stv6110x;
  329. stv6110x = kzalloc(sizeof(*stv6110x), GFP_KERNEL);
  330. if (!stv6110x)
  331. return -ENOMEM;
  332. stv6110x->frontend = config->frontend;
  333. stv6110x->i2c = client->adapter;
  334. stv6110x->config = config;
  335. stv6110x->devctl = &stv6110x_ctl;
  336. st6110x_init_regs(stv6110x);
  337. stv6110x_setup_divider(stv6110x);
  338. stv6110x_set_frontend_opts(stv6110x);
  339. dev_info(&stv6110x->i2c->dev, "Probed STV6110x\n");
  340. i2c_set_clientdata(client, stv6110x);
  341. /* setup callbacks */
  342. config->get_devctl = stv6110x_get_devctl;
  343. return 0;
  344. }
  345. static int stv6110x_remove(struct i2c_client *client)
  346. {
  347. struct stv6110x_state *stv6110x = i2c_get_clientdata(client);
  348. stv6110x_release(stv6110x->frontend);
  349. return 0;
  350. }
  351. const struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
  352. const struct stv6110x_config *config,
  353. struct i2c_adapter *i2c)
  354. {
  355. struct stv6110x_state *stv6110x;
  356. stv6110x = kzalloc(sizeof(*stv6110x), GFP_KERNEL);
  357. if (!stv6110x)
  358. return NULL;
  359. stv6110x->frontend = fe;
  360. stv6110x->i2c = i2c;
  361. stv6110x->config = config;
  362. stv6110x->devctl = &stv6110x_ctl;
  363. st6110x_init_regs(stv6110x);
  364. stv6110x_setup_divider(stv6110x);
  365. stv6110x_set_frontend_opts(stv6110x);
  366. fe->tuner_priv = stv6110x;
  367. fe->ops.tuner_ops = stv6110x_ops;
  368. dev_info(&stv6110x->i2c->dev, "Attaching STV6110x\n");
  369. return stv6110x->devctl;
  370. }
  371. EXPORT_SYMBOL(stv6110x_attach);
  372. static const struct i2c_device_id stv6110x_id_table[] = {
  373. {"stv6110x", 0},
  374. {}
  375. };
  376. MODULE_DEVICE_TABLE(i2c, stv6110x_id_table);
  377. static struct i2c_driver stv6110x_driver = {
  378. .driver = {
  379. .name = "stv6110x",
  380. .suppress_bind_attrs = true,
  381. },
  382. .probe = stv6110x_probe,
  383. .remove = stv6110x_remove,
  384. .id_table = stv6110x_id_table,
  385. };
  386. module_i2c_driver(stv6110x_driver);
  387. MODULE_AUTHOR("Manu Abraham");
  388. MODULE_DESCRIPTION("STV6110x Silicon tuner");
  389. MODULE_LICENSE("GPL");