blob: 51fc0af01578e01e14b564141b08eed8c1a74926 [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
Hans Verkuil3578d3d2006-01-09 15:25:41 -020026static int set_audclk_freq(struct i2c_client *client, u32 freq)
Hans Verkuilbd985162005-11-13 16:07:56 -080027{
28 struct cx25840_state *state = i2c_get_clientdata(client);
29
Hans Verkuil3578d3d2006-01-09 15:25:41 -020030 if (freq != 32000 && freq != 44100 && freq != 48000)
31 return -EINVAL;
32
Hans Verkuilbd985162005-11-13 16:07:56 -080033 /* common for all inputs and rates */
34 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
35 cx25840_write(client, 0x127, 0x50);
36
Hans Verkuila8bbf122006-01-09 15:25:42 -020037 if (state->aud_input != CX25840_AUDIO_SERIAL) {
Hans Verkuilbd985162005-11-13 16:07:56 -080038 switch (freq) {
Hans Verkuil3578d3d2006-01-09 15:25:41 -020039 case 32000:
Hans Verkuilbd985162005-11-13 16:07:56 -080040 /* VID_PLL and AUX_PLL */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030041 cx25840_write4(client, 0x108, 0x1006040f);
Hans Verkuilbd985162005-11-13 16:07:56 -080042
43 /* AUX_PLL_FRAC */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030044 cx25840_write4(client, 0x110, 0x01bb39ee);
Hans Verkuilbd985162005-11-13 16:07:56 -080045
Hans Verkuile2b8cf42006-04-22 10:22:46 -030046 if (state->is_cx25836)
47 break;
48
Hans Verkuilbd985162005-11-13 16:07:56 -080049 /* src3/4/6_ctl = 0x0801f77f */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030050 cx25840_write4(client, 0x900, 0x0801f77f);
51 cx25840_write4(client, 0x904, 0x0801f77f);
52 cx25840_write4(client, 0x90c, 0x0801f77f);
Hans Verkuilbd985162005-11-13 16:07:56 -080053 break;
54
Hans Verkuil3578d3d2006-01-09 15:25:41 -020055 case 44100:
Hans Verkuilbd985162005-11-13 16:07:56 -080056 /* VID_PLL and AUX_PLL */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030057 cx25840_write4(client, 0x108, 0x1009040f);
Hans Verkuilbd985162005-11-13 16:07:56 -080058
59 /* AUX_PLL_FRAC */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030060 cx25840_write4(client, 0x110, 0x00ec6bd6);
Hans Verkuilbd985162005-11-13 16:07:56 -080061
Hans Verkuile2b8cf42006-04-22 10:22:46 -030062 if (state->is_cx25836)
63 break;
64
Hans Verkuilbd985162005-11-13 16:07:56 -080065 /* src3/4/6_ctl = 0x08016d59 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030066 cx25840_write4(client, 0x900, 0x08016d59);
67 cx25840_write4(client, 0x904, 0x08016d59);
68 cx25840_write4(client, 0x90c, 0x08016d59);
Hans Verkuilbd985162005-11-13 16:07:56 -080069 break;
70
Hans Verkuil3578d3d2006-01-09 15:25:41 -020071 case 48000:
Hans Verkuilbd985162005-11-13 16:07:56 -080072 /* VID_PLL and AUX_PLL */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030073 cx25840_write4(client, 0x108, 0x100a040f);
Hans Verkuilbd985162005-11-13 16:07:56 -080074
75 /* AUX_PLL_FRAC */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030076 cx25840_write4(client, 0x110, 0x0098d6e5);
Hans Verkuilbd985162005-11-13 16:07:56 -080077
Hans Verkuile2b8cf42006-04-22 10:22:46 -030078 if (state->is_cx25836)
79 break;
80
Hans Verkuilbd985162005-11-13 16:07:56 -080081 /* src3/4/6_ctl = 0x08014faa */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030082 cx25840_write4(client, 0x900, 0x08014faa);
83 cx25840_write4(client, 0x904, 0x08014faa);
84 cx25840_write4(client, 0x90c, 0x08014faa);
Hans Verkuilbd985162005-11-13 16:07:56 -080085 break;
86 }
Hans Verkuila8bbf122006-01-09 15:25:42 -020087 } else {
Hans Verkuilbd985162005-11-13 16:07:56 -080088 switch (freq) {
Hans Verkuil3578d3d2006-01-09 15:25:41 -020089 case 32000:
Hans Verkuilbd985162005-11-13 16:07:56 -080090 /* VID_PLL and AUX_PLL */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030091 cx25840_write4(client, 0x108, 0x1e08040f);
Hans Verkuilbd985162005-11-13 16:07:56 -080092
93 /* AUX_PLL_FRAC */
Hans Verkuil4a56eb32007-12-02 07:03:45 -030094 cx25840_write4(client, 0x110, 0x012a0869);
Hans Verkuilbd985162005-11-13 16:07:56 -080095
Hans Verkuile2b8cf42006-04-22 10:22:46 -030096 if (state->is_cx25836)
97 break;
98
Hans Verkuilbd985162005-11-13 16:07:56 -080099 /* src1_ctl = 0x08010000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300100 cx25840_write4(client, 0x8f8, 0x08010000);
Hans Verkuilbd985162005-11-13 16:07:56 -0800101
102 /* src3/4/6_ctl = 0x08020000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300103 cx25840_write4(client, 0x900, 0x08020000);
104 cx25840_write4(client, 0x904, 0x08020000);
105 cx25840_write4(client, 0x90c, 0x08020000);
Hans Verkuilbd985162005-11-13 16:07:56 -0800106
107 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
108 cx25840_write(client, 0x127, 0x54);
109 break;
110
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200111 case 44100:
Hans Verkuilbd985162005-11-13 16:07:56 -0800112 /* VID_PLL and AUX_PLL */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300113 cx25840_write4(client, 0x108, 0x1809040f);
Hans Verkuilbd985162005-11-13 16:07:56 -0800114
115 /* AUX_PLL_FRAC */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300116 cx25840_write4(client, 0x110, 0x00ec6bd6);
Hans Verkuilbd985162005-11-13 16:07:56 -0800117
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300118 if (state->is_cx25836)
119 break;
120
Hans Verkuilbd985162005-11-13 16:07:56 -0800121 /* src1_ctl = 0x08010000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300122 cx25840_write4(client, 0x8f8, 0x080160cd);
Hans Verkuilbd985162005-11-13 16:07:56 -0800123
124 /* src3/4/6_ctl = 0x08020000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300125 cx25840_write4(client, 0x900, 0x08017385);
126 cx25840_write4(client, 0x904, 0x08017385);
127 cx25840_write4(client, 0x90c, 0x08017385);
Hans Verkuilbd985162005-11-13 16:07:56 -0800128 break;
129
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200130 case 48000:
Hans Verkuilbd985162005-11-13 16:07:56 -0800131 /* VID_PLL and AUX_PLL */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300132 cx25840_write4(client, 0x108, 0x180a040f);
Hans Verkuilbd985162005-11-13 16:07:56 -0800133
134 /* AUX_PLL_FRAC */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300135 cx25840_write4(client, 0x110, 0x0098d6e5);
Hans Verkuilbd985162005-11-13 16:07:56 -0800136
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300137 if (state->is_cx25836)
138 break;
139
Hans Verkuilbd985162005-11-13 16:07:56 -0800140 /* src1_ctl = 0x08010000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300141 cx25840_write4(client, 0x8f8, 0x08018000);
Hans Verkuilbd985162005-11-13 16:07:56 -0800142
143 /* src3/4/6_ctl = 0x08020000 */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300144 cx25840_write4(client, 0x900, 0x08015555);
145 cx25840_write4(client, 0x904, 0x08015555);
146 cx25840_write4(client, 0x90c, 0x08015555);
Hans Verkuilbd985162005-11-13 16:07:56 -0800147 break;
148 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800149 }
150
Hans Verkuilbd985162005-11-13 16:07:56 -0800151 state->audclk_freq = freq;
152
153 return 0;
154}
155
Hans Verkuila8bbf122006-01-09 15:25:42 -0200156void cx25840_audio_set_path(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800157{
158 struct cx25840_state *state = i2c_get_clientdata(client);
159
Hans Verkuil82677612007-08-07 07:16:07 -0300160 /* assert soft reset */
161 cx25840_and_or(client, 0x810, ~0x1, 0x01);
162
Hans Verkuilbd985162005-11-13 16:07:56 -0800163 /* stop microcontroller */
164 cx25840_and_or(client, 0x803, ~0x10, 0);
165
166 /* Mute everything to prevent the PFFT! */
167 cx25840_write(client, 0x8d3, 0x1f);
168
Hans Verkuila8bbf122006-01-09 15:25:42 -0200169 if (state->aud_input == CX25840_AUDIO_SERIAL) {
Hans Verkuilbd985162005-11-13 16:07:56 -0800170 /* Set Path1 to Serial Audio Input */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300171 cx25840_write4(client, 0x8d0, 0x01011012);
Hans Verkuilbd985162005-11-13 16:07:56 -0800172
173 /* The microcontroller should not be started for the
174 * non-tuner inputs: autodetection is specific for
175 * TV audio. */
Hans Verkuila8bbf122006-01-09 15:25:42 -0200176 } else {
177 /* Set Path1 to Analog Demod Main Channel */
Hans Verkuil4a56eb32007-12-02 07:03:45 -0300178 cx25840_write4(client, 0x8d0, 0x1f063870);
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300179 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800180
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300181 set_audclk_freq(client, state->audclk_freq);
182
183 if (state->aud_input != CX25840_AUDIO_SERIAL) {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200184 /* When the microcontroller detects the
185 * audio format, it will unmute the lines */
186 cx25840_and_or(client, 0x803, ~0x10, 0x10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800187 }
Hans Verkuil82677612007-08-07 07:16:07 -0300188
189 /* deassert soft reset */
190 cx25840_and_or(client, 0x810, ~0x1, 0x00);
Hans Verkuilbd985162005-11-13 16:07:56 -0800191}
192
Hans Verkuild92c20e2006-01-09 15:32:41 -0200193static int get_volume(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800194{
Hans Verkuil87410da2007-08-05 08:00:36 -0300195 struct cx25840_state *state = i2c_get_clientdata(client);
196 int vol;
197
198 if (state->unmute_volume >= 0)
199 return state->unmute_volume;
200
Hans Verkuilbd985162005-11-13 16:07:56 -0800201 /* Volume runs +18dB to -96dB in 1/2dB steps
202 * change to fit the msp3400 -114dB to +12dB range */
203
204 /* check PATH1_VOLUME */
Hans Verkuil87410da2007-08-05 08:00:36 -0300205 vol = 228 - cx25840_read(client, 0x8d4);
Hans Verkuilbd985162005-11-13 16:07:56 -0800206 vol = (vol / 2) + 23;
207 return vol << 9;
208}
209
Hans Verkuild92c20e2006-01-09 15:32:41 -0200210static void set_volume(struct i2c_client *client, int volume)
Hans Verkuilbd985162005-11-13 16:07:56 -0800211{
Hans Verkuil87410da2007-08-05 08:00:36 -0300212 struct cx25840_state *state = i2c_get_clientdata(client);
213 int vol;
214
215 if (state->unmute_volume >= 0) {
216 state->unmute_volume = volume;
217 return;
218 }
219
220 /* Convert the volume to msp3400 values (0-127) */
221 vol = volume >> 9;
222
Hans Verkuilbd985162005-11-13 16:07:56 -0800223 /* now scale it up to cx25840 values
224 * -114dB to -96dB maps to 0
225 * this should be 19, but in my testing that was 4dB too loud */
226 if (vol <= 23) {
227 vol = 0;
228 } else {
229 vol -= 23;
230 }
231
232 /* PATH1_VOLUME */
233 cx25840_write(client, 0x8d4, 228 - (vol * 2));
234}
235
Hans Verkuild92c20e2006-01-09 15:32:41 -0200236static int get_bass(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800237{
238 /* bass is 49 steps +12dB to -12dB */
239
240 /* check PATH1_EQ_BASS_VOL */
241 int bass = cx25840_read(client, 0x8d9) & 0x3f;
242 bass = (((48 - bass) * 0xffff) + 47) / 48;
243 return bass;
244}
245
Hans Verkuild92c20e2006-01-09 15:32:41 -0200246static void set_bass(struct i2c_client *client, int bass)
Hans Verkuilbd985162005-11-13 16:07:56 -0800247{
248 /* PATH1_EQ_BASS_VOL */
249 cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
250}
251
Hans Verkuild92c20e2006-01-09 15:32:41 -0200252static int get_treble(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800253{
254 /* treble is 49 steps +12dB to -12dB */
255
256 /* check PATH1_EQ_TREBLE_VOL */
257 int treble = cx25840_read(client, 0x8db) & 0x3f;
258 treble = (((48 - treble) * 0xffff) + 47) / 48;
259 return treble;
260}
261
Hans Verkuild92c20e2006-01-09 15:32:41 -0200262static void set_treble(struct i2c_client *client, int treble)
Hans Verkuilbd985162005-11-13 16:07:56 -0800263{
264 /* PATH1_EQ_TREBLE_VOL */
265 cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
266}
267
Hans Verkuild92c20e2006-01-09 15:32:41 -0200268static int get_balance(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800269{
270 /* balance is 7 bit, 0 to -96dB */
271
272 /* check PATH1_BAL_LEVEL */
273 int balance = cx25840_read(client, 0x8d5) & 0x7f;
274 /* check PATH1_BAL_LEFT */
275 if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
276 balance = 0x80 - balance;
277 else
278 balance = 0x80 + balance;
279 return balance << 8;
280}
281
Hans Verkuild92c20e2006-01-09 15:32:41 -0200282static void set_balance(struct i2c_client *client, int balance)
Hans Verkuilbd985162005-11-13 16:07:56 -0800283{
284 int bal = balance >> 8;
285 if (bal > 0x80) {
286 /* PATH1_BAL_LEFT */
287 cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
288 /* PATH1_BAL_LEVEL */
289 cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
290 } else {
291 /* PATH1_BAL_LEFT */
292 cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
293 /* PATH1_BAL_LEVEL */
294 cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
295 }
296}
297
Hans Verkuild92c20e2006-01-09 15:32:41 -0200298static int get_mute(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800299{
Hans Verkuil87410da2007-08-05 08:00:36 -0300300 struct cx25840_state *state = i2c_get_clientdata(client);
301
302 return state->unmute_volume >= 0;
Hans Verkuilbd985162005-11-13 16:07:56 -0800303}
304
Hans Verkuild92c20e2006-01-09 15:32:41 -0200305static void set_mute(struct i2c_client *client, int mute)
Hans Verkuilbd985162005-11-13 16:07:56 -0800306{
307 struct cx25840_state *state = i2c_get_clientdata(client);
308
Hans Verkuil87410da2007-08-05 08:00:36 -0300309 if (mute && state->unmute_volume == -1) {
310 int vol = get_volume(client);
311
312 set_volume(client, 0);
313 state->unmute_volume = vol;
314 }
315 else if (!mute && state->unmute_volume != -1) {
316 int vol = state->unmute_volume;
317
318 state->unmute_volume = -1;
319 set_volume(client, vol);
Hans Verkuilbd985162005-11-13 16:07:56 -0800320 }
321}
322
323int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
324{
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300325 struct cx25840_state *state = i2c_get_clientdata(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800326 struct v4l2_control *ctrl = arg;
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300327 int retval;
Hans Verkuilbd985162005-11-13 16:07:56 -0800328
329 switch (cmd) {
Hans Verkuilbd985162005-11-13 16:07:56 -0800330 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
Hans Verkuil82677612007-08-07 07:16:07 -0300331 if (!state->is_cx25836)
332 cx25840_and_or(client, 0x810, ~0x1, 1);
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300333 if (state->aud_input != CX25840_AUDIO_SERIAL) {
334 cx25840_and_or(client, 0x803, ~0x10, 0);
335 cx25840_write(client, 0x8d3, 0x1f);
336 }
337 retval = set_audclk_freq(client, *(u32 *)arg);
338 if (state->aud_input != CX25840_AUDIO_SERIAL) {
339 cx25840_and_or(client, 0x803, ~0x10, 0x10);
340 }
Hans Verkuil82677612007-08-07 07:16:07 -0300341 if (!state->is_cx25836)
342 cx25840_and_or(client, 0x810, ~0x1, 0);
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300343 return retval;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200344
Hans Verkuilbd985162005-11-13 16:07:56 -0800345 case VIDIOC_G_CTRL:
346 switch (ctrl->id) {
347 case V4L2_CID_AUDIO_VOLUME:
348 ctrl->value = get_volume(client);
349 break;
350 case V4L2_CID_AUDIO_BASS:
351 ctrl->value = get_bass(client);
352 break;
353 case V4L2_CID_AUDIO_TREBLE:
354 ctrl->value = get_treble(client);
355 break;
356 case V4L2_CID_AUDIO_BALANCE:
357 ctrl->value = get_balance(client);
358 break;
359 case V4L2_CID_AUDIO_MUTE:
360 ctrl->value = get_mute(client);
361 break;
362 default:
363 return -EINVAL;
364 }
365 break;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200366
Hans Verkuilbd985162005-11-13 16:07:56 -0800367 case VIDIOC_S_CTRL:
368 switch (ctrl->id) {
369 case V4L2_CID_AUDIO_VOLUME:
370 set_volume(client, ctrl->value);
371 break;
372 case V4L2_CID_AUDIO_BASS:
373 set_bass(client, ctrl->value);
374 break;
375 case V4L2_CID_AUDIO_TREBLE:
376 set_treble(client, ctrl->value);
377 break;
378 case V4L2_CID_AUDIO_BALANCE:
379 set_balance(client, ctrl->value);
380 break;
381 case V4L2_CID_AUDIO_MUTE:
382 set_mute(client, ctrl->value);
383 break;
384 default:
385 return -EINVAL;
386 }
387 break;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200388
Hans Verkuilbd985162005-11-13 16:07:56 -0800389 default:
390 return -EINVAL;
391 }
392
393 return 0;
394}