radio-si4713.c 10.0 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 26 27 28 29
/*
 * drivers/media/radio/radio-si4713.c
 *
 * Platform Driver for Silicon Labs Si4713 FM Radio Transmitter:
 *
 * Copyright (c) 2008 Instituto Nokia de Tecnologia - INdT
 * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/videodev2.h>
30
#include <linux/slab.h>
31 32 33 34 35 36 37 38 39 40 41
#include <media/v4l2-device.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/radio-si4713.h>

/* module parameters */
static int radio_nr = -1;	/* radio device minor (-1 ==> auto assign) */
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr,
		 "Minor number for radio device (-1 ==> auto assign)");

42
MODULE_LICENSE("GPL v2");
43 44 45 46 47 48 49 50 51 52 53 54 55
MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
MODULE_VERSION("0.0.1");

/* Driver state struct */
struct radio_si4713_device {
	struct v4l2_device		v4l2_dev;
	struct video_device		*radio_dev;
};

/* radio_si4713_fops - file operations interface */
static const struct v4l2_file_operations radio_si4713_fops = {
	.owner		= THIS_MODULE,
56 57
	/* Note: locking is done at the subdev level in the i2c driver. */
	.unlocked_ioctl	= video_ioctl2,
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
};

/* Video4Linux Interface */
static int radio_si4713_fill_audout(struct v4l2_audioout *vao)
{
	/* TODO: check presence of audio output */
	strlcpy(vao->name, "FM Modulator Audio Out", 32);

	return 0;
}

static int radio_si4713_enumaudout(struct file *file, void *priv,
						struct v4l2_audioout *vao)
{
	return radio_si4713_fill_audout(vao);
}

static int radio_si4713_g_audout(struct file *file, void *priv,
					struct v4l2_audioout *vao)
{
	int rval = radio_si4713_fill_audout(vao);

	vao->index = 0;

	return rval;
}

static int radio_si4713_s_audout(struct file *file, void *priv,
86
					const struct v4l2_audioout *vao)
87 88 89 90 91 92 93 94 95 96
{
	return vao->index ? -EINVAL : 0;
}

/* radio_si4713_querycap - query device capabilities */
static int radio_si4713_querycap(struct file *file, void *priv,
					struct v4l2_capability *capability)
{
	strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver));
	strlcpy(capability->card, "Silicon Labs Si4713 Modulator",
97
		sizeof(capability->card));
98 99 100 101 102 103 104
	capability->capabilities = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;

	return 0;
}

/* radio_si4713_queryctrl - enumerate control items */
static int radio_si4713_queryctrl(struct file *file, void *priv,
105
				  struct v4l2_queryctrl *qc)
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
{
	/* Must be sorted from low to high control ID! */
	static const u32 user_ctrls[] = {
		V4L2_CID_USER_CLASS,
		V4L2_CID_AUDIO_MUTE,
		0
	};

	/* Must be sorted from low to high control ID! */
	static const u32 fmtx_ctrls[] = {
		V4L2_CID_FM_TX_CLASS,
		V4L2_CID_RDS_TX_DEVIATION,
		V4L2_CID_RDS_TX_PI,
		V4L2_CID_RDS_TX_PTY,
		V4L2_CID_RDS_TX_PS_NAME,
		V4L2_CID_RDS_TX_RADIO_TEXT,
		V4L2_CID_AUDIO_LIMITER_ENABLED,
		V4L2_CID_AUDIO_LIMITER_RELEASE_TIME,
		V4L2_CID_AUDIO_LIMITER_DEVIATION,
		V4L2_CID_AUDIO_COMPRESSION_ENABLED,
		V4L2_CID_AUDIO_COMPRESSION_GAIN,
		V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
		V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME,
		V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME,
		V4L2_CID_PILOT_TONE_ENABLED,
		V4L2_CID_PILOT_TONE_DEVIATION,
		V4L2_CID_PILOT_TONE_FREQUENCY,
		V4L2_CID_TUNE_PREEMPHASIS,
		V4L2_CID_TUNE_POWER_LEVEL,
		V4L2_CID_TUNE_ANTENNA_CAPACITOR,
		0
	};
	static const u32 *ctrl_classes[] = {
		user_ctrls,
		fmtx_ctrls,
		NULL
	};
	struct radio_si4713_device *rsdev;

	rsdev = video_get_drvdata(video_devdata(file));

	qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
	if (qc->id == 0)
		return -EINVAL;

	if (qc->id == V4L2_CID_USER_CLASS || qc->id == V4L2_CID_FM_TX_CLASS)
		return v4l2_ctrl_query_fill(qc, 0, 0, 0, 0);

	return v4l2_device_call_until_err(&rsdev->v4l2_dev, 0, core,
155
					  queryctrl, qc);
156 157 158 159 160 161 162 163 164 165 166 167
}

/*
 * v4l2 ioctl call backs.
 * we are just a wrapper for v4l2_sub_devs.
 */
static inline struct v4l2_device *get_v4l2_dev(struct file *file)
{
	return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
}

static int radio_si4713_g_ext_ctrls(struct file *file, void *p,
168
				    struct v4l2_ext_controls *vecs)
169 170
{
	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
171
					  g_ext_ctrls, vecs);
172 173 174
}

static int radio_si4713_s_ext_ctrls(struct file *file, void *p,
175
				    struct v4l2_ext_controls *vecs)
176 177
{
	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
178
					  s_ext_ctrls, vecs);
179 180 181
}

static int radio_si4713_g_ctrl(struct file *file, void *p,
182
			       struct v4l2_control *vc)
183 184
{
	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
185
					  g_ctrl, vc);
186 187 188
}

static int radio_si4713_s_ctrl(struct file *file, void *p,
189
			       struct v4l2_control *vc)
