cx25840-audio.c 8.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* cx25840 audio functions
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#include <linux/videodev2.h>
#include <linux/i2c.h>
#include <media/audiochip.h>
#include <media/v4l2-common.h>

#include "cx25840.h"

26
static int set_audclk_freq(struct i2c_client *client, u32 freq)
27 28 29
{
	struct cx25840_state *state = i2c_get_clientdata(client);

30 31 32
	if (freq != 32000 && freq != 44100 && freq != 48000)
		return -EINVAL;

33 34 35 36 37 38 39
	/* assert soft reset */
	cx25840_and_or(client, 0x810, ~0x1, 0x01);

	/* common for all inputs and rates */
	/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
	cx25840_write(client, 0x127, 0x50);

40
	if (state->aud_input != CX25840_AUDIO_SERIAL) {
41
		switch (freq) {
42
		case 32000:
43 44 45 46 47 48 49 50 51 52 53 54
			/* VID_PLL and AUX_PLL */
			cx25840_write4(client, 0x108, 0x0f040610);

			/* AUX_PLL_FRAC */
			cx25840_write4(client, 0x110, 0xee39bb01);

			/* src3/4/6_ctl = 0x0801f77f */
			cx25840_write4(client, 0x900, 0x7ff70108);
			cx25840_write4(client, 0x904, 0x7ff70108);
			cx25840_write4(client, 0x90c, 0x7ff70108);
			break;

55
		case 44100:
56 57 58 59 60 61 62 63 64 65 66 67
			/* VID_PLL and AUX_PLL */
			cx25840_write4(client, 0x108, 0x0f040910);

			/* AUX_PLL_FRAC */
			cx25840_write4(client, 0x110, 0xd66bec00);

			/* src3/4/6_ctl = 0x08016d59 */
			cx25840_write4(client, 0x900, 0x596d0108);
			cx25840_write4(client, 0x904, 0x596d0108);
			cx25840_write4(client, 0x90c, 0x596d0108);
			break;

68
		case 48000:
69 70 71 72 73 74 75 76 77 78 79 80
			/* VID_PLL and AUX_PLL */
			cx25840_write4(client, 0x108, 0x0f040a10);

			/* AUX_PLL_FRAC */
			cx25840_write4(client, 0x110, 0xe5d69800);

			/* src3/4/6_ctl = 0x08014faa */
			cx25840_write4(client, 0x900, 0xaa4f0108);
			cx25840_write4(client, 0x904, 0xaa4f0108);
			cx25840_write4(client, 0x90c, 0xaa4f0108);
			break;
		}
81
	} else {
82
		switch (freq) {
83
		case 32000:
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
			/* VID_PLL and AUX_PLL */
			cx25840_write4(client, 0x108, 0x0f04081e);

			/* AUX_PLL_FRAC */
			cx25840_write4(client, 0x110, 0x69082a01);

			/* src1_ctl = 0x08010000 */
			cx25840_write4(client, 0x8f8, 0x00000108);

			/* src3/4/6_ctl = 0x08020000 */
			cx25840_write4(client, 0x900, 0x00000208);
			cx25840_write4(client, 0x904, 0x00000208);
			cx25840_write4(client, 0x90c, 0x00000208);

			/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
			cx25840_write(client, 0x127, 0x54);
			break;

102
		case 44100:
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
			/* VID_PLL and AUX_PLL */
			cx25840_write4(client, 0x108, 0x0f040918);

			/* AUX_PLL_FRAC */
			cx25840_write4(client, 0x110, 0xd66bec00);

			/* src1_ctl = 0x08010000 */
			cx25840_write4(client, 0x8f8, 0xcd600108);

			/* src3/4/6_ctl = 0x08020000 */
			cx25840_write4(client, 0x900, 0x85730108);
			cx25840_write4(client, 0x904, 0x85730108);
			cx25840_write4(client, 0x90c, 0x85730108);
			break;

118
		case 48000:
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
			/* VID_PLL and AUX_PLL */
			cx25840_write4(client, 0x108, 0x0f040a18);

			/* AUX_PLL_FRAC */
			cx25840_write4(client, 0x110, 0xe5d69800);

			/* src1_ctl = 0x08010000 */
			cx25840_write4(client, 0x8f8, 0x00800108);

			/* src3/4/6_ctl = 0x08020000 */
			cx25840_write4(client, 0x900, 0x55550108);
			cx25840_write4(client, 0x904, 0x55550108);
			cx25840_write4(client, 0x90c, 0x55550108);
			break;
		}
	}

	/* deassert soft reset */
	cx25840_and_or(client, 0x810, ~0x1, 0x00);

	state->audclk_freq = freq;

	return 0;
}

144
void cx25840_audio_set_path(struct i2c_client *client)
145 146 147 148 149 150 151 152 153
{
	struct cx25840_state *state = i2c_get_clientdata(client);

	/* stop microcontroller */
	cx25840_and_or(client, 0x803, ~0x10, 0);

	/* Mute everything to prevent the PFFT! */
	cx25840_write(client, 0x8d3, 0x1f);

154
	if (state->aud_input == CX25840_AUDIO_SERIAL) {
155 156 157 158 159 160
		/* Set Path1 to Serial Audio Input */
		cx25840_write4(client, 0x8d0, 0x12100101);

		/* The microcontroller should not be started for the
		 * non-tuner inputs: autodetection is specific for
		 * TV audio. */
161 162 163
	} else {
		/* Set Path1 to Analog Demod Main Channel */
		cx25840_write4(client, 0x8d0, 0x7038061f);
164

165 166 167
		/* When the microcontroller detects the
		 * audio format, it will unmute the lines */
		cx25840_and_or(client, 0x803, ~0x10, 0x10);
168 169
	}

170
	set_audclk_freq(client, state->audclk_freq);
171 172
}

