blob: 38e53b303cc35bd836361c9e0652aaec23a67489 [file] [log] [blame]
Hans Verkuilac247432007-07-27 06:56:50 -03001/*
2 * vp27smpx - driver version 0.0.1
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 *
Kazuhiko Kawakamida80be22007-08-18 11:39:28 -03006 * Based on a tvaudio patch from Takahiro Adachi <tadachi@tadachi-net.com>
7 * and Kazuhiko Kawakami <kazz-0@mail.goo.ne.jp>
Hans Verkuilac247432007-07-27 06:56:50 -03008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/ioctl.h>
27#include <asm/uaccess.h>
28#include <linux/i2c.h>
29#include <linux/i2c-id.h>
Hans Verkuil33b687c2008-07-25 05:32:50 -030030#include <linux/videodev2.h>
Hans Verkuild9460f02008-11-29 13:01:28 -030031#include <media/v4l2-device.h>
Hans Verkuilac247432007-07-27 06:56:50 -030032#include <media/v4l2-chip-ident.h>
Hans Verkuilf69d4192007-12-12 07:24:27 -030033#include <media/v4l2-i2c-drv.h>
Hans Verkuilac247432007-07-27 06:56:50 -030034
35MODULE_DESCRIPTION("vp27smpx driver");
36MODULE_AUTHOR("Hans Verkuil");
37MODULE_LICENSE("GPL");
38
Hans Verkuilac247432007-07-27 06:56:50 -030039
40/* ----------------------------------------------------------------------- */
41
42struct vp27smpx_state {
Hans Verkuild9460f02008-11-29 13:01:28 -030043 struct v4l2_subdev sd;
Hans Verkuilac247432007-07-27 06:56:50 -030044 int radio;
45 u32 audmode;
46};
47
Hans Verkuild9460f02008-11-29 13:01:28 -030048static inline struct vp27smpx_state *to_state(struct v4l2_subdev *sd)
Hans Verkuilac247432007-07-27 06:56:50 -030049{
Hans Verkuild9460f02008-11-29 13:01:28 -030050 return container_of(sd, struct vp27smpx_state, sd);
51}
52
53static void vp27smpx_set_audmode(struct v4l2_subdev *sd, u32 audmode)
54{
55 struct vp27smpx_state *state = to_state(sd);
56 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilac247432007-07-27 06:56:50 -030057 u8 data[3] = { 0x00, 0x00, 0x04 };
58
59 switch (audmode) {
Hans Verkuil35df38c2007-12-12 07:40:54 -030060 case V4L2_TUNER_MODE_MONO:
61 case V4L2_TUNER_MODE_LANG1:
62 break;
63 case V4L2_TUNER_MODE_STEREO:
64 case V4L2_TUNER_MODE_LANG1_LANG2:
65 data[1] = 0x01;
66 break;
67 case V4L2_TUNER_MODE_LANG2:
68 data[1] = 0x02;
69 break;
Hans Verkuilac247432007-07-27 06:56:50 -030070 }
71
Hans Verkuil35df38c2007-12-12 07:40:54 -030072 if (i2c_master_send(client, data, sizeof(data)) != sizeof(data))
Hans Verkuild9460f02008-11-29 13:01:28 -030073 v4l2_err(sd, "I/O error setting audmode\n");
Hans Verkuil35df38c2007-12-12 07:40:54 -030074 else
Hans Verkuilac247432007-07-27 06:56:50 -030075 state->audmode = audmode;
Hans Verkuilac247432007-07-27 06:56:50 -030076}
77
Hans Verkuild9460f02008-11-29 13:01:28 -030078static int vp27smpx_s_radio(struct v4l2_subdev *sd)
Hans Verkuilac247432007-07-27 06:56:50 -030079{
Hans Verkuild9460f02008-11-29 13:01:28 -030080 struct vp27smpx_state *state = to_state(sd);
Hans Verkuilac247432007-07-27 06:56:50 -030081
Hans Verkuild9460f02008-11-29 13:01:28 -030082 state->radio = 1;
Hans Verkuilac247432007-07-27 06:56:50 -030083 return 0;
84}
85
Hans Verkuild9460f02008-11-29 13:01:28 -030086static int vp27smpx_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
87{
88 struct vp27smpx_state *state = to_state(sd);
89
90 state->radio = 0;
91 return 0;
92}
93
94static int vp27smpx_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
95{
96 struct vp27smpx_state *state = to_state(sd);
97
98 if (!state->radio)
99 vp27smpx_set_audmode(sd, vt->audmode);
100 return 0;
101}
102
103static int vp27smpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
104{
105 struct vp27smpx_state *state = to_state(sd);
106
107 if (state->radio)
108 return 0;
109 vt->audmode = state->audmode;
110 vt->capability = V4L2_TUNER_CAP_STEREO |
111 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
112 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
113 return 0;
114}
115
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300116static int vp27smpx_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
Hans Verkuild9460f02008-11-29 13:01:28 -0300117{
118 struct i2c_client *client = v4l2_get_subdevdata(sd);
119
120 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_VP27SMPX, 0);
121}
122
123static int vp27smpx_log_status(struct v4l2_subdev *sd)
124{
125 struct vp27smpx_state *state = to_state(sd);
126
127 v4l2_info(sd, "Audio Mode: %u%s\n", state->audmode,
128 state->radio ? " (Radio)" : "");
129 return 0;
130}
131
Hans Verkuild9460f02008-11-29 13:01:28 -0300132/* ----------------------------------------------------------------------- */
133
134static const struct v4l2_subdev_core_ops vp27smpx_core_ops = {
135 .log_status = vp27smpx_log_status,
136 .g_chip_ident = vp27smpx_g_chip_ident,
Hans Verkuilf41737e2009-04-01 03:52:39 -0300137 .s_std = vp27smpx_s_std,
Hans Verkuild9460f02008-11-29 13:01:28 -0300138};
139
140static const struct v4l2_subdev_tuner_ops vp27smpx_tuner_ops = {
141 .s_radio = vp27smpx_s_radio,
Hans Verkuild9460f02008-11-29 13:01:28 -0300142 .s_tuner = vp27smpx_s_tuner,
143 .g_tuner = vp27smpx_g_tuner,
144};
145
146static const struct v4l2_subdev_ops vp27smpx_ops = {
147 .core = &vp27smpx_core_ops,
148 .tuner = &vp27smpx_tuner_ops,
149};
150
Hans Verkuilac247432007-07-27 06:56:50 -0300151/* ----------------------------------------------------------------------- */
152
153/* i2c implementation */
154
155/*
156 * Generic i2c probe
157 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
158 */
159
Jean Delvared2653e92008-04-29 23:11:39 +0200160static int vp27smpx_probe(struct i2c_client *client,
161 const struct i2c_device_id *id)
Hans Verkuilac247432007-07-27 06:56:50 -0300162{
Hans Verkuilac247432007-07-27 06:56:50 -0300163 struct vp27smpx_state *state;
Hans Verkuild9460f02008-11-29 13:01:28 -0300164 struct v4l2_subdev *sd;
Hans Verkuilac247432007-07-27 06:56:50 -0300165
166 /* Check if the adapter supports the needed features */
Hans Verkuil45eea272007-09-13 11:11:44 -0300167 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Hans Verkuil188f3452007-09-16 10:47:15 -0300168 return -EIO;
Hans Verkuilac247432007-07-27 06:56:50 -0300169
Hans Verkuil35df38c2007-12-12 07:40:54 -0300170 v4l_info(client, "chip found @ 0x%x (%s)\n",
171 client->addr << 1, client->adapter->name);
Hans Verkuilac247432007-07-27 06:56:50 -0300172
173 state = kzalloc(sizeof(struct vp27smpx_state), GFP_KERNEL);
Hans Verkuil35df38c2007-12-12 07:40:54 -0300174 if (state == NULL)
Hans Verkuilac247432007-07-27 06:56:50 -0300175 return -ENOMEM;
Hans Verkuild9460f02008-11-29 13:01:28 -0300176 sd = &state->sd;
177 v4l2_i2c_subdev_init(sd, client, &vp27smpx_ops);
Hans Verkuilac247432007-07-27 06:56:50 -0300178 state->audmode = V4L2_TUNER_MODE_STEREO;
Hans Verkuilac247432007-07-27 06:56:50 -0300179
180 /* initialize vp27smpx */
Hans Verkuild9460f02008-11-29 13:01:28 -0300181 vp27smpx_set_audmode(sd, state->audmode);
Hans Verkuilac247432007-07-27 06:56:50 -0300182 return 0;
183}
184
Hans Verkuil45eea272007-09-13 11:11:44 -0300185static int vp27smpx_remove(struct i2c_client *client)
Hans Verkuilac247432007-07-27 06:56:50 -0300186{
Hans Verkuild9460f02008-11-29 13:01:28 -0300187 struct v4l2_subdev *sd = i2c_get_clientdata(client);
188
189 v4l2_device_unregister_subdev(sd);
190 kfree(to_state(sd));
Hans Verkuilac247432007-07-27 06:56:50 -0300191 return 0;
192}
193
194/* ----------------------------------------------------------------------- */
195
Jean Delvareaf294862008-05-18 20:49:40 +0200196static const struct i2c_device_id vp27smpx_id[] = {
197 { "vp27smpx", 0 },
198 { }
199};
200MODULE_DEVICE_TABLE(i2c, vp27smpx_id);
201
Hans Verkuil45eea272007-09-13 11:11:44 -0300202static struct v4l2_i2c_driver_data v4l2_i2c_data = {
203 .name = "vp27smpx",
Hans Verkuil45eea272007-09-13 11:11:44 -0300204 .probe = vp27smpx_probe,
205 .remove = vp27smpx_remove,
Jean Delvareaf294862008-05-18 20:49:40 +0200206 .id_table = vp27smpx_id,
Hans Verkuilac247432007-07-27 06:56:50 -0300207};