blob: 34b96c7cfd620007b88a7082166413c284ac7a0e [file] [log] [blame]
Hans Verkuilbd985162005-11-13 16:07:56 -08001/* 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>
Hans Verkuilbd985162005-11-13 16:07:56 -080021#include <media/v4l2-common.h>
Hans Verkuil31bc09b2006-03-25 10:26:09 -030022#include <media/cx25840.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080023
Hans Verkuil31bc09b2006-03-25 10:26:09 -030024#include "cx25840-core.h"
Hans Verkuilbd985162005-11-13 16:07:56 -080025
Andy Walls9eef5502009-09-26 23:54:20 -030026/*
27 * Note: The PLL and SRC parameters are based on a reference frequency that
28 * would ideally be:
29 *
30 * NTSC Color subcarrier freq * 8 = 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
31 *
32 * However, it's not the exact reference frequency that matters, only that the
33 * firmware and modules that comprise the driver for a particular board all
34 * use the same value (close to the ideal value).
35 *
36 * Comments below will note which reference frequency is assumed for various
37 * parameters. They will usually be one of
38 *
39 * ref_freq = 28.636360 MHz
40 * or
41 * ref_freq = 28.636363 MHz
42 */
43
44static int cx25840_set_audclk_freq(struct i2c_client *client, u32 freq)
Hans Verkuilbd985162005-11-13 16:07:56 -080045{
Hans Verkuil9357b312008-11-29 12:50:06 -030046 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuilbd985162005-11-13 16:07:56 -080047
Hans Verkuila8bbf122006-01-09 15:25:42 -020048 if (state->aud_input != CX25840_AUDIO_SERIAL) {
Hans Verkuilbd985162005-11-13 16:07:56 -080049 switch (freq) {
Hans Verkuil3578d3d2006-01-09 15:25:41 -020050 case 32000:
Andy Walls9eef5502009-09-26 23:54:20 -030051 /*
52 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
53 * AUX_PLL Integer = 0x06, AUX PLL Post Divider = 0x10
54 */
55 cx25840_write4(client, 0x108, 0x1006040f);
Hans Verkuilbd985162005-11-13 16:07:56 -080056
Andy Walls9eef5502009-09-26 23:54:20 -030057 /*
58 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
59 * 28636360 * 0xf.15f17f0/4 = 108 MHz
60 * 432 MHz pre-postdivide
61 */
Sri Deevi149783b2009-03-03 06:07:42 -030062
Andy Walls9eef5502009-09-26 23:54:20 -030063 /*
64 * AUX_PLL Fraction = 0x1bb39ee
65 * 28636363 * 0x6.dd9cf70/0x10 = 32000 * 384
66 * 196.6 MHz pre-postdivide
67 * FIXME < 200 MHz is out of specified valid range
68 * FIXME 28636363 ref_freq doesn't match VID PLL ref
69 */
70 cx25840_write4(client, 0x110, 0x01bb39ee);
71
72 /*
73 * SA_MCLK_SEL = 1
74 * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
75 */
76 cx25840_write(client, 0x127, 0x50);
Hans Verkuilbd985162005-11-13 16:07:56 -080077
Andy Walls2a03f032009-09-26 23:47:21 -030078 if (is_cx2583x(state))
Hans Verkuile2b8cf42006-04-22 10:22:46 -030079 break;
80
Andy Walls9eef5502009-09-26 23:54:20 -030081 /* src3/4/6_ctl */
82 /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030083 cx25840_write4(client, 0x900, 0x0801f77f);
84 cx25840_write4(client, 0x904, 0x0801f77f);
85 cx25840_write4(client, 0x90c, 0x0801f77f);
Hans Verkuilbd985162005-11-13 16:07:56 -080086 break;
87
Hans Verkuil3578d3d2006-01-09 15:25:41 -020088 case 44100:
Andy Walls9eef5502009-09-26 23:54:20 -030089 /*
90 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
91 * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x10
92 */
93 cx25840_write4(client, 0x108, 0x1009040f);
Hans Verkuilbd985162005-11-13 16:07:56 -080094
Andy Walls9eef5502009-09-26 23:54:20 -030095 /*
96 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
97 * 28636360 * 0xf.15f17f0/4 = 108 MHz
98 * 432 MHz pre-postdivide
99 */
Sri Deevi149783b2009-03-03 06:07:42 -0300100
Andy Walls9eef5502009-09-26 23:54:20 -0300101 /*
102 * AUX_PLL Fraction = 0x0ec6bd6
103 * 28636363 * 0x9.7635eb0/0x10 = 44100 * 384
104 * 271 MHz pre-postdivide
105 * FIXME 28636363 ref_freq doesn't match VID PLL ref
106 */
107 cx25840_write4(client, 0x110, 0x00ec6bd6);
108
109 /*
110 * SA_MCLK_SEL = 1
111 * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
112 */
113 cx25840_write(client, 0x127, 0x50);
Hans Verkuilbd985162005-11-13 16:07:56 -0800114
Andy Walls2a03f032009-09-26 23:47:21 -0300115 if (is_cx2583x(state))
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300116 break;
117
Andy Walls9eef5502009-09-26 23:54:20 -0300118 /* src3/4/6_ctl */
119 /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300120 cx25840_write4(client, 0x900, 0x08016d59);
121 cx25840_write4(client, 0x904, 0x08016d59);
122 cx25840_write4(client, 0x90c, 0x08016d59);
Hans Verkuilbd985162005-11-13 16:07:56 -0800123 break;
124
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200125 case 48000:
Andy Walls9eef5502009-09-26 23:54:20 -0300126 /*
127 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
128 * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x10
129 */
130 cx25840_write4(client, 0x108, 0x100a040f);
Sri Deevi149783b2009-03-03 06:07:42 -0300131
Andy Walls9eef5502009-09-26 23:54:20 -0300132 /*
133 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
134 * 28636360 * 0xf.15f17f0/4 = 108 MHz
135 * 432 MHz pre-postdivide
136 */
Sri Deevi149783b2009-03-03 06:07:42 -0300137
Andy Walls9eef5502009-09-26 23:54:20 -0300138 /*
139 * AUX_PLL Fraction = 0x098d6e5
140 * 28636363 * 0xa.4c6b728/0x10 = 48000 * 384
141 * 295 MHz pre-postdivide
142 * FIXME 28636363 ref_freq doesn't match VID PLL ref
143 */
144 cx25840_write4(client, 0x110, 0x0098d6e5);
145
146 /*
147 * SA_MCLK_SEL = 1
148 * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
149 */
150 cx25840_write(client, 0x127, 0x50);
Hans Verkuilbd985162005-11-13 16:07:56 -0800151
Andy Walls2a03f032009-09-26 23:47:21 -0300152 if (is_cx2583x(state))
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300153 break;
154
Andy Walls9eef5502009-09-26 23:54:20 -0300155 /* src3/4/6_ctl */
156 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300157 cx25840_write4(client, 0x900, 0x08014faa);
158 cx25840_write4(client, 0x904, 0x08014faa);
159 cx25840_write4(client, 0x90c, 0x08014faa);
Hans Verkuilbd985162005-11-13 16:07:56 -0800160 break;
161 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200162 } else {
Hans Verkuilbd985162005-11-13 16:07:56 -0800163 switch (freq) {
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200164 case 32000:
Andy Walls9eef5502009-09-26 23:54:20 -0300165 /*
166 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
167 * AUX_PLL Integer = 0x08, AUX PLL Post Divider = 0x1e
168 */
169 cx25840_write4(client, 0x108, 0x1e08040f);
Sri Deevi149783b2009-03-03 06:07:42 -0300170
Andy Walls9eef5502009-09-26 23:54:20 -0300171 /*
172 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
173 * 28636360 * 0xf.15f17f0/4 = 108 MHz
174 * 432 MHz pre-postdivide
175 */
Sri Deevi149783b2009-03-03 06:07:42 -0300176
Andy Walls9eef5502009-09-26 23:54:20 -0300177 /*
178 * AUX_PLL Fraction = 0x12a0869
179 * 28636363 * 0x8.9504348/0x1e = 32000 * 256
180 * 246 MHz pre-postdivide
181 * FIXME 28636363 ref_freq doesn't match VID PLL ref
182 */
183 cx25840_write4(client, 0x110, 0x012a0869);
184
185 /*
186 * SA_MCLK_SEL = 1
187 * SA_MCLK_DIV = 0x14 = 256/384 * AUX_PLL post dvivider
188 */
189 cx25840_write(client, 0x127, 0x54);
Hans Verkuilbd985162005-11-13 16:07:56 -0800190
Andy Walls2a03f032009-09-26 23:47:21 -0300191 if (is_cx2583x(state))
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300192 break;
193
Andy Walls9eef5502009-09-26 23:54:20 -0300194 /* src1_ctl */
195 /* 0x1.0000 = 32000/32000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300196 cx25840_write4(client, 0x8f8, 0x08010000);
Hans Verkuilbd985162005-11-13 16:07:56 -0800197
Andy Walls9eef5502009-09-26 23:54:20 -0300198 /* src3/4/6_ctl */
199 /* 0x2.0000 = 2 * (32000/32000) */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300200 cx25840_write4(client, 0x900, 0x08020000);
201 cx25840_write4(client, 0x904, 0x08020000);
202 cx25840_write4(client, 0x90c, 0x08020000);
Hans Verkuilbd985162005-11-13 16:07:56 -0800203 break;
204
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200205 case 44100:
Andy Walls9eef5502009-09-26 23:54:20 -0300206 /*
207 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
208 * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x18
209 */
210 cx25840_write4(client, 0x108, 0x1809040f);
Steven Tothf2340812008-01-10 01:22:39 -0300211
Andy Walls9eef5502009-09-26 23:54:20 -0300212 /*
213 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
214 * 28636360 * 0xf.15f17f0/4 = 108 MHz
215 * 432 MHz pre-postdivide
216 */
Sri Deevi149783b2009-03-03 06:07:42 -0300217
Andy Walls9eef5502009-09-26 23:54:20 -0300218 /*
219 * AUX_PLL Fraction = 0x0ec6bd6
220 * 28636363 * 0x9.7635eb0/0x18 = 44100 * 256
221 * 271 MHz pre-postdivide
222 * FIXME 28636363 ref_freq doesn't match VID PLL ref
223 */
224 cx25840_write4(client, 0x110, 0x00ec6bd6);
Sri Deevi149783b2009-03-03 06:07:42 -0300225
Andy Walls9eef5502009-09-26 23:54:20 -0300226 /*
227 * SA_MCLK_SEL = 1
228 * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
229 */
230 cx25840_write(client, 0x127, 0x50);
Hans Verkuilbd985162005-11-13 16:07:56 -0800231
Andy Walls2a03f032009-09-26 23:47:21 -0300232 if (is_cx2583x(state))
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300233 break;
234
Andy Walls9eef5502009-09-26 23:54:20 -0300235 /* src1_ctl */
236 /* 0x1.60cd = 44100/32000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300237 cx25840_write4(client, 0x8f8, 0x080160cd);
Hans Verkuilbd985162005-11-13 16:07:56 -0800238
Andy Walls9eef5502009-09-26 23:54:20 -0300239 /* src3/4/6_ctl */
240 /* 0x1.7385 = 2 * (32000/44100) */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300241 cx25840_write4(client, 0x900, 0x08017385);
242 cx25840_write4(client, 0x904, 0x08017385);
243 cx25840_write4(client, 0x90c, 0x08017385);
Hans Verkuilbd985162005-11-13 16:07:56 -0800244 break;
245
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200246 case 48000:
Andy Walls9eef5502009-09-26 23:54:20 -0300247 /*
248 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
249 * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x18
250 */
251 cx25840_write4(client, 0x108, 0x180a040f);
Hans Verkuilbd985162005-11-13 16:07:56 -0800252
Andy Walls9eef5502009-09-26 23:54:20 -0300253 /*
254 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
255 * 28636360 * 0xf.15f17f0/4 = 108 MHz
256 * 432 MHz pre-postdivide
257 */
258
259 /*
260 * AUX_PLL Fraction = 0x098d6e5
261 * 28636363 * 0xa.4c6b728/0x18 = 48000 * 256
262 * 295 MHz pre-postdivide
263 * FIXME 28636363 ref_freq doesn't match VID PLL ref
264 */
265 cx25840_write4(client, 0x110, 0x0098d6e5);
266
267 /*
268 * SA_MCLK_SEL = 1
269 * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
270 */
271 cx25840_write(client, 0x127, 0x50);
Hans Verkuilbd985162005-11-13 16:07:56 -0800272
Andy Walls2a03f032009-09-26 23:47:21 -0300273 if (is_cx2583x(state))
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300274 break;
275
Andy Walls9eef5502009-09-26 23:54:20 -0300276 /* src1_ctl */
277 /* 0x1.8000 = 48000/32000 */
278 cx25840_write4(client, 0x8f8, 0x08018000);
Hans Verkuilbd985162005-11-13 16:07:56 -0800279
Andy Walls9eef5502009-09-26 23:54:20 -0300280 /* src3/4/6_ctl */
281 /* 0x1.5555 = 2 * (32000/48000) */
282 cx25840_write4(client, 0x900, 0x08015555);
283 cx25840_write4(client, 0x904, 0x08015555);
284 cx25840_write4(client, 0x90c, 0x08015555);
Hans Verkuilbd985162005-11-13 16:07:56 -0800285 break;
286 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800287 }
288
Hans Verkuilbd985162005-11-13 16:07:56 -0800289 state->audclk_freq = freq;
290
291 return 0;
292}
293
Andy Walls9eef5502009-09-26 23:54:20 -0300294static inline int cx25836_set_audclk_freq(struct i2c_client *client, u32 freq)
295{
296 return cx25840_set_audclk_freq(client, freq);
297}
298
299static int cx23885_set_audclk_freq(struct i2c_client *client, u32 freq)
300{
301 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
302
303 if (state->aud_input != CX25840_AUDIO_SERIAL) {
304 switch (freq) {
305 case 32000:
306 case 44100:
307 case 48000:
308 /* We don't have register values
309 * so avoid destroying registers. */
310 /* FIXME return -EINVAL; */
311 break;
312 }
313 } else {
314 switch (freq) {
315 case 32000:
316 case 44100:
317 /* We don't have register values
318 * so avoid destroying registers. */
319 /* FIXME return -EINVAL; */
320 break;
321
322 case 48000:
323 /* src1_ctl */
324 /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
325 cx25840_write4(client, 0x8f8, 0x0801867c);
326
327 /* src3/4/6_ctl */
328 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
329 cx25840_write4(client, 0x900, 0x08014faa);
330 cx25840_write4(client, 0x904, 0x08014faa);
331 cx25840_write4(client, 0x90c, 0x08014faa);
332 break;
333 }
334 }
335
336 state->audclk_freq = freq;
337
338 return 0;
339}
340
341static int cx231xx_set_audclk_freq(struct i2c_client *client, u32 freq)
342{
343 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
344
345 if (state->aud_input != CX25840_AUDIO_SERIAL) {
346 switch (freq) {
347 case 32000:
348 /* src3/4/6_ctl */
349 /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
350 cx25840_write4(client, 0x900, 0x0801f77f);
351 cx25840_write4(client, 0x904, 0x0801f77f);
352 cx25840_write4(client, 0x90c, 0x0801f77f);
353 break;
354
355 case 44100:
356 /* src3/4/6_ctl */
357 /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
358 cx25840_write4(client, 0x900, 0x08016d59);
359 cx25840_write4(client, 0x904, 0x08016d59);
360 cx25840_write4(client, 0x90c, 0x08016d59);
361 break;
362
363 case 48000:
364 /* src3/4/6_ctl */
365 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
366 cx25840_write4(client, 0x900, 0x08014faa);
367 cx25840_write4(client, 0x904, 0x08014faa);
368 cx25840_write4(client, 0x90c, 0x08014faa);
369 break;
370 }
371 } else {
372 switch (freq) {
373 /* FIXME These cases make different assumptions about audclk */
374 case 32000:
375 /* src1_ctl */
376 /* 0x1.0000 = 32000/32000 */
377 cx25840_write4(client, 0x8f8, 0x08010000);
378
379 /* src3/4/6_ctl */
380 /* 0x2.0000 = 2 * (32000/32000) */
381 cx25840_write4(client, 0x900, 0x08020000);
382 cx25840_write4(client, 0x904, 0x08020000);
383 cx25840_write4(client, 0x90c, 0x08020000);
384 break;
385
386 case 44100:
387 /* src1_ctl */
388 /* 0x1.60cd = 44100/32000 */
389 cx25840_write4(client, 0x8f8, 0x080160cd);
390
391 /* src3/4/6_ctl */
392 /* 0x1.7385 = 2 * (32000/44100) */
393 cx25840_write4(client, 0x900, 0x08017385);
394 cx25840_write4(client, 0x904, 0x08017385);
395 cx25840_write4(client, 0x90c, 0x08017385);
396 break;
397
398 case 48000:
399 /* src1_ctl */
400 /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
401 cx25840_write4(client, 0x8f8, 0x0801867c);
402
403 /* src3/4/6_ctl */
404 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
405 cx25840_write4(client, 0x900, 0x08014faa);
406 cx25840_write4(client, 0x904, 0x08014faa);
407 cx25840_write4(client, 0x90c, 0x08014faa);
408 break;
409 }
410 }
411
412 state->audclk_freq = freq;
413
414 return 0;
415}
416
417static int set_audclk_freq(struct i2c_client *client, u32 freq)
418{
419 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
420
421 if (freq != 32000 && freq != 44100 && freq != 48000)
422 return -EINVAL;
423
424 if (is_cx231xx(state))
425 return cx231xx_set_audclk_freq(client, freq);
426
427 if (is_cx2388x(state))
428 return cx23885_set_audclk_freq(client, freq);
429
430 if (is_cx2583x(state))
431 return cx25836_set_audclk_freq(client, freq);
432
433 return cx25840_set_audclk_freq(client, freq);
434}
435
Hans Verkuila8bbf122006-01-09 15:25:42 -0200436void cx25840_audio_set_path(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800437{
Hans Verkuil9357b312008-11-29 12:50:06 -0300438 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuilbd985162005-11-13 16:07:56 -0800439
Sven Barth5af79f82010-07-10 15:02:21 -0300440 if (!is_cx2583x(state)) {
441 /* assert soft reset */
442 cx25840_and_or(client, 0x810, ~0x1, 0x01);
Hans Verkuil82677612007-08-07 07:16:07 -0300443
Sven Barth5af79f82010-07-10 15:02:21 -0300444 /* stop microcontroller */
445 cx25840_and_or(client, 0x803, ~0x10, 0);
Hans Verkuilbd985162005-11-13 16:07:56 -0800446
Sven Barth5af79f82010-07-10 15:02:21 -0300447 /* Mute everything to prevent the PFFT! */
448 cx25840_write(client, 0x8d3, 0x1f);
Hans Verkuilbd985162005-11-13 16:07:56 -0800449
Sven Barth5af79f82010-07-10 15:02:21 -0300450 if (state->aud_input == CX25840_AUDIO_SERIAL) {
451 /* Set Path1 to Serial Audio Input */
452 cx25840_write4(client, 0x8d0, 0x01011012);
Hans Verkuilbd985162005-11-13 16:07:56 -0800453
Sven Barth5af79f82010-07-10 15:02:21 -0300454 /* The microcontroller should not be started for the
455 * non-tuner inputs: autodetection is specific for
456 * TV audio. */
457 } else {
458 /* Set Path1 to Analog Demod Main Channel */
459 cx25840_write4(client, 0x8d0, 0x1f063870);
460 }
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300461 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800462
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300463 set_audclk_freq(client, state->audclk_freq);
464
Sven Barth5af79f82010-07-10 15:02:21 -0300465 if (!is_cx2583x(state)) {
466 if (state->aud_input != CX25840_AUDIO_SERIAL) {
467 /* When the microcontroller detects the
468 * audio format, it will unmute the lines */
469 cx25840_and_or(client, 0x803, ~0x10, 0x10);
470 }
471
472 /* deassert soft reset */
473 cx25840_and_or(client, 0x810, ~0x1, 0x00);
474
475 /* Ensure the controller is running when we exit */
476 if (is_cx2388x(state) || is_cx231xx(state))
477 cx25840_and_or(client, 0x803, ~0x10, 0x10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800478 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800479}
480
Hans Verkuild92c20e2006-01-09 15:32:41 -0200481static void set_volume(struct i2c_client *client, int volume)
Hans Verkuilbd985162005-11-13 16:07:56 -0800482{
Hans Verkuil87410da2007-08-05 08:00:36 -0300483 int vol;
484
Hans Verkuil87410da2007-08-05 08:00:36 -0300485 /* Convert the volume to msp3400 values (0-127) */
486 vol = volume >> 9;
487
Hans Verkuilbd985162005-11-13 16:07:56 -0800488 /* now scale it up to cx25840 values
489 * -114dB to -96dB maps to 0
490 * this should be 19, but in my testing that was 4dB too loud */
491 if (vol <= 23) {
492 vol = 0;
493 } else {
494 vol -= 23;
495 }
496
497 /* PATH1_VOLUME */
498 cx25840_write(client, 0x8d4, 228 - (vol * 2));
499}
500
Hans Verkuild92c20e2006-01-09 15:32:41 -0200501static void set_balance(struct i2c_client *client, int balance)
Hans Verkuilbd985162005-11-13 16:07:56 -0800502{
503 int bal = balance >> 8;
504 if (bal > 0x80) {
505 /* PATH1_BAL_LEFT */
506 cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
507 /* PATH1_BAL_LEVEL */
508 cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
509 } else {
510 /* PATH1_BAL_LEFT */
511 cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
512 /* PATH1_BAL_LEVEL */
513 cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
514 }
515}
516
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300517int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
Hans Verkuilbd985162005-11-13 16:07:56 -0800518{
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300519 struct i2c_client *client = v4l2_get_subdevdata(sd);
520 struct cx25840_state *state = to_state(sd);
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300521 int retval;
Hans Verkuilbd985162005-11-13 16:07:56 -0800522
Andy Walls2a03f032009-09-26 23:47:21 -0300523 if (!is_cx2583x(state))
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300524 cx25840_and_or(client, 0x810, ~0x1, 1);
525 if (state->aud_input != CX25840_AUDIO_SERIAL) {
526 cx25840_and_or(client, 0x803, ~0x10, 0);
527 cx25840_write(client, 0x8d3, 0x1f);
528 }
529 retval = set_audclk_freq(client, freq);
530 if (state->aud_input != CX25840_AUDIO_SERIAL)
531 cx25840_and_or(client, 0x803, ~0x10, 0x10);
Andy Walls2a03f032009-09-26 23:47:21 -0300532 if (!is_cx2583x(state))
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300533 cx25840_and_or(client, 0x810, ~0x1, 0);
534 return retval;
535}
Hans Verkuila8bbf122006-01-09 15:25:42 -0200536
Hans Verkuile34e6582010-08-06 10:55:39 -0300537static int cx25840_audio_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300538{
Hans Verkuile34e6582010-08-06 10:55:39 -0300539 struct v4l2_subdev *sd = to_sd(ctrl);
540 struct cx25840_state *state = to_state(sd);
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300541 struct i2c_client *client = v4l2_get_subdevdata(sd);
542
543 switch (ctrl->id) {
544 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuile34e6582010-08-06 10:55:39 -0300545 if (state->mute->val)
546 set_volume(client, 0);
547 else
548 set_volume(client, state->volume->val);
Hans Verkuilbd985162005-11-13 16:07:56 -0800549 break;
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300550 case V4L2_CID_AUDIO_BASS:
Hans Verkuile34e6582010-08-06 10:55:39 -0300551 /* PATH1_EQ_BASS_VOL */
552 cx25840_and_or(client, 0x8d9, ~0x3f,
553 48 - (ctrl->val * 48 / 0xffff));
Hans Verkuilbd985162005-11-13 16:07:56 -0800554 break;
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300555 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuile34e6582010-08-06 10:55:39 -0300556 /* PATH1_EQ_TREBLE_VOL */
557 cx25840_and_or(client, 0x8db, ~0x3f,
558 48 - (ctrl->val * 48 / 0xffff));
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300559 break;
560 case V4L2_CID_AUDIO_BALANCE:
Hans Verkuile34e6582010-08-06 10:55:39 -0300561 set_balance(client, ctrl->val);
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300562 break;
Hans Verkuilbd985162005-11-13 16:07:56 -0800563 default:
564 return -EINVAL;
565 }
Hans Verkuildf1d5ed2009-03-30 06:26:40 -0300566 return 0;
567}
Hans Verkuilbd985162005-11-13 16:07:56 -0800568
Hans Verkuile34e6582010-08-06 10:55:39 -0300569const struct v4l2_ctrl_ops cx25840_audio_ctrl_ops = {
570 .s_ctrl = cx25840_audio_s_ctrl,
571};