blob: b65e6803e6c64983d07f4d6c7a00d1e2fefbb96e [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>
Al Viro84a9f332008-06-22 14:19:29 -030018#include <asm/unaligned.h>
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030019#include "tuner-i2c.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030020#include "tuner-xc2028.h"
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030021#include "tuner-xc2028-types.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030022
Michel Ludwig701672e2007-07-18 10:29:10 -030023#include <linux/dvb/frontend.h>
24#include "dvb_frontend.h"
25
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -030026
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030027static int debug;
28module_param(debug, int, 0644);
29MODULE_PARM_DESC(debug, "enable verbose debug messages");
30
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -030031static char audio_std[8];
32module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
33MODULE_PARM_DESC(audio_std,
34 "Audio standard. XC3028 audio decoder explicitly "
35 "needs to know what audio\n"
36 "standard is needed for some video standards with audio A2 or NICAM.\n"
37 "The valid values are:\n"
38 "A2\n"
39 "A2/A\n"
40 "A2/B\n"
41 "NICAM\n"
42 "NICAM/A\n"
43 "NICAM/B\n");
44
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -030045static char firmware_name[FIRMWARE_NAME_MAX];
46module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
47MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
48 "default firmware name\n");
49
Michael Krufkyc663d032008-04-18 21:22:50 -030050static LIST_HEAD(hybrid_tuner_instance_list);
Chris Pascoeaa501be2007-11-19 04:45:38 -030051static DEFINE_MUTEX(xc2028_list_mutex);
52
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030053/* struct for storing firmware table */
54struct firmware_description {
55 unsigned int type;
56 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030057 __u16 int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030058 unsigned char *ptr;
59 unsigned int size;
60};
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030061
Chris Pascoee0f0b372007-11-19 11:22:03 -030062struct firmware_properties {
63 unsigned int type;
64 v4l2_std_id id;
65 v4l2_std_id std_req;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030066 __u16 int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -030067 unsigned int scode_table;
68 int scode_nr;
69};
70
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030071struct xc2028_data {
Michael Krufkyc663d032008-04-18 21:22:50 -030072 struct list_head hybrid_tuner_instance_list;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030073 struct tuner_i2c_props i2c_props;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030074 __u32 frequency;
75
76 struct firmware_description *firm;
77 int firm_size;
Chris Pascoe06fd82d2007-11-19 06:06:08 -030078 __u16 firm_version;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030079
Chris Pascoe8bf799a2007-11-19 11:35:45 -030080 __u16 hwmodel;
81 __u16 hwvers;
82
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030083 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030084
Chris Pascoee0f0b372007-11-19 11:22:03 -030085 struct firmware_properties cur_fw;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030086
87 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030088};
89
Chris Pascoe47cc5b72007-11-19 04:14:23 -030090#define i2c_send(priv, buf, size) ({ \
91 int _rc; \
92 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
93 if (size != _rc) \
94 tuner_info("i2c output error: rc = %d (should be %d)\n",\
95 _rc, (int)size); \
96 _rc; \
97})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030098
Chris Pascoe47cc5b72007-11-19 04:14:23 -030099#define i2c_rcv(priv, buf, size) ({ \
100 int _rc; \
101 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
102 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300103 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300104 _rc, (int)size); \
105 _rc; \
106})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300107
Chris Pascoe7d58d112007-11-19 04:31:58 -0300108#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
109 int _rc; \
110 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
111 ibuf, isize); \
112 if (isize != _rc) \
113 tuner_err("i2c input error: rc = %d (should be %d)\n", \
114 _rc, (int)isize); \
115 _rc; \
116})
117
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300118#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300119 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300120 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300121 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300122 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300123 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300124 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
125 } else \
126 msleep(10); \
127 _rc; \
128})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300129
Devin Heitmueller83244022008-04-17 21:41:16 -0300130static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300131{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300132 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300133 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300134
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300135 tuner_dbg("%s %04x called\n", __func__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300136
Chris Pascoe7d58d112007-11-19 04:31:58 -0300137 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300138 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300139
Chris Pascoe7d58d112007-11-19 04:31:58 -0300140 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
141 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300142
Chris Pascoe7d58d112007-11-19 04:31:58 -0300143 *val = (ibuf[1]) | (ibuf[0] << 8);
144 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300145}
146
Chris Pascoee0262682007-12-02 06:30:50 -0300147#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Adrian Bunk29bec0b2008-04-22 14:41:45 -0300148static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300149{
150 if (type & BASE)
151 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300152 if (type & INIT1)
153 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300154 if (type & F8MHZ)
155 printk("F8MHZ ");
156 if (type & MTS)
157 printk("MTS ");
158 if (type & D2620)
159 printk("D2620 ");
160 if (type & D2633)
161 printk("D2633 ");
162 if (type & DTV6)
163 printk("DTV6 ");
164 if (type & QAM)
165 printk("QAM ");
166 if (type & DTV7)
167 printk("DTV7 ");
168 if (type & DTV78)
169 printk("DTV78 ");
170 if (type & DTV8)
171 printk("DTV8 ");
172 if (type & FM)
173 printk("FM ");
174 if (type & INPUT1)
175 printk("INPUT1 ");
176 if (type & LCD)
177 printk("LCD ");
178 if (type & NOGD)
179 printk("NOGD ");
180 if (type & MONO)
181 printk("MONO ");
182 if (type & ATSC)
183 printk("ATSC ");
184 if (type & IF)
185 printk("IF ");
186 if (type & LG60)
187 printk("LG60 ");
188 if (type & ATI638)
189 printk("ATI638 ");
190 if (type & OREN538)
191 printk("OREN538 ");
192 if (type & OREN36)
193 printk("OREN36 ");
194 if (type & TOYOTA388)
195 printk("TOYOTA388 ");
196 if (type & TOYOTA794)
197 printk("TOYOTA794 ");
198 if (type & DIBCOM52)
199 printk("DIBCOM52 ");
200 if (type & ZARLINK456)
201 printk("ZARLINK456 ");
202 if (type & CHINA)
203 printk("CHINA ");
204 if (type & F6MHZ)
205 printk("F6MHZ ");
206 if (type & INPUT2)
207 printk("INPUT2 ");
208 if (type & SCODE)
209 printk("SCODE ");
Chris Pascoee0262682007-12-02 06:30:50 -0300210 if (type & HAS_IF)
211 printk("HAS_IF_%d ", int_freq);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300212}
213
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300214static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300215{
Chris Pascoee155d902007-11-19 04:16:47 -0300216 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300217 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300218 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300219 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300220 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300221 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300222 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300223 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300224 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300225 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300226 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300227 return V4L2_STD_NICAM_B;
228
229 return 0;
230}
231
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300232static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300233{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300234 int i;
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -0300235 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300236
237 if (!priv->firm)
238 return;
239
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300240 for (i = 0; i < priv->firm_size; i++)
241 kfree(priv->firm[i].ptr);
242
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300243 kfree(priv->firm);
244
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300245 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300246 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300247
248 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300249}
250
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300251static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300252{
253 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300254 const struct firmware *fw = NULL;
David Woodhousec63e87e2008-05-24 00:13:34 +0100255 const unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300256 int rc = 0;
257 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300258 char name[33];
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300259 char *fname;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300260
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300261 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300262
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300263 if (!firmware_name[0])
264 fname = priv->ctrl.fname;
265 else
266 fname = firmware_name;
267
268 tuner_dbg("Reading firmware %s\n", fname);
269 rc = request_firmware(&fw, fname, &priv->i2c_props.adap->dev);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300270 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300271 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300272 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300273 fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300274 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300275 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300276 rc, fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300277
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300278 return rc;
279 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300280 p = fw->data;
281 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300282
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300283 if (fw->size < sizeof(name) - 1 + 2 + 2) {
284 tuner_err("Error: firmware file %s has invalid size!\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300285 fname);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300286 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300287 }
288
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300289 memcpy(name, p, sizeof(name) - 1);
290 name[sizeof(name) - 1] = 0;
291 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300292
Al Viro84a9f332008-06-22 14:19:29 -0300293 priv->firm_version = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300294 p += 2;
295
Al Viro84a9f332008-06-22 14:19:29 -0300296 n_array = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300297 p += 2;
298
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300299 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300300 n_array, fname, name,
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300301 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300302
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300303 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300304 if (priv->firm == NULL) {
305 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300306 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300307 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300308 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300309 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300310
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300311 n = -1;
312 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300313 __u32 type, size;
314 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300315 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300316
317 n++;
318 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300319 tuner_err("More firmware images in file than "
320 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300321 goto corrupt;
322 }
323
324 /* Checks if there's enough bytes to read */
Al Viro84a9f332008-06-22 14:19:29 -0300325 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
326 goto header;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300327
Al Viro84a9f332008-06-22 14:19:29 -0300328 type = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300329 p += sizeof(type);
330
Al Viro84a9f332008-06-22 14:19:29 -0300331 id = get_unaligned_le64(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300332 p += sizeof(id);
333
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300334 if (type & HAS_IF) {
Al Viro84a9f332008-06-22 14:19:29 -0300335 int_freq = get_unaligned_le16(p);
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300336 p += sizeof(int_freq);
Al Viro84a9f332008-06-22 14:19:29 -0300337 if (endp - p < sizeof(size))
338 goto header;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300339 }
340
Al Viro84a9f332008-06-22 14:19:29 -0300341 size = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300342 p += sizeof(size);
343
Al Viro84a9f332008-06-22 14:19:29 -0300344 if (!size || size > endp - p) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300345 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300346 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300347 printk("(%x), id %llx is corrupted "
348 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300349 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300350 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300351 goto corrupt;
352 }
353
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300354 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300355 if (priv->firm[n].ptr == NULL) {
356 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300357 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300358 goto err;
359 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300360 tuner_dbg("Reading firmware type ");
361 if (debug) {
Chris Pascoee0262682007-12-02 06:30:50 -0300362 dump_firm_type_and_int_freq(type, int_freq);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300363 printk("(%x), id %llx, size=%d.\n",
Chris Pascoee0262682007-12-02 06:30:50 -0300364 type, (unsigned long long)id, size);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300365 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300366
367 memcpy(priv->firm[n].ptr, p, size);
368 priv->firm[n].type = type;
369 priv->firm[n].id = id;
370 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300371 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300372
373 p += size;
374 }
375
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300376 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300377 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300378 goto corrupt;
379 }
380
381 goto done;
382
Al Viro84a9f332008-06-22 14:19:29 -0300383header:
384 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300385corrupt:
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
Michael Krufkyd7cba042008-09-12 13:31:45 -0300492static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
493{
494 struct xc2028_data *priv = fe->tuner_priv;
495
496 /* analog side (tuner-core) uses i2c_adap->algo_data.
497 * digital side is not guaranteed to have algo_data defined.
498 *
499 * digital side will always have fe->dvb defined.
500 * analog side (tuner-core) doesn't (yet) define fe->dvb.
501 */
502
503 return (!fe->callback) ? -EINVAL :
504 fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
505 fe->dvb->priv : priv->i2c_props.adap->algo_data,
506 DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
507}
508
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300509static int load_firmware(struct dvb_frontend *fe, unsigned int type,
510 v4l2_std_id *id)
511{
512 struct xc2028_data *priv = fe->tuner_priv;
513 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300514 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300515
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300516 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300517
518 pos = seek_firmware(fe, type, id);
519 if (pos < 0)
520 return pos;
521
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300522 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300523 dump_firm_type(priv->firm[pos].type);
524 printk("(%x), id %016llx.\n", priv->firm[pos].type,
525 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300526
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300527 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300528 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300529
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300530 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300531 __u16 size;
532
533 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300534 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300535 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300536 return -EINVAL;
537 }
538
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300539 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300540 p += sizeof(size);
541
542 if (size == 0xffff)
543 return 0;
544
545 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300546 /* Special callback command received */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300547 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300548 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300549 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300550 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300551 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300552 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300553 continue;
554 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300555 if (size >= 0xff00) {
556 switch (size) {
557 case 0xff00:
Michael Krufkyd7cba042008-09-12 13:31:45 -0300558 rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
Michel Ludwig5403bba2007-11-16 07:49:49 -0300559 if (rc < 0) {
560 tuner_err("Error at RESET code %d\n",
561 (*p) & 0x7f);
562 return -EINVAL;
563 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300564 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300565 default:
566 tuner_info("Invalid RESET code %d\n",
567 size & 0x7f);
568 return -EINVAL;
569
570 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300571 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300572 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300573
574 /* Checks for a sleep command */
575 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300576 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300577 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300578 }
579
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300580 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300581 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300582 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300583 return -EINVAL;
584 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300585
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300586 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300587 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300588 size--;
589
590 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300591 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300592 int len = (size < priv->ctrl.max_len - 1) ?
593 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300594
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300595 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300596
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300597 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300598 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300599 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300600 return -EINVAL;
601 }
602
603 p += len;
604 size -= len;
605 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300606 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300607 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300608}
609
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300610static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300611 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300612{
613 struct xc2028_data *priv = fe->tuner_priv;
614 int pos, rc;
615 unsigned char *p;
616
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300617 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300618
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300619 if (!int_freq) {
620 pos = seek_firmware(fe, type, id);
621 if (pos < 0)
622 return pos;
623 } else {
624 for (pos = 0; pos < priv->firm_size; pos++) {
625 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300626 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300627 break;
628 }
629 if (pos == priv->firm_size)
630 return -ENOENT;
631 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300632
633 p = priv->firm[pos].ptr;
634
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300635 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300636 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
637 return -EINVAL;
638 p += 12 * scode;
639 } else {
640 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
641 * has a 2-byte size header in the firmware format. */
642 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
643 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
644 return -EINVAL;
645 p += 14 * scode + 2;
646 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300647
Chris Pascoed7b22c52007-11-19 10:12:45 -0300648 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300649 dump_firm_type_and_int_freq(priv->firm[pos].type,
650 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300651 printk("(%x), id %016llx.\n", priv->firm[pos].type,
652 (unsigned long long)*id);
653
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300654 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300655 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
656 else
657 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
658 if (rc < 0)
659 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300660
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300661 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300662 if (rc < 0)
663 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300664
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300665 rc = send_seq(priv, {0x00, 0x8c});
666 if (rc < 0)
667 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300668
669 return 0;
670}
671
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300672static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300673 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300674{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300675 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300676 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300677 int rc = 0, is_retry = 0;
678 u16 version, hwmodel;
679 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300680
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300681 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300682
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300683 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300684 if (!priv->ctrl.fname) {
685 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300686 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300687 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300688
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300689 rc = load_all_firmwares(fe);
690 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300691 return rc;
692 }
693
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300694 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300695 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300696
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300697retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300698 new_fw.type = type;
699 new_fw.id = std;
700 new_fw.std_req = std;
701 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
702 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300703 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300704
705 tuner_dbg("checking firmware, user requested type=");
706 if (debug) {
707 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300708 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300709 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300710 if (!int_freq) {
711 printk("scode_tbl ");
712 dump_firm_type(priv->ctrl.scode_table);
713 printk("(%x), ", priv->ctrl.scode_table);
714 } else
715 printk("int_freq %d, ", new_fw.int_freq);
716 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300717 }
718
719 /* No need to reload base firmware if it matches */
720 if (((BASE | new_fw.type) & BASE_TYPES) ==
721 (priv->cur_fw.type & BASE_TYPES)) {
722 tuner_dbg("BASE firmware not changed.\n");
723 goto skip_base;
724 }
725
726 /* Updating BASE - forget about all currently loaded firmware */
727 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
728
729 /* Reset is needed before loading firmware */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300730 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300731 if (rc < 0)
732 goto fail;
733
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300734 /* BASE firmwares are all std0 */
735 std0 = 0;
736 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300737 if (rc < 0) {
738 tuner_err("Error %d while loading base firmware\n",
739 rc);
740 goto fail;
741 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300742
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300743 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300744 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300745
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300746 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300747 if (rc == -ENOENT)
748 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
749 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300750 if (rc < 0 && rc != -ENOENT) {
751 tuner_err("Error %d while loading init1 firmware\n",
752 rc);
753 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300754 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300755
Chris Pascoee0f0b372007-11-19 11:22:03 -0300756skip_base:
757 /*
758 * No need to reload standard specific firmware if base firmware
759 * was not reloaded and requested video standards have not changed.
760 */
761 if (priv->cur_fw.type == (BASE | new_fw.type) &&
762 priv->cur_fw.std_req == std) {
763 tuner_dbg("Std-specific firmware already loaded.\n");
764 goto skip_std_specific;
765 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300766
Chris Pascoee0f0b372007-11-19 11:22:03 -0300767 /* Reloading std-specific firmware forces a SCODE update */
768 priv->cur_fw.scode_table = 0;
769
Chris Pascoee0f0b372007-11-19 11:22:03 -0300770 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300771 if (rc == -ENOENT)
772 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
773
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300774 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300775 goto fail;
776
777skip_std_specific:
778 if (priv->cur_fw.scode_table == new_fw.scode_table &&
779 priv->cur_fw.scode_nr == new_fw.scode_nr) {
780 tuner_dbg("SCODE firmware already loaded.\n");
781 goto check_device;
782 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300783
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300784 if (new_fw.type & FM)
785 goto check_device;
786
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300787 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300788 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300789
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300790 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
791 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300792
Chris Pascoee0f0b372007-11-19 11:22:03 -0300793check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300794 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
795 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
796 tuner_err("Unable to read tuner registers.\n");
797 goto fail;
798 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300799
Devin Heitmuellerb37f2d62008-04-21 07:02:09 -0300800 tuner_dbg("Device is Xceive %d version %d.%d, "
801 "firmware version %d.%d\n",
802 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
803 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300804
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300805 /* Check firmware version against what we downloaded. */
806 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
807 tuner_err("Incorrect readback of firmware version.\n");
808 goto fail;
809 }
810
811 /* Check that the tuner hardware model remains consistent over time. */
812 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
813 priv->hwmodel = hwmodel;
814 priv->hwvers = version & 0xff00;
815 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
816 priv->hwvers != (version & 0xff00)) {
817 tuner_err("Read invalid device hardware information - tuner "
818 "hung?\n");
819 goto fail;
820 }
821
Chris Pascoee0f0b372007-11-19 11:22:03 -0300822 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
823
824 /*
825 * By setting BASE in cur_fw.type only after successfully loading all
826 * firmwares, we can:
827 * 1. Identify that BASE firmware with type=0 has been loaded;
828 * 2. Tell whether BASE firmware was just changed the next time through.
829 */
830 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300831
832 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300833
834fail:
835 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300836 if (!is_retry) {
837 msleep(50);
838 is_retry = 1;
839 tuner_dbg("Retrying firmware load\n");
840 goto retry;
841 }
842
Chris Pascoee0f0b372007-11-19 11:22:03 -0300843 if (rc == -ENOENT)
844 rc = -EINVAL;
845 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300846}
847
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300848static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300849{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300850 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300851 u16 frq_lock, signal = 0;
852 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300853
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300854 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300855
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300856 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300857
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300858 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300859 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300860 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300861 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300862
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300863 /* Frequency is locked */
864 if (frq_lock == 1)
865 signal = 32768;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300866
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300867 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300868 rc = xc2028_get_reg(priv, 0x0040, &signal);
869 if (rc < 0)
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300870 goto ret;
871
872 /* Use both frq_lock and signal to generate the result */
873 signal = signal || ((signal & 0x07) << 12);
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300874
875ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300876 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300877
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300878 *strength = signal;
879
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300880 tuner_dbg("signal strength is %d\n", signal);
881
Chris Pascoe7d58d112007-11-19 04:31:58 -0300882 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300883}
884
885#define DIV 15625
886
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300887static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300888 enum tuner_mode new_mode,
889 unsigned int type,
890 v4l2_std_id std,
891 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300892{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300893 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300894 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300895 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300896 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300897
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300898 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300899
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300900 mutex_lock(&priv->lock);
901
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300902 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300903
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300904 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300905 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300906
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300907 /* On some cases xc2028 can disable video output, if
908 * very weak signals are received. By sending a soft
909 * reset, this is re-enabled. So, it is better to always
910 * send a soft reset before changing channels, to be sure
911 * that xc2028 will be in a safe state.
912 * Maybe this might also be needed for DTV.
913 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300914 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300915 rc = send_seq(priv, {0x00, 0x00});
Michael Krufkyd536c9d2007-12-18 10:42:33 -0300916 } else if (priv->cur_fw.type & ATSC) {
917 offset = 1750000;
918 } else {
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300919 offset = 2750000;
Chris Pascoe897b8422007-12-02 09:39:18 -0300920 /*
921 * We must adjust the offset by 500kHz in two cases in order
922 * to correctly center the IF output:
923 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
924 * selected and a 7MHz channel is tuned;
925 * 2) When tuning a VHF channel with DTV78 firmware.
926 */
927 if (((priv->cur_fw.type & DTV7) &&
928 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
929 ((priv->cur_fw.type & DTV78) && freq < 470000000))
Chris Pascoea44f1c42007-11-19 06:35:26 -0300930 offset -= 500000;
931 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300932
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300933 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300934
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300935 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300936 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300937 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
938 else
939 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
940 if (rc < 0)
941 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300942
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -0300943 /* Return code shouldn't be checked.
944 The reset CLK is needed only with tm6000.
945 Driver should work fine even if this fails.
946 */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300947 do_tuner_callback(fe, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300948
949 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300950
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300951 buf[0] = 0xff & (div >> 24);
952 buf[1] = 0xff & (div >> 16);
953 buf[2] = 0xff & (div >> 8);
954 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300955
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300956 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300957 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300958 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300959 msleep(100);
960
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300961 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300962
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300963 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
964 buf[0], buf[1], buf[2], buf[3],
965 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300966
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300967 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300968
969ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300970 mutex_unlock(&priv->lock);
971
972 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300973}
974
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300975static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300976 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300977{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300978 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300979 unsigned int type=0;
980
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300981 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -0300982
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300983 if (p->mode == V4L2_TUNER_RADIO) {
984 type |= FM;
985 if (priv->ctrl.input1)
986 type |= INPUT1;
987 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300988 T_ANALOG_TV, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300989 }
990
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -0300991 /* if std is not defined, choose one */
992 if (!p->std)
993 p->std = V4L2_STD_MN;
994
995 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300996 if (!(p->std & V4L2_STD_MN))
997 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -0300998
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300999 /* Add audio hack to std mask */
1000 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001001
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001002 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001003 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -03001004}
1005
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001006static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -03001007 struct dvb_frontend_parameters *p)
1008{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001009 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001010 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001011 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -03001012 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -03001013
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001014 tuner_dbg("%s called\n", __func__);
Michel Ludwig701672e2007-07-18 10:29:10 -03001015
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001016 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001017 case FE_OFDM:
1018 bw = p->u.ofdm.bandwidth;
1019 break;
1020 case FE_QAM:
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001021 tuner_info("WARN: There are some reports that "
1022 "QAM 6 MHz doesn't work.\n"
1023 "If this works for you, please report by "
1024 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001025 bw = BANDWIDTH_6_MHZ;
1026 type |= QAM;
1027 break;
1028 case FE_ATSC:
1029 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001030 /* The only ATSC firmware (at least on v2.7) is D2633 */
1031 type |= ATSC | D2633;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001032 break;
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001033 /* DVB-S is not supported */
1034 default:
1035 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001036 }
1037
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001038 switch (bw) {
1039 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001040 if (p->frequency < 470000000)
1041 priv->ctrl.vhfbw7 = 0;
1042 else
1043 priv->ctrl.uhfbw8 = 1;
1044 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1045 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001046 break;
1047 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001048 if (p->frequency < 470000000)
1049 priv->ctrl.vhfbw7 = 1;
1050 else
1051 priv->ctrl.uhfbw8 = 0;
1052 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1053 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001054 break;
1055 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001056 type |= DTV6;
1057 priv->ctrl.vhfbw7 = 0;
1058 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001059 break;
1060 default:
1061 tuner_err("error: bandwidth not supported.\n");
1062 };
1063
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001064 /*
1065 Selects between D2633 or D2620 firmware.
1066 It doesn't make sense for ATSC, since it should be D2633 on all cases
1067 */
1068 if (fe->ops.info.type != FE_ATSC) {
1069 switch (priv->ctrl.type) {
1070 case XC2028_D2633:
1071 type |= D2633;
1072 break;
1073 case XC2028_D2620:
1074 type |= D2620;
1075 break;
1076 case XC2028_AUTO:
1077 default:
1078 /* Zarlink seems to need D2633 */
1079 if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
1080 type |= D2633;
1081 else
1082 type |= D2620;
1083 }
1084 }
1085
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001086 /* All S-code tables need a 200kHz shift */
1087 if (priv->ctrl.demod)
Chris Pascoead35ce92007-12-02 06:36:42 -03001088 demod = priv->ctrl.demod + 200;
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001089
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001090 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001091 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001092}
1093
Chris Pascoe45819c32007-11-19 11:41:20 -03001094
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001095static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001096{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001097 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001098
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001099 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001100
Chris Pascoeaa501be2007-11-19 04:45:38 -03001101 mutex_lock(&xc2028_list_mutex);
1102
Michael Krufkyc663d032008-04-18 21:22:50 -03001103 /* only perform final cleanup if this is the last instance */
1104 if (hybrid_tuner_report_instance_count(priv) == 1) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001105 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001106 free_firmware(priv);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001107 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001108
Michael Krufkyc663d032008-04-18 21:22:50 -03001109 if (priv)
1110 hybrid_tuner_release_state(priv);
1111
Chris Pascoeaa501be2007-11-19 04:45:38 -03001112 mutex_unlock(&xc2028_list_mutex);
1113
Michael Krufkyc663d032008-04-18 21:22:50 -03001114 fe->tuner_priv = NULL;
1115
Michel Ludwig701672e2007-07-18 10:29:10 -03001116 return 0;
1117}
1118
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001119static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001120{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001121 struct xc2028_data *priv = fe->tuner_priv;
1122
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001123 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001124
1125 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001126
1127 return 0;
1128}
1129
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001130static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001131{
1132 struct xc2028_data *priv = fe->tuner_priv;
1133 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001134 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001135
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001136 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001137
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001138 mutex_lock(&priv->lock);
1139
Chris Pascoe0a196b62007-11-19 09:29:59 -03001140 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001141 if (priv->ctrl.max_len < 9)
1142 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001143
1144 if (p->fname) {
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001145 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1146 kfree(priv->ctrl.fname);
1147 free_firmware(priv);
1148 }
1149
Chris Pascoe0a196b62007-11-19 09:29:59 -03001150 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1151 if (priv->ctrl.fname == NULL)
1152 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001153 }
1154
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001155 mutex_unlock(&priv->lock);
1156
Chris Pascoe0a196b62007-11-19 09:29:59 -03001157 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001158}
1159
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001160static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001161 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001162 .name = "Xceive XC3028",
1163 .frequency_min = 42000000,
1164 .frequency_max = 864000000,
1165 .frequency_step = 50000,
1166 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001167
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001168 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001169 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001170 .release = xc2028_dvb_release,
1171 .get_frequency = xc2028_get_frequency,
1172 .get_rf_strength = xc2028_signal,
1173 .set_params = xc2028_set_params,
Michel Ludwig701672e2007-07-18 10:29:10 -03001174};
1175
Michael Krufky7972f982007-12-21 16:12:09 -03001176struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1177 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001178{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001179 struct xc2028_data *priv;
Michael Krufkyc663d032008-04-18 21:22:50 -03001180 int instance;
Michel Ludwig701672e2007-07-18 10:29:10 -03001181
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001182 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001183 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001184
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001185 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001186 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001187
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001188 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001189 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001190 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001191 }
1192
Chris Pascoeaa501be2007-11-19 04:45:38 -03001193 mutex_lock(&xc2028_list_mutex);
1194
Michael Krufkyc663d032008-04-18 21:22:50 -03001195 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1196 hybrid_tuner_instance_list,
1197 cfg->i2c_adap, cfg->i2c_addr,
1198 "xc2028");
1199 switch (instance) {
1200 case 0:
1201 /* memory allocation failure */
1202 goto fail;
1203 break;
1204 case 1:
1205 /* new tuner instance */
Chris Pascoe0a196b62007-11-19 09:29:59 -03001206 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001207
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001208 mutex_init(&priv->lock);
1209
Michael Krufkyc663d032008-04-18 21:22:50 -03001210 fe->tuner_priv = priv;
1211 break;
1212 case 2:
1213 /* existing tuner instance */
1214 fe->tuner_priv = priv;
1215 break;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001216 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001217
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001218 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001219 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001220
1221 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001222
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001223 if (cfg->ctrl)
1224 xc2028_set_config(fe, cfg->ctrl);
1225
Chris Pascoeaa501be2007-11-19 04:45:38 -03001226 mutex_unlock(&xc2028_list_mutex);
1227
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001228 return fe;
Michael Krufkyc663d032008-04-18 21:22:50 -03001229fail:
1230 mutex_unlock(&xc2028_list_mutex);
1231
1232 xc2028_dvb_release(fe);
1233 return NULL;
Michel Ludwig701672e2007-07-18 10:29:10 -03001234}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001235
Michel Ludwig701672e2007-07-18 10:29:10 -03001236EXPORT_SYMBOL(xc2028_attach);
1237
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001238MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001239MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001240MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1241MODULE_LICENSE("GPL");