saa7706h.c 11.9 KB
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/*
 * saa7706.c Philips SAA7706H Car Radio DSP driver
 * Copyright (c) 2009 Intel Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>

#define DRIVER_NAME "saa7706h"

/* the I2C memory map looks like this

	$1C00 - $FFFF Not Used
	$2200 - $3FFF Reserved YRAM (DSP2) space
	$2000 - $21FF YRAM (DSP2)
	$1FF0 - $1FFF Hardware Registers
	$1280 - $1FEF Reserved XRAM (DSP2) space
	$1000 - $127F XRAM (DSP2)
	$0FFF        DSP CONTROL
	$0A00 - $0FFE Reserved
	$0980 - $09FF Reserved YRAM (DSP1) space
	$0800 - $097F YRAM (DSP1)
	$0200 - $07FF Not Used
	$0180 - $01FF Reserved XRAM (DSP1) space
	$0000 - $017F XRAM (DSP1)
*/

#define SAA7706H_REG_CTRL		0x0fff
#define SAA7706H_CTRL_BYP_PLL		0x0001
#define SAA7706H_CTRL_PLL_DIV_MASK	0x003e
#define SAA7706H_CTRL_PLL3_62975MHZ	0x003e
#define SAA7706H_CTRL_DSP_TURBO		0x0040
#define SAA7706H_CTRL_PC_RESET_DSP1	0x0080
#define SAA7706H_CTRL_PC_RESET_DSP2	0x0100
#define SAA7706H_CTRL_DSP1_ROM_EN_MASK	0x0600
#define SAA7706H_CTRL_DSP1_FUNC_PROM	0x0000
#define SAA7706H_CTRL_DSP2_ROM_EN_MASK	0x1800
#define SAA7706H_CTRL_DSP2_FUNC_PROM	0x0000
#define SAA7706H_CTRL_DIG_SIL_INTERPOL	0x8000

#define SAA7706H_REG_EVALUATION			0x1ff0
#define SAA7706H_EVAL_DISABLE_CHARGE_PUMP	0x000001
#define SAA7706H_EVAL_DCS_CLOCK			0x000002
#define SAA7706H_EVAL_GNDRC1_ENABLE		0x000004
#define SAA7706H_EVAL_GNDRC2_ENABLE		0x000008

#define SAA7706H_REG_CL_GEN1			0x1ff3
#define SAA7706H_CL_GEN1_MIN_LOOPGAIN_MASK	0x00000f
#define SAA7706H_CL_GEN1_LOOPGAIN_MASK		0x0000f0
#define SAA7706H_CL_GEN1_COARSE_RATION		0xffff00

#define SAA7706H_REG_CL_GEN2			0x1ff4
#define SAA7706H_CL_GEN2_WSEDGE_FALLING		0x000001
#define SAA7706H_CL_GEN2_STOP_VCO		0x000002
#define SAA7706H_CL_GEN2_FRERUN			0x000004
#define SAA7706H_CL_GEN2_ADAPTIVE		0x000008
#define SAA7706H_CL_GEN2_FINE_RATIO_MASK	0x0ffff0

#define SAA7706H_REG_CL_GEN4		0x1ff6
#define SAA7706H_CL_GEN4_BYPASS_PLL1	0x001000
#define SAA7706H_CL_GEN4_PLL1_DIV_MASK	0x03e000
#define SAA7706H_CL_GEN4_DSP1_TURBO	0x040000

#define SAA7706H_REG_SEL	0x1ff7
#define SAA7706H_SEL_DSP2_SRCA_MASK	0x000007
#define SAA7706H_SEL_DSP2_FMTA_MASK	0x000031
#define SAA7706H_SEL_DSP2_SRCB_MASK	0x0001c0
#define SAA7706H_SEL_DSP2_FMTB_MASK	0x000e00
#define SAA7706H_SEL_DSP1_SRC_MASK	0x003000
#define SAA7706H_SEL_DSP1_FMT_MASK	0x01c003
#define SAA7706H_SEL_SPDIF2		0x020000
#define SAA7706H_SEL_HOST_IO_FMT_MASK	0x1c0000
#define SAA7706H_SEL_EN_HOST_IO		0x200000

#define SAA7706H_REG_IAC		0x1ff8
#define SAA7706H_REG_CLK_SET		0x1ff9
#define SAA7706H_REG_CLK_COEFF		0x1ffa
#define SAA7706H_REG_INPUT_SENS		0x1ffb
#define SAA7706H_INPUT_SENS_RDS_VOL_MASK	0x0003f
#define SAA7706H_INPUT_SENS_FM_VOL_MASK		0x00fc0
#define SAA7706H_INPUT_SENS_FM_MPX		0x01000
#define SAA7706H_INPUT_SENS_OFF_FILTER_A_EN	0x02000
#define SAA7706H_INPUT_SENS_OFF_FILTER_B_EN	0x04000
#define SAA7706H_REG_PHONE_NAV_AUDIO	0x1ffc
#define SAA7706H_REG_IO_CONF_DSP2	0x1ffd
#define SAA7706H_REG_STATUS_DSP2	0x1ffe
#define SAA7706H_REG_PC_DSP2		0x1fff

