blob: 0cbde17bfbb7a7471616112dda6dd5507f21e169 [file] [log] [blame]
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001/* tuner-xc2028
2 *
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -03003 * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org)
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03004 *
Michel Ludwig701672e2007-07-18 10:29:10 -03005 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03007 *
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03008 * This code is placed under the terms of the GNU General Public License v2
9 */
10
11#include <linux/i2c.h>
12#include <asm/div64.h>
13#include <linux/firmware.h>
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -030014#include <linux/videodev2.h>
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030015#include <linux/delay.h>
Michel Ludwig701672e2007-07-18 10:29:10 -030016#include <media/tuner.h>
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -030017#include <linux/mutex.h>
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030018#include "tuner-i2c.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030019#include "tuner-xc2028.h"
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030020#include "tuner-xc2028-types.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030021
Michel Ludwig701672e2007-07-18 10:29:10 -030022#include <linux/dvb/frontend.h>
23#include "dvb_frontend.h"
24
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -030025
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030026static int debug;
27module_param(debug, int, 0644);
28MODULE_PARM_DESC(debug, "enable verbose debug messages");
29
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -030030static char audio_std[8];
31module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
32MODULE_PARM_DESC(audio_std,
33 "Audio standard. XC3028 audio decoder explicitly "
34 "needs to know what audio\n"
35 "standard is needed for some video standards with audio A2 or NICAM.\n"
36 "The valid values are:\n"
37 "A2\n"
38 "A2/A\n"
39 "A2/B\n"
40 "NICAM\n"
41 "NICAM/A\n"
42 "NICAM/B\n");
43
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -030044static char firmware_name[FIRMWARE_NAME_MAX];
45module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
46MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
47 "default firmware name\n");
48
Michael Krufkyc663d032008-04-18 21:22:50 -030049static LIST_HEAD(hybrid_tuner_instance_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 {
Michael Krufkyc663d032008-04-18 21:22:50 -030071 struct list_head hybrid_tuner_instance_list;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030072 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;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030076 __u32 frequency;
77
78 struct firmware_description *firm;
79 int firm_size;
Chris Pascoe06fd82d2007-11-19 06:06:08 -030080 __u16 firm_version;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030081
Chris Pascoe8bf799a2007-11-19 11:35:45 -030082 __u16 hwmodel;
83 __u16 hwvers;
84
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030085 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030086
Chris Pascoee0f0b372007-11-19 11:22:03 -030087 struct firmware_properties cur_fw;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030088
89 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030090};
91
Chris Pascoe47cc5b72007-11-19 04:14:23 -030092#define i2c_send(priv, buf, size) ({ \
93 int _rc; \
94 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
95 if (size != _rc) \
96 tuner_info("i2c output error: rc = %d (should be %d)\n",\
97 _rc, (int)size); \
98 _rc; \
99})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300100
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300101#define i2c_rcv(priv, buf, size) ({ \
102 int _rc; \
103 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
104 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300105 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300106 _rc, (int)size); \
107 _rc; \
108})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300109
Chris Pascoe7d58d112007-11-19 04:31:58 -0300110#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
111 int _rc; \
112 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
113 ibuf, isize); \
114 if (isize != _rc) \
115 tuner_err("i2c input error: rc = %d (should be %d)\n", \
116 _rc, (int)isize); \
117 _rc; \
118})
119
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300120#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300121 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300122 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300123 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300124 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300125 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300126 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
127 } else \
128 msleep(10); \
129 _rc; \
130})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300131
Devin Heitmueller83244022008-04-17 21:41:16 -0300132static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300133{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300134 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300135 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300136
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300137 tuner_dbg("%s %04x called\n", __func__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300138
Chris Pascoe7d58d112007-11-19 04:31:58 -0300139 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300140 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300141
Chris Pascoe7d58d112007-11-19 04:31:58 -0300142 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
143 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300144
Chris Pascoe7d58d112007-11-19 04:31:58 -0300145 *val = (ibuf[1]) | (ibuf[0] << 8);
146 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300147}
148
Chris Pascoee0262682007-12-02 06:30:50 -0300149#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Adrian Bunk29bec0b2008-04-22 14:41:45 -0300150static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300151{
152 if (type & BASE)
153 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300154 if (type & INIT1)
155 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300156 if (type & F8MHZ)
157 printk("F8MHZ ");
158 if (type & MTS)
159 printk("MTS ");
160 if (type & D2620)
161 printk("D2620 ");
162 if (type & D2633)
163 printk("D2633 ");
164 if (type & DTV6)
165 printk("DTV6 ");
166 if (type & QAM)
167 printk("QAM ");
168 if (type & DTV7)
169 printk("DTV7 ");
170 if (type & DTV78)
171 printk("DTV78 ");
172 if (type & DTV8)
173 printk("DTV8 ");
174 if (type & FM)
175 printk("FM ");
176 if (type & INPUT1)
177 printk("INPUT1 ");
178 if (type & LCD)
179 printk("LCD ");
180 if (type & NOGD)
181 printk("NOGD ");
182 if (type & MONO)
183 printk("MONO ");
184 if (type & ATSC)
185 printk("ATSC ");
186 if (type & IF)
187 printk("IF ");
188 if (type & LG60)
189 printk("LG60 ");
190 if (type & ATI638)
191 printk("ATI638 ");
192 if (type & OREN538)
193 printk("OREN538 ");
194 if (type & OREN36)
195 printk("OREN36 ");
196 if (type & TOYOTA388)
197 printk("TOYOTA388 ");
198 if (type & TOYOTA794)
199 printk("TOYOTA794 ");
200 if (type & DIBCOM52)
201 printk("DIBCOM52 ");
202 if (type & ZARLINK456)
203 printk("ZARLINK456 ");
204 if (type & CHINA)
205 printk("CHINA ");
206 if (type & F6MHZ)
207 printk("F6MHZ ");
208 if (type & INPUT2)
209 printk("INPUT2 ");
210 if (type & SCODE)
211 printk("SCODE ");
Chris Pascoee0262682007-12-02 06:30:50 -0300212 if (type & HAS_IF)
213 printk("HAS_IF_%d ", int_freq);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300214}
215
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300216static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300217{
Chris Pascoee155d902007-11-19 04:16:47 -0300218 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300219 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300220 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300221 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300222 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300223 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300224 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300225 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300226 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300227 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300228 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300229 return V4L2_STD_NICAM_B;
230
231 return 0;
232}
233
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300234static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300235{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300236 int i;
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -0300237 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300238
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
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300263 tuner_dbg("%s called\n", __func__);
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;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300406 unsigned int type_mask = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300407
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300408 tuner_dbg("%s called, want type=", __func__);
Chris Pascoeb1535292007-11-19 10:04:06 -0300409 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)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300423 type_mask = 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;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300426 type_mask = SCODE_TYPES & ~HAS_IF;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300427 } else if (type & DTV_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300428 type_mask = DTV_TYPES;
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300429 else if (type & STD_SPECIFIC_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300430 type_mask = STD_SPECIFIC_TYPES;
431
432 type &= type_mask;
433
Harvey Harrison8367fe22008-04-25 01:28:10 -0300434 if (!(type & SCODE))
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300435 type_mask = ~0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300436
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300437 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300438 for (i = 0; i < priv->firm_size; i++) {
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300439 if ((type == (priv->firm[i].type & type_mask)) &&
Chris Pascoeef207fe2007-12-02 09:30:55 -0300440 (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300441 goto found;
442 }
443
444 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300445 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300446 v4l2_std_id match_mask;
447 int nr_matches;
448
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300449 if (type != (priv->firm[i].type & type_mask))
Chris Pascoeb1535292007-11-19 10:04:06 -0300450 continue;
451
452 match_mask = *id & priv->firm[i].id;
453 if (!match_mask)
454 continue;
455
456 if ((*id & match_mask) == *id)
457 goto found; /* Supports all the requested standards */
458
459 nr_matches = hweight64(match_mask);
460 if (nr_matches > best_nr_matches) {
461 best_nr_matches = nr_matches;
462 best_i = i;
463 }
464 }
465
466 if (best_nr_matches > 0) {
467 tuner_dbg("Selecting best matching firmware (%d bits) for "
468 "type=", best_nr_matches);
469 dump_firm_type(type);
470 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
471 i = best_i;
472 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300473 }
474
475 /*FIXME: Would make sense to seek for type "hint" match ? */
476
Chris Pascoeb1535292007-11-19 10:04:06 -0300477 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300478 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300479
480found:
481 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300482
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300483ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300484 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300485 if (debug) {
486 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300487 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300488 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300489 return i;
490}
491
492static int load_firmware(struct dvb_frontend *fe, unsigned int type,
493 v4l2_std_id *id)
494{
495 struct xc2028_data *priv = fe->tuner_priv;
496 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300497 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300498
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300499 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300500
501 pos = seek_firmware(fe, type, id);
502 if (pos < 0)
503 return pos;
504
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300505 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300506 dump_firm_type(priv->firm[pos].type);
507 printk("(%x), id %016llx.\n", priv->firm[pos].type,
508 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300509
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300510 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300511 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300512
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300513 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300514 __u16 size;
515
516 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300517 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300518 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300519 return -EINVAL;
520 }
521
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300522 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300523 p += sizeof(size);
524
525 if (size == 0xffff)
526 return 0;
527
528 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300529 /* Special callback command received */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300530 rc = priv->tuner_callback(priv->video_dev,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300531 XC2028_TUNER_RESET, 0);
532 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300533 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300534 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300535 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300536 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300537 continue;
538 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300539 if (size >= 0xff00) {
540 switch (size) {
541 case 0xff00:
542 rc = priv->tuner_callback(priv->video_dev,
543 XC2028_RESET_CLK, 0);
544 if (rc < 0) {
545 tuner_err("Error at RESET code %d\n",
546 (*p) & 0x7f);
547 return -EINVAL;
548 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300549 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300550 default:
551 tuner_info("Invalid RESET code %d\n",
552 size & 0x7f);
553 return -EINVAL;
554
555 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300556 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300557 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300558
559 /* Checks for a sleep command */
560 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300561 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300562 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300563 }
564
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300565 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300566 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300567 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300568 return -EINVAL;
569 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300570
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300571 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300572 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300573 size--;
574
575 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300576 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300577 int len = (size < priv->ctrl.max_len - 1) ?
578 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300579
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300580 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300581
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300582 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300583 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300584 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300585 return -EINVAL;
586 }
587
588 p += len;
589 size -= len;
590 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300591 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300592 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300593}
594
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300595static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300596 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300597{
598 struct xc2028_data *priv = fe->tuner_priv;
599 int pos, rc;
600 unsigned char *p;
601
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300602 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300603
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300604 if (!int_freq) {
605 pos = seek_firmware(fe, type, id);
606 if (pos < 0)
607 return pos;
608 } else {
609 for (pos = 0; pos < priv->firm_size; pos++) {
610 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300611 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300612 break;
613 }
614 if (pos == priv->firm_size)
615 return -ENOENT;
616 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300617
618 p = priv->firm[pos].ptr;
619
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300620 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300621 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
622 return -EINVAL;
623 p += 12 * scode;
624 } else {
625 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
626 * has a 2-byte size header in the firmware format. */
627 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
628 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
629 return -EINVAL;
630 p += 14 * scode + 2;
631 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300632
Chris Pascoed7b22c52007-11-19 10:12:45 -0300633 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300634 dump_firm_type_and_int_freq(priv->firm[pos].type,
635 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300636 printk("(%x), id %016llx.\n", priv->firm[pos].type,
637 (unsigned long long)*id);
638
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300639 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300640 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
641 else
642 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
643 if (rc < 0)
644 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300645
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300646 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300647 if (rc < 0)
648 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300649
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300650 rc = send_seq(priv, {0x00, 0x8c});
651 if (rc < 0)
652 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300653
654 return 0;
655}
656
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300657static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300658 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300659{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300660 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300661 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300662 int rc = 0, is_retry = 0;
663 u16 version, hwmodel;
664 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300665
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300666 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300667
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300668 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300669 if (!priv->ctrl.fname) {
670 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300671 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300672 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300673
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300674 rc = load_all_firmwares(fe);
675 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300676 return rc;
677 }
678
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300679 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300680 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300681
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300682retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300683 new_fw.type = type;
684 new_fw.id = std;
685 new_fw.std_req = std;
686 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
687 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300688 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300689
690 tuner_dbg("checking firmware, user requested type=");
691 if (debug) {
692 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300693 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300694 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300695 if (!int_freq) {
696 printk("scode_tbl ");
697 dump_firm_type(priv->ctrl.scode_table);
698 printk("(%x), ", priv->ctrl.scode_table);
699 } else
700 printk("int_freq %d, ", new_fw.int_freq);
701 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300702 }
703
704 /* No need to reload base firmware if it matches */
705 if (((BASE | new_fw.type) & BASE_TYPES) ==
706 (priv->cur_fw.type & BASE_TYPES)) {
707 tuner_dbg("BASE firmware not changed.\n");
708 goto skip_base;
709 }
710
711 /* Updating BASE - forget about all currently loaded firmware */
712 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
713
714 /* Reset is needed before loading firmware */
715 rc = priv->tuner_callback(priv->video_dev,
716 XC2028_TUNER_RESET, 0);
717 if (rc < 0)
718 goto fail;
719
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300720 /* BASE firmwares are all std0 */
721 std0 = 0;
722 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300723 if (rc < 0) {
724 tuner_err("Error %d while loading base firmware\n",
725 rc);
726 goto fail;
727 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300728
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300729 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300730 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300731
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300732 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300733 if (rc == -ENOENT)
734 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
735 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300736 if (rc < 0 && rc != -ENOENT) {
737 tuner_err("Error %d while loading init1 firmware\n",
738 rc);
739 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300740 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300741
Chris Pascoee0f0b372007-11-19 11:22:03 -0300742skip_base:
743 /*
744 * No need to reload standard specific firmware if base firmware
745 * was not reloaded and requested video standards have not changed.
746 */
747 if (priv->cur_fw.type == (BASE | new_fw.type) &&
748 priv->cur_fw.std_req == std) {
749 tuner_dbg("Std-specific firmware already loaded.\n");
750 goto skip_std_specific;
751 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300752
Chris Pascoee0f0b372007-11-19 11:22:03 -0300753 /* Reloading std-specific firmware forces a SCODE update */
754 priv->cur_fw.scode_table = 0;
755
Chris Pascoee0f0b372007-11-19 11:22:03 -0300756 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300757 if (rc == -ENOENT)
758 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
759
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300760 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300761 goto fail;
762
763skip_std_specific:
764 if (priv->cur_fw.scode_table == new_fw.scode_table &&
765 priv->cur_fw.scode_nr == new_fw.scode_nr) {
766 tuner_dbg("SCODE firmware already loaded.\n");
767 goto check_device;
768 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300769
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300770 if (new_fw.type & FM)
771 goto check_device;
772
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300773 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300774 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300775
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300776 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
777 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300778
Chris Pascoee0f0b372007-11-19 11:22:03 -0300779check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300780 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
781 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
782 tuner_err("Unable to read tuner registers.\n");
783 goto fail;
784 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300785
Devin Heitmuellerb37f2d62008-04-21 07:02:09 -0300786 tuner_dbg("Device is Xceive %d version %d.%d, "
787 "firmware version %d.%d\n",
788 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
789 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300790
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300791 /* Check firmware version against what we downloaded. */
792 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
793 tuner_err("Incorrect readback of firmware version.\n");
794 goto fail;
795 }
796
797 /* Check that the tuner hardware model remains consistent over time. */
798 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
799 priv->hwmodel = hwmodel;
800 priv->hwvers = version & 0xff00;
801 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
802 priv->hwvers != (version & 0xff00)) {
803 tuner_err("Read invalid device hardware information - tuner "
804 "hung?\n");
805 goto fail;
806 }
807
Chris Pascoee0f0b372007-11-19 11:22:03 -0300808 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
809
810 /*
811 * By setting BASE in cur_fw.type only after successfully loading all
812 * firmwares, we can:
813 * 1. Identify that BASE firmware with type=0 has been loaded;
814 * 2. Tell whether BASE firmware was just changed the next time through.
815 */
816 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300817
818 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300819
820fail:
821 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300822 if (!is_retry) {
823 msleep(50);
824 is_retry = 1;
825 tuner_dbg("Retrying firmware load\n");
826 goto retry;
827 }
828
Chris Pascoee0f0b372007-11-19 11:22:03 -0300829 if (rc == -ENOENT)
830 rc = -EINVAL;
831 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300832}
833
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300834static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300835{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300836 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300837 u16 frq_lock, signal = 0;
838 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300839
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300840 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300841
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300842 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300843
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300844 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300845 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300846 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300847 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300848
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300849 /* Frequency is locked */
850 if (frq_lock == 1)
851 signal = 32768;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300852
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300853 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300854 rc = xc2028_get_reg(priv, 0x0040, &signal);
855 if (rc < 0)
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300856 goto ret;
857
858 /* Use both frq_lock and signal to generate the result */
859 signal = signal || ((signal & 0x07) << 12);
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300860
861ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300862 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300863
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300864 *strength = signal;
865
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300866 tuner_dbg("signal strength is %d\n", signal);
867
Chris Pascoe7d58d112007-11-19 04:31:58 -0300868 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300869}
870
871#define DIV 15625
872
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300873static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300874 enum tuner_mode new_mode,
875 unsigned int type,
876 v4l2_std_id std,
877 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300878{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300879 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300880 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300881 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300882 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300883
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300884 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300885
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300886 mutex_lock(&priv->lock);
887
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300888 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300889
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300890 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300891 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300892
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300893 /* On some cases xc2028 can disable video output, if
894 * very weak signals are received. By sending a soft
895 * reset, this is re-enabled. So, it is better to always
896 * send a soft reset before changing channels, to be sure
897 * that xc2028 will be in a safe state.
898 * Maybe this might also be needed for DTV.
899 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300900 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300901 rc = send_seq(priv, {0x00, 0x00});
Michael Krufkyd536c9d2007-12-18 10:42:33 -0300902 } else if (priv->cur_fw.type & ATSC) {
903 offset = 1750000;
904 } else {
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300905 offset = 2750000;
Chris Pascoe897b8422007-12-02 09:39:18 -0300906 /*
907 * We must adjust the offset by 500kHz in two cases in order
908 * to correctly center the IF output:
909 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
910 * selected and a 7MHz channel is tuned;
911 * 2) When tuning a VHF channel with DTV78 firmware.
912 */
913 if (((priv->cur_fw.type & DTV7) &&
914 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
915 ((priv->cur_fw.type & DTV78) && freq < 470000000))
Chris Pascoea44f1c42007-11-19 06:35:26 -0300916 offset -= 500000;
917 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300918
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300919 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300920
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300921 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300922 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300923 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
924 else
925 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
926 if (rc < 0)
927 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300928
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -0300929 /* Return code shouldn't be checked.
930 The reset CLK is needed only with tm6000.
931 Driver should work fine even if this fails.
932 */
933 priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300934
935 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300936
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300937 buf[0] = 0xff & (div >> 24);
938 buf[1] = 0xff & (div >> 16);
939 buf[2] = 0xff & (div >> 8);
940 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300941
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300942 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300943 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300944 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300945 msleep(100);
946
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300947 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300948
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300949 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
950 buf[0], buf[1], buf[2], buf[3],
951 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300952
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300953 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300954
955ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300956 mutex_unlock(&priv->lock);
957
958 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300959}
960
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300961static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300962 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300963{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300964 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300965 unsigned int type=0;
966
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300967 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -0300968
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300969 if (p->mode == V4L2_TUNER_RADIO) {
970 type |= FM;
971 if (priv->ctrl.input1)
972 type |= INPUT1;
973 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300974 T_ANALOG_TV, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300975 }
976
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -0300977 /* if std is not defined, choose one */
978 if (!p->std)
979 p->std = V4L2_STD_MN;
980
981 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300982 if (!(p->std & V4L2_STD_MN))
983 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -0300984
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300985 /* Add audio hack to std mask */
986 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300987
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300988 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300989 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -0300990}
991
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300992static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -0300993 struct dvb_frontend_parameters *p)
994{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300995 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300996 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300997 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -0300998 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -0300999
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001000 tuner_dbg("%s called\n", __func__);
Michel Ludwig701672e2007-07-18 10:29:10 -03001001
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001002 if (priv->ctrl.d2633)
1003 type |= D2633;
1004 else
1005 type |= D2620;
1006
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001007 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001008 case FE_OFDM:
1009 bw = p->u.ofdm.bandwidth;
1010 break;
1011 case FE_QAM:
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001012 tuner_info("WARN: There are some reports that "
1013 "QAM 6 MHz doesn't work.\n"
1014 "If this works for you, please report by "
1015 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001016 bw = BANDWIDTH_6_MHZ;
1017 type |= QAM;
1018 break;
1019 case FE_ATSC:
1020 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -03001021 /* The only ATSC firmware (at least on v2.7) is D2633,
1022 so overrides ctrl->d2633 */
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001023 type |= ATSC| D2633;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -03001024 type &= ~D2620;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001025 break;
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001026 /* DVB-S is not supported */
1027 default:
1028 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001029 }
1030
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001031 switch (bw) {
1032 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001033 if (p->frequency < 470000000)
1034 priv->ctrl.vhfbw7 = 0;
1035 else
1036 priv->ctrl.uhfbw8 = 1;
1037 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1038 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001039 break;
1040 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001041 if (p->frequency < 470000000)
1042 priv->ctrl.vhfbw7 = 1;
1043 else
1044 priv->ctrl.uhfbw8 = 0;
1045 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1046 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001047 break;
1048 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001049 type |= DTV6;
1050 priv->ctrl.vhfbw7 = 0;
1051 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001052 break;
1053 default:
1054 tuner_err("error: bandwidth not supported.\n");
1055 };
1056
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001057 /* All S-code tables need a 200kHz shift */
1058 if (priv->ctrl.demod)
Chris Pascoead35ce92007-12-02 06:36:42 -03001059 demod = priv->ctrl.demod + 200;
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001060
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001061 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001062 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001063}
1064
Chris Pascoe45819c32007-11-19 11:41:20 -03001065
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001066static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001067{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001068 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001069
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001070 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001071
Chris Pascoeaa501be2007-11-19 04:45:38 -03001072 mutex_lock(&xc2028_list_mutex);
1073
Michael Krufkyc663d032008-04-18 21:22:50 -03001074 /* only perform final cleanup if this is the last instance */
1075 if (hybrid_tuner_report_instance_count(priv) == 1) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001076 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001077 free_firmware(priv);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001078 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001079
Michael Krufkyc663d032008-04-18 21:22:50 -03001080 if (priv)
1081 hybrid_tuner_release_state(priv);
1082
Chris Pascoeaa501be2007-11-19 04:45:38 -03001083 mutex_unlock(&xc2028_list_mutex);
1084
Michael Krufkyc663d032008-04-18 21:22:50 -03001085 fe->tuner_priv = NULL;
1086
Michel Ludwig701672e2007-07-18 10:29:10 -03001087 return 0;
1088}
1089
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001090static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001091{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001092 struct xc2028_data *priv = fe->tuner_priv;
1093
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001094 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001095
1096 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001097
1098 return 0;
1099}
1100
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001101static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001102{
1103 struct xc2028_data *priv = fe->tuner_priv;
1104 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001105 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001106
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001107 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001108
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001109 mutex_lock(&priv->lock);
1110
Chris Pascoe0a196b62007-11-19 09:29:59 -03001111 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001112 if (priv->ctrl.max_len < 9)
1113 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001114
1115 if (p->fname) {
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001116 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1117 kfree(priv->ctrl.fname);
1118 free_firmware(priv);
1119 }
1120
Chris Pascoe0a196b62007-11-19 09:29:59 -03001121 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1122 if (priv->ctrl.fname == NULL)
1123 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001124 }
1125
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001126 mutex_unlock(&priv->lock);
1127
Chris Pascoe0a196b62007-11-19 09:29:59 -03001128 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001129}
1130
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001131static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001132 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001133 .name = "Xceive XC3028",
1134 .frequency_min = 42000000,
1135 .frequency_max = 864000000,
1136 .frequency_step = 50000,
1137 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001138
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001139 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001140 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001141 .release = xc2028_dvb_release,
1142 .get_frequency = xc2028_get_frequency,
1143 .get_rf_strength = xc2028_signal,
1144 .set_params = xc2028_set_params,
Michel Ludwig701672e2007-07-18 10:29:10 -03001145};
1146
Michael Krufky7972f982007-12-21 16:12:09 -03001147struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1148 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001149{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001150 struct xc2028_data *priv;
Michael Krufkyc663d032008-04-18 21:22:50 -03001151 int instance;
Michel Ludwig701672e2007-07-18 10:29:10 -03001152
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001153 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001154 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001155
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001156 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001157 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001158
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001159 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001160 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001161 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001162 }
1163
Chris Pascoeaa501be2007-11-19 04:45:38 -03001164 mutex_lock(&xc2028_list_mutex);
1165
Michael Krufkyc663d032008-04-18 21:22:50 -03001166 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1167 hybrid_tuner_instance_list,
1168 cfg->i2c_adap, cfg->i2c_addr,
1169 "xc2028");
1170 switch (instance) {
1171 case 0:
1172 /* memory allocation failure */
1173 goto fail;
1174 break;
1175 case 1:
1176 /* new tuner instance */
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001177 priv->tuner_callback = cfg->callback;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001178 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001179
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001180 mutex_init(&priv->lock);
1181
Michael Krufkyc663d032008-04-18 21:22:50 -03001182 /* analog side (tuner-core) uses i2c_adap->algo_data.
1183 * digital side is not guaranteed to have algo_data defined.
1184 *
1185 * digital side will always have fe->dvb defined.
1186 * analog side (tuner-core) doesn't (yet) define fe->dvb.
1187 */
1188 priv->video_dev = ((fe->dvb) && (fe->dvb->priv)) ?
1189 fe->dvb->priv : cfg->i2c_adap->algo_data;
1190
1191 fe->tuner_priv = priv;
1192 break;
1193 case 2:
1194 /* existing tuner instance */
1195 fe->tuner_priv = priv;
1196 break;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001197 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001198
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001199 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001200 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001201
1202 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001203
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001204 if (cfg->ctrl)
1205 xc2028_set_config(fe, cfg->ctrl);
1206
Chris Pascoeaa501be2007-11-19 04:45:38 -03001207 mutex_unlock(&xc2028_list_mutex);
1208
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001209 return fe;
Michael Krufkyc663d032008-04-18 21:22:50 -03001210fail:
1211 mutex_unlock(&xc2028_list_mutex);
1212
1213 xc2028_dvb_release(fe);
1214 return NULL;
Michel Ludwig701672e2007-07-18 10:29:10 -03001215}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001216
Michel Ludwig701672e2007-07-18 10:29:10 -03001217EXPORT_SYMBOL(xc2028_attach);
1218
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001219MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001220MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001221MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1222MODULE_LICENSE("GPL");