mxl5xx.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the MaxLinear MxL5xx family of tuners/demods
  4. *
  5. * Copyright (C) 2014-2015 Ralph Metzler <[email protected]>
  6. * Marcus Metzler <[email protected]>
  7. * developed for Digital Devices GmbH
  8. *
  9. * based on code:
  10. * Copyright (c) 2011-2013 MaxLinear, Inc. All rights reserved
  11. * which was released under GPL V2
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/firmware.h>
  19. #include <linux/i2c.h>
  20. #include <linux/mutex.h>
  21. #include <linux/vmalloc.h>
  22. #include <asm/div64.h>
  23. #include <asm/unaligned.h>
  24. #include <media/dvb_frontend.h>
  25. #include "mxl5xx.h"
  26. #include "mxl5xx_regs.h"
  27. #include "mxl5xx_defs.h"
  28. #define BYTE0(v) ((v >> 0) & 0xff)
  29. #define BYTE1(v) ((v >> 8) & 0xff)
  30. #define BYTE2(v) ((v >> 16) & 0xff)
  31. #define BYTE3(v) ((v >> 24) & 0xff)
  32. static LIST_HEAD(mxllist);
  33. struct mxl_base {
  34. struct list_head mxllist;
  35. struct list_head mxls;
  36. u8 adr;
  37. struct i2c_adapter *i2c;
  38. u32 count;
  39. u32 type;
  40. u32 sku_type;
  41. u32 chipversion;
  42. u32 clock;
  43. u32 fwversion;
  44. u8 *ts_map;
  45. u8 can_clkout;
  46. u8 chan_bond;
  47. u8 demod_num;
  48. u8 tuner_num;
  49. unsigned long next_tune;
  50. struct mutex i2c_lock;
  51. struct mutex status_lock;
  52. struct mutex tune_lock;
  53. u8 buf[MXL_HYDRA_OEM_MAX_CMD_BUFF_LEN];
  54. u32 cmd_size;
  55. u8 cmd_data[MAX_CMD_DATA];
  56. };
  57. struct mxl {
  58. struct list_head mxl;
  59. struct mxl_base *base;
  60. struct dvb_frontend fe;
  61. struct device *i2cdev;
  62. u32 demod;
  63. u32 tuner;
  64. u32 tuner_in_use;
  65. u8 xbar[3];
  66. unsigned long tune_time;
  67. };
  68. static void convert_endian(u8 flag, u32 size, u8 *d)
  69. {
  70. u32 i;
  71. if (!flag)
  72. return;
  73. for (i = 0; i < (size & ~3); i += 4) {
  74. d[i + 0] ^= d[i + 3];
  75. d[i + 3] ^= d[i + 0];
  76. d[i + 0] ^= d[i + 3];
  77. d[i + 1] ^= d[i + 2];
  78. d[i + 2] ^= d[i + 1];
  79. d[i + 1] ^= d[i + 2];
  80. }
  81. switch (size & 3) {
  82. case 0:
  83. case 1:
  84. /* do nothing */
  85. break;
  86. case 2:
  87. d[i + 0] ^= d[i + 1];
  88. d[i + 1] ^= d[i + 0];
  89. d[i + 0] ^= d[i + 1];
  90. break;
  91. case 3:
  92. d[i + 0] ^= d[i + 2];
  93. d[i + 2] ^= d[i + 0];
  94. d[i + 0] ^= d[i + 2];
  95. break;
  96. }
  97. }
  98. static int i2c_write(struct i2c_adapter *adap, u8 adr,
  99. u8 *data, u32 len)
  100. {
  101. struct i2c_msg msg = {.addr = adr, .flags = 0,
  102. .buf = data, .len = len};
  103. return (i2c_transfer(adap, &msg, 1) == 1) ? 0 : -1;
  104. }
  105. static int i2c_read(struct i2c_adapter *adap, u8 adr,
  106. u8 *data, u32 len)
  107. {
  108. struct i2c_msg msg = {.addr = adr, .flags = I2C_M_RD,
  109. .buf = data, .len = len};
  110. return (i2c_transfer(adap, &msg, 1) == 1) ? 0 : -1;
  111. }
  112. static int i2cread(struct mxl *state, u8 *data, int len)
  113. {
  114. return i2c_read(state->base->i2c, state->base->adr, data, len);
  115. }
  116. static int i2cwrite(struct mxl *state, u8 *data, int len)
  117. {
  118. return i2c_write(state->base->i2c, state->base->adr, data, len);
  119. }
  120. static int read_register_unlocked(struct mxl *state, u32 reg, u32 *val)
  121. {
  122. int stat;
  123. u8 data[MXL_HYDRA_REG_SIZE_IN_BYTES + MXL_HYDRA_I2C_HDR_SIZE] = {
  124. MXL_HYDRA_PLID_REG_READ, 0x04,
  125. GET_BYTE(reg, 0), GET_BYTE(reg, 1),
  126. GET_BYTE(reg, 2), GET_BYTE(reg, 3),
  127. };
  128. stat = i2cwrite(state, data,
  129. MXL_HYDRA_REG_SIZE_IN_BYTES + MXL_HYDRA_I2C_HDR_SIZE);
  130. if (stat)
  131. dev_err(state->i2cdev, "i2c read error 1\n");
  132. if (!stat)
  133. stat = i2cread(state, (u8 *) val,
  134. MXL_HYDRA_REG_SIZE_IN_BYTES);
  135. le32_to_cpus(val);
  136. if (stat)
  137. dev_err(state->i2cdev, "i2c read error 2\n");
  138. return stat;
  139. }
  140. #define DMA_I2C_INTERRUPT_ADDR 0x8000011C
  141. #define DMA_INTR_PROT_WR_CMP 0x08
  142. static int send_command(struct mxl *state, u32 size, u8 *buf)
  143. {
  144. int stat;
  145. u32 val, count = 10;
  146. mutex_lock(&state->base->i2c_lock);
  147. if (state->base->fwversion > 0x02010109) {
  148. read_register_unlocked(state, DMA_I2C_INTERRUPT_ADDR, &val);
  149. if (DMA_INTR_PROT_WR_CMP & val)
  150. dev_info(state->i2cdev, "%s busy\n", __func__);
  151. while ((DMA_INTR_PROT_WR_CMP & val) && --count) {
  152. mutex_unlock(&state->base->i2c_lock);
  153. usleep_range(1000, 2000);
  154. mutex_lock(&state->base->i2c_lock);
  155. read_register_unlocked(state, DMA_I2C_INTERRUPT_ADDR,
  156. &val);
  157. }
  158. if (!count) {
  159. dev_info(state->i2cdev, "%s busy\n", __func__);
  160. mutex_unlock(&state->base->i2c_lock);
  161. return -EBUSY;
  162. }
  163. }
  164. stat = i2cwrite(state, buf, size);
  165. mutex_unlock(&state->base->i2c_lock);
  166. return stat;
  167. }
  168. static int write_register(struct mxl *state, u32 reg, u32 val)
  169. {
  170. int stat;
  171. u8 data[MXL_HYDRA_REG_WRITE_LEN] = {
  172. MXL_HYDRA_PLID_REG_WRITE, 0x08,
  173. BYTE0(reg), BYTE1(reg), BYTE2(reg), BYTE3(reg),
  174. BYTE0(val), BYTE1(val), BYTE2(val), BYTE3(val),
  175. };
  176. mutex_lock(&state->base->i2c_lock);
  177. stat = i2cwrite(state, data, sizeof(data));
  178. mutex_unlock(&state->base->i2c_lock);
  179. if (stat)
  180. dev_err(state->i2cdev, "i2c write error\n");
  181. return stat;
  182. }
  183. static int write_firmware_block(struct mxl *state,
  184. u32 reg, u32 size, u8 *reg_data_ptr)
  185. {
  186. int stat;
  187. u8 *buf = state->base->buf;
  188. mutex_lock(&state->base->i2c_lock);
  189. buf[0] = MXL_HYDRA_PLID_REG_WRITE;
  190. buf[1] = size + 4;
  191. buf[2] = GET_BYTE(reg, 0);
  192. buf[3] = GET_BYTE(reg, 1);
  193. buf[4] = GET_BYTE(reg, 2);
  194. buf[5] = GET_BYTE(reg, 3);
  195. memcpy(&buf[6], reg_data_ptr, size);
  196. stat = i2cwrite(state, buf,
  197. MXL_HYDRA_I2C_HDR_SIZE +
  198. MXL_HYDRA_REG_SIZE_IN_BYTES + size);
  199. mutex_unlock(&state->base->i2c_lock);
  200. if (stat)
  201. dev_err(state->i2cdev, "fw block write failed\n");
  202. return stat;
  203. }
  204. static int read_register(struct mxl *state, u32 reg, u32 *val)
  205. {
  206. int stat;
  207. u8 data[MXL_HYDRA_REG_SIZE_IN_BYTES + MXL_HYDRA_I2C_HDR_SIZE] = {
  208. MXL_HYDRA_PLID_REG_READ, 0x04,
  209. GET_BYTE(reg, 0), GET_BYTE(reg, 1),
  210. GET_BYTE(reg, 2), GET_BYTE(reg, 3),
  211. };
  212. mutex_lock(&state->base->i2c_lock);
  213. stat = i2cwrite(state, data,
  214. MXL_HYDRA_REG_SIZE_IN_BYTES + MXL_HYDRA_I2C_HDR_SIZE);
  215. if (stat)
  216. dev_err(state->i2cdev, "i2c read error 1\n");
  217. if (!stat)
  218. stat = i2cread(state, (u8 *) val,
  219. MXL_HYDRA_REG_SIZE_IN_BYTES);
  220. mutex_unlock(&state->base->i2c_lock);
  221. le32_to_cpus(val);
  222. if (stat)
  223. dev_err(state->i2cdev, "i2c read error 2\n");
  224. return stat;
  225. }
  226. static int read_register_block(struct mxl *state, u32 reg, u32 size, u8 *data)
  227. {
  228. int stat;
  229. u8 *buf = state->base->buf;
  230. mutex_lock(&state->base->i2c_lock);
  231. buf[0] = MXL_HYDRA_PLID_REG_READ;
  232. buf[1] = size + 4;
  233. buf[2] = GET_BYTE(reg, 0);
  234. buf[3] = GET_BYTE(reg, 1);
  235. buf[4] = GET_BYTE(reg, 2);
  236. buf[5] = GET_BYTE(reg, 3);
  237. stat = i2cwrite(state, buf,
  238. MXL_HYDRA_I2C_HDR_SIZE + MXL_HYDRA_REG_SIZE_IN_BYTES);
  239. if (!stat) {
  240. stat = i2cread(state, data, size);
  241. convert_endian(MXL_ENABLE_BIG_ENDIAN, size, data);
  242. }
  243. mutex_unlock(&state->base->i2c_lock);
  244. return stat;
  245. }
  246. static int read_by_mnemonic(struct mxl *state,
  247. u32 reg, u8 lsbloc, u8 numofbits, u32 *val)
  248. {
  249. u32 data = 0, mask = 0;
  250. int stat;
  251. stat = read_register(state, reg, &data);
  252. if (stat)
  253. return stat;
  254. mask = MXL_GET_REG_MASK_32(lsbloc, numofbits);
  255. data &= mask;
  256. data >>= lsbloc;
  257. *val = data;
  258. return 0;
  259. }
  260. static int update_by_mnemonic(struct mxl *state,
  261. u32 reg, u8 lsbloc, u8 numofbits, u32 val)
  262. {
  263. u32 data, mask;
  264. int stat;
  265. stat = read_register(state, reg, &data);
  266. if (stat)
  267. return stat;
  268. mask = MXL_GET_REG_MASK_32(lsbloc, numofbits);
  269. data = (data & ~mask) | ((val << lsbloc) & mask);
  270. stat = write_register(state, reg, data);
  271. return stat;
  272. }
  273. static int firmware_is_alive(struct mxl *state)
  274. {
  275. u32 hb0, hb1;
  276. if (read_register(state, HYDRA_HEAR_BEAT, &hb0))
  277. return 0;
  278. msleep(20);
  279. if (read_register(state, HYDRA_HEAR_BEAT, &hb1))
  280. return 0;
  281. if (hb1 == hb0)
  282. return 0;
  283. return 1;
  284. }
  285. static int init(struct dvb_frontend *fe)
  286. {
  287. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  288. /* init fe stats */
  289. p->strength.len = 1;
  290. p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  291. p->cnr.len = 1;
  292. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  293. p->pre_bit_error.len = 1;
  294. p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  295. p->pre_bit_count.len = 1;
  296. p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  297. p->post_bit_error.len = 1;
  298. p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  299. p->post_bit_count.len = 1;
  300. p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  301. return 0;
  302. }
  303. static void release(struct dvb_frontend *fe)
  304. {
  305. struct mxl *state = fe->demodulator_priv;
  306. list_del(&state->mxl);
  307. /* Release one frontend, two more shall take its place! */
  308. state->base->count--;
  309. if (state->base->count == 0) {
  310. list_del(&state->base->mxllist);
  311. kfree(state->base);
  312. }
  313. kfree(state);
  314. }
  315. static enum dvbfe_algo get_algo(struct dvb_frontend *fe)
  316. {
  317. return DVBFE_ALGO_HW;
  318. }
  319. static u32 gold2root(u32 gold)
  320. {
  321. u32 x, g, tmp = gold;
  322. if (tmp >= 0x3ffff)
  323. tmp = 0;
  324. for (g = 0, x = 1; g < tmp; g++)
  325. x = (((x ^ (x >> 7)) & 1) << 17) | (x >> 1);
  326. return x;
  327. }
  328. static int cfg_scrambler(struct mxl *state, u32 gold)
  329. {
  330. u32 root;
  331. u8 buf[26] = {
  332. MXL_HYDRA_PLID_CMD_WRITE, 24,
  333. 0, MXL_HYDRA_DEMOD_SCRAMBLE_CODE_CMD, 0, 0,
  334. state->demod, 0, 0, 0,
  335. 0, 0, 0, 0, 0, 0, 0, 0,
  336. 0, 0, 0, 0, 1, 0, 0, 0,
  337. };
  338. root = gold2root(gold);
  339. buf[25] = (root >> 24) & 0xff;
  340. buf[24] = (root >> 16) & 0xff;
  341. buf[23] = (root >> 8) & 0xff;
  342. buf[22] = root & 0xff;
  343. return send_command(state, sizeof(buf), buf);
  344. }
  345. static int cfg_demod_abort_tune(struct mxl *state)
  346. {
  347. struct MXL_HYDRA_DEMOD_ABORT_TUNE_T abort_tune_cmd;
  348. u8 cmd_size = sizeof(abort_tune_cmd);
  349. u8 cmd_buff[MXL_HYDRA_OEM_MAX_CMD_BUFF_LEN];
  350. abort_tune_cmd.demod_id = state->demod;
  351. BUILD_HYDRA_CMD(MXL_HYDRA_ABORT_TUNE_CMD, MXL_CMD_WRITE,
  352. cmd_size, &abort_tune_cmd, cmd_buff);
  353. return send_command(state, cmd_size + MXL_HYDRA_CMD_HEADER_SIZE,
  354. &cmd_buff[0]);
  355. }
  356. static int send_master_cmd(struct dvb_frontend *fe,
  357. struct dvb_diseqc_master_cmd *cmd)
  358. {
  359. /*struct mxl *state = fe->demodulator_priv;*/
  360. return 0; /*CfgDemodAbortTune(state);*/
  361. }
  362. static int set_parameters(struct dvb_frontend *fe)
  363. {
  364. struct mxl *state = fe->demodulator_priv;
  365. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  366. struct MXL_HYDRA_DEMOD_PARAM_T demod_chan_cfg;
  367. u8 cmd_size = sizeof(demod_chan_cfg);
  368. u8 cmd_buff[MXL_HYDRA_OEM_MAX_CMD_BUFF_LEN];
  369. u32 srange = 10;
  370. int stat;
  371. if (p->frequency < 950000 || p->frequency > 2150000)
  372. return -EINVAL;
  373. if (p->symbol_rate < 1000000 || p->symbol_rate > 45000000)
  374. return -EINVAL;
  375. /* CfgDemodAbortTune(state); */
  376. switch (p->delivery_system) {
  377. case SYS_DSS:
  378. demod_chan_cfg.standard = MXL_HYDRA_DSS;
  379. demod_chan_cfg.roll_off = MXL_HYDRA_ROLLOFF_AUTO;
  380. break;
  381. case SYS_DVBS:
  382. srange = p->symbol_rate / 1000000;
  383. if (srange > 10)
  384. srange = 10;
  385. demod_chan_cfg.standard = MXL_HYDRA_DVBS;
  386. demod_chan_cfg.roll_off = MXL_HYDRA_ROLLOFF_0_35;
  387. demod_chan_cfg.modulation_scheme = MXL_HYDRA_MOD_QPSK;
  388. demod_chan_cfg.pilots = MXL_HYDRA_PILOTS_OFF;
  389. break;
  390. case SYS_DVBS2:
  391. demod_chan_cfg.standard = MXL_HYDRA_DVBS2;
  392. demod_chan_cfg.roll_off = MXL_HYDRA_ROLLOFF_AUTO;
  393. demod_chan_cfg.modulation_scheme = MXL_HYDRA_MOD_AUTO;
  394. demod_chan_cfg.pilots = MXL_HYDRA_PILOTS_AUTO;
  395. cfg_scrambler(state, p->scrambling_sequence_index);
  396. break;
  397. default:
  398. return -EINVAL;
  399. }
  400. demod_chan_cfg.tuner_index = state->tuner;
  401. demod_chan_cfg.demod_index = state->demod;
  402. demod_chan_cfg.frequency_in_hz = p->frequency * 1000;
  403. demod_chan_cfg.symbol_rate_in_hz = p->symbol_rate;
  404. demod_chan_cfg.max_carrier_offset_in_mhz = srange;
  405. demod_chan_cfg.spectrum_inversion = MXL_HYDRA_SPECTRUM_AUTO;
  406. demod_chan_cfg.fec_code_rate = MXL_HYDRA_FEC_AUTO;
  407. mutex_lock(&state->base->tune_lock);
  408. if (time_after(jiffies + msecs_to_jiffies(200),
  409. state->base->next_tune))
  410. while (time_before(jiffies, state->base->next_tune))
  411. usleep_range(10000, 11000);
  412. state->base->next_tune = jiffies + msecs_to_jiffies(100);
  413. state->tuner_in_use = state->tuner;
  414. BUILD_HYDRA_CMD(MXL_HYDRA_DEMOD_SET_PARAM_CMD, MXL_CMD_WRITE,
  415. cmd_size, &demod_chan_cfg, cmd_buff);
  416. stat = send_command(state, cmd_size + MXL_HYDRA_CMD_HEADER_SIZE,
  417. &cmd_buff[0]);
  418. mutex_unlock(&state->base->tune_lock);
  419. return stat;
  420. }
  421. static int enable_tuner(struct mxl *state, u32 tuner, u32 enable);
  422. static int sleep(struct dvb_frontend *fe)
  423. {
  424. struct mxl *state = fe->demodulator_priv;
  425. struct mxl *p;
  426. cfg_demod_abort_tune(state);
  427. if (state->tuner_in_use != 0xffffffff) {
  428. mutex_lock(&state->base->tune_lock);
  429. state->tuner_in_use = 0xffffffff;
  430. list_for_each_entry(p, &state->base->mxls, mxl) {
  431. if (p->tuner_in_use == state->tuner)
  432. break;
  433. }
  434. if (&p->mxl == &state->base->mxls)
  435. enable_tuner(state, state->tuner, 0);
  436. mutex_unlock(&state->base->tune_lock);
  437. }
  438. return 0;
  439. }
  440. static int read_snr(struct dvb_frontend *fe)
  441. {
  442. struct mxl *state = fe->demodulator_priv;
  443. int stat;
  444. u32 reg_data = 0;
  445. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  446. mutex_lock(&state->base->status_lock);
  447. HYDRA_DEMOD_STATUS_LOCK(state, state->demod);
  448. stat = read_register(state, (HYDRA_DMD_SNR_ADDR_OFFSET +
  449. HYDRA_DMD_STATUS_OFFSET(state->demod)),
  450. &reg_data);
  451. HYDRA_DEMOD_STATUS_UNLOCK(state, state->demod);
  452. mutex_unlock(&state->base->status_lock);
  453. p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  454. p->cnr.stat[0].svalue = (s16)reg_data * 10;
  455. return stat;
  456. }
  457. static int read_ber(struct dvb_frontend *fe)
  458. {
  459. struct mxl *state = fe->demodulator_priv;
  460. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  461. u32 reg[8];
  462. mutex_lock(&state->base->status_lock);
  463. HYDRA_DEMOD_STATUS_LOCK(state, state->demod);
  464. read_register_block(state,
  465. (HYDRA_DMD_DVBS_1ST_CORR_RS_ERRORS_ADDR_OFFSET +
  466. HYDRA_DMD_STATUS_OFFSET(state->demod)),
  467. (4 * sizeof(u32)),
  468. (u8 *) &reg[0]);
  469. HYDRA_DEMOD_STATUS_UNLOCK(state, state->demod);
  470. switch (p->delivery_system) {
  471. case SYS_DSS:
  472. case SYS_DVBS:
  473. p->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  474. p->pre_bit_error.stat[0].uvalue = reg[2];
  475. p->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  476. p->pre_bit_count.stat[0].uvalue = reg[3];
  477. break;
  478. default:
  479. break;
  480. }
  481. read_register_block(state,
  482. (HYDRA_DMD_DVBS2_CRC_ERRORS_ADDR_OFFSET +
  483. HYDRA_DMD_STATUS_OFFSET(state->demod)),
  484. (7 * sizeof(u32)),
  485. (u8 *) &reg[0]);
  486. switch (p->delivery_system) {
  487. case SYS_DSS:
  488. case SYS_DVBS:
  489. p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  490. p->post_bit_error.stat[0].uvalue = reg[5];
  491. p->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  492. p->post_bit_count.stat[0].uvalue = reg[6];
  493. break;
  494. case SYS_DVBS2:
  495. p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  496. p->post_bit_error.stat[0].uvalue = reg[1];
  497. p->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  498. p->post_bit_count.stat[0].uvalue = reg[2];
  499. break;
  500. default:
  501. break;
  502. }
  503. mutex_unlock(&state->base->status_lock);
  504. return 0;
  505. }
  506. static int read_signal_strength(struct dvb_frontend *fe)
  507. {
  508. struct mxl *state = fe->demodulator_priv;
  509. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  510. int stat;
  511. u32 reg_data = 0;
  512. mutex_lock(&state->base->status_lock);
  513. HYDRA_DEMOD_STATUS_LOCK(state, state->demod);
  514. stat = read_register(state, (HYDRA_DMD_STATUS_INPUT_POWER_ADDR +
  515. HYDRA_DMD_STATUS_OFFSET(state->demod)),
  516. &reg_data);
  517. HYDRA_DEMOD_STATUS_UNLOCK(state, state->demod);
  518. mutex_unlock(&state->base->status_lock);
  519. p->strength.stat[0].scale = FE_SCALE_DECIBEL;
  520. p->strength.stat[0].svalue = (s16) reg_data * 10; /* fix scale */
  521. return stat;
  522. }
  523. static int read_status(struct dvb_frontend *fe, enum fe_status *status)
  524. {
  525. struct mxl *state = fe->demodulator_priv;
  526. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  527. u32 reg_data = 0;
  528. mutex_lock(&state->base->status_lock);
  529. HYDRA_DEMOD_STATUS_LOCK(state, state->demod);
  530. read_register(state, (HYDRA_DMD_LOCK_STATUS_ADDR_OFFSET +
  531. HYDRA_DMD_STATUS_OFFSET(state->demod)),
  532. &reg_data);
  533. HYDRA_DEMOD_STATUS_UNLOCK(state, state->demod);
  534. mutex_unlock(&state->base->status_lock);
  535. *status = (reg_data == 1) ? 0x1f : 0;
  536. /* signal statistics */
  537. /* signal strength is always available */
  538. read_signal_strength(fe);
  539. if (*status & FE_HAS_CARRIER)
  540. read_snr(fe);
  541. else
  542. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  543. if (*status & FE_HAS_SYNC)
  544. read_ber(fe);
  545. else {
  546. p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  547. p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  548. p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  549. p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  550. }
  551. return 0;
  552. }
  553. static int tune(struct dvb_frontend *fe, bool re_tune,
  554. unsigned int mode_flags,
  555. unsigned int *delay, enum fe_status *status)
  556. {
  557. struct mxl *state = fe->demodulator_priv;
  558. int r = 0;
  559. *delay = HZ / 2;
  560. if (re_tune) {
  561. r = set_parameters(fe);
  562. if (r)
  563. return r;
  564. state->tune_time = jiffies;
  565. }
  566. return read_status(fe, status);
  567. }
  568. static enum fe_code_rate conv_fec(enum MXL_HYDRA_FEC_E fec)
  569. {
  570. enum fe_code_rate fec2fec[11] = {
  571. FEC_NONE, FEC_1_2, FEC_3_5, FEC_2_3,
  572. FEC_3_4, FEC_4_5, FEC_5_6, FEC_6_7,
  573. FEC_7_8, FEC_8_9, FEC_9_10
  574. };
  575. if (fec > MXL_HYDRA_FEC_9_10)
  576. return FEC_NONE;
  577. return fec2fec[fec];
  578. }
  579. static int get_frontend(struct dvb_frontend *fe,
  580. struct dtv_frontend_properties *p)
  581. {
  582. struct mxl *state = fe->demodulator_priv;
  583. u32 reg_data[MXL_DEMOD_CHAN_PARAMS_BUFF_SIZE];
  584. u32 freq;
  585. mutex_lock(&state->base->status_lock);
  586. HYDRA_DEMOD_STATUS_LOCK(state, state->demod);
  587. read_register_block(state,
  588. (HYDRA_DMD_STANDARD_ADDR_OFFSET +
  589. HYDRA_DMD_STATUS_OFFSET(state->demod)),
  590. (MXL_DEMOD_CHAN_PARAMS_BUFF_SIZE * 4), /* 25 * 4 bytes */
  591. (u8 *) &reg_data[0]);
  592. /* read demod channel parameters */
  593. read_register_block(state,
  594. (HYDRA_DMD_STATUS_CENTER_FREQ_IN_KHZ_ADDR +
  595. HYDRA_DMD_STATUS_OFFSET(state->demod)),
  596. (4), /* 4 bytes */
  597. (u8 *) &freq);
  598. HYDRA_DEMOD_STATUS_UNLOCK(state, state->demod);
  599. mutex_unlock(&state->base->status_lock);
  600. dev_dbg(state->i2cdev, "freq=%u delsys=%u srate=%u\n",
  601. freq * 1000, reg_data[DMD_STANDARD_ADDR],
  602. reg_data[DMD_SYMBOL_RATE_ADDR]);
  603. p->symbol_rate = reg_data[DMD_SYMBOL_RATE_ADDR];
  604. p->frequency = freq;
  605. /*
  606. * p->delivery_system =
  607. * (MXL_HYDRA_BCAST_STD_E) regData[DMD_STANDARD_ADDR];
  608. * p->inversion =
  609. * (MXL_HYDRA_SPECTRUM_E) regData[DMD_SPECTRUM_INVERSION_ADDR];
  610. * freqSearchRangeKHz =
  611. * (regData[DMD_FREQ_SEARCH_RANGE_IN_KHZ_ADDR]);
  612. */
  613. p->fec_inner = conv_fec(reg_data[DMD_FEC_CODE_RATE_ADDR]);
  614. switch (p->delivery_system) {
  615. case SYS_DSS:
  616. break;
  617. case SYS_DVBS2:
  618. switch ((enum MXL_HYDRA_PILOTS_E)
  619. reg_data[DMD_DVBS2_PILOT_ON_OFF_ADDR]) {
  620. case MXL_HYDRA_PILOTS_OFF:
  621. p->pilot = PILOT_OFF;
  622. break;
  623. case MXL_HYDRA_PILOTS_ON:
  624. p->pilot = PILOT_ON;
  625. break;
  626. default:
  627. break;
  628. }
  629. fallthrough;
  630. case SYS_DVBS:
  631. switch ((enum MXL_HYDRA_MODULATION_E)
  632. reg_data[DMD_MODULATION_SCHEME_ADDR]) {
  633. case MXL_HYDRA_MOD_QPSK:
  634. p->modulation = QPSK;
  635. break;
  636. case MXL_HYDRA_MOD_8PSK:
  637. p->modulation = PSK_8;
  638. break;
  639. default:
  640. break;
  641. }
  642. switch ((enum MXL_HYDRA_ROLLOFF_E)
  643. reg_data[DMD_SPECTRUM_ROLL_OFF_ADDR]) {
  644. case MXL_HYDRA_ROLLOFF_0_20:
  645. p->rolloff = ROLLOFF_20;
  646. break;
  647. case MXL_HYDRA_ROLLOFF_0_35:
  648. p->rolloff = ROLLOFF_35;
  649. break;
  650. case MXL_HYDRA_ROLLOFF_0_25:
  651. p->rolloff = ROLLOFF_25;
  652. break;
  653. default:
  654. break;
  655. }
  656. break;
  657. default:
  658. return -EINVAL;
  659. }
  660. return 0;
  661. }
  662. static int set_input(struct dvb_frontend *fe, int input)
  663. {
  664. struct mxl *state = fe->demodulator_priv;
  665. state->tuner = input;
  666. return 0;
  667. }
  668. static const struct dvb_frontend_ops mxl_ops = {
  669. .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
  670. .info = {
  671. .name = "MaxLinear MxL5xx DVB-S/S2 tuner-demodulator",
  672. .frequency_min_hz = 300 * MHz,
  673. .frequency_max_hz = 2350 * MHz,
  674. .symbol_rate_min = 1000000,
  675. .symbol_rate_max = 45000000,
  676. .caps = FE_CAN_INVERSION_AUTO |
  677. FE_CAN_FEC_AUTO |
  678. FE_CAN_QPSK |
  679. FE_CAN_2G_MODULATION
  680. },
  681. .init = init,
  682. .release = release,
  683. .get_frontend_algo = get_algo,
  684. .tune = tune,
  685. .read_status = read_status,
  686. .sleep = sleep,
  687. .get_frontend = get_frontend,
  688. .diseqc_send_master_cmd = send_master_cmd,
  689. };
  690. static struct mxl_base *match_base(struct i2c_adapter *i2c, u8 adr)
  691. {
  692. struct mxl_base *p;
  693. list_for_each_entry(p, &mxllist, mxllist)
  694. if (p->i2c == i2c && p->adr == adr)
  695. return p;
  696. return NULL;
  697. }
  698. static void cfg_dev_xtal(struct mxl *state, u32 freq, u32 cap, u32 enable)
  699. {
  700. if (state->base->can_clkout || !enable)
  701. update_by_mnemonic(state, 0x90200054, 23, 1, enable);
  702. if (freq == 24000000)
  703. write_register(state, HYDRA_CRYSTAL_SETTING, 0);
  704. else
  705. write_register(state, HYDRA_CRYSTAL_SETTING, 1);
  706. write_register(state, HYDRA_CRYSTAL_CAP, cap);
  707. }
  708. static u32 get_big_endian(u8 num_of_bits, const u8 buf[])
  709. {
  710. u32 ret_value = 0;
  711. switch (num_of_bits) {
  712. case 24:
  713. ret_value = (((u32) buf[0]) << 16) |
  714. (((u32) buf[1]) << 8) | buf[2];
  715. break;
  716. case 32:
  717. ret_value = (((u32) buf[0]) << 24) |
  718. (((u32) buf[1]) << 16) |
  719. (((u32) buf[2]) << 8) | buf[3];
  720. break;
  721. default:
  722. break;
  723. }
  724. return ret_value;
  725. }
  726. static int write_fw_segment(struct mxl *state,
  727. u32 mem_addr, u32 total_size, u8 *data_ptr)
  728. {
  729. int status;
  730. u32 data_count = 0;
  731. u32 size = 0;
  732. u32 orig_size = 0;
  733. u8 *w_buf_ptr = NULL;
  734. u32 block_size = ((MXL_HYDRA_OEM_MAX_BLOCK_WRITE_LENGTH -
  735. (MXL_HYDRA_I2C_HDR_SIZE +
  736. MXL_HYDRA_REG_SIZE_IN_BYTES)) / 4) * 4;
  737. u8 w_msg_buffer[MXL_HYDRA_OEM_MAX_BLOCK_WRITE_LENGTH -
  738. (MXL_HYDRA_I2C_HDR_SIZE + MXL_HYDRA_REG_SIZE_IN_BYTES)];
  739. do {
  740. size = orig_size = (((u32)(data_count + block_size)) > total_size) ?
  741. (total_size - data_count) : block_size;
  742. if (orig_size & 3)
  743. size = (orig_size + 4) & ~3;
  744. w_buf_ptr = &w_msg_buffer[0];
  745. memset((void *) w_buf_ptr, 0, size);
  746. memcpy((void *) w_buf_ptr, (void *) data_ptr, orig_size);
  747. convert_endian(1, size, w_buf_ptr);
  748. status = write_firmware_block(state, mem_addr, size, w_buf_ptr);
  749. if (status)
  750. return status;
  751. data_count += size;
  752. mem_addr += size;
  753. data_ptr += size;
  754. } while (data_count < total_size);
  755. return status;
  756. }
  757. static int do_firmware_download(struct mxl *state, u8 *mbin_buffer_ptr,
  758. u32 mbin_buffer_size)
  759. {
  760. int status;
  761. u32 index = 0;
  762. u32 seg_length = 0;
  763. u32 seg_address = 0;
  764. struct MBIN_FILE_T *mbin_ptr = (struct MBIN_FILE_T *)mbin_buffer_ptr;
  765. struct MBIN_SEGMENT_T *segment_ptr;
  766. enum MXL_BOOL_E xcpu_fw_flag = MXL_FALSE;
  767. if (mbin_ptr->header.id != MBIN_FILE_HEADER_ID) {
  768. dev_err(state->i2cdev, "%s: Invalid file header ID (%c)\n",
  769. __func__, mbin_ptr->header.id);
  770. return -EINVAL;
  771. }
  772. status = write_register(state, FW_DL_SIGN_ADDR, 0);
  773. if (status)
  774. return status;
  775. segment_ptr = (struct MBIN_SEGMENT_T *) (&mbin_ptr->data[0]);
  776. for (index = 0; index < mbin_ptr->header.num_segments; index++) {
  777. if (segment_ptr->header.id != MBIN_SEGMENT_HEADER_ID) {
  778. dev_err(state->i2cdev, "%s: Invalid segment header ID (%c)\n",
  779. __func__, segment_ptr->header.id);
  780. return -EINVAL;
  781. }
  782. seg_length = get_big_endian(24,
  783. &(segment_ptr->header.len24[0]));
  784. seg_address = get_big_endian(32,
  785. &(segment_ptr->header.address[0]));
  786. if (state->base->type == MXL_HYDRA_DEVICE_568) {
  787. if ((((seg_address & 0x90760000) == 0x90760000) ||
  788. ((seg_address & 0x90740000) == 0x90740000)) &&
  789. (xcpu_fw_flag == MXL_FALSE)) {
  790. update_by_mnemonic(state, 0x8003003C, 0, 1, 1);
  791. msleep(200);
  792. write_register(state, 0x90720000, 0);
  793. usleep_range(10000, 11000);
  794. xcpu_fw_flag = MXL_TRUE;
  795. }
  796. status = write_fw_segment(state, seg_address,
  797. seg_length,
  798. (u8 *) segment_ptr->data);
  799. } else {
  800. if (((seg_address & 0x90760000) != 0x90760000) &&
  801. ((seg_address & 0x90740000) != 0x90740000))
  802. status = write_fw_segment(state, seg_address,
  803. seg_length, (u8 *) segment_ptr->data);
  804. }
  805. if (status)
  806. return status;
  807. segment_ptr = (struct MBIN_SEGMENT_T *)
  808. &(segment_ptr->data[((seg_length + 3) / 4) * 4]);
  809. }
  810. return status;
  811. }
  812. static int check_fw(struct mxl *state, u8 *mbin, u32 mbin_len)
  813. {
  814. struct MBIN_FILE_HEADER_T *fh = (struct MBIN_FILE_HEADER_T *) mbin;
  815. u32 flen = (fh->image_size24[0] << 16) |
  816. (fh->image_size24[1] << 8) | fh->image_size24[2];
  817. u8 *fw, cs = 0;
  818. u32 i;
  819. if (fh->id != 'M' || fh->fmt_version != '1' || flen > 0x3FFF0) {
  820. dev_info(state->i2cdev, "Invalid FW Header\n");
  821. return -1;
  822. }
  823. fw = mbin + sizeof(struct MBIN_FILE_HEADER_T);
  824. for (i = 0; i < flen; i += 1)
  825. cs += fw[i];
  826. if (cs != fh->image_checksum) {
  827. dev_info(state->i2cdev, "Invalid FW Checksum\n");
  828. return -1;
  829. }
  830. return 0;
  831. }
  832. static int firmware_download(struct mxl *state, u8 *mbin, u32 mbin_len)
  833. {
  834. int status;
  835. u32 reg_data = 0;
  836. struct MXL_HYDRA_SKU_COMMAND_T dev_sku_cfg;
  837. u8 cmd_size = sizeof(struct MXL_HYDRA_SKU_COMMAND_T);
  838. u8 cmd_buff[sizeof(struct MXL_HYDRA_SKU_COMMAND_T) + 6];
  839. if (check_fw(state, mbin, mbin_len))
  840. return -1;
  841. /* put CPU into reset */
  842. status = update_by_mnemonic(state, 0x8003003C, 0, 1, 0);
  843. if (status)
  844. return status;
  845. usleep_range(1000, 2000);
  846. /* Reset TX FIFO's, BBAND, XBAR */
  847. status = write_register(state, HYDRA_RESET_TRANSPORT_FIFO_REG,
  848. HYDRA_RESET_TRANSPORT_FIFO_DATA);
  849. if (status)
  850. return status;
  851. status = write_register(state, HYDRA_RESET_BBAND_REG,
  852. HYDRA_RESET_BBAND_DATA);
  853. if (status)
  854. return status;
  855. status = write_register(state, HYDRA_RESET_XBAR_REG,
  856. HYDRA_RESET_XBAR_DATA);
  857. if (status)
  858. return status;
  859. /* Disable clock to Baseband, Wideband, SerDes,
  860. * Alias ext & Transport modules
  861. */
  862. status = write_register(state, HYDRA_MODULES_CLK_2_REG,
  863. HYDRA_DISABLE_CLK_2);
  864. if (status)
  865. return status;
  866. /* Clear Software & Host interrupt status - (Clear on read) */
  867. status = read_register(state, HYDRA_PRCM_ROOT_CLK_REG, &reg_data);
  868. if (status)
  869. return status;
  870. status = do_firmware_download(state, mbin, mbin_len);
  871. if (status)
  872. return status;
  873. if (state->base->type == MXL_HYDRA_DEVICE_568) {
  874. usleep_range(10000, 11000);
  875. /* bring XCPU out of reset */
  876. status = write_register(state, 0x90720000, 1);
  877. if (status)
  878. return status;
  879. msleep(500);
  880. /* Enable XCPU UART message processing in MCPU */
  881. status = write_register(state, 0x9076B510, 1);
  882. if (status)
  883. return status;
  884. } else {
  885. /* Bring CPU out of reset */
  886. status = update_by_mnemonic(state, 0x8003003C, 0, 1, 1);
  887. if (status)
  888. return status;
  889. /* Wait until FW boots */
  890. msleep(150);
  891. }
  892. /* Initialize XPT XBAR */
  893. status = write_register(state, XPT_DMD0_BASEADDR, 0x76543210);
  894. if (status)
  895. return status;
  896. if (!firmware_is_alive(state))
  897. return -1;
  898. dev_info(state->i2cdev, "Hydra FW alive. Hail!\n");
  899. /* sometimes register values are wrong shortly
  900. * after first heart beats
  901. */
  902. msleep(50);
  903. dev_sku_cfg.sku_type = state->base->sku_type;
  904. BUILD_HYDRA_CMD(MXL_HYDRA_DEV_CFG_SKU_CMD, MXL_CMD_WRITE,
  905. cmd_size, &dev_sku_cfg, cmd_buff);
  906. status = send_command(state, cmd_size + MXL_HYDRA_CMD_HEADER_SIZE,
  907. &cmd_buff[0]);
  908. return status;
  909. }
  910. static int cfg_ts_pad_mux(struct mxl *state, enum MXL_BOOL_E enable_serial_ts)
  911. {
  912. int status = 0;
  913. u32 pad_mux_value = 0;
  914. if (enable_serial_ts == MXL_TRUE) {
  915. pad_mux_value = 0;
  916. if ((state->base->type == MXL_HYDRA_DEVICE_541) ||
  917. (state->base->type == MXL_HYDRA_DEVICE_541S))
  918. pad_mux_value = 2;
  919. } else {
  920. if ((state->base->type == MXL_HYDRA_DEVICE_581) ||
  921. (state->base->type == MXL_HYDRA_DEVICE_581S))
  922. pad_mux_value = 2;
  923. else
  924. pad_mux_value = 3;
  925. }
  926. switch (state->base->type) {
  927. case MXL_HYDRA_DEVICE_561:
  928. case MXL_HYDRA_DEVICE_581:
  929. case MXL_HYDRA_DEVICE_541:
  930. case MXL_HYDRA_DEVICE_541S:
  931. case MXL_HYDRA_DEVICE_561S:
  932. case MXL_HYDRA_DEVICE_581S:
  933. status |= update_by_mnemonic(state, 0x90000170, 24, 3,
  934. pad_mux_value);
  935. status |= update_by_mnemonic(state, 0x90000170, 28, 3,
  936. pad_mux_value);
  937. status |= update_by_mnemonic(state, 0x90000174, 0, 3,
  938. pad_mux_value);
  939. status |= update_by_mnemonic(state, 0x90000174, 4, 3,
  940. pad_mux_value);
  941. status |= update_by_mnemonic(state, 0x90000174, 8, 3,
  942. pad_mux_value);
  943. status |= update_by_mnemonic(state, 0x90000174, 12, 3,
  944. pad_mux_value);
  945. status |= update_by_mnemonic(state, 0x90000174, 16, 3,
  946. pad_mux_value);
  947. status |= update_by_mnemonic(state, 0x90000174, 20, 3,
  948. pad_mux_value);
  949. status |= update_by_mnemonic(state, 0x90000174, 24, 3,
  950. pad_mux_value);
  951. status |= update_by_mnemonic(state, 0x90000174, 28, 3,
  952. pad_mux_value);
  953. status |= update_by_mnemonic(state, 0x90000178, 0, 3,
  954. pad_mux_value);
  955. status |= update_by_mnemonic(state, 0x90000178, 4, 3,
  956. pad_mux_value);
  957. status |= update_by_mnemonic(state, 0x90000178, 8, 3,
  958. pad_mux_value);
  959. break;
  960. case MXL_HYDRA_DEVICE_544:
  961. case MXL_HYDRA_DEVICE_542:
  962. status |= update_by_mnemonic(state, 0x9000016C, 4, 3, 1);
  963. status |= update_by_mnemonic(state, 0x9000016C, 8, 3, 0);
  964. status |= update_by_mnemonic(state, 0x9000016C, 12, 3, 0);
  965. status |= update_by_mnemonic(state, 0x9000016C, 16, 3, 0);
  966. status |= update_by_mnemonic(state, 0x90000170, 0, 3, 0);
  967. status |= update_by_mnemonic(state, 0x90000178, 12, 3, 1);
  968. status |= update_by_mnemonic(state, 0x90000178, 16, 3, 1);
  969. status |= update_by_mnemonic(state, 0x90000178, 20, 3, 1);
  970. status |= update_by_mnemonic(state, 0x90000178, 24, 3, 1);
  971. status |= update_by_mnemonic(state, 0x9000017C, 0, 3, 1);
  972. status |= update_by_mnemonic(state, 0x9000017C, 4, 3, 1);
  973. if (enable_serial_ts == MXL_ENABLE) {
  974. status |= update_by_mnemonic(state,
  975. 0x90000170, 4, 3, 0);
  976. status |= update_by_mnemonic(state,
  977. 0x90000170, 8, 3, 0);
  978. status |= update_by_mnemonic(state,
  979. 0x90000170, 12, 3, 0);
  980. status |= update_by_mnemonic(state,
  981. 0x90000170, 16, 3, 0);
  982. status |= update_by_mnemonic(state,
  983. 0x90000170, 20, 3, 1);
  984. status |= update_by_mnemonic(state,
  985. 0x90000170, 24, 3, 1);
  986. status |= update_by_mnemonic(state,
  987. 0x90000170, 28, 3, 2);
  988. status |= update_by_mnemonic(state,
  989. 0x90000174, 0, 3, 2);
  990. status |= update_by_mnemonic(state,
  991. 0x90000174, 4, 3, 2);
  992. status |= update_by_mnemonic(state,
  993. 0x90000174, 8, 3, 2);
  994. status |= update_by_mnemonic(state,
  995. 0x90000174, 12, 3, 2);
  996. status |= update_by_mnemonic(state,
  997. 0x90000174, 16, 3, 2);
  998. status |= update_by_mnemonic(state,
  999. 0x90000174, 20, 3, 2);
  1000. status |= update_by_mnemonic(state,
  1001. 0x90000174, 24, 3, 2);
  1002. status |= update_by_mnemonic(state,
  1003. 0x90000174, 28, 3, 2);
  1004. status |= update_by_mnemonic(state,
  1005. 0x90000178, 0, 3, 2);
  1006. status |= update_by_mnemonic(state,
  1007. 0x90000178, 4, 3, 2);
  1008. status |= update_by_mnemonic(state,
  1009. 0x90000178, 8, 3, 2);
  1010. } else {
  1011. status |= update_by_mnemonic(state,
  1012. 0x90000170, 4, 3, 3);
  1013. status |= update_by_mnemonic(state,
  1014. 0x90000170, 8, 3, 3);
  1015. status |= update_by_mnemonic(state,
  1016. 0x90000170, 12, 3, 3);
  1017. status |= update_by_mnemonic(state,
  1018. 0x90000170, 16, 3, 3);
  1019. status |= update_by_mnemonic(state,
  1020. 0x90000170, 20, 3, 3);
  1021. status |= update_by_mnemonic(state,
  1022. 0x90000170, 24, 3, 3);
  1023. status |= update_by_mnemonic(state,
  1024. 0x90000170, 28, 3, 3);
  1025. status |= update_by_mnemonic(state,
  1026. 0x90000174, 0, 3, 3);
  1027. status |= update_by_mnemonic(state,
  1028. 0x90000174, 4, 3, 3);
  1029. status |= update_by_mnemonic(state,
  1030. 0x90000174, 8, 3, 3);
  1031. status |= update_by_mnemonic(state,
  1032. 0x90000174, 12, 3, 3);
  1033. status |= update_by_mnemonic(state,
  1034. 0x90000174, 16, 3, 3);
  1035. status |= update_by_mnemonic(state,
  1036. 0x90000174, 20, 3, 1);
  1037. status |= update_by_mnemonic(state,
  1038. 0x90000174, 24, 3, 1);
  1039. status |= update_by_mnemonic(state,
  1040. 0x90000174, 28, 3, 1);
  1041. status |= update_by_mnemonic(state,
  1042. 0x90000178, 0, 3, 1);
  1043. status |= update_by_mnemonic(state,
  1044. 0x90000178, 4, 3, 1);
  1045. status |= update_by_mnemonic(state,
  1046. 0x90000178, 8, 3, 1);
  1047. }
  1048. break;
  1049. case MXL_HYDRA_DEVICE_568:
  1050. if (enable_serial_ts == MXL_FALSE) {
  1051. status |= update_by_mnemonic(state,
  1052. 0x9000016C, 8, 3, 5);
  1053. status |= update_by_mnemonic(state,
  1054. 0x9000016C, 12, 3, 5);
  1055. status |= update_by_mnemonic(state,
  1056. 0x9000016C, 16, 3, 5);
  1057. status |= update_by_mnemonic(state,
  1058. 0x9000016C, 20, 3, 5);
  1059. status |= update_by_mnemonic(state,
  1060. 0x9000016C, 24, 3, 5);
  1061. status |= update_by_mnemonic(state,
  1062. 0x9000016C, 28, 3, 5);
  1063. status |= update_by_mnemonic(state,
  1064. 0x90000170, 0, 3, 5);
  1065. status |= update_by_mnemonic(state,
  1066. 0x90000170, 4, 3, 5);
  1067. status |= update_by_mnemonic(state,
  1068. 0x90000170, 8, 3, 5);
  1069. status |= update_by_mnemonic(state,
  1070. 0x90000170, 12, 3, 5);
  1071. status |= update_by_mnemonic(state,
  1072. 0x90000170, 16, 3, 5);
  1073. status |= update_by_mnemonic(state,
  1074. 0x90000170, 20, 3, 5);
  1075. status |= update_by_mnemonic(state,
  1076. 0x90000170, 24, 3, pad_mux_value);
  1077. status |= update_by_mnemonic(state,
  1078. 0x90000174, 0, 3, pad_mux_value);
  1079. status |= update_by_mnemonic(state,
  1080. 0x90000174, 4, 3, pad_mux_value);
  1081. status |= update_by_mnemonic(state,
  1082. 0x90000174, 8, 3, pad_mux_value);
  1083. status |= update_by_mnemonic(state,
  1084. 0x90000174, 12, 3, pad_mux_value);
  1085. status |= update_by_mnemonic(state,
  1086. 0x90000174, 16, 3, pad_mux_value);
  1087. status |= update_by_mnemonic(state,
  1088. 0x90000174, 20, 3, pad_mux_value);
  1089. status |= update_by_mnemonic(state,
  1090. 0x90000174, 24, 3, pad_mux_value);
  1091. status |= update_by_mnemonic(state,
  1092. 0x90000174, 28, 3, pad_mux_value);
  1093. status |= update_by_mnemonic(state,
  1094. 0x90000178, 0, 3, pad_mux_value);
  1095. status |= update_by_mnemonic(state,
  1096. 0x90000178, 4, 3, pad_mux_value);
  1097. status |= update_by_mnemonic(state,
  1098. 0x90000178, 8, 3, 5);
  1099. status |= update_by_mnemonic(state,
  1100. 0x90000178, 12, 3, 5);
  1101. status |= update_by_mnemonic(state,
  1102. 0x90000178, 16, 3, 5);
  1103. status |= update_by_mnemonic(state,
  1104. 0x90000178, 20, 3, 5);
  1105. status |= update_by_mnemonic(state,
  1106. 0x90000178, 24, 3, 5);
  1107. status |= update_by_mnemonic(state,
  1108. 0x90000178, 28, 3, 5);
  1109. status |= update_by_mnemonic(state,
  1110. 0x9000017C, 0, 3, 5);
  1111. status |= update_by_mnemonic(state,
  1112. 0x9000017C, 4, 3, 5);
  1113. } else {
  1114. status |= update_by_mnemonic(state,
  1115. 0x90000170, 4, 3, pad_mux_value);
  1116. status |= update_by_mnemonic(state,
  1117. 0x90000170, 8, 3, pad_mux_value);
  1118. status |= update_by_mnemonic(state,
  1119. 0x90000170, 12, 3, pad_mux_value);
  1120. status |= update_by_mnemonic(state,
  1121. 0x90000170, 16, 3, pad_mux_value);
  1122. status |= update_by_mnemonic(state,
  1123. 0x90000170, 20, 3, pad_mux_value);
  1124. status |= update_by_mnemonic(state,
  1125. 0x90000170, 24, 3, pad_mux_value);
  1126. status |= update_by_mnemonic(state,
  1127. 0x90000170, 28, 3, pad_mux_value);
  1128. status |= update_by_mnemonic(state,
  1129. 0x90000174, 0, 3, pad_mux_value);
  1130. status |= update_by_mnemonic(state,
  1131. 0x90000174, 4, 3, pad_mux_value);
  1132. status |= update_by_mnemonic(state,
  1133. 0x90000174, 8, 3, pad_mux_value);
  1134. status |= update_by_mnemonic(state,
  1135. 0x90000174, 12, 3, pad_mux_value);
  1136. }
  1137. break;
  1138. case MXL_HYDRA_DEVICE_584:
  1139. default:
  1140. status |= update_by_mnemonic(state,
  1141. 0x90000170, 4, 3, pad_mux_value);
  1142. status |= update_by_mnemonic(state,
  1143. 0x90000170, 8, 3, pad_mux_value);
  1144. status |= update_by_mnemonic(state,
  1145. 0x90000170, 12, 3, pad_mux_value);
  1146. status |= update_by_mnemonic(state,
  1147. 0x90000170, 16, 3, pad_mux_value);
  1148. status |= update_by_mnemonic(state,
  1149. 0x90000170, 20, 3, pad_mux_value);
  1150. status |= update_by_mnemonic(state,
  1151. 0x90000170, 24, 3, pad_mux_value);
  1152. status |= update_by_mnemonic(state,
  1153. 0x90000170, 28, 3, pad_mux_value);
  1154. status |= update_by_mnemonic(state,
  1155. 0x90000174, 0, 3, pad_mux_value);
  1156. status |= update_by_mnemonic(state,
  1157. 0x90000174, 4, 3, pad_mux_value);
  1158. status |= update_by_mnemonic(state,
  1159. 0x90000174, 8, 3, pad_mux_value);
  1160. status |= update_by_mnemonic(state,
  1161. 0x90000174, 12, 3, pad_mux_value);
  1162. break;
  1163. }
  1164. return status;
  1165. }
  1166. static int set_drive_strength(struct mxl *state,
  1167. enum MXL_HYDRA_TS_DRIVE_STRENGTH_E ts_drive_strength)
  1168. {
  1169. int stat = 0;
  1170. u32 val;
  1171. read_register(state, 0x90000194, &val);
  1172. dev_info(state->i2cdev, "DIGIO = %08x\n", val);
  1173. dev_info(state->i2cdev, "set drive_strength = %u\n", ts_drive_strength);
  1174. stat |= update_by_mnemonic(state, 0x90000194, 0, 3, ts_drive_strength);
  1175. stat |= update_by_mnemonic(state, 0x90000194, 20, 3, ts_drive_strength);
  1176. stat |= update_by_mnemonic(state, 0x90000194, 24, 3, ts_drive_strength);
  1177. stat |= update_by_mnemonic(state, 0x90000198, 12, 3, ts_drive_strength);
  1178. stat |= update_by_mnemonic(state, 0x90000198, 16, 3, ts_drive_strength);
  1179. stat |= update_by_mnemonic(state, 0x90000198, 20, 3, ts_drive_strength);
  1180. stat |= update_by_mnemonic(state, 0x90000198, 24, 3, ts_drive_strength);
  1181. stat |= update_by_mnemonic(state, 0x9000019C, 0, 3, ts_drive_strength);
  1182. stat |= update_by_mnemonic(state, 0x9000019C, 4, 3, ts_drive_strength);
  1183. stat |= update_by_mnemonic(state, 0x9000019C, 8, 3, ts_drive_strength);
  1184. stat |= update_by_mnemonic(state, 0x9000019C, 24, 3, ts_drive_strength);
  1185. stat |= update_by_mnemonic(state, 0x9000019C, 28, 3, ts_drive_strength);
  1186. stat |= update_by_mnemonic(state, 0x900001A0, 0, 3, ts_drive_strength);
  1187. stat |= update_by_mnemonic(state, 0x900001A0, 4, 3, ts_drive_strength);
  1188. stat |= update_by_mnemonic(state, 0x900001A0, 20, 3, ts_drive_strength);
  1189. stat |= update_by_mnemonic(state, 0x900001A0, 24, 3, ts_drive_strength);
  1190. stat |= update_by_mnemonic(state, 0x900001A0, 28, 3, ts_drive_strength);
  1191. return stat;
  1192. }
  1193. static int enable_tuner(struct mxl *state, u32 tuner, u32 enable)
  1194. {
  1195. int stat = 0;
  1196. struct MXL_HYDRA_TUNER_CMD ctrl_tuner_cmd;
  1197. u8 cmd_size = sizeof(ctrl_tuner_cmd);
  1198. u8 cmd_buff[MXL_HYDRA_OEM_MAX_CMD_BUFF_LEN];
  1199. u32 val, count = 10;
  1200. ctrl_tuner_cmd.tuner_id = tuner;
  1201. ctrl_tuner_cmd.enable = enable;
  1202. BUILD_HYDRA_CMD(MXL_HYDRA_TUNER_ACTIVATE_CMD, MXL_CMD_WRITE,
  1203. cmd_size, &ctrl_tuner_cmd, cmd_buff);
  1204. stat = send_command(state, cmd_size + MXL_HYDRA_CMD_HEADER_SIZE,
  1205. &cmd_buff[0]);
  1206. if (stat)
  1207. return stat;
  1208. read_register(state, HYDRA_TUNER_ENABLE_COMPLETE, &val);
  1209. while (--count && ((val >> tuner) & 1) != enable) {
  1210. msleep(20);
  1211. read_register(state, HYDRA_TUNER_ENABLE_COMPLETE, &val);
  1212. }
  1213. if (!count)
  1214. return -1;
  1215. read_register(state, HYDRA_TUNER_ENABLE_COMPLETE, &val);
  1216. dev_dbg(state->i2cdev, "tuner %u ready = %u\n",
  1217. tuner, (val >> tuner) & 1);
  1218. return 0;
  1219. }
  1220. static int config_ts(struct mxl *state, enum MXL_HYDRA_DEMOD_ID_E demod_id,
  1221. struct MXL_HYDRA_MPEGOUT_PARAM_T *mpeg_out_param_ptr)
  1222. {
  1223. int status = 0;
  1224. u32 nco_count_min = 0;
  1225. u32 clk_type = 0;
  1226. struct MXL_REG_FIELD_T xpt_sync_polarity[MXL_HYDRA_DEMOD_MAX] = {
  1227. {0x90700010, 8, 1}, {0x90700010, 9, 1},
  1228. {0x90700010, 10, 1}, {0x90700010, 11, 1},
  1229. {0x90700010, 12, 1}, {0x90700010, 13, 1},
  1230. {0x90700010, 14, 1}, {0x90700010, 15, 1} };
  1231. struct MXL_REG_FIELD_T xpt_clock_polarity[MXL_HYDRA_DEMOD_MAX] = {
  1232. {0x90700010, 16, 1}, {0x90700010, 17, 1},
  1233. {0x90700010, 18, 1}, {0x90700010, 19, 1},
  1234. {0x90700010, 20, 1}, {0x90700010, 21, 1},
  1235. {0x90700010, 22, 1}, {0x90700010, 23, 1} };
  1236. struct MXL_REG_FIELD_T xpt_valid_polarity[MXL_HYDRA_DEMOD_MAX] = {
  1237. {0x90700014, 0, 1}, {0x90700014, 1, 1},
  1238. {0x90700014, 2, 1}, {0x90700014, 3, 1},
  1239. {0x90700014, 4, 1}, {0x90700014, 5, 1},
  1240. {0x90700014, 6, 1}, {0x90700014, 7, 1} };
  1241. struct MXL_REG_FIELD_T xpt_ts_clock_phase[MXL_HYDRA_DEMOD_MAX] = {
  1242. {0x90700018, 0, 3}, {0x90700018, 4, 3},
  1243. {0x90700018, 8, 3}, {0x90700018, 12, 3},
  1244. {0x90700018, 16, 3}, {0x90700018, 20, 3},
  1245. {0x90700018, 24, 3}, {0x90700018, 28, 3} };
  1246. struct MXL_REG_FIELD_T xpt_lsb_first[MXL_HYDRA_DEMOD_MAX] = {
  1247. {0x9070000C, 16, 1}, {0x9070000C, 17, 1},
  1248. {0x9070000C, 18, 1}, {0x9070000C, 19, 1},
  1249. {0x9070000C, 20, 1}, {0x9070000C, 21, 1},
  1250. {0x9070000C, 22, 1}, {0x9070000C, 23, 1} };
  1251. struct MXL_REG_FIELD_T xpt_sync_byte[MXL_HYDRA_DEMOD_MAX] = {
  1252. {0x90700010, 0, 1}, {0x90700010, 1, 1},
  1253. {0x90700010, 2, 1}, {0x90700010, 3, 1},
  1254. {0x90700010, 4, 1}, {0x90700010, 5, 1},
  1255. {0x90700010, 6, 1}, {0x90700010, 7, 1} };
  1256. struct MXL_REG_FIELD_T xpt_enable_output[MXL_HYDRA_DEMOD_MAX] = {
  1257. {0x9070000C, 0, 1}, {0x9070000C, 1, 1},
  1258. {0x9070000C, 2, 1}, {0x9070000C, 3, 1},
  1259. {0x9070000C, 4, 1}, {0x9070000C, 5, 1},
  1260. {0x9070000C, 6, 1}, {0x9070000C, 7, 1} };
  1261. struct MXL_REG_FIELD_T xpt_err_replace_sync[MXL_HYDRA_DEMOD_MAX] = {
  1262. {0x9070000C, 24, 1}, {0x9070000C, 25, 1},
  1263. {0x9070000C, 26, 1}, {0x9070000C, 27, 1},
  1264. {0x9070000C, 28, 1}, {0x9070000C, 29, 1},
  1265. {0x9070000C, 30, 1}, {0x9070000C, 31, 1} };
  1266. struct MXL_REG_FIELD_T xpt_err_replace_valid[MXL_HYDRA_DEMOD_MAX] = {
  1267. {0x90700014, 8, 1}, {0x90700014, 9, 1},
  1268. {0x90700014, 10, 1}, {0x90700014, 11, 1},
  1269. {0x90700014, 12, 1}, {0x90700014, 13, 1},
  1270. {0x90700014, 14, 1}, {0x90700014, 15, 1} };
  1271. struct MXL_REG_FIELD_T xpt_continuous_clock[MXL_HYDRA_DEMOD_MAX] = {
  1272. {0x907001D4, 0, 1}, {0x907001D4, 1, 1},
  1273. {0x907001D4, 2, 1}, {0x907001D4, 3, 1},
  1274. {0x907001D4, 4, 1}, {0x907001D4, 5, 1},
  1275. {0x907001D4, 6, 1}, {0x907001D4, 7, 1} };
  1276. struct MXL_REG_FIELD_T xpt_nco_clock_rate[MXL_HYDRA_DEMOD_MAX] = {
  1277. {0x90700044, 16, 80}, {0x90700044, 16, 81},
  1278. {0x90700044, 16, 82}, {0x90700044, 16, 83},
  1279. {0x90700044, 16, 84}, {0x90700044, 16, 85},
  1280. {0x90700044, 16, 86}, {0x90700044, 16, 87} };
  1281. demod_id = state->base->ts_map[demod_id];
  1282. if (mpeg_out_param_ptr->enable == MXL_ENABLE) {
  1283. if (mpeg_out_param_ptr->mpeg_mode ==
  1284. MXL_HYDRA_MPEG_MODE_PARALLEL) {
  1285. } else {
  1286. cfg_ts_pad_mux(state, MXL_TRUE);
  1287. update_by_mnemonic(state,
  1288. 0x90700010, 27, 1, MXL_FALSE);
  1289. }
  1290. }
  1291. nco_count_min =
  1292. (u32)(MXL_HYDRA_NCO_CLK / mpeg_out_param_ptr->max_mpeg_clk_rate);
  1293. if (state->base->chipversion >= 2) {
  1294. status |= update_by_mnemonic(state,
  1295. xpt_nco_clock_rate[demod_id].reg_addr, /* Reg Addr */
  1296. xpt_nco_clock_rate[demod_id].lsb_pos, /* LSB pos */
  1297. xpt_nco_clock_rate[demod_id].num_of_bits, /* Num of bits */
  1298. nco_count_min); /* Data */
  1299. } else
  1300. update_by_mnemonic(state, 0x90700044, 16, 8, nco_count_min);
  1301. if (mpeg_out_param_ptr->mpeg_clk_type == MXL_HYDRA_MPEG_CLK_CONTINUOUS)
  1302. clk_type = 1;
  1303. if (mpeg_out_param_ptr->mpeg_mode < MXL_HYDRA_MPEG_MODE_PARALLEL) {
  1304. status |= update_by_mnemonic(state,
  1305. xpt_continuous_clock[demod_id].reg_addr,
  1306. xpt_continuous_clock[demod_id].lsb_pos,
  1307. xpt_continuous_clock[demod_id].num_of_bits,
  1308. clk_type);
  1309. } else
  1310. update_by_mnemonic(state, 0x907001D4, 8, 1, clk_type);
  1311. status |= update_by_mnemonic(state,
  1312. xpt_sync_polarity[demod_id].reg_addr,
  1313. xpt_sync_polarity[demod_id].lsb_pos,
  1314. xpt_sync_polarity[demod_id].num_of_bits,
  1315. mpeg_out_param_ptr->mpeg_sync_pol);
  1316. status |= update_by_mnemonic(state,
  1317. xpt_valid_polarity[demod_id].reg_addr,
  1318. xpt_valid_polarity[demod_id].lsb_pos,
  1319. xpt_valid_polarity[demod_id].num_of_bits,
  1320. mpeg_out_param_ptr->mpeg_valid_pol);
  1321. status |= update_by_mnemonic(state,
  1322. xpt_clock_polarity[demod_id].reg_addr,
  1323. xpt_clock_polarity[demod_id].lsb_pos,
  1324. xpt_clock_polarity[demod_id].num_of_bits,
  1325. mpeg_out_param_ptr->mpeg_clk_pol);
  1326. status |= update_by_mnemonic(state,
  1327. xpt_sync_byte[demod_id].reg_addr,
  1328. xpt_sync_byte[demod_id].lsb_pos,
  1329. xpt_sync_byte[demod_id].num_of_bits,
  1330. mpeg_out_param_ptr->mpeg_sync_pulse_width);
  1331. status |= update_by_mnemonic(state,
  1332. xpt_ts_clock_phase[demod_id].reg_addr,
  1333. xpt_ts_clock_phase[demod_id].lsb_pos,
  1334. xpt_ts_clock_phase[demod_id].num_of_bits,
  1335. mpeg_out_param_ptr->mpeg_clk_phase);
  1336. status |= update_by_mnemonic(state,
  1337. xpt_lsb_first[demod_id].reg_addr,
  1338. xpt_lsb_first[demod_id].lsb_pos,
  1339. xpt_lsb_first[demod_id].num_of_bits,
  1340. mpeg_out_param_ptr->lsb_or_msb_first);
  1341. switch (mpeg_out_param_ptr->mpeg_error_indication) {
  1342. case MXL_HYDRA_MPEG_ERR_REPLACE_SYNC:
  1343. status |= update_by_mnemonic(state,
  1344. xpt_err_replace_sync[demod_id].reg_addr,
  1345. xpt_err_replace_sync[demod_id].lsb_pos,
  1346. xpt_err_replace_sync[demod_id].num_of_bits,
  1347. MXL_TRUE);
  1348. status |= update_by_mnemonic(state,
  1349. xpt_err_replace_valid[demod_id].reg_addr,
  1350. xpt_err_replace_valid[demod_id].lsb_pos,
  1351. xpt_err_replace_valid[demod_id].num_of_bits,
  1352. MXL_FALSE);
  1353. break;
  1354. case MXL_HYDRA_MPEG_ERR_REPLACE_VALID:
  1355. status |= update_by_mnemonic(state,
  1356. xpt_err_replace_sync[demod_id].reg_addr,
  1357. xpt_err_replace_sync[demod_id].lsb_pos,
  1358. xpt_err_replace_sync[demod_id].num_of_bits,
  1359. MXL_FALSE);
  1360. status |= update_by_mnemonic(state,
  1361. xpt_err_replace_valid[demod_id].reg_addr,
  1362. xpt_err_replace_valid[demod_id].lsb_pos,
  1363. xpt_err_replace_valid[demod_id].num_of_bits,
  1364. MXL_TRUE);
  1365. break;
  1366. case MXL_HYDRA_MPEG_ERR_INDICATION_DISABLED:
  1367. default:
  1368. status |= update_by_mnemonic(state,
  1369. xpt_err_replace_sync[demod_id].reg_addr,
  1370. xpt_err_replace_sync[demod_id].lsb_pos,
  1371. xpt_err_replace_sync[demod_id].num_of_bits,
  1372. MXL_FALSE);
  1373. status |= update_by_mnemonic(state,
  1374. xpt_err_replace_valid[demod_id].reg_addr,
  1375. xpt_err_replace_valid[demod_id].lsb_pos,
  1376. xpt_err_replace_valid[demod_id].num_of_bits,
  1377. MXL_FALSE);
  1378. break;
  1379. }
  1380. if (mpeg_out_param_ptr->mpeg_mode != MXL_HYDRA_MPEG_MODE_PARALLEL) {
  1381. status |= update_by_mnemonic(state,
  1382. xpt_enable_output[demod_id].reg_addr,
  1383. xpt_enable_output[demod_id].lsb_pos,
  1384. xpt_enable_output[demod_id].num_of_bits,
  1385. mpeg_out_param_ptr->enable);
  1386. }
  1387. return status;
  1388. }
  1389. static int config_mux(struct mxl *state)
  1390. {
  1391. update_by_mnemonic(state, 0x9070000C, 0, 1, 0);
  1392. update_by_mnemonic(state, 0x9070000C, 1, 1, 0);
  1393. update_by_mnemonic(state, 0x9070000C, 2, 1, 0);
  1394. update_by_mnemonic(state, 0x9070000C, 3, 1, 0);
  1395. update_by_mnemonic(state, 0x9070000C, 4, 1, 0);
  1396. update_by_mnemonic(state, 0x9070000C, 5, 1, 0);
  1397. update_by_mnemonic(state, 0x9070000C, 6, 1, 0);
  1398. update_by_mnemonic(state, 0x9070000C, 7, 1, 0);
  1399. update_by_mnemonic(state, 0x90700008, 0, 2, 1);
  1400. update_by_mnemonic(state, 0x90700008, 2, 2, 1);
  1401. return 0;
  1402. }
  1403. static int load_fw(struct mxl *state, struct mxl5xx_cfg *cfg)
  1404. {
  1405. int stat = 0;
  1406. u8 *buf;
  1407. if (cfg->fw)
  1408. return firmware_download(state, cfg->fw, cfg->fw_len);
  1409. if (!cfg->fw_read)
  1410. return -1;
  1411. buf = vmalloc(0x40000);
  1412. if (!buf)
  1413. return -ENOMEM;
  1414. cfg->fw_read(cfg->fw_priv, buf, 0x40000);
  1415. stat = firmware_download(state, buf, 0x40000);
  1416. vfree(buf);
  1417. return stat;
  1418. }
  1419. static int validate_sku(struct mxl *state)
  1420. {
  1421. u32 pad_mux_bond = 0, prcm_chip_id = 0, prcm_so_cid = 0;
  1422. int status;
  1423. u32 type = state->base->type;
  1424. status = read_by_mnemonic(state, 0x90000190, 0, 3, &pad_mux_bond);
  1425. status |= read_by_mnemonic(state, 0x80030000, 0, 12, &prcm_chip_id);
  1426. status |= read_by_mnemonic(state, 0x80030004, 24, 8, &prcm_so_cid);
  1427. if (status)
  1428. return -1;
  1429. dev_info(state->i2cdev, "padMuxBond=%08x, prcmChipId=%08x, prcmSoCId=%08x\n",
  1430. pad_mux_bond, prcm_chip_id, prcm_so_cid);
  1431. if (prcm_chip_id != 0x560) {
  1432. switch (pad_mux_bond) {
  1433. case MXL_HYDRA_SKU_ID_581:
  1434. if (type == MXL_HYDRA_DEVICE_581)
  1435. return 0;
  1436. if (type == MXL_HYDRA_DEVICE_581S) {
  1437. state->base->type = MXL_HYDRA_DEVICE_581;
  1438. return 0;
  1439. }
  1440. break;
  1441. case MXL_HYDRA_SKU_ID_584:
  1442. if (type == MXL_HYDRA_DEVICE_584)
  1443. return 0;
  1444. break;
  1445. case MXL_HYDRA_SKU_ID_544:
  1446. if (type == MXL_HYDRA_DEVICE_544)
  1447. return 0;
  1448. if (type == MXL_HYDRA_DEVICE_542)
  1449. return 0;
  1450. break;
  1451. case MXL_HYDRA_SKU_ID_582:
  1452. if (type == MXL_HYDRA_DEVICE_582)
  1453. return 0;
  1454. break;
  1455. default:
  1456. return -1;
  1457. }
  1458. } else {
  1459. }
  1460. return -1;
  1461. }
  1462. static int get_fwinfo(struct mxl *state)
  1463. {
  1464. int status;
  1465. u32 val = 0;
  1466. status = read_by_mnemonic(state, 0x90000190, 0, 3, &val);
  1467. if (status)
  1468. return status;
  1469. dev_info(state->i2cdev, "chipID=%08x\n", val);
  1470. status = read_by_mnemonic(state, 0x80030004, 8, 8, &val);
  1471. if (status)
  1472. return status;
  1473. dev_info(state->i2cdev, "chipVer=%08x\n", val);
  1474. status = read_register(state, HYDRA_FIRMWARE_VERSION, &val);
  1475. if (status)
  1476. return status;
  1477. dev_info(state->i2cdev, "FWVer=%08x\n", val);
  1478. state->base->fwversion = val;
  1479. return status;
  1480. }
  1481. static u8 ts_map1_to_1[MXL_HYDRA_DEMOD_MAX] = {
  1482. MXL_HYDRA_DEMOD_ID_0,
  1483. MXL_HYDRA_DEMOD_ID_1,
  1484. MXL_HYDRA_DEMOD_ID_2,
  1485. MXL_HYDRA_DEMOD_ID_3,
  1486. MXL_HYDRA_DEMOD_ID_4,
  1487. MXL_HYDRA_DEMOD_ID_5,
  1488. MXL_HYDRA_DEMOD_ID_6,
  1489. MXL_HYDRA_DEMOD_ID_7,
  1490. };
  1491. static u8 ts_map54x[MXL_HYDRA_DEMOD_MAX] = {
  1492. MXL_HYDRA_DEMOD_ID_2,
  1493. MXL_HYDRA_DEMOD_ID_3,
  1494. MXL_HYDRA_DEMOD_ID_4,
  1495. MXL_HYDRA_DEMOD_ID_5,
  1496. MXL_HYDRA_DEMOD_MAX,
  1497. MXL_HYDRA_DEMOD_MAX,
  1498. MXL_HYDRA_DEMOD_MAX,
  1499. MXL_HYDRA_DEMOD_MAX,
  1500. };
  1501. static int probe(struct mxl *state, struct mxl5xx_cfg *cfg)
  1502. {
  1503. u32 chipver;
  1504. int fw, status, j;
  1505. struct MXL_HYDRA_MPEGOUT_PARAM_T mpeg_interface_cfg;
  1506. state->base->ts_map = ts_map1_to_1;
  1507. switch (state->base->type) {
  1508. case MXL_HYDRA_DEVICE_581:
  1509. case MXL_HYDRA_DEVICE_581S:
  1510. state->base->can_clkout = 1;
  1511. state->base->demod_num = 8;
  1512. state->base->tuner_num = 1;
  1513. state->base->sku_type = MXL_HYDRA_SKU_TYPE_581;
  1514. break;
  1515. case MXL_HYDRA_DEVICE_582:
  1516. state->base->can_clkout = 1;
  1517. state->base->demod_num = 8;
  1518. state->base->tuner_num = 3;
  1519. state->base->sku_type = MXL_HYDRA_SKU_TYPE_582;
  1520. break;
  1521. case MXL_HYDRA_DEVICE_585:
  1522. state->base->can_clkout = 0;
  1523. state->base->demod_num = 8;
  1524. state->base->tuner_num = 4;
  1525. state->base->sku_type = MXL_HYDRA_SKU_TYPE_585;
  1526. break;
  1527. case MXL_HYDRA_DEVICE_544:
  1528. state->base->can_clkout = 0;
  1529. state->base->demod_num = 4;
  1530. state->base->tuner_num = 4;
  1531. state->base->sku_type = MXL_HYDRA_SKU_TYPE_544;
  1532. state->base->ts_map = ts_map54x;
  1533. break;
  1534. case MXL_HYDRA_DEVICE_541:
  1535. case MXL_HYDRA_DEVICE_541S:
  1536. state->base->can_clkout = 0;
  1537. state->base->demod_num = 4;
  1538. state->base->tuner_num = 1;
  1539. state->base->sku_type = MXL_HYDRA_SKU_TYPE_541;
  1540. state->base->ts_map = ts_map54x;
  1541. break;
  1542. case MXL_HYDRA_DEVICE_561:
  1543. case MXL_HYDRA_DEVICE_561S:
  1544. state->base->can_clkout = 0;
  1545. state->base->demod_num = 6;
  1546. state->base->tuner_num = 1;
  1547. state->base->sku_type = MXL_HYDRA_SKU_TYPE_561;
  1548. break;
  1549. case MXL_HYDRA_DEVICE_568:
  1550. state->base->can_clkout = 0;
  1551. state->base->demod_num = 8;
  1552. state->base->tuner_num = 1;
  1553. state->base->chan_bond = 1;
  1554. state->base->sku_type = MXL_HYDRA_SKU_TYPE_568;
  1555. break;
  1556. case MXL_HYDRA_DEVICE_542:
  1557. state->base->can_clkout = 1;
  1558. state->base->demod_num = 4;
  1559. state->base->tuner_num = 3;
  1560. state->base->sku_type = MXL_HYDRA_SKU_TYPE_542;
  1561. state->base->ts_map = ts_map54x;
  1562. break;
  1563. case MXL_HYDRA_DEVICE_TEST:
  1564. case MXL_HYDRA_DEVICE_584:
  1565. default:
  1566. state->base->can_clkout = 0;
  1567. state->base->demod_num = 8;
  1568. state->base->tuner_num = 4;
  1569. state->base->sku_type = MXL_HYDRA_SKU_TYPE_584;
  1570. break;
  1571. }
  1572. status = validate_sku(state);
  1573. if (status)
  1574. return status;
  1575. update_by_mnemonic(state, 0x80030014, 9, 1, 1);
  1576. update_by_mnemonic(state, 0x8003003C, 12, 1, 1);
  1577. status = read_by_mnemonic(state, 0x80030000, 12, 4, &chipver);
  1578. if (status)
  1579. state->base->chipversion = 0;
  1580. else
  1581. state->base->chipversion = (chipver == 2) ? 2 : 1;
  1582. dev_info(state->i2cdev, "Hydra chip version %u\n",
  1583. state->base->chipversion);
  1584. cfg_dev_xtal(state, cfg->clk, cfg->cap, 0);
  1585. fw = firmware_is_alive(state);
  1586. if (!fw) {
  1587. status = load_fw(state, cfg);
  1588. if (status)
  1589. return status;
  1590. }
  1591. get_fwinfo(state);
  1592. config_mux(state);
  1593. mpeg_interface_cfg.enable = MXL_ENABLE;
  1594. mpeg_interface_cfg.lsb_or_msb_first = MXL_HYDRA_MPEG_SERIAL_MSB_1ST;
  1595. /* supports only (0-104&139)MHz */
  1596. if (cfg->ts_clk)
  1597. mpeg_interface_cfg.max_mpeg_clk_rate = cfg->ts_clk;
  1598. else
  1599. mpeg_interface_cfg.max_mpeg_clk_rate = 69; /* 139; */
  1600. mpeg_interface_cfg.mpeg_clk_phase = MXL_HYDRA_MPEG_CLK_PHASE_SHIFT_0_DEG;
  1601. mpeg_interface_cfg.mpeg_clk_pol = MXL_HYDRA_MPEG_CLK_IN_PHASE;
  1602. /* MXL_HYDRA_MPEG_CLK_GAPPED; */
  1603. mpeg_interface_cfg.mpeg_clk_type = MXL_HYDRA_MPEG_CLK_CONTINUOUS;
  1604. mpeg_interface_cfg.mpeg_error_indication =
  1605. MXL_HYDRA_MPEG_ERR_INDICATION_DISABLED;
  1606. mpeg_interface_cfg.mpeg_mode = MXL_HYDRA_MPEG_MODE_SERIAL_3_WIRE;
  1607. mpeg_interface_cfg.mpeg_sync_pol = MXL_HYDRA_MPEG_ACTIVE_HIGH;
  1608. mpeg_interface_cfg.mpeg_sync_pulse_width = MXL_HYDRA_MPEG_SYNC_WIDTH_BIT;
  1609. mpeg_interface_cfg.mpeg_valid_pol = MXL_HYDRA_MPEG_ACTIVE_HIGH;
  1610. for (j = 0; j < state->base->demod_num; j++) {
  1611. status = config_ts(state, (enum MXL_HYDRA_DEMOD_ID_E) j,
  1612. &mpeg_interface_cfg);
  1613. if (status)
  1614. return status;
  1615. }
  1616. set_drive_strength(state, 1);
  1617. return 0;
  1618. }
  1619. struct dvb_frontend *mxl5xx_attach(struct i2c_adapter *i2c,
  1620. struct mxl5xx_cfg *cfg, u32 demod, u32 tuner,
  1621. int (**fn_set_input)(struct dvb_frontend *, int))
  1622. {
  1623. struct mxl *state;
  1624. struct mxl_base *base;
  1625. state = kzalloc(sizeof(struct mxl), GFP_KERNEL);
  1626. if (!state)
  1627. return NULL;
  1628. state->demod = demod;
  1629. state->tuner = tuner;
  1630. state->tuner_in_use = 0xffffffff;
  1631. state->i2cdev = &i2c->dev;
  1632. base = match_base(i2c, cfg->adr);
  1633. if (base) {
  1634. base->count++;
  1635. if (base->count > base->demod_num)
  1636. goto fail;
  1637. state->base = base;
  1638. } else {
  1639. base = kzalloc(sizeof(struct mxl_base), GFP_KERNEL);
  1640. if (!base)
  1641. goto fail;
  1642. base->i2c = i2c;
  1643. base->adr = cfg->adr;
  1644. base->type = cfg->type;
  1645. base->count = 1;
  1646. mutex_init(&base->i2c_lock);
  1647. mutex_init(&base->status_lock);
  1648. mutex_init(&base->tune_lock);
  1649. INIT_LIST_HEAD(&base->mxls);
  1650. state->base = base;
  1651. if (probe(state, cfg) < 0) {
  1652. kfree(base);
  1653. goto fail;
  1654. }
  1655. list_add(&base->mxllist, &mxllist);
  1656. }
  1657. state->fe.ops = mxl_ops;
  1658. state->xbar[0] = 4;
  1659. state->xbar[1] = demod;
  1660. state->xbar[2] = 8;
  1661. state->fe.demodulator_priv = state;
  1662. *fn_set_input = set_input;
  1663. list_add(&state->mxl, &base->mxls);
  1664. return &state->fe;
  1665. fail:
  1666. kfree(state);
  1667. return NULL;
  1668. }
  1669. EXPORT_SYMBOL_GPL(mxl5xx_attach);
  1670. MODULE_DESCRIPTION("MaxLinear MxL5xx DVB-S/S2 tuner-demodulator driver");
  1671. MODULE_AUTHOR("Ralph and Marcus Metzler, Metzler Brothers Systementwicklung GbR");
  1672. MODULE_LICENSE("GPL v2");