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