mxl5xx.c 53 KB

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