Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / media / video / cx25840 / cx25840-audio.c
blobf897c1ebd5f3d809f4bf6bf6f111b2754395a4bc
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)
31 return -EINVAL;
33 /* common for all inputs and rates */
34 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
35 cx25840_write(client, 0x127, 0x50);
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);
43 /* AUX_PLL_FRAC */
44 cx25840_write4(client, 0x110, 0xee39bb01);
46 if (state->is_cx25836)
47 break;
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;
55 case 44100:
56 /* VID_PLL and AUX_PLL */
57 cx25840_write4(client, 0x108, 0x0f040910);
59 /* AUX_PLL_FRAC */
60 cx25840_write4(client, 0x110, 0xd66bec00);
62 if (state->is_cx25836)
63 break;
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;
71 case 48000:
72 /* VID_PLL and AUX_PLL */
73 cx25840_write4(client, 0x108, 0x0f040a10);
75 /* AUX_PLL_FRAC */
76 cx25840_write4(client, 0x110, 0xe5d69800);
78 if (state->is_cx25836)
79 break;
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;
87 } else {
88 switch (freq) {
89 case 32000:
90 /* VID_PLL and AUX_PLL */
91 cx25840_write4(client, 0x108, 0x0f04081e);
93 /* AUX_PLL_FRAC */
94 cx25840_write4(client, 0x110, 0x69082a01);
96 if (state->is_cx25836)
97 break;
99 /* src1_ctl = 0x08010000 */
100 cx25840_write4(client, 0x8f8, 0x00000108);
102 /* src3/4/6_ctl = 0x08020000 */
103 cx25840_write4(client, 0x900, 0x00000208);
104 cx25840_write4(client, 0x904, 0x00000208);
105 cx25840_write4(client, 0x90c, 0x00000208);
107 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
108 cx25840_write(client, 0x127, 0x54);
109 break;
111 case 44100:
112 /* VID_PLL and AUX_PLL */
113 cx25840_write4(client, 0x108, 0x0f040918);
115 /* AUX_PLL_FRAC */
116 cx25840_write4(client, 0x110, 0xd66bec00);
118 if (state->is_cx25836)
119 break;
121 /* src1_ctl = 0x08010000 */
122 cx25840_write4(client, 0x8f8, 0xcd600108);
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;
130 case 48000:
131 /* VID_PLL and AUX_PLL */
132 cx25840_write4(client, 0x108, 0x0f040a18);
134 /* AUX_PLL_FRAC */
135 cx25840_write4(client, 0x110, 0xe5d69800);
137 if (state->is_cx25836)
138 break;
140 /* src1_ctl = 0x08010000 */
141 cx25840_write4(client, 0x8f8, 0x00800108);
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;
151 state->audclk_freq = freq;
153 return 0;
156 void cx25840_audio_set_path(struct i2c_client *client)
158 struct cx25840_state *state = i2c_get_clientdata(client);
160 /* stop microcontroller */
161 cx25840_and_or(client, 0x803, ~0x10, 0);
163 /* assert soft reset */
164 if (!state->is_cx25836)
165 cx25840_and_or(client, 0x810, ~0x1, 0x01);
167 /* Mute everything to prevent the PFFT! */
168 cx25840_write(client, 0x8d3, 0x1f);
170 if (state->aud_input == CX25840_AUDIO_SERIAL) {
171 /* Set Path1 to Serial Audio Input */
172 cx25840_write4(client, 0x8d0, 0x12100101);
174 /* The microcontroller should not be started for the
175 * non-tuner inputs: autodetection is specific for
176 * TV audio. */
177 } else {
178 /* Set Path1 to Analog Demod Main Channel */
179 cx25840_write4(client, 0x8d0, 0x7038061f);
182 set_audclk_freq(client, state->audclk_freq);
184 /* deassert soft reset */
185 if (!state->is_cx25836)
186 cx25840_and_or(client, 0x810, ~0x1, 0x00);
188 if (state->aud_input != CX25840_AUDIO_SERIAL) {
189 /* When the microcontroller detects the
190 * audio format, it will unmute the lines */
191 cx25840_and_or(client, 0x803, ~0x10, 0x10);
195 static int get_volume(struct i2c_client *client)
197 /* Volume runs +18dB to -96dB in 1/2dB steps
198 * change to fit the msp3400 -114dB to +12dB range */
200 /* check PATH1_VOLUME */
201 int vol = 228 - cx25840_read(client, 0x8d4);
202 vol = (vol / 2) + 23;
203 return vol << 9;
206 static void set_volume(struct i2c_client *client, int volume)
208 /* First convert the volume to msp3400 values (0-127) */
209 int vol = volume >> 9;
210 /* now scale it up to cx25840 values
211 * -114dB to -96dB maps to 0
212 * this should be 19, but in my testing that was 4dB too loud */
213 if (vol <= 23) {
214 vol = 0;
215 } else {
216 vol -= 23;
219 /* PATH1_VOLUME */
220 cx25840_write(client, 0x8d4, 228 - (vol * 2));
223 static int get_bass(struct i2c_client *client)
225 /* bass is 49 steps +12dB to -12dB */
227 /* check PATH1_EQ_BASS_VOL */
228 int bass = cx25840_read(client, 0x8d9) & 0x3f;
229 bass = (((48 - bass) * 0xffff) + 47) / 48;
230 return bass;
233 static void set_bass(struct i2c_client *client, int bass)
235 /* PATH1_EQ_BASS_VOL */
236 cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
239 static int get_treble(struct i2c_client *client)
241 /* treble is 49 steps +12dB to -12dB */
243 /* check PATH1_EQ_TREBLE_VOL */
244 int treble = cx25840_read(client, 0x8db) & 0x3f;
245 treble = (((48 - treble) * 0xffff) + 47) / 48;
246 return treble;
249 static void set_treble(struct i2c_client *client, int treble)
251 /* PATH1_EQ_TREBLE_VOL */
252 cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
255 static int get_balance(struct i2c_client *client)
257 /* balance is 7 bit, 0 to -96dB */
259 /* check PATH1_BAL_LEVEL */
260 int balance = cx25840_read(client, 0x8d5) & 0x7f;
261 /* check PATH1_BAL_LEFT */
262 if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
263 balance = 0x80 - balance;
264 else
265 balance = 0x80 + balance;
266 return balance << 8;
269 static void set_balance(struct i2c_client *client, int balance)
271 int bal = balance >> 8;
272 if (bal > 0x80) {
273 /* PATH1_BAL_LEFT */
274 cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
275 /* PATH1_BAL_LEVEL */
276 cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
277 } else {
278 /* PATH1_BAL_LEFT */
279 cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
280 /* PATH1_BAL_LEVEL */
281 cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
285 static int get_mute(struct i2c_client *client)
287 /* check SRC1_MUTE_EN */
288 return cx25840_read(client, 0x8d3) & 0x2 ? 1 : 0;
291 static void set_mute(struct i2c_client *client, int mute)
293 struct cx25840_state *state = i2c_get_clientdata(client);
295 if (state->aud_input != CX25840_AUDIO_SERIAL) {
296 /* Must turn off microcontroller in order to mute sound.
297 * Not sure if this is the best method, but it does work.
298 * If the microcontroller is running, then it will undo any
299 * changes to the mute register. */
300 if (mute) {
301 /* disable microcontroller */
302 cx25840_and_or(client, 0x803, ~0x10, 0x00);
303 cx25840_write(client, 0x8d3, 0x1f);
304 } else {
305 /* enable microcontroller */
306 cx25840_and_or(client, 0x803, ~0x10, 0x10);
308 } else {
309 /* SRC1_MUTE_EN */
310 cx25840_and_or(client, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
314 int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
316 struct cx25840_state *state = i2c_get_clientdata(client);
317 struct v4l2_control *ctrl = arg;
318 int retval;
320 switch (cmd) {
321 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
322 if (state->aud_input != CX25840_AUDIO_SERIAL) {
323 cx25840_and_or(client, 0x803, ~0x10, 0);
324 cx25840_write(client, 0x8d3, 0x1f);
326 if (!state->is_cx25836)
327 cx25840_and_or(client, 0x810, ~0x1, 1);
328 retval = set_audclk_freq(client, *(u32 *)arg);
329 if (!state->is_cx25836)
330 cx25840_and_or(client, 0x810, ~0x1, 0);
331 if (state->aud_input != CX25840_AUDIO_SERIAL) {
332 cx25840_and_or(client, 0x803, ~0x10, 0x10);
334 return retval;
336 case VIDIOC_G_CTRL:
337 switch (ctrl->id) {
338 case V4L2_CID_AUDIO_VOLUME:
339 ctrl->value = get_volume(client);
340 break;
341 case V4L2_CID_AUDIO_BASS:
342 ctrl->value = get_bass(client);
343 break;
344 case V4L2_CID_AUDIO_TREBLE:
345 ctrl->value = get_treble(client);
346 break;
347 case V4L2_CID_AUDIO_BALANCE:
348 ctrl->value = get_balance(client);
349 break;
350 case V4L2_CID_AUDIO_MUTE:
351 ctrl->value = get_mute(client);
352 break;
353 default:
354 return -EINVAL;
356 break;
358 case VIDIOC_S_CTRL:
359 switch (ctrl->id) {
360 case V4L2_CID_AUDIO_VOLUME:
361 set_volume(client, ctrl->value);
362 break;
363 case V4L2_CID_AUDIO_BASS:
364 set_bass(client, ctrl->value);
365 break;
366 case V4L2_CID_AUDIO_TREBLE:
367 set_treble(client, ctrl->value);
368 break;
369 case V4L2_CID_AUDIO_BALANCE:
370 set_balance(client, ctrl->value);
371 break;
372 case V4L2_CID_AUDIO_MUTE:
373 set_mute(client, ctrl->value);
374 break;
375 default:
376 return -EINVAL;
378 break;
380 default:
381 return -EINVAL;
384 return 0;