18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * vp27smpx - driver version 0.0.1
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on a tvaudio patch from Takahiro Adachi <tadachi@tadachi-net.com>
88c2ecf20Sopenharmony_ci * and Kazuhiko Kawakami <kazz-0@mail.goo.ne.jp>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/ioctl.h>
158c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
168c2ecf20Sopenharmony_ci#include <linux/i2c.h>
178c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
188c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("vp27smpx driver");
218c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hans Verkuil");
228c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct vp27smpx_state {
288c2ecf20Sopenharmony_ci	struct v4l2_subdev sd;
298c2ecf20Sopenharmony_ci	int radio;
308c2ecf20Sopenharmony_ci	u32 audmode;
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic inline struct vp27smpx_state *to_state(struct v4l2_subdev *sd)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	return container_of(sd, struct vp27smpx_state, sd);
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic void vp27smpx_set_audmode(struct v4l2_subdev *sd, u32 audmode)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct vp27smpx_state *state = to_state(sd);
418c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
428c2ecf20Sopenharmony_ci	u8 data[3] = { 0x00, 0x00, 0x04 };
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	switch (audmode) {
458c2ecf20Sopenharmony_ci	case V4L2_TUNER_MODE_MONO:
468c2ecf20Sopenharmony_ci	case V4L2_TUNER_MODE_LANG1:
478c2ecf20Sopenharmony_ci		break;
488c2ecf20Sopenharmony_ci	case V4L2_TUNER_MODE_STEREO:
498c2ecf20Sopenharmony_ci	case V4L2_TUNER_MODE_LANG1_LANG2:
508c2ecf20Sopenharmony_ci		data[1] = 0x01;
518c2ecf20Sopenharmony_ci		break;
528c2ecf20Sopenharmony_ci	case V4L2_TUNER_MODE_LANG2:
538c2ecf20Sopenharmony_ci		data[1] = 0x02;
548c2ecf20Sopenharmony_ci		break;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (i2c_master_send(client, data, sizeof(data)) != sizeof(data))
588c2ecf20Sopenharmony_ci		v4l2_err(sd, "I/O error setting audmode\n");
598c2ecf20Sopenharmony_ci	else
608c2ecf20Sopenharmony_ci		state->audmode = audmode;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int vp27smpx_s_radio(struct v4l2_subdev *sd)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct vp27smpx_state *state = to_state(sd);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	state->radio = 1;
688c2ecf20Sopenharmony_ci	return 0;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic int vp27smpx_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct vp27smpx_state *state = to_state(sd);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	state->radio = 0;
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic int vp27smpx_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct vp27smpx_state *state = to_state(sd);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (!state->radio)
848c2ecf20Sopenharmony_ci		vp27smpx_set_audmode(sd, vt->audmode);
858c2ecf20Sopenharmony_ci	return 0;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int vp27smpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct vp27smpx_state *state = to_state(sd);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (state->radio)
938c2ecf20Sopenharmony_ci		return 0;
948c2ecf20Sopenharmony_ci	vt->audmode = state->audmode;
958c2ecf20Sopenharmony_ci	vt->capability = V4L2_TUNER_CAP_STEREO |
968c2ecf20Sopenharmony_ci		V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
978c2ecf20Sopenharmony_ci	vt->rxsubchans = V4L2_TUNER_SUB_MONO;
988c2ecf20Sopenharmony_ci	return 0;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic int vp27smpx_log_status(struct v4l2_subdev *sd)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct vp27smpx_state *state = to_state(sd);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	v4l2_info(sd, "Audio Mode: %u%s\n", state->audmode,
1068c2ecf20Sopenharmony_ci			state->radio ? " (Radio)" : "");
1078c2ecf20Sopenharmony_ci	return 0;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops vp27smpx_core_ops = {
1138c2ecf20Sopenharmony_ci	.log_status = vp27smpx_log_status,
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_tuner_ops vp27smpx_tuner_ops = {
1178c2ecf20Sopenharmony_ci	.s_radio = vp27smpx_s_radio,
1188c2ecf20Sopenharmony_ci	.s_tuner = vp27smpx_s_tuner,
1198c2ecf20Sopenharmony_ci	.g_tuner = vp27smpx_g_tuner,
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops vp27smpx_video_ops = {
1238c2ecf20Sopenharmony_ci	.s_std = vp27smpx_s_std,
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops vp27smpx_ops = {
1278c2ecf20Sopenharmony_ci	.core = &vp27smpx_core_ops,
1288c2ecf20Sopenharmony_ci	.tuner = &vp27smpx_tuner_ops,
1298c2ecf20Sopenharmony_ci	.video = &vp27smpx_video_ops,
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* i2c implementation */
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/*
1378c2ecf20Sopenharmony_ci * Generic i2c probe
1388c2ecf20Sopenharmony_ci * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
1398c2ecf20Sopenharmony_ci */
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic int vp27smpx_probe(struct i2c_client *client,
1428c2ecf20Sopenharmony_ci			  const struct i2c_device_id *id)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct vp27smpx_state *state;
1458c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/* Check if the adapter supports the needed features */
1488c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1498c2ecf20Sopenharmony_ci		return -EIO;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	v4l_info(client, "chip found @ 0x%x (%s)\n",
1528c2ecf20Sopenharmony_ci			client->addr << 1, client->adapter->name);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
1558c2ecf20Sopenharmony_ci	if (state == NULL)
1568c2ecf20Sopenharmony_ci		return -ENOMEM;
1578c2ecf20Sopenharmony_ci	sd = &state->sd;
1588c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &vp27smpx_ops);
1598c2ecf20Sopenharmony_ci	state->audmode = V4L2_TUNER_MODE_STEREO;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	/* initialize vp27smpx */
1628c2ecf20Sopenharmony_ci	vp27smpx_set_audmode(sd, state->audmode);
1638c2ecf20Sopenharmony_ci	return 0;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic int vp27smpx_remove(struct i2c_client *client)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
1718c2ecf20Sopenharmony_ci	return 0;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic const struct i2c_device_id vp27smpx_id[] = {
1778c2ecf20Sopenharmony_ci	{ "vp27smpx", 0 },
1788c2ecf20Sopenharmony_ci	{ }
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, vp27smpx_id);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic struct i2c_driver vp27smpx_driver = {
1838c2ecf20Sopenharmony_ci	.driver = {
1848c2ecf20Sopenharmony_ci		.name	= "vp27smpx",
1858c2ecf20Sopenharmony_ci	},
1868c2ecf20Sopenharmony_ci	.probe		= vp27smpx_probe,
1878c2ecf20Sopenharmony_ci	.remove		= vp27smpx_remove,
1888c2ecf20Sopenharmony_ci	.id_table	= vp27smpx_id,
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cimodule_i2c_driver(vp27smpx_driver);
192