s5h1420.c 25 KB

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