dib9000.c 65 KB

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