blob: 9dd688ec3cff4051d89d33d3af5a4a5c4e84630d [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
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;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300407 unsigned int ign_firm_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)
424 type &= 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;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300427 ign_firm_type_mask = HAS_IF;
428 } else if (type & DTV_TYPES)
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300429 type &= DTV_TYPES;
430 else if (type & STD_SPECIFIC_TYPES)
431 type &= STD_SPECIFIC_TYPES;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300432
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300433 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300434 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeef207fe2007-12-02 09:30:55 -0300435 if ((type == (priv->firm[i].type & ~ign_firm_type_mask)) &&
436 (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300437 goto found;
438 }
439
440 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300441 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300442 v4l2_std_id match_mask;
443 int nr_matches;
444
Chris Pascoeef207fe2007-12-02 09:30:55 -0300445 if (type != (priv->firm[i].type & ~ign_firm_type_mask))
Chris Pascoeb1535292007-11-19 10:04:06 -0300446 continue;
447
448 match_mask = *id & priv->firm[i].id;
449 if (!match_mask)
450 continue;
451
452 if ((*id & match_mask) == *id)
453 goto found; /* Supports all the requested standards */
454
455 nr_matches = hweight64(match_mask);
456 if (nr_matches > best_nr_matches) {
457 best_nr_matches = nr_matches;
458 best_i = i;
459 }
460 }
461
462 if (best_nr_matches > 0) {
463 tuner_dbg("Selecting best matching firmware (%d bits) for "
464 "type=", best_nr_matches);
465 dump_firm_type(type);
466 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
467 i = best_i;
468 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300469 }
470
471 /*FIXME: Would make sense to seek for type "hint" match ? */
472
Chris Pascoeb1535292007-11-19 10:04:06 -0300473 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300474 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300475
476found:
477 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300478
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300479ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300480 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300481 if (debug) {
482 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300483 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300484 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300485 return i;
486}
487
488static int load_firmware(struct dvb_frontend *fe, unsigned int type,
489 v4l2_std_id *id)
490{
491 struct xc2028_data *priv = fe->tuner_priv;
492 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300493 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300494
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300495 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300496
497 pos = seek_firmware(fe, type, id);
498 if (pos < 0)
499 return pos;
500
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300501 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300502 dump_firm_type(priv->firm[pos].type);
503 printk("(%x), id %016llx.\n", priv->firm[pos].type,
504 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300505
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300506 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300507 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300508
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300509 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300510 __u16 size;
511
512 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300513 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300514 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300515 return -EINVAL;
516 }
517
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300518 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300519 p += sizeof(size);
520
521 if (size == 0xffff)
522 return 0;
523
524 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300525 /* Special callback command received */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300526 rc = priv->tuner_callback(priv->video_dev,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300527 XC2028_TUNER_RESET, 0);
528 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300529 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300530 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300531 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300532 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300533 continue;
534 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300535 if (size >= 0xff00) {
536 switch (size) {
537 case 0xff00:
538 rc = priv->tuner_callback(priv->video_dev,
539 XC2028_RESET_CLK, 0);
540 if (rc < 0) {
541 tuner_err("Error at RESET code %d\n",
542 (*p) & 0x7f);
543 return -EINVAL;
544 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300545 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300546 default:
547 tuner_info("Invalid RESET code %d\n",
548 size & 0x7f);
549 return -EINVAL;
550
551 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300552 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300553 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300554
555 /* Checks for a sleep command */
556 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300557 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300558 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300559 }
560
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300561 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300562 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300563 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300564 return -EINVAL;
565 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300566
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300567 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300568 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300569 size--;
570
571 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300572 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300573 int len = (size < priv->ctrl.max_len - 1) ?
574 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300575
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300576 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300577
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300578 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300579 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300580 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300581 return -EINVAL;
582 }
583
584 p += len;
585 size -= len;
586 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300587 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300588 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300589}
590
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300591static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300592 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300593{
594 struct xc2028_data *priv = fe->tuner_priv;
595 int pos, rc;
596 unsigned char *p;
597
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300598 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300599
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300600 if (!int_freq) {
601 pos = seek_firmware(fe, type, id);
602 if (pos < 0)
603 return pos;
604 } else {
605 for (pos = 0; pos < priv->firm_size; pos++) {
606 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300607 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300608 break;
609 }
610 if (pos == priv->firm_size)
611 return -ENOENT;
612 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300613
614 p = priv->firm[pos].ptr;
615
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300616 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300617 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
618 return -EINVAL;
619 p += 12 * scode;
620 } else {
621 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
622 * has a 2-byte size header in the firmware format. */
623 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
624 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
625 return -EINVAL;
626 p += 14 * scode + 2;
627 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300628
Chris Pascoed7b22c52007-11-19 10:12:45 -0300629 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300630 dump_firm_type_and_int_freq(priv->firm[pos].type,
631 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300632 printk("(%x), id %016llx.\n", priv->firm[pos].type,
633 (unsigned long long)*id);
634
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300635 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300636 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
637 else
638 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
639 if (rc < 0)
640 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300641
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300642 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300643 if (rc < 0)
644 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300645
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300646 rc = send_seq(priv, {0x00, 0x8c});
647 if (rc < 0)
648 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300649
650 return 0;
651}
652
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300653static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300654 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300655{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300656 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300657 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300658 int rc = 0, is_retry = 0;
659 u16 version, hwmodel;
660 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300661
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300662 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300663
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300664 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300665 if (!priv->ctrl.fname) {
666 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300667 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300668 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300669
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300670 rc = load_all_firmwares(fe);
671 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300672 return rc;
673 }
674
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300675 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300676 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300677
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300678retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300679 new_fw.type = type;
680 new_fw.id = std;
681 new_fw.std_req = std;
682 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
683 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300684 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300685
686 tuner_dbg("checking firmware, user requested type=");
687 if (debug) {
688 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300689 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300690 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300691 if (!int_freq) {
692 printk("scode_tbl ");
693 dump_firm_type(priv->ctrl.scode_table);
694 printk("(%x), ", priv->ctrl.scode_table);
695 } else
696 printk("int_freq %d, ", new_fw.int_freq);
697 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300698 }
699
700 /* No need to reload base firmware if it matches */
701 if (((BASE | new_fw.type) & BASE_TYPES) ==
702 (priv->cur_fw.type & BASE_TYPES)) {
703 tuner_dbg("BASE firmware not changed.\n");
704 goto skip_base;
705 }
706
707 /* Updating BASE - forget about all currently loaded firmware */
708 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
709
710 /* Reset is needed before loading firmware */
711 rc = priv->tuner_callback(priv->video_dev,
712 XC2028_TUNER_RESET, 0);
713 if (rc < 0)
714 goto fail;
715
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300716 /* BASE firmwares are all std0 */
717 std0 = 0;
718 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300719 if (rc < 0) {
720 tuner_err("Error %d while loading base firmware\n",
721 rc);
722 goto fail;
723 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300724
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300725 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300726 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300727
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300728 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300729 if (rc == -ENOENT)
730 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
731 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300732 if (rc < 0 && rc != -ENOENT) {
733 tuner_err("Error %d while loading init1 firmware\n",
734 rc);
735 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300736 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300737
Chris Pascoee0f0b372007-11-19 11:22:03 -0300738skip_base:
739 /*
740 * No need to reload standard specific firmware if base firmware
741 * was not reloaded and requested video standards have not changed.
742 */
743 if (priv->cur_fw.type == (BASE | new_fw.type) &&
744 priv->cur_fw.std_req == std) {
745 tuner_dbg("Std-specific firmware already loaded.\n");
746 goto skip_std_specific;
747 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300748
Chris Pascoee0f0b372007-11-19 11:22:03 -0300749 /* Reloading std-specific firmware forces a SCODE update */
750 priv->cur_fw.scode_table = 0;
751
Chris Pascoee0f0b372007-11-19 11:22:03 -0300752 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300753 if (rc == -ENOENT)
754 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
755
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300756 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300757 goto fail;
758
759skip_std_specific:
760 if (priv->cur_fw.scode_table == new_fw.scode_table &&
761 priv->cur_fw.scode_nr == new_fw.scode_nr) {
762 tuner_dbg("SCODE firmware already loaded.\n");
763 goto check_device;
764 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300765
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300766 if (new_fw.type & FM)
767 goto check_device;
768
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300769 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300770 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300771
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300772 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
773 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300774
Chris Pascoee0f0b372007-11-19 11:22:03 -0300775check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300776 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
777 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
778 tuner_err("Unable to read tuner registers.\n");
779 goto fail;
780 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300781
782 tuner_info("Device is Xceive %d version %d.%d, "
783 "firmware version %d.%d\n",
784 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
785 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300786
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300787 /* Check firmware version against what we downloaded. */
788 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
789 tuner_err("Incorrect readback of firmware version.\n");
790 goto fail;
791 }
792
793 /* Check that the tuner hardware model remains consistent over time. */
794 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
795 priv->hwmodel = hwmodel;
796 priv->hwvers = version & 0xff00;
797 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
798 priv->hwvers != (version & 0xff00)) {
799 tuner_err("Read invalid device hardware information - tuner "
800 "hung?\n");
801 goto fail;
802 }
803
Chris Pascoee0f0b372007-11-19 11:22:03 -0300804 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
805
806 /*
807 * By setting BASE in cur_fw.type only after successfully loading all
808 * firmwares, we can:
809 * 1. Identify that BASE firmware with type=0 has been loaded;
810 * 2. Tell whether BASE firmware was just changed the next time through.
811 */
812 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300813
814 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300815
816fail:
817 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300818 if (!is_retry) {
819 msleep(50);
820 is_retry = 1;
821 tuner_dbg("Retrying firmware load\n");
822 goto retry;
823 }
824
Chris Pascoee0f0b372007-11-19 11:22:03 -0300825 if (rc == -ENOENT)
826 rc = -EINVAL;
827 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300828}
829
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300830static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300831{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300832 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300833 u16 frq_lock, signal = 0;
834 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300835
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300836 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300837
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300838 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300839
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300840 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300841 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
842 if (rc < 0 || frq_lock == 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300843 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300844
845 /* Frequency is locked. Return signal quality */
846
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300847 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300848 rc = xc2028_get_reg(priv, 0x0040, &signal);
849 if (rc < 0)
850 signal = -frq_lock;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300851
852ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300853 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300854
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300855 *strength = signal;
856
Chris Pascoe7d58d112007-11-19 04:31:58 -0300857 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300858}
859
860#define DIV 15625
861
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300862static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300863 enum tuner_mode new_mode,
864 unsigned int type,
865 v4l2_std_id std,
866 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300867{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300868 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300869 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300870 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300871 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300872
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300873 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300874
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300875 mutex_lock(&priv->lock);
876
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300877 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300878
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300879 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300880 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300881
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300882 /* On some cases xc2028 can disable video output, if
883 * very weak signals are received. By sending a soft
884 * reset, this is re-enabled. So, it is better to always
885 * send a soft reset before changing channels, to be sure
886 * that xc2028 will be in a safe state.
887 * Maybe this might also be needed for DTV.
888 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300889 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300890 rc = send_seq(priv, {0x00, 0x00});
Michael Krufkyd536c9d2007-12-18 10:42:33 -0300891 } else if (priv->cur_fw.type & ATSC) {
892 offset = 1750000;
893 } else {
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300894 offset = 2750000;
Chris Pascoe897b8422007-12-02 09:39:18 -0300895 /*
896 * We must adjust the offset by 500kHz in two cases in order
897 * to correctly center the IF output:
898 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
899 * selected and a 7MHz channel is tuned;
900 * 2) When tuning a VHF channel with DTV78 firmware.
901 */
902 if (((priv->cur_fw.type & DTV7) &&
903 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
904 ((priv->cur_fw.type & DTV78) && freq < 470000000))
Chris Pascoea44f1c42007-11-19 06:35:26 -0300905 offset -= 500000;
906 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300907
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300908 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300909
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300910 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300911 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300912 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
913 else
914 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
915 if (rc < 0)
916 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300917
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -0300918 /* Return code shouldn't be checked.
919 The reset CLK is needed only with tm6000.
920 Driver should work fine even if this fails.
921 */
922 priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300923
924 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300925
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300926 buf[0] = 0xff & (div >> 24);
927 buf[1] = 0xff & (div >> 16);
928 buf[2] = 0xff & (div >> 8);
929 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300930
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300931 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300932 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300933 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300934 msleep(100);
935
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300936 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300937
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300938 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
939 buf[0], buf[1], buf[2], buf[3],
940 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300941
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300942 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300943
944ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300945 mutex_unlock(&priv->lock);
946
947 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300948}
949
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300950static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300951 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300952{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300953 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300954 unsigned int type=0;
955
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300956 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -0300957
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300958 if (p->mode == V4L2_TUNER_RADIO) {
959 type |= FM;
960 if (priv->ctrl.input1)
961 type |= INPUT1;
962 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300963 T_ANALOG_TV, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300964 }
965
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -0300966 /* if std is not defined, choose one */
967 if (!p->std)
968 p->std = V4L2_STD_MN;
969
970 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300971 if (!(p->std & V4L2_STD_MN))
972 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -0300973
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300974 /* Add audio hack to std mask */
975 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300976
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300977 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300978 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -0300979}
980
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300981static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -0300982 struct dvb_frontend_parameters *p)
983{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300984 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300985 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300986 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -0300987 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -0300988
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300989 tuner_dbg("%s called\n", __func__);
Michel Ludwig701672e2007-07-18 10:29:10 -0300990
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300991 if (priv->ctrl.d2633)
992 type |= D2633;
993 else
994 type |= D2620;
995
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300996 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300997 case FE_OFDM:
998 bw = p->u.ofdm.bandwidth;
999 break;
1000 case FE_QAM:
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001001 tuner_info("WARN: There are some reports that "
1002 "QAM 6 MHz doesn't work.\n"
1003 "If this works for you, please report by "
1004 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001005 bw = BANDWIDTH_6_MHZ;
1006 type |= QAM;
1007 break;
1008 case FE_ATSC:
1009 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -03001010 /* The only ATSC firmware (at least on v2.7) is D2633,
1011 so overrides ctrl->d2633 */
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001012 type |= ATSC| D2633;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -03001013 type &= ~D2620;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001014 break;
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001015 /* DVB-S is not supported */
1016 default:
1017 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001018 }
1019
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001020 switch (bw) {
1021 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001022 if (p->frequency < 470000000)
1023 priv->ctrl.vhfbw7 = 0;
1024 else
1025 priv->ctrl.uhfbw8 = 1;
1026 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1027 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001028 break;
1029 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001030 if (p->frequency < 470000000)
1031 priv->ctrl.vhfbw7 = 1;
1032 else
1033 priv->ctrl.uhfbw8 = 0;
1034 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1035 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001036 break;
1037 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001038 type |= DTV6;
1039 priv->ctrl.vhfbw7 = 0;
1040 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001041 break;
1042 default:
1043 tuner_err("error: bandwidth not supported.\n");
1044 };
1045
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001046 /* All S-code tables need a 200kHz shift */
1047 if (priv->ctrl.demod)
Chris Pascoead35ce92007-12-02 06:36:42 -03001048 demod = priv->ctrl.demod + 200;
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001049
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001050 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001051 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001052}
1053
Chris Pascoe45819c32007-11-19 11:41:20 -03001054
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001055static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001056{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001057 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001058
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001059 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001060
Chris Pascoeaa501be2007-11-19 04:45:38 -03001061 mutex_lock(&xc2028_list_mutex);
1062
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001063 priv->count--;
1064
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001065 if (!priv->count) {
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001066 list_del(&priv->xc2028_list);
1067
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001068 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001069
1070 free_firmware(priv);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001071 kfree(priv);
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001072 fe->tuner_priv = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001073 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001074
Chris Pascoeaa501be2007-11-19 04:45:38 -03001075 mutex_unlock(&xc2028_list_mutex);
1076
Michel Ludwig701672e2007-07-18 10:29:10 -03001077 return 0;
1078}
1079
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001080static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001081{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001082 struct xc2028_data *priv = fe->tuner_priv;
1083
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001084 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001085
1086 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001087
1088 return 0;
1089}
1090
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001091static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001092{
1093 struct xc2028_data *priv = fe->tuner_priv;
1094 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001095 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001096
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001097 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001098
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001099 mutex_lock(&priv->lock);
1100
Chris Pascoe0a196b62007-11-19 09:29:59 -03001101 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001102 if (priv->ctrl.max_len < 9)
1103 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001104
1105 if (p->fname) {
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001106 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1107 kfree(priv->ctrl.fname);
1108 free_firmware(priv);
1109 }
1110
Chris Pascoe0a196b62007-11-19 09:29:59 -03001111 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1112 if (priv->ctrl.fname == NULL)
1113 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001114 }
1115
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001116 mutex_unlock(&priv->lock);
1117
Chris Pascoe0a196b62007-11-19 09:29:59 -03001118 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001119}
1120
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001121static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001122 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001123 .name = "Xceive XC3028",
1124 .frequency_min = 42000000,
1125 .frequency_max = 864000000,
1126 .frequency_step = 50000,
1127 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001128
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001129 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001130 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001131 .release = xc2028_dvb_release,
1132 .get_frequency = xc2028_get_frequency,
1133 .get_rf_strength = xc2028_signal,
1134 .set_params = xc2028_set_params,
Michel Ludwig701672e2007-07-18 10:29:10 -03001135};
1136
Michael Krufky7972f982007-12-21 16:12:09 -03001137struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1138 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001139{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001140 struct xc2028_data *priv;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001141 void *video_dev;
Michel Ludwig701672e2007-07-18 10:29:10 -03001142
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001143 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001144 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001145
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001146 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001147 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001148
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001149 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001150 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001151 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001152 }
1153
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001154 video_dev = cfg->i2c_adap->algo_data;
1155
1156 if (debug)
1157 printk(KERN_DEBUG "xc2028: video_dev =%p\n", video_dev);
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001158
Chris Pascoeaa501be2007-11-19 04:45:38 -03001159 mutex_lock(&xc2028_list_mutex);
1160
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001161 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001162 if (&priv->i2c_props.adap->dev == &cfg->i2c_adap->dev) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001163 video_dev = NULL;
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001164 if (debug)
1165 printk(KERN_DEBUG "xc2028: reusing device\n");
1166
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001167 break;
1168 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001169 }
1170
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001171 if (video_dev) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001172 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Chris Pascoeaa501be2007-11-19 04:45:38 -03001173 if (priv == NULL) {
1174 mutex_unlock(&xc2028_list_mutex);
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001175 return NULL;
Chris Pascoeaa501be2007-11-19 04:45:38 -03001176 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001177
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001178 priv->i2c_props.addr = cfg->i2c_addr;
1179 priv->i2c_props.adap = cfg->i2c_adap;
Michael Krufky27566652008-04-22 14:41:53 -03001180 priv->i2c_props.name = "xc2028";
1181
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001182 priv->video_dev = video_dev;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001183 priv->tuner_callback = cfg->callback;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001184 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001185
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001186 mutex_init(&priv->lock);
1187
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001188 list_add_tail(&priv->xc2028_list, &xc2028_list);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001189 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001190
1191 fe->tuner_priv = priv;
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001192 priv->count++;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001193
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001194 if (debug)
1195 printk(KERN_DEBUG "xc2028: usage count is %i\n", priv->count);
1196
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001197 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001198 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001199
1200 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001201
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001202 if (cfg->ctrl)
1203 xc2028_set_config(fe, cfg->ctrl);
1204
Chris Pascoeaa501be2007-11-19 04:45:38 -03001205 mutex_unlock(&xc2028_list_mutex);
1206
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001207 return fe;
Michel Ludwig701672e2007-07-18 10:29:10 -03001208}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001209
Michel Ludwig701672e2007-07-18 10:29:10 -03001210EXPORT_SYMBOL(xc2028_attach);
1211
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001212MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001213MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001214MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1215MODULE_LICENSE("GPL");