s5h1420.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for
  4. * Samsung S5H1420 and
  5. * PnpNetwork PN1010 QPSK Demodulator
  6. *
  7. * Copyright (C) 2005 Andrew de Quincey <adq_dvb@lidskialf.net>
  8. * Copyright (C) 2005-8 Patrick Boettcher <pb@linuxtv.org>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/jiffies.h>
  17. #include <asm/div64.h>
  18. #include <linux/i2c.h>
  19. #include <media/dvb_frontend.h>
  20. #include "s5h1420.h"
  21. #include "s5h1420_priv.h"
  22. #define TONE_FREQ 22000
  23. struct s5h1420_state {
  24. struct i2c_adapter* i2c;
  25. const struct s5h1420_config* config;
  26. struct dvb_frontend frontend;
  27. struct i2c_adapter tuner_i2c_adapter;
  28. u8 CON_1_val;
  29. u8 postlocked:1;
  30. u32 fclk;
  31. u32 tunedfreq;
  32. enum fe_code_rate fec_inner;
  33. u32 symbol_rate;
  34. /* FIXME: ugly workaround for flexcop's incapable i2c-controller
  35. * it does not support repeated-start, workaround: write addr-1
  36. * and then read
  37. */
  38. u8 shadow[256];
  39. };
  40. static u32 s5h1420_getsymbolrate(struct s5h1420_state* state);
  41. static int s5h1420_get_tune_settings(struct dvb_frontend* fe,
  42. struct dvb_frontend_tune_settings* fesettings);
  43. static int debug;
  44. module_param(debug, int, 0644);
  45. MODULE_PARM_DESC(debug, "enable debugging");
  46. #define dprintk(x...) do { \
  47. if (debug) \
  48. printk(KERN_DEBUG "S5H1420: " x); \
  49. } while (0)
  50. static u8 s5h1420_readreg(struct s5h1420_state *state, u8 reg)
  51. {
  52. int ret;
  53. u8 b[2];
  54. struct i2c_msg msg[] = {
  55. { .addr = state->config->demod_address, .flags = 0, .buf = b, .len = 2 },
  56. { .addr = state->config->demod_address, .flags = 0, .buf = &reg, .len = 1 },
  57. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = 1 },
  58. };
  59. b[0] = (reg - 1) & 0xff;
  60. b[1] = state->shadow[(reg - 1) & 0xff];
  61. if (state->config->repeated_start_workaround) {
  62. ret = i2c_transfer(state->i2c, msg, 3);
  63. if (ret != 3)
  64. return ret;
  65. } else {
  66. ret = i2c_transfer(state->i2c, &msg[1], 1);
  67. if (ret != 1)
  68. return ret;
  69. ret = i2c_transfer(state->i2c, &msg[2], 1);
  70. if (ret != 1)
  71. return ret;
  72. }
  73. /* dprintk("rd(%02x): %02x %02x\n", state->config->demod_address, reg, b[0]); */
  74. return b[0];
  75. }
  76. static int s5h1420_writereg (struct s5h1420_state* state, u8 reg, u8 data)
  77. {
  78. u8 buf[] = { reg, data };
  79. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  80. int err;
  81. /* dprintk("wr(%02x): %02x %02x\n", state->config->demod_address, reg, data); */
  82. err = i2c_transfer(state->i2c, &msg, 1);
  83. if (err != 1) {
  84. dprintk("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __func__, err, reg, data);
  85. return -EREMOTEIO;
  86. }
  87. state->shadow[reg] = data;
  88. return 0;
  89. }
  90. static int s5h1420_set_voltage(struct dvb_frontend *fe,
  91. enum fe_sec_voltage voltage)
  92. {
  93. struct s5h1420_state* state = fe->demodulator_priv;
  94. dprintk("enter %s\n", __func__);
  95. switch(voltage) {
  96. case SEC_VOLTAGE_13:
  97. s5h1420_writereg(state, 0x3c,
  98. (s5h1420_readreg(state, 0x3c) & 0xfe) | 0x02);
  99. break;
  100. case SEC_VOLTAGE_18:
  101. s5h1420_writereg(state, 0x3c, s5h1420_readreg(state, 0x3c) | 0x03);
  102. break;
  103. case SEC_VOLTAGE_OFF:
  104. s5h1420_writereg(state, 0x3c, s5h1420_readreg(state, 0x3c) & 0xfd);
  105. break;
  106. }
  107. dprintk("leave %s\n", __func__);
  108. return 0;
  109. }
  110. static int s5h1420_set_tone(struct dvb_frontend *fe,
  111. enum fe_sec_tone_mode tone)
  112. {
  113. struct s5h1420_state* state = fe->demodulator_priv;
  114. dprintk("enter %s\n", __func__);
  115. switch(tone) {
  116. case SEC_TONE_ON:
  117. s5h1420_writereg(state, 0x3b,
  118. (s5h1420_readreg(state, 0x3b) & 0x74) | 0x08);
  119. break;
  120. case SEC_TONE_OFF:
  121. s5h1420_writereg(state, 0x3b,
  122. (s5h1420_readreg(state, 0x3b) & 0x74) | 0x01);
  123. break;
  124. }
  125. dprintk("leave %s\n", __func__);
  126. return 0;
  127. }
  128. static int s5h1420_send_master_cmd (struct dvb_frontend* fe,
  129. struct dvb_diseqc_master_cmd* cmd)
  130. {
  131. struct s5h1420_state* state = fe->demodulator_priv;
  132. u8 val;
  133. int i;
  134. unsigned long timeout;
  135. int result = 0;
  136. dprintk("enter %s\n", __func__);
  137. if (cmd->msg_len > sizeof(cmd->msg))
  138. return -EINVAL;
  139. /* setup for DISEQC */
  140. val = s5h1420_readreg(state, 0x3b);
  141. s5h1420_writereg(state, 0x3b, 0x02);
  142. msleep(15);
  143. /* write the DISEQC command bytes */
  144. for(i=0; i< cmd->msg_len; i++) {
  145. s5h1420_writereg(state, 0x3d + i, cmd->msg[i]);
  146. }
  147. /* kick off transmission */
  148. s5h1420_writereg(state, 0x3b, s5h1420_readreg(state, 0x3b) |
  149. ((cmd->msg_len-1) << 4) | 0x08);
  150. /* wait for transmission to complete */
  151. timeout = jiffies + ((100*HZ) / 1000);
  152. while(time_before(jiffies, timeout)) {
  153. if (!(s5h1420_readreg(state, 0x3b) & 0x08))
  154. break;
  155. msleep(5);
  156. }
  157. if (time_after(jiffies, timeout))
  158. result = -ETIMEDOUT;
  159. /* restore original settings */
  160. s5h1420_writereg(state, 0x3b, val);
  161. msleep(15);
  162. dprintk("leave %s\n", __func__);
  163. return result;
  164. }
  165. static int s5h1420_recv_slave_reply (struct dvb_frontend* fe,
  166. struct dvb_diseqc_slave_reply* reply)
  167. {
  168. struct s5h1420_state* state = fe->demodulator_priv;
  169. u8 val;
  170. int i;
  171. int length;
  172. unsigned long timeout;
  173. int result = 0;
  174. /* setup for DISEQC receive */
  175. val = s5h1420_readreg(state, 0x3b);
  176. s5h1420_writereg(state, 0x3b, 0x82); /* FIXME: guess - do we need to set DIS_RDY(0x08) in receive mode? */
  177. msleep(15);
  178. /* wait for reception to complete */
  179. timeout = jiffies + ((reply->timeout*HZ) / 1000);
  180. while(time_before(jiffies, timeout)) {
  181. if (!(s5h1420_readreg(state, 0x3b) & 0x80)) /* FIXME: do we test DIS_RDY(0x08) or RCV_EN(0x80)? */
  182. break;
  183. msleep(5);
  184. }
  185. if (time_after(jiffies, timeout)) {
  186. result = -ETIMEDOUT;
  187. goto exit;
  188. }
  189. /* check error flag - FIXME: not sure what this does - docs do not describe
  190. * beyond "error flag for diseqc receive data :( */
  191. if (s5h1420_readreg(state, 0x49)) {
  192. result = -EIO;
  193. goto exit;
  194. }
  195. /* check length */
  196. length = (s5h1420_readreg(state, 0x3b) & 0x70) >> 4;
  197. if (length > sizeof(reply->msg)) {
  198. result = -EOVERFLOW;
  199. goto exit;
  200. }
  201. reply->msg_len = length;
  202. /* extract data */
  203. for(i=0; i< length; i++) {
  204. reply->msg[i] = s5h1420_readreg(state, 0x3d + i);
  205. }
  206. exit:
  207. /* restore original settings */
  208. s5h1420_writereg(state, 0x3b, val);
  209. msleep(15);
  210. return result;
  211. }
  212. static int s5h1420_send_burst(struct dvb_frontend *fe,
  213. enum fe_sec_mini_cmd minicmd)
  214. {
  215. struct s5h1420_state* state = fe->demodulator_priv;
  216. u8 val;
  217. int result = 0;
  218. unsigned long timeout;
  219. /* setup for tone burst */
  220. val = s5h1420_readreg(state, 0x3b);
  221. s5h1420_writereg(state, 0x3b, (s5h1420_readreg(state, 0x3b) & 0x70) | 0x01);
  222. /* set value for B position if requested */
  223. if (minicmd == SEC_MINI_B) {
  224. s5h1420_writereg(state, 0x3b, s5h1420_readreg(state, 0x3b) | 0x04);
  225. }
  226. msleep(15);
  227. /* start transmission */
  228. s5h1420_writereg(state, 0x3b, s5h1420_readreg(state, 0x3b) | 0x08);
  229. /* wait for transmission to complete */
  230. timeout = jiffies + ((100*HZ) / 1000);
  231. while(time_before(jiffies, timeout)) {
  232. if (!(s5h1420_readreg(state, 0x3b) & 0x08))
  233. break;
  234. msleep(5);
  235. }
  236. if (time_after(jiffies, timeout))
  237. result = -ETIMEDOUT;
  238. /* restore original settings */
  239. s5h1420_writereg(state, 0x3b, val);
  240. msleep(15);
  241. return result;
  242. }
  243. static enum fe_status s5h1420_get_status_bits(struct s5h1420_state *state)
  244. {
  245. u8 val;
  246. enum fe_status status = 0;
  247. val = s5h1420_readreg(state, 0x14);
  248. if (val & 0x02)
  249. status |= FE_HAS_SIGNAL;
  250. if (val & 0x01)
  251. status |= FE_HAS_CARRIER;
  252. val = s5h1420_readreg(state, 0x36);
  253. if (val & 0x01)
  254. status |= FE_HAS_VITERBI;
  255. if (val & 0x20)
  256. status |= FE_HAS_SYNC;
  257. if (status == (FE_HAS_SIGNAL|FE_HAS_CARRIER|FE_HAS_VITERBI|FE_HAS_SYNC))
  258. status |= FE_HAS_LOCK;
  259. return status;
  260. }
  261. static int s5h1420_read_status(struct dvb_frontend *fe,
  262. enum fe_status *status)
  263. {
  264. struct s5h1420_state* state = fe->demodulator_priv;
  265. u8 val;
  266. dprintk("enter %s\n", __func__);
  267. if (status == NULL)
  268. return -EINVAL;
  269. /* determine lock state */
  270. *status = s5h1420_get_status_bits(state);
  271. /* fix for FEC 5/6 inversion issue - if it doesn't quite lock, invert
  272. the inversion, wait a bit and check again */
  273. if (*status == (FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI)) {
  274. val = s5h1420_readreg(state, Vit10);
  275. if ((val & 0x07) == 0x03) {
  276. if (val & 0x08)
  277. s5h1420_writereg(state, Vit09, 0x13);
  278. else
  279. s5h1420_writereg(state, Vit09, 0x1b);
  280. /* wait a bit then update lock status */
  281. mdelay(200);
  282. *status = s5h1420_get_status_bits(state);
  283. }
  284. }
  285. /* perform post lock setup */
  286. if ((*status & FE_HAS_LOCK) && !state->postlocked) {
  287. /* calculate the data rate */
  288. u32 tmp = s5h1420_getsymbolrate(state);
  289. switch (s5h1420_readreg(state, Vit10) & 0x07) {
  290. case 0: tmp = (tmp * 2 * 1) / 2; break;
  291. case 1: tmp = (tmp * 2 * 2) / 3; break;
  292. case 2: tmp = (tmp * 2 * 3) / 4; break;
  293. case 3: tmp = (tmp * 2 * 5) / 6; break;
  294. case 4: tmp = (tmp * 2 * 6) / 7; break;
  295. case 5: tmp = (tmp * 2 * 7) / 8; break;
  296. }
  297. if (tmp == 0) {
  298. printk(KERN_ERR "s5h1420: avoided division by 0\n");
  299. tmp = 1;
  300. }
  301. tmp = state->fclk / tmp;
  302. /* set the MPEG_CLK_INTL for the calculated data rate */
  303. if (tmp < 2)
  304. val = 0x00;
  305. else if (tmp < 5)
  306. val = 0x01;
  307. else if (tmp < 9)
  308. val = 0x02;
  309. else if (tmp < 13)
  310. val = 0x03;
  311. else if (tmp < 17)
  312. val = 0x04;
  313. else if (tmp < 25)
  314. val = 0x05;
  315. else if (tmp < 33)
  316. val = 0x06;
  317. else
  318. val = 0x07;
  319. dprintk("for MPEG_CLK_INTL %d %x\n", tmp, val);
  320. s5h1420_writereg(state, FEC01, 0x18);
  321. s5h1420_writereg(state, FEC01, 0x10);
  322. s5h1420_writereg(state, FEC01, val);
  323. /* Enable "MPEG_Out" */
  324. val = s5h1420_readreg(state, Mpeg02);
  325. s5h1420_writereg(state, Mpeg02, val | (1 << 6));
  326. /* kicker disable */
  327. val = s5h1420_readreg(state, QPSK01) & 0x7f;
  328. s5h1420_writereg(state, QPSK01, val);
  329. /* DC freeze TODO it was never activated by default or it can stay activated */
  330. if (s5h1420_getsymbolrate(state) >= 20000000) {
  331. s5h1420_writereg(state, Loop04, 0x8a);
  332. s5h1420_writereg(state, Loop05, 0x6a);
  333. } else {
  334. s5h1420_writereg(state, Loop04, 0x58);
  335. s5h1420_writereg(state, Loop05, 0x27);
  336. }
  337. /* post-lock processing has been done! */
  338. state->postlocked = 1;
  339. }
  340. dprintk("leave %s\n", __func__);
  341. return 0;
  342. }
  343. static int s5h1420_read_ber(struct dvb_frontend* fe, u32* ber)
  344. {
  345. struct s5h1420_state* state = fe->demodulator_priv;
  346. s5h1420_writereg(state, 0x46, 0x1d);
  347. mdelay(25);
  348. *ber = (s5h1420_readreg(state, 0x48) << 8) | s5h1420_readreg(state, 0x47);
  349. return 0;
  350. }
  351. static int s5h1420_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  352. {
  353. struct s5h1420_state* state = fe->demodulator_priv;
  354. u8 val = s5h1420_readreg(state, 0x15);
  355. *strength = (u16) ((val << 8) | val);
  356. return 0;
  357. }
  358. static int s5h1420_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  359. {
  360. struct s5h1420_state* state = fe->demodulator_priv;
  361. s5h1420_writereg(state, 0x46, 0x1f);
  362. mdelay(25);
  363. *ucblocks = (s5h1420_readreg(state, 0x48) << 8) | s5h1420_readreg(state, 0x47);
  364. return 0;
  365. }
  366. static void s5h1420_reset(struct s5h1420_state* state)
  367. {
  368. dprintk("%s\n", __func__);
  369. s5h1420_writereg (state, 0x01, 0x08);
  370. s5h1420_writereg (state, 0x01, 0x00);
  371. udelay(10);
  372. }
  373. static void s5h1420_setsymbolrate(struct s5h1420_state* state,
  374. struct dtv_frontend_properties *p)
  375. {
  376. u8 v;
  377. u64 val;
  378. dprintk("enter %s\n", __func__);
  379. val = ((u64) p->symbol_rate / 1000ULL) * (1ULL<<24);
  380. if (p->symbol_rate < 29000000)
  381. val *= 2;
  382. do_div(val, (state->fclk / 1000));
  383. dprintk("symbol rate register: %06llx\n", (unsigned long long)val);
  384. v = s5h1420_readreg(state, Loop01);
  385. s5h1420_writereg(state, Loop01, v & 0x7f);
  386. s5h1420_writereg(state, Tnco01, val >> 16);
  387. s5h1420_writereg(state, Tnco02, val >> 8);
  388. s5h1420_writereg(state, Tnco03, val & 0xff);
  389. s5h1420_writereg(state, Loop01, v | 0x80);
  390. dprintk("leave %s\n", __func__);
  391. }
  392. static u32 s5h1420_getsymbolrate(struct s5h1420_state* state)
  393. {
  394. return state->symbol_rate;
  395. }
  396. static void s5h1420_setfreqoffset(struct s5h1420_state* state, int freqoffset)
  397. {
  398. int val;
  399. u8 v;
  400. dprintk("enter %s\n", __func__);
  401. /* remember freqoffset is in kHz, but the chip wants the offset in Hz, so
  402. * divide fclk by 1000000 to get the correct value. */
  403. val = -(int) ((freqoffset * (1<<24)) / (state->fclk / 1000000));
  404. dprintk("phase rotator/freqoffset: %d %06x\n", freqoffset, val);
  405. v = s5h1420_readreg(state, Loop01);
  406. s5h1420_writereg(state, Loop01, v & 0xbf);
  407. s5h1420_writereg(state, Pnco01, val >> 16);
  408. s5h1420_writereg(state, Pnco02, val >> 8);
  409. s5h1420_writereg(state, Pnco03, val & 0xff);
  410. s5h1420_writereg(state, Loop01, v | 0x40);
  411. dprintk("leave %s\n", __func__);
  412. }
  413. static int s5h1420_getfreqoffset(struct s5h1420_state* state)
  414. {
  415. int val;
  416. s5h1420_writereg(state, 0x06, s5h1420_readreg(state, 0x06) | 0x08);
  417. val = s5h1420_readreg(state, 0x0e) << 16;
  418. val |= s5h1420_readreg(state, 0x0f) << 8;
  419. val |= s5h1420_readreg(state, 0x10);
  420. s5h1420_writereg(state, 0x06, s5h1420_readreg(state, 0x06) & 0xf7);
  421. if (val & 0x800000)
  422. val |= 0xff000000;
  423. /* remember freqoffset is in kHz, but the chip wants the offset in Hz, so
  424. * divide fclk by 1000000 to get the correct value. */
  425. val = (((-val) * (state->fclk/1000000)) / (1<<24));
  426. return val;
  427. }
  428. static void s5h1420_setfec_inversion(struct s5h1420_state* state,
  429. struct dtv_frontend_properties *p)
  430. {
  431. u8 inversion = 0;
  432. u8 vit08, vit09;
  433. dprintk("enter %s\n", __func__);
  434. if (p->inversion == INVERSION_OFF)
  435. inversion = state->config->invert ? 0x08 : 0;
  436. else if (p->inversion == INVERSION_ON)
  437. inversion = state->config->invert ? 0 : 0x08;
  438. if ((p->fec_inner == FEC_AUTO) || (p->inversion == INVERSION_AUTO)) {
  439. vit08 = 0x3f;
  440. vit09 = 0;
  441. } else {
  442. switch (p->fec_inner) {
  443. case FEC_1_2:
  444. vit08 = 0x01;
  445. vit09 = 0x10;
  446. break;
  447. case FEC_2_3:
  448. vit08 = 0x02;
  449. vit09 = 0x11;
  450. break;
  451. case FEC_3_4:
  452. vit08 = 0x04;
  453. vit09 = 0x12;
  454. break;
  455. case FEC_5_6:
  456. vit08 = 0x08;
  457. vit09 = 0x13;
  458. break;
  459. case FEC_6_7:
  460. vit08 = 0x10;
  461. vit09 = 0x14;
  462. break;
  463. case FEC_7_8:
  464. vit08 = 0x20;
  465. vit09 = 0x15;
  466. break;
  467. default:
  468. return;
  469. }
  470. }
  471. vit09 |= inversion;
  472. dprintk("fec: %02x %02x\n", vit08, vit09);
  473. s5h1420_writereg(state, Vit08, vit08);
  474. s5h1420_writereg(state, Vit09, vit09);
  475. dprintk("leave %s\n", __func__);
  476. }
  477. static enum fe_code_rate s5h1420_getfec(struct s5h1420_state *state)
  478. {
  479. switch(s5h1420_readreg(state, 0x32) & 0x07) {
  480. case 0:
  481. return FEC_1_2;
  482. case 1:
  483. return FEC_2_3;
  484. case 2:
  485. return FEC_3_4;
  486. case 3:
  487. return FEC_5_6;
  488. case 4:
  489. return FEC_6_7;
  490. case 5:
  491. return FEC_7_8;
  492. }
  493. return FEC_NONE;
  494. }
  495. static enum fe_spectral_inversion
  496. s5h1420_getinversion(struct s5h1420_state *state)
  497. {
  498. if (s5h1420_readreg(state, 0x32) & 0x08)
  499. return INVERSION_ON;
  500. return INVERSION_OFF;
  501. }
  502. static int s5h1420_set_frontend(struct dvb_frontend *fe)
  503. {
  504. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  505. struct s5h1420_state* state = fe->demodulator_priv;
  506. int frequency_delta;
  507. struct dvb_frontend_tune_settings fesettings;
  508. dprintk("enter %s\n", __func__);
  509. /* check if we should do a fast-tune */
  510. s5h1420_get_tune_settings(fe, &fesettings);
  511. frequency_delta = p->frequency - state->tunedfreq;
  512. if ((frequency_delta > -fesettings.max_drift) &&
  513. (frequency_delta < fesettings.max_drift) &&
  514. (frequency_delta != 0) &&
  515. (state->fec_inner == p->fec_inner) &&
  516. (state->symbol_rate == p->symbol_rate)) {
  517. if (fe->ops.tuner_ops.set_params) {
  518. fe->ops.tuner_ops.set_params(fe);
  519. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  520. }
  521. if (fe->ops.tuner_ops.get_frequency) {
  522. u32 tmp;
  523. fe->ops.tuner_ops.get_frequency(fe, &tmp);
  524. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  525. s5h1420_setfreqoffset(state, p->frequency - tmp);
  526. } else {
  527. s5h1420_setfreqoffset(state, 0);
  528. }
  529. dprintk("simple tune\n");
  530. return 0;
  531. }
  532. dprintk("tuning demod\n");
  533. /* first of all, software reset */
  534. s5h1420_reset(state);
  535. /* set s5h1420 fclk PLL according to desired symbol rate */
  536. if (p->symbol_rate > 33000000)
  537. state->fclk = 80000000;
  538. else if (p->symbol_rate > 28500000)
  539. state->fclk = 59000000;
  540. else if (p->symbol_rate > 25000000)
  541. state->fclk = 86000000;
  542. else if (p->symbol_rate > 1900000)
  543. state->fclk = 88000000;
  544. else
  545. state->fclk = 44000000;
  546. dprintk("pll01: %d, ToneFreq: %d\n", state->fclk/1000000 - 8, (state->fclk + (TONE_FREQ * 32) - 1) / (TONE_FREQ * 32));
  547. s5h1420_writereg(state, PLL01, state->fclk/1000000 - 8);
  548. s5h1420_writereg(state, PLL02, 0x40);
  549. s5h1420_writereg(state, DiS01, (state->fclk + (TONE_FREQ * 32) - 1) / (TONE_FREQ * 32));
  550. /* TODO DC offset removal, config parameter ? */
  551. if (p->symbol_rate > 29000000)
  552. s5h1420_writereg(state, QPSK01, 0xae | 0x10);
  553. else
  554. s5h1420_writereg(state, QPSK01, 0xac | 0x10);
  555. /* set misc registers */
  556. s5h1420_writereg(state, CON_1, 0x00);
  557. s5h1420_writereg(state, QPSK02, 0x00);
  558. s5h1420_writereg(state, Pre01, 0xb0);
  559. s5h1420_writereg(state, Loop01, 0xF0);
  560. s5h1420_writereg(state, Loop02, 0x2a); /* e7 for s5h1420 */
  561. s5h1420_writereg(state, Loop03, 0x79); /* 78 for s5h1420 */
  562. if (p->symbol_rate > 20000000)
  563. s5h1420_writereg(state, Loop04, 0x79);
  564. else
  565. s5h1420_writereg(state, Loop04, 0x58);
  566. s5h1420_writereg(state, Loop05, 0x6b);
  567. if (p->symbol_rate >= 8000000)
  568. s5h1420_writereg(state, Post01, (0 << 6) | 0x10);
  569. else if (p->symbol_rate >= 4000000)
  570. s5h1420_writereg(state, Post01, (1 << 6) | 0x10);
  571. else
  572. s5h1420_writereg(state, Post01, (3 << 6) | 0x10);
  573. s5h1420_writereg(state, Monitor12, 0x00); /* unfreeze DC compensation */
  574. s5h1420_writereg(state, Sync01, 0x33);
  575. s5h1420_writereg(state, Mpeg01, state->config->cdclk_polarity);
  576. s5h1420_writereg(state, Mpeg02, 0x3d); /* Parallel output more, disabled -> enabled later */
  577. s5h1420_writereg(state, Err01, 0x03); /* 0x1d for s5h1420 */
  578. s5h1420_writereg(state, Vit06, 0x6e); /* 0x8e for s5h1420 */
  579. s5h1420_writereg(state, DiS03, 0x00);
  580. s5h1420_writereg(state, Rf01, 0x61); /* Tuner i2c address - for the gate controller */
  581. /* set tuner PLL */
  582. if (fe->ops.tuner_ops.set_params) {
  583. fe->ops.tuner_ops.set_params(fe);
  584. if (fe->ops.i2c_gate_ctrl)
  585. fe->ops.i2c_gate_ctrl(fe, 0);
  586. s5h1420_setfreqoffset(state, 0);
  587. }
  588. /* set the reset of the parameters */
  589. s5h1420_setsymbolrate(state, p);
  590. s5h1420_setfec_inversion(state, p);
  591. /* start QPSK */
  592. s5h1420_writereg(state, QPSK01, s5h1420_readreg(state, QPSK01) | 1);
  593. state->fec_inner = p->fec_inner;
  594. state->symbol_rate = p->symbol_rate;
  595. state->postlocked = 0;
  596. state->tunedfreq = p->frequency;
  597. dprintk("leave %s\n", __func__);
  598. return 0;
  599. }
  600. static int s5h1420_get_frontend(struct dvb_frontend* fe,
  601. struct dtv_frontend_properties *p)
  602. {
  603. struct s5h1420_state* state = fe->demodulator_priv;
  604. p->frequency = state->tunedfreq + s5h1420_getfreqoffset(state);
  605. p->inversion = s5h1420_getinversion(state);
  606. p->symbol_rate = s5h1420_getsymbolrate(state);
  607. p->fec_inner = s5h1420_getfec(state);
  608. return 0;
  609. }
  610. static int s5h1420_get_tune_settings(struct dvb_frontend* fe,
  611. struct dvb_frontend_tune_settings* fesettings)
  612. {
  613. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  614. if (p->symbol_rate > 20000000) {
  615. fesettings->min_delay_ms = 50;
  616. fesettings->step_size = 2000;
  617. fesettings->max_drift = 8000;
  618. } else if (p->symbol_rate > 12000000) {
  619. fesettings->min_delay_ms = 100;
  620. fesettings->step_size = 1500;
  621. fesettings->max_drift = 9000;
  622. } else if (p->symbol_rate > 8000000) {
  623. fesettings->min_delay_ms = 100;
  624. fesettings->step_size = 1000;
  625. fesettings->max_drift = 8000;
  626. } else if (p->symbol_rate > 4000000) {
  627. fesettings->min_delay_ms = 100;
  628. fesettings->step_size = 500;
  629. fesettings->max_drift = 7000;
  630. } else if (p->symbol_rate > 2000000) {
  631. fesettings->min_delay_ms = 200;
  632. fesettings->step_size = (p->symbol_rate / 8000);
  633. fesettings->max_drift = 14 * fesettings->step_size;
  634. } else {
  635. fesettings->min_delay_ms = 200;
  636. fesettings->step_size = (p->symbol_rate / 8000);
  637. fesettings->max_drift = 18 * fesettings->step_size;
  638. }
  639. return 0;
  640. }
  641. static int s5h1420_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  642. {
  643. struct s5h1420_state* state = fe->demodulator_priv;
  644. if (enable)
  645. return s5h1420_writereg(state, 0x02, state->CON_1_val | 1);
  646. else
  647. return s5h1420_writereg(state, 0x02, state->CON_1_val & 0xfe);
  648. }
  649. static int s5h1420_init (struct dvb_frontend* fe)
  650. {
  651. struct s5h1420_state* state = fe->demodulator_priv;
  652. /* disable power down and do reset */
  653. state->CON_1_val = state->config->serial_mpeg << 4;
  654. s5h1420_writereg(state, 0x02, state->CON_1_val);
  655. msleep(10);
  656. s5h1420_reset(state);
  657. return 0;
  658. }
  659. static int s5h1420_sleep(struct dvb_frontend* fe)
  660. {
  661. struct s5h1420_state* state = fe->demodulator_priv;
  662. state->CON_1_val = 0x12;
  663. return s5h1420_writereg(state, 0x02, state->CON_1_val);
  664. }
  665. static void s5h1420_release(struct dvb_frontend* fe)
  666. {
  667. struct s5h1420_state* state = fe->demodulator_priv;
  668. i2c_del_adapter(&state->tuner_i2c_adapter);
  669. kfree(state);
  670. }
  671. static u32 s5h1420_tuner_i2c_func(struct i2c_adapter *adapter)
  672. {
  673. return I2C_FUNC_I2C;
  674. }
  675. static int s5h1420_tuner_i2c_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  676. {
  677. struct s5h1420_state *state = i2c_get_adapdata(i2c_adap);
  678. struct i2c_msg m[3];
  679. u8 tx_open[2] = { CON_1, state->CON_1_val | 1 }; /* repeater stops once there was a stop condition */
  680. if (1 + num > ARRAY_SIZE(m)) {
  681. printk(KERN_WARNING
  682. "%s: i2c xfer: num=%d is too big!\n",
  683. KBUILD_MODNAME, num);
  684. return -EOPNOTSUPP;
  685. }
  686. memset(m, 0, sizeof(struct i2c_msg) * (1 + num));
  687. m[0].addr = state->config->demod_address;
  688. m[0].buf = tx_open;
  689. m[0].len = 2;
  690. memcpy(&m[1], msg, sizeof(struct i2c_msg) * num);
  691. return i2c_transfer(state->i2c, m, 1 + num) == 1 + num ? num : -EIO;
  692. }
  693. static const struct i2c_algorithm s5h1420_tuner_i2c_algo = {
  694. .master_xfer = s5h1420_tuner_i2c_tuner_xfer,
  695. .functionality = s5h1420_tuner_i2c_func,
  696. };
  697. struct i2c_adapter *s5h1420_get_tuner_i2c_adapter(struct dvb_frontend *fe)
  698. {
  699. struct s5h1420_state *state = fe->demodulator_priv;
  700. return &state->tuner_i2c_adapter;
  701. }
  702. EXPORT_SYMBOL(s5h1420_get_tuner_i2c_adapter);
  703. static const struct dvb_frontend_ops s5h1420_ops;
  704. struct dvb_frontend *s5h1420_attach(const struct s5h1420_config *config,
  705. struct i2c_adapter *i2c)
  706. {
  707. /* allocate memory for the internal state */
  708. struct s5h1420_state *state = kzalloc(sizeof(struct s5h1420_state), GFP_KERNEL);
  709. u8 i;
  710. if (state == NULL)
  711. goto error;
  712. /* setup the state */
  713. state->config = config;
  714. state->i2c = i2c;
  715. state->postlocked = 0;
  716. state->fclk = 88000000;
  717. state->tunedfreq = 0;
  718. state->fec_inner = FEC_NONE;
  719. state->symbol_rate = 0;
  720. /* check if the demod is there + identify it */
  721. i = s5h1420_readreg(state, ID01);
  722. if (i != 0x03)
  723. goto error;
  724. memset(state->shadow, 0xff, sizeof(state->shadow));
  725. for (i = 0; i < 0x50; i++)
  726. state->shadow[i] = s5h1420_readreg(state, i);
  727. /* create dvb_frontend */
  728. memcpy(&state->frontend.ops, &s5h1420_ops, sizeof(struct dvb_frontend_ops));
  729. state->frontend.demodulator_priv = state;
  730. /* create tuner i2c adapter */
  731. strscpy(state->tuner_i2c_adapter.name, "S5H1420-PN1010 tuner I2C bus",
  732. sizeof(state->tuner_i2c_adapter.name));
  733. state->tuner_i2c_adapter.algo = &s5h1420_tuner_i2c_algo;
  734. state->tuner_i2c_adapter.algo_data = NULL;
  735. i2c_set_adapdata(&state->tuner_i2c_adapter, state);
  736. if (i2c_add_adapter(&state->tuner_i2c_adapter) < 0) {
  737. printk(KERN_ERR "S5H1420/PN1010: tuner i2c bus could not be initialized\n");
  738. goto error;
  739. }
  740. return &state->frontend;
  741. error:
  742. kfree(state);
  743. return NULL;
  744. }
  745. EXPORT_SYMBOL(s5h1420_attach);
  746. static const struct dvb_frontend_ops s5h1420_ops = {
  747. .delsys = { SYS_DVBS },
  748. .info = {
  749. .name = "Samsung S5H1420/PnpNetwork PN1010 DVB-S",
  750. .frequency_min_hz = 950 * MHz,
  751. .frequency_max_hz = 2150 * MHz,
  752. .frequency_stepsize_hz = 125 * kHz,
  753. .frequency_tolerance_hz = 29500 * kHz,
  754. .symbol_rate_min = 1000000,
  755. .symbol_rate_max = 45000000,
  756. /* .symbol_rate_tolerance = ???,*/
  757. .caps = FE_CAN_INVERSION_AUTO |
  758. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  759. FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  760. FE_CAN_QPSK
  761. },
  762. .release = s5h1420_release,
  763. .init = s5h1420_init,
  764. .sleep = s5h1420_sleep,
  765. .i2c_gate_ctrl = s5h1420_i2c_gate_ctrl,
  766. .set_frontend = s5h1420_set_frontend,
  767. .get_frontend = s5h1420_get_frontend,
  768. .get_tune_settings = s5h1420_get_tune_settings,
  769. .read_status = s5h1420_read_status,
  770. .read_ber = s5h1420_read_ber,
  771. .read_signal_strength = s5h1420_read_signal_strength,
  772. .read_ucblocks = s5h1420_read_ucblocks,
  773. .diseqc_send_master_cmd = s5h1420_send_master_cmd,
  774. .diseqc_recv_slave_reply = s5h1420_recv_slave_reply,
  775. .diseqc_send_burst = s5h1420_send_burst,
  776. .set_tone = s5h1420_set_tone,
  777. .set_voltage = s5h1420_set_voltage,
  778. };
  779. MODULE_DESCRIPTION("Samsung S5H1420/PnpNetwork PN1010 DVB-S Demodulator driver");
  780. MODULE_AUTHOR("Andrew de Quincey, Patrick Boettcher");
  781. MODULE_LICENSE("GPL");