2 * Linux-DVB Driver for DiBcom's DiB9000 and demodulator-family.
4 * Copyright (C) 2005-10 DiBcom (http://www.dibcom.fr/)
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.
10 #include <linux/kernel.h>
11 #include <linux/i2c.h>
12 #include <linux/mutex.h>
15 #include "dvb_frontend.h"
18 #include "dibx000_common.h"
21 module_param(debug
, int, 0644);
22 MODULE_PARM_DESC(debug
, "turn on debugging (default: 0)");
24 #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB9000: "); printk(args); printk("\n"); } } while (0)
25 #define MAX_NUMBER_OF_FRONTENDS 6
28 struct i2c_adapter
*i2c_adap
;
34 struct dib9000_pid_ctrl
{
35 #define DIB9000_PID_FILTER_CTRL 0
36 #define DIB9000_PID_FILTER 1
43 struct dib9000_state
{
44 struct i2c_device i2c
;
46 struct dibx000_i2c_master i2c_master
;
47 struct i2c_adapter tuner_adap
;
48 struct i2c_adapter component_bus
;
53 enum frontend_tune_state tune_state
;
55 struct dvb_frontend_parametersContext channel_status
;
59 #define DIB9000_GPIO_DEFAULT_DIRECTIONS 0xffff
61 #define DIB9000_GPIO_DEFAULT_VALUES 0x0000
63 #define DIB9000_GPIO_DEFAULT_PWM_POS 0xffff
66 union { /* common for all chips */
72 struct dib9000_fe_memory_map
{
78 struct mutex mbx_if_lock
; /* to protect read/write operations */
79 struct mutex mbx_lock
; /* to protect the whole mailbox handling */
81 struct mutex mem_lock
; /* to protect the memory accesses */
82 struct mutex mem_mbx_lock
; /* to protect the memory-based mailbox */
84 #define MBX_MAX_WORDS (256 - 200 - 2)
85 #define DIB9000_MSG_CACHE_SIZE 2
86 u16 message_cache
[DIB9000_MSG_CACHE_SIZE
][MBX_MAX_WORDS
];
91 union { /* common for all platforms */
93 struct dib9000_config cfg
;
97 struct dvb_frontend
*fe
[MAX_NUMBER_OF_FRONTENDS
];
98 u16 component_bus_speed
;
100 /* for the I2C transfer */
101 struct i2c_msg msg
[2];
102 u8 i2c_write_buffer
[255];
103 u8 i2c_read_buffer
[255];
104 struct mutex demod_lock
;
105 u8 get_frontend_internal
;
106 struct dib9000_pid_ctrl pid_ctrl
[10];
107 s8 pid_ctrl_index
; /* -1: empty list; -2: do not use the list */
110 static const u32 fe_info
[44] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112 0, 0, 0, 0, 0, 0, 0, 0
115 enum dib9000_power_mode
{
116 DIB9000_POWER_ALL
= 0,
119 DIB9000_POWER_INTERF_ANALOG_AGC
,
120 DIB9000_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD
,
121 DIB9000_POWER_COR4_CRY_ESRAM_MOUT_NUD
,
122 DIB9000_POWER_INTERFACE_ONLY
,
125 enum dib9000_out_messages
{
127 OUT_MSG_HOST_BUF_FAIL
,
129 OUT_MSG_BRIDGE_I2C_W
,
130 OUT_MSG_BRIDGE_I2C_R
,
131 OUT_MSG_BRIDGE_APB_W
,
132 OUT_MSG_BRIDGE_APB_R
,
133 OUT_MSG_SCAN_CHANNEL
,
138 OUT_MSG_ENABLE_TIME_SLICE
,
140 OUT_MSG_FE_CHANNEL_SEARCH
,
141 OUT_MSG_FE_CHANNEL_TUNE
,
149 OUT_MSG_ENABLE_DIVERSITY
,
150 OUT_MSG_SET_OUTPUT_MODE
,
151 OUT_MSG_SET_PRIORITARY_CHANNEL
,
156 enum dib9000_in_messages
{
160 IN_MSG_ACK_FREE_ITEM
,
163 IN_MSG_RAWTS_MONITOR
,
164 IN_MSG_END_BRIDGE_I2C_RW
,
165 IN_MSG_END_BRIDGE_APB_RW
,
170 IN_MSG_FE_FW_DL_DONE
,
172 IN_MSG_ACK_CHANGE_SVC
,
176 /* memory_access requests */
177 #define FE_MM_W_CHANNEL 0
178 #define FE_MM_W_FE_INFO 1
179 #define FE_MM_RW_SYNC 2
181 #define FE_SYNC_CHANNEL 1
182 #define FE_SYNC_W_GENERIC_MONIT 2
183 #define FE_SYNC_COMPONENT_ACCESS 3
185 #define FE_MM_R_CHANNEL_SEARCH_STATE 3
186 #define FE_MM_R_CHANNEL_UNION_CONTEXT 4
187 #define FE_MM_R_FE_INFO 5
188 #define FE_MM_R_FE_MONITOR 6
190 #define FE_MM_W_CHANNEL_HEAD 7
191 #define FE_MM_W_CHANNEL_UNION 8
192 #define FE_MM_W_CHANNEL_CONTEXT 9
193 #define FE_MM_R_CHANNEL_UNION 10
194 #define FE_MM_R_CHANNEL_CONTEXT 11
195 #define FE_MM_R_CHANNEL_TUNE_STATE 12
197 #define FE_MM_R_GENERIC_MONITORING_SIZE 13
198 #define FE_MM_W_GENERIC_MONITORING 14
199 #define FE_MM_R_GENERIC_MONITORING 15
201 #define FE_MM_W_COMPONENT_ACCESS 16
202 #define FE_MM_RW_COMPONENT_ACCESS_BUFFER 17
203 static int dib9000_risc_apb_access_read(struct dib9000_state
*state
, u32 address
, u16 attribute
, const u8
* tx
, u32 txlen
, u8
* b
, u32 len
);
204 static int dib9000_risc_apb_access_write(struct dib9000_state
*state
, u32 address
, u16 attribute
, const u8
* b
, u32 len
);
206 static u16
to_fw_output_mode(u16 mode
)
211 case OUTMODE_MPEG2_PAR_GATED_CLK
:
213 case OUTMODE_MPEG2_PAR_CONT_CLK
:
215 case OUTMODE_MPEG2_SERIAL
:
217 case OUTMODE_DIVERSITY
:
219 case OUTMODE_MPEG2_FIFO
:
221 case OUTMODE_ANALOG_ADC
:
228 static u16
dib9000_read16_attr(struct dib9000_state
*state
, u16 reg
, u8
* b
, u32 len
, u16 attribute
)
230 u32 chunk_size
= 126;
234 if (state
->platform
.risc
.fw_is_running
&& (reg
< 1024))
235 return dib9000_risc_apb_access_read(state
, reg
, attribute
, NULL
, 0, b
, len
);
237 memset(state
->msg
, 0, 2 * sizeof(struct i2c_msg
));
238 state
->msg
[0].addr
= state
->i2c
.i2c_addr
>> 1;
239 state
->msg
[0].flags
= 0;
240 state
->msg
[0].buf
= state
->i2c_write_buffer
;
241 state
->msg
[0].len
= 2;
242 state
->msg
[1].addr
= state
->i2c
.i2c_addr
>> 1;
243 state
->msg
[1].flags
= I2C_M_RD
;
244 state
->msg
[1].buf
= b
;
245 state
->msg
[1].len
= len
;
247 state
->i2c_write_buffer
[0] = reg
>> 8;
248 state
->i2c_write_buffer
[1] = reg
& 0xff;
250 if (attribute
& DATA_BUS_ACCESS_MODE_8BIT
)
251 state
->i2c_write_buffer
[0] |= (1 << 5);
252 if (attribute
& DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT
)
253 state
->i2c_write_buffer
[0] |= (1 << 4);
256 l
= len
< chunk_size
? len
: chunk_size
;
257 state
->msg
[1].len
= l
;
258 state
->msg
[1].buf
= b
;
259 ret
= i2c_transfer(state
->i2c
.i2c_adap
, state
->msg
, 2) != 2 ? -EREMOTEIO
: 0;
261 dprintk("i2c read error on %d", reg
);
268 if (!(attribute
& DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT
))
270 } while ((ret
== 0) && len
);
275 static u16
dib9000_i2c_read16(struct i2c_device
*i2c
, u16 reg
)
277 struct i2c_msg msg
[2] = {
278 {.addr
= i2c
->i2c_addr
>> 1, .flags
= 0,
279 .buf
= i2c
->i2c_write_buffer
, .len
= 2},
280 {.addr
= i2c
->i2c_addr
>> 1, .flags
= I2C_M_RD
,
281 .buf
= i2c
->i2c_read_buffer
, .len
= 2},
284 i2c
->i2c_write_buffer
[0] = reg
>> 8;
285 i2c
->i2c_write_buffer
[1] = reg
& 0xff;
287 if (i2c_transfer(i2c
->i2c_adap
, msg
, 2) != 2) {
288 dprintk("read register %x error", reg
);
292 return (i2c
->i2c_read_buffer
[0] << 8) | i2c
->i2c_read_buffer
[1];
295 static inline u16
dib9000_read_word(struct dib9000_state
*state
, u16 reg
)
297 if (dib9000_read16_attr(state
, reg
, state
->i2c_read_buffer
, 2, 0) != 0)
299 return (state
->i2c_read_buffer
[0] << 8) | state
->i2c_read_buffer
[1];
302 static inline u16
dib9000_read_word_attr(struct dib9000_state
*state
, u16 reg
, u16 attribute
)
304 if (dib9000_read16_attr(state
, reg
, state
->i2c_read_buffer
, 2,
307 return (state
->i2c_read_buffer
[0] << 8) | state
->i2c_read_buffer
[1];
310 #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)
312 static u16
dib9000_write16_attr(struct dib9000_state
*state
, u16 reg
, const u8
* buf
, u32 len
, u16 attribute
)
314 u32 chunk_size
= 126;
318 if (state
->platform
.risc
.fw_is_running
&& (reg
< 1024)) {
319 if (dib9000_risc_apb_access_write
320 (state
, reg
, DATA_BUS_ACCESS_MODE_16BIT
| DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT
| attribute
, buf
, len
) != 0)
325 memset(&state
->msg
[0], 0, sizeof(struct i2c_msg
));
326 state
->msg
[0].addr
= state
->i2c
.i2c_addr
>> 1;
327 state
->msg
[0].flags
= 0;
328 state
->msg
[0].buf
= state
->i2c_write_buffer
;
329 state
->msg
[0].len
= len
+ 2;
331 state
->i2c_write_buffer
[0] = (reg
>> 8) & 0xff;
332 state
->i2c_write_buffer
[1] = (reg
) & 0xff;
334 if (attribute
& DATA_BUS_ACCESS_MODE_8BIT
)
335 state
->i2c_write_buffer
[0] |= (1 << 5);
336 if (attribute
& DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT
)
337 state
->i2c_write_buffer
[0] |= (1 << 4);
340 l
= len
< chunk_size
? len
: chunk_size
;
341 state
->msg
[0].len
= l
+ 2;
342 memcpy(&state
->i2c_write_buffer
[2], buf
, l
);
344 ret
= i2c_transfer(state
->i2c
.i2c_adap
, state
->msg
, 1) != 1 ? -EREMOTEIO
: 0;
349 if (!(attribute
& DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT
))
351 } while ((ret
== 0) && len
);
356 static int dib9000_i2c_write16(struct i2c_device
*i2c
, u16 reg
, u16 val
)
358 struct i2c_msg msg
= {
359 .addr
= i2c
->i2c_addr
>> 1, .flags
= 0,
360 .buf
= i2c
->i2c_write_buffer
, .len
= 4
363 i2c
->i2c_write_buffer
[0] = (reg
>> 8) & 0xff;
364 i2c
->i2c_write_buffer
[1] = reg
& 0xff;
365 i2c
->i2c_write_buffer
[2] = (val
>> 8) & 0xff;
366 i2c
->i2c_write_buffer
[3] = val
& 0xff;
368 return i2c_transfer(i2c
->i2c_adap
, &msg
, 1) != 1 ? -EREMOTEIO
: 0;
371 static inline int dib9000_write_word(struct dib9000_state
*state
, u16 reg
, u16 val
)
373 u8 b
[2] = { val
>> 8, val
& 0xff };
374 return dib9000_write16_attr(state
, reg
, b
, 2, 0);
377 static inline int dib9000_write_word_attr(struct dib9000_state
*state
, u16 reg
, u16 val
, u16 attribute
)
379 u8 b
[2] = { val
>> 8, val
& 0xff };
380 return dib9000_write16_attr(state
, reg
, b
, 2, attribute
);
383 #define dib9000_write(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, 0)
384 #define dib9000_write16_noinc(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
385 #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))
387 #define dib9000_mbx_send(state, id, data, len) dib9000_mbx_send_attr(state, id, data, len, 0)
388 #define dib9000_mbx_get_message(state, id, msg, len) dib9000_mbx_get_message_attr(state, id, msg, len, 0)
390 #define MAC_IRQ (1 << 1)
391 #define IRQ_POL_MSK (1 << 4)
393 #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)
394 #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)
396 static void dib9000_risc_mem_setup_cmd(struct dib9000_state
*state
, u32 addr
, u32 len
, u8 reading
)
400 /* dprintk("%d memcmd: %d %d %d\n", state->fe_id, addr, addr+len, len); */
406 b
[4] = (u8
) (addr
>> 8);
407 b
[5] = (u8
) (addr
& 0xff);
411 b
[12] = (u8
) (addr
>> 8);
412 b
[13] = (u8
) (addr
& 0xff);
417 b
[8] = (u8
) (addr
>> 8);
418 b
[9] = (u8
) (addr
& 0xff);
420 dib9000_write(state
, 1056, b
, 14);
422 dib9000_write_word(state
, 1056, (1 << 15) | 1);
423 state
->platform
.risc
.memcmd
= -1; /* if it was called directly reset it - to force a future setup-call to set it */
426 static void dib9000_risc_mem_setup(struct dib9000_state
*state
, u8 cmd
)
428 struct dib9000_fe_memory_map
*m
= &state
->platform
.risc
.fe_mm
[cmd
& 0x7f];
429 /* decide whether we need to "refresh" the memory controller */
430 if (state
->platform
.risc
.memcmd
== cmd
&& /* same command */
431 !(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 */
433 dib9000_risc_mem_setup_cmd(state
, m
->addr
, m
->size
, cmd
& 0x80);
434 state
->platform
.risc
.memcmd
= cmd
;
437 static int dib9000_risc_mem_read(struct dib9000_state
*state
, u8 cmd
, u8
* b
, u16 len
)
439 if (!state
->platform
.risc
.fw_is_running
)
442 if (mutex_lock_interruptible(&state
->platform
.risc
.mem_lock
) < 0) {
443 dprintk("could not get the lock");
446 dib9000_risc_mem_setup(state
, cmd
| 0x80);
447 dib9000_risc_mem_read_chunks(state
, b
, len
);
448 mutex_unlock(&state
->platform
.risc
.mem_lock
);
452 static int dib9000_risc_mem_write(struct dib9000_state
*state
, u8 cmd
, const u8
* b
)
454 struct dib9000_fe_memory_map
*m
= &state
->platform
.risc
.fe_mm
[cmd
];
455 if (!state
->platform
.risc
.fw_is_running
)
458 if (mutex_lock_interruptible(&state
->platform
.risc
.mem_lock
) < 0) {
459 dprintk("could not get the lock");
462 dib9000_risc_mem_setup(state
, cmd
);
463 dib9000_risc_mem_write_chunks(state
, b
, m
->size
);
464 mutex_unlock(&state
->platform
.risc
.mem_lock
);
468 static int dib9000_firmware_download(struct dib9000_state
*state
, u8 risc_id
, u16 key
, const u8
* code
, u32 len
)
477 /* config crtl reg */
478 dib9000_write_word(state
, 1024 + offs
, 0x000f);
479 dib9000_write_word(state
, 1025 + offs
, 0);
480 dib9000_write_word(state
, 1031 + offs
, key
);
482 dprintk("going to download %dB of microcode", len
);
483 if (dib9000_write16_noinc(state
, 1026 + offs
, (u8
*) code
, (u16
) len
) != 0) {
484 dprintk("error while downloading microcode for RISC %c", 'A' + risc_id
);
488 dprintk("Microcode for RISC %c loaded", 'A' + risc_id
);
493 static int dib9000_mbx_host_init(struct dib9000_state
*state
, u8 risc_id
)
505 dib9000_write_word(state
, 1027 + mbox_offs
, 0x8000);
507 /* Read reset status */
509 reset_reg
= dib9000_read_word(state
, 1027 + mbox_offs
);
511 } while ((reset_reg
& 0x8000) && --tries
);
513 if (reset_reg
& 0x8000) {
514 dprintk("MBX: init ERROR, no response from RISC %c", 'A' + risc_id
);
517 dprintk("MBX: initialized");
521 #define MAX_MAILBOX_TRY 100
522 static int dib9000_mbx_send_attr(struct dib9000_state
*state
, u8 id
, u16
* data
, u8 len
, u16 attr
)
530 if (!state
->platform
.risc
.fw_is_running
)
533 if (mutex_lock_interruptible(&state
->platform
.risc
.mbx_if_lock
) < 0) {
534 dprintk("could not get the lock");
537 tmp
= MAX_MAILBOX_TRY
;
539 size
= dib9000_read_word_attr(state
, 1043, attr
) & 0xff;
540 if ((size
+ len
+ 1) > MBX_MAX_WORDS
&& --tmp
) {
541 dprintk("MBX: RISC mbx full, retrying");
547 /*dprintk( "MBX: size: %d", size); */
554 dprintk("--> %02x %d ", id
, len
+ 1);
555 for (i
= 0; i
< len
; i
++)
556 dprintk("%04x ", data
[i
]);
560 /* byte-order conversion - works on big (where it is not necessary) or little endian */
562 for (i
= 0; i
< len
; i
++) {
571 if (dib9000_write16_noinc_attr(state
, 1045, b
, 2, attr
) != 0 || dib9000_write16_noinc_attr(state
, 1045, (u8
*) data
, len
* 2, attr
) != 0) {
576 /* update register nb_mes_in_RX */
577 ret
= (u8
) dib9000_write_word_attr(state
, 1043, 1 << 14, attr
);
580 mutex_unlock(&state
->platform
.risc
.mbx_if_lock
);
585 static u8
dib9000_mbx_read(struct dib9000_state
*state
, u16
* data
, u8 risc_id
, u16 attr
)
595 if (!state
->platform
.risc
.fw_is_running
)
598 if (mutex_lock_interruptible(&state
->platform
.risc
.mbx_if_lock
) < 0) {
599 dprintk("could not get the lock");
607 /* Length and type in the first word */
608 *data
= dib9000_read_word_attr(state
, 1029 + mc_base
, attr
);
611 if (size
<= MBX_MAX_WORDS
) {
613 size
--; /* Initial word already read */
615 dib9000_read16_noinc_attr(state
, 1029 + mc_base
, (u8
*) data
, size
* 2, attr
);
617 /* to word conversion */
618 for (i
= 0; i
< size
; i
++) {
620 *data
= (tmp
>> 8) | (tmp
<< 8);
626 for (i
= 0; i
< size
+ 1; i
++)
627 dprintk("%04x ", d
[i
]);
631 dprintk("MBX: message is too big for message cache (%d), flushing message", size
);
632 size
--; /* Initial word already read */
634 dib9000_read16_noinc_attr(state
, 1029 + mc_base
, (u8
*) data
, 2, attr
);
636 /* Update register nb_mes_in_TX */
637 dib9000_write_word_attr(state
, 1028 + mc_base
, 1 << 14, attr
);
639 mutex_unlock(&state
->platform
.risc
.mbx_if_lock
);
644 static int dib9000_risc_debug_buf(struct dib9000_state
*state
, u16
* data
, u8 size
)
646 u32 ts
= data
[1] << 16 | data
[0];
647 char *b
= (char *)&data
[2];
649 b
[2 * (size
- 2) - 1] = '\0'; /* Bullet proof the buffer */
654 dprintk("RISC%d: %d.%04d %s", state
->fe_id
, ts
/ 10000, ts
% 10000, *b
? b
: "<empty>");
658 static int dib9000_mbx_fetch_to_cache(struct dib9000_state
*state
, u16 attr
)
663 /* find a free slot */
664 for (i
= 0; i
< DIB9000_MSG_CACHE_SIZE
; i
++) {
665 block
= state
->platform
.risc
.message_cache
[i
];
667 size
= dib9000_mbx_read(state
, block
, 1, attr
);
669 /* dprintk( "MBX: fetched %04x message to cache", *block); */
671 switch (*block
>> 8) {
672 case IN_MSG_DEBUG_BUF
:
673 dib9000_risc_debug_buf(state
, block
+ 1, size
); /* debug-messages are going to be printed right away */
674 *block
= 0; /* free the block */
677 case IN_MSG_DATA
: /* FE-TRACE */
678 dib9000_risc_data_process(state
, block
+ 1, size
);
689 dprintk("MBX: no free cache-slot found for new message...");
693 static u8
dib9000_mbx_count(struct dib9000_state
*state
, u8 risc_id
, u16 attr
)
696 return (u8
) (dib9000_read_word_attr(state
, 1028, attr
) >> 10) & 0x1f; /* 5 bit field */
698 return (u8
) (dib9000_read_word_attr(state
, 1044, attr
) >> 8) & 0x7f; /* 7 bit field */
701 static int dib9000_mbx_process(struct dib9000_state
*state
, u16 attr
)
705 if (!state
->platform
.risc
.fw_is_running
)
708 if (mutex_lock_interruptible(&state
->platform
.risc
.mbx_lock
) < 0) {
709 dprintk("could not get the lock");
713 if (dib9000_mbx_count(state
, 1, attr
)) /* 1=RiscB */
714 ret
= dib9000_mbx_fetch_to_cache(state
, attr
);
716 dib9000_read_word_attr(state
, 1229, attr
); /* Clear the IRQ */
718 /* dprintk( "cleared IRQ: %x", tmp); */
719 mutex_unlock(&state
->platform
.risc
.mbx_lock
);
724 static int dib9000_mbx_get_message_attr(struct dib9000_state
*state
, u16 id
, u16
* msg
, u8
* size
, u16 attr
)
732 /* dib9000_mbx_get_from_cache(); */
733 for (i
= 0; i
< DIB9000_MSG_CACHE_SIZE
; i
++) {
734 block
= state
->platform
.risc
.message_cache
[i
];
735 if ((*block
>> 8) == id
) {
736 *size
= (*block
& 0xff) - 1;
737 memcpy(msg
, block
+ 1, (*size
) * 2);
738 *block
= 0; /* free the block */
739 i
= 0; /* signal that we found a message */
747 if (dib9000_mbx_process(state
, attr
) == -1) /* try to fetch one message - if any */
753 dprintk("waiting for message %d timed out", id
);
760 static int dib9000_risc_check_version(struct dib9000_state
*state
)
766 if (dib9000_mbx_send(state
, OUT_MSG_REQ_VERSION
, &fw_version
, 1) != 0)
769 if (dib9000_mbx_get_message(state
, IN_MSG_VERSION
, (u16
*) r
, &size
) < 0)
772 fw_version
= (r
[0] << 8) | r
[1];
773 dprintk("RISC: ver: %d.%02d (IC: %d)", fw_version
>> 10, fw_version
& 0x3ff, (r
[2] << 8) | r
[3]);
775 if ((fw_version
>> 10) != 7)
778 switch (fw_version
& 0x3ff) {
787 dprintk("RISC: invalid firmware version");
791 dprintk("RISC: valid firmware version");
795 static int dib9000_fw_boot(struct dib9000_state
*state
, const u8
* codeA
, u32 lenA
, const u8
* codeB
, u32 lenB
)
797 /* Reconfig pool mac ram */
798 dib9000_write_word(state
, 1225, 0x02); /* A: 8k C, 4 k D - B: 32k C 6 k D - IRAM 96k */
799 dib9000_write_word(state
, 1226, 0x05);
801 /* Toggles IP crypto to Host APB interface. */
802 dib9000_write_word(state
, 1542, 1);
804 /* Set jump and no jump in the dma box */
805 dib9000_write_word(state
, 1074, 0);
806 dib9000_write_word(state
, 1075, 0);
808 /* Set MAC as APB Master. */
809 dib9000_write_word(state
, 1237, 0);
811 /* Reset the RISCs */
813 dib9000_write_word(state
, 1024, 2);
815 dib9000_write_word(state
, 1024, 15);
817 dib9000_write_word(state
, 1040, 2);
820 dib9000_firmware_download(state
, 0, 0x1234, codeA
, lenA
);
822 dib9000_firmware_download(state
, 1, 0x1234, codeB
, lenB
);
826 dib9000_write_word(state
, 1024, 0);
828 dib9000_write_word(state
, 1040, 0);
831 if (dib9000_mbx_host_init(state
, 0) != 0)
834 if (dib9000_mbx_host_init(state
, 1) != 0)
838 state
->platform
.risc
.fw_is_running
= 1;
840 if (dib9000_risc_check_version(state
) != 0)
843 state
->platform
.risc
.memcmd
= 0xff;
847 static u16
dib9000_identify(struct i2c_device
*client
)
851 value
= dib9000_i2c_read16(client
, 896);
852 if (value
!= 0x01b3) {
853 dprintk("wrong Vendor ID (0x%x)", value
);
857 value
= dib9000_i2c_read16(client
, 897);
858 if (value
!= 0x4000 && value
!= 0x4001 && value
!= 0x4002 && value
!= 0x4003 && value
!= 0x4004 && value
!= 0x4005) {
859 dprintk("wrong Device ID (0x%x)", value
);
863 /* protect this driver to be used with 7000PC */
864 if (value
== 0x4000 && dib9000_i2c_read16(client
, 769) == 0x4000) {
865 dprintk("this driver does not work with DiB7000PC");
871 dprintk("found DiB7000MA/PA/MB/PB");
874 dprintk("found DiB7000HC");
877 dprintk("found DiB7000MC");
880 dprintk("found DiB9000A");
883 dprintk("found DiB9000H");
886 dprintk("found DiB9000M");
893 static void dib9000_set_power_mode(struct dib9000_state
*state
, enum dib9000_power_mode mode
)
895 /* by default everything is going to be powered off */
896 u16 reg_903
= 0x3fff, reg_904
= 0xffff, reg_905
= 0xffff, reg_906
;
899 if (state
->revision
== 0x4003 || state
->revision
== 0x4004 || state
->revision
== 0x4005)
904 reg_906
= dib9000_read_word(state
, 906 + offset
) | 0x3; /* keep settings for RISC */
906 /* now, depending on the requested mode, we power on */
908 /* power up everything in the demod */
909 case DIB9000_POWER_ALL
:
916 /* just leave power on the control-interfaces: GPIO and (I2C or SDIO or SRAM) */
917 case DIB9000_POWER_INTERFACE_ONLY
: /* TODO power up either SDIO or I2C or SRAM */
918 reg_905
&= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 2));
921 case DIB9000_POWER_INTERF_ANALOG_AGC
:
922 reg_903
&= ~((1 << 15) | (1 << 14) | (1 << 11) | (1 << 10));
923 reg_905
&= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 2));
924 reg_906
&= ~((1 << 0));
927 case DIB9000_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD
:
931 reg_906
&= ~((1 << 0));
934 case DIB9000_POWER_COR4_CRY_ESRAM_MOUT_NUD
:
938 reg_906
&= ~((1 << 0));
941 case DIB9000_POWER_NO
:
945 /* always power down unused parts */
946 if (!state
->platform
.host
.mobile_mode
)
947 reg_904
|= (1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) | (1 << 1);
949 /* P_sdio_select_clk = 0 on MC and after */
950 if (state
->revision
!= 0x4000)
953 dib9000_write_word(state
, 903 + offset
, reg_903
);
954 dib9000_write_word(state
, 904 + offset
, reg_904
);
955 dib9000_write_word(state
, 905 + offset
, reg_905
);
956 dib9000_write_word(state
, 906 + offset
, reg_906
);
959 static int dib9000_fw_reset(struct dvb_frontend
*fe
)
961 struct dib9000_state
*state
= fe
->demodulator_priv
;
963 dib9000_write_word(state
, 1817, 0x0003);
965 dib9000_write_word(state
, 1227, 1);
966 dib9000_write_word(state
, 1227, 0);
968 switch ((state
->revision
= dib9000_identify(&state
->i2c
))) {
978 /* reset the i2c-master to use the host interface */
979 dibx000_reset_i2c_master(&state
->i2c_master
);
981 dib9000_set_power_mode(state
, DIB9000_POWER_ALL
);
983 /* unforce divstr regardless whether i2c enumeration was done or not */
984 dib9000_write_word(state
, 1794, dib9000_read_word(state
, 1794) & ~(1 << 1));
985 dib9000_write_word(state
, 1796, 0);
986 dib9000_write_word(state
, 1805, 0x805);
988 /* restart all parts */
989 dib9000_write_word(state
, 898, 0xffff);
990 dib9000_write_word(state
, 899, 0xffff);
991 dib9000_write_word(state
, 900, 0x0001);
992 dib9000_write_word(state
, 901, 0xff19);
993 dib9000_write_word(state
, 902, 0x003c);
995 dib9000_write_word(state
, 898, 0);
996 dib9000_write_word(state
, 899, 0);
997 dib9000_write_word(state
, 900, 0);
998 dib9000_write_word(state
, 901, 0);
999 dib9000_write_word(state
, 902, 0);
1001 dib9000_write_word(state
, 911, state
->chip
.d9
.cfg
.if_drives
);
1003 dib9000_set_power_mode(state
, DIB9000_POWER_INTERFACE_ONLY
);
1008 static int dib9000_risc_apb_access_read(struct dib9000_state
*state
, u32 address
, u16 attribute
, const u8
* tx
, u32 txlen
, u8
* b
, u32 len
)
1013 if (address
>= 1024 || !state
->platform
.risc
.fw_is_running
)
1016 /* dprintk( "APB access thru rd fw %d %x", address, attribute); */
1018 mb
[0] = (u16
) address
;
1020 dib9000_mbx_send_attr(state
, OUT_MSG_BRIDGE_APB_R
, mb
, 2, attribute
);
1021 switch (dib9000_mbx_get_message_attr(state
, IN_MSG_END_BRIDGE_APB_RW
, mb
, &s
, attribute
)) {
1024 for (i
= 0; i
< s
; i
++) {
1025 b
[i
* 2] = (mb
[i
+ 1] >> 8) & 0xff;
1026 b
[i
* 2 + 1] = (mb
[i
+ 1]) & 0xff;
1035 static int dib9000_risc_apb_access_write(struct dib9000_state
*state
, u32 address
, u16 attribute
, const u8
* b
, u32 len
)
1040 if (address
>= 1024 || !state
->platform
.risc
.fw_is_running
)
1043 /* dprintk( "APB access thru wr fw %d %x", address, attribute); */
1045 mb
[0] = (unsigned short)address
;
1046 for (i
= 0; i
< len
&& i
< 20; i
+= 2)
1047 mb
[1 + (i
/ 2)] = (b
[i
] << 8 | b
[i
+ 1]);
1049 dib9000_mbx_send_attr(state
, OUT_MSG_BRIDGE_APB_W
, mb
, 1 + len
/ 2, attribute
);
1050 return dib9000_mbx_get_message_attr(state
, IN_MSG_END_BRIDGE_APB_RW
, mb
, &s
, attribute
) == 1 ? 0 : -EINVAL
;
1053 static int dib9000_fw_memmbx_sync(struct dib9000_state
*state
, u8 i
)
1057 if (!state
->platform
.risc
.fw_is_running
)
1059 dib9000_risc_mem_write(state
, FE_MM_RW_SYNC
, &i
);
1061 dib9000_risc_mem_read(state
, FE_MM_RW_SYNC
, state
->i2c_read_buffer
, 1);
1062 } while (state
->i2c_read_buffer
[0] && index_loop
--);
1069 static int dib9000_fw_init(struct dib9000_state
*state
)
1071 struct dibGPIOFunction
*f
;
1076 if (dib9000_fw_boot(state
, NULL
, 0, state
->chip
.d9
.cfg
.microcode_B_fe_buffer
, state
->chip
.d9
.cfg
.microcode_B_fe_size
) != 0)
1079 /* initialize the firmware */
1080 for (i
= 0; i
< ARRAY_SIZE(state
->chip
.d9
.cfg
.gpio_function
); i
++) {
1081 f
= &state
->chip
.d9
.cfg
.gpio_function
[i
];
1083 switch (f
->function
) {
1084 case BOARD_GPIO_FUNCTION_COMPONENT_ON
:
1085 b
[0] = (u16
) f
->mask
;
1086 b
[1] = (u16
) f
->direction
;
1087 b
[2] = (u16
) f
->value
;
1089 case BOARD_GPIO_FUNCTION_COMPONENT_OFF
:
1090 b
[3] = (u16
) f
->mask
;
1091 b
[4] = (u16
) f
->direction
;
1092 b
[5] = (u16
) f
->value
;
1097 if (dib9000_mbx_send(state
, OUT_MSG_CONF_GPIO
, b
, 15) != 0)
1101 b
[0] = state
->chip
.d9
.cfg
.subband
.size
; /* type == 0 -> GPIO - PWM not yet supported */
1102 for (i
= 0; i
< state
->chip
.d9
.cfg
.subband
.size
; i
++) {
1103 b
[1 + i
* 4] = state
->chip
.d9
.cfg
.subband
.subband
[i
].f_mhz
;
1104 b
[2 + i
* 4] = (u16
) state
->chip
.d9
.cfg
.subband
.subband
[i
].gpio
.mask
;
1105 b
[3 + i
* 4] = (u16
) state
->chip
.d9
.cfg
.subband
.subband
[i
].gpio
.direction
;
1106 b
[4 + i
* 4] = (u16
) state
->chip
.d9
.cfg
.subband
.subband
[i
].gpio
.value
;
1108 b
[1 + i
* 4] = 0; /* fe_id */
1109 if (dib9000_mbx_send(state
, OUT_MSG_SUBBAND_SEL
, b
, 2 + 4 * i
) != 0)
1112 /* 0 - id, 1 - no_of_frontends */
1113 b
[0] = (0 << 8) | 1;
1114 /* 0 = i2c-address demod, 0 = tuner */
1115 b
[1] = (0 << 8) | (0);
1116 b
[2] = (u16
) (((state
->chip
.d9
.cfg
.xtal_clock_khz
* 1000) >> 16) & 0xffff);
1117 b
[3] = (u16
) (((state
->chip
.d9
.cfg
.xtal_clock_khz
* 1000)) & 0xffff);
1118 b
[4] = (u16
) ((state
->chip
.d9
.cfg
.vcxo_timer
>> 16) & 0xffff);
1119 b
[5] = (u16
) ((state
->chip
.d9
.cfg
.vcxo_timer
) & 0xffff);
1120 b
[6] = (u16
) ((state
->chip
.d9
.cfg
.timing_frequency
>> 16) & 0xffff);
1121 b
[7] = (u16
) ((state
->chip
.d9
.cfg
.timing_frequency
) & 0xffff);
1122 b
[29] = state
->chip
.d9
.cfg
.if_drives
;
1123 if (dib9000_mbx_send(state
, OUT_MSG_INIT_DEMOD
, b
, ARRAY_SIZE(b
)) != 0)
1126 if (dib9000_mbx_send(state
, OUT_MSG_FE_FW_DL
, NULL
, 0) != 0)
1129 if (dib9000_mbx_get_message(state
, IN_MSG_FE_FW_DL_DONE
, b
, &size
) < 0)
1132 if (size
> ARRAY_SIZE(b
)) {
1133 dprintk("error : firmware returned %dbytes needed but the used buffer has only %dbytes\n Firmware init ABORTED", size
,
1134 (int)ARRAY_SIZE(b
));
1138 for (i
= 0; i
< size
; i
+= 2) {
1139 state
->platform
.risc
.fe_mm
[i
/ 2].addr
= b
[i
+ 0];
1140 state
->platform
.risc
.fe_mm
[i
/ 2].size
= b
[i
+ 1];
1146 static void dib9000_fw_set_channel_head(struct dib9000_state
*state
)
1149 u32 freq
= state
->fe
[0]->dtv_property_cache
.frequency
/ 1000;
1150 if (state
->fe_id
% 2)
1153 b
[0] = (u8
) ((freq
>> 0) & 0xff);
1154 b
[1] = (u8
) ((freq
>> 8) & 0xff);
1155 b
[2] = (u8
) ((freq
>> 16) & 0xff);
1156 b
[3] = (u8
) ((freq
>> 24) & 0xff);
1157 b
[4] = (u8
) ((state
->fe
[0]->dtv_property_cache
.bandwidth_hz
/ 1000 >> 0) & 0xff);
1158 b
[5] = (u8
) ((state
->fe
[0]->dtv_property_cache
.bandwidth_hz
/ 1000 >> 8) & 0xff);
1159 b
[6] = (u8
) ((state
->fe
[0]->dtv_property_cache
.bandwidth_hz
/ 1000 >> 16) & 0xff);
1160 b
[7] = (u8
) ((state
->fe
[0]->dtv_property_cache
.bandwidth_hz
/ 1000 >> 24) & 0xff);
1161 b
[8] = 0x80; /* do not wait for CELL ID when doing autosearch */
1162 if (state
->fe
[0]->dtv_property_cache
.delivery_system
== SYS_DVBT
)
1164 dib9000_risc_mem_write(state
, FE_MM_W_CHANNEL_HEAD
, b
);
1167 static int dib9000_fw_get_channel(struct dvb_frontend
*fe
)
1169 struct dib9000_state
*state
= fe
->demodulator_priv
;
1170 struct dibDVBTChannel
{
1171 s8 spectrum_inversion
;
1185 struct dibDVBTChannel
*ch
;
1188 if (mutex_lock_interruptible(&state
->platform
.risc
.mem_mbx_lock
) < 0) {
1189 dprintk("could not get the lock");
1192 if (dib9000_fw_memmbx_sync(state
, FE_SYNC_CHANNEL
) < 0) {
1197 dib9000_risc_mem_read(state
, FE_MM_R_CHANNEL_UNION
,
1198 state
->i2c_read_buffer
, sizeof(struct dibDVBTChannel
));
1199 ch
= (struct dibDVBTChannel
*)state
->i2c_read_buffer
;
1202 switch (ch
->spectrum_inversion
& 0x7) {
1204 state
->fe
[0]->dtv_property_cache
.inversion
= INVERSION_ON
;
1207 state
->fe
[0]->dtv_property_cache
.inversion
= INVERSION_OFF
;
1211 state
->fe
[0]->dtv_property_cache
.inversion
= INVERSION_AUTO
;
1216 state
->fe
[0]->dtv_property_cache
.transmission_mode
= TRANSMISSION_MODE_2K
;
1219 state
->fe
[0]->dtv_property_cache
.transmission_mode
= TRANSMISSION_MODE_4K
;
1222 state
->fe
[0]->dtv_property_cache
.transmission_mode
= TRANSMISSION_MODE_8K
;
1226 state
->fe
[0]->dtv_property_cache
.transmission_mode
= TRANSMISSION_MODE_AUTO
;
1229 switch (ch
->guard
) {
1231 state
->fe
[0]->dtv_property_cache
.guard_interval
= GUARD_INTERVAL_1_32
;
1234 state
->fe
[0]->dtv_property_cache
.guard_interval
= GUARD_INTERVAL_1_16
;
1237 state
->fe
[0]->dtv_property_cache
.guard_interval
= GUARD_INTERVAL_1_8
;
1240 state
->fe
[0]->dtv_property_cache
.guard_interval
= GUARD_INTERVAL_1_4
;
1244 state
->fe
[0]->dtv_property_cache
.guard_interval
= GUARD_INTERVAL_AUTO
;
1247 switch (ch
->constellation
) {
1249 state
->fe
[0]->dtv_property_cache
.modulation
= QAM_64
;
1252 state
->fe
[0]->dtv_property_cache
.modulation
= QAM_16
;
1255 state
->fe
[0]->dtv_property_cache
.modulation
= QPSK
;
1259 state
->fe
[0]->dtv_property_cache
.modulation
= QAM_AUTO
;
1264 state
->fe
[0]->dtv_property_cache
.hierarchy
= HIERARCHY_NONE
;
1267 state
->fe
[0]->dtv_property_cache
.hierarchy
= HIERARCHY_1
;
1271 state
->fe
[0]->dtv_property_cache
.hierarchy
= HIERARCHY_AUTO
;
1274 switch (ch
->code_rate_hp
) {
1276 state
->fe
[0]->dtv_property_cache
.code_rate_HP
= FEC_1_2
;
1279 state
->fe
[0]->dtv_property_cache
.code_rate_HP
= FEC_2_3
;
1282 state
->fe
[0]->dtv_property_cache
.code_rate_HP
= FEC_3_4
;
1285 state
->fe
[0]->dtv_property_cache
.code_rate_HP
= FEC_5_6
;
1288 state
->fe
[0]->dtv_property_cache
.code_rate_HP
= FEC_7_8
;
1292 state
->fe
[0]->dtv_property_cache
.code_rate_HP
= FEC_AUTO
;
1295 switch (ch
->code_rate_lp
) {
1297 state
->fe
[0]->dtv_property_cache
.code_rate_LP
= FEC_1_2
;
1300 state
->fe
[0]->dtv_property_cache
.code_rate_LP
= FEC_2_3
;
1303 state
->fe
[0]->dtv_property_cache
.code_rate_LP
= FEC_3_4
;
1306 state
->fe
[0]->dtv_property_cache
.code_rate_LP
= FEC_5_6
;
1309 state
->fe
[0]->dtv_property_cache
.code_rate_LP
= FEC_7_8
;
1313 state
->fe
[0]->dtv_property_cache
.code_rate_LP
= FEC_AUTO
;
1318 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
1322 static int dib9000_fw_set_channel_union(struct dvb_frontend
*fe
)
1324 struct dib9000_state
*state
= fe
->demodulator_priv
;
1325 struct dibDVBTChannel
{
1326 s8 spectrum_inversion
;
1340 struct dibDVBTChannel ch
;
1342 switch (state
->fe
[0]->dtv_property_cache
.inversion
) {
1344 ch
.spectrum_inversion
= 1;
1347 ch
.spectrum_inversion
= 0;
1350 case INVERSION_AUTO
:
1351 ch
.spectrum_inversion
= -1;
1354 switch (state
->fe
[0]->dtv_property_cache
.transmission_mode
) {
1355 case TRANSMISSION_MODE_2K
:
1358 case TRANSMISSION_MODE_4K
:
1361 case TRANSMISSION_MODE_8K
:
1365 case TRANSMISSION_MODE_AUTO
:
1369 switch (state
->fe
[0]->dtv_property_cache
.guard_interval
) {
1370 case GUARD_INTERVAL_1_32
:
1373 case GUARD_INTERVAL_1_16
:
1376 case GUARD_INTERVAL_1_8
:
1379 case GUARD_INTERVAL_1_4
:
1383 case GUARD_INTERVAL_AUTO
:
1387 switch (state
->fe
[0]->dtv_property_cache
.modulation
) {
1389 ch
.constellation
= 2;
1392 ch
.constellation
= 1;
1395 ch
.constellation
= 0;
1399 ch
.constellation
= -1;
1402 switch (state
->fe
[0]->dtv_property_cache
.hierarchy
) {
1403 case HIERARCHY_NONE
:
1412 case HIERARCHY_AUTO
:
1417 switch (state
->fe
[0]->dtv_property_cache
.code_rate_HP
) {
1419 ch
.code_rate_hp
= 1;
1422 ch
.code_rate_hp
= 2;
1425 ch
.code_rate_hp
= 3;
1428 ch
.code_rate_hp
= 5;
1431 ch
.code_rate_hp
= 7;
1435 ch
.code_rate_hp
= -1;
1438 switch (state
->fe
[0]->dtv_property_cache
.code_rate_LP
) {
1440 ch
.code_rate_lp
= 1;
1443 ch
.code_rate_lp
= 2;
1446 ch
.code_rate_lp
= 3;
1449 ch
.code_rate_lp
= 5;
1452 ch
.code_rate_lp
= 7;
1456 ch
.code_rate_lp
= -1;
1460 ch
.intlv_native
= 1;
1462 dib9000_risc_mem_write(state
, FE_MM_W_CHANNEL_UNION
, (u8
*) &ch
);
1467 static int dib9000_fw_tune(struct dvb_frontend
*fe
)
1469 struct dib9000_state
*state
= fe
->demodulator_priv
;
1470 int ret
= 10, search
= state
->channel_status
.status
== CHANNEL_STATUS_PARAMETERS_UNKNOWN
;
1473 switch (state
->tune_state
) {
1474 case CT_DEMOD_START
:
1475 dib9000_fw_set_channel_head(state
);
1477 /* write the channel context - a channel is initialized to 0, so it is OK */
1478 dib9000_risc_mem_write(state
, FE_MM_W_CHANNEL_CONTEXT
, (u8
*) fe_info
);
1479 dib9000_risc_mem_write(state
, FE_MM_W_FE_INFO
, (u8
*) fe_info
);
1482 dib9000_mbx_send(state
, OUT_MSG_FE_CHANNEL_SEARCH
, NULL
, 0);
1484 dib9000_fw_set_channel_union(fe
);
1485 dib9000_mbx_send(state
, OUT_MSG_FE_CHANNEL_TUNE
, NULL
, 0);
1487 state
->tune_state
= CT_DEMOD_STEP_1
;
1489 case CT_DEMOD_STEP_1
:
1491 dib9000_risc_mem_read(state
, FE_MM_R_CHANNEL_SEARCH_STATE
, state
->i2c_read_buffer
, 1);
1493 dib9000_risc_mem_read(state
, FE_MM_R_CHANNEL_TUNE_STATE
, state
->i2c_read_buffer
, 1);
1494 i
= (s8
)state
->i2c_read_buffer
[0];
1495 switch (i
) { /* something happened */
1498 case -2: /* tps locks are "slower" than MPEG locks -> even in autosearch data is OK here */
1500 state
->status
= FE_STATUS_DEMOD_SUCCESS
;
1502 state
->tune_state
= CT_DEMOD_STOP
;
1503 state
->status
= FE_STATUS_LOCKED
;
1507 state
->status
= FE_STATUS_TUNE_FAILED
;
1508 state
->tune_state
= CT_DEMOD_STOP
;
1513 ret
= FE_CALLBACK_TIME_NEVER
;
1520 static int dib9000_fw_set_diversity_in(struct dvb_frontend
*fe
, int onoff
)
1522 struct dib9000_state
*state
= fe
->demodulator_priv
;
1523 u16 mode
= (u16
) onoff
;
1524 return dib9000_mbx_send(state
, OUT_MSG_ENABLE_DIVERSITY
, &mode
, 1);
1527 static int dib9000_fw_set_output_mode(struct dvb_frontend
*fe
, int mode
)
1529 struct dib9000_state
*state
= fe
->demodulator_priv
;
1530 u16 outreg
, smo_mode
;
1532 dprintk("setting output mode for demod %p to %d", fe
, mode
);
1535 case OUTMODE_MPEG2_PAR_GATED_CLK
:
1536 outreg
= (1 << 10); /* 0x0400 */
1538 case OUTMODE_MPEG2_PAR_CONT_CLK
:
1539 outreg
= (1 << 10) | (1 << 6); /* 0x0440 */
1541 case OUTMODE_MPEG2_SERIAL
:
1542 outreg
= (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
1544 case OUTMODE_DIVERSITY
:
1545 outreg
= (1 << 10) | (4 << 6); /* 0x0500 */
1547 case OUTMODE_MPEG2_FIFO
:
1548 outreg
= (1 << 10) | (5 << 6);
1550 case OUTMODE_HIGH_Z
:
1554 dprintk("Unhandled output_mode passed to be set for demod %p", &state
->fe
[0]);
1558 dib9000_write_word(state
, 1795, outreg
);
1561 case OUTMODE_MPEG2_PAR_GATED_CLK
:
1562 case OUTMODE_MPEG2_PAR_CONT_CLK
:
1563 case OUTMODE_MPEG2_SERIAL
:
1564 case OUTMODE_MPEG2_FIFO
:
1565 smo_mode
= (dib9000_read_word(state
, 295) & 0x0010) | (1 << 1);
1566 if (state
->chip
.d9
.cfg
.output_mpeg2_in_188_bytes
)
1567 smo_mode
|= (1 << 5);
1568 dib9000_write_word(state
, 295, smo_mode
);
1572 outreg
= to_fw_output_mode(mode
);
1573 return dib9000_mbx_send(state
, OUT_MSG_SET_OUTPUT_MODE
, &outreg
, 1);
1576 static int dib9000_tuner_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg msg
[], int num
)
1578 struct dib9000_state
*state
= i2c_get_adapdata(i2c_adap
);
1579 u16 i
, len
, t
, index_msg
;
1581 for (index_msg
= 0; index_msg
< num
; index_msg
++) {
1582 if (msg
[index_msg
].flags
& I2C_M_RD
) { /* read */
1583 len
= msg
[index_msg
].len
;
1587 if (dib9000_read_word(state
, 790) != 0)
1588 dprintk("TunerITF: read busy");
1590 dib9000_write_word(state
, 784, (u16
) (msg
[index_msg
].addr
));
1591 dib9000_write_word(state
, 787, (len
/ 2) - 1);
1592 dib9000_write_word(state
, 786, 1); /* start read */
1595 while (dib9000_read_word(state
, 790) != (len
/ 2) && i
)
1599 dprintk("TunerITF: read failed");
1601 for (i
= 0; i
< len
; i
+= 2) {
1602 t
= dib9000_read_word(state
, 785);
1603 msg
[index_msg
].buf
[i
] = (t
>> 8) & 0xff;
1604 msg
[index_msg
].buf
[i
+ 1] = (t
) & 0xff;
1606 if (dib9000_read_word(state
, 790) != 0)
1607 dprintk("TunerITF: read more data than expected");
1610 while (dib9000_read_word(state
, 789) && i
)
1613 dprintk("TunerITF: write busy");
1615 len
= msg
[index_msg
].len
;
1619 for (i
= 0; i
< len
; i
+= 2)
1620 dib9000_write_word(state
, 785, (msg
[index_msg
].buf
[i
] << 8) | msg
[index_msg
].buf
[i
+ 1]);
1621 dib9000_write_word(state
, 784, (u16
) msg
[index_msg
].addr
);
1622 dib9000_write_word(state
, 787, (len
/ 2) - 1);
1623 dib9000_write_word(state
, 786, 0); /* start write */
1626 while (dib9000_read_word(state
, 791) > 0 && i
)
1629 dprintk("TunerITF: write failed");
1635 int dib9000_fw_set_component_bus_speed(struct dvb_frontend
*fe
, u16 speed
)
1637 struct dib9000_state
*state
= fe
->demodulator_priv
;
1639 state
->component_bus_speed
= speed
;
1642 EXPORT_SYMBOL(dib9000_fw_set_component_bus_speed
);
1644 static int dib9000_fw_component_bus_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg msg
[], int num
)
1646 struct dib9000_state
*state
= i2c_get_adapdata(i2c_adap
);
1647 u8 type
= 0; /* I2C */
1648 u8 port
= DIBX000_I2C_INTERFACE_GPIO_3_4
;
1649 u16 scl
= state
->component_bus_speed
; /* SCL frequency */
1650 struct dib9000_fe_memory_map
*m
= &state
->platform
.risc
.fe_mm
[FE_MM_RW_COMPONENT_ACCESS_BUFFER
];
1655 p
[2] = msg
[0].addr
<< 1;
1657 p
[3] = (u8
) scl
& 0xff; /* scl */
1658 p
[4] = (u8
) (scl
>> 8);
1663 p
[9] = (u8
) (msg
[0].len
);
1664 p
[10] = (u8
) (msg
[0].len
>> 8);
1665 if ((num
> 1) && (msg
[1].flags
& I2C_M_RD
)) {
1666 p
[11] = (u8
) (msg
[1].len
);
1667 p
[12] = (u8
) (msg
[1].len
>> 8);
1673 if (mutex_lock_interruptible(&state
->platform
.risc
.mem_mbx_lock
) < 0) {
1674 dprintk("could not get the lock");
1678 dib9000_risc_mem_write(state
, FE_MM_W_COMPONENT_ACCESS
, p
);
1681 dib9000_risc_mem_setup_cmd(state
, m
->addr
, msg
[0].len
, 0);
1682 dib9000_risc_mem_write_chunks(state
, msg
[0].buf
, msg
[0].len
);
1685 /* do the transaction */
1686 if (dib9000_fw_memmbx_sync(state
, FE_SYNC_COMPONENT_ACCESS
) < 0) {
1687 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
1691 /* read back any possible result */
1692 if ((num
> 1) && (msg
[1].flags
& I2C_M_RD
))
1693 dib9000_risc_mem_read(state
, FE_MM_RW_COMPONENT_ACCESS_BUFFER
, msg
[1].buf
, msg
[1].len
);
1695 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
1700 static u32
dib9000_i2c_func(struct i2c_adapter
*adapter
)
1702 return I2C_FUNC_I2C
;
1705 static struct i2c_algorithm dib9000_tuner_algo
= {
1706 .master_xfer
= dib9000_tuner_xfer
,
1707 .functionality
= dib9000_i2c_func
,
1710 static struct i2c_algorithm dib9000_component_bus_algo
= {
1711 .master_xfer
= dib9000_fw_component_bus_xfer
,
1712 .functionality
= dib9000_i2c_func
,
1715 struct i2c_adapter
*dib9000_get_tuner_interface(struct dvb_frontend
*fe
)
1717 struct dib9000_state
*st
= fe
->demodulator_priv
;
1718 return &st
->tuner_adap
;
1720 EXPORT_SYMBOL(dib9000_get_tuner_interface
);
1722 struct i2c_adapter
*dib9000_get_component_bus_interface(struct dvb_frontend
*fe
)
1724 struct dib9000_state
*st
= fe
->demodulator_priv
;
1725 return &st
->component_bus
;
1727 EXPORT_SYMBOL(dib9000_get_component_bus_interface
);
1729 struct i2c_adapter
*dib9000_get_i2c_master(struct dvb_frontend
*fe
, enum dibx000_i2c_interface intf
, int gating
)
1731 struct dib9000_state
*st
= fe
->demodulator_priv
;
1732 return dibx000_get_i2c_adapter(&st
->i2c_master
, intf
, gating
);
1734 EXPORT_SYMBOL(dib9000_get_i2c_master
);
1736 int dib9000_set_i2c_adapter(struct dvb_frontend
*fe
, struct i2c_adapter
*i2c
)
1738 struct dib9000_state
*st
= fe
->demodulator_priv
;
1740 st
->i2c
.i2c_adap
= i2c
;
1743 EXPORT_SYMBOL(dib9000_set_i2c_adapter
);
1745 static int dib9000_cfg_gpio(struct dib9000_state
*st
, u8 num
, u8 dir
, u8 val
)
1747 st
->gpio_dir
= dib9000_read_word(st
, 773);
1748 st
->gpio_dir
&= ~(1 << num
); /* reset the direction bit */
1749 st
->gpio_dir
|= (dir
& 0x1) << num
; /* set the new direction */
1750 dib9000_write_word(st
, 773, st
->gpio_dir
);
1752 st
->gpio_val
= dib9000_read_word(st
, 774);
1753 st
->gpio_val
&= ~(1 << num
); /* reset the direction bit */
1754 st
->gpio_val
|= (val
& 0x01) << num
; /* set the new value */
1755 dib9000_write_word(st
, 774, st
->gpio_val
);
1757 dprintk("gpio dir: %04x: gpio val: %04x", st
->gpio_dir
, st
->gpio_val
);
1762 int dib9000_set_gpio(struct dvb_frontend
*fe
, u8 num
, u8 dir
, u8 val
)
1764 struct dib9000_state
*state
= fe
->demodulator_priv
;
1765 return dib9000_cfg_gpio(state
, num
, dir
, val
);
1767 EXPORT_SYMBOL(dib9000_set_gpio
);
1769 int dib9000_fw_pid_filter_ctrl(struct dvb_frontend
*fe
, u8 onoff
)
1771 struct dib9000_state
*state
= fe
->demodulator_priv
;
1775 if ((state
->pid_ctrl_index
!= -2) && (state
->pid_ctrl_index
< 9)) {
1776 /* postpone the pid filtering cmd */
1777 dprintk("pid filter cmd postpone");
1778 state
->pid_ctrl_index
++;
1779 state
->pid_ctrl
[state
->pid_ctrl_index
].cmd
= DIB9000_PID_FILTER_CTRL
;
1780 state
->pid_ctrl
[state
->pid_ctrl_index
].onoff
= onoff
;
1784 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
1785 dprintk("could not get the lock");
1789 val
= dib9000_read_word(state
, 294 + 1) & 0xffef;
1790 val
|= (onoff
& 0x1) << 4;
1792 dprintk("PID filter enabled %d", onoff
);
1793 ret
= dib9000_write_word(state
, 294 + 1, val
);
1794 mutex_unlock(&state
->demod_lock
);
1798 EXPORT_SYMBOL(dib9000_fw_pid_filter_ctrl
);
1800 int dib9000_fw_pid_filter(struct dvb_frontend
*fe
, u8 id
, u16 pid
, u8 onoff
)
1802 struct dib9000_state
*state
= fe
->demodulator_priv
;
1805 if (state
->pid_ctrl_index
!= -2) {
1806 /* postpone the pid filtering cmd */
1807 dprintk("pid filter postpone");
1808 if (state
->pid_ctrl_index
< 9) {
1809 state
->pid_ctrl_index
++;
1810 state
->pid_ctrl
[state
->pid_ctrl_index
].cmd
= DIB9000_PID_FILTER
;
1811 state
->pid_ctrl
[state
->pid_ctrl_index
].id
= id
;
1812 state
->pid_ctrl
[state
->pid_ctrl_index
].pid
= pid
;
1813 state
->pid_ctrl
[state
->pid_ctrl_index
].onoff
= onoff
;
1815 dprintk("can not add any more pid ctrl cmd");
1819 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
1820 dprintk("could not get the lock");
1823 dprintk("Index %x, PID %d, OnOff %d", id
, pid
, onoff
);
1824 ret
= dib9000_write_word(state
, 300 + 1 + id
,
1825 onoff
? (1 << 13) | pid
: 0);
1826 mutex_unlock(&state
->demod_lock
);
1829 EXPORT_SYMBOL(dib9000_fw_pid_filter
);
1831 int dib9000_firmware_post_pll_init(struct dvb_frontend
*fe
)
1833 struct dib9000_state
*state
= fe
->demodulator_priv
;
1834 return dib9000_fw_init(state
);
1836 EXPORT_SYMBOL(dib9000_firmware_post_pll_init
);
1838 static void dib9000_release(struct dvb_frontend
*demod
)
1840 struct dib9000_state
*st
= demod
->demodulator_priv
;
1843 for (index_frontend
= 1; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (st
->fe
[index_frontend
] != NULL
); index_frontend
++)
1844 dvb_frontend_detach(st
->fe
[index_frontend
]);
1846 dibx000_exit_i2c_master(&st
->i2c_master
);
1848 i2c_del_adapter(&st
->tuner_adap
);
1849 i2c_del_adapter(&st
->component_bus
);
1854 static int dib9000_wakeup(struct dvb_frontend
*fe
)
1859 static int dib9000_sleep(struct dvb_frontend
*fe
)
1861 struct dib9000_state
*state
= fe
->demodulator_priv
;
1865 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
1866 dprintk("could not get the lock");
1869 for (index_frontend
= 1; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
1870 ret
= state
->fe
[index_frontend
]->ops
.sleep(state
->fe
[index_frontend
]);
1874 ret
= dib9000_mbx_send(state
, OUT_MSG_FE_SLEEP
, NULL
, 0);
1877 mutex_unlock(&state
->demod_lock
);
1881 static int dib9000_fe_get_tune_settings(struct dvb_frontend
*fe
, struct dvb_frontend_tune_settings
*tune
)
1883 tune
->min_delay_ms
= 1000;
1887 static int dib9000_get_frontend(struct dvb_frontend
*fe
)
1889 struct dib9000_state
*state
= fe
->demodulator_priv
;
1890 u8 index_frontend
, sub_index_frontend
;
1894 if (state
->get_frontend_internal
== 0) {
1895 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
1896 dprintk("could not get the lock");
1901 for (index_frontend
= 1; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
1902 state
->fe
[index_frontend
]->ops
.read_status(state
->fe
[index_frontend
], &stat
);
1903 if (stat
& FE_HAS_SYNC
) {
1904 dprintk("TPS lock on the slave%i", index_frontend
);
1906 /* synchronize the cache with the other frontends */
1907 state
->fe
[index_frontend
]->ops
.get_frontend(state
->fe
[index_frontend
]);
1908 for (sub_index_frontend
= 0; (sub_index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[sub_index_frontend
] != NULL
);
1909 sub_index_frontend
++) {
1910 if (sub_index_frontend
!= index_frontend
) {
1911 state
->fe
[sub_index_frontend
]->dtv_property_cache
.modulation
=
1912 state
->fe
[index_frontend
]->dtv_property_cache
.modulation
;
1913 state
->fe
[sub_index_frontend
]->dtv_property_cache
.inversion
=
1914 state
->fe
[index_frontend
]->dtv_property_cache
.inversion
;
1915 state
->fe
[sub_index_frontend
]->dtv_property_cache
.transmission_mode
=
1916 state
->fe
[index_frontend
]->dtv_property_cache
.transmission_mode
;
1917 state
->fe
[sub_index_frontend
]->dtv_property_cache
.guard_interval
=
1918 state
->fe
[index_frontend
]->dtv_property_cache
.guard_interval
;
1919 state
->fe
[sub_index_frontend
]->dtv_property_cache
.hierarchy
=
1920 state
->fe
[index_frontend
]->dtv_property_cache
.hierarchy
;
1921 state
->fe
[sub_index_frontend
]->dtv_property_cache
.code_rate_HP
=
1922 state
->fe
[index_frontend
]->dtv_property_cache
.code_rate_HP
;
1923 state
->fe
[sub_index_frontend
]->dtv_property_cache
.code_rate_LP
=
1924 state
->fe
[index_frontend
]->dtv_property_cache
.code_rate_LP
;
1925 state
->fe
[sub_index_frontend
]->dtv_property_cache
.rolloff
=
1926 state
->fe
[index_frontend
]->dtv_property_cache
.rolloff
;
1934 /* get the channel from master chip */
1935 ret
= dib9000_fw_get_channel(fe
);
1939 /* synchronize the cache with the other frontends */
1940 for (index_frontend
= 1; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
1941 state
->fe
[index_frontend
]->dtv_property_cache
.inversion
= fe
->dtv_property_cache
.inversion
;
1942 state
->fe
[index_frontend
]->dtv_property_cache
.transmission_mode
= fe
->dtv_property_cache
.transmission_mode
;
1943 state
->fe
[index_frontend
]->dtv_property_cache
.guard_interval
= fe
->dtv_property_cache
.guard_interval
;
1944 state
->fe
[index_frontend
]->dtv_property_cache
.modulation
= fe
->dtv_property_cache
.modulation
;
1945 state
->fe
[index_frontend
]->dtv_property_cache
.hierarchy
= fe
->dtv_property_cache
.hierarchy
;
1946 state
->fe
[index_frontend
]->dtv_property_cache
.code_rate_HP
= fe
->dtv_property_cache
.code_rate_HP
;
1947 state
->fe
[index_frontend
]->dtv_property_cache
.code_rate_LP
= fe
->dtv_property_cache
.code_rate_LP
;
1948 state
->fe
[index_frontend
]->dtv_property_cache
.rolloff
= fe
->dtv_property_cache
.rolloff
;
1953 if (state
->get_frontend_internal
== 0)
1954 mutex_unlock(&state
->demod_lock
);
1958 static int dib9000_set_tune_state(struct dvb_frontend
*fe
, enum frontend_tune_state tune_state
)
1960 struct dib9000_state
*state
= fe
->demodulator_priv
;
1961 state
->tune_state
= tune_state
;
1962 if (tune_state
== CT_DEMOD_START
)
1963 state
->status
= FE_STATUS_TUNE_PENDING
;
1968 static u32
dib9000_get_status(struct dvb_frontend
*fe
)
1970 struct dib9000_state
*state
= fe
->demodulator_priv
;
1971 return state
->status
;
1974 static int dib9000_set_channel_status(struct dvb_frontend
*fe
, struct dvb_frontend_parametersContext
*channel_status
)
1976 struct dib9000_state
*state
= fe
->demodulator_priv
;
1978 memcpy(&state
->channel_status
, channel_status
, sizeof(struct dvb_frontend_parametersContext
));
1982 static int dib9000_set_frontend(struct dvb_frontend
*fe
)
1984 struct dib9000_state
*state
= fe
->demodulator_priv
;
1985 int sleep_time
, sleep_time_slave
;
1986 u32 frontend_status
;
1987 u8 nbr_pending
, exit_condition
, index_frontend
, index_frontend_success
;
1988 struct dvb_frontend_parametersContext channel_status
;
1990 /* check that the correct parameters are set */
1991 if (state
->fe
[0]->dtv_property_cache
.frequency
== 0) {
1992 dprintk("dib9000: must specify frequency ");
1996 if (state
->fe
[0]->dtv_property_cache
.bandwidth_hz
== 0) {
1997 dprintk("dib9000: must specify bandwidth ");
2001 state
->pid_ctrl_index
= -1; /* postpone the pid filtering cmd */
2002 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
2003 dprintk("could not get the lock");
2007 fe
->dtv_property_cache
.delivery_system
= SYS_DVBT
;
2009 /* set the master status */
2010 if (state
->fe
[0]->dtv_property_cache
.transmission_mode
== TRANSMISSION_MODE_AUTO
||
2011 state
->fe
[0]->dtv_property_cache
.guard_interval
== GUARD_INTERVAL_AUTO
||
2012 state
->fe
[0]->dtv_property_cache
.modulation
== QAM_AUTO
||
2013 state
->fe
[0]->dtv_property_cache
.code_rate_HP
== FEC_AUTO
) {
2014 /* no channel specified, autosearch the channel */
2015 state
->channel_status
.status
= CHANNEL_STATUS_PARAMETERS_UNKNOWN
;
2017 state
->channel_status
.status
= CHANNEL_STATUS_PARAMETERS_SET
;
2019 /* set mode and status for the different frontends */
2020 for (index_frontend
= 0; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
2021 dib9000_fw_set_diversity_in(state
->fe
[index_frontend
], 1);
2023 /* synchronization of the cache */
2024 memcpy(&state
->fe
[index_frontend
]->dtv_property_cache
, &fe
->dtv_property_cache
, sizeof(struct dtv_frontend_properties
));
2026 state
->fe
[index_frontend
]->dtv_property_cache
.delivery_system
= SYS_DVBT
;
2027 dib9000_fw_set_output_mode(state
->fe
[index_frontend
], OUTMODE_HIGH_Z
);
2029 dib9000_set_channel_status(state
->fe
[index_frontend
], &state
->channel_status
);
2030 dib9000_set_tune_state(state
->fe
[index_frontend
], CT_DEMOD_START
);
2034 exit_condition
= 0; /* 0: tune pending; 1: tune failed; 2:tune success */
2035 index_frontend_success
= 0;
2037 sleep_time
= dib9000_fw_tune(state
->fe
[0]);
2038 for (index_frontend
= 1; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
2039 sleep_time_slave
= dib9000_fw_tune(state
->fe
[index_frontend
]);
2040 if (sleep_time
== FE_CALLBACK_TIME_NEVER
)
2041 sleep_time
= sleep_time_slave
;
2042 else if ((sleep_time_slave
!= FE_CALLBACK_TIME_NEVER
) && (sleep_time_slave
> sleep_time
))
2043 sleep_time
= sleep_time_slave
;
2045 if (sleep_time
!= FE_CALLBACK_TIME_NEVER
)
2046 msleep(sleep_time
/ 10);
2052 index_frontend_success
= 0;
2053 for (index_frontend
= 0; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
2054 frontend_status
= -dib9000_get_status(state
->fe
[index_frontend
]);
2055 if (frontend_status
> -FE_STATUS_TUNE_PENDING
) {
2056 exit_condition
= 2; /* tune success */
2057 index_frontend_success
= index_frontend
;
2060 if (frontend_status
== -FE_STATUS_TUNE_PENDING
)
2061 nbr_pending
++; /* some frontends are still tuning */
2063 if ((exit_condition
!= 2) && (nbr_pending
== 0))
2064 exit_condition
= 1; /* if all tune are done and no success, exit: tune failed */
2066 } while (exit_condition
== 0);
2068 /* check the tune result */
2069 if (exit_condition
== 1) { /* tune failed */
2070 dprintk("tune failed");
2071 mutex_unlock(&state
->demod_lock
);
2072 /* tune failed; put all the pid filtering cmd to junk */
2073 state
->pid_ctrl_index
= -1;
2077 dprintk("tune success on frontend%i", index_frontend_success
);
2079 /* synchronize all the channel cache */
2080 state
->get_frontend_internal
= 1;
2081 dib9000_get_frontend(state
->fe
[0]);
2082 state
->get_frontend_internal
= 0;
2084 /* retune the other frontends with the found channel */
2085 channel_status
.status
= CHANNEL_STATUS_PARAMETERS_SET
;
2086 for (index_frontend
= 0; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
2087 /* only retune the frontends which was not tuned success */
2088 if (index_frontend
!= index_frontend_success
) {
2089 dib9000_set_channel_status(state
->fe
[index_frontend
], &channel_status
);
2090 dib9000_set_tune_state(state
->fe
[index_frontend
], CT_DEMOD_START
);
2094 sleep_time
= FE_CALLBACK_TIME_NEVER
;
2095 for (index_frontend
= 0; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
2096 if (index_frontend
!= index_frontend_success
) {
2097 sleep_time_slave
= dib9000_fw_tune(state
->fe
[index_frontend
]);
2098 if (sleep_time
== FE_CALLBACK_TIME_NEVER
)
2099 sleep_time
= sleep_time_slave
;
2100 else if ((sleep_time_slave
!= FE_CALLBACK_TIME_NEVER
) && (sleep_time_slave
> sleep_time
))
2101 sleep_time
= sleep_time_slave
;
2104 if (sleep_time
!= FE_CALLBACK_TIME_NEVER
)
2105 msleep(sleep_time
/ 10);
2110 for (index_frontend
= 0; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
2111 if (index_frontend
!= index_frontend_success
) {
2112 frontend_status
= -dib9000_get_status(state
->fe
[index_frontend
]);
2113 if ((index_frontend
!= index_frontend_success
) && (frontend_status
== -FE_STATUS_TUNE_PENDING
))
2114 nbr_pending
++; /* some frontends are still tuning */
2117 } while (nbr_pending
!= 0);
2119 /* set the output mode */
2120 dib9000_fw_set_output_mode(state
->fe
[0], state
->chip
.d9
.cfg
.output_mode
);
2121 for (index_frontend
= 1; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++)
2122 dib9000_fw_set_output_mode(state
->fe
[index_frontend
], OUTMODE_DIVERSITY
);
2124 /* turn off the diversity for the last frontend */
2125 dib9000_fw_set_diversity_in(state
->fe
[index_frontend
- 1], 0);
2127 mutex_unlock(&state
->demod_lock
);
2128 if (state
->pid_ctrl_index
>= 0) {
2129 u8 index_pid_filter_cmd
;
2130 u8 pid_ctrl_index
= state
->pid_ctrl_index
;
2132 state
->pid_ctrl_index
= -2;
2133 for (index_pid_filter_cmd
= 0;
2134 index_pid_filter_cmd
<= pid_ctrl_index
;
2135 index_pid_filter_cmd
++) {
2136 if (state
->pid_ctrl
[index_pid_filter_cmd
].cmd
== DIB9000_PID_FILTER_CTRL
)
2137 dib9000_fw_pid_filter_ctrl(state
->fe
[0],
2138 state
->pid_ctrl
[index_pid_filter_cmd
].onoff
);
2139 else if (state
->pid_ctrl
[index_pid_filter_cmd
].cmd
== DIB9000_PID_FILTER
)
2140 dib9000_fw_pid_filter(state
->fe
[0],
2141 state
->pid_ctrl
[index_pid_filter_cmd
].id
,
2142 state
->pid_ctrl
[index_pid_filter_cmd
].pid
,
2143 state
->pid_ctrl
[index_pid_filter_cmd
].onoff
);
2146 /* do not postpone any more the pid filtering */
2147 state
->pid_ctrl_index
= -2;
2152 static u16
dib9000_read_lock(struct dvb_frontend
*fe
)
2154 struct dib9000_state
*state
= fe
->demodulator_priv
;
2156 return dib9000_read_word(state
, 535);
2159 static int dib9000_read_status(struct dvb_frontend
*fe
, fe_status_t
* stat
)
2161 struct dib9000_state
*state
= fe
->demodulator_priv
;
2163 u16 lock
= 0, lock_slave
= 0;
2165 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
2166 dprintk("could not get the lock");
2169 for (index_frontend
= 1; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++)
2170 lock_slave
|= dib9000_read_lock(state
->fe
[index_frontend
]);
2172 lock
= dib9000_read_word(state
, 535);
2176 if ((lock
& 0x8000) || (lock_slave
& 0x8000))
2177 *stat
|= FE_HAS_SIGNAL
;
2178 if ((lock
& 0x3000) || (lock_slave
& 0x3000))
2179 *stat
|= FE_HAS_CARRIER
;
2180 if ((lock
& 0x0100) || (lock_slave
& 0x0100))
2181 *stat
|= FE_HAS_VITERBI
;
2182 if (((lock
& 0x0038) == 0x38) || ((lock_slave
& 0x0038) == 0x38))
2183 *stat
|= FE_HAS_SYNC
;
2184 if ((lock
& 0x0008) || (lock_slave
& 0x0008))
2185 *stat
|= FE_HAS_LOCK
;
2187 mutex_unlock(&state
->demod_lock
);
2192 static int dib9000_read_ber(struct dvb_frontend
*fe
, u32
* ber
)
2194 struct dib9000_state
*state
= fe
->demodulator_priv
;
2198 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
2199 dprintk("could not get the lock");
2202 if (mutex_lock_interruptible(&state
->platform
.risc
.mem_mbx_lock
) < 0) {
2203 dprintk("could not get the lock");
2207 if (dib9000_fw_memmbx_sync(state
, FE_SYNC_CHANNEL
) < 0) {
2208 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
2212 dib9000_risc_mem_read(state
, FE_MM_R_FE_MONITOR
,
2213 state
->i2c_read_buffer
, 16 * 2);
2214 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
2216 c
= (u16
*)state
->i2c_read_buffer
;
2218 *ber
= c
[10] << 16 | c
[11];
2221 mutex_unlock(&state
->demod_lock
);
2225 static int dib9000_read_signal_strength(struct dvb_frontend
*fe
, u16
* strength
)
2227 struct dib9000_state
*state
= fe
->demodulator_priv
;
2229 u16
*c
= (u16
*)state
->i2c_read_buffer
;
2233 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
2234 dprintk("could not get the lock");
2238 for (index_frontend
= 1; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++) {
2239 state
->fe
[index_frontend
]->ops
.read_signal_strength(state
->fe
[index_frontend
], &val
);
2240 if (val
> 65535 - *strength
)
2246 if (mutex_lock_interruptible(&state
->platform
.risc
.mem_mbx_lock
) < 0) {
2247 dprintk("could not get the lock");
2251 if (dib9000_fw_memmbx_sync(state
, FE_SYNC_CHANNEL
) < 0) {
2252 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
2256 dib9000_risc_mem_read(state
, FE_MM_R_FE_MONITOR
, (u8
*) c
, 16 * 2);
2257 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
2260 if (val
> 65535 - *strength
)
2266 mutex_unlock(&state
->demod_lock
);
2270 static u32
dib9000_get_snr(struct dvb_frontend
*fe
)
2272 struct dib9000_state
*state
= fe
->demodulator_priv
;
2273 u16
*c
= (u16
*)state
->i2c_read_buffer
;
2277 if (mutex_lock_interruptible(&state
->platform
.risc
.mem_mbx_lock
) < 0) {
2278 dprintk("could not get the lock");
2281 if (dib9000_fw_memmbx_sync(state
, FE_SYNC_CHANNEL
) < 0) {
2282 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
2285 dib9000_risc_mem_read(state
, FE_MM_R_FE_MONITOR
, (u8
*) c
, 16 * 2);
2286 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
2289 n
= (val
>> 4) & 0xff;
2290 exp
= ((val
& 0xf) << 2);
2292 exp
+= ((val
>> 14) & 0x3);
2293 if ((exp
& 0x20) != 0)
2297 s
= (val
>> 6) & 0xFF;
2299 if ((exp
& 0x20) != 0)
2304 u32 t
= (s
/ n
) << 16;
2305 return t
+ ((s
<< 16) - n
* t
) / n
;
2310 static int dib9000_read_snr(struct dvb_frontend
*fe
, u16
* snr
)
2312 struct dib9000_state
*state
= fe
->demodulator_priv
;
2316 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
2317 dprintk("could not get the lock");
2320 snr_master
= dib9000_get_snr(fe
);
2321 for (index_frontend
= 1; (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
); index_frontend
++)
2322 snr_master
+= dib9000_get_snr(state
->fe
[index_frontend
]);
2324 if ((snr_master
>> 16) != 0) {
2325 snr_master
= 10 * intlog10(snr_master
>> 16);
2326 *snr
= snr_master
/ ((1 << 24) / 10);
2330 mutex_unlock(&state
->demod_lock
);
2335 static int dib9000_read_unc_blocks(struct dvb_frontend
*fe
, u32
* unc
)
2337 struct dib9000_state
*state
= fe
->demodulator_priv
;
2338 u16
*c
= (u16
*)state
->i2c_read_buffer
;
2341 if (mutex_lock_interruptible(&state
->demod_lock
) < 0) {
2342 dprintk("could not get the lock");
2345 if (mutex_lock_interruptible(&state
->platform
.risc
.mem_mbx_lock
) < 0) {
2346 dprintk("could not get the lock");
2350 if (dib9000_fw_memmbx_sync(state
, FE_SYNC_CHANNEL
) < 0) {
2351 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
2355 dib9000_risc_mem_read(state
, FE_MM_R_FE_MONITOR
, (u8
*) c
, 16 * 2);
2356 mutex_unlock(&state
->platform
.risc
.mem_mbx_lock
);
2361 mutex_unlock(&state
->demod_lock
);
2365 int dib9000_i2c_enumeration(struct i2c_adapter
*i2c
, int no_of_demods
, u8 default_addr
, u8 first_addr
)
2369 struct i2c_device client
= {.i2c_adap
= i2c
};
2371 client
.i2c_write_buffer
= kzalloc(4 * sizeof(u8
), GFP_KERNEL
);
2372 if (!client
.i2c_write_buffer
) {
2373 dprintk("%s: not enough memory", __func__
);
2376 client
.i2c_read_buffer
= kzalloc(4 * sizeof(u8
), GFP_KERNEL
);
2377 if (!client
.i2c_read_buffer
) {
2378 dprintk("%s: not enough memory", __func__
);
2383 client
.i2c_addr
= default_addr
+ 16;
2384 dib9000_i2c_write16(&client
, 1796, 0x0);
2386 for (k
= no_of_demods
- 1; k
>= 0; k
--) {
2387 /* designated i2c address */
2388 new_addr
= first_addr
+ (k
<< 1);
2389 client
.i2c_addr
= default_addr
;
2391 dib9000_i2c_write16(&client
, 1817, 3);
2392 dib9000_i2c_write16(&client
, 1796, 0);
2393 dib9000_i2c_write16(&client
, 1227, 1);
2394 dib9000_i2c_write16(&client
, 1227, 0);
2396 client
.i2c_addr
= new_addr
;
2397 dib9000_i2c_write16(&client
, 1817, 3);
2398 dib9000_i2c_write16(&client
, 1796, 0);
2399 dib9000_i2c_write16(&client
, 1227, 1);
2400 dib9000_i2c_write16(&client
, 1227, 0);
2402 if (dib9000_identify(&client
) == 0) {
2403 client
.i2c_addr
= default_addr
;
2404 if (dib9000_identify(&client
) == 0) {
2405 dprintk("DiB9000 #%d: not identified", k
);
2411 dib9000_i2c_write16(&client
, 1795, (1 << 10) | (4 << 6));
2412 dib9000_i2c_write16(&client
, 1794, (new_addr
<< 2) | 2);
2414 dprintk("IC %d initialized (to i2c_address 0x%x)", k
, new_addr
);
2417 for (k
= 0; k
< no_of_demods
; k
++) {
2418 new_addr
= first_addr
| (k
<< 1);
2419 client
.i2c_addr
= new_addr
;
2421 dib9000_i2c_write16(&client
, 1794, (new_addr
<< 2));
2422 dib9000_i2c_write16(&client
, 1795, 0);
2426 kfree(client
.i2c_read_buffer
);
2428 kfree(client
.i2c_write_buffer
);
2432 EXPORT_SYMBOL(dib9000_i2c_enumeration
);
2434 int dib9000_set_slave_frontend(struct dvb_frontend
*fe
, struct dvb_frontend
*fe_slave
)
2436 struct dib9000_state
*state
= fe
->demodulator_priv
;
2437 u8 index_frontend
= 1;
2439 while ((index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
))
2441 if (index_frontend
< MAX_NUMBER_OF_FRONTENDS
) {
2442 dprintk("set slave fe %p to index %i", fe_slave
, index_frontend
);
2443 state
->fe
[index_frontend
] = fe_slave
;
2447 dprintk("too many slave frontend");
2450 EXPORT_SYMBOL(dib9000_set_slave_frontend
);
2452 int dib9000_remove_slave_frontend(struct dvb_frontend
*fe
)
2454 struct dib9000_state
*state
= fe
->demodulator_priv
;
2455 u8 index_frontend
= 1;
2457 while ((index_frontend
< MAX_NUMBER_OF_FRONTENDS
) && (state
->fe
[index_frontend
] != NULL
))
2459 if (index_frontend
!= 1) {
2460 dprintk("remove slave fe %p (index %i)", state
->fe
[index_frontend
- 1], index_frontend
- 1);
2461 state
->fe
[index_frontend
] = NULL
;
2465 dprintk("no frontend to be removed");
2468 EXPORT_SYMBOL(dib9000_remove_slave_frontend
);
2470 struct dvb_frontend
*dib9000_get_slave_frontend(struct dvb_frontend
*fe
, int slave_index
)
2472 struct dib9000_state
*state
= fe
->demodulator_priv
;
2474 if (slave_index
>= MAX_NUMBER_OF_FRONTENDS
)
2476 return state
->fe
[slave_index
];
2478 EXPORT_SYMBOL(dib9000_get_slave_frontend
);
2480 static struct dvb_frontend_ops dib9000_ops
;
2481 struct dvb_frontend
*dib9000_attach(struct i2c_adapter
*i2c_adap
, u8 i2c_addr
, const struct dib9000_config
*cfg
)
2483 struct dvb_frontend
*fe
;
2484 struct dib9000_state
*st
;
2485 st
= kzalloc(sizeof(struct dib9000_state
), GFP_KERNEL
);
2488 fe
= kzalloc(sizeof(struct dvb_frontend
), GFP_KERNEL
);
2494 memcpy(&st
->chip
.d9
.cfg
, cfg
, sizeof(struct dib9000_config
));
2495 st
->i2c
.i2c_adap
= i2c_adap
;
2496 st
->i2c
.i2c_addr
= i2c_addr
;
2497 st
->i2c
.i2c_write_buffer
= st
->i2c_write_buffer
;
2498 st
->i2c
.i2c_read_buffer
= st
->i2c_read_buffer
;
2500 st
->gpio_dir
= DIB9000_GPIO_DEFAULT_DIRECTIONS
;
2501 st
->gpio_val
= DIB9000_GPIO_DEFAULT_VALUES
;
2502 st
->gpio_pwm_pos
= DIB9000_GPIO_DEFAULT_PWM_POS
;
2504 mutex_init(&st
->platform
.risc
.mbx_if_lock
);
2505 mutex_init(&st
->platform
.risc
.mbx_lock
);
2506 mutex_init(&st
->platform
.risc
.mem_lock
);
2507 mutex_init(&st
->platform
.risc
.mem_mbx_lock
);
2508 mutex_init(&st
->demod_lock
);
2509 st
->get_frontend_internal
= 0;
2511 st
->pid_ctrl_index
= -2;
2514 fe
->demodulator_priv
= st
;
2515 memcpy(&st
->fe
[0]->ops
, &dib9000_ops
, sizeof(struct dvb_frontend_ops
));
2517 /* Ensure the output mode remains at the previous default if it's
2518 * not specifically set by the caller.
2520 if ((st
->chip
.d9
.cfg
.output_mode
!= OUTMODE_MPEG2_SERIAL
) && (st
->chip
.d9
.cfg
.output_mode
!= OUTMODE_MPEG2_PAR_GATED_CLK
))
2521 st
->chip
.d9
.cfg
.output_mode
= OUTMODE_MPEG2_FIFO
;
2523 if (dib9000_identify(&st
->i2c
) == 0)
2526 dibx000_init_i2c_master(&st
->i2c_master
, DIB7000MC
, st
->i2c
.i2c_adap
, st
->i2c
.i2c_addr
);
2528 st
->tuner_adap
.dev
.parent
= i2c_adap
->dev
.parent
;
2529 strncpy(st
->tuner_adap
.name
, "DIB9000_FW TUNER ACCESS", sizeof(st
->tuner_adap
.name
));
2530 st
->tuner_adap
.algo
= &dib9000_tuner_algo
;
2531 st
->tuner_adap
.algo_data
= NULL
;
2532 i2c_set_adapdata(&st
->tuner_adap
, st
);
2533 if (i2c_add_adapter(&st
->tuner_adap
) < 0)
2536 st
->component_bus
.dev
.parent
= i2c_adap
->dev
.parent
;
2537 strncpy(st
->component_bus
.name
, "DIB9000_FW COMPONENT BUS ACCESS", sizeof(st
->component_bus
.name
));
2538 st
->component_bus
.algo
= &dib9000_component_bus_algo
;
2539 st
->component_bus
.algo_data
= NULL
;
2540 st
->component_bus_speed
= 340;
2541 i2c_set_adapdata(&st
->component_bus
, st
);
2542 if (i2c_add_adapter(&st
->component_bus
) < 0)
2543 goto component_bus_add_error
;
2545 dib9000_fw_reset(fe
);
2549 component_bus_add_error
:
2550 i2c_del_adapter(&st
->tuner_adap
);
2555 EXPORT_SYMBOL(dib9000_attach
);
2557 static struct dvb_frontend_ops dib9000_ops
= {
2558 .delsys
= { SYS_DVBT
},
2560 .name
= "DiBcom 9000",
2561 .frequency_min
= 44250000,
2562 .frequency_max
= 867250000,
2563 .frequency_stepsize
= 62500,
2564 .caps
= FE_CAN_INVERSION_AUTO
|
2565 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
2566 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
2567 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
| FE_CAN_QAM_AUTO
|
2568 FE_CAN_TRANSMISSION_MODE_AUTO
| FE_CAN_GUARD_INTERVAL_AUTO
| FE_CAN_RECOVER
| FE_CAN_HIERARCHY_AUTO
,
2571 .release
= dib9000_release
,
2573 .init
= dib9000_wakeup
,
2574 .sleep
= dib9000_sleep
,
2576 .set_frontend
= dib9000_set_frontend
,
2577 .get_tune_settings
= dib9000_fe_get_tune_settings
,
2578 .get_frontend
= dib9000_get_frontend
,
2580 .read_status
= dib9000_read_status
,
2581 .read_ber
= dib9000_read_ber
,
2582 .read_signal_strength
= dib9000_read_signal_strength
,
2583 .read_snr
= dib9000_read_snr
,
2584 .read_ucblocks
= dib9000_read_unc_blocks
,
2587 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
2588 MODULE_AUTHOR("Olivier Grenie <ogrenie@dibcom.fr>");
2589 MODULE_DESCRIPTION("Driver for the DiBcom 9000 COFDM demodulator");
2590 MODULE_LICENSE("GPL");