1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "ivtv-driver.h"
23#include "ivtv-cards.h"
24#include "ivtv-gpio.h"
25#include "tuner-xc2028.h"
26#include <media/tuner.h>
27#include <media/v4l2-ctrls.h>
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102#define IVTV_REG_GPIO_IN 0x9008
103#define IVTV_REG_GPIO_OUT 0x900c
104#define IVTV_REG_GPIO_DIR 0x9020
105
106void ivtv_reset_ir_gpio(struct ivtv *itv)
107{
108 int curdir, curout;
109
110 if (itv->card->type != IVTV_CARD_PVR_150)
111 return;
112 IVTV_DEBUG_INFO("Resetting PVR150 IR\n");
113 curout = read_reg(IVTV_REG_GPIO_OUT);
114 curdir = read_reg(IVTV_REG_GPIO_DIR);
115 curdir |= 0x80;
116 write_reg(curdir, IVTV_REG_GPIO_DIR);
117 curout = (curout & ~0xF) | 1;
118 write_reg(curout, IVTV_REG_GPIO_OUT);
119
120 schedule_timeout_interruptible(msecs_to_jiffies(1));
121 curout |= 2;
122 write_reg(curout, IVTV_REG_GPIO_OUT);
123 curdir &= ~0x80;
124 write_reg(curdir, IVTV_REG_GPIO_DIR);
125}
126
127
128int ivtv_reset_tuner_gpio(void *dev, int component, int cmd, int value)
129{
130 struct i2c_algo_bit_data *algo = dev;
131 struct ivtv *itv = algo->data;
132 u32 curout;
133
134 if (cmd != XC2028_TUNER_RESET)
135 return 0;
136 IVTV_DEBUG_INFO("Resetting tuner\n");
137 curout = read_reg(IVTV_REG_GPIO_OUT);
138 curout &= ~(1 << itv->card->xceive_pin);
139 write_reg(curout, IVTV_REG_GPIO_OUT);
140 schedule_timeout_interruptible(msecs_to_jiffies(1));
141
142 curout |= 1 << itv->card->xceive_pin;
143 write_reg(curout, IVTV_REG_GPIO_OUT);
144 schedule_timeout_interruptible(msecs_to_jiffies(1));
145 return 0;
146}
147
148static inline struct ivtv *sd_to_ivtv(struct v4l2_subdev *sd)
149{
150 return container_of(sd, struct ivtv, sd_gpio);
151}
152
153static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
154{
155 return &container_of(ctrl->handler, struct ivtv, hdl_gpio)->sd_gpio;
156}
157
158static int subdev_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
159{
160 struct ivtv *itv = sd_to_ivtv(sd);
161 u16 mask, data;
162
163 mask = itv->card->gpio_audio_freq.mask;
164 switch (freq) {
165 case 32000:
166 data = itv->card->gpio_audio_freq.f32000;
167 break;
168 case 44100:
169 data = itv->card->gpio_audio_freq.f44100;
170 break;
171 case 48000:
172 default:
173 data = itv->card->gpio_audio_freq.f48000;
174 break;
175 }
176 if (mask)
177 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
178 return 0;
179}
180
181static int subdev_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
182{
183 struct ivtv *itv = sd_to_ivtv(sd);
184 u16 mask;
185
186 mask = itv->card->gpio_audio_detect.mask;
187 if (mask == 0 || (read_reg(IVTV_REG_GPIO_IN) & mask))
188 vt->rxsubchans = V4L2_TUNER_SUB_STEREO |
189 V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
190 else
191 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
192 return 0;
193}
194
195static int subdev_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
196{
197 struct ivtv *itv = sd_to_ivtv(sd);
198 u16 mask, data;
199
200 mask = itv->card->gpio_audio_mode.mask;
201 switch (vt->audmode) {
202 case V4L2_TUNER_MODE_LANG1:
203 data = itv->card->gpio_audio_mode.lang1;
204 break;
205 case V4L2_TUNER_MODE_LANG2:
206 data = itv->card->gpio_audio_mode.lang2;
207 break;
208 case V4L2_TUNER_MODE_MONO:
209 data = itv->card->gpio_audio_mode.mono;
210 break;
211 case V4L2_TUNER_MODE_STEREO:
212 case V4L2_TUNER_MODE_LANG1_LANG2:
213 default:
214 data = itv->card->gpio_audio_mode.stereo;
215 break;
216 }
217 if (mask)
218 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
219 return 0;
220}
221
222static int subdev_s_radio(struct v4l2_subdev *sd)
223{
224 struct ivtv *itv = sd_to_ivtv(sd);
225 u16 mask, data;
226
227 mask = itv->card->gpio_audio_input.mask;
228 data = itv->card->gpio_audio_input.radio;
229 if (mask)
230 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
231 return 0;
232}
233
234static int subdev_s_audio_routing(struct v4l2_subdev *sd,
235 u32 input, u32 output, u32 config)
236{
237 struct ivtv *itv = sd_to_ivtv(sd);
238 u16 mask, data;
239
240 if (input > 2)
241 return -EINVAL;
242 mask = itv->card->gpio_audio_input.mask;
243 switch (input) {
244 case 0:
245 data = itv->card->gpio_audio_input.tuner;
246 break;
247 case 1:
248 data = itv->card->gpio_audio_input.linein;
249 break;
250 case 2:
251 default:
252 data = itv->card->gpio_audio_input.radio;
253 break;
254 }
255 if (mask)
256 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
257 return 0;
258}
259
260static int subdev_s_ctrl(struct v4l2_ctrl *ctrl)
261{
262 struct v4l2_subdev *sd = to_sd(ctrl);
263 struct ivtv *itv = sd_to_ivtv(sd);
264 u16 mask, data;
265
266 switch (ctrl->id) {
267 case V4L2_CID_AUDIO_MUTE:
268 mask = itv->card->gpio_audio_mute.mask;
269 data = ctrl->val ? itv->card->gpio_audio_mute.mute : 0;
270 if (mask)
271 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) |
272 (data & mask), IVTV_REG_GPIO_OUT);
273 return 0;
274 }
275 return -EINVAL;
276}
277
278
279static int subdev_log_status(struct v4l2_subdev *sd)
280{
281 struct ivtv *itv = sd_to_ivtv(sd);
282
283 IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
284 read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),
285 read_reg(IVTV_REG_GPIO_IN));
286 v4l2_ctrl_handler_log_status(&itv->hdl_gpio, sd->name);
287 return 0;
288}
289
290static int subdev_s_video_routing(struct v4l2_subdev *sd,
291 u32 input, u32 output, u32 config)
292{
293 struct ivtv *itv = sd_to_ivtv(sd);
294 u16 mask, data;
295
296 if (input > 2)
297 return -EINVAL;
298 mask = itv->card->gpio_video_input.mask;
299 if (input == 0)
300 data = itv->card->gpio_video_input.tuner;
301 else if (input == 1)
302 data = itv->card->gpio_video_input.composite;
303 else
304 data = itv->card->gpio_video_input.svideo;
305 if (mask)
306 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
307 return 0;
308}
309
310static const struct v4l2_ctrl_ops gpio_ctrl_ops = {
311 .s_ctrl = subdev_s_ctrl,
312};
313
314static const struct v4l2_subdev_core_ops subdev_core_ops = {
315 .log_status = subdev_log_status,
316};
317
318static const struct v4l2_subdev_tuner_ops subdev_tuner_ops = {
319 .s_radio = subdev_s_radio,
320 .g_tuner = subdev_g_tuner,
321 .s_tuner = subdev_s_tuner,
322};
323
324static const struct v4l2_subdev_audio_ops subdev_audio_ops = {
325 .s_clock_freq = subdev_s_clock_freq,
326 .s_routing = subdev_s_audio_routing,
327};
328
329static const struct v4l2_subdev_video_ops subdev_video_ops = {
330 .s_routing = subdev_s_video_routing,
331};
332
333static const struct v4l2_subdev_ops subdev_ops = {
334 .core = &subdev_core_ops,
335 .tuner = &subdev_tuner_ops,
336 .audio = &subdev_audio_ops,
337 .video = &subdev_video_ops,
338};
339
340int ivtv_gpio_init(struct ivtv *itv)
341{
342 u16 pin = 0;
343
344 if (itv->card->xceive_pin)
345 pin = 1 << itv->card->xceive_pin;
346
347 if ((itv->card->gpio_init.direction | pin) == 0)
348 return 0;
349
350 IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",
351 read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT));
352
353
354 write_reg(itv->card->gpio_init.initial_value | pin, IVTV_REG_GPIO_OUT);
355 write_reg(itv->card->gpio_init.direction | pin, IVTV_REG_GPIO_DIR);
356 v4l2_subdev_init(&itv->sd_gpio, &subdev_ops);
357 snprintf(itv->sd_gpio.name, sizeof(itv->sd_gpio.name), "%s-gpio", itv->v4l2_dev.name);
358 itv->sd_gpio.grp_id = IVTV_HW_GPIO;
359 v4l2_ctrl_handler_init(&itv->hdl_gpio, 1);
360 v4l2_ctrl_new_std(&itv->hdl_gpio, &gpio_ctrl_ops,
361 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
362 if (itv->hdl_gpio.error)
363 return itv->hdl_gpio.error;
364 itv->sd_gpio.ctrl_handler = &itv->hdl_gpio;
365 v4l2_ctrl_handler_setup(&itv->hdl_gpio);
366 return v4l2_device_register_subdev(&itv->v4l2_dev, &itv->sd_gpio);
367}
368