s5h1420.c 25 KB

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