staging: delete non-required instances of include <linux/init.h>
[deliverable/linux.git] / drivers / staging / media / go7007 / s2250-board.c
1 /*
2 * Copyright (C) 2008 Sensoray Company Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16 */
17
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/i2c.h>
21 #include <linux/videodev2.h>
22 #include <linux/slab.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-subdev.h>
26 #include "go7007-priv.h"
27
28 MODULE_DESCRIPTION("Sensoray 2250/2251 i2c v4l2 subdev driver");
29 MODULE_LICENSE("GPL v2");
30
31 /*
32 * Note: this board has two i2c devices: a vpx3226f and a tlv320aic23b.
33 * Due to the unusual way these are accessed on this device we do not
34 * reuse the i2c drivers, but instead they are implemented in this
35 * driver. It would be nice to improve on this, though.
36 */
37
38 #define TLV320_ADDRESS 0x34
39 #define VPX322_ADDR_ANALOGCONTROL1 0x02
40 #define VPX322_ADDR_BRIGHTNESS0 0x0127
41 #define VPX322_ADDR_BRIGHTNESS1 0x0131
42 #define VPX322_ADDR_CONTRAST0 0x0128
43 #define VPX322_ADDR_CONTRAST1 0x0132
44 #define VPX322_ADDR_HUE 0x00dc
45 #define VPX322_ADDR_SAT 0x0030
46
47 struct go7007_usb_board {
48 unsigned int flags;
49 struct go7007_board_info main_info;
50 };
51
52 struct go7007_usb {
53 struct go7007_usb_board *board;
54 struct mutex i2c_lock;
55 struct usb_device *usbdev;
56 struct urb *video_urbs[8];
57 struct urb *audio_urbs[8];
58 struct urb *intr_urb;
59 };
60
61 static unsigned char aud_regs[] = {
62 0x1e, 0x00,
63 0x00, 0x17,
64 0x02, 0x17,
65 0x04, 0xf9,
66 0x06, 0xf9,
67 0x08, 0x02,
68 0x0a, 0x00,
69 0x0c, 0x00,
70 0x0a, 0x00,
71 0x0c, 0x00,
72 0x0e, 0x02,
73 0x10, 0x00,
74 0x12, 0x01,
75 0x00, 0x00,
76 };
77
78
79 static unsigned char vid_regs[] = {
80 0xF2, 0x0f,
81 0xAA, 0x00,
82 0xF8, 0xff,
83 0x00, 0x00,
84 };
85
86 static u16 vid_regs_fp[] = {
87 0x028, 0x067,
88 0x120, 0x016,
89 0x121, 0xcF2,
90 0x122, 0x0F2,
91 0x123, 0x00c,
92 0x124, 0x2d0,
93 0x125, 0x2e0,
94 0x126, 0x004,
95 0x128, 0x1E0,
96 0x12A, 0x016,
97 0x12B, 0x0F2,
98 0x12C, 0x0F2,
99 0x12D, 0x00c,
100 0x12E, 0x2d0,
101 0x12F, 0x2e0,
102 0x130, 0x004,
103 0x132, 0x1E0,
104 0x140, 0x060,
105 0x153, 0x00C,
106 0x154, 0x200,
107 0x150, 0x801,
108 0x000, 0x000
109 };
110
111 /* PAL specific values */
112 static u16 vid_regs_fp_pal[] = {
113 0x120, 0x017,
114 0x121, 0xd22,
115 0x122, 0x122,
116 0x12A, 0x017,
117 0x12B, 0x122,
118 0x12C, 0x122,
119 0x140, 0x060,
120 0x000, 0x000,
121 };
122
123 struct s2250 {
124 struct v4l2_subdev sd;
125 struct v4l2_ctrl_handler hdl;
126 v4l2_std_id std;
127 int input;
128 int brightness;
129 int contrast;
130 int saturation;
131 int hue;
132 int reg12b_val;
133 int audio_input;
134 struct i2c_client *audio;
135 };
136
137 static inline struct s2250 *to_state(struct v4l2_subdev *sd)
138 {
139 return container_of(sd, struct s2250, sd);
140 }
141
142 /* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
143 static int go7007_usb_vendor_request(struct go7007 *go, u16 request,
144 u16 value, u16 index, void *transfer_buffer, int length, int in)
145 {
146 struct go7007_usb *usb = go->hpi_context;
147 int timeout = 5000;
148
149 if (in) {
150 return usb_control_msg(usb->usbdev,
151 usb_rcvctrlpipe(usb->usbdev, 0), request,
152 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
153 value, index, transfer_buffer, length, timeout);
154 } else {
155 return usb_control_msg(usb->usbdev,
156 usb_sndctrlpipe(usb->usbdev, 0), request,
157 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
158 value, index, transfer_buffer, length, timeout);
159 }
160 }
161 /* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
162
163 static int write_reg(struct i2c_client *client, u8 reg, u8 value)
164 {
165 struct go7007 *go = i2c_get_adapdata(client->adapter);
166 struct go7007_usb *usb;
167 int rc;
168 int dev_addr = client->addr << 1; /* firmware wants 8-bit address */
169 u8 *buf;
170
171 if (go == NULL)
172 return -ENODEV;
173
174 if (go->status == STATUS_SHUTDOWN)
175 return -EBUSY;
176
177 buf = kzalloc(16, GFP_KERNEL);
178 if (buf == NULL)
179 return -ENOMEM;
180
181 usb = go->hpi_context;
182 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
183 dev_info(&client->dev, "i2c lock failed\n");
184 kfree(buf);
185 return -EINTR;
186 }
187 rc = go7007_usb_vendor_request(go, 0x55, dev_addr,
188 (reg<<8 | value),
189 buf,
190 16, 1);
191
192 mutex_unlock(&usb->i2c_lock);
193 kfree(buf);
194 return rc;
195 }
196
197 static int write_reg_fp(struct i2c_client *client, u16 addr, u16 val)
198 {
199 struct go7007 *go = i2c_get_adapdata(client->adapter);
200 struct go7007_usb *usb;
201 int rc;
202 u8 *buf;
203 struct s2250 *dec = i2c_get_clientdata(client);
204
205 if (go == NULL)
206 return -ENODEV;
207
208 if (go->status == STATUS_SHUTDOWN)
209 return -EBUSY;
210
211 buf = kzalloc(16, GFP_KERNEL);
212
213 if (buf == NULL)
214 return -ENOMEM;
215
216
217
218 memset(buf, 0xcd, 6);
219
220 usb = go->hpi_context;
221 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
222 dev_info(&client->dev, "i2c lock failed\n");
223 kfree(buf);
224 return -EINTR;
225 }
226 rc = go7007_usb_vendor_request(go, 0x57, addr, val, buf, 16, 1);
227 mutex_unlock(&usb->i2c_lock);
228 if (rc < 0) {
229 kfree(buf);
230 return rc;
231 }
232
233 if (buf[0] == 0) {
234 unsigned int subaddr, val_read;
235
236 subaddr = (buf[4] << 8) + buf[5];
237 val_read = (buf[2] << 8) + buf[3];
238 kfree(buf);
239 if (val_read != val) {
240 dev_info(&client->dev, "invalid fp write %x %x\n",
241 val_read, val);
242 return -EFAULT;
243 }
244 if (subaddr != addr) {
245 dev_info(&client->dev, "invalid fp write addr %x %x\n",
246 subaddr, addr);
247 return -EFAULT;
248 }
249 } else {
250 kfree(buf);
251 return -EFAULT;
252 }
253
254 /* save last 12b value */
255 if (addr == 0x12b)
256 dec->reg12b_val = val;
257
258 return 0;
259 }
260
261 static int read_reg_fp(struct i2c_client *client, u16 addr, u16 *val)
262 {
263 struct go7007 *go = i2c_get_adapdata(client->adapter);
264 struct go7007_usb *usb;
265 int rc;
266 u8 *buf;
267
268 if (go == NULL)
269 return -ENODEV;
270
271 if (go->status == STATUS_SHUTDOWN)
272 return -EBUSY;
273
274 buf = kzalloc(16, GFP_KERNEL);
275
276 if (buf == NULL)
277 return -ENOMEM;
278
279
280
281 memset(buf, 0xcd, 6);
282 usb = go->hpi_context;
283 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
284 dev_info(&client->dev, "i2c lock failed\n");
285 kfree(buf);
286 return -EINTR;
287 }
288 rc = go7007_usb_vendor_request(go, 0x58, addr, 0, buf, 16, 1);
289 mutex_unlock(&usb->i2c_lock);
290 if (rc < 0) {
291 kfree(buf);
292 return rc;
293 }
294
295 *val = (buf[0] << 8) | buf[1];
296 kfree(buf);
297
298 return 0;
299 }
300
301
302 static int write_regs(struct i2c_client *client, u8 *regs)
303 {
304 int i;
305
306 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
307 if (write_reg(client, regs[i], regs[i+1]) < 0) {
308 dev_info(&client->dev, "failed\n");
309 return -1;
310 }
311 }
312 return 0;
313 }
314
315 static int write_regs_fp(struct i2c_client *client, u16 *regs)
316 {
317 int i;
318
319 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
320 if (write_reg_fp(client, regs[i], regs[i+1]) < 0) {
321 dev_info(&client->dev, "failed fp\n");
322 return -1;
323 }
324 }
325 return 0;
326 }
327
328
329 /* ------------------------------------------------------------------------- */
330
331 static int s2250_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
332 u32 config)
333 {
334 struct s2250 *state = to_state(sd);
335 struct i2c_client *client = v4l2_get_subdevdata(sd);
336 int vidsys;
337
338 vidsys = (state->std == V4L2_STD_NTSC) ? 0x01 : 0x00;
339 if (input == 0) {
340 /* composite */
341 write_reg_fp(client, 0x20, 0x020 | vidsys);
342 write_reg_fp(client, 0x21, 0x662);
343 write_reg_fp(client, 0x140, 0x060);
344 } else if (input == 1) {
345 /* S-Video */
346 write_reg_fp(client, 0x20, 0x040 | vidsys);
347 write_reg_fp(client, 0x21, 0x666);
348 write_reg_fp(client, 0x140, 0x060);
349 } else {
350 return -EINVAL;
351 }
352 state->input = input;
353 return 0;
354 }
355
356 static int s2250_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
357 {
358 struct s2250 *state = to_state(sd);
359 struct i2c_client *client = v4l2_get_subdevdata(sd);
360 u16 vidsource;
361
362 vidsource = (state->input == 1) ? 0x040 : 0x020;
363 if (norm & V4L2_STD_625_50) {
364 write_regs_fp(client, vid_regs_fp);
365 write_regs_fp(client, vid_regs_fp_pal);
366 write_reg_fp(client, 0x20, vidsource);
367 } else {
368 write_regs_fp(client, vid_regs_fp);
369 write_reg_fp(client, 0x20, vidsource | 1);
370 }
371 state->std = norm;
372 return 0;
373 }
374
375 static int s2250_s_ctrl(struct v4l2_ctrl *ctrl)
376 {
377 struct s2250 *state = container_of(ctrl->handler, struct s2250, hdl);
378 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
379 u16 oldvalue;
380
381 switch (ctrl->id) {
382 case V4L2_CID_BRIGHTNESS:
383 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS0, &oldvalue);
384 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS0,
385 ctrl->val | (oldvalue & ~0xff));
386 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS1, &oldvalue);
387 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS1,
388 ctrl->val | (oldvalue & ~0xff));
389 write_reg_fp(client, 0x140, 0x60);
390 break;
391 case V4L2_CID_CONTRAST:
392 read_reg_fp(client, VPX322_ADDR_CONTRAST0, &oldvalue);
393 write_reg_fp(client, VPX322_ADDR_CONTRAST0,
394 ctrl->val | (oldvalue & ~0x3f));
395 read_reg_fp(client, VPX322_ADDR_CONTRAST1, &oldvalue);
396 write_reg_fp(client, VPX322_ADDR_CONTRAST1,
397 ctrl->val | (oldvalue & ~0x3f));
398 write_reg_fp(client, 0x140, 0x60);
399 break;
400 case V4L2_CID_SATURATION:
401 write_reg_fp(client, VPX322_ADDR_SAT, ctrl->val);
402 break;
403 case V4L2_CID_HUE:
404 write_reg_fp(client, VPX322_ADDR_HUE, ctrl->val);
405 break;
406 default:
407 return -EINVAL;
408 }
409 return 0;
410 }
411
412 static int s2250_s_mbus_fmt(struct v4l2_subdev *sd,
413 struct v4l2_mbus_framefmt *fmt)
414 {
415 struct s2250 *state = to_state(sd);
416 struct i2c_client *client = v4l2_get_subdevdata(sd);
417
418 if (fmt->height < 640) {
419 write_reg_fp(client, 0x12b, state->reg12b_val | 0x400);
420 write_reg_fp(client, 0x140, 0x060);
421 } else {
422 write_reg_fp(client, 0x12b, state->reg12b_val & ~0x400);
423 write_reg_fp(client, 0x140, 0x060);
424 }
425 return 0;
426 }
427
428 static int s2250_s_audio_routing(struct v4l2_subdev *sd, u32 input, u32 output,
429 u32 config)
430 {
431 struct s2250 *state = to_state(sd);
432
433 switch (input) {
434 case 0:
435 write_reg(state->audio, 0x08, 0x02); /* Line In */
436 break;
437 case 1:
438 write_reg(state->audio, 0x08, 0x04); /* Mic */
439 break;
440 case 2:
441 write_reg(state->audio, 0x08, 0x05); /* Mic Boost */
442 break;
443 default:
444 return -EINVAL;
445 }
446 state->audio_input = input;
447 return 0;
448 }
449
450
451 static int s2250_log_status(struct v4l2_subdev *sd)
452 {
453 struct s2250 *state = to_state(sd);
454
455 v4l2_info(sd, "Standard: %s\n", state->std == V4L2_STD_NTSC ? "NTSC" :
456 state->std == V4L2_STD_PAL ? "PAL" :
457 state->std == V4L2_STD_SECAM ? "SECAM" :
458 "unknown");
459 v4l2_info(sd, "Input: %s\n", state->input == 0 ? "Composite" :
460 state->input == 1 ? "S-video" :
461 "error");
462 v4l2_info(sd, "Audio input: %s\n", state->audio_input == 0 ? "Line In" :
463 state->audio_input == 1 ? "Mic" :
464 state->audio_input == 2 ? "Mic Boost" :
465 "error");
466 return v4l2_ctrl_subdev_log_status(sd);
467 }
468
469 /* --------------------------------------------------------------------------*/
470
471 static const struct v4l2_ctrl_ops s2250_ctrl_ops = {
472 .s_ctrl = s2250_s_ctrl,
473 };
474
475 static const struct v4l2_subdev_core_ops s2250_core_ops = {
476 .log_status = s2250_log_status,
477 .s_std = s2250_s_std,
478 };
479
480 static const struct v4l2_subdev_audio_ops s2250_audio_ops = {
481 .s_routing = s2250_s_audio_routing,
482 };
483
484 static const struct v4l2_subdev_video_ops s2250_video_ops = {
485 .s_routing = s2250_s_video_routing,
486 .s_mbus_fmt = s2250_s_mbus_fmt,
487 };
488
489 static const struct v4l2_subdev_ops s2250_ops = {
490 .core = &s2250_core_ops,
491 .audio = &s2250_audio_ops,
492 .video = &s2250_video_ops,
493 };
494
495 /* --------------------------------------------------------------------------*/
496
497 static int s2250_probe(struct i2c_client *client,
498 const struct i2c_device_id *id)
499 {
500 struct i2c_client *audio;
501 struct i2c_adapter *adapter = client->adapter;
502 struct s2250 *state;
503 struct v4l2_subdev *sd;
504 u8 *data;
505 struct go7007 *go = i2c_get_adapdata(adapter);
506 struct go7007_usb *usb = go->hpi_context;
507
508 audio = i2c_new_dummy(adapter, TLV320_ADDRESS >> 1);
509 if (audio == NULL)
510 return -ENOMEM;
511
512 state = kzalloc(sizeof(struct s2250), GFP_KERNEL);
513 if (state == NULL) {
514 i2c_unregister_device(audio);
515 return -ENOMEM;
516 }
517
518 sd = &state->sd;
519 v4l2_i2c_subdev_init(sd, client, &s2250_ops);
520
521 v4l2_info(sd, "initializing %s at address 0x%x on %s\n",
522 "Sensoray 2250/2251", client->addr, client->adapter->name);
523
524 v4l2_ctrl_handler_init(&state->hdl, 4);
525 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
526 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
527 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
528 V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x32);
529 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
530 V4L2_CID_SATURATION, 0, 4094, 1, 2070);
531 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
532 V4L2_CID_HUE, -512, 511, 1, 0);
533 sd->ctrl_handler = &state->hdl;
534 if (state->hdl.error) {
535 int err = state->hdl.error;
536
537 v4l2_ctrl_handler_free(&state->hdl);
538 kfree(state);
539 return err;
540 }
541
542 state->std = V4L2_STD_NTSC;
543 state->brightness = 50;
544 state->contrast = 50;
545 state->saturation = 50;
546 state->hue = 0;
547 state->audio = audio;
548
549 /* initialize the audio */
550 if (write_regs(audio, aud_regs) < 0) {
551 dev_err(&client->dev, "error initializing audio\n");
552 goto fail;
553 }
554
555 if (write_regs(client, vid_regs) < 0) {
556 dev_err(&client->dev, "error initializing decoder\n");
557 goto fail;
558 }
559 if (write_regs_fp(client, vid_regs_fp) < 0) {
560 dev_err(&client->dev, "error initializing decoder\n");
561 goto fail;
562 }
563 /* set default channel */
564 /* composite */
565 write_reg_fp(client, 0x20, 0x020 | 1);
566 write_reg_fp(client, 0x21, 0x662);
567 write_reg_fp(client, 0x140, 0x060);
568
569 /* set default audio input */
570 state->audio_input = 0;
571 write_reg(client, 0x08, 0x02); /* Line In */
572
573 if (mutex_lock_interruptible(&usb->i2c_lock) == 0) {
574 data = kzalloc(16, GFP_KERNEL);
575 if (data != NULL) {
576 int rc;
577 rc = go7007_usb_vendor_request(go, 0x41, 0, 0,
578 data, 16, 1);
579 if (rc > 0) {
580 u8 mask;
581 data[0] = 0;
582 mask = 1<<5;
583 data[0] &= ~mask;
584 data[1] |= mask;
585 go7007_usb_vendor_request(go, 0x40, 0,
586 (data[1]<<8)
587 + data[1],
588 data, 16, 0);
589 }
590 kfree(data);
591 }
592 mutex_unlock(&usb->i2c_lock);
593 }
594
595 v4l2_info(sd, "initialized successfully\n");
596 return 0;
597
598 fail:
599 i2c_unregister_device(audio);
600 v4l2_ctrl_handler_free(&state->hdl);
601 kfree(state);
602 return -EIO;
603 }
604
605 static int s2250_remove(struct i2c_client *client)
606 {
607 struct s2250 *state = to_state(i2c_get_clientdata(client));
608
609 v4l2_device_unregister_subdev(&state->sd);
610 v4l2_ctrl_handler_free(&state->hdl);
611 kfree(state);
612 return 0;
613 }
614
615 static const struct i2c_device_id s2250_id[] = {
616 { "s2250", 0 },
617 { }
618 };
619 MODULE_DEVICE_TABLE(i2c, s2250_id);
620
621 static struct i2c_driver s2250_driver = {
622 .driver = {
623 .owner = THIS_MODULE,
624 .name = "s2250",
625 },
626 .probe = s2250_probe,
627 .remove = s2250_remove,
628 .id_table = s2250_id,
629 };
630
631 module_i2c_driver(s2250_driver);
This page took 0.078771 seconds and 5 git commands to generate.