dib9000.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  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, u8 * b, u32 len);
  160. static int dib9000_risc_apb_access_write(struct dib9000_state *state, u32 address, u16 attribute, const u8 * b, u32 len);
  161. static u16 to_fw_output_mode(u16 mode)
  162. {
  163. switch (mode) {
  164. case OUTMODE_HIGH_Z:
  165. return 0;
  166. case OUTMODE_MPEG2_PAR_GATED_CLK:
  167. return 4;
  168. case OUTMODE_MPEG2_PAR_CONT_CLK:
  169. return 8;
  170. case OUTMODE_MPEG2_SERIAL:
  171. return 16;
  172. case OUTMODE_DIVERSITY:
  173. return 128;
  174. case OUTMODE_MPEG2_FIFO:
  175. return 2;
  176. case OUTMODE_ANALOG_ADC:
  177. return 1;
  178. default:
  179. return 0;
  180. }
  181. }
  182. static u16 dib9000_read16_attr(struct dib9000_state *state, u16 reg, u8 * b, u32 len, u16 attribute)
  183. {
  184. u32 chunk_size = 126;
  185. u32 l;
  186. int ret;
  187. u8 wb[2] = { reg >> 8, reg & 0xff };
  188. struct i2c_msg msg[2] = {
  189. {.addr = state->i2c.i2c_addr >> 1, .flags = 0, .buf = wb, .len = 2},
  190. {.addr = state->i2c.i2c_addr >> 1, .flags = I2C_M_RD, .buf = b, .len = len},
  191. };
  192. if (state->platform.risc.fw_is_running && (reg < 1024))
  193. return dib9000_risc_apb_access_read(state, reg, attribute, NULL, 0, b, len);
  194. if (attribute & DATA_BUS_ACCESS_MODE_8BIT)
  195. wb[0] |= (1 << 5);
  196. if (attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  197. wb[0] |= (1 << 4);
  198. do {
  199. l = len < chunk_size ? len : chunk_size;
  200. msg[1].len = l;
  201. msg[1].buf = b;
  202. ret = i2c_transfer(state->i2c.i2c_adap, msg, 2) != 2 ? -EREMOTEIO : 0;
  203. if (ret != 0) {
  204. dprintk("i2c read error on %d", reg);
  205. return -EREMOTEIO;
  206. }
  207. b += l;
  208. len -= l;
  209. if (!(attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT))
  210. reg += l / 2;
  211. } while ((ret == 0) && len);
  212. return 0;
  213. }
  214. static u16 dib9000_i2c_read16(struct i2c_device *i2c, u16 reg)
  215. {
  216. u8 b[2];
  217. u8 wb[2] = { reg >> 8, reg & 0xff };
  218. struct i2c_msg msg[2] = {
  219. {.addr = i2c->i2c_addr >> 1, .flags = 0, .buf = wb, .len = 2},
  220. {.addr = i2c->i2c_addr >> 1, .flags = I2C_M_RD, .buf = b, .len = 2},
  221. };
  222. if (i2c_transfer(i2c->i2c_adap, msg, 2) != 2) {
  223. dprintk("read register %x error", reg);
  224. return 0;
  225. }
  226. return (b[0] << 8) | b[1];
  227. }
  228. static inline u16 dib9000_read_word(struct dib9000_state *state, u16 reg)
  229. {
  230. u8 b[2];
  231. if (dib9000_read16_attr(state, reg, b, 2, 0) != 0)
  232. return 0;
  233. return (b[0] << 8 | b[1]);
  234. }
  235. static inline u16 dib9000_read_word_attr(struct dib9000_state *state, u16 reg, u16 attribute)
  236. {
  237. u8 b[2];
  238. if (dib9000_read16_attr(state, reg, b, 2, attribute) != 0)
  239. return 0;
  240. return (b[0] << 8 | b[1]);
  241. }
  242. #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)
  243. static u16 dib9000_write16_attr(struct dib9000_state *state, u16 reg, const u8 * buf, u32 len, u16 attribute)
  244. {
  245. u8 b[255];
  246. u32 chunk_size = 126;
  247. u32 l;
  248. int ret;
  249. struct i2c_msg msg = {
  250. .addr = state->i2c.i2c_addr >> 1, .flags = 0, .buf = b, .len = len + 2
  251. };
  252. if (state->platform.risc.fw_is_running && (reg < 1024)) {
  253. if (dib9000_risc_apb_access_write
  254. (state, reg, DATA_BUS_ACCESS_MODE_16BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT | attribute, buf, len) != 0)
  255. return -EINVAL;
  256. return 0;
  257. }
  258. b[0] = (reg >> 8) & 0xff;
  259. b[1] = (reg) & 0xff;
  260. if (attribute & DATA_BUS_ACCESS_MODE_8BIT)
  261. b[0] |= (1 << 5);
  262. if (attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  263. b[0] |= (1 << 4);
  264. do {
  265. l = len < chunk_size ? len : chunk_size;
  266. msg.len = l + 2;
  267. memcpy(&b[2], buf, l);
  268. ret = i2c_transfer(state->i2c.i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  269. buf += l;
  270. len -= l;
  271. if (!(attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT))
  272. reg += l / 2;
  273. } while ((ret == 0) && len);
  274. return ret;
  275. }
  276. static int dib9000_i2c_write16(struct i2c_device *i2c, u16 reg, u16 val)
  277. {
  278. u8 b[4] = { (reg >> 8) & 0xff, reg & 0xff, (val >> 8) & 0xff, val & 0xff };
  279. struct i2c_msg msg = {
  280. .addr = i2c->i2c_addr >> 1, .flags = 0, .buf = b, .len = 4
  281. };
  282. return i2c_transfer(i2c->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  283. }
  284. static inline int dib9000_write_word(struct dib9000_state *state, u16 reg, u16 val)
  285. {
  286. u8 b[2] = { val >> 8, val & 0xff };
  287. return dib9000_write16_attr(state, reg, b, 2, 0);
  288. }
  289. static inline int dib9000_write_word_attr(struct dib9000_state *state, u16 reg, u16 val, u16 attribute)
  290. {
  291. u8 b[2] = { val >> 8, val & 0xff };
  292. return dib9000_write16_attr(state, reg, b, 2, attribute);
  293. }
  294. #define dib9000_write(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, 0)
  295. #define dib9000_write16_noinc(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  296. #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))
  297. #define dib9000_mbx_send(state, id, data, len) dib9000_mbx_send_attr(state, id, data, len, 0)
  298. #define dib9000_mbx_get_message(state, id, msg, len) dib9000_mbx_get_message_attr(state, id, msg, len, 0)
  299. #define MAC_IRQ (1 << 1)
  300. #define IRQ_POL_MSK (1 << 4)
  301. #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)
  302. #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)
  303. static void dib9000_risc_mem_setup_cmd(struct dib9000_state *state, u32 addr, u32 len, u8 reading)
  304. {
  305. u8 b[14] = { 0 };
  306. /* dprintk("%d memcmd: %d %d %d\n", state->fe_id, addr, addr+len, len); */
  307. /* b[0] = 0 << 7; */
  308. b[1] = 1;
  309. /* b[2] = 0; */
  310. /* b[3] = 0; */
  311. b[4] = (u8) (addr >> 8);
  312. b[5] = (u8) (addr & 0xff);
  313. /* b[10] = 0; */
  314. /* b[11] = 0; */
  315. b[12] = (u8) (addr >> 8);
  316. b[13] = (u8) (addr & 0xff);
  317. addr += len;
  318. /* b[6] = 0; */
  319. /* b[7] = 0; */
  320. b[8] = (u8) (addr >> 8);
  321. b[9] = (u8) (addr & 0xff);
  322. dib9000_write(state, 1056, b, 14);
  323. if (reading)
  324. dib9000_write_word(state, 1056, (1 << 15) | 1);
  325. state->platform.risc.memcmd = -1; /* if it was called directly reset it - to force a future setup-call to set it */
  326. }
  327. static void dib9000_risc_mem_setup(struct dib9000_state *state, u8 cmd)
  328. {
  329. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[cmd & 0x7f];
  330. /* decide whether we need to "refresh" the memory controller */
  331. if (state->platform.risc.memcmd == cmd && /* same command */
  332. !(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 */
  333. return;
  334. dib9000_risc_mem_setup_cmd(state, m->addr, m->size, cmd & 0x80);
  335. state->platform.risc.memcmd = cmd;
  336. }
  337. static int dib9000_risc_mem_read(struct dib9000_state *state, u8 cmd, u8 * b, u16 len)
  338. {
  339. if (!state->platform.risc.fw_is_running)
  340. return -EIO;
  341. DibAcquireLock(&state->platform.risc.mem_lock);
  342. dib9000_risc_mem_setup(state, cmd | 0x80);
  343. dib9000_risc_mem_read_chunks(state, b, len);
  344. DibReleaseLock(&state->platform.risc.mem_lock);
  345. return 0;
  346. }
  347. static int dib9000_risc_mem_write(struct dib9000_state *state, u8 cmd, const u8 * b)
  348. {
  349. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[cmd];
  350. if (!state->platform.risc.fw_is_running)
  351. return -EIO;
  352. DibAcquireLock(&state->platform.risc.mem_lock);
  353. dib9000_risc_mem_setup(state, cmd);
  354. dib9000_risc_mem_write_chunks(state, b, m->size);
  355. DibReleaseLock(&state->platform.risc.mem_lock);
  356. return 0;
  357. }
  358. static int dib9000_firmware_download(struct dib9000_state *state, u8 risc_id, u16 key, const u8 * code, u32 len)
  359. {
  360. u16 offs;
  361. if (risc_id == 1)
  362. offs = 16;
  363. else
  364. offs = 0;
  365. /* config crtl reg */
  366. dib9000_write_word(state, 1024 + offs, 0x000f);
  367. dib9000_write_word(state, 1025 + offs, 0);
  368. dib9000_write_word(state, 1031 + offs, key);
  369. dprintk("going to download %dB of microcode", len);
  370. if (dib9000_write16_noinc(state, 1026 + offs, (u8 *) code, (u16) len) != 0) {
  371. dprintk("error while downloading microcode for RISC %c", 'A' + risc_id);
  372. return -EIO;
  373. }
  374. dprintk("Microcode for RISC %c loaded", 'A' + risc_id);
  375. return 0;
  376. }
  377. static int dib9000_mbx_host_init(struct dib9000_state *state, u8 risc_id)
  378. {
  379. u16 mbox_offs;
  380. u16 reset_reg;
  381. u16 tries = 1000;
  382. if (risc_id == 1)
  383. mbox_offs = 16;
  384. else
  385. mbox_offs = 0;
  386. /* Reset mailbox */
  387. dib9000_write_word(state, 1027 + mbox_offs, 0x8000);
  388. /* Read reset status */
  389. do {
  390. reset_reg = dib9000_read_word(state, 1027 + mbox_offs);
  391. msleep(100);
  392. } while ((reset_reg & 0x8000) && --tries);
  393. if (reset_reg & 0x8000) {
  394. dprintk("MBX: init ERROR, no response from RISC %c", 'A' + risc_id);
  395. return -EIO;
  396. }
  397. dprintk("MBX: initialized");
  398. return 0;
  399. }
  400. #define MAX_MAILBOX_TRY 100
  401. static int dib9000_mbx_send_attr(struct dib9000_state *state, u8 id, u16 * data, u8 len, u16 attr)
  402. {
  403. u8 *d, b[2];
  404. u16 tmp;
  405. u16 size;
  406. u32 i;
  407. int ret = 0;
  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. value = dib9000_i2c_read16(client, 896);
  663. if (value != 0x01b3) {
  664. dprintk("wrong Vendor ID (0x%x)", value);
  665. return 0;
  666. }
  667. value = dib9000_i2c_read16(client, 897);
  668. if (value != 0x4000 && value != 0x4001 && value != 0x4002 && value != 0x4003 && value != 0x4004 && value != 0x4005) {
  669. dprintk("wrong Device ID (0x%x)", value);
  670. return 0;
  671. }
  672. /* protect this driver to be used with 7000PC */
  673. if (value == 0x4000 && dib9000_i2c_read16(client, 769) == 0x4000) {
  674. dprintk("this driver does not work with DiB7000PC");
  675. return 0;
  676. }
  677. switch (value) {
  678. case 0x4000:
  679. dprintk("found DiB7000MA/PA/MB/PB");
  680. break;
  681. case 0x4001:
  682. dprintk("found DiB7000HC");
  683. break;
  684. case 0x4002:
  685. dprintk("found DiB7000MC");
  686. break;
  687. case 0x4003:
  688. dprintk("found DiB9000A");
  689. break;
  690. case 0x4004:
  691. dprintk("found DiB9000H");
  692. break;
  693. case 0x4005:
  694. dprintk("found DiB9000M");
  695. break;
  696. }
  697. return value;
  698. }
  699. static void dib9000_set_power_mode(struct dib9000_state *state, enum dib9000_power_mode mode)
  700. {
  701. /* by default everything is going to be powered off */
  702. u16 reg_903 = 0x3fff, reg_904 = 0xffff, reg_905 = 0xffff, reg_906;
  703. u8 offset;
  704. if (state->revision == 0x4003 || state->revision == 0x4004 || state->revision == 0x4005)
  705. offset = 1;
  706. else
  707. offset = 0;
  708. reg_906 = dib9000_read_word(state, 906 + offset) | 0x3; /* keep settings for RISC */
  709. /* now, depending on the requested mode, we power on */
  710. switch (mode) {
  711. /* power up everything in the demod */
  712. case DIB9000_POWER_ALL:
  713. reg_903 = 0x0000;
  714. reg_904 = 0x0000;
  715. reg_905 = 0x0000;
  716. reg_906 = 0x0000;
  717. break;
  718. /* just leave power on the control-interfaces: GPIO and (I2C or SDIO or SRAM) */
  719. case DIB9000_POWER_INTERFACE_ONLY: /* TODO power up either SDIO or I2C or SRAM */
  720. reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 2));
  721. break;
  722. case DIB9000_POWER_INTERF_ANALOG_AGC:
  723. reg_903 &= ~((1 << 15) | (1 << 14) | (1 << 11) | (1 << 10));
  724. reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 2));
  725. reg_906 &= ~((1 << 0));
  726. break;
  727. case DIB9000_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD:
  728. reg_903 = 0x0000;
  729. reg_904 = 0x801f;
  730. reg_905 = 0x0000;
  731. reg_906 &= ~((1 << 0));
  732. break;
  733. case DIB9000_POWER_COR4_CRY_ESRAM_MOUT_NUD:
  734. reg_903 = 0x0000;
  735. reg_904 = 0x8000;
  736. reg_905 = 0x010b;
  737. reg_906 &= ~((1 << 0));
  738. break;
  739. default:
  740. case DIB9000_POWER_NO:
  741. break;
  742. }
  743. /* always power down unused parts */
  744. if (!state->platform.host.mobile_mode)
  745. reg_904 |= (1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) | (1 << 1);
  746. /* P_sdio_select_clk = 0 on MC and after */
  747. if (state->revision != 0x4000)
  748. reg_906 <<= 1;
  749. dib9000_write_word(state, 903 + offset, reg_903);
  750. dib9000_write_word(state, 904 + offset, reg_904);
  751. dib9000_write_word(state, 905 + offset, reg_905);
  752. dib9000_write_word(state, 906 + offset, reg_906);
  753. }
  754. static int dib9000_fw_reset(struct dvb_frontend *fe)
  755. {
  756. struct dib9000_state *state = fe->demodulator_priv;
  757. dib9000_write_word(state, 1817, 0x0003);
  758. dib9000_write_word(state, 1227, 1);
  759. dib9000_write_word(state, 1227, 0);
  760. switch ((state->revision = dib9000_identify(&state->i2c))) {
  761. case 0x4003:
  762. case 0x4004:
  763. case 0x4005:
  764. state->reg_offs = 1;
  765. break;
  766. default:
  767. return -EINVAL;
  768. }
  769. /* reset the i2c-master to use the host interface */
  770. dibx000_reset_i2c_master(&state->i2c_master);
  771. dib9000_set_power_mode(state, DIB9000_POWER_ALL);
  772. /* unforce divstr regardless whether i2c enumeration was done or not */
  773. dib9000_write_word(state, 1794, dib9000_read_word(state, 1794) & ~(1 << 1));
  774. dib9000_write_word(state, 1796, 0);
  775. dib9000_write_word(state, 1805, 0x805);
  776. /* restart all parts */
  777. dib9000_write_word(state, 898, 0xffff);
  778. dib9000_write_word(state, 899, 0xffff);
  779. dib9000_write_word(state, 900, 0x0001);
  780. dib9000_write_word(state, 901, 0xff19);
  781. dib9000_write_word(state, 902, 0x003c);
  782. dib9000_write_word(state, 898, 0);
  783. dib9000_write_word(state, 899, 0);
  784. dib9000_write_word(state, 900, 0);
  785. dib9000_write_word(state, 901, 0);
  786. dib9000_write_word(state, 902, 0);
  787. dib9000_write_word(state, 911, state->chip.d9.cfg.if_drives);
  788. dib9000_set_power_mode(state, DIB9000_POWER_INTERFACE_ONLY);
  789. return 0;
  790. }
  791. static int dib9000_risc_apb_access_read(struct dib9000_state *state, u32 address, u16 attribute, const u8 * tx, u32 txlen, 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--;
  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)
  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. return -EIO;
  848. /* initialize the firmware */
  849. for (i = 0; i < ARRAY_SIZE(state->chip.d9.cfg.gpio_function); i++) {
  850. f = &state->chip.d9.cfg.gpio_function[i];
  851. if (f->mask) {
  852. switch (f->function) {
  853. case BOARD_GPIO_FUNCTION_COMPONENT_ON:
  854. b[0] = (u16) f->mask;
  855. b[1] = (u16) f->direction;
  856. b[2] = (u16) f->value;
  857. break;
  858. case BOARD_GPIO_FUNCTION_COMPONENT_OFF:
  859. b[3] = (u16) f->mask;
  860. b[4] = (u16) f->direction;
  861. b[5] = (u16) f->value;
  862. break;
  863. }
  864. }
  865. }
  866. if (dib9000_mbx_send(state, OUT_MSG_CONF_GPIO, b, 15) != 0)
  867. return -EIO;
  868. /* subband */
  869. b[0] = state->chip.d9.cfg.subband.size; /* type == 0 -> GPIO - PWM not yet supported */
  870. for (i = 0; i < state->chip.d9.cfg.subband.size; i++) {
  871. b[1 + i * 4] = state->chip.d9.cfg.subband.subband[i].f_mhz;
  872. b[2 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.mask;
  873. b[3 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.direction;
  874. b[4 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.value;
  875. }
  876. b[1 + i * 4] = 0; /* fe_id */
  877. if (dib9000_mbx_send(state, OUT_MSG_SUBBAND_SEL, b, 2 + 4 * i) != 0)
  878. return -EIO;
  879. /* 0 - id, 1 - no_of_frontends */
  880. b[0] = (0 << 8) | 1;
  881. /* 0 = i2c-address demod, 0 = tuner */
  882. b[1] = (0 << 8) | (0);
  883. b[2] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000) >> 16) & 0xffff);
  884. b[3] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000)) & 0xffff);
  885. b[4] = (u16) ((state->chip.d9.cfg.vcxo_timer >> 16) & 0xffff);
  886. b[5] = (u16) ((state->chip.d9.cfg.vcxo_timer) & 0xffff);
  887. b[6] = (u16) ((state->chip.d9.cfg.timing_frequency >> 16) & 0xffff);
  888. b[7] = (u16) ((state->chip.d9.cfg.timing_frequency) & 0xffff);
  889. b[29] = state->chip.d9.cfg.if_drives;
  890. if (dib9000_mbx_send(state, OUT_MSG_INIT_DEMOD, b, ARRAY_SIZE(b)) != 0)
  891. return -EIO;
  892. if (dib9000_mbx_send(state, OUT_MSG_FE_FW_DL, NULL, 0) != 0)
  893. return -EIO;
  894. if (dib9000_mbx_get_message(state, IN_MSG_FE_FW_DL_DONE, b, &size) < 0)
  895. return -EIO;
  896. if (size > ARRAY_SIZE(b)) {
  897. dprintk("error : firmware returned %dbytes needed but the used buffer has only %dbytes\n Firmware init ABORTED", size,
  898. (int)ARRAY_SIZE(b));
  899. return -EINVAL;
  900. }
  901. for (i = 0; i < size; i += 2) {
  902. state->platform.risc.fe_mm[i / 2].addr = b[i + 0];
  903. state->platform.risc.fe_mm[i / 2].size = b[i + 1];
  904. }
  905. return 0;
  906. }
  907. static void dib9000_fw_set_channel_head(struct dib9000_state *state, struct dvb_frontend_parameters *ch)
  908. {
  909. u8 b[9];
  910. u32 freq = state->fe[0]->dtv_property_cache.frequency / 1000;
  911. if (state->fe_id % 2)
  912. freq += 101;
  913. b[0] = (u8) ((freq >> 0) & 0xff);
  914. b[1] = (u8) ((freq >> 8) & 0xff);
  915. b[2] = (u8) ((freq >> 16) & 0xff);
  916. b[3] = (u8) ((freq >> 24) & 0xff);
  917. b[4] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 0) & 0xff);
  918. b[5] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 8) & 0xff);
  919. b[6] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 16) & 0xff);
  920. b[7] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 24) & 0xff);
  921. b[8] = 0x80; /* do not wait for CELL ID when doing autosearch */
  922. if (state->fe[0]->dtv_property_cache.delivery_system == SYS_DVBT)
  923. b[8] |= 1;
  924. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_HEAD, b);
  925. }
  926. static int dib9000_fw_get_channel(struct dvb_frontend *fe, struct dvb_frontend_parameters *channel)
  927. {
  928. struct dib9000_state *state = fe->demodulator_priv;
  929. struct dibDVBTChannel {
  930. s8 spectrum_inversion;
  931. s8 nfft;
  932. s8 guard;
  933. s8 constellation;
  934. s8 hrch;
  935. s8 alpha;
  936. s8 code_rate_hp;
  937. s8 code_rate_lp;
  938. s8 select_hp;
  939. s8 intlv_native;
  940. };
  941. struct dibDVBTChannel ch;
  942. int ret = 0;
  943. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  944. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
  945. goto error;
  946. ret = -EIO;
  947. }
  948. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_UNION, (u8 *) &ch, sizeof(struct dibDVBTChannel));
  949. switch (ch.spectrum_inversion & 0x7) {
  950. case 1:
  951. state->fe[0]->dtv_property_cache.inversion = INVERSION_ON;
  952. break;
  953. case 0:
  954. state->fe[0]->dtv_property_cache.inversion = INVERSION_OFF;
  955. break;
  956. default:
  957. case -1:
  958. state->fe[0]->dtv_property_cache.inversion = INVERSION_AUTO;
  959. break;
  960. }
  961. switch (ch.nfft) {
  962. case 0:
  963. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
  964. break;
  965. case 2:
  966. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_4K;
  967. break;
  968. case 1:
  969. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  970. break;
  971. default:
  972. case -1:
  973. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_AUTO;
  974. break;
  975. }
  976. switch (ch.guard) {
  977. case 0:
  978. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
  979. break;
  980. case 1:
  981. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
  982. break;
  983. case 2:
  984. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  985. break;
  986. case 3:
  987. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
  988. break;
  989. default:
  990. case -1:
  991. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_AUTO;
  992. break;
  993. }
  994. switch (ch.constellation) {
  995. case 2:
  996. state->fe[0]->dtv_property_cache.modulation = QAM_64;
  997. break;
  998. case 1:
  999. state->fe[0]->dtv_property_cache.modulation = QAM_16;
  1000. break;
  1001. case 0:
  1002. state->fe[0]->dtv_property_cache.modulation = QPSK;
  1003. break;
  1004. default:
  1005. case -1:
  1006. state->fe[0]->dtv_property_cache.modulation = QAM_AUTO;
  1007. break;
  1008. }
  1009. switch (ch.hrch) {
  1010. case 0:
  1011. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_NONE;
  1012. break;
  1013. case 1:
  1014. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_1;
  1015. break;
  1016. default:
  1017. case -1:
  1018. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_AUTO;
  1019. break;
  1020. }
  1021. switch (ch.code_rate_hp) {
  1022. case 1:
  1023. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_1_2;
  1024. break;
  1025. case 2:
  1026. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_2_3;
  1027. break;
  1028. case 3:
  1029. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_3_4;
  1030. break;
  1031. case 5:
  1032. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_5_6;
  1033. break;
  1034. case 7:
  1035. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_7_8;
  1036. break;
  1037. default:
  1038. case -1:
  1039. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_AUTO;
  1040. break;
  1041. }
  1042. switch (ch.code_rate_lp) {
  1043. case 1:
  1044. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_1_2;
  1045. break;
  1046. case 2:
  1047. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_2_3;
  1048. break;
  1049. case 3:
  1050. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_3_4;
  1051. break;
  1052. case 5:
  1053. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_5_6;
  1054. break;
  1055. case 7:
  1056. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_7_8;
  1057. break;
  1058. default:
  1059. case -1:
  1060. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_AUTO;
  1061. break;
  1062. }
  1063. error:
  1064. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1065. return ret;
  1066. }
  1067. static int dib9000_fw_set_channel_union(struct dvb_frontend *fe, struct dvb_frontend_parameters *channel)
  1068. {
  1069. struct dib9000_state *state = fe->demodulator_priv;
  1070. struct dibDVBTChannel {
  1071. s8 spectrum_inversion;
  1072. s8 nfft;
  1073. s8 guard;
  1074. s8 constellation;
  1075. s8 hrch;
  1076. s8 alpha;
  1077. s8 code_rate_hp;
  1078. s8 code_rate_lp;
  1079. s8 select_hp;
  1080. s8 intlv_native;
  1081. };
  1082. struct dibDVBTChannel ch;
  1083. switch (state->fe[0]->dtv_property_cache.inversion) {
  1084. case INVERSION_ON:
  1085. ch.spectrum_inversion = 1;
  1086. break;
  1087. case INVERSION_OFF:
  1088. ch.spectrum_inversion = 0;
  1089. break;
  1090. default:
  1091. case INVERSION_AUTO:
  1092. ch.spectrum_inversion = -1;
  1093. break;
  1094. }
  1095. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  1096. case TRANSMISSION_MODE_2K:
  1097. ch.nfft = 0;
  1098. break;
  1099. case TRANSMISSION_MODE_4K:
  1100. ch.nfft = 2;
  1101. break;
  1102. case TRANSMISSION_MODE_8K:
  1103. ch.nfft = 1;
  1104. break;
  1105. default:
  1106. case TRANSMISSION_MODE_AUTO:
  1107. ch.nfft = 1;
  1108. break;
  1109. }
  1110. switch (state->fe[0]->dtv_property_cache.guard_interval) {
  1111. case GUARD_INTERVAL_1_32:
  1112. ch.guard = 0;
  1113. break;
  1114. case GUARD_INTERVAL_1_16:
  1115. ch.guard = 1;
  1116. break;
  1117. case GUARD_INTERVAL_1_8:
  1118. ch.guard = 2;
  1119. break;
  1120. case GUARD_INTERVAL_1_4:
  1121. ch.guard = 3;
  1122. break;
  1123. default:
  1124. case GUARD_INTERVAL_AUTO:
  1125. ch.guard = -1;
  1126. break;
  1127. }
  1128. switch (state->fe[0]->dtv_property_cache.modulation) {
  1129. case QAM_64:
  1130. ch.constellation = 2;
  1131. break;
  1132. case QAM_16:
  1133. ch.constellation = 1;
  1134. break;
  1135. case QPSK:
  1136. ch.constellation = 0;
  1137. break;
  1138. default:
  1139. case QAM_AUTO:
  1140. ch.constellation = -1;
  1141. break;
  1142. }
  1143. switch (state->fe[0]->dtv_property_cache.hierarchy) {
  1144. case HIERARCHY_NONE:
  1145. ch.hrch = 0;
  1146. break;
  1147. case HIERARCHY_1:
  1148. case HIERARCHY_2:
  1149. case HIERARCHY_4:
  1150. ch.hrch = 1;
  1151. break;
  1152. default:
  1153. case HIERARCHY_AUTO:
  1154. ch.hrch = -1;
  1155. break;
  1156. }
  1157. ch.alpha = 1;
  1158. switch (state->fe[0]->dtv_property_cache.code_rate_HP) {
  1159. case FEC_1_2:
  1160. ch.code_rate_hp = 1;
  1161. break;
  1162. case FEC_2_3:
  1163. ch.code_rate_hp = 2;
  1164. break;
  1165. case FEC_3_4:
  1166. ch.code_rate_hp = 3;
  1167. break;
  1168. case FEC_5_6:
  1169. ch.code_rate_hp = 5;
  1170. break;
  1171. case FEC_7_8:
  1172. ch.code_rate_hp = 7;
  1173. break;
  1174. default:
  1175. case FEC_AUTO:
  1176. ch.code_rate_hp = -1;
  1177. break;
  1178. }
  1179. switch (state->fe[0]->dtv_property_cache.code_rate_LP) {
  1180. case FEC_1_2:
  1181. ch.code_rate_lp = 1;
  1182. break;
  1183. case FEC_2_3:
  1184. ch.code_rate_lp = 2;
  1185. break;
  1186. case FEC_3_4:
  1187. ch.code_rate_lp = 3;
  1188. break;
  1189. case FEC_5_6:
  1190. ch.code_rate_lp = 5;
  1191. break;
  1192. case FEC_7_8:
  1193. ch.code_rate_lp = 7;
  1194. break;
  1195. default:
  1196. case FEC_AUTO:
  1197. ch.code_rate_lp = -1;
  1198. break;
  1199. }
  1200. ch.select_hp = 1;
  1201. ch.intlv_native = 1;
  1202. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_UNION, (u8 *) &ch);
  1203. return 0;
  1204. }
  1205. static int dib9000_fw_tune(struct dvb_frontend *fe, struct dvb_frontend_parameters *ch)
  1206. {
  1207. struct dib9000_state *state = fe->demodulator_priv;
  1208. int ret = 10, search = state->channel_status.status == CHANNEL_STATUS_PARAMETERS_UNKNOWN;
  1209. s8 i;
  1210. switch (state->tune_state) {
  1211. case CT_DEMOD_START:
  1212. dib9000_fw_set_channel_head(state, ch);
  1213. /* write the channel context - a channel is initialized to 0, so it is OK */
  1214. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_CONTEXT, (u8 *) fe_info);
  1215. dib9000_risc_mem_write(state, FE_MM_W_FE_INFO, (u8 *) fe_info);
  1216. if (search)
  1217. dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_SEARCH, NULL, 0);
  1218. else {
  1219. dib9000_fw_set_channel_union(fe, ch);
  1220. dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_TUNE, NULL, 0);
  1221. }
  1222. state->tune_state = CT_DEMOD_STEP_1;
  1223. break;
  1224. case CT_DEMOD_STEP_1:
  1225. if (search)
  1226. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_SEARCH_STATE, (u8 *) &i, 1);
  1227. else
  1228. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_TUNE_STATE, (u8 *) &i, 1);
  1229. switch (i) { /* something happened */
  1230. case 0:
  1231. break;
  1232. case -2: /* tps locks are "slower" than MPEG locks -> even in autosearch data is OK here */
  1233. if (search)
  1234. state->status = FE_STATUS_DEMOD_SUCCESS;
  1235. else {
  1236. state->tune_state = CT_DEMOD_STOP;
  1237. state->status = FE_STATUS_LOCKED;
  1238. }
  1239. break;
  1240. default:
  1241. state->status = FE_STATUS_TUNE_FAILED;
  1242. state->tune_state = CT_DEMOD_STOP;
  1243. break;
  1244. }
  1245. break;
  1246. default:
  1247. ret = FE_CALLBACK_TIME_NEVER;
  1248. break;
  1249. }
  1250. return ret;
  1251. }
  1252. static int dib9000_fw_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1253. {
  1254. struct dib9000_state *state = fe->demodulator_priv;
  1255. u16 mode = (u16) onoff;
  1256. return dib9000_mbx_send(state, OUT_MSG_ENABLE_DIVERSITY, &mode, 1);
  1257. }
  1258. static int dib9000_fw_set_output_mode(struct dvb_frontend *fe, int mode)
  1259. {
  1260. struct dib9000_state *state = fe->demodulator_priv;
  1261. u16 outreg, smo_mode;
  1262. dprintk("setting output mode for demod %p to %d", fe, mode);
  1263. switch (mode) {
  1264. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1265. outreg = (1 << 10); /* 0x0400 */
  1266. break;
  1267. case OUTMODE_MPEG2_PAR_CONT_CLK:
  1268. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  1269. break;
  1270. case OUTMODE_MPEG2_SERIAL:
  1271. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
  1272. break;
  1273. case OUTMODE_DIVERSITY:
  1274. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  1275. break;
  1276. case OUTMODE_MPEG2_FIFO:
  1277. outreg = (1 << 10) | (5 << 6);
  1278. break;
  1279. case OUTMODE_HIGH_Z:
  1280. outreg = 0;
  1281. break;
  1282. default:
  1283. dprintk("Unhandled output_mode passed to be set for demod %p", &state->fe[0]);
  1284. return -EINVAL;
  1285. }
  1286. dib9000_write_word(state, 1795, outreg);
  1287. switch (mode) {
  1288. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1289. case OUTMODE_MPEG2_PAR_CONT_CLK:
  1290. case OUTMODE_MPEG2_SERIAL:
  1291. case OUTMODE_MPEG2_FIFO:
  1292. smo_mode = (dib9000_read_word(state, 295) & 0x0010) | (1 << 1);
  1293. if (state->chip.d9.cfg.output_mpeg2_in_188_bytes)
  1294. smo_mode |= (1 << 5);
  1295. dib9000_write_word(state, 295, smo_mode);
  1296. break;
  1297. }
  1298. outreg = to_fw_output_mode(mode);
  1299. return dib9000_mbx_send(state, OUT_MSG_SET_OUTPUT_MODE, &outreg, 1);
  1300. }
  1301. static int dib9000_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1302. {
  1303. struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
  1304. u16 i, len, t, index_msg;
  1305. for (index_msg = 0; index_msg < num; index_msg++) {
  1306. if (msg[index_msg].flags & I2C_M_RD) { /* read */
  1307. len = msg[index_msg].len;
  1308. if (len > 16)
  1309. len = 16;
  1310. if (dib9000_read_word(state, 790) != 0)
  1311. dprintk("TunerITF: read busy");
  1312. dib9000_write_word(state, 784, (u16) (msg[index_msg].addr));
  1313. dib9000_write_word(state, 787, (len / 2) - 1);
  1314. dib9000_write_word(state, 786, 1); /* start read */
  1315. i = 1000;
  1316. while (dib9000_read_word(state, 790) != (len / 2) && i)
  1317. i--;
  1318. if (i == 0)
  1319. dprintk("TunerITF: read failed");
  1320. for (i = 0; i < len; i += 2) {
  1321. t = dib9000_read_word(state, 785);
  1322. msg[index_msg].buf[i] = (t >> 8) & 0xff;
  1323. msg[index_msg].buf[i + 1] = (t) & 0xff;
  1324. }
  1325. if (dib9000_read_word(state, 790) != 0)
  1326. dprintk("TunerITF: read more data than expected");
  1327. } else {
  1328. i = 1000;
  1329. while (dib9000_read_word(state, 789) && i)
  1330. i--;
  1331. if (i == 0)
  1332. dprintk("TunerITF: write busy");
  1333. len = msg[index_msg].len;
  1334. if (len > 16)
  1335. len = 16;
  1336. for (i = 0; i < len; i += 2)
  1337. dib9000_write_word(state, 785, (msg[index_msg].buf[i] << 8) | msg[index_msg].buf[i + 1]);
  1338. dib9000_write_word(state, 784, (u16) msg[index_msg].addr);
  1339. dib9000_write_word(state, 787, (len / 2) - 1);
  1340. dib9000_write_word(state, 786, 0); /* start write */
  1341. i = 1000;
  1342. while (dib9000_read_word(state, 791) > 0 && i)
  1343. i--;
  1344. if (i == 0)
  1345. dprintk("TunerITF: write failed");
  1346. }
  1347. }
  1348. return num;
  1349. }
  1350. int dib9000_fw_set_component_bus_speed(struct dvb_frontend *fe, u16 speed)
  1351. {
  1352. struct dib9000_state *state = fe->demodulator_priv;
  1353. state->component_bus_speed = speed;
  1354. return 0;
  1355. }
  1356. EXPORT_SYMBOL(dib9000_fw_set_component_bus_speed);
  1357. static int dib9000_fw_component_bus_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1358. {
  1359. struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
  1360. u8 type = 0; /* I2C */
  1361. u8 port = DIBX000_I2C_INTERFACE_GPIO_3_4;
  1362. u16 scl = state->component_bus_speed; /* SCL frequency */
  1363. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[FE_MM_RW_COMPONENT_ACCESS_BUFFER];
  1364. u8 p[13] = { 0 };
  1365. p[0] = type;
  1366. p[1] = port;
  1367. p[2] = msg[0].addr << 1;
  1368. p[3] = (u8) scl & 0xff; /* scl */
  1369. p[4] = (u8) (scl >> 8);
  1370. p[7] = 0;
  1371. p[8] = 0;
  1372. p[9] = (u8) (msg[0].len);
  1373. p[10] = (u8) (msg[0].len >> 8);
  1374. if ((num > 1) && (msg[1].flags & I2C_M_RD)) {
  1375. p[11] = (u8) (msg[1].len);
  1376. p[12] = (u8) (msg[1].len >> 8);
  1377. } else {
  1378. p[11] = 0;
  1379. p[12] = 0;
  1380. }
  1381. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1382. dib9000_risc_mem_write(state, FE_MM_W_COMPONENT_ACCESS, p);
  1383. { /* write-part */
  1384. dib9000_risc_mem_setup_cmd(state, m->addr, msg[0].len, 0);
  1385. dib9000_risc_mem_write_chunks(state, msg[0].buf, msg[0].len);
  1386. }
  1387. /* do the transaction */
  1388. if (dib9000_fw_memmbx_sync(state, FE_SYNC_COMPONENT_ACCESS) < 0) {
  1389. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1390. return 0;
  1391. }
  1392. /* read back any possible result */
  1393. if ((num > 1) && (msg[1].flags & I2C_M_RD))
  1394. dib9000_risc_mem_read(state, FE_MM_RW_COMPONENT_ACCESS_BUFFER, msg[1].buf, msg[1].len);
  1395. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1396. return num;
  1397. }
  1398. static u32 dib9000_i2c_func(struct i2c_adapter *adapter)
  1399. {
  1400. return I2C_FUNC_I2C;
  1401. }
  1402. static struct i2c_algorithm dib9000_tuner_algo = {
  1403. .master_xfer = dib9000_tuner_xfer,
  1404. .functionality = dib9000_i2c_func,
  1405. };
  1406. static struct i2c_algorithm dib9000_component_bus_algo = {
  1407. .master_xfer = dib9000_fw_component_bus_xfer,
  1408. .functionality = dib9000_i2c_func,
  1409. };
  1410. struct i2c_adapter *dib9000_get_tuner_interface(struct dvb_frontend *fe)
  1411. {
  1412. struct dib9000_state *st = fe->demodulator_priv;
  1413. return &st->tuner_adap;
  1414. }
  1415. EXPORT_SYMBOL(dib9000_get_tuner_interface);
  1416. struct i2c_adapter *dib9000_get_component_bus_interface(struct dvb_frontend *fe)
  1417. {
  1418. struct dib9000_state *st = fe->demodulator_priv;
  1419. return &st->component_bus;
  1420. }
  1421. EXPORT_SYMBOL(dib9000_get_component_bus_interface);
  1422. struct i2c_adapter *dib9000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
  1423. {
  1424. struct dib9000_state *st = fe->demodulator_priv;
  1425. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1426. }
  1427. EXPORT_SYMBOL(dib9000_get_i2c_master);
  1428. int dib9000_set_i2c_adapter(struct dvb_frontend *fe, struct i2c_adapter *i2c)
  1429. {
  1430. struct dib9000_state *st = fe->demodulator_priv;
  1431. st->i2c.i2c_adap = i2c;
  1432. return 0;
  1433. }
  1434. EXPORT_SYMBOL(dib9000_set_i2c_adapter);
  1435. static int dib9000_cfg_gpio(struct dib9000_state *st, u8 num, u8 dir, u8 val)
  1436. {
  1437. st->gpio_dir = dib9000_read_word(st, 773);
  1438. st->gpio_dir &= ~(1 << num); /* reset the direction bit */
  1439. st->gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  1440. dib9000_write_word(st, 773, st->gpio_dir);
  1441. st->gpio_val = dib9000_read_word(st, 774);
  1442. st->gpio_val &= ~(1 << num); /* reset the direction bit */
  1443. st->gpio_val |= (val & 0x01) << num; /* set the new value */
  1444. dib9000_write_word(st, 774, st->gpio_val);
  1445. dprintk("gpio dir: %04x: gpio val: %04x", st->gpio_dir, st->gpio_val);
  1446. return 0;
  1447. }
  1448. int dib9000_set_gpio(struct dvb_frontend *fe, u8 num, u8 dir, u8 val)
  1449. {
  1450. struct dib9000_state *state = fe->demodulator_priv;
  1451. return dib9000_cfg_gpio(state, num, dir, val);
  1452. }
  1453. EXPORT_SYMBOL(dib9000_set_gpio);
  1454. int dib9000_fw_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1455. {
  1456. struct dib9000_state *state = fe->demodulator_priv;
  1457. u16 val = dib9000_read_word(state, 294 + 1) & 0xffef;
  1458. val |= (onoff & 0x1) << 4;
  1459. dprintk("PID filter enabled %d", onoff);
  1460. return dib9000_write_word(state, 294 + 1, val);
  1461. }
  1462. EXPORT_SYMBOL(dib9000_fw_pid_filter_ctrl);
  1463. int dib9000_fw_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1464. {
  1465. struct dib9000_state *state = fe->demodulator_priv;
  1466. dprintk("Index %x, PID %d, OnOff %d", id, pid, onoff);
  1467. return dib9000_write_word(state, 300 + 1 + id, onoff ? (1 << 13) | pid : 0);
  1468. }
  1469. EXPORT_SYMBOL(dib9000_fw_pid_filter);
  1470. int dib9000_firmware_post_pll_init(struct dvb_frontend *fe)
  1471. {
  1472. struct dib9000_state *state = fe->demodulator_priv;
  1473. return dib9000_fw_init(state);
  1474. }
  1475. EXPORT_SYMBOL(dib9000_firmware_post_pll_init);
  1476. static void dib9000_release(struct dvb_frontend *demod)
  1477. {
  1478. struct dib9000_state *st = demod->demodulator_priv;
  1479. u8 index_frontend;
  1480. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (st->fe[index_frontend] != NULL); index_frontend++)
  1481. dvb_frontend_detach(st->fe[index_frontend]);
  1482. DibFreeLock(&state->platform.risc.mbx_if_lock);
  1483. DibFreeLock(&state->platform.risc.mbx_lock);
  1484. DibFreeLock(&state->platform.risc.mem_lock);
  1485. DibFreeLock(&state->platform.risc.mem_mbx_lock);
  1486. dibx000_exit_i2c_master(&st->i2c_master);
  1487. i2c_del_adapter(&st->tuner_adap);
  1488. i2c_del_adapter(&st->component_bus);
  1489. kfree(st->fe[0]);
  1490. kfree(st);
  1491. }
  1492. static int dib9000_wakeup(struct dvb_frontend *fe)
  1493. {
  1494. return 0;
  1495. }
  1496. static int dib9000_sleep(struct dvb_frontend *fe)
  1497. {
  1498. struct dib9000_state *state = fe->demodulator_priv;
  1499. u8 index_frontend;
  1500. int ret;
  1501. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1502. ret = state->fe[index_frontend]->ops.sleep(state->fe[index_frontend]);
  1503. if (ret < 0)
  1504. return ret;
  1505. }
  1506. return dib9000_mbx_send(state, OUT_MSG_FE_SLEEP, NULL, 0);
  1507. }
  1508. static int dib9000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  1509. {
  1510. tune->min_delay_ms = 1000;
  1511. return 0;
  1512. }
  1513. static int dib9000_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1514. {
  1515. struct dib9000_state *state = fe->demodulator_priv;
  1516. u8 index_frontend, sub_index_frontend;
  1517. fe_status_t stat;
  1518. int ret;
  1519. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1520. state->fe[index_frontend]->ops.read_status(state->fe[index_frontend], &stat);
  1521. if (stat & FE_HAS_SYNC) {
  1522. dprintk("TPS lock on the slave%i", index_frontend);
  1523. /* synchronize the cache with the other frontends */
  1524. state->fe[index_frontend]->ops.get_frontend(state->fe[index_frontend], fep);
  1525. for (sub_index_frontend = 0; (sub_index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[sub_index_frontend] != NULL);
  1526. sub_index_frontend++) {
  1527. if (sub_index_frontend != index_frontend) {
  1528. state->fe[sub_index_frontend]->dtv_property_cache.modulation =
  1529. state->fe[index_frontend]->dtv_property_cache.modulation;
  1530. state->fe[sub_index_frontend]->dtv_property_cache.inversion =
  1531. state->fe[index_frontend]->dtv_property_cache.inversion;
  1532. state->fe[sub_index_frontend]->dtv_property_cache.transmission_mode =
  1533. state->fe[index_frontend]->dtv_property_cache.transmission_mode;
  1534. state->fe[sub_index_frontend]->dtv_property_cache.guard_interval =
  1535. state->fe[index_frontend]->dtv_property_cache.guard_interval;
  1536. state->fe[sub_index_frontend]->dtv_property_cache.hierarchy =
  1537. state->fe[index_frontend]->dtv_property_cache.hierarchy;
  1538. state->fe[sub_index_frontend]->dtv_property_cache.code_rate_HP =
  1539. state->fe[index_frontend]->dtv_property_cache.code_rate_HP;
  1540. state->fe[sub_index_frontend]->dtv_property_cache.code_rate_LP =
  1541. state->fe[index_frontend]->dtv_property_cache.code_rate_LP;
  1542. state->fe[sub_index_frontend]->dtv_property_cache.rolloff =
  1543. state->fe[index_frontend]->dtv_property_cache.rolloff;
  1544. }
  1545. }
  1546. return 0;
  1547. }
  1548. }
  1549. /* get the channel from master chip */
  1550. ret = dib9000_fw_get_channel(fe, fep);
  1551. if (ret != 0)
  1552. return ret;
  1553. /* synchronize the cache with the other frontends */
  1554. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1555. state->fe[index_frontend]->dtv_property_cache.inversion = fe->dtv_property_cache.inversion;
  1556. state->fe[index_frontend]->dtv_property_cache.transmission_mode = fe->dtv_property_cache.transmission_mode;
  1557. state->fe[index_frontend]->dtv_property_cache.guard_interval = fe->dtv_property_cache.guard_interval;
  1558. state->fe[index_frontend]->dtv_property_cache.modulation = fe->dtv_property_cache.modulation;
  1559. state->fe[index_frontend]->dtv_property_cache.hierarchy = fe->dtv_property_cache.hierarchy;
  1560. state->fe[index_frontend]->dtv_property_cache.code_rate_HP = fe->dtv_property_cache.code_rate_HP;
  1561. state->fe[index_frontend]->dtv_property_cache.code_rate_LP = fe->dtv_property_cache.code_rate_LP;
  1562. state->fe[index_frontend]->dtv_property_cache.rolloff = fe->dtv_property_cache.rolloff;
  1563. }
  1564. return 0;
  1565. }
  1566. static int dib9000_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  1567. {
  1568. struct dib9000_state *state = fe->demodulator_priv;
  1569. state->tune_state = tune_state;
  1570. if (tune_state == CT_DEMOD_START)
  1571. state->status = FE_STATUS_TUNE_PENDING;
  1572. return 0;
  1573. }
  1574. static u32 dib9000_get_status(struct dvb_frontend *fe)
  1575. {
  1576. struct dib9000_state *state = fe->demodulator_priv;
  1577. return state->status;
  1578. }
  1579. static int dib9000_set_channel_status(struct dvb_frontend *fe, struct dvb_frontend_parametersContext *channel_status)
  1580. {
  1581. struct dib9000_state *state = fe->demodulator_priv;
  1582. memcpy(&state->channel_status, channel_status, sizeof(struct dvb_frontend_parametersContext));
  1583. return 0;
  1584. }
  1585. static int dib9000_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1586. {
  1587. struct dib9000_state *state = fe->demodulator_priv;
  1588. int sleep_time, sleep_time_slave;
  1589. u32 frontend_status;
  1590. u8 nbr_pending, exit_condition, index_frontend, index_frontend_success;
  1591. struct dvb_frontend_parametersContext channel_status;
  1592. /* check that the correct parameters are set */
  1593. if (state->fe[0]->dtv_property_cache.frequency == 0) {
  1594. dprintk("dib9000: must specify frequency ");
  1595. return 0;
  1596. }
  1597. if (state->fe[0]->dtv_property_cache.bandwidth_hz == 0) {
  1598. dprintk("dib9000: must specify bandwidth ");
  1599. return 0;
  1600. }
  1601. fe->dtv_property_cache.delivery_system = SYS_DVBT;
  1602. /* set the master status */
  1603. if (fep->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO ||
  1604. fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO || fep->u.ofdm.constellation == QAM_AUTO || fep->u.ofdm.code_rate_HP == FEC_AUTO) {
  1605. /* no channel specified, autosearch the channel */
  1606. state->channel_status.status = CHANNEL_STATUS_PARAMETERS_UNKNOWN;
  1607. } else
  1608. state->channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;
  1609. /* set mode and status for the different frontends */
  1610. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1611. dib9000_fw_set_diversity_in(state->fe[index_frontend], 1);
  1612. /* synchronization of the cache */
  1613. memcpy(&state->fe[index_frontend]->dtv_property_cache, &fe->dtv_property_cache, sizeof(struct dtv_frontend_properties));
  1614. state->fe[index_frontend]->dtv_property_cache.delivery_system = SYS_DVBT;
  1615. dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_HIGH_Z);
  1616. dib9000_set_channel_status(state->fe[index_frontend], &state->channel_status);
  1617. dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  1618. }
  1619. /* actual tune */
  1620. exit_condition = 0; /* 0: tune pending; 1: tune failed; 2:tune success */
  1621. index_frontend_success = 0;
  1622. do {
  1623. sleep_time = dib9000_fw_tune(state->fe[0], NULL);
  1624. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1625. sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend], NULL);
  1626. if (sleep_time == FE_CALLBACK_TIME_NEVER)
  1627. sleep_time = sleep_time_slave;
  1628. else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
  1629. sleep_time = sleep_time_slave;
  1630. }
  1631. if (sleep_time != FE_CALLBACK_TIME_NEVER)
  1632. msleep(sleep_time / 10);
  1633. else
  1634. break;
  1635. nbr_pending = 0;
  1636. exit_condition = 0;
  1637. index_frontend_success = 0;
  1638. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1639. frontend_status = -dib9000_get_status(state->fe[index_frontend]);
  1640. if (frontend_status > -FE_STATUS_TUNE_PENDING) {
  1641. exit_condition = 2; /* tune success */
  1642. index_frontend_success = index_frontend;
  1643. break;
  1644. }
  1645. if (frontend_status == -FE_STATUS_TUNE_PENDING)
  1646. nbr_pending++; /* some frontends are still tuning */
  1647. }
  1648. if ((exit_condition != 2) && (nbr_pending == 0))
  1649. exit_condition = 1; /* if all tune are done and no success, exit: tune failed */
  1650. } while (exit_condition == 0);
  1651. /* check the tune result */
  1652. if (exit_condition == 1) { /* tune failed */
  1653. dprintk("tune failed");
  1654. return 0;
  1655. }
  1656. dprintk("tune success on frontend%i", index_frontend_success);
  1657. /* synchronize all the channel cache */
  1658. dib9000_get_frontend(state->fe[0], fep);
  1659. /* retune the other frontends with the found channel */
  1660. channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;
  1661. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1662. /* only retune the frontends which was not tuned success */
  1663. if (index_frontend != index_frontend_success) {
  1664. dib9000_set_channel_status(state->fe[index_frontend], &channel_status);
  1665. dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  1666. }
  1667. }
  1668. do {
  1669. sleep_time = FE_CALLBACK_TIME_NEVER;
  1670. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1671. if (index_frontend != index_frontend_success) {
  1672. sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend], NULL);
  1673. if (sleep_time == FE_CALLBACK_TIME_NEVER)
  1674. sleep_time = sleep_time_slave;
  1675. else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
  1676. sleep_time = sleep_time_slave;
  1677. }
  1678. }
  1679. if (sleep_time != FE_CALLBACK_TIME_NEVER)
  1680. msleep(sleep_time / 10);
  1681. else
  1682. break;
  1683. nbr_pending = 0;
  1684. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1685. if (index_frontend != index_frontend_success) {
  1686. frontend_status = -dib9000_get_status(state->fe[index_frontend]);
  1687. if ((index_frontend != index_frontend_success) && (frontend_status == -FE_STATUS_TUNE_PENDING))
  1688. nbr_pending++; /* some frontends are still tuning */
  1689. }
  1690. }
  1691. } while (nbr_pending != 0);
  1692. /* set the output mode */
  1693. dib9000_fw_set_output_mode(state->fe[0], state->chip.d9.cfg.output_mode);
  1694. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1695. dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_DIVERSITY);
  1696. /* turn off the diversity for the last frontend */
  1697. dib9000_fw_set_diversity_in(state->fe[index_frontend - 1], 0);
  1698. return 0;
  1699. }
  1700. static u16 dib9000_read_lock(struct dvb_frontend *fe)
  1701. {
  1702. struct dib9000_state *state = fe->demodulator_priv;
  1703. return dib9000_read_word(state, 535);
  1704. }
  1705. static int dib9000_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  1706. {
  1707. struct dib9000_state *state = fe->demodulator_priv;
  1708. u8 index_frontend;
  1709. u16 lock = 0, lock_slave = 0;
  1710. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1711. lock_slave |= dib9000_read_lock(state->fe[index_frontend]);
  1712. lock = dib9000_read_word(state, 535);
  1713. *stat = 0;
  1714. if ((lock & 0x8000) || (lock_slave & 0x8000))
  1715. *stat |= FE_HAS_SIGNAL;
  1716. if ((lock & 0x3000) || (lock_slave & 0x3000))
  1717. *stat |= FE_HAS_CARRIER;
  1718. if ((lock & 0x0100) || (lock_slave & 0x0100))
  1719. *stat |= FE_HAS_VITERBI;
  1720. if (((lock & 0x0038) == 0x38) || ((lock_slave & 0x0038) == 0x38))
  1721. *stat |= FE_HAS_SYNC;
  1722. if ((lock & 0x0008) || (lock_slave & 0x0008))
  1723. *stat |= FE_HAS_LOCK;
  1724. return 0;
  1725. }
  1726. static int dib9000_read_ber(struct dvb_frontend *fe, u32 * ber)
  1727. {
  1728. struct dib9000_state *state = fe->demodulator_priv;
  1729. u16 c[16];
  1730. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1731. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1732. return -EIO;
  1733. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, sizeof(c));
  1734. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1735. *ber = c[10] << 16 | c[11];
  1736. return 0;
  1737. }
  1738. static int dib9000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1739. {
  1740. struct dib9000_state *state = fe->demodulator_priv;
  1741. u8 index_frontend;
  1742. u16 c[16];
  1743. u16 val;
  1744. *strength = 0;
  1745. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1746. state->fe[index_frontend]->ops.read_signal_strength(state->fe[index_frontend], &val);
  1747. if (val > 65535 - *strength)
  1748. *strength = 65535;
  1749. else
  1750. *strength += val;
  1751. }
  1752. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1753. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1754. return -EIO;
  1755. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, sizeof(c));
  1756. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1757. val = 65535 - c[4];
  1758. if (val > 65535 - *strength)
  1759. *strength = 65535;
  1760. else
  1761. *strength += val;
  1762. return 0;
  1763. }
  1764. static u32 dib9000_get_snr(struct dvb_frontend *fe)
  1765. {
  1766. struct dib9000_state *state = fe->demodulator_priv;
  1767. u16 c[16];
  1768. u32 n, s, exp;
  1769. u16 val;
  1770. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1771. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1772. return -EIO;
  1773. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, sizeof(c));
  1774. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1775. val = c[7];
  1776. n = (val >> 4) & 0xff;
  1777. exp = ((val & 0xf) << 2);
  1778. val = c[8];
  1779. exp += ((val >> 14) & 0x3);
  1780. if ((exp & 0x20) != 0)
  1781. exp -= 0x40;
  1782. n <<= exp + 16;
  1783. s = (val >> 6) & 0xFF;
  1784. exp = (val & 0x3F);
  1785. if ((exp & 0x20) != 0)
  1786. exp -= 0x40;
  1787. s <<= exp + 16;
  1788. if (n > 0) {
  1789. u32 t = (s / n) << 16;
  1790. return t + ((s << 16) - n * t) / n;
  1791. }
  1792. return 0xffffffff;
  1793. }
  1794. static int dib9000_read_snr(struct dvb_frontend *fe, u16 * snr)
  1795. {
  1796. struct dib9000_state *state = fe->demodulator_priv;
  1797. u8 index_frontend;
  1798. u32 snr_master;
  1799. snr_master = dib9000_get_snr(fe);
  1800. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1801. snr_master += dib9000_get_snr(state->fe[index_frontend]);
  1802. if ((snr_master >> 16) != 0) {
  1803. snr_master = 10 * intlog10(snr_master >> 16);
  1804. *snr = snr_master / ((1 << 24) / 10);
  1805. } else
  1806. *snr = 0;
  1807. return 0;
  1808. }
  1809. static int dib9000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1810. {
  1811. struct dib9000_state *state = fe->demodulator_priv;
  1812. u16 c[16];
  1813. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1814. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1815. return -EIO;
  1816. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, sizeof(c));
  1817. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1818. *unc = c[12];
  1819. return 0;
  1820. }
  1821. int dib9000_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, u8 first_addr)
  1822. {
  1823. int k = 0;
  1824. u8 new_addr = 0;
  1825. struct i2c_device client = {.i2c_adap = i2c };
  1826. client.i2c_addr = default_addr + 16;
  1827. dib9000_i2c_write16(&client, 1796, 0x0);
  1828. for (k = no_of_demods - 1; k >= 0; k--) {
  1829. /* designated i2c address */
  1830. new_addr = first_addr + (k << 1);
  1831. client.i2c_addr = default_addr;
  1832. dib9000_i2c_write16(&client, 1817, 3);
  1833. dib9000_i2c_write16(&client, 1796, 0);
  1834. dib9000_i2c_write16(&client, 1227, 1);
  1835. dib9000_i2c_write16(&client, 1227, 0);
  1836. client.i2c_addr = new_addr;
  1837. dib9000_i2c_write16(&client, 1817, 3);
  1838. dib9000_i2c_write16(&client, 1796, 0);
  1839. dib9000_i2c_write16(&client, 1227, 1);
  1840. dib9000_i2c_write16(&client, 1227, 0);
  1841. if (dib9000_identify(&client) == 0) {
  1842. client.i2c_addr = default_addr;
  1843. if (dib9000_identify(&client) == 0) {
  1844. dprintk("DiB9000 #%d: not identified", k);
  1845. return -EIO;
  1846. }
  1847. }
  1848. dib9000_i2c_write16(&client, 1795, (1 << 10) | (4 << 6));
  1849. dib9000_i2c_write16(&client, 1794, (new_addr << 2) | 2);
  1850. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1851. }
  1852. for (k = 0; k < no_of_demods; k++) {
  1853. new_addr = first_addr | (k << 1);
  1854. client.i2c_addr = new_addr;
  1855. dib9000_i2c_write16(&client, 1794, (new_addr << 2));
  1856. dib9000_i2c_write16(&client, 1795, 0);
  1857. }
  1858. return 0;
  1859. }
  1860. EXPORT_SYMBOL(dib9000_i2c_enumeration);
  1861. int dib9000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
  1862. {
  1863. struct dib9000_state *state = fe->demodulator_priv;
  1864. u8 index_frontend = 1;
  1865. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  1866. index_frontend++;
  1867. if (index_frontend < MAX_NUMBER_OF_FRONTENDS) {
  1868. dprintk("set slave fe %p to index %i", fe_slave, index_frontend);
  1869. state->fe[index_frontend] = fe_slave;
  1870. return 0;
  1871. }
  1872. dprintk("too many slave frontend");
  1873. return -ENOMEM;
  1874. }
  1875. EXPORT_SYMBOL(dib9000_set_slave_frontend);
  1876. int dib9000_remove_slave_frontend(struct dvb_frontend *fe)
  1877. {
  1878. struct dib9000_state *state = fe->demodulator_priv;
  1879. u8 index_frontend = 1;
  1880. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  1881. index_frontend++;
  1882. if (index_frontend != 1) {
  1883. dprintk("remove slave fe %p (index %i)", state->fe[index_frontend - 1], index_frontend - 1);
  1884. state->fe[index_frontend] = NULL;
  1885. return 0;
  1886. }
  1887. dprintk("no frontend to be removed");
  1888. return -ENODEV;
  1889. }
  1890. EXPORT_SYMBOL(dib9000_remove_slave_frontend);
  1891. struct dvb_frontend *dib9000_get_slave_frontend(struct dvb_frontend *fe, int slave_index)
  1892. {
  1893. struct dib9000_state *state = fe->demodulator_priv;
  1894. if (slave_index >= MAX_NUMBER_OF_FRONTENDS)
  1895. return NULL;
  1896. return state->fe[slave_index];
  1897. }
  1898. EXPORT_SYMBOL(dib9000_get_slave_frontend);
  1899. static struct dvb_frontend_ops dib9000_ops;
  1900. struct dvb_frontend *dib9000_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, const struct dib9000_config *cfg)
  1901. {
  1902. struct dvb_frontend *fe;
  1903. struct dib9000_state *st;
  1904. st = kzalloc(sizeof(struct dib9000_state), GFP_KERNEL);
  1905. if (st == NULL)
  1906. return NULL;
  1907. fe = kzalloc(sizeof(struct dvb_frontend), GFP_KERNEL);
  1908. if (fe == NULL)
  1909. return NULL;
  1910. memcpy(&st->chip.d9.cfg, cfg, sizeof(struct dib9000_config));
  1911. st->i2c.i2c_adap = i2c_adap;
  1912. st->i2c.i2c_addr = i2c_addr;
  1913. st->gpio_dir = DIB9000_GPIO_DEFAULT_DIRECTIONS;
  1914. st->gpio_val = DIB9000_GPIO_DEFAULT_VALUES;
  1915. st->gpio_pwm_pos = DIB9000_GPIO_DEFAULT_PWM_POS;
  1916. DibInitLock(&st->platform.risc.mbx_if_lock);
  1917. DibInitLock(&st->platform.risc.mbx_lock);
  1918. DibInitLock(&st->platform.risc.mem_lock);
  1919. DibInitLock(&st->platform.risc.mem_mbx_lock);
  1920. st->fe[0] = fe;
  1921. fe->demodulator_priv = st;
  1922. memcpy(&st->fe[0]->ops, &dib9000_ops, sizeof(struct dvb_frontend_ops));
  1923. /* Ensure the output mode remains at the previous default if it's
  1924. * not specifically set by the caller.
  1925. */
  1926. if ((st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  1927. st->chip.d9.cfg.output_mode = OUTMODE_MPEG2_FIFO;
  1928. if (dib9000_identify(&st->i2c) == 0)
  1929. goto error;
  1930. dibx000_init_i2c_master(&st->i2c_master, DIB7000MC, st->i2c.i2c_adap, st->i2c.i2c_addr);
  1931. st->tuner_adap.dev.parent = i2c_adap->dev.parent;
  1932. strncpy(st->tuner_adap.name, "DIB9000_FW TUNER ACCESS", sizeof(st->tuner_adap.name));
  1933. st->tuner_adap.algo = &dib9000_tuner_algo;
  1934. st->tuner_adap.algo_data = NULL;
  1935. i2c_set_adapdata(&st->tuner_adap, st);
  1936. if (i2c_add_adapter(&st->tuner_adap) < 0)
  1937. goto error;
  1938. st->component_bus.dev.parent = i2c_adap->dev.parent;
  1939. strncpy(st->component_bus.name, "DIB9000_FW COMPONENT BUS ACCESS", sizeof(st->component_bus.name));
  1940. st->component_bus.algo = &dib9000_component_bus_algo;
  1941. st->component_bus.algo_data = NULL;
  1942. st->component_bus_speed = 340;
  1943. i2c_set_adapdata(&st->component_bus, st);
  1944. if (i2c_add_adapter(&st->component_bus) < 0)
  1945. goto component_bus_add_error;
  1946. dib9000_fw_reset(fe);
  1947. return fe;
  1948. component_bus_add_error:
  1949. i2c_del_adapter(&st->tuner_adap);
  1950. error:
  1951. kfree(st);
  1952. return NULL;
  1953. }
  1954. EXPORT_SYMBOL(dib9000_attach);
  1955. static struct dvb_frontend_ops dib9000_ops = {
  1956. .info = {
  1957. .name = "DiBcom 9000",
  1958. .type = FE_OFDM,
  1959. .frequency_min = 44250000,
  1960. .frequency_max = 867250000,
  1961. .frequency_stepsize = 62500,
  1962. .caps = FE_CAN_INVERSION_AUTO |
  1963. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1964. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1965. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1966. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  1967. },
  1968. .release = dib9000_release,
  1969. .init = dib9000_wakeup,
  1970. .sleep = dib9000_sleep,
  1971. .set_frontend = dib9000_set_frontend,
  1972. .get_tune_settings = dib9000_fe_get_tune_settings,
  1973. .get_frontend = dib9000_get_frontend,
  1974. .read_status = dib9000_read_status,
  1975. .read_ber = dib9000_read_ber,
  1976. .read_signal_strength = dib9000_read_signal_strength,
  1977. .read_snr = dib9000_read_snr,
  1978. .read_ucblocks = dib9000_read_unc_blocks,
  1979. };
  1980. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  1981. MODULE_AUTHOR("Olivier Grenie <ogrenie@dibcom.fr>");
  1982. MODULE_DESCRIPTION("Driver for the DiBcom 9000 COFDM demodulator");
  1983. MODULE_LICENSE("GPL");