dib9000.c 71 KB

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