xref: /openbmc/linux/drivers/media/i2c/m52790.c (revision 7e89bd9f)
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/slab.h>
26 #include <linux/ioctl.h>
27 #include <asm/uaccess.h>
28 #include <linux/i2c.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 
34 MODULE_DESCRIPTION("i2c device driver for m52790 A/V switch");
35 MODULE_AUTHOR("Hans Verkuil");
36 MODULE_LICENSE("GPL");
37 
38 
39 struct m52790_state {
40 	struct v4l2_subdev sd;
41 	u16 input;
42 	u16 output;
43 };
44 
45 static inline struct m52790_state *to_state(struct v4l2_subdev *sd)
46 {
47 	return container_of(sd, struct m52790_state, sd);
48 }
49 
50 /* ----------------------------------------------------------------------- */
51 
52 static int m52790_write(struct v4l2_subdev *sd)
53 {
54 	struct m52790_state *state = to_state(sd);
55 	struct i2c_client *client = v4l2_get_subdevdata(sd);
56 
57 	u8 sw1 = (state->input | state->output) & 0xff;
58 	u8 sw2 = (state->input | state->output) >> 8;
59 
60 	return i2c_smbus_write_byte_data(client, sw1, sw2);
61 }
62 
63 /* Note: audio and video are linked and cannot be switched separately.
64    So audio and video routing commands are identical for this chip.
65    In theory the video amplifier and audio modes could be handled
66    separately for the output, but that seems to be overkill right now.
67    The same holds for implementing an audio mute control, this is now
68    part of the audio output routing. The normal case is that another
69    chip takes care of the actual muting so making it part of the
70    output routing seems to be the right thing to do for now. */
71 static int m52790_s_routing(struct v4l2_subdev *sd,
72 			    u32 input, u32 output, u32 config)
73 {
74 	struct m52790_state *state = to_state(sd);
75 
76 	state->input = input;
77 	state->output = 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_dbg_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, &reg->match))
89 		return -EINVAL;
90 	if (reg->reg != 0)
91 		return -EINVAL;
92 	reg->size = 1;
93 	reg->val = state->input | state->output;
94 	return 0;
95 }
96 
97 static int m52790_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
98 {
99 	struct m52790_state *state = to_state(sd);
100 	struct i2c_client *client = v4l2_get_subdevdata(sd);
101 
102 	if (!v4l2_chip_match_i2c_client(client, &reg->match))
103 		return -EINVAL;
104 	if (reg->reg != 0)
105 		return -EINVAL;
106 	state->input = reg->val & 0x0303;
107 	state->output = reg->val & ~0x0303;
108 	m52790_write(sd);
109 	return 0;
110 }
111 #endif
112 
113 static int m52790_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
114 {
115 	struct i2c_client *client = v4l2_get_subdevdata(sd);
116 
117 	return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_M52790, 0);
118 }
119 
120 static int m52790_log_status(struct v4l2_subdev *sd)
121 {
122 	struct m52790_state *state = to_state(sd);
123 
124 	v4l2_info(sd, "Switch 1: %02x\n",
125 			(state->input | state->output) & 0xff);
126 	v4l2_info(sd, "Switch 2: %02x\n",
127 			(state->input | state->output) >> 8);
128 	return 0;
129 }
130 
131 /* ----------------------------------------------------------------------- */
132 
133 static const struct v4l2_subdev_core_ops m52790_core_ops = {
134 	.log_status = m52790_log_status,
135 	.g_chip_ident = m52790_g_chip_ident,
136 #ifdef CONFIG_VIDEO_ADV_DEBUG
137 	.g_register = m52790_g_register,
138 	.s_register = m52790_s_register,
139 #endif
140 };
141 
142 static const struct v4l2_subdev_audio_ops m52790_audio_ops = {
143 	.s_routing = m52790_s_routing,
144 };
145 
146 static const struct v4l2_subdev_video_ops m52790_video_ops = {
147 	.s_routing = m52790_s_routing,
148 };
149 
150 static const struct v4l2_subdev_ops m52790_ops = {
151 	.core = &m52790_core_ops,
152 	.audio = &m52790_audio_ops,
153 	.video = &m52790_video_ops,
154 };
155 
156 /* ----------------------------------------------------------------------- */
157 
158 /* i2c implementation */
159 
160 static int m52790_probe(struct i2c_client *client,
161 			const struct i2c_device_id *id)
162 {
163 	struct m52790_state *state;
164 	struct v4l2_subdev *sd;
165 
166 	/* Check if the adapter supports the needed features */
167 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
168 		return -EIO;
169 
170 	v4l_info(client, "chip found @ 0x%x (%s)\n",
171 			client->addr << 1, client->adapter->name);
172 
173 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
174 	if (state == NULL)
175 		return -ENOMEM;
176 
177 	sd = &state->sd;
178 	v4l2_i2c_subdev_init(sd, client, &m52790_ops);
179 	state->input = M52790_IN_TUNER;
180 	state->output = M52790_OUT_STEREO;
181 	m52790_write(sd);
182 	return 0;
183 }
184 
185 static int m52790_remove(struct i2c_client *client)
186 {
187 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
188 
189 	v4l2_device_unregister_subdev(sd);
190 	return 0;
191 }
192 
193 /* ----------------------------------------------------------------------- */
194 
195 static const struct i2c_device_id m52790_id[] = {
196 	{ "m52790", 0 },
197 	{ }
198 };
199 MODULE_DEVICE_TABLE(i2c, m52790_id);
200 
201 static struct i2c_driver m52790_driver = {
202 	.driver = {
203 		.owner	= THIS_MODULE,
204 		.name	= "m52790",
205 	},
206 	.probe		= m52790_probe,
207 	.remove		= m52790_remove,
208 	.id_table	= m52790_id,
209 };
210 
211 module_i2c_driver(m52790_driver);
212