blob: a32094e545f77422d83dedd22def075434846e41 [file] [log] [blame]
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001/* tuner-xc2028
2 *
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -03003 * Copyright (c) 2007-2008 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
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030026static int debug;
27module_param(debug, int, 0644);
28MODULE_PARM_DESC(debug, "enable verbose debug messages");
29
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -030030static char audio_std[8];
31module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
32MODULE_PARM_DESC(audio_std,
33 "Audio standard. XC3028 audio decoder explicitly "
34 "needs to know what audio\n"
35 "standard is needed for some video standards with audio A2 or NICAM.\n"
36 "The valid values are:\n"
37 "A2\n"
38 "A2/A\n"
39 "A2/B\n"
40 "NICAM\n"
41 "NICAM/A\n"
42 "NICAM/B\n");
43
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -030044static char firmware_name[FIRMWARE_NAME_MAX];
45module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
46MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
47 "default firmware name\n");
48
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030049static LIST_HEAD(xc2028_list);
Chris Pascoeaa501be2007-11-19 04:45:38 -030050static DEFINE_MUTEX(xc2028_list_mutex);
51
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030052/* struct for storing firmware table */
53struct firmware_description {
54 unsigned int type;
55 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030056 __u16 int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030057 unsigned char *ptr;
58 unsigned int size;
59};
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030060
Chris Pascoee0f0b372007-11-19 11:22:03 -030061struct firmware_properties {
62 unsigned int type;
63 v4l2_std_id id;
64 v4l2_std_id std_req;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030065 __u16 int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -030066 unsigned int scode_table;
67 int scode_nr;
68};
69
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030070struct xc2028_data {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030071 struct list_head xc2028_list;
72 struct tuner_i2c_props i2c_props;
73 int (*tuner_callback) (void *dev,
74 int command, int arg);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030075 void *video_dev;
76 int count;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030077 __u32 frequency;
78
79 struct firmware_description *firm;
80 int firm_size;
Chris Pascoe06fd82d2007-11-19 06:06:08 -030081 __u16 firm_version;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030082
Chris Pascoe8bf799a2007-11-19 11:35:45 -030083 __u16 hwmodel;
84 __u16 hwvers;
85
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030086 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030087
Chris Pascoee0f0b372007-11-19 11:22:03 -030088 struct firmware_properties cur_fw;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030089
90 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030091};
92
Chris Pascoe47cc5b72007-11-19 04:14:23 -030093#define i2c_send(priv, buf, size) ({ \
94 int _rc; \
95 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
96 if (size != _rc) \
97 tuner_info("i2c output error: rc = %d (should be %d)\n",\
98 _rc, (int)size); \
99 _rc; \
100})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300101
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300102#define i2c_rcv(priv, buf, size) ({ \
103 int _rc; \
104 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
105 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300106 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300107 _rc, (int)size); \
108 _rc; \
109})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300110
Chris Pascoe7d58d112007-11-19 04:31:58 -0300111#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
112 int _rc; \
113 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
114 ibuf, isize); \
115 if (isize != _rc) \
116 tuner_err("i2c input error: rc = %d (should be %d)\n", \
117 _rc, (int)isize); \
118 _rc; \
119})
120
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300121#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300122 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300123 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300124 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300125 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300126 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300127 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
128 } else \
129 msleep(10); \
130 _rc; \
131})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300132
Devin Heitmueller83244022008-04-17 21:41:16 -0300133static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300134{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300135 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300136 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300137
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300138 tuner_dbg("%s %04x called\n", __func__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300139
Chris Pascoe7d58d112007-11-19 04:31:58 -0300140 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300141 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300142
Chris Pascoe7d58d112007-11-19 04:31:58 -0300143 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
144 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300145
Chris Pascoe7d58d112007-11-19 04:31:58 -0300146 *val = (ibuf[1]) | (ibuf[0] << 8);
147 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300148}
149
Chris Pascoee0262682007-12-02 06:30:50 -0300150#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Adrian Bunk29bec0b2008-04-22 14:41:45 -0300151static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300152{
153 if (type & BASE)
154 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300155 if (type & INIT1)
156 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300157 if (type & F8MHZ)
158 printk("F8MHZ ");
159 if (type & MTS)
160 printk("MTS ");
161 if (type & D2620)
162 printk("D2620 ");
163 if (type & D2633)
164 printk("D2633 ");
165 if (type & DTV6)
166 printk("DTV6 ");
167 if (type & QAM)
168 printk("QAM ");
169 if (type & DTV7)
170 printk("DTV7 ");
171 if (type & DTV78)
172 printk("DTV78 ");
173 if (type & DTV8)
174 printk("DTV8 ");
175 if (type & FM)
176 printk("FM ");
177 if (type & INPUT1)
178 printk("INPUT1 ");
179 if (type & LCD)
180 printk("LCD ");
181 if (type & NOGD)
182 printk("NOGD ");
183 if (type & MONO)
184 printk("MONO ");
185 if (type & ATSC)
186 printk("ATSC ");
187 if (type & IF)
188 printk("IF ");
189 if (type & LG60)
190 printk("LG60 ");
191 if (type & ATI638)
192 printk("ATI638 ");
193 if (type & OREN538)
194 printk("OREN538 ");
195 if (type & OREN36)
196 printk("OREN36 ");
197 if (type & TOYOTA388)
198 printk("TOYOTA388 ");
199 if (type & TOYOTA794)
200 printk("TOYOTA794 ");
201 if (type & DIBCOM52)
202 printk("DIBCOM52 ");
203 if (type & ZARLINK456)
204 printk("ZARLINK456 ");
205 if (type & CHINA)
206 printk("CHINA ");
207 if (type & F6MHZ)
208 printk("F6MHZ ");
209 if (type & INPUT2)
210 printk("INPUT2 ");
211 if (type & SCODE)
212 printk("SCODE ");
Chris Pascoee0262682007-12-02 06:30:50 -0300213 if (type & HAS_IF)
214 printk("HAS_IF_%d ", int_freq);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300215}
216
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300217static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300218{
Chris Pascoee155d902007-11-19 04:16:47 -0300219 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300220 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300221 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300222 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300223 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300224 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300225 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300226 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300227 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300228 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300229 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300230 return V4L2_STD_NICAM_B;
231
232 return 0;
233}
234
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300235static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300236{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300237 int i;
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -0300238 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300239
240 if (!priv->firm)
241 return;
242
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300243 for (i = 0; i < priv->firm_size; i++)
244 kfree(priv->firm[i].ptr);
245
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300246 kfree(priv->firm);
247
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300248 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300249 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300250
251 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300252}
253
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300254static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300255{
256 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300257 const struct firmware *fw = NULL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300258 unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300259 int rc = 0;
260 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300261 char name[33];
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300262 char *fname;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300263
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300264 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300265
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300266 if (!firmware_name[0])
267 fname = priv->ctrl.fname;
268 else
269 fname = firmware_name;
270
271 tuner_dbg("Reading firmware %s\n", fname);
272 rc = request_firmware(&fw, fname, &priv->i2c_props.adap->dev);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300273 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300274 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300275 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300276 fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300277 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300278 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300279 rc, fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300280
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300281 return rc;
282 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300283 p = fw->data;
284 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300285
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300286 if (fw->size < sizeof(name) - 1 + 2 + 2) {
287 tuner_err("Error: firmware file %s has invalid size!\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300288 fname);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300289 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300290 }
291
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300292 memcpy(name, p, sizeof(name) - 1);
293 name[sizeof(name) - 1] = 0;
294 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300295
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300296 priv->firm_version = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300297 p += 2;
298
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300299 n_array = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300300 p += 2;
301
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300302 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300303 n_array, fname, name,
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300304 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300305
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300306 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300307 if (priv->firm == NULL) {
308 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300309 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300310 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300311 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300312 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300313
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300314 n = -1;
315 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300316 __u32 type, size;
317 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300318 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300319
320 n++;
321 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300322 tuner_err("More firmware images in file than "
323 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300324 goto corrupt;
325 }
326
327 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300328 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300329 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300330 goto corrupt;
331 }
332
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300333 type = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300334 p += sizeof(type);
335
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300336 id = le64_to_cpu(*(v4l2_std_id *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300337 p += sizeof(id);
338
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300339 if (type & HAS_IF) {
340 int_freq = le16_to_cpu(*(__u16 *) p);
341 p += sizeof(int_freq);
342 }
343
Michel Ludwig2fc580f2007-11-16 07:19:35 -0300344 size = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300345 p += sizeof(size);
346
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300347 if ((!size) || (size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300348 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300349 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300350 printk("(%x), id %llx is corrupted "
351 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300352 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300353 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300354 goto corrupt;
355 }
356
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300357 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300358 if (priv->firm[n].ptr == NULL) {
359 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300360 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300361 goto err;
362 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300363 tuner_dbg("Reading firmware type ");
364 if (debug) {
Chris Pascoee0262682007-12-02 06:30:50 -0300365 dump_firm_type_and_int_freq(type, int_freq);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300366 printk("(%x), id %llx, size=%d.\n",
Chris Pascoee0262682007-12-02 06:30:50 -0300367 type, (unsigned long long)id, size);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300368 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300369
370 memcpy(priv->firm[n].ptr, p, size);
371 priv->firm[n].type = type;
372 priv->firm[n].id = id;
373 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300374 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300375
376 p += size;
377 }
378
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300379 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300380 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300381 goto corrupt;
382 }
383
384 goto done;
385
386corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300387 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300388 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300389
390err:
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300391 tuner_info("Releasing partially loaded firmware file.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300392 free_firmware(priv);
393
394done:
395 release_firmware(fw);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300396 if (rc == 0)
397 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300398
399 return rc;
400}
401
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300402static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
403 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300404{
405 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoeb1535292007-11-19 10:04:06 -0300406 int i, best_i = -1, best_nr_matches = 0;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300407 unsigned int type_mask = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300408
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300409 tuner_dbg("%s called, want type=", __func__);
Chris Pascoeb1535292007-11-19 10:04:06 -0300410 if (debug) {
411 dump_firm_type(type);
412 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
413 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300414
415 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300416 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300417 return -EINVAL;
418 }
419
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300420 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300421 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300422
Chris Pascoee0f0b372007-11-19 11:22:03 -0300423 if (type & BASE)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300424 type_mask = BASE_TYPES;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300425 else if (type & SCODE) {
Chris Pascoee0f0b372007-11-19 11:22:03 -0300426 type &= SCODE_TYPES;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300427 type_mask = SCODE_TYPES & ~HAS_IF;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300428 } else if (type & DTV_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300429 type_mask = DTV_TYPES;
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300430 else if (type & STD_SPECIFIC_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300431 type_mask = STD_SPECIFIC_TYPES;
432
433 type &= type_mask;
434
435 if (!type & SCODE)
436 type_mask = ~0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300437
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300438 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300439 for (i = 0; i < priv->firm_size; i++) {
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300440 if ((type == (priv->firm[i].type & type_mask)) &&
Chris Pascoeef207fe2007-12-02 09:30:55 -0300441 (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300442 goto found;
443 }
444
445 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300446 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300447 v4l2_std_id match_mask;
448 int nr_matches;
449
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300450 if (type != (priv->firm[i].type & type_mask))
Chris Pascoeb1535292007-11-19 10:04:06 -0300451 continue;
452
453 match_mask = *id & priv->firm[i].id;
454 if (!match_mask)
455 continue;
456
457 if ((*id & match_mask) == *id)
458 goto found; /* Supports all the requested standards */
459
460 nr_matches = hweight64(match_mask);
461 if (nr_matches > best_nr_matches) {
462 best_nr_matches = nr_matches;
463 best_i = i;
464 }
465 }
466
467 if (best_nr_matches > 0) {
468 tuner_dbg("Selecting best matching firmware (%d bits) for "
469 "type=", best_nr_matches);
470 dump_firm_type(type);
471 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
472 i = best_i;
473 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300474 }
475
476 /*FIXME: Would make sense to seek for type "hint" match ? */
477
Chris Pascoeb1535292007-11-19 10:04:06 -0300478 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300479 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300480
481found:
482 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300483
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300484ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300485 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300486 if (debug) {
487 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300488 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300489 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300490 return i;
491}
492
493static int load_firmware(struct dvb_frontend *fe, unsigned int type,
494 v4l2_std_id *id)
495{
496 struct xc2028_data *priv = fe->tuner_priv;
497 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300498 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300499
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300500 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300501
502 pos = seek_firmware(fe, type, id);
503 if (pos < 0)
504 return pos;
505
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300506 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300507 dump_firm_type(priv->firm[pos].type);
508 printk("(%x), id %016llx.\n", priv->firm[pos].type,
509 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300510
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300511 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300512 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300513
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300514 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300515 __u16 size;
516
517 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300518 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300519 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300520 return -EINVAL;
521 }
522
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300523 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300524 p += sizeof(size);
525
526 if (size == 0xffff)
527 return 0;
528
529 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300530 /* Special callback command received */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300531 rc = priv->tuner_callback(priv->video_dev,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300532 XC2028_TUNER_RESET, 0);
533 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300534 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300535 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300536 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300537 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300538 continue;
539 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300540 if (size >= 0xff00) {
541 switch (size) {
542 case 0xff00:
543 rc = priv->tuner_callback(priv->video_dev,
544 XC2028_RESET_CLK, 0);
545 if (rc < 0) {
546 tuner_err("Error at RESET code %d\n",
547 (*p) & 0x7f);
548 return -EINVAL;
549 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300550 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300551 default:
552 tuner_info("Invalid RESET code %d\n",
553 size & 0x7f);
554 return -EINVAL;
555
556 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300557 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300558 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300559
560 /* Checks for a sleep command */
561 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300562 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300563 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300564 }
565
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300566 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300567 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300568 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300569 return -EINVAL;
570 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300571
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300572 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300573 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300574 size--;
575
576 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300577 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300578 int len = (size < priv->ctrl.max_len - 1) ?
579 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300580
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300581 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300582
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300583 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300584 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300585 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300586 return -EINVAL;
587 }
588
589 p += len;
590 size -= len;
591 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300592 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300593 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300594}
595
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300596static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300597 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300598{
599 struct xc2028_data *priv = fe->tuner_priv;
600 int pos, rc;
601 unsigned char *p;
602
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300603 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300604
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300605 if (!int_freq) {
606 pos = seek_firmware(fe, type, id);
607 if (pos < 0)
608 return pos;
609 } else {
610 for (pos = 0; pos < priv->firm_size; pos++) {
611 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300612 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300613 break;
614 }
615 if (pos == priv->firm_size)
616 return -ENOENT;
617 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300618
619 p = priv->firm[pos].ptr;
620
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300621 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300622 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
623 return -EINVAL;
624 p += 12 * scode;
625 } else {
626 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
627 * has a 2-byte size header in the firmware format. */
628 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
629 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
630 return -EINVAL;
631 p += 14 * scode + 2;
632 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300633
Chris Pascoed7b22c52007-11-19 10:12:45 -0300634 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300635 dump_firm_type_and_int_freq(priv->firm[pos].type,
636 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300637 printk("(%x), id %016llx.\n", priv->firm[pos].type,
638 (unsigned long long)*id);
639
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300640 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300641 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
642 else
643 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
644 if (rc < 0)
645 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300646
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300647 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300648 if (rc < 0)
649 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300650
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300651 rc = send_seq(priv, {0x00, 0x8c});
652 if (rc < 0)
653 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300654
655 return 0;
656}
657
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300658static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300659 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300660{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300661 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300662 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300663 int rc = 0, is_retry = 0;
664 u16 version, hwmodel;
665 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300666
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300667 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300668
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300669 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300670 if (!priv->ctrl.fname) {
671 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300672 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300673 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300674
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300675 rc = load_all_firmwares(fe);
676 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300677 return rc;
678 }
679
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300680 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300681 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300682
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300683retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300684 new_fw.type = type;
685 new_fw.id = std;
686 new_fw.std_req = std;
687 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
688 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300689 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300690
691 tuner_dbg("checking firmware, user requested type=");
692 if (debug) {
693 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300694 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300695 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300696 if (!int_freq) {
697 printk("scode_tbl ");
698 dump_firm_type(priv->ctrl.scode_table);
699 printk("(%x), ", priv->ctrl.scode_table);
700 } else
701 printk("int_freq %d, ", new_fw.int_freq);
702 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300703 }
704
705 /* No need to reload base firmware if it matches */
706 if (((BASE | new_fw.type) & BASE_TYPES) ==
707 (priv->cur_fw.type & BASE_TYPES)) {
708 tuner_dbg("BASE firmware not changed.\n");
709 goto skip_base;
710 }
711
712 /* Updating BASE - forget about all currently loaded firmware */
713 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
714
715 /* Reset is needed before loading firmware */
716 rc = priv->tuner_callback(priv->video_dev,
717 XC2028_TUNER_RESET, 0);
718 if (rc < 0)
719 goto fail;
720
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300721 /* BASE firmwares are all std0 */
722 std0 = 0;
723 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300724 if (rc < 0) {
725 tuner_err("Error %d while loading base firmware\n",
726 rc);
727 goto fail;
728 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300729
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300730 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300731 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300732
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300733 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300734 if (rc == -ENOENT)
735 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
736 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300737 if (rc < 0 && rc != -ENOENT) {
738 tuner_err("Error %d while loading init1 firmware\n",
739 rc);
740 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300741 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300742
Chris Pascoee0f0b372007-11-19 11:22:03 -0300743skip_base:
744 /*
745 * No need to reload standard specific firmware if base firmware
746 * was not reloaded and requested video standards have not changed.
747 */
748 if (priv->cur_fw.type == (BASE | new_fw.type) &&
749 priv->cur_fw.std_req == std) {
750 tuner_dbg("Std-specific firmware already loaded.\n");
751 goto skip_std_specific;
752 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300753
Chris Pascoee0f0b372007-11-19 11:22:03 -0300754 /* Reloading std-specific firmware forces a SCODE update */
755 priv->cur_fw.scode_table = 0;
756
Chris Pascoee0f0b372007-11-19 11:22:03 -0300757 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300758 if (rc == -ENOENT)
759 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
760
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300761 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300762 goto fail;
763
764skip_std_specific:
765 if (priv->cur_fw.scode_table == new_fw.scode_table &&
766 priv->cur_fw.scode_nr == new_fw.scode_nr) {
767 tuner_dbg("SCODE firmware already loaded.\n");
768 goto check_device;
769 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300770
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300771 if (new_fw.type & FM)
772 goto check_device;
773
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300774 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300775 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300776
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300777 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
778 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300779
Chris Pascoee0f0b372007-11-19 11:22:03 -0300780check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300781 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
782 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
783 tuner_err("Unable to read tuner registers.\n");
784 goto fail;
785 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300786
Devin Heitmuellerb37f2d62008-04-21 07:02:09 -0300787 tuner_dbg("Device is Xceive %d version %d.%d, "
788 "firmware version %d.%d\n",
789 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
790 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300791
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300792 /* Check firmware version against what we downloaded. */
793 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
794 tuner_err("Incorrect readback of firmware version.\n");
795 goto fail;
796 }
797
798 /* Check that the tuner hardware model remains consistent over time. */
799 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
800 priv->hwmodel = hwmodel;
801 priv->hwvers = version & 0xff00;
802 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
803 priv->hwvers != (version & 0xff00)) {
804 tuner_err("Read invalid device hardware information - tuner "
805 "hung?\n");
806 goto fail;
807 }
808
Chris Pascoee0f0b372007-11-19 11:22:03 -0300809 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
810
811 /*
812 * By setting BASE in cur_fw.type only after successfully loading all
813 * firmwares, we can:
814 * 1. Identify that BASE firmware with type=0 has been loaded;
815 * 2. Tell whether BASE firmware was just changed the next time through.
816 */
817 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300818
819 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300820
821fail:
822 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300823 if (!is_retry) {
824 msleep(50);
825 is_retry = 1;
826 tuner_dbg("Retrying firmware load\n");
827 goto retry;
828 }
829
Chris Pascoee0f0b372007-11-19 11:22:03 -0300830 if (rc == -ENOENT)
831 rc = -EINVAL;
832 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300833}
834
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300835static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300836{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300837 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300838 u16 frq_lock, signal = 0;
839 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300840
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300841 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300842
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300843 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300844
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300845 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300846 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
847 if (rc < 0 || frq_lock == 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300848 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300849
850 /* Frequency is locked. Return signal quality */
851
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300852 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300853 rc = xc2028_get_reg(priv, 0x0040, &signal);
854 if (rc < 0)
855 signal = -frq_lock;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300856
857ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300858 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300859
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300860 *strength = signal;
861
Chris Pascoe7d58d112007-11-19 04:31:58 -0300862 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300863}
864
865#define DIV 15625
866
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300867static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300868 enum tuner_mode new_mode,
869 unsigned int type,
870 v4l2_std_id std,
871 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300872{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300873 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300874 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300875 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300876 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300877
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300878 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300879
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300880 mutex_lock(&priv->lock);
881
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300882 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300883
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300884 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300885 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300886
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300887 /* On some cases xc2028 can disable video output, if
888 * very weak signals are received. By sending a soft
889 * reset, this is re-enabled. So, it is better to always
890 * send a soft reset before changing channels, to be sure
891 * that xc2028 will be in a safe state.
892 * Maybe this might also be needed for DTV.
893 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300894 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300895 rc = send_seq(priv, {0x00, 0x00});
Michael Krufkyd536c9d2007-12-18 10:42:33 -0300896 } else if (priv->cur_fw.type & ATSC) {
897 offset = 1750000;
898 } else {
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300899 offset = 2750000;
Chris Pascoe897b8422007-12-02 09:39:18 -0300900 /*
901 * We must adjust the offset by 500kHz in two cases in order
902 * to correctly center the IF output:
903 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
904 * selected and a 7MHz channel is tuned;
905 * 2) When tuning a VHF channel with DTV78 firmware.
906 */
907 if (((priv->cur_fw.type & DTV7) &&
908 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
909 ((priv->cur_fw.type & DTV78) && freq < 470000000))
Chris Pascoea44f1c42007-11-19 06:35:26 -0300910 offset -= 500000;
911 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300912
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300913 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300914
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300915 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300916 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300917 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
918 else
919 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
920 if (rc < 0)
921 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300922
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -0300923 /* Return code shouldn't be checked.
924 The reset CLK is needed only with tm6000.
925 Driver should work fine even if this fails.
926 */
927 priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300928
929 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300930
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300931 buf[0] = 0xff & (div >> 24);
932 buf[1] = 0xff & (div >> 16);
933 buf[2] = 0xff & (div >> 8);
934 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300935
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300936 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300937 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300938 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300939 msleep(100);
940
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300941 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300942
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300943 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
944 buf[0], buf[1], buf[2], buf[3],
945 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300946
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300947 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300948
949ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300950 mutex_unlock(&priv->lock);
951
952 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300953}
954
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300955static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300956 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300957{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300958 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300959 unsigned int type=0;
960
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300961 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -0300962
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300963 if (p->mode == V4L2_TUNER_RADIO) {
964 type |= FM;
965 if (priv->ctrl.input1)
966 type |= INPUT1;
967 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300968 T_ANALOG_TV, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300969 }
970
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -0300971 /* if std is not defined, choose one */
972 if (!p->std)
973 p->std = V4L2_STD_MN;
974
975 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300976 if (!(p->std & V4L2_STD_MN))
977 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -0300978
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300979 /* Add audio hack to std mask */
980 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300981
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300982 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300983 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -0300984}
985
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300986static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -0300987 struct dvb_frontend_parameters *p)
988{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300989 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300990 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300991 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -0300992 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -0300993
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300994 tuner_dbg("%s called\n", __func__);
Michel Ludwig701672e2007-07-18 10:29:10 -0300995
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300996 if (priv->ctrl.d2633)
997 type |= D2633;
998 else
999 type |= D2620;
1000
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001001 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001002 case FE_OFDM:
1003 bw = p->u.ofdm.bandwidth;
1004 break;
1005 case FE_QAM:
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001006 tuner_info("WARN: There are some reports that "
1007 "QAM 6 MHz doesn't work.\n"
1008 "If this works for you, please report by "
1009 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001010 bw = BANDWIDTH_6_MHZ;
1011 type |= QAM;
1012 break;
1013 case FE_ATSC:
1014 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -03001015 /* The only ATSC firmware (at least on v2.7) is D2633,
1016 so overrides ctrl->d2633 */
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001017 type |= ATSC| D2633;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -03001018 type &= ~D2620;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001019 break;
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001020 /* DVB-S is not supported */
1021 default:
1022 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001023 }
1024
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001025 switch (bw) {
1026 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001027 if (p->frequency < 470000000)
1028 priv->ctrl.vhfbw7 = 0;
1029 else
1030 priv->ctrl.uhfbw8 = 1;
1031 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1032 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001033 break;
1034 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001035 if (p->frequency < 470000000)
1036 priv->ctrl.vhfbw7 = 1;
1037 else
1038 priv->ctrl.uhfbw8 = 0;
1039 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1040 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001041 break;
1042 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001043 type |= DTV6;
1044 priv->ctrl.vhfbw7 = 0;
1045 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001046 break;
1047 default:
1048 tuner_err("error: bandwidth not supported.\n");
1049 };
1050
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001051 /* All S-code tables need a 200kHz shift */
1052 if (priv->ctrl.demod)
Chris Pascoead35ce92007-12-02 06:36:42 -03001053 demod = priv->ctrl.demod + 200;
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001054
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001055 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001056 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001057}
1058
Chris Pascoe45819c32007-11-19 11:41:20 -03001059
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001060static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001061{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001062 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001063
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001064 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001065
Chris Pascoeaa501be2007-11-19 04:45:38 -03001066 mutex_lock(&xc2028_list_mutex);
1067
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001068 priv->count--;
1069
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001070 if (!priv->count) {
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001071 list_del(&priv->xc2028_list);
1072
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001073 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001074
1075 free_firmware(priv);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001076 kfree(priv);
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001077 fe->tuner_priv = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001078 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001079
Chris Pascoeaa501be2007-11-19 04:45:38 -03001080 mutex_unlock(&xc2028_list_mutex);
1081
Michel Ludwig701672e2007-07-18 10:29:10 -03001082 return 0;
1083}
1084
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001085static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001086{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001087 struct xc2028_data *priv = fe->tuner_priv;
1088
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001089 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001090
1091 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001092
1093 return 0;
1094}
1095
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001096static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001097{
1098 struct xc2028_data *priv = fe->tuner_priv;
1099 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001100 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001101
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001102 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001103
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001104 mutex_lock(&priv->lock);
1105
Chris Pascoe0a196b62007-11-19 09:29:59 -03001106 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001107 if (priv->ctrl.max_len < 9)
1108 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001109
1110 if (p->fname) {
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001111 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1112 kfree(priv->ctrl.fname);
1113 free_firmware(priv);
1114 }
1115
Chris Pascoe0a196b62007-11-19 09:29:59 -03001116 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1117 if (priv->ctrl.fname == NULL)
1118 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001119 }
1120
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001121 mutex_unlock(&priv->lock);
1122
Chris Pascoe0a196b62007-11-19 09:29:59 -03001123 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001124}
1125
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001126static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001127 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001128 .name = "Xceive XC3028",
1129 .frequency_min = 42000000,
1130 .frequency_max = 864000000,
1131 .frequency_step = 50000,
1132 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001133
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001134 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001135 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001136 .release = xc2028_dvb_release,
1137 .get_frequency = xc2028_get_frequency,
1138 .get_rf_strength = xc2028_signal,
1139 .set_params = xc2028_set_params,
Michel Ludwig701672e2007-07-18 10:29:10 -03001140};
1141
Michael Krufky7972f982007-12-21 16:12:09 -03001142struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1143 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001144{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001145 struct xc2028_data *priv;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001146 void *video_dev;
Michel Ludwig701672e2007-07-18 10:29:10 -03001147
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001148 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001149 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001150
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001151 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001152 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001153
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001154 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001155 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001156 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001157 }
1158
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001159 video_dev = cfg->i2c_adap->algo_data;
1160
1161 if (debug)
1162 printk(KERN_DEBUG "xc2028: video_dev =%p\n", video_dev);
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001163
Chris Pascoeaa501be2007-11-19 04:45:38 -03001164 mutex_lock(&xc2028_list_mutex);
1165
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001166 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001167 if (&priv->i2c_props.adap->dev == &cfg->i2c_adap->dev) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001168 video_dev = NULL;
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001169 if (debug)
1170 printk(KERN_DEBUG "xc2028: reusing device\n");
1171
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001172 break;
1173 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001174 }
1175
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001176 if (video_dev) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001177 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Chris Pascoeaa501be2007-11-19 04:45:38 -03001178 if (priv == NULL) {
1179 mutex_unlock(&xc2028_list_mutex);
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001180 return NULL;
Chris Pascoeaa501be2007-11-19 04:45:38 -03001181 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001182
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001183 priv->i2c_props.addr = cfg->i2c_addr;
1184 priv->i2c_props.adap = cfg->i2c_adap;
Michael Krufky27566652008-04-22 14:41:53 -03001185 priv->i2c_props.name = "xc2028";
1186
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001187 priv->video_dev = video_dev;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001188 priv->tuner_callback = cfg->callback;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001189 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001190
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001191 mutex_init(&priv->lock);
1192
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001193 list_add_tail(&priv->xc2028_list, &xc2028_list);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001194 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001195
1196 fe->tuner_priv = priv;
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001197 priv->count++;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001198
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001199 if (debug)
1200 printk(KERN_DEBUG "xc2028: usage count is %i\n", priv->count);
1201
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001202 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001203 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001204
1205 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001206
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001207 if (cfg->ctrl)
1208 xc2028_set_config(fe, cfg->ctrl);
1209
Chris Pascoeaa501be2007-11-19 04:45:38 -03001210 mutex_unlock(&xc2028_list_mutex);
1211
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001212 return fe;
Michel Ludwig701672e2007-07-18 10:29:10 -03001213}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001214
Michel Ludwig701672e2007-07-18 10:29:10 -03001215EXPORT_SYMBOL(xc2028_attach);
1216
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001217MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001218MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001219MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1220MODULE_LICENSE("GPL");