Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 1 | /* |
| 2 | * DVB USB Linux driver for Intel CE6230 DVB-T USB2.0 receiver |
| 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 "ce6230.h" |
| 23 | #include "zl10353.h" |
| 24 | #include "mxl5005s.h" |
| 25 | |
| 26 | /* debug */ |
| 27 | static int dvb_usb_ce6230_debug; |
| 28 | module_param_named(debug, dvb_usb_ce6230_debug, int, 0644); |
| 29 | MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS); |
| 30 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 31 | |
| 32 | static struct zl10353_config ce6230_zl10353_config; |
| 33 | |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 34 | static int ce6230_ctrl_msg(struct dvb_usb_device *d, struct req_t *req) |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 35 | { |
| 36 | int ret; |
| 37 | unsigned int pipe; |
| 38 | u8 request; |
| 39 | u8 requesttype; |
| 40 | u16 value; |
| 41 | u16 index; |
Florian Mickler | 029461d | 2011-03-20 18:50:49 -0300 | [diff] [blame] | 42 | u8 *buf; |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 43 | |
| 44 | request = req->cmd; |
| 45 | value = req->value; |
| 46 | index = req->index; |
| 47 | |
| 48 | switch (req->cmd) { |
| 49 | case I2C_READ: |
| 50 | case DEMOD_READ: |
| 51 | case REG_READ: |
| 52 | requesttype = (USB_TYPE_VENDOR | USB_DIR_IN); |
| 53 | break; |
| 54 | case I2C_WRITE: |
| 55 | case DEMOD_WRITE: |
| 56 | case REG_WRITE: |
| 57 | requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT); |
| 58 | break; |
| 59 | default: |
| 60 | err("unknown command:%02x", req->cmd); |
| 61 | ret = -EPERM; |
| 62 | goto error; |
| 63 | } |
| 64 | |
Florian Mickler | 029461d | 2011-03-20 18:50:49 -0300 | [diff] [blame] | 65 | buf = kmalloc(req->data_len, GFP_KERNEL); |
| 66 | if (!buf) { |
| 67 | ret = -ENOMEM; |
| 68 | goto error; |
| 69 | } |
| 70 | |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 71 | if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) { |
| 72 | /* write */ |
| 73 | memcpy(buf, req->data, req->data_len); |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 74 | pipe = usb_sndctrlpipe(d->udev, 0); |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 75 | } else { |
| 76 | /* read */ |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 77 | pipe = usb_rcvctrlpipe(d->udev, 0); |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | msleep(1); /* avoid I2C errors */ |
| 81 | |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 82 | ret = usb_control_msg(d->udev, pipe, request, requesttype, value, index, |
Florian Mickler | 029461d | 2011-03-20 18:50:49 -0300 | [diff] [blame] | 83 | buf, req->data_len, CE6230_USB_TIMEOUT); |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 84 | |
| 85 | ce6230_debug_dump(request, requesttype, value, index, buf, |
| 86 | req->data_len, deb_xfer); |
| 87 | |
| 88 | if (ret < 0) |
| 89 | deb_info("%s: usb_control_msg failed:%d\n", __func__, ret); |
| 90 | else |
| 91 | ret = 0; |
| 92 | |
| 93 | /* read request, copy returned data to return buf */ |
| 94 | if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN)) |
| 95 | memcpy(req->data, buf, req->data_len); |
| 96 | |
Florian Mickler | 029461d | 2011-03-20 18:50:49 -0300 | [diff] [blame] | 97 | kfree(buf); |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 98 | error: |
| 99 | return ret; |
| 100 | } |
| 101 | |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 102 | /* I2C */ |
| 103 | static int ce6230_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], |
| 104 | int num) |
| 105 | { |
| 106 | struct dvb_usb_device *d = i2c_get_adapdata(adap); |
| 107 | int i = 0; |
| 108 | struct req_t req; |
Mauro Carvalho Chehab | 2ddacee | 2009-03-26 12:26:48 -0300 | [diff] [blame] | 109 | int ret = 0; |
Jean Delvare | 7157fbd | 2009-11-04 15:26:59 -0300 | [diff] [blame] | 110 | memset(&req, 0, sizeof(req)); |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 111 | |
| 112 | if (num > 2) |
| 113 | return -EINVAL; |
| 114 | |
| 115 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) |
| 116 | return -EAGAIN; |
| 117 | |
| 118 | while (i < num) { |
| 119 | if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) { |
| 120 | if (msg[i].addr == |
| 121 | ce6230_zl10353_config.demod_address) { |
| 122 | req.cmd = DEMOD_READ; |
| 123 | req.value = msg[i].addr >> 1; |
| 124 | req.index = msg[i].buf[0]; |
| 125 | req.data_len = msg[i+1].len; |
| 126 | req.data = &msg[i+1].buf[0]; |
| 127 | ret = ce6230_ctrl_msg(d, &req); |
| 128 | } else { |
| 129 | err("i2c read not implemented"); |
| 130 | ret = -EPERM; |
| 131 | } |
| 132 | i += 2; |
| 133 | } else { |
| 134 | if (msg[i].addr == |
| 135 | ce6230_zl10353_config.demod_address) { |
| 136 | req.cmd = DEMOD_WRITE; |
| 137 | req.value = msg[i].addr >> 1; |
| 138 | req.index = msg[i].buf[0]; |
| 139 | req.data_len = msg[i].len-1; |
| 140 | req.data = &msg[i].buf[1]; |
| 141 | ret = ce6230_ctrl_msg(d, &req); |
| 142 | } else { |
| 143 | req.cmd = I2C_WRITE; |
| 144 | req.value = 0x2000 + (msg[i].addr >> 1); |
| 145 | req.index = 0x0000; |
| 146 | req.data_len = msg[i].len; |
| 147 | req.data = &msg[i].buf[0]; |
| 148 | ret = ce6230_ctrl_msg(d, &req); |
| 149 | } |
| 150 | i += 1; |
| 151 | } |
| 152 | if (ret) |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | mutex_unlock(&d->i2c_mutex); |
| 157 | return ret ? ret : i; |
| 158 | } |
| 159 | |
| 160 | static u32 ce6230_i2c_func(struct i2c_adapter *adapter) |
| 161 | { |
| 162 | return I2C_FUNC_I2C; |
| 163 | } |
| 164 | |
| 165 | static struct i2c_algorithm ce6230_i2c_algo = { |
| 166 | .master_xfer = ce6230_i2c_xfer, |
| 167 | .functionality = ce6230_i2c_func, |
| 168 | }; |
| 169 | |
| 170 | /* Callbacks for DVB USB */ |
| 171 | static struct zl10353_config ce6230_zl10353_config = { |
| 172 | .demod_address = 0x1e, |
| 173 | .adc_clock = 450000, |
| 174 | .if2 = 45700, |
| 175 | .no_tuner = 1, |
| 176 | .parallel_ts = 1, |
| 177 | .clock_ctl_1 = 0x34, |
| 178 | .pll_0 = 0x0e, |
| 179 | }; |
| 180 | |
| 181 | static int ce6230_zl10353_frontend_attach(struct dvb_usb_adapter *adap) |
| 182 | { |
| 183 | deb_info("%s:\n", __func__); |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 184 | adap->fe[0] = dvb_attach(zl10353_attach, &ce6230_zl10353_config, |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 185 | &adap->dev->i2c_adap); |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 186 | if (adap->fe[0] == NULL) |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 187 | return -ENODEV; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static struct mxl5005s_config ce6230_mxl5003s_config = { |
| 192 | .i2c_address = 0xc6, |
| 193 | .if_freq = IF_FREQ_4570000HZ, |
| 194 | .xtal_freq = CRYSTAL_FREQ_16000000HZ, |
| 195 | .agc_mode = MXL_SINGLE_AGC, |
| 196 | .tracking_filter = MXL_TF_DEFAULT, |
| 197 | .rssi_enable = MXL_RSSI_ENABLE, |
| 198 | .cap_select = MXL_CAP_SEL_ENABLE, |
| 199 | .div_out = MXL_DIV_OUT_4, |
| 200 | .clock_out = MXL_CLOCK_OUT_DISABLE, |
| 201 | .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM, |
| 202 | .top = MXL5005S_TOP_25P2, |
| 203 | .mod_mode = MXL_DIGITAL_MODE, |
| 204 | .if_mode = MXL_ZERO_IF, |
| 205 | .AgcMasterByte = 0x00, |
| 206 | }; |
| 207 | |
| 208 | static int ce6230_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap) |
| 209 | { |
| 210 | int ret; |
| 211 | deb_info("%s:\n", __func__); |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 212 | ret = dvb_attach(mxl5005s_attach, adap->fe[0], &adap->dev->i2c_adap, |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 213 | &ce6230_mxl5003s_config) == NULL ? -ENODEV : 0; |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | static int ce6230_power_ctrl(struct dvb_usb_device *d, int onoff) |
| 218 | { |
| 219 | int ret; |
| 220 | deb_info("%s: onoff:%d\n", __func__, onoff); |
| 221 | |
| 222 | /* InterfaceNumber 1 / AlternateSetting 0 idle |
| 223 | InterfaceNumber 1 / AlternateSetting 1 streaming */ |
| 224 | ret = usb_set_interface(d->udev, 1, onoff); |
| 225 | if (ret) |
| 226 | err("usb_set_interface failed with error:%d", ret); |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | /* DVB USB Driver stuff */ |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 232 | static struct dvb_usb_device_properties ce6230_props = { |
| 233 | .driver_name = KBUILD_MODNAME, |
| 234 | .owner = THIS_MODULE, |
| 235 | .adapter_nr = adapter_nr, |
| 236 | .bInterfaceNumber = 1, |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 237 | |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 238 | .i2c_algo = &ce6230_i2c_algo, |
| 239 | .power_ctrl = ce6230_power_ctrl, |
| 240 | .frontend_attach = ce6230_zl10353_frontend_attach, |
| 241 | .tuner_attach = ce6230_mxl5003s_tuner_attach, |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 242 | |
| 243 | .num_adapters = 1, |
| 244 | .adapter = { |
| 245 | { |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 246 | .stream = { |
| 247 | .type = USB_BULK, |
| 248 | .count = 6, |
| 249 | .endpoint = 0x82, |
| 250 | .u = { |
| 251 | .bulk = { |
Antti Palosaari | eb3b2d8 | 2009-09-16 20:21:52 -0300 | [diff] [blame] | 252 | .buffersize = (16*512), |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | }, |
| 256 | } |
| 257 | }, |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 258 | }; |
| 259 | |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 260 | static const struct usb_device_id ce6230_id_table[] = { |
| 261 | { DVB_USB_DEVICE(USB_VID_INTEL, USB_PID_INTEL_CE9500, |
| 262 | &ce6230_props, "Intel CE9500 reference design", NULL) }, |
| 263 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A310, |
| 264 | &ce6230_props, "AVerMedia A310 USB 2.0 DVB-T tuner", NULL) }, |
| 265 | { } |
| 266 | }; |
| 267 | MODULE_DEVICE_TABLE(usb, ce6230_id_table); |
| 268 | |
| 269 | static struct usb_driver ce6230_usb_driver = { |
| 270 | .name = KBUILD_MODNAME, |
| 271 | .id_table = ce6230_id_table, |
| 272 | .probe = dvb_usbv2_probe, |
| 273 | .disconnect = dvb_usbv2_disconnect, |
| 274 | .suspend = dvb_usbv2_suspend, |
| 275 | .resume = dvb_usbv2_resume, |
| 276 | .no_dynamic_id = 1, |
| 277 | .soft_unbind = 1, |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 278 | }; |
| 279 | |
Antti Palosaari | ab84f18 | 2012-06-13 23:03:08 -0300 | [diff] [blame^] | 280 | module_usb_driver(ce6230_usb_driver); |
Antti Palosaari | eebb876 | 2009-03-25 16:59:45 -0300 | [diff] [blame] | 281 | |
| 282 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 283 | MODULE_DESCRIPTION("Driver for Intel CE6230 DVB-T USB2.0"); |
| 284 | MODULE_LICENSE("GPL"); |