blob: 098a9956a2bce908a777b1ab05872b87e124f9b2 [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
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
Chris Pascoe7d58d112007-11-19 04:31:58 -0300133static unsigned 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
Chris Pascoe7d58d112007-11-19 04:31:58 -0300138 tuner_dbg("%s %04x called\n", __FUNCTION__, 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;
238
239 if (!priv->firm)
240 return;
241
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300242 for (i = 0; i < priv->firm_size; i++)
243 kfree(priv->firm[i].ptr);
244
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300245 kfree(priv->firm);
246
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300247 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300248 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300249
250 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300251}
252
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300253static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300254{
255 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300256 const struct firmware *fw = NULL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300257 unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300258 int rc = 0;
259 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300260 char name[33];
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300261 char *fname;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300262
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300263 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300264
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300265 if (!firmware_name[0])
266 fname = priv->ctrl.fname;
267 else
268 fname = firmware_name;
269
270 tuner_dbg("Reading firmware %s\n", fname);
271 rc = request_firmware(&fw, fname, &priv->i2c_props.adap->dev);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300272 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300273 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300274 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300275 fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300276 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300277 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300278 rc, fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300279
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300280 return rc;
281 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300282 p = fw->data;
283 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300284
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300285 if (fw->size < sizeof(name) - 1 + 2 + 2) {
286 tuner_err("Error: firmware file %s has invalid size!\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300287 fname);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300288 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300289 }
290
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300291 memcpy(name, p, sizeof(name) - 1);
292 name[sizeof(name) - 1] = 0;
293 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300294
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300295 priv->firm_version = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300296 p += 2;
297
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300298 n_array = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300299 p += 2;
300
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300301 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300302 n_array, fname, name,
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300303 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300304
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300305 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300306 if (priv->firm == NULL) {
307 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300308 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300309 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300310 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300311 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300312
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300313 n = -1;
314 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300315 __u32 type, size;
316 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300317 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300318
319 n++;
320 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300321 tuner_err("More firmware images in file than "
322 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300323 goto corrupt;
324 }
325
326 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300327 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300328 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300329 goto corrupt;
330 }
331
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300332 type = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300333 p += sizeof(type);
334
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300335 id = le64_to_cpu(*(v4l2_std_id *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300336 p += sizeof(id);
337
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300338 if (type & HAS_IF) {
339 int_freq = le16_to_cpu(*(__u16 *) p);
340 p += sizeof(int_freq);
341 }
342
Michel Ludwig2fc580f2007-11-16 07:19:35 -0300343 size = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300344 p += sizeof(size);
345
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300346 if ((!size) || (size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300347 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300348 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300349 printk("(%x), id %llx is corrupted "
350 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300351 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300352 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300353 goto corrupt;
354 }
355
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300356 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300357 if (priv->firm[n].ptr == NULL) {
358 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300359 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300360 goto err;
361 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300362 tuner_dbg("Reading firmware type ");
363 if (debug) {
Chris Pascoee0262682007-12-02 06:30:50 -0300364 dump_firm_type_and_int_freq(type, int_freq);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300365 printk("(%x), id %llx, size=%d.\n",
Chris Pascoee0262682007-12-02 06:30:50 -0300366 type, (unsigned long long)id, size);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300367 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300368
369 memcpy(priv->firm[n].ptr, p, size);
370 priv->firm[n].type = type;
371 priv->firm[n].id = id;
372 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300373 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300374
375 p += size;
376 }
377
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300378 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300379 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300380 goto corrupt;
381 }
382
383 goto done;
384
385corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300386 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300387 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300388
389err:
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300390 tuner_info("Releasing partially loaded firmware file.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300391 free_firmware(priv);
392
393done:
394 release_firmware(fw);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300395 if (rc == 0)
396 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300397
398 return rc;
399}
400
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300401static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
402 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300403{
404 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoeb1535292007-11-19 10:04:06 -0300405 int i, best_i = -1, best_nr_matches = 0;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300406 unsigned int ign_firm_type_mask = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300407
Chris Pascoeb1535292007-11-19 10:04:06 -0300408 tuner_dbg("%s called, want type=", __FUNCTION__);
409 if (debug) {
410 dump_firm_type(type);
411 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
412 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300413
414 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300415 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300416 return -EINVAL;
417 }
418
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300419 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300420 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300421
Chris Pascoee0f0b372007-11-19 11:22:03 -0300422 if (type & BASE)
423 type &= BASE_TYPES;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300424 else if (type & SCODE) {
Chris Pascoee0f0b372007-11-19 11:22:03 -0300425 type &= SCODE_TYPES;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300426 ign_firm_type_mask = HAS_IF;
427 } else if (type & DTV_TYPES)
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300428 type &= DTV_TYPES;
429 else if (type & STD_SPECIFIC_TYPES)
430 type &= STD_SPECIFIC_TYPES;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300431
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300432 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300433 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeef207fe2007-12-02 09:30:55 -0300434 if ((type == (priv->firm[i].type & ~ign_firm_type_mask)) &&
435 (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300436 goto found;
437 }
438
439 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300440 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300441 v4l2_std_id match_mask;
442 int nr_matches;
443
Chris Pascoeef207fe2007-12-02 09:30:55 -0300444 if (type != (priv->firm[i].type & ~ign_firm_type_mask))
Chris Pascoeb1535292007-11-19 10:04:06 -0300445 continue;
446
447 match_mask = *id & priv->firm[i].id;
448 if (!match_mask)
449 continue;
450
451 if ((*id & match_mask) == *id)
452 goto found; /* Supports all the requested standards */
453
454 nr_matches = hweight64(match_mask);
455 if (nr_matches > best_nr_matches) {
456 best_nr_matches = nr_matches;
457 best_i = i;
458 }
459 }
460
461 if (best_nr_matches > 0) {
462 tuner_dbg("Selecting best matching firmware (%d bits) for "
463 "type=", best_nr_matches);
464 dump_firm_type(type);
465 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
466 i = best_i;
467 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300468 }
469
470 /*FIXME: Would make sense to seek for type "hint" match ? */
471
Chris Pascoeb1535292007-11-19 10:04:06 -0300472 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300473 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300474
475found:
476 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300477
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300478ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300479 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300480 if (debug) {
481 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300482 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300483 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300484 return i;
485}
486
487static int load_firmware(struct dvb_frontend *fe, unsigned int type,
488 v4l2_std_id *id)
489{
490 struct xc2028_data *priv = fe->tuner_priv;
491 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300492 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300493
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300494 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300495
496 pos = seek_firmware(fe, type, id);
497 if (pos < 0)
498 return pos;
499
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300500 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300501 dump_firm_type(priv->firm[pos].type);
502 printk("(%x), id %016llx.\n", priv->firm[pos].type,
503 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300504
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300505 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300506 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300507
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300508 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300509 __u16 size;
510
511 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300512 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300513 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300514 return -EINVAL;
515 }
516
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300517 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300518 p += sizeof(size);
519
520 if (size == 0xffff)
521 return 0;
522
523 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300524 /* Special callback command received */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300525 rc = priv->tuner_callback(priv->video_dev,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300526 XC2028_TUNER_RESET, 0);
527 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300528 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300529 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300530 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300531 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300532 continue;
533 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300534 if (size >= 0xff00) {
535 switch (size) {
536 case 0xff00:
537 rc = priv->tuner_callback(priv->video_dev,
538 XC2028_RESET_CLK, 0);
539 if (rc < 0) {
540 tuner_err("Error at RESET code %d\n",
541 (*p) & 0x7f);
542 return -EINVAL;
543 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300544 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300545 default:
546 tuner_info("Invalid RESET code %d\n",
547 size & 0x7f);
548 return -EINVAL;
549
550 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300551 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300552 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300553
554 /* Checks for a sleep command */
555 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300556 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300557 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300558 }
559
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300560 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300561 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300562 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300563 return -EINVAL;
564 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300565
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300566 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300567 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300568 size--;
569
570 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300571 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300572 int len = (size < priv->ctrl.max_len - 1) ?
573 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300574
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300575 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300576
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300577 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300578 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300579 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300580 return -EINVAL;
581 }
582
583 p += len;
584 size -= len;
585 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300586 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300587 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300588}
589
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300590static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300591 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300592{
593 struct xc2028_data *priv = fe->tuner_priv;
594 int pos, rc;
595 unsigned char *p;
596
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300597 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300598
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300599 if (!int_freq) {
600 pos = seek_firmware(fe, type, id);
601 if (pos < 0)
602 return pos;
603 } else {
604 for (pos = 0; pos < priv->firm_size; pos++) {
605 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300606 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300607 break;
608 }
609 if (pos == priv->firm_size)
610 return -ENOENT;
611 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300612
613 p = priv->firm[pos].ptr;
614
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300615 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300616 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
617 return -EINVAL;
618 p += 12 * scode;
619 } else {
620 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
621 * has a 2-byte size header in the firmware format. */
622 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
623 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
624 return -EINVAL;
625 p += 14 * scode + 2;
626 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300627
Chris Pascoed7b22c52007-11-19 10:12:45 -0300628 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300629 dump_firm_type_and_int_freq(priv->firm[pos].type,
630 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300631 printk("(%x), id %016llx.\n", priv->firm[pos].type,
632 (unsigned long long)*id);
633
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300634 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300635 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
636 else
637 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
638 if (rc < 0)
639 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300640
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300641 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300642 if (rc < 0)
643 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300644
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300645 rc = send_seq(priv, {0x00, 0x8c});
646 if (rc < 0)
647 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300648
649 return 0;
650}
651
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300652static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300653 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300654{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300655 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300656 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300657 int rc = 0, is_retry = 0;
658 u16 version, hwmodel;
659 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300660
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300661 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300662
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300663 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300664 if (!priv->ctrl.fname) {
665 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300666 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300667 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300668
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300669 rc = load_all_firmwares(fe);
670 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300671 return rc;
672 }
673
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300674 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300675 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300676
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300677retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300678 new_fw.type = type;
679 new_fw.id = std;
680 new_fw.std_req = std;
681 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
682 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300683 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300684
685 tuner_dbg("checking firmware, user requested type=");
686 if (debug) {
687 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300688 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300689 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300690 if (!int_freq) {
691 printk("scode_tbl ");
692 dump_firm_type(priv->ctrl.scode_table);
693 printk("(%x), ", priv->ctrl.scode_table);
694 } else
695 printk("int_freq %d, ", new_fw.int_freq);
696 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300697 }
698
699 /* No need to reload base firmware if it matches */
700 if (((BASE | new_fw.type) & BASE_TYPES) ==
701 (priv->cur_fw.type & BASE_TYPES)) {
702 tuner_dbg("BASE firmware not changed.\n");
703 goto skip_base;
704 }
705
706 /* Updating BASE - forget about all currently loaded firmware */
707 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
708
709 /* Reset is needed before loading firmware */
710 rc = priv->tuner_callback(priv->video_dev,
711 XC2028_TUNER_RESET, 0);
712 if (rc < 0)
713 goto fail;
714
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300715 /* BASE firmwares are all std0 */
716 std0 = 0;
717 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300718 if (rc < 0) {
719 tuner_err("Error %d while loading base firmware\n",
720 rc);
721 goto fail;
722 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300723
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300724 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300725 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300726
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300727 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300728 if (rc == -ENOENT)
729 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
730 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300731 if (rc < 0 && rc != -ENOENT) {
732 tuner_err("Error %d while loading init1 firmware\n",
733 rc);
734 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300735 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300736
Chris Pascoee0f0b372007-11-19 11:22:03 -0300737skip_base:
738 /*
739 * No need to reload standard specific firmware if base firmware
740 * was not reloaded and requested video standards have not changed.
741 */
742 if (priv->cur_fw.type == (BASE | new_fw.type) &&
743 priv->cur_fw.std_req == std) {
744 tuner_dbg("Std-specific firmware already loaded.\n");
745 goto skip_std_specific;
746 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300747
Chris Pascoee0f0b372007-11-19 11:22:03 -0300748 /* Reloading std-specific firmware forces a SCODE update */
749 priv->cur_fw.scode_table = 0;
750
Chris Pascoee0f0b372007-11-19 11:22:03 -0300751 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300752 if (rc == -ENOENT)
753 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
754
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300755 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300756 goto fail;
757
758skip_std_specific:
759 if (priv->cur_fw.scode_table == new_fw.scode_table &&
760 priv->cur_fw.scode_nr == new_fw.scode_nr) {
761 tuner_dbg("SCODE firmware already loaded.\n");
762 goto check_device;
763 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300764
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300765 if (new_fw.type & FM)
766 goto check_device;
767
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300768 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300769 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300770
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300771 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
772 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300773
Chris Pascoee0f0b372007-11-19 11:22:03 -0300774check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300775 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
776 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
777 tuner_err("Unable to read tuner registers.\n");
778 goto fail;
779 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300780
781 tuner_info("Device is Xceive %d version %d.%d, "
782 "firmware version %d.%d\n",
783 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
784 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300785
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300786 /* Check firmware version against what we downloaded. */
787 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
788 tuner_err("Incorrect readback of firmware version.\n");
789 goto fail;
790 }
791
792 /* Check that the tuner hardware model remains consistent over time. */
793 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
794 priv->hwmodel = hwmodel;
795 priv->hwvers = version & 0xff00;
796 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
797 priv->hwvers != (version & 0xff00)) {
798 tuner_err("Read invalid device hardware information - tuner "
799 "hung?\n");
800 goto fail;
801 }
802
Chris Pascoee0f0b372007-11-19 11:22:03 -0300803 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
804
805 /*
806 * By setting BASE in cur_fw.type only after successfully loading all
807 * firmwares, we can:
808 * 1. Identify that BASE firmware with type=0 has been loaded;
809 * 2. Tell whether BASE firmware was just changed the next time through.
810 */
811 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300812
813 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300814
815fail:
816 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300817 if (!is_retry) {
818 msleep(50);
819 is_retry = 1;
820 tuner_dbg("Retrying firmware load\n");
821 goto retry;
822 }
823
Chris Pascoee0f0b372007-11-19 11:22:03 -0300824 if (rc == -ENOENT)
825 rc = -EINVAL;
826 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300827}
828
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300829static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300830{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300831 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300832 u16 frq_lock, signal = 0;
833 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300834
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300835 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300836
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300837 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300838
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300839 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300840 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
841 if (rc < 0 || frq_lock == 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300842 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300843
844 /* Frequency is locked. Return signal quality */
845
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300846 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300847 rc = xc2028_get_reg(priv, 0x0040, &signal);
848 if (rc < 0)
849 signal = -frq_lock;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300850
851ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300852 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300853
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300854 *strength = signal;
855
Chris Pascoe7d58d112007-11-19 04:31:58 -0300856 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300857}
858
859#define DIV 15625
860
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300861static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300862 enum tuner_mode new_mode,
863 unsigned int type,
864 v4l2_std_id std,
865 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300866{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300867 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300868 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300869 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300870 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300871
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300872 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300873
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300874 mutex_lock(&priv->lock);
875
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300876 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300877
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300878 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300879 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300880
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300881 /* On some cases xc2028 can disable video output, if
882 * very weak signals are received. By sending a soft
883 * reset, this is re-enabled. So, it is better to always
884 * send a soft reset before changing channels, to be sure
885 * that xc2028 will be in a safe state.
886 * Maybe this might also be needed for DTV.
887 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300888 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300889 rc = send_seq(priv, {0x00, 0x00});
Michael Krufkyd536c9d2007-12-18 10:42:33 -0300890 } else if (priv->cur_fw.type & ATSC) {
891 offset = 1750000;
892 } else {
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300893 offset = 2750000;
Chris Pascoe897b8422007-12-02 09:39:18 -0300894 /*
895 * We must adjust the offset by 500kHz in two cases in order
896 * to correctly center the IF output:
897 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
898 * selected and a 7MHz channel is tuned;
899 * 2) When tuning a VHF channel with DTV78 firmware.
900 */
901 if (((priv->cur_fw.type & DTV7) &&
902 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
903 ((priv->cur_fw.type & DTV78) && freq < 470000000))
Chris Pascoea44f1c42007-11-19 06:35:26 -0300904 offset -= 500000;
905 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300906
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300907 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300908
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300909 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300910 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300911 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
912 else
913 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
914 if (rc < 0)
915 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300916
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -0300917 /* Return code shouldn't be checked.
918 The reset CLK is needed only with tm6000.
919 Driver should work fine even if this fails.
920 */
921 priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300922
923 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300924
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300925 buf[0] = 0xff & (div >> 24);
926 buf[1] = 0xff & (div >> 16);
927 buf[2] = 0xff & (div >> 8);
928 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300929
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300930 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300931 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300932 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300933 msleep(100);
934
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300935 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300936
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300937 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
938 buf[0], buf[1], buf[2], buf[3],
939 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300940
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300941 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300942
943ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300944 mutex_unlock(&priv->lock);
945
946 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300947}
948
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300949static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300950 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300951{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300952 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300953 unsigned int type=0;
954
955 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -0300956
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300957 if (p->mode == V4L2_TUNER_RADIO) {
958 type |= FM;
959 if (priv->ctrl.input1)
960 type |= INPUT1;
961 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300962 T_ANALOG_TV, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300963 }
964
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -0300965 /* if std is not defined, choose one */
966 if (!p->std)
967 p->std = V4L2_STD_MN;
968
969 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300970 if (!(p->std & V4L2_STD_MN))
971 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -0300972
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300973 /* Add audio hack to std mask */
974 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300975
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300976 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300977 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -0300978}
979
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300980static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -0300981 struct dvb_frontend_parameters *p)
982{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300983 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300984 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300985 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -0300986 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -0300987
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300988 tuner_dbg("%s called\n", __FUNCTION__);
Michel Ludwig701672e2007-07-18 10:29:10 -0300989
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300990 if (priv->ctrl.d2633)
991 type |= D2633;
992 else
993 type |= D2620;
994
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300995 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300996 case FE_OFDM:
997 bw = p->u.ofdm.bandwidth;
998 break;
999 case FE_QAM:
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001000 tuner_info("WARN: There are some reports that "
1001 "QAM 6 MHz doesn't work.\n"
1002 "If this works for you, please report by "
1003 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001004 bw = BANDWIDTH_6_MHZ;
1005 type |= QAM;
1006 break;
1007 case FE_ATSC:
1008 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -03001009 /* The only ATSC firmware (at least on v2.7) is D2633,
1010 so overrides ctrl->d2633 */
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001011 type |= ATSC| D2633;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -03001012 type &= ~D2620;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001013 break;
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001014 /* DVB-S is not supported */
1015 default:
1016 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001017 }
1018
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001019 switch (bw) {
1020 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001021 if (p->frequency < 470000000)
1022 priv->ctrl.vhfbw7 = 0;
1023 else
1024 priv->ctrl.uhfbw8 = 1;
1025 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1026 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001027 break;
1028 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001029 if (p->frequency < 470000000)
1030 priv->ctrl.vhfbw7 = 1;
1031 else
1032 priv->ctrl.uhfbw8 = 0;
1033 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1034 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001035 break;
1036 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001037 type |= DTV6;
1038 priv->ctrl.vhfbw7 = 0;
1039 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001040 break;
1041 default:
1042 tuner_err("error: bandwidth not supported.\n");
1043 };
1044
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001045 /* All S-code tables need a 200kHz shift */
1046 if (priv->ctrl.demod)
Chris Pascoead35ce92007-12-02 06:36:42 -03001047 demod = priv->ctrl.demod + 200;
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001048
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001049 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001050 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001051}
1052
Chris Pascoe45819c32007-11-19 11:41:20 -03001053static int xc2028_sleep(struct dvb_frontend *fe)
1054{
1055 struct xc2028_data *priv = fe->tuner_priv;
1056 int rc = 0;
1057
1058 tuner_dbg("%s called\n", __FUNCTION__);
1059
1060 mutex_lock(&priv->lock);
1061
1062 if (priv->firm_version < 0x0202)
1063 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1064 else
1065 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1066
1067 priv->cur_fw.type = 0; /* need firmware reload */
1068
1069 mutex_unlock(&priv->lock);
1070
1071 return rc;
1072}
1073
1074
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001075static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001076{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001077 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001078
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001079 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001080
Chris Pascoeaa501be2007-11-19 04:45:38 -03001081 mutex_lock(&xc2028_list_mutex);
1082
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001083 priv->count--;
1084
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001085 if (!priv->count) {
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001086 list_del(&priv->xc2028_list);
1087
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001088 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001089
1090 free_firmware(priv);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001091 kfree(priv);
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001092 fe->tuner_priv = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001093 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001094
Chris Pascoeaa501be2007-11-19 04:45:38 -03001095 mutex_unlock(&xc2028_list_mutex);
1096
Michel Ludwig701672e2007-07-18 10:29:10 -03001097 return 0;
1098}
1099
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001100static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001101{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001102 struct xc2028_data *priv = fe->tuner_priv;
1103
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001104 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001105
1106 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001107
1108 return 0;
1109}
1110
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001111static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001112{
1113 struct xc2028_data *priv = fe->tuner_priv;
1114 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001115 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001116
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001117 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001118
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001119 mutex_lock(&priv->lock);
1120
Chris Pascoe0a196b62007-11-19 09:29:59 -03001121 kfree(priv->ctrl.fname);
1122 free_firmware(priv);
1123
1124 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1125 priv->ctrl.fname = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001126
1127 if (p->fname) {
Chris Pascoe0a196b62007-11-19 09:29:59 -03001128 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1129 if (priv->ctrl.fname == NULL)
1130 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001131 }
1132
Chris Pascoe0a196b62007-11-19 09:29:59 -03001133 if (priv->ctrl.max_len < 9)
1134 priv->ctrl.max_len = 13;
Mauro Carvalho Chehab352fae12007-11-01 16:56:26 -03001135
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001136 mutex_unlock(&priv->lock);
1137
Chris Pascoe0a196b62007-11-19 09:29:59 -03001138 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001139}
1140
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001141static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001142 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001143 .name = "Xceive XC3028",
1144 .frequency_min = 42000000,
1145 .frequency_max = 864000000,
1146 .frequency_step = 50000,
1147 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001148
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001149 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001150 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001151 .release = xc2028_dvb_release,
1152 .get_frequency = xc2028_get_frequency,
1153 .get_rf_strength = xc2028_signal,
1154 .set_params = xc2028_set_params,
Chris Pascoe45819c32007-11-19 11:41:20 -03001155 .sleep = xc2028_sleep,
Michel Ludwig701672e2007-07-18 10:29:10 -03001156
Michel Ludwig701672e2007-07-18 10:29:10 -03001157};
1158
Michael Krufky7972f982007-12-21 16:12:09 -03001159struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1160 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001161{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001162 struct xc2028_data *priv;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001163 void *video_dev;
Michel Ludwig701672e2007-07-18 10:29:10 -03001164
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001165 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001166 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001167
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001168 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001169 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001170
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001171 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001172 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001173 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001174 }
1175
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001176 video_dev = cfg->i2c_adap->algo_data;
1177
1178 if (debug)
1179 printk(KERN_DEBUG "xc2028: video_dev =%p\n", video_dev);
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001180
Chris Pascoeaa501be2007-11-19 04:45:38 -03001181 mutex_lock(&xc2028_list_mutex);
1182
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001183 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001184 if (&priv->i2c_props.adap->dev == &cfg->i2c_adap->dev) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001185 video_dev = NULL;
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001186 if (debug)
1187 printk(KERN_DEBUG "xc2028: reusing device\n");
1188
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001189 break;
1190 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001191 }
1192
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001193 if (video_dev) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001194 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Chris Pascoeaa501be2007-11-19 04:45:38 -03001195 if (priv == NULL) {
1196 mutex_unlock(&xc2028_list_mutex);
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001197 return NULL;
Chris Pascoeaa501be2007-11-19 04:45:38 -03001198 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001199
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001200 priv->i2c_props.addr = cfg->i2c_addr;
1201 priv->i2c_props.adap = cfg->i2c_adap;
Michael Krufky27566652008-04-22 14:41:53 -03001202 priv->i2c_props.name = "xc2028";
1203
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001204 priv->video_dev = video_dev;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001205 priv->tuner_callback = cfg->callback;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001206 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001207
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001208 mutex_init(&priv->lock);
1209
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001210 list_add_tail(&priv->xc2028_list, &xc2028_list);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001211 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001212
1213 fe->tuner_priv = priv;
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001214 priv->count++;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001215
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001216 if (debug)
1217 printk(KERN_DEBUG "xc2028: usage count is %i\n", priv->count);
1218
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001219 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001220 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001221
1222 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001223
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001224 if (cfg->ctrl)
1225 xc2028_set_config(fe, cfg->ctrl);
1226
Chris Pascoeaa501be2007-11-19 04:45:38 -03001227 mutex_unlock(&xc2028_list_mutex);
1228
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001229 return fe;
Michel Ludwig701672e2007-07-18 10:29:10 -03001230}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001231
Michel Ludwig701672e2007-07-18 10:29:10 -03001232EXPORT_SYMBOL(xc2028_attach);
1233
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001234MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001235MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001236MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1237MODULE_LICENSE("GPL");