Merge git://git.kernel.org/pub/scm/linux/kernel/git/bart/ide-2.6
[sfrench/cifs-2.6.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 = 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         cx25840_write(client, 0x127, 0x50);
36
37         if (state->aud_input != CX25840_AUDIO_SERIAL) {
38                 switch (freq) {
39                 case 32000:
40                         /* VID_PLL and AUX_PLL */
41                         cx25840_write4(client, 0x108, 0x0f040610);
42
43                         /* AUX_PLL_FRAC */
44                         cx25840_write4(client, 0x110, 0xee39bb01);
45
46                         if (state->is_cx25836)
47                                 break;
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 44100:
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                         if (state->is_cx25836)
63                                 break;
64
65                         /* src3/4/6_ctl = 0x08016d59 */
66                         cx25840_write4(client, 0x900, 0x596d0108);
67                         cx25840_write4(client, 0x904, 0x596d0108);
68                         cx25840_write4(client, 0x90c, 0x596d0108);
69                         break;
70
71                 case 48000:
72                         /* VID_PLL and AUX_PLL */
73                         cx25840_write4(client, 0x108, 0x0f040a10);
74
75                         /* AUX_PLL_FRAC */
76                         cx25840_write4(client, 0x110, 0xe5d69800);
77
78                         if (state->is_cx25836)
79                                 break;
80
81                         /* src3/4/6_ctl = 0x08014faa */
82                         cx25840_write4(client, 0x900, 0xaa4f0108);
83                         cx25840_write4(client, 0x904, 0xaa4f0108);
84                         cx25840_write4(client, 0x90c, 0xaa4f0108);
85                         break;
86                 }
87         } else {
88                 switch (freq) {
89                 case 32000:
90                         /* VID_PLL and AUX_PLL */
91                         cx25840_write4(client, 0x108, 0x0f04081e);
92
93                         /* AUX_PLL_FRAC */
94                         cx25840_write4(client, 0x110, 0x69082a01);
95
96                         if (state->is_cx25836)
97                                 break;
98
99                         /* src1_ctl = 0x08010000 */
100                         cx25840_write4(client, 0x8f8, 0x00000108);
101
102                         /* src3/4/6_ctl = 0x08020000 */
103                         cx25840_write4(client, 0x900, 0x00000208);
104                         cx25840_write4(client, 0x904, 0x00000208);
105                         cx25840_write4(client, 0x90c, 0x00000208);
106
107                         /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
108                         cx25840_write(client, 0x127, 0x54);
109                         break;
110
111                 case 44100:
112                         /* VID_PLL and AUX_PLL */
113                         cx25840_write4(client, 0x108, 0x0f040918);
114
115                         /* AUX_PLL_FRAC */
116                         cx25840_write4(client, 0x110, 0xd66bec00);
117
118                         if (state->is_cx25836)
119                                 break;
120
121                         /* src1_ctl = 0x08010000 */
122                         cx25840_write4(client, 0x8f8, 0xcd600108);
123
124                         /* src3/4/6_ctl = 0x08020000 */
125                         cx25840_write4(client, 0x900, 0x85730108);
126                         cx25840_write4(client, 0x904, 0x85730108);
127                         cx25840_write4(client, 0x90c, 0x85730108);
128                         break;
129
130                 case 48000:
131                         /* VID_PLL and AUX_PLL */
132                         cx25840_write4(client, 0x108, 0x0f040a18);
133
134                         /* AUX_PLL_FRAC */
135                         cx25840_write4(client, 0x110, 0xe5d69800);
136
137                         if (state->is_cx25836)
138                                 break;
139
140                         /* src1_ctl = 0x08010000 */
141                         cx25840_write4(client, 0x8f8, 0x00800108);
142
143                         /* src3/4/6_ctl = 0x08020000 */
144                         cx25840_write4(client, 0x900, 0x55550108);
145                         cx25840_write4(client, 0x904, 0x55550108);
146                         cx25840_write4(client, 0x90c, 0x55550108);
147                         break;
148                 }
149         }
150
151         state->audclk_freq = freq;
152
153         return 0;
154 }
155
156 void cx25840_audio_set_path(struct i2c_client *client)
157 {
158         struct cx25840_state *state = i2c_get_clientdata(client);
159
160         /* assert soft reset */
161         cx25840_and_or(client, 0x810, ~0x1, 0x01);
162
163         /* 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
169         if (state->aud_input == CX25840_AUDIO_SERIAL) {
170                 /* Set Path1 to Serial Audio Input */
171                 cx25840_write4(client, 0x8d0, 0x12100101);
172
173                 /* The microcontroller should not be started for the
174                  * non-tuner inputs: autodetection is specific for
175                  * TV audio. */
176         } else {
177                 /* Set Path1 to Analog Demod Main Channel */
178                 cx25840_write4(client, 0x8d0, 0x7038061f);
179         }
180
181         set_audclk_freq(client, state->audclk_freq);
182
183         if (state->aud_input != CX25840_AUDIO_SERIAL) {
184                 /* When the microcontroller detects the
185                  * audio format, it will unmute the lines */
186                 cx25840_and_or(client, 0x803, ~0x10, 0x10);
187         }
188
189         /* deassert soft reset */
190         cx25840_and_or(client, 0x810, ~0x1, 0x00);
191 }
192
193 static int get_volume(struct i2c_client *client)
194 {
195         struct cx25840_state *state = i2c_get_clientdata(client);
196         int vol;
197
198         if (state->unmute_volume >= 0)
199                 return state->unmute_volume;
200
201         /* Volume runs +18dB to -96dB in 1/2dB steps
202          * change to fit the msp3400 -114dB to +12dB range */
203
204         /* check PATH1_VOLUME */
205         vol = 228 - cx25840_read(client, 0x8d4);
206         vol = (vol / 2) + 23;
207         return vol << 9;
208 }
209
210 static void set_volume(struct i2c_client *client, int volume)
211 {
212         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
223         /* 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
236 static int get_bass(struct i2c_client *client)
237 {
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
246 static void set_bass(struct i2c_client *client, int bass)
247 {
248         /* PATH1_EQ_BASS_VOL */
249         cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
250 }
251
252 static int get_treble(struct i2c_client *client)
253 {
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
262 static void set_treble(struct i2c_client *client, int treble)
263 {
264         /* PATH1_EQ_TREBLE_VOL */
265         cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
266 }
267
268 static int get_balance(struct i2c_client *client)
269 {
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
282 static void set_balance(struct i2c_client *client, int balance)
283 {
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
298 static int get_mute(struct i2c_client *client)
299 {
300         struct cx25840_state *state = i2c_get_clientdata(client);
301
302         return state->unmute_volume >= 0;
303 }
304
305 static void set_mute(struct i2c_client *client, int mute)
306 {
307         struct cx25840_state *state = i2c_get_clientdata(client);
308
309         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);
320         }
321 }
322
323 int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
324 {
325         struct cx25840_state *state = i2c_get_clientdata(client);
326         struct v4l2_control *ctrl = arg;
327         int retval;
328
329         switch (cmd) {
330         case VIDIOC_INT_AUDIO_CLOCK_FREQ:
331                 if (!state->is_cx25836)
332                         cx25840_and_or(client, 0x810, ~0x1, 1);
333                 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                 }
341                 if (!state->is_cx25836)
342                         cx25840_and_or(client, 0x810, ~0x1, 0);
343                 return retval;
344
345         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;
366
367         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;
388
389         default:
390                 return -EINVAL;
391         }
392
393         return 0;
394 }