blob: c15efb6e7771987240000a38040454521552a930 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Hans Verkuilac247432007-07-27 06:56:50 -030027#include <linux/ioctl.h>
28#include <asm/uaccess.h>
29#include <linux/i2c.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>
33
34MODULE_DESCRIPTION("vp27smpx driver");
35MODULE_AUTHOR("Hans Verkuil");
36MODULE_LICENSE("GPL");
37
Hans Verkuilac247432007-07-27 06:56:50 -030038
39/* ----------------------------------------------------------------------- */
40
41struct vp27smpx_state {
Hans Verkuild9460f02008-11-29 13:01:28 -030042 struct v4l2_subdev sd;
Hans Verkuilac247432007-07-27 06:56:50 -030043 int radio;
44 u32 audmode;
45};
46
Hans Verkuild9460f02008-11-29 13:01:28 -030047static inline struct vp27smpx_state *to_state(struct v4l2_subdev *sd)
Hans Verkuilac247432007-07-27 06:56:50 -030048{
Hans Verkuild9460f02008-11-29 13:01:28 -030049 return container_of(sd, struct vp27smpx_state, sd);
50}
51
52static void vp27smpx_set_audmode(struct v4l2_subdev *sd, u32 audmode)
53{
54 struct vp27smpx_state *state = to_state(sd);
55 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilac247432007-07-27 06:56:50 -030056 u8 data[3] = { 0x00, 0x00, 0x04 };
57
58 switch (audmode) {
Hans Verkuil35df38c2007-12-12 07:40:54 -030059 case V4L2_TUNER_MODE_MONO:
60 case V4L2_TUNER_MODE_LANG1:
61 break;
62 case V4L2_TUNER_MODE_STEREO:
63 case V4L2_TUNER_MODE_LANG1_LANG2:
64 data[1] = 0x01;
65 break;
66 case V4L2_TUNER_MODE_LANG2:
67 data[1] = 0x02;
68 break;
Hans Verkuilac247432007-07-27 06:56:50 -030069 }
70
Hans Verkuil35df38c2007-12-12 07:40:54 -030071 if (i2c_master_send(client, data, sizeof(data)) != sizeof(data))
Hans Verkuild9460f02008-11-29 13:01:28 -030072 v4l2_err(sd, "I/O error setting audmode\n");
Hans Verkuil35df38c2007-12-12 07:40:54 -030073 else
Hans Verkuilac247432007-07-27 06:56:50 -030074 state->audmode = audmode;
Hans Verkuilac247432007-07-27 06:56:50 -030075}
76
Hans Verkuild9460f02008-11-29 13:01:28 -030077static int vp27smpx_s_radio(struct v4l2_subdev *sd)
Hans Verkuilac247432007-07-27 06:56:50 -030078{
Hans Verkuild9460f02008-11-29 13:01:28 -030079 struct vp27smpx_state *state = to_state(sd);
Hans Verkuilac247432007-07-27 06:56:50 -030080
Hans Verkuild9460f02008-11-29 13:01:28 -030081 state->radio = 1;
Hans Verkuilac247432007-07-27 06:56:50 -030082 return 0;
83}
84
Hans Verkuild9460f02008-11-29 13:01:28 -030085static int vp27smpx_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
86{
87 struct vp27smpx_state *state = to_state(sd);
88
89 state->radio = 0;
90 return 0;
91}
92
93static int vp27smpx_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
94{
95 struct vp27smpx_state *state = to_state(sd);
96
97 if (!state->radio)
98 vp27smpx_set_audmode(sd, vt->audmode);
99 return 0;
100}
101
102static int vp27smpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
103{
104 struct vp27smpx_state *state = to_state(sd);
105
106 if (state->radio)
107 return 0;
108 vt->audmode = state->audmode;
109 vt->capability = V4L2_TUNER_CAP_STEREO |
110 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
111 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
112 return 0;
113}
114
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300115static int vp27smpx_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
Hans Verkuild9460f02008-11-29 13:01:28 -0300116{
117 struct i2c_client *client = v4l2_get_subdevdata(sd);
118
119 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_VP27SMPX, 0);
120}
121
122static int vp27smpx_log_status(struct v4l2_subdev *sd)
123{
124 struct vp27smpx_state *state = to_state(sd);
125
126 v4l2_info(sd, "Audio Mode: %u%s\n", state->audmode,
127 state->radio ? " (Radio)" : "");
128 return 0;
129}
130
Hans Verkuild9460f02008-11-29 13:01:28 -0300131/* ----------------------------------------------------------------------- */
132
133static const struct v4l2_subdev_core_ops vp27smpx_core_ops = {
134 .log_status = vp27smpx_log_status,
135 .g_chip_ident = vp27smpx_g_chip_ident,
Hans Verkuilf41737e2009-04-01 03:52:39 -0300136 .s_std = vp27smpx_s_std,
Hans Verkuild9460f02008-11-29 13:01:28 -0300137};
138
139static const struct v4l2_subdev_tuner_ops vp27smpx_tuner_ops = {
140 .s_radio = vp27smpx_s_radio,
Hans Verkuild9460f02008-11-29 13:01:28 -0300141 .s_tuner = vp27smpx_s_tuner,
142 .g_tuner = vp27smpx_g_tuner,
143};
144
145static const struct v4l2_subdev_ops vp27smpx_ops = {
146 .core = &vp27smpx_core_ops,
147 .tuner = &vp27smpx_tuner_ops,
148};
149
Hans Verkuilac247432007-07-27 06:56:50 -0300150/* ----------------------------------------------------------------------- */
151
152/* i2c implementation */
153
154/*
155 * Generic i2c probe
156 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
157 */
158
Jean Delvared2653e92008-04-29 23:11:39 +0200159static int vp27smpx_probe(struct i2c_client *client,
160 const struct i2c_device_id *id)
Hans Verkuilac247432007-07-27 06:56:50 -0300161{
Hans Verkuilac247432007-07-27 06:56:50 -0300162 struct vp27smpx_state *state;
Hans Verkuild9460f02008-11-29 13:01:28 -0300163 struct v4l2_subdev *sd;
Hans Verkuilac247432007-07-27 06:56:50 -0300164
165 /* Check if the adapter supports the needed features */
Hans Verkuil45eea272007-09-13 11:11:44 -0300166 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Hans Verkuil188f3452007-09-16 10:47:15 -0300167 return -EIO;
Hans Verkuilac247432007-07-27 06:56:50 -0300168
Hans Verkuil35df38c2007-12-12 07:40:54 -0300169 v4l_info(client, "chip found @ 0x%x (%s)\n",
170 client->addr << 1, client->adapter->name);
Hans Verkuilac247432007-07-27 06:56:50 -0300171
172 state = kzalloc(sizeof(struct vp27smpx_state), GFP_KERNEL);
Hans Verkuil35df38c2007-12-12 07:40:54 -0300173 if (state == NULL)
Hans Verkuilac247432007-07-27 06:56:50 -0300174 return -ENOMEM;
Hans Verkuild9460f02008-11-29 13:01:28 -0300175 sd = &state->sd;
176 v4l2_i2c_subdev_init(sd, client, &vp27smpx_ops);
Hans Verkuilac247432007-07-27 06:56:50 -0300177 state->audmode = V4L2_TUNER_MODE_STEREO;
Hans Verkuilac247432007-07-27 06:56:50 -0300178
179 /* initialize vp27smpx */
Hans Verkuild9460f02008-11-29 13:01:28 -0300180 vp27smpx_set_audmode(sd, state->audmode);
Hans Verkuilac247432007-07-27 06:56:50 -0300181 return 0;
182}
183
Hans Verkuil45eea272007-09-13 11:11:44 -0300184static int vp27smpx_remove(struct i2c_client *client)
Hans Verkuilac247432007-07-27 06:56:50 -0300185{
Hans Verkuild9460f02008-11-29 13:01:28 -0300186 struct v4l2_subdev *sd = i2c_get_clientdata(client);
187
188 v4l2_device_unregister_subdev(sd);
189 kfree(to_state(sd));
Hans Verkuilac247432007-07-27 06:56:50 -0300190 return 0;
191}
192
193/* ----------------------------------------------------------------------- */
194
Jean Delvareaf294862008-05-18 20:49:40 +0200195static const struct i2c_device_id vp27smpx_id[] = {
196 { "vp27smpx", 0 },
197 { }
198};
199MODULE_DEVICE_TABLE(i2c, vp27smpx_id);
200
Hans Verkuil1f352662010-09-15 14:56:12 -0300201static struct i2c_driver vp27smpx_driver = {
202 .driver = {
203 .owner = THIS_MODULE,
204 .name = "vp27smpx",
205 },
206 .probe = vp27smpx_probe,
207 .remove = vp27smpx_remove,
208 .id_table = vp27smpx_id,
Hans Verkuilac247432007-07-27 06:56:50 -0300209};
Hans Verkuil1f352662010-09-15 14:56:12 -0300210
211static __init int init_vp27smpx(void)
212{
213 return i2c_add_driver(&vp27smpx_driver);
214}
215
216static __exit void exit_vp27smpx(void)
217{
218 i2c_del_driver(&vp27smpx_driver);
219}
220
221module_init(init_vp27smpx);
222module_exit(exit_vp27smpx);