diff options
-rw-r--r-- | drivers/media/video/cx25840/cx25840-audio.c | 369 | ||||
-rw-r--r-- | drivers/media/video/cx25840/cx25840-core.c | 1024 | ||||
-rw-r--r-- | drivers/media/video/cx25840/cx25840-firmware.c | 167 | ||||
-rw-r--r-- | drivers/media/video/cx25840/cx25840-vbi.c | 315 | ||||
-rw-r--r-- | drivers/media/video/cx25840/cx25840.h | 85 |
5 files changed, 1960 insertions, 0 deletions
diff --git a/drivers/media/video/cx25840/cx25840-audio.c b/drivers/media/video/cx25840/cx25840-audio.c new file mode 100644 index 000000000000..3905580b1946 --- /dev/null +++ b/drivers/media/video/cx25840/cx25840-audio.c | |||
@@ -0,0 +1,369 @@ | |||
1 | /* cx25840 audio functions | ||
2 | * | ||
3 | * This program is free software; you can redistribute it and/or | ||
4 | * modify it under the terms of the GNU General Public License | ||
5 | * as published by the Free Software Foundation; either version 2 | ||
6 | * of the License, or (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program; if not, write to the Free Software | ||
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
16 | */ | ||
17 | |||
18 | |||
19 | #include <linux/videodev2.h> | ||
20 | #include <linux/i2c.h> | ||
21 | #include <media/audiochip.h> | ||
22 | #include <media/i2c-compat.h> | ||
23 | #include <media/v4l2-common.h> | ||
24 | |||
25 | #include "cx25840.h" | ||
26 | |||
27 | inline static int set_audclk_freq(struct i2c_client *client, | ||
28 | enum v4l2_audio_clock_freq freq) | ||
29 | { | ||
30 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
31 | |||
32 | /* assert soft reset */ | ||
33 | cx25840_and_or(client, 0x810, ~0x1, 0x01); | ||
34 | |||
35 | /* common for all inputs and rates */ | ||
36 | /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */ | ||
37 | cx25840_write(client, 0x127, 0x50); | ||
38 | |||
39 | switch (state->audio_input) { | ||
40 | case AUDIO_TUNER: | ||
41 | switch (freq) { | ||
42 | case V4L2_AUDCLK_32_KHZ: | ||
43 | /* VID_PLL and AUX_PLL */ | ||
44 | cx25840_write4(client, 0x108, 0x0f040610); | ||
45 | |||
46 | /* AUX_PLL_FRAC */ | ||
47 | cx25840_write4(client, 0x110, 0xee39bb01); | ||
48 | |||
49 | /* src3/4/6_ctl = 0x0801f77f */ | ||
50 | cx25840_write4(client, 0x900, 0x7ff70108); | ||
51 | cx25840_write4(client, 0x904, 0x7ff70108); | ||
52 | cx25840_write4(client, 0x90c, 0x7ff70108); | ||
53 | break; | ||
54 | |||
55 | case V4L2_AUDCLK_441_KHZ: | ||
56 | /* VID_PLL and AUX_PLL */ | ||
57 | cx25840_write4(client, 0x108, 0x0f040910); | ||
58 | |||
59 | /* AUX_PLL_FRAC */ | ||
60 | cx25840_write4(client, 0x110, 0xd66bec00); | ||
61 | |||
62 | /* src3/4/6_ctl = 0x08016d59 */ | ||
63 | cx25840_write4(client, 0x900, 0x596d0108); | ||
64 | cx25840_write4(client, 0x904, 0x596d0108); | ||
65 | cx25840_write4(client, 0x90c, 0x596d0108); | ||
66 | break; | ||
67 | |||
68 | case V4L2_AUDCLK_48_KHZ: | ||
69 | /* VID_PLL and AUX_PLL */ | ||
70 | cx25840_write4(client, 0x108, 0x0f040a10); | ||
71 | |||
72 | /* AUX_PLL_FRAC */ | ||
73 | cx25840_write4(client, 0x110, 0xe5d69800); | ||
74 | |||
75 | /* src3/4/6_ctl = 0x08014faa */ | ||
76 | cx25840_write4(client, 0x900, 0xaa4f0108); | ||
77 | cx25840_write4(client, 0x904, 0xaa4f0108); | ||
78 | cx25840_write4(client, 0x90c, 0xaa4f0108); | ||
79 | break; | ||
80 | } | ||
81 | break; | ||
82 | |||
83 | case AUDIO_EXTERN_1: | ||
84 | case AUDIO_EXTERN_2: | ||
85 | case AUDIO_INTERN: | ||
86 | case AUDIO_RADIO: | ||
87 | switch (freq) { | ||
88 | case V4L2_AUDCLK_32_KHZ: | ||
89 | /* VID_PLL and AUX_PLL */ | ||
90 | cx25840_write4(client, 0x108, 0x0f04081e); | ||
91 | |||
92 | /* AUX_PLL_FRAC */ | ||
93 | cx25840_write4(client, 0x110, 0x69082a01); | ||
94 | |||
95 | /* src1_ctl = 0x08010000 */ | ||
96 | cx25840_write4(client, 0x8f8, 0x00000108); | ||
97 | |||
98 | /* src3/4/6_ctl = 0x08020000 */ | ||
99 | cx25840_write4(client, 0x900, 0x00000208); | ||
100 | cx25840_write4(client, 0x904, 0x00000208); | ||
101 | cx25840_write4(client, 0x90c, 0x00000208); | ||
102 | |||
103 | /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */ | ||
104 | cx25840_write(client, 0x127, 0x54); | ||
105 | break; | ||
106 | |||
107 | case V4L2_AUDCLK_441_KHZ: | ||
108 | /* VID_PLL and AUX_PLL */ | ||
109 | cx25840_write4(client, 0x108, 0x0f040918); | ||
110 | |||
111 | /* AUX_PLL_FRAC */ | ||
112 | cx25840_write4(client, 0x110, 0xd66bec00); | ||
113 | |||
114 | /* src1_ctl = 0x08010000 */ | ||
115 | cx25840_write4(client, 0x8f8, 0xcd600108); | ||
116 | |||
117 | /* src3/4/6_ctl = 0x08020000 */ | ||
118 | cx25840_write4(client, 0x900, 0x85730108); | ||
119 | cx25840_write4(client, 0x904, 0x85730108); | ||
120 | cx25840_write4(client, 0x90c, 0x85730108); | ||
121 | break; | ||
122 | |||
123 | case V4L2_AUDCLK_48_KHZ: | ||
124 | /* VID_PLL and AUX_PLL */ | ||
125 | cx25840_write4(client, 0x108, 0x0f040a18); | ||
126 | |||
127 | /* AUX_PLL_FRAC */ | ||
128 | cx25840_write4(client, 0x110, 0xe5d69800); | ||
129 | |||
130 | /* src1_ctl = 0x08010000 */ | ||
131 | cx25840_write4(client, 0x8f8, 0x00800108); | ||
132 | |||
133 | /* src3/4/6_ctl = 0x08020000 */ | ||
134 | cx25840_write4(client, 0x900, 0x55550108); | ||
135 | cx25840_write4(client, 0x904, 0x55550108); | ||
136 | cx25840_write4(client, 0x90c, 0x55550108); | ||
137 | break; | ||
138 | } | ||
139 | break; | ||
140 | } | ||
141 | |||
142 | /* deassert soft reset */ | ||
143 | cx25840_and_or(client, 0x810, ~0x1, 0x00); | ||
144 | |||
145 | state->audclk_freq = freq; | ||
146 | |||
147 | return 0; | ||
148 | } | ||
149 | |||
150 | static int set_input(struct i2c_client *client, int audio_input) | ||
151 | { | ||
152 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
153 | |||
154 | cx25840_dbg("set audio input (%d)\n", audio_input); | ||
155 | |||
156 | /* stop microcontroller */ | ||
157 | cx25840_and_or(client, 0x803, ~0x10, 0); | ||
158 | |||
159 | /* Mute everything to prevent the PFFT! */ | ||
160 | cx25840_write(client, 0x8d3, 0x1f); | ||
161 | |||
162 | switch (audio_input) { | ||
163 | case AUDIO_TUNER: | ||
164 | /* Set Path1 to Analog Demod Main Channel */ | ||
165 | cx25840_write4(client, 0x8d0, 0x7038061f); | ||
166 | |||
167 | /* When the microcontroller detects the | ||
168 | * audio format, it will unmute the lines */ | ||
169 | cx25840_and_or(client, 0x803, ~0x10, 0x10); | ||
170 | break; | ||
171 | |||
172 | case AUDIO_EXTERN_1: | ||
173 | case AUDIO_EXTERN_2: | ||
174 | case AUDIO_INTERN: | ||
175 | case AUDIO_RADIO: | ||
176 | /* Set Path1 to Serial Audio Input */ | ||
177 | cx25840_write4(client, 0x8d0, 0x12100101); | ||
178 | |||
179 | /* The microcontroller should not be started for the | ||
180 | * non-tuner inputs: autodetection is specific for | ||
181 | * TV audio. */ | ||
182 | break; | ||
183 | |||
184 | default: | ||
185 | cx25840_dbg("Invalid audio input selection %d\n", audio_input); | ||
186 | return -EINVAL; | ||
187 | } | ||
188 | |||
189 | state->audio_input = audio_input; | ||
190 | |||
191 | return set_audclk_freq(client, state->audclk_freq); | ||
192 | } | ||
193 | |||
194 | inline static int get_volume(struct i2c_client *client) | ||
195 | { | ||
196 | /* Volume runs +18dB to -96dB in 1/2dB steps | ||
197 | * change to fit the msp3400 -114dB to +12dB range */ | ||
198 | |||
199 | /* check PATH1_VOLUME */ | ||
200 | int vol = 228 - cx25840_read(client, 0x8d4); | ||
201 | vol = (vol / 2) + 23; | ||
202 | return vol << 9; | ||
203 | } | ||
204 | |||
205 | inline static void set_volume(struct i2c_client *client, int volume) | ||
206 | { | ||
207 | /* First convert the volume to msp3400 values (0-127) */ | ||
208 | int vol = volume >> 9; | ||
209 | /* now scale it up to cx25840 values | ||
210 | * -114dB to -96dB maps to 0 | ||
211 | * this should be 19, but in my testing that was 4dB too loud */ | ||
212 | if (vol <= 23) { | ||
213 | vol = 0; | ||
214 | } else { | ||
215 | vol -= 23; | ||
216 | } | ||
217 | |||
218 | /* PATH1_VOLUME */ | ||
219 | cx25840_write(client, 0x8d4, 228 - (vol * 2)); | ||
220 | } | ||
221 | |||
222 | inline static int get_bass(struct i2c_client *client) | ||
223 | { | ||
224 | /* bass is 49 steps +12dB to -12dB */ | ||
225 | |||
226 | /* check PATH1_EQ_BASS_VOL */ | ||
227 | int bass = cx25840_read(client, 0x8d9) & 0x3f; | ||
228 | bass = (((48 - bass) * 0xffff) + 47) / 48; | ||
229 | return bass; | ||
230 | } | ||
231 | |||
232 | inline static void set_bass(struct i2c_client *client, int bass) | ||
233 | { | ||
234 | /* PATH1_EQ_BASS_VOL */ | ||
235 | cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff)); | ||
236 | } | ||
237 | |||
238 | inline static int get_treble(struct i2c_client *client) | ||
239 | { | ||
240 | /* treble is 49 steps +12dB to -12dB */ | ||
241 | |||
242 | /* check PATH1_EQ_TREBLE_VOL */ | ||
243 | int treble = cx25840_read(client, 0x8db) & 0x3f; | ||
244 | treble = (((48 - treble) * 0xffff) + 47) / 48; | ||
245 | return treble; | ||
246 | } | ||
247 | |||
248 | inline static void set_treble(struct i2c_client *client, int treble) | ||
249 | { | ||
250 | /* PATH1_EQ_TREBLE_VOL */ | ||
251 | cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff)); | ||
252 | } | ||
253 | |||
254 | inline static int get_balance(struct i2c_client *client) | ||
255 | { | ||
256 | /* balance is 7 bit, 0 to -96dB */ | ||
257 | |||
258 | /* check PATH1_BAL_LEVEL */ | ||
259 | int balance = cx25840_read(client, 0x8d5) & 0x7f; | ||
260 | /* check PATH1_BAL_LEFT */ | ||
261 | if ((cx25840_read(client, 0x8d5) & 0x80) == 0) | ||
262 | balance = 0x80 - balance; | ||
263 | else | ||
264 | balance = 0x80 + balance; | ||
265 | return balance << 8; | ||
266 | } | ||
267 | |||
268 | inline static void set_balance(struct i2c_client *client, int balance) | ||
269 | { | ||
270 | int bal = balance >> 8; | ||
271 | if (bal > 0x80) { | ||
272 | /* PATH1_BAL_LEFT */ | ||
273 | cx25840_and_or(client, 0x8d5, 0x7f, 0x80); | ||
274 | /* PATH1_BAL_LEVEL */ | ||
275 | cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f); | ||
276 | } else { | ||
277 | /* PATH1_BAL_LEFT */ | ||
278 | cx25840_and_or(client, 0x8d5, 0x7f, 0x00); | ||
279 | /* PATH1_BAL_LEVEL */ | ||
280 | cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal); | ||
281 | } | ||
282 | } | ||
283 | |||
284 | inline static int get_mute(struct i2c_client *client) | ||
285 | { | ||
286 | /* check SRC1_MUTE_EN */ | ||
287 | return cx25840_read(client, 0x8d3) & 0x2 ? 1 : 0; | ||
288 | } | ||
289 | |||
290 | inline static void set_mute(struct i2c_client *client, int mute) | ||
291 | { | ||
292 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
293 | |||
294 | if (state->audio_input == AUDIO_TUNER) { | ||
295 | /* Must turn off microcontroller in order to mute sound. | ||
296 | * Not sure if this is the best method, but it does work. | ||
297 | * If the microcontroller is running, then it will undo any | ||
298 | * changes to the mute register. */ | ||
299 | if (mute) { | ||
300 | /* disable microcontroller */ | ||
301 | cx25840_and_or(client, 0x803, ~0x10, 0x00); | ||
302 | cx25840_write(client, 0x8d3, 0x1f); | ||
303 | } else { | ||
304 | /* enable microcontroller */ | ||
305 | cx25840_and_or(client, 0x803, ~0x10, 0x10); | ||
306 | } | ||
307 | } else { | ||
308 | /* SRC1_MUTE_EN */ | ||
309 | cx25840_and_or(client, 0x8d3, ~0x2, mute ? 0x02 : 0x00); | ||
310 | } | ||
311 | } | ||
312 | |||
313 | int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg) | ||
314 | { | ||
315 | struct v4l2_control *ctrl = arg; | ||
316 | |||
317 | switch (cmd) { | ||
318 | case AUDC_SET_INPUT: | ||
319 | return set_input(client, *(int *)arg); | ||
320 | case VIDIOC_INT_AUDIO_CLOCK_FREQ: | ||
321 | return set_audclk_freq(client, *(enum v4l2_audio_clock_freq *)arg); | ||
322 | case VIDIOC_G_CTRL: | ||
323 | switch (ctrl->id) { | ||
324 | case V4L2_CID_AUDIO_VOLUME: | ||
325 | ctrl->value = get_volume(client); | ||
326 | break; | ||
327 | case V4L2_CID_AUDIO_BASS: | ||
328 | ctrl->value = get_bass(client); | ||
329 | break; | ||
330 | case V4L2_CID_AUDIO_TREBLE: | ||
331 | ctrl->value = get_treble(client); | ||
332 | break; | ||
333 | case V4L2_CID_AUDIO_BALANCE: | ||
334 | ctrl->value = get_balance(client); | ||
335 | break; | ||
336 | case V4L2_CID_AUDIO_MUTE: | ||
337 | ctrl->value = get_mute(client); | ||
338 | break; | ||
339 | default: | ||
340 | return -EINVAL; | ||
341 | } | ||
342 | break; | ||
343 | case VIDIOC_S_CTRL: | ||
344 | switch (ctrl->id) { | ||
345 | case V4L2_CID_AUDIO_VOLUME: | ||
346 | set_volume(client, ctrl->value); | ||
347 | break; | ||
348 | case V4L2_CID_AUDIO_BASS: | ||
349 | set_bass(client, ctrl->value); | ||
350 | break; | ||
351 | case V4L2_CID_AUDIO_TREBLE: | ||
352 | set_treble(client, ctrl->value); | ||
353 | break; | ||
354 | case V4L2_CID_AUDIO_BALANCE: | ||
355 | set_balance(client, ctrl->value); | ||
356 | break; | ||
357 | case V4L2_CID_AUDIO_MUTE: | ||
358 | set_mute(client, ctrl->value); | ||
359 | break; | ||
360 | default: | ||
361 | return -EINVAL; | ||
362 | } | ||
363 | break; | ||
364 | default: | ||
365 | return -EINVAL; | ||
366 | } | ||
367 | |||
368 | return 0; | ||
369 | } | ||
diff --git a/drivers/media/video/cx25840/cx25840-core.c b/drivers/media/video/cx25840/cx25840-core.c new file mode 100644 index 000000000000..805273e5f919 --- /dev/null +++ b/drivers/media/video/cx25840/cx25840-core.c | |||
@@ -0,0 +1,1024 @@ | |||
1 | /* cx25840 - Conexant CX25840 audio/video decoder driver | ||
2 | * | ||
3 | * Copyright (C) 2004 Ulf Eklund | ||
4 | * | ||
5 | * Based on the saa7115 driver and on the first verison of Chris Kennedy's | ||
6 | * cx25840 driver. | ||
7 | * | ||
8 | * Changes by Tyler Trafford <tatrafford@comcast.net> | ||
9 | * - cleanup/rewrite for V4L2 API (2005) | ||
10 | * | ||
11 | * VBI support by Hans Verkuil <hverkuil@xs4all.nl>. | ||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or | ||
14 | * modify it under the terms of the GNU General Public License | ||
15 | * as published by the Free Software Foundation; either version 2 | ||
16 | * of the License, or (at your option) any later version. | ||
17 | * | ||
18 | * This program is distributed in the hope that it will be useful, | ||
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
21 | * GNU General Public License for more details. | ||
22 | * | ||
23 | * You should have received a copy of the GNU General Public License | ||
24 | * along with this program; if not, write to the Free Software | ||
25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
26 | */ | ||
27 | |||
28 | |||
29 | #include <linux/kernel.h> | ||
30 | #include <linux/module.h> | ||
31 | #include <linux/slab.h> | ||
32 | #include <linux/videodev2.h> | ||
33 | #include <linux/i2c.h> | ||
34 | #include <media/audiochip.h> | ||
35 | #include <media/i2c-compat.h> | ||
36 | #include <media/v4l2-common.h> | ||
37 | |||
38 | #include "cx25840.h" | ||
39 | |||
40 | MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver"); | ||
41 | MODULE_AUTHOR("Ulf Eklund <ivtv@eklund.to>"); | ||
42 | MODULE_AUTHOR("Chris Kennedy <c@groovy.org>"); | ||
43 | MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>"); | ||
44 | MODULE_AUTHOR("Tyler Trafford <tatrafford@comcast.net>"); | ||
45 | MODULE_LICENSE("GPL"); | ||
46 | |||
47 | static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END }; | ||
48 | |||
49 | |||
50 | int cx25840_debug = 0; | ||
51 | |||
52 | module_param(cx25840_debug, bool, 0644); | ||
53 | |||
54 | MODULE_PARM_DESC(cx25840_debug, "Debugging messages [0=Off (default) 1=On]"); | ||
55 | |||
56 | I2C_CLIENT_INSMOD; | ||
57 | |||
58 | /* ----------------------------------------------------------------------- */ | ||
59 | |||
60 | int cx25840_write(struct i2c_client *client, u16 addr, u8 value) | ||
61 | { | ||
62 | u8 buffer[3]; | ||
63 | buffer[0] = addr >> 8; | ||
64 | buffer[1] = addr & 0xff; | ||
65 | buffer[2] = value; | ||
66 | return i2c_master_send(client, buffer, 3); | ||
67 | } | ||
68 | |||
69 | int cx25840_write4(struct i2c_client *client, u16 addr, u32 value) | ||
70 | { | ||
71 | u8 buffer[6]; | ||
72 | buffer[0] = addr >> 8; | ||
73 | buffer[1] = addr & 0xff; | ||
74 | buffer[2] = value >> 24; | ||
75 | buffer[3] = (value >> 16) & 0xff; | ||
76 | buffer[4] = (value >> 8) & 0xff; | ||
77 | buffer[5] = value & 0xff; | ||
78 | return i2c_master_send(client, buffer, 6); | ||
79 | } | ||
80 | |||
81 | u8 cx25840_read(struct i2c_client * client, u16 addr) | ||
82 | { | ||
83 | u8 buffer[2]; | ||
84 | buffer[0] = addr >> 8; | ||
85 | buffer[1] = addr & 0xff; | ||
86 | |||
87 | if (i2c_master_send(client, buffer, 2) < 2) | ||
88 | return 0; | ||
89 | |||
90 | if (i2c_master_recv(client, buffer, 1) < 1) | ||
91 | return 0; | ||
92 | |||
93 | return buffer[0]; | ||
94 | } | ||
95 | |||
96 | u32 cx25840_read4(struct i2c_client * client, u16 addr) | ||
97 | { | ||
98 | u8 buffer[4]; | ||
99 | buffer[0] = addr >> 8; | ||
100 | buffer[1] = addr & 0xff; | ||
101 | |||
102 | if (i2c_master_send(client, buffer, 2) < 2) | ||
103 | return 0; | ||
104 | |||
105 | if (i2c_master_recv(client, buffer, 4) < 4) | ||
106 | return 0; | ||
107 | |||
108 | return (buffer[0] << 24) | (buffer[1] << 16) | | ||
109 | (buffer[2] << 8) | buffer[3]; | ||
110 | } | ||
111 | |||
112 | int cx25840_and_or(struct i2c_client *client, u16 addr, u8 and_mask, | ||
113 | u8 or_value) | ||
114 | { | ||
115 | return cx25840_write(client, addr, | ||
116 | (cx25840_read(client, addr) & and_mask) | | ||
117 | or_value); | ||
118 | } | ||
119 | |||
120 | /* ----------------------------------------------------------------------- */ | ||
121 | |||
122 | static int set_input(struct i2c_client *, enum cx25840_input); | ||
123 | static void input_change(struct i2c_client *); | ||
124 | static void log_status(struct i2c_client *client); | ||
125 | |||
126 | /* ----------------------------------------------------------------------- */ | ||
127 | |||
128 | static inline void init_dll1(struct i2c_client *client) | ||
129 | { | ||
130 | /* This is the Hauppauge sequence used to | ||
131 | * initialize the Delay Lock Loop 1 (ADC DLL). */ | ||
132 | cx25840_write(client, 0x159, 0x23); | ||
133 | cx25840_write(client, 0x15a, 0x87); | ||
134 | cx25840_write(client, 0x15b, 0x06); | ||
135 | cx25840_write(client, 0x159, 0xe1); | ||
136 | cx25840_write(client, 0x15a, 0x86); | ||
137 | cx25840_write(client, 0x159, 0xe0); | ||
138 | cx25840_write(client, 0x159, 0xe1); | ||
139 | cx25840_write(client, 0x15b, 0x10); | ||
140 | } | ||
141 | |||
142 | static inline void init_dll2(struct i2c_client *client) | ||
143 | { | ||
144 | /* This is the Hauppauge sequence used to | ||
145 | * initialize the Delay Lock Loop 2 (ADC DLL). */ | ||
146 | cx25840_write(client, 0x15d, 0xe3); | ||
147 | cx25840_write(client, 0x15e, 0x86); | ||
148 | cx25840_write(client, 0x15f, 0x06); | ||
149 | cx25840_write(client, 0x15d, 0xe1); | ||
150 | cx25840_write(client, 0x15d, 0xe0); | ||
151 | cx25840_write(client, 0x15d, 0xe1); | ||
152 | } | ||
153 | |||
154 | static void cx25840_initialize(struct i2c_client *client, int loadfw) | ||
155 | { | ||
156 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
157 | |||
158 | /* datasheet startup in numbered steps, refer to page 3-77 */ | ||
159 | /* 2. */ | ||
160 | cx25840_and_or(client, 0x803, ~0x10, 0x00); | ||
161 | /* The default of this register should be 4, but I get 0 instead. | ||
162 | * Set this register to 4 manually. */ | ||
163 | cx25840_write(client, 0x000, 0x04); | ||
164 | /* 3. */ | ||
165 | init_dll1(client); | ||
166 | init_dll2(client); | ||
167 | cx25840_write(client, 0x136, 0x0a); | ||
168 | /* 4. */ | ||
169 | cx25840_write(client, 0x13c, 0x01); | ||
170 | cx25840_write(client, 0x13c, 0x00); | ||
171 | /* 5. */ | ||
172 | if (loadfw) | ||
173 | cx25840_loadfw(client); | ||
174 | /* 6. */ | ||
175 | cx25840_write(client, 0x115, 0x8c); | ||
176 | cx25840_write(client, 0x116, 0x07); | ||
177 | cx25840_write(client, 0x118, 0x02); | ||
178 | /* 7. */ | ||
179 | cx25840_write(client, 0x4a5, 0x80); | ||
180 | cx25840_write(client, 0x4a5, 0x00); | ||
181 | cx25840_write(client, 0x402, 0x00); | ||
182 | /* 8. */ | ||
183 | cx25840_write(client, 0x401, 0x18); | ||
184 | cx25840_write(client, 0x4a2, 0x10); | ||
185 | cx25840_write(client, 0x402, 0x04); | ||
186 | /* 10. */ | ||
187 | cx25840_write(client, 0x8d3, 0x1f); | ||
188 | cx25840_write(client, 0x8e3, 0x03); | ||
189 | |||
190 | cx25840_vbi_setup(client); | ||
191 | |||
192 | /* trial and error says these are needed to get audio */ | ||
193 | cx25840_write(client, 0x914, 0xa0); | ||
194 | cx25840_write(client, 0x918, 0xa0); | ||
195 | cx25840_write(client, 0x919, 0x01); | ||
196 | |||
197 | /* stereo prefered */ | ||
198 | cx25840_write(client, 0x809, 0x04); | ||
199 | /* AC97 shift */ | ||
200 | cx25840_write(client, 0x8cf, 0x0f); | ||
201 | |||
202 | /* (re)set video input */ | ||
203 | set_input(client, state->input); | ||
204 | /* (re)set audio input */ | ||
205 | cx25840_audio(client, AUDC_SET_INPUT, &state->audio_input); | ||
206 | |||
207 | /* start microcontroller */ | ||
208 | cx25840_and_or(client, 0x803, ~0x10, 0x10); | ||
209 | } | ||
210 | |||
211 | /* ----------------------------------------------------------------------- */ | ||
212 | |||
213 | static void input_change(struct i2c_client *client) | ||
214 | { | ||
215 | v4l2_std_id std = cx25840_get_v4lstd(client); | ||
216 | |||
217 | if (std & V4L2_STD_PAL) { | ||
218 | /* Follow tuner change procedure for PAL */ | ||
219 | cx25840_write(client, 0x808, 0xff); | ||
220 | cx25840_write(client, 0x80b, 0x10); | ||
221 | } else if (std & V4L2_STD_SECAM) { | ||
222 | /* Select autodetect for SECAM */ | ||
223 | cx25840_write(client, 0x808, 0xff); | ||
224 | cx25840_write(client, 0x80b, 0x10); | ||
225 | } else if (std & V4L2_STD_NTSC) { | ||
226 | /* NTSC */ | ||
227 | cx25840_write(client, 0x808, 0xf6); | ||
228 | cx25840_write(client, 0x80b, 0x00); | ||
229 | } | ||
230 | |||
231 | if (cx25840_read(client, 0x803) & 0x10) { | ||
232 | /* restart audio decoder microcontroller */ | ||
233 | cx25840_and_or(client, 0x803, ~0x10, 0x00); | ||
234 | cx25840_and_or(client, 0x803, ~0x10, 0x10); | ||
235 | } | ||
236 | } | ||
237 | |||
238 | static int set_input(struct i2c_client *client, enum cx25840_input input) | ||
239 | { | ||
240 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
241 | |||
242 | cx25840_dbg("decoder set input (%d)\n", input); | ||
243 | |||
244 | switch (input) { | ||
245 | case CX25840_TUNER: | ||
246 | cx25840_dbg("now setting Tuner input\n"); | ||
247 | |||
248 | if (state->cardtype == CARDTYPE_PVR150) { | ||
249 | /* CH_SEL_ADC2=1 */ | ||
250 | cx25840_and_or(client, 0x102, ~0x2, 0x02); | ||
251 | } | ||
252 | |||
253 | /* Video Input Control */ | ||
254 | if (state->cardtype == CARDTYPE_PG600) { | ||
255 | cx25840_write(client, 0x103, 0x11); | ||
256 | } else { | ||
257 | cx25840_write(client, 0x103, 0x46); | ||
258 | } | ||
259 | |||
260 | /* INPUT_MODE=0 */ | ||
261 | cx25840_and_or(client, 0x401, ~0x6, 0x00); | ||
262 | break; | ||
263 | |||
264 | case CX25840_COMPOSITE0: | ||
265 | case CX25840_COMPOSITE1: | ||
266 | cx25840_dbg("now setting Composite input\n"); | ||
267 | |||
268 | /* Video Input Control */ | ||
269 | if (state->cardtype == CARDTYPE_PG600) { | ||
270 | cx25840_write(client, 0x103, 0x00); | ||
271 | } else { | ||
272 | cx25840_write(client, 0x103, 0x02); | ||
273 | } | ||
274 | |||
275 | /* INPUT_MODE=0 */ | ||
276 | cx25840_and_or(client, 0x401, ~0x6, 0x00); | ||
277 | break; | ||
278 | |||
279 | case CX25840_SVIDEO0: | ||
280 | case CX25840_SVIDEO1: | ||
281 | cx25840_dbg("now setting S-Video input\n"); | ||
282 | |||
283 | /* CH_SEL_ADC2=0 */ | ||
284 | cx25840_and_or(client, 0x102, ~0x2, 0x00); | ||
285 | |||
286 | /* Video Input Control */ | ||
287 | if (state->cardtype == CARDTYPE_PG600) { | ||
288 | cx25840_write(client, 0x103, 0x02); | ||
289 | } else { | ||
290 | cx25840_write(client, 0x103, 0x10); | ||
291 | } | ||
292 | |||
293 | /* INPUT_MODE=1 */ | ||
294 | cx25840_and_or(client, 0x401, ~0x6, 0x02); | ||
295 | break; | ||
296 | |||
297 | default: | ||
298 | cx25840_err("%d is not a valid input!\n", input); | ||
299 | return -EINVAL; | ||
300 | } | ||
301 | |||
302 | state->input = input; | ||
303 | input_change(client); | ||
304 | return 0; | ||
305 | } | ||
306 | |||
307 | /* ----------------------------------------------------------------------- */ | ||
308 | |||
309 | static int set_v4lstd(struct i2c_client *client, v4l2_std_id std) | ||
310 | { | ||
311 | u8 fmt; | ||
312 | |||
313 | switch (std) { | ||
314 | /* zero is autodetect */ | ||
315 | case 0: fmt = 0x0; break; | ||
316 | /* default ntsc to ntsc-m */ | ||
317 | case V4L2_STD_NTSC: | ||
318 | case V4L2_STD_NTSC_M: fmt = 0x1; break; | ||
319 | case V4L2_STD_NTSC_M_JP: fmt = 0x2; break; | ||
320 | case V4L2_STD_NTSC_443: fmt = 0x3; break; | ||
321 | case V4L2_STD_PAL: fmt = 0x4; break; | ||
322 | case V4L2_STD_PAL_M: fmt = 0x5; break; | ||
323 | case V4L2_STD_PAL_N: fmt = 0x6; break; | ||
324 | case V4L2_STD_PAL_Nc: fmt = 0x7; break; | ||
325 | case V4L2_STD_PAL_60: fmt = 0x8; break; | ||
326 | case V4L2_STD_SECAM: fmt = 0xc; break; | ||
327 | default: | ||
328 | return -ERANGE; | ||
329 | } | ||
330 | |||
331 | cx25840_and_or(client, 0x400, ~0xf, fmt); | ||
332 | cx25840_vbi_setup(client); | ||
333 | return 0; | ||
334 | } | ||
335 | |||
336 | v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client) | ||
337 | { | ||
338 | /* check VID_FMT_SEL first */ | ||
339 | u8 fmt = cx25840_read(client, 0x400) & 0xf; | ||
340 | |||
341 | if (!fmt) { | ||
342 | /* check AFD_FMT_STAT if set to autodetect */ | ||
343 | fmt = cx25840_read(client, 0x40d) & 0xf; | ||
344 | } | ||
345 | |||
346 | switch (fmt) { | ||
347 | case 0x1: return V4L2_STD_NTSC_M; | ||
348 | case 0x2: return V4L2_STD_NTSC_M_JP; | ||
349 | case 0x3: return V4L2_STD_NTSC_443; | ||
350 | case 0x4: return V4L2_STD_PAL; | ||
351 | case 0x5: return V4L2_STD_PAL_M; | ||
352 | case 0x6: return V4L2_STD_PAL_N; | ||
353 | case 0x7: return V4L2_STD_PAL_Nc; | ||
354 | case 0x8: return V4L2_STD_PAL_60; | ||
355 | case 0xc: return V4L2_STD_SECAM; | ||
356 | default: return V4L2_STD_UNKNOWN; | ||
357 | } | ||
358 | } | ||
359 | |||
360 | /* ----------------------------------------------------------------------- */ | ||
361 | |||
362 | static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl) | ||
363 | { | ||
364 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
365 | |||
366 | switch (ctrl->id) { | ||
367 | case CX25840_CID_CARDTYPE: | ||
368 | switch (ctrl->value) { | ||
369 | case CARDTYPE_PVR150: | ||
370 | case CARDTYPE_PG600: | ||
371 | state->cardtype = ctrl->value; | ||
372 | break; | ||
373 | default: | ||
374 | return -ERANGE; | ||
375 | } | ||
376 | |||
377 | set_input(client, state->input); | ||
378 | break; | ||
379 | |||
380 | case V4L2_CID_BRIGHTNESS: | ||
381 | if (ctrl->value < 0 || ctrl->value > 255) { | ||
382 | cx25840_err("invalid brightness setting %d\n", | ||
383 | ctrl->value); | ||
384 | return -ERANGE; | ||
385 | } | ||
386 | |||
387 | cx25840_write(client, 0x414, ctrl->value - 128); | ||
388 | break; | ||
389 | |||
390 | case V4L2_CID_CONTRAST: | ||
391 | if (ctrl->value < 0 || ctrl->value > 127) { | ||
392 | cx25840_err("invalid contrast setting %d\n", | ||
393 | ctrl->value); | ||
394 | return -ERANGE; | ||
395 | } | ||
396 | |||
397 | cx25840_write(client, 0x415, ctrl->value << 1); | ||
398 | break; | ||
399 | |||
400 | case V4L2_CID_SATURATION: | ||
401 | if (ctrl->value < 0 || ctrl->value > 127) { | ||
402 | cx25840_err("invalid saturation setting %d\n", | ||
403 | ctrl->value); | ||
404 | return -ERANGE; | ||
405 | } | ||
406 | |||
407 | cx25840_write(client, 0x420, ctrl->value << 1); | ||
408 | cx25840_write(client, 0x421, ctrl->value << 1); | ||
409 | break; | ||
410 | |||
411 | case V4L2_CID_HUE: | ||
412 | if (ctrl->value < -127 || ctrl->value > 127) { | ||
413 | cx25840_err("invalid hue setting %d\n", ctrl->value); | ||
414 | return -ERANGE; | ||
415 | } | ||
416 | |||
417 | cx25840_write(client, 0x422, ctrl->value); | ||
418 | break; | ||
419 | |||
420 | case V4L2_CID_AUDIO_VOLUME: | ||
421 | case V4L2_CID_AUDIO_BASS: | ||
422 | case V4L2_CID_AUDIO_TREBLE: | ||
423 | case V4L2_CID_AUDIO_BALANCE: | ||
424 | case V4L2_CID_AUDIO_MUTE: | ||
425 | return cx25840_audio(client, VIDIOC_S_CTRL, ctrl); | ||
426 | } | ||
427 | |||
428 | return 0; | ||
429 | } | ||
430 | |||
431 | static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl) | ||
432 | { | ||
433 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
434 | |||
435 | switch (ctrl->id) { | ||
436 | case CX25840_CID_CARDTYPE: | ||
437 | ctrl->value = state->cardtype; | ||
438 | break; | ||
439 | case V4L2_CID_BRIGHTNESS: | ||
440 | ctrl->value = cx25840_read(client, 0x414) + 128; | ||
441 | break; | ||
442 | case V4L2_CID_CONTRAST: | ||
443 | ctrl->value = cx25840_read(client, 0x415) >> 1; | ||
444 | break; | ||
445 | case V4L2_CID_SATURATION: | ||
446 | ctrl->value = cx25840_read(client, 0x420) >> 1; | ||
447 | break; | ||
448 | case V4L2_CID_HUE: | ||
449 | ctrl->value = cx25840_read(client, 0x422); | ||
450 | break; | ||
451 | case V4L2_CID_AUDIO_VOLUME: | ||
452 | case V4L2_CID_AUDIO_BASS: | ||
453 | case V4L2_CID_AUDIO_TREBLE: | ||
454 | case V4L2_CID_AUDIO_BALANCE: | ||
455 | case V4L2_CID_AUDIO_MUTE: | ||
456 | return cx25840_audio(client, VIDIOC_G_CTRL, ctrl); | ||
457 | default: | ||
458 | return -EINVAL; | ||
459 | } | ||
460 | |||
461 | return 0; | ||
462 | } | ||
463 | |||
464 | /* ----------------------------------------------------------------------- */ | ||
465 | |||
466 | static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt) | ||
467 | { | ||
468 | switch (fmt->type) { | ||
469 | case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE: | ||
470 | return cx25840_vbi(client, VIDIOC_G_FMT, fmt); | ||
471 | default: | ||
472 | return -EINVAL; | ||
473 | } | ||
474 | |||
475 | return 0; | ||
476 | } | ||
477 | |||
478 | static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt) | ||
479 | { | ||
480 | struct v4l2_pix_format *pix; | ||
481 | int HSC, VSC, Vsrc, Hsrc, filter, Vlines; | ||
482 | int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC); | ||
483 | |||
484 | switch (fmt->type) { | ||
485 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: | ||
486 | pix = &(fmt->fmt.pix); | ||
487 | |||
488 | Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4; | ||
489 | Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4; | ||
490 | |||
491 | Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4; | ||
492 | Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4; | ||
493 | |||
494 | Vlines = pix->height + (is_pal ? 4 : 7); | ||
495 | |||
496 | if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) || | ||
497 | (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) { | ||
498 | cx25840_err("%dx%d is not a valid size!\n", | ||
499 | pix->width, pix->height); | ||
500 | return -ERANGE; | ||
501 | } | ||
502 | |||
503 | HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20); | ||
504 | VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9)); | ||
505 | VSC &= 0x1fff; | ||
506 | |||
507 | if (pix->width >= 385) | ||
508 | filter = 0; | ||
509 | else if (pix->width > 192) | ||
510 | filter = 1; | ||
511 | else if (pix->width > 96) | ||
512 | filter = 2; | ||
513 | else | ||
514 | filter = 3; | ||
515 | |||
516 | cx25840_dbg("decoder set size %dx%d -> scale %ux%u\n", | ||
517 | pix->width, pix->height, HSC, VSC); | ||
518 | |||
519 | /* HSCALE=HSC */ | ||
520 | cx25840_write(client, 0x418, HSC & 0xff); | ||
521 | cx25840_write(client, 0x419, (HSC >> 8) & 0xff); | ||
522 | cx25840_write(client, 0x41a, HSC >> 16); | ||
523 | /* VSCALE=VSC */ | ||
524 | cx25840_write(client, 0x41c, VSC & 0xff); | ||
525 | cx25840_write(client, 0x41d, VSC >> 8); | ||
526 | /* VS_INTRLACE=1 VFILT=filter */ | ||
527 | cx25840_write(client, 0x41e, 0x8 | filter); | ||
528 | break; | ||
529 | |||
530 | case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE: | ||
531 | return cx25840_vbi(client, VIDIOC_S_FMT, fmt); | ||
532 | |||
533 | case V4L2_BUF_TYPE_VBI_CAPTURE: | ||
534 | return cx25840_vbi(client, VIDIOC_S_FMT, fmt); | ||
535 | |||
536 | default: | ||
537 | return -EINVAL; | ||
538 | } | ||
539 | |||
540 | return 0; | ||
541 | } | ||
542 | |||
543 | /* ----------------------------------------------------------------------- */ | ||
544 | |||
545 | static int cx25840_command(struct i2c_client *client, unsigned int cmd, | ||
546 | void *arg) | ||
547 | { | ||
548 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
549 | struct v4l2_tuner *vt = arg; | ||
550 | int result = 0; | ||
551 | |||
552 | switch (cmd) { | ||
553 | case 0: | ||
554 | break; | ||
555 | |||
556 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
557 | /* ioctls to allow direct access to the | ||
558 | * cx25840 registers for testing */ | ||
559 | case VIDIOC_INT_G_REGISTER: | ||
560 | { | ||
561 | struct v4l2_register *reg = arg; | ||
562 | |||
563 | if (reg->i2c_id != I2C_DRIVERID_CX25840) | ||
564 | return -EINVAL; | ||
565 | reg->val = cx25840_read(client, reg->reg & 0x0fff); | ||
566 | break; | ||
567 | } | ||
568 | |||
569 | case VIDIOC_INT_S_REGISTER: | ||
570 | { | ||
571 | struct v4l2_register *reg = arg; | ||
572 | |||
573 | if (reg->i2c_id != I2C_DRIVERID_CX25840) | ||
574 | return -EINVAL; | ||
575 | if (!capable(CAP_SYS_ADMIN)) | ||
576 | return -EPERM; | ||
577 | cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff); | ||
578 | break; | ||
579 | } | ||
580 | #endif | ||
581 | |||
582 | case VIDIOC_INT_DECODE_VBI_LINE: | ||
583 | return cx25840_vbi(client, cmd, arg); | ||
584 | |||
585 | case VIDIOC_INT_AUDIO_CLOCK_FREQ: | ||
586 | case AUDC_SET_INPUT: | ||
587 | result = cx25840_audio(client, cmd, arg); | ||
588 | break; | ||
589 | |||
590 | case VIDIOC_STREAMON: | ||
591 | cx25840_dbg("enable output\n"); | ||
592 | cx25840_write(client, 0x115, 0x8c); | ||
593 | cx25840_write(client, 0x116, 0x07); | ||
594 | break; | ||
595 | |||
596 | case VIDIOC_STREAMOFF: | ||
597 | cx25840_dbg("disable output\n"); | ||
598 | cx25840_write(client, 0x115, 0x00); | ||
599 | cx25840_write(client, 0x116, 0x00); | ||
600 | break; | ||
601 | |||
602 | case VIDIOC_LOG_STATUS: | ||
603 | log_status(client); | ||
604 | break; | ||
605 | |||
606 | case VIDIOC_G_CTRL: | ||
607 | result = get_v4lctrl(client, (struct v4l2_control *)arg); | ||
608 | break; | ||
609 | |||
610 | case VIDIOC_S_CTRL: | ||
611 | result = set_v4lctrl(client, (struct v4l2_control *)arg); | ||
612 | break; | ||
613 | |||
614 | case VIDIOC_G_STD: | ||
615 | *(v4l2_std_id *)arg = cx25840_get_v4lstd(client); | ||
616 | break; | ||
617 | |||
618 | case VIDIOC_S_STD: | ||
619 | result = set_v4lstd(client, *(v4l2_std_id *)arg); | ||
620 | break; | ||
621 | |||
622 | case VIDIOC_G_INPUT: | ||
623 | *(int *)arg = state->input; | ||
624 | break; | ||
625 | |||
626 | case VIDIOC_S_INPUT: | ||
627 | result = set_input(client, *(int *)arg); | ||
628 | break; | ||
629 | |||
630 | case VIDIOC_S_FREQUENCY: | ||
631 | input_change(client); | ||
632 | break; | ||
633 | |||
634 | case VIDIOC_G_TUNER: | ||
635 | { | ||
636 | u8 mode = cx25840_read(client, 0x804); | ||
637 | u8 pref = cx25840_read(client, 0x809) & 0xf; | ||
638 | u8 vpres = cx25840_read(client, 0x80a) & 0x10; | ||
639 | int val = 0; | ||
640 | |||
641 | vt->capability |= | ||
642 | V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 | | ||
643 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP; | ||
644 | |||
645 | vt->signal = vpres ? 0xffff : 0x0; | ||
646 | |||
647 | /* get rxsubchans and audmode */ | ||
648 | if ((mode & 0xf) == 1) | ||
649 | val |= V4L2_TUNER_SUB_STEREO; | ||
650 | else | ||
651 | val |= V4L2_TUNER_SUB_MONO; | ||
652 | |||
653 | if (mode == 2 || mode == 4) | ||
654 | val |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; | ||
655 | |||
656 | if (mode & 0x10) | ||
657 | val |= V4L2_TUNER_SUB_SAP; | ||
658 | |||
659 | vt->rxsubchans = val; | ||
660 | |||
661 | switch (pref) { | ||
662 | case 0: | ||
663 | vt->audmode = V4L2_TUNER_MODE_MONO; | ||
664 | break; | ||
665 | case 1: | ||
666 | case 2: | ||
667 | vt->audmode = V4L2_TUNER_MODE_LANG2; | ||
668 | break; | ||
669 | case 4: | ||
670 | default: | ||
671 | vt->audmode = V4L2_TUNER_MODE_STEREO; | ||
672 | } | ||
673 | break; | ||
674 | } | ||
675 | |||
676 | case VIDIOC_S_TUNER: | ||
677 | switch (vt->audmode) { | ||
678 | case V4L2_TUNER_MODE_MONO: | ||
679 | case V4L2_TUNER_MODE_LANG1: | ||
680 | /* Force PREF_MODE to MONO */ | ||
681 | cx25840_and_or(client, 0x809, ~0xf, 0x00); | ||
682 | break; | ||
683 | case V4L2_TUNER_MODE_STEREO: | ||
684 | /* Force PREF_MODE to STEREO */ | ||
685 | cx25840_and_or(client, 0x809, ~0xf, 0x04); | ||
686 | break; | ||
687 | case V4L2_TUNER_MODE_LANG2: | ||
688 | /* Force PREF_MODE to LANG2 */ | ||
689 | cx25840_and_or(client, 0x809, ~0xf, 0x01); | ||
690 | break; | ||
691 | } | ||
692 | break; | ||
693 | |||
694 | case VIDIOC_G_FMT: | ||
695 | result = get_v4lfmt(client, (struct v4l2_format *)arg); | ||
696 | break; | ||
697 | |||
698 | case VIDIOC_S_FMT: | ||
699 | result = set_v4lfmt(client, (struct v4l2_format *)arg); | ||
700 | break; | ||
701 | |||
702 | case VIDIOC_INT_RESET: | ||
703 | cx25840_initialize(client, 0); | ||
704 | break; | ||
705 | |||
706 | case VIDIOC_INT_G_CHIP_IDENT: | ||
707 | *(enum v4l2_chip_ident *)arg = | ||
708 | V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf); | ||
709 | break; | ||
710 | |||
711 | default: | ||
712 | cx25840_err("invalid ioctl %x\n", cmd); | ||
713 | return -EINVAL; | ||
714 | } | ||
715 | |||
716 | return result; | ||
717 | } | ||
718 | |||
719 | /* ----------------------------------------------------------------------- */ | ||
720 | |||
721 | struct i2c_driver i2c_driver_cx25840; | ||
722 | |||
723 | static int cx25840_detect_client(struct i2c_adapter *adapter, int address, | ||
724 | int kind) | ||
725 | { | ||
726 | struct i2c_client *client; | ||
727 | struct cx25840_state *state; | ||
728 | u16 device_id; | ||
729 | |||
730 | /* Check if the adapter supports the needed features | ||
731 | * Not until kernel version 2.6.11 did the bit-algo | ||
732 | * correctly report that it would do an I2C-level xfer */ | ||
733 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) | ||
734 | return 0; | ||
735 | |||
736 | client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL); | ||
737 | if (client == 0) | ||
738 | return -ENOMEM; | ||
739 | |||
740 | memset(client, 0, sizeof(struct i2c_client)); | ||
741 | client->addr = address; | ||
742 | client->adapter = adapter; | ||
743 | client->driver = &i2c_driver_cx25840; | ||
744 | client->flags = I2C_CLIENT_ALLOW_USE; | ||
745 | snprintf(client->name, sizeof(client->name) - 1, "cx25840"); | ||
746 | |||
747 | cx25840_dbg("detecting cx25840 client on address 0x%x\n", address << 1); | ||
748 | |||
749 | device_id = cx25840_read(client, 0x101) << 8; | ||
750 | device_id |= cx25840_read(client, 0x100); | ||
751 | |||
752 | /* The high byte of the device ID should be | ||
753 | * 0x84 if chip is present */ | ||
754 | if ((device_id & 0xff00) != 0x8400) { | ||
755 | cx25840_dbg("cx25840 not found\n"); | ||
756 | kfree(client); | ||
757 | return 0; | ||
758 | } | ||
759 | |||
760 | cx25840_info("cx25%3x-2%x found @ 0x%x (%s)\n", | ||
761 | (device_id & 0xfff0) >> 4, | ||
762 | (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3, | ||
763 | address << 1, adapter->name); | ||
764 | |||
765 | state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL); | ||
766 | if (state == NULL) { | ||
767 | kfree(client); | ||
768 | return -ENOMEM; | ||
769 | } | ||
770 | |||
771 | i2c_set_clientdata(client, state); | ||
772 | memset(state, 0, sizeof(struct cx25840_state)); | ||
773 | state->input = CX25840_TUNER; | ||
774 | state->audclk_freq = V4L2_AUDCLK_48_KHZ; | ||
775 | state->audio_input = AUDIO_TUNER; | ||
776 | state->cardtype = CARDTYPE_PVR150; | ||
777 | |||
778 | cx25840_initialize(client, 1); | ||
779 | |||
780 | i2c_attach_client(client); | ||
781 | |||
782 | return 0; | ||
783 | } | ||
784 | |||
785 | static int cx25840_attach_adapter(struct i2c_adapter *adapter) | ||
786 | { | ||
787 | #ifdef I2C_CLASS_TV_ANALOG | ||
788 | if (adapter->class & I2C_CLASS_TV_ANALOG) | ||
789 | #else | ||
790 | if (adapter->id == I2C_HW_B_BT848) | ||
791 | #endif | ||
792 | return i2c_probe(adapter, &addr_data, &cx25840_detect_client); | ||
793 | return 0; | ||
794 | } | ||
795 | |||
796 | static int cx25840_detach_client(struct i2c_client *client) | ||
797 | { | ||
798 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
799 | int err; | ||
800 | |||
801 | err = i2c_detach_client(client); | ||
802 | if (err) { | ||
803 | return err; | ||
804 | } | ||
805 | |||
806 | kfree(state); | ||
807 | kfree(client); | ||
808 | |||
809 | return 0; | ||
810 | } | ||
811 | |||
812 | /* ----------------------------------------------------------------------- */ | ||
813 | |||
814 | struct i2c_driver i2c_driver_cx25840 = { | ||
815 | .name = "cx25840", | ||
816 | |||
817 | .id = I2C_DRIVERID_CX25840, | ||
818 | .flags = I2C_DF_NOTIFY, | ||
819 | |||
820 | .attach_adapter = cx25840_attach_adapter, | ||
821 | .detach_client = cx25840_detach_client, | ||
822 | .command = cx25840_command, | ||
823 | .owner = THIS_MODULE, | ||
824 | }; | ||
825 | |||
826 | |||
827 | static int __init m__init(void) | ||
828 | { | ||
829 | return i2c_add_driver(&i2c_driver_cx25840); | ||
830 | } | ||
831 | |||
832 | static void __exit m__exit(void) | ||
833 | { | ||
834 | i2c_del_driver(&i2c_driver_cx25840); | ||
835 | } | ||
836 | |||
837 | module_init(m__init); | ||
838 | module_exit(m__exit); | ||
839 | |||
840 | /* ----------------------------------------------------------------------- */ | ||
841 | |||
842 | static void log_status(struct i2c_client *client) | ||
843 | { | ||
844 | static const char *const fmt_strs[] = { | ||
845 | "0x0", | ||
846 | "NTSC-M", "NTSC-J", "NTSC-4.43", | ||
847 | "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60", | ||
848 | "0x9", "0xA", "0xB", | ||
849 | "SECAM", | ||
850 | "0xD", "0xE", "0xF" | ||
851 | }; | ||
852 | |||
853 | struct cx25840_state *state = i2c_get_clientdata(client); | ||
854 | u8 microctrl_vidfmt = cx25840_read(client, 0x80a); | ||
855 | u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf; | ||
856 | u8 gen_stat1 = cx25840_read(client, 0x40d); | ||
857 | u8 download_ctl = cx25840_read(client, 0x803); | ||
858 | u8 mod_det_stat0 = cx25840_read(client, 0x804); | ||
859 | u8 mod_det_stat1 = cx25840_read(client, 0x805); | ||
860 | u8 audio_config = cx25840_read(client, 0x808); | ||
861 | u8 pref_mode = cx25840_read(client, 0x809); | ||
862 | u8 afc0 = cx25840_read(client, 0x80b); | ||
863 | u8 mute_ctl = cx25840_read(client, 0x8d3); | ||
864 | char *p; | ||
865 | |||
866 | cx25840_info("Video signal: %spresent\n", | ||
867 | (microctrl_vidfmt & 0x10) ? "" : "not "); | ||
868 | cx25840_info("Detected format: %s\n", | ||
869 | fmt_strs[gen_stat1 & 0xf]); | ||
870 | |||
871 | switch (mod_det_stat0) { | ||
872 | case 0x00: p = "mono"; break; | ||
873 | case 0x01: p = "stereo"; break; | ||
874 | case 0x02: p = "dual"; break; | ||
875 | case 0x04: p = "tri"; break; | ||
876 | case 0x10: p = "mono with SAP"; break; | ||
877 | case 0x11: p = "stereo with SAP"; break; | ||
878 | case 0x12: p = "dual with SAP"; break; | ||
879 | case 0x14: p = "tri with SAP"; break; | ||
880 | case 0xfe: p = "forced mode"; break; | ||
881 | default: p = "not defined"; | ||
882 | } | ||
883 | cx25840_info("Detected audio mode: %s\n", p); | ||
884 | |||
885 | switch (mod_det_stat1) { | ||
886 | case 0x00: p = "not defined"; break; | ||
887 | case 0x01: p = "EIAJ"; break; | ||
888 | case 0x02: p = "A2-M"; break; | ||
889 | case 0x03: p = "A2-BG"; break; | ||
890 | case 0x04: p = "A2-DK1"; break; | ||
891 | case 0x05: p = "A2-DK2"; break; | ||
892 | case 0x06: p = "A2-DK3"; break; | ||
893 | case 0x07: p = "A1 (6.0 MHz FM Mono)"; break; | ||
894 | case 0x08: p = "AM-L"; break; | ||
895 | case 0x09: p = "NICAM-BG"; break; | ||
896 | case 0x0a: p = "NICAM-DK"; break; | ||
897 | case 0x0b: p = "NICAM-I"; break; | ||
898 | case 0x0c: p = "NICAM-L"; break; | ||
899 | case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break; | ||
900 | case 0x0e: p = "IF FM Radio"; break; | ||
901 | case 0x0f: p = "BTSC"; break; | ||
902 | case 0x10: p = "high-deviation FM"; break; | ||
903 | case 0x11: p = "very high-deviation FM"; break; | ||
904 | case 0xfd: p = "unknown audio standard"; break; | ||
905 | case 0xfe: p = "forced audio standard"; break; | ||
906 | case 0xff: p = "no detected audio standard"; break; | ||
907 | default: p = "not defined"; | ||
908 | } | ||
909 | cx25840_info("Detected audio standard: %s\n", p); | ||
910 | cx25840_info("Audio muted: %s\n", | ||
911 | (mute_ctl & 0x2) ? "yes" : "no"); | ||
912 | cx25840_info("Audio microcontroller: %s\n", | ||
913 | (download_ctl & 0x10) ? "running" : "stopped"); | ||
914 | |||
915 | switch (audio_config >> 4) { | ||
916 | case 0x00: p = "undefined"; break; | ||
917 | case 0x01: p = "BTSC"; break; | ||
918 | case 0x02: p = "EIAJ"; break; | ||
919 | case 0x03: p = "A2-M"; break; | ||
920 | case 0x04: p = "A2-BG"; break; | ||
921 | case 0x05: p = "A2-DK1"; break; | ||
922 | case 0x06: p = "A2-DK2"; break; | ||
923 | case 0x07: p = "A2-DK3"; break; | ||
924 | case 0x08: p = "A1 (6.0 MHz FM Mono)"; break; | ||
925 | case 0x09: p = "AM-L"; break; | ||
926 | case 0x0a: p = "NICAM-BG"; break; | ||
927 | case 0x0b: p = "NICAM-DK"; break; | ||
928 | case 0x0c: p = "NICAM-I"; break; | ||
929 | case 0x0d: p = "NICAM-L"; break; | ||
930 | case 0x0e: p = "FM radio"; break; | ||
931 | case 0x0f: p = "automatic detection"; break; | ||
932 | default: p = "undefined"; | ||
933 | } | ||
934 | cx25840_info("Configured audio standard: %s\n", p); | ||
935 | |||
936 | if ((audio_config >> 4) < 0xF) { | ||
937 | switch (audio_config & 0xF) { | ||
938 | case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break; | ||
939 | case 0x01: p = "MONO2 (LANGUAGE B)"; break; | ||
940 | case 0x02: p = "MONO3 (STEREO forced MONO)"; break; | ||
941 | case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break; | ||
942 | case 0x04: p = "STEREO"; break; | ||
943 | case 0x05: p = "DUAL1 (AB)"; break; | ||
944 | case 0x06: p = "DUAL2 (AC) (FM)"; break; | ||
945 | case 0x07: p = "DUAL3 (BC) (FM)"; break; | ||
946 | case 0x08: p = "DUAL4 (AC) (AM)"; break; | ||
947 | case 0x09: p = "DUAL5 (BC) (AM)"; break; | ||
948 | case 0x0a: p = "SAP"; break; | ||
949 | default: p = "undefined"; | ||
950 | } | ||
951 | cx25840_info("Configured audio mode: %s\n", p); | ||
952 | } else { | ||
953 | switch (audio_config & 0xF) { | ||
954 | case 0x00: p = "BG"; break; | ||
955 | case 0x01: p = "DK1"; break; | ||
956 | case 0x02: p = "DK2"; break; | ||
957 | case 0x03: p = "DK3"; break; | ||
958 | case 0x04: p = "I"; break; | ||
959 | case 0x05: p = "L"; break; | ||
960 | case 0x06: p = "BTSC"; break; | ||
961 | case 0x07: p = "EIAJ"; break; | ||
962 | case 0x08: p = "A2-M"; break; | ||
963 | case 0x09: p = "FM Radio"; break; | ||
964 | case 0x0f: p = "automatic standard and mode detection"; break; | ||
965 | default: p = "undefined"; | ||
966 | } | ||
967 | cx25840_info("Configured audio system: %s\n", p); | ||
968 | } | ||
969 | |||
970 | cx25840_info("Specified standard: %s\n", | ||
971 | vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection"); | ||
972 | |||
973 | switch (state->input) { | ||
974 | case CX25840_COMPOSITE0: p = "Composite 0"; break; | ||
975 | case CX25840_COMPOSITE1: p = "Composite 1"; break; | ||
976 | case CX25840_SVIDEO0: p = "S-Video 0"; break; | ||
977 | case CX25840_SVIDEO1: p = "S-Video 1"; break; | ||
978 | case CX25840_TUNER: p = "Tuner"; break; | ||
979 | } | ||
980 | cx25840_info("Specified input: %s\n", p); | ||
981 | cx25840_info("Specified audio input: %s\n", | ||
982 | state->audio_input == 0 ? "Tuner" : "External"); | ||
983 | |||
984 | switch (state->audclk_freq) { | ||
985 | case V4L2_AUDCLK_441_KHZ: p = "44.1 kHz"; break; | ||
986 | case V4L2_AUDCLK_48_KHZ: p = "48 kHz"; break; | ||
987 | case V4L2_AUDCLK_32_KHZ: p = "32 kHz"; break; | ||
988 | default: p = "undefined"; | ||
989 | } | ||
990 | cx25840_info("Specified audioclock freq: %s\n", p); | ||
991 | |||
992 | switch (pref_mode & 0xf) { | ||
993 | case 0: p = "mono/language A"; break; | ||
994 | case 1: p = "language B"; break; | ||
995 | case 2: p = "language C"; break; | ||
996 | case 3: p = "analog fallback"; break; | ||
997 | case 4: p = "stereo"; break; | ||
998 | case 5: p = "language AC"; break; | ||
999 | case 6: p = "language BC"; break; | ||
1000 | case 7: p = "language AB"; break; | ||
1001 | default: p = "undefined"; | ||
1002 | } | ||
1003 | cx25840_info("Preferred audio mode: %s\n", p); | ||
1004 | |||
1005 | if ((audio_config & 0xf) == 0xf) { | ||
1006 | switch ((afc0 >> 3) & 0x3) { | ||
1007 | case 0: p = "system DK"; break; | ||
1008 | case 1: p = "system L"; break; | ||
1009 | case 2: p = "autodetect"; break; | ||
1010 | default: p = "undefined"; | ||
1011 | } | ||
1012 | cx25840_info("Selected 65 MHz format: %s\n", p); | ||
1013 | |||
1014 | switch (afc0 & 0x7) { | ||
1015 | case 0: p = "chroma"; break; | ||
1016 | case 1: p = "BTSC"; break; | ||
1017 | case 2: p = "EIAJ"; break; | ||
1018 | case 3: p = "A2-M"; break; | ||
1019 | case 4: p = "autodetect"; break; | ||
1020 | default: p = "undefined"; | ||
1021 | } | ||
1022 | cx25840_info("Selected 45 MHz format: %s\n", p); | ||
1023 | } | ||
1024 | } | ||
diff --git a/drivers/media/video/cx25840/cx25840-firmware.c b/drivers/media/video/cx25840/cx25840-firmware.c new file mode 100644 index 000000000000..0ce4a9550eb2 --- /dev/null +++ b/drivers/media/video/cx25840/cx25840-firmware.c | |||
@@ -0,0 +1,167 @@ | |||
1 | /* cx25840 firmware functions | ||
2 | * | ||
3 | * This program is free software; you can redistribute it and/or | ||
4 | * modify it under the terms of the GNU General Public License | ||
5 | * as published by the Free Software Foundation; either version 2 | ||
6 | * of the License, or (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program; if not, write to the Free Software | ||
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
16 | */ | ||
17 | |||
18 | |||
19 | #include <linux/module.h> | ||
20 | #include <linux/i2c.h> | ||
21 | #include <linux/i2c-algo-bit.h> | ||
22 | #include <linux/firmware.h> | ||
23 | #include <media/v4l2-common.h> | ||
24 | |||
25 | #include "cx25840.h" | ||
26 | |||
27 | #define FWFILE "HcwMakoA.ROM" | ||
28 | #define FWSEND 1024 | ||
29 | |||
30 | #define FWDEV(x) &((x)->adapter->dev) | ||
31 | |||
32 | static int fastfw = 1; | ||
33 | static char *firmware = FWFILE; | ||
34 | |||
35 | module_param(fastfw, bool, 0444); | ||
36 | module_param(firmware, charp, 0444); | ||
37 | |||
38 | MODULE_PARM_DESC(fastfw, "Load firmware fast [0=100MHz 1=333MHz (default)]"); | ||
39 | MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]"); | ||
40 | |||
41 | static inline void set_i2c_delay(struct i2c_client *client, int delay) | ||
42 | { | ||
43 | struct i2c_algo_bit_data *algod = client->adapter->algo_data; | ||
44 | |||
45 | /* We aren't guaranteed to be using algo_bit, | ||
46 | * so avoid the null pointer dereference | ||
47 | * and disable the 'fast firmware load' */ | ||
48 | if (algod) { | ||
49 | algod->udelay = delay; | ||
50 | } else { | ||
51 | fastfw = 0; | ||
52 | } | ||
53 | } | ||
54 | |||
55 | static inline void start_fw_load(struct i2c_client *client) | ||
56 | { | ||
57 | /* DL_ADDR_LB=0 DL_ADDR_HB=0 */ | ||
58 | cx25840_write(client, 0x800, 0x00); | ||
59 | cx25840_write(client, 0x801, 0x00); | ||
60 | // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1 | ||
61 | cx25840_write(client, 0x803, 0x0b); | ||
62 | /* AUTO_INC_DIS=1 */ | ||
63 | cx25840_write(client, 0x000, 0x20); | ||
64 | |||
65 | if (fastfw) | ||
66 | set_i2c_delay(client, 3); | ||
67 | } | ||
68 | |||
69 | static inline void end_fw_load(struct i2c_client *client) | ||
70 | { | ||
71 | if (fastfw) | ||
72 | set_i2c_delay(client, 10); | ||
73 | |||
74 | /* AUTO_INC_DIS=0 */ | ||
75 | cx25840_write(client, 0x000, 0x00); | ||
76 | /* DL_ENABLE=0 */ | ||
77 | cx25840_write(client, 0x803, 0x03); | ||
78 | } | ||
79 | |||
80 | static inline int check_fw_load(struct i2c_client *client, int size) | ||
81 | { | ||
82 | /* DL_ADDR_HB DL_ADDR_LB */ | ||
83 | int s = cx25840_read(client, 0x801) << 8; | ||
84 | s |= cx25840_read(client, 0x800); | ||
85 | |||
86 | if (size != s) { | ||
87 | cx25840_err("firmware %s load failed\n", firmware); | ||
88 | return -EINVAL; | ||
89 | } | ||
90 | |||
91 | cx25840_info("loaded %s firmware (%d bytes)\n", firmware, size); | ||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | static inline int fw_write(struct i2c_client *client, u8 * data, int size) | ||
96 | { | ||
97 | if (i2c_master_send(client, data, size) < size) { | ||
98 | |||
99 | if (fastfw) { | ||
100 | cx25840_err("333MHz i2c firmware load failed\n"); | ||
101 | fastfw = 0; | ||
102 | set_i2c_delay(client, 10); | ||
103 | |||
104 | if (i2c_master_send(client, data, size) < size) { | ||
105 | cx25840_err | ||
106 | ("100MHz i2c firmware load failed\n"); | ||
107 | return -ENOSYS; | ||
108 | } | ||
109 | |||
110 | } else { | ||
111 | cx25840_err("firmware load i2c failure\n"); | ||
112 | return -ENOSYS; | ||
113 | } | ||
114 | |||
115 | } | ||
116 | |||
117 | return 0; | ||
118 | } | ||
119 | |||
120 | int cx25840_loadfw(struct i2c_client *client) | ||
121 | { | ||
122 | const struct firmware *fw = NULL; | ||
123 | u8 buffer[4], *ptr; | ||
124 | int size, send, retval; | ||
125 | |||
126 | if (request_firmware(&fw, firmware, FWDEV(client)) != 0) { | ||
127 | cx25840_err("unable to open firmware %s\n", firmware); | ||
128 | return -EINVAL; | ||
129 | } | ||
130 | |||
131 | start_fw_load(client); | ||
132 | |||
133 | buffer[0] = 0x08; | ||
134 | buffer[1] = 0x02; | ||
135 | buffer[2] = fw->data[0]; | ||
136 | buffer[3] = fw->data[1]; | ||
137 | retval = fw_write(client, buffer, 4); | ||
138 | |||
139 | if (retval < 0) { | ||
140 | release_firmware(fw); | ||
141 | return retval; | ||
142 | } | ||
143 | |||
144 | size = fw->size - 2; | ||
145 | ptr = fw->data; | ||
146 | while (size > 0) { | ||
147 | ptr[0] = 0x08; | ||
148 | ptr[1] = 0x02; | ||
149 | send = size > (FWSEND - 2) ? FWSEND : size + 2; | ||
150 | retval = fw_write(client, ptr, send); | ||
151 | |||
152 | if (retval < 0) { | ||
153 | release_firmware(fw); | ||
154 | return retval; | ||
155 | } | ||
156 | |||
157 | size -= FWSEND - 2; | ||
158 | ptr += FWSEND - 2; | ||
159 | } | ||
160 | |||
161 | end_fw_load(client); | ||
162 | |||
163 | size = fw->size; | ||
164 | release_firmware(fw); | ||
165 | |||
166 | return check_fw_load(client, size); | ||
167 | } | ||
diff --git a/drivers/media/video/cx25840/cx25840-vbi.c b/drivers/media/video/cx25840/cx25840-vbi.c new file mode 100644 index 000000000000..13ba4e15ddea --- /dev/null +++ b/drivers/media/video/cx25840/cx25840-vbi.c | |||
@@ -0,0 +1,315 @@ | |||
1 | /* cx25840 VBI functions | ||
2 | * | ||
3 | * This program is free software; you can redistribute it and/or | ||
4 | * modify it under the terms of the GNU General Public License | ||
5 | * as published by the Free Software Foundation; either version 2 | ||
6 | * of the License, or (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program; if not, write to the Free Software | ||
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
16 | */ | ||
17 | |||
18 | |||
19 | #include <linux/videodev2.h> | ||
20 | #include <linux/i2c.h> | ||
21 | #include <media/v4l2-common.h> | ||
22 | |||
23 | #include "cx25840.h" | ||
24 | |||
25 | static inline int odd_parity(u8 c) | ||
26 | { | ||
27 | c ^= (c >> 4); | ||
28 | c ^= (c >> 2); | ||
29 | c ^= (c >> 1); | ||
30 | |||
31 | return c & 1; | ||
32 | } | ||
33 | |||
34 | static inline int decode_vps(u8 * dst, u8 * p) | ||
35 | { | ||
36 | static const u8 biphase_tbl[] = { | ||
37 | 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4, | ||
38 | 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0, | ||
39 | 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96, | ||
40 | 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2, | ||
41 | 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94, | ||
42 | 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0, | ||
43 | 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4, | ||
44 | 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0, | ||
45 | 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5, | ||
46 | 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1, | ||
47 | 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87, | ||
48 | 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3, | ||
49 | 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85, | ||
50 | 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1, | ||
51 | 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5, | ||
52 | 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1, | ||
53 | 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4, | ||
54 | 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0, | ||
55 | 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86, | ||
56 | 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2, | ||
57 | 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84, | ||
58 | 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0, | ||
59 | 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4, | ||
60 | 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0, | ||
61 | 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4, | ||
62 | 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0, | ||
63 | 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96, | ||
64 | 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2, | ||
65 | 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94, | ||
66 | 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0, | ||
67 | 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4, | ||
68 | 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0, | ||
69 | }; | ||
70 | |||
71 | u8 c, err = 0; | ||
72 | int i; | ||
73 | |||
74 | for (i = 0; i < 2 * 13; i += 2) { | ||
75 | err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]]; | ||
76 | c = (biphase_tbl[p[i + 1]] & 0xf) | | ||
77 | ((biphase_tbl[p[i]] & 0xf) << 4); | ||
78 | dst[i / 2] = c; | ||
79 | } | ||
80 | |||
81 | return err & 0xf0; | ||
82 | } | ||
83 | |||
84 | void cx25840_vbi_setup(struct i2c_client *client) | ||
85 | { | ||
86 | v4l2_std_id std = cx25840_get_v4lstd(client); | ||
87 | |||
88 | if (std & ~V4L2_STD_NTSC) { | ||
89 | /* datasheet startup, step 8d */ | ||
90 | cx25840_write(client, 0x49f, 0x11); | ||
91 | |||
92 | cx25840_write(client, 0x470, 0x84); | ||
93 | cx25840_write(client, 0x471, 0x00); | ||
94 | cx25840_write(client, 0x472, 0x2d); | ||
95 | cx25840_write(client, 0x473, 0x5d); | ||
96 | |||
97 | cx25840_write(client, 0x474, 0x24); | ||
98 | cx25840_write(client, 0x475, 0x40); | ||
99 | cx25840_write(client, 0x476, 0x24); | ||
100 | cx25840_write(client, 0x477, 0x28); | ||
101 | |||
102 | cx25840_write(client, 0x478, 0x1f); | ||
103 | cx25840_write(client, 0x479, 0x02); | ||
104 | |||
105 | if (std & V4L2_STD_SECAM) { | ||
106 | cx25840_write(client, 0x47a, 0x80); | ||
107 | cx25840_write(client, 0x47b, 0x00); | ||
108 | cx25840_write(client, 0x47c, 0x5f); | ||
109 | cx25840_write(client, 0x47d, 0x42); | ||
110 | } else { | ||
111 | cx25840_write(client, 0x47a, 0x90); | ||
112 | cx25840_write(client, 0x47b, 0x20); | ||
113 | cx25840_write(client, 0x47c, 0x63); | ||
114 | cx25840_write(client, 0x47d, 0x82); | ||
115 | } | ||
116 | |||
117 | cx25840_write(client, 0x47e, 0x0a); | ||
118 | cx25840_write(client, 0x47f, 0x01); | ||
119 | } else { | ||
120 | /* datasheet startup, step 8d */ | ||
121 | cx25840_write(client, 0x49f, 0x14); | ||
122 | |||
123 | cx25840_write(client, 0x470, 0x7a); | ||
124 | cx25840_write(client, 0x471, 0x00); | ||
125 | cx25840_write(client, 0x472, 0x2d); | ||
126 | cx25840_write(client, 0x473, 0x5b); | ||
127 | |||
128 | cx25840_write(client, 0x474, 0x1a); | ||
129 | cx25840_write(client, 0x475, 0x70); | ||
130 | cx25840_write(client, 0x476, 0x1e); | ||
131 | cx25840_write(client, 0x477, 0x1e); | ||
132 | |||
133 | cx25840_write(client, 0x478, 0x1f); | ||
134 | cx25840_write(client, 0x479, 0x02); | ||
135 | cx25840_write(client, 0x47a, 0x50); | ||
136 | cx25840_write(client, 0x47b, 0x66); | ||
137 | |||
138 | cx25840_write(client, 0x47c, 0x1f); | ||
139 | cx25840_write(client, 0x47d, 0x7c); | ||
140 | cx25840_write(client, 0x47e, 0x08); | ||
141 | cx25840_write(client, 0x47f, 0x00); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | int cx25840_vbi(struct i2c_client *client, unsigned int cmd, void *arg) | ||
146 | { | ||
147 | struct v4l2_format *fmt; | ||
148 | struct v4l2_sliced_vbi_format *svbi; | ||
149 | |||
150 | switch (cmd) { | ||
151 | case VIDIOC_G_FMT: | ||
152 | { | ||
153 | static u16 lcr2vbi[] = { | ||
154 | 0, V4L2_SLICED_TELETEXT_B, 0, /* 1 */ | ||
155 | 0, V4L2_SLICED_WSS_625, 0, /* 4 */ | ||
156 | V4L2_SLICED_CAPTION_525, /* 6 */ | ||
157 | 0, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */ | ||
158 | 0, 0, 0, 0 | ||
159 | }; | ||
160 | int i; | ||
161 | |||
162 | fmt = arg; | ||
163 | if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) | ||
164 | return -EINVAL; | ||
165 | svbi = &fmt->fmt.sliced; | ||
166 | memset(svbi, 0, sizeof(*svbi)); | ||
167 | /* we're done if raw VBI is active */ | ||
168 | if ((cx25840_read(client, 0x404) & 0x10) == 0) | ||
169 | break; | ||
170 | |||
171 | for (i = 7; i <= 23; i++) { | ||
172 | u8 v = cx25840_read(client, 0x424 + i - 7); | ||
173 | |||
174 | svbi->service_lines[0][i] = lcr2vbi[v >> 4]; | ||
175 | svbi->service_lines[1][i] = lcr2vbi[v & 0xf]; | ||
176 | svbi->service_set |= | ||
177 | svbi->service_lines[0][i] | svbi->service_lines[1][i]; | ||
178 | } | ||
179 | break; | ||
180 | } | ||
181 | |||
182 | case VIDIOC_S_FMT: | ||
183 | { | ||
184 | int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC); | ||
185 | int vbi_offset = is_pal ? 1 : 0; | ||
186 | int i, x; | ||
187 | u8 lcr[24]; | ||
188 | |||
189 | fmt = arg; | ||
190 | if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) | ||
191 | return -EINVAL; | ||
192 | svbi = &fmt->fmt.sliced; | ||
193 | if (svbi->service_set == 0) { | ||
194 | /* raw VBI */ | ||
195 | memset(svbi, 0, sizeof(*svbi)); | ||
196 | |||
197 | /* Setup VBI */ | ||
198 | cx25840_vbi_setup(client); | ||
199 | |||
200 | /* VBI Offset */ | ||
201 | cx25840_write(client, 0x47f, vbi_offset); | ||
202 | cx25840_write(client, 0x404, 0x2e); | ||
203 | break; | ||
204 | } | ||
205 | |||
206 | for (x = 0; x <= 23; x++) | ||
207 | lcr[x] = 0x00; | ||
208 | |||
209 | /* Setup VBI */ | ||
210 | cx25840_vbi_setup(client); | ||
211 | |||
212 | /* Sliced VBI */ | ||
213 | cx25840_write(client, 0x404, 0x36); /* Ancillery data */ | ||
214 | cx25840_write(client, 0x406, 0x13); | ||
215 | cx25840_write(client, 0x47f, vbi_offset); | ||
216 | |||
217 | if (is_pal) { | ||
218 | for (i = 0; i <= 6; i++) | ||
219 | svbi->service_lines[0][i] = | ||
220 | svbi->service_lines[1][i] = 0; | ||
221 | } else { | ||
222 | for (i = 0; i <= 9; i++) | ||
223 | svbi->service_lines[0][i] = | ||
224 | svbi->service_lines[1][i] = 0; | ||
225 | |||
226 | for (i = 22; i <= 23; i++) | ||
227 | svbi->service_lines[0][i] = | ||
228 | svbi->service_lines[1][i] = 0; | ||
229 | } | ||
230 | |||
231 | for (i = 7; i <= 23; i++) { | ||
232 | for (x = 0; x <= 1; x++) { | ||
233 | switch (svbi->service_lines[1-x][i]) { | ||
234 | case V4L2_SLICED_TELETEXT_B: | ||
235 | lcr[i] |= 1 << (4 * x); | ||
236 | break; | ||
237 | case V4L2_SLICED_WSS_625: | ||
238 | lcr[i] |= 4 << (4 * x); | ||
239 | break; | ||
240 | case V4L2_SLICED_CAPTION_525: | ||
241 | lcr[i] |= 6 << (4 * x); | ||
242 | break; | ||
243 | case V4L2_SLICED_VPS: | ||
244 | lcr[i] |= 9 << (4 * x); | ||
245 | break; | ||
246 | } | ||
247 | } | ||
248 | } | ||
249 | |||
250 | for (x = 1, i = 0x424; i <= 0x434; i++, x++) { | ||
251 | cx25840_write(client, i, lcr[6 + x]); | ||
252 | } | ||
253 | |||
254 | cx25840_write(client, 0x43c, 0x16); | ||
255 | |||
256 | if (is_pal) { | ||
257 | cx25840_write(client, 0x474, 0x2a); | ||
258 | } else { | ||
259 | cx25840_write(client, 0x474, 0x1a + 6); | ||
260 | } | ||
261 | break; | ||
262 | } | ||
263 | |||
264 | case VIDIOC_INT_DECODE_VBI_LINE: | ||
265 | { | ||
266 | struct v4l2_decode_vbi_line *vbi = arg; | ||
267 | u8 *p = vbi->p; | ||
268 | int id1, id2, l, err = 0; | ||
269 | |||
270 | if (p[0] || p[1] != 0xff || p[2] != 0xff || | ||
271 | (p[3] != 0x55 && p[3] != 0x91)) { | ||
272 | vbi->line = vbi->type = 0; | ||
273 | break; | ||
274 | } | ||
275 | |||
276 | p += 4; | ||
277 | id1 = p[-1]; | ||
278 | id2 = p[0] & 0xf; | ||
279 | l = p[2] & 0x3f; | ||
280 | l += 5; | ||
281 | p += 4; | ||
282 | |||
283 | switch (id2) { | ||
284 | case 1: | ||
285 | id2 = V4L2_SLICED_TELETEXT_B; | ||
286 | break; | ||
287 | case 4: | ||
288 | id2 = V4L2_SLICED_WSS_625; | ||
289 | break; | ||
290 | case 6: | ||
291 | id2 = V4L2_SLICED_CAPTION_525; | ||
292 | err = !odd_parity(p[0]) || !odd_parity(p[1]); | ||
293 | break; | ||
294 | case 9: | ||
295 | id2 = V4L2_SLICED_VPS; | ||
296 | if (decode_vps(p, p) != 0) { | ||
297 | err = 1; | ||
298 | } | ||
299 | break; | ||
300 | default: | ||
301 | id2 = 0; | ||
302 | err = 1; | ||
303 | break; | ||
304 | } | ||
305 | |||
306 | vbi->type = err ? 0 : id2; | ||
307 | vbi->line = err ? 0 : l; | ||
308 | vbi->is_second_field = err ? 0 : (id1 == 0x55); | ||
309 | vbi->p = p; | ||
310 | break; | ||
311 | } | ||
312 | } | ||
313 | |||
314 | return 0; | ||
315 | } | ||
diff --git a/drivers/media/video/cx25840/cx25840.h b/drivers/media/video/cx25840/cx25840.h new file mode 100644 index 000000000000..5c3f0639fb77 --- /dev/null +++ b/drivers/media/video/cx25840/cx25840.h | |||
@@ -0,0 +1,85 @@ | |||
1 | /* cx25840 API header | ||
2 | * | ||
3 | * Copyright (C) 2003-2004 Chris Kennedy | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or | ||
6 | * modify it under the terms of the GNU General Public License | ||
7 | * as published by the Free Software Foundation; either version 2 | ||
8 | * of the License, or (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #ifndef _CX25840_H_ | ||
21 | #define _CX25840_H_ | ||
22 | |||
23 | |||
24 | #include <linux/videodev2.h> | ||
25 | #include <linux/i2c.h> | ||
26 | |||
27 | extern int cx25840_debug; | ||
28 | |||
29 | #define cx25840_dbg(fmt, arg...) do { if (cx25840_debug) \ | ||
30 | printk(KERN_INFO "%s debug %d-%04x: " fmt, client->driver->name, \ | ||
31 | i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0) | ||
32 | |||
33 | #define cx25840_err(fmt, arg...) do { \ | ||
34 | printk(KERN_ERR "%s %d-%04x: " fmt, client->driver->name, \ | ||
35 | i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0) | ||
36 | |||
37 | #define cx25840_info(fmt, arg...) do { \ | ||
38 | printk(KERN_INFO "%s %d-%04x: " fmt, client->driver->name, \ | ||
39 | i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0) | ||
40 | |||
41 | #define CX25840_CID_CARDTYPE (V4L2_CID_PRIVATE_BASE+0) | ||
42 | |||
43 | enum cx25840_cardtype { | ||
44 | CARDTYPE_PVR150, | ||
45 | CARDTYPE_PG600 | ||
46 | }; | ||
47 | |||
48 | enum cx25840_input { | ||
49 | CX25840_TUNER, | ||
50 | CX25840_COMPOSITE0, | ||
51 | CX25840_COMPOSITE1, | ||
52 | CX25840_SVIDEO0, | ||
53 | CX25840_SVIDEO1 | ||
54 | }; | ||
55 | |||
56 | struct cx25840_state { | ||
57 | enum cx25840_cardtype cardtype; | ||
58 | enum cx25840_input input; | ||
59 | int audio_input; | ||
60 | enum v4l2_audio_clock_freq audclk_freq; | ||
61 | }; | ||
62 | |||
63 | /* ----------------------------------------------------------------------- */ | ||
64 | /* cx25850-core.c */ | ||
65 | int cx25840_write(struct i2c_client *client, u16 addr, u8 value); | ||
66 | int cx25840_write4(struct i2c_client *client, u16 addr, u32 value); | ||
67 | u8 cx25840_read(struct i2c_client *client, u16 addr); | ||
68 | u32 cx25840_read4(struct i2c_client *client, u16 addr); | ||
69 | int cx25840_and_or(struct i2c_client *client, u16 addr, u8 mask, u8 value); | ||
70 | v4l2_std_id cx25840_get_v4lstd(struct i2c_client *client); | ||
71 | |||
72 | /* ----------------------------------------------------------------------- */ | ||
73 | /* cx25850-firmware.c */ | ||
74 | int cx25840_loadfw(struct i2c_client *client); | ||
75 | |||
76 | /* ----------------------------------------------------------------------- */ | ||
77 | /* cx25850-audio.c */ | ||
78 | int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg); | ||
79 | |||
80 | /* ----------------------------------------------------------------------- */ | ||
81 | /* cx25850-vbi.c */ | ||
82 | void cx25840_vbi_setup(struct i2c_client *client); | ||
83 | int cx25840_vbi(struct i2c_client *client, unsigned int cmd, void *arg); | ||
84 | |||
85 | #endif | ||