dib9000.c 71 KB

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