dib9000.c 70 KB

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