cx24120.c 42 KB

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