173
static int get_volume(struct i2c_client *client)
174 175 176 177 178 179 180 181 182 183
{
	/* Volume runs +18dB to -96dB in 1/2dB steps
	 * change to fit the msp3400 -114dB to +12dB range */

	/* check PATH1_VOLUME */
	int vol = 228 - cx25840_read(client, 0x8d4);
	vol = (vol / 2) + 23;
	return vol << 9;
}

184
static void set_volume(struct i2c_client *client, int volume)
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
{
	/* First convert the volume to msp3400 values (0-127) */
	int vol = volume >> 9;
	/* now scale it up to cx25840 values
	 * -114dB to -96dB maps to 0
	 * this should be 19, but in my testing that was 4dB too loud */
	if (vol <= 23) {
		vol = 0;
	} else {
		vol -= 23;
	}

	/* PATH1_VOLUME */
	cx25840_write(client, 0x8d4, 228 - (vol * 2));
}

201
static int get_bass(struct i2c_client *client)
202 203 204 205 206 207 208 209 210
{
	/* bass is 49 steps +12dB to -12dB */

	/* check PATH1_EQ_BASS_VOL */
	int bass = cx25840_read(client, 0x8d9) & 0x3f;
	bass = (((48 - bass) * 0xffff) + 47) / 48;
	return bass;
}

211
static void set_bass(struct i2c_client *client, int bass)
212 213 214 215 216
{
	/* PATH1_EQ_BASS_VOL */
	cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
}

217
static int get_treble(struct i2c_client *client)
218 219 220 221 222 223 224 225 226
{
	/* treble is 49 steps +12dB to -12dB */

	/* check PATH1_EQ_TREBLE_VOL */
	int treble = cx25840_read(client, 0x8db) & 0x3f;
	treble = (((48 - treble) * 0xffff) + 47) / 48;
	return treble;
}

227
static void set_treble(struct i2c_client *client, int treble)
228 229 230 231 232
{
	/* PATH1_EQ_TREBLE_VOL */
	cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
}

233
static int get_balance(struct i2c_client *client)
234 235 236 237 238 239 240 241 242 243 244 245 246
{
	/* balance is 7 bit, 0 to -96dB */

	/* check PATH1_BAL_LEVEL */
	int balance = cx25840_read(client, 0x8d5) & 0x7f;
	/* check PATH1_BAL_LEFT */
	if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
		balance = 0x80 - balance;
	else
		balance = 0x80 + balance;
	return balance << 8;
}

247
static void set_balance(struct i2c_client *client, int balance)
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
{
	int bal = balance >> 8;
	if (bal > 0x80) {
		/* PATH1_BAL_LEFT */
		cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
		/* PATH1_BAL_LEVEL */
		cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
	} else {
		/* PATH1_BAL_LEFT */
		cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
		/* PATH1_BAL_LEVEL */
		cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
	}
}

263
static int get_mute(struct i2c_client *client)
264 265 266 267 268
{
	/* check SRC1_MUTE_EN */
	return cx25840_read(client, 0x8d3) & 0x2 ? 1 : 0;
}

269
static void set_mute(struct i2c_client *client, int mute)
270 271 272
{
	struct cx25840_state *state = i2c_get_clientdata(client);

273
	if (state->aud_input != CX25840_AUDIO_SERIAL) {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		/* Must turn off microcontroller in order to mute sound.
		 * Not sure if this is the best method, but it does work.
		 * If the microcontroller is running, then it will undo any
		 * changes to the mute register. */
		if (mute) {
			/* disable microcontroller */
			cx25840_and_or(client, 0x803, ~0x10, 0x00);
			cx25840_write(client, 0x8d3, 0x1f);
		} else {
			/* enable microcontroller */
			cx25840_and_or(client, 0x803, ~0x10, 0x10);
		}
	} else {
		/* SRC1_MUTE_EN */
		cx25840_and_or(client, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
	}
}

int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
{
	struct v4l2_control *ctrl = arg;

	switch (cmd) {
	case VIDIOC_INT_AUDIO_CLOCK_FREQ:
298
		return set_audclk_freq(client, *(u32 *)arg);
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	case VIDIOC_G_CTRL:
		switch (ctrl->id) {
		case V4L2_CID_AUDIO_VOLUME:
			ctrl->value = get_volume(client);
			break;
		case V4L2_CID_AUDIO_BASS:
			ctrl->value = get_bass(client);
			break;
		case V4L2_CID_AUDIO_TREBLE:
			ctrl->value = get_treble(client);
			break;
		case V4L2_CID_AUDIO_BALANCE:
			ctrl->value = get_balance(client);
			break;
		case V4L2_CID_AUDIO_MUTE:
			ctrl->value = get_mute(client);
			break;
		default:
			return -EINVAL;
		}
		break;
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	case VIDIOC_S_CTRL:
		switch (ctrl->id) {
		case V4L2_CID_AUDIO_VOLUME:
			set_volume(client, ctrl->value);
			break;
		case V4L2_CID_AUDIO_BASS:
			set_bass(client, ctrl->value);
			break;
		case V4L2_CID_AUDIO_TREBLE:
			set_treble(client, ctrl->value);
			break;
		case V4L2_CID_AUDIO_BALANCE:
			set_balance(client, ctrl->value);
			break;
		case V4L2_CID_AUDIO_MUTE:
			set_mute(client, ctrl->value);
			break;
		default:
			return -EINVAL;
		}
		break;
343

344 345 346 347 348 349
	default:
		return -EINVAL;
	}

	return 0;
}