cx24120.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Conexant cx24120/cx24118 - DVBS/S2 Satellite demod/tuner driver
  4. Copyright (C) 2008 Patrick Boettcher <[email protected]>
  5. Copyright (C) 2009 Sergey Tyurin <forum.free-x.de>
  6. Updated 2012 by Jannis Achstetter <[email protected]>
  7. Copyright (C) 2015 Jemma Denson <[email protected]>
  8. April 2015
  9. Refactored & simplified driver
  10. Updated to work with delivery system supplied by DVBv5
  11. Add frequency, fec & pilot to get_frontend
  12. Cards supported: Technisat Skystar S2
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/init.h>
  19. #include <linux/firmware.h>
  20. #include <media/dvb_frontend.h>
  21. #include "cx24120.h"
  22. #define CX24120_SEARCH_RANGE_KHZ 5000
  23. #define CX24120_FIRMWARE "dvb-fe-cx24120-1.20.58.2.fw"
  24. /* cx24120 i2c registers */
  25. #define CX24120_REG_CMD_START 0x00 /* write cmd_id */
  26. #define CX24120_REG_CMD_ARGS 0x01 /* write command arguments */
  27. #define CX24120_REG_CMD_END 0x1f /* write 0x01 for end */
  28. #define CX24120_REG_MAILBOX 0x33
  29. #define CX24120_REG_FREQ3 0x34 /* frequency */
  30. #define CX24120_REG_FREQ2 0x35
  31. #define CX24120_REG_FREQ1 0x36
  32. #define CX24120_REG_FECMODE 0x39 /* FEC status */
  33. #define CX24120_REG_STATUS 0x3a /* Tuner status */
  34. #define CX24120_REG_SIGSTR_H 0x3a /* Signal strength high */
  35. #define CX24120_REG_SIGSTR_L 0x3b /* Signal strength low byte */
  36. #define CX24120_REG_QUALITY_H 0x40 /* SNR high byte */
  37. #define CX24120_REG_QUALITY_L 0x41 /* SNR low byte */
  38. #define CX24120_REG_BER_HH 0x47 /* BER high byte of high word */
  39. #define CX24120_REG_BER_HL 0x48 /* BER low byte of high word */
  40. #define CX24120_REG_BER_LH 0x49 /* BER high byte of low word */
  41. #define CX24120_REG_BER_LL 0x4a /* BER low byte of low word */
  42. #define CX24120_REG_UCB_H 0x50 /* UCB high byte */
  43. #define CX24120_REG_UCB_L 0x51 /* UCB low byte */
  44. #define CX24120_REG_CLKDIV 0xe6
  45. #define CX24120_REG_RATEDIV 0xf0
  46. #define CX24120_REG_REVISION 0xff /* Chip revision (ro) */
  47. /* Command messages */
  48. enum command_message_id {
  49. CMD_VCO_SET = 0x10, /* cmd.len = 12; */
  50. CMD_TUNEREQUEST = 0x11, /* cmd.len = 15; */
  51. CMD_MPEG_ONOFF = 0x13, /* cmd.len = 4; */
  52. CMD_MPEG_INIT = 0x14, /* cmd.len = 7; */
  53. CMD_BANDWIDTH = 0x15, /* cmd.len = 12; */
  54. CMD_CLOCK_READ = 0x16, /* read clock */
  55. CMD_CLOCK_SET = 0x17, /* cmd.len = 10; */
  56. CMD_DISEQC_MSG1 = 0x20, /* cmd.len = 11; */
  57. CMD_DISEQC_MSG2 = 0x21, /* cmd.len = d->msg_len + 6; */
  58. CMD_SETVOLTAGE = 0x22, /* cmd.len = 2; */
  59. CMD_SETTONE = 0x23, /* cmd.len = 4; */
  60. CMD_DISEQC_BURST = 0x24, /* cmd.len not used !!! */
  61. CMD_READ_SNR = 0x1a, /* Read signal strength */
  62. CMD_START_TUNER = 0x1b, /* ??? */
  63. CMD_FWVERSION = 0x35,
  64. CMD_BER_CTRL = 0x3c, /* cmd.len = 0x03; */
  65. };
  66. #define CX24120_MAX_CMD_LEN 30
  67. /* pilot mask */
  68. #define CX24120_PILOT_OFF 0x00
  69. #define CX24120_PILOT_ON 0x40
  70. #define CX24120_PILOT_AUTO 0x80
  71. /* signal status */
  72. #define CX24120_HAS_SIGNAL 0x01
  73. #define CX24120_HAS_CARRIER 0x02
  74. #define CX24120_HAS_VITERBI 0x04
  75. #define CX24120_HAS_LOCK 0x08
  76. #define CX24120_HAS_UNK1 0x10
  77. #define CX24120_HAS_UNK2 0x20
  78. #define CX24120_STATUS_MASK 0x0f
  79. #define CX24120_SIGNAL_MASK 0xc0
  80. /* ber window */
  81. #define CX24120_BER_WINDOW 16
  82. #define CX24120_BER_WSIZE ((1 << CX24120_BER_WINDOW) * 208 * 8)
  83. #define info(args...) pr_info("cx24120: " args)
  84. #define err(args...) pr_err("cx24120: ### ERROR: " args)
  85. /* The Demod/Tuner can't easily provide these, we cache them */
  86. struct cx24120_tuning {
  87. u32 frequency;
  88. u32 symbol_rate;
  89. enum fe_spectral_inversion inversion;
  90. enum fe_code_rate fec;
  91. enum fe_delivery_system delsys;
  92. enum fe_modulation modulation;
  93. enum fe_pilot pilot;
  94. /* Demod values */
  95. u8 fec_val;
  96. u8 fec_mask;
  97. u8 clkdiv;
  98. u8 ratediv;
  99. u8 inversion_val;
  100. u8 pilot_val;
  101. };
  102. /* Private state */
  103. struct cx24120_state {
  104. struct i2c_adapter *i2c;
  105. const struct cx24120_config *config;
  106. struct dvb_frontend frontend;
  107. u8 cold_init;
  108. u8 mpeg_enabled;
  109. u8 need_clock_set;
  110. /* current and next tuning parameters */
  111. struct cx24120_tuning dcur;
  112. struct cx24120_tuning dnxt;
  113. enum fe_status fe_status;
  114. /* dvbv5 stats calculations */
  115. u32 bitrate;
  116. u32 berw_usecs;
  117. u32 ber_prev;
  118. u32 ucb_offset;
  119. unsigned long ber_jiffies_stats;
  120. unsigned long per_jiffies_stats;
  121. };
  122. /* Command message to firmware */
  123. struct cx24120_cmd {
  124. u8 id;
  125. u8 len;
  126. u8 arg[CX24120_MAX_CMD_LEN];
  127. };
  128. /* Read single register */
  129. static int cx24120_readreg(struct cx24120_state *state, u8 reg)
  130. {
  131. int ret;
  132. u8 buf = 0;
  133. struct i2c_msg msg[] = {
  134. {
  135. .addr = state->config->i2c_addr,
  136. .flags = 0,
  137. .len = 1,
  138. .buf = &reg
  139. }, {
  140. .addr = state->config->i2c_addr,
  141. .flags = I2C_M_RD,
  142. .len = 1,
  143. .buf = &buf
  144. }
  145. };
  146. ret = i2c_transfer(state->i2c, msg, 2);
  147. if (ret != 2) {
  148. err("Read error: reg=0x%02x, ret=%i)\n", reg, ret);
  149. return ret;
  150. }
  151. dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, buf);
  152. return buf;
  153. }
  154. /* Write single register */
  155. static int cx24120_writereg(struct cx24120_state *state, u8 reg, u8 data)
  156. {
  157. u8 buf[] = { reg, data };
  158. struct i2c_msg msg = {
  159. .addr = state->config->i2c_addr,
  160. .flags = 0,
  161. .buf = buf,
  162. .len = 2
  163. };
  164. int ret;
  165. ret = i2c_transfer(state->i2c, &msg, 1);
  166. if (ret != 1) {
  167. err("Write error: i2c_write error(err == %i, 0x%02x: 0x%02x)\n",
  168. ret, reg, data);
  169. return ret;
  170. }
  171. dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, data);
  172. return 0;
  173. }
  174. /* Write multiple registers in chunks of i2c_wr_max-sized buffers */
  175. static int cx24120_writeregs(struct cx24120_state *state,
  176. u8 reg, const u8 *values, u16 len, u8 incr)
  177. {
  178. int ret;
  179. u16 max = state->config->i2c_wr_max > 0 ?
  180. state->config->i2c_wr_max :
  181. len;
  182. struct i2c_msg msg = {
  183. .addr = state->config->i2c_addr,
  184. .flags = 0,
  185. };
  186. msg.buf = kmalloc(max + 1, GFP_KERNEL);
  187. if (!msg.buf)
  188. return -ENOMEM;
  189. while (len) {
  190. msg.buf[0] = reg;
  191. msg.len = len > max ? max : len;
  192. memcpy(&msg.buf[1], values, msg.len);
  193. len -= msg.len; /* data length revers counter */
  194. values += msg.len; /* incr data pointer */
  195. if (incr)
  196. reg += msg.len;
  197. msg.len++; /* don't forget the addr byte */
  198. ret = i2c_transfer(state->i2c, &msg, 1);
  199. if (ret != 1) {
  200. err("i2c_write error(err == %i, 0x%02x)\n", ret, reg);
  201. goto out;
  202. }
  203. dev_dbg(&state->i2c->dev, "reg=0x%02x; data=%*ph\n",
  204. reg, msg.len - 1, msg.buf + 1);
  205. }
  206. ret = 0;
  207. out:
  208. kfree(msg.buf);
  209. return ret;
  210. }
  211. static const struct dvb_frontend_ops cx24120_ops;
  212. struct dvb_frontend *cx24120_attach(const struct cx24120_config *config,
  213. struct i2c_adapter *i2c)
  214. {
  215. struct cx24120_state *state;
  216. int demod_rev;
  217. info("Conexant cx24120/cx24118 - DVBS/S2 Satellite demod/tuner\n");
  218. state = kzalloc(sizeof(*state), GFP_KERNEL);
  219. if (!state) {
  220. err("Unable to allocate memory for cx24120_state\n");
  221. goto error;
  222. }
  223. /* setup the state */
  224. state->config = config;
  225. state->i2c = i2c;
  226. /* check if the demod is present and has proper type */
  227. demod_rev = cx24120_readreg(state, CX24120_REG_REVISION);
  228. switch (demod_rev) {
  229. case 0x07:
  230. info("Demod cx24120 rev. 0x07 detected.\n");
  231. break;
  232. case 0x05:
  233. info("Demod cx24120 rev. 0x05 detected.\n");
  234. break;
  235. default:
  236. err("Unsupported demod revision: 0x%x detected.\n", demod_rev);
  237. goto error;
  238. }
  239. /* create dvb_frontend */
  240. state->cold_init = 0;
  241. memcpy(&state->frontend.ops, &cx24120_ops,
  242. sizeof(struct dvb_frontend_ops));
  243. state->frontend.demodulator_priv = state;
  244. info("Conexant cx24120/cx24118 attached.\n");
  245. return &state->frontend;
  246. error:
  247. kfree(state);
  248. return NULL;
  249. }
  250. EXPORT_SYMBOL_GPL(cx24120_attach);
  251. static int cx24120_test_rom(struct cx24120_state *state)
  252. {
  253. int err, ret;
  254. err = cx24120_readreg(state, 0xfd);
  255. if (err & 4) {
  256. ret = cx24120_readreg(state, 0xdf) & 0xfe;
  257. err = cx24120_writereg(state, 0xdf, ret);
  258. }
  259. return err;
  260. }
  261. static int cx24120_read_snr(struct dvb_frontend *fe, u16 *snr)
  262. {
  263. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  264. if (c->cnr.stat[0].scale != FE_SCALE_DECIBEL)
  265. *snr = 0;
  266. else
  267. *snr = div_s64(c->cnr.stat[0].svalue, 100);
  268. return 0;
  269. }
  270. static int cx24120_read_ber(struct dvb_frontend *fe, u32 *ber)
  271. {
  272. struct cx24120_state *state = fe->demodulator_priv;
  273. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  274. if (c->post_bit_error.stat[0].scale != FE_SCALE_COUNTER) {
  275. *ber = 0;
  276. return 0;
  277. }
  278. *ber = c->post_bit_error.stat[0].uvalue - state->ber_prev;
  279. state->ber_prev = c->post_bit_error.stat[0].uvalue;
  280. return 0;
  281. }
  282. static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state,
  283. u8 flag);
  284. /* Check if we're running a command that needs to disable mpeg out */
  285. static void cx24120_check_cmd(struct cx24120_state *state, u8 id)
  286. {
  287. switch (id) {
  288. case CMD_TUNEREQUEST:
  289. case CMD_CLOCK_READ:
  290. case CMD_DISEQC_MSG1:
  291. case CMD_DISEQC_MSG2:
  292. case CMD_SETVOLTAGE:
  293. case CMD_SETTONE:
  294. case CMD_DISEQC_BURST:
  295. cx24120_msg_mpeg_output_global_config(state, 0);
  296. /* Old driver would do a msleep(100) here */
  297. return;
  298. default:
  299. return;
  300. }
  301. }
  302. /* Send a message to the firmware */
  303. static int cx24120_message_send(struct cx24120_state *state,
  304. struct cx24120_cmd *cmd)
  305. {
  306. int ficus;
  307. if (state->mpeg_enabled) {
  308. /* Disable mpeg out on certain commands */
  309. cx24120_check_cmd(state, cmd->id);
  310. }
  311. cx24120_writereg(state, CX24120_REG_CMD_START, cmd->id);
  312. cx24120_writeregs(state, CX24120_REG_CMD_ARGS, &cmd->arg[0],
  313. cmd->len, 1);
  314. cx24120_writereg(state, CX24120_REG_CMD_END, 0x01);
  315. ficus = 1000;
  316. while (cx24120_readreg(state, CX24120_REG_CMD_END)) {
  317. msleep(20);
  318. ficus -= 20;
  319. if (ficus == 0) {
  320. err("Error sending message to firmware\n");
  321. return -EREMOTEIO;
  322. }
  323. }
  324. dev_dbg(&state->i2c->dev, "sent message 0x%02x\n", cmd->id);
  325. return 0;
  326. }
  327. /* Send a message and fill arg[] with the results */
  328. static int cx24120_message_sendrcv(struct cx24120_state *state,
  329. struct cx24120_cmd *cmd, u8 numreg)
  330. {
  331. int ret, i;
  332. if (numreg > CX24120_MAX_CMD_LEN) {
  333. err("Too many registers to read. cmd->reg = %d", numreg);
  334. return -EREMOTEIO;
  335. }
  336. ret = cx24120_message_send(state, cmd);
  337. if (ret != 0)
  338. return ret;
  339. if (!numreg)
  340. return 0;
  341. /* Read numreg registers starting from register cmd->len */
  342. for (i = 0; i < numreg; i++)
  343. cmd->arg[i] = cx24120_readreg(state, (cmd->len + i + 1));
  344. return 0;
  345. }
  346. static int cx24120_read_signal_strength(struct dvb_frontend *fe,
  347. u16 *signal_strength)
  348. {
  349. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  350. if (c->strength.stat[0].scale != FE_SCALE_RELATIVE)
  351. *signal_strength = 0;
  352. else
  353. *signal_strength = c->strength.stat[0].uvalue;
  354. return 0;
  355. }
  356. static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state,
  357. u8 enable)
  358. {
  359. struct cx24120_cmd cmd;
  360. int ret;
  361. cmd.id = CMD_MPEG_ONOFF;
  362. cmd.len = 4;
  363. cmd.arg[0] = 0x01;
  364. cmd.arg[1] = 0x00;
  365. cmd.arg[2] = enable ? 0 : (u8)(-1);
  366. cmd.arg[3] = 0x01;
  367. ret = cx24120_message_send(state, &cmd);
  368. if (ret != 0) {
  369. dev_dbg(&state->i2c->dev, "failed to %s MPEG output\n",
  370. enable ? "enable" : "disable");
  371. return ret;
  372. }
  373. state->mpeg_enabled = enable;
  374. dev_dbg(&state->i2c->dev, "MPEG output %s\n",
  375. enable ? "enabled" : "disabled");
  376. return 0;
  377. }
  378. static int cx24120_msg_mpeg_output_config(struct cx24120_state *state, u8 seq)
  379. {
  380. struct cx24120_cmd cmd;
  381. struct cx24120_initial_mpeg_config i =
  382. state->config->initial_mpeg_config;
  383. cmd.id = CMD_MPEG_INIT;
  384. cmd.len = 7;
  385. cmd.arg[0] = seq; /* sequental number - can be 0,1,2 */
  386. cmd.arg[1] = ((i.x1 & 0x01) << 1) | ((i.x1 >> 1) & 0x01);
  387. cmd.arg[2] = 0x05;
  388. cmd.arg[3] = 0x02;
  389. cmd.arg[4] = ((i.x2 >> 1) & 0x01);
  390. cmd.arg[5] = (i.x2 & 0xf0) | (i.x3 & 0x0f);
  391. cmd.arg[6] = 0x10;
  392. return cx24120_message_send(state, &cmd);
  393. }
  394. static int cx24120_diseqc_send_burst(struct dvb_frontend *fe,
  395. enum fe_sec_mini_cmd burst)
  396. {
  397. struct cx24120_state *state = fe->demodulator_priv;
  398. struct cx24120_cmd cmd;
  399. dev_dbg(&state->i2c->dev, "\n");
  400. /*
  401. * Yes, cmd.len is set to zero. The old driver
  402. * didn't specify any len, but also had a
  403. * memset 0 before every use of the cmd struct
  404. * which would have set it to zero.
  405. * This quite probably needs looking into.
  406. */
  407. cmd.id = CMD_DISEQC_BURST;
  408. cmd.len = 0;
  409. cmd.arg[0] = 0x00;
  410. cmd.arg[1] = (burst == SEC_MINI_B) ? 0x01 : 0x00;
  411. return cx24120_message_send(state, &cmd);
  412. }
  413. static int cx24120_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
  414. {
  415. struct cx24120_state *state = fe->demodulator_priv;
  416. struct cx24120_cmd cmd;
  417. dev_dbg(&state->i2c->dev, "(%d)\n", tone);
  418. if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
  419. err("Invalid tone=%d\n", tone);
  420. return -EINVAL;
  421. }
  422. cmd.id = CMD_SETTONE;
  423. cmd.len = 4;
  424. cmd.arg[0] = 0x00;
  425. cmd.arg[1] = 0x00;
  426. cmd.arg[2] = 0x00;
  427. cmd.arg[3] = (tone == SEC_TONE_ON) ? 0x01 : 0x00;
  428. return cx24120_message_send(state, &cmd);
  429. }
  430. static int cx24120_set_voltage(struct dvb_frontend *fe,
  431. enum fe_sec_voltage voltage)
  432. {
  433. struct cx24120_state *state = fe->demodulator_priv;
  434. struct cx24120_cmd cmd;
  435. dev_dbg(&state->i2c->dev, "(%d)\n", voltage);
  436. cmd.id = CMD_SETVOLTAGE;
  437. cmd.len = 2;
  438. cmd.arg[0] = 0x00;
  439. cmd.arg[1] = (voltage == SEC_VOLTAGE_18) ? 0x01 : 0x00;
  440. return cx24120_message_send(state, &cmd);
  441. }
  442. static int cx24120_send_diseqc_msg(struct dvb_frontend *fe,
  443. struct dvb_diseqc_master_cmd *d)
  444. {
  445. struct cx24120_state *state = fe->demodulator_priv;
  446. struct cx24120_cmd cmd;
  447. int back_count;
  448. dev_dbg(&state->i2c->dev, "\n");
  449. cmd.id = CMD_DISEQC_MSG1;
  450. cmd.len = 11;
  451. cmd.arg[0] = 0x00;
  452. cmd.arg[1] = 0x00;
  453. cmd.arg[2] = 0x03;
  454. cmd.arg[3] = 0x16;
  455. cmd.arg[4] = 0x28;
  456. cmd.arg[5] = 0x01;
  457. cmd.arg[6] = 0x01;
  458. cmd.arg[7] = 0x14;
  459. cmd.arg[8] = 0x19;
  460. cmd.arg[9] = 0x14;
  461. cmd.arg[10] = 0x1e;
  462. if (cx24120_message_send(state, &cmd)) {
  463. err("send 1st message(0x%x) failed\n", cmd.id);
  464. return -EREMOTEIO;
  465. }
  466. cmd.id = CMD_DISEQC_MSG2;
  467. cmd.len = d->msg_len + 6;
  468. cmd.arg[0] = 0x00;
  469. cmd.arg[1] = 0x01;
  470. cmd.arg[2] = 0x02;
  471. cmd.arg[3] = 0x00;
  472. cmd.arg[4] = 0x00;
  473. cmd.arg[5] = d->msg_len;
  474. memcpy(&cmd.arg[6], &d->msg, d->msg_len);
  475. if (cx24120_message_send(state, &cmd)) {
  476. err("send 2nd message(0x%x) failed\n", cmd.id);
  477. return -EREMOTEIO;
  478. }
  479. back_count = 500;
  480. do {
  481. if (!(cx24120_readreg(state, 0x93) & 0x01)) {
  482. dev_dbg(&state->i2c->dev, "diseqc sequence sent\n");
  483. return 0;
  484. }
  485. msleep(20);
  486. back_count -= 20;
  487. } while (back_count);
  488. err("Too long waiting for diseqc.\n");
  489. return -ETIMEDOUT;
  490. }
  491. static void cx24120_get_stats(struct cx24120_state *state)
  492. {
  493. struct dvb_frontend *fe = &state->frontend;
  494. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  495. struct cx24120_cmd cmd;
  496. int ret, cnr, msecs;
  497. u16 sig, ucb;
  498. u32 ber;
  499. dev_dbg(&state->i2c->dev, "\n");
  500. /* signal strength */
  501. if (state->fe_status & FE_HAS_SIGNAL) {
  502. cmd.id = CMD_READ_SNR;
  503. cmd.len = 1;
  504. cmd.arg[0] = 0x00;
  505. ret = cx24120_message_send(state, &cmd);
  506. if (ret != 0) {
  507. err("error reading signal strength\n");
  508. return;
  509. }
  510. /* raw */
  511. sig = cx24120_readreg(state, CX24120_REG_SIGSTR_H) >> 6;
  512. sig = sig << 8;
  513. sig |= cx24120_readreg(state, CX24120_REG_SIGSTR_L);
  514. dev_dbg(&state->i2c->dev,
  515. "signal strength from firmware = 0x%x\n", sig);
  516. /* cooked */
  517. sig = -100 * sig + 94324;
  518. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  519. c->strength.stat[0].uvalue = sig;
  520. } else {
  521. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  522. }
  523. /* CNR */
  524. if (state->fe_status & FE_HAS_VITERBI) {
  525. cnr = cx24120_readreg(state, CX24120_REG_QUALITY_H) << 8;
  526. cnr |= cx24120_readreg(state, CX24120_REG_QUALITY_L);
  527. dev_dbg(&state->i2c->dev, "read SNR index = %d\n", cnr);
  528. /* guessed - seems about right */
  529. cnr = cnr * 100;
  530. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  531. c->cnr.stat[0].svalue = cnr;
  532. } else {
  533. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  534. }
  535. /* BER & UCB require lock */
  536. if (!(state->fe_status & FE_HAS_LOCK)) {
  537. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  538. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  539. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  540. c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  541. return;
  542. }
  543. /* BER */
  544. if (time_after(jiffies, state->ber_jiffies_stats)) {
  545. msecs = (state->berw_usecs + 500) / 1000;
  546. state->ber_jiffies_stats = jiffies + msecs_to_jiffies(msecs);
  547. ber = cx24120_readreg(state, CX24120_REG_BER_HH) << 24;
  548. ber |= cx24120_readreg(state, CX24120_REG_BER_HL) << 16;
  549. ber |= cx24120_readreg(state, CX24120_REG_BER_LH) << 8;
  550. ber |= cx24120_readreg(state, CX24120_REG_BER_LL);
  551. dev_dbg(&state->i2c->dev, "read BER index = %d\n", ber);
  552. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  553. c->post_bit_error.stat[0].uvalue += ber;
  554. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  555. c->post_bit_count.stat[0].uvalue += CX24120_BER_WSIZE;
  556. }
  557. /* UCB */
  558. if (time_after(jiffies, state->per_jiffies_stats)) {
  559. state->per_jiffies_stats = jiffies + msecs_to_jiffies(1000);
  560. ucb = cx24120_readreg(state, CX24120_REG_UCB_H) << 8;
  561. ucb |= cx24120_readreg(state, CX24120_REG_UCB_L);
  562. dev_dbg(&state->i2c->dev, "ucblocks = %d\n", ucb);
  563. /* handle reset */
  564. if (ucb < state->ucb_offset)
  565. state->ucb_offset = c->block_error.stat[0].uvalue;
  566. c->block_error.stat[0].scale = FE_SCALE_COUNTER;
  567. c->block_error.stat[0].uvalue = ucb + state->ucb_offset;
  568. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  569. c->block_count.stat[0].uvalue += state->bitrate / 8 / 208;
  570. }
  571. }
  572. static void cx24120_set_clock_ratios(struct dvb_frontend *fe);
  573. /* Read current tuning status */
  574. static int cx24120_read_status(struct dvb_frontend *fe, enum fe_status *status)
  575. {
  576. struct cx24120_state *state = fe->demodulator_priv;
  577. int lock;
  578. lock = cx24120_readreg(state, CX24120_REG_STATUS);
  579. dev_dbg(&state->i2c->dev, "status = 0x%02x\n", lock);
  580. *status = 0;
  581. if (lock & CX24120_HAS_SIGNAL)
  582. *status = FE_HAS_SIGNAL;
  583. if (lock & CX24120_HAS_CARRIER)
  584. *status |= FE_HAS_CARRIER;
  585. if (lock & CX24120_HAS_VITERBI)
  586. *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
  587. if (lock & CX24120_HAS_LOCK)
  588. *status |= FE_HAS_LOCK;
  589. /*
  590. * TODO: is FE_HAS_SYNC in the right place?
  591. * Other cx241xx drivers have this slightly
  592. * different
  593. */
  594. state->fe_status = *status;
  595. cx24120_get_stats(state);
  596. /* Set the clock once tuned in */
  597. if (state->need_clock_set && *status & FE_HAS_LOCK) {
  598. /* Set clock ratios */
  599. cx24120_set_clock_ratios(fe);
  600. /* Old driver would do a msleep(200) here */
  601. /* Renable mpeg output */
  602. if (!state->mpeg_enabled)
  603. cx24120_msg_mpeg_output_global_config(state, 1);
  604. state->need_clock_set = 0;
  605. }
  606. return 0;
  607. }
  608. /*
  609. * FEC & modulation lookup table
  610. * Used for decoding the REG_FECMODE register
  611. * once tuned in.
  612. */
  613. struct cx24120_modfec {
  614. enum fe_delivery_system delsys;
  615. enum fe_modulation mod;
  616. enum fe_code_rate fec;
  617. u8 val;
  618. };
  619. static const struct cx24120_modfec modfec_lookup_table[] = {
  620. /*delsys mod fec val */
  621. { SYS_DVBS, QPSK, FEC_1_2, 0x01 },
  622. { SYS_DVBS, QPSK, FEC_2_3, 0x02 },
  623. { SYS_DVBS, QPSK, FEC_3_4, 0x03 },
  624. { SYS_DVBS, QPSK, FEC_4_5, 0x04 },
  625. { SYS_DVBS, QPSK, FEC_5_6, 0x05 },
  626. { SYS_DVBS, QPSK, FEC_6_7, 0x06 },
  627. { SYS_DVBS, QPSK, FEC_7_8, 0x07 },
  628. { SYS_DVBS2, QPSK, FEC_1_2, 0x04 },
  629. { SYS_DVBS2, QPSK, FEC_3_5, 0x05 },
  630. { SYS_DVBS2, QPSK, FEC_2_3, 0x06 },
  631. { SYS_DVBS2, QPSK, FEC_3_4, 0x07 },
  632. { SYS_DVBS2, QPSK, FEC_4_5, 0x08 },
  633. { SYS_DVBS2, QPSK, FEC_5_6, 0x09 },
  634. { SYS_DVBS2, QPSK, FEC_8_9, 0x0a },
  635. { SYS_DVBS2, QPSK, FEC_9_10, 0x0b },
  636. { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c },
  637. { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d },
  638. { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e },
  639. { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f },
  640. { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 },
  641. { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 },
  642. };
  643. /* Retrieve current fec, modulation & pilot values */
  644. static int cx24120_get_fec(struct dvb_frontend *fe)
  645. {
  646. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  647. struct cx24120_state *state = fe->demodulator_priv;
  648. int idx;
  649. int ret;
  650. int fec;
  651. ret = cx24120_readreg(state, CX24120_REG_FECMODE);
  652. fec = ret & 0x3f; /* Lower 6 bits */
  653. dev_dbg(&state->i2c->dev, "raw fec = %d\n", fec);
  654. for (idx = 0; idx < ARRAY_SIZE(modfec_lookup_table); idx++) {
  655. if (modfec_lookup_table[idx].delsys != state->dcur.delsys)
  656. continue;
  657. if (modfec_lookup_table[idx].val != fec)
  658. continue;
  659. break; /* found */
  660. }
  661. if (idx >= ARRAY_SIZE(modfec_lookup_table)) {
  662. dev_dbg(&state->i2c->dev, "couldn't find fec!\n");
  663. return -EINVAL;
  664. }
  665. /* save values back to cache */
  666. c->modulation = modfec_lookup_table[idx].mod;
  667. c->fec_inner = modfec_lookup_table[idx].fec;
  668. c->pilot = (ret & 0x80) ? PILOT_ON : PILOT_OFF;
  669. dev_dbg(&state->i2c->dev, "mod(%d), fec(%d), pilot(%d)\n",
  670. c->modulation, c->fec_inner, c->pilot);
  671. return 0;
  672. }
  673. /* Calculate ber window time */
  674. static void cx24120_calculate_ber_window(struct cx24120_state *state, u32 rate)
  675. {
  676. struct dvb_frontend *fe = &state->frontend;
  677. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  678. u64 tmp;
  679. /*
  680. * Calculate bitrate from rate in the clock ratios table.
  681. * This isn't *exactly* right but close enough.
  682. */
  683. tmp = (u64)c->symbol_rate * rate;
  684. do_div(tmp, 256);
  685. state->bitrate = tmp;
  686. /* usecs per ber window */
  687. tmp = 1000000ULL * CX24120_BER_WSIZE;
  688. do_div(tmp, state->bitrate);
  689. state->berw_usecs = tmp;
  690. dev_dbg(&state->i2c->dev, "bitrate: %u, berw_usecs: %u\n",
  691. state->bitrate, state->berw_usecs);
  692. }
  693. /*
  694. * Clock ratios lookup table
  695. *
  696. * Values obtained from much larger table in old driver
  697. * which had numerous entries which would never match.
  698. *
  699. * There's probably some way of calculating these but I
  700. * can't determine the pattern
  701. */
  702. struct cx24120_clock_ratios_table {
  703. enum fe_delivery_system delsys;
  704. enum fe_pilot pilot;
  705. enum fe_modulation mod;
  706. enum fe_code_rate fec;
  707. u32 m_rat;
  708. u32 n_rat;
  709. u32 rate;
  710. };
  711. static const struct cx24120_clock_ratios_table clock_ratios_table[] = {
  712. /*delsys pilot mod fec m_rat n_rat rate */
  713. { SYS_DVBS2, PILOT_OFF, QPSK, FEC_1_2, 273088, 254505, 274 },
  714. { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_5, 17272, 13395, 330 },
  715. { SYS_DVBS2, PILOT_OFF, QPSK, FEC_2_3, 24344, 16967, 367 },
  716. { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_4, 410788, 254505, 413 },
  717. { SYS_DVBS2, PILOT_OFF, QPSK, FEC_4_5, 438328, 254505, 440 },
  718. { SYS_DVBS2, PILOT_OFF, QPSK, FEC_5_6, 30464, 16967, 459 },
  719. { SYS_DVBS2, PILOT_OFF, QPSK, FEC_8_9, 487832, 254505, 490 },
  720. { SYS_DVBS2, PILOT_OFF, QPSK, FEC_9_10, 493952, 254505, 496 },
  721. { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_5, 328168, 169905, 494 },
  722. { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_2_3, 24344, 11327, 550 },
  723. { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_4, 410788, 169905, 618 },
  724. { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_5_6, 30464, 11327, 688 },
  725. { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_8_9, 487832, 169905, 735 },
  726. { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_9_10, 493952, 169905, 744 },
  727. { SYS_DVBS2, PILOT_ON, QPSK, FEC_1_2, 273088, 260709, 268 },
  728. { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_5, 328168, 260709, 322 },
  729. { SYS_DVBS2, PILOT_ON, QPSK, FEC_2_3, 121720, 86903, 358 },
  730. { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_4, 410788, 260709, 403 },
  731. { SYS_DVBS2, PILOT_ON, QPSK, FEC_4_5, 438328, 260709, 430 },
  732. { SYS_DVBS2, PILOT_ON, QPSK, FEC_5_6, 152320, 86903, 448 },
  733. { SYS_DVBS2, PILOT_ON, QPSK, FEC_8_9, 487832, 260709, 479 },
  734. { SYS_DVBS2, PILOT_ON, QPSK, FEC_9_10, 493952, 260709, 485 },
  735. { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_5, 328168, 173853, 483 },
  736. { SYS_DVBS2, PILOT_ON, PSK_8, FEC_2_3, 121720, 57951, 537 },
  737. { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_4, 410788, 173853, 604 },
  738. { SYS_DVBS2, PILOT_ON, PSK_8, FEC_5_6, 152320, 57951, 672 },
  739. { SYS_DVBS2, PILOT_ON, PSK_8, FEC_8_9, 487832, 173853, 718 },
  740. { SYS_DVBS2, PILOT_ON, PSK_8, FEC_9_10, 493952, 173853, 727 },
  741. { SYS_DVBS, PILOT_OFF, QPSK, FEC_1_2, 152592, 152592, 256 },
  742. { SYS_DVBS, PILOT_OFF, QPSK, FEC_2_3, 305184, 228888, 341 },
  743. { SYS_DVBS, PILOT_OFF, QPSK, FEC_3_4, 457776, 305184, 384 },
  744. { SYS_DVBS, PILOT_OFF, QPSK, FEC_5_6, 762960, 457776, 427 },
  745. { SYS_DVBS, PILOT_OFF, QPSK, FEC_7_8, 1068144, 610368, 448 },
  746. };
  747. /* Set clock ratio from lookup table */
  748. static void cx24120_set_clock_ratios(struct dvb_frontend *fe)
  749. {
  750. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  751. struct cx24120_state *state = fe->demodulator_priv;
  752. struct cx24120_cmd cmd;
  753. int ret, idx;
  754. /* Find fec, modulation, pilot */
  755. ret = cx24120_get_fec(fe);
  756. if (ret != 0)
  757. return;
  758. /* Find the clock ratios in the lookup table */
  759. for (idx = 0; idx < ARRAY_SIZE(clock_ratios_table); idx++) {
  760. if (clock_ratios_table[idx].delsys != state->dcur.delsys)
  761. continue;
  762. if (clock_ratios_table[idx].mod != c->modulation)
  763. continue;
  764. if (clock_ratios_table[idx].fec != c->fec_inner)
  765. continue;
  766. if (clock_ratios_table[idx].pilot != c->pilot)
  767. continue;
  768. break; /* found */
  769. }
  770. if (idx >= ARRAY_SIZE(clock_ratios_table)) {
  771. info("Clock ratio not found - data reception in danger\n");
  772. return;
  773. }
  774. /* Read current values? */
  775. cmd.id = CMD_CLOCK_READ;
  776. cmd.len = 1;
  777. cmd.arg[0] = 0x00;
  778. ret = cx24120_message_sendrcv(state, &cmd, 6);
  779. if (ret != 0)
  780. return;
  781. /* in cmd[0]-[5] - result */
  782. dev_dbg(&state->i2c->dev, "m=%d, n=%d; idx: %d m=%d, n=%d, rate=%d\n",
  783. cmd.arg[2] | (cmd.arg[1] << 8) | (cmd.arg[0] << 16),
  784. cmd.arg[5] | (cmd.arg[4] << 8) | (cmd.arg[3] << 16),
  785. idx,
  786. clock_ratios_table[idx].m_rat,
  787. clock_ratios_table[idx].n_rat,
  788. clock_ratios_table[idx].rate);
  789. /* Set the clock */
  790. cmd.id = CMD_CLOCK_SET;
  791. cmd.len = 10;
  792. cmd.arg[0] = 0;
  793. cmd.arg[1] = 0x10;
  794. cmd.arg[2] = (clock_ratios_table[idx].m_rat >> 16) & 0xff;
  795. cmd.arg[3] = (clock_ratios_table[idx].m_rat >> 8) & 0xff;
  796. cmd.arg[4] = (clock_ratios_table[idx].m_rat >> 0) & 0xff;
  797. cmd.arg[5] = (clock_ratios_table[idx].n_rat >> 16) & 0xff;
  798. cmd.arg[6] = (clock_ratios_table[idx].n_rat >> 8) & 0xff;
  799. cmd.arg[7] = (clock_ratios_table[idx].n_rat >> 0) & 0xff;
  800. cmd.arg[8] = (clock_ratios_table[idx].rate >> 8) & 0xff;
  801. cmd.arg[9] = (clock_ratios_table[idx].rate >> 0) & 0xff;
  802. ret = cx24120_message_send(state, &cmd);
  803. if (ret != 0)
  804. return;
  805. /* Calculate ber window rates for stat work */
  806. cx24120_calculate_ber_window(state, clock_ratios_table[idx].rate);
  807. }
  808. /* Set inversion value */
  809. static int cx24120_set_inversion(struct cx24120_state *state,
  810. enum fe_spectral_inversion inversion)
  811. {
  812. dev_dbg(&state->i2c->dev, "(%d)\n", inversion);
  813. switch (inversion) {
  814. case INVERSION_OFF:
  815. state->dnxt.inversion_val = 0x00;
  816. break;
  817. case INVERSION_ON:
  818. state->dnxt.inversion_val = 0x04;
  819. break;
  820. case INVERSION_AUTO:
  821. state->dnxt.inversion_val = 0x0c;
  822. break;
  823. default:
  824. return -EINVAL;
  825. }
  826. state->dnxt.inversion = inversion;
  827. return 0;
  828. }
  829. /* FEC lookup table for tuning */
  830. struct cx24120_modfec_table {
  831. enum fe_delivery_system delsys;
  832. enum fe_modulation mod;
  833. enum fe_code_rate fec;
  834. u8 val;
  835. };
  836. static const struct cx24120_modfec_table modfec_table[] = {
  837. /*delsys mod fec val */
  838. { SYS_DVBS, QPSK, FEC_1_2, 0x2e },
  839. { SYS_DVBS, QPSK, FEC_2_3, 0x2f },
  840. { SYS_DVBS, QPSK, FEC_3_4, 0x30 },
  841. { SYS_DVBS, QPSK, FEC_5_6, 0x31 },
  842. { SYS_DVBS, QPSK, FEC_6_7, 0x32 },
  843. { SYS_DVBS, QPSK, FEC_7_8, 0x33 },
  844. { SYS_DVBS2, QPSK, FEC_1_2, 0x04 },
  845. { SYS_DVBS2, QPSK, FEC_3_5, 0x05 },
  846. { SYS_DVBS2, QPSK, FEC_2_3, 0x06 },
  847. { SYS_DVBS2, QPSK, FEC_3_4, 0x07 },
  848. { SYS_DVBS2, QPSK, FEC_4_5, 0x08 },
  849. { SYS_DVBS2, QPSK, FEC_5_6, 0x09 },
  850. { SYS_DVBS2, QPSK, FEC_8_9, 0x0a },
  851. { SYS_DVBS2, QPSK, FEC_9_10, 0x0b },
  852. { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c },
  853. { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d },
  854. { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e },
  855. { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f },
  856. { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 },
  857. { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 },
  858. };
  859. /* Set fec_val & fec_mask values from delsys, modulation & fec */
  860. static int cx24120_set_fec(struct cx24120_state *state, enum fe_modulation mod,
  861. enum fe_code_rate fec)
  862. {
  863. int idx;
  864. dev_dbg(&state->i2c->dev, "(0x%02x,0x%02x)\n", mod, fec);
  865. state->dnxt.fec = fec;
  866. /* Lookup fec_val from modfec table */
  867. for (idx = 0; idx < ARRAY_SIZE(modfec_table); idx++) {
  868. if (modfec_table[idx].delsys != state->dnxt.delsys)
  869. continue;
  870. if (modfec_table[idx].mod != mod)
  871. continue;
  872. if (modfec_table[idx].fec != fec)
  873. continue;
  874. /* found */
  875. state->dnxt.fec_mask = 0x00;
  876. state->dnxt.fec_val = modfec_table[idx].val;
  877. return 0;
  878. }
  879. if (state->dnxt.delsys == SYS_DVBS2) {
  880. /* DVBS2 auto is 0x00/0x00 */
  881. state->dnxt.fec_mask = 0x00;
  882. state->dnxt.fec_val = 0x00;
  883. } else {
  884. /* Set DVB-S to auto */
  885. state->dnxt.fec_val = 0x2e;
  886. state->dnxt.fec_mask = 0xac;
  887. }
  888. return 0;
  889. }
  890. /* Set pilot */
  891. static int cx24120_set_pilot(struct cx24120_state *state, enum fe_pilot pilot)
  892. {
  893. dev_dbg(&state->i2c->dev, "(%d)\n", pilot);
  894. /* Pilot only valid in DVBS2 */
  895. if (state->dnxt.delsys != SYS_DVBS2) {
  896. state->dnxt.pilot_val = CX24120_PILOT_OFF;
  897. return 0;
  898. }
  899. switch (pilot) {
  900. case PILOT_OFF:
  901. state->dnxt.pilot_val = CX24120_PILOT_OFF;
  902. break;
  903. case PILOT_ON:
  904. state->dnxt.pilot_val = CX24120_PILOT_ON;
  905. break;
  906. case PILOT_AUTO:
  907. default:
  908. state->dnxt.pilot_val = CX24120_PILOT_AUTO;
  909. }
  910. return 0;
  911. }
  912. /* Set symbol rate */
  913. static int cx24120_set_symbolrate(struct cx24120_state *state, u32 rate)
  914. {
  915. dev_dbg(&state->i2c->dev, "(%d)\n", rate);
  916. state->dnxt.symbol_rate = rate;
  917. /* Check symbol rate */
  918. if (rate > 31000000) {
  919. state->dnxt.clkdiv = (-(rate < 31000001) & 3) + 2;
  920. state->dnxt.ratediv = (-(rate < 31000001) & 6) + 4;
  921. } else {
  922. state->dnxt.clkdiv = 3;
  923. state->dnxt.ratediv = 6;
  924. }
  925. return 0;
  926. }
  927. /* Overwrite the current tuning params, we are about to tune */
  928. static void cx24120_clone_params(struct dvb_frontend *fe)
  929. {
  930. struct cx24120_state *state = fe->demodulator_priv;
  931. state->dcur = state->dnxt;
  932. }
  933. static int cx24120_set_frontend(struct dvb_frontend *fe)
  934. {
  935. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  936. struct cx24120_state *state = fe->demodulator_priv;
  937. struct cx24120_cmd cmd;
  938. int ret;
  939. switch (c->delivery_system) {
  940. case SYS_DVBS2:
  941. dev_dbg(&state->i2c->dev, "DVB-S2\n");
  942. break;
  943. case SYS_DVBS:
  944. dev_dbg(&state->i2c->dev, "DVB-S\n");
  945. break;
  946. default:
  947. dev_dbg(&state->i2c->dev,
  948. "delivery system(%d) not supported\n",
  949. c->delivery_system);
  950. return -EINVAL;
  951. }
  952. state->dnxt.delsys = c->delivery_system;
  953. state->dnxt.modulation = c->modulation;
  954. state->dnxt.frequency = c->frequency;
  955. state->dnxt.pilot = c->pilot;
  956. ret = cx24120_set_inversion(state, c->inversion);
  957. if (ret != 0)
  958. return ret;
  959. ret = cx24120_set_fec(state, c->modulation, c->fec_inner);
  960. if (ret != 0)
  961. return ret;
  962. ret = cx24120_set_pilot(state, c->pilot);
  963. if (ret != 0)
  964. return ret;
  965. ret = cx24120_set_symbolrate(state, c->symbol_rate);
  966. if (ret != 0)
  967. return ret;
  968. /* discard the 'current' tuning parameters and prepare to tune */
  969. cx24120_clone_params(fe);
  970. dev_dbg(&state->i2c->dev,
  971. "delsys = %d\n", state->dcur.delsys);
  972. dev_dbg(&state->i2c->dev,
  973. "modulation = %d\n", state->dcur.modulation);
  974. dev_dbg(&state->i2c->dev,
  975. "frequency = %d\n", state->dcur.frequency);
  976. dev_dbg(&state->i2c->dev,
  977. "pilot = %d (val = 0x%02x)\n",
  978. state->dcur.pilot, state->dcur.pilot_val);
  979. dev_dbg(&state->i2c->dev,
  980. "symbol_rate = %d (clkdiv/ratediv = 0x%02x/0x%02x)\n",
  981. state->dcur.symbol_rate,
  982. state->dcur.clkdiv, state->dcur.ratediv);
  983. dev_dbg(&state->i2c->dev,
  984. "FEC = %d (mask/val = 0x%02x/0x%02x)\n",
  985. state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
  986. dev_dbg(&state->i2c->dev,
  987. "Inversion = %d (val = 0x%02x)\n",
  988. state->dcur.inversion, state->dcur.inversion_val);
  989. /* Flag that clock needs to be set after tune */
  990. state->need_clock_set = 1;
  991. /* Tune in */
  992. cmd.id = CMD_TUNEREQUEST;
  993. cmd.len = 15;
  994. cmd.arg[0] = 0;
  995. cmd.arg[1] = (state->dcur.frequency & 0xff0000) >> 16;
  996. cmd.arg[2] = (state->dcur.frequency & 0x00ff00) >> 8;
  997. cmd.arg[3] = (state->dcur.frequency & 0x0000ff);
  998. cmd.arg[4] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
  999. cmd.arg[5] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
  1000. cmd.arg[6] = state->dcur.inversion;
  1001. cmd.arg[7] = state->dcur.fec_val | state->dcur.pilot_val;
  1002. cmd.arg[8] = CX24120_SEARCH_RANGE_KHZ >> 8;
  1003. cmd.arg[9] = CX24120_SEARCH_RANGE_KHZ & 0xff;
  1004. cmd.arg[10] = 0; /* maybe rolloff? */
  1005. cmd.arg[11] = state->dcur.fec_mask;
  1006. cmd.arg[12] = state->dcur.ratediv;
  1007. cmd.arg[13] = state->dcur.clkdiv;
  1008. cmd.arg[14] = 0;
  1009. /* Send tune command */
  1010. ret = cx24120_message_send(state, &cmd);
  1011. if (ret != 0)
  1012. return ret;
  1013. /* Write symbol rate values */
  1014. ret = cx24120_writereg(state, CX24120_REG_CLKDIV, state->dcur.clkdiv);
  1015. ret = cx24120_readreg(state, CX24120_REG_RATEDIV);
  1016. ret &= 0xfffffff0;
  1017. ret |= state->dcur.ratediv;
  1018. ret = cx24120_writereg(state, CX24120_REG_RATEDIV, ret);
  1019. return 0;
  1020. }
  1021. /* Set vco from config */
  1022. static int cx24120_set_vco(struct cx24120_state *state)
  1023. {
  1024. struct cx24120_cmd cmd;
  1025. u32 nxtal_khz, vco;
  1026. u64 inv_vco;
  1027. u32 xtal_khz = state->config->xtal_khz;
  1028. nxtal_khz = xtal_khz * 4;
  1029. vco = nxtal_khz * 10;
  1030. inv_vco = DIV_ROUND_CLOSEST_ULL(0x400000000ULL, vco);
  1031. dev_dbg(&state->i2c->dev, "xtal=%d, vco=%d, inv_vco=%lld\n",
  1032. xtal_khz, vco, inv_vco);
  1033. cmd.id = CMD_VCO_SET;
  1034. cmd.len = 12;
  1035. cmd.arg[0] = (vco >> 16) & 0xff;
  1036. cmd.arg[1] = (vco >> 8) & 0xff;
  1037. cmd.arg[2] = vco & 0xff;
  1038. cmd.arg[3] = (inv_vco >> 8) & 0xff;
  1039. cmd.arg[4] = (inv_vco) & 0xff;
  1040. cmd.arg[5] = 0x03;
  1041. cmd.arg[6] = (nxtal_khz >> 8) & 0xff;
  1042. cmd.arg[7] = nxtal_khz & 0xff;
  1043. cmd.arg[8] = 0x06;
  1044. cmd.arg[9] = 0x03;
  1045. cmd.arg[10] = (xtal_khz >> 16) & 0xff;
  1046. cmd.arg[11] = xtal_khz & 0xff;
  1047. return cx24120_message_send(state, &cmd);
  1048. }
  1049. static int cx24120_init(struct dvb_frontend *fe)
  1050. {
  1051. const struct firmware *fw;
  1052. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1053. struct cx24120_state *state = fe->demodulator_priv;
  1054. struct cx24120_cmd cmd;
  1055. u8 reg;
  1056. int ret, i;
  1057. unsigned char vers[4];
  1058. if (state->cold_init)
  1059. return 0;
  1060. /* ???? */
  1061. cx24120_writereg(state, 0xea, 0x00);
  1062. cx24120_test_rom(state);
  1063. reg = cx24120_readreg(state, 0xfb) & 0xfe;
  1064. cx24120_writereg(state, 0xfb, reg);
  1065. reg = cx24120_readreg(state, 0xfc) & 0xfe;
  1066. cx24120_writereg(state, 0xfc, reg);
  1067. cx24120_writereg(state, 0xc3, 0x04);
  1068. cx24120_writereg(state, 0xc4, 0x04);
  1069. cx24120_writereg(state, 0xce, 0x00);
  1070. cx24120_writereg(state, 0xcf, 0x00);
  1071. reg = cx24120_readreg(state, 0xea) & 0xfe;
  1072. cx24120_writereg(state, 0xea, reg);
  1073. cx24120_writereg(state, 0xeb, 0x0c);
  1074. cx24120_writereg(state, 0xec, 0x06);
  1075. cx24120_writereg(state, 0xed, 0x05);
  1076. cx24120_writereg(state, 0xee, 0x03);
  1077. cx24120_writereg(state, 0xef, 0x05);
  1078. cx24120_writereg(state, 0xf3, 0x03);
  1079. cx24120_writereg(state, 0xf4, 0x44);
  1080. for (i = 0; i < 3; i++) {
  1081. cx24120_writereg(state, 0xf0 + i, 0x04);
  1082. cx24120_writereg(state, 0xe6 + i, 0x02);
  1083. }
  1084. cx24120_writereg(state, 0xea, (reg | 0x01));
  1085. for (i = 0; i < 6; i += 2) {
  1086. cx24120_writereg(state, 0xc5 + i, 0x00);
  1087. cx24120_writereg(state, 0xc6 + i, 0x00);
  1088. }
  1089. cx24120_writereg(state, 0xe4, 0x03);
  1090. cx24120_writereg(state, 0xeb, 0x0a);
  1091. dev_dbg(&state->i2c->dev, "requesting firmware (%s) to download...\n",
  1092. CX24120_FIRMWARE);
  1093. ret = state->config->request_firmware(fe, &fw, CX24120_FIRMWARE);
  1094. if (ret) {
  1095. err("Could not load firmware (%s): %d\n", CX24120_FIRMWARE,
  1096. ret);
  1097. return ret;
  1098. }
  1099. dev_dbg(&state->i2c->dev,
  1100. "Firmware found, size %d bytes (%02x %02x .. %02x %02x)\n",
  1101. (int)fw->size, /* firmware_size in bytes */
  1102. fw->data[0], /* fw 1st byte */
  1103. fw->data[1], /* fw 2d byte */
  1104. fw->data[fw->size - 2], /* fw before last byte */
  1105. fw->data[fw->size - 1]); /* fw last byte */
  1106. cx24120_test_rom(state);
  1107. reg = cx24120_readreg(state, 0xfb) & 0xfe;
  1108. cx24120_writereg(state, 0xfb, reg);
  1109. cx24120_writereg(state, 0xe0, 0x76);
  1110. cx24120_writereg(state, 0xf7, 0x81);
  1111. cx24120_writereg(state, 0xf8, 0x00);
  1112. cx24120_writereg(state, 0xf9, 0x00);
  1113. cx24120_writeregs(state, 0xfa, fw->data, (fw->size - 1), 0x00);
  1114. cx24120_writereg(state, 0xf7, 0xc0);
  1115. cx24120_writereg(state, 0xe0, 0x00);
  1116. reg = (fw->size - 2) & 0x00ff;
  1117. cx24120_writereg(state, 0xf8, reg);
  1118. reg = ((fw->size - 2) >> 8) & 0x00ff;
  1119. cx24120_writereg(state, 0xf9, reg);
  1120. cx24120_writereg(state, 0xf7, 0x00);
  1121. cx24120_writereg(state, 0xdc, 0x00);
  1122. cx24120_writereg(state, 0xdc, 0x07);
  1123. msleep(500);
  1124. /* Check final byte matches final byte of firmware */
  1125. reg = cx24120_readreg(state, 0xe1);
  1126. if (reg == fw->data[fw->size - 1]) {
  1127. dev_dbg(&state->i2c->dev, "Firmware uploaded successfully\n");
  1128. ret = 0;
  1129. } else {
  1130. err("Firmware upload failed. Last byte returned=0x%x\n", ret);
  1131. ret = -EREMOTEIO;
  1132. }
  1133. cx24120_writereg(state, 0xdc, 0x00);
  1134. release_firmware(fw);
  1135. if (ret != 0)
  1136. return ret;
  1137. /* Start tuner */
  1138. cmd.id = CMD_START_TUNER;
  1139. cmd.len = 3;
  1140. cmd.arg[0] = 0x00;
  1141. cmd.arg[1] = 0x00;
  1142. cmd.arg[2] = 0x00;
  1143. if (cx24120_message_send(state, &cmd) != 0) {
  1144. err("Error tuner start! :(\n");
  1145. return -EREMOTEIO;
  1146. }
  1147. /* Set VCO */
  1148. ret = cx24120_set_vco(state);
  1149. if (ret != 0) {
  1150. err("Error set VCO! :(\n");
  1151. return ret;
  1152. }
  1153. /* set bandwidth */
  1154. cmd.id = CMD_BANDWIDTH;
  1155. cmd.len = 12;
  1156. cmd.arg[0] = 0x00;
  1157. cmd.arg[1] = 0x00;
  1158. cmd.arg[2] = 0x00;
  1159. cmd.arg[3] = 0x00;
  1160. cmd.arg[4] = 0x05;
  1161. cmd.arg[5] = 0x02;
  1162. cmd.arg[6] = 0x02;
  1163. cmd.arg[7] = 0x00;
  1164. cmd.arg[8] = 0x05;
  1165. cmd.arg[9] = 0x02;
  1166. cmd.arg[10] = 0x02;
  1167. cmd.arg[11] = 0x00;
  1168. if (cx24120_message_send(state, &cmd)) {
  1169. err("Error set bandwidth!\n");
  1170. return -EREMOTEIO;
  1171. }
  1172. reg = cx24120_readreg(state, 0xba);
  1173. if (reg > 3) {
  1174. dev_dbg(&state->i2c->dev, "Reset-readreg 0xba: %x\n", ret);
  1175. err("Error initialising tuner!\n");
  1176. return -EREMOTEIO;
  1177. }
  1178. dev_dbg(&state->i2c->dev, "Tuner initialised correctly.\n");
  1179. /* Initialise mpeg outputs */
  1180. cx24120_writereg(state, 0xeb, 0x0a);
  1181. if (cx24120_msg_mpeg_output_global_config(state, 0) ||
  1182. cx24120_msg_mpeg_output_config(state, 0) ||
  1183. cx24120_msg_mpeg_output_config(state, 1) ||
  1184. cx24120_msg_mpeg_output_config(state, 2)) {
  1185. err("Error initialising mpeg output. :(\n");
  1186. return -EREMOTEIO;
  1187. }
  1188. /* Set size of BER window */
  1189. cmd.id = CMD_BER_CTRL;
  1190. cmd.len = 3;
  1191. cmd.arg[0] = 0x00;
  1192. cmd.arg[1] = CX24120_BER_WINDOW;
  1193. cmd.arg[2] = CX24120_BER_WINDOW;
  1194. if (cx24120_message_send(state, &cmd)) {
  1195. err("Error setting ber window\n");
  1196. return -EREMOTEIO;
  1197. }
  1198. /* Firmware CMD 35: Get firmware version */
  1199. cmd.id = CMD_FWVERSION;
  1200. cmd.len = 1;
  1201. for (i = 0; i < 4; i++) {
  1202. cmd.arg[0] = i;
  1203. ret = cx24120_message_send(state, &cmd);
  1204. if (ret != 0)
  1205. return ret;
  1206. vers[i] = cx24120_readreg(state, CX24120_REG_MAILBOX);
  1207. }
  1208. info("FW version %i.%i.%i.%i\n", vers[0], vers[1], vers[2], vers[3]);
  1209. /* init stats here in order signal app which stats are supported */
  1210. c->strength.len = 1;
  1211. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1212. c->cnr.len = 1;
  1213. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1214. c->post_bit_error.len = 1;
  1215. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1216. c->post_bit_count.len = 1;
  1217. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1218. c->block_error.len = 1;
  1219. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1220. c->block_count.len = 1;
  1221. c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1222. state->cold_init = 1;
  1223. return 0;
  1224. }
  1225. static int cx24120_tune(struct dvb_frontend *fe, bool re_tune,
  1226. unsigned int mode_flags, unsigned int *delay,
  1227. enum fe_status *status)
  1228. {
  1229. struct cx24120_state *state = fe->demodulator_priv;
  1230. int ret;
  1231. dev_dbg(&state->i2c->dev, "(%d)\n", re_tune);
  1232. /* TODO: Do we need to set delay? */
  1233. if (re_tune) {
  1234. ret = cx24120_set_frontend(fe);
  1235. if (ret)
  1236. return ret;
  1237. }
  1238. return cx24120_read_status(fe, status);
  1239. }
  1240. static enum dvbfe_algo cx24120_get_algo(struct dvb_frontend *fe)
  1241. {
  1242. return DVBFE_ALGO_HW;
  1243. }
  1244. static int cx24120_sleep(struct dvb_frontend *fe)
  1245. {
  1246. return 0;
  1247. }
  1248. static int cx24120_get_frontend(struct dvb_frontend *fe,
  1249. struct dtv_frontend_properties *c)
  1250. {
  1251. struct cx24120_state *state = fe->demodulator_priv;
  1252. u8 freq1, freq2, freq3;
  1253. int status;
  1254. dev_dbg(&state->i2c->dev, "\n");
  1255. /* don't return empty data if we're not tuned in */
  1256. status = cx24120_readreg(state, CX24120_REG_STATUS);
  1257. if (!(status & CX24120_HAS_LOCK))
  1258. return 0;
  1259. /* Get frequency */
  1260. freq1 = cx24120_readreg(state, CX24120_REG_FREQ1);
  1261. freq2 = cx24120_readreg(state, CX24120_REG_FREQ2);
  1262. freq3 = cx24120_readreg(state, CX24120_REG_FREQ3);
  1263. c->frequency = (freq3 << 16) | (freq2 << 8) | freq1;
  1264. dev_dbg(&state->i2c->dev, "frequency = %d\n", c->frequency);
  1265. /* Get modulation, fec, pilot */
  1266. cx24120_get_fec(fe);
  1267. return 0;
  1268. }
  1269. static void cx24120_release(struct dvb_frontend *fe)
  1270. {
  1271. struct cx24120_state *state = fe->demodulator_priv;
  1272. dev_dbg(&state->i2c->dev, "Clear state structure\n");
  1273. kfree(state);
  1274. }
  1275. static int cx24120_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  1276. {
  1277. struct cx24120_state *state = fe->demodulator_priv;
  1278. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1279. if (c->block_error.stat[0].scale != FE_SCALE_COUNTER) {
  1280. *ucblocks = 0;
  1281. return 0;
  1282. }
  1283. *ucblocks = c->block_error.stat[0].uvalue - state->ucb_offset;
  1284. return 0;
  1285. }
  1286. static const struct dvb_frontend_ops cx24120_ops = {
  1287. .delsys = { SYS_DVBS, SYS_DVBS2 },
  1288. .info = {
  1289. .name = "Conexant CX24120/CX24118",
  1290. .frequency_min_hz = 950 * MHz,
  1291. .frequency_max_hz = 2150 * MHz,
  1292. .frequency_stepsize_hz = 1011 * kHz,
  1293. .frequency_tolerance_hz = 5 * MHz,
  1294. .symbol_rate_min = 1000000,
  1295. .symbol_rate_max = 45000000,
  1296. .caps = FE_CAN_INVERSION_AUTO |
  1297. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1298. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  1299. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1300. FE_CAN_2G_MODULATION |
  1301. FE_CAN_QPSK | FE_CAN_RECOVER
  1302. },
  1303. .release = cx24120_release,
  1304. .init = cx24120_init,
  1305. .sleep = cx24120_sleep,
  1306. .tune = cx24120_tune,
  1307. .get_frontend_algo = cx24120_get_algo,
  1308. .set_frontend = cx24120_set_frontend,
  1309. .get_frontend = cx24120_get_frontend,
  1310. .read_status = cx24120_read_status,
  1311. .read_ber = cx24120_read_ber,
  1312. .read_signal_strength = cx24120_read_signal_strength,
  1313. .read_snr = cx24120_read_snr,
  1314. .read_ucblocks = cx24120_read_ucblocks,
  1315. .diseqc_send_master_cmd = cx24120_send_diseqc_msg,
  1316. .diseqc_send_burst = cx24120_diseqc_send_burst,
  1317. .set_tone = cx24120_set_tone,
  1318. .set_voltage = cx24120_set_voltage,
  1319. };
  1320. MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24120/CX24118 hardware");
  1321. MODULE_AUTHOR("Jemma Denson");
  1322. MODULE_LICENSE("GPL");