blob: 96d61707f501cfc28b7caf1136741fcfc415f0aa [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Al Viro84a9f332008-06-22 14:19:29 -030019#include <asm/unaligned.h>
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030020#include "tuner-i2c.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030021#include "tuner-xc2028.h"
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030022#include "tuner-xc2028-types.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030023
Michel Ludwig701672e2007-07-18 10:29:10 -030024#include <linux/dvb/frontend.h>
25#include "dvb_frontend.h"
26
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -030027
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030028static int debug;
29module_param(debug, int, 0644);
30MODULE_PARM_DESC(debug, "enable verbose debug messages");
31
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -030032static int no_poweroff;
33module_param(no_poweroff, int, 0644);
Devin Heitmueller49008772009-04-28 16:22:47 -030034MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -030035 "1 keep device energized and with tuner ready all the times.\n"
36 " Faster, but consumes more power and keeps the device hotter\n");
37
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -030038static char audio_std[8];
39module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
40MODULE_PARM_DESC(audio_std,
41 "Audio standard. XC3028 audio decoder explicitly "
42 "needs to know what audio\n"
43 "standard is needed for some video standards with audio A2 or NICAM.\n"
44 "The valid values are:\n"
45 "A2\n"
46 "A2/A\n"
47 "A2/B\n"
48 "NICAM\n"
49 "NICAM/A\n"
50 "NICAM/B\n");
51
Samuel Ortiz4327b772009-05-27 00:49:33 +020052static char firmware_name[30];
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -030053module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
54MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
55 "default firmware name\n");
56
Michael Krufkyc663d032008-04-18 21:22:50 -030057static LIST_HEAD(hybrid_tuner_instance_list);
Chris Pascoeaa501be2007-11-19 04:45:38 -030058static DEFINE_MUTEX(xc2028_list_mutex);
59
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030060/* struct for storing firmware table */
61struct firmware_description {
62 unsigned int type;
63 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030064 __u16 int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030065 unsigned char *ptr;
66 unsigned int size;
67};
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030068
Chris Pascoee0f0b372007-11-19 11:22:03 -030069struct firmware_properties {
70 unsigned int type;
71 v4l2_std_id id;
72 v4l2_std_id std_req;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030073 __u16 int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -030074 unsigned int scode_table;
75 int scode_nr;
76};
77
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030078struct xc2028_data {
Michael Krufkyc663d032008-04-18 21:22:50 -030079 struct list_head hybrid_tuner_instance_list;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030080 struct tuner_i2c_props i2c_props;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030081 __u32 frequency;
82
83 struct firmware_description *firm;
84 int firm_size;
Chris Pascoe06fd82d2007-11-19 06:06:08 -030085 __u16 firm_version;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030086
Chris Pascoe8bf799a2007-11-19 11:35:45 -030087 __u16 hwmodel;
88 __u16 hwvers;
89
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030090 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030091
Chris Pascoee0f0b372007-11-19 11:22:03 -030092 struct firmware_properties cur_fw;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030093
94 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030095};
96
Chris Pascoe47cc5b72007-11-19 04:14:23 -030097#define i2c_send(priv, buf, size) ({ \
98 int _rc; \
99 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
100 if (size != _rc) \
101 tuner_info("i2c output error: rc = %d (should be %d)\n",\
102 _rc, (int)size); \
103 _rc; \
104})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300105
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300106#define i2c_rcv(priv, buf, size) ({ \
107 int _rc; \
108 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
109 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300110 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300111 _rc, (int)size); \
112 _rc; \
113})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300114
Chris Pascoe7d58d112007-11-19 04:31:58 -0300115#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
116 int _rc; \
117 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
118 ibuf, isize); \
119 if (isize != _rc) \
120 tuner_err("i2c input error: rc = %d (should be %d)\n", \
121 _rc, (int)isize); \
122 _rc; \
123})
124
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300125#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300126 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300127 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300128 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300129 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300130 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300131 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
132 } else \
133 msleep(10); \
134 _rc; \
135})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300136
Devin Heitmueller83244022008-04-17 21:41:16 -0300137static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300138{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300139 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300140 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300141
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300142 tuner_dbg("%s %04x called\n", __func__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300143
Chris Pascoe7d58d112007-11-19 04:31:58 -0300144 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300145 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300146
Chris Pascoe7d58d112007-11-19 04:31:58 -0300147 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
148 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300149
Chris Pascoe7d58d112007-11-19 04:31:58 -0300150 *val = (ibuf[1]) | (ibuf[0] << 8);
151 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300152}
153
Chris Pascoee0262682007-12-02 06:30:50 -0300154#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Adrian Bunk29bec0b2008-04-22 14:41:45 -0300155static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300156{
157 if (type & BASE)
158 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300159 if (type & INIT1)
160 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300161 if (type & F8MHZ)
162 printk("F8MHZ ");
163 if (type & MTS)
164 printk("MTS ");
165 if (type & D2620)
166 printk("D2620 ");
167 if (type & D2633)
168 printk("D2633 ");
169 if (type & DTV6)
170 printk("DTV6 ");
171 if (type & QAM)
172 printk("QAM ");
173 if (type & DTV7)
174 printk("DTV7 ");
175 if (type & DTV78)
176 printk("DTV78 ");
177 if (type & DTV8)
178 printk("DTV8 ");
179 if (type & FM)
180 printk("FM ");
181 if (type & INPUT1)
182 printk("INPUT1 ");
183 if (type & LCD)
184 printk("LCD ");
185 if (type & NOGD)
186 printk("NOGD ");
187 if (type & MONO)
188 printk("MONO ");
189 if (type & ATSC)
190 printk("ATSC ");
191 if (type & IF)
192 printk("IF ");
193 if (type & LG60)
194 printk("LG60 ");
195 if (type & ATI638)
196 printk("ATI638 ");
197 if (type & OREN538)
198 printk("OREN538 ");
199 if (type & OREN36)
200 printk("OREN36 ");
201 if (type & TOYOTA388)
202 printk("TOYOTA388 ");
203 if (type & TOYOTA794)
204 printk("TOYOTA794 ");
205 if (type & DIBCOM52)
206 printk("DIBCOM52 ");
207 if (type & ZARLINK456)
208 printk("ZARLINK456 ");
209 if (type & CHINA)
210 printk("CHINA ");
211 if (type & F6MHZ)
212 printk("F6MHZ ");
213 if (type & INPUT2)
214 printk("INPUT2 ");
215 if (type & SCODE)
216 printk("SCODE ");
Chris Pascoee0262682007-12-02 06:30:50 -0300217 if (type & HAS_IF)
218 printk("HAS_IF_%d ", int_freq);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300219}
220
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300221static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300222{
Chris Pascoee155d902007-11-19 04:16:47 -0300223 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300224 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300225 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300226 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300227 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300228 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300229 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300230 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300231 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300232 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300233 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300234 return V4L2_STD_NICAM_B;
235
236 return 0;
237}
238
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300239static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300240{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300241 int i;
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -0300242 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300243
244 if (!priv->firm)
245 return;
246
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300247 for (i = 0; i < priv->firm_size; i++)
248 kfree(priv->firm[i].ptr);
249
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300250 kfree(priv->firm);
251
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300252 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300253 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300254
255 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300256}
257
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300258static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300259{
260 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300261 const struct firmware *fw = NULL;
David Woodhousec63e87e2008-05-24 00:13:34 +0100262 const unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300263 int rc = 0;
264 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300265 char name[33];
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300266 char *fname;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300267
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300268 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300269
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300270 if (!firmware_name[0])
271 fname = priv->ctrl.fname;
272 else
273 fname = firmware_name;
274
275 tuner_dbg("Reading firmware %s\n", fname);
Jean Delvaree9785252009-04-26 05:43:59 -0300276 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300277 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300278 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300279 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300280 fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300281 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300282 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300283 rc, fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300284
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300285 return rc;
286 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300287 p = fw->data;
288 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300289
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300290 if (fw->size < sizeof(name) - 1 + 2 + 2) {
291 tuner_err("Error: firmware file %s has invalid size!\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300292 fname);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300293 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300294 }
295
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300296 memcpy(name, p, sizeof(name) - 1);
297 name[sizeof(name) - 1] = 0;
298 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300299
Al Viro84a9f332008-06-22 14:19:29 -0300300 priv->firm_version = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300301 p += 2;
302
Al Viro84a9f332008-06-22 14:19:29 -0300303 n_array = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300304 p += 2;
305
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300306 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300307 n_array, fname, name,
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300308 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300309
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300310 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300311 if (priv->firm == NULL) {
312 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300313 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300314 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300315 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300316 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300317
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300318 n = -1;
319 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300320 __u32 type, size;
321 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300322 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300323
324 n++;
325 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300326 tuner_err("More firmware images in file than "
327 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300328 goto corrupt;
329 }
330
331 /* Checks if there's enough bytes to read */
Al Viro84a9f332008-06-22 14:19:29 -0300332 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
333 goto header;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300334
Al Viro84a9f332008-06-22 14:19:29 -0300335 type = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300336 p += sizeof(type);
337
Al Viro84a9f332008-06-22 14:19:29 -0300338 id = get_unaligned_le64(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300339 p += sizeof(id);
340
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300341 if (type & HAS_IF) {
Al Viro84a9f332008-06-22 14:19:29 -0300342 int_freq = get_unaligned_le16(p);
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300343 p += sizeof(int_freq);
Al Viro84a9f332008-06-22 14:19:29 -0300344 if (endp - p < sizeof(size))
345 goto header;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300346 }
347
Al Viro84a9f332008-06-22 14:19:29 -0300348 size = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300349 p += sizeof(size);
350
Al Viro84a9f332008-06-22 14:19:29 -0300351 if (!size || size > endp - p) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300352 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300353 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300354 printk("(%x), id %llx is corrupted "
355 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300356 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300357 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300358 goto corrupt;
359 }
360
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300361 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300362 if (priv->firm[n].ptr == NULL) {
363 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300364 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300365 goto err;
366 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300367 tuner_dbg("Reading firmware type ");
368 if (debug) {
Chris Pascoee0262682007-12-02 06:30:50 -0300369 dump_firm_type_and_int_freq(type, int_freq);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300370 printk("(%x), id %llx, size=%d.\n",
Chris Pascoee0262682007-12-02 06:30:50 -0300371 type, (unsigned long long)id, size);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300372 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300373
374 memcpy(priv->firm[n].ptr, p, size);
375 priv->firm[n].type = type;
376 priv->firm[n].id = id;
377 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300378 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300379
380 p += size;
381 }
382
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300383 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300384 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300385 goto corrupt;
386 }
387
388 goto done;
389
Al Viro84a9f332008-06-22 14:19:29 -0300390header:
391 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300392corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300393 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300394 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300395
396err:
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300397 tuner_info("Releasing partially loaded firmware file.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300398 free_firmware(priv);
399
400done:
401 release_firmware(fw);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300402 if (rc == 0)
403 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300404
405 return rc;
406}
407
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300408static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
409 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300410{
411 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoeb1535292007-11-19 10:04:06 -0300412 int i, best_i = -1, best_nr_matches = 0;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300413 unsigned int type_mask = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300414
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300415 tuner_dbg("%s called, want type=", __func__);
Chris Pascoeb1535292007-11-19 10:04:06 -0300416 if (debug) {
417 dump_firm_type(type);
418 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
419 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300420
421 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300422 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300423 return -EINVAL;
424 }
425
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300426 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300427 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300428
Chris Pascoee0f0b372007-11-19 11:22:03 -0300429 if (type & BASE)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300430 type_mask = BASE_TYPES;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300431 else if (type & SCODE) {
Chris Pascoee0f0b372007-11-19 11:22:03 -0300432 type &= SCODE_TYPES;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300433 type_mask = SCODE_TYPES & ~HAS_IF;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300434 } else if (type & DTV_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300435 type_mask = DTV_TYPES;
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300436 else if (type & STD_SPECIFIC_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300437 type_mask = STD_SPECIFIC_TYPES;
438
439 type &= type_mask;
440
Harvey Harrison8367fe22008-04-25 01:28:10 -0300441 if (!(type & SCODE))
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300442 type_mask = ~0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300443
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300444 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300445 for (i = 0; i < priv->firm_size; i++) {
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300446 if ((type == (priv->firm[i].type & type_mask)) &&
Chris Pascoeef207fe2007-12-02 09:30:55 -0300447 (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300448 goto found;
449 }
450
451 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300452 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300453 v4l2_std_id match_mask;
454 int nr_matches;
455
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300456 if (type != (priv->firm[i].type & type_mask))
Chris Pascoeb1535292007-11-19 10:04:06 -0300457 continue;
458
459 match_mask = *id & priv->firm[i].id;
460 if (!match_mask)
461 continue;
462
463 if ((*id & match_mask) == *id)
464 goto found; /* Supports all the requested standards */
465
466 nr_matches = hweight64(match_mask);
467 if (nr_matches > best_nr_matches) {
468 best_nr_matches = nr_matches;
469 best_i = i;
470 }
471 }
472
473 if (best_nr_matches > 0) {
474 tuner_dbg("Selecting best matching firmware (%d bits) for "
475 "type=", best_nr_matches);
476 dump_firm_type(type);
477 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
478 i = best_i;
479 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300480 }
481
482 /*FIXME: Would make sense to seek for type "hint" match ? */
483
Chris Pascoeb1535292007-11-19 10:04:06 -0300484 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300485 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300486
487found:
488 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300489
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300490ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300491 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300492 if (debug) {
493 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300494 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300495 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300496 return i;
497}
498
Michael Krufkyd7cba042008-09-12 13:31:45 -0300499static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
500{
501 struct xc2028_data *priv = fe->tuner_priv;
502
503 /* analog side (tuner-core) uses i2c_adap->algo_data.
504 * digital side is not guaranteed to have algo_data defined.
505 *
506 * digital side will always have fe->dvb defined.
507 * analog side (tuner-core) doesn't (yet) define fe->dvb.
508 */
509
510 return (!fe->callback) ? -EINVAL :
511 fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
512 fe->dvb->priv : priv->i2c_props.adap->algo_data,
513 DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
514}
515
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300516static int load_firmware(struct dvb_frontend *fe, unsigned int type,
517 v4l2_std_id *id)
518{
519 struct xc2028_data *priv = fe->tuner_priv;
520 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300521 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300522
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300523 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300524
525 pos = seek_firmware(fe, type, id);
526 if (pos < 0)
527 return pos;
528
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300529 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300530 dump_firm_type(priv->firm[pos].type);
531 printk("(%x), id %016llx.\n", priv->firm[pos].type,
532 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300533
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300534 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300535 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300536
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300537 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300538 __u16 size;
539
540 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300541 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300542 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300543 return -EINVAL;
544 }
545
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300546 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300547 p += sizeof(size);
548
549 if (size == 0xffff)
550 return 0;
551
552 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300553 /* Special callback command received */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300554 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300555 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300556 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300557 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300558 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300559 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300560 continue;
561 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300562 if (size >= 0xff00) {
563 switch (size) {
564 case 0xff00:
Michael Krufkyd7cba042008-09-12 13:31:45 -0300565 rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
Michel Ludwig5403bba2007-11-16 07:49:49 -0300566 if (rc < 0) {
567 tuner_err("Error at RESET code %d\n",
568 (*p) & 0x7f);
569 return -EINVAL;
570 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300571 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300572 default:
573 tuner_info("Invalid RESET code %d\n",
574 size & 0x7f);
575 return -EINVAL;
576
577 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300578 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300579 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300580
581 /* Checks for a sleep command */
582 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300583 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300584 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300585 }
586
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300587 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300588 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300589 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300590 return -EINVAL;
591 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300592
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300593 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300594 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300595 size--;
596
597 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300598 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300599 int len = (size < priv->ctrl.max_len - 1) ?
600 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300601
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300602 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300603
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300604 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300605 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300606 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300607 return -EINVAL;
608 }
609
610 p += len;
611 size -= len;
612 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300613 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300614 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300615}
616
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300617static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300618 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300619{
620 struct xc2028_data *priv = fe->tuner_priv;
621 int pos, rc;
622 unsigned char *p;
623
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300624 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300625
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300626 if (!int_freq) {
627 pos = seek_firmware(fe, type, id);
628 if (pos < 0)
629 return pos;
630 } else {
631 for (pos = 0; pos < priv->firm_size; pos++) {
632 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300633 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300634 break;
635 }
636 if (pos == priv->firm_size)
637 return -ENOENT;
638 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300639
640 p = priv->firm[pos].ptr;
641
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300642 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300643 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
644 return -EINVAL;
645 p += 12 * scode;
646 } else {
647 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
648 * has a 2-byte size header in the firmware format. */
649 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
650 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
651 return -EINVAL;
652 p += 14 * scode + 2;
653 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300654
Chris Pascoed7b22c52007-11-19 10:12:45 -0300655 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300656 dump_firm_type_and_int_freq(priv->firm[pos].type,
657 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300658 printk("(%x), id %016llx.\n", priv->firm[pos].type,
659 (unsigned long long)*id);
660
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300661 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300662 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
663 else
664 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
665 if (rc < 0)
666 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300667
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300668 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300669 if (rc < 0)
670 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300671
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300672 rc = send_seq(priv, {0x00, 0x8c});
673 if (rc < 0)
674 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300675
676 return 0;
677}
678
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300679static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300680 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300681{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300682 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300683 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300684 int rc = 0, is_retry = 0;
685 u16 version, hwmodel;
686 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300687
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300688 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300689
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300690 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300691 if (!priv->ctrl.fname) {
692 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300693 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300694 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300695
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300696 rc = load_all_firmwares(fe);
697 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300698 return rc;
699 }
700
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300701 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300702 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300703
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300704retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300705 new_fw.type = type;
706 new_fw.id = std;
707 new_fw.std_req = std;
708 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
709 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300710 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300711
712 tuner_dbg("checking firmware, user requested type=");
713 if (debug) {
714 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300715 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300716 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300717 if (!int_freq) {
718 printk("scode_tbl ");
719 dump_firm_type(priv->ctrl.scode_table);
720 printk("(%x), ", priv->ctrl.scode_table);
721 } else
722 printk("int_freq %d, ", new_fw.int_freq);
723 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300724 }
725
726 /* No need to reload base firmware if it matches */
727 if (((BASE | new_fw.type) & BASE_TYPES) ==
728 (priv->cur_fw.type & BASE_TYPES)) {
729 tuner_dbg("BASE firmware not changed.\n");
730 goto skip_base;
731 }
732
733 /* Updating BASE - forget about all currently loaded firmware */
734 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
735
736 /* Reset is needed before loading firmware */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300737 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300738 if (rc < 0)
739 goto fail;
740
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300741 /* BASE firmwares are all std0 */
742 std0 = 0;
743 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300744 if (rc < 0) {
745 tuner_err("Error %d while loading base firmware\n",
746 rc);
747 goto fail;
748 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300749
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300750 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300751 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300752
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300753 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300754 if (rc == -ENOENT)
755 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
756 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300757 if (rc < 0 && rc != -ENOENT) {
758 tuner_err("Error %d while loading init1 firmware\n",
759 rc);
760 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300761 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300762
Chris Pascoee0f0b372007-11-19 11:22:03 -0300763skip_base:
764 /*
765 * No need to reload standard specific firmware if base firmware
766 * was not reloaded and requested video standards have not changed.
767 */
768 if (priv->cur_fw.type == (BASE | new_fw.type) &&
769 priv->cur_fw.std_req == std) {
770 tuner_dbg("Std-specific firmware already loaded.\n");
771 goto skip_std_specific;
772 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300773
Chris Pascoee0f0b372007-11-19 11:22:03 -0300774 /* Reloading std-specific firmware forces a SCODE update */
775 priv->cur_fw.scode_table = 0;
776
Chris Pascoee0f0b372007-11-19 11:22:03 -0300777 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300778 if (rc == -ENOENT)
779 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
780
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300781 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300782 goto fail;
783
784skip_std_specific:
785 if (priv->cur_fw.scode_table == new_fw.scode_table &&
786 priv->cur_fw.scode_nr == new_fw.scode_nr) {
787 tuner_dbg("SCODE firmware already loaded.\n");
788 goto check_device;
789 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300790
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300791 if (new_fw.type & FM)
792 goto check_device;
793
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300794 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300795 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300796
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300797 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
798 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300799
Chris Pascoee0f0b372007-11-19 11:22:03 -0300800check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300801 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
802 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
803 tuner_err("Unable to read tuner registers.\n");
804 goto fail;
805 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300806
Devin Heitmuellerb37f2d62008-04-21 07:02:09 -0300807 tuner_dbg("Device is Xceive %d version %d.%d, "
808 "firmware version %d.%d\n",
809 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
810 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300811
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300812 /* Check firmware version against what we downloaded. */
813 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
814 tuner_err("Incorrect readback of firmware version.\n");
815 goto fail;
816 }
817
818 /* Check that the tuner hardware model remains consistent over time. */
819 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
820 priv->hwmodel = hwmodel;
821 priv->hwvers = version & 0xff00;
822 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
823 priv->hwvers != (version & 0xff00)) {
824 tuner_err("Read invalid device hardware information - tuner "
825 "hung?\n");
826 goto fail;
827 }
828
Chris Pascoee0f0b372007-11-19 11:22:03 -0300829 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
830
831 /*
832 * By setting BASE in cur_fw.type only after successfully loading all
833 * firmwares, we can:
834 * 1. Identify that BASE firmware with type=0 has been loaded;
835 * 2. Tell whether BASE firmware was just changed the next time through.
836 */
837 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300838
839 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300840
841fail:
842 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300843 if (!is_retry) {
844 msleep(50);
845 is_retry = 1;
846 tuner_dbg("Retrying firmware load\n");
847 goto retry;
848 }
849
Chris Pascoee0f0b372007-11-19 11:22:03 -0300850 if (rc == -ENOENT)
851 rc = -EINVAL;
852 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300853}
854
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300855static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300856{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300857 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300858 u16 frq_lock, signal = 0;
859 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300860
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300861 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300862
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300863 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300864
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300865 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300866 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300867 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300868 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300869
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300870 /* Frequency is locked */
871 if (frq_lock == 1)
872 signal = 32768;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300873
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300874 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300875 rc = xc2028_get_reg(priv, 0x0040, &signal);
876 if (rc < 0)
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300877 goto ret;
878
879 /* Use both frq_lock and signal to generate the result */
880 signal = signal || ((signal & 0x07) << 12);
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300881
882ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300883 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300884
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300885 *strength = signal;
886
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300887 tuner_dbg("signal strength is %d\n", signal);
888
Chris Pascoe7d58d112007-11-19 04:31:58 -0300889 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300890}
891
892#define DIV 15625
893
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300894static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300895 enum tuner_mode new_mode,
896 unsigned int type,
897 v4l2_std_id std,
898 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300899{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300900 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300901 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300902 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300903 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300904
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300905 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300906
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300907 mutex_lock(&priv->lock);
908
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300909 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300910
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300911 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300912 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300913
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300914 /* On some cases xc2028 can disable video output, if
915 * very weak signals are received. By sending a soft
916 * reset, this is re-enabled. So, it is better to always
917 * send a soft reset before changing channels, to be sure
918 * that xc2028 will be in a safe state.
919 * Maybe this might also be needed for DTV.
920 */
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200921 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300922 rc = send_seq(priv, {0x00, 0x00});
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300923
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200924 /* Analog modes require offset = 0 */
925 } else {
926 /*
927 * Digital modes require an offset to adjust to the
928 * proper frequency. The offset depends on what
929 * firmware version is used.
930 */
931
932 /*
933 * Adjust to the center frequency. This is calculated by the
934 * formula: offset = 1.25MHz - BW/2
935 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
936 * further adjustment to get the frequency center on VHF
937 */
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300938 if (priv->cur_fw.type & DTV6)
939 offset = 1750000;
940 else if (priv->cur_fw.type & DTV7)
941 offset = 2250000;
942 else /* DTV8 or DTV78 */
943 offset = 2750000;
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300944 if ((priv->cur_fw.type & DTV78) && freq < 470000000)
Chris Pascoea44f1c42007-11-19 06:35:26 -0300945 offset -= 500000;
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200946
947 /*
948 * xc3028 additional "magic"
949 * Depending on the firmware version, it needs some adjustments
950 * to properly centralize the frequency. This seems to be
951 * needed to compensate the SCODE table adjustments made by
952 * newer firmwares
953 */
954
955#if 1
956 /*
957 * The proper adjustment would be to do it at s-code table.
958 * However, this didn't work, as reported by
959 * Robert Lowery <rglowery@exemail.com.au>
960 */
961
962 if (priv->cur_fw.type & DTV7)
963 offset += 500000;
964
965#else
966 /*
967 * Still need tests for XC3028L (firmware 3.2 or upper)
968 * So, for now, let's just comment the per-firmware
969 * version of this change. Reports with xc3028l working
970 * with and without the lines bellow are welcome
971 */
972
973 if (priv->firm_version < 0x0302) {
974 if (priv->cur_fw.type & DTV7)
975 offset += 500000;
976 } else {
977 if (priv->cur_fw.type & DTV7)
978 offset -= 300000;
979 else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
980 offset += 200000;
981 }
982#endif
Chris Pascoea44f1c42007-11-19 06:35:26 -0300983 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300984
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300985 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300986
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300987 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300988 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300989 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
990 else
991 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
992 if (rc < 0)
993 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300994
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -0300995 /* Return code shouldn't be checked.
996 The reset CLK is needed only with tm6000.
997 Driver should work fine even if this fails.
998 */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300999 do_tuner_callback(fe, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001000
1001 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -03001002
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001003 buf[0] = 0xff & (div >> 24);
1004 buf[1] = 0xff & (div >> 16);
1005 buf[2] = 0xff & (div >> 8);
1006 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001007
Chris Pascoe47cc5b72007-11-19 04:14:23 -03001008 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001009 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001010 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001011 msleep(100);
1012
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001013 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001014
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -03001015 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
1016 buf[0], buf[1], buf[2], buf[3],
1017 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001018
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001019 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001020
1021ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001022 mutex_unlock(&priv->lock);
1023
1024 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001025}
1026
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001027static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001028 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -03001029{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001030 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001031 unsigned int type=0;
1032
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001033 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -03001034
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001035 if (p->mode == V4L2_TUNER_RADIO) {
1036 type |= FM;
1037 if (priv->ctrl.input1)
1038 type |= INPUT1;
1039 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehabe2860d92009-06-06 08:15:08 -03001040 T_RADIO, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001041 }
1042
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -03001043 /* if std is not defined, choose one */
1044 if (!p->std)
1045 p->std = V4L2_STD_MN;
1046
1047 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001048 if (!(p->std & V4L2_STD_MN))
1049 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -03001050
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001051 /* Add audio hack to std mask */
1052 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001053
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001054 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001055 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -03001056}
1057
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001058static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -03001059 struct dvb_frontend_parameters *p)
1060{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001061 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001062 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001063 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -03001064 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -03001065
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001066 tuner_dbg("%s called\n", __func__);
Michel Ludwig701672e2007-07-18 10:29:10 -03001067
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001068 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001069 case FE_OFDM:
1070 bw = p->u.ofdm.bandwidth;
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001071 /*
1072 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1073 * Both seem to require QAM firmware for OFDM decoding
1074 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1075 */
1076 if (bw == BANDWIDTH_6_MHZ)
1077 type |= QAM;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001078 break;
1079 case FE_ATSC:
1080 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001081 /* The only ATSC firmware (at least on v2.7) is D2633 */
1082 type |= ATSC | D2633;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001083 break;
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001084 /* DVB-S and pure QAM (FE_QAM) are not supported */
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001085 default:
1086 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001087 }
1088
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001089 switch (bw) {
1090 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001091 if (p->frequency < 470000000)
1092 priv->ctrl.vhfbw7 = 0;
1093 else
1094 priv->ctrl.uhfbw8 = 1;
1095 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1096 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001097 break;
1098 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001099 if (p->frequency < 470000000)
1100 priv->ctrl.vhfbw7 = 1;
1101 else
1102 priv->ctrl.uhfbw8 = 0;
1103 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1104 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001105 break;
1106 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001107 type |= DTV6;
1108 priv->ctrl.vhfbw7 = 0;
1109 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001110 break;
1111 default:
1112 tuner_err("error: bandwidth not supported.\n");
1113 };
1114
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001115 /*
1116 Selects between D2633 or D2620 firmware.
1117 It doesn't make sense for ATSC, since it should be D2633 on all cases
1118 */
1119 if (fe->ops.info.type != FE_ATSC) {
1120 switch (priv->ctrl.type) {
1121 case XC2028_D2633:
1122 type |= D2633;
1123 break;
1124 case XC2028_D2620:
1125 type |= D2620;
1126 break;
1127 case XC2028_AUTO:
1128 default:
1129 /* Zarlink seems to need D2633 */
1130 if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
1131 type |= D2633;
1132 else
1133 type |= D2620;
1134 }
1135 }
1136
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001137 /* All S-code tables need a 200kHz shift */
Andy Walls6e707b42009-06-11 07:57:50 -03001138 if (priv->ctrl.demod) {
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001139 demod = priv->ctrl.demod;
1140
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001141 /*
1142 * Newer firmwares require a 200 kHz offset only for ATSC
1143 */
1144 if (type == ATSC || priv->firm_version < 0x0302)
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001145 demod += 200;
Andy Walls6e707b42009-06-11 07:57:50 -03001146 /*
1147 * The DTV7 S-code table needs a 700 kHz shift.
Andy Walls6e707b42009-06-11 07:57:50 -03001148 *
1149 * DTV7 is only used in Australia. Germany or Italy may also
1150 * use this firmware after initialization, but a tune to a UHF
1151 * channel should then cause DTV78 to be used.
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001152 *
1153 * Unfortunately, on real-field tests, the s-code offset
1154 * didn't work as expected, as reported by
1155 * Robert Lowery <rglowery@exemail.com.au>
Andy Walls6e707b42009-06-11 07:57:50 -03001156 */
Andy Walls6e707b42009-06-11 07:57:50 -03001157 }
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001158
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001159 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001160 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001161}
1162
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001163static int xc2028_sleep(struct dvb_frontend *fe)
1164{
1165 struct xc2028_data *priv = fe->tuner_priv;
1166 int rc = 0;
1167
Devin Heitmueller93b99922009-08-03 22:52:59 -03001168 /* Avoid firmware reload on slow devices or if PM disabled */
1169 if (no_poweroff || priv->ctrl.disable_power_mgmt)
Mauro Carvalho Chehab10f201a2008-12-05 10:49:53 -03001170 return 0;
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001171
1172 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
Mauro Carvalho Chehabe278e742008-12-18 06:00:25 -03001173 if (debug > 1) {
1174 tuner_dbg("Printing sleep stack trace:\n");
1175 dump_stack();
1176 }
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001177
1178 mutex_lock(&priv->lock);
1179
1180 if (priv->firm_version < 0x0202)
1181 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1182 else
1183 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1184
1185 priv->cur_fw.type = 0; /* need firmware reload */
1186
1187 mutex_unlock(&priv->lock);
1188
1189 return rc;
1190}
Chris Pascoe45819c32007-11-19 11:41:20 -03001191
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001192static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001193{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001194 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001195
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001196 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001197
Chris Pascoeaa501be2007-11-19 04:45:38 -03001198 mutex_lock(&xc2028_list_mutex);
1199
Michael Krufkyc663d032008-04-18 21:22:50 -03001200 /* only perform final cleanup if this is the last instance */
1201 if (hybrid_tuner_report_instance_count(priv) == 1) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001202 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001203 free_firmware(priv);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001204 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001205
Michael Krufkyc663d032008-04-18 21:22:50 -03001206 if (priv)
1207 hybrid_tuner_release_state(priv);
1208
Chris Pascoeaa501be2007-11-19 04:45:38 -03001209 mutex_unlock(&xc2028_list_mutex);
1210
Michael Krufkyc663d032008-04-18 21:22:50 -03001211 fe->tuner_priv = NULL;
1212
Michel Ludwig701672e2007-07-18 10:29:10 -03001213 return 0;
1214}
1215
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001216static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001217{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001218 struct xc2028_data *priv = fe->tuner_priv;
1219
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001220 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001221
1222 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001223
1224 return 0;
1225}
1226
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001227static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001228{
1229 struct xc2028_data *priv = fe->tuner_priv;
1230 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001231 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001232
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001233 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001234
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001235 mutex_lock(&priv->lock);
1236
Chris Pascoe0a196b62007-11-19 09:29:59 -03001237 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001238 if (priv->ctrl.max_len < 9)
1239 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001240
1241 if (p->fname) {
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001242 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1243 kfree(priv->ctrl.fname);
1244 free_firmware(priv);
1245 }
1246
Chris Pascoe0a196b62007-11-19 09:29:59 -03001247 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1248 if (priv->ctrl.fname == NULL)
1249 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001250 }
1251
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001252 mutex_unlock(&priv->lock);
1253
Chris Pascoe0a196b62007-11-19 09:29:59 -03001254 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001255}
1256
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001257static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001258 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001259 .name = "Xceive XC3028",
1260 .frequency_min = 42000000,
1261 .frequency_max = 864000000,
1262 .frequency_step = 50000,
1263 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001264
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001265 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001266 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001267 .release = xc2028_dvb_release,
1268 .get_frequency = xc2028_get_frequency,
1269 .get_rf_strength = xc2028_signal,
1270 .set_params = xc2028_set_params,
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001271 .sleep = xc2028_sleep,
Michel Ludwig701672e2007-07-18 10:29:10 -03001272};
1273
Michael Krufky7972f982007-12-21 16:12:09 -03001274struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1275 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001276{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001277 struct xc2028_data *priv;
Michael Krufkyc663d032008-04-18 21:22:50 -03001278 int instance;
Michel Ludwig701672e2007-07-18 10:29:10 -03001279
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001280 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001281 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001282
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001283 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001284 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001285
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001286 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001287 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001288 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001289 }
1290
Chris Pascoeaa501be2007-11-19 04:45:38 -03001291 mutex_lock(&xc2028_list_mutex);
1292
Michael Krufkyc663d032008-04-18 21:22:50 -03001293 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1294 hybrid_tuner_instance_list,
1295 cfg->i2c_adap, cfg->i2c_addr,
1296 "xc2028");
1297 switch (instance) {
1298 case 0:
1299 /* memory allocation failure */
1300 goto fail;
1301 break;
1302 case 1:
1303 /* new tuner instance */
Chris Pascoe0a196b62007-11-19 09:29:59 -03001304 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001305
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001306 mutex_init(&priv->lock);
1307
Michael Krufkyc663d032008-04-18 21:22:50 -03001308 fe->tuner_priv = priv;
1309 break;
1310 case 2:
1311 /* existing tuner instance */
1312 fe->tuner_priv = priv;
1313 break;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001314 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001315
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001316 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001317 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001318
1319 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001320
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001321 if (cfg->ctrl)
1322 xc2028_set_config(fe, cfg->ctrl);
1323
Chris Pascoeaa501be2007-11-19 04:45:38 -03001324 mutex_unlock(&xc2028_list_mutex);
1325
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001326 return fe;
Michael Krufkyc663d032008-04-18 21:22:50 -03001327fail:
1328 mutex_unlock(&xc2028_list_mutex);
1329
1330 xc2028_dvb_release(fe);
1331 return NULL;
Michel Ludwig701672e2007-07-18 10:29:10 -03001332}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001333
Michel Ludwig701672e2007-07-18 10:29:10 -03001334EXPORT_SYMBOL(xc2028_attach);
1335
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001336MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001337MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001338MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1339MODULE_LICENSE("GPL");