1 /* cx25840 audio functions
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.
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.
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.
19 #include <linux/videodev2.h>
20 #include <linux/i2c.h>
21 #include <media/v4l2-common.h>
22 #include <media/cx25840.h>
24 #include "cx25840-core.h"
26 static int set_audclk_freq(struct i2c_client *client, u32 freq)
28 struct cx25840_state *state = i2c_get_clientdata(client);
30 if (freq != 32000 && freq != 44100 && freq != 48000)
33 /* common for all inputs and rates */
34 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
35 if (!state->is_cx23885)
36 cx25840_write(client, 0x127, 0x50);
38 if (state->aud_input != CX25840_AUDIO_SERIAL) {
41 if (state->is_cx23885) {
42 /* We don't have register values
43 * so avoid destroying registers. */
46 /* VID_PLL and AUX_PLL */
47 cx25840_write4(client, 0x108, 0x1006040f);
50 cx25840_write4(client, 0x110, 0x01bb39ee);
52 if (state->is_cx25836)
55 /* src3/4/6_ctl = 0x0801f77f */
56 cx25840_write4(client, 0x900, 0x0801f77f);
57 cx25840_write4(client, 0x904, 0x0801f77f);
58 cx25840_write4(client, 0x90c, 0x0801f77f);
62 if (state->is_cx23885) {
63 /* We don't have register values
64 * so avoid destroying registers. */
67 /* VID_PLL and AUX_PLL */
68 cx25840_write4(client, 0x108, 0x1009040f);
71 cx25840_write4(client, 0x110, 0x00ec6bd6);
73 if (state->is_cx25836)
76 /* src3/4/6_ctl = 0x08016d59 */
77 cx25840_write4(client, 0x900, 0x08016d59);
78 cx25840_write4(client, 0x904, 0x08016d59);
79 cx25840_write4(client, 0x90c, 0x08016d59);
83 if (state->is_cx23885) {
84 /* We don't have register values
85 * so avoid destroying registers. */
88 /* VID_PLL and AUX_PLL */
89 cx25840_write4(client, 0x108, 0x100a040f);
92 cx25840_write4(client, 0x110, 0x0098d6e5);
94 if (state->is_cx25836)
97 /* src3/4/6_ctl = 0x08014faa */
98 cx25840_write4(client, 0x900, 0x08014faa);
99 cx25840_write4(client, 0x904, 0x08014faa);
100 cx25840_write4(client, 0x90c, 0x08014faa);
106 if (state->is_cx23885) {
107 /* We don't have register values
108 * so avoid destroying registers. */
111 /* VID_PLL and AUX_PLL */
112 cx25840_write4(client, 0x108, 0x1e08040f);
115 cx25840_write4(client, 0x110, 0x012a0869);
117 if (state->is_cx25836)
120 /* src1_ctl = 0x08010000 */
121 cx25840_write4(client, 0x8f8, 0x08010000);
123 /* src3/4/6_ctl = 0x08020000 */
124 cx25840_write4(client, 0x900, 0x08020000);
125 cx25840_write4(client, 0x904, 0x08020000);
126 cx25840_write4(client, 0x90c, 0x08020000);
128 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
129 cx25840_write(client, 0x127, 0x54);
133 if (state->is_cx23885) {
134 /* We don't have register values
135 * so avoid destroying registers. */
139 /* VID_PLL and AUX_PLL */
140 cx25840_write4(client, 0x108, 0x1809040f);
143 cx25840_write4(client, 0x110, 0x00ec6bd6);
145 if (state->is_cx25836)
148 /* src1_ctl = 0x08010000 */
149 cx25840_write4(client, 0x8f8, 0x080160cd);
151 /* src3/4/6_ctl = 0x08020000 */
152 cx25840_write4(client, 0x900, 0x08017385);
153 cx25840_write4(client, 0x904, 0x08017385);
154 cx25840_write4(client, 0x90c, 0x08017385);
158 if (!state->is_cx23885) {
159 /* VID_PLL and AUX_PLL */
160 cx25840_write4(client, 0x108, 0x180a040f);
163 cx25840_write4(client, 0x110, 0x0098d6e5);
166 if (state->is_cx25836)
169 if (!state->is_cx23885) {
171 cx25840_write4(client, 0x8f8, 0x08018000);
174 cx25840_write4(client, 0x900, 0x08015555);
175 cx25840_write4(client, 0x904, 0x08015555);
176 cx25840_write4(client, 0x90c, 0x08015555);
179 cx25840_write4(client, 0x8f8, 0x0801867c);
181 cx25840_write4(client, 0x900, 0x08014faa);
182 cx25840_write4(client, 0x904, 0x08014faa);
183 cx25840_write4(client, 0x90c, 0x08014faa);
189 state->audclk_freq = freq;
194 void cx25840_audio_set_path(struct i2c_client *client)
196 struct cx25840_state *state = i2c_get_clientdata(client);
198 /* assert soft reset */
199 cx25840_and_or(client, 0x810, ~0x1, 0x01);
201 /* stop microcontroller */
202 cx25840_and_or(client, 0x803, ~0x10, 0);
204 /* Mute everything to prevent the PFFT! */
205 cx25840_write(client, 0x8d3, 0x1f);
207 if (state->aud_input == CX25840_AUDIO_SERIAL) {
208 /* Set Path1 to Serial Audio Input */
209 cx25840_write4(client, 0x8d0, 0x01011012);
211 /* The microcontroller should not be started for the
212 * non-tuner inputs: autodetection is specific for
215 /* Set Path1 to Analog Demod Main Channel */
216 cx25840_write4(client, 0x8d0, 0x1f063870);
219 set_audclk_freq(client, state->audclk_freq);
221 if (state->aud_input != CX25840_AUDIO_SERIAL) {
222 /* When the microcontroller detects the
223 * audio format, it will unmute the lines */
224 cx25840_and_or(client, 0x803, ~0x10, 0x10);
227 /* deassert soft reset */
228 cx25840_and_or(client, 0x810, ~0x1, 0x00);
230 if (state->is_cx23885) {
231 /* Ensure the controller is running when we exit */
232 cx25840_and_or(client, 0x803, ~0x10, 0x10);
236 static int get_volume(struct i2c_client *client)
238 struct cx25840_state *state = i2c_get_clientdata(client);
241 if (state->unmute_volume >= 0)
242 return state->unmute_volume;
244 /* Volume runs +18dB to -96dB in 1/2dB steps
245 * change to fit the msp3400 -114dB to +12dB range */
247 /* check PATH1_VOLUME */
248 vol = 228 - cx25840_read(client, 0x8d4);
249 vol = (vol / 2) + 23;
253 static void set_volume(struct i2c_client *client, int volume)
255 struct cx25840_state *state = i2c_get_clientdata(client);
258 if (state->unmute_volume >= 0) {
259 state->unmute_volume = volume;
263 /* Convert the volume to msp3400 values (0-127) */
266 /* now scale it up to cx25840 values
267 * -114dB to -96dB maps to 0
268 * this should be 19, but in my testing that was 4dB too loud */
276 cx25840_write(client, 0x8d4, 228 - (vol * 2));
279 static int get_bass(struct i2c_client *client)
281 /* bass is 49 steps +12dB to -12dB */
283 /* check PATH1_EQ_BASS_VOL */
284 int bass = cx25840_read(client, 0x8d9) & 0x3f;
285 bass = (((48 - bass) * 0xffff) + 47) / 48;
289 static void set_bass(struct i2c_client *client, int bass)
291 /* PATH1_EQ_BASS_VOL */
292 cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
295 static int get_treble(struct i2c_client *client)
297 /* treble is 49 steps +12dB to -12dB */
299 /* check PATH1_EQ_TREBLE_VOL */
300 int treble = cx25840_read(client, 0x8db) & 0x3f;
301 treble = (((48 - treble) * 0xffff) + 47) / 48;
305 static void set_treble(struct i2c_client *client, int treble)
307 /* PATH1_EQ_TREBLE_VOL */
308 cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
311 static int get_balance(struct i2c_client *client)
313 /* balance is 7 bit, 0 to -96dB */
315 /* check PATH1_BAL_LEVEL */
316 int balance = cx25840_read(client, 0x8d5) & 0x7f;
317 /* check PATH1_BAL_LEFT */
318 if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
319 balance = 0x80 - balance;
321 balance = 0x80 + balance;
325 static void set_balance(struct i2c_client *client, int balance)
327 int bal = balance >> 8;
330 cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
331 /* PATH1_BAL_LEVEL */
332 cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
335 cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
336 /* PATH1_BAL_LEVEL */
337 cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
341 static int get_mute(struct i2c_client *client)
343 struct cx25840_state *state = i2c_get_clientdata(client);
345 return state->unmute_volume >= 0;
348 static void set_mute(struct i2c_client *client, int mute)
350 struct cx25840_state *state = i2c_get_clientdata(client);
352 if (mute && state->unmute_volume == -1) {
353 int vol = get_volume(client);
355 set_volume(client, 0);
356 state->unmute_volume = vol;
358 else if (!mute && state->unmute_volume != -1) {
359 int vol = state->unmute_volume;
361 state->unmute_volume = -1;
362 set_volume(client, vol);
366 int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
368 struct cx25840_state *state = i2c_get_clientdata(client);
369 struct v4l2_control *ctrl = arg;
373 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
374 if (!state->is_cx25836)
375 cx25840_and_or(client, 0x810, ~0x1, 1);
376 if (state->aud_input != CX25840_AUDIO_SERIAL) {
377 cx25840_and_or(client, 0x803, ~0x10, 0);
378 cx25840_write(client, 0x8d3, 0x1f);
380 retval = set_audclk_freq(client, *(u32 *)arg);
381 if (state->aud_input != CX25840_AUDIO_SERIAL) {
382 cx25840_and_or(client, 0x803, ~0x10, 0x10);
384 if (!state->is_cx25836)
385 cx25840_and_or(client, 0x810, ~0x1, 0);
390 case V4L2_CID_AUDIO_VOLUME:
391 ctrl->value = get_volume(client);
393 case V4L2_CID_AUDIO_BASS:
394 ctrl->value = get_bass(client);
396 case V4L2_CID_AUDIO_TREBLE:
397 ctrl->value = get_treble(client);
399 case V4L2_CID_AUDIO_BALANCE:
400 ctrl->value = get_balance(client);
402 case V4L2_CID_AUDIO_MUTE:
403 ctrl->value = get_mute(client);
412 case V4L2_CID_AUDIO_VOLUME:
413 set_volume(client, ctrl->value);
415 case V4L2_CID_AUDIO_BASS:
416 set_bass(client, ctrl->value);
418 case V4L2_CID_AUDIO_TREBLE:
419 set_treble(client, ctrl->value);
421 case V4L2_CID_AUDIO_BALANCE:
422 set_balance(client, ctrl->value);
424 case V4L2_CID_AUDIO_MUTE:
425 set_mute(client, ctrl->value);