]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/m52790.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6-omap-h63xx.git] / drivers / media / video / m52790.c
1 /*
2  * m52790 i2c ivtv driver.
3  * Copyright (C) 2007  Hans Verkuil
4  *
5  * A/V source switching Mitsubishi M52790SP/FP
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/ioctl.h>
26 #include <asm/uaccess.h>
27 #include <linux/i2c.h>
28 #include <linux/i2c-id.h>
29 #include <linux/videodev2.h>
30 #include <media/m52790.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-chip-ident.h>
33 #include <media/v4l2-i2c-drv.h>
34
35 MODULE_DESCRIPTION("i2c device driver for m52790 A/V switch");
36 MODULE_AUTHOR("Hans Verkuil");
37 MODULE_LICENSE("GPL");
38
39
40 struct m52790_state {
41         struct v4l2_subdev sd;
42         u16 input;
43         u16 output;
44 };
45
46 static inline struct m52790_state *to_state(struct v4l2_subdev *sd)
47 {
48         return container_of(sd, struct m52790_state, sd);
49 }
50
51 /* ----------------------------------------------------------------------- */
52
53 static int m52790_write(struct v4l2_subdev *sd)
54 {
55         struct m52790_state *state = to_state(sd);
56         struct i2c_client *client = v4l2_get_subdevdata(sd);
57
58         u8 sw1 = (state->input | state->output) & 0xff;
59         u8 sw2 = (state->input | state->output) >> 8;
60
61         return i2c_smbus_write_byte_data(client, sw1, sw2);
62 }
63
64 /* Note: audio and video are linked and cannot be switched separately.
65    So audio and video routing commands are identical for this chip.
66    In theory the video amplifier and audio modes could be handled
67    separately for the output, but that seems to be overkill right now.
68    The same holds for implementing an audio mute control, this is now
69    part of the audio output routing. The normal case is that another
70    chip takes care of the actual muting so making it part of the
71    output routing seems to be the right thing to do for now. */
72 static int m52790_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
73 {
74         struct m52790_state *state = to_state(sd);
75
76         state->input = route->input;
77         state->output = route->output;
78         m52790_write(sd);
79         return 0;
80 }
81
82 #ifdef CONFIG_VIDEO_ADV_DEBUG
83 static int m52790_g_register(struct v4l2_subdev *sd, struct v4l2_register *reg)
84 {
85         struct m52790_state *state = to_state(sd);
86         struct i2c_client *client = v4l2_get_subdevdata(sd);
87
88         if (!v4l2_chip_match_i2c_client(client,
89                                 reg->match_type, reg->match_chip))
90                 return -EINVAL;
91         if (!capable(CAP_SYS_ADMIN))
92                 return -EPERM;
93         if (reg->reg != 0)
94                 return -EINVAL;
95         reg->val = state->input | state->output;
96         return 0;
97 }
98
99 static int m52790_s_register(struct v4l2_subdev *sd, struct v4l2_register *reg)
100 {
101         struct m52790_state *state = to_state(sd);
102         struct i2c_client *client = v4l2_get_subdevdata(sd);
103
104         if (!v4l2_chip_match_i2c_client(client,
105                                 reg->match_type, reg->match_chip))
106                 return -EINVAL;
107         if (!capable(CAP_SYS_ADMIN))
108                 return -EPERM;
109         if (reg->reg != 0)
110                 return -EINVAL;
111         state->input = reg->val & 0x0303;
112         state->output = reg->val & ~0x0303;
113         m52790_write(sd);
114         return 0;
115 }
116 #endif
117
118 static int m52790_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_chip_ident *chip)
119 {
120         struct i2c_client *client = v4l2_get_subdevdata(sd);
121
122         return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_M52790, 0);
123 }
124
125 static int m52790_log_status(struct v4l2_subdev *sd)
126 {
127         struct m52790_state *state = to_state(sd);
128
129         v4l2_info(sd, "Switch 1: %02x\n",
130                         (state->input | state->output) & 0xff);
131         v4l2_info(sd, "Switch 2: %02x\n",
132                         (state->input | state->output) >> 8);
133         return 0;
134 }
135
136 static int m52790_command(struct i2c_client *client, unsigned cmd, void *arg)
137 {
138         return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
139 }
140
141 /* ----------------------------------------------------------------------- */
142
143 static const struct v4l2_subdev_core_ops m52790_core_ops = {
144         .log_status = m52790_log_status,
145         .g_chip_ident = m52790_g_chip_ident,
146 #ifdef CONFIG_VIDEO_ADV_DEBUG
147         .g_register = m52790_g_register,
148         .s_register = m52790_s_register,
149 #endif
150 };
151
152 static const struct v4l2_subdev_audio_ops m52790_audio_ops = {
153         .s_routing = m52790_s_routing,
154 };
155
156 static const struct v4l2_subdev_video_ops m52790_video_ops = {
157         .s_routing = m52790_s_routing,
158 };
159
160 static const struct v4l2_subdev_ops m52790_ops = {
161         .core = &m52790_core_ops,
162         .audio = &m52790_audio_ops,
163         .video = &m52790_video_ops,
164 };
165
166 /* ----------------------------------------------------------------------- */
167
168 /* i2c implementation */
169
170 static int m52790_probe(struct i2c_client *client,
171                         const struct i2c_device_id *id)
172 {
173         struct m52790_state *state;
174         struct v4l2_subdev *sd;
175
176         /* Check if the adapter supports the needed features */
177         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
178                 return -EIO;
179
180         v4l_info(client, "chip found @ 0x%x (%s)\n",
181                         client->addr << 1, client->adapter->name);
182
183         state = kmalloc(sizeof(struct m52790_state), GFP_KERNEL);
184         if (state == NULL)
185                 return -ENOMEM;
186
187         sd = &state->sd;
188         v4l2_i2c_subdev_init(sd, client, &m52790_ops);
189         state->input = M52790_IN_TUNER;
190         state->output = M52790_OUT_STEREO;
191         m52790_write(sd);
192         return 0;
193 }
194
195 static int m52790_remove(struct i2c_client *client)
196 {
197         struct v4l2_subdev *sd = i2c_get_clientdata(client);
198
199         v4l2_device_unregister_subdev(sd);
200         kfree(to_state(sd));
201         return 0;
202 }
203
204 /* ----------------------------------------------------------------------- */
205
206 static const struct i2c_device_id m52790_id[] = {
207         { "m52790", 0 },
208         { }
209 };
210 MODULE_DEVICE_TABLE(i2c, m52790_id);
211
212 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
213         .name = "m52790",
214         .driverid = I2C_DRIVERID_M52790,
215         .command = m52790_command,
216         .probe = m52790_probe,
217         .remove = m52790_remove,
218         .id_table = m52790_id,
219 };