cx24116.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. Conexant cx24116/cx24118 - DVBS/S2 Satellite demod/tuner driver
  3. Copyright (C) 2006-2008 Steven Toth <stoth@hauppauge.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. /*
  17. * Updates by Darron Broad 2007.
  18. *
  19. * March
  20. * Fixed some bugs.
  21. * Added diseqc support.
  22. * Added corrected signal strength support.
  23. *
  24. * August
  25. * Sync with legacy version.
  26. * Some clean ups.
  27. */
  28. /* Updates by Igor Liplianin
  29. *
  30. * September, 9th 2008
  31. * Fixed locking on high symbol rates (>30000).
  32. */
  33. #include <linux/slab.h>
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/init.h>
  38. #include <linux/firmware.h>
  39. #include "dvb_frontend.h"
  40. #include "cx24116.h"
  41. /*
  42. * Fetch firmware in the following manner.
  43. *
  44. * #!/bin/sh
  45. * wget ftp://167.206.143.11/outgoing/Oxford/88x_2_117_24275_1_INF.zip
  46. * unzip 88x_2_117_24275_1_INF.zip
  47. * dd if=Driver88/hcw88bda.sys of=dvb-fe-cx24116.fw skip=81768 bs=1 count=32522
  48. */
  49. #define CX24116_DEFAULT_FIRMWARE "dvb-fe-cx24116.fw"
  50. #define CX24116_SEARCH_RANGE_KHZ 5000
  51. /* registers (TO BE COMPLETED) */
  52. #define CX24116_REG_SIGNAL (0xd5)
  53. /* arg buffer size */
  54. #define CX24116_ARGLEN (0x1e)
  55. /* arg offset for DiSEqC */
  56. #define CX24116_DISEQC_BURST (1)
  57. #define CX24116_DISEQC_ARG2_2 (2) /* unknown value=2 */
  58. #define CX24116_DISEQC_ARG3_0 (3) /* unknown value=0 */
  59. #define CX24116_DISEQC_ARG4_0 (4) /* unknown value=0 */
  60. #define CX24116_DISEQC_MSGLEN (5)
  61. #define CX24116_DISEQC_MSGOFS (6)
  62. /* DiSEqC burst */
  63. #define CX24116_DISEQC_MINI_A (0)
  64. #define CX24116_DISEQC_MINI_B (1)
  65. static int debug = 0;
  66. #define dprintk(args...) \
  67. do { \
  68. if (debug) printk ("cx24116: " args); \
  69. } while (0)
  70. enum cmds
  71. {
  72. CMD_INIT_CMD10 = 0x10,
  73. CMD_TUNEREQUEST = 0x11,
  74. CMD_INIT_CMD13 = 0x13,
  75. CMD_INIT_CMD14 = 0x14,
  76. CMD_SEND_DISEQC = 0x21,
  77. CMD_SET_TONEPRE = 0x22,
  78. CMD_SET_TONE = 0x23,
  79. };
  80. /* The Demod/Tuner can't easily provide these, we cache them */
  81. struct cx24116_tuning
  82. {
  83. u32 frequency;
  84. u32 symbol_rate;
  85. fe_spectral_inversion_t inversion;
  86. fe_code_rate_t fec;
  87. fe_modulation_t modulation;
  88. /* Demod values */
  89. u8 fec_val;
  90. u8 fec_mask;
  91. u8 inversion_val;
  92. };
  93. /* Basic commands that are sent to the firmware */
  94. struct cx24116_cmd
  95. {
  96. u8 len;
  97. u8 args[CX24116_ARGLEN];
  98. };
  99. struct cx24116_state
  100. {
  101. struct i2c_adapter* i2c;
  102. const struct cx24116_config* config;
  103. struct dvb_frontend frontend;
  104. struct cx24116_tuning dcur;
  105. struct cx24116_tuning dnxt;
  106. u8 skip_fw_load;
  107. u8 burst;
  108. };
  109. static int cx24116_writereg(struct cx24116_state* state, int reg, int data)
  110. {
  111. u8 buf[] = { reg, data };
  112. struct i2c_msg msg = { .addr = state->config->demod_address,
  113. .flags = 0, .buf = buf, .len = 2 };
  114. int err;
  115. if (debug>1)
  116. printk("cx24116: %s: write reg 0x%02x, value 0x%02x\n",
  117. __func__,reg, data);
  118. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  119. printk("%s: writereg error(err == %i, reg == 0x%02x,"
  120. " value == 0x%02x)\n", __func__, err, reg, data);
  121. return -EREMOTEIO;
  122. }
  123. return 0;
  124. }
  125. /* Bulk byte writes to a single I2C address, for 32k firmware load */
  126. static int cx24116_writeregN(struct cx24116_state* state, int reg, u8 *data, u16 len)
  127. {
  128. int ret = -EREMOTEIO;
  129. struct i2c_msg msg;
  130. u8 *buf;
  131. buf = kmalloc(len + 1, GFP_KERNEL);
  132. if (buf == NULL) {
  133. printk("Unable to kmalloc\n");
  134. ret = -ENOMEM;
  135. goto error;
  136. }
  137. *(buf) = reg;
  138. memcpy(buf + 1, data, len);
  139. msg.addr = state->config->demod_address;
  140. msg.flags = 0;
  141. msg.buf = buf;
  142. msg.len = len + 1;
  143. if (debug>1)
  144. printk("cx24116: %s: write regN 0x%02x, len = %d\n",
  145. __func__,reg, len);
  146. if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  147. printk("%s: writereg error(err == %i, reg == 0x%02x\n",
  148. __func__, ret, reg);
  149. ret = -EREMOTEIO;
  150. }
  151. error:
  152. kfree(buf);
  153. return ret;
  154. }
  155. static int cx24116_readreg(struct cx24116_state* state, u8 reg)
  156. {
  157. int ret;
  158. u8 b0[] = { reg };
  159. u8 b1[] = { 0 };
  160. struct i2c_msg msg[] = {
  161. { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  162. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 }
  163. };
  164. ret = i2c_transfer(state->i2c, msg, 2);
  165. if (ret != 2) {
  166. printk("%s: reg=0x%x (error=%d)\n", __func__, reg, ret);
  167. return ret;
  168. }
  169. if (debug>1)
  170. printk("cx24116: read reg 0x%02x, value 0x%02x\n",reg, b1[0]);
  171. return b1[0];
  172. }
  173. static int cx24116_set_inversion(struct cx24116_state* state, fe_spectral_inversion_t inversion)
  174. {
  175. dprintk("%s(%d)\n", __func__, inversion);
  176. switch (inversion) {
  177. case INVERSION_OFF:
  178. state->dnxt.inversion_val = 0x00;
  179. break;
  180. case INVERSION_ON:
  181. state->dnxt.inversion_val = 0x04;
  182. break;
  183. case INVERSION_AUTO:
  184. state->dnxt.inversion_val = 0x0C;
  185. break;
  186. default:
  187. return -EINVAL;
  188. }
  189. state->dnxt.inversion = inversion;
  190. return 0;
  191. }
  192. /* A table of modulation, fec and configuration bytes for the demod.
  193. * Not all S2 mmodulation schemes are support and not all rates with
  194. * a scheme are support. Especially, no auto detect when in S2 mode.
  195. */
  196. struct cx24116_modfec {
  197. fe_modulation_t modulation;
  198. fe_code_rate_t fec;
  199. u8 mask; /* In DVBS mode this is used to autodetect */
  200. u8 val; /* Passed to the firmware to indicate mode selection */
  201. } CX24116_MODFEC_MODES[] = {
  202. /* QPSK. For unknown rates we set hardware to auto detect 0xfe 0x30 */
  203. { QPSK, FEC_NONE, 0xfe, 0x30 },
  204. { QPSK, FEC_1_2, 0x02, 0x2e },
  205. { QPSK, FEC_2_3, 0x04, 0x2f },
  206. { QPSK, FEC_3_4, 0x08, 0x30 },
  207. { QPSK, FEC_4_5, 0xfe, 0x30 },
  208. { QPSK, FEC_5_6, 0x20, 0x31 },
  209. { QPSK, FEC_6_7, 0xfe, 0x30 },
  210. { QPSK, FEC_7_8, 0x80, 0x32 },
  211. { QPSK, FEC_8_9, 0xfe, 0x30 },
  212. { QPSK, FEC_AUTO, 0xfe, 0x30 },
  213. /* NBC-QPSK */
  214. { NBC_QPSK, FEC_1_2, 0x00, 0x04 },
  215. { NBC_QPSK, FEC_3_5, 0x00, 0x05 },
  216. { NBC_QPSK, FEC_2_3, 0x00, 0x06 },
  217. { NBC_QPSK, FEC_3_4, 0x00, 0x07 },
  218. { NBC_QPSK, FEC_4_5, 0x00, 0x08 },
  219. { NBC_QPSK, FEC_5_6, 0x00, 0x09 },
  220. { NBC_QPSK, FEC_8_9, 0x00, 0x0a },
  221. { NBC_QPSK, FEC_9_10, 0x00, 0x0b },
  222. /* 8PSK */
  223. { _8PSK, FEC_3_5, 0x00, 0x0c },
  224. { _8PSK, FEC_2_3, 0x00, 0x0d },
  225. { _8PSK, FEC_3_4, 0x00, 0x0e },
  226. { _8PSK, FEC_5_6, 0x00, 0x0f },
  227. { _8PSK, FEC_9_10, 0x00, 0x11 },
  228. };
  229. static int cx24116_lookup_fecmod(struct cx24116_state* state,
  230. fe_modulation_t m, fe_code_rate_t f)
  231. {
  232. int i, ret = -EOPNOTSUPP;
  233. for(i=0 ; i < sizeof(CX24116_MODFEC_MODES) / sizeof(struct cx24116_modfec) ; i++)
  234. {
  235. if( (m == CX24116_MODFEC_MODES[i].modulation) &&
  236. (f == CX24116_MODFEC_MODES[i].fec) )
  237. {
  238. ret = i;
  239. break;
  240. }
  241. }
  242. return ret;
  243. }
  244. static int cx24116_set_fec(struct cx24116_state* state, fe_modulation_t mod, fe_code_rate_t fec)
  245. {
  246. int ret = 0;
  247. dprintk("%s()\n", __func__);
  248. ret = cx24116_lookup_fecmod(state, mod, fec);
  249. if(ret < 0)
  250. return ret;
  251. state->dnxt.fec_val = CX24116_MODFEC_MODES[ret].val;
  252. state->dnxt.fec_mask = CX24116_MODFEC_MODES[ret].mask;
  253. dprintk("%s() fec_val/mask = 0x%02x/0x%02x\n", __func__,
  254. state->dnxt.fec_val, state->dnxt.fec_mask);
  255. return 0;
  256. }
  257. static int cx24116_set_symbolrate(struct cx24116_state* state, u32 rate)
  258. {
  259. int ret = 0;
  260. dprintk("%s()\n", __func__);
  261. state->dnxt.symbol_rate = rate;
  262. dprintk("%s() symbol_rate = %d\n", __func__, state->dnxt.symbol_rate);
  263. /* check if symbol rate is within limits */
  264. if ((state->dnxt.symbol_rate > state->frontend.ops.info.symbol_rate_max) ||
  265. (state->dnxt.symbol_rate < state->frontend.ops.info.symbol_rate_min))
  266. ret = -EOPNOTSUPP;
  267. return ret;
  268. }
  269. static int cx24116_load_firmware (struct dvb_frontend* fe, const struct firmware *fw);
  270. static int cx24116_firmware_ondemand(struct dvb_frontend* fe)
  271. {
  272. struct cx24116_state *state = fe->demodulator_priv;
  273. const struct firmware *fw;
  274. int ret = 0;
  275. dprintk("%s()\n",__func__);
  276. if (cx24116_readreg(state, 0x20) > 0)
  277. {
  278. if (state->skip_fw_load)
  279. return 0;
  280. /* Load firmware */
  281. /* request the firmware, this will block until someone uploads it */
  282. printk("%s: Waiting for firmware upload (%s)...\n", __func__, CX24116_DEFAULT_FIRMWARE);
  283. ret = request_firmware(&fw, CX24116_DEFAULT_FIRMWARE, &state->i2c->dev);
  284. printk("%s: Waiting for firmware upload(2)...\n", __func__);
  285. if (ret) {
  286. printk("%s: No firmware uploaded (timeout or file not found?)\n", __func__);
  287. return ret;
  288. }
  289. /* Make sure we don't recurse back through here during loading */
  290. state->skip_fw_load = 1;
  291. ret = cx24116_load_firmware(fe, fw);
  292. if (ret)
  293. printk("%s: Writing firmware to device failed\n", __func__);
  294. release_firmware(fw);
  295. printk("%s: Firmware upload %s\n", __func__, ret == 0 ? "complete" : "failed");
  296. /* Ensure firmware is always loaded if required */
  297. state->skip_fw_load = 0;
  298. }
  299. return ret;
  300. }
  301. /* Take a basic firmware command structure, format it and forward it for processing */
  302. static int cx24116_cmd_execute(struct dvb_frontend* fe, struct cx24116_cmd *cmd)
  303. {
  304. struct cx24116_state *state = fe->demodulator_priv;
  305. int i, ret;
  306. dprintk("%s()\n", __func__);
  307. /* Load the firmware if required */
  308. if ( (ret = cx24116_firmware_ondemand(fe)) != 0)
  309. {
  310. printk("%s(): Unable initialise the firmware\n", __func__);
  311. return ret;
  312. }
  313. /* Write the command */
  314. for(i = 0; i < cmd->len ; i++)
  315. {
  316. dprintk("%s: 0x%02x == 0x%02x\n", __func__, i, cmd->args[i]);
  317. cx24116_writereg(state, i, cmd->args[i]);
  318. }
  319. /* Start execution and wait for cmd to terminate */
  320. cx24116_writereg(state, 0x1f, 0x01);
  321. while( cx24116_readreg(state, 0x1f) )
  322. {
  323. msleep(10);
  324. if(i++ > 64)
  325. {
  326. /* Avoid looping forever if the firmware does no respond */
  327. printk("%s() Firmware not responding\n", __func__);
  328. return -EREMOTEIO;
  329. }
  330. }
  331. return 0;
  332. }
  333. static int cx24116_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
  334. {
  335. struct cx24116_state* state = fe->demodulator_priv;
  336. struct cx24116_cmd cmd;
  337. int ret;
  338. dprintk("%s\n", __func__);
  339. dprintk("Firmware is %zu bytes (%02x %02x .. %02x %02x)\n"
  340. ,fw->size
  341. ,fw->data[0]
  342. ,fw->data[1]
  343. ,fw->data[ fw->size-2 ]
  344. ,fw->data[ fw->size-1 ]
  345. );
  346. /* Toggle 88x SRST pin to reset demod */
  347. if (state->config->reset_device)
  348. state->config->reset_device(fe);
  349. /* Begin the firmware load process */
  350. /* Prepare the demod, load the firmware, cleanup after load */
  351. cx24116_writereg(state, 0xF1, 0x08);
  352. cx24116_writereg(state, 0xF2, cx24116_readreg(state, 0xF2) | 0x03);
  353. cx24116_writereg(state, 0xF3, 0x46);
  354. cx24116_writereg(state, 0xF9, 0x00);
  355. cx24116_writereg(state, 0xF0, 0x03);
  356. cx24116_writereg(state, 0xF4, 0x81);
  357. cx24116_writereg(state, 0xF5, 0x00);
  358. cx24116_writereg(state, 0xF6, 0x00);
  359. /* write the entire firmware as one transaction */
  360. cx24116_writeregN(state, 0xF7, fw->data, fw->size);
  361. cx24116_writereg(state, 0xF4, 0x10);
  362. cx24116_writereg(state, 0xF0, 0x00);
  363. cx24116_writereg(state, 0xF8, 0x06);
  364. /* Firmware CMD 10: Chip config? */
  365. cmd.args[0x00] = CMD_INIT_CMD10;
  366. cmd.args[0x01] = 0x05;
  367. cmd.args[0x02] = 0xdc;
  368. cmd.args[0x03] = 0xda;
  369. cmd.args[0x04] = 0xae;
  370. cmd.args[0x05] = 0xaa;
  371. cmd.args[0x06] = 0x04;
  372. cmd.args[0x07] = 0x9d;
  373. cmd.args[0x08] = 0xfc;
  374. cmd.args[0x09] = 0x06;
  375. cmd.len= 0x0a;
  376. ret = cx24116_cmd_execute(fe, &cmd);
  377. if (ret != 0)
  378. return ret;
  379. cx24116_writereg(state, 0x9d, 0x00);
  380. /* Firmware CMD 14: Unknown */
  381. cmd.args[0x00] = CMD_INIT_CMD14;
  382. cmd.args[0x01] = 0x00;
  383. cmd.args[0x02] = 0x00;
  384. cmd.len= 0x03;
  385. ret = cx24116_cmd_execute(fe, &cmd);
  386. if (ret != 0)
  387. return ret;
  388. cx24116_writereg(state, 0xe5, 0x00);
  389. /* Firmware CMD 13: Unknown - Firmware config? */
  390. cmd.args[0x00] = CMD_INIT_CMD13;
  391. cmd.args[0x01] = 0x01;
  392. cmd.args[0x02] = 0x75;
  393. cmd.args[0x03] = 0x00;
  394. if (state->config->mpg_clk_pos_pol)
  395. cmd.args[0x04] = state->config->mpg_clk_pos_pol;
  396. else
  397. cmd.args[0x04] = 0x02;
  398. cmd.args[0x05] = 0x00;
  399. cmd.len= 0x06;
  400. ret = cx24116_cmd_execute(fe, &cmd);
  401. if (ret != 0)
  402. return ret;
  403. return 0;
  404. }
  405. static int cx24116_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  406. {
  407. /* The isl6421 module will override this function in the fops. */
  408. dprintk("%s() This should never appear if the isl6421 module is loaded correctly\n",__func__);
  409. return -EOPNOTSUPP;
  410. }
  411. static int cx24116_read_status(struct dvb_frontend* fe, fe_status_t* status)
  412. {
  413. struct cx24116_state *state = fe->demodulator_priv;
  414. int lock = cx24116_readreg(state, 0x9d);
  415. dprintk("%s: status = 0x%02x\n", __func__, lock);
  416. *status = 0;
  417. if (lock & 0x01)
  418. *status |= FE_HAS_SIGNAL;
  419. if (lock & 0x02)
  420. *status |= FE_HAS_CARRIER;
  421. if (lock & 0x04)
  422. *status |= FE_HAS_VITERBI;
  423. if (lock & 0x08)
  424. *status |= FE_HAS_SYNC | FE_HAS_LOCK;
  425. return 0;
  426. }
  427. /* TODO: Not clear how we do this */
  428. static int cx24116_read_ber(struct dvb_frontend* fe, u32* ber)
  429. {
  430. //struct cx24116_state *state = fe->demodulator_priv;
  431. dprintk("%s()\n", __func__);
  432. *ber = 0;
  433. return 0;
  434. }
  435. /* Signal strength (0..100)% = (sig & 0xf0) * 10 + (sig & 0x0f) * 10 / 16 */
  436. static int cx24116_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
  437. {
  438. struct cx24116_state *state = fe->demodulator_priv;
  439. u8 strength_reg;
  440. static const u32 strength_tab[] = { /* 10 x Table (rounded up) */
  441. 0x00000,0x0199A,0x03333,0x04ccD,0x06667,0x08000,0x0999A,0x0b333,0x0cccD,0x0e667,
  442. 0x10000,0x1199A,0x13333,0x14ccD,0x16667,0x18000 };
  443. dprintk("%s()\n", __func__);
  444. strength_reg = cx24116_readreg(state, CX24116_REG_SIGNAL);
  445. if(strength_reg < 0xa0)
  446. *signal_strength = strength_tab [ ( strength_reg & 0xf0 ) >> 4 ] +
  447. ( strength_tab [ ( strength_reg & 0x0f ) ] >> 4 );
  448. else
  449. *signal_strength = 0xffff;
  450. dprintk("%s: Signal strength (raw / cooked) = (0x%02x / 0x%04x)\n",
  451. __func__,strength_reg,*signal_strength);
  452. return 0;
  453. }
  454. /* TODO: Not clear how we do this */
  455. static int cx24116_read_snr(struct dvb_frontend* fe, u16* snr)
  456. {
  457. //struct cx24116_state *state = fe->demodulator_priv;
  458. dprintk("%s()\n", __func__);
  459. *snr = 0;
  460. return 0;
  461. }
  462. /* TODO: Not clear how we do this */
  463. static int cx24116_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  464. {
  465. //struct cx24116_state *state = fe->demodulator_priv;
  466. dprintk("%s()\n", __func__);
  467. *ucblocks = 0;
  468. return 0;
  469. }
  470. /* Overwrite the current tuning params, we are about to tune */
  471. static void cx24116_clone_params(struct dvb_frontend* fe)
  472. {
  473. struct cx24116_state *state = fe->demodulator_priv;
  474. memcpy(&state->dcur, &state->dnxt, sizeof(state->dcur));
  475. }
  476. static int cx24116_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  477. {
  478. struct cx24116_cmd cmd;
  479. int ret;
  480. dprintk("%s(%d)\n", __func__, tone);
  481. if ( (tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF) ) {
  482. printk("%s: Invalid, tone=%d\n", __func__, tone);
  483. return -EINVAL;
  484. }
  485. /* This is always done before the tone is set */
  486. cmd.args[0x00] = CMD_SET_TONEPRE;
  487. cmd.args[0x01] = 0x00;
  488. cmd.len= 0x02;
  489. ret = cx24116_cmd_execute(fe, &cmd);
  490. if (ret != 0)
  491. return ret;
  492. /* Now we set the tone */
  493. cmd.args[0x00] = CMD_SET_TONE;
  494. cmd.args[0x01] = 0x00;
  495. cmd.args[0x02] = 0x00;
  496. switch (tone) {
  497. case SEC_TONE_ON:
  498. dprintk("%s: setting tone on\n", __func__);
  499. cmd.args[0x03] = 0x01;
  500. break;
  501. case SEC_TONE_OFF:
  502. dprintk("%s: setting tone off\n",__func__);
  503. cmd.args[0x03] = 0x00;
  504. break;
  505. }
  506. cmd.len= 0x04;
  507. return cx24116_cmd_execute(fe, &cmd);
  508. }
  509. /* Initialise DiSEqC */
  510. static int cx24116_diseqc_init(struct dvb_frontend* fe)
  511. {
  512. struct cx24116_state *state = fe->demodulator_priv;
  513. /* Default DiSEqC burst state */
  514. state->burst = CX24116_DISEQC_MINI_A;
  515. return 0;
  516. }
  517. /* Send DiSEqC message with derived burst (hack) || previous burst */
  518. static int cx24116_send_diseqc_msg(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd *d)
  519. {
  520. struct cx24116_state *state = fe->demodulator_priv;
  521. struct cx24116_cmd cmd;
  522. int i, ret;
  523. /* Dump DiSEqC message */
  524. if (debug) {
  525. printk("cx24116: %s(", __func__);
  526. for(i = 0 ; i < d->msg_len ;) {
  527. printk("0x%02x", d->msg[i]);
  528. if(++i < d->msg_len)
  529. printk(", ");
  530. }
  531. printk(")\n");
  532. }
  533. if(d->msg_len > (CX24116_ARGLEN - CX24116_DISEQC_MSGOFS))
  534. return -EINVAL;
  535. cmd.args[0x00] = CMD_SEND_DISEQC;
  536. cmd.args[CX24116_DISEQC_ARG2_2] = 0x02;
  537. cmd.args[CX24116_DISEQC_ARG3_0] = 0x00;
  538. cmd.args[CX24116_DISEQC_ARG4_0] = 0x00;
  539. /* DiSEqC message */
  540. for (i = 0; i < d->msg_len; i++)
  541. cmd.args[CX24116_DISEQC_MSGOFS + i] = d->msg[i];
  542. /* Hack: Derive burst from command else use previous burst */
  543. if(d->msg_len >= 4 && d->msg[2] == 0x38)
  544. cmd.args[CX24116_DISEQC_BURST] = (d->msg[3] >> 2) & 1;
  545. else
  546. cmd.args[CX24116_DISEQC_BURST] = state->burst;
  547. cmd.args[CX24116_DISEQC_MSGLEN] = d->msg_len;
  548. cmd.len = CX24116_DISEQC_MSGOFS + d->msg_len;
  549. ret = cx24116_cmd_execute(fe, &cmd);
  550. /* Firmware command duration is unknown, so guess...
  551. *
  552. * Eutelsat spec:
  553. * >15ms delay +
  554. * 13.5ms per byte +
  555. * >15ms delay +
  556. * 12.5ms burst +
  557. * >15ms delay
  558. */
  559. if(ret == 0)
  560. msleep( (cmd.args[CX24116_DISEQC_MSGLEN] << 4) + 60 );
  561. return ret;
  562. }
  563. /* Send DiSEqC burst */
  564. static int cx24116_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  565. {
  566. struct cx24116_state *state = fe->demodulator_priv;
  567. struct cx24116_cmd cmd;
  568. int ret;
  569. dprintk("%s(%d)\n",__func__,(int)burst);
  570. cmd.args[0x00] = CMD_SEND_DISEQC;
  571. cmd.args[CX24116_DISEQC_ARG2_2] = 0x02;
  572. cmd.args[CX24116_DISEQC_ARG3_0] = 0x00;
  573. cmd.args[CX24116_DISEQC_ARG4_0] = 0x00;
  574. if (burst == SEC_MINI_A)
  575. cmd.args[CX24116_DISEQC_BURST] = CX24116_DISEQC_MINI_A;
  576. else if(burst == SEC_MINI_B)
  577. cmd.args[CX24116_DISEQC_BURST] = CX24116_DISEQC_MINI_B;
  578. else
  579. return -EINVAL;
  580. /* Cache as previous burst state */
  581. state->burst= cmd.args[CX24116_DISEQC_BURST];
  582. cmd.args[CX24116_DISEQC_MSGLEN] = 0x00;
  583. cmd.len= CX24116_DISEQC_MSGOFS;
  584. ret= cx24116_cmd_execute(fe, &cmd);
  585. /* Firmware command duration is unknown, so guess... */
  586. if(ret == 0)
  587. msleep(60);
  588. return ret;
  589. }
  590. static void cx24116_release(struct dvb_frontend* fe)
  591. {
  592. struct cx24116_state* state = fe->demodulator_priv;
  593. dprintk("%s\n",__func__);
  594. kfree(state);
  595. }
  596. static struct dvb_frontend_ops cx24116_ops;
  597. struct dvb_frontend* cx24116_attach(const struct cx24116_config* config,
  598. struct i2c_adapter* i2c)
  599. {
  600. struct cx24116_state* state = NULL;
  601. int ret;
  602. dprintk("%s\n",__func__);
  603. /* allocate memory for the internal state */
  604. state = kmalloc(sizeof(struct cx24116_state), GFP_KERNEL);
  605. if (state == NULL) {
  606. printk("Unable to kmalloc\n");
  607. goto error;
  608. }
  609. /* setup the state */
  610. memset(state, 0, sizeof(struct cx24116_state));
  611. state->config = config;
  612. state->i2c = i2c;
  613. /* check if the demod is present */
  614. ret = (cx24116_readreg(state, 0xFF) << 8) | cx24116_readreg(state, 0xFE);
  615. if (ret != 0x0501) {
  616. printk("Invalid probe, probably not a CX24116 device\n");
  617. goto error;
  618. }
  619. /* create dvb_frontend */
  620. memcpy(&state->frontend.ops, &cx24116_ops, sizeof(struct dvb_frontend_ops));
  621. state->frontend.demodulator_priv = state;
  622. return &state->frontend;
  623. error:
  624. kfree(state);
  625. return NULL;
  626. }
  627. static int cx24116_get_params(struct dvb_frontend* fe)
  628. {
  629. struct cx24116_state *state = fe->demodulator_priv;
  630. struct dtv_frontend_properties *cache = &fe->dtv_property_cache;
  631. dprintk("%s()\n",__func__);
  632. cache->frequency = state->dcur.frequency;
  633. cache->inversion = state->dcur.inversion;
  634. cache->modulation = state->dcur.modulation;
  635. cache->fec_inner = state->dcur.fec;
  636. cache->symbol_rate = state->dcur.symbol_rate;
  637. return 0;
  638. }
  639. static int cx24116_initfe(struct dvb_frontend* fe)
  640. {
  641. dprintk("%s()\n",__func__);
  642. return cx24116_diseqc_init(fe);
  643. }
  644. static int cx24116_set_property(struct dvb_frontend *fe, struct dtv_property* tvp)
  645. {
  646. dprintk("%s(..)\n", __func__);
  647. return 0;
  648. }
  649. static int cx24116_set_params(struct dvb_frontend *fe)
  650. {
  651. dprintk("%s(..) We were notified that a tune request may occur\n", __func__);
  652. return 0;
  653. }
  654. /* dvb-core told us to tune, the tv property cache will be complete,
  655. * it's safe for is to pull values and use them for tuning purposes.
  656. */
  657. static int cx24116_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  658. {
  659. struct cx24116_state *state = fe->demodulator_priv;
  660. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  661. struct cx24116_cmd cmd;
  662. fe_status_t tunerstat;
  663. int ret, above30msps;
  664. u8 retune=4;
  665. dprintk("%s()\n",__func__);
  666. state->dnxt.modulation = c->modulation;
  667. state->dnxt.frequency = c->frequency;
  668. if ((ret = cx24116_set_inversion(state, c->inversion)) != 0)
  669. return ret;
  670. if ((ret = cx24116_set_fec(state, c->modulation, c->fec_inner)) != 0)
  671. return ret;
  672. if ((ret = cx24116_set_symbolrate(state, c->symbol_rate)) != 0)
  673. return ret;
  674. /* discard the 'current' tuning parameters and prepare to tune */
  675. cx24116_clone_params(fe);
  676. dprintk("%s: frequency = %d\n", __func__, state->dcur.frequency);
  677. dprintk("%s: symbol_rate = %d\n", __func__, state->dcur.symbol_rate);
  678. dprintk("%s: FEC = %d (mask/val = 0x%02x/0x%02x)\n", __func__,
  679. state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
  680. dprintk("%s: Inversion = %d (val = 0x%02x)\n", __func__,
  681. state->dcur.inversion, state->dcur.inversion_val);
  682. if (state->config->set_ts_params)
  683. state->config->set_ts_params(fe, 0);
  684. above30msps = (state->dcur.symbol_rate > 30000000);
  685. if (above30msps){
  686. cx24116_writereg(state, 0xF9, 0x01);
  687. cx24116_writereg(state, 0xF3, 0x44);
  688. } else {
  689. cx24116_writereg(state, 0xF9, 0x00);
  690. cx24116_writereg(state, 0xF3, 0x46);
  691. }
  692. /* Prepare a tune request */
  693. cmd.args[0x00] = CMD_TUNEREQUEST;
  694. /* Frequency */
  695. cmd.args[0x01] = (state->dcur.frequency & 0xff0000) >> 16;
  696. cmd.args[0x02] = (state->dcur.frequency & 0x00ff00) >> 8;
  697. cmd.args[0x03] = (state->dcur.frequency & 0x0000ff);
  698. /* Symbol Rate */
  699. cmd.args[0x04] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
  700. cmd.args[0x05] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
  701. /* Automatic Inversion */
  702. cmd.args[0x06] = state->dcur.inversion_val;
  703. /* Modulation / FEC & Pilot Off */
  704. cmd.args[0x07] = state->dcur.fec_val;
  705. if (c->pilot == PILOT_ON)
  706. cmd.args[0x07] |= 0x40;
  707. cmd.args[0x08] = CX24116_SEARCH_RANGE_KHZ >> 8;
  708. cmd.args[0x09] = CX24116_SEARCH_RANGE_KHZ & 0xff;
  709. cmd.args[0x0a] = 0x00;
  710. cmd.args[0x0b] = 0x00;
  711. cmd.args[0x0c] = 0x02;
  712. cmd.args[0x0d] = state->dcur.fec_mask;
  713. if (above30msps){
  714. cmd.args[0x0e] = 0x04;
  715. cmd.args[0x0f] = 0x00;
  716. cmd.args[0x10] = 0x01;
  717. cmd.args[0x11] = 0x77;
  718. cmd.args[0x12] = 0x36;
  719. } else {
  720. cmd.args[0x0e] = 0x06;
  721. cmd.args[0x0f] = 0x00;
  722. cmd.args[0x10] = 0x00;
  723. cmd.args[0x11] = 0xFA;
  724. cmd.args[0x12] = 0x24;
  725. }
  726. cmd.len= 0x13;
  727. /* We need to support pilot and non-pilot tuning in the
  728. * driver automatically. This is a workaround for because
  729. * the demod does not support autodetect.
  730. */
  731. do {
  732. /* Reset status register? */
  733. cx24116_writereg(state, 0x9d, 0xc1);
  734. /* Tune */
  735. ret = cx24116_cmd_execute(fe, &cmd);
  736. if( ret != 0 )
  737. break;
  738. /* The hardware can take time to lock, wait a while */
  739. msleep(500);
  740. cx24116_read_status(fe, &tunerstat);
  741. if(tunerstat & FE_HAS_SIGNAL) {
  742. if(tunerstat & FE_HAS_SYNC)
  743. /* Tuned */
  744. break;
  745. else if(c->pilot == PILOT_AUTO)
  746. /* Toggle pilot bit */
  747. cmd.args[0x07] ^= 0x40;
  748. }
  749. }
  750. while(--retune);
  751. return ret;
  752. }
  753. static struct dvb_frontend_ops cx24116_ops = {
  754. .info = {
  755. .name = "Conexant CX24116/CX24118",
  756. .type = FE_QPSK,
  757. .frequency_min = 950000,
  758. .frequency_max = 2150000,
  759. .frequency_stepsize = 1011, /* kHz for QPSK frontends */
  760. .frequency_tolerance = 5000,
  761. .symbol_rate_min = 1000000,
  762. .symbol_rate_max = 45000000,
  763. .caps = FE_CAN_INVERSION_AUTO |
  764. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  765. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  766. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  767. FE_CAN_QPSK | FE_CAN_RECOVER
  768. },
  769. .release = cx24116_release,
  770. .init = cx24116_initfe,
  771. .read_status = cx24116_read_status,
  772. .read_ber = cx24116_read_ber,
  773. .read_signal_strength = cx24116_read_signal_strength,
  774. .read_snr = cx24116_read_snr,
  775. .read_ucblocks = cx24116_read_ucblocks,
  776. .set_tone = cx24116_set_tone,
  777. .set_voltage = cx24116_set_voltage,
  778. .diseqc_send_master_cmd = cx24116_send_diseqc_msg,
  779. .diseqc_send_burst = cx24116_diseqc_send_burst,
  780. .set_property = cx24116_set_property,
  781. .set_params = cx24116_set_params,
  782. .set_frontend = cx24116_set_frontend,
  783. };
  784. module_param(debug, int, 0644);
  785. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  786. MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24116/cx24118 hardware");
  787. MODULE_AUTHOR("Steven Toth");
  788. MODULE_LICENSE("GPL");
  789. EXPORT_SYMBOL(cx24116_attach);