190 191
{
	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
192
					  s_ctrl, vc);
193 194 195
}

static int radio_si4713_g_modulator(struct file *file, void *p,
196
				    struct v4l2_modulator *vm)
197 198
{
	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
199
					  g_modulator, vm);
200 201 202
}

static int radio_si4713_s_modulator(struct file *file, void *p,
203
				    const struct v4l2_modulator *vm)
204 205
{
	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
206
					  s_modulator, vm);
207 208 209
}

static int radio_si4713_g_frequency(struct file *file, void *p,
210
				    struct v4l2_frequency *vf)
211 212
{
	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
213
					  g_frequency, vf);
214 215 216
}

static int radio_si4713_s_frequency(struct file *file, void *p,
217
				    struct v4l2_frequency *vf)
218 219
{
	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
220
					  s_frequency, vf);
221 222
}

223
static long radio_si4713_default(struct file *file, void *p,
224
				 bool valid_prio, int cmd, void *arg)
225 226
{
	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
227
					  ioctl, cmd, arg);
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
}

static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
	.vidioc_enumaudout	= radio_si4713_enumaudout,
	.vidioc_g_audout	= radio_si4713_g_audout,
	.vidioc_s_audout	= radio_si4713_s_audout,
	.vidioc_querycap	= radio_si4713_querycap,
	.vidioc_queryctrl	= radio_si4713_queryctrl,
	.vidioc_g_ext_ctrls	= radio_si4713_g_ext_ctrls,
	.vidioc_s_ext_ctrls	= radio_si4713_s_ext_ctrls,
	.vidioc_g_ctrl		= radio_si4713_g_ctrl,
	.vidioc_s_ctrl		= radio_si4713_s_ctrl,
	.vidioc_g_modulator	= radio_si4713_g_modulator,
	.vidioc_s_modulator	= radio_si4713_s_modulator,
	.vidioc_g_frequency	= radio_si4713_g_frequency,
	.vidioc_s_frequency	= radio_si4713_s_frequency,
	.vidioc_default		= radio_si4713_default,
};

/* radio_si4713_vdev_template - video device interface */
static struct video_device radio_si4713_vdev_template = {
	.fops			= &radio_si4713_fops,
	.name			= "radio-si4713",
	.release		= video_device_release,
	.ioctl_ops		= &radio_si4713_ioctl_ops,
253
	.vfl_dir		= VFL_DIR_TX,
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
};

/* Platform driver interface */
/* radio_si4713_pdriver_probe - probe for the device */
static int radio_si4713_pdriver_probe(struct platform_device *pdev)
{
	struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
	struct radio_si4713_device *rsdev;
	struct i2c_adapter *adapter;
	struct v4l2_subdev *sd;
	int rval = 0;

	if (!pdata) {
		dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
		rval = -EINVAL;
		goto exit;
	}

272
	rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
273 274 275 276 277 278 279 280 281
	if (!rsdev) {
		dev_err(&pdev->dev, "Failed to alloc video device.\n");
		rval = -ENOMEM;
		goto exit;
	}

	rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
	if (rval) {
		dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
282
		goto exit;
283 284 285 286 287
	}

	adapter = i2c_get_adapter(pdata->i2c_bus);
	if (!adapter) {
		dev_err(&pdev->dev, "Cannot get i2c adapter %d\n",
288
			pdata->i2c_bus);
289 290 291 292
		rval = -ENODEV;
		goto unregister_v4l2_dev;
	}

293
	sd = v4l2_i2c_new_subdev_board(&rsdev->v4l2_dev, adapter,
294
				       pdata->subdev_board_info, NULL);
295 296 297
	if (!sd) {
		dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
		rval = -ENODEV;
298
		goto put_adapter;
299 300 301 302 303 304
	}

	rsdev->radio_dev = video_device_alloc();
	if (!rsdev->radio_dev) {
		dev_err(&pdev->dev, "Failed to alloc video device.\n");
		rval = -ENOMEM;
305
		goto put_adapter;
306 307 308
	}

	memcpy(rsdev->radio_dev, &radio_si4713_vdev_template,
309
	       sizeof(radio_si4713_vdev_template));
310 311 312 313 314 315 316 317 318 319 320 321
	video_set_drvdata(rsdev->radio_dev, rsdev);
	if (video_register_device(rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
		dev_err(&pdev->dev, "Could not register video device.\n");
		rval = -EIO;
		goto free_vdev;
	}
	dev_info(&pdev->dev, "New device successfully probed\n");

	goto exit;

free_vdev:
	video_device_release(rsdev->radio_dev);
322 323
put_adapter:
	i2c_put_adapter(adapter);
324 325 326 327 328 329 330
unregister_v4l2_dev:
	v4l2_device_unregister(&rsdev->v4l2_dev);
exit:
	return rval;
}

/* radio_si4713_pdriver_remove - remove the device */
331
static int radio_si4713_pdriver_remove(struct platform_device *pdev)
332 333
{
	struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
334 335 336
	struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
					    struct v4l2_subdev, list);
	struct i2c_client *client = v4l2_get_subdevdata(sd);
337
	struct radio_si4713_device *rsdev;
338

339
	rsdev = container_of(v4l2_dev, struct radio_si4713_device, v4l2_dev);
340
	video_unregister_device(rsdev->radio_dev);
341
	i2c_put_adapter(client->adapter);
342 343 344 345 346 347 348 349 350 351
	v4l2_device_unregister(&rsdev->v4l2_dev);

	return 0;
}

static struct platform_driver radio_si4713_pdriver = {
	.driver		= {
		.name	= "radio-si4713",
	},
	.probe		= radio_si4713_pdriver_probe,
352
	.remove         = radio_si4713_pdriver_remove,
353 354
};

355
module_platform_driver(radio_si4713_pdriver);