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