GNU Linux-libre 4.9-gnu1
[releases.git] / drivers / media / usb / dvb-usb / dtv5100.c
1 /*
2  * DVB USB Linux driver for AME DTV-5100 USB2.0 DVB-T
3  *
4  * Copyright (C) 2008  Antoine Jacquet <royale@zerezo.com>
5  * http://royale.zerezo.com/dtv5100/
6  *
7  * Inspired by gl861.c and au6610.c drivers
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "dtv5100.h"
25 #include "zl10353.h"
26 #include "qt1010.h"
27
28 /* debug */
29 static int dvb_usb_dtv5100_debug;
30 module_param_named(debug, dvb_usb_dtv5100_debug, int, 0644);
31 MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
32 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
33
34 struct dtv5100_state {
35         unsigned char data[80];
36 };
37
38 static int dtv5100_i2c_msg(struct dvb_usb_device *d, u8 addr,
39                            u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
40 {
41         struct dtv5100_state *st = d->priv;
42         u8 request;
43         u8 type;
44         u16 value;
45         u16 index;
46
47         switch (wlen) {
48         case 1:
49                 /* write { reg }, read { value } */
50                 request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_READ :
51                                                         DTV5100_TUNER_READ);
52                 type = USB_TYPE_VENDOR | USB_DIR_IN;
53                 value = 0;
54                 break;
55         case 2:
56                 /* write { reg, value } */
57                 request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_WRITE :
58                                                         DTV5100_TUNER_WRITE);
59                 type = USB_TYPE_VENDOR | USB_DIR_OUT;
60                 value = wbuf[1];
61                 break;
62         default:
63                 warn("wlen = %x, aborting.", wlen);
64                 return -EINVAL;
65         }
66         index = (addr << 8) + wbuf[0];
67
68         memcpy(st->data, rbuf, rlen);
69         msleep(1); /* avoid I2C errors */
70         return usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), request,
71                                type, value, index, st->data, rlen,
72                                DTV5100_USB_TIMEOUT);
73 }
74
75 /* I2C */
76 static int dtv5100_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
77                             int num)
78 {
79         struct dvb_usb_device *d = i2c_get_adapdata(adap);
80         int i;
81
82         if (num > 2)
83                 return -EINVAL;
84
85         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
86                 return -EAGAIN;
87
88         for (i = 0; i < num; i++) {
89                 /* write/read request */
90                 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
91                         if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf,
92                                             msg[i].len, msg[i+1].buf,
93                                             msg[i+1].len) < 0)
94                                 break;
95                         i++;
96                 } else if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf,
97                                            msg[i].len, NULL, 0) < 0)
98                                 break;
99         }
100
101         mutex_unlock(&d->i2c_mutex);
102         return i;
103 }
104
105 static u32 dtv5100_i2c_func(struct i2c_adapter *adapter)
106 {
107         return I2C_FUNC_I2C;
108 }
109
110 static struct i2c_algorithm dtv5100_i2c_algo = {
111         .master_xfer   = dtv5100_i2c_xfer,
112         .functionality = dtv5100_i2c_func,
113 };
114
115 /* Callbacks for DVB USB */
116 static struct zl10353_config dtv5100_zl10353_config = {
117         .demod_address = DTV5100_DEMOD_ADDR,
118         .no_tuner = 1,
119         .parallel_ts = 1,
120 };
121
122 static int dtv5100_frontend_attach(struct dvb_usb_adapter *adap)
123 {
124         adap->fe_adap[0].fe = dvb_attach(zl10353_attach, &dtv5100_zl10353_config,
125                               &adap->dev->i2c_adap);
126         if (adap->fe_adap[0].fe == NULL)
127                 return -EIO;
128
129         /* disable i2c gate, or it won't work... is this safe? */
130         adap->fe_adap[0].fe->ops.i2c_gate_ctrl = NULL;
131
132         return 0;
133 }
134
135 static struct qt1010_config dtv5100_qt1010_config = {
136         .i2c_address = DTV5100_TUNER_ADDR
137 };
138
139 static int dtv5100_tuner_attach(struct dvb_usb_adapter *adap)
140 {
141         return dvb_attach(qt1010_attach,
142                           adap->fe_adap[0].fe, &adap->dev->i2c_adap,
143                           &dtv5100_qt1010_config) == NULL ? -ENODEV : 0;
144 }
145
146 /* DVB USB Driver stuff */
147 static struct dvb_usb_device_properties dtv5100_properties;
148
149 static int dtv5100_probe(struct usb_interface *intf,
150                          const struct usb_device_id *id)
151 {
152         int i, ret;
153         struct usb_device *udev = interface_to_usbdev(intf);
154
155         /* initialize non qt1010/zl10353 part? */
156         for (i = 0; dtv5100_init[i].request; i++) {
157                 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
158                                       dtv5100_init[i].request,
159                                       USB_TYPE_VENDOR | USB_DIR_OUT,
160                                       dtv5100_init[i].value,
161                                       dtv5100_init[i].index, NULL, 0,
162                                       DTV5100_USB_TIMEOUT);
163                 if (ret)
164                         return ret;
165         }
166
167         ret = dvb_usb_device_init(intf, &dtv5100_properties,
168                                   THIS_MODULE, NULL, adapter_nr);
169         if (ret)
170                 return ret;
171
172         return 0;
173 }
174
175 static struct usb_device_id dtv5100_table[] = {
176         { USB_DEVICE(0x06be, 0xa232) },
177         { }             /* Terminating entry */
178 };
179 MODULE_DEVICE_TABLE(usb, dtv5100_table);
180
181 static struct dvb_usb_device_properties dtv5100_properties = {
182         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
183         .usb_ctrl = DEVICE_SPECIFIC,
184
185         .size_of_priv = sizeof(struct dtv5100_state),
186
187         .num_adapters = 1,
188         .adapter = {{
189                 .num_frontends = 1,
190                 .fe = {{
191                 .frontend_attach = dtv5100_frontend_attach,
192                 .tuner_attach    = dtv5100_tuner_attach,
193
194                 .stream = {
195                         .type = USB_BULK,
196                         .count = 8,
197                         .endpoint = 0x82,
198                         .u = {
199                                 .bulk = {
200                                         .buffersize = 4096,
201                                 }
202                         }
203                 },
204                 }},
205         } },
206
207         .i2c_algo = &dtv5100_i2c_algo,
208
209         .num_device_descs = 1,
210         .devices = {
211                 {
212                         .name = "AME DTV-5100 USB2.0 DVB-T",
213                         .cold_ids = { NULL },
214                         .warm_ids = { &dtv5100_table[0], NULL },
215                 },
216         }
217 };
218
219 static struct usb_driver dtv5100_driver = {
220         .name           = "dvb_usb_dtv5100",
221         .probe          = dtv5100_probe,
222         .disconnect     = dvb_usb_device_exit,
223         .id_table       = dtv5100_table,
224 };
225
226 module_usb_driver(dtv5100_driver);
227
228 MODULE_AUTHOR(DRIVER_AUTHOR);
229 MODULE_DESCRIPTION(DRIVER_DESC);
230 MODULE_LICENSE("GPL");