dib9000.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  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 ret = 0, *d, b[2];
  404. u16 tmp;
  405. u16 size;
  406. u32 i;
  407. if (!state->platform.risc.fw_is_running)
  408. return -EINVAL;
  409. DibAcquireLock(&state->platform.risc.mbx_if_lock);
  410. tmp = MAX_MAILBOX_TRY;
  411. do {
  412. size = dib9000_read_word_attr(state, 1043, attr) & 0xff;
  413. if ((size + len + 1) > MBX_MAX_WORDS && --tmp) {
  414. dprintk("MBX: RISC mbx full, retrying");
  415. msleep(100);
  416. } else
  417. break;
  418. } while (1);
  419. /*dprintk( "MBX: size: %d", size); */
  420. if (tmp == 0) {
  421. ret = -EINVAL;
  422. goto out;
  423. }
  424. #ifdef DUMP_MSG
  425. dprintk("--> %02x %d ", id, len + 1);
  426. for (i = 0; i < len; i++)
  427. dprintk("%04x ", data[i]);
  428. dprintk("\n");
  429. #endif
  430. /* byte-order conversion - works on big (where it is not necessary) or little endian */
  431. d = (u8 *) data;
  432. for (i = 0; i < len; i++) {
  433. tmp = data[i];
  434. *d++ = tmp >> 8;
  435. *d++ = tmp & 0xff;
  436. }
  437. /* write msg */
  438. b[0] = id;
  439. b[1] = len + 1;
  440. if (dib9000_write16_noinc_attr(state, 1045, b, 2, attr) != 0 || dib9000_write16_noinc_attr(state, 1045, (u8 *) data, len * 2, attr) != 0) {
  441. ret = -EIO;
  442. goto out;
  443. }
  444. /* update register nb_mes_in_RX */
  445. ret = (u8) dib9000_write_word_attr(state, 1043, 1 << 14, attr);
  446. out:
  447. DibReleaseLock(&state->platform.risc.mbx_if_lock);
  448. return ret;
  449. }
  450. static u8 dib9000_mbx_read(struct dib9000_state *state, u16 * data, u8 risc_id, u16 attr)
  451. {
  452. #ifdef DUMP_MSG
  453. u16 *d = data;
  454. #endif
  455. u16 tmp, i;
  456. u8 size;
  457. u8 mc_base;
  458. if (!state->platform.risc.fw_is_running)
  459. return 0;
  460. DibAcquireLock(&state->platform.risc.mbx_if_lock);
  461. if (risc_id == 1)
  462. mc_base = 16;
  463. else
  464. mc_base = 0;
  465. /* Length and type in the first word */
  466. *data = dib9000_read_word_attr(state, 1029 + mc_base, attr);
  467. size = *data & 0xff;
  468. if (size <= MBX_MAX_WORDS) {
  469. data++;
  470. size--; /* Initial word already read */
  471. dib9000_read16_noinc_attr(state, 1029 + mc_base, (u8 *) data, size * 2, attr);
  472. /* to word conversion */
  473. for (i = 0; i < size; i++) {
  474. tmp = *data;
  475. *data = (tmp >> 8) | (tmp << 8);
  476. data++;
  477. }
  478. #ifdef DUMP_MSG
  479. dprintk("<-- ");
  480. for (i = 0; i < size + 1; i++)
  481. dprintk("%04x ", d[i]);
  482. dprintk("\n");
  483. #endif
  484. } else {
  485. dprintk("MBX: message is too big for message cache (%d), flushing message", size);
  486. size--; /* Initial word already read */
  487. while (size--)
  488. dib9000_read16_noinc_attr(state, 1029 + mc_base, (u8 *) data, 2, attr);
  489. }
  490. /* Update register nb_mes_in_TX */
  491. dib9000_write_word_attr(state, 1028 + mc_base, 1 << 14, attr);
  492. DibReleaseLock(&state->platform.risc.mbx_if_lock);
  493. return size + 1;
  494. }
  495. static int dib9000_risc_debug_buf(struct dib9000_state *state, u16 * data, u8 size)
  496. {
  497. u32 ts = data[1] << 16 | data[0];
  498. char *b = (char *)&data[2];
  499. b[2 * (size - 2) - 1] = '\0'; /* Bullet proof the buffer */
  500. if (*b == '~') {
  501. b++;
  502. dprintk(b);
  503. } else
  504. dprintk("RISC%d: %d.%04d %s", state->fe_id, ts / 10000, ts % 10000, *b ? b : "<emtpy>");
  505. return 1;
  506. }
  507. static int dib9000_mbx_fetch_to_cache(struct dib9000_state *state, u16 attr)
  508. {
  509. int i;
  510. u8 size;
  511. u16 *block;
  512. /* find a free slot */
  513. for (i = 0; i < DIB9000_MSG_CACHE_SIZE; i++) {
  514. block = state->platform.risc.message_cache[i];
  515. if (*block == 0) {
  516. size = dib9000_mbx_read(state, block, 1, attr);
  517. /* dprintk( "MBX: fetched %04x message to cache", *block); */
  518. switch (*block >> 8) {
  519. case IN_MSG_DEBUG_BUF:
  520. dib9000_risc_debug_buf(state, block + 1, size); /* debug-messages are going to be printed right away */
  521. *block = 0; /* free the block */
  522. break;
  523. #if 0
  524. case IN_MSG_DATA: /* FE-TRACE */
  525. dib9000_risc_data_process(state, block + 1, size);
  526. *block = 0;
  527. break;
  528. #endif
  529. default:
  530. break;
  531. }
  532. return 1;
  533. }
  534. }
  535. dprintk("MBX: no free cache-slot found for new message...");
  536. return -1;
  537. }
  538. static u8 dib9000_mbx_count(struct dib9000_state *state, u8 risc_id, u16 attr)
  539. {
  540. if (risc_id == 0)
  541. return (u8) (dib9000_read_word_attr(state, 1028, attr) >> 10) & 0x1f; /* 5 bit field */
  542. else
  543. return (u8) (dib9000_read_word_attr(state, 1044, attr) >> 8) & 0x7f; /* 7 bit field */
  544. }
  545. static int dib9000_mbx_process(struct dib9000_state *state, u16 attr)
  546. {
  547. int ret = 0;
  548. u16 tmp;
  549. if (!state->platform.risc.fw_is_running)
  550. return -1;
  551. DibAcquireLock(&state->platform.risc.mbx_lock);
  552. if (dib9000_mbx_count(state, 1, attr)) /* 1=RiscB */
  553. ret = dib9000_mbx_fetch_to_cache(state, attr);
  554. tmp = dib9000_read_word_attr(state, 1229, attr); /* Clear the IRQ */
  555. /* if (tmp) */
  556. /* dprintk( "cleared IRQ: %x", tmp); */
  557. DibReleaseLock(&state->platform.risc.mbx_lock);
  558. return ret;
  559. }
  560. static int dib9000_mbx_get_message_attr(struct dib9000_state *state, u16 id, u16 * msg, u8 * size, u16 attr)
  561. {
  562. u8 i;
  563. u16 *block;
  564. u16 timeout = 30;
  565. *msg = 0;
  566. do {
  567. /* dib9000_mbx_get_from_cache(); */
  568. for (i = 0; i < DIB9000_MSG_CACHE_SIZE; i++) {
  569. block = state->platform.risc.message_cache[i];
  570. if ((*block >> 8) == id) {
  571. *size = (*block & 0xff) - 1;
  572. memcpy(msg, block + 1, (*size) * 2);
  573. *block = 0; /* free the block */
  574. i = 0; /* signal that we found a message */
  575. break;
  576. }
  577. }
  578. if (i == 0)
  579. break;
  580. if (dib9000_mbx_process(state, attr) == -1) /* try to fetch one message - if any */
  581. return -1;
  582. } while (--timeout);
  583. if (timeout == 0) {
  584. dprintk("waiting for message %d timed out", id);
  585. return -1;
  586. }
  587. return i == 0;
  588. }
  589. static int dib9000_risc_check_version(struct dib9000_state *state)
  590. {
  591. u8 r[4];
  592. u8 size;
  593. u16 fw_version = 0;
  594. if (dib9000_mbx_send(state, OUT_MSG_REQ_VERSION, &fw_version, 1) != 0)
  595. return -EIO;
  596. if (dib9000_mbx_get_message(state, IN_MSG_VERSION, (u16 *) r, &size) < 0)
  597. return -EIO;
  598. fw_version = (r[0] << 8) | r[1];
  599. dprintk("RISC: ver: %d.%02d (IC: %d)", fw_version >> 10, fw_version & 0x3ff, (r[2] << 8) | r[3]);
  600. if ((fw_version >> 10) != 7)
  601. return -EINVAL;
  602. switch (fw_version & 0x3ff) {
  603. case 11:
  604. case 12:
  605. case 14:
  606. case 15:
  607. case 16:
  608. case 17:
  609. break;
  610. default:
  611. dprintk("RISC: invalid firmware version");
  612. return -EINVAL;
  613. }
  614. dprintk("RISC: valid firmware version");
  615. return 0;
  616. }
  617. static int dib9000_fw_boot(struct dib9000_state *state, const u8 * codeA, u32 lenA, const u8 * codeB, u32 lenB)
  618. {
  619. /* Reconfig pool mac ram */
  620. dib9000_write_word(state, 1225, 0x02); /* A: 8k C, 4 k D - B: 32k C 6 k D - IRAM 96k */
  621. dib9000_write_word(state, 1226, 0x05);
  622. /* Toggles IP crypto to Host APB interface. */
  623. dib9000_write_word(state, 1542, 1);
  624. /* Set jump and no jump in the dma box */
  625. dib9000_write_word(state, 1074, 0);
  626. dib9000_write_word(state, 1075, 0);
  627. /* Set MAC as APB Master. */
  628. dib9000_write_word(state, 1237, 0);
  629. /* Reset the RISCs */
  630. if (codeA != NULL)
  631. dib9000_write_word(state, 1024, 2);
  632. else
  633. dib9000_write_word(state, 1024, 15);
  634. if (codeB != NULL)
  635. dib9000_write_word(state, 1040, 2);
  636. if (codeA != NULL)
  637. dib9000_firmware_download(state, 0, 0x1234, codeA, lenA);
  638. if (codeB != NULL)
  639. dib9000_firmware_download(state, 1, 0x1234, codeB, lenB);
  640. /* Run the RISCs */
  641. if (codeA != NULL)
  642. dib9000_write_word(state, 1024, 0);
  643. if (codeB != NULL)
  644. dib9000_write_word(state, 1040, 0);
  645. if (codeA != NULL)
  646. if (dib9000_mbx_host_init(state, 0) != 0)
  647. return -EIO;
  648. if (codeB != NULL)
  649. if (dib9000_mbx_host_init(state, 1) != 0)
  650. return -EIO;
  651. msleep(100);
  652. state->platform.risc.fw_is_running = 1;
  653. if (dib9000_risc_check_version(state) != 0)
  654. return -EINVAL;
  655. state->platform.risc.memcmd = 0xff;
  656. return 0;
  657. }
  658. static u16 dib9000_identify(struct i2c_device *client)
  659. {
  660. u16 value;
  661. value = dib9000_i2c_read16(client, 896);
  662. if (value != 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);
  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, u8 * b, u32 len)
  791. {
  792. u16 mb[10];
  793. u8 i, s;
  794. if (address >= 1024 || !state->platform.risc.fw_is_running)
  795. return -EINVAL;
  796. /* dprintk( "APB access thru rd fw %d %x", address, attribute); */
  797. mb[0] = (u16) address;
  798. mb[1] = len / 2;
  799. dib9000_mbx_send_attr(state, OUT_MSG_BRIDGE_APB_R, mb, 2, attribute);
  800. switch (dib9000_mbx_get_message_attr(state, IN_MSG_END_BRIDGE_APB_RW, mb, &s, attribute)) {
  801. case 1:
  802. s--;
  803. for (i = 0; i < s; i++) {
  804. b[i * 2] = (mb[i + 1] >> 8) & 0xff;
  805. b[i * 2 + 1] = (mb[i + 1]) & 0xff;
  806. }
  807. return 0;
  808. default:
  809. return -EIO;
  810. }
  811. return -EIO;
  812. }
  813. static int dib9000_risc_apb_access_write(struct dib9000_state *state, u32 address, u16 attribute, const u8 * b, u32 len)
  814. {
  815. u16 mb[10];
  816. u8 s, i;
  817. if (address >= 1024 || !state->platform.risc.fw_is_running)
  818. return -EINVAL;
  819. /* dprintk( "APB access thru wr fw %d %x", address, attribute); */
  820. mb[0] = (unsigned short)address;
  821. for (i = 0; i < len && i < 20; i += 2)
  822. mb[1 + (i / 2)] = (b[i] << 8 | b[i + 1]);
  823. dib9000_mbx_send_attr(state, OUT_MSG_BRIDGE_APB_W, mb, 1 + len / 2, attribute);
  824. return dib9000_mbx_get_message_attr(state, IN_MSG_END_BRIDGE_APB_RW, mb, &s, attribute) == 1 ? 0 : -EINVAL;
  825. }
  826. static int dib9000_fw_memmbx_sync(struct dib9000_state *state, u8 i)
  827. {
  828. u8 index_loop = 10;
  829. if (!state->platform.risc.fw_is_running)
  830. return 0;
  831. dib9000_risc_mem_write(state, FE_MM_RW_SYNC, &i);
  832. do {
  833. dib9000_risc_mem_read(state, FE_MM_RW_SYNC, &i, 1);
  834. } while (i && index_loop--);
  835. if (index_loop > 0)
  836. return 0;
  837. return -EIO;
  838. }
  839. static int dib9000_fw_init(struct dib9000_state *state)
  840. {
  841. struct dibGPIOFunction *f;
  842. u16 b[40] = { 0 };
  843. u8 i;
  844. u8 size;
  845. if (dib9000_fw_boot(state, NULL, 0, state->chip.d9.cfg.microcode_B_fe_buffer, state->chip.d9.cfg.microcode_B_fe_size) != 0)
  846. return -EIO;
  847. /* initialize the firmware */
  848. for (i = 0; i < ARRAY_SIZE(state->chip.d9.cfg.gpio_function); i++) {
  849. f = &state->chip.d9.cfg.gpio_function[i];
  850. if (f->mask) {
  851. switch (f->function) {
  852. case BOARD_GPIO_FUNCTION_COMPONENT_ON:
  853. b[0] = (u16) f->mask;
  854. b[1] = (u16) f->direction;
  855. b[2] = (u16) f->value;
  856. break;
  857. case BOARD_GPIO_FUNCTION_COMPONENT_OFF:
  858. b[3] = (u16) f->mask;
  859. b[4] = (u16) f->direction;
  860. b[5] = (u16) f->value;
  861. break;
  862. }
  863. }
  864. }
  865. if (dib9000_mbx_send(state, OUT_MSG_CONF_GPIO, b, 15) != 0)
  866. return -EIO;
  867. /* subband */
  868. b[0] = state->chip.d9.cfg.subband.size; /* type == 0 -> GPIO - PWM not yet supported */
  869. for (i = 0; i < state->chip.d9.cfg.subband.size; i++) {
  870. b[1 + i * 4] = state->chip.d9.cfg.subband.subband[i].f_mhz;
  871. b[2 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.mask;
  872. b[3 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.direction;
  873. b[4 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.value;
  874. }
  875. b[1 + i * 4] = 0; /* fe_id */
  876. if (dib9000_mbx_send(state, OUT_MSG_SUBBAND_SEL, b, 2 + 4 * i) != 0)
  877. return -EIO;
  878. /* 0 - id, 1 - no_of_frontends */
  879. b[0] = (0 << 8) | 1;
  880. /* 0 = i2c-address demod, 0 = tuner */
  881. b[1] = (0 << 8) | (0);
  882. b[2] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000) >> 16) & 0xffff);
  883. b[3] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000)) & 0xffff);
  884. b[4] = (u16) ((state->chip.d9.cfg.vcxo_timer >> 16) & 0xffff);
  885. b[5] = (u16) ((state->chip.d9.cfg.vcxo_timer) & 0xffff);
  886. b[6] = (u16) ((state->chip.d9.cfg.timing_frequency >> 16) & 0xffff);
  887. b[7] = (u16) ((state->chip.d9.cfg.timing_frequency) & 0xffff);
  888. b[29] = state->chip.d9.cfg.if_drives;
  889. if (dib9000_mbx_send(state, OUT_MSG_INIT_DEMOD, b, ARRAY_SIZE(b)) != 0)
  890. return -EIO;
  891. if (dib9000_mbx_send(state, OUT_MSG_FE_FW_DL, NULL, 0) != 0)
  892. return -EIO;
  893. if (dib9000_mbx_get_message(state, IN_MSG_FE_FW_DL_DONE, b, &size) < 0)
  894. return -EIO;
  895. if (size > ARRAY_SIZE(b)) {
  896. dprintk("error : firmware returned %dbytes needed but the used buffer has only %dbytes\n Firmware init ABORTED", size,
  897. (int)ARRAY_SIZE(b));
  898. return -EINVAL;
  899. }
  900. for (i = 0; i < size; i += 2) {
  901. state->platform.risc.fe_mm[i / 2].addr = b[i + 0];
  902. state->platform.risc.fe_mm[i / 2].size = b[i + 1];
  903. }
  904. return 0;
  905. }
  906. static void dib9000_fw_set_channel_head(struct dib9000_state *state, struct dvb_frontend_parameters *ch)
  907. {
  908. u8 b[9];
  909. u32 freq = state->fe[0]->dtv_property_cache.frequency / 1000;
  910. if (state->fe_id % 2)
  911. freq += 101;
  912. b[0] = (u8) ((freq >> 0) & 0xff);
  913. b[1] = (u8) ((freq >> 8) & 0xff);
  914. b[2] = (u8) ((freq >> 16) & 0xff);
  915. b[3] = (u8) ((freq >> 24) & 0xff);
  916. b[4] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 0) & 0xff);
  917. b[5] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 8) & 0xff);
  918. b[6] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 16) & 0xff);
  919. b[7] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 24) & 0xff);
  920. b[8] = 0x80; /* do not wait for CELL ID when doing autosearch */
  921. if (state->fe[0]->dtv_property_cache.delivery_system == SYS_DVBT)
  922. b[8] |= 1;
  923. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_HEAD, b);
  924. }
  925. static int dib9000_fw_get_channel(struct dvb_frontend *fe, struct dvb_frontend_parameters *channel)
  926. {
  927. struct dib9000_state *state = fe->demodulator_priv;
  928. struct dibDVBTChannel {
  929. s8 spectrum_inversion;
  930. s8 nfft;
  931. s8 guard;
  932. s8 constellation;
  933. s8 hrch;
  934. s8 alpha;
  935. s8 code_rate_hp;
  936. s8 code_rate_lp;
  937. s8 select_hp;
  938. s8 intlv_native;
  939. };
  940. struct dibDVBTChannel ch;
  941. int ret = 0;
  942. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  943. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
  944. goto error;
  945. ret = -EIO;
  946. }
  947. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_UNION, (u8 *) &ch, sizeof(struct dibDVBTChannel));
  948. switch (ch.spectrum_inversion & 0x7) {
  949. case 1:
  950. state->fe[0]->dtv_property_cache.inversion = INVERSION_ON;
  951. break;
  952. case 0:
  953. state->fe[0]->dtv_property_cache.inversion = INVERSION_OFF;
  954. break;
  955. default:
  956. case -1:
  957. state->fe[0]->dtv_property_cache.inversion = INVERSION_AUTO;
  958. break;
  959. }
  960. switch (ch.nfft) {
  961. case 0:
  962. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
  963. break;
  964. case 2:
  965. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_4K;
  966. break;
  967. case 1:
  968. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  969. break;
  970. default:
  971. case -1:
  972. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_AUTO;
  973. break;
  974. }
  975. switch (ch.guard) {
  976. case 0:
  977. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
  978. break;
  979. case 1:
  980. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
  981. break;
  982. case 2:
  983. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  984. break;
  985. case 3:
  986. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
  987. break;
  988. default:
  989. case -1:
  990. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_AUTO;
  991. break;
  992. }
  993. switch (ch.constellation) {
  994. case 2:
  995. state->fe[0]->dtv_property_cache.modulation = QAM_64;
  996. break;
  997. case 1:
  998. state->fe[0]->dtv_property_cache.modulation = QAM_16;
  999. break;
  1000. case 0:
  1001. state->fe[0]->dtv_property_cache.modulation = QPSK;
  1002. break;
  1003. default:
  1004. case -1:
  1005. state->fe[0]->dtv_property_cache.modulation = QAM_AUTO;
  1006. break;
  1007. }
  1008. switch (ch.hrch) {
  1009. case 0:
  1010. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_NONE;
  1011. break;
  1012. case 1:
  1013. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_1;
  1014. break;
  1015. default:
  1016. case -1:
  1017. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_AUTO;
  1018. break;
  1019. }
  1020. switch (ch.code_rate_hp) {
  1021. case 1:
  1022. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_1_2;
  1023. break;
  1024. case 2:
  1025. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_2_3;
  1026. break;
  1027. case 3:
  1028. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_3_4;
  1029. break;
  1030. case 5:
  1031. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_5_6;
  1032. break;
  1033. case 7:
  1034. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_7_8;
  1035. break;
  1036. default:
  1037. case -1:
  1038. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_AUTO;
  1039. break;
  1040. }
  1041. switch (ch.code_rate_lp) {
  1042. case 1:
  1043. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_1_2;
  1044. break;
  1045. case 2:
  1046. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_2_3;
  1047. break;
  1048. case 3:
  1049. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_3_4;
  1050. break;
  1051. case 5:
  1052. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_5_6;
  1053. break;
  1054. case 7:
  1055. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_7_8;
  1056. break;
  1057. default:
  1058. case -1:
  1059. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_AUTO;
  1060. break;
  1061. }
  1062. error:
  1063. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1064. return ret;
  1065. }
  1066. static int dib9000_fw_set_channel_union(struct dvb_frontend *fe, struct dvb_frontend_parameters *channel)
  1067. {
  1068. struct dib9000_state *state = fe->demodulator_priv;
  1069. struct dibDVBTChannel {
  1070. s8 spectrum_inversion;
  1071. s8 nfft;
  1072. s8 guard;
  1073. s8 constellation;
  1074. s8 hrch;
  1075. s8 alpha;
  1076. s8 code_rate_hp;
  1077. s8 code_rate_lp;
  1078. s8 select_hp;
  1079. s8 intlv_native;
  1080. };
  1081. struct dibDVBTChannel ch;
  1082. switch (state->fe[0]->dtv_property_cache.inversion) {
  1083. case INVERSION_ON:
  1084. ch.spectrum_inversion = 1;
  1085. break;
  1086. case INVERSION_OFF:
  1087. ch.spectrum_inversion = 0;
  1088. break;
  1089. default:
  1090. case INVERSION_AUTO:
  1091. ch.spectrum_inversion = -1;
  1092. break;
  1093. }
  1094. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  1095. case TRANSMISSION_MODE_2K:
  1096. ch.nfft = 0;
  1097. break;
  1098. case TRANSMISSION_MODE_4K:
  1099. ch.nfft = 2;
  1100. break;
  1101. case TRANSMISSION_MODE_8K:
  1102. ch.nfft = 1;
  1103. break;
  1104. default:
  1105. case TRANSMISSION_MODE_AUTO:
  1106. ch.nfft = 1;
  1107. break;
  1108. }
  1109. switch (state->fe[0]->dtv_property_cache.guard_interval) {
  1110. case GUARD_INTERVAL_1_32:
  1111. ch.guard = 0;
  1112. break;
  1113. case GUARD_INTERVAL_1_16:
  1114. ch.guard = 1;
  1115. break;
  1116. case GUARD_INTERVAL_1_8:
  1117. ch.guard = 2;
  1118. break;
  1119. case GUARD_INTERVAL_1_4:
  1120. ch.guard = 3;
  1121. break;
  1122. default:
  1123. case GUARD_INTERVAL_AUTO:
  1124. ch.guard = -1;
  1125. break;
  1126. }
  1127. switch (state->fe[0]->dtv_property_cache.modulation) {
  1128. case QAM_64:
  1129. ch.constellation = 2;
  1130. break;
  1131. case QAM_16:
  1132. ch.constellation = 1;
  1133. break;
  1134. case QPSK:
  1135. ch.constellation = 0;
  1136. break;
  1137. default:
  1138. case QAM_AUTO:
  1139. ch.constellation = -1;
  1140. break;
  1141. }
  1142. switch (state->fe[0]->dtv_property_cache.hierarchy) {
  1143. case HIERARCHY_NONE:
  1144. ch.hrch = 0;
  1145. break;
  1146. case HIERARCHY_1:
  1147. case HIERARCHY_2:
  1148. case HIERARCHY_4:
  1149. ch.hrch = 1;
  1150. break;
  1151. default:
  1152. case HIERARCHY_AUTO:
  1153. ch.hrch = -1;
  1154. break;
  1155. }
  1156. ch.alpha = 1;
  1157. switch (state->fe[0]->dtv_property_cache.code_rate_HP) {
  1158. case FEC_1_2:
  1159. ch.code_rate_hp = 1;
  1160. break;
  1161. case FEC_2_3:
  1162. ch.code_rate_hp = 2;
  1163. break;
  1164. case FEC_3_4:
  1165. ch.code_rate_hp = 3;
  1166. break;
  1167. case FEC_5_6:
  1168. ch.code_rate_hp = 5;
  1169. break;
  1170. case FEC_7_8:
  1171. ch.code_rate_hp = 7;
  1172. break;
  1173. default:
  1174. case FEC_AUTO:
  1175. ch.code_rate_hp = -1;
  1176. break;
  1177. }
  1178. switch (state->fe[0]->dtv_property_cache.code_rate_LP) {
  1179. case FEC_1_2:
  1180. ch.code_rate_lp = 1;
  1181. break;
  1182. case FEC_2_3:
  1183. ch.code_rate_lp = 2;
  1184. break;
  1185. case FEC_3_4:
  1186. ch.code_rate_lp = 3;
  1187. break;
  1188. case FEC_5_6:
  1189. ch.code_rate_lp = 5;
  1190. break;
  1191. case FEC_7_8:
  1192. ch.code_rate_lp = 7;
  1193. break;
  1194. default:
  1195. case FEC_AUTO:
  1196. ch.code_rate_lp = -1;
  1197. break;
  1198. }
  1199. ch.select_hp = 1;
  1200. ch.intlv_native = 1;
  1201. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_UNION, (u8 *) &ch);
  1202. return 0;
  1203. }
  1204. static int dib9000_fw_tune(struct dvb_frontend *fe, struct dvb_frontend_parameters *ch)
  1205. {
  1206. struct dib9000_state *state = fe->demodulator_priv;
  1207. int ret = 10, search = state->channel_status.status == CHANNEL_STATUS_PARAMETERS_UNKNOWN;
  1208. s8 i;
  1209. switch (state->tune_state) {
  1210. case CT_DEMOD_START:
  1211. dib9000_fw_set_channel_head(state, ch);
  1212. /* write the channel context - a channel is initialized to 0, so it is OK */
  1213. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_CONTEXT, (u8 *) fe_info);
  1214. dib9000_risc_mem_write(state, FE_MM_W_FE_INFO, (u8 *) fe_info);
  1215. if (search)
  1216. dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_SEARCH, NULL, 0);
  1217. else {
  1218. dib9000_fw_set_channel_union(fe, ch);
  1219. dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_TUNE, NULL, 0);
  1220. }
  1221. state->tune_state = CT_DEMOD_STEP_1;
  1222. break;
  1223. case CT_DEMOD_STEP_1:
  1224. if (search)
  1225. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_SEARCH_STATE, (u8 *) &i, 1);
  1226. else
  1227. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_TUNE_STATE, (u8 *) &i, 1);
  1228. switch (i) { /* something happened */
  1229. case 0:
  1230. break;
  1231. case -2: /* tps locks are "slower" than MPEG locks -> even in autosearch data is OK here */
  1232. if (search)
  1233. state->status = FE_STATUS_DEMOD_SUCCESS;
  1234. else {
  1235. state->tune_state = CT_DEMOD_STOP;
  1236. state->status = FE_STATUS_LOCKED;
  1237. }
  1238. break;
  1239. default:
  1240. state->status = FE_STATUS_TUNE_FAILED;
  1241. state->tune_state = CT_DEMOD_STOP;
  1242. break;
  1243. }
  1244. break;
  1245. default:
  1246. ret = FE_CALLBACK_TIME_NEVER;
  1247. break;
  1248. }
  1249. return ret;
  1250. }
  1251. static int dib9000_fw_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1252. {
  1253. struct dib9000_state *state = fe->demodulator_priv;
  1254. u16 mode = (u16) onoff;
  1255. return dib9000_mbx_send(state, OUT_MSG_ENABLE_DIVERSITY, &mode, 1);
  1256. }
  1257. static int dib9000_fw_set_output_mode(struct dvb_frontend *fe, int mode)
  1258. {
  1259. struct dib9000_state *state = fe->demodulator_priv;
  1260. u16 outreg, smo_mode;
  1261. dprintk("setting output mode for demod %p to %d", fe, mode);
  1262. switch (mode) {
  1263. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1264. outreg = (1 << 10); /* 0x0400 */
  1265. break;
  1266. case OUTMODE_MPEG2_PAR_CONT_CLK:
  1267. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  1268. break;
  1269. case OUTMODE_MPEG2_SERIAL:
  1270. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
  1271. break;
  1272. case OUTMODE_DIVERSITY:
  1273. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  1274. break;
  1275. case OUTMODE_MPEG2_FIFO:
  1276. outreg = (1 << 10) | (5 << 6);
  1277. break;
  1278. case OUTMODE_HIGH_Z:
  1279. outreg = 0;
  1280. break;
  1281. default:
  1282. dprintk("Unhandled output_mode passed to be set for demod %p", &state->fe[0]);
  1283. return -EINVAL;
  1284. }
  1285. dib9000_write_word(state, 1795, outreg);
  1286. switch (mode) {
  1287. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1288. case OUTMODE_MPEG2_PAR_CONT_CLK:
  1289. case OUTMODE_MPEG2_SERIAL:
  1290. case OUTMODE_MPEG2_FIFO:
  1291. smo_mode = (dib9000_read_word(state, 295) & 0x0010) | (1 << 1);
  1292. if (state->chip.d9.cfg.output_mpeg2_in_188_bytes)
  1293. smo_mode |= (1 << 5);
  1294. dib9000_write_word(state, 295, smo_mode);
  1295. break;
  1296. }
  1297. outreg = to_fw_output_mode(mode);
  1298. return dib9000_mbx_send(state, OUT_MSG_SET_OUTPUT_MODE, &outreg, 1);
  1299. }
  1300. static int dib9000_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1301. {
  1302. struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
  1303. u16 i, len, t, index_msg;
  1304. for (index_msg = 0; index_msg < num; index_msg++) {
  1305. if (msg[index_msg].flags & I2C_M_RD) { /* read */
  1306. len = msg[index_msg].len;
  1307. if (len > 16)
  1308. len = 16;
  1309. if (dib9000_read_word(state, 790) != 0)
  1310. dprintk("TunerITF: read busy");
  1311. dib9000_write_word(state, 784, (u16) (msg[index_msg].addr));
  1312. dib9000_write_word(state, 787, (len / 2) - 1);
  1313. dib9000_write_word(state, 786, 1); /* start read */
  1314. i = 1000;
  1315. while (dib9000_read_word(state, 790) != (len / 2) && i)
  1316. i--;
  1317. if (i == 0)
  1318. dprintk("TunerITF: read failed");
  1319. for (i = 0; i < len; i += 2) {
  1320. t = dib9000_read_word(state, 785);
  1321. msg[index_msg].buf[i] = (t >> 8) & 0xff;
  1322. msg[index_msg].buf[i + 1] = (t) & 0xff;
  1323. }
  1324. if (dib9000_read_word(state, 790) != 0)
  1325. dprintk("TunerITF: read more data than expected");
  1326. } else {
  1327. i = 1000;
  1328. while (dib9000_read_word(state, 789) && i)
  1329. i--;
  1330. if (i == 0)
  1331. dprintk("TunerITF: write busy");
  1332. len = msg[index_msg].len;
  1333. if (len > 16)
  1334. len = 16;
  1335. for (i = 0; i < len; i += 2)
  1336. dib9000_write_word(state, 785, (msg[index_msg].buf[i] << 8) | msg[index_msg].buf[i + 1]);
  1337. dib9000_write_word(state, 784, (u16) msg[index_msg].addr);
  1338. dib9000_write_word(state, 787, (len / 2) - 1);
  1339. dib9000_write_word(state, 786, 0); /* start write */
  1340. i = 1000;
  1341. while (dib9000_read_word(state, 791) > 0 && i)
  1342. i--;
  1343. if (i == 0)
  1344. dprintk("TunerITF: write failed");
  1345. }
  1346. }
  1347. return num;
  1348. }
  1349. int dib9000_fw_set_component_bus_speed(struct dvb_frontend *fe, u16 speed)
  1350. {
  1351. struct dib9000_state *state = fe->demodulator_priv;
  1352. state->component_bus_speed = speed;
  1353. return 0;
  1354. }
  1355. EXPORT_SYMBOL(dib9000_fw_set_component_bus_speed);
  1356. static int dib9000_fw_component_bus_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1357. {
  1358. struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
  1359. u8 type = 0; /* I2C */
  1360. u8 port = DIBX000_I2C_INTERFACE_GPIO_3_4;
  1361. u16 scl = state->component_bus_speed; /* SCL frequency */
  1362. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[FE_MM_RW_COMPONENT_ACCESS_BUFFER];
  1363. u8 p[13] = { 0 };
  1364. p[0] = type;
  1365. p[1] = port;
  1366. p[2] = msg[0].addr << 1;
  1367. p[3] = (u8) scl & 0xff; /* scl */
  1368. p[4] = (u8) (scl >> 8);
  1369. p[7] = 0;
  1370. p[8] = 0;
  1371. p[9] = (u8) (msg[0].len);
  1372. p[10] = (u8) (msg[0].len >> 8);
  1373. if ((num > 1) && (msg[1].flags & I2C_M_RD)) {
  1374. p[11] = (u8) (msg[1].len);
  1375. p[12] = (u8) (msg[1].len >> 8);
  1376. } else {
  1377. p[11] = 0;
  1378. p[12] = 0;
  1379. }
  1380. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1381. dib9000_risc_mem_write(state, FE_MM_W_COMPONENT_ACCESS, p);
  1382. { /* write-part */
  1383. dib9000_risc_mem_setup_cmd(state, m->addr, msg[0].len, 0);
  1384. dib9000_risc_mem_write_chunks(state, msg[0].buf, msg[0].len);
  1385. }
  1386. /* do the transaction */
  1387. if (dib9000_fw_memmbx_sync(state, FE_SYNC_COMPONENT_ACCESS) < 0) {
  1388. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1389. return 0;
  1390. }
  1391. /* read back any possible result */
  1392. if ((num > 1) && (msg[1].flags & I2C_M_RD))
  1393. dib9000_risc_mem_read(state, FE_MM_RW_COMPONENT_ACCESS_BUFFER, msg[1].buf, msg[1].len);
  1394. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1395. return num;
  1396. }
  1397. static u32 dib9000_i2c_func(struct i2c_adapter *adapter)
  1398. {
  1399. return I2C_FUNC_I2C;
  1400. }
  1401. static struct i2c_algorithm dib9000_tuner_algo = {
  1402. .master_xfer = dib9000_tuner_xfer,
  1403. .functionality = dib9000_i2c_func,
  1404. };
  1405. static struct i2c_algorithm dib9000_component_bus_algo = {
  1406. .master_xfer = dib9000_fw_component_bus_xfer,
  1407. .functionality = dib9000_i2c_func,
  1408. };
  1409. struct i2c_adapter *dib9000_get_tuner_interface(struct dvb_frontend *fe)
  1410. {
  1411. struct dib9000_state *st = fe->demodulator_priv;
  1412. return &st->tuner_adap;
  1413. }
  1414. EXPORT_SYMBOL(dib9000_get_tuner_interface);
  1415. struct i2c_adapter *dib9000_get_component_bus_interface(struct dvb_frontend *fe)
  1416. {
  1417. struct dib9000_state *st = fe->demodulator_priv;
  1418. return &st->component_bus;
  1419. }
  1420. EXPORT_SYMBOL(dib9000_get_component_bus_interface);
  1421. struct i2c_adapter *dib9000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
  1422. {
  1423. struct dib9000_state *st = fe->demodulator_priv;
  1424. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1425. }
  1426. EXPORT_SYMBOL(dib9000_get_i2c_master);
  1427. int dib9000_set_i2c_adapter(struct dvb_frontend *fe, struct i2c_adapter *i2c)
  1428. {
  1429. struct dib9000_state *st = fe->demodulator_priv;
  1430. st->i2c.i2c_adap = i2c;
  1431. return 0;
  1432. }
  1433. EXPORT_SYMBOL(dib9000_set_i2c_adapter);
  1434. static int dib9000_cfg_gpio(struct dib9000_state *st, u8 num, u8 dir, u8 val)
  1435. {
  1436. st->gpio_dir = dib9000_read_word(st, 773);
  1437. st->gpio_dir &= ~(1 << num); /* reset the direction bit */
  1438. st->gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  1439. dib9000_write_word(st, 773, st->gpio_dir);
  1440. st->gpio_val = dib9000_read_word(st, 774);
  1441. st->gpio_val &= ~(1 << num); /* reset the direction bit */
  1442. st->gpio_val |= (val & 0x01) << num; /* set the new value */
  1443. dib9000_write_word(st, 774, st->gpio_val);
  1444. dprintk("gpio dir: %04x: gpio val: %04x", st->gpio_dir, st->gpio_val);
  1445. return 0;
  1446. }
  1447. int dib9000_set_gpio(struct dvb_frontend *fe, u8 num, u8 dir, u8 val)
  1448. {
  1449. struct dib9000_state *state = fe->demodulator_priv;
  1450. return dib9000_cfg_gpio(state, num, dir, val);
  1451. }
  1452. EXPORT_SYMBOL(dib9000_set_gpio);
  1453. int dib9000_fw_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1454. {
  1455. struct dib9000_state *state = fe->demodulator_priv;
  1456. u16 val = dib9000_read_word(state, 294 + 1) & 0xffef;
  1457. val |= (onoff & 0x1) << 4;
  1458. dprintk("PID filter enabled %d", onoff);
  1459. return dib9000_write_word(state, 294 + 1, val);
  1460. }
  1461. EXPORT_SYMBOL(dib9000_fw_pid_filter_ctrl);
  1462. int dib9000_fw_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1463. {
  1464. struct dib9000_state *state = fe->demodulator_priv;
  1465. dprintk("Index %x, PID %d, OnOff %d", id, pid, onoff);
  1466. return dib9000_write_word(state, 300 + 1 + id, onoff ? (1 << 13) | pid : 0);
  1467. }
  1468. EXPORT_SYMBOL(dib9000_fw_pid_filter);
  1469. int dib9000_firmware_post_pll_init(struct dvb_frontend *fe)
  1470. {
  1471. struct dib9000_state *state = fe->demodulator_priv;
  1472. return dib9000_fw_init(state);
  1473. }
  1474. EXPORT_SYMBOL(dib9000_firmware_post_pll_init);
  1475. static void dib9000_release(struct dvb_frontend *demod)
  1476. {
  1477. struct dib9000_state *st = demod->demodulator_priv;
  1478. u8 index_frontend;
  1479. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (st->fe[index_frontend] != NULL); index_frontend++)
  1480. dvb_frontend_detach(st->fe[index_frontend]);
  1481. DibFreeLock(&state->platform.risc.mbx_if_lock);
  1482. DibFreeLock(&state->platform.risc.mbx_lock);
  1483. DibFreeLock(&state->platform.risc.mem_lock);
  1484. DibFreeLock(&state->platform.risc.mem_mbx_lock);
  1485. dibx000_exit_i2c_master(&st->i2c_master);
  1486. i2c_del_adapter(&st->tuner_adap);
  1487. i2c_del_adapter(&st->component_bus);
  1488. kfree(st->fe[0]);
  1489. kfree(st);
  1490. }
  1491. static int dib9000_wakeup(struct dvb_frontend *fe)
  1492. {
  1493. return 0;
  1494. }
  1495. static int dib9000_sleep(struct dvb_frontend *fe)
  1496. {
  1497. struct dib9000_state *state = fe->demodulator_priv;
  1498. u8 index_frontend;
  1499. int ret;
  1500. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1501. ret = state->fe[index_frontend]->ops.sleep(state->fe[index_frontend]);
  1502. if (ret < 0)
  1503. return ret;
  1504. }
  1505. return dib9000_mbx_send(state, OUT_MSG_FE_SLEEP, NULL, 0);
  1506. }
  1507. static int dib9000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  1508. {
  1509. tune->min_delay_ms = 1000;
  1510. return 0;
  1511. }
  1512. static int dib9000_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1513. {
  1514. struct dib9000_state *state = fe->demodulator_priv;
  1515. u8 index_frontend, sub_index_frontend;
  1516. fe_status_t stat;
  1517. int ret;
  1518. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1519. state->fe[index_frontend]->ops.read_status(state->fe[index_frontend], &stat);
  1520. if (stat & FE_HAS_SYNC) {
  1521. dprintk("TPS lock on the slave%i", index_frontend);
  1522. /* synchronize the cache with the other frontends */
  1523. state->fe[index_frontend]->ops.get_frontend(state->fe[index_frontend], fep);
  1524. for (sub_index_frontend = 0; (sub_index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[sub_index_frontend] != NULL);
  1525. sub_index_frontend++) {
  1526. if (sub_index_frontend != index_frontend) {
  1527. state->fe[sub_index_frontend]->dtv_property_cache.modulation =
  1528. state->fe[index_frontend]->dtv_property_cache.modulation;
  1529. state->fe[sub_index_frontend]->dtv_property_cache.inversion =
  1530. state->fe[index_frontend]->dtv_property_cache.inversion;
  1531. state->fe[sub_index_frontend]->dtv_property_cache.transmission_mode =
  1532. state->fe[index_frontend]->dtv_property_cache.transmission_mode;
  1533. state->fe[sub_index_frontend]->dtv_property_cache.guard_interval =
  1534. state->fe[index_frontend]->dtv_property_cache.guard_interval;
  1535. state->fe[sub_index_frontend]->dtv_property_cache.hierarchy =
  1536. state->fe[index_frontend]->dtv_property_cache.hierarchy;
  1537. state->fe[sub_index_frontend]->dtv_property_cache.code_rate_HP =
  1538. state->fe[index_frontend]->dtv_property_cache.code_rate_HP;
  1539. state->fe[sub_index_frontend]->dtv_property_cache.code_rate_LP =
  1540. state->fe[index_frontend]->dtv_property_cache.code_rate_LP;
  1541. state->fe[sub_index_frontend]->dtv_property_cache.rolloff =
  1542. state->fe[index_frontend]->dtv_property_cache.rolloff;
  1543. }
  1544. }
  1545. return 0;
  1546. }
  1547. }
  1548. /* get the channel from master chip */
  1549. ret = dib9000_fw_get_channel(fe, fep);
  1550. if (ret != 0)
  1551. return ret;
  1552. /* synchronize the cache with the other frontends */
  1553. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1554. state->fe[index_frontend]->dtv_property_cache.inversion = fe->dtv_property_cache.inversion;
  1555. state->fe[index_frontend]->dtv_property_cache.transmission_mode = fe->dtv_property_cache.transmission_mode;
  1556. state->fe[index_frontend]->dtv_property_cache.guard_interval = fe->dtv_property_cache.guard_interval;
  1557. state->fe[index_frontend]->dtv_property_cache.modulation = fe->dtv_property_cache.modulation;
  1558. state->fe[index_frontend]->dtv_property_cache.hierarchy = fe->dtv_property_cache.hierarchy;
  1559. state->fe[index_frontend]->dtv_property_cache.code_rate_HP = fe->dtv_property_cache.code_rate_HP;
  1560. state->fe[index_frontend]->dtv_property_cache.code_rate_LP = fe->dtv_property_cache.code_rate_LP;
  1561. state->fe[index_frontend]->dtv_property_cache.rolloff = fe->dtv_property_cache.rolloff;
  1562. }
  1563. return 0;
  1564. }
  1565. static int dib9000_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  1566. {
  1567. struct dib9000_state *state = fe->demodulator_priv;
  1568. state->tune_state = tune_state;
  1569. if (tune_state == CT_DEMOD_START)
  1570. state->status = FE_STATUS_TUNE_PENDING;
  1571. return 0;
  1572. }
  1573. static u32 dib9000_get_status(struct dvb_frontend *fe)
  1574. {
  1575. struct dib9000_state *state = fe->demodulator_priv;
  1576. return state->status;
  1577. }
  1578. static int dib9000_set_channel_status(struct dvb_frontend *fe, struct dvb_frontend_parametersContext *channel_status)
  1579. {
  1580. struct dib9000_state *state = fe->demodulator_priv;
  1581. memcpy(&state->channel_status, channel_status, sizeof(struct dvb_frontend_parametersContext));
  1582. return 0;
  1583. }
  1584. static int dib9000_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1585. {
  1586. struct dib9000_state *state = fe->demodulator_priv;
  1587. int sleep_time, sleep_time_slave;
  1588. u32 frontend_status;
  1589. u8 nbr_pending, exit_condition, index_frontend, index_frontend_success;
  1590. struct dvb_frontend_parametersContext channel_status;
  1591. /* check that the correct parameters are set */
  1592. if (state->fe[0]->dtv_property_cache.frequency == 0) {
  1593. dprintk("dib9000: must specify frequency ");
  1594. return 0;
  1595. }
  1596. if (state->fe[0]->dtv_property_cache.bandwidth_hz == 0) {
  1597. dprintk("dib9000: must specify bandwidth ");
  1598. return 0;
  1599. }
  1600. fe->dtv_property_cache.delivery_system = SYS_DVBT;
  1601. /* set the master status */
  1602. if (fep->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO ||
  1603. fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO || fep->u.ofdm.constellation == QAM_AUTO || fep->u.ofdm.code_rate_HP == FEC_AUTO) {
  1604. /* no channel specified, autosearch the channel */
  1605. state->channel_status.status = CHANNEL_STATUS_PARAMETERS_UNKNOWN;
  1606. } else
  1607. state->channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;
  1608. /* set mode and status for the different frontends */
  1609. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1610. dib9000_fw_set_diversity_in(state->fe[index_frontend], 1);
  1611. /* synchronization of the cache */
  1612. memcpy(&state->fe[index_frontend]->dtv_property_cache, &fe->dtv_property_cache, sizeof(struct dtv_frontend_properties));
  1613. state->fe[index_frontend]->dtv_property_cache.delivery_system = SYS_DVBT;
  1614. dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_HIGH_Z);
  1615. dib9000_set_channel_status(state->fe[index_frontend], &state->channel_status);
  1616. dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  1617. }
  1618. /* actual tune */
  1619. exit_condition = 0; /* 0: tune pending; 1: tune failed; 2:tune success */
  1620. index_frontend_success = 0;
  1621. do {
  1622. sleep_time = dib9000_fw_tune(state->fe[0], NULL);
  1623. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1624. sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend], NULL);
  1625. if (sleep_time == FE_CALLBACK_TIME_NEVER)
  1626. sleep_time = sleep_time_slave;
  1627. else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
  1628. sleep_time = sleep_time_slave;
  1629. }
  1630. if (sleep_time != FE_CALLBACK_TIME_NEVER)
  1631. msleep(sleep_time / 10);
  1632. else
  1633. break;
  1634. nbr_pending = 0;
  1635. exit_condition = 0;
  1636. index_frontend_success = 0;
  1637. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1638. frontend_status = -dib9000_get_status(state->fe[index_frontend]);
  1639. if (frontend_status > -FE_STATUS_TUNE_PENDING) {
  1640. exit_condition = 2; /* tune success */
  1641. index_frontend_success = index_frontend;
  1642. break;
  1643. }
  1644. if (frontend_status == -FE_STATUS_TUNE_PENDING)
  1645. nbr_pending++; /* some frontends are still tuning */
  1646. }
  1647. if ((exit_condition != 2) && (nbr_pending == 0))
  1648. exit_condition = 1; /* if all tune are done and no success, exit: tune failed */
  1649. } while (exit_condition == 0);
  1650. /* check the tune result */
  1651. if (exit_condition == 1) { /* tune failed */
  1652. dprintk("tune failed");
  1653. return 0;
  1654. }
  1655. dprintk("tune success on frontend%i", index_frontend_success);
  1656. /* synchronize all the channel cache */
  1657. dib9000_get_frontend(state->fe[0], fep);
  1658. /* retune the other frontends with the found channel */
  1659. channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;
  1660. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1661. /* only retune the frontends which was not tuned success */
  1662. if (index_frontend != index_frontend_success) {
  1663. dib9000_set_channel_status(state->fe[index_frontend], &channel_status);
  1664. dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  1665. }
  1666. }
  1667. do {
  1668. sleep_time = FE_CALLBACK_TIME_NEVER;
  1669. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1670. if (index_frontend != index_frontend_success) {
  1671. sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend], NULL);
  1672. if (sleep_time == FE_CALLBACK_TIME_NEVER)
  1673. sleep_time = sleep_time_slave;
  1674. else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
  1675. sleep_time = sleep_time_slave;
  1676. }
  1677. }
  1678. if (sleep_time != FE_CALLBACK_TIME_NEVER)
  1679. msleep(sleep_time / 10);
  1680. else
  1681. break;
  1682. nbr_pending = 0;
  1683. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1684. if (index_frontend != index_frontend_success) {
  1685. frontend_status = -dib9000_get_status(state->fe[index_frontend]);
  1686. if ((index_frontend != index_frontend_success) && (frontend_status == -FE_STATUS_TUNE_PENDING))
  1687. nbr_pending++; /* some frontends are still tuning */
  1688. }
  1689. }
  1690. } while (nbr_pending != 0);
  1691. /* set the output mode */
  1692. dib9000_fw_set_output_mode(state->fe[0], state->chip.d9.cfg.output_mode);
  1693. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1694. dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_DIVERSITY);
  1695. /* turn off the diversity for the last frontend */
  1696. dib9000_fw_set_diversity_in(state->fe[index_frontend - 1], 0);
  1697. return 0;
  1698. }
  1699. static u16 dib9000_read_lock(struct dvb_frontend *fe)
  1700. {
  1701. struct dib9000_state *state = fe->demodulator_priv;
  1702. return dib9000_read_word(state, 535);
  1703. }
  1704. static int dib9000_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  1705. {
  1706. struct dib9000_state *state = fe->demodulator_priv;
  1707. u8 index_frontend;
  1708. u16 lock = 0, lock_slave = 0;
  1709. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1710. lock_slave |= dib9000_read_lock(state->fe[index_frontend]);
  1711. lock = dib9000_read_word(state, 535);
  1712. *stat = 0;
  1713. if ((lock & 0x8000) || (lock_slave & 0x8000))
  1714. *stat |= FE_HAS_SIGNAL;
  1715. if ((lock & 0x3000) || (lock_slave & 0x3000))
  1716. *stat |= FE_HAS_CARRIER;
  1717. if ((lock & 0x0100) || (lock_slave & 0x0100))
  1718. *stat |= FE_HAS_VITERBI;
  1719. if (((lock & 0x0038) == 0x38) || ((lock_slave & 0x0038) == 0x38))
  1720. *stat |= FE_HAS_SYNC;
  1721. if ((lock & 0x0008) || (lock_slave & 0x0008))
  1722. *stat |= FE_HAS_LOCK;
  1723. return 0;
  1724. }
  1725. static int dib9000_read_ber(struct dvb_frontend *fe, u32 * ber)
  1726. {
  1727. struct dib9000_state *state = fe->demodulator_priv;
  1728. u16 c[16];
  1729. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1730. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1731. return -EIO;
  1732. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, sizeof(c));
  1733. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1734. *ber = c[10] << 16 | c[11];
  1735. return 0;
  1736. }
  1737. static int dib9000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1738. {
  1739. struct dib9000_state *state = fe->demodulator_priv;
  1740. u8 index_frontend;
  1741. u16 c[16];
  1742. u16 val;
  1743. *strength = 0;
  1744. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1745. state->fe[index_frontend]->ops.read_signal_strength(state->fe[index_frontend], &val);
  1746. if (val > 65535 - *strength)
  1747. *strength = 65535;
  1748. else
  1749. *strength += val;
  1750. }
  1751. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1752. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1753. return -EIO;
  1754. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, sizeof(c));
  1755. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1756. val = 65535 - c[4];
  1757. if (val > 65535 - *strength)
  1758. *strength = 65535;
  1759. else
  1760. *strength += val;
  1761. return 0;
  1762. }
  1763. static u32 dib9000_get_snr(struct dvb_frontend *fe)
  1764. {
  1765. struct dib9000_state *state = fe->demodulator_priv;
  1766. u16 c[16];
  1767. u32 n, s, exp;
  1768. u16 val;
  1769. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1770. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1771. return -EIO;
  1772. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, sizeof(c));
  1773. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1774. val = c[7];
  1775. n = (val >> 4) & 0xff;
  1776. exp = ((val & 0xf) << 2);
  1777. val = c[8];
  1778. exp += ((val >> 14) & 0x3);
  1779. if ((exp & 0x20) != 0)
  1780. exp -= 0x40;
  1781. n <<= exp + 16;
  1782. s = (val >> 6) & 0xFF;
  1783. exp = (val & 0x3F);
  1784. if ((exp & 0x20) != 0)
  1785. exp -= 0x40;
  1786. s <<= exp + 16;
  1787. if (n > 0) {
  1788. u32 t = (s / n) << 16;
  1789. return t + ((s << 16) - n * t) / n;
  1790. }
  1791. return 0xffffffff;
  1792. }
  1793. static int dib9000_read_snr(struct dvb_frontend *fe, u16 * snr)
  1794. {
  1795. struct dib9000_state *state = fe->demodulator_priv;
  1796. u8 index_frontend;
  1797. u32 snr_master;
  1798. snr_master = dib9000_get_snr(fe);
  1799. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1800. snr_master += dib9000_get_snr(state->fe[index_frontend]);
  1801. if ((snr_master >> 16) != 0) {
  1802. snr_master = 10 * intlog10(snr_master >> 16);
  1803. *snr = snr_master / ((1 << 24) / 10);
  1804. } else
  1805. *snr = 0;
  1806. return 0;
  1807. }
  1808. static int dib9000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1809. {
  1810. struct dib9000_state *state = fe->demodulator_priv;
  1811. u16 c[16];
  1812. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1813. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1814. return -EIO;
  1815. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, sizeof(c));
  1816. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1817. *unc = c[12];
  1818. return 0;
  1819. }
  1820. int dib9000_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, u8 first_addr)
  1821. {
  1822. int k = 0;
  1823. u8 new_addr = 0;
  1824. struct i2c_device client = {.i2c_adap = i2c };
  1825. client.i2c_addr = default_addr + 16;
  1826. dib9000_i2c_write16(&client, 1796, 0x0);
  1827. for (k = no_of_demods - 1; k >= 0; k--) {
  1828. /* designated i2c address */
  1829. new_addr = first_addr + (k << 1);
  1830. client.i2c_addr = default_addr;
  1831. dib9000_i2c_write16(&client, 1817, 3);
  1832. dib9000_i2c_write16(&client, 1796, 0);
  1833. dib9000_i2c_write16(&client, 1227, 1);
  1834. dib9000_i2c_write16(&client, 1227, 0);
  1835. client.i2c_addr = new_addr;
  1836. dib9000_i2c_write16(&client, 1817, 3);
  1837. dib9000_i2c_write16(&client, 1796, 0);
  1838. dib9000_i2c_write16(&client, 1227, 1);
  1839. dib9000_i2c_write16(&client, 1227, 0);
  1840. if (dib9000_identify(&client) == 0) {
  1841. client.i2c_addr = default_addr;
  1842. if (dib9000_identify(&client) == 0) {
  1843. dprintk("DiB9000 #%d: not identified", k);
  1844. return -EIO;
  1845. }
  1846. }
  1847. dib9000_i2c_write16(&client, 1795, (1 << 10) | (4 << 6));
  1848. dib9000_i2c_write16(&client, 1794, (new_addr << 2) | 2);
  1849. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1850. }
  1851. for (k = 0; k < no_of_demods; k++) {
  1852. new_addr = first_addr | (k << 1);
  1853. client.i2c_addr = new_addr;
  1854. dib9000_i2c_write16(&client, 1794, (new_addr << 2));
  1855. dib9000_i2c_write16(&client, 1795, 0);
  1856. }
  1857. return 0;
  1858. }
  1859. EXPORT_SYMBOL(dib9000_i2c_enumeration);
  1860. int dib9000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
  1861. {
  1862. struct dib9000_state *state = fe->demodulator_priv;
  1863. u8 index_frontend = 1;
  1864. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  1865. index_frontend++;
  1866. if (index_frontend < MAX_NUMBER_OF_FRONTENDS) {
  1867. dprintk("set slave fe %p to index %i", fe_slave, index_frontend);
  1868. state->fe[index_frontend] = fe_slave;
  1869. return 0;
  1870. }
  1871. dprintk("too many slave frontend");
  1872. return -ENOMEM;
  1873. }
  1874. EXPORT_SYMBOL(dib9000_set_slave_frontend);
  1875. int dib9000_remove_slave_frontend(struct dvb_frontend *fe)
  1876. {
  1877. struct dib9000_state *state = fe->demodulator_priv;
  1878. u8 index_frontend = 1;
  1879. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  1880. index_frontend++;
  1881. if (index_frontend != 1) {
  1882. dprintk("remove slave fe %p (index %i)", state->fe[index_frontend - 1], index_frontend - 1);
  1883. state->fe[index_frontend] = NULL;
  1884. return 0;
  1885. }
  1886. dprintk("no frontend to be removed");
  1887. return -ENODEV;
  1888. }
  1889. EXPORT_SYMBOL(dib9000_remove_slave_frontend);
  1890. struct dvb_frontend *dib9000_get_slave_frontend(struct dvb_frontend *fe, int slave_index)
  1891. {
  1892. struct dib9000_state *state = fe->demodulator_priv;
  1893. if (slave_index >= MAX_NUMBER_OF_FRONTENDS)
  1894. return NULL;
  1895. return state->fe[slave_index];
  1896. }
  1897. EXPORT_SYMBOL(dib9000_get_slave_frontend);
  1898. static struct dvb_frontend_ops dib9000_ops;
  1899. struct dvb_frontend *dib9000_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, const struct dib9000_config *cfg)
  1900. {
  1901. struct dvb_frontend *fe;
  1902. struct dib9000_state *st;
  1903. st = kzalloc(sizeof(struct dib9000_state), GFP_KERNEL);
  1904. if (st == NULL)
  1905. return NULL;
  1906. fe = kzalloc(sizeof(struct dvb_frontend), GFP_KERNEL);
  1907. if (fe == NULL)
  1908. return NULL;
  1909. memcpy(&st->chip.d9.cfg, cfg, sizeof(struct dib9000_config));
  1910. st->i2c.i2c_adap = i2c_adap;
  1911. st->i2c.i2c_addr = i2c_addr;
  1912. st->gpio_dir = DIB9000_GPIO_DEFAULT_DIRECTIONS;
  1913. st->gpio_val = DIB9000_GPIO_DEFAULT_VALUES;
  1914. st->gpio_pwm_pos = DIB9000_GPIO_DEFAULT_PWM_POS;
  1915. DibInitLock(&st->platform.risc.mbx_if_lock);
  1916. DibInitLock(&st->platform.risc.mbx_lock);
  1917. DibInitLock(&st->platform.risc.mem_lock);
  1918. DibInitLock(&st->platform.risc.mem_mbx_lock);
  1919. st->fe[0] = fe;
  1920. fe->demodulator_priv = st;
  1921. memcpy(&st->fe[0]->ops, &dib9000_ops, sizeof(struct dvb_frontend_ops));
  1922. /* Ensure the output mode remains at the previous default if it's
  1923. * not specifically set by the caller.
  1924. */
  1925. if ((st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  1926. st->chip.d9.cfg.output_mode = OUTMODE_MPEG2_FIFO;
  1927. if (dib9000_identify(&st->i2c) == 0)
  1928. goto error;
  1929. dibx000_init_i2c_master(&st->i2c_master, DIB7000MC, st->i2c.i2c_adap, st->i2c.i2c_addr);
  1930. st->tuner_adap.dev.parent = i2c_adap->dev.parent;
  1931. strncpy(st->tuner_adap.name, "DIB9000_FW TUNER ACCESS", sizeof(st->tuner_adap.name));
  1932. st->tuner_adap.algo = &dib9000_tuner_algo;
  1933. st->tuner_adap.algo_data = NULL;
  1934. i2c_set_adapdata(&st->tuner_adap, st);
  1935. if (i2c_add_adapter(&st->tuner_adap) < 0)
  1936. goto error;
  1937. st->component_bus.dev.parent = i2c_adap->dev.parent;
  1938. strncpy(st->component_bus.name, "DIB9000_FW COMPONENT BUS ACCESS", sizeof(st->component_bus.name));
  1939. st->component_bus.algo = &dib9000_component_bus_algo;
  1940. st->component_bus.algo_data = NULL;
  1941. st->component_bus_speed = 340;
  1942. i2c_set_adapdata(&st->component_bus, st);
  1943. if (i2c_add_adapter(&st->component_bus) < 0)
  1944. goto component_bus_add_error;
  1945. dib9000_fw_reset(fe);
  1946. return fe;
  1947. component_bus_add_error:
  1948. i2c_del_adapter(&st->tuner_adap);
  1949. error:
  1950. kfree(st);
  1951. return NULL;
  1952. }
  1953. EXPORT_SYMBOL(dib9000_attach);
  1954. static struct dvb_frontend_ops dib9000_ops = {
  1955. .info = {
  1956. .name = "DiBcom 9000",
  1957. .type = FE_OFDM,
  1958. .frequency_min = 44250000,
  1959. .frequency_max = 867250000,
  1960. .frequency_stepsize = 62500,
  1961. .caps = FE_CAN_INVERSION_AUTO |
  1962. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1963. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1964. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1965. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  1966. },
  1967. .release = dib9000_release,
  1968. .init = dib9000_wakeup,
  1969. .sleep = dib9000_sleep,
  1970. .set_frontend = dib9000_set_frontend,
  1971. .get_tune_settings = dib9000_fe_get_tune_settings,
  1972. .get_frontend = dib9000_get_frontend,
  1973. .read_status = dib9000_read_status,
  1974. .read_ber = dib9000_read_ber,
  1975. .read_signal_strength = dib9000_read_signal_strength,
  1976. .read_snr = dib9000_read_snr,
  1977. .read_ucblocks = dib9000_read_unc_blocks,
  1978. };
  1979. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  1980. MODULE_AUTHOR("Olivier Grenie <ogrenie@dibcom.fr>");
  1981. MODULE_DESCRIPTION("Driver for the DiBcom 9000 COFDM demodulator");
  1982. MODULE_LICENSE("GPL");