cx24120.c 41 KB

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