ce6230.c 7.1 KB

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