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)
33 /* assert soft reset */
34 if (!state->is_cx25836)
35 cx25840_and_or(client, 0x810, ~0x1, 0x01);
37 /* common for all inputs and rates */
38 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
39 cx25840_write(client, 0x127, 0x50);
41 if (state->aud_input != CX25840_AUDIO_SERIAL) {
44 /* VID_PLL and AUX_PLL */
45 cx25840_write4(client, 0x108, 0x0f040610);
48 cx25840_write4(client, 0x110, 0xee39bb01);
50 if (state->is_cx25836)
53 /* src3/4/6_ctl = 0x0801f77f */
54 cx25840_write4(client, 0x900, 0x7ff70108);
55 cx25840_write4(client, 0x904, 0x7ff70108);
56 cx25840_write4(client, 0x90c, 0x7ff70108);
60 /* VID_PLL and AUX_PLL */
61 cx25840_write4(client, 0x108, 0x0f040910);
64 cx25840_write4(client, 0x110, 0xd66bec00);
66 if (state->is_cx25836)
69 /* src3/4/6_ctl = 0x08016d59 */
70 cx25840_write4(client, 0x900, 0x596d0108);
71 cx25840_write4(client, 0x904, 0x596d0108);
72 cx25840_write4(client, 0x90c, 0x596d0108);
76 /* VID_PLL and AUX_PLL */
77 cx25840_write4(client, 0x108, 0x0f040a10);
80 cx25840_write4(client, 0x110, 0xe5d69800);
82 if (state->is_cx25836)
85 /* src3/4/6_ctl = 0x08014faa */
86 cx25840_write4(client, 0x900, 0xaa4f0108);
87 cx25840_write4(client, 0x904, 0xaa4f0108);
88 cx25840_write4(client, 0x90c, 0xaa4f0108);
94 /* VID_PLL and AUX_PLL */
95 cx25840_write4(client, 0x108, 0x0f04081e);
98 cx25840_write4(client, 0x110, 0x69082a01);
100 if (state->is_cx25836)
103 /* src1_ctl = 0x08010000 */
104 cx25840_write4(client, 0x8f8, 0x00000108);
106 /* src3/4/6_ctl = 0x08020000 */
107 cx25840_write4(client, 0x900, 0x00000208);
108 cx25840_write4(client, 0x904, 0x00000208);
109 cx25840_write4(client, 0x90c, 0x00000208);
111 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
112 cx25840_write(client, 0x127, 0x54);
116 /* VID_PLL and AUX_PLL */
117 cx25840_write4(client, 0x108, 0x0f040918);
120 cx25840_write4(client, 0x110, 0xd66bec00);
122 if (state->is_cx25836)
125 /* src1_ctl = 0x08010000 */
126 cx25840_write4(client, 0x8f8, 0xcd600108);
128 /* src3/4/6_ctl = 0x08020000 */
129 cx25840_write4(client, 0x900, 0x85730108);
130 cx25840_write4(client, 0x904, 0x85730108);
131 cx25840_write4(client, 0x90c, 0x85730108);
135 /* VID_PLL and AUX_PLL */
136 cx25840_write4(client, 0x108, 0x0f040a18);
139 cx25840_write4(client, 0x110, 0xe5d69800);
141 if (state->is_cx25836)
144 /* src1_ctl = 0x08010000 */
145 cx25840_write4(client, 0x8f8, 0x00800108);
147 /* src3/4/6_ctl = 0x08020000 */
148 cx25840_write4(client, 0x900, 0x55550108);
149 cx25840_write4(client, 0x904, 0x55550108);
150 cx25840_write4(client, 0x90c, 0x55550108);
155 /* deassert soft reset */
156 if (!state->is_cx25836)
157 cx25840_and_or(client, 0x810, ~0x1, 0x00);
159 state->audclk_freq = freq;
164 void cx25840_audio_set_path(struct i2c_client *client)
166 struct cx25840_state *state = i2c_get_clientdata(client);
168 /* stop microcontroller */
169 cx25840_and_or(client, 0x803, ~0x10, 0);
171 /* Mute everything to prevent the PFFT! */
172 cx25840_write(client, 0x8d3, 0x1f);
174 if (state->aud_input == CX25840_AUDIO_SERIAL) {
175 /* Set Path1 to Serial Audio Input */
176 cx25840_write4(client, 0x8d0, 0x12100101);
178 /* The microcontroller should not be started for the
179 * non-tuner inputs: autodetection is specific for
182 /* Set Path1 to Analog Demod Main Channel */
183 cx25840_write4(client, 0x8d0, 0x7038061f);
185 /* When the microcontroller detects the
186 * audio format, it will unmute the lines */
187 cx25840_and_or(client, 0x803, ~0x10, 0x10);
190 set_audclk_freq(client, state->audclk_freq);
193 static int get_volume(struct i2c_client *client)
195 /* Volume runs +18dB to -96dB in 1/2dB steps
196 * change to fit the msp3400 -114dB to +12dB range */
198 /* check PATH1_VOLUME */
199 int vol = 228 - cx25840_read(client, 0x8d4);
200 vol = (vol / 2) + 23;
204 static void set_volume(struct i2c_client *client, int volume)
206 /* First convert the volume to msp3400 values (0-127) */
207 int vol = volume >> 9;
208 /* now scale it up to cx25840 values
209 * -114dB to -96dB maps to 0
210 * this should be 19, but in my testing that was 4dB too loud */
218 cx25840_write(client, 0x8d4, 228 - (vol * 2));
221 static int get_bass(struct i2c_client *client)
223 /* bass is 49 steps +12dB to -12dB */
225 /* check PATH1_EQ_BASS_VOL */
226 int bass = cx25840_read(client, 0x8d9) & 0x3f;
227 bass = (((48 - bass) * 0xffff) + 47) / 48;
231 static void set_bass(struct i2c_client *client, int bass)
233 /* PATH1_EQ_BASS_VOL */
234 cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
237 static int get_treble(struct i2c_client *client)
239 /* treble is 49 steps +12dB to -12dB */
241 /* check PATH1_EQ_TREBLE_VOL */
242 int treble = cx25840_read(client, 0x8db) & 0x3f;
243 treble = (((48 - treble) * 0xffff) + 47) / 48;
247 static void set_treble(struct i2c_client *client, int treble)
249 /* PATH1_EQ_TREBLE_VOL */
250 cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
253 static int get_balance(struct i2c_client *client)
255 /* balance is 7 bit, 0 to -96dB */
257 /* check PATH1_BAL_LEVEL */
258 int balance = cx25840_read(client, 0x8d5) & 0x7f;
259 /* check PATH1_BAL_LEFT */
260 if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
261 balance = 0x80 - balance;
263 balance = 0x80 + balance;
267 static void set_balance(struct i2c_client *client, int balance)
269 int bal = balance >> 8;
272 cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
273 /* PATH1_BAL_LEVEL */
274 cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
277 cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
278 /* PATH1_BAL_LEVEL */
279 cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
283 static int get_mute(struct i2c_client *client)
285 /* check SRC1_MUTE_EN */
286 return cx25840_read(client, 0x8d3) & 0x2 ? 1 : 0;
289 static void set_mute(struct i2c_client *client, int mute)
291 struct cx25840_state *state = i2c_get_clientdata(client);
293 if (state->aud_input != CX25840_AUDIO_SERIAL) {
294 /* Must turn off microcontroller in order to mute sound.
295 * Not sure if this is the best method, but it does work.
296 * If the microcontroller is running, then it will undo any
297 * changes to the mute register. */
299 /* disable microcontroller */
300 cx25840_and_or(client, 0x803, ~0x10, 0x00);
301 cx25840_write(client, 0x8d3, 0x1f);
303 /* enable microcontroller */
304 cx25840_and_or(client, 0x803, ~0x10, 0x10);
308 cx25840_and_or(client, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
312 int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
314 struct v4l2_control *ctrl = arg;
317 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
318 return set_audclk_freq(client, *(u32 *)arg);
322 case V4L2_CID_AUDIO_VOLUME:
323 ctrl->value = get_volume(client);
325 case V4L2_CID_AUDIO_BASS:
326 ctrl->value = get_bass(client);
328 case V4L2_CID_AUDIO_TREBLE:
329 ctrl->value = get_treble(client);
331 case V4L2_CID_AUDIO_BALANCE:
332 ctrl->value = get_balance(client);
334 case V4L2_CID_AUDIO_MUTE:
335 ctrl->value = get_mute(client);
344 case V4L2_CID_AUDIO_VOLUME:
345 set_volume(client, ctrl->value);
347 case V4L2_CID_AUDIO_BASS:
348 set_bass(client, ctrl->value);
350 case V4L2_CID_AUDIO_TREBLE:
351 set_treble(client, ctrl->value);
353 case V4L2_CID_AUDIO_BALANCE:
354 set_balance(client, ctrl->value);
356 case V4L2_CID_AUDIO_MUTE:
357 set_mute(client, ctrl->value);