vp27smpx.c 5.3 KB
Newer Older
H
Hans Verkuil 已提交
1 2 3 4 5
/*
 * vp27smpx - driver version 0.0.1
 *
 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
 *
6 7
 * Based on a tvaudio patch from Takahiro Adachi <tadachi@tadachi-net.com>
 * and Kazuhiko Kawakami <kazz-0@mail.goo.ne.jp>
H
Hans Verkuil 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/types.h>
26
#include <linux/slab.h>
H
Hans Verkuil 已提交
27 28 29
#include <linux/ioctl.h>
#include <asm/uaccess.h>
#include <linux/i2c.h>
30
#include <linux/videodev2.h>
31
#include <media/v4l2-device.h>
H
Hans Verkuil 已提交
32 33 34 35 36 37 38 39 40 41
#include <media/v4l2-chip-ident.h>

MODULE_DESCRIPTION("vp27smpx driver");
MODULE_AUTHOR("Hans Verkuil");
MODULE_LICENSE("GPL");


/* ----------------------------------------------------------------------- */

struct vp27smpx_state {
42
	struct v4l2_subdev sd;
H
Hans Verkuil 已提交
43 44 45 46
	int radio;
	u32 audmode;
};

47
static inline struct vp27smpx_state *to_state(struct v4l2_subdev *sd)
H
Hans Verkuil 已提交
48
{
49 50 51 52 53 54 55
	return container_of(sd, struct vp27smpx_state, sd);
}

static void vp27smpx_set_audmode(struct v4l2_subdev *sd, u32 audmode)
{
	struct vp27smpx_state *state = to_state(sd);
	struct i2c_client *client = v4l2_get_subdevdata(sd);
H
Hans Verkuil 已提交
56 57 58
	u8 data[3] = { 0x00, 0x00, 0x04 };

	switch (audmode) {
59 60 61 62 63 64 65 66 67 68
	case V4L2_TUNER_MODE_MONO:
	case V4L2_TUNER_MODE_LANG1:
		break;
	case V4L2_TUNER_MODE_STEREO:
	case V4L2_TUNER_MODE_LANG1_LANG2:
		data[1] = 0x01;
		break;
	case V4L2_TUNER_MODE_LANG2:
		data[1] = 0x02;
		break;
H
Hans Verkuil 已提交
69 70
	}

71
	if (i2c_master_send(client, data, sizeof(data)) != sizeof(data))
72
		v4l2_err(sd, "I/O error setting audmode\n");
73
	else
H
Hans Verkuil 已提交
74 75 76
		state->audmode = audmode;
}

77
static int vp27smpx_s_radio(struct v4l2_subdev *sd)
H
Hans Verkuil 已提交
78
{
79
	struct vp27smpx_state *state = to_state(sd);
H
Hans Verkuil 已提交
80

81 82 83
	state->radio = 1;
	return 0;
}
H
Hans Verkuil 已提交
84

85 86 87
static int vp27smpx_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
{
	struct vp27smpx_state *state = to_state(sd);
H
Hans Verkuil 已提交
88

89 90 91
	state->radio = 0;
	return 0;
}
H
Hans Verkuil 已提交
92

93 94 95
static int vp27smpx_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
{
	struct vp27smpx_state *state = to_state(sd);
H
Hans Verkuil 已提交
96

97 98 99 100
	if (!state->radio)
		vp27smpx_set_audmode(sd, vt->audmode);
	return 0;
}
H
Hans Verkuil 已提交
101

102 103 104 105 106 107 108 109 110 111 112 113
static int vp27smpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
{
	struct vp27smpx_state *state = to_state(sd);

	if (state->radio)
		return 0;
	vt->audmode = state->audmode;
	vt->capability = V4L2_TUNER_CAP_STEREO |
		V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
	vt->rxsubchans = V4L2_TUNER_SUB_MONO;
	return 0;
}
H
Hans Verkuil 已提交
114

115
static int vp27smpx_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
116 117 118 119 120 121 122 123 124 125 126 127
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);

	return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_VP27SMPX, 0);
}

static int vp27smpx_log_status(struct v4l2_subdev *sd)
{
	struct vp27smpx_state *state = to_state(sd);

	v4l2_info(sd, "Audio Mode: %u%s\n", state->audmode,
			state->radio ? " (Radio)" : "");
H
Hans Verkuil 已提交
128 129 130
	return 0;
}

131 132 133 134 135
/* ----------------------------------------------------------------------- */

static const struct v4l2_subdev_core_ops vp27smpx_core_ops = {
	.log_status = vp27smpx_log_status,
	.g_chip_ident = vp27smpx_g_chip_ident,
136
	.s_std = vp27smpx_s_std,
137 138 139 140 141 142 143 144 145 146 147 148 149
};

static const struct v4l2_subdev_tuner_ops vp27smpx_tuner_ops = {
	.s_radio = vp27smpx_s_radio,
	.s_tuner = vp27smpx_s_tuner,
	.g_tuner = vp27smpx_g_tuner,
};

static const struct v4l2_subdev_ops vp27smpx_ops = {
	.core = &vp27smpx_core_ops,
	.tuner = &vp27smpx_tuner_ops,
};

H
Hans Verkuil 已提交
150 151 152 153 154 155 156 157 158
/* ----------------------------------------------------------------------- */

/* i2c implementation */

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

159 160
static int vp27smpx_probe(struct i2c_client *client,
			  const struct i2c_device_id *id)
H
Hans Verkuil 已提交
161 162
{
	struct vp27smpx_state *state;
163
	struct v4l2_subdev *sd;
H
Hans Verkuil 已提交
164 165

	/* Check if the adapter supports the needed features */
166
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
167
		return -EIO;
H
Hans Verkuil 已提交
168

169 170
	v4l_info(client, "chip found @ 0x%x (%s)\n",
			client->addr << 1, client->adapter->name);
H
Hans Verkuil 已提交
171 172

	state = kzalloc(sizeof(struct vp27smpx_state), GFP_KERNEL);
173
	if (state == NULL)
H
Hans Verkuil 已提交
174
		return -ENOMEM;
175 176
	sd = &state->sd;
	v4l2_i2c_subdev_init(sd, client, &vp27smpx_ops);
H
Hans Verkuil 已提交
177 178 179
	state->audmode = V4L2_TUNER_MODE_STEREO;

	/* initialize vp27smpx */
180
	vp27smpx_set_audmode(sd, state->audmode);
H
Hans Verkuil 已提交
181 182 183
	return 0;
}

184
static int vp27smpx_remove(struct i2c_client *client)
H
Hans Verkuil 已提交
185
{
186 187 188 189
	struct v4l2_subdev *sd = i2c_get_clientdata(client);

	v4l2_device_unregister_subdev(sd);
	kfree(to_state(sd));
H
Hans Verkuil 已提交
190 191 192 193 194
	return 0;
}

/* ----------------------------------------------------------------------- */

195 196 197 198 199 200
static const struct i2c_device_id vp27smpx_id[] = {
	{ "vp27smpx", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, vp27smpx_id);

201 202 203 204 205 206 207 208
static struct i2c_driver vp27smpx_driver = {
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "vp27smpx",
	},
	.probe		= vp27smpx_probe,
	.remove		= vp27smpx_remove,
	.id_table	= vp27smpx_id,
H
Hans Verkuil 已提交
209
};
210

211
module_i2c_driver(vp27smpx_driver);