]>
Commit | Line | Data |
---|---|---|
761dacd2 HV |
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. | |
761dacd2 HV |
16 | */ |
17 | ||
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/types.h> | |
5a0e3ad6 | 21 | #include <linux/slab.h> |
761dacd2 | 22 | #include <linux/ioctl.h> |
7c0f6ba6 | 23 | #include <linux/uaccess.h> |
761dacd2 | 24 | #include <linux/i2c.h> |
33b687cf | 25 | #include <linux/videodev2.h> |
b5dcee22 | 26 | #include <media/i2c/m52790.h> |
5d302f2e | 27 | #include <media/v4l2-device.h> |
761dacd2 HV |
28 | |
29 | MODULE_DESCRIPTION("i2c device driver for m52790 A/V switch"); | |
30 | MODULE_AUTHOR("Hans Verkuil"); | |
31 | MODULE_LICENSE("GPL"); | |
32 | ||
761dacd2 HV |
33 | |
34 | struct m52790_state { | |
5d302f2e | 35 | struct v4l2_subdev sd; |
761dacd2 HV |
36 | u16 input; |
37 | u16 output; | |
38 | }; | |
39 | ||
5d302f2e HV |
40 | static inline struct m52790_state *to_state(struct v4l2_subdev *sd) |
41 | { | |
42 | return container_of(sd, struct m52790_state, sd); | |
43 | } | |
44 | ||
761dacd2 HV |
45 | /* ----------------------------------------------------------------------- */ |
46 | ||
5d302f2e | 47 | static int m52790_write(struct v4l2_subdev *sd) |
761dacd2 | 48 | { |
5d302f2e HV |
49 | struct m52790_state *state = to_state(sd); |
50 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
51 | ||
761dacd2 HV |
52 | u8 sw1 = (state->input | state->output) & 0xff; |
53 | u8 sw2 = (state->input | state->output) >> 8; | |
54 | ||
55 | return i2c_smbus_write_byte_data(client, sw1, sw2); | |
56 | } | |
57 | ||
5d302f2e HV |
58 | /* Note: audio and video are linked and cannot be switched separately. |
59 | So audio and video routing commands are identical for this chip. | |
60 | In theory the video amplifier and audio modes could be handled | |
61 | separately for the output, but that seems to be overkill right now. | |
62 | The same holds for implementing an audio mute control, this is now | |
63 | part of the audio output routing. The normal case is that another | |
64 | chip takes care of the actual muting so making it part of the | |
65 | output routing seems to be the right thing to do for now. */ | |
5325b427 HV |
66 | static int m52790_s_routing(struct v4l2_subdev *sd, |
67 | u32 input, u32 output, u32 config) | |
761dacd2 | 68 | { |
5d302f2e HV |
69 | struct m52790_state *state = to_state(sd); |
70 | ||
5325b427 HV |
71 | state->input = input; |
72 | state->output = output; | |
5d302f2e HV |
73 | m52790_write(sd); |
74 | return 0; | |
75 | } | |
761dacd2 HV |
76 | |
77 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
aecde8b5 | 78 | static int m52790_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg) |
5d302f2e HV |
79 | { |
80 | struct m52790_state *state = to_state(sd); | |
761dacd2 | 81 | |
5d302f2e HV |
82 | if (reg->reg != 0) |
83 | return -EINVAL; | |
aecde8b5 | 84 | reg->size = 1; |
5d302f2e HV |
85 | reg->val = state->input | state->output; |
86 | return 0; | |
87 | } | |
761dacd2 | 88 | |
977ba3b1 | 89 | static int m52790_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg) |
5d302f2e HV |
90 | { |
91 | struct m52790_state *state = to_state(sd); | |
761dacd2 | 92 | |
5d302f2e HV |
93 | if (reg->reg != 0) |
94 | return -EINVAL; | |
95 | state->input = reg->val & 0x0303; | |
96 | state->output = reg->val & ~0x0303; | |
97 | m52790_write(sd); | |
761dacd2 HV |
98 | return 0; |
99 | } | |
5d302f2e HV |
100 | #endif |
101 | ||
5d302f2e HV |
102 | static int m52790_log_status(struct v4l2_subdev *sd) |
103 | { | |
104 | struct m52790_state *state = to_state(sd); | |
105 | ||
106 | v4l2_info(sd, "Switch 1: %02x\n", | |
107 | (state->input | state->output) & 0xff); | |
108 | v4l2_info(sd, "Switch 2: %02x\n", | |
109 | (state->input | state->output) >> 8); | |
110 | return 0; | |
111 | } | |
112 | ||
5d302f2e HV |
113 | /* ----------------------------------------------------------------------- */ |
114 | ||
115 | static const struct v4l2_subdev_core_ops m52790_core_ops = { | |
116 | .log_status = m52790_log_status, | |
5d302f2e HV |
117 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
118 | .g_register = m52790_g_register, | |
119 | .s_register = m52790_s_register, | |
120 | #endif | |
121 | }; | |
122 | ||
123 | static const struct v4l2_subdev_audio_ops m52790_audio_ops = { | |
124 | .s_routing = m52790_s_routing, | |
125 | }; | |
126 | ||
127 | static const struct v4l2_subdev_video_ops m52790_video_ops = { | |
128 | .s_routing = m52790_s_routing, | |
129 | }; | |
130 | ||
131 | static const struct v4l2_subdev_ops m52790_ops = { | |
132 | .core = &m52790_core_ops, | |
133 | .audio = &m52790_audio_ops, | |
134 | .video = &m52790_video_ops, | |
135 | }; | |
761dacd2 HV |
136 | |
137 | /* ----------------------------------------------------------------------- */ | |
138 | ||
139 | /* i2c implementation */ | |
140 | ||
d2653e92 JD |
141 | static int m52790_probe(struct i2c_client *client, |
142 | const struct i2c_device_id *id) | |
761dacd2 HV |
143 | { |
144 | struct m52790_state *state; | |
5d302f2e | 145 | struct v4l2_subdev *sd; |
761dacd2 HV |
146 | |
147 | /* Check if the adapter supports the needed features */ | |
148 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
149 | return -EIO; | |
150 | ||
761dacd2 HV |
151 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
152 | client->addr << 1, client->adapter->name); | |
153 | ||
c02b211d | 154 | state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL); |
761dacd2 HV |
155 | if (state == NULL) |
156 | return -ENOMEM; | |
157 | ||
5d302f2e HV |
158 | sd = &state->sd; |
159 | v4l2_i2c_subdev_init(sd, client, &m52790_ops); | |
761dacd2 HV |
160 | state->input = M52790_IN_TUNER; |
161 | state->output = M52790_OUT_STEREO; | |
5d302f2e | 162 | m52790_write(sd); |
761dacd2 HV |
163 | return 0; |
164 | } | |
165 | ||
166 | static int m52790_remove(struct i2c_client *client) | |
167 | { | |
5d302f2e HV |
168 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
169 | ||
170 | v4l2_device_unregister_subdev(sd); | |
761dacd2 HV |
171 | return 0; |
172 | } | |
173 | ||
174 | /* ----------------------------------------------------------------------- */ | |
175 | ||
af294867 JD |
176 | static const struct i2c_device_id m52790_id[] = { |
177 | { "m52790", 0 }, | |
178 | { } | |
179 | }; | |
180 | MODULE_DEVICE_TABLE(i2c, m52790_id); | |
181 | ||
d30f60b3 HV |
182 | static struct i2c_driver m52790_driver = { |
183 | .driver = { | |
d30f60b3 HV |
184 | .name = "m52790", |
185 | }, | |
186 | .probe = m52790_probe, | |
187 | .remove = m52790_remove, | |
188 | .id_table = m52790_id, | |
761dacd2 | 189 | }; |
d30f60b3 | 190 | |
c6e8d86f | 191 | module_i2c_driver(m52790_driver); |