dib9000.c 71 KB

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