V4L/DVB (7598): em28xx: several fixes on gpio programming
[safe/jmp/linux-2.6] / drivers / media / video / em28xx / em28xx-dvb.c
1 /*
2  DVB device driver for em28xx
3
4  (c) 2008 Mauro Carvalho Chehab <mchehab@infradead.org>
5
6  (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7         - Fixes for the driver to properly work with HVR-950
8
9  Based on cx88-dvb and saa7134-dvb originally written by:
10         (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
11         (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
12
13  This program is free software; you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation; either version 2 of the License.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/usb.h>
20
21 #include "em28xx.h"
22 #include <media/v4l2-common.h>
23 #include <media/videobuf-vmalloc.h>
24
25 #include "lgdt330x.h"
26
27 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
28 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
29 MODULE_LICENSE("GPL");
30
31 static unsigned int debug;
32 module_param(debug, int, 0644);
33 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
34
35 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
36
37 #define dprintk(level, fmt, arg...) do {                        \
38 if (debug >= level)                                             \
39         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg)  \
40 } while (0)
41
42 static int
43 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
44 {
45         struct em28xx_fh *fh = vq->priv_data;
46         struct em28xx        *dev = fh->dev;
47
48         *size = 16 * fh->dev->width * fh->dev->height >> 3;
49         if (0 == *count)
50                 *count = EM28XX_DEF_BUF;
51
52         if (*count < EM28XX_MIN_BUF)
53                 *count = EM28XX_MIN_BUF;
54
55         dev->mode = EM28XX_DIGITAL_MODE;
56
57         return 0;
58 }
59
60 /* ------------------------------------------------------------------ */
61
62 static struct lgdt330x_config em2880_lgdt3303_dev = {
63         .demod_address = 0x0e,
64         .demod_chip = LGDT3303,
65 };
66
67 /* ------------------------------------------------------------------ */
68
69 static int attach_xc3028(u8 addr, struct em28xx *dev)
70 {
71         struct dvb_frontend *fe;
72         struct xc2028_ctrl ctl;
73         struct xc2028_config cfg;
74
75         memset (&cfg, 0, sizeof(cfg));
76         cfg.i2c_adap  = &dev->i2c_adap;
77         cfg.i2c_addr  = addr;
78         cfg.ctrl      = &ctl;
79         cfg.callback  = em28xx_tuner_callback;
80
81         em28xx_setup_xc3028(dev, &ctl);
82
83         if (!dev->dvb.frontend) {
84                 printk(KERN_ERR "%s/2: dvb frontend not attached. "
85                                 "Can't attach xc3028\n",
86                        dev->name);
87                 return -EINVAL;
88         }
89
90         fe = dvb_attach(xc2028_attach, dev->dvb.frontend, &cfg);
91         if (!fe) {
92                 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
93                        dev->name);
94                 dvb_frontend_detach(dev->dvb.frontend);
95                 dvb_unregister_frontend(dev->dvb.frontend);
96                 dev->dvb.frontend = NULL;
97                 return -EINVAL;
98         }
99
100         printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
101
102         return 0;
103 }
104
105 static int dvb_init(struct em28xx *dev)
106 {
107         /* init struct videobuf_dvb */
108         dev->dvb.name = dev->name;
109
110         dev->qops->buf_setup = buffer_setup;
111
112         videobuf_queue_vmalloc_init(&dev->dvb.dvbq, dev->qops,
113                         &dev->udev->dev, &dev->slock,
114                         V4L2_BUF_TYPE_VIDEO_CAPTURE,
115                         V4L2_FIELD_ALTERNATE,
116                         sizeof(struct em28xx_buffer), dev);
117
118         /* init frontend */
119         switch (dev->model) {
120         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
121                 /* Enable lgdt330x */
122                 dev->mode = EM28XX_DIGITAL_MODE;
123                 em28xx_tuner_callback(dev, XC2028_TUNER_RESET, 0);
124
125                 dev->dvb.frontend = dvb_attach(lgdt330x_attach,
126                                                &em2880_lgdt3303_dev,
127                                                &dev->i2c_adap);
128                 if (attach_xc3028(0x61, dev) < 0)
129                         return -EINVAL;
130                 break;
131         default:
132                 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
133                                 " isn't supported yet\n",
134                        dev->name);
135                 break;
136         }
137         if (NULL == dev->dvb.frontend) {
138                 printk(KERN_ERR
139                        "%s/2: frontend initialization failed\n",
140                        dev->name);
141                 return -EINVAL;
142         }
143
144         /* register everything */
145         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev,
146                                      &dev->udev->dev,
147                                      adapter_nr);
148 }
149
150 static int dvb_fini(struct em28xx *dev)
151 {
152         if (dev->dvb.frontend)
153                 videobuf_dvb_unregister(&dev->dvb);
154
155         return 0;
156 }
157
158 static struct em28xx_ops dvb_ops = {
159         .id   = EM28XX_DVB,
160         .name = "Em28xx dvb Extension",
161         .init = dvb_init,
162         .fini = dvb_fini,
163 };
164
165 static int __init em28xx_dvb_register(void)
166 {
167         return em28xx_register_extension(&dvb_ops);
168 }
169
170 static void __exit em28xx_dvb_unregister(void)
171 {
172         em28xx_unregister_extension(&dvb_ops);
173 }
174
175 module_init(em28xx_dvb_register);
176 module_exit(em28xx_dvb_unregister);