blob: b67ad4deff8a27a974d7115c83df3a658e045baf [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 /* assert soft reset */
Hans Verkuile2b8cf42006-04-22 10:22:46 -030034 if (!state->is_cx25836)
35 cx25840_and_or(client, 0x810, ~0x1, 0x01);
Hans Verkuilbd985162005-11-13 16:07:56 -080036
37 /* common for all inputs and rates */
38 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
39 cx25840_write(client, 0x127, 0x50);
40
Hans Verkuila8bbf122006-01-09 15:25:42 -020041 if (state->aud_input != CX25840_AUDIO_SERIAL) {
Hans Verkuilbd985162005-11-13 16:07:56 -080042 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
Hans Verkuile2b8cf42006-04-22 10:22:46 -030050 if (state->is_cx25836)
51 break;
52
Hans Verkuilbd985162005-11-13 16:07:56 -080053 /* src3/4/6_ctl = 0x0801f77f */
54 cx25840_write4(client, 0x900, 0x7ff70108);
55 cx25840_write4(client, 0x904, 0x7ff70108);
56 cx25840_write4(client, 0x90c, 0x7ff70108);
57 break;
58
Hans Verkuil3578d3d2006-01-09 15:25:41 -020059 case 44100:
Hans Verkuilbd985162005-11-13 16:07:56 -080060 /* VID_PLL and AUX_PLL */
61 cx25840_write4(client, 0x108, 0x0f040910);
62
63 /* AUX_PLL_FRAC */
64 cx25840_write4(client, 0x110, 0xd66bec00);
65
Hans Verkuile2b8cf42006-04-22 10:22:46 -030066 if (state->is_cx25836)
67 break;
68
Hans Verkuilbd985162005-11-13 16:07:56 -080069 /* src3/4/6_ctl = 0x08016d59 */
70 cx25840_write4(client, 0x900, 0x596d0108);
71 cx25840_write4(client, 0x904, 0x596d0108);
72 cx25840_write4(client, 0x90c, 0x596d0108);
73 break;
74
Hans Verkuil3578d3d2006-01-09 15:25:41 -020075 case 48000:
Hans Verkuilbd985162005-11-13 16:07:56 -080076 /* VID_PLL and AUX_PLL */
77 cx25840_write4(client, 0x108, 0x0f040a10);
78
79 /* AUX_PLL_FRAC */
80 cx25840_write4(client, 0x110, 0xe5d69800);
81
Hans Verkuile2b8cf42006-04-22 10:22:46 -030082 if (state->is_cx25836)
83 break;
84
Hans Verkuilbd985162005-11-13 16:07:56 -080085 /* src3/4/6_ctl = 0x08014faa */
86 cx25840_write4(client, 0x900, 0xaa4f0108);
87 cx25840_write4(client, 0x904, 0xaa4f0108);
88 cx25840_write4(client, 0x90c, 0xaa4f0108);
89 break;
90 }
Hans Verkuila8bbf122006-01-09 15:25:42 -020091 } else {
Hans Verkuilbd985162005-11-13 16:07:56 -080092 switch (freq) {
Hans Verkuil3578d3d2006-01-09 15:25:41 -020093 case 32000:
Hans Verkuilbd985162005-11-13 16:07:56 -080094 /* VID_PLL and AUX_PLL */
95 cx25840_write4(client, 0x108, 0x0f04081e);
96
97 /* AUX_PLL_FRAC */
98 cx25840_write4(client, 0x110, 0x69082a01);
99
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300100 if (state->is_cx25836)
101 break;
102
Hans Verkuilbd985162005-11-13 16:07:56 -0800103 /* src1_ctl = 0x08010000 */
104 cx25840_write4(client, 0x8f8, 0x00000108);
105
106 /* src3/4/6_ctl = 0x08020000 */
107 cx25840_write4(client, 0x900, 0x00000208);
108 cx25840_write4(client, 0x904, 0x00000208);
109 cx25840_write4(client, 0x90c, 0x00000208);
110
111 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
112 cx25840_write(client, 0x127, 0x54);
113 break;
114
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200115 case 44100:
Hans Verkuilbd985162005-11-13 16:07:56 -0800116 /* VID_PLL and AUX_PLL */
117 cx25840_write4(client, 0x108, 0x0f040918);
118
119 /* AUX_PLL_FRAC */
120 cx25840_write4(client, 0x110, 0xd66bec00);
121
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300122 if (state->is_cx25836)
123 break;
124
Hans Verkuilbd985162005-11-13 16:07:56 -0800125 /* src1_ctl = 0x08010000 */
126 cx25840_write4(client, 0x8f8, 0xcd600108);
127
128 /* src3/4/6_ctl = 0x08020000 */
129 cx25840_write4(client, 0x900, 0x85730108);
130 cx25840_write4(client, 0x904, 0x85730108);
131 cx25840_write4(client, 0x90c, 0x85730108);
132 break;
133
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200134 case 48000:
Hans Verkuilbd985162005-11-13 16:07:56 -0800135 /* VID_PLL and AUX_PLL */
136 cx25840_write4(client, 0x108, 0x0f040a18);
137
138 /* AUX_PLL_FRAC */
139 cx25840_write4(client, 0x110, 0xe5d69800);
140
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300141 if (state->is_cx25836)
142 break;
143
Hans Verkuilbd985162005-11-13 16:07:56 -0800144 /* src1_ctl = 0x08010000 */
145 cx25840_write4(client, 0x8f8, 0x00800108);
146
147 /* src3/4/6_ctl = 0x08020000 */
148 cx25840_write4(client, 0x900, 0x55550108);
149 cx25840_write4(client, 0x904, 0x55550108);
150 cx25840_write4(client, 0x90c, 0x55550108);
151 break;
152 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800153 }
154
155 /* deassert soft reset */
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300156 if (!state->is_cx25836)
157 cx25840_and_or(client, 0x810, ~0x1, 0x00);
Hans Verkuilbd985162005-11-13 16:07:56 -0800158
159 state->audclk_freq = freq;
160
161 return 0;
162}
163
Hans Verkuila8bbf122006-01-09 15:25:42 -0200164void cx25840_audio_set_path(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800165{
166 struct cx25840_state *state = i2c_get_clientdata(client);
167
Hans Verkuilbd985162005-11-13 16:07:56 -0800168 /* stop microcontroller */
169 cx25840_and_or(client, 0x803, ~0x10, 0);
170
171 /* Mute everything to prevent the PFFT! */
172 cx25840_write(client, 0x8d3, 0x1f);
173
Hans Verkuila8bbf122006-01-09 15:25:42 -0200174 if (state->aud_input == CX25840_AUDIO_SERIAL) {
Hans Verkuilbd985162005-11-13 16:07:56 -0800175 /* Set Path1 to Serial Audio Input */
176 cx25840_write4(client, 0x8d0, 0x12100101);
177
178 /* The microcontroller should not be started for the
179 * non-tuner inputs: autodetection is specific for
180 * TV audio. */
Hans Verkuila8bbf122006-01-09 15:25:42 -0200181 } else {
182 /* Set Path1 to Analog Demod Main Channel */
183 cx25840_write4(client, 0x8d0, 0x7038061f);
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300184 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800185
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300186 set_audclk_freq(client, state->audclk_freq);
187
188 if (state->aud_input != CX25840_AUDIO_SERIAL) {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200189 /* When the microcontroller detects the
190 * audio format, it will unmute the lines */
191 cx25840_and_or(client, 0x803, ~0x10, 0x10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800192 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800193}
194
Hans Verkuild92c20e2006-01-09 15:32:41 -0200195static int get_volume(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800196{
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
Hans Verkuild92c20e2006-01-09 15:32:41 -0200206static void set_volume(struct i2c_client *client, int volume)
Hans Verkuilbd985162005-11-13 16:07:56 -0800207{
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
Hans Verkuild92c20e2006-01-09 15:32:41 -0200223static int get_bass(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800224{
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
Hans Verkuild92c20e2006-01-09 15:32:41 -0200233static void set_bass(struct i2c_client *client, int bass)
Hans Verkuilbd985162005-11-13 16:07:56 -0800234{
235 /* PATH1_EQ_BASS_VOL */
236 cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
237}
238
Hans Verkuild92c20e2006-01-09 15:32:41 -0200239static int get_treble(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800240{
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
Hans Verkuild92c20e2006-01-09 15:32:41 -0200249static void set_treble(struct i2c_client *client, int treble)
Hans Verkuilbd985162005-11-13 16:07:56 -0800250{
251 /* PATH1_EQ_TREBLE_VOL */
252 cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
253}
254
Hans Verkuild92c20e2006-01-09 15:32:41 -0200255static int get_balance(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800256{
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
Hans Verkuild92c20e2006-01-09 15:32:41 -0200269static void set_balance(struct i2c_client *client, int balance)
Hans Verkuilbd985162005-11-13 16:07:56 -0800270{
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
Hans Verkuild92c20e2006-01-09 15:32:41 -0200285static int get_mute(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800286{
287 /* check SRC1_MUTE_EN */
288 return cx25840_read(client, 0x8d3) & 0x2 ? 1 : 0;
289}
290
Hans Verkuild92c20e2006-01-09 15:32:41 -0200291static void set_mute(struct i2c_client *client, int mute)
Hans Verkuilbd985162005-11-13 16:07:56 -0800292{
293 struct cx25840_state *state = i2c_get_clientdata(client);
294
Hans Verkuila8bbf122006-01-09 15:25:42 -0200295 if (state->aud_input != CX25840_AUDIO_SERIAL) {
Hans Verkuilbd985162005-11-13 16:07:56 -0800296 /* 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{
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300316 struct cx25840_state *state = i2c_get_clientdata(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800317 struct v4l2_control *ctrl = arg;
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300318 int retval;
Hans Verkuilbd985162005-11-13 16:07:56 -0800319
320 switch (cmd) {
Hans Verkuilbd985162005-11-13 16:07:56 -0800321 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
Hans Verkuilc0c044a2006-04-29 12:11:18 -0300322 if (state->aud_input != CX25840_AUDIO_SERIAL) {
323 cx25840_and_or(client, 0x803, ~0x10, 0);
324 cx25840_write(client, 0x8d3, 0x1f);
325 }
326 retval = set_audclk_freq(client, *(u32 *)arg);
327 if (state->aud_input != CX25840_AUDIO_SERIAL) {
328 cx25840_and_or(client, 0x803, ~0x10, 0x10);
329 }
330 return retval;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200331
Hans Verkuilbd985162005-11-13 16:07:56 -0800332 case VIDIOC_G_CTRL:
333 switch (ctrl->id) {
334 case V4L2_CID_AUDIO_VOLUME:
335 ctrl->value = get_volume(client);
336 break;
337 case V4L2_CID_AUDIO_BASS:
338 ctrl->value = get_bass(client);
339 break;
340 case V4L2_CID_AUDIO_TREBLE:
341 ctrl->value = get_treble(client);
342 break;
343 case V4L2_CID_AUDIO_BALANCE:
344 ctrl->value = get_balance(client);
345 break;
346 case V4L2_CID_AUDIO_MUTE:
347 ctrl->value = get_mute(client);
348 break;
349 default:
350 return -EINVAL;
351 }
352 break;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200353
Hans Verkuilbd985162005-11-13 16:07:56 -0800354 case VIDIOC_S_CTRL:
355 switch (ctrl->id) {
356 case V4L2_CID_AUDIO_VOLUME:
357 set_volume(client, ctrl->value);
358 break;
359 case V4L2_CID_AUDIO_BASS:
360 set_bass(client, ctrl->value);
361 break;
362 case V4L2_CID_AUDIO_TREBLE:
363 set_treble(client, ctrl->value);
364 break;
365 case V4L2_CID_AUDIO_BALANCE:
366 set_balance(client, ctrl->value);
367 break;
368 case V4L2_CID_AUDIO_MUTE:
369 set_mute(client, ctrl->value);
370 break;
371 default:
372 return -EINVAL;
373 }
374 break;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200375
Hans Verkuilbd985162005-11-13 16:07:56 -0800376 default:
377 return -EINVAL;
378 }
379
380 return 0;
381}