1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "cx18-driver.h"
26#include "cx18-io.h"
27#include "cx18-cards.h"
28#include "cx18-gpio.h"
29#include "tuner-xc2028.h"
30
31
32
33
34#define CX18_REG_GPIO_IN 0xc72010
35#define CX18_REG_GPIO_OUT1 0xc78100
36#define CX18_REG_GPIO_DIR1 0xc78108
37#define CX18_REG_GPIO_OUT2 0xc78104
38#define CX18_REG_GPIO_DIR2 0xc7810c
39
40
41
42
43
44
45
46
47
48
49
50
51
52static void gpio_write(struct cx18 *cx)
53{
54 u32 dir_lo = cx->gpio_dir & 0xffff;
55 u32 val_lo = cx->gpio_val & 0xffff;
56 u32 dir_hi = cx->gpio_dir >> 16;
57 u32 val_hi = cx->gpio_val >> 16;
58
59 cx18_write_reg_expect(cx, dir_lo << 16,
60 CX18_REG_GPIO_DIR1, ~dir_lo, dir_lo);
61 cx18_write_reg_expect(cx, (dir_lo << 16) | val_lo,
62 CX18_REG_GPIO_OUT1, val_lo, dir_lo);
63 cx18_write_reg_expect(cx, dir_hi << 16,
64 CX18_REG_GPIO_DIR2, ~dir_hi, dir_hi);
65 cx18_write_reg_expect(cx, (dir_hi << 16) | val_hi,
66 CX18_REG_GPIO_OUT2, val_hi, dir_hi);
67}
68
69static void gpio_update(struct cx18 *cx, u32 mask, u32 data)
70{
71 if (mask == 0)
72 return;
73
74 mutex_lock(&cx->gpio_lock);
75 cx->gpio_val = (cx->gpio_val & ~mask) | (data & mask);
76 gpio_write(cx);
77 mutex_unlock(&cx->gpio_lock);
78}
79
80static void gpio_reset_seq(struct cx18 *cx, u32 active_lo, u32 active_hi,
81 unsigned int assert_msecs,
82 unsigned int recovery_msecs)
83{
84 u32 mask;
85
86 mask = active_lo | active_hi;
87 if (mask == 0)
88 return;
89
90
91
92
93
94
95
96
97 gpio_update(cx, mask, ~active_lo);
98 schedule_timeout_uninterruptible(msecs_to_jiffies(assert_msecs));
99
100
101 gpio_update(cx, mask, ~active_hi);
102 schedule_timeout_uninterruptible(msecs_to_jiffies(recovery_msecs));
103}
104
105
106
107
108static int gpiomux_log_status(struct v4l2_subdev *sd)
109{
110 struct cx18 *cx = v4l2_get_subdevdata(sd);
111
112 mutex_lock(&cx->gpio_lock);
113 CX18_INFO_DEV(sd, "GPIO: direction 0x%08x, value 0x%08x\n",
114 cx->gpio_dir, cx->gpio_val);
115 mutex_unlock(&cx->gpio_lock);
116 return 0;
117}
118
119static int gpiomux_s_radio(struct v4l2_subdev *sd)
120{
121 struct cx18 *cx = v4l2_get_subdevdata(sd);
122
123
124
125
126
127
128 gpio_update(cx, cx->card->gpio_audio_input.mask,
129 cx->card->gpio_audio_input.radio);
130 return 0;
131}
132
133static int gpiomux_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
134{
135 struct cx18 *cx = v4l2_get_subdevdata(sd);
136 u32 data;
137
138 switch (cx->card->audio_inputs[cx->audio_input].muxer_input) {
139 case 1:
140 data = cx->card->gpio_audio_input.linein;
141 break;
142 case 0:
143 data = cx->card->gpio_audio_input.tuner;
144 break;
145 default:
146
147
148
149
150
151 data = cx->card->gpio_audio_input.tuner;
152 break;
153 }
154 gpio_update(cx, cx->card->gpio_audio_input.mask, data);
155 return 0;
156}
157
158static int gpiomux_s_audio_routing(struct v4l2_subdev *sd,
159 u32 input, u32 output, u32 config)
160{
161 struct cx18 *cx = v4l2_get_subdevdata(sd);
162 u32 data;
163
164 switch (input) {
165 case 0:
166 data = cx->card->gpio_audio_input.tuner;
167 break;
168 case 1:
169 data = cx->card->gpio_audio_input.linein;
170 break;
171 case 2:
172 data = cx->card->gpio_audio_input.radio;
173 break;
174 default:
175 return -EINVAL;
176 }
177 gpio_update(cx, cx->card->gpio_audio_input.mask, data);
178 return 0;
179}
180
181static const struct v4l2_subdev_core_ops gpiomux_core_ops = {
182 .log_status = gpiomux_log_status,
183 .s_std = gpiomux_s_std,
184};
185
186static const struct v4l2_subdev_tuner_ops gpiomux_tuner_ops = {
187 .s_radio = gpiomux_s_radio,
188};
189
190static const struct v4l2_subdev_audio_ops gpiomux_audio_ops = {
191 .s_routing = gpiomux_s_audio_routing,
192};
193
194static const struct v4l2_subdev_ops gpiomux_ops = {
195 .core = &gpiomux_core_ops,
196 .tuner = &gpiomux_tuner_ops,
197 .audio = &gpiomux_audio_ops,
198};
199
200
201
202
203static int resetctrl_log_status(struct v4l2_subdev *sd)
204{
205 struct cx18 *cx = v4l2_get_subdevdata(sd);
206
207 mutex_lock(&cx->gpio_lock);
208 CX18_INFO_DEV(sd, "GPIO: direction 0x%08x, value 0x%08x\n",
209 cx->gpio_dir, cx->gpio_val);
210 mutex_unlock(&cx->gpio_lock);
211 return 0;
212}
213
214static int resetctrl_reset(struct v4l2_subdev *sd, u32 val)
215{
216 struct cx18 *cx = v4l2_get_subdevdata(sd);
217 const struct cx18_gpio_i2c_slave_reset *p;
218
219 p = &cx->card->gpio_i2c_slave_reset;
220 switch (val) {
221 case CX18_GPIO_RESET_I2C:
222 gpio_reset_seq(cx, p->active_lo_mask, p->active_hi_mask,
223 p->msecs_asserted, p->msecs_recovery);
224 break;
225 case CX18_GPIO_RESET_Z8F0811:
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240 gpio_reset_seq(cx, p->ir_reset_mask, 0,
241 p->msecs_asserted, p->msecs_recovery);
242 break;
243 case CX18_GPIO_RESET_XC2028:
244 if (cx->card->tuners[0].tuner == TUNER_XC2028)
245 gpio_reset_seq(cx, (1 << cx->card->xceive_pin), 0,
246 1, 1);
247 break;
248 }
249 return 0;
250}
251
252static const struct v4l2_subdev_core_ops resetctrl_core_ops = {
253 .log_status = resetctrl_log_status,
254 .reset = resetctrl_reset,
255};
256
257static const struct v4l2_subdev_ops resetctrl_ops = {
258 .core = &resetctrl_core_ops,
259};
260
261
262
263
264void cx18_gpio_init(struct cx18 *cx)
265{
266 mutex_lock(&cx->gpio_lock);
267 cx->gpio_dir = cx->card->gpio_init.direction;
268 cx->gpio_val = cx->card->gpio_init.initial_value;
269
270 if (cx->card->tuners[0].tuner == TUNER_XC2028) {
271 cx->gpio_dir |= 1 << cx->card->xceive_pin;
272 cx->gpio_val |= 1 << cx->card->xceive_pin;
273 }
274
275 if (cx->gpio_dir == 0) {
276 mutex_unlock(&cx->gpio_lock);
277 return;
278 }
279
280 CX18_DEBUG_INFO("GPIO initial dir: %08x/%08x out: %08x/%08x\n",
281 cx18_read_reg(cx, CX18_REG_GPIO_DIR1),
282 cx18_read_reg(cx, CX18_REG_GPIO_DIR2),
283 cx18_read_reg(cx, CX18_REG_GPIO_OUT1),
284 cx18_read_reg(cx, CX18_REG_GPIO_OUT2));
285
286 gpio_write(cx);
287 mutex_unlock(&cx->gpio_lock);
288}
289
290int cx18_gpio_register(struct cx18 *cx, u32 hw)
291{
292 struct v4l2_subdev *sd;
293 const struct v4l2_subdev_ops *ops;
294 char *str;
295
296 switch (hw) {
297 case CX18_HW_GPIO_MUX:
298 sd = &cx->sd_gpiomux;
299 ops = &gpiomux_ops;
300 str = "gpio-mux";
301 break;
302 case CX18_HW_GPIO_RESET_CTRL:
303 sd = &cx->sd_resetctrl;
304 ops = &resetctrl_ops;
305 str = "gpio-reset-ctrl";
306 break;
307 default:
308 return -EINVAL;
309 }
310
311 v4l2_subdev_init(sd, ops);
312 v4l2_set_subdevdata(sd, cx);
313 snprintf(sd->name, sizeof(sd->name), "%s %s", cx->v4l2_dev.name, str);
314 sd->grp_id = hw;
315 return v4l2_device_register_subdev(&cx->v4l2_dev, sd);
316}
317
318void cx18_reset_ir_gpio(void *data)
319{
320 struct cx18 *cx = to_cx18((struct v4l2_device *)data);
321
322 if (cx->card->gpio_i2c_slave_reset.ir_reset_mask == 0)
323 return;
324
325 CX18_DEBUG_INFO("Resetting IR microcontroller\n");
326
327 v4l2_subdev_call(&cx->sd_resetctrl,
328 core, reset, CX18_GPIO_RESET_Z8F0811);
329}
330EXPORT_SYMBOL(cx18_reset_ir_gpio);
331
332
333
334int cx18_reset_tuner_gpio(void *dev, int component, int cmd, int value)
335{
336 struct i2c_algo_bit_data *algo = dev;
337 struct cx18_i2c_algo_callback_data *cb_data = algo->data;
338 struct cx18 *cx = cb_data->cx;
339
340 if (cmd != XC2028_TUNER_RESET ||
341 cx->card->tuners[0].tuner != TUNER_XC2028)
342 return 0;
343
344 CX18_DEBUG_INFO("Resetting XCeive tuner\n");
345 return v4l2_subdev_call(&cx->sd_resetctrl,
346 core, reset, CX18_GPIO_RESET_XC2028);
347}
348