blob: 53f0fbb33cc631a234b50716a36be14b48b79b26 [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); \
Mauro Carvalho Chehabe5cc2bf2008-01-08 11:26:59 -0300103 msleep(priv->ctrl.msleep); \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300104 _rc; \
105})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300106
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300107#define i2c_rcv(priv, buf, size) ({ \
108 int _rc; \
109 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
110 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300111 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300112 _rc, (int)size); \
113 _rc; \
114})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300115
Chris Pascoe7d58d112007-11-19 04:31:58 -0300116#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
117 int _rc; \
118 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
119 ibuf, isize); \
120 if (isize != _rc) \
121 tuner_err("i2c input error: rc = %d (should be %d)\n", \
122 _rc, (int)isize); \
Mauro Carvalho Chehabe5cc2bf2008-01-08 11:26:59 -0300123 msleep(priv->ctrl.msleep); \
Chris Pascoe7d58d112007-11-19 04:31:58 -0300124 _rc; \
125})
126
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300127#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300128 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300129 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300130 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300131 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300132 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300133 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
134 } else \
Mauro Carvalho Chehabe5cc2bf2008-01-08 11:26:59 -0300135 msleep(priv->ctrl.msleep); \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300136 _rc; \
137})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300138
Devin Heitmueller83244022008-04-17 21:41:16 -0300139static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300140{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300141 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300142 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300143
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300144 tuner_dbg("%s %04x called\n", __func__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300145
Chris Pascoe7d58d112007-11-19 04:31:58 -0300146 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300147 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300148
Chris Pascoe7d58d112007-11-19 04:31:58 -0300149 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
150 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300151
Chris Pascoe7d58d112007-11-19 04:31:58 -0300152 *val = (ibuf[1]) | (ibuf[0] << 8);
153 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300154}
155
Chris Pascoee0262682007-12-02 06:30:50 -0300156#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Adrian Bunk29bec0b2008-04-22 14:41:45 -0300157static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300158{
159 if (type & BASE)
160 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300161 if (type & INIT1)
162 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300163 if (type & F8MHZ)
164 printk("F8MHZ ");
165 if (type & MTS)
166 printk("MTS ");
167 if (type & D2620)
168 printk("D2620 ");
169 if (type & D2633)
170 printk("D2633 ");
171 if (type & DTV6)
172 printk("DTV6 ");
173 if (type & QAM)
174 printk("QAM ");
175 if (type & DTV7)
176 printk("DTV7 ");
177 if (type & DTV78)
178 printk("DTV78 ");
179 if (type & DTV8)
180 printk("DTV8 ");
181 if (type & FM)
182 printk("FM ");
183 if (type & INPUT1)
184 printk("INPUT1 ");
185 if (type & LCD)
186 printk("LCD ");
187 if (type & NOGD)
188 printk("NOGD ");
189 if (type & MONO)
190 printk("MONO ");
191 if (type & ATSC)
192 printk("ATSC ");
193 if (type & IF)
194 printk("IF ");
195 if (type & LG60)
196 printk("LG60 ");
197 if (type & ATI638)
198 printk("ATI638 ");
199 if (type & OREN538)
200 printk("OREN538 ");
201 if (type & OREN36)
202 printk("OREN36 ");
203 if (type & TOYOTA388)
204 printk("TOYOTA388 ");
205 if (type & TOYOTA794)
206 printk("TOYOTA794 ");
207 if (type & DIBCOM52)
208 printk("DIBCOM52 ");
209 if (type & ZARLINK456)
210 printk("ZARLINK456 ");
211 if (type & CHINA)
212 printk("CHINA ");
213 if (type & F6MHZ)
214 printk("F6MHZ ");
215 if (type & INPUT2)
216 printk("INPUT2 ");
217 if (type & SCODE)
218 printk("SCODE ");
Chris Pascoee0262682007-12-02 06:30:50 -0300219 if (type & HAS_IF)
220 printk("HAS_IF_%d ", int_freq);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300221}
222
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300223static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300224{
Chris Pascoee155d902007-11-19 04:16:47 -0300225 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300226 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300227 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300228 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300229 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300230 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300231 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300232 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300233 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300234 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300235 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300236 return V4L2_STD_NICAM_B;
237
238 return 0;
239}
240
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300241static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300242{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300243 int i;
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -0300244 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300245
246 if (!priv->firm)
247 return;
248
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300249 for (i = 0; i < priv->firm_size; i++)
250 kfree(priv->firm[i].ptr);
251
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300252 kfree(priv->firm);
253
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300254 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300255 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300256
257 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300258}
259
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300260static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300261{
262 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300263 const struct firmware *fw = NULL;
David Woodhousec63e87e2008-05-24 00:13:34 +0100264 const unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300265 int rc = 0;
266 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300267 char name[33];
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300268 char *fname;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300269
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300270 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300271
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300272 if (!firmware_name[0])
273 fname = priv->ctrl.fname;
274 else
275 fname = firmware_name;
276
277 tuner_dbg("Reading firmware %s\n", fname);
Jean Delvaree9785252009-04-26 05:43:59 -0300278 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300279 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300280 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300281 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300282 fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300283 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300284 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300285 rc, fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300286
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300287 return rc;
288 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300289 p = fw->data;
290 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300291
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300292 if (fw->size < sizeof(name) - 1 + 2 + 2) {
293 tuner_err("Error: firmware file %s has invalid size!\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300294 fname);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300295 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300296 }
297
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300298 memcpy(name, p, sizeof(name) - 1);
299 name[sizeof(name) - 1] = 0;
300 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300301
Al Viro84a9f332008-06-22 14:19:29 -0300302 priv->firm_version = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300303 p += 2;
304
Al Viro84a9f332008-06-22 14:19:29 -0300305 n_array = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300306 p += 2;
307
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300308 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300309 n_array, fname, name,
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300310 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300311
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300312 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300313 if (priv->firm == NULL) {
314 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300315 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300316 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300317 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300318 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300319
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300320 n = -1;
321 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300322 __u32 type, size;
323 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300324 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300325
326 n++;
327 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300328 tuner_err("More firmware images in file than "
329 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300330 goto corrupt;
331 }
332
333 /* Checks if there's enough bytes to read */
Al Viro84a9f332008-06-22 14:19:29 -0300334 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
335 goto header;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300336
Al Viro84a9f332008-06-22 14:19:29 -0300337 type = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300338 p += sizeof(type);
339
Al Viro84a9f332008-06-22 14:19:29 -0300340 id = get_unaligned_le64(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300341 p += sizeof(id);
342
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300343 if (type & HAS_IF) {
Al Viro84a9f332008-06-22 14:19:29 -0300344 int_freq = get_unaligned_le16(p);
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300345 p += sizeof(int_freq);
Al Viro84a9f332008-06-22 14:19:29 -0300346 if (endp - p < sizeof(size))
347 goto header;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300348 }
349
Al Viro84a9f332008-06-22 14:19:29 -0300350 size = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300351 p += sizeof(size);
352
Al Viro84a9f332008-06-22 14:19:29 -0300353 if (!size || size > endp - p) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300354 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300355 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300356 printk("(%x), id %llx is corrupted "
357 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300358 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300359 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300360 goto corrupt;
361 }
362
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300363 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300364 if (priv->firm[n].ptr == NULL) {
365 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300366 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300367 goto err;
368 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300369 tuner_dbg("Reading firmware type ");
370 if (debug) {
Chris Pascoee0262682007-12-02 06:30:50 -0300371 dump_firm_type_and_int_freq(type, int_freq);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300372 printk("(%x), id %llx, size=%d.\n",
Chris Pascoee0262682007-12-02 06:30:50 -0300373 type, (unsigned long long)id, size);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300374 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300375
376 memcpy(priv->firm[n].ptr, p, size);
377 priv->firm[n].type = type;
378 priv->firm[n].id = id;
379 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300380 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300381
382 p += size;
383 }
384
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300385 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300386 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300387 goto corrupt;
388 }
389
390 goto done;
391
Al Viro84a9f332008-06-22 14:19:29 -0300392header:
393 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300394corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300395 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300396 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300397
398err:
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300399 tuner_info("Releasing partially loaded firmware file.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300400 free_firmware(priv);
401
402done:
403 release_firmware(fw);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300404 if (rc == 0)
405 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300406
407 return rc;
408}
409
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300410static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
411 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300412{
413 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoeb1535292007-11-19 10:04:06 -0300414 int i, best_i = -1, best_nr_matches = 0;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300415 unsigned int type_mask = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300416
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300417 tuner_dbg("%s called, want type=", __func__);
Chris Pascoeb1535292007-11-19 10:04:06 -0300418 if (debug) {
419 dump_firm_type(type);
420 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
421 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300422
423 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300424 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300425 return -EINVAL;
426 }
427
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300428 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300429 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300430
Chris Pascoee0f0b372007-11-19 11:22:03 -0300431 if (type & BASE)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300432 type_mask = BASE_TYPES;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300433 else if (type & SCODE) {
Chris Pascoee0f0b372007-11-19 11:22:03 -0300434 type &= SCODE_TYPES;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300435 type_mask = SCODE_TYPES & ~HAS_IF;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300436 } else if (type & DTV_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300437 type_mask = DTV_TYPES;
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300438 else if (type & STD_SPECIFIC_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300439 type_mask = STD_SPECIFIC_TYPES;
440
441 type &= type_mask;
442
Harvey Harrison8367fe22008-04-25 01:28:10 -0300443 if (!(type & SCODE))
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300444 type_mask = ~0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300445
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300446 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300447 for (i = 0; i < priv->firm_size; i++) {
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300448 if ((type == (priv->firm[i].type & type_mask)) &&
Chris Pascoeef207fe2007-12-02 09:30:55 -0300449 (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300450 goto found;
451 }
452
453 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300454 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300455 v4l2_std_id match_mask;
456 int nr_matches;
457
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300458 if (type != (priv->firm[i].type & type_mask))
Chris Pascoeb1535292007-11-19 10:04:06 -0300459 continue;
460
461 match_mask = *id & priv->firm[i].id;
462 if (!match_mask)
463 continue;
464
465 if ((*id & match_mask) == *id)
466 goto found; /* Supports all the requested standards */
467
468 nr_matches = hweight64(match_mask);
469 if (nr_matches > best_nr_matches) {
470 best_nr_matches = nr_matches;
471 best_i = i;
472 }
473 }
474
475 if (best_nr_matches > 0) {
476 tuner_dbg("Selecting best matching firmware (%d bits) for "
477 "type=", best_nr_matches);
478 dump_firm_type(type);
479 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
480 i = best_i;
481 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300482 }
483
484 /*FIXME: Would make sense to seek for type "hint" match ? */
485
Chris Pascoeb1535292007-11-19 10:04:06 -0300486 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300487 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300488
489found:
490 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300491
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300492ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300493 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300494 if (debug) {
495 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300496 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300497 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300498 return i;
499}
500
Michael Krufkyd7cba042008-09-12 13:31:45 -0300501static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
502{
503 struct xc2028_data *priv = fe->tuner_priv;
504
505 /* analog side (tuner-core) uses i2c_adap->algo_data.
506 * digital side is not guaranteed to have algo_data defined.
507 *
508 * digital side will always have fe->dvb defined.
509 * analog side (tuner-core) doesn't (yet) define fe->dvb.
510 */
511
512 return (!fe->callback) ? -EINVAL :
513 fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
514 fe->dvb->priv : priv->i2c_props.adap->algo_data,
515 DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
516}
517
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300518static int load_firmware(struct dvb_frontend *fe, unsigned int type,
519 v4l2_std_id *id)
520{
521 struct xc2028_data *priv = fe->tuner_priv;
522 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300523 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300524
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300525 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300526
527 pos = seek_firmware(fe, type, id);
528 if (pos < 0)
529 return pos;
530
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300531 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300532 dump_firm_type(priv->firm[pos].type);
533 printk("(%x), id %016llx.\n", priv->firm[pos].type,
534 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300535
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300536 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300537 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300538
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300539 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300540 __u16 size;
541
542 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300543 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300544 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300545 return -EINVAL;
546 }
547
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300548 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300549 p += sizeof(size);
550
551 if (size == 0xffff)
552 return 0;
553
554 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300555 /* Special callback command received */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300556 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300557 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300558 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300559 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300560 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300561 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300562 continue;
563 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300564 if (size >= 0xff00) {
565 switch (size) {
566 case 0xff00:
Michael Krufkyd7cba042008-09-12 13:31:45 -0300567 rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
Michel Ludwig5403bba2007-11-16 07:49:49 -0300568 if (rc < 0) {
569 tuner_err("Error at RESET code %d\n",
570 (*p) & 0x7f);
571 return -EINVAL;
572 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300573 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300574 default:
575 tuner_info("Invalid RESET code %d\n",
576 size & 0x7f);
577 return -EINVAL;
578
579 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300580 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300581 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300582
583 /* Checks for a sleep command */
584 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300585 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300586 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300587 }
588
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300589 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300590 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300591 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300592 return -EINVAL;
593 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300594
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300595 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300596 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300597 size--;
598
599 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300600 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300601 int len = (size < priv->ctrl.max_len - 1) ?
602 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300603
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300604 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300605
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300606 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300607 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300608 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300609 return -EINVAL;
610 }
611
612 p += len;
613 size -= len;
614 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300615 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300616 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300617}
618
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300619static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300620 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300621{
622 struct xc2028_data *priv = fe->tuner_priv;
623 int pos, rc;
624 unsigned char *p;
625
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300626 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300627
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300628 if (!int_freq) {
629 pos = seek_firmware(fe, type, id);
630 if (pos < 0)
631 return pos;
632 } else {
633 for (pos = 0; pos < priv->firm_size; pos++) {
634 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300635 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300636 break;
637 }
638 if (pos == priv->firm_size)
639 return -ENOENT;
640 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300641
642 p = priv->firm[pos].ptr;
643
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300644 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300645 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
646 return -EINVAL;
647 p += 12 * scode;
648 } else {
649 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
650 * has a 2-byte size header in the firmware format. */
651 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
652 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
653 return -EINVAL;
654 p += 14 * scode + 2;
655 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300656
Chris Pascoed7b22c52007-11-19 10:12:45 -0300657 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300658 dump_firm_type_and_int_freq(priv->firm[pos].type,
659 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300660 printk("(%x), id %016llx.\n", priv->firm[pos].type,
661 (unsigned long long)*id);
662
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300663 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300664 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
665 else
666 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
667 if (rc < 0)
668 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300669
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300670 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300671 if (rc < 0)
672 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300673
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300674 rc = send_seq(priv, {0x00, 0x8c});
675 if (rc < 0)
676 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300677
678 return 0;
679}
680
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300681static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300682 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300683{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300684 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300685 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300686 int rc = 0, is_retry = 0;
687 u16 version, hwmodel;
688 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300689
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300690 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300691
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300692 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300693 if (!priv->ctrl.fname) {
694 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300695 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300696 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300697
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300698 rc = load_all_firmwares(fe);
699 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300700 return rc;
701 }
702
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300703 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300704 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300705
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300706retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300707 new_fw.type = type;
708 new_fw.id = std;
709 new_fw.std_req = std;
710 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
711 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300712 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300713
714 tuner_dbg("checking firmware, user requested type=");
715 if (debug) {
716 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300717 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300718 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300719 if (!int_freq) {
720 printk("scode_tbl ");
721 dump_firm_type(priv->ctrl.scode_table);
722 printk("(%x), ", priv->ctrl.scode_table);
723 } else
724 printk("int_freq %d, ", new_fw.int_freq);
725 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300726 }
727
728 /* No need to reload base firmware if it matches */
729 if (((BASE | new_fw.type) & BASE_TYPES) ==
730 (priv->cur_fw.type & BASE_TYPES)) {
731 tuner_dbg("BASE firmware not changed.\n");
732 goto skip_base;
733 }
734
735 /* Updating BASE - forget about all currently loaded firmware */
736 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
737
738 /* Reset is needed before loading firmware */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300739 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300740 if (rc < 0)
741 goto fail;
742
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300743 /* BASE firmwares are all std0 */
744 std0 = 0;
745 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300746 if (rc < 0) {
747 tuner_err("Error %d while loading base firmware\n",
748 rc);
749 goto fail;
750 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300751
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300752 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300753 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300754
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300755 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300756 if (rc == -ENOENT)
757 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
758 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300759 if (rc < 0 && rc != -ENOENT) {
760 tuner_err("Error %d while loading init1 firmware\n",
761 rc);
762 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300763 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300764
Chris Pascoee0f0b372007-11-19 11:22:03 -0300765skip_base:
766 /*
767 * No need to reload standard specific firmware if base firmware
768 * was not reloaded and requested video standards have not changed.
769 */
770 if (priv->cur_fw.type == (BASE | new_fw.type) &&
771 priv->cur_fw.std_req == std) {
772 tuner_dbg("Std-specific firmware already loaded.\n");
773 goto skip_std_specific;
774 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300775
Chris Pascoee0f0b372007-11-19 11:22:03 -0300776 /* Reloading std-specific firmware forces a SCODE update */
777 priv->cur_fw.scode_table = 0;
778
Chris Pascoee0f0b372007-11-19 11:22:03 -0300779 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300780 if (rc == -ENOENT)
781 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
782
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300783 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300784 goto fail;
785
786skip_std_specific:
787 if (priv->cur_fw.scode_table == new_fw.scode_table &&
788 priv->cur_fw.scode_nr == new_fw.scode_nr) {
789 tuner_dbg("SCODE firmware already loaded.\n");
790 goto check_device;
791 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300792
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300793 if (new_fw.type & FM)
794 goto check_device;
795
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300796 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300797 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300798
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300799 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
800 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300801
Chris Pascoee0f0b372007-11-19 11:22:03 -0300802check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300803 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
804 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
805 tuner_err("Unable to read tuner registers.\n");
806 goto fail;
807 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300808
Devin Heitmuellerb37f2d62008-04-21 07:02:09 -0300809 tuner_dbg("Device is Xceive %d version %d.%d, "
810 "firmware version %d.%d\n",
811 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
812 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300813
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300814 /* Check firmware version against what we downloaded. */
815 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
Mauro Carvalho Chehab2d5024a2009-09-14 10:23:20 -0300816 if (!priv->ctrl.read_not_reliable) {
817 tuner_err("Incorrect readback of firmware version.\n");
818 goto fail;
819 } else {
820 tuner_err("Returned an incorrect version. However, "
821 "read is not reliable enough. Ignoring it.\n");
822 hwmodel = 3028;
823 }
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300824 }
825
826 /* Check that the tuner hardware model remains consistent over time. */
827 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
828 priv->hwmodel = hwmodel;
829 priv->hwvers = version & 0xff00;
830 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
831 priv->hwvers != (version & 0xff00)) {
832 tuner_err("Read invalid device hardware information - tuner "
833 "hung?\n");
834 goto fail;
835 }
836
Chris Pascoee0f0b372007-11-19 11:22:03 -0300837 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
838
839 /*
840 * By setting BASE in cur_fw.type only after successfully loading all
841 * firmwares, we can:
842 * 1. Identify that BASE firmware with type=0 has been loaded;
843 * 2. Tell whether BASE firmware was just changed the next time through.
844 */
845 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300846
847 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300848
849fail:
850 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300851 if (!is_retry) {
852 msleep(50);
853 is_retry = 1;
854 tuner_dbg("Retrying firmware load\n");
855 goto retry;
856 }
857
Chris Pascoee0f0b372007-11-19 11:22:03 -0300858 if (rc == -ENOENT)
859 rc = -EINVAL;
860 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300861}
862
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300863static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300864{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300865 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300866 u16 frq_lock, signal = 0;
867 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300868
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300869 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300870
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300871 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300872
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300873 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300874 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300875 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300876 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300877
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300878 /* Frequency is locked */
879 if (frq_lock == 1)
880 signal = 32768;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300881
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300882 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300883 rc = xc2028_get_reg(priv, 0x0040, &signal);
884 if (rc < 0)
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300885 goto ret;
886
887 /* Use both frq_lock and signal to generate the result */
888 signal = signal || ((signal & 0x07) << 12);
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300889
890ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300891 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300892
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300893 *strength = signal;
894
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300895 tuner_dbg("signal strength is %d\n", signal);
896
Chris Pascoe7d58d112007-11-19 04:31:58 -0300897 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300898}
899
900#define DIV 15625
901
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300902static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300903 enum tuner_mode new_mode,
904 unsigned int type,
905 v4l2_std_id std,
906 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300907{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300908 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300909 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300910 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300911 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300912
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300913 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300914
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300915 mutex_lock(&priv->lock);
916
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300917 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300918
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300919 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300920 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300921
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300922 /* On some cases xc2028 can disable video output, if
923 * very weak signals are received. By sending a soft
924 * reset, this is re-enabled. So, it is better to always
925 * send a soft reset before changing channels, to be sure
926 * that xc2028 will be in a safe state.
927 * Maybe this might also be needed for DTV.
928 */
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200929 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300930 rc = send_seq(priv, {0x00, 0x00});
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300931
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200932 /* Analog modes require offset = 0 */
933 } else {
934 /*
935 * Digital modes require an offset to adjust to the
936 * proper frequency. The offset depends on what
937 * firmware version is used.
938 */
939
940 /*
941 * Adjust to the center frequency. This is calculated by the
942 * formula: offset = 1.25MHz - BW/2
943 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
944 * further adjustment to get the frequency center on VHF
945 */
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300946 if (priv->cur_fw.type & DTV6)
947 offset = 1750000;
948 else if (priv->cur_fw.type & DTV7)
949 offset = 2250000;
950 else /* DTV8 or DTV78 */
951 offset = 2750000;
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300952 if ((priv->cur_fw.type & DTV78) && freq < 470000000)
Chris Pascoea44f1c42007-11-19 06:35:26 -0300953 offset -= 500000;
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200954
955 /*
956 * xc3028 additional "magic"
957 * Depending on the firmware version, it needs some adjustments
958 * to properly centralize the frequency. This seems to be
959 * needed to compensate the SCODE table adjustments made by
960 * newer firmwares
961 */
962
963#if 1
964 /*
965 * The proper adjustment would be to do it at s-code table.
966 * However, this didn't work, as reported by
967 * Robert Lowery <rglowery@exemail.com.au>
968 */
969
970 if (priv->cur_fw.type & DTV7)
971 offset += 500000;
972
973#else
974 /*
975 * Still need tests for XC3028L (firmware 3.2 or upper)
976 * So, for now, let's just comment the per-firmware
977 * version of this change. Reports with xc3028l working
978 * with and without the lines bellow are welcome
979 */
980
981 if (priv->firm_version < 0x0302) {
982 if (priv->cur_fw.type & DTV7)
983 offset += 500000;
984 } else {
985 if (priv->cur_fw.type & DTV7)
986 offset -= 300000;
987 else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
988 offset += 200000;
989 }
990#endif
Chris Pascoea44f1c42007-11-19 06:35:26 -0300991 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300992
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300993 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300994
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300995 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300996 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300997 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
998 else
999 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
1000 if (rc < 0)
1001 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001002
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -03001003 /* Return code shouldn't be checked.
1004 The reset CLK is needed only with tm6000.
1005 Driver should work fine even if this fails.
1006 */
Mauro Carvalho Chehabe5cc2bf2008-01-08 11:26:59 -03001007 msleep(priv->ctrl.msleep);
Michael Krufkyd7cba042008-09-12 13:31:45 -03001008 do_tuner_callback(fe, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001009
1010 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -03001011
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001012 buf[0] = 0xff & (div >> 24);
1013 buf[1] = 0xff & (div >> 16);
1014 buf[2] = 0xff & (div >> 8);
1015 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001016
Chris Pascoe47cc5b72007-11-19 04:14:23 -03001017 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001018 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001019 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001020 msleep(100);
1021
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001022 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001023
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -03001024 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
1025 buf[0], buf[1], buf[2], buf[3],
1026 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001027
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001028 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001029
1030ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001031 mutex_unlock(&priv->lock);
1032
1033 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001034}
1035
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001036static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001037 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -03001038{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001039 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001040 unsigned int type=0;
1041
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001042 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -03001043
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001044 if (p->mode == V4L2_TUNER_RADIO) {
1045 type |= FM;
1046 if (priv->ctrl.input1)
1047 type |= INPUT1;
1048 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehabe2860d92009-06-06 08:15:08 -03001049 T_RADIO, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001050 }
1051
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -03001052 /* if std is not defined, choose one */
1053 if (!p->std)
1054 p->std = V4L2_STD_MN;
1055
1056 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001057 if (!(p->std & V4L2_STD_MN))
1058 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -03001059
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001060 /* Add audio hack to std mask */
1061 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001062
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001063 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001064 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -03001065}
1066
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001067static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -03001068 struct dvb_frontend_parameters *p)
1069{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001070 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001071 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001072 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -03001073 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -03001074
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001075 tuner_dbg("%s called\n", __func__);
Michel Ludwig701672e2007-07-18 10:29:10 -03001076
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001077 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001078 case FE_OFDM:
1079 bw = p->u.ofdm.bandwidth;
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001080 /*
1081 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1082 * Both seem to require QAM firmware for OFDM decoding
1083 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1084 */
1085 if (bw == BANDWIDTH_6_MHZ)
1086 type |= QAM;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001087 break;
1088 case FE_ATSC:
1089 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001090 /* The only ATSC firmware (at least on v2.7) is D2633 */
1091 type |= ATSC | D2633;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001092 break;
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001093 /* DVB-S and pure QAM (FE_QAM) are not supported */
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001094 default:
1095 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001096 }
1097
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001098 switch (bw) {
1099 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001100 if (p->frequency < 470000000)
1101 priv->ctrl.vhfbw7 = 0;
1102 else
1103 priv->ctrl.uhfbw8 = 1;
1104 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1105 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001106 break;
1107 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001108 if (p->frequency < 470000000)
1109 priv->ctrl.vhfbw7 = 1;
1110 else
1111 priv->ctrl.uhfbw8 = 0;
1112 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1113 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001114 break;
1115 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001116 type |= DTV6;
1117 priv->ctrl.vhfbw7 = 0;
1118 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001119 break;
1120 default:
1121 tuner_err("error: bandwidth not supported.\n");
1122 };
1123
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001124 /*
1125 Selects between D2633 or D2620 firmware.
1126 It doesn't make sense for ATSC, since it should be D2633 on all cases
1127 */
1128 if (fe->ops.info.type != FE_ATSC) {
1129 switch (priv->ctrl.type) {
1130 case XC2028_D2633:
1131 type |= D2633;
1132 break;
1133 case XC2028_D2620:
1134 type |= D2620;
1135 break;
1136 case XC2028_AUTO:
1137 default:
1138 /* Zarlink seems to need D2633 */
1139 if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
1140 type |= D2633;
1141 else
1142 type |= D2620;
1143 }
1144 }
1145
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001146 /* All S-code tables need a 200kHz shift */
Andy Walls6e707b42009-06-11 07:57:50 -03001147 if (priv->ctrl.demod) {
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001148 demod = priv->ctrl.demod;
1149
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001150 /*
1151 * Newer firmwares require a 200 kHz offset only for ATSC
1152 */
1153 if (type == ATSC || priv->firm_version < 0x0302)
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001154 demod += 200;
Andy Walls6e707b42009-06-11 07:57:50 -03001155 /*
1156 * The DTV7 S-code table needs a 700 kHz shift.
Andy Walls6e707b42009-06-11 07:57:50 -03001157 *
1158 * DTV7 is only used in Australia. Germany or Italy may also
1159 * use this firmware after initialization, but a tune to a UHF
1160 * channel should then cause DTV78 to be used.
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001161 *
1162 * Unfortunately, on real-field tests, the s-code offset
1163 * didn't work as expected, as reported by
1164 * Robert Lowery <rglowery@exemail.com.au>
Andy Walls6e707b42009-06-11 07:57:50 -03001165 */
Andy Walls6e707b42009-06-11 07:57:50 -03001166 }
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001167
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001168 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001169 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001170}
1171
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001172static int xc2028_sleep(struct dvb_frontend *fe)
1173{
1174 struct xc2028_data *priv = fe->tuner_priv;
1175 int rc = 0;
1176
Devin Heitmueller93b99922009-08-03 22:52:59 -03001177 /* Avoid firmware reload on slow devices or if PM disabled */
1178 if (no_poweroff || priv->ctrl.disable_power_mgmt)
Mauro Carvalho Chehab10f201a2008-12-05 10:49:53 -03001179 return 0;
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001180
1181 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
Mauro Carvalho Chehabe278e742008-12-18 06:00:25 -03001182 if (debug > 1) {
1183 tuner_dbg("Printing sleep stack trace:\n");
1184 dump_stack();
1185 }
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001186
1187 mutex_lock(&priv->lock);
1188
1189 if (priv->firm_version < 0x0202)
1190 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1191 else
1192 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1193
1194 priv->cur_fw.type = 0; /* need firmware reload */
1195
1196 mutex_unlock(&priv->lock);
1197
1198 return rc;
1199}
Chris Pascoe45819c32007-11-19 11:41:20 -03001200
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001201static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001202{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001203 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001204
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001205 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001206
Chris Pascoeaa501be2007-11-19 04:45:38 -03001207 mutex_lock(&xc2028_list_mutex);
1208
Michael Krufkyc663d032008-04-18 21:22:50 -03001209 /* only perform final cleanup if this is the last instance */
1210 if (hybrid_tuner_report_instance_count(priv) == 1) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001211 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001212 free_firmware(priv);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001213 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001214
Michael Krufkyc663d032008-04-18 21:22:50 -03001215 if (priv)
1216 hybrid_tuner_release_state(priv);
1217
Chris Pascoeaa501be2007-11-19 04:45:38 -03001218 mutex_unlock(&xc2028_list_mutex);
1219
Michael Krufkyc663d032008-04-18 21:22:50 -03001220 fe->tuner_priv = NULL;
1221
Michel Ludwig701672e2007-07-18 10:29:10 -03001222 return 0;
1223}
1224
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001225static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001226{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001227 struct xc2028_data *priv = fe->tuner_priv;
1228
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001229 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001230
1231 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001232
1233 return 0;
1234}
1235
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001236static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001237{
1238 struct xc2028_data *priv = fe->tuner_priv;
1239 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001240 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001241
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001242 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001243
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001244 mutex_lock(&priv->lock);
1245
Chris Pascoe0a196b62007-11-19 09:29:59 -03001246 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001247 if (priv->ctrl.max_len < 9)
1248 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001249
1250 if (p->fname) {
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001251 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1252 kfree(priv->ctrl.fname);
1253 free_firmware(priv);
1254 }
1255
Chris Pascoe0a196b62007-11-19 09:29:59 -03001256 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1257 if (priv->ctrl.fname == NULL)
1258 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001259 }
1260
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001261 mutex_unlock(&priv->lock);
1262
Chris Pascoe0a196b62007-11-19 09:29:59 -03001263 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001264}
1265
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001266static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001267 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001268 .name = "Xceive XC3028",
1269 .frequency_min = 42000000,
1270 .frequency_max = 864000000,
1271 .frequency_step = 50000,
1272 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001273
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001274 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001275 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001276 .release = xc2028_dvb_release,
1277 .get_frequency = xc2028_get_frequency,
1278 .get_rf_strength = xc2028_signal,
1279 .set_params = xc2028_set_params,
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001280 .sleep = xc2028_sleep,
Michel Ludwig701672e2007-07-18 10:29:10 -03001281};
1282
Michael Krufky7972f982007-12-21 16:12:09 -03001283struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1284 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001285{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001286 struct xc2028_data *priv;
Michael Krufkyc663d032008-04-18 21:22:50 -03001287 int instance;
Michel Ludwig701672e2007-07-18 10:29:10 -03001288
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001289 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001290 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001291
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001292 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001293 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001294
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001295 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001296 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001297 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001298 }
1299
Chris Pascoeaa501be2007-11-19 04:45:38 -03001300 mutex_lock(&xc2028_list_mutex);
1301
Michael Krufkyc663d032008-04-18 21:22:50 -03001302 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1303 hybrid_tuner_instance_list,
1304 cfg->i2c_adap, cfg->i2c_addr,
1305 "xc2028");
1306 switch (instance) {
1307 case 0:
1308 /* memory allocation failure */
1309 goto fail;
1310 break;
1311 case 1:
1312 /* new tuner instance */
Chris Pascoe0a196b62007-11-19 09:29:59 -03001313 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001314
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001315 mutex_init(&priv->lock);
1316
Michael Krufkyc663d032008-04-18 21:22:50 -03001317 fe->tuner_priv = priv;
1318 break;
1319 case 2:
1320 /* existing tuner instance */
1321 fe->tuner_priv = priv;
1322 break;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001323 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001324
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001325 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001326 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001327
1328 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001329
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001330 if (cfg->ctrl)
1331 xc2028_set_config(fe, cfg->ctrl);
1332
Chris Pascoeaa501be2007-11-19 04:45:38 -03001333 mutex_unlock(&xc2028_list_mutex);
1334
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001335 return fe;
Michael Krufkyc663d032008-04-18 21:22:50 -03001336fail:
1337 mutex_unlock(&xc2028_list_mutex);
1338
1339 xc2028_dvb_release(fe);
1340 return NULL;
Michel Ludwig701672e2007-07-18 10:29:10 -03001341}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001342
Michel Ludwig701672e2007-07-18 10:29:10 -03001343EXPORT_SYMBOL(xc2028_attach);
1344
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001345MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001346MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001347MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1348MODULE_LICENSE("GPL");