blob: 6c44bd9c1704fcd1624ef62c4f0d89a944319e6b [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>
21#include <media/audiochip.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080022#include <media/v4l2-common.h>
23
24#include "cx25840.h"
25
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 /* assert soft reset */
34 cx25840_and_or(client, 0x810, ~0x1, 0x01);
35
36 /* common for all inputs and rates */
37 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
38 cx25840_write(client, 0x127, 0x50);
39
40 switch (state->audio_input) {
41 case AUDIO_TUNER:
42 switch (freq) {
Hans Verkuil3578d3d2006-01-09 15:25:41 -020043 case 32000:
Hans Verkuilbd985162005-11-13 16:07:56 -080044 /* VID_PLL and AUX_PLL */
45 cx25840_write4(client, 0x108, 0x0f040610);
46
47 /* AUX_PLL_FRAC */
48 cx25840_write4(client, 0x110, 0xee39bb01);
49
50 /* src3/4/6_ctl = 0x0801f77f */
51 cx25840_write4(client, 0x900, 0x7ff70108);
52 cx25840_write4(client, 0x904, 0x7ff70108);
53 cx25840_write4(client, 0x90c, 0x7ff70108);
54 break;
55
Hans Verkuil3578d3d2006-01-09 15:25:41 -020056 case 44100:
Hans Verkuilbd985162005-11-13 16:07:56 -080057 /* VID_PLL and AUX_PLL */
58 cx25840_write4(client, 0x108, 0x0f040910);
59
60 /* AUX_PLL_FRAC */
61 cx25840_write4(client, 0x110, 0xd66bec00);
62
63 /* src3/4/6_ctl = 0x08016d59 */
64 cx25840_write4(client, 0x900, 0x596d0108);
65 cx25840_write4(client, 0x904, 0x596d0108);
66 cx25840_write4(client, 0x90c, 0x596d0108);
67 break;
68
Hans Verkuil3578d3d2006-01-09 15:25:41 -020069 case 48000:
Hans Verkuilbd985162005-11-13 16:07:56 -080070 /* VID_PLL and AUX_PLL */
71 cx25840_write4(client, 0x108, 0x0f040a10);
72
73 /* AUX_PLL_FRAC */
74 cx25840_write4(client, 0x110, 0xe5d69800);
75
76 /* src3/4/6_ctl = 0x08014faa */
77 cx25840_write4(client, 0x900, 0xaa4f0108);
78 cx25840_write4(client, 0x904, 0xaa4f0108);
79 cx25840_write4(client, 0x90c, 0xaa4f0108);
80 break;
81 }
82 break;
83
84 case AUDIO_EXTERN_1:
85 case AUDIO_EXTERN_2:
86 case AUDIO_INTERN:
87 case AUDIO_RADIO:
88 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 */
91 cx25840_write4(client, 0x108, 0x0f04081e);
92
93 /* AUX_PLL_FRAC */
94 cx25840_write4(client, 0x110, 0x69082a01);
95
96 /* src1_ctl = 0x08010000 */
97 cx25840_write4(client, 0x8f8, 0x00000108);
98
99 /* src3/4/6_ctl = 0x08020000 */
100 cx25840_write4(client, 0x900, 0x00000208);
101 cx25840_write4(client, 0x904, 0x00000208);
102 cx25840_write4(client, 0x90c, 0x00000208);
103
104 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
105 cx25840_write(client, 0x127, 0x54);
106 break;
107
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200108 case 44100:
Hans Verkuilbd985162005-11-13 16:07:56 -0800109 /* VID_PLL and AUX_PLL */
110 cx25840_write4(client, 0x108, 0x0f040918);
111
112 /* AUX_PLL_FRAC */
113 cx25840_write4(client, 0x110, 0xd66bec00);
114
115 /* src1_ctl = 0x08010000 */
116 cx25840_write4(client, 0x8f8, 0xcd600108);
117
118 /* src3/4/6_ctl = 0x08020000 */
119 cx25840_write4(client, 0x900, 0x85730108);
120 cx25840_write4(client, 0x904, 0x85730108);
121 cx25840_write4(client, 0x90c, 0x85730108);
122 break;
123
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200124 case 48000:
Hans Verkuilbd985162005-11-13 16:07:56 -0800125 /* VID_PLL and AUX_PLL */
126 cx25840_write4(client, 0x108, 0x0f040a18);
127
128 /* AUX_PLL_FRAC */
129 cx25840_write4(client, 0x110, 0xe5d69800);
130
131 /* src1_ctl = 0x08010000 */
132 cx25840_write4(client, 0x8f8, 0x00800108);
133
134 /* src3/4/6_ctl = 0x08020000 */
135 cx25840_write4(client, 0x900, 0x55550108);
136 cx25840_write4(client, 0x904, 0x55550108);
137 cx25840_write4(client, 0x90c, 0x55550108);
138 break;
139 }
140 break;
141 }
142
143 /* deassert soft reset */
144 cx25840_and_or(client, 0x810, ~0x1, 0x00);
145
146 state->audclk_freq = freq;
147
148 return 0;
149}
150
151static int set_input(struct i2c_client *client, int audio_input)
152{
153 struct cx25840_state *state = i2c_get_clientdata(client);
154
155 cx25840_dbg("set audio input (%d)\n", audio_input);
156
157 /* stop microcontroller */
158 cx25840_and_or(client, 0x803, ~0x10, 0);
159
160 /* Mute everything to prevent the PFFT! */
161 cx25840_write(client, 0x8d3, 0x1f);
162
163 switch (audio_input) {
164 case AUDIO_TUNER:
165 /* Set Path1 to Analog Demod Main Channel */
166 cx25840_write4(client, 0x8d0, 0x7038061f);
167
168 /* When the microcontroller detects the
169 * audio format, it will unmute the lines */
170 cx25840_and_or(client, 0x803, ~0x10, 0x10);
171 break;
172
173 case AUDIO_EXTERN_1:
174 case AUDIO_EXTERN_2:
175 case AUDIO_INTERN:
176 case AUDIO_RADIO:
177 /* Set Path1 to Serial Audio Input */
178 cx25840_write4(client, 0x8d0, 0x12100101);
179
180 /* The microcontroller should not be started for the
181 * non-tuner inputs: autodetection is specific for
182 * TV audio. */
183 break;
184
185 default:
186 cx25840_dbg("Invalid audio input selection %d\n", audio_input);
187 return -EINVAL;
188 }
189
190 state->audio_input = audio_input;
191
192 return set_audclk_freq(client, state->audclk_freq);
193}
194
195inline static int get_volume(struct i2c_client *client)
196{
197 /* Volume runs +18dB to -96dB in 1/2dB steps
198 * change to fit the msp3400 -114dB to +12dB range */
199
200 /* check PATH1_VOLUME */
201 int vol = 228 - cx25840_read(client, 0x8d4);
202 vol = (vol / 2) + 23;
203 return vol << 9;
204}
205
206inline static void set_volume(struct i2c_client *client, int volume)
207{
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;
217 }
218
219 /* PATH1_VOLUME */
220 cx25840_write(client, 0x8d4, 228 - (vol * 2));
221}
222
223inline static int get_bass(struct i2c_client *client)
224{
225 /* bass is 49 steps +12dB to -12dB */
226
227 /* check PATH1_EQ_BASS_VOL */
228 int bass = cx25840_read(client, 0x8d9) & 0x3f;
229 bass = (((48 - bass) * 0xffff) + 47) / 48;
230 return bass;
231}
232
233inline static void set_bass(struct i2c_client *client, int bass)
234{
235 /* PATH1_EQ_BASS_VOL */
236 cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
237}
238
239inline static int get_treble(struct i2c_client *client)
240{
241 /* treble is 49 steps +12dB to -12dB */
242
243 /* check PATH1_EQ_TREBLE_VOL */
244 int treble = cx25840_read(client, 0x8db) & 0x3f;
245 treble = (((48 - treble) * 0xffff) + 47) / 48;
246 return treble;
247}
248
249inline static void set_treble(struct i2c_client *client, int treble)
250{
251 /* PATH1_EQ_TREBLE_VOL */
252 cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
253}
254
255inline static int get_balance(struct i2c_client *client)
256{
257 /* balance is 7 bit, 0 to -96dB */
258
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;
267}
268
269inline static void set_balance(struct i2c_client *client, int balance)
270{
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);
282 }
283}
284
285inline static int get_mute(struct i2c_client *client)
286{
287 /* check SRC1_MUTE_EN */
288 return cx25840_read(client, 0x8d3) & 0x2 ? 1 : 0;
289}
290
291inline static void set_mute(struct i2c_client *client, int mute)
292{
293 struct cx25840_state *state = i2c_get_clientdata(client);
294
295 if (state->audio_input == AUDIO_TUNER) {
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);
307 }
308 } else {
309 /* SRC1_MUTE_EN */
310 cx25840_and_or(client, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
311 }
312}
313
314int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
315{
316 struct v4l2_control *ctrl = arg;
317
318 switch (cmd) {
319 case AUDC_SET_INPUT:
320 return set_input(client, *(int *)arg);
321 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200322 return set_audclk_freq(client, *(u32 *)arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800323 case VIDIOC_G_CTRL:
324 switch (ctrl->id) {
325 case V4L2_CID_AUDIO_VOLUME:
326 ctrl->value = get_volume(client);
327 break;
328 case V4L2_CID_AUDIO_BASS:
329 ctrl->value = get_bass(client);
330 break;
331 case V4L2_CID_AUDIO_TREBLE:
332 ctrl->value = get_treble(client);
333 break;
334 case V4L2_CID_AUDIO_BALANCE:
335 ctrl->value = get_balance(client);
336 break;
337 case V4L2_CID_AUDIO_MUTE:
338 ctrl->value = get_mute(client);
339 break;
340 default:
341 return -EINVAL;
342 }
343 break;
344 case VIDIOC_S_CTRL:
345 switch (ctrl->id) {
346 case V4L2_CID_AUDIO_VOLUME:
347 set_volume(client, ctrl->value);
348 break;
349 case V4L2_CID_AUDIO_BASS:
350 set_bass(client, ctrl->value);
351 break;
352 case V4L2_CID_AUDIO_TREBLE:
353 set_treble(client, ctrl->value);
354 break;
355 case V4L2_CID_AUDIO_BALANCE:
356 set_balance(client, ctrl->value);
357 break;
358 case V4L2_CID_AUDIO_MUTE:
359 set_mute(client, ctrl->value);
360 break;
361 default:
362 return -EINVAL;
363 }
364 break;
365 default:
366 return -EINVAL;
367 }
368
369 return 0;
370}