xref: /openbmc/linux/drivers/media/i2c/vp27smpx.c (revision 5e0266f0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * vp27smpx - driver version 0.0.1
4  *
5  * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
6  *
7  * Based on a tvaudio patch from Takahiro Adachi <tadachi@tadachi-net.com>
8  * and Kazuhiko Kawakami <kazz-0@mail.goo.ne.jp>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/ioctl.h>
15 #include <linux/uaccess.h>
16 #include <linux/i2c.h>
17 #include <linux/videodev2.h>
18 #include <media/v4l2-device.h>
19 
20 MODULE_DESCRIPTION("vp27smpx driver");
21 MODULE_AUTHOR("Hans Verkuil");
22 MODULE_LICENSE("GPL");
23 
24 
25 /* ----------------------------------------------------------------------- */
26 
27 struct vp27smpx_state {
28 	struct v4l2_subdev sd;
29 	int radio;
30 	u32 audmode;
31 };
32 
33 static inline struct vp27smpx_state *to_state(struct v4l2_subdev *sd)
34 {
35 	return container_of(sd, struct vp27smpx_state, sd);
36 }
37 
38 static void vp27smpx_set_audmode(struct v4l2_subdev *sd, u32 audmode)
39 {
40 	struct vp27smpx_state *state = to_state(sd);
41 	struct i2c_client *client = v4l2_get_subdevdata(sd);
42 	u8 data[3] = { 0x00, 0x00, 0x04 };
43 
44 	switch (audmode) {
45 	case V4L2_TUNER_MODE_MONO:
46 	case V4L2_TUNER_MODE_LANG1:
47 		break;
48 	case V4L2_TUNER_MODE_STEREO:
49 	case V4L2_TUNER_MODE_LANG1_LANG2:
50 		data[1] = 0x01;
51 		break;
52 	case V4L2_TUNER_MODE_LANG2:
53 		data[1] = 0x02;
54 		break;
55 	}
56 
57 	if (i2c_master_send(client, data, sizeof(data)) != sizeof(data))
58 		v4l2_err(sd, "I/O error setting audmode\n");
59 	else
60 		state->audmode = audmode;
61 }
62 
63 static int vp27smpx_s_radio(struct v4l2_subdev *sd)
64 {
65 	struct vp27smpx_state *state = to_state(sd);
66 
67 	state->radio = 1;
68 	return 0;
69 }
70 
71 static int vp27smpx_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
72 {
73 	struct vp27smpx_state *state = to_state(sd);
74 
75 	state->radio = 0;
76 	return 0;
77 }
78 
79 static int vp27smpx_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
80 {
81 	struct vp27smpx_state *state = to_state(sd);
82 
83 	if (!state->radio)
84 		vp27smpx_set_audmode(sd, vt->audmode);
85 	return 0;
86 }
87 
88 static int vp27smpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
89 {
90 	struct vp27smpx_state *state = to_state(sd);
91 
92 	if (state->radio)
93 		return 0;
94 	vt->audmode = state->audmode;
95 	vt->capability = V4L2_TUNER_CAP_STEREO |
96 		V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
97 	vt->rxsubchans = V4L2_TUNER_SUB_MONO;
98 	return 0;
99 }
100 
101 static int vp27smpx_log_status(struct v4l2_subdev *sd)
102 {
103 	struct vp27smpx_state *state = to_state(sd);
104 
105 	v4l2_info(sd, "Audio Mode: %u%s\n", state->audmode,
106 			state->radio ? " (Radio)" : "");
107 	return 0;
108 }
109 
110 /* ----------------------------------------------------------------------- */
111 
112 static const struct v4l2_subdev_core_ops vp27smpx_core_ops = {
113 	.log_status = vp27smpx_log_status,
114 };
115 
116 static const struct v4l2_subdev_tuner_ops vp27smpx_tuner_ops = {
117 	.s_radio = vp27smpx_s_radio,
118 	.s_tuner = vp27smpx_s_tuner,
119 	.g_tuner = vp27smpx_g_tuner,
120 };
121 
122 static const struct v4l2_subdev_video_ops vp27smpx_video_ops = {
123 	.s_std = vp27smpx_s_std,
124 };
125 
126 static const struct v4l2_subdev_ops vp27smpx_ops = {
127 	.core = &vp27smpx_core_ops,
128 	.tuner = &vp27smpx_tuner_ops,
129 	.video = &vp27smpx_video_ops,
130 };
131 
132 /* ----------------------------------------------------------------------- */
133 
134 /* i2c implementation */
135 
136 /*
137  * Generic i2c probe
138  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
139  */
140 
141 static int vp27smpx_probe(struct i2c_client *client)
142 {
143 	struct vp27smpx_state *state;
144 	struct v4l2_subdev *sd;
145 
146 	/* Check if the adapter supports the needed features */
147 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
148 		return -EIO;
149 
150 	v4l_info(client, "chip found @ 0x%x (%s)\n",
151 			client->addr << 1, client->adapter->name);
152 
153 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
154 	if (state == NULL)
155 		return -ENOMEM;
156 	sd = &state->sd;
157 	v4l2_i2c_subdev_init(sd, client, &vp27smpx_ops);
158 	state->audmode = V4L2_TUNER_MODE_STEREO;
159 
160 	/* initialize vp27smpx */
161 	vp27smpx_set_audmode(sd, state->audmode);
162 	return 0;
163 }
164 
165 static void vp27smpx_remove(struct i2c_client *client)
166 {
167 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
168 
169 	v4l2_device_unregister_subdev(sd);
170 }
171 
172 /* ----------------------------------------------------------------------- */
173 
174 static const struct i2c_device_id vp27smpx_id[] = {
175 	{ "vp27smpx", 0 },
176 	{ }
177 };
178 MODULE_DEVICE_TABLE(i2c, vp27smpx_id);
179 
180 static struct i2c_driver vp27smpx_driver = {
181 	.driver = {
182 		.name	= "vp27smpx",
183 	},
184 	.probe_new	= vp27smpx_probe,
185 	.remove		= vp27smpx_remove,
186 	.id_table	= vp27smpx_id,
187 };
188 
189 module_i2c_driver(vp27smpx_driver);
190