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