blob: 7acc175d805a198f89a2f6ab087f702e79f0f4ca [file] [log] [blame]
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001/* tuner-xc2028
2 *
3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03004 *
Michel Ludwig701672e2007-07-18 10:29:10 -03005 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03007 *
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03008 * This code is placed under the terms of the GNU General Public License v2
9 */
10
11#include <linux/i2c.h>
12#include <asm/div64.h>
13#include <linux/firmware.h>
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -030014#include <linux/videodev2.h>
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030015#include <linux/delay.h>
Michel Ludwig701672e2007-07-18 10:29:10 -030016#include <media/tuner.h>
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -030017#include <linux/mutex.h>
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030018#include "tuner-i2c.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030019#include "tuner-xc2028.h"
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030020#include "tuner-xc2028-types.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030021
Michel Ludwig701672e2007-07-18 10:29:10 -030022#include <linux/dvb/frontend.h>
23#include "dvb_frontend.h"
24
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -030025
Hans Verkuil9dd659d2007-11-04 11:03:36 -030026#define PREFIX "xc2028"
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030027
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030028static int debug;
29module_param(debug, int, 0644);
30MODULE_PARM_DESC(debug, "enable verbose debug messages");
31
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -030032static char audio_std[8];
33module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
34MODULE_PARM_DESC(audio_std,
35 "Audio standard. XC3028 audio decoder explicitly "
36 "needs to know what audio\n"
37 "standard is needed for some video standards with audio A2 or NICAM.\n"
38 "The valid values are:\n"
39 "A2\n"
40 "A2/A\n"
41 "A2/B\n"
42 "NICAM\n"
43 "NICAM/A\n"
44 "NICAM/B\n");
45
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030046static LIST_HEAD(xc2028_list);
Chris Pascoeaa501be2007-11-19 04:45:38 -030047static DEFINE_MUTEX(xc2028_list_mutex);
48
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030049/* struct for storing firmware table */
50struct firmware_description {
51 unsigned int type;
52 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030053 __u16 int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030054 unsigned char *ptr;
55 unsigned int size;
56};
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030057
Chris Pascoee0f0b372007-11-19 11:22:03 -030058struct firmware_properties {
59 unsigned int type;
60 v4l2_std_id id;
61 v4l2_std_id std_req;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030062 __u16 int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -030063 unsigned int scode_table;
64 int scode_nr;
65};
66
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030067struct xc2028_data {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030068 struct list_head xc2028_list;
69 struct tuner_i2c_props i2c_props;
70 int (*tuner_callback) (void *dev,
71 int command, int arg);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030072 void *video_dev;
73 int count;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030074 __u32 frequency;
75
76 struct firmware_description *firm;
77 int firm_size;
Chris Pascoe06fd82d2007-11-19 06:06:08 -030078 __u16 firm_version;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030079
Chris Pascoe8bf799a2007-11-19 11:35:45 -030080 __u16 hwmodel;
81 __u16 hwvers;
82
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030083 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030084
Chris Pascoee0f0b372007-11-19 11:22:03 -030085 struct firmware_properties cur_fw;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030086
87 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030088};
89
Chris Pascoe47cc5b72007-11-19 04:14:23 -030090#define i2c_send(priv, buf, size) ({ \
91 int _rc; \
92 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
93 if (size != _rc) \
94 tuner_info("i2c output error: rc = %d (should be %d)\n",\
95 _rc, (int)size); \
96 _rc; \
97})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030098
Chris Pascoe47cc5b72007-11-19 04:14:23 -030099#define i2c_rcv(priv, buf, size) ({ \
100 int _rc; \
101 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
102 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300103 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300104 _rc, (int)size); \
105 _rc; \
106})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300107
Chris Pascoe7d58d112007-11-19 04:31:58 -0300108#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
109 int _rc; \
110 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
111 ibuf, isize); \
112 if (isize != _rc) \
113 tuner_err("i2c input error: rc = %d (should be %d)\n", \
114 _rc, (int)isize); \
115 _rc; \
116})
117
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300118#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300119 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300120 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300121 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300122 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300123 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300124 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
125 } else \
126 msleep(10); \
127 _rc; \
128})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300129
Chris Pascoe7d58d112007-11-19 04:31:58 -0300130static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300131{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300132 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300133 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300134
Chris Pascoe7d58d112007-11-19 04:31:58 -0300135 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300136
Chris Pascoe7d58d112007-11-19 04:31:58 -0300137 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300138 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300139
Chris Pascoe7d58d112007-11-19 04:31:58 -0300140 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
141 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300142
Chris Pascoe7d58d112007-11-19 04:31:58 -0300143 *val = (ibuf[1]) | (ibuf[0] << 8);
144 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300145}
146
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300147void dump_firm_type(unsigned int type)
148{
149 if (type & BASE)
150 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300151 if (type & INIT1)
152 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300153 if (type & F8MHZ)
154 printk("F8MHZ ");
155 if (type & MTS)
156 printk("MTS ");
157 if (type & D2620)
158 printk("D2620 ");
159 if (type & D2633)
160 printk("D2633 ");
161 if (type & DTV6)
162 printk("DTV6 ");
163 if (type & QAM)
164 printk("QAM ");
165 if (type & DTV7)
166 printk("DTV7 ");
167 if (type & DTV78)
168 printk("DTV78 ");
169 if (type & DTV8)
170 printk("DTV8 ");
171 if (type & FM)
172 printk("FM ");
173 if (type & INPUT1)
174 printk("INPUT1 ");
175 if (type & LCD)
176 printk("LCD ");
177 if (type & NOGD)
178 printk("NOGD ");
179 if (type & MONO)
180 printk("MONO ");
181 if (type & ATSC)
182 printk("ATSC ");
183 if (type & IF)
184 printk("IF ");
185 if (type & LG60)
186 printk("LG60 ");
187 if (type & ATI638)
188 printk("ATI638 ");
189 if (type & OREN538)
190 printk("OREN538 ");
191 if (type & OREN36)
192 printk("OREN36 ");
193 if (type & TOYOTA388)
194 printk("TOYOTA388 ");
195 if (type & TOYOTA794)
196 printk("TOYOTA794 ");
197 if (type & DIBCOM52)
198 printk("DIBCOM52 ");
199 if (type & ZARLINK456)
200 printk("ZARLINK456 ");
201 if (type & CHINA)
202 printk("CHINA ");
203 if (type & F6MHZ)
204 printk("F6MHZ ");
205 if (type & INPUT2)
206 printk("INPUT2 ");
207 if (type & SCODE)
208 printk("SCODE ");
209}
210
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300211static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300212{
Chris Pascoee155d902007-11-19 04:16:47 -0300213 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300214 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300215 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300216 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300217 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300218 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300219 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300220 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300221 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300222 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300223 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300224 return V4L2_STD_NICAM_B;
225
226 return 0;
227}
228
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300229static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300230{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300231 int i;
232
233 if (!priv->firm)
234 return;
235
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300236 for (i = 0; i < priv->firm_size; i++)
237 kfree(priv->firm[i].ptr);
238
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300239 kfree(priv->firm);
240
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300241 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300242 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300243
244 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300245}
246
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300247static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300248{
249 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300250 const struct firmware *fw = NULL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300251 unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300252 int rc = 0;
253 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300254 char name[33];
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300255
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300256 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300257
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300258 tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300259 rc = request_firmware(&fw, priv->ctrl.fname,
260 &priv->i2c_props.adap->dev);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300261 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300262 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300263 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300264 priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300265 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300266 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300267 rc, priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300268
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300269 return rc;
270 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300271 p = fw->data;
272 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300273
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300274 if (fw->size < sizeof(name) - 1 + 2 + 2) {
275 tuner_err("Error: firmware file %s has invalid size!\n",
276 priv->ctrl.fname);
277 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300278 }
279
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300280 memcpy(name, p, sizeof(name) - 1);
281 name[sizeof(name) - 1] = 0;
282 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300283
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300284 priv->firm_version = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300285 p += 2;
286
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300287 n_array = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300288 p += 2;
289
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300290 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
291 n_array, priv->ctrl.fname, name,
292 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300293
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300294 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300295 if (priv->firm == NULL) {
296 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300297 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300298 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300299 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300300 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300301
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300302 n = -1;
303 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300304 __u32 type, size;
305 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300306 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300307
308 n++;
309 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300310 tuner_err("More firmware images in file than "
311 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300312 goto corrupt;
313 }
314
315 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300316 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300317 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300318 goto corrupt;
319 }
320
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300321 type = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300322 p += sizeof(type);
323
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300324 id = le64_to_cpu(*(v4l2_std_id *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300325 p += sizeof(id);
326
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300327 if (type & HAS_IF) {
328 int_freq = le16_to_cpu(*(__u16 *) p);
329 p += sizeof(int_freq);
330 }
331
Michel Ludwig2fc580f2007-11-16 07:19:35 -0300332 size = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300333 p += sizeof(size);
334
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300335 if ((!size) || (size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300336 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300337 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300338 printk("(%x), id %llx is corrupted "
339 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300340 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300341 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300342 goto corrupt;
343 }
344
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300345 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300346 if (priv->firm[n].ptr == NULL) {
347 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300348 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300349 goto err;
350 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300351 tuner_dbg("Reading firmware type ");
352 if (debug) {
353 dump_firm_type(type);
354 printk("(%x), id %llx, size=%d.\n",
355 type, (unsigned long long)id, size);
356 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300357
358 memcpy(priv->firm[n].ptr, p, size);
359 priv->firm[n].type = type;
360 priv->firm[n].id = id;
361 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300362 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300363
364 p += size;
365 }
366
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300367 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300368 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300369 goto corrupt;
370 }
371
372 goto done;
373
374corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300375 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300376 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300377
378err:
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300379 tuner_info("Releasing partially loaded firmware file.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300380 free_firmware(priv);
381
382done:
383 release_firmware(fw);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300384 if (rc == 0)
385 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300386
387 return rc;
388}
389
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300390static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
391 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300392{
393 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoeb1535292007-11-19 10:04:06 -0300394 int i, best_i = -1, best_nr_matches = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300395
Chris Pascoeb1535292007-11-19 10:04:06 -0300396 tuner_dbg("%s called, want type=", __FUNCTION__);
397 if (debug) {
398 dump_firm_type(type);
399 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
400 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300401
402 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300403 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300404 return -EINVAL;
405 }
406
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300407 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300408 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300409
Chris Pascoee0f0b372007-11-19 11:22:03 -0300410 if (type & BASE)
411 type &= BASE_TYPES;
412 else if (type & SCODE)
413 type &= SCODE_TYPES;
414 else if (type & DTV_TYPES)
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300415 type &= DTV_TYPES;
416 else if (type & STD_SPECIFIC_TYPES)
417 type &= STD_SPECIFIC_TYPES;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300418
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300419 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300420 for (i = 0; i < priv->firm_size; i++) {
421 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300422 goto found;
423 }
424
425 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300426 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300427 v4l2_std_id match_mask;
428 int nr_matches;
429
430 if (type != priv->firm[i].type)
431 continue;
432
433 match_mask = *id & priv->firm[i].id;
434 if (!match_mask)
435 continue;
436
437 if ((*id & match_mask) == *id)
438 goto found; /* Supports all the requested standards */
439
440 nr_matches = hweight64(match_mask);
441 if (nr_matches > best_nr_matches) {
442 best_nr_matches = nr_matches;
443 best_i = i;
444 }
445 }
446
447 if (best_nr_matches > 0) {
448 tuner_dbg("Selecting best matching firmware (%d bits) for "
449 "type=", best_nr_matches);
450 dump_firm_type(type);
451 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
452 i = best_i;
453 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300454 }
455
456 /*FIXME: Would make sense to seek for type "hint" match ? */
457
Chris Pascoeb1535292007-11-19 10:04:06 -0300458 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300459 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300460
461found:
462 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300463
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300464ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300465 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300466 if (debug) {
467 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300468 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300469 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300470 return i;
471}
472
473static int load_firmware(struct dvb_frontend *fe, unsigned int type,
474 v4l2_std_id *id)
475{
476 struct xc2028_data *priv = fe->tuner_priv;
477 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300478 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300479
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300480 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300481
482 pos = seek_firmware(fe, type, id);
483 if (pos < 0)
484 return pos;
485
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300486 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300487 dump_firm_type(priv->firm[pos].type);
488 printk("(%x), id %016llx.\n", priv->firm[pos].type,
489 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300490
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300491 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300492 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300493
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300494 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300495 __u16 size;
496
497 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300498 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300499 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300500 return -EINVAL;
501 }
502
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300503 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300504 p += sizeof(size);
505
506 if (size == 0xffff)
507 return 0;
508
509 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300510 /* Special callback command received */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300511 rc = priv->tuner_callback(priv->video_dev,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300512 XC2028_TUNER_RESET, 0);
513 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300514 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300515 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300516 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300517 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300518 continue;
519 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300520 if (size >= 0xff00) {
521 switch (size) {
522 case 0xff00:
523 rc = priv->tuner_callback(priv->video_dev,
524 XC2028_RESET_CLK, 0);
525 if (rc < 0) {
526 tuner_err("Error at RESET code %d\n",
527 (*p) & 0x7f);
528 return -EINVAL;
529 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300530 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300531 default:
532 tuner_info("Invalid RESET code %d\n",
533 size & 0x7f);
534 return -EINVAL;
535
536 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300537 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300538 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300539
540 /* Checks for a sleep command */
541 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300542 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300543 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300544 }
545
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300546 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300547 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300548 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300549 return -EINVAL;
550 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300551
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300552 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300553 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300554 size--;
555
556 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300557 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300558 int len = (size < priv->ctrl.max_len - 1) ?
559 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300560
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300561 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300562
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300563 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300564 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300565 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300566 return -EINVAL;
567 }
568
569 p += len;
570 size -= len;
571 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300572 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300573 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300574}
575
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300576static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300577 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300578{
579 struct xc2028_data *priv = fe->tuner_priv;
580 int pos, rc;
581 unsigned char *p;
582
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300583 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300584
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300585 if (!int_freq) {
586 pos = seek_firmware(fe, type, id);
587 if (pos < 0)
588 return pos;
589 } else {
590 for (pos = 0; pos < priv->firm_size; pos++) {
591 if ((priv->firm[pos].int_freq == int_freq) &&
592 (type & HAS_IF))
593 break;
594 }
595 if (pos == priv->firm_size)
596 return -ENOENT;
597 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300598
599 p = priv->firm[pos].ptr;
600
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300601 if (type & HAS_IF) {
602 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
603 return -EINVAL;
604 p += 12 * scode;
605 } else {
606 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
607 * has a 2-byte size header in the firmware format. */
608 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
609 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
610 return -EINVAL;
611 p += 14 * scode + 2;
612 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300613
Chris Pascoed7b22c52007-11-19 10:12:45 -0300614 tuner_info("Loading SCODE for type=");
615 dump_firm_type(priv->firm[pos].type);
616 printk("(%x), id %016llx.\n", priv->firm[pos].type,
617 (unsigned long long)*id);
618
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300619 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300620 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
621 else
622 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
623 if (rc < 0)
624 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300625
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300626 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300627 if (rc < 0)
628 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300629
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300630 rc = send_seq(priv, {0x00, 0x8c});
631 if (rc < 0)
632 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300633
634 return 0;
635}
636
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300637static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300638 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300639{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300640 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300641 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300642 int rc = 0, is_retry = 0;
643 u16 version, hwmodel;
644 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300645
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300646 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300647
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300648 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300649 if (!priv->ctrl.fname) {
650 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300651 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300652 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300653
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300654 rc = load_all_firmwares(fe);
655 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300656 return rc;
657 }
658
Mauro Carvalho Chehab5add9a62007-11-22 12:08:53 -0300659 if (priv->ctrl.mts)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300660 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300661
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300662retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300663 new_fw.type = type;
664 new_fw.id = std;
665 new_fw.std_req = std;
666 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
667 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300668 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300669
670 tuner_dbg("checking firmware, user requested type=");
671 if (debug) {
672 dump_firm_type(new_fw.type);
673 printk("(%x), id %016llx, scode_tbl ", new_fw.type,
674 (unsigned long long)new_fw.std_req);
675 dump_firm_type(priv->ctrl.scode_table);
676 printk("(%x), scode_nr %d\n", priv->ctrl.scode_table,
677 new_fw.scode_nr);
678 }
679
680 /* No need to reload base firmware if it matches */
681 if (((BASE | new_fw.type) & BASE_TYPES) ==
682 (priv->cur_fw.type & BASE_TYPES)) {
683 tuner_dbg("BASE firmware not changed.\n");
684 goto skip_base;
685 }
686
687 /* Updating BASE - forget about all currently loaded firmware */
688 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
689
690 /* Reset is needed before loading firmware */
691 rc = priv->tuner_callback(priv->video_dev,
692 XC2028_TUNER_RESET, 0);
693 if (rc < 0)
694 goto fail;
695
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300696 /* BASE firmwares are all std0 */
697 std0 = 0;
698 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300699 if (rc < 0) {
700 tuner_err("Error %d while loading base firmware\n",
701 rc);
702 goto fail;
703 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300704
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300705 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300706 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300707
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300708 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300709 if (rc == -ENOENT)
710 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
711 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300712 if (rc < 0 && rc != -ENOENT) {
713 tuner_err("Error %d while loading init1 firmware\n",
714 rc);
715 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300716 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300717
Chris Pascoee0f0b372007-11-19 11:22:03 -0300718skip_base:
719 /*
720 * No need to reload standard specific firmware if base firmware
721 * was not reloaded and requested video standards have not changed.
722 */
723 if (priv->cur_fw.type == (BASE | new_fw.type) &&
724 priv->cur_fw.std_req == std) {
725 tuner_dbg("Std-specific firmware already loaded.\n");
726 goto skip_std_specific;
727 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300728
Chris Pascoee0f0b372007-11-19 11:22:03 -0300729 /* Reloading std-specific firmware forces a SCODE update */
730 priv->cur_fw.scode_table = 0;
731
Chris Pascoee0f0b372007-11-19 11:22:03 -0300732 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300733 if (rc == -ENOENT)
734 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
735
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300736 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300737 goto fail;
738
739skip_std_specific:
740 if (priv->cur_fw.scode_table == new_fw.scode_table &&
741 priv->cur_fw.scode_nr == new_fw.scode_nr) {
742 tuner_dbg("SCODE firmware already loaded.\n");
743 goto check_device;
744 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300745
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300746 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300747 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300748
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300749 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
750 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300751
Chris Pascoee0f0b372007-11-19 11:22:03 -0300752check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300753 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
754 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
755 tuner_err("Unable to read tuner registers.\n");
756 goto fail;
757 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300758
759 tuner_info("Device is Xceive %d version %d.%d, "
760 "firmware version %d.%d\n",
761 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
762 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300763
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300764 /* Check firmware version against what we downloaded. */
765 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
766 tuner_err("Incorrect readback of firmware version.\n");
767 goto fail;
768 }
769
770 /* Check that the tuner hardware model remains consistent over time. */
771 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
772 priv->hwmodel = hwmodel;
773 priv->hwvers = version & 0xff00;
774 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
775 priv->hwvers != (version & 0xff00)) {
776 tuner_err("Read invalid device hardware information - tuner "
777 "hung?\n");
778 goto fail;
779 }
780
Chris Pascoee0f0b372007-11-19 11:22:03 -0300781 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
782
783 /*
784 * By setting BASE in cur_fw.type only after successfully loading all
785 * firmwares, we can:
786 * 1. Identify that BASE firmware with type=0 has been loaded;
787 * 2. Tell whether BASE firmware was just changed the next time through.
788 */
789 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300790
791 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300792
793fail:
794 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300795 if (!is_retry) {
796 msleep(50);
797 is_retry = 1;
798 tuner_dbg("Retrying firmware load\n");
799 goto retry;
800 }
801
Chris Pascoee0f0b372007-11-19 11:22:03 -0300802 if (rc == -ENOENT)
803 rc = -EINVAL;
804 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300805}
806
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300807static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300808{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300809 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300810 u16 frq_lock, signal = 0;
811 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300812
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300813 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300814
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300815 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300816
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300817 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300818 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
819 if (rc < 0 || frq_lock == 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300820 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300821
822 /* Frequency is locked. Return signal quality */
823
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300824 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300825 rc = xc2028_get_reg(priv, 0x0040, &signal);
826 if (rc < 0)
827 signal = -frq_lock;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300828
829ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300830 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300831
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300832 *strength = signal;
833
Chris Pascoe7d58d112007-11-19 04:31:58 -0300834 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300835}
836
837#define DIV 15625
838
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300839static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300840 enum tuner_mode new_mode,
841 unsigned int type,
842 v4l2_std_id std,
843 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300844{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300845 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300846 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300847 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300848 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300849
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300850 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300851
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300852 mutex_lock(&priv->lock);
853
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300854 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300855
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300856 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300857 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300858
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300859 /* On some cases xc2028 can disable video output, if
860 * very weak signals are received. By sending a soft
861 * reset, this is re-enabled. So, it is better to always
862 * send a soft reset before changing channels, to be sure
863 * that xc2028 will be in a safe state.
864 * Maybe this might also be needed for DTV.
865 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300866 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300867 rc = send_seq(priv, {0x00, 0x00});
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300868 } else {
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300869 offset = 2750000;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300870 if (priv->cur_fw.type & DTV7)
Chris Pascoea44f1c42007-11-19 06:35:26 -0300871 offset -= 500000;
872 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300873
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300874 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300875
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300876 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300877 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300878 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
879 else
880 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
881 if (rc < 0)
882 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300883
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300884 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300885 if (rc < 0)
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300886 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300887
888 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300889
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300890 buf[0] = 0xff & (div >> 24);
891 buf[1] = 0xff & (div >> 16);
892 buf[2] = 0xff & (div >> 8);
893 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300894
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300895 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300896 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300897 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300898 msleep(100);
899
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300900 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300901
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300902 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
903 buf[0], buf[1], buf[2], buf[3],
904 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300905
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300906 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300907
908ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300909 mutex_unlock(&priv->lock);
910
911 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300912}
913
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300914static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300915 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300916{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300917 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300918 unsigned int type=0;
919
920 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -0300921
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300922 if (p->mode == V4L2_TUNER_RADIO) {
923 type |= FM;
924 if (priv->ctrl.input1)
925 type |= INPUT1;
926 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300927 T_ANALOG_TV, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300928 }
929
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -0300930 /* if std is not defined, choose one */
931 if (!p->std)
932 p->std = V4L2_STD_MN;
933
934 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300935 if (!(p->std & V4L2_STD_MN))
936 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -0300937
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300938 /* Add audio hack to std mask */
939 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300940
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300941 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300942 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -0300943}
944
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300945static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -0300946 struct dvb_frontend_parameters *p)
947{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300948 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300949 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300950 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -0300951
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300952 tuner_dbg("%s called\n", __FUNCTION__);
Michel Ludwig701672e2007-07-18 10:29:10 -0300953
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300954 if (priv->ctrl.d2633)
955 type |= D2633;
956 else
957 type |= D2620;
958
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300959 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300960 case FE_OFDM:
961 bw = p->u.ofdm.bandwidth;
962 break;
963 case FE_QAM:
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -0300964 tuner_info("WARN: There are some reports that "
965 "QAM 6 MHz doesn't work.\n"
966 "If this works for you, please report by "
967 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300968 bw = BANDWIDTH_6_MHZ;
969 type |= QAM;
970 break;
971 case FE_ATSC:
972 bw = BANDWIDTH_6_MHZ;
973 type |= ATSC| D2633;
974 break;
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -0300975 /* DVB-S is not supported */
976 default:
977 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300978 }
979
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300980 /* FIXME:
981 There are two Scodes that will never be selected:
982 DTV78 ZARLINK456, DTV78 DIBCOM52
983 When it should opt for DTV78 instead of DTV7 or DTV8?
984 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300985 switch (bw) {
986 case BANDWIDTH_8_MHZ:
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300987 type |= DTV8 | F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300988 break;
989 case BANDWIDTH_7_MHZ:
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300990 type |= DTV7 | F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300991 break;
992 case BANDWIDTH_6_MHZ:
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300993 type |= DTV6 ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300994 break;
995 default:
996 tuner_err("error: bandwidth not supported.\n");
997 };
998
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300999 /* All S-code tables need a 200kHz shift */
1000 if (priv->ctrl.demod)
1001 priv->ctrl.demod += 200;
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001002
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001003 return generic_set_freq(fe, p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001004 T_DIGITAL_TV, type, 0, priv->ctrl.demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001005}
1006
Chris Pascoe45819c32007-11-19 11:41:20 -03001007static int xc2028_sleep(struct dvb_frontend *fe)
1008{
1009 struct xc2028_data *priv = fe->tuner_priv;
1010 int rc = 0;
1011
1012 tuner_dbg("%s called\n", __FUNCTION__);
1013
1014 mutex_lock(&priv->lock);
1015
1016 if (priv->firm_version < 0x0202)
1017 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1018 else
1019 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1020
1021 priv->cur_fw.type = 0; /* need firmware reload */
1022
1023 mutex_unlock(&priv->lock);
1024
1025 return rc;
1026}
1027
1028
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001029static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001030{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001031 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001032
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001033 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001034
Chris Pascoeaa501be2007-11-19 04:45:38 -03001035 mutex_lock(&xc2028_list_mutex);
1036
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001037 priv->count--;
1038
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001039 if (!priv->count) {
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001040 list_del(&priv->xc2028_list);
1041
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001042 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001043
1044 free_firmware(priv);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001045 kfree(priv);
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001046 fe->tuner_priv = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001047 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001048
Chris Pascoeaa501be2007-11-19 04:45:38 -03001049 mutex_unlock(&xc2028_list_mutex);
1050
Michel Ludwig701672e2007-07-18 10:29:10 -03001051 return 0;
1052}
1053
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001054static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001055{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001056 struct xc2028_data *priv = fe->tuner_priv;
1057
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001058 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001059
1060 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001061
1062 return 0;
1063}
1064
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001065static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001066{
1067 struct xc2028_data *priv = fe->tuner_priv;
1068 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001069 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001070
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001071 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001072
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001073 mutex_lock(&priv->lock);
1074
Chris Pascoe0a196b62007-11-19 09:29:59 -03001075 kfree(priv->ctrl.fname);
1076 free_firmware(priv);
1077
1078 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1079 priv->ctrl.fname = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001080
1081 if (p->fname) {
Chris Pascoe0a196b62007-11-19 09:29:59 -03001082 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1083 if (priv->ctrl.fname == NULL)
1084 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001085 }
1086
Chris Pascoe0a196b62007-11-19 09:29:59 -03001087 if (priv->ctrl.max_len < 9)
1088 priv->ctrl.max_len = 13;
Mauro Carvalho Chehab352fae12007-11-01 16:56:26 -03001089
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001090 mutex_unlock(&priv->lock);
1091
Chris Pascoe0a196b62007-11-19 09:29:59 -03001092 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001093}
1094
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001095static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001096 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001097 .name = "Xceive XC3028",
1098 .frequency_min = 42000000,
1099 .frequency_max = 864000000,
1100 .frequency_step = 50000,
1101 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001102
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001103 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001104 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001105 .release = xc2028_dvb_release,
1106 .get_frequency = xc2028_get_frequency,
1107 .get_rf_strength = xc2028_signal,
1108 .set_params = xc2028_set_params,
Chris Pascoe45819c32007-11-19 11:41:20 -03001109 .sleep = xc2028_sleep,
Michel Ludwig701672e2007-07-18 10:29:10 -03001110
Michel Ludwig701672e2007-07-18 10:29:10 -03001111};
1112
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001113void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001114{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001115 struct xc2028_data *priv;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001116 void *video_dev;
Michel Ludwig701672e2007-07-18 10:29:10 -03001117
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001118 if (debug)
Chris Pascoe3157ece2007-11-19 04:34:29 -03001119 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001120
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001121 if (NULL == cfg || NULL == cfg->video_dev)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001122 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001123
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001124 if (!fe) {
Chris Pascoe3157ece2007-11-19 04:34:29 -03001125 printk(KERN_ERR PREFIX ": No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001126 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001127 }
1128
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001129 video_dev = cfg->video_dev;
1130
Chris Pascoeaa501be2007-11-19 04:45:38 -03001131 mutex_lock(&xc2028_list_mutex);
1132
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001133 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001134 if (priv->video_dev == cfg->video_dev) {
1135 video_dev = NULL;
1136 break;
1137 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001138 }
1139
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001140 if (video_dev) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001141 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Chris Pascoeaa501be2007-11-19 04:45:38 -03001142 if (priv == NULL) {
1143 mutex_unlock(&xc2028_list_mutex);
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001144 return NULL;
Chris Pascoeaa501be2007-11-19 04:45:38 -03001145 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001146
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001147 priv->i2c_props.addr = cfg->i2c_addr;
1148 priv->i2c_props.adap = cfg->i2c_adap;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001149 priv->video_dev = video_dev;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001150 priv->tuner_callback = cfg->callback;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001151 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001152
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001153 mutex_init(&priv->lock);
1154
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001155 list_add_tail(&priv->xc2028_list, &xc2028_list);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001156 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001157
1158 fe->tuner_priv = priv;
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001159 priv->count++;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001160
1161 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001162 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001163
1164 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001165
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001166 if (cfg->ctrl)
1167 xc2028_set_config(fe, cfg->ctrl);
1168
Chris Pascoeaa501be2007-11-19 04:45:38 -03001169 mutex_unlock(&xc2028_list_mutex);
1170
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001171 return fe;
Michel Ludwig701672e2007-07-18 10:29:10 -03001172}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001173
Michel Ludwig701672e2007-07-18 10:29:10 -03001174EXPORT_SYMBOL(xc2028_attach);
1175
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001176MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001177MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001178MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1179MODULE_LICENSE("GPL");