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>
25 static int set_audclk_freq(struct i2c_client *client, u32 freq)
27 struct cx25840_state *state = i2c_get_clientdata(client);
29 if (freq != 32000 && freq != 44100 && freq != 48000)
32 /* assert soft reset */
33 cx25840_and_or(client, 0x810, ~0x1, 0x01);
35 /* common for all inputs and rates */
36 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
37 cx25840_write(client, 0x127, 0x50);
39 if (state->aud_input != CX25840_AUDIO_SERIAL) {
42 /* VID_PLL and AUX_PLL */
43 cx25840_write4(client, 0x108, 0x0f040610);
46 cx25840_write4(client, 0x110, 0xee39bb01);
48 /* src3/4/6_ctl = 0x0801f77f */
49 cx25840_write4(client, 0x900, 0x7ff70108);
50 cx25840_write4(client, 0x904, 0x7ff70108);
51 cx25840_write4(client, 0x90c, 0x7ff70108);
55 /* VID_PLL and AUX_PLL */
56 cx25840_write4(client, 0x108, 0x0f040910);
59 cx25840_write4(client, 0x110, 0xd66bec00);
61 /* src3/4/6_ctl = 0x08016d59 */
62 cx25840_write4(client, 0x900, 0x596d0108);
63 cx25840_write4(client, 0x904, 0x596d0108);
64 cx25840_write4(client, 0x90c, 0x596d0108);
68 /* VID_PLL and AUX_PLL */
69 cx25840_write4(client, 0x108, 0x0f040a10);
72 cx25840_write4(client, 0x110, 0xe5d69800);
74 /* src3/4/6_ctl = 0x08014faa */
75 cx25840_write4(client, 0x900, 0xaa4f0108);
76 cx25840_write4(client, 0x904, 0xaa4f0108);
77 cx25840_write4(client, 0x90c, 0xaa4f0108);
83 /* VID_PLL and AUX_PLL */
84 cx25840_write4(client, 0x108, 0x0f04081e);
87 cx25840_write4(client, 0x110, 0x69082a01);
89 /* src1_ctl = 0x08010000 */
90 cx25840_write4(client, 0x8f8, 0x00000108);
92 /* src3/4/6_ctl = 0x08020000 */
93 cx25840_write4(client, 0x900, 0x00000208);
94 cx25840_write4(client, 0x904, 0x00000208);
95 cx25840_write4(client, 0x90c, 0x00000208);
97 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
98 cx25840_write(client, 0x127, 0x54);
102 /* VID_PLL and AUX_PLL */
103 cx25840_write4(client, 0x108, 0x0f040918);
106 cx25840_write4(client, 0x110, 0xd66bec00);
108 /* src1_ctl = 0x08010000 */
109 cx25840_write4(client, 0x8f8, 0xcd600108);
111 /* src3/4/6_ctl = 0x08020000 */
112 cx25840_write4(client, 0x900, 0x85730108);
113 cx25840_write4(client, 0x904, 0x85730108);
114 cx25840_write4(client, 0x90c, 0x85730108);
118 /* VID_PLL and AUX_PLL */
119 cx25840_write4(client, 0x108, 0x0f040a18);
122 cx25840_write4(client, 0x110, 0xe5d69800);
124 /* src1_ctl = 0x08010000 */
125 cx25840_write4(client, 0x8f8, 0x00800108);
127 /* src3/4/6_ctl = 0x08020000 */
128 cx25840_write4(client, 0x900, 0x55550108);
129 cx25840_write4(client, 0x904, 0x55550108);
130 cx25840_write4(client, 0x90c, 0x55550108);
135 /* deassert soft reset */
136 cx25840_and_or(client, 0x810, ~0x1, 0x00);
138 state->audclk_freq = freq;
143 void cx25840_audio_set_path(struct i2c_client *client)
145 struct cx25840_state *state = i2c_get_clientdata(client);
147 /* stop microcontroller */
148 cx25840_and_or(client, 0x803, ~0x10, 0);
150 /* Mute everything to prevent the PFFT! */
151 cx25840_write(client, 0x8d3, 0x1f);
153 if (state->aud_input == CX25840_AUDIO_SERIAL) {
154 /* Set Path1 to Serial Audio Input */
155 cx25840_write4(client, 0x8d0, 0x12100101);
157 /* The microcontroller should not be started for the
158 * non-tuner inputs: autodetection is specific for
161 /* Set Path1 to Analog Demod Main Channel */
162 cx25840_write4(client, 0x8d0, 0x7038061f);
164 /* When the microcontroller detects the
165 * audio format, it will unmute the lines */
166 cx25840_and_or(client, 0x803, ~0x10, 0x10);
169 set_audclk_freq(client, state->audclk_freq);
172 static int get_volume(struct i2c_client *client)
174 /* Volume runs +18dB to -96dB in 1/2dB steps
175 * change to fit the msp3400 -114dB to +12dB range */
177 /* check PATH1_VOLUME */
178 int vol = 228 - cx25840_read(client, 0x8d4);
179 vol = (vol / 2) + 23;
183 static void set_volume(struct i2c_client *client, int volume)
185 /* First convert the volume to msp3400 values (0-127) */
186 int vol = volume >> 9;
187 /* now scale it up to cx25840 values
188 * -114dB to -96dB maps to 0
189 * this should be 19, but in my testing that was 4dB too loud */
197 cx25840_write(client, 0x8d4, 228 - (vol * 2));
200 static int get_bass(struct i2c_client *client)
202 /* bass is 49 steps +12dB to -12dB */
204 /* check PATH1_EQ_BASS_VOL */
205 int bass = cx25840_read(client, 0x8d9) & 0x3f;
206 bass = (((48 - bass) * 0xffff) + 47) / 48;
210 static void set_bass(struct i2c_client *client, int bass)
212 /* PATH1_EQ_BASS_VOL */
213 cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
216 static int get_treble(struct i2c_client *client)
218 /* treble is 49 steps +12dB to -12dB */
220 /* check PATH1_EQ_TREBLE_VOL */
221 int treble = cx25840_read(client, 0x8db) & 0x3f;
222 treble = (((48 - treble) * 0xffff) + 47) / 48;
226 static void set_treble(struct i2c_client *client, int treble)
228 /* PATH1_EQ_TREBLE_VOL */
229 cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
232 static int get_balance(struct i2c_client *client)
234 /* balance is 7 bit, 0 to -96dB */
236 /* check PATH1_BAL_LEVEL */
237 int balance = cx25840_read(client, 0x8d5) & 0x7f;
238 /* check PATH1_BAL_LEFT */
239 if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
240 balance = 0x80 - balance;
242 balance = 0x80 + balance;
246 static void set_balance(struct i2c_client *client, int balance)
248 int bal = balance >> 8;
251 cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
252 /* PATH1_BAL_LEVEL */
253 cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
256 cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
257 /* PATH1_BAL_LEVEL */
258 cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
262 static int get_mute(struct i2c_client *client)
264 /* check SRC1_MUTE_EN */
265 return cx25840_read(client, 0x8d3) & 0x2 ? 1 : 0;
268 static void set_mute(struct i2c_client *client, int mute)
270 struct cx25840_state *state = i2c_get_clientdata(client);
272 if (state->aud_input != CX25840_AUDIO_SERIAL) {
273 /* Must turn off microcontroller in order to mute sound.
274 * Not sure if this is the best method, but it does work.
275 * If the microcontroller is running, then it will undo any
276 * changes to the mute register. */
278 /* disable microcontroller */
279 cx25840_and_or(client, 0x803, ~0x10, 0x00);
280 cx25840_write(client, 0x8d3, 0x1f);
282 /* enable microcontroller */
283 cx25840_and_or(client, 0x803, ~0x10, 0x10);
287 cx25840_and_or(client, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
291 int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
293 struct v4l2_control *ctrl = arg;
296 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
297 return set_audclk_freq(client, *(u32 *)arg);
301 case V4L2_CID_AUDIO_VOLUME:
302 ctrl->value = get_volume(client);
304 case V4L2_CID_AUDIO_BASS:
305 ctrl->value = get_bass(client);
307 case V4L2_CID_AUDIO_TREBLE:
308 ctrl->value = get_treble(client);
310 case V4L2_CID_AUDIO_BALANCE:
311 ctrl->value = get_balance(client);
313 case V4L2_CID_AUDIO_MUTE:
314 ctrl->value = get_mute(client);
323 case V4L2_CID_AUDIO_VOLUME:
324 set_volume(client, ctrl->value);
326 case V4L2_CID_AUDIO_BASS:
327 set_bass(client, ctrl->value);
329 case V4L2_CID_AUDIO_TREBLE:
330 set_treble(client, ctrl->value);
332 case V4L2_CID_AUDIO_BALANCE:
333 set_balance(client, ctrl->value);
335 case V4L2_CID_AUDIO_MUTE:
336 set_mute(client, ctrl->value);