dib9000.c 71 KB

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