mxl5xx.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  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 <rjkm@metzlerbros.de>
  6. * Marcus Metzler <mocm@metzlerbros.de>
  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. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * version 2, as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  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. /* Fall through */
  640. case SYS_DVBS:
  641. switch ((enum MXL_HYDRA_MODULATION_E)
  642. reg_data[DMD_MODULATION_SCHEME_ADDR]) {
  643. case MXL_HYDRA_MOD_QPSK:
  644. p->modulation = QPSK;
  645. break;
  646. case MXL_HYDRA_MOD_8PSK:
  647. p->modulation = PSK_8;
  648. break;
  649. default:
  650. break;
  651. }
  652. switch ((enum MXL_HYDRA_ROLLOFF_E)
  653. reg_data[DMD_SPECTRUM_ROLL_OFF_ADDR]) {
  654. case MXL_HYDRA_ROLLOFF_0_20:
  655. p->rolloff = ROLLOFF_20;
  656. break;
  657. case MXL_HYDRA_ROLLOFF_0_35:
  658. p->rolloff = ROLLOFF_35;
  659. break;
  660. case MXL_HYDRA_ROLLOFF_0_25:
  661. p->rolloff = ROLLOFF_25;
  662. break;
  663. default:
  664. break;
  665. }
  666. break;
  667. default:
  668. return -EINVAL;
  669. }
  670. return 0;
  671. }
  672. static int set_input(struct dvb_frontend *fe, int input)
  673. {
  674. struct mxl *state = fe->demodulator_priv;
  675. state->tuner = input;
  676. return 0;
  677. }
  678. static const struct dvb_frontend_ops mxl_ops = {
  679. .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
  680. .info = {
  681. .name = "MaxLinear MxL5xx DVB-S/S2 tuner-demodulator",
  682. .frequency_min_hz = 300 * MHz,
  683. .frequency_max_hz = 2350 * MHz,
  684. .symbol_rate_min = 1000000,
  685. .symbol_rate_max = 45000000,
  686. .caps = FE_CAN_INVERSION_AUTO |
  687. FE_CAN_FEC_AUTO |
  688. FE_CAN_QPSK |
  689. FE_CAN_2G_MODULATION
  690. },
  691. .init = init,
  692. .release = release,
  693. .get_frontend_algo = get_algo,
  694. .tune = tune,
  695. .read_status = read_status,
  696. .sleep = sleep,
  697. .get_frontend = get_frontend,
  698. .diseqc_send_master_cmd = send_master_cmd,
  699. };
  700. static struct mxl_base *match_base(struct i2c_adapter *i2c, u8 adr)
  701. {
  702. struct mxl_base *p;
  703. list_for_each_entry(p, &mxllist, mxllist)
  704. if (p->i2c == i2c && p->adr == adr)
  705. return p;
  706. return NULL;
  707. }
  708. static void cfg_dev_xtal(struct mxl *state, u32 freq, u32 cap, u32 enable)
  709. {
  710. if (state->base->can_clkout || !enable)
  711. update_by_mnemonic(state, 0x90200054, 23, 1, enable);
  712. if (freq == 24000000)
  713. write_register(state, HYDRA_CRYSTAL_SETTING, 0);
  714. else
  715. write_register(state, HYDRA_CRYSTAL_SETTING, 1);
  716. write_register(state, HYDRA_CRYSTAL_CAP, cap);
  717. }
  718. static u32 get_big_endian(u8 num_of_bits, const u8 buf[])
  719. {
  720. u32 ret_value = 0;
  721. switch (num_of_bits) {
  722. case 24:
  723. ret_value = (((u32) buf[0]) << 16) |
  724. (((u32) buf[1]) << 8) | buf[2];
  725. break;
  726. case 32:
  727. ret_value = (((u32) buf[0]) << 24) |
  728. (((u32) buf[1]) << 16) |
  729. (((u32) buf[2]) << 8) | buf[3];
  730. break;
  731. default:
  732. break;
  733. }
  734. return ret_value;
  735. }
  736. static int write_fw_segment(struct mxl *state,
  737. u32 mem_addr, u32 total_size, u8 *data_ptr)
  738. {
  739. int status;
  740. u32 data_count = 0;
  741. u32 size = 0;
  742. u32 orig_size = 0;
  743. u8 *w_buf_ptr = NULL;
  744. u32 block_size = ((MXL_HYDRA_OEM_MAX_BLOCK_WRITE_LENGTH -
  745. (MXL_HYDRA_I2C_HDR_SIZE +
  746. MXL_HYDRA_REG_SIZE_IN_BYTES)) / 4) * 4;
  747. u8 w_msg_buffer[MXL_HYDRA_OEM_MAX_BLOCK_WRITE_LENGTH -
  748. (MXL_HYDRA_I2C_HDR_SIZE + MXL_HYDRA_REG_SIZE_IN_BYTES)];
  749. do {
  750. size = orig_size = (((u32)(data_count + block_size)) > total_size) ?
  751. (total_size - data_count) : block_size;
  752. if (orig_size & 3)
  753. size = (orig_size + 4) & ~3;
  754. w_buf_ptr = &w_msg_buffer[0];
  755. memset((void *) w_buf_ptr, 0, size);
  756. memcpy((void *) w_buf_ptr, (void *) data_ptr, orig_size);
  757. convert_endian(1, size, w_buf_ptr);
  758. status = write_firmware_block(state, mem_addr, size, w_buf_ptr);
  759. if (status)
  760. return status;
  761. data_count += size;
  762. mem_addr += size;
  763. data_ptr += size;
  764. } while (data_count < total_size);
  765. return status;
  766. }
  767. static int do_firmware_download(struct mxl *state, u8 *mbin_buffer_ptr,
  768. u32 mbin_buffer_size)
  769. {
  770. int status;
  771. u32 index = 0;
  772. u32 seg_length = 0;
  773. u32 seg_address = 0;
  774. struct MBIN_FILE_T *mbin_ptr = (struct MBIN_FILE_T *)mbin_buffer_ptr;
  775. struct MBIN_SEGMENT_T *segment_ptr;
  776. enum MXL_BOOL_E xcpu_fw_flag = MXL_FALSE;
  777. if (mbin_ptr->header.id != MBIN_FILE_HEADER_ID) {
  778. dev_err(state->i2cdev, "%s: Invalid file header ID (%c)\n",
  779. __func__, mbin_ptr->header.id);
  780. return -EINVAL;
  781. }
  782. status = write_register(state, FW_DL_SIGN_ADDR, 0);
  783. if (status)
  784. return status;
  785. segment_ptr = (struct MBIN_SEGMENT_T *) (&mbin_ptr->data[0]);
  786. for (index = 0; index < mbin_ptr->header.num_segments; index++) {
  787. if (segment_ptr->header.id != MBIN_SEGMENT_HEADER_ID) {
  788. dev_err(state->i2cdev, "%s: Invalid segment header ID (%c)\n",
  789. __func__, segment_ptr->header.id);
  790. return -EINVAL;
  791. }
  792. seg_length = get_big_endian(24,
  793. &(segment_ptr->header.len24[0]));
  794. seg_address = get_big_endian(32,
  795. &(segment_ptr->header.address[0]));
  796. if (state->base->type == MXL_HYDRA_DEVICE_568) {
  797. if ((((seg_address & 0x90760000) == 0x90760000) ||
  798. ((seg_address & 0x90740000) == 0x90740000)) &&
  799. (xcpu_fw_flag == MXL_FALSE)) {
  800. update_by_mnemonic(state, 0x8003003C, 0, 1, 1);
  801. msleep(200);
  802. write_register(state, 0x90720000, 0);
  803. usleep_range(10000, 11000);
  804. xcpu_fw_flag = MXL_TRUE;
  805. }
  806. status = write_fw_segment(state, seg_address,
  807. seg_length,
  808. (u8 *) segment_ptr->data);
  809. } else {
  810. if (((seg_address & 0x90760000) != 0x90760000) &&
  811. ((seg_address & 0x90740000) != 0x90740000))
  812. status = write_fw_segment(state, seg_address,
  813. seg_length, (u8 *) segment_ptr->data);
  814. }
  815. if (status)
  816. return status;
  817. segment_ptr = (struct MBIN_SEGMENT_T *)
  818. &(segment_ptr->data[((seg_length + 3) / 4) * 4]);
  819. }
  820. return status;
  821. }
  822. static int check_fw(struct mxl *state, u8 *mbin, u32 mbin_len)
  823. {
  824. struct MBIN_FILE_HEADER_T *fh = (struct MBIN_FILE_HEADER_T *) mbin;
  825. u32 flen = (fh->image_size24[0] << 16) |
  826. (fh->image_size24[1] << 8) | fh->image_size24[2];
  827. u8 *fw, cs = 0;
  828. u32 i;
  829. if (fh->id != 'M' || fh->fmt_version != '1' || flen > 0x3FFF0) {
  830. dev_info(state->i2cdev, "Invalid FW Header\n");
  831. return -1;
  832. }
  833. fw = mbin + sizeof(struct MBIN_FILE_HEADER_T);
  834. for (i = 0; i < flen; i += 1)
  835. cs += fw[i];
  836. if (cs != fh->image_checksum) {
  837. dev_info(state->i2cdev, "Invalid FW Checksum\n");
  838. return -1;
  839. }
  840. return 0;
  841. }
  842. static int firmware_download(struct mxl *state, u8 *mbin, u32 mbin_len)
  843. {
  844. int status;
  845. u32 reg_data = 0;
  846. struct MXL_HYDRA_SKU_COMMAND_T dev_sku_cfg;
  847. u8 cmd_size = sizeof(struct MXL_HYDRA_SKU_COMMAND_T);
  848. u8 cmd_buff[sizeof(struct MXL_HYDRA_SKU_COMMAND_T) + 6];
  849. if (check_fw(state, mbin, mbin_len))
  850. return -1;
  851. /* put CPU into reset */
  852. status = update_by_mnemonic(state, 0x8003003C, 0, 1, 0);
  853. if (status)
  854. return status;
  855. usleep_range(1000, 2000);
  856. /* Reset TX FIFO's, BBAND, XBAR */
  857. status = write_register(state, HYDRA_RESET_TRANSPORT_FIFO_REG,
  858. HYDRA_RESET_TRANSPORT_FIFO_DATA);
  859. if (status)
  860. return status;
  861. status = write_register(state, HYDRA_RESET_BBAND_REG,
  862. HYDRA_RESET_BBAND_DATA);
  863. if (status)
  864. return status;
  865. status = write_register(state, HYDRA_RESET_XBAR_REG,
  866. HYDRA_RESET_XBAR_DATA);
  867. if (status)
  868. return status;
  869. /* Disable clock to Baseband, Wideband, SerDes,
  870. * Alias ext & Transport modules
  871. */
  872. status = write_register(state, HYDRA_MODULES_CLK_2_REG,
  873. HYDRA_DISABLE_CLK_2);
  874. if (status)
  875. return status;
  876. /* Clear Software & Host interrupt status - (Clear on read) */
  877. status = read_register(state, HYDRA_PRCM_ROOT_CLK_REG, &reg_data);
  878. if (status)
  879. return status;
  880. status = do_firmware_download(state, mbin, mbin_len);
  881. if (status)
  882. return status;
  883. if (state->base->type == MXL_HYDRA_DEVICE_568) {
  884. usleep_range(10000, 11000);
  885. /* bring XCPU out of reset */
  886. status = write_register(state, 0x90720000, 1);
  887. if (status)
  888. return status;
  889. msleep(500);
  890. /* Enable XCPU UART message processing in MCPU */
  891. status = write_register(state, 0x9076B510, 1);
  892. if (status)
  893. return status;
  894. } else {
  895. /* Bring CPU out of reset */
  896. status = update_by_mnemonic(state, 0x8003003C, 0, 1, 1);
  897. if (status)
  898. return status;
  899. /* Wait until FW boots */
  900. msleep(150);
  901. }
  902. /* Initialize XPT XBAR */
  903. status = write_register(state, XPT_DMD0_BASEADDR, 0x76543210);
  904. if (status)
  905. return status;
  906. if (!firmware_is_alive(state))
  907. return -1;
  908. dev_info(state->i2cdev, "Hydra FW alive. Hail!\n");
  909. /* sometimes register values are wrong shortly
  910. * after first heart beats
  911. */
  912. msleep(50);
  913. dev_sku_cfg.sku_type = state->base->sku_type;
  914. BUILD_HYDRA_CMD(MXL_HYDRA_DEV_CFG_SKU_CMD, MXL_CMD_WRITE,
  915. cmd_size, &dev_sku_cfg, cmd_buff);
  916. status = send_command(state, cmd_size + MXL_HYDRA_CMD_HEADER_SIZE,
  917. &cmd_buff[0]);
  918. return status;
  919. }
  920. static int cfg_ts_pad_mux(struct mxl *state, enum MXL_BOOL_E enable_serial_ts)
  921. {
  922. int status = 0;
  923. u32 pad_mux_value = 0;
  924. if (enable_serial_ts == MXL_TRUE) {
  925. pad_mux_value = 0;
  926. if ((state->base->type == MXL_HYDRA_DEVICE_541) ||
  927. (state->base->type == MXL_HYDRA_DEVICE_541S))
  928. pad_mux_value = 2;
  929. } else {
  930. if ((state->base->type == MXL_HYDRA_DEVICE_581) ||
  931. (state->base->type == MXL_HYDRA_DEVICE_581S))
  932. pad_mux_value = 2;
  933. else
  934. pad_mux_value = 3;
  935. }
  936. switch (state->base->type) {
  937. case MXL_HYDRA_DEVICE_561:
  938. case MXL_HYDRA_DEVICE_581:
  939. case MXL_HYDRA_DEVICE_541:
  940. case MXL_HYDRA_DEVICE_541S:
  941. case MXL_HYDRA_DEVICE_561S:
  942. case MXL_HYDRA_DEVICE_581S:
  943. status |= update_by_mnemonic(state, 0x90000170, 24, 3,
  944. pad_mux_value);
  945. status |= update_by_mnemonic(state, 0x90000170, 28, 3,
  946. pad_mux_value);
  947. status |= update_by_mnemonic(state, 0x90000174, 0, 3,
  948. pad_mux_value);
  949. status |= update_by_mnemonic(state, 0x90000174, 4, 3,
  950. pad_mux_value);
  951. status |= update_by_mnemonic(state, 0x90000174, 8, 3,
  952. pad_mux_value);
  953. status |= update_by_mnemonic(state, 0x90000174, 12, 3,
  954. pad_mux_value);
  955. status |= update_by_mnemonic(state, 0x90000174, 16, 3,
  956. pad_mux_value);
  957. status |= update_by_mnemonic(state, 0x90000174, 20, 3,
  958. pad_mux_value);
  959. status |= update_by_mnemonic(state, 0x90000174, 24, 3,
  960. pad_mux_value);
  961. status |= update_by_mnemonic(state, 0x90000174, 28, 3,
  962. pad_mux_value);
  963. status |= update_by_mnemonic(state, 0x90000178, 0, 3,
  964. pad_mux_value);
  965. status |= update_by_mnemonic(state, 0x90000178, 4, 3,
  966. pad_mux_value);
  967. status |= update_by_mnemonic(state, 0x90000178, 8, 3,
  968. pad_mux_value);
  969. break;
  970. case MXL_HYDRA_DEVICE_544:
  971. case MXL_HYDRA_DEVICE_542:
  972. status |= update_by_mnemonic(state, 0x9000016C, 4, 3, 1);
  973. status |= update_by_mnemonic(state, 0x9000016C, 8, 3, 0);
  974. status |= update_by_mnemonic(state, 0x9000016C, 12, 3, 0);
  975. status |= update_by_mnemonic(state, 0x9000016C, 16, 3, 0);
  976. status |= update_by_mnemonic(state, 0x90000170, 0, 3, 0);
  977. status |= update_by_mnemonic(state, 0x90000178, 12, 3, 1);
  978. status |= update_by_mnemonic(state, 0x90000178, 16, 3, 1);
  979. status |= update_by_mnemonic(state, 0x90000178, 20, 3, 1);
  980. status |= update_by_mnemonic(state, 0x90000178, 24, 3, 1);
  981. status |= update_by_mnemonic(state, 0x9000017C, 0, 3, 1);
  982. status |= update_by_mnemonic(state, 0x9000017C, 4, 3, 1);
  983. if (enable_serial_ts == MXL_ENABLE) {
  984. status |= update_by_mnemonic(state,
  985. 0x90000170, 4, 3, 0);
  986. status |= update_by_mnemonic(state,
  987. 0x90000170, 8, 3, 0);
  988. status |= update_by_mnemonic(state,
  989. 0x90000170, 12, 3, 0);
  990. status |= update_by_mnemonic(state,
  991. 0x90000170, 16, 3, 0);
  992. status |= update_by_mnemonic(state,
  993. 0x90000170, 20, 3, 1);
  994. status |= update_by_mnemonic(state,
  995. 0x90000170, 24, 3, 1);
  996. status |= update_by_mnemonic(state,
  997. 0x90000170, 28, 3, 2);
  998. status |= update_by_mnemonic(state,
  999. 0x90000174, 0, 3, 2);
  1000. status |= update_by_mnemonic(state,
  1001. 0x90000174, 4, 3, 2);
  1002. status |= update_by_mnemonic(state,
  1003. 0x90000174, 8, 3, 2);
  1004. status |= update_by_mnemonic(state,
  1005. 0x90000174, 12, 3, 2);
  1006. status |= update_by_mnemonic(state,
  1007. 0x90000174, 16, 3, 2);
  1008. status |= update_by_mnemonic(state,
  1009. 0x90000174, 20, 3, 2);
  1010. status |= update_by_mnemonic(state,
  1011. 0x90000174, 24, 3, 2);
  1012. status |= update_by_mnemonic(state,
  1013. 0x90000174, 28, 3, 2);
  1014. status |= update_by_mnemonic(state,
  1015. 0x90000178, 0, 3, 2);
  1016. status |= update_by_mnemonic(state,
  1017. 0x90000178, 4, 3, 2);
  1018. status |= update_by_mnemonic(state,
  1019. 0x90000178, 8, 3, 2);
  1020. } else {
  1021. status |= update_by_mnemonic(state,
  1022. 0x90000170, 4, 3, 3);
  1023. status |= update_by_mnemonic(state,
  1024. 0x90000170, 8, 3, 3);
  1025. status |= update_by_mnemonic(state,
  1026. 0x90000170, 12, 3, 3);
  1027. status |= update_by_mnemonic(state,
  1028. 0x90000170, 16, 3, 3);
  1029. status |= update_by_mnemonic(state,
  1030. 0x90000170, 20, 3, 3);
  1031. status |= update_by_mnemonic(state,
  1032. 0x90000170, 24, 3, 3);
  1033. status |= update_by_mnemonic(state,
  1034. 0x90000170, 28, 3, 3);
  1035. status |= update_by_mnemonic(state,
  1036. 0x90000174, 0, 3, 3);
  1037. status |= update_by_mnemonic(state,
  1038. 0x90000174, 4, 3, 3);
  1039. status |= update_by_mnemonic(state,
  1040. 0x90000174, 8, 3, 3);
  1041. status |= update_by_mnemonic(state,
  1042. 0x90000174, 12, 3, 3);
  1043. status |= update_by_mnemonic(state,
  1044. 0x90000174, 16, 3, 3);
  1045. status |= update_by_mnemonic(state,
  1046. 0x90000174, 20, 3, 1);
  1047. status |= update_by_mnemonic(state,
  1048. 0x90000174, 24, 3, 1);
  1049. status |= update_by_mnemonic(state,
  1050. 0x90000174, 28, 3, 1);
  1051. status |= update_by_mnemonic(state,
  1052. 0x90000178, 0, 3, 1);
  1053. status |= update_by_mnemonic(state,
  1054. 0x90000178, 4, 3, 1);
  1055. status |= update_by_mnemonic(state,
  1056. 0x90000178, 8, 3, 1);
  1057. }
  1058. break;
  1059. case MXL_HYDRA_DEVICE_568:
  1060. if (enable_serial_ts == MXL_FALSE) {
  1061. status |= update_by_mnemonic(state,
  1062. 0x9000016C, 8, 3, 5);
  1063. status |= update_by_mnemonic(state,
  1064. 0x9000016C, 12, 3, 5);
  1065. status |= update_by_mnemonic(state,
  1066. 0x9000016C, 16, 3, 5);
  1067. status |= update_by_mnemonic(state,
  1068. 0x9000016C, 20, 3, 5);
  1069. status |= update_by_mnemonic(state,
  1070. 0x9000016C, 24, 3, 5);
  1071. status |= update_by_mnemonic(state,
  1072. 0x9000016C, 28, 3, 5);
  1073. status |= update_by_mnemonic(state,
  1074. 0x90000170, 0, 3, 5);
  1075. status |= update_by_mnemonic(state,
  1076. 0x90000170, 4, 3, 5);
  1077. status |= update_by_mnemonic(state,
  1078. 0x90000170, 8, 3, 5);
  1079. status |= update_by_mnemonic(state,
  1080. 0x90000170, 12, 3, 5);
  1081. status |= update_by_mnemonic(state,
  1082. 0x90000170, 16, 3, 5);
  1083. status |= update_by_mnemonic(state,
  1084. 0x90000170, 20, 3, 5);
  1085. status |= update_by_mnemonic(state,
  1086. 0x90000170, 24, 3, pad_mux_value);
  1087. status |= update_by_mnemonic(state,
  1088. 0x90000174, 0, 3, pad_mux_value);
  1089. status |= update_by_mnemonic(state,
  1090. 0x90000174, 4, 3, pad_mux_value);
  1091. status |= update_by_mnemonic(state,
  1092. 0x90000174, 8, 3, pad_mux_value);
  1093. status |= update_by_mnemonic(state,
  1094. 0x90000174, 12, 3, pad_mux_value);
  1095. status |= update_by_mnemonic(state,
  1096. 0x90000174, 16, 3, pad_mux_value);
  1097. status |= update_by_mnemonic(state,
  1098. 0x90000174, 20, 3, pad_mux_value);
  1099. status |= update_by_mnemonic(state,
  1100. 0x90000174, 24, 3, pad_mux_value);
  1101. status |= update_by_mnemonic(state,
  1102. 0x90000174, 28, 3, pad_mux_value);
  1103. status |= update_by_mnemonic(state,
  1104. 0x90000178, 0, 3, pad_mux_value);
  1105. status |= update_by_mnemonic(state,
  1106. 0x90000178, 4, 3, pad_mux_value);
  1107. status |= update_by_mnemonic(state,
  1108. 0x90000178, 8, 3, 5);
  1109. status |= update_by_mnemonic(state,
  1110. 0x90000178, 12, 3, 5);
  1111. status |= update_by_mnemonic(state,
  1112. 0x90000178, 16, 3, 5);
  1113. status |= update_by_mnemonic(state,
  1114. 0x90000178, 20, 3, 5);
  1115. status |= update_by_mnemonic(state,
  1116. 0x90000178, 24, 3, 5);
  1117. status |= update_by_mnemonic(state,
  1118. 0x90000178, 28, 3, 5);
  1119. status |= update_by_mnemonic(state,
  1120. 0x9000017C, 0, 3, 5);
  1121. status |= update_by_mnemonic(state,
  1122. 0x9000017C, 4, 3, 5);
  1123. } else {
  1124. status |= update_by_mnemonic(state,
  1125. 0x90000170, 4, 3, pad_mux_value);
  1126. status |= update_by_mnemonic(state,
  1127. 0x90000170, 8, 3, pad_mux_value);
  1128. status |= update_by_mnemonic(state,
  1129. 0x90000170, 12, 3, pad_mux_value);
  1130. status |= update_by_mnemonic(state,
  1131. 0x90000170, 16, 3, pad_mux_value);
  1132. status |= update_by_mnemonic(state,
  1133. 0x90000170, 20, 3, pad_mux_value);
  1134. status |= update_by_mnemonic(state,
  1135. 0x90000170, 24, 3, pad_mux_value);
  1136. status |= update_by_mnemonic(state,
  1137. 0x90000170, 28, 3, pad_mux_value);
  1138. status |= update_by_mnemonic(state,
  1139. 0x90000174, 0, 3, pad_mux_value);
  1140. status |= update_by_mnemonic(state,
  1141. 0x90000174, 4, 3, pad_mux_value);
  1142. status |= update_by_mnemonic(state,
  1143. 0x90000174, 8, 3, pad_mux_value);
  1144. status |= update_by_mnemonic(state,
  1145. 0x90000174, 12, 3, pad_mux_value);
  1146. }
  1147. break;
  1148. case MXL_HYDRA_DEVICE_584:
  1149. default:
  1150. status |= update_by_mnemonic(state,
  1151. 0x90000170, 4, 3, pad_mux_value);
  1152. status |= update_by_mnemonic(state,
  1153. 0x90000170, 8, 3, pad_mux_value);
  1154. status |= update_by_mnemonic(state,
  1155. 0x90000170, 12, 3, pad_mux_value);
  1156. status |= update_by_mnemonic(state,
  1157. 0x90000170, 16, 3, pad_mux_value);
  1158. status |= update_by_mnemonic(state,
  1159. 0x90000170, 20, 3, pad_mux_value);
  1160. status |= update_by_mnemonic(state,
  1161. 0x90000170, 24, 3, pad_mux_value);
  1162. status |= update_by_mnemonic(state,
  1163. 0x90000170, 28, 3, pad_mux_value);
  1164. status |= update_by_mnemonic(state,
  1165. 0x90000174, 0, 3, pad_mux_value);
  1166. status |= update_by_mnemonic(state,
  1167. 0x90000174, 4, 3, pad_mux_value);
  1168. status |= update_by_mnemonic(state,
  1169. 0x90000174, 8, 3, pad_mux_value);
  1170. status |= update_by_mnemonic(state,
  1171. 0x90000174, 12, 3, pad_mux_value);
  1172. break;
  1173. }
  1174. return status;
  1175. }
  1176. static int set_drive_strength(struct mxl *state,
  1177. enum MXL_HYDRA_TS_DRIVE_STRENGTH_E ts_drive_strength)
  1178. {
  1179. int stat = 0;
  1180. u32 val;
  1181. read_register(state, 0x90000194, &val);
  1182. dev_info(state->i2cdev, "DIGIO = %08x\n", val);
  1183. dev_info(state->i2cdev, "set drive_strength = %u\n", ts_drive_strength);
  1184. stat |= update_by_mnemonic(state, 0x90000194, 0, 3, ts_drive_strength);
  1185. stat |= update_by_mnemonic(state, 0x90000194, 20, 3, ts_drive_strength);
  1186. stat |= update_by_mnemonic(state, 0x90000194, 24, 3, ts_drive_strength);
  1187. stat |= update_by_mnemonic(state, 0x90000198, 12, 3, ts_drive_strength);
  1188. stat |= update_by_mnemonic(state, 0x90000198, 16, 3, ts_drive_strength);
  1189. stat |= update_by_mnemonic(state, 0x90000198, 20, 3, ts_drive_strength);
  1190. stat |= update_by_mnemonic(state, 0x90000198, 24, 3, ts_drive_strength);
  1191. stat |= update_by_mnemonic(state, 0x9000019C, 0, 3, ts_drive_strength);
  1192. stat |= update_by_mnemonic(state, 0x9000019C, 4, 3, ts_drive_strength);
  1193. stat |= update_by_mnemonic(state, 0x9000019C, 8, 3, ts_drive_strength);
  1194. stat |= update_by_mnemonic(state, 0x9000019C, 24, 3, ts_drive_strength);
  1195. stat |= update_by_mnemonic(state, 0x9000019C, 28, 3, ts_drive_strength);
  1196. stat |= update_by_mnemonic(state, 0x900001A0, 0, 3, ts_drive_strength);
  1197. stat |= update_by_mnemonic(state, 0x900001A0, 4, 3, ts_drive_strength);
  1198. stat |= update_by_mnemonic(state, 0x900001A0, 20, 3, ts_drive_strength);
  1199. stat |= update_by_mnemonic(state, 0x900001A0, 24, 3, ts_drive_strength);
  1200. stat |= update_by_mnemonic(state, 0x900001A0, 28, 3, ts_drive_strength);
  1201. return stat;
  1202. }
  1203. static int enable_tuner(struct mxl *state, u32 tuner, u32 enable)
  1204. {
  1205. int stat = 0;
  1206. struct MXL_HYDRA_TUNER_CMD ctrl_tuner_cmd;
  1207. u8 cmd_size = sizeof(ctrl_tuner_cmd);
  1208. u8 cmd_buff[MXL_HYDRA_OEM_MAX_CMD_BUFF_LEN];
  1209. u32 val, count = 10;
  1210. ctrl_tuner_cmd.tuner_id = tuner;
  1211. ctrl_tuner_cmd.enable = enable;
  1212. BUILD_HYDRA_CMD(MXL_HYDRA_TUNER_ACTIVATE_CMD, MXL_CMD_WRITE,
  1213. cmd_size, &ctrl_tuner_cmd, cmd_buff);
  1214. stat = send_command(state, cmd_size + MXL_HYDRA_CMD_HEADER_SIZE,
  1215. &cmd_buff[0]);
  1216. if (stat)
  1217. return stat;
  1218. read_register(state, HYDRA_TUNER_ENABLE_COMPLETE, &val);
  1219. while (--count && ((val >> tuner) & 1) != enable) {
  1220. msleep(20);
  1221. read_register(state, HYDRA_TUNER_ENABLE_COMPLETE, &val);
  1222. }
  1223. if (!count)
  1224. return -1;
  1225. read_register(state, HYDRA_TUNER_ENABLE_COMPLETE, &val);
  1226. dev_dbg(state->i2cdev, "tuner %u ready = %u\n",
  1227. tuner, (val >> tuner) & 1);
  1228. return 0;
  1229. }
  1230. static int config_ts(struct mxl *state, enum MXL_HYDRA_DEMOD_ID_E demod_id,
  1231. struct MXL_HYDRA_MPEGOUT_PARAM_T *mpeg_out_param_ptr)
  1232. {
  1233. int status = 0;
  1234. u32 nco_count_min = 0;
  1235. u32 clk_type = 0;
  1236. struct MXL_REG_FIELD_T xpt_sync_polarity[MXL_HYDRA_DEMOD_MAX] = {
  1237. {0x90700010, 8, 1}, {0x90700010, 9, 1},
  1238. {0x90700010, 10, 1}, {0x90700010, 11, 1},
  1239. {0x90700010, 12, 1}, {0x90700010, 13, 1},
  1240. {0x90700010, 14, 1}, {0x90700010, 15, 1} };
  1241. struct MXL_REG_FIELD_T xpt_clock_polarity[MXL_HYDRA_DEMOD_MAX] = {
  1242. {0x90700010, 16, 1}, {0x90700010, 17, 1},
  1243. {0x90700010, 18, 1}, {0x90700010, 19, 1},
  1244. {0x90700010, 20, 1}, {0x90700010, 21, 1},
  1245. {0x90700010, 22, 1}, {0x90700010, 23, 1} };
  1246. struct MXL_REG_FIELD_T xpt_valid_polarity[MXL_HYDRA_DEMOD_MAX] = {
  1247. {0x90700014, 0, 1}, {0x90700014, 1, 1},
  1248. {0x90700014, 2, 1}, {0x90700014, 3, 1},
  1249. {0x90700014, 4, 1}, {0x90700014, 5, 1},
  1250. {0x90700014, 6, 1}, {0x90700014, 7, 1} };
  1251. struct MXL_REG_FIELD_T xpt_ts_clock_phase[MXL_HYDRA_DEMOD_MAX] = {
  1252. {0x90700018, 0, 3}, {0x90700018, 4, 3},
  1253. {0x90700018, 8, 3}, {0x90700018, 12, 3},
  1254. {0x90700018, 16, 3}, {0x90700018, 20, 3},
  1255. {0x90700018, 24, 3}, {0x90700018, 28, 3} };
  1256. struct MXL_REG_FIELD_T xpt_lsb_first[MXL_HYDRA_DEMOD_MAX] = {
  1257. {0x9070000C, 16, 1}, {0x9070000C, 17, 1},
  1258. {0x9070000C, 18, 1}, {0x9070000C, 19, 1},
  1259. {0x9070000C, 20, 1}, {0x9070000C, 21, 1},
  1260. {0x9070000C, 22, 1}, {0x9070000C, 23, 1} };
  1261. struct MXL_REG_FIELD_T xpt_sync_byte[MXL_HYDRA_DEMOD_MAX] = {
  1262. {0x90700010, 0, 1}, {0x90700010, 1, 1},
  1263. {0x90700010, 2, 1}, {0x90700010, 3, 1},
  1264. {0x90700010, 4, 1}, {0x90700010, 5, 1},
  1265. {0x90700010, 6, 1}, {0x90700010, 7, 1} };
  1266. struct MXL_REG_FIELD_T xpt_enable_output[MXL_HYDRA_DEMOD_MAX] = {
  1267. {0x9070000C, 0, 1}, {0x9070000C, 1, 1},
  1268. {0x9070000C, 2, 1}, {0x9070000C, 3, 1},
  1269. {0x9070000C, 4, 1}, {0x9070000C, 5, 1},
  1270. {0x9070000C, 6, 1}, {0x9070000C, 7, 1} };
  1271. struct MXL_REG_FIELD_T xpt_err_replace_sync[MXL_HYDRA_DEMOD_MAX] = {
  1272. {0x9070000C, 24, 1}, {0x9070000C, 25, 1},
  1273. {0x9070000C, 26, 1}, {0x9070000C, 27, 1},
  1274. {0x9070000C, 28, 1}, {0x9070000C, 29, 1},
  1275. {0x9070000C, 30, 1}, {0x9070000C, 31, 1} };
  1276. struct MXL_REG_FIELD_T xpt_err_replace_valid[MXL_HYDRA_DEMOD_MAX] = {
  1277. {0x90700014, 8, 1}, {0x90700014, 9, 1},
  1278. {0x90700014, 10, 1}, {0x90700014, 11, 1},
  1279. {0x90700014, 12, 1}, {0x90700014, 13, 1},
  1280. {0x90700014, 14, 1}, {0x90700014, 15, 1} };
  1281. struct MXL_REG_FIELD_T xpt_continuous_clock[MXL_HYDRA_DEMOD_MAX] = {
  1282. {0x907001D4, 0, 1}, {0x907001D4, 1, 1},
  1283. {0x907001D4, 2, 1}, {0x907001D4, 3, 1},
  1284. {0x907001D4, 4, 1}, {0x907001D4, 5, 1},
  1285. {0x907001D4, 6, 1}, {0x907001D4, 7, 1} };
  1286. struct MXL_REG_FIELD_T xpt_nco_clock_rate[MXL_HYDRA_DEMOD_MAX] = {
  1287. {0x90700044, 16, 80}, {0x90700044, 16, 81},
  1288. {0x90700044, 16, 82}, {0x90700044, 16, 83},
  1289. {0x90700044, 16, 84}, {0x90700044, 16, 85},
  1290. {0x90700044, 16, 86}, {0x90700044, 16, 87} };
  1291. demod_id = state->base->ts_map[demod_id];
  1292. if (mpeg_out_param_ptr->enable == MXL_ENABLE) {
  1293. if (mpeg_out_param_ptr->mpeg_mode ==
  1294. MXL_HYDRA_MPEG_MODE_PARALLEL) {
  1295. } else {
  1296. cfg_ts_pad_mux(state, MXL_TRUE);
  1297. update_by_mnemonic(state,
  1298. 0x90700010, 27, 1, MXL_FALSE);
  1299. }
  1300. }
  1301. nco_count_min =
  1302. (u32)(MXL_HYDRA_NCO_CLK / mpeg_out_param_ptr->max_mpeg_clk_rate);
  1303. if (state->base->chipversion >= 2) {
  1304. status |= update_by_mnemonic(state,
  1305. xpt_nco_clock_rate[demod_id].reg_addr, /* Reg Addr */
  1306. xpt_nco_clock_rate[demod_id].lsb_pos, /* LSB pos */
  1307. xpt_nco_clock_rate[demod_id].num_of_bits, /* Num of bits */
  1308. nco_count_min); /* Data */
  1309. } else
  1310. update_by_mnemonic(state, 0x90700044, 16, 8, nco_count_min);
  1311. if (mpeg_out_param_ptr->mpeg_clk_type == MXL_HYDRA_MPEG_CLK_CONTINUOUS)
  1312. clk_type = 1;
  1313. if (mpeg_out_param_ptr->mpeg_mode < MXL_HYDRA_MPEG_MODE_PARALLEL) {
  1314. status |= update_by_mnemonic(state,
  1315. xpt_continuous_clock[demod_id].reg_addr,
  1316. xpt_continuous_clock[demod_id].lsb_pos,
  1317. xpt_continuous_clock[demod_id].num_of_bits,
  1318. clk_type);
  1319. } else
  1320. update_by_mnemonic(state, 0x907001D4, 8, 1, clk_type);
  1321. status |= update_by_mnemonic(state,
  1322. xpt_sync_polarity[demod_id].reg_addr,
  1323. xpt_sync_polarity[demod_id].lsb_pos,
  1324. xpt_sync_polarity[demod_id].num_of_bits,
  1325. mpeg_out_param_ptr->mpeg_sync_pol);
  1326. status |= update_by_mnemonic(state,
  1327. xpt_valid_polarity[demod_id].reg_addr,
  1328. xpt_valid_polarity[demod_id].lsb_pos,
  1329. xpt_valid_polarity[demod_id].num_of_bits,
  1330. mpeg_out_param_ptr->mpeg_valid_pol);
  1331. status |= update_by_mnemonic(state,
  1332. xpt_clock_polarity[demod_id].reg_addr,
  1333. xpt_clock_polarity[demod_id].lsb_pos,
  1334. xpt_clock_polarity[demod_id].num_of_bits,
  1335. mpeg_out_param_ptr->mpeg_clk_pol);
  1336. status |= update_by_mnemonic(state,
  1337. xpt_sync_byte[demod_id].reg_addr,
  1338. xpt_sync_byte[demod_id].lsb_pos,
  1339. xpt_sync_byte[demod_id].num_of_bits,
  1340. mpeg_out_param_ptr->mpeg_sync_pulse_width);
  1341. status |= update_by_mnemonic(state,
  1342. xpt_ts_clock_phase[demod_id].reg_addr,
  1343. xpt_ts_clock_phase[demod_id].lsb_pos,
  1344. xpt_ts_clock_phase[demod_id].num_of_bits,
  1345. mpeg_out_param_ptr->mpeg_clk_phase);
  1346. status |= update_by_mnemonic(state,
  1347. xpt_lsb_first[demod_id].reg_addr,
  1348. xpt_lsb_first[demod_id].lsb_pos,
  1349. xpt_lsb_first[demod_id].num_of_bits,
  1350. mpeg_out_param_ptr->lsb_or_msb_first);
  1351. switch (mpeg_out_param_ptr->mpeg_error_indication) {
  1352. case MXL_HYDRA_MPEG_ERR_REPLACE_SYNC:
  1353. status |= update_by_mnemonic(state,
  1354. xpt_err_replace_sync[demod_id].reg_addr,
  1355. xpt_err_replace_sync[demod_id].lsb_pos,
  1356. xpt_err_replace_sync[demod_id].num_of_bits,
  1357. MXL_TRUE);
  1358. status |= update_by_mnemonic(state,
  1359. xpt_err_replace_valid[demod_id].reg_addr,
  1360. xpt_err_replace_valid[demod_id].lsb_pos,
  1361. xpt_err_replace_valid[demod_id].num_of_bits,
  1362. MXL_FALSE);
  1363. break;
  1364. case MXL_HYDRA_MPEG_ERR_REPLACE_VALID:
  1365. status |= update_by_mnemonic(state,
  1366. xpt_err_replace_sync[demod_id].reg_addr,
  1367. xpt_err_replace_sync[demod_id].lsb_pos,
  1368. xpt_err_replace_sync[demod_id].num_of_bits,
  1369. MXL_FALSE);
  1370. status |= update_by_mnemonic(state,
  1371. xpt_err_replace_valid[demod_id].reg_addr,
  1372. xpt_err_replace_valid[demod_id].lsb_pos,
  1373. xpt_err_replace_valid[demod_id].num_of_bits,
  1374. MXL_TRUE);
  1375. break;
  1376. case MXL_HYDRA_MPEG_ERR_INDICATION_DISABLED:
  1377. default:
  1378. status |= update_by_mnemonic(state,
  1379. xpt_err_replace_sync[demod_id].reg_addr,
  1380. xpt_err_replace_sync[demod_id].lsb_pos,
  1381. xpt_err_replace_sync[demod_id].num_of_bits,
  1382. MXL_FALSE);
  1383. status |= update_by_mnemonic(state,
  1384. xpt_err_replace_valid[demod_id].reg_addr,
  1385. xpt_err_replace_valid[demod_id].lsb_pos,
  1386. xpt_err_replace_valid[demod_id].num_of_bits,
  1387. MXL_FALSE);
  1388. break;
  1389. }
  1390. if (mpeg_out_param_ptr->mpeg_mode != MXL_HYDRA_MPEG_MODE_PARALLEL) {
  1391. status |= update_by_mnemonic(state,
  1392. xpt_enable_output[demod_id].reg_addr,
  1393. xpt_enable_output[demod_id].lsb_pos,
  1394. xpt_enable_output[demod_id].num_of_bits,
  1395. mpeg_out_param_ptr->enable);
  1396. }
  1397. return status;
  1398. }
  1399. static int config_mux(struct mxl *state)
  1400. {
  1401. update_by_mnemonic(state, 0x9070000C, 0, 1, 0);
  1402. update_by_mnemonic(state, 0x9070000C, 1, 1, 0);
  1403. update_by_mnemonic(state, 0x9070000C, 2, 1, 0);
  1404. update_by_mnemonic(state, 0x9070000C, 3, 1, 0);
  1405. update_by_mnemonic(state, 0x9070000C, 4, 1, 0);
  1406. update_by_mnemonic(state, 0x9070000C, 5, 1, 0);
  1407. update_by_mnemonic(state, 0x9070000C, 6, 1, 0);
  1408. update_by_mnemonic(state, 0x9070000C, 7, 1, 0);
  1409. update_by_mnemonic(state, 0x90700008, 0, 2, 1);
  1410. update_by_mnemonic(state, 0x90700008, 2, 2, 1);
  1411. return 0;
  1412. }
  1413. static int load_fw(struct mxl *state, struct mxl5xx_cfg *cfg)
  1414. {
  1415. int stat = 0;
  1416. u8 *buf;
  1417. if (cfg->fw)
  1418. return firmware_download(state, cfg->fw, cfg->fw_len);
  1419. if (!cfg->fw_read)
  1420. return -1;
  1421. buf = vmalloc(0x40000);
  1422. if (!buf)
  1423. return -ENOMEM;
  1424. cfg->fw_read(cfg->fw_priv, buf, 0x40000);
  1425. stat = firmware_download(state, buf, 0x40000);
  1426. vfree(buf);
  1427. return stat;
  1428. }
  1429. static int validate_sku(struct mxl *state)
  1430. {
  1431. u32 pad_mux_bond = 0, prcm_chip_id = 0, prcm_so_cid = 0;
  1432. int status;
  1433. u32 type = state->base->type;
  1434. status = read_by_mnemonic(state, 0x90000190, 0, 3, &pad_mux_bond);
  1435. status |= read_by_mnemonic(state, 0x80030000, 0, 12, &prcm_chip_id);
  1436. status |= read_by_mnemonic(state, 0x80030004, 24, 8, &prcm_so_cid);
  1437. if (status)
  1438. return -1;
  1439. dev_info(state->i2cdev, "padMuxBond=%08x, prcmChipId=%08x, prcmSoCId=%08x\n",
  1440. pad_mux_bond, prcm_chip_id, prcm_so_cid);
  1441. if (prcm_chip_id != 0x560) {
  1442. switch (pad_mux_bond) {
  1443. case MXL_HYDRA_SKU_ID_581:
  1444. if (type == MXL_HYDRA_DEVICE_581)
  1445. return 0;
  1446. if (type == MXL_HYDRA_DEVICE_581S) {
  1447. state->base->type = MXL_HYDRA_DEVICE_581;
  1448. return 0;
  1449. }
  1450. break;
  1451. case MXL_HYDRA_SKU_ID_584:
  1452. if (type == MXL_HYDRA_DEVICE_584)
  1453. return 0;
  1454. break;
  1455. case MXL_HYDRA_SKU_ID_544:
  1456. if (type == MXL_HYDRA_DEVICE_544)
  1457. return 0;
  1458. if (type == MXL_HYDRA_DEVICE_542)
  1459. return 0;
  1460. break;
  1461. case MXL_HYDRA_SKU_ID_582:
  1462. if (type == MXL_HYDRA_DEVICE_582)
  1463. return 0;
  1464. break;
  1465. default:
  1466. return -1;
  1467. }
  1468. } else {
  1469. }
  1470. return -1;
  1471. }
  1472. static int get_fwinfo(struct mxl *state)
  1473. {
  1474. int status;
  1475. u32 val = 0;
  1476. status = read_by_mnemonic(state, 0x90000190, 0, 3, &val);
  1477. if (status)
  1478. return status;
  1479. dev_info(state->i2cdev, "chipID=%08x\n", val);
  1480. status = read_by_mnemonic(state, 0x80030004, 8, 8, &val);
  1481. if (status)
  1482. return status;
  1483. dev_info(state->i2cdev, "chipVer=%08x\n", val);
  1484. status = read_register(state, HYDRA_FIRMWARE_VERSION, &val);
  1485. if (status)
  1486. return status;
  1487. dev_info(state->i2cdev, "FWVer=%08x\n", val);
  1488. state->base->fwversion = val;
  1489. return status;
  1490. }
  1491. static u8 ts_map1_to_1[MXL_HYDRA_DEMOD_MAX] = {
  1492. MXL_HYDRA_DEMOD_ID_0,
  1493. MXL_HYDRA_DEMOD_ID_1,
  1494. MXL_HYDRA_DEMOD_ID_2,
  1495. MXL_HYDRA_DEMOD_ID_3,
  1496. MXL_HYDRA_DEMOD_ID_4,
  1497. MXL_HYDRA_DEMOD_ID_5,
  1498. MXL_HYDRA_DEMOD_ID_6,
  1499. MXL_HYDRA_DEMOD_ID_7,
  1500. };
  1501. static u8 ts_map54x[MXL_HYDRA_DEMOD_MAX] = {
  1502. MXL_HYDRA_DEMOD_ID_2,
  1503. MXL_HYDRA_DEMOD_ID_3,
  1504. MXL_HYDRA_DEMOD_ID_4,
  1505. MXL_HYDRA_DEMOD_ID_5,
  1506. MXL_HYDRA_DEMOD_MAX,
  1507. MXL_HYDRA_DEMOD_MAX,
  1508. MXL_HYDRA_DEMOD_MAX,
  1509. MXL_HYDRA_DEMOD_MAX,
  1510. };
  1511. static int probe(struct mxl *state, struct mxl5xx_cfg *cfg)
  1512. {
  1513. u32 chipver;
  1514. int fw, status, j;
  1515. struct MXL_HYDRA_MPEGOUT_PARAM_T mpeg_interface_cfg;
  1516. state->base->ts_map = ts_map1_to_1;
  1517. switch (state->base->type) {
  1518. case MXL_HYDRA_DEVICE_581:
  1519. case MXL_HYDRA_DEVICE_581S:
  1520. state->base->can_clkout = 1;
  1521. state->base->demod_num = 8;
  1522. state->base->tuner_num = 1;
  1523. state->base->sku_type = MXL_HYDRA_SKU_TYPE_581;
  1524. break;
  1525. case MXL_HYDRA_DEVICE_582:
  1526. state->base->can_clkout = 1;
  1527. state->base->demod_num = 8;
  1528. state->base->tuner_num = 3;
  1529. state->base->sku_type = MXL_HYDRA_SKU_TYPE_582;
  1530. break;
  1531. case MXL_HYDRA_DEVICE_585:
  1532. state->base->can_clkout = 0;
  1533. state->base->demod_num = 8;
  1534. state->base->tuner_num = 4;
  1535. state->base->sku_type = MXL_HYDRA_SKU_TYPE_585;
  1536. break;
  1537. case MXL_HYDRA_DEVICE_544:
  1538. state->base->can_clkout = 0;
  1539. state->base->demod_num = 4;
  1540. state->base->tuner_num = 4;
  1541. state->base->sku_type = MXL_HYDRA_SKU_TYPE_544;
  1542. state->base->ts_map = ts_map54x;
  1543. break;
  1544. case MXL_HYDRA_DEVICE_541:
  1545. case MXL_HYDRA_DEVICE_541S:
  1546. state->base->can_clkout = 0;
  1547. state->base->demod_num = 4;
  1548. state->base->tuner_num = 1;
  1549. state->base->sku_type = MXL_HYDRA_SKU_TYPE_541;
  1550. state->base->ts_map = ts_map54x;
  1551. break;
  1552. case MXL_HYDRA_DEVICE_561:
  1553. case MXL_HYDRA_DEVICE_561S:
  1554. state->base->can_clkout = 0;
  1555. state->base->demod_num = 6;
  1556. state->base->tuner_num = 1;
  1557. state->base->sku_type = MXL_HYDRA_SKU_TYPE_561;
  1558. break;
  1559. case MXL_HYDRA_DEVICE_568:
  1560. state->base->can_clkout = 0;
  1561. state->base->demod_num = 8;
  1562. state->base->tuner_num = 1;
  1563. state->base->chan_bond = 1;
  1564. state->base->sku_type = MXL_HYDRA_SKU_TYPE_568;
  1565. break;
  1566. case MXL_HYDRA_DEVICE_542:
  1567. state->base->can_clkout = 1;
  1568. state->base->demod_num = 4;
  1569. state->base->tuner_num = 3;
  1570. state->base->sku_type = MXL_HYDRA_SKU_TYPE_542;
  1571. state->base->ts_map = ts_map54x;
  1572. break;
  1573. case MXL_HYDRA_DEVICE_TEST:
  1574. case MXL_HYDRA_DEVICE_584:
  1575. default:
  1576. state->base->can_clkout = 0;
  1577. state->base->demod_num = 8;
  1578. state->base->tuner_num = 4;
  1579. state->base->sku_type = MXL_HYDRA_SKU_TYPE_584;
  1580. break;
  1581. }
  1582. status = validate_sku(state);
  1583. if (status)
  1584. return status;
  1585. update_by_mnemonic(state, 0x80030014, 9, 1, 1);
  1586. update_by_mnemonic(state, 0x8003003C, 12, 1, 1);
  1587. status = read_by_mnemonic(state, 0x80030000, 12, 4, &chipver);
  1588. if (status)
  1589. state->base->chipversion = 0;
  1590. else
  1591. state->base->chipversion = (chipver == 2) ? 2 : 1;
  1592. dev_info(state->i2cdev, "Hydra chip version %u\n",
  1593. state->base->chipversion);
  1594. cfg_dev_xtal(state, cfg->clk, cfg->cap, 0);
  1595. fw = firmware_is_alive(state);
  1596. if (!fw) {
  1597. status = load_fw(state, cfg);
  1598. if (status)
  1599. return status;
  1600. }
  1601. get_fwinfo(state);
  1602. config_mux(state);
  1603. mpeg_interface_cfg.enable = MXL_ENABLE;
  1604. mpeg_interface_cfg.lsb_or_msb_first = MXL_HYDRA_MPEG_SERIAL_MSB_1ST;
  1605. /* supports only (0-104&139)MHz */
  1606. if (cfg->ts_clk)
  1607. mpeg_interface_cfg.max_mpeg_clk_rate = cfg->ts_clk;
  1608. else
  1609. mpeg_interface_cfg.max_mpeg_clk_rate = 69; /* 139; */
  1610. mpeg_interface_cfg.mpeg_clk_phase = MXL_HYDRA_MPEG_CLK_PHASE_SHIFT_0_DEG;
  1611. mpeg_interface_cfg.mpeg_clk_pol = MXL_HYDRA_MPEG_CLK_IN_PHASE;
  1612. /* MXL_HYDRA_MPEG_CLK_GAPPED; */
  1613. mpeg_interface_cfg.mpeg_clk_type = MXL_HYDRA_MPEG_CLK_CONTINUOUS;
  1614. mpeg_interface_cfg.mpeg_error_indication =
  1615. MXL_HYDRA_MPEG_ERR_INDICATION_DISABLED;
  1616. mpeg_interface_cfg.mpeg_mode = MXL_HYDRA_MPEG_MODE_SERIAL_3_WIRE;
  1617. mpeg_interface_cfg.mpeg_sync_pol = MXL_HYDRA_MPEG_ACTIVE_HIGH;
  1618. mpeg_interface_cfg.mpeg_sync_pulse_width = MXL_HYDRA_MPEG_SYNC_WIDTH_BIT;
  1619. mpeg_interface_cfg.mpeg_valid_pol = MXL_HYDRA_MPEG_ACTIVE_HIGH;
  1620. for (j = 0; j < state->base->demod_num; j++) {
  1621. status = config_ts(state, (enum MXL_HYDRA_DEMOD_ID_E) j,
  1622. &mpeg_interface_cfg);
  1623. if (status)
  1624. return status;
  1625. }
  1626. set_drive_strength(state, 1);
  1627. return 0;
  1628. }
  1629. struct dvb_frontend *mxl5xx_attach(struct i2c_adapter *i2c,
  1630. struct mxl5xx_cfg *cfg, u32 demod, u32 tuner,
  1631. int (**fn_set_input)(struct dvb_frontend *, int))
  1632. {
  1633. struct mxl *state;
  1634. struct mxl_base *base;
  1635. state = kzalloc(sizeof(struct mxl), GFP_KERNEL);
  1636. if (!state)
  1637. return NULL;
  1638. state->demod = demod;
  1639. state->tuner = tuner;
  1640. state->tuner_in_use = 0xffffffff;
  1641. state->i2cdev = &i2c->dev;
  1642. base = match_base(i2c, cfg->adr);
  1643. if (base) {
  1644. base->count++;
  1645. if (base->count > base->demod_num)
  1646. goto fail;
  1647. state->base = base;
  1648. } else {
  1649. base = kzalloc(sizeof(struct mxl_base), GFP_KERNEL);
  1650. if (!base)
  1651. goto fail;
  1652. base->i2c = i2c;
  1653. base->adr = cfg->adr;
  1654. base->type = cfg->type;
  1655. base->count = 1;
  1656. mutex_init(&base->i2c_lock);
  1657. mutex_init(&base->status_lock);
  1658. mutex_init(&base->tune_lock);
  1659. INIT_LIST_HEAD(&base->mxls);
  1660. state->base = base;
  1661. if (probe(state, cfg) < 0) {
  1662. kfree(base);
  1663. goto fail;
  1664. }
  1665. list_add(&base->mxllist, &mxllist);
  1666. }
  1667. state->fe.ops = mxl_ops;
  1668. state->xbar[0] = 4;
  1669. state->xbar[1] = demod;
  1670. state->xbar[2] = 8;
  1671. state->fe.demodulator_priv = state;
  1672. *fn_set_input = set_input;
  1673. list_add(&state->mxl, &base->mxls);
  1674. return &state->fe;
  1675. fail:
  1676. kfree(state);
  1677. return NULL;
  1678. }
  1679. EXPORT_SYMBOL_GPL(mxl5xx_attach);
  1680. MODULE_DESCRIPTION("MaxLinear MxL5xx DVB-S/S2 tuner-demodulator driver");
  1681. MODULE_AUTHOR("Ralph and Marcus Metzler, Metzler Brothers Systementwicklung GbR");
  1682. MODULE_LICENSE("GPL v2");