ce6230.c 6.9 KB

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