#define SAA7706H_DSP1_MOD0	0x0800
#define SAA7706H_DSP1_ROM_VER	0x097f
#define SAA7706H_DSP2_MPTR0	0x1000

#define SAA7706H_DSP1_MODPNTR	0x0000

#define SAA7706H_DSP2_XMEM_CONTLLCW	0x113e
#define SAA7706H_DSP2_XMEM_BUSAMP	0x114a
#define SAA7706H_DSP2_XMEM_FDACPNTR	0x11f9
#define SAA7706H_DSP2_XMEM_IIS1PNTR	0x11fb

#define SAA7706H_DSP2_YMEM_PVGA		0x212a
#define SAA7706H_DSP2_YMEM_PVAT1	0x212b
#define SAA7706H_DSP2_YMEM_PVAT		0x212c
#define SAA7706H_DSP2_YMEM_ROM_VER	0x21ff

#define SUPPORTED_DSP1_ROM_VER		0x667

struct saa7706h_state {
	struct v4l2_subdev sd;
	struct v4l2_ctrl_handler hdl;
	unsigned muted;
};

static inline struct saa7706h_state *to_state(struct v4l2_subdev *sd)
{
	return container_of(sd, struct saa7706h_state, sd);
}

static int saa7706h_i2c_send(struct i2c_client *client, const u8 *data, int len)
{
	int err = i2c_master_send(client, data, len);
	if (err == len)
		return 0;
	return err > 0 ? -EIO : err;
}

static int saa7706h_i2c_transfer(struct i2c_client *client,
	struct i2c_msg *msgs, int num)
{
	int err = i2c_transfer(client->adapter, msgs, num);
	if (err == num)
		return 0;
	return err > 0 ? -EIO : err;
}

static int saa7706h_set_reg24(struct v4l2_subdev *sd, u16 reg, u32 val)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	u8 buf[5];
	int pos = 0;

	buf[pos++] = reg >> 8;
	buf[pos++] = reg;
	buf[pos++] = val >> 16;
	buf[pos++] = val >> 8;
	buf[pos++] = val;

	return saa7706h_i2c_send(client, buf, pos);
}

static int saa7706h_set_reg24_err(struct v4l2_subdev *sd, u16 reg, u32 val,
	int *err)
{
	return *err ? *err : saa7706h_set_reg24(sd, reg, val);
}

static int saa7706h_set_reg16(struct v4l2_subdev *sd, u16 reg, u16 val)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	u8 buf[4];
	int pos = 0;

	buf[pos++] = reg >> 8;
	buf[pos++] = reg;
	buf[pos++] = val >> 8;
	buf[pos++] = val;

	return saa7706h_i2c_send(client, buf, pos);
}

static int saa7706h_set_reg16_err(struct v4l2_subdev *sd, u16 reg, u16 val,
	int *err)
{
	return *err ? *err : saa7706h_set_reg16(sd, reg, val);
}

static int saa7706h_get_reg16(struct v4l2_subdev *sd, u16 reg)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	u8 buf[2];
	int err;
	u8 regaddr[] = {reg >> 8, reg};
	struct i2c_msg msg[] = {
					{
						.addr = client->addr,
						.len = sizeof(regaddr),
						.buf = regaddr
					},
					{
						.addr = client->addr,
						.flags = I2C_M_RD,
						.len = sizeof(buf),
						.buf = buf
					}
				};

	err = saa7706h_i2c_transfer(client, msg, ARRAY_SIZE(msg));
	if (err)
		return err;

	return buf[0] << 8 | buf[1];
}

static int saa7706h_unmute(struct v4l2_subdev *sd)
{
	struct saa7706h_state *state = to_state(sd);
	int err = 0;

	err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
		SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
		SAA7706H_CTRL_PC_RESET_DSP2, &err);

	/* newer versions of the chip requires a small sleep after reset */
	msleep(1);

	err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
		SAA7706H_CTRL_PLL3_62975MHZ, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_EVALUATION, 0, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN1, 0x040022, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN2,
		SAA7706H_CL_GEN2_WSEDGE_FALLING, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN4, 0x024080, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_SEL, 0x200080, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IAC, 0xf4caed, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_SET, 0x124334, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_COEFF, 0x004a1a,
		&err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_INPUT_SENS, 0x0071c7,
		&err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PHONE_NAV_AUDIO,
		0x0e22ff, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IO_CONF_DSP2, 0x001ff8,
		&err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_STATUS_DSP2, 0x080003,
		&err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PC_DSP2, 0x000004, &err);

	err = saa7706h_set_reg16_err(sd, SAA7706H_DSP1_MOD0, 0x0c6c, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_MPTR0, 0x000b4b, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x000600, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x0000c0, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000819,
		&err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x00085a,
		&err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_BUSAMP, 0x7fffff,
		&err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_FDACPNTR, 0x2000cb,
		&err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_IIS1PNTR, 0x2000cb,
		&err);

	err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVGA, 0x0f80, &err);

	err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT1, 0x0800,
		&err);

	err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT, 0x0800, &err);

	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000905,
		&err);
	if (!err)
		state->muted = 0;
	return err;
}

static int saa7706h_mute(struct v4l2_subdev *sd)
{
	struct saa7706h_state *state = to_state(sd);
	int err;

	err = saa7706h_set_reg16(sd, SAA7706H_REG_CTRL,
		SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
		SAA7706H_CTRL_PC_RESET_DSP2);
	if (!err)
		state->muted = 1;
	return err;
}

static int saa7706h_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct saa7706h_state *state =
		container_of(ctrl->handler, struct saa7706h_state, hdl);

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		if (ctrl->val)
			return saa7706h_mute(&state->sd);
		return saa7706h_unmute(&state->sd);
	}
	return -EINVAL;
}

static const struct v4l2_ctrl_ops saa7706h_ctrl_ops = {
	.s_ctrl = saa7706h_s_ctrl,
};

static const struct v4l2_subdev_core_ops saa7706h_core_ops = {
	.g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
	.try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
	.s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
	.g_ctrl = v4l2_subdev_g_ctrl,
	.s_ctrl = v4l2_subdev_s_ctrl,
	.queryctrl = v4l2_subdev_queryctrl,
	.querymenu = v4l2_subdev_querymenu,
};

static const struct v4l2_subdev_ops saa7706h_ops = {
	.core = &saa7706h_core_ops,
};

/*
 * Generic i2c probe
 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
 */

static int saa7706h_probe(struct i2c_client *client,
			  const struct i2c_device_id *id)
{
	struct saa7706h_state *state;
	struct v4l2_subdev *sd;
	int err;

	/* Check if the adapter supports the needed features */
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -EIO;

	v4l_info(client, "chip found @ 0x%02x (%s)\n",
			client->addr << 1, client->adapter->name);

	state = kzalloc(sizeof(struct saa7706h_state), GFP_KERNEL);
	if (state == NULL)
		return -ENOMEM;
	sd = &state->sd;
	v4l2_i2c_subdev_init(sd, client, &saa7706h_ops);

	v4l2_ctrl_handler_init(&state->hdl, 4);
	v4l2_ctrl_new_std(&state->hdl, &saa7706h_ctrl_ops,
			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
	sd->ctrl_handler = &state->hdl;
	err = state->hdl.error;
	if (err)
		goto err;

	/* check the rom versions */
	err = saa7706h_get_reg16(sd, SAA7706H_DSP1_ROM_VER);
	if (err < 0)
		goto err;
	if (err != SUPPORTED_DSP1_ROM_VER)
		v4l2_warn(sd, "Unknown DSP1 ROM code version: 0x%x\n", err);
	state->muted = 1;

	/* startup in a muted state */
	err = saa7706h_mute(sd);
	if (err)
		goto err;

	return 0;

err:
	v4l2_device_unregister_subdev(sd);
	v4l2_ctrl_handler_free(&state->hdl);
	kfree(to_state(sd));

	printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", err);

	return err;
}

static int saa7706h_remove(struct i2c_client *client)
{
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct saa7706h_state *state = to_state(sd);

	saa7706h_mute(sd);
	v4l2_device_unregister_subdev(sd);
	v4l2_ctrl_handler_free(&state->hdl);
	kfree(to_state(sd));
	return 0;
}

static const struct i2c_device_id saa7706h_id[] = {
	{DRIVER_NAME, 0},
	{},
};

MODULE_DEVICE_TABLE(i2c, saa7706h_id);

static struct i2c_driver saa7706h_driver = {
	.driver = {
		.owner	= THIS_MODULE,
		.name	= DRIVER_NAME,
	},
	.probe		= saa7706h_probe,
	.remove		= saa7706h_remove,
	.id_table	= saa7706h_id,
};

module_i2c_driver(saa7706h_driver);

MODULE_DESCRIPTION("SAA7706H Car Radio DSP driver");
MODULE_AUTHOR("Mocean Laboratories");
MODULE_LICENSE("GPL v2");