V4L/DVB (10952): cx25840: prepare it to be used by cx231xx module
[pandora-kernel.git] / drivers / media / video / cx25840 / cx25840-audio.c
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/v4l2-common.h>
22 #include <media/cx25840.h>
23
24 #include "cx25840-core.h"
25
26 static int set_audclk_freq(struct i2c_client *client, u32 freq)
27 {
28         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
29
30         if (freq != 32000 && freq != 44100 && freq != 48000)
31                 return -EINVAL;
32
33         /* common for all inputs and rates */
34         /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
35         if (!state->is_cx23885 && !state->is_cx231xx)
36                 cx25840_write(client, 0x127, 0x50);
37
38         if (state->aud_input != CX25840_AUDIO_SERIAL) {
39                 switch (freq) {
40                 case 32000:
41                         if (state->is_cx23885) {
42                                 /* We don't have register values
43                                  * so avoid destroying registers. */
44                                 break;
45                         }
46
47                         if (!state->is_cx231xx) {
48
49                                /* VID_PLL and AUX_PLL */
50                                cx25840_write4(client, 0x108, 0x1006040f);
51
52                                /* AUX_PLL_FRAC */
53                                cx25840_write4(client, 0x110, 0x01bb39ee);
54                         }
55
56                         if (state->is_cx25836)
57                                 break;
58
59                         /* src3/4/6_ctl = 0x0801f77f */
60                         cx25840_write4(client, 0x900, 0x0801f77f);
61                         cx25840_write4(client, 0x904, 0x0801f77f);
62                         cx25840_write4(client, 0x90c, 0x0801f77f);
63                         break;
64
65                 case 44100:
66                         if (state->is_cx23885) {
67                                 /* We don't have register values
68                                  * so avoid destroying registers. */
69                                 break;
70                         }
71
72                         if (!state->is_cx231xx) {
73
74                                 /* VID_PLL and AUX_PLL */
75                                 cx25840_write4(client, 0x108, 0x1009040f);
76
77                                 /* AUX_PLL_FRAC */
78                                 cx25840_write4(client, 0x110, 0x00ec6bd6);
79                         }
80
81                         if (state->is_cx25836)
82                                 break;
83
84                         /* src3/4/6_ctl = 0x08016d59 */
85                         cx25840_write4(client, 0x900, 0x08016d59);
86                         cx25840_write4(client, 0x904, 0x08016d59);
87                         cx25840_write4(client, 0x90c, 0x08016d59);
88                         break;
89
90                 case 48000:
91                         if (state->is_cx23885) {
92                                 /* We don't have register values
93                                  * so avoid destroying registers. */
94                                 break;
95                         }
96
97                         if (!state->is_cx231xx) {
98
99                         /* VID_PLL and AUX_PLL */
100                         cx25840_write4(client, 0x108, 0x100a040f);
101
102                         /* AUX_PLL_FRAC */
103                         cx25840_write4(client, 0x110, 0x0098d6e5);
104                         }
105
106                         if (state->is_cx25836)
107                                 break;
108
109                         /* src3/4/6_ctl = 0x08014faa */
110                         cx25840_write4(client, 0x900, 0x08014faa);
111                         cx25840_write4(client, 0x904, 0x08014faa);
112                         cx25840_write4(client, 0x90c, 0x08014faa);
113                         break;
114                 }
115         } else {
116                 switch (freq) {
117                 case 32000:
118                         if (state->is_cx23885) {
119                                 /* We don't have register values
120                                  * so avoid destroying registers. */
121                                 break;
122                         }
123
124                         if (!state->is_cx231xx) {
125
126                         /* VID_PLL and AUX_PLL */
127                         cx25840_write4(client, 0x108, 0x1e08040f);
128
129                         /* AUX_PLL_FRAC */
130                         cx25840_write4(client, 0x110, 0x012a0869);
131                         }
132
133                         if (state->is_cx25836)
134                                 break;
135
136                         /* src1_ctl = 0x08010000 */
137                         cx25840_write4(client, 0x8f8, 0x08010000);
138
139                         /* src3/4/6_ctl = 0x08020000 */
140                         cx25840_write4(client, 0x900, 0x08020000);
141                         cx25840_write4(client, 0x904, 0x08020000);
142                         cx25840_write4(client, 0x90c, 0x08020000);
143
144                         /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
145                         cx25840_write(client, 0x127, 0x54);
146                         break;
147
148                 case 44100:
149                         if (state->is_cx23885) {
150                                 /* We don't have register values
151                                  * so avoid destroying registers. */
152                                 break;
153                         }
154
155
156                         if (!state->is_cx231xx) {
157
158                         /* VID_PLL and AUX_PLL */
159                         cx25840_write4(client, 0x108, 0x1809040f);
160
161                         /* AUX_PLL_FRAC */
162                         cx25840_write4(client, 0x110, 0x00ec6bd6);
163                         }
164
165                         if (state->is_cx25836)
166                                 break;
167
168                         /* src1_ctl = 0x08010000 */
169                         cx25840_write4(client, 0x8f8, 0x080160cd);
170
171                         /* src3/4/6_ctl = 0x08020000 */
172                         cx25840_write4(client, 0x900, 0x08017385);
173                         cx25840_write4(client, 0x904, 0x08017385);
174                         cx25840_write4(client, 0x90c, 0x08017385);
175                         break;
176
177                 case 48000:
178                         if (!state->is_cx23885 && !state->is_cx231xx) {
179                                 /* VID_PLL and AUX_PLL */
180                                 cx25840_write4(client, 0x108, 0x180a040f);
181
182                                 /* AUX_PLL_FRAC */
183                                 cx25840_write4(client, 0x110, 0x0098d6e5);
184                         }
185
186                         if (state->is_cx25836)
187                                 break;
188
189                         if (!state->is_cx23885 && !state->is_cx231xx) {
190                                 /* src1_ctl */
191                                 cx25840_write4(client, 0x8f8, 0x08018000);
192
193                                 /* src3/4/6_ctl */
194                                 cx25840_write4(client, 0x900, 0x08015555);
195                                 cx25840_write4(client, 0x904, 0x08015555);
196                                 cx25840_write4(client, 0x90c, 0x08015555);
197                         } else {
198
199                                 cx25840_write4(client, 0x8f8, 0x0801867c);
200
201                                 cx25840_write4(client, 0x900, 0x08014faa);
202                                 cx25840_write4(client, 0x904, 0x08014faa);
203                                 cx25840_write4(client, 0x90c, 0x08014faa);
204                         }
205                         break;
206                 }
207         }
208
209         state->audclk_freq = freq;
210
211         return 0;
212 }
213
214 void cx25840_audio_set_path(struct i2c_client *client)
215 {
216         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
217
218         /* assert soft reset */
219         cx25840_and_or(client, 0x810, ~0x1, 0x01);
220
221         /* stop microcontroller */
222         cx25840_and_or(client, 0x803, ~0x10, 0);
223
224         /* Mute everything to prevent the PFFT! */
225         cx25840_write(client, 0x8d3, 0x1f);
226
227         if (state->aud_input == CX25840_AUDIO_SERIAL) {
228                 /* Set Path1 to Serial Audio Input */
229                 cx25840_write4(client, 0x8d0, 0x01011012);
230
231                 /* The microcontroller should not be started for the
232                  * non-tuner inputs: autodetection is specific for
233                  * TV audio. */
234         } else {
235                 /* Set Path1 to Analog Demod Main Channel */
236                 cx25840_write4(client, 0x8d0, 0x1f063870);
237         }
238
239         set_audclk_freq(client, state->audclk_freq);
240
241         if (state->aud_input != CX25840_AUDIO_SERIAL) {
242                 /* When the microcontroller detects the
243                  * audio format, it will unmute the lines */
244                 cx25840_and_or(client, 0x803, ~0x10, 0x10);
245         }
246
247         /* deassert soft reset */
248         cx25840_and_or(client, 0x810, ~0x1, 0x00);
249
250         if (state->is_cx23885 || state->is_cx231xx) {
251                 /* Ensure the controller is running when we exit */
252                 cx25840_and_or(client, 0x803, ~0x10, 0x10);
253         }
254 }
255
256 static int get_volume(struct i2c_client *client)
257 {
258         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
259         int vol;
260
261         if (state->unmute_volume >= 0)
262                 return state->unmute_volume;
263
264         /* Volume runs +18dB to -96dB in 1/2dB steps
265          * change to fit the msp3400 -114dB to +12dB range */
266
267         /* check PATH1_VOLUME */
268         vol = 228 - cx25840_read(client, 0x8d4);
269         vol = (vol / 2) + 23;
270         return vol << 9;
271 }
272
273 static void set_volume(struct i2c_client *client, int volume)
274 {
275         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
276         int vol;
277
278         if (state->unmute_volume >= 0) {
279                 state->unmute_volume = volume;
280                 return;
281         }
282
283         /* Convert the volume to msp3400 values (0-127) */
284         vol = volume >> 9;
285
286         /* now scale it up to cx25840 values
287          * -114dB to -96dB maps to 0
288          * this should be 19, but in my testing that was 4dB too loud */
289         if (vol <= 23) {
290                 vol = 0;
291         } else {
292                 vol -= 23;
293         }
294
295         /* PATH1_VOLUME */
296         cx25840_write(client, 0x8d4, 228 - (vol * 2));
297 }
298
299 static int get_bass(struct i2c_client *client)
300 {
301         /* bass is 49 steps +12dB to -12dB */
302
303         /* check PATH1_EQ_BASS_VOL */
304         int bass = cx25840_read(client, 0x8d9) & 0x3f;
305         bass = (((48 - bass) * 0xffff) + 47) / 48;
306         return bass;
307 }
308
309 static void set_bass(struct i2c_client *client, int bass)
310 {
311         /* PATH1_EQ_BASS_VOL */
312         cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
313 }
314
315 static int get_treble(struct i2c_client *client)
316 {
317         /* treble is 49 steps +12dB to -12dB */
318
319         /* check PATH1_EQ_TREBLE_VOL */
320         int treble = cx25840_read(client, 0x8db) & 0x3f;
321         treble = (((48 - treble) * 0xffff) + 47) / 48;
322         return treble;
323 }
324
325 static void set_treble(struct i2c_client *client, int treble)
326 {
327         /* PATH1_EQ_TREBLE_VOL */
328         cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
329 }
330
331 static int get_balance(struct i2c_client *client)
332 {
333         /* balance is 7 bit, 0 to -96dB */
334
335         /* check PATH1_BAL_LEVEL */
336         int balance = cx25840_read(client, 0x8d5) & 0x7f;
337         /* check PATH1_BAL_LEFT */
338         if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
339                 balance = 0x80 - balance;
340         else
341                 balance = 0x80 + balance;
342         return balance << 8;
343 }
344
345 static void set_balance(struct i2c_client *client, int balance)
346 {
347         int bal = balance >> 8;
348         if (bal > 0x80) {
349                 /* PATH1_BAL_LEFT */
350                 cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
351                 /* PATH1_BAL_LEVEL */
352                 cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
353         } else {
354                 /* PATH1_BAL_LEFT */
355                 cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
356                 /* PATH1_BAL_LEVEL */
357                 cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
358         }
359 }
360
361 static int get_mute(struct i2c_client *client)
362 {
363         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
364
365         return state->unmute_volume >= 0;
366 }
367
368 static void set_mute(struct i2c_client *client, int mute)
369 {
370         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
371
372         if (mute && state->unmute_volume == -1) {
373                 int vol = get_volume(client);
374
375                 set_volume(client, 0);
376                 state->unmute_volume = vol;
377         }
378         else if (!mute && state->unmute_volume != -1) {
379                 int vol = state->unmute_volume;
380
381                 state->unmute_volume = -1;
382                 set_volume(client, vol);
383         }
384 }
385
386 int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
387 {
388         struct i2c_client *client = v4l2_get_subdevdata(sd);
389         struct cx25840_state *state = to_state(sd);
390         int retval;
391
392         if (!state->is_cx25836)
393                 cx25840_and_or(client, 0x810, ~0x1, 1);
394         if (state->aud_input != CX25840_AUDIO_SERIAL) {
395                 cx25840_and_or(client, 0x803, ~0x10, 0);
396                 cx25840_write(client, 0x8d3, 0x1f);
397         }
398         retval = set_audclk_freq(client, freq);
399         if (state->aud_input != CX25840_AUDIO_SERIAL)
400                 cx25840_and_or(client, 0x803, ~0x10, 0x10);
401         if (!state->is_cx25836)
402                 cx25840_and_or(client, 0x810, ~0x1, 0);
403         return retval;
404 }
405
406 int cx25840_audio_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
407 {
408         struct i2c_client *client = v4l2_get_subdevdata(sd);
409
410         switch (ctrl->id) {
411         case V4L2_CID_AUDIO_VOLUME:
412                 ctrl->value = get_volume(client);
413                 break;
414         case V4L2_CID_AUDIO_BASS:
415                 ctrl->value = get_bass(client);
416                 break;
417         case V4L2_CID_AUDIO_TREBLE:
418                 ctrl->value = get_treble(client);
419                 break;
420         case V4L2_CID_AUDIO_BALANCE:
421                 ctrl->value = get_balance(client);
422                 break;
423         case V4L2_CID_AUDIO_MUTE:
424                 ctrl->value = get_mute(client);
425                 break;
426         default:
427                 return -EINVAL;
428         }
429         return 0;
430 }
431
432 int cx25840_audio_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
433 {
434         struct i2c_client *client = v4l2_get_subdevdata(sd);
435
436         switch (ctrl->id) {
437         case V4L2_CID_AUDIO_VOLUME:
438                 set_volume(client, ctrl->value);
439                 break;
440         case V4L2_CID_AUDIO_BASS:
441                 set_bass(client, ctrl->value);
442                 break;
443         case V4L2_CID_AUDIO_TREBLE:
444                 set_treble(client, ctrl->value);
445                 break;
446         case V4L2_CID_AUDIO_BALANCE:
447                 set_balance(client, ctrl->value);
448                 break;
449         case V4L2_CID_AUDIO_MUTE:
450                 set_mute(client, ctrl->value);
451                 break;
452         default:
453                 return -EINVAL;
454         }
455         return 0;
456 }