blob: d7317e798cc474cb86bbca07a3fc945fbf800502 [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>
25#include <linux/ioctl.h>
26#include <asm/uaccess.h>
27#include <linux/i2c.h>
28#include <linux/i2c-id.h>
Hans Verkuil33b687c2008-07-25 05:32:50 -030029#include <linux/videodev2.h>
Hans Verkuil761dacd2007-10-30 05:41:25 -030030#include <media/m52790.h>
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030031#include <media/v4l2-device.h>
Hans Verkuil761dacd2007-10-30 05:41:25 -030032#include <media/v4l2-chip-ident.h>
Hans Verkuilf69d4192007-12-12 07:24:27 -030033#include <media/v4l2-i2c-drv.h>
Hans Verkuil761dacd2007-10-30 05:41:25 -030034
35MODULE_DESCRIPTION("i2c device driver for m52790 A/V switch");
36MODULE_AUTHOR("Hans Verkuil");
37MODULE_LICENSE("GPL");
38
Hans Verkuil761dacd2007-10-30 05:41:25 -030039
40struct m52790_state {
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030041 struct v4l2_subdev sd;
Hans Verkuil761dacd2007-10-30 05:41:25 -030042 u16 input;
43 u16 output;
44};
45
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030046static inline struct m52790_state *to_state(struct v4l2_subdev *sd)
47{
48 return container_of(sd, struct m52790_state, sd);
49}
50
Hans Verkuil761dacd2007-10-30 05:41:25 -030051/* ----------------------------------------------------------------------- */
52
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030053static int m52790_write(struct v4l2_subdev *sd)
Hans Verkuil761dacd2007-10-30 05:41:25 -030054{
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030055 struct m52790_state *state = to_state(sd);
56 struct i2c_client *client = v4l2_get_subdevdata(sd);
57
Hans Verkuil761dacd2007-10-30 05:41:25 -030058 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
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030064/* 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. */
Hans Verkuil5325b422009-04-02 11:26:22 -030072static int m52790_s_routing(struct v4l2_subdev *sd,
73 u32 input, u32 output, u32 config)
Hans Verkuil761dacd2007-10-30 05:41:25 -030074{
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030075 struct m52790_state *state = to_state(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -030076
Hans Verkuil5325b422009-04-02 11:26:22 -030077 state->input = input;
78 state->output = output;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030079 m52790_write(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -030080 return 0;
81}
82
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030083#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b2008-12-30 07:14:19 -030084static int m52790_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030085{
86 struct m52790_state *state = to_state(sd);
87 struct i2c_client *client = v4l2_get_subdevdata(sd);
88
Hans Verkuilaecde8b2008-12-30 07:14:19 -030089 if (!v4l2_chip_match_i2c_client(client, &reg->match))
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030090 return -EINVAL;
91 if (!capable(CAP_SYS_ADMIN))
92 return -EPERM;
93 if (reg->reg != 0)
94 return -EINVAL;
Hans Verkuilaecde8b2008-12-30 07:14:19 -030095 reg->size = 1;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -030096 reg->val = state->input | state->output;
97 return 0;
98}
99
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300100static int m52790_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300101{
102 struct m52790_state *state = to_state(sd);
103 struct i2c_client *client = v4l2_get_subdevdata(sd);
104
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300105 if (!v4l2_chip_match_i2c_client(client, &reg->match))
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300106 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
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300118static int m52790_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300119{
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
125static 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
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300136/* ----------------------------------------------------------------------- */
137
138static const struct v4l2_subdev_core_ops m52790_core_ops = {
139 .log_status = m52790_log_status,
140 .g_chip_ident = m52790_g_chip_ident,
141#ifdef CONFIG_VIDEO_ADV_DEBUG
142 .g_register = m52790_g_register,
143 .s_register = m52790_s_register,
144#endif
145};
146
147static const struct v4l2_subdev_audio_ops m52790_audio_ops = {
148 .s_routing = m52790_s_routing,
149};
150
151static const struct v4l2_subdev_video_ops m52790_video_ops = {
152 .s_routing = m52790_s_routing,
153};
154
155static const struct v4l2_subdev_ops m52790_ops = {
156 .core = &m52790_core_ops,
157 .audio = &m52790_audio_ops,
158 .video = &m52790_video_ops,
159};
160
Hans Verkuil761dacd2007-10-30 05:41:25 -0300161/* ----------------------------------------------------------------------- */
162
163/* i2c implementation */
164
Jean Delvared2653e92008-04-29 23:11:39 +0200165static int m52790_probe(struct i2c_client *client,
166 const struct i2c_device_id *id)
Hans Verkuil761dacd2007-10-30 05:41:25 -0300167{
168 struct m52790_state *state;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300169 struct v4l2_subdev *sd;
Hans Verkuil761dacd2007-10-30 05:41:25 -0300170
171 /* Check if the adapter supports the needed features */
172 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
173 return -EIO;
174
Hans Verkuil761dacd2007-10-30 05:41:25 -0300175 v4l_info(client, "chip found @ 0x%x (%s)\n",
176 client->addr << 1, client->adapter->name);
177
178 state = kmalloc(sizeof(struct m52790_state), GFP_KERNEL);
179 if (state == NULL)
180 return -ENOMEM;
181
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300182 sd = &state->sd;
183 v4l2_i2c_subdev_init(sd, client, &m52790_ops);
Hans Verkuil761dacd2007-10-30 05:41:25 -0300184 state->input = M52790_IN_TUNER;
185 state->output = M52790_OUT_STEREO;
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300186 m52790_write(sd);
Hans Verkuil761dacd2007-10-30 05:41:25 -0300187 return 0;
188}
189
190static int m52790_remove(struct i2c_client *client)
191{
Hans Verkuil5d302f2e2008-11-29 12:51:32 -0300192 struct v4l2_subdev *sd = i2c_get_clientdata(client);
193
194 v4l2_device_unregister_subdev(sd);
195 kfree(to_state(sd));
Hans Verkuil761dacd2007-10-30 05:41:25 -0300196 return 0;
197}
198
199/* ----------------------------------------------------------------------- */
200
Jean Delvareaf294862008-05-18 20:49:40 +0200201static const struct i2c_device_id m52790_id[] = {
202 { "m52790", 0 },
203 { }
204};
205MODULE_DEVICE_TABLE(i2c, m52790_id);
206
Hans Verkuil761dacd2007-10-30 05:41:25 -0300207static struct v4l2_i2c_driver_data v4l2_i2c_data = {
208 .name = "m52790",
Hans Verkuil761dacd2007-10-30 05:41:25 -0300209 .probe = m52790_probe,
210 .remove = m52790_remove,
Jean Delvareaf294862008-05-18 20:49:40 +0200211 .id_table = m52790_id,
Hans Verkuil761dacd2007-10-30 05:41:25 -0300212};