]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/usb/dvb-usb-v2/ec168.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.git] / drivers / media / usb / dvb-usb-v2 / ec168.c
CommitLineData
2bf290be
AP
1/*
2 * E3C EC168 DVB USB driver
3 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
2bf290be
AP
16 */
17
18#include "ec168.h"
19#include "ec100.h"
20#include "mxl5005s.h"
21
2bf290be
AP
22DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
23
3eee0472 24static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
2bf290be
AP
25{
26 int ret;
27 unsigned int pipe;
28 u8 request, requesttype;
26b72c6e
FM
29 u8 *buf;
30
2bf290be
AP
31 switch (req->cmd) {
32 case DOWNLOAD_FIRMWARE:
33 case GPIO:
34 case WRITE_I2C:
35 case STREAMING_CTRL:
36 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
37 request = req->cmd;
38 break;
39 case READ_I2C:
40 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
41 request = req->cmd;
42 break;
43 case GET_CONFIG:
44 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
45 request = CONFIG;
46 break;
47 case SET_CONFIG:
48 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
49 request = CONFIG;
50 break;
51 case WRITE_DEMOD:
52 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
53 request = DEMOD_RW;
54 break;
55 case READ_DEMOD:
56 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
57 request = DEMOD_RW;
58 break;
59 default:
3cedcab8
AP
60 dev_err(&d->udev->dev, "%s: unknown command=%02x\n",
61 KBUILD_MODNAME, req->cmd);
ce6ea9a9 62 ret = -EINVAL;
2bf290be
AP
63 goto error;
64 }
65
26b72c6e
FM
66 buf = kmalloc(req->size, GFP_KERNEL);
67 if (!buf) {
68 ret = -ENOMEM;
69 goto error;
70 }
71
2bf290be
AP
72 if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
73 /* write */
74 memcpy(buf, req->data, req->size);
3eee0472 75 pipe = usb_sndctrlpipe(d->udev, 0);
2bf290be
AP
76 } else {
77 /* read */
3eee0472 78 pipe = usb_rcvctrlpipe(d->udev, 0);
2bf290be
AP
79 }
80
81 msleep(1); /* avoid I2C errors */
82
3eee0472 83 ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value,
26b72c6e 84 req->index, buf, req->size, EC168_USB_TIMEOUT);
2bf290be 85
d89b9369
AP
86 dvb_usb_dbg_usb_control_msg(d->udev, request, requesttype, req->value,
87 req->index, buf, req->size);
2bf290be
AP
88
89 if (ret < 0)
26b72c6e 90 goto err_dealloc;
2bf290be
AP
91 else
92 ret = 0;
93
94 /* read request, copy returned data to return buf */
95 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
96 memcpy(req->data, buf, req->size);
97
26b72c6e 98 kfree(buf);
2bf290be 99 return ret;
26b72c6e
FM
100
101err_dealloc:
102 kfree(buf);
2bf290be 103error:
3cedcab8 104 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
2bf290be
AP
105 return ret;
106}
107
2bf290be 108/* I2C */
3eee0472
AP
109static struct ec100_config ec168_ec100_config;
110
2bf290be
AP
111static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
112 int num)
113{
114 struct dvb_usb_device *d = i2c_get_adapdata(adap);
115 struct ec168_req req;
116 int i = 0;
117 int ret;
118
119 if (num > 2)
120 return -EINVAL;
121
122 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
123 return -EAGAIN;
124
125 while (i < num) {
126 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
127 if (msg[i].addr == ec168_ec100_config.demod_address) {
128 req.cmd = READ_DEMOD;
129 req.value = 0;
130 req.index = 0xff00 + msg[i].buf[0]; /* reg */
131 req.size = msg[i+1].len; /* bytes to read */
132 req.data = &msg[i+1].buf[0];
133 ret = ec168_ctrl_msg(d, &req);
134 i += 2;
135 } else {
3cedcab8
AP
136 dev_err(&d->udev->dev, "%s: I2C read not " \
137 "implemented\n",
ce6ea9a9
AP
138 KBUILD_MODNAME);
139 ret = -EOPNOTSUPP;
2bf290be
AP
140 i += 2;
141 }
142 } else {
143 if (msg[i].addr == ec168_ec100_config.demod_address) {
144 req.cmd = WRITE_DEMOD;
145 req.value = msg[i].buf[1]; /* val */
146 req.index = 0xff00 + msg[i].buf[0]; /* reg */
147 req.size = 0;
148 req.data = NULL;
149 ret = ec168_ctrl_msg(d, &req);
150 i += 1;
151 } else {
152 req.cmd = WRITE_I2C;
153 req.value = msg[i].buf[0]; /* val */
154 req.index = 0x0100 + msg[i].addr; /* I2C addr */
155 req.size = msg[i].len-1;
156 req.data = &msg[i].buf[1];
157 ret = ec168_ctrl_msg(d, &req);
158 i += 1;
159 }
160 }
161 if (ret)
162 goto error;
163
164 }
165 ret = i;
166
167error:
168 mutex_unlock(&d->i2c_mutex);
6c49d793 169 return ret;
2bf290be
AP
170}
171
2bf290be
AP
172static u32 ec168_i2c_func(struct i2c_adapter *adapter)
173{
174 return I2C_FUNC_I2C;
175}
176
177static struct i2c_algorithm ec168_i2c_algo = {
178 .master_xfer = ec168_i2c_xfer,
179 .functionality = ec168_i2c_func,
180};
181
182/* Callbacks for DVB USB */
a0921af7 183static int ec168_identify_state(struct dvb_usb_device *d, const char **name)
2bf290be 184{
3eee0472
AP
185 int ret;
186 u8 reply;
187 struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
3cedcab8 188 dev_dbg(&d->udev->dev, "%s:\n", __func__);
2bf290be 189
3eee0472
AP
190 ret = ec168_ctrl_msg(d, &req);
191 if (ret)
192 goto error;
2bf290be 193
3cedcab8 194 dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
2bf290be 195
3eee0472
AP
196 if (reply == 0x01)
197 ret = WARM;
198 else
199 ret = COLD;
2bf290be 200
3eee0472
AP
201 return ret;
202error:
3cedcab8 203 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
3eee0472 204 return ret;
2bf290be
AP
205}
206
3eee0472
AP
207static int ec168_download_firmware(struct dvb_usb_device *d,
208 const struct firmware *fw)
2bf290be 209{
77e28ec2 210 int ret, len, remaining;
2bf290be 211 struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
3cedcab8 212 dev_dbg(&d->udev->dev, "%s:\n", __func__);
2bf290be 213
77e28ec2
AP
214 #define LEN_MAX 2048 /* max packet size */
215 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
216 len = remaining;
217 if (len > LEN_MAX)
218 len = LEN_MAX;
2bf290be
AP
219
220 req.size = len;
77e28ec2
AP
221 req.data = (u8 *) &fw->data[fw->size - remaining];
222 req.index = fw->size - remaining;
2bf290be 223
3eee0472 224 ret = ec168_ctrl_msg(d, &req);
2bf290be 225 if (ret) {
3cedcab8
AP
226 dev_err(&d->udev->dev,
227 "%s: firmware download failed=%d\n",
77e28ec2 228 KBUILD_MODNAME, ret);
2bf290be
AP
229 goto error;
230 }
231 }
77e28ec2 232
2bf290be
AP
233 req.size = 0;
234
235 /* set "warm"? */
236 req.cmd = SET_CONFIG;
237 req.value = 0;
238 req.index = 0x0001;
3eee0472 239 ret = ec168_ctrl_msg(d, &req);
2bf290be
AP
240 if (ret)
241 goto error;
242
243 /* really needed - no idea what does */
244 req.cmd = GPIO;
245 req.value = 0;
246 req.index = 0x0206;
3eee0472 247 ret = ec168_ctrl_msg(d, &req);
2bf290be
AP
248 if (ret)
249 goto error;
250
251 /* activate tuner I2C? */
252 req.cmd = WRITE_I2C;
253 req.value = 0;
254 req.index = 0x00c6;
3eee0472 255 ret = ec168_ctrl_msg(d, &req);
2bf290be
AP
256 if (ret)
257 goto error;
258
259 return ret;
260error:
3cedcab8 261 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
2bf290be
AP
262 return ret;
263}
264
3eee0472
AP
265static struct ec100_config ec168_ec100_config = {
266 .demod_address = 0xff, /* not real address, demod is integrated */
267};
268
269static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
2bf290be 270{
3cedcab8
AP
271 struct dvb_usb_device *d = adap_to_d(adap);
272 dev_dbg(&d->udev->dev, "%s:\n", __func__);
273
3eee0472 274 adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
3cedcab8 275 &d->i2c_adap);
3eee0472
AP
276 if (adap->fe[0] == NULL)
277 return -ENODEV;
2bf290be 278
3eee0472 279 return 0;
2bf290be
AP
280}
281
3eee0472
AP
282static struct mxl5005s_config ec168_mxl5003s_config = {
283 .i2c_address = 0xc6,
284 .if_freq = IF_FREQ_4570000HZ,
285 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
286 .agc_mode = MXL_SINGLE_AGC,
287 .tracking_filter = MXL_TF_OFF,
288 .rssi_enable = MXL_RSSI_ENABLE,
289 .cap_select = MXL_CAP_SEL_ENABLE,
290 .div_out = MXL_DIV_OUT_4,
291 .clock_out = MXL_CLOCK_OUT_DISABLE,
292 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
293 .top = MXL5005S_TOP_25P2,
294 .mod_mode = MXL_DIGITAL_MODE,
295 .if_mode = MXL_ZERO_IF,
296 .AgcMasterByte = 0x00,
297};
2bf290be 298
3eee0472 299static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
2bf290be 300{
3cedcab8
AP
301 struct dvb_usb_device *d = adap_to_d(adap);
302 dev_dbg(&d->udev->dev, "%s:\n", __func__);
303
304 return dvb_attach(mxl5005s_attach, adap->fe[0], &d->i2c_adap,
3eee0472 305 &ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
2bf290be
AP
306}
307
a13a6e1f 308static int ec168_streaming_ctrl(struct dvb_frontend *fe, int onoff)
3eee0472 309{
3cedcab8 310 struct dvb_usb_device *d = fe_to_d(fe);
3eee0472 311 struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
3cedcab8
AP
312 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
313
3eee0472
AP
314 if (onoff)
315 req.index = 0x0102;
3cedcab8 316 return ec168_ctrl_msg(d, &req);
3eee0472 317}
2bf290be 318
3eee0472
AP
319/* DVB USB Driver stuff */
320/* bInterfaceNumber 0 is HID
321 * bInterfaceNumber 1 is DVB-T */
322static struct dvb_usb_device_properties ec168_props = {
323 .driver_name = KBUILD_MODNAME,
324 .owner = THIS_MODULE,
325 .adapter_nr = adapter_nr,
326 .bInterfaceNumber = 1,
2bf290be 327
3eee0472 328 .identify_state = ec168_identify_state,
6a60e3f6 329 .firmware = EC168_FIRMWARE,
3eee0472 330 .download_firmware = ec168_download_firmware,
2bf290be 331
3eee0472
AP
332 .i2c_algo = &ec168_i2c_algo,
333 .frontend_attach = ec168_ec100_frontend_attach,
334 .tuner_attach = ec168_mxl5003s_tuner_attach,
335 .streaming_ctrl = ec168_streaming_ctrl,
2bf290be
AP
336
337 .num_adapters = 1,
338 .adapter = {
339 {
2731d4ed 340 .stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
2bf290be
AP
341 }
342 },
3eee0472 343};
2bf290be 344
3eee0472
AP
345static const struct dvb_usb_driver_info ec168_driver_info = {
346 .name = "E3C EC168 reference design",
347 .props = &ec168_props,
348};
2bf290be 349
3eee0472
AP
350static const struct usb_device_id ec168_id[] = {
351 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168),
352 .driver_info = (kernel_ulong_t) &ec168_driver_info },
353 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2),
354 .driver_info = (kernel_ulong_t) &ec168_driver_info },
355 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3),
356 .driver_info = (kernel_ulong_t) &ec168_driver_info },
357 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4),
358 .driver_info = (kernel_ulong_t) &ec168_driver_info },
359 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5),
360 .driver_info = (kernel_ulong_t) &ec168_driver_info },
361 {}
2bf290be 362};
3eee0472 363MODULE_DEVICE_TABLE(usb, ec168_id);
2bf290be
AP
364
365static struct usb_driver ec168_driver = {
3eee0472
AP
366 .name = KBUILD_MODNAME,
367 .id_table = ec168_id,
368 .probe = dvb_usbv2_probe,
369 .disconnect = dvb_usbv2_disconnect,
370 .suspend = dvb_usbv2_suspend,
371 .resume = dvb_usbv2_resume,
372 .no_dynamic_id = 1,
373 .soft_unbind = 1,
2bf290be
AP
374};
375
ecb3b2b3 376module_usb_driver(ec168_driver);
2bf290be
AP
377
378MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
ce6ea9a9 379MODULE_DESCRIPTION("E3C EC168 driver");
2bf290be 380MODULE_LICENSE("GPL");
6a60e3f6 381MODULE_FIRMWARE(EC168_FIRMWARE);