dib9000.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Linux-DVB Driver for DiBcom's DiB9000 and demodulator-family.
  4. *
  5. * Copyright (C) 2005-10 DiBcom (http://www.dibcom.fr/)
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/i2c.h>
  10. #include <linux/mutex.h>
  11. #include <media/dvb_math.h>
  12. #include <media/dvb_frontend.h>
  13. #include "dib9000.h"
  14. #include "dibx000_common.h"
  15. static int debug;
  16. module_param(debug, int, 0644);
  17. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  18. #define dprintk(fmt, arg...) do { \
  19. if (debug) \
  20. printk(KERN_DEBUG pr_fmt("%s: " fmt), \
  21. __func__, ##arg); \
  22. } while (0)
  23. #define MAX_NUMBER_OF_FRONTENDS 6
  24. struct i2c_device {
  25. struct i2c_adapter *i2c_adap;
  26. u8 i2c_addr;
  27. u8 *i2c_read_buffer;
  28. u8 *i2c_write_buffer;
  29. };
  30. struct dib9000_pid_ctrl {
  31. #define DIB9000_PID_FILTER_CTRL 0
  32. #define DIB9000_PID_FILTER 1
  33. u8 cmd;
  34. u8 id;
  35. u16 pid;
  36. u8 onoff;
  37. };
  38. struct dib9000_state {
  39. struct i2c_device i2c;
  40. struct dibx000_i2c_master i2c_master;
  41. struct i2c_adapter tuner_adap;
  42. struct i2c_adapter component_bus;
  43. u16 revision;
  44. u8 reg_offs;
  45. enum frontend_tune_state tune_state;
  46. u32 status;
  47. struct dvb_frontend_parametersContext channel_status;
  48. u8 fe_id;
  49. #define DIB9000_GPIO_DEFAULT_DIRECTIONS 0xffff
  50. u16 gpio_dir;
  51. #define DIB9000_GPIO_DEFAULT_VALUES 0x0000
  52. u16 gpio_val;
  53. #define DIB9000_GPIO_DEFAULT_PWM_POS 0xffff
  54. u16 gpio_pwm_pos;
  55. union { /* common for all chips */
  56. struct {
  57. u8 mobile_mode:1;
  58. } host;
  59. struct {
  60. struct dib9000_fe_memory_map {
  61. u16 addr;
  62. u16 size;
  63. } fe_mm[18];
  64. u8 memcmd;
  65. struct mutex mbx_if_lock; /* to protect read/write operations */
  66. struct mutex mbx_lock; /* to protect the whole mailbox handling */
  67. struct mutex mem_lock; /* to protect the memory accesses */
  68. struct mutex mem_mbx_lock; /* to protect the memory-based mailbox */
  69. #define MBX_MAX_WORDS (256 - 200 - 2)
  70. #define DIB9000_MSG_CACHE_SIZE 2
  71. u16 message_cache[DIB9000_MSG_CACHE_SIZE][MBX_MAX_WORDS];
  72. u8 fw_is_running;
  73. } risc;
  74. } platform;
  75. union { /* common for all platforms */
  76. struct {
  77. struct dib9000_config cfg;
  78. } d9;
  79. } chip;
  80. struct dvb_frontend *fe[MAX_NUMBER_OF_FRONTENDS];
  81. u16 component_bus_speed;
  82. /* for the I2C transfer */
  83. struct i2c_msg msg[2];
  84. u8 i2c_write_buffer[255];
  85. u8 i2c_read_buffer[255];
  86. struct mutex demod_lock;
  87. u8 get_frontend_internal;
  88. struct dib9000_pid_ctrl pid_ctrl[10];
  89. s8 pid_ctrl_index; /* -1: empty list; -2: do not use the list */
  90. };
  91. static const u32 fe_info[44] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  92. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  93. 0, 0, 0, 0, 0, 0, 0, 0
  94. };
  95. enum dib9000_power_mode {
  96. DIB9000_POWER_ALL = 0,
  97. DIB9000_POWER_NO,
  98. DIB9000_POWER_INTERF_ANALOG_AGC,
  99. DIB9000_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD,
  100. DIB9000_POWER_COR4_CRY_ESRAM_MOUT_NUD,
  101. DIB9000_POWER_INTERFACE_ONLY,
  102. };
  103. enum dib9000_out_messages {
  104. OUT_MSG_HBM_ACK,
  105. OUT_MSG_HOST_BUF_FAIL,
  106. OUT_MSG_REQ_VERSION,
  107. OUT_MSG_BRIDGE_I2C_W,
  108. OUT_MSG_BRIDGE_I2C_R,
  109. OUT_MSG_BRIDGE_APB_W,
  110. OUT_MSG_BRIDGE_APB_R,
  111. OUT_MSG_SCAN_CHANNEL,
  112. OUT_MSG_MONIT_DEMOD,
  113. OUT_MSG_CONF_GPIO,
  114. OUT_MSG_DEBUG_HELP,
  115. OUT_MSG_SUBBAND_SEL,
  116. OUT_MSG_ENABLE_TIME_SLICE,
  117. OUT_MSG_FE_FW_DL,
  118. OUT_MSG_FE_CHANNEL_SEARCH,
  119. OUT_MSG_FE_CHANNEL_TUNE,
  120. OUT_MSG_FE_SLEEP,
  121. OUT_MSG_FE_SYNC,
  122. OUT_MSG_CTL_MONIT,
  123. OUT_MSG_CONF_SVC,
  124. OUT_MSG_SET_HBM,
  125. OUT_MSG_INIT_DEMOD,
  126. OUT_MSG_ENABLE_DIVERSITY,
  127. OUT_MSG_SET_OUTPUT_MODE,
  128. OUT_MSG_SET_PRIORITARY_CHANNEL,
  129. OUT_MSG_ACK_FRG,
  130. OUT_MSG_INIT_PMU,
  131. };
  132. enum dib9000_in_messages {
  133. IN_MSG_DATA,
  134. IN_MSG_FRAME_INFO,
  135. IN_MSG_CTL_MONIT,
  136. IN_MSG_ACK_FREE_ITEM,
  137. IN_MSG_DEBUG_BUF,
  138. IN_MSG_MPE_MONITOR,
  139. IN_MSG_RAWTS_MONITOR,
  140. IN_MSG_END_BRIDGE_I2C_RW,
  141. IN_MSG_END_BRIDGE_APB_RW,
  142. IN_MSG_VERSION,
  143. IN_MSG_END_OF_SCAN,
  144. IN_MSG_MONIT_DEMOD,
  145. IN_MSG_ERROR,
  146. IN_MSG_FE_FW_DL_DONE,
  147. IN_MSG_EVENT,
  148. IN_MSG_ACK_CHANGE_SVC,
  149. IN_MSG_HBM_PROF,
  150. };
  151. /* memory_access requests */
  152. #define FE_MM_W_CHANNEL 0
  153. #define FE_MM_W_FE_INFO 1
  154. #define FE_MM_RW_SYNC 2
  155. #define FE_SYNC_CHANNEL 1
  156. #define FE_SYNC_W_GENERIC_MONIT 2
  157. #define FE_SYNC_COMPONENT_ACCESS 3
  158. #define FE_MM_R_CHANNEL_SEARCH_STATE 3
  159. #define FE_MM_R_CHANNEL_UNION_CONTEXT 4
  160. #define FE_MM_R_FE_INFO 5
  161. #define FE_MM_R_FE_MONITOR 6
  162. #define FE_MM_W_CHANNEL_HEAD 7
  163. #define FE_MM_W_CHANNEL_UNION 8
  164. #define FE_MM_W_CHANNEL_CONTEXT 9
  165. #define FE_MM_R_CHANNEL_UNION 10
  166. #define FE_MM_R_CHANNEL_CONTEXT 11
  167. #define FE_MM_R_CHANNEL_TUNE_STATE 12
  168. #define FE_MM_R_GENERIC_MONITORING_SIZE 13
  169. #define FE_MM_W_GENERIC_MONITORING 14
  170. #define FE_MM_R_GENERIC_MONITORING 15
  171. #define FE_MM_W_COMPONENT_ACCESS 16
  172. #define FE_MM_RW_COMPONENT_ACCESS_BUFFER 17
  173. static int dib9000_risc_apb_access_read(struct dib9000_state *state, u32 address, u16 attribute, const u8 * tx, u32 txlen, u8 * b, u32 len);
  174. static int dib9000_risc_apb_access_write(struct dib9000_state *state, u32 address, u16 attribute, const u8 * b, u32 len);
  175. static u16 to_fw_output_mode(u16 mode)
  176. {
  177. switch (mode) {
  178. case OUTMODE_HIGH_Z:
  179. return 0;
  180. case OUTMODE_MPEG2_PAR_GATED_CLK:
  181. return 4;
  182. case OUTMODE_MPEG2_PAR_CONT_CLK:
  183. return 8;
  184. case OUTMODE_MPEG2_SERIAL:
  185. return 16;
  186. case OUTMODE_DIVERSITY:
  187. return 128;
  188. case OUTMODE_MPEG2_FIFO:
  189. return 2;
  190. case OUTMODE_ANALOG_ADC:
  191. return 1;
  192. default:
  193. return 0;
  194. }
  195. }
  196. static int dib9000_read16_attr(struct dib9000_state *state, u16 reg, u8 *b, u32 len, u16 attribute)
  197. {
  198. u32 chunk_size = 126;
  199. u32 l;
  200. int ret;
  201. if (state->platform.risc.fw_is_running && (reg < 1024))
  202. return dib9000_risc_apb_access_read(state, reg, attribute, NULL, 0, b, len);
  203. memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
  204. state->msg[0].addr = state->i2c.i2c_addr >> 1;
  205. state->msg[0].flags = 0;
  206. state->msg[0].buf = state->i2c_write_buffer;
  207. state->msg[0].len = 2;
  208. state->msg[1].addr = state->i2c.i2c_addr >> 1;
  209. state->msg[1].flags = I2C_M_RD;
  210. state->msg[1].buf = b;
  211. state->msg[1].len = len;
  212. state->i2c_write_buffer[0] = reg >> 8;
  213. state->i2c_write_buffer[1] = reg & 0xff;
  214. if (attribute & DATA_BUS_ACCESS_MODE_8BIT)
  215. state->i2c_write_buffer[0] |= (1 << 5);
  216. if (attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  217. state->i2c_write_buffer[0] |= (1 << 4);
  218. do {
  219. l = len < chunk_size ? len : chunk_size;
  220. state->msg[1].len = l;
  221. state->msg[1].buf = b;
  222. ret = i2c_transfer(state->i2c.i2c_adap, state->msg, 2) != 2 ? -EREMOTEIO : 0;
  223. if (ret != 0) {
  224. dprintk("i2c read error on %d\n", reg);
  225. return -EREMOTEIO;
  226. }
  227. b += l;
  228. len -= l;
  229. if (!(attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT))
  230. reg += l / 2;
  231. } while ((ret == 0) && len);
  232. return 0;
  233. }
  234. static u16 dib9000_i2c_read16(struct i2c_device *i2c, u16 reg)
  235. {
  236. struct i2c_msg msg[2] = {
  237. {.addr = i2c->i2c_addr >> 1, .flags = 0,
  238. .buf = i2c->i2c_write_buffer, .len = 2},
  239. {.addr = i2c->i2c_addr >> 1, .flags = I2C_M_RD,
  240. .buf = i2c->i2c_read_buffer, .len = 2},
  241. };
  242. i2c->i2c_write_buffer[0] = reg >> 8;
  243. i2c->i2c_write_buffer[1] = reg & 0xff;
  244. if (i2c_transfer(i2c->i2c_adap, msg, 2) != 2) {
  245. dprintk("read register %x error\n", reg);
  246. return 0;
  247. }
  248. return (i2c->i2c_read_buffer[0] << 8) | i2c->i2c_read_buffer[1];
  249. }
  250. static inline u16 dib9000_read_word(struct dib9000_state *state, u16 reg)
  251. {
  252. if (dib9000_read16_attr(state, reg, state->i2c_read_buffer, 2, 0) != 0)
  253. return 0;
  254. return (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
  255. }
  256. static inline u16 dib9000_read_word_attr(struct dib9000_state *state, u16 reg, u16 attribute)
  257. {
  258. if (dib9000_read16_attr(state, reg, state->i2c_read_buffer, 2,
  259. attribute) != 0)
  260. return 0;
  261. return (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
  262. }
  263. #define dib9000_read16_noinc_attr(state, reg, b, len, attribute) dib9000_read16_attr(state, reg, b, len, (attribute) | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  264. static int dib9000_write16_attr(struct dib9000_state *state, u16 reg, const u8 *buf, u32 len, u16 attribute)
  265. {
  266. u32 chunk_size = 126;
  267. u32 l;
  268. int ret;
  269. if (state->platform.risc.fw_is_running && (reg < 1024)) {
  270. if (dib9000_risc_apb_access_write
  271. (state, reg, DATA_BUS_ACCESS_MODE_16BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT | attribute, buf, len) != 0)
  272. return -EINVAL;
  273. return 0;
  274. }
  275. memset(&state->msg[0], 0, sizeof(struct i2c_msg));
  276. state->msg[0].addr = state->i2c.i2c_addr >> 1;
  277. state->msg[0].flags = 0;
  278. state->msg[0].buf = state->i2c_write_buffer;
  279. state->msg[0].len = len + 2;
  280. state->i2c_write_buffer[0] = (reg >> 8) & 0xff;
  281. state->i2c_write_buffer[1] = (reg) & 0xff;
  282. if (attribute & DATA_BUS_ACCESS_MODE_8BIT)
  283. state->i2c_write_buffer[0] |= (1 << 5);
  284. if (attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  285. state->i2c_write_buffer[0] |= (1 << 4);
  286. do {
  287. l = len < chunk_size ? len : chunk_size;
  288. state->msg[0].len = l + 2;
  289. memcpy(&state->i2c_write_buffer[2], buf, l);
  290. ret = i2c_transfer(state->i2c.i2c_adap, state->msg, 1) != 1 ? -EREMOTEIO : 0;
  291. buf += l;
  292. len -= l;
  293. if (!(attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT))
  294. reg += l / 2;
  295. } while ((ret == 0) && len);
  296. return ret;
  297. }
  298. static int dib9000_i2c_write16(struct i2c_device *i2c, u16 reg, u16 val)
  299. {
  300. struct i2c_msg msg = {
  301. .addr = i2c->i2c_addr >> 1, .flags = 0,
  302. .buf = i2c->i2c_write_buffer, .len = 4
  303. };
  304. i2c->i2c_write_buffer[0] = (reg >> 8) & 0xff;
  305. i2c->i2c_write_buffer[1] = reg & 0xff;
  306. i2c->i2c_write_buffer[2] = (val >> 8) & 0xff;
  307. i2c->i2c_write_buffer[3] = val & 0xff;
  308. return i2c_transfer(i2c->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  309. }
  310. static inline int dib9000_write_word(struct dib9000_state *state, u16 reg, u16 val)
  311. {
  312. u8 b[2] = { val >> 8, val & 0xff };
  313. return dib9000_write16_attr(state, reg, b, 2, 0);
  314. }
  315. static inline int dib9000_write_word_attr(struct dib9000_state *state, u16 reg, u16 val, u16 attribute)
  316. {
  317. u8 b[2] = { val >> 8, val & 0xff };
  318. return dib9000_write16_attr(state, reg, b, 2, attribute);
  319. }
  320. #define dib9000_write(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, 0)
  321. #define dib9000_write16_noinc(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  322. #define dib9000_write16_noinc_attr(state, reg, buf, len, attribute) dib9000_write16_attr(state, reg, buf, len, DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT | (attribute))
  323. #define dib9000_mbx_send(state, id, data, len) dib9000_mbx_send_attr(state, id, data, len, 0)
  324. #define dib9000_mbx_get_message(state, id, msg, len) dib9000_mbx_get_message_attr(state, id, msg, len, 0)
  325. #define MAC_IRQ (1 << 1)
  326. #define IRQ_POL_MSK (1 << 4)
  327. #define dib9000_risc_mem_read_chunks(state, b, len) dib9000_read16_attr(state, 1063, b, len, DATA_BUS_ACCESS_MODE_8BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  328. #define dib9000_risc_mem_write_chunks(state, buf, len) dib9000_write16_attr(state, 1063, buf, len, DATA_BUS_ACCESS_MODE_8BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  329. static void dib9000_risc_mem_setup_cmd(struct dib9000_state *state, u32 addr, u32 len, u8 reading)
  330. {
  331. u8 b[14] = { 0 };
  332. /* dprintk("%d memcmd: %d %d %d\n", state->fe_id, addr, addr+len, len); */
  333. /* b[0] = 0 << 7; */
  334. b[1] = 1;
  335. /* b[2] = 0; */
  336. /* b[3] = 0; */
  337. b[4] = (u8) (addr >> 8);
  338. b[5] = (u8) (addr & 0xff);
  339. /* b[10] = 0; */
  340. /* b[11] = 0; */
  341. b[12] = (u8) (addr >> 8);
  342. b[13] = (u8) (addr & 0xff);
  343. addr += len;
  344. /* b[6] = 0; */
  345. /* b[7] = 0; */
  346. b[8] = (u8) (addr >> 8);
  347. b[9] = (u8) (addr & 0xff);
  348. dib9000_write(state, 1056, b, 14);
  349. if (reading)
  350. dib9000_write_word(state, 1056, (1 << 15) | 1);
  351. state->platform.risc.memcmd = -1; /* if it was called directly reset it - to force a future setup-call to set it */
  352. }
  353. static void dib9000_risc_mem_setup(struct dib9000_state *state, u8 cmd)
  354. {
  355. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[cmd & 0x7f];
  356. /* decide whether we need to "refresh" the memory controller */
  357. if (state->platform.risc.memcmd == cmd && /* same command */
  358. !(cmd & 0x80 && m->size < 67)) /* and we do not want to read something with less than 67 bytes looping - working around a bug in the memory controller */
  359. return;
  360. dib9000_risc_mem_setup_cmd(state, m->addr, m->size, cmd & 0x80);
  361. state->platform.risc.memcmd = cmd;
  362. }
  363. static int dib9000_risc_mem_read(struct dib9000_state *state, u8 cmd, u8 * b, u16 len)
  364. {
  365. if (!state->platform.risc.fw_is_running)
  366. return -EIO;
  367. if (mutex_lock_interruptible(&state->platform.risc.mem_lock) < 0) {
  368. dprintk("could not get the lock\n");
  369. return -EINTR;
  370. }
  371. dib9000_risc_mem_setup(state, cmd | 0x80);
  372. dib9000_risc_mem_read_chunks(state, b, len);
  373. mutex_unlock(&state->platform.risc.mem_lock);
  374. return 0;
  375. }
  376. static int dib9000_risc_mem_write(struct dib9000_state *state, u8 cmd, const u8 * b)
  377. {
  378. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[cmd];
  379. if (!state->platform.risc.fw_is_running)
  380. return -EIO;
  381. if (mutex_lock_interruptible(&state->platform.risc.mem_lock) < 0) {
  382. dprintk("could not get the lock\n");
  383. return -EINTR;
  384. }
  385. dib9000_risc_mem_setup(state, cmd);
  386. dib9000_risc_mem_write_chunks(state, b, m->size);
  387. mutex_unlock(&state->platform.risc.mem_lock);
  388. return 0;
  389. }
  390. static int dib9000_firmware_download(struct dib9000_state *state, u8 risc_id, u16 key, const u8 * code, u32 len)
  391. {
  392. u16 offs;
  393. if (risc_id == 1)
  394. offs = 16;
  395. else
  396. offs = 0;
  397. /* config crtl reg */
  398. dib9000_write_word(state, 1024 + offs, 0x000f);
  399. dib9000_write_word(state, 1025 + offs, 0);
  400. dib9000_write_word(state, 1031 + offs, key);
  401. dprintk("going to download %dB of microcode\n", len);
  402. if (dib9000_write16_noinc(state, 1026 + offs, (u8 *) code, (u16) len) != 0) {
  403. dprintk("error while downloading microcode for RISC %c\n", 'A' + risc_id);
  404. return -EIO;
  405. }
  406. dprintk("Microcode for RISC %c loaded\n", 'A' + risc_id);
  407. return 0;
  408. }
  409. static int dib9000_mbx_host_init(struct dib9000_state *state, u8 risc_id)
  410. {
  411. u16 mbox_offs;
  412. u16 reset_reg;
  413. u16 tries = 1000;
  414. if (risc_id == 1)
  415. mbox_offs = 16;
  416. else
  417. mbox_offs = 0;
  418. /* Reset mailbox */
  419. dib9000_write_word(state, 1027 + mbox_offs, 0x8000);
  420. /* Read reset status */
  421. do {
  422. reset_reg = dib9000_read_word(state, 1027 + mbox_offs);
  423. msleep(100);
  424. } while ((reset_reg & 0x8000) && --tries);
  425. if (reset_reg & 0x8000) {
  426. dprintk("MBX: init ERROR, no response from RISC %c\n", 'A' + risc_id);
  427. return -EIO;
  428. }
  429. dprintk("MBX: initialized\n");
  430. return 0;
  431. }
  432. #define MAX_MAILBOX_TRY 100
  433. static int dib9000_mbx_send_attr(struct dib9000_state *state, u8 id, u16 * data, u8 len, u16 attr)
  434. {
  435. u8 *d, b[2];
  436. u16 tmp;
  437. u16 size;
  438. u32 i;
  439. int ret = 0;
  440. if (!state->platform.risc.fw_is_running)
  441. return -EINVAL;
  442. if (mutex_lock_interruptible(&state->platform.risc.mbx_if_lock) < 0) {
  443. dprintk("could not get the lock\n");
  444. return -EINTR;
  445. }
  446. tmp = MAX_MAILBOX_TRY;
  447. do {
  448. size = dib9000_read_word_attr(state, 1043, attr) & 0xff;
  449. if ((size + len + 1) > MBX_MAX_WORDS && --tmp) {
  450. dprintk("MBX: RISC mbx full, retrying\n");
  451. msleep(100);
  452. } else
  453. break;
  454. } while (1);
  455. /*dprintk( "MBX: size: %d\n", size); */
  456. if (tmp == 0) {
  457. ret = -EINVAL;
  458. goto out;
  459. }
  460. #ifdef DUMP_MSG
  461. dprintk("--> %02x %d %*ph\n", id, len + 1, len, data);
  462. #endif
  463. /* byte-order conversion - works on big (where it is not necessary) or little endian */
  464. d = (u8 *) data;
  465. for (i = 0; i < len; i++) {
  466. tmp = data[i];
  467. *d++ = tmp >> 8;
  468. *d++ = tmp & 0xff;
  469. }
  470. /* write msg */
  471. b[0] = id;
  472. b[1] = len + 1;
  473. if (dib9000_write16_noinc_attr(state, 1045, b, 2, attr) != 0 || dib9000_write16_noinc_attr(state, 1045, (u8 *) data, len * 2, attr) != 0) {
  474. ret = -EIO;
  475. goto out;
  476. }
  477. /* update register nb_mes_in_RX */
  478. ret = (u8) dib9000_write_word_attr(state, 1043, 1 << 14, attr);
  479. out:
  480. mutex_unlock(&state->platform.risc.mbx_if_lock);
  481. return ret;
  482. }
  483. static u8 dib9000_mbx_read(struct dib9000_state *state, u16 * data, u8 risc_id, u16 attr)
  484. {
  485. #ifdef DUMP_MSG
  486. u16 *d = data;
  487. #endif
  488. u16 tmp, i;
  489. u8 size;
  490. u8 mc_base;
  491. if (!state->platform.risc.fw_is_running)
  492. return 0;
  493. if (mutex_lock_interruptible(&state->platform.risc.mbx_if_lock) < 0) {
  494. dprintk("could not get the lock\n");
  495. return 0;
  496. }
  497. if (risc_id == 1)
  498. mc_base = 16;
  499. else
  500. mc_base = 0;
  501. /* Length and type in the first word */
  502. *data = dib9000_read_word_attr(state, 1029 + mc_base, attr);
  503. size = *data & 0xff;
  504. if (size <= MBX_MAX_WORDS) {
  505. data++;
  506. size--; /* Initial word already read */
  507. dib9000_read16_noinc_attr(state, 1029 + mc_base, (u8 *) data, size * 2, attr);
  508. /* to word conversion */
  509. for (i = 0; i < size; i++) {
  510. tmp = *data;
  511. *data = (tmp >> 8) | (tmp << 8);
  512. data++;
  513. }
  514. #ifdef DUMP_MSG
  515. dprintk("<--\n");
  516. for (i = 0; i < size + 1; i++)
  517. dprintk("%04x\n", d[i]);
  518. dprintk("\n");
  519. #endif
  520. } else {
  521. dprintk("MBX: message is too big for message cache (%d), flushing message\n", size);
  522. size--; /* Initial word already read */
  523. while (size--)
  524. dib9000_read16_noinc_attr(state, 1029 + mc_base, (u8 *) data, 2, attr);
  525. }
  526. /* Update register nb_mes_in_TX */
  527. dib9000_write_word_attr(state, 1028 + mc_base, 1 << 14, attr);
  528. mutex_unlock(&state->platform.risc.mbx_if_lock);
  529. return size + 1;
  530. }
  531. static int dib9000_risc_debug_buf(struct dib9000_state *state, u16 * data, u8 size)
  532. {
  533. u32 ts = data[1] << 16 | data[0];
  534. char *b = (char *)&data[2];
  535. b[2 * (size - 2) - 1] = '\0'; /* Bullet proof the buffer */
  536. if (*b == '~') {
  537. b++;
  538. dprintk("%s\n", b);
  539. } else
  540. dprintk("RISC%d: %d.%04d %s\n",
  541. state->fe_id,
  542. ts / 10000, ts % 10000, *b ? b : "<empty>");
  543. return 1;
  544. }
  545. static int dib9000_mbx_fetch_to_cache(struct dib9000_state *state, u16 attr)
  546. {
  547. int i;
  548. u8 size;
  549. u16 *block;
  550. /* find a free slot */
  551. for (i = 0; i < DIB9000_MSG_CACHE_SIZE; i++) {
  552. block = state->platform.risc.message_cache[i];
  553. if (*block == 0) {
  554. size = dib9000_mbx_read(state, block, 1, attr);
  555. /* dprintk( "MBX: fetched %04x message to cache\n", *block); */
  556. switch (*block >> 8) {
  557. case IN_MSG_DEBUG_BUF:
  558. dib9000_risc_debug_buf(state, block + 1, size); /* debug-messages are going to be printed right away */
  559. *block = 0; /* free the block */
  560. break;
  561. #if 0
  562. case IN_MSG_DATA: /* FE-TRACE */
  563. dib9000_risc_data_process(state, block + 1, size);
  564. *block = 0;
  565. break;
  566. #endif
  567. default:
  568. break;
  569. }
  570. return 1;
  571. }
  572. }
  573. dprintk("MBX: no free cache-slot found for new message...\n");
  574. return -1;
  575. }
  576. static u8 dib9000_mbx_count(struct dib9000_state *state, u8 risc_id, u16 attr)
  577. {
  578. if (risc_id == 0)
  579. return (u8) (dib9000_read_word_attr(state, 1028, attr) >> 10) & 0x1f; /* 5 bit field */
  580. else
  581. return (u8) (dib9000_read_word_attr(state, 1044, attr) >> 8) & 0x7f; /* 7 bit field */
  582. }
  583. static int dib9000_mbx_process(struct dib9000_state *state, u16 attr)
  584. {
  585. int ret = 0;
  586. if (!state->platform.risc.fw_is_running)
  587. return -1;
  588. if (mutex_lock_interruptible(&state->platform.risc.mbx_lock) < 0) {
  589. dprintk("could not get the lock\n");
  590. return -1;
  591. }
  592. if (dib9000_mbx_count(state, 1, attr)) /* 1=RiscB */
  593. ret = dib9000_mbx_fetch_to_cache(state, attr);
  594. dib9000_read_word_attr(state, 1229, attr); /* Clear the IRQ */
  595. /* if (tmp) */
  596. /* dprintk( "cleared IRQ: %x\n", tmp); */
  597. mutex_unlock(&state->platform.risc.mbx_lock);
  598. return ret;
  599. }
  600. static int dib9000_mbx_get_message_attr(struct dib9000_state *state, u16 id, u16 * msg, u8 * size, u16 attr)
  601. {
  602. u8 i;
  603. u16 *block;
  604. u16 timeout = 30;
  605. *msg = 0;
  606. do {
  607. /* dib9000_mbx_get_from_cache(); */
  608. for (i = 0; i < DIB9000_MSG_CACHE_SIZE; i++) {
  609. block = state->platform.risc.message_cache[i];
  610. if ((*block >> 8) == id) {
  611. *size = (*block & 0xff) - 1;
  612. memcpy(msg, block + 1, (*size) * 2);
  613. *block = 0; /* free the block */
  614. i = 0; /* signal that we found a message */
  615. break;
  616. }
  617. }
  618. if (i == 0)
  619. break;
  620. if (dib9000_mbx_process(state, attr) == -1) /* try to fetch one message - if any */
  621. return -1;
  622. } while (--timeout);
  623. if (timeout == 0) {
  624. dprintk("waiting for message %d timed out\n", id);
  625. return -1;
  626. }
  627. return i == 0;
  628. }
  629. static int dib9000_risc_check_version(struct dib9000_state *state)
  630. {
  631. u8 r[4];
  632. u8 size;
  633. u16 fw_version = 0;
  634. if (dib9000_mbx_send(state, OUT_MSG_REQ_VERSION, &fw_version, 1) != 0)
  635. return -EIO;
  636. if (dib9000_mbx_get_message(state, IN_MSG_VERSION, (u16 *) r, &size) < 0)
  637. return -EIO;
  638. fw_version = (r[0] << 8) | r[1];
  639. dprintk("RISC: ver: %d.%02d (IC: %d)\n", fw_version >> 10, fw_version & 0x3ff, (r[2] << 8) | r[3]);
  640. if ((fw_version >> 10) != 7)
  641. return -EINVAL;
  642. switch (fw_version & 0x3ff) {
  643. case 11:
  644. case 12:
  645. case 14:
  646. case 15:
  647. case 16:
  648. case 17:
  649. break;
  650. default:
  651. dprintk("RISC: invalid firmware version");
  652. return -EINVAL;
  653. }
  654. dprintk("RISC: valid firmware version");
  655. return 0;
  656. }
  657. static int dib9000_fw_boot(struct dib9000_state *state, const u8 * codeA, u32 lenA, const u8 * codeB, u32 lenB)
  658. {
  659. /* Reconfig pool mac ram */
  660. dib9000_write_word(state, 1225, 0x02); /* A: 8k C, 4 k D - B: 32k C 6 k D - IRAM 96k */
  661. dib9000_write_word(state, 1226, 0x05);
  662. /* Toggles IP crypto to Host APB interface. */
  663. dib9000_write_word(state, 1542, 1);
  664. /* Set jump and no jump in the dma box */
  665. dib9000_write_word(state, 1074, 0);
  666. dib9000_write_word(state, 1075, 0);
  667. /* Set MAC as APB Master. */
  668. dib9000_write_word(state, 1237, 0);
  669. /* Reset the RISCs */
  670. if (codeA != NULL)
  671. dib9000_write_word(state, 1024, 2);
  672. else
  673. dib9000_write_word(state, 1024, 15);
  674. if (codeB != NULL)
  675. dib9000_write_word(state, 1040, 2);
  676. if (codeA != NULL)
  677. dib9000_firmware_download(state, 0, 0x1234, codeA, lenA);
  678. if (codeB != NULL)
  679. dib9000_firmware_download(state, 1, 0x1234, codeB, lenB);
  680. /* Run the RISCs */
  681. if (codeA != NULL)
  682. dib9000_write_word(state, 1024, 0);
  683. if (codeB != NULL)
  684. dib9000_write_word(state, 1040, 0);
  685. if (codeA != NULL)
  686. if (dib9000_mbx_host_init(state, 0) != 0)
  687. return -EIO;
  688. if (codeB != NULL)
  689. if (dib9000_mbx_host_init(state, 1) != 0)
  690. return -EIO;
  691. msleep(100);
  692. state->platform.risc.fw_is_running = 1;
  693. if (dib9000_risc_check_version(state) != 0)
  694. return -EINVAL;
  695. state->platform.risc.memcmd = 0xff;
  696. return 0;
  697. }
  698. static u16 dib9000_identify(struct i2c_device *client)
  699. {
  700. u16 value;
  701. value = dib9000_i2c_read16(client, 896);
  702. if (value != 0x01b3) {
  703. dprintk("wrong Vendor ID (0x%x)\n", value);
  704. return 0;
  705. }
  706. value = dib9000_i2c_read16(client, 897);
  707. if (value != 0x4000 && value != 0x4001 && value != 0x4002 && value != 0x4003 && value != 0x4004 && value != 0x4005) {
  708. dprintk("wrong Device ID (0x%x)\n", value);
  709. return 0;
  710. }
  711. /* protect this driver to be used with 7000PC */
  712. if (value == 0x4000 && dib9000_i2c_read16(client, 769) == 0x4000) {
  713. dprintk("this driver does not work with DiB7000PC\n");
  714. return 0;
  715. }
  716. switch (value) {
  717. case 0x4000:
  718. dprintk("found DiB7000MA/PA/MB/PB\n");
  719. break;
  720. case 0x4001:
  721. dprintk("found DiB7000HC\n");
  722. break;
  723. case 0x4002:
  724. dprintk("found DiB7000MC\n");
  725. break;
  726. case 0x4003:
  727. dprintk("found DiB9000A\n");
  728. break;
  729. case 0x4004:
  730. dprintk("found DiB9000H\n");
  731. break;
  732. case 0x4005:
  733. dprintk("found DiB9000M\n");
  734. break;
  735. }
  736. return value;
  737. }
  738. static void dib9000_set_power_mode(struct dib9000_state *state, enum dib9000_power_mode mode)
  739. {
  740. /* by default everything is going to be powered off */
  741. u16 reg_903 = 0x3fff, reg_904 = 0xffff, reg_905 = 0xffff, reg_906;
  742. u8 offset;
  743. if (state->revision == 0x4003 || state->revision == 0x4004 || state->revision == 0x4005)
  744. offset = 1;
  745. else
  746. offset = 0;
  747. reg_906 = dib9000_read_word(state, 906 + offset) | 0x3; /* keep settings for RISC */
  748. /* now, depending on the requested mode, we power on */
  749. switch (mode) {
  750. /* power up everything in the demod */
  751. case DIB9000_POWER_ALL:
  752. reg_903 = 0x0000;
  753. reg_904 = 0x0000;
  754. reg_905 = 0x0000;
  755. reg_906 = 0x0000;
  756. break;
  757. /* just leave power on the control-interfaces: GPIO and (I2C or SDIO or SRAM) */
  758. case DIB9000_POWER_INTERFACE_ONLY: /* TODO power up either SDIO or I2C or SRAM */
  759. reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 2));
  760. break;
  761. case DIB9000_POWER_INTERF_ANALOG_AGC:
  762. reg_903 &= ~((1 << 15) | (1 << 14) | (1 << 11) | (1 << 10));
  763. reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 2));
  764. reg_906 &= ~((1 << 0));
  765. break;
  766. case DIB9000_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD:
  767. reg_903 = 0x0000;
  768. reg_904 = 0x801f;
  769. reg_905 = 0x0000;
  770. reg_906 &= ~((1 << 0));
  771. break;
  772. case DIB9000_POWER_COR4_CRY_ESRAM_MOUT_NUD:
  773. reg_903 = 0x0000;
  774. reg_904 = 0x8000;
  775. reg_905 = 0x010b;
  776. reg_906 &= ~((1 << 0));
  777. break;
  778. default:
  779. case DIB9000_POWER_NO:
  780. break;
  781. }
  782. /* always power down unused parts */
  783. if (!state->platform.host.mobile_mode)
  784. reg_904 |= (1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) | (1 << 1);
  785. /* P_sdio_select_clk = 0 on MC and after */
  786. if (state->revision != 0x4000)
  787. reg_906 <<= 1;
  788. dib9000_write_word(state, 903 + offset, reg_903);
  789. dib9000_write_word(state, 904 + offset, reg_904);
  790. dib9000_write_word(state, 905 + offset, reg_905);
  791. dib9000_write_word(state, 906 + offset, reg_906);
  792. }
  793. static int dib9000_fw_reset(struct dvb_frontend *fe)
  794. {
  795. struct dib9000_state *state = fe->demodulator_priv;
  796. dib9000_write_word(state, 1817, 0x0003);
  797. dib9000_write_word(state, 1227, 1);
  798. dib9000_write_word(state, 1227, 0);
  799. switch ((state->revision = dib9000_identify(&state->i2c))) {
  800. case 0x4003:
  801. case 0x4004:
  802. case 0x4005:
  803. state->reg_offs = 1;
  804. break;
  805. default:
  806. return -EINVAL;
  807. }
  808. /* reset the i2c-master to use the host interface */
  809. dibx000_reset_i2c_master(&state->i2c_master);
  810. dib9000_set_power_mode(state, DIB9000_POWER_ALL);
  811. /* unforce divstr regardless whether i2c enumeration was done or not */
  812. dib9000_write_word(state, 1794, dib9000_read_word(state, 1794) & ~(1 << 1));
  813. dib9000_write_word(state, 1796, 0);
  814. dib9000_write_word(state, 1805, 0x805);
  815. /* restart all parts */
  816. dib9000_write_word(state, 898, 0xffff);
  817. dib9000_write_word(state, 899, 0xffff);
  818. dib9000_write_word(state, 900, 0x0001);
  819. dib9000_write_word(state, 901, 0xff19);
  820. dib9000_write_word(state, 902, 0x003c);
  821. dib9000_write_word(state, 898, 0);
  822. dib9000_write_word(state, 899, 0);
  823. dib9000_write_word(state, 900, 0);
  824. dib9000_write_word(state, 901, 0);
  825. dib9000_write_word(state, 902, 0);
  826. dib9000_write_word(state, 911, state->chip.d9.cfg.if_drives);
  827. dib9000_set_power_mode(state, DIB9000_POWER_INTERFACE_ONLY);
  828. return 0;
  829. }
  830. static int dib9000_risc_apb_access_read(struct dib9000_state *state, u32 address, u16 attribute, const u8 * tx, u32 txlen, u8 * b, u32 len)
  831. {
  832. u16 mb[10];
  833. u8 i, s;
  834. if (address >= 1024 || !state->platform.risc.fw_is_running)
  835. return -EINVAL;
  836. /* dprintk( "APB access through rd fw %d %x\n", address, attribute); */
  837. mb[0] = (u16) address;
  838. mb[1] = len / 2;
  839. dib9000_mbx_send_attr(state, OUT_MSG_BRIDGE_APB_R, mb, 2, attribute);
  840. switch (dib9000_mbx_get_message_attr(state, IN_MSG_END_BRIDGE_APB_RW, mb, &s, attribute)) {
  841. case 1:
  842. s--;
  843. for (i = 0; i < s; i++) {
  844. b[i * 2] = (mb[i + 1] >> 8) & 0xff;
  845. b[i * 2 + 1] = (mb[i + 1]) & 0xff;
  846. }
  847. return 0;
  848. default:
  849. return -EIO;
  850. }
  851. return -EIO;
  852. }
  853. static int dib9000_risc_apb_access_write(struct dib9000_state *state, u32 address, u16 attribute, const u8 * b, u32 len)
  854. {
  855. u16 mb[10];
  856. u8 s, i;
  857. if (address >= 1024 || !state->platform.risc.fw_is_running)
  858. return -EINVAL;
  859. if (len > 18)
  860. return -EINVAL;
  861. /* dprintk( "APB access through wr fw %d %x\n", address, attribute); */
  862. mb[0] = (u16)address;
  863. for (i = 0; i + 1 < len; i += 2)
  864. mb[1 + i / 2] = b[i] << 8 | b[i + 1];
  865. if (len & 1)
  866. mb[1 + len / 2] = b[len - 1] << 8;
  867. dib9000_mbx_send_attr(state, OUT_MSG_BRIDGE_APB_W, mb, (3 + len) / 2, attribute);
  868. return dib9000_mbx_get_message_attr(state, IN_MSG_END_BRIDGE_APB_RW, mb, &s, attribute) == 1 ? 0 : -EINVAL;
  869. }
  870. static int dib9000_fw_memmbx_sync(struct dib9000_state *state, u8 i)
  871. {
  872. u8 index_loop = 10;
  873. if (!state->platform.risc.fw_is_running)
  874. return 0;
  875. dib9000_risc_mem_write(state, FE_MM_RW_SYNC, &i);
  876. do {
  877. dib9000_risc_mem_read(state, FE_MM_RW_SYNC, state->i2c_read_buffer, 1);
  878. } while (state->i2c_read_buffer[0] && index_loop--);
  879. if (index_loop > 0)
  880. return 0;
  881. return -EIO;
  882. }
  883. static int dib9000_fw_init(struct dib9000_state *state)
  884. {
  885. struct dibGPIOFunction *f;
  886. u16 b[40] = { 0 };
  887. u8 i;
  888. u8 size;
  889. if (dib9000_fw_boot(state, NULL, 0, state->chip.d9.cfg.microcode_B_fe_buffer, state->chip.d9.cfg.microcode_B_fe_size) != 0)
  890. return -EIO;
  891. /* initialize the firmware */
  892. for (i = 0; i < ARRAY_SIZE(state->chip.d9.cfg.gpio_function); i++) {
  893. f = &state->chip.d9.cfg.gpio_function[i];
  894. if (f->mask) {
  895. switch (f->function) {
  896. case BOARD_GPIO_FUNCTION_COMPONENT_ON:
  897. b[0] = (u16) f->mask;
  898. b[1] = (u16) f->direction;
  899. b[2] = (u16) f->value;
  900. break;
  901. case BOARD_GPIO_FUNCTION_COMPONENT_OFF:
  902. b[3] = (u16) f->mask;
  903. b[4] = (u16) f->direction;
  904. b[5] = (u16) f->value;
  905. break;
  906. }
  907. }
  908. }
  909. if (dib9000_mbx_send(state, OUT_MSG_CONF_GPIO, b, 15) != 0)
  910. return -EIO;
  911. /* subband */
  912. b[0] = state->chip.d9.cfg.subband.size; /* type == 0 -> GPIO - PWM not yet supported */
  913. for (i = 0; i < state->chip.d9.cfg.subband.size; i++) {
  914. b[1 + i * 4] = state->chip.d9.cfg.subband.subband[i].f_mhz;
  915. b[2 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.mask;
  916. b[3 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.direction;
  917. b[4 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.value;
  918. }
  919. b[1 + i * 4] = 0; /* fe_id */
  920. if (dib9000_mbx_send(state, OUT_MSG_SUBBAND_SEL, b, 2 + 4 * i) != 0)
  921. return -EIO;
  922. /* 0 - id, 1 - no_of_frontends */
  923. b[0] = (0 << 8) | 1;
  924. /* 0 = i2c-address demod, 0 = tuner */
  925. b[1] = (0 << 8) | (0);
  926. b[2] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000) >> 16) & 0xffff);
  927. b[3] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000)) & 0xffff);
  928. b[4] = (u16) ((state->chip.d9.cfg.vcxo_timer >> 16) & 0xffff);
  929. b[5] = (u16) ((state->chip.d9.cfg.vcxo_timer) & 0xffff);
  930. b[6] = (u16) ((state->chip.d9.cfg.timing_frequency >> 16) & 0xffff);
  931. b[7] = (u16) ((state->chip.d9.cfg.timing_frequency) & 0xffff);
  932. b[29] = state->chip.d9.cfg.if_drives;
  933. if (dib9000_mbx_send(state, OUT_MSG_INIT_DEMOD, b, ARRAY_SIZE(b)) != 0)
  934. return -EIO;
  935. if (dib9000_mbx_send(state, OUT_MSG_FE_FW_DL, NULL, 0) != 0)
  936. return -EIO;
  937. if (dib9000_mbx_get_message(state, IN_MSG_FE_FW_DL_DONE, b, &size) < 0)
  938. return -EIO;
  939. if (size > ARRAY_SIZE(b)) {
  940. dprintk("error : firmware returned %dbytes needed but the used buffer has only %dbytes\n Firmware init ABORTED", size,
  941. (int)ARRAY_SIZE(b));
  942. return -EINVAL;
  943. }
  944. for (i = 0; i < size; i += 2) {
  945. state->platform.risc.fe_mm[i / 2].addr = b[i + 0];
  946. state->platform.risc.fe_mm[i / 2].size = b[i + 1];
  947. }
  948. return 0;
  949. }
  950. static void dib9000_fw_set_channel_head(struct dib9000_state *state)
  951. {
  952. u8 b[9];
  953. u32 freq = state->fe[0]->dtv_property_cache.frequency / 1000;
  954. if (state->fe_id % 2)
  955. freq += 101;
  956. b[0] = (u8) ((freq >> 0) & 0xff);
  957. b[1] = (u8) ((freq >> 8) & 0xff);
  958. b[2] = (u8) ((freq >> 16) & 0xff);
  959. b[3] = (u8) ((freq >> 24) & 0xff);
  960. b[4] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 0) & 0xff);
  961. b[5] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 8) & 0xff);
  962. b[6] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 16) & 0xff);
  963. b[7] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 24) & 0xff);
  964. b[8] = 0x80; /* do not wait for CELL ID when doing autosearch */
  965. if (state->fe[0]->dtv_property_cache.delivery_system == SYS_DVBT)
  966. b[8] |= 1;
  967. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_HEAD, b);
  968. }
  969. static int dib9000_fw_get_channel(struct dvb_frontend *fe)
  970. {
  971. struct dib9000_state *state = fe->demodulator_priv;
  972. struct dibDVBTChannel {
  973. s8 spectrum_inversion;
  974. s8 nfft;
  975. s8 guard;
  976. s8 constellation;
  977. s8 hrch;
  978. s8 alpha;
  979. s8 code_rate_hp;
  980. s8 code_rate_lp;
  981. s8 select_hp;
  982. s8 intlv_native;
  983. };
  984. struct dibDVBTChannel *ch;
  985. int ret = 0;
  986. if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
  987. dprintk("could not get the lock\n");
  988. return -EINTR;
  989. }
  990. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
  991. ret = -EIO;
  992. goto error;
  993. }
  994. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_UNION,
  995. state->i2c_read_buffer, sizeof(struct dibDVBTChannel));
  996. ch = (struct dibDVBTChannel *)state->i2c_read_buffer;
  997. switch (ch->spectrum_inversion & 0x7) {
  998. case 1:
  999. state->fe[0]->dtv_property_cache.inversion = INVERSION_ON;
  1000. break;
  1001. case 0:
  1002. state->fe[0]->dtv_property_cache.inversion = INVERSION_OFF;
  1003. break;
  1004. default:
  1005. case -1:
  1006. state->fe[0]->dtv_property_cache.inversion = INVERSION_AUTO;
  1007. break;
  1008. }
  1009. switch (ch->nfft) {
  1010. case 0:
  1011. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
  1012. break;
  1013. case 2:
  1014. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_4K;
  1015. break;
  1016. case 1:
  1017. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  1018. break;
  1019. default:
  1020. case -1:
  1021. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_AUTO;
  1022. break;
  1023. }
  1024. switch (ch->guard) {
  1025. case 0:
  1026. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
  1027. break;
  1028. case 1:
  1029. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
  1030. break;
  1031. case 2:
  1032. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  1033. break;
  1034. case 3:
  1035. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
  1036. break;
  1037. default:
  1038. case -1:
  1039. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_AUTO;
  1040. break;
  1041. }
  1042. switch (ch->constellation) {
  1043. case 2:
  1044. state->fe[0]->dtv_property_cache.modulation = QAM_64;
  1045. break;
  1046. case 1:
  1047. state->fe[0]->dtv_property_cache.modulation = QAM_16;
  1048. break;
  1049. case 0:
  1050. state->fe[0]->dtv_property_cache.modulation = QPSK;
  1051. break;
  1052. default:
  1053. case -1:
  1054. state->fe[0]->dtv_property_cache.modulation = QAM_AUTO;
  1055. break;
  1056. }
  1057. switch (ch->hrch) {
  1058. case 0:
  1059. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_NONE;
  1060. break;
  1061. case 1:
  1062. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_1;
  1063. break;
  1064. default:
  1065. case -1:
  1066. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_AUTO;
  1067. break;
  1068. }
  1069. switch (ch->code_rate_hp) {
  1070. case 1:
  1071. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_1_2;
  1072. break;
  1073. case 2:
  1074. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_2_3;
  1075. break;
  1076. case 3:
  1077. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_3_4;
  1078. break;
  1079. case 5:
  1080. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_5_6;
  1081. break;
  1082. case 7:
  1083. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_7_8;
  1084. break;
  1085. default:
  1086. case -1:
  1087. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_AUTO;
  1088. break;
  1089. }
  1090. switch (ch->code_rate_lp) {
  1091. case 1:
  1092. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_1_2;
  1093. break;
  1094. case 2:
  1095. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_2_3;
  1096. break;
  1097. case 3:
  1098. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_3_4;
  1099. break;
  1100. case 5:
  1101. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_5_6;
  1102. break;
  1103. case 7:
  1104. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_7_8;
  1105. break;
  1106. default:
  1107. case -1:
  1108. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_AUTO;
  1109. break;
  1110. }
  1111. error:
  1112. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  1113. return ret;
  1114. }
  1115. static int dib9000_fw_set_channel_union(struct dvb_frontend *fe)
  1116. {
  1117. struct dib9000_state *state = fe->demodulator_priv;
  1118. struct dibDVBTChannel {
  1119. s8 spectrum_inversion;
  1120. s8 nfft;
  1121. s8 guard;
  1122. s8 constellation;
  1123. s8 hrch;
  1124. s8 alpha;
  1125. s8 code_rate_hp;
  1126. s8 code_rate_lp;
  1127. s8 select_hp;
  1128. s8 intlv_native;
  1129. };
  1130. struct dibDVBTChannel ch;
  1131. switch (state->fe[0]->dtv_property_cache.inversion) {
  1132. case INVERSION_ON:
  1133. ch.spectrum_inversion = 1;
  1134. break;
  1135. case INVERSION_OFF:
  1136. ch.spectrum_inversion = 0;
  1137. break;
  1138. default:
  1139. case INVERSION_AUTO:
  1140. ch.spectrum_inversion = -1;
  1141. break;
  1142. }
  1143. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  1144. case TRANSMISSION_MODE_2K:
  1145. ch.nfft = 0;
  1146. break;
  1147. case TRANSMISSION_MODE_4K:
  1148. ch.nfft = 2;
  1149. break;
  1150. case TRANSMISSION_MODE_8K:
  1151. ch.nfft = 1;
  1152. break;
  1153. default:
  1154. case TRANSMISSION_MODE_AUTO:
  1155. ch.nfft = 1;
  1156. break;
  1157. }
  1158. switch (state->fe[0]->dtv_property_cache.guard_interval) {
  1159. case GUARD_INTERVAL_1_32:
  1160. ch.guard = 0;
  1161. break;
  1162. case GUARD_INTERVAL_1_16:
  1163. ch.guard = 1;
  1164. break;
  1165. case GUARD_INTERVAL_1_8:
  1166. ch.guard = 2;
  1167. break;
  1168. case GUARD_INTERVAL_1_4:
  1169. ch.guard = 3;
  1170. break;
  1171. default:
  1172. case GUARD_INTERVAL_AUTO:
  1173. ch.guard = -1;
  1174. break;
  1175. }
  1176. switch (state->fe[0]->dtv_property_cache.modulation) {
  1177. case QAM_64:
  1178. ch.constellation = 2;
  1179. break;
  1180. case QAM_16:
  1181. ch.constellation = 1;
  1182. break;
  1183. case QPSK:
  1184. ch.constellation = 0;
  1185. break;
  1186. default:
  1187. case QAM_AUTO:
  1188. ch.constellation = -1;
  1189. break;
  1190. }
  1191. switch (state->fe[0]->dtv_property_cache.hierarchy) {
  1192. case HIERARCHY_NONE:
  1193. ch.hrch = 0;
  1194. break;
  1195. case HIERARCHY_1:
  1196. case HIERARCHY_2:
  1197. case HIERARCHY_4:
  1198. ch.hrch = 1;
  1199. break;
  1200. default:
  1201. case HIERARCHY_AUTO:
  1202. ch.hrch = -1;
  1203. break;
  1204. }
  1205. ch.alpha = 1;
  1206. switch (state->fe[0]->dtv_property_cache.code_rate_HP) {
  1207. case FEC_1_2:
  1208. ch.code_rate_hp = 1;
  1209. break;
  1210. case FEC_2_3:
  1211. ch.code_rate_hp = 2;
  1212. break;
  1213. case FEC_3_4:
  1214. ch.code_rate_hp = 3;
  1215. break;
  1216. case FEC_5_6:
  1217. ch.code_rate_hp = 5;
  1218. break;
  1219. case FEC_7_8:
  1220. ch.code_rate_hp = 7;
  1221. break;
  1222. default:
  1223. case FEC_AUTO:
  1224. ch.code_rate_hp = -1;
  1225. break;
  1226. }
  1227. switch (state->fe[0]->dtv_property_cache.code_rate_LP) {
  1228. case FEC_1_2:
  1229. ch.code_rate_lp = 1;
  1230. break;
  1231. case FEC_2_3:
  1232. ch.code_rate_lp = 2;
  1233. break;
  1234. case FEC_3_4:
  1235. ch.code_rate_lp = 3;
  1236. break;
  1237. case FEC_5_6:
  1238. ch.code_rate_lp = 5;
  1239. break;
  1240. case FEC_7_8:
  1241. ch.code_rate_lp = 7;
  1242. break;
  1243. default:
  1244. case FEC_AUTO:
  1245. ch.code_rate_lp = -1;
  1246. break;
  1247. }
  1248. ch.select_hp = 1;
  1249. ch.intlv_native = 1;
  1250. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_UNION, (u8 *) &ch);
  1251. return 0;
  1252. }
  1253. static int dib9000_fw_tune(struct dvb_frontend *fe)
  1254. {
  1255. struct dib9000_state *state = fe->demodulator_priv;
  1256. int ret = 10, search = state->channel_status.status == CHANNEL_STATUS_PARAMETERS_UNKNOWN;
  1257. s8 i;
  1258. switch (state->tune_state) {
  1259. case CT_DEMOD_START:
  1260. dib9000_fw_set_channel_head(state);
  1261. /* write the channel context - a channel is initialized to 0, so it is OK */
  1262. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_CONTEXT, (u8 *) fe_info);
  1263. dib9000_risc_mem_write(state, FE_MM_W_FE_INFO, (u8 *) fe_info);
  1264. if (search)
  1265. dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_SEARCH, NULL, 0);
  1266. else {
  1267. dib9000_fw_set_channel_union(fe);
  1268. dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_TUNE, NULL, 0);
  1269. }
  1270. state->tune_state = CT_DEMOD_STEP_1;
  1271. break;
  1272. case CT_DEMOD_STEP_1:
  1273. if (search)
  1274. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_SEARCH_STATE, state->i2c_read_buffer, 1);
  1275. else
  1276. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_TUNE_STATE, state->i2c_read_buffer, 1);
  1277. i = (s8)state->i2c_read_buffer[0];
  1278. switch (i) { /* something happened */
  1279. case 0:
  1280. break;
  1281. case -2: /* tps locks are "slower" than MPEG locks -> even in autosearch data is OK here */
  1282. if (search)
  1283. state->status = FE_STATUS_DEMOD_SUCCESS;
  1284. else {
  1285. state->tune_state = CT_DEMOD_STOP;
  1286. state->status = FE_STATUS_LOCKED;
  1287. }
  1288. break;
  1289. default:
  1290. state->status = FE_STATUS_TUNE_FAILED;
  1291. state->tune_state = CT_DEMOD_STOP;
  1292. break;
  1293. }
  1294. break;
  1295. default:
  1296. ret = FE_CALLBACK_TIME_NEVER;
  1297. break;
  1298. }
  1299. return ret;
  1300. }
  1301. static int dib9000_fw_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1302. {
  1303. struct dib9000_state *state = fe->demodulator_priv;
  1304. u16 mode = (u16) onoff;
  1305. return dib9000_mbx_send(state, OUT_MSG_ENABLE_DIVERSITY, &mode, 1);
  1306. }
  1307. static int dib9000_fw_set_output_mode(struct dvb_frontend *fe, int mode)
  1308. {
  1309. struct dib9000_state *state = fe->demodulator_priv;
  1310. u16 outreg, smo_mode;
  1311. dprintk("setting output mode for demod %p to %d\n", fe, mode);
  1312. switch (mode) {
  1313. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1314. outreg = (1 << 10); /* 0x0400 */
  1315. break;
  1316. case OUTMODE_MPEG2_PAR_CONT_CLK:
  1317. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  1318. break;
  1319. case OUTMODE_MPEG2_SERIAL:
  1320. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
  1321. break;
  1322. case OUTMODE_DIVERSITY:
  1323. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  1324. break;
  1325. case OUTMODE_MPEG2_FIFO:
  1326. outreg = (1 << 10) | (5 << 6);
  1327. break;
  1328. case OUTMODE_HIGH_Z:
  1329. outreg = 0;
  1330. break;
  1331. default:
  1332. dprintk("Unhandled output_mode passed to be set for demod %p\n", &state->fe[0]);
  1333. return -EINVAL;
  1334. }
  1335. dib9000_write_word(state, 1795, outreg);
  1336. switch (mode) {
  1337. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1338. case OUTMODE_MPEG2_PAR_CONT_CLK:
  1339. case OUTMODE_MPEG2_SERIAL:
  1340. case OUTMODE_MPEG2_FIFO:
  1341. smo_mode = (dib9000_read_word(state, 295) & 0x0010) | (1 << 1);
  1342. if (state->chip.d9.cfg.output_mpeg2_in_188_bytes)
  1343. smo_mode |= (1 << 5);
  1344. dib9000_write_word(state, 295, smo_mode);
  1345. break;
  1346. }
  1347. outreg = to_fw_output_mode(mode);
  1348. return dib9000_mbx_send(state, OUT_MSG_SET_OUTPUT_MODE, &outreg, 1);
  1349. }
  1350. static int dib9000_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1351. {
  1352. struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
  1353. u16 i, len, t, index_msg;
  1354. for (index_msg = 0; index_msg < num; index_msg++) {
  1355. if (msg[index_msg].flags & I2C_M_RD) { /* read */
  1356. len = msg[index_msg].len;
  1357. if (len > 16)
  1358. len = 16;
  1359. if (dib9000_read_word(state, 790) != 0)
  1360. dprintk("TunerITF: read busy\n");
  1361. dib9000_write_word(state, 784, (u16) (msg[index_msg].addr));
  1362. dib9000_write_word(state, 787, (len / 2) - 1);
  1363. dib9000_write_word(state, 786, 1); /* start read */
  1364. i = 1000;
  1365. while (dib9000_read_word(state, 790) != (len / 2) && i)
  1366. i--;
  1367. if (i == 0)
  1368. dprintk("TunerITF: read failed\n");
  1369. for (i = 0; i < len; i += 2) {
  1370. t = dib9000_read_word(state, 785);
  1371. msg[index_msg].buf[i] = (t >> 8) & 0xff;
  1372. msg[index_msg].buf[i + 1] = (t) & 0xff;
  1373. }
  1374. if (dib9000_read_word(state, 790) != 0)
  1375. dprintk("TunerITF: read more data than expected\n");
  1376. } else {
  1377. i = 1000;
  1378. while (dib9000_read_word(state, 789) && i)
  1379. i--;
  1380. if (i == 0)
  1381. dprintk("TunerITF: write busy\n");
  1382. len = msg[index_msg].len;
  1383. if (len > 16)
  1384. len = 16;
  1385. for (i = 0; i < len; i += 2)
  1386. dib9000_write_word(state, 785, (msg[index_msg].buf[i] << 8) | msg[index_msg].buf[i + 1]);
  1387. dib9000_write_word(state, 784, (u16) msg[index_msg].addr);
  1388. dib9000_write_word(state, 787, (len / 2) - 1);
  1389. dib9000_write_word(state, 786, 0); /* start write */
  1390. i = 1000;
  1391. while (dib9000_read_word(state, 791) > 0 && i)
  1392. i--;
  1393. if (i == 0)
  1394. dprintk("TunerITF: write failed\n");
  1395. }
  1396. }
  1397. return num;
  1398. }
  1399. int dib9000_fw_set_component_bus_speed(struct dvb_frontend *fe, u16 speed)
  1400. {
  1401. struct dib9000_state *state = fe->demodulator_priv;
  1402. state->component_bus_speed = speed;
  1403. return 0;
  1404. }
  1405. EXPORT_SYMBOL(dib9000_fw_set_component_bus_speed);
  1406. static int dib9000_fw_component_bus_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1407. {
  1408. struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
  1409. u8 type = 0; /* I2C */
  1410. u8 port = DIBX000_I2C_INTERFACE_GPIO_3_4;
  1411. u16 scl = state->component_bus_speed; /* SCL frequency */
  1412. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[FE_MM_RW_COMPONENT_ACCESS_BUFFER];
  1413. u8 p[13] = { 0 };
  1414. p[0] = type;
  1415. p[1] = port;
  1416. p[2] = msg[0].addr << 1;
  1417. p[3] = (u8) scl & 0xff; /* scl */
  1418. p[4] = (u8) (scl >> 8);
  1419. p[7] = 0;
  1420. p[8] = 0;
  1421. p[9] = (u8) (msg[0].len);
  1422. p[10] = (u8) (msg[0].len >> 8);
  1423. if ((num > 1) && (msg[1].flags & I2C_M_RD)) {
  1424. p[11] = (u8) (msg[1].len);
  1425. p[12] = (u8) (msg[1].len >> 8);
  1426. } else {
  1427. p[11] = 0;
  1428. p[12] = 0;
  1429. }
  1430. if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
  1431. dprintk("could not get the lock\n");
  1432. return 0;
  1433. }
  1434. dib9000_risc_mem_write(state, FE_MM_W_COMPONENT_ACCESS, p);
  1435. { /* write-part */
  1436. dib9000_risc_mem_setup_cmd(state, m->addr, msg[0].len, 0);
  1437. dib9000_risc_mem_write_chunks(state, msg[0].buf, msg[0].len);
  1438. }
  1439. /* do the transaction */
  1440. if (dib9000_fw_memmbx_sync(state, FE_SYNC_COMPONENT_ACCESS) < 0) {
  1441. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  1442. return 0;
  1443. }
  1444. /* read back any possible result */
  1445. if ((num > 1) && (msg[1].flags & I2C_M_RD))
  1446. dib9000_risc_mem_read(state, FE_MM_RW_COMPONENT_ACCESS_BUFFER, msg[1].buf, msg[1].len);
  1447. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  1448. return num;
  1449. }
  1450. static u32 dib9000_i2c_func(struct i2c_adapter *adapter)
  1451. {
  1452. return I2C_FUNC_I2C;
  1453. }
  1454. static const struct i2c_algorithm dib9000_tuner_algo = {
  1455. .master_xfer = dib9000_tuner_xfer,
  1456. .functionality = dib9000_i2c_func,
  1457. };
  1458. static const struct i2c_algorithm dib9000_component_bus_algo = {
  1459. .master_xfer = dib9000_fw_component_bus_xfer,
  1460. .functionality = dib9000_i2c_func,
  1461. };
  1462. struct i2c_adapter *dib9000_get_tuner_interface(struct dvb_frontend *fe)
  1463. {
  1464. struct dib9000_state *st = fe->demodulator_priv;
  1465. return &st->tuner_adap;
  1466. }
  1467. EXPORT_SYMBOL(dib9000_get_tuner_interface);
  1468. struct i2c_adapter *dib9000_get_component_bus_interface(struct dvb_frontend *fe)
  1469. {
  1470. struct dib9000_state *st = fe->demodulator_priv;
  1471. return &st->component_bus;
  1472. }
  1473. EXPORT_SYMBOL(dib9000_get_component_bus_interface);
  1474. struct i2c_adapter *dib9000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
  1475. {
  1476. struct dib9000_state *st = fe->demodulator_priv;
  1477. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1478. }
  1479. EXPORT_SYMBOL(dib9000_get_i2c_master);
  1480. int dib9000_set_i2c_adapter(struct dvb_frontend *fe, struct i2c_adapter *i2c)
  1481. {
  1482. struct dib9000_state *st = fe->demodulator_priv;
  1483. st->i2c.i2c_adap = i2c;
  1484. return 0;
  1485. }
  1486. EXPORT_SYMBOL(dib9000_set_i2c_adapter);
  1487. static int dib9000_cfg_gpio(struct dib9000_state *st, u8 num, u8 dir, u8 val)
  1488. {
  1489. st->gpio_dir = dib9000_read_word(st, 773);
  1490. st->gpio_dir &= ~(1 << num); /* reset the direction bit */
  1491. st->gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  1492. dib9000_write_word(st, 773, st->gpio_dir);
  1493. st->gpio_val = dib9000_read_word(st, 774);
  1494. st->gpio_val &= ~(1 << num); /* reset the direction bit */
  1495. st->gpio_val |= (val & 0x01) << num; /* set the new value */
  1496. dib9000_write_word(st, 774, st->gpio_val);
  1497. dprintk("gpio dir: %04x: gpio val: %04x\n", st->gpio_dir, st->gpio_val);
  1498. return 0;
  1499. }
  1500. int dib9000_set_gpio(struct dvb_frontend *fe, u8 num, u8 dir, u8 val)
  1501. {
  1502. struct dib9000_state *state = fe->demodulator_priv;
  1503. return dib9000_cfg_gpio(state, num, dir, val);
  1504. }
  1505. EXPORT_SYMBOL(dib9000_set_gpio);
  1506. int dib9000_fw_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1507. {
  1508. struct dib9000_state *state = fe->demodulator_priv;
  1509. u16 val;
  1510. int ret;
  1511. if ((state->pid_ctrl_index != -2) && (state->pid_ctrl_index < 9)) {
  1512. /* postpone the pid filtering cmd */
  1513. dprintk("pid filter cmd postpone\n");
  1514. state->pid_ctrl_index++;
  1515. state->pid_ctrl[state->pid_ctrl_index].cmd = DIB9000_PID_FILTER_CTRL;
  1516. state->pid_ctrl[state->pid_ctrl_index].onoff = onoff;
  1517. return 0;
  1518. }
  1519. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1520. dprintk("could not get the lock\n");
  1521. return -EINTR;
  1522. }
  1523. val = dib9000_read_word(state, 294 + 1) & 0xffef;
  1524. val |= (onoff & 0x1) << 4;
  1525. dprintk("PID filter enabled %d\n", onoff);
  1526. ret = dib9000_write_word(state, 294 + 1, val);
  1527. mutex_unlock(&state->demod_lock);
  1528. return ret;
  1529. }
  1530. EXPORT_SYMBOL(dib9000_fw_pid_filter_ctrl);
  1531. int dib9000_fw_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1532. {
  1533. struct dib9000_state *state = fe->demodulator_priv;
  1534. int ret;
  1535. if (state->pid_ctrl_index != -2) {
  1536. /* postpone the pid filtering cmd */
  1537. dprintk("pid filter postpone\n");
  1538. if (state->pid_ctrl_index < 9) {
  1539. state->pid_ctrl_index++;
  1540. state->pid_ctrl[state->pid_ctrl_index].cmd = DIB9000_PID_FILTER;
  1541. state->pid_ctrl[state->pid_ctrl_index].id = id;
  1542. state->pid_ctrl[state->pid_ctrl_index].pid = pid;
  1543. state->pid_ctrl[state->pid_ctrl_index].onoff = onoff;
  1544. } else
  1545. dprintk("can not add any more pid ctrl cmd\n");
  1546. return 0;
  1547. }
  1548. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1549. dprintk("could not get the lock\n");
  1550. return -EINTR;
  1551. }
  1552. dprintk("Index %x, PID %d, OnOff %d\n", id, pid, onoff);
  1553. ret = dib9000_write_word(state, 300 + 1 + id,
  1554. onoff ? (1 << 13) | pid : 0);
  1555. mutex_unlock(&state->demod_lock);
  1556. return ret;
  1557. }
  1558. EXPORT_SYMBOL(dib9000_fw_pid_filter);
  1559. int dib9000_firmware_post_pll_init(struct dvb_frontend *fe)
  1560. {
  1561. struct dib9000_state *state = fe->demodulator_priv;
  1562. return dib9000_fw_init(state);
  1563. }
  1564. EXPORT_SYMBOL(dib9000_firmware_post_pll_init);
  1565. static void dib9000_release(struct dvb_frontend *demod)
  1566. {
  1567. struct dib9000_state *st = demod->demodulator_priv;
  1568. u8 index_frontend;
  1569. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (st->fe[index_frontend] != NULL); index_frontend++)
  1570. dvb_frontend_detach(st->fe[index_frontend]);
  1571. dibx000_exit_i2c_master(&st->i2c_master);
  1572. i2c_del_adapter(&st->tuner_adap);
  1573. i2c_del_adapter(&st->component_bus);
  1574. kfree(st->fe[0]);
  1575. kfree(st);
  1576. }
  1577. static int dib9000_wakeup(struct dvb_frontend *fe)
  1578. {
  1579. return 0;
  1580. }
  1581. static int dib9000_sleep(struct dvb_frontend *fe)
  1582. {
  1583. struct dib9000_state *state = fe->demodulator_priv;
  1584. u8 index_frontend;
  1585. int ret = 0;
  1586. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1587. dprintk("could not get the lock\n");
  1588. return -EINTR;
  1589. }
  1590. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1591. ret = state->fe[index_frontend]->ops.sleep(state->fe[index_frontend]);
  1592. if (ret < 0)
  1593. goto error;
  1594. }
  1595. ret = dib9000_mbx_send(state, OUT_MSG_FE_SLEEP, NULL, 0);
  1596. error:
  1597. mutex_unlock(&state->demod_lock);
  1598. return ret;
  1599. }
  1600. static int dib9000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  1601. {
  1602. tune->min_delay_ms = 1000;
  1603. return 0;
  1604. }
  1605. static int dib9000_get_frontend(struct dvb_frontend *fe,
  1606. struct dtv_frontend_properties *c)
  1607. {
  1608. struct dib9000_state *state = fe->demodulator_priv;
  1609. u8 index_frontend, sub_index_frontend;
  1610. enum fe_status stat;
  1611. int ret = 0;
  1612. if (state->get_frontend_internal == 0) {
  1613. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1614. dprintk("could not get the lock\n");
  1615. return -EINTR;
  1616. }
  1617. }
  1618. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1619. state->fe[index_frontend]->ops.read_status(state->fe[index_frontend], &stat);
  1620. if (stat & FE_HAS_SYNC) {
  1621. dprintk("TPS lock on the slave%i\n", index_frontend);
  1622. /* synchronize the cache with the other frontends */
  1623. state->fe[index_frontend]->ops.get_frontend(state->fe[index_frontend], c);
  1624. for (sub_index_frontend = 0; (sub_index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[sub_index_frontend] != NULL);
  1625. sub_index_frontend++) {
  1626. if (sub_index_frontend != index_frontend) {
  1627. state->fe[sub_index_frontend]->dtv_property_cache.modulation =
  1628. state->fe[index_frontend]->dtv_property_cache.modulation;
  1629. state->fe[sub_index_frontend]->dtv_property_cache.inversion =
  1630. state->fe[index_frontend]->dtv_property_cache.inversion;
  1631. state->fe[sub_index_frontend]->dtv_property_cache.transmission_mode =
  1632. state->fe[index_frontend]->dtv_property_cache.transmission_mode;
  1633. state->fe[sub_index_frontend]->dtv_property_cache.guard_interval =
  1634. state->fe[index_frontend]->dtv_property_cache.guard_interval;
  1635. state->fe[sub_index_frontend]->dtv_property_cache.hierarchy =
  1636. state->fe[index_frontend]->dtv_property_cache.hierarchy;
  1637. state->fe[sub_index_frontend]->dtv_property_cache.code_rate_HP =
  1638. state->fe[index_frontend]->dtv_property_cache.code_rate_HP;
  1639. state->fe[sub_index_frontend]->dtv_property_cache.code_rate_LP =
  1640. state->fe[index_frontend]->dtv_property_cache.code_rate_LP;
  1641. state->fe[sub_index_frontend]->dtv_property_cache.rolloff =
  1642. state->fe[index_frontend]->dtv_property_cache.rolloff;
  1643. }
  1644. }
  1645. ret = 0;
  1646. goto return_value;
  1647. }
  1648. }
  1649. /* get the channel from master chip */
  1650. ret = dib9000_fw_get_channel(fe);
  1651. if (ret != 0)
  1652. goto return_value;
  1653. /* synchronize the cache with the other frontends */
  1654. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1655. state->fe[index_frontend]->dtv_property_cache.inversion = c->inversion;
  1656. state->fe[index_frontend]->dtv_property_cache.transmission_mode = c->transmission_mode;
  1657. state->fe[index_frontend]->dtv_property_cache.guard_interval = c->guard_interval;
  1658. state->fe[index_frontend]->dtv_property_cache.modulation = c->modulation;
  1659. state->fe[index_frontend]->dtv_property_cache.hierarchy = c->hierarchy;
  1660. state->fe[index_frontend]->dtv_property_cache.code_rate_HP = c->code_rate_HP;
  1661. state->fe[index_frontend]->dtv_property_cache.code_rate_LP = c->code_rate_LP;
  1662. state->fe[index_frontend]->dtv_property_cache.rolloff = c->rolloff;
  1663. }
  1664. ret = 0;
  1665. return_value:
  1666. if (state->get_frontend_internal == 0)
  1667. mutex_unlock(&state->demod_lock);
  1668. return ret;
  1669. }
  1670. static int dib9000_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  1671. {
  1672. struct dib9000_state *state = fe->demodulator_priv;
  1673. state->tune_state = tune_state;
  1674. if (tune_state == CT_DEMOD_START)
  1675. state->status = FE_STATUS_TUNE_PENDING;
  1676. return 0;
  1677. }
  1678. static u32 dib9000_get_status(struct dvb_frontend *fe)
  1679. {
  1680. struct dib9000_state *state = fe->demodulator_priv;
  1681. return state->status;
  1682. }
  1683. static int dib9000_set_channel_status(struct dvb_frontend *fe, struct dvb_frontend_parametersContext *channel_status)
  1684. {
  1685. struct dib9000_state *state = fe->demodulator_priv;
  1686. memcpy(&state->channel_status, channel_status, sizeof(struct dvb_frontend_parametersContext));
  1687. return 0;
  1688. }
  1689. static int dib9000_set_frontend(struct dvb_frontend *fe)
  1690. {
  1691. struct dib9000_state *state = fe->demodulator_priv;
  1692. int sleep_time, sleep_time_slave;
  1693. u32 frontend_status;
  1694. u8 nbr_pending, exit_condition, index_frontend, index_frontend_success;
  1695. struct dvb_frontend_parametersContext channel_status;
  1696. /* check that the correct parameters are set */
  1697. if (state->fe[0]->dtv_property_cache.frequency == 0) {
  1698. dprintk("dib9000: must specify frequency\n");
  1699. return 0;
  1700. }
  1701. if (state->fe[0]->dtv_property_cache.bandwidth_hz == 0) {
  1702. dprintk("dib9000: must specify bandwidth\n");
  1703. return 0;
  1704. }
  1705. state->pid_ctrl_index = -1; /* postpone the pid filtering cmd */
  1706. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1707. dprintk("could not get the lock\n");
  1708. return 0;
  1709. }
  1710. fe->dtv_property_cache.delivery_system = SYS_DVBT;
  1711. /* set the master status */
  1712. if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO ||
  1713. state->fe[0]->dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO ||
  1714. state->fe[0]->dtv_property_cache.modulation == QAM_AUTO ||
  1715. state->fe[0]->dtv_property_cache.code_rate_HP == FEC_AUTO) {
  1716. /* no channel specified, autosearch the channel */
  1717. state->channel_status.status = CHANNEL_STATUS_PARAMETERS_UNKNOWN;
  1718. } else
  1719. state->channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;
  1720. /* set mode and status for the different frontends */
  1721. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1722. dib9000_fw_set_diversity_in(state->fe[index_frontend], 1);
  1723. /* synchronization of the cache */
  1724. memcpy(&state->fe[index_frontend]->dtv_property_cache, &fe->dtv_property_cache, sizeof(struct dtv_frontend_properties));
  1725. state->fe[index_frontend]->dtv_property_cache.delivery_system = SYS_DVBT;
  1726. dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_HIGH_Z);
  1727. dib9000_set_channel_status(state->fe[index_frontend], &state->channel_status);
  1728. dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  1729. }
  1730. /* actual tune */
  1731. exit_condition = 0; /* 0: tune pending; 1: tune failed; 2:tune success */
  1732. index_frontend_success = 0;
  1733. do {
  1734. sleep_time = dib9000_fw_tune(state->fe[0]);
  1735. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1736. sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend]);
  1737. if (sleep_time == FE_CALLBACK_TIME_NEVER)
  1738. sleep_time = sleep_time_slave;
  1739. else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
  1740. sleep_time = sleep_time_slave;
  1741. }
  1742. if (sleep_time != FE_CALLBACK_TIME_NEVER)
  1743. msleep(sleep_time / 10);
  1744. else
  1745. break;
  1746. nbr_pending = 0;
  1747. exit_condition = 0;
  1748. index_frontend_success = 0;
  1749. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1750. frontend_status = -dib9000_get_status(state->fe[index_frontend]);
  1751. if (frontend_status > -FE_STATUS_TUNE_PENDING) {
  1752. exit_condition = 2; /* tune success */
  1753. index_frontend_success = index_frontend;
  1754. break;
  1755. }
  1756. if (frontend_status == -FE_STATUS_TUNE_PENDING)
  1757. nbr_pending++; /* some frontends are still tuning */
  1758. }
  1759. if ((exit_condition != 2) && (nbr_pending == 0))
  1760. exit_condition = 1; /* if all tune are done and no success, exit: tune failed */
  1761. } while (exit_condition == 0);
  1762. /* check the tune result */
  1763. if (exit_condition == 1) { /* tune failed */
  1764. dprintk("tune failed\n");
  1765. mutex_unlock(&state->demod_lock);
  1766. /* tune failed; put all the pid filtering cmd to junk */
  1767. state->pid_ctrl_index = -1;
  1768. return 0;
  1769. }
  1770. dprintk("tune success on frontend%i\n", index_frontend_success);
  1771. /* synchronize all the channel cache */
  1772. state->get_frontend_internal = 1;
  1773. dib9000_get_frontend(state->fe[0], &state->fe[0]->dtv_property_cache);
  1774. state->get_frontend_internal = 0;
  1775. /* retune the other frontends with the found channel */
  1776. channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;
  1777. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1778. /* only retune the frontends which was not tuned success */
  1779. if (index_frontend != index_frontend_success) {
  1780. dib9000_set_channel_status(state->fe[index_frontend], &channel_status);
  1781. dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  1782. }
  1783. }
  1784. do {
  1785. sleep_time = FE_CALLBACK_TIME_NEVER;
  1786. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1787. if (index_frontend != index_frontend_success) {
  1788. sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend]);
  1789. if (sleep_time == FE_CALLBACK_TIME_NEVER)
  1790. sleep_time = sleep_time_slave;
  1791. else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
  1792. sleep_time = sleep_time_slave;
  1793. }
  1794. }
  1795. if (sleep_time != FE_CALLBACK_TIME_NEVER)
  1796. msleep(sleep_time / 10);
  1797. else
  1798. break;
  1799. nbr_pending = 0;
  1800. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1801. if (index_frontend != index_frontend_success) {
  1802. frontend_status = -dib9000_get_status(state->fe[index_frontend]);
  1803. if ((index_frontend != index_frontend_success) && (frontend_status == -FE_STATUS_TUNE_PENDING))
  1804. nbr_pending++; /* some frontends are still tuning */
  1805. }
  1806. }
  1807. } while (nbr_pending != 0);
  1808. /* set the output mode */
  1809. dib9000_fw_set_output_mode(state->fe[0], state->chip.d9.cfg.output_mode);
  1810. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1811. dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_DIVERSITY);
  1812. /* turn off the diversity for the last frontend */
  1813. dib9000_fw_set_diversity_in(state->fe[index_frontend - 1], 0);
  1814. mutex_unlock(&state->demod_lock);
  1815. if (state->pid_ctrl_index >= 0) {
  1816. u8 index_pid_filter_cmd;
  1817. u8 pid_ctrl_index = state->pid_ctrl_index;
  1818. state->pid_ctrl_index = -2;
  1819. for (index_pid_filter_cmd = 0;
  1820. index_pid_filter_cmd <= pid_ctrl_index;
  1821. index_pid_filter_cmd++) {
  1822. if (state->pid_ctrl[index_pid_filter_cmd].cmd == DIB9000_PID_FILTER_CTRL)
  1823. dib9000_fw_pid_filter_ctrl(state->fe[0],
  1824. state->pid_ctrl[index_pid_filter_cmd].onoff);
  1825. else if (state->pid_ctrl[index_pid_filter_cmd].cmd == DIB9000_PID_FILTER)
  1826. dib9000_fw_pid_filter(state->fe[0],
  1827. state->pid_ctrl[index_pid_filter_cmd].id,
  1828. state->pid_ctrl[index_pid_filter_cmd].pid,
  1829. state->pid_ctrl[index_pid_filter_cmd].onoff);
  1830. }
  1831. }
  1832. /* do not postpone any more the pid filtering */
  1833. state->pid_ctrl_index = -2;
  1834. return 0;
  1835. }
  1836. static u16 dib9000_read_lock(struct dvb_frontend *fe)
  1837. {
  1838. struct dib9000_state *state = fe->demodulator_priv;
  1839. return dib9000_read_word(state, 535);
  1840. }
  1841. static int dib9000_read_status(struct dvb_frontend *fe, enum fe_status *stat)
  1842. {
  1843. struct dib9000_state *state = fe->demodulator_priv;
  1844. u8 index_frontend;
  1845. u16 lock = 0, lock_slave = 0;
  1846. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1847. dprintk("could not get the lock\n");
  1848. return -EINTR;
  1849. }
  1850. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1851. lock_slave |= dib9000_read_lock(state->fe[index_frontend]);
  1852. lock = dib9000_read_word(state, 535);
  1853. *stat = 0;
  1854. if ((lock & 0x8000) || (lock_slave & 0x8000))
  1855. *stat |= FE_HAS_SIGNAL;
  1856. if ((lock & 0x3000) || (lock_slave & 0x3000))
  1857. *stat |= FE_HAS_CARRIER;
  1858. if ((lock & 0x0100) || (lock_slave & 0x0100))
  1859. *stat |= FE_HAS_VITERBI;
  1860. if (((lock & 0x0038) == 0x38) || ((lock_slave & 0x0038) == 0x38))
  1861. *stat |= FE_HAS_SYNC;
  1862. if ((lock & 0x0008) || (lock_slave & 0x0008))
  1863. *stat |= FE_HAS_LOCK;
  1864. mutex_unlock(&state->demod_lock);
  1865. return 0;
  1866. }
  1867. static int dib9000_read_ber(struct dvb_frontend *fe, u32 * ber)
  1868. {
  1869. struct dib9000_state *state = fe->demodulator_priv;
  1870. u16 *c;
  1871. int ret = 0;
  1872. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1873. dprintk("could not get the lock\n");
  1874. return -EINTR;
  1875. }
  1876. if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
  1877. dprintk("could not get the lock\n");
  1878. ret = -EINTR;
  1879. goto error;
  1880. }
  1881. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
  1882. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  1883. ret = -EIO;
  1884. goto error;
  1885. }
  1886. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR,
  1887. state->i2c_read_buffer, 16 * 2);
  1888. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  1889. c = (u16 *)state->i2c_read_buffer;
  1890. *ber = c[10] << 16 | c[11];
  1891. error:
  1892. mutex_unlock(&state->demod_lock);
  1893. return ret;
  1894. }
  1895. static int dib9000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1896. {
  1897. struct dib9000_state *state = fe->demodulator_priv;
  1898. u8 index_frontend;
  1899. u16 *c = (u16 *)state->i2c_read_buffer;
  1900. u16 val;
  1901. int ret = 0;
  1902. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1903. dprintk("could not get the lock\n");
  1904. return -EINTR;
  1905. }
  1906. *strength = 0;
  1907. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1908. state->fe[index_frontend]->ops.read_signal_strength(state->fe[index_frontend], &val);
  1909. if (val > 65535 - *strength)
  1910. *strength = 65535;
  1911. else
  1912. *strength += val;
  1913. }
  1914. if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
  1915. dprintk("could not get the lock\n");
  1916. ret = -EINTR;
  1917. goto error;
  1918. }
  1919. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
  1920. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  1921. ret = -EIO;
  1922. goto error;
  1923. }
  1924. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, 16 * 2);
  1925. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  1926. val = 65535 - c[4];
  1927. if (val > 65535 - *strength)
  1928. *strength = 65535;
  1929. else
  1930. *strength += val;
  1931. error:
  1932. mutex_unlock(&state->demod_lock);
  1933. return ret;
  1934. }
  1935. static u32 dib9000_get_snr(struct dvb_frontend *fe)
  1936. {
  1937. struct dib9000_state *state = fe->demodulator_priv;
  1938. u16 *c = (u16 *)state->i2c_read_buffer;
  1939. u32 n, s, exp;
  1940. u16 val;
  1941. if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
  1942. dprintk("could not get the lock\n");
  1943. return 0;
  1944. }
  1945. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
  1946. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  1947. return 0;
  1948. }
  1949. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, 16 * 2);
  1950. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  1951. val = c[7];
  1952. n = (val >> 4) & 0xff;
  1953. exp = ((val & 0xf) << 2);
  1954. val = c[8];
  1955. exp += ((val >> 14) & 0x3);
  1956. if ((exp & 0x20) != 0)
  1957. exp -= 0x40;
  1958. n <<= exp + 16;
  1959. s = (val >> 6) & 0xFF;
  1960. exp = (val & 0x3F);
  1961. if ((exp & 0x20) != 0)
  1962. exp -= 0x40;
  1963. s <<= exp + 16;
  1964. if (n > 0) {
  1965. u32 t = (s / n) << 16;
  1966. return t + ((s << 16) - n * t) / n;
  1967. }
  1968. return 0xffffffff;
  1969. }
  1970. static int dib9000_read_snr(struct dvb_frontend *fe, u16 * snr)
  1971. {
  1972. struct dib9000_state *state = fe->demodulator_priv;
  1973. u8 index_frontend;
  1974. u32 snr_master;
  1975. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1976. dprintk("could not get the lock\n");
  1977. return -EINTR;
  1978. }
  1979. snr_master = dib9000_get_snr(fe);
  1980. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1981. snr_master += dib9000_get_snr(state->fe[index_frontend]);
  1982. if ((snr_master >> 16) != 0) {
  1983. snr_master = 10 * intlog10(snr_master >> 16);
  1984. *snr = snr_master / ((1 << 24) / 10);
  1985. } else
  1986. *snr = 0;
  1987. mutex_unlock(&state->demod_lock);
  1988. return 0;
  1989. }
  1990. static int dib9000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1991. {
  1992. struct dib9000_state *state = fe->demodulator_priv;
  1993. u16 *c = (u16 *)state->i2c_read_buffer;
  1994. int ret = 0;
  1995. if (mutex_lock_interruptible(&state->demod_lock) < 0) {
  1996. dprintk("could not get the lock\n");
  1997. return -EINTR;
  1998. }
  1999. if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
  2000. dprintk("could not get the lock\n");
  2001. ret = -EINTR;
  2002. goto error;
  2003. }
  2004. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
  2005. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  2006. ret = -EIO;
  2007. goto error;
  2008. }
  2009. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, 16 * 2);
  2010. mutex_unlock(&state->platform.risc.mem_mbx_lock);
  2011. *unc = c[12];
  2012. error:
  2013. mutex_unlock(&state->demod_lock);
  2014. return ret;
  2015. }
  2016. int dib9000_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, u8 first_addr)
  2017. {
  2018. int k = 0, ret = 0;
  2019. u8 new_addr = 0;
  2020. struct i2c_device client = {.i2c_adap = i2c };
  2021. client.i2c_write_buffer = kzalloc(4, GFP_KERNEL);
  2022. if (!client.i2c_write_buffer) {
  2023. dprintk("%s: not enough memory\n", __func__);
  2024. return -ENOMEM;
  2025. }
  2026. client.i2c_read_buffer = kzalloc(4, GFP_KERNEL);
  2027. if (!client.i2c_read_buffer) {
  2028. dprintk("%s: not enough memory\n", __func__);
  2029. ret = -ENOMEM;
  2030. goto error_memory;
  2031. }
  2032. client.i2c_addr = default_addr + 16;
  2033. dib9000_i2c_write16(&client, 1796, 0x0);
  2034. for (k = no_of_demods - 1; k >= 0; k--) {
  2035. /* designated i2c address */
  2036. new_addr = first_addr + (k << 1);
  2037. client.i2c_addr = default_addr;
  2038. dib9000_i2c_write16(&client, 1817, 3);
  2039. dib9000_i2c_write16(&client, 1796, 0);
  2040. dib9000_i2c_write16(&client, 1227, 1);
  2041. dib9000_i2c_write16(&client, 1227, 0);
  2042. client.i2c_addr = new_addr;
  2043. dib9000_i2c_write16(&client, 1817, 3);
  2044. dib9000_i2c_write16(&client, 1796, 0);
  2045. dib9000_i2c_write16(&client, 1227, 1);
  2046. dib9000_i2c_write16(&client, 1227, 0);
  2047. if (dib9000_identify(&client) == 0) {
  2048. client.i2c_addr = default_addr;
  2049. if (dib9000_identify(&client) == 0) {
  2050. dprintk("DiB9000 #%d: not identified\n", k);
  2051. ret = -EIO;
  2052. goto error;
  2053. }
  2054. }
  2055. dib9000_i2c_write16(&client, 1795, (1 << 10) | (4 << 6));
  2056. dib9000_i2c_write16(&client, 1794, (new_addr << 2) | 2);
  2057. dprintk("IC %d initialized (to i2c_address 0x%x)\n", k, new_addr);
  2058. }
  2059. for (k = 0; k < no_of_demods; k++) {
  2060. new_addr = first_addr | (k << 1);
  2061. client.i2c_addr = new_addr;
  2062. dib9000_i2c_write16(&client, 1794, (new_addr << 2));
  2063. dib9000_i2c_write16(&client, 1795, 0);
  2064. }
  2065. error:
  2066. kfree(client.i2c_read_buffer);
  2067. error_memory:
  2068. kfree(client.i2c_write_buffer);
  2069. return ret;
  2070. }
  2071. EXPORT_SYMBOL(dib9000_i2c_enumeration);
  2072. int dib9000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
  2073. {
  2074. struct dib9000_state *state = fe->demodulator_priv;
  2075. u8 index_frontend = 1;
  2076. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  2077. index_frontend++;
  2078. if (index_frontend < MAX_NUMBER_OF_FRONTENDS) {
  2079. dprintk("set slave fe %p to index %i\n", fe_slave, index_frontend);
  2080. state->fe[index_frontend] = fe_slave;
  2081. return 0;
  2082. }
  2083. dprintk("too many slave frontend\n");
  2084. return -ENOMEM;
  2085. }
  2086. EXPORT_SYMBOL(dib9000_set_slave_frontend);
  2087. struct dvb_frontend *dib9000_get_slave_frontend(struct dvb_frontend *fe, int slave_index)
  2088. {
  2089. struct dib9000_state *state = fe->demodulator_priv;
  2090. if (slave_index >= MAX_NUMBER_OF_FRONTENDS)
  2091. return NULL;
  2092. return state->fe[slave_index];
  2093. }
  2094. EXPORT_SYMBOL(dib9000_get_slave_frontend);
  2095. static const struct dvb_frontend_ops dib9000_ops;
  2096. struct dvb_frontend *dib9000_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, const struct dib9000_config *cfg)
  2097. {
  2098. struct dvb_frontend *fe;
  2099. struct dib9000_state *st;
  2100. st = kzalloc(sizeof(struct dib9000_state), GFP_KERNEL);
  2101. if (st == NULL)
  2102. return NULL;
  2103. fe = kzalloc(sizeof(struct dvb_frontend), GFP_KERNEL);
  2104. if (fe == NULL) {
  2105. kfree(st);
  2106. return NULL;
  2107. }
  2108. memcpy(&st->chip.d9.cfg, cfg, sizeof(struct dib9000_config));
  2109. st->i2c.i2c_adap = i2c_adap;
  2110. st->i2c.i2c_addr = i2c_addr;
  2111. st->i2c.i2c_write_buffer = st->i2c_write_buffer;
  2112. st->i2c.i2c_read_buffer = st->i2c_read_buffer;
  2113. st->gpio_dir = DIB9000_GPIO_DEFAULT_DIRECTIONS;
  2114. st->gpio_val = DIB9000_GPIO_DEFAULT_VALUES;
  2115. st->gpio_pwm_pos = DIB9000_GPIO_DEFAULT_PWM_POS;
  2116. mutex_init(&st->platform.risc.mbx_if_lock);
  2117. mutex_init(&st->platform.risc.mbx_lock);
  2118. mutex_init(&st->platform.risc.mem_lock);
  2119. mutex_init(&st->platform.risc.mem_mbx_lock);
  2120. mutex_init(&st->demod_lock);
  2121. st->get_frontend_internal = 0;
  2122. st->pid_ctrl_index = -2;
  2123. st->fe[0] = fe;
  2124. fe->demodulator_priv = st;
  2125. memcpy(&st->fe[0]->ops, &dib9000_ops, sizeof(struct dvb_frontend_ops));
  2126. /* Ensure the output mode remains at the previous default if it's
  2127. * not specifically set by the caller.
  2128. */
  2129. if ((st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  2130. st->chip.d9.cfg.output_mode = OUTMODE_MPEG2_FIFO;
  2131. if (dib9000_identify(&st->i2c) == 0)
  2132. goto error;
  2133. dibx000_init_i2c_master(&st->i2c_master, DIB7000MC, st->i2c.i2c_adap, st->i2c.i2c_addr);
  2134. st->tuner_adap.dev.parent = i2c_adap->dev.parent;
  2135. strscpy(st->tuner_adap.name, "DIB9000_FW TUNER ACCESS",
  2136. sizeof(st->tuner_adap.name));
  2137. st->tuner_adap.algo = &dib9000_tuner_algo;
  2138. st->tuner_adap.algo_data = NULL;
  2139. i2c_set_adapdata(&st->tuner_adap, st);
  2140. if (i2c_add_adapter(&st->tuner_adap) < 0)
  2141. goto error;
  2142. st->component_bus.dev.parent = i2c_adap->dev.parent;
  2143. strscpy(st->component_bus.name, "DIB9000_FW COMPONENT BUS ACCESS",
  2144. sizeof(st->component_bus.name));
  2145. st->component_bus.algo = &dib9000_component_bus_algo;
  2146. st->component_bus.algo_data = NULL;
  2147. st->component_bus_speed = 340;
  2148. i2c_set_adapdata(&st->component_bus, st);
  2149. if (i2c_add_adapter(&st->component_bus) < 0)
  2150. goto component_bus_add_error;
  2151. dib9000_fw_reset(fe);
  2152. return fe;
  2153. component_bus_add_error:
  2154. i2c_del_adapter(&st->tuner_adap);
  2155. error:
  2156. kfree(st);
  2157. return NULL;
  2158. }
  2159. EXPORT_SYMBOL(dib9000_attach);
  2160. static const struct dvb_frontend_ops dib9000_ops = {
  2161. .delsys = { SYS_DVBT },
  2162. .info = {
  2163. .name = "DiBcom 9000",
  2164. .frequency_min_hz = 44250 * kHz,
  2165. .frequency_max_hz = 867250 * kHz,
  2166. .frequency_stepsize_hz = 62500,
  2167. .caps = FE_CAN_INVERSION_AUTO |
  2168. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  2169. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  2170. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  2171. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  2172. },
  2173. .release = dib9000_release,
  2174. .init = dib9000_wakeup,
  2175. .sleep = dib9000_sleep,
  2176. .set_frontend = dib9000_set_frontend,
  2177. .get_tune_settings = dib9000_fe_get_tune_settings,
  2178. .get_frontend = dib9000_get_frontend,
  2179. .read_status = dib9000_read_status,
  2180. .read_ber = dib9000_read_ber,
  2181. .read_signal_strength = dib9000_read_signal_strength,
  2182. .read_snr = dib9000_read_snr,
  2183. .read_ucblocks = dib9000_read_unc_blocks,
  2184. };
  2185. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
  2186. MODULE_AUTHOR("Olivier Grenie <olivier.grenie@parrot.com>");
  2187. MODULE_DESCRIPTION("Driver for the DiBcom 9000 COFDM demodulator");
  2188. MODULE_LICENSE("GPL");