blob: 4491d018eba6518c0e663419bc7e7a3a292f6511 [file] [log] [blame]
Hans Verkuil761dacd2007-10-30 05:41:25 -03001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Hans Verkuil761dacd2007-10-30 05:41:25 -030026#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 Verkuil761dacd2007-10-30 05:41:25 -030031#include <media/m52790.h>
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030032#include <media/v4l2-device.h>
Hans Verkuil761dacd2007-10-30 05:41:25 -030033#include <media/v4l2-chip-ident.h>
Hans Verkuilf69d4192007-12-12 07:24:27 -030034#include <media/v4l2-i2c-drv.h>
Hans Verkuil761dacd2007-10-30 05:41:25 -030035
36MODULE_DESCRIPTION("i2c device driver for m52790 A/V switch");
37MODULE_AUTHOR("Hans Verkuil");
38MODULE_LICENSE("GPL");
39
Hans Verkuil761dacd2007-10-30 05:41:25 -030040
41struct m52790_state {
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030042 struct v4l2_subdev sd;
Hans Verkuil761dacd2007-10-30 05:41:25 -030043 u16 input;
44 u16 output;
45};
46
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030047static inline struct m52790_state *to_state(struct v4l2_subdev *sd)
48{
49 return container_of(sd, struct m52790_state, sd);
50}
51
Hans Verkuil761dacd2007-10-30 05:41:25 -030052/* ----------------------------------------------------------------------- */
53
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030054static int m52790_write(struct v4l2_subdev *sd)
Hans Verkuil761dacd2007-10-30 05:41:25 -030055{
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030056 struct m52790_state *state = to_state(sd);
57 struct i2c_client *client = v4l2_get_subdevdata(sd);
58
Hans Verkuil761dacd2007-10-30 05:41:25 -030059 u8 sw1 = (state->input | state->output) & 0xff;
60 u8 sw2 = (state->input | state->output) >> 8;
61
62 return i2c_smbus_write_byte_data(client, sw1, sw2);
63}
64
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030065/* Note: audio and video are linked and cannot be switched separately.
66 So audio and video routing commands are identical for this chip.
67 In theory the video amplifier and audio modes could be handled
68 separately for the output, but that seems to be overkill right now.
69 The same holds for implementing an audio mute control, this is now
70 part of the audio output routing. The normal case is that another
71 chip takes care of the actual muting so making it part of the
72 output routing seems to be the right thing to do for now. */
Hans Verkuil5325b422009-04-02 11:26:22 -030073static int m52790_s_routing(struct v4l2_subdev *sd,
74 u32 input, u32 output, u32 config)
Hans Verkuil761dacd2007-10-30 05:41:25 -030075{
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030076 struct m52790_state *state = to_state(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -030077
Hans Verkuil5325b422009-04-02 11:26:22 -030078 state->input = input;
79 state->output = output;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030080 m52790_write(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -030081 return 0;
82}
83
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030084#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b2008-12-30 07:14:19 -030085static int m52790_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030086{
87 struct m52790_state *state = to_state(sd);
88 struct i2c_client *client = v4l2_get_subdevdata(sd);
89
Hans Verkuilaecde8b2008-12-30 07:14:19 -030090 if (!v4l2_chip_match_i2c_client(client, &reg->match))
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030091 return -EINVAL;
92 if (!capable(CAP_SYS_ADMIN))
93 return -EPERM;
94 if (reg->reg != 0)
95 return -EINVAL;
Hans Verkuilaecde8b2008-12-30 07:14:19 -030096 reg->size = 1;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030097 reg->val = state->input | state->output;
98 return 0;
99}
100
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300101static int m52790_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300102{
103 struct m52790_state *state = to_state(sd);
104 struct i2c_client *client = v4l2_get_subdevdata(sd);
105
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300106 if (!v4l2_chip_match_i2c_client(client, &reg->match))
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300107 return -EINVAL;
108 if (!capable(CAP_SYS_ADMIN))
109 return -EPERM;
110 if (reg->reg != 0)
111 return -EINVAL;
112 state->input = reg->val & 0x0303;
113 state->output = reg->val & ~0x0303;
114 m52790_write(sd);
115 return 0;
116}
117#endif
118
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300119static int m52790_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300120{
121 struct i2c_client *client = v4l2_get_subdevdata(sd);
122
123 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_M52790, 0);
124}
125
126static int m52790_log_status(struct v4l2_subdev *sd)
127{
128 struct m52790_state *state = to_state(sd);
129
130 v4l2_info(sd, "Switch 1: %02x\n",
131 (state->input | state->output) & 0xff);
132 v4l2_info(sd, "Switch 2: %02x\n",
133 (state->input | state->output) >> 8);
134 return 0;
135}
136
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300137/* ----------------------------------------------------------------------- */
138
139static const struct v4l2_subdev_core_ops m52790_core_ops = {
140 .log_status = m52790_log_status,
141 .g_chip_ident = m52790_g_chip_ident,
142#ifdef CONFIG_VIDEO_ADV_DEBUG
143 .g_register = m52790_g_register,
144 .s_register = m52790_s_register,
145#endif
146};
147
148static const struct v4l2_subdev_audio_ops m52790_audio_ops = {
149 .s_routing = m52790_s_routing,
150};
151
152static const struct v4l2_subdev_video_ops m52790_video_ops = {
153 .s_routing = m52790_s_routing,
154};
155
156static const struct v4l2_subdev_ops m52790_ops = {
157 .core = &m52790_core_ops,
158 .audio = &m52790_audio_ops,
159 .video = &m52790_video_ops,
160};
161
Hans Verkuil761dacd2007-10-30 05:41:25 -0300162/* ----------------------------------------------------------------------- */
163
164/* i2c implementation */
165
Jean Delvared2653e92008-04-29 23:11:39 +0200166static int m52790_probe(struct i2c_client *client,
167 const struct i2c_device_id *id)
Hans Verkuil761dacd2007-10-30 05:41:25 -0300168{
169 struct m52790_state *state;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300170 struct v4l2_subdev *sd;
Hans Verkuil761dacd2007-10-30 05:41:25 -0300171
172 /* Check if the adapter supports the needed features */
173 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
174 return -EIO;
175
Hans Verkuil761dacd2007-10-30 05:41:25 -0300176 v4l_info(client, "chip found @ 0x%x (%s)\n",
177 client->addr << 1, client->adapter->name);
178
179 state = kmalloc(sizeof(struct m52790_state), GFP_KERNEL);
180 if (state == NULL)
181 return -ENOMEM;
182
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300183 sd = &state->sd;
184 v4l2_i2c_subdev_init(sd, client, &m52790_ops);
Hans Verkuil761dacd2007-10-30 05:41:25 -0300185 state->input = M52790_IN_TUNER;
186 state->output = M52790_OUT_STEREO;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300187 m52790_write(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -0300188 return 0;
189}
190
191static int m52790_remove(struct i2c_client *client)
192{
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300193 struct v4l2_subdev *sd = i2c_get_clientdata(client);
194
195 v4l2_device_unregister_subdev(sd);
196 kfree(to_state(sd));
Hans Verkuil761dacd2007-10-30 05:41:25 -0300197 return 0;
198}
199
200/* ----------------------------------------------------------------------- */
201
Jean Delvareaf294862008-05-18 20:49:40 +0200202static const struct i2c_device_id m52790_id[] = {
203 { "m52790", 0 },
204 { }
205};
206MODULE_DEVICE_TABLE(i2c, m52790_id);
207
Hans Verkuil761dacd2007-10-30 05:41:25 -0300208static struct v4l2_i2c_driver_data v4l2_i2c_data = {
209 .name = "m52790",
Hans Verkuil761dacd2007-10-30 05:41:25 -0300210 .probe = m52790_probe,
211 .remove = m52790_remove,
Jean Delvareaf294862008-05-18 20:49:40 +0200212 .id_table = m52790_id,
Hans Verkuil761dacd2007-10-30 05:41:25 -0300213};