Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af9035.c
Go to the documentation of this file.
1 /*
2  * Afatech AF9035 DVB USB driver
3  *
4  * Copyright (C) 2009 Antti Palosaari <[email protected]>
5  * Copyright (C) 2012 Antti Palosaari <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "af9035.h"
23 
25 
26 static u16 af9035_checksum(const u8 *buf, size_t len)
27 {
28  size_t i;
29  u16 checksum = 0;
30 
31  for (i = 1; i < len; i++) {
32  if (i % 2)
33  checksum += buf[i] << 8;
34  else
35  checksum += buf[i];
36  }
37  checksum = ~checksum;
38 
39  return checksum;
40 }
41 
42 static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
43 {
44 #define BUF_LEN 64
45 #define REQ_HDR_LEN 4 /* send header size */
46 #define ACK_HDR_LEN 3 /* rece header size */
47 #define CHECKSUM_LEN 2
48 #define USB_TIMEOUT 2000
49  struct state *state = d_to_priv(d);
50  int ret, wlen, rlen;
51  u8 buf[BUF_LEN];
52  u16 checksum, tmp_checksum;
53 
54  /* buffer overflow check */
55  if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
56  req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
57  dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
58  __func__, req->wlen, req->rlen);
59  return -EINVAL;
60  }
61 
62  buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
63  buf[1] = req->mbox;
64  buf[2] = req->cmd;
65  buf[3] = state->seq++;
66  memcpy(&buf[REQ_HDR_LEN], req->wbuf, req->wlen);
67 
68  wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
69  rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
70 
71  /* calc and add checksum */
72  checksum = af9035_checksum(buf, buf[0] - 1);
73  buf[buf[0] - 1] = (checksum >> 8);
74  buf[buf[0] - 0] = (checksum & 0xff);
75 
76  /* no ack for these packets */
77  if (req->cmd == CMD_FW_DL)
78  rlen = 0;
79 
80  ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen);
81  if (ret)
82  goto err;
83 
84  /* no ack for those packets */
85  if (req->cmd == CMD_FW_DL)
86  goto exit;
87 
88  /* verify checksum */
89  checksum = af9035_checksum(buf, rlen - 2);
90  tmp_checksum = (buf[rlen - 2] << 8) | buf[rlen - 1];
91  if (tmp_checksum != checksum) {
92  dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \
93  "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd,
94  tmp_checksum, checksum);
95  ret = -EIO;
96  goto err;
97  }
98 
99  /* check status */
100  if (buf[2]) {
101  dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
102  __func__, req->cmd, buf[2]);
103  ret = -EIO;
104  goto err;
105  }
106 
107  /* read request, copy returned data to return buf */
108  if (req->rlen)
109  memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen);
110 
111 exit:
112  return 0;
113 
114 err:
115  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
116 
117  return ret;
118 }
119 
120 /* write multiple registers */
121 static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
122 {
123  u8 wbuf[6 + len];
124  u8 mbox = (reg >> 16) & 0xff;
125  struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
126 
127  wbuf[0] = len;
128  wbuf[1] = 2;
129  wbuf[2] = 0;
130  wbuf[3] = 0;
131  wbuf[4] = (reg >> 8) & 0xff;
132  wbuf[5] = (reg >> 0) & 0xff;
133  memcpy(&wbuf[6], val, len);
134 
135  return af9035_ctrl_msg(d, &req);
136 }
137 
138 /* read multiple registers */
139 static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
140 {
141  u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
142  u8 mbox = (reg >> 16) & 0xff;
143  struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
144 
145  return af9035_ctrl_msg(d, &req);
146 }
147 
148 /* write single register */
149 static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
150 {
151  return af9035_wr_regs(d, reg, &val, 1);
152 }
153 
154 /* read single register */
155 static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
156 {
157  return af9035_rd_regs(d, reg, val, 1);
158 }
159 
160 /* write single register with mask */
161 static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
162  u8 mask)
163 {
164  int ret;
165  u8 tmp;
166 
167  /* no need for read if whole reg is written */
168  if (mask != 0xff) {
169  ret = af9035_rd_regs(d, reg, &tmp, 1);
170  if (ret)
171  return ret;
172 
173  val &= mask;
174  tmp &= ~mask;
175  val |= tmp;
176  }
177 
178  return af9035_wr_regs(d, reg, &val, 1);
179 }
180 
181 static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
182  struct i2c_msg msg[], int num)
183 {
184  struct dvb_usb_device *d = i2c_get_adapdata(adap);
185  struct state *state = d_to_priv(d);
186  int ret;
187 
188  if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
189  return -EAGAIN;
190 
191  /*
192  * I2C sub header is 5 bytes long. Meaning of those bytes are:
193  * 0: data len
194  * 1: I2C addr << 1
195  * 2: reg addr len
196  * byte 3 and 4 can be used as reg addr
197  * 3: reg addr MSB
198  * used when reg addr len is set to 2
199  * 4: reg addr LSB
200  * used when reg addr len is set to 1 or 2
201  *
202  * For the simplify we do not use register addr at all.
203  * NOTE: As a firmware knows tuner type there is very small possibility
204  * there could be some tuner I2C hacks done by firmware and this may
205  * lead problems if firmware expects those bytes are used.
206  */
207  if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
208  (msg[1].flags & I2C_M_RD)) {
209  if (msg[0].len > 40 || msg[1].len > 40) {
210  /* TODO: correct limits > 40 */
211  ret = -EOPNOTSUPP;
212  } else if (msg[0].addr == state->af9033_config[0].i2c_addr) {
213  /* integrated demod */
214  u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
215  msg[0].buf[2];
216  ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
217  msg[1].len);
218  } else {
219  /* I2C */
220  u8 buf[5 + msg[0].len];
221  struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
222  buf, msg[1].len, msg[1].buf };
223  buf[0] = msg[1].len;
224  buf[1] = msg[0].addr << 1;
225  buf[2] = 0x00; /* reg addr len */
226  buf[3] = 0x00; /* reg addr MSB */
227  buf[4] = 0x00; /* reg addr LSB */
228  memcpy(&buf[5], msg[0].buf, msg[0].len);
229  ret = af9035_ctrl_msg(d, &req);
230  }
231  } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
232  if (msg[0].len > 40) {
233  /* TODO: correct limits > 40 */
234  ret = -EOPNOTSUPP;
235  } else if (msg[0].addr == state->af9033_config[0].i2c_addr) {
236  /* integrated demod */
237  u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
238  msg[0].buf[2];
239  ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
240  msg[0].len - 3);
241  } else {
242  /* I2C */
243  u8 buf[5 + msg[0].len];
244  struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
245  0, NULL };
246  buf[0] = msg[0].len;
247  buf[1] = msg[0].addr << 1;
248  buf[2] = 0x00; /* reg addr len */
249  buf[3] = 0x00; /* reg addr MSB */
250  buf[4] = 0x00; /* reg addr LSB */
251  memcpy(&buf[5], msg[0].buf, msg[0].len);
252  ret = af9035_ctrl_msg(d, &req);
253  }
254  } else {
255  /*
256  * We support only two kind of I2C transactions:
257  * 1) 1 x read + 1 x write
258  * 2) 1 x write
259  */
260  ret = -EOPNOTSUPP;
261  }
262 
263  mutex_unlock(&d->i2c_mutex);
264 
265  if (ret < 0)
266  return ret;
267  else
268  return num;
269 }
270 
271 static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
272 {
273  return I2C_FUNC_I2C;
274 }
275 
276 static struct i2c_algorithm af9035_i2c_algo = {
277  .master_xfer = af9035_i2c_master_xfer,
278  .functionality = af9035_i2c_functionality,
279 };
280 
281 static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
282 {
283  int ret;
284  u8 wbuf[1] = { 1 };
285  u8 rbuf[4];
286  struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
287  sizeof(rbuf), rbuf };
288 
289  ret = af9035_ctrl_msg(d, &req);
290  if (ret < 0)
291  goto err;
292 
293  dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
294  if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
295  ret = WARM;
296  else
297  ret = COLD;
298 
299  return ret;
300 
301 err:
302  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
303 
304  return ret;
305 }
306 
307 static int af9035_download_firmware(struct dvb_usb_device *d,
308  const struct firmware *fw)
309 {
310  int ret, i, j, len;
311  u8 wbuf[1];
312  u8 rbuf[4];
313  struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
314  struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
315  struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
316  u8 hdr_core;
317  u16 hdr_addr, hdr_data_len, hdr_checksum;
318  #define MAX_DATA 58
319  #define HDR_SIZE 7
320 
321  /*
322  * Thanks to Daniel Glöckner <[email protected]> about that info!
323  *
324  * byte 0: MCS 51 core
325  * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
326  * address spaces
327  * byte 1-2: Big endian destination address
328  * byte 3-4: Big endian number of data bytes following the header
329  * byte 5-6: Big endian header checksum, apparently ignored by the chip
330  * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
331  */
332 
333  for (i = fw->size; i > HDR_SIZE;) {
334  hdr_core = fw->data[fw->size - i + 0];
335  hdr_addr = fw->data[fw->size - i + 1] << 8;
336  hdr_addr |= fw->data[fw->size - i + 2] << 0;
337  hdr_data_len = fw->data[fw->size - i + 3] << 8;
338  hdr_data_len |= fw->data[fw->size - i + 4] << 0;
339  hdr_checksum = fw->data[fw->size - i + 5] << 8;
340  hdr_checksum |= fw->data[fw->size - i + 6] << 0;
341 
342  dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
343  "checksum=%04x\n", __func__, hdr_core, hdr_addr,
344  hdr_data_len, hdr_checksum);
345 
346  if (((hdr_core != 1) && (hdr_core != 2)) ||
347  (hdr_data_len > i)) {
348  dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
349  break;
350  }
351 
352  /* download begin packet */
353  req.cmd = CMD_FW_DL_BEGIN;
354  ret = af9035_ctrl_msg(d, &req);
355  if (ret < 0)
356  goto err;
357 
358  /* download firmware packet(s) */
359  for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
360  len = j;
361  if (len > MAX_DATA)
362  len = MAX_DATA;
363  req_fw_dl.wlen = len;
364  req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
365  HDR_SIZE + hdr_data_len - j];
366  ret = af9035_ctrl_msg(d, &req_fw_dl);
367  if (ret < 0)
368  goto err;
369  }
370 
371  /* download end packet */
372  req.cmd = CMD_FW_DL_END;
373  ret = af9035_ctrl_msg(d, &req);
374  if (ret < 0)
375  goto err;
376 
377  i -= hdr_data_len + HDR_SIZE;
378 
379  dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
380  __func__, fw->size - i);
381  }
382 
383  /* firmware loaded, request boot */
384  req.cmd = CMD_FW_BOOT;
385  ret = af9035_ctrl_msg(d, &req);
386  if (ret < 0)
387  goto err;
388 
389  /* ensure firmware starts */
390  wbuf[0] = 1;
391  ret = af9035_ctrl_msg(d, &req_fw_ver);
392  if (ret < 0)
393  goto err;
394 
395  if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
396  dev_err(&d->udev->dev, "%s: firmware did not run\n",
397  KBUILD_MODNAME);
398  ret = -ENODEV;
399  goto err;
400  }
401 
402  dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
403  KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
404 
405  return 0;
406 
407 err:
408  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
409 
410  return ret;
411 }
412 
413 static int af9035_download_firmware_it9135(struct dvb_usb_device *d,
414  const struct firmware *fw)
415 {
416  int ret, i, i_prev;
417  u8 wbuf[1];
418  u8 rbuf[4];
419  struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
420  struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
421  struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
422  #define HDR_SIZE 7
423 
424  /*
425  * There seems to be following firmware header. Meaning of bytes 0-3
426  * is unknown.
427  *
428  * 0: 3
429  * 1: 0, 1
430  * 2: 0
431  * 3: 1, 2, 3
432  * 4: addr MSB
433  * 5: addr LSB
434  * 6: count of data bytes ?
435  */
436 
437  for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
438  if (i == fw->size ||
439  (fw->data[i + 0] == 0x03 &&
440  (fw->data[i + 1] == 0x00 ||
441  fw->data[i + 1] == 0x01) &&
442  fw->data[i + 2] == 0x00)) {
443  req_fw_dl.wlen = i - i_prev;
444  req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
445  i_prev = i;
446  ret = af9035_ctrl_msg(d, &req_fw_dl);
447  if (ret < 0)
448  goto err;
449 
450  dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
451  __func__, i);
452  }
453  }
454 
455  /* firmware loaded, request boot */
456  req.cmd = CMD_FW_BOOT;
457  ret = af9035_ctrl_msg(d, &req);
458  if (ret < 0)
459  goto err;
460 
461  /* ensure firmware starts */
462  wbuf[0] = 1;
463  ret = af9035_ctrl_msg(d, &req_fw_ver);
464  if (ret < 0)
465  goto err;
466 
467  if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
468  dev_err(&d->udev->dev, "%s: firmware did not run\n",
469  KBUILD_MODNAME);
470  ret = -ENODEV;
471  goto err;
472  }
473 
474  dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
475  KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
476 
477  return 0;
478 
479 err:
480  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
481 
482  return ret;
483 }
484 
485 static int af9035_read_config(struct dvb_usb_device *d)
486 {
487  struct state *state = d_to_priv(d);
488  int ret, i, eeprom_shift = 0;
489  u8 tmp;
490  u16 tmp16;
491 
492  /* check if there is dual tuners */
493  ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
494  if (ret < 0)
495  goto err;
496 
497  state->dual_mode = tmp;
498  dev_dbg(&d->udev->dev, "%s: dual mode=%d\n",
499  __func__, state->dual_mode);
500 
501  for (i = 0; i < state->dual_mode + 1; i++) {
502  /* tuner */
503  ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp);
504  if (ret < 0)
505  goto err;
506 
507  state->af9033_config[i].tuner = tmp;
508  dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
509  __func__, i, tmp);
510 
511  switch (tmp) {
513  case AF9033_TUNER_FC0011:
516  case AF9033_TUNER_FC2580:
517  state->af9033_config[i].spec_inv = 1;
518  break;
519  default:
520  dev_warn(&d->udev->dev, "%s: tuner id=%02x not " \
521  "supported, please report!",
522  KBUILD_MODNAME, tmp);
523  }
524 
525  /* tuner IF frequency */
526  ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
527  if (ret < 0)
528  goto err;
529 
530  tmp16 = tmp;
531 
532  ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
533  if (ret < 0)
534  goto err;
535 
536  tmp16 |= tmp << 8;
537 
538  dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
539 
540  eeprom_shift = 0x10; /* shift for the 2nd tuner params */
541  }
542 
543  /* get demod clock */
544  ret = af9035_rd_reg(d, 0x00d800, &tmp);
545  if (ret < 0)
546  goto err;
547 
548  tmp = (tmp >> 0) & 0x0f;
549 
550  for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
551  state->af9033_config[i].clock = clock_lut[tmp];
552 
553  return 0;
554 
555 err:
556  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
557 
558  return ret;
559 }
560 
561 static int af9035_read_config_it9135(struct dvb_usb_device *d)
562 {
563  struct state *state = d_to_priv(d);
564  int ret, i;
565  u8 tmp;
566 
567  state->dual_mode = false;
568 
569  /* get demod clock */
570  ret = af9035_rd_reg(d, 0x00d800, &tmp);
571  if (ret < 0)
572  goto err;
573 
574  tmp = (tmp >> 0) & 0x0f;
575 
576  for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
577  state->af9033_config[i].clock = clock_lut_it9135[tmp];
578 
579  return 0;
580 
581 err:
582  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
583 
584  return ret;
585 }
586 
587 static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
588  int cmd, int arg)
589 {
590  int ret;
591  u8 val;
592 
593  dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
594 
595  /*
596  * CEN always enabled by hardware wiring
597  * RESETN GPIOT3
598  * RXEN GPIOT2
599  */
600 
601  switch (cmd) {
602  case TUA9001_CMD_RESETN:
603  if (arg)
604  val = 0x00;
605  else
606  val = 0x01;
607 
608  ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
609  if (ret < 0)
610  goto err;
611  break;
612  case TUA9001_CMD_RXEN:
613  if (arg)
614  val = 0x01;
615  else
616  val = 0x00;
617 
618  ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
619  if (ret < 0)
620  goto err;
621  break;
622  }
623 
624  return 0;
625 
626 err:
627  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
628 
629  return ret;
630 }
631 
632 
633 static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
634  int cmd, int arg)
635 {
636  int ret;
637 
638  switch (cmd) {
640  /* Tuner enable */
641  ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
642  if (ret < 0)
643  goto err;
644 
645  ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
646  if (ret < 0)
647  goto err;
648 
649  ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
650  if (ret < 0)
651  goto err;
652 
653  /* LED */
654  ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
655  if (ret < 0)
656  goto err;
657 
658  ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
659  if (ret < 0)
660  goto err;
661 
662  usleep_range(10000, 50000);
663  break;
665  ret = af9035_wr_reg(d, 0xd8e9, 1);
666  if (ret < 0)
667  goto err;
668 
669  ret = af9035_wr_reg(d, 0xd8e8, 1);
670  if (ret < 0)
671  goto err;
672 
673  ret = af9035_wr_reg(d, 0xd8e7, 1);
674  if (ret < 0)
675  goto err;
676 
677  usleep_range(10000, 20000);
678 
679  ret = af9035_wr_reg(d, 0xd8e7, 0);
680  if (ret < 0)
681  goto err;
682 
683  usleep_range(10000, 20000);
684  break;
685  default:
686  ret = -EINVAL;
687  goto err;
688  }
689 
690  return 0;
691 
692 err:
693  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
694 
695  return ret;
696 }
697 
698 static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
699 {
700  struct state *state = d_to_priv(d);
701 
702  switch (state->af9033_config[0].tuner) {
703  case AF9033_TUNER_FC0011:
704  return af9035_fc0011_tuner_callback(d, cmd, arg);
706  return af9035_tua9001_tuner_callback(d, cmd, arg);
707  default:
708  break;
709  }
710 
711  return 0;
712 }
713 
714 static int af9035_frontend_callback(void *adapter_priv, int component,
715  int cmd, int arg)
716 {
717  struct i2c_adapter *adap = adapter_priv;
718  struct dvb_usb_device *d = i2c_get_adapdata(adap);
719 
720  dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
721  __func__, component, cmd, arg);
722 
723  switch (component) {
725  return af9035_tuner_callback(d, cmd, arg);
726  default:
727  break;
728  }
729 
730  return 0;
731 }
732 
733 static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
734 {
735  struct state *state = adap_to_priv(adap);
736  struct dvb_usb_device *d = adap_to_d(adap);
737  int ret;
738 
739  if (!state->af9033_config[adap->id].tuner) {
740  /* unsupported tuner */
741  ret = -ENODEV;
742  goto err;
743  }
744 
745  if (adap->id == 0) {
746  state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
747  state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
748 
749  ret = af9035_wr_reg(d, 0x00417f,
750  state->af9033_config[1].i2c_addr);
751  if (ret < 0)
752  goto err;
753 
754  ret = af9035_wr_reg(d, 0x00d81a,
755  state->dual_mode);
756  if (ret < 0)
757  goto err;
758  }
759 
760  /* attach demodulator */
761  adap->fe[0] = dvb_attach(af9033_attach,
762  &state->af9033_config[adap->id], &d->i2c_adap);
763  if (adap->fe[0] == NULL) {
764  ret = -ENODEV;
765  goto err;
766  }
767 
768  /* disable I2C-gate */
769  adap->fe[0]->ops.i2c_gate_ctrl = NULL;
770  adap->fe[0]->callback = af9035_frontend_callback;
771 
772  return 0;
773 
774 err:
775  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
776 
777  return ret;
778 }
779 
780 static struct tua9001_config af9035_tua9001_config = {
781  .i2c_addr = 0x60,
782 };
783 
784 static const struct fc0011_config af9035_fc0011_config = {
785  .i2c_address = 0x60,
786 };
787 
788 static struct mxl5007t_config af9035_mxl5007t_config = {
789  .xtal_freq_hz = MxL_XTAL_24_MHZ,
790  .if_freq_hz = MxL_IF_4_57_MHZ,
791  .invert_if = 0,
792  .loop_thru_enable = 0,
793  .clk_out_enable = 0,
794  .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
795 };
796 
797 static struct tda18218_config af9035_tda18218_config = {
798  .i2c_address = 0x60,
799  .i2c_wr_max = 21,
800 };
801 
802 static const struct fc2580_config af9035_fc2580_config = {
803  .i2c_addr = 0x56,
804  .clock = 16384000,
805 };
806 
807 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
808 {
809  struct state *state = adap_to_priv(adap);
810  struct dvb_usb_device *d = adap_to_d(adap);
811  int ret;
812  struct dvb_frontend *fe;
813 
814  switch (state->af9033_config[adap->id].tuner) {
816  /* AF9035 gpiot3 = TUA9001 RESETN
817  AF9035 gpiot2 = TUA9001 RXEN */
818 
819  /* configure gpiot2 and gpiot2 as output */
820  ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
821  if (ret < 0)
822  goto err;
823 
824  ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
825  if (ret < 0)
826  goto err;
827 
828  ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
829  if (ret < 0)
830  goto err;
831 
832  ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
833  if (ret < 0)
834  goto err;
835 
836  /* attach tuner */
837  fe = dvb_attach(tua9001_attach, adap->fe[0],
838  &d->i2c_adap, &af9035_tua9001_config);
839  break;
840  case AF9033_TUNER_FC0011:
841  fe = dvb_attach(fc0011_attach, adap->fe[0],
842  &d->i2c_adap, &af9035_fc0011_config);
843  break;
845  ret = af9035_wr_reg(d, 0x00d8e0, 1);
846  if (ret < 0)
847  goto err;
848  ret = af9035_wr_reg(d, 0x00d8e1, 1);
849  if (ret < 0)
850  goto err;
851  ret = af9035_wr_reg(d, 0x00d8df, 0);
852  if (ret < 0)
853  goto err;
854 
855  msleep(30);
856 
857  ret = af9035_wr_reg(d, 0x00d8df, 1);
858  if (ret < 0)
859  goto err;
860 
861  msleep(300);
862 
863  ret = af9035_wr_reg(d, 0x00d8c0, 1);
864  if (ret < 0)
865  goto err;
866  ret = af9035_wr_reg(d, 0x00d8c1, 1);
867  if (ret < 0)
868  goto err;
869  ret = af9035_wr_reg(d, 0x00d8bf, 0);
870  if (ret < 0)
871  goto err;
872  ret = af9035_wr_reg(d, 0x00d8b4, 1);
873  if (ret < 0)
874  goto err;
875  ret = af9035_wr_reg(d, 0x00d8b5, 1);
876  if (ret < 0)
877  goto err;
878  ret = af9035_wr_reg(d, 0x00d8b3, 1);
879  if (ret < 0)
880  goto err;
881 
882  /* attach tuner */
883  fe = dvb_attach(mxl5007t_attach, adap->fe[0],
884  &d->i2c_adap, 0x60, &af9035_mxl5007t_config);
885  break;
887  /* attach tuner */
888  fe = dvb_attach(tda18218_attach, adap->fe[0],
889  &d->i2c_adap, &af9035_tda18218_config);
890  break;
891  case AF9033_TUNER_FC2580:
892  /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
893  ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
894  if (ret < 0)
895  goto err;
896 
897  ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
898  if (ret < 0)
899  goto err;
900 
901  ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
902  if (ret < 0)
903  goto err;
904 
905  usleep_range(10000, 50000);
906  /* attach tuner */
907  fe = dvb_attach(fc2580_attach, adap->fe[0],
908  &d->i2c_adap, &af9035_fc2580_config);
909  break;
910  default:
911  fe = NULL;
912  }
913 
914  if (fe == NULL) {
915  ret = -ENODEV;
916  goto err;
917  }
918 
919  return 0;
920 
921 err:
922  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
923 
924  return ret;
925 }
926 
927 static int af9035_init(struct dvb_usb_device *d)
928 {
929  struct state *state = d_to_priv(d);
930  int ret, i;
931  u16 frame_size = 87 * 188 / 4;
932  u8 packet_size = 512 / 4;
933  struct reg_val_mask tab[] = {
934  { 0x80f99d, 0x01, 0x01 },
935  { 0x80f9a4, 0x01, 0x01 },
936  { 0x00dd11, 0x00, 0x20 },
937  { 0x00dd11, 0x00, 0x40 },
938  { 0x00dd13, 0x00, 0x20 },
939  { 0x00dd13, 0x00, 0x40 },
940  { 0x00dd11, 0x20, 0x20 },
941  { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
942  { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
943  { 0x00dd0c, packet_size, 0xff},
944  { 0x00dd11, state->dual_mode << 6, 0x40 },
945  { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
946  { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
947  { 0x00dd0d, packet_size, 0xff },
948  { 0x80f9a3, 0x00, 0x01 },
949  { 0x80f9cd, 0x00, 0x01 },
950  { 0x80f99d, 0x00, 0x01 },
951  { 0x80f9a4, 0x00, 0x01 },
952  };
953 
954  dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
955  "packet_size=%02x\n", __func__,
956  d->udev->speed, frame_size, packet_size);
957 
958  /* init endpoints */
959  for (i = 0; i < ARRAY_SIZE(tab); i++) {
960  ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
961  tab[i].mask);
962  if (ret < 0)
963  goto err;
964  }
965 
966  return 0;
967 
968 err:
969  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
970 
971  return ret;
972 }
973 
974 static int af9035_rc_query(struct dvb_usb_device *d)
975 {
976  unsigned int key;
977  unsigned char b[4];
978  int ret;
979  struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b };
980 
981  ret = af9035_ctrl_msg(d, &req);
982  if (ret < 0)
983  goto err;
984 
985  if ((b[2] + b[3]) == 0xff) {
986  if ((b[0] + b[1]) == 0xff) {
987  /* NEC */
988  key = b[0] << 8 | b[2];
989  } else {
990  /* ext. NEC */
991  key = b[0] << 16 | b[1] << 8 | b[2];
992  }
993  } else {
994  key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3];
995  }
996 
997  rc_keydown(d->rc_dev, key, 0);
998 
999 err:
1000  /* ignore errors */
1001  return 0;
1002 }
1003 
1004 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1005 {
1006  int ret;
1007  u8 tmp;
1008 
1009  ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp);
1010  if (ret < 0)
1011  goto err;
1012 
1013  dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1014 
1015  /* don't activate rc if in HID mode or if not available */
1016  if (tmp == 5) {
1017  ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp);
1018  if (ret < 0)
1019  goto err;
1020 
1021  dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1022 
1023  switch (tmp) {
1024  case 0: /* NEC */
1025  default:
1027  break;
1028  case 1: /* RC6 */
1030  break;
1031  }
1032 
1033  rc->query = af9035_rc_query;
1034  rc->interval = 500;
1035 
1036  /* load empty to enable rc */
1037  if (!rc->map_name)
1038  rc->map_name = RC_MAP_EMPTY;
1039  }
1040 
1041  return 0;
1042 
1043 err:
1044  dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1045 
1046  return ret;
1047 }
1048 
1049 /* interface 0 is used by DVB-T receiver and
1050  interface 1 is for remote controller (HID) */
1051 static const struct dvb_usb_device_properties af9035_props = {
1052  .driver_name = KBUILD_MODNAME,
1053  .owner = THIS_MODULE,
1054  .adapter_nr = adapter_nr,
1055  .size_of_priv = sizeof(struct state),
1056 
1057  .generic_bulk_ctrl_endpoint = 0x02,
1058  .generic_bulk_ctrl_endpoint_response = 0x81,
1059 
1060  .identify_state = af9035_identify_state,
1061  .firmware = AF9035_FIRMWARE_AF9035,
1062  .download_firmware = af9035_download_firmware,
1063 
1064  .i2c_algo = &af9035_i2c_algo,
1065  .read_config = af9035_read_config,
1066  .frontend_attach = af9035_frontend_attach,
1067  .tuner_attach = af9035_tuner_attach,
1068  .init = af9035_init,
1069  .get_rc_config = af9035_get_rc_config,
1070 
1071  .num_adapters = 1,
1072  .adapter = {
1073  {
1074  .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1075  }, {
1076  .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1077  },
1078  },
1079 };
1080 
1081 static const struct dvb_usb_device_properties it9135_props = {
1082  .driver_name = KBUILD_MODNAME,
1083  .owner = THIS_MODULE,
1084  .adapter_nr = adapter_nr,
1085  .size_of_priv = sizeof(struct state),
1086 
1087  .generic_bulk_ctrl_endpoint = 0x02,
1088  .generic_bulk_ctrl_endpoint_response = 0x81,
1089 
1090  .identify_state = af9035_identify_state,
1091  .firmware = AF9035_FIRMWARE_IT9135,
1092  .download_firmware = af9035_download_firmware_it9135,
1093 
1094  .i2c_algo = &af9035_i2c_algo,
1095  .read_config = af9035_read_config_it9135,
1096  .frontend_attach = af9035_frontend_attach,
1097  .tuner_attach = af9035_tuner_attach,
1098  .init = af9035_init,
1099  .get_rc_config = af9035_get_rc_config,
1100 
1101  .num_adapters = 1,
1102  .adapter = {
1103  {
1104  .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1105  }, {
1106  .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1107  },
1108  },
1109 };
1110 
1111 static const struct usb_device_id af9035_id_table[] = {
1113  &af9035_props, "Afatech AF9035 reference design", NULL) },
1115  &af9035_props, "Afatech AF9035 reference design", NULL) },
1117  &af9035_props, "Afatech AF9035 reference design", NULL) },
1119  &af9035_props, "Afatech AF9035 reference design", NULL) },
1121  &af9035_props, "Afatech AF9035 reference design", NULL) },
1123  &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1125  &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1127  &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1129  &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1131  &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1133  &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
1135  &af9035_props, "Asus U3100Mini Plus", NULL) },
1136  { }
1137 };
1138 MODULE_DEVICE_TABLE(usb, af9035_id_table);
1139 
1140 static struct usb_driver af9035_usb_driver = {
1141  .name = KBUILD_MODNAME,
1142  .id_table = af9035_id_table,
1143  .probe = dvb_usbv2_probe,
1144  .disconnect = dvb_usbv2_disconnect,
1145  .suspend = dvb_usbv2_suspend,
1146  .resume = dvb_usbv2_resume,
1147  .reset_resume = dvb_usbv2_reset_resume,
1148  .no_dynamic_id = 1,
1149  .soft_unbind = 1,
1150 };
1151 
1152 module_usb_driver(af9035_usb_driver);
1153 
1154 MODULE_AUTHOR("Antti Palosaari <[email protected]>");
1155 MODULE_DESCRIPTION("Afatech AF9035 driver");
1156 MODULE_LICENSE("GPL");