blob: b6ce528e1889f69bba7c514edb93092a3dec8478 [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); \
Devin Heitmueller70ca3c42010-01-19 01:38:45 -0300103 if (priv->ctrl.msleep) \
104 msleep(priv->ctrl.msleep); \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300105 _rc; \
106})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300107
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300108#define i2c_rcv(priv, buf, size) ({ \
109 int _rc; \
110 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
111 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300112 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300113 _rc, (int)size); \
114 _rc; \
115})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300116
Chris Pascoe7d58d112007-11-19 04:31:58 -0300117#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
118 int _rc; \
119 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
120 ibuf, isize); \
121 if (isize != _rc) \
122 tuner_err("i2c input error: rc = %d (should be %d)\n", \
123 _rc, (int)isize); \
Devin Heitmueller70ca3c42010-01-19 01:38:45 -0300124 if (priv->ctrl.msleep) \
125 msleep(priv->ctrl.msleep); \
Chris Pascoe7d58d112007-11-19 04:31:58 -0300126 _rc; \
127})
128
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300129#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300130 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300131 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300132 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300133 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300134 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300135 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
Devin Heitmueller70ca3c42010-01-19 01:38:45 -0300136 } else if (priv->ctrl.msleep) \
Mauro Carvalho Chehabe5cc2bf2008-01-08 11:26:59 -0300137 msleep(priv->ctrl.msleep); \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300138 _rc; \
139})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300140
Devin Heitmueller83244022008-04-17 21:41:16 -0300141static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300142{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300143 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300144 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300145
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300146 tuner_dbg("%s %04x called\n", __func__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300147
Chris Pascoe7d58d112007-11-19 04:31:58 -0300148 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300149 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300150
Chris Pascoe7d58d112007-11-19 04:31:58 -0300151 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
152 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300153
Chris Pascoe7d58d112007-11-19 04:31:58 -0300154 *val = (ibuf[1]) | (ibuf[0] << 8);
155 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300156}
157
Chris Pascoee0262682007-12-02 06:30:50 -0300158#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Adrian Bunk29bec0b2008-04-22 14:41:45 -0300159static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300160{
161 if (type & BASE)
162 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300163 if (type & INIT1)
164 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300165 if (type & F8MHZ)
166 printk("F8MHZ ");
167 if (type & MTS)
168 printk("MTS ");
169 if (type & D2620)
170 printk("D2620 ");
171 if (type & D2633)
172 printk("D2633 ");
173 if (type & DTV6)
174 printk("DTV6 ");
175 if (type & QAM)
176 printk("QAM ");
177 if (type & DTV7)
178 printk("DTV7 ");
179 if (type & DTV78)
180 printk("DTV78 ");
181 if (type & DTV8)
182 printk("DTV8 ");
183 if (type & FM)
184 printk("FM ");
185 if (type & INPUT1)
186 printk("INPUT1 ");
187 if (type & LCD)
188 printk("LCD ");
189 if (type & NOGD)
190 printk("NOGD ");
191 if (type & MONO)
192 printk("MONO ");
193 if (type & ATSC)
194 printk("ATSC ");
195 if (type & IF)
196 printk("IF ");
197 if (type & LG60)
198 printk("LG60 ");
199 if (type & ATI638)
200 printk("ATI638 ");
201 if (type & OREN538)
202 printk("OREN538 ");
203 if (type & OREN36)
204 printk("OREN36 ");
205 if (type & TOYOTA388)
206 printk("TOYOTA388 ");
207 if (type & TOYOTA794)
208 printk("TOYOTA794 ");
209 if (type & DIBCOM52)
210 printk("DIBCOM52 ");
211 if (type & ZARLINK456)
212 printk("ZARLINK456 ");
213 if (type & CHINA)
214 printk("CHINA ");
215 if (type & F6MHZ)
216 printk("F6MHZ ");
217 if (type & INPUT2)
218 printk("INPUT2 ");
219 if (type & SCODE)
220 printk("SCODE ");
Chris Pascoee0262682007-12-02 06:30:50 -0300221 if (type & HAS_IF)
222 printk("HAS_IF_%d ", int_freq);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300223}
224
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300225static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300226{
Chris Pascoee155d902007-11-19 04:16:47 -0300227 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300228 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300229 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300230 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300231 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300232 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300233 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300234 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300235 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300236 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300237 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300238 return V4L2_STD_NICAM_B;
239
240 return 0;
241}
242
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300243static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300244{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300245 int i;
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -0300246 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300247
248 if (!priv->firm)
249 return;
250
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300251 for (i = 0; i < priv->firm_size; i++)
252 kfree(priv->firm[i].ptr);
253
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300254 kfree(priv->firm);
255
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300256 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300257 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300258
259 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300260}
261
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300262static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300263{
264 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300265 const struct firmware *fw = NULL;
David Woodhousec63e87e2008-05-24 00:13:34 +0100266 const unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300267 int rc = 0;
268 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300269 char name[33];
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300270 char *fname;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300271
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300272 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300273
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300274 if (!firmware_name[0])
275 fname = priv->ctrl.fname;
276 else
277 fname = firmware_name;
278
279 tuner_dbg("Reading firmware %s\n", fname);
Jean Delvaree9785252009-04-26 05:43:59 -0300280 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300281 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300282 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300283 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300284 fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300285 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300286 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300287 rc, fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300288
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300289 return rc;
290 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300291 p = fw->data;
292 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300293
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300294 if (fw->size < sizeof(name) - 1 + 2 + 2) {
295 tuner_err("Error: firmware file %s has invalid size!\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300296 fname);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300297 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300298 }
299
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300300 memcpy(name, p, sizeof(name) - 1);
301 name[sizeof(name) - 1] = 0;
302 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300303
Al Viro84a9f332008-06-22 14:19:29 -0300304 priv->firm_version = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300305 p += 2;
306
Al Viro84a9f332008-06-22 14:19:29 -0300307 n_array = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300308 p += 2;
309
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300310 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300311 n_array, fname, name,
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300312 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300313
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300314 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300315 if (priv->firm == NULL) {
316 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300317 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300318 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300319 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300320 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300321
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300322 n = -1;
323 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300324 __u32 type, size;
325 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300326 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300327
328 n++;
329 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300330 tuner_err("More firmware images in file than "
331 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300332 goto corrupt;
333 }
334
335 /* Checks if there's enough bytes to read */
Al Viro84a9f332008-06-22 14:19:29 -0300336 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
337 goto header;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300338
Al Viro84a9f332008-06-22 14:19:29 -0300339 type = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300340 p += sizeof(type);
341
Al Viro84a9f332008-06-22 14:19:29 -0300342 id = get_unaligned_le64(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300343 p += sizeof(id);
344
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300345 if (type & HAS_IF) {
Al Viro84a9f332008-06-22 14:19:29 -0300346 int_freq = get_unaligned_le16(p);
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300347 p += sizeof(int_freq);
Al Viro84a9f332008-06-22 14:19:29 -0300348 if (endp - p < sizeof(size))
349 goto header;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300350 }
351
Al Viro84a9f332008-06-22 14:19:29 -0300352 size = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300353 p += sizeof(size);
354
Al Viro84a9f332008-06-22 14:19:29 -0300355 if (!size || size > endp - p) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300356 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300357 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300358 printk("(%x), id %llx is corrupted "
359 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300360 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300361 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300362 goto corrupt;
363 }
364
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300365 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300366 if (priv->firm[n].ptr == NULL) {
367 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300368 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300369 goto err;
370 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300371 tuner_dbg("Reading firmware type ");
372 if (debug) {
Chris Pascoee0262682007-12-02 06:30:50 -0300373 dump_firm_type_and_int_freq(type, int_freq);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300374 printk("(%x), id %llx, size=%d.\n",
Chris Pascoee0262682007-12-02 06:30:50 -0300375 type, (unsigned long long)id, size);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300376 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300377
378 memcpy(priv->firm[n].ptr, p, size);
379 priv->firm[n].type = type;
380 priv->firm[n].id = id;
381 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300382 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300383
384 p += size;
385 }
386
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300387 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300388 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300389 goto corrupt;
390 }
391
392 goto done;
393
Al Viro84a9f332008-06-22 14:19:29 -0300394header:
395 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300396corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300397 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300398 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300399
400err:
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300401 tuner_info("Releasing partially loaded firmware file.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300402 free_firmware(priv);
403
404done:
405 release_firmware(fw);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300406 if (rc == 0)
407 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300408
409 return rc;
410}
411
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300412static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
413 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300414{
415 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoeb1535292007-11-19 10:04:06 -0300416 int i, best_i = -1, best_nr_matches = 0;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300417 unsigned int type_mask = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300418
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300419 tuner_dbg("%s called, want type=", __func__);
Chris Pascoeb1535292007-11-19 10:04:06 -0300420 if (debug) {
421 dump_firm_type(type);
422 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
423 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300424
425 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300426 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300427 return -EINVAL;
428 }
429
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300430 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300431 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300432
Chris Pascoee0f0b372007-11-19 11:22:03 -0300433 if (type & BASE)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300434 type_mask = BASE_TYPES;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300435 else if (type & SCODE) {
Chris Pascoee0f0b372007-11-19 11:22:03 -0300436 type &= SCODE_TYPES;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300437 type_mask = SCODE_TYPES & ~HAS_IF;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300438 } else if (type & DTV_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300439 type_mask = DTV_TYPES;
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300440 else if (type & STD_SPECIFIC_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300441 type_mask = STD_SPECIFIC_TYPES;
442
443 type &= type_mask;
444
Harvey Harrison8367fe22008-04-25 01:28:10 -0300445 if (!(type & SCODE))
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300446 type_mask = ~0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300447
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300448 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300449 for (i = 0; i < priv->firm_size; i++) {
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300450 if ((type == (priv->firm[i].type & type_mask)) &&
Chris Pascoeef207fe2007-12-02 09:30:55 -0300451 (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300452 goto found;
453 }
454
455 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300456 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300457 v4l2_std_id match_mask;
458 int nr_matches;
459
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300460 if (type != (priv->firm[i].type & type_mask))
Chris Pascoeb1535292007-11-19 10:04:06 -0300461 continue;
462
463 match_mask = *id & priv->firm[i].id;
464 if (!match_mask)
465 continue;
466
467 if ((*id & match_mask) == *id)
468 goto found; /* Supports all the requested standards */
469
470 nr_matches = hweight64(match_mask);
471 if (nr_matches > best_nr_matches) {
472 best_nr_matches = nr_matches;
473 best_i = i;
474 }
475 }
476
477 if (best_nr_matches > 0) {
478 tuner_dbg("Selecting best matching firmware (%d bits) for "
479 "type=", best_nr_matches);
480 dump_firm_type(type);
481 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
482 i = best_i;
483 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300484 }
485
486 /*FIXME: Would make sense to seek for type "hint" match ? */
487
Chris Pascoeb1535292007-11-19 10:04:06 -0300488 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300489 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300490
491found:
492 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300493
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300494ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300495 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300496 if (debug) {
497 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300498 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300499 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300500 return i;
501}
502
Michael Krufkyd7cba042008-09-12 13:31:45 -0300503static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
504{
505 struct xc2028_data *priv = fe->tuner_priv;
506
507 /* analog side (tuner-core) uses i2c_adap->algo_data.
508 * digital side is not guaranteed to have algo_data defined.
509 *
510 * digital side will always have fe->dvb defined.
511 * analog side (tuner-core) doesn't (yet) define fe->dvb.
512 */
513
514 return (!fe->callback) ? -EINVAL :
515 fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
516 fe->dvb->priv : priv->i2c_props.adap->algo_data,
517 DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
518}
519
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300520static int load_firmware(struct dvb_frontend *fe, unsigned int type,
521 v4l2_std_id *id)
522{
523 struct xc2028_data *priv = fe->tuner_priv;
524 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300525 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300526
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300527 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300528
529 pos = seek_firmware(fe, type, id);
530 if (pos < 0)
531 return pos;
532
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300533 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300534 dump_firm_type(priv->firm[pos].type);
535 printk("(%x), id %016llx.\n", priv->firm[pos].type,
536 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300537
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300538 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300539 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300540
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300541 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300542 __u16 size;
543
544 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300545 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300546 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300547 return -EINVAL;
548 }
549
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300550 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300551 p += sizeof(size);
552
553 if (size == 0xffff)
554 return 0;
555
556 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300557 /* Special callback command received */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300558 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300559 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300560 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300561 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300562 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300563 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300564 continue;
565 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300566 if (size >= 0xff00) {
567 switch (size) {
568 case 0xff00:
Michael Krufkyd7cba042008-09-12 13:31:45 -0300569 rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
Michel Ludwig5403bba2007-11-16 07:49:49 -0300570 if (rc < 0) {
571 tuner_err("Error at RESET code %d\n",
572 (*p) & 0x7f);
573 return -EINVAL;
574 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300575 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300576 default:
577 tuner_info("Invalid RESET code %d\n",
578 size & 0x7f);
579 return -EINVAL;
580
581 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300582 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300583 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300584
585 /* Checks for a sleep command */
586 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300587 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300588 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300589 }
590
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300591 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300592 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300593 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300594 return -EINVAL;
595 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300596
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300597 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300598 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300599 size--;
600
601 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300602 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300603 int len = (size < priv->ctrl.max_len - 1) ?
604 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300605
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300606 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300607
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300608 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300609 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300610 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300611 return -EINVAL;
612 }
613
614 p += len;
615 size -= len;
616 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300617 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300618 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300619}
620
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300621static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300622 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300623{
624 struct xc2028_data *priv = fe->tuner_priv;
625 int pos, rc;
626 unsigned char *p;
627
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300628 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300629
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300630 if (!int_freq) {
631 pos = seek_firmware(fe, type, id);
632 if (pos < 0)
633 return pos;
634 } else {
635 for (pos = 0; pos < priv->firm_size; pos++) {
636 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300637 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300638 break;
639 }
640 if (pos == priv->firm_size)
641 return -ENOENT;
642 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300643
644 p = priv->firm[pos].ptr;
645
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300646 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300647 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
648 return -EINVAL;
649 p += 12 * scode;
650 } else {
651 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
652 * has a 2-byte size header in the firmware format. */
653 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
654 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
655 return -EINVAL;
656 p += 14 * scode + 2;
657 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300658
Chris Pascoed7b22c52007-11-19 10:12:45 -0300659 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300660 dump_firm_type_and_int_freq(priv->firm[pos].type,
661 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300662 printk("(%x), id %016llx.\n", priv->firm[pos].type,
663 (unsigned long long)*id);
664
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300665 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300666 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
667 else
668 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
669 if (rc < 0)
670 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300671
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300672 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300673 if (rc < 0)
674 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300675
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300676 rc = send_seq(priv, {0x00, 0x8c});
677 if (rc < 0)
678 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300679
680 return 0;
681}
682
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300683static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300684 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300685{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300686 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300687 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300688 int rc = 0, is_retry = 0;
689 u16 version, hwmodel;
690 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300691
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300692 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300693
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300694 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300695 if (!priv->ctrl.fname) {
696 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300697 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300698 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300699
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300700 rc = load_all_firmwares(fe);
701 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300702 return rc;
703 }
704
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300705 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300706 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300707
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300708retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300709 new_fw.type = type;
710 new_fw.id = std;
711 new_fw.std_req = std;
712 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
713 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300714 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300715
716 tuner_dbg("checking firmware, user requested type=");
717 if (debug) {
718 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300719 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300720 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300721 if (!int_freq) {
722 printk("scode_tbl ");
723 dump_firm_type(priv->ctrl.scode_table);
724 printk("(%x), ", priv->ctrl.scode_table);
725 } else
726 printk("int_freq %d, ", new_fw.int_freq);
727 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300728 }
729
730 /* No need to reload base firmware if it matches */
731 if (((BASE | new_fw.type) & BASE_TYPES) ==
732 (priv->cur_fw.type & BASE_TYPES)) {
733 tuner_dbg("BASE firmware not changed.\n");
734 goto skip_base;
735 }
736
737 /* Updating BASE - forget about all currently loaded firmware */
738 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
739
740 /* Reset is needed before loading firmware */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300741 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300742 if (rc < 0)
743 goto fail;
744
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300745 /* BASE firmwares are all std0 */
746 std0 = 0;
747 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300748 if (rc < 0) {
749 tuner_err("Error %d while loading base firmware\n",
750 rc);
751 goto fail;
752 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300753
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300754 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300755 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300756
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300757 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300758 if (rc == -ENOENT)
759 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
760 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300761 if (rc < 0 && rc != -ENOENT) {
762 tuner_err("Error %d while loading init1 firmware\n",
763 rc);
764 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300765 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300766
Chris Pascoee0f0b372007-11-19 11:22:03 -0300767skip_base:
768 /*
769 * No need to reload standard specific firmware if base firmware
770 * was not reloaded and requested video standards have not changed.
771 */
772 if (priv->cur_fw.type == (BASE | new_fw.type) &&
773 priv->cur_fw.std_req == std) {
774 tuner_dbg("Std-specific firmware already loaded.\n");
775 goto skip_std_specific;
776 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300777
Chris Pascoee0f0b372007-11-19 11:22:03 -0300778 /* Reloading std-specific firmware forces a SCODE update */
779 priv->cur_fw.scode_table = 0;
780
Chris Pascoee0f0b372007-11-19 11:22:03 -0300781 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300782 if (rc == -ENOENT)
783 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
784
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300785 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300786 goto fail;
787
788skip_std_specific:
789 if (priv->cur_fw.scode_table == new_fw.scode_table &&
790 priv->cur_fw.scode_nr == new_fw.scode_nr) {
791 tuner_dbg("SCODE firmware already loaded.\n");
792 goto check_device;
793 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300794
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300795 if (new_fw.type & FM)
796 goto check_device;
797
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300798 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300799 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300800
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300801 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
802 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300803
Chris Pascoee0f0b372007-11-19 11:22:03 -0300804check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300805 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
806 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
807 tuner_err("Unable to read tuner registers.\n");
808 goto fail;
809 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300810
Devin Heitmuellerb37f2d62008-04-21 07:02:09 -0300811 tuner_dbg("Device is Xceive %d version %d.%d, "
812 "firmware version %d.%d\n",
813 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
814 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300815
Mauro Carvalho Chehab0fb84ce2008-12-02 08:30:16 -0300816
817 if (priv->ctrl.read_not_reliable)
818 goto read_not_reliable;
819
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300820 /* Check firmware version against what we downloaded. */
821 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
Mauro Carvalho Chehab2d5024a2009-09-14 10:23:20 -0300822 if (!priv->ctrl.read_not_reliable) {
823 tuner_err("Incorrect readback of firmware version.\n");
824 goto fail;
825 } else {
826 tuner_err("Returned an incorrect version. However, "
827 "read is not reliable enough. Ignoring it.\n");
828 hwmodel = 3028;
829 }
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300830 }
831
832 /* Check that the tuner hardware model remains consistent over time. */
833 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
834 priv->hwmodel = hwmodel;
835 priv->hwvers = version & 0xff00;
836 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
837 priv->hwvers != (version & 0xff00)) {
838 tuner_err("Read invalid device hardware information - tuner "
839 "hung?\n");
840 goto fail;
841 }
842
Mauro Carvalho Chehab0fb84ce2008-12-02 08:30:16 -0300843read_not_reliable:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300844 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
845
846 /*
847 * By setting BASE in cur_fw.type only after successfully loading all
848 * firmwares, we can:
849 * 1. Identify that BASE firmware with type=0 has been loaded;
850 * 2. Tell whether BASE firmware was just changed the next time through.
851 */
852 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300853
854 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300855
856fail:
857 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300858 if (!is_retry) {
859 msleep(50);
860 is_retry = 1;
861 tuner_dbg("Retrying firmware load\n");
862 goto retry;
863 }
864
Chris Pascoee0f0b372007-11-19 11:22:03 -0300865 if (rc == -ENOENT)
866 rc = -EINVAL;
867 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300868}
869
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300870static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300871{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300872 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300873 u16 frq_lock, signal = 0;
874 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300875
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300876 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300877
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300878 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300879
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300880 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300881 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300882 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300883 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300884
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300885 /* Frequency is locked */
886 if (frq_lock == 1)
887 signal = 32768;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300888
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300889 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300890 rc = xc2028_get_reg(priv, 0x0040, &signal);
891 if (rc < 0)
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300892 goto ret;
893
894 /* Use both frq_lock and signal to generate the result */
895 signal = signal || ((signal & 0x07) << 12);
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300896
897ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300898 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300899
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300900 *strength = signal;
901
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300902 tuner_dbg("signal strength is %d\n", signal);
903
Chris Pascoe7d58d112007-11-19 04:31:58 -0300904 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300905}
906
907#define DIV 15625
908
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300909static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300910 enum tuner_mode new_mode,
911 unsigned int type,
912 v4l2_std_id std,
913 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300914{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300915 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300916 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300917 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300918 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300919
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300920 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300921
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300922 mutex_lock(&priv->lock);
923
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300924 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300925
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300926 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300927 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300928
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300929 /* On some cases xc2028 can disable video output, if
930 * very weak signals are received. By sending a soft
931 * reset, this is re-enabled. So, it is better to always
932 * send a soft reset before changing channels, to be sure
933 * that xc2028 will be in a safe state.
934 * Maybe this might also be needed for DTV.
935 */
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200936 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300937 rc = send_seq(priv, {0x00, 0x00});
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300938
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200939 /* Analog modes require offset = 0 */
940 } else {
941 /*
942 * Digital modes require an offset to adjust to the
943 * proper frequency. The offset depends on what
944 * firmware version is used.
945 */
946
947 /*
948 * Adjust to the center frequency. This is calculated by the
949 * formula: offset = 1.25MHz - BW/2
950 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
951 * further adjustment to get the frequency center on VHF
952 */
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300953 if (priv->cur_fw.type & DTV6)
954 offset = 1750000;
955 else if (priv->cur_fw.type & DTV7)
956 offset = 2250000;
957 else /* DTV8 or DTV78 */
958 offset = 2750000;
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300959 if ((priv->cur_fw.type & DTV78) && freq < 470000000)
Chris Pascoea44f1c42007-11-19 06:35:26 -0300960 offset -= 500000;
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200961
962 /*
963 * xc3028 additional "magic"
964 * Depending on the firmware version, it needs some adjustments
965 * to properly centralize the frequency. This seems to be
966 * needed to compensate the SCODE table adjustments made by
967 * newer firmwares
968 */
969
970#if 1
971 /*
972 * The proper adjustment would be to do it at s-code table.
973 * However, this didn't work, as reported by
974 * Robert Lowery <rglowery@exemail.com.au>
975 */
976
977 if (priv->cur_fw.type & DTV7)
978 offset += 500000;
979
980#else
981 /*
982 * Still need tests for XC3028L (firmware 3.2 or upper)
983 * So, for now, let's just comment the per-firmware
984 * version of this change. Reports with xc3028l working
985 * with and without the lines bellow are welcome
986 */
987
988 if (priv->firm_version < 0x0302) {
989 if (priv->cur_fw.type & DTV7)
990 offset += 500000;
991 } else {
992 if (priv->cur_fw.type & DTV7)
993 offset -= 300000;
994 else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
995 offset += 200000;
996 }
997#endif
Chris Pascoea44f1c42007-11-19 06:35:26 -0300998 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300999
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001000 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -03001001
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001002 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001003 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -03001004 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
1005 else
1006 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
1007 if (rc < 0)
1008 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001009
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -03001010 /* Return code shouldn't be checked.
1011 The reset CLK is needed only with tm6000.
1012 Driver should work fine even if this fails.
1013 */
Devin Heitmueller70ca3c42010-01-19 01:38:45 -03001014 if (priv->ctrl.msleep)
1015 msleep(priv->ctrl.msleep);
Michael Krufkyd7cba042008-09-12 13:31:45 -03001016 do_tuner_callback(fe, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001017
1018 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -03001019
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001020 buf[0] = 0xff & (div >> 24);
1021 buf[1] = 0xff & (div >> 16);
1022 buf[2] = 0xff & (div >> 8);
1023 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001024
Chris Pascoe47cc5b72007-11-19 04:14:23 -03001025 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001026 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001027 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001028 msleep(100);
1029
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001030 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001031
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -03001032 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
1033 buf[0], buf[1], buf[2], buf[3],
1034 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001035
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001036 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001037
1038ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001039 mutex_unlock(&priv->lock);
1040
1041 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001042}
1043
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001044static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001045 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -03001046{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001047 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001048 unsigned int type=0;
1049
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001050 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -03001051
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001052 if (p->mode == V4L2_TUNER_RADIO) {
1053 type |= FM;
1054 if (priv->ctrl.input1)
1055 type |= INPUT1;
1056 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehabe2860d92009-06-06 08:15:08 -03001057 T_RADIO, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001058 }
1059
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -03001060 /* if std is not defined, choose one */
1061 if (!p->std)
1062 p->std = V4L2_STD_MN;
1063
1064 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001065 if (!(p->std & V4L2_STD_MN))
1066 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -03001067
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001068 /* Add audio hack to std mask */
1069 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001070
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001071 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001072 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -03001073}
1074
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001075static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -03001076 struct dvb_frontend_parameters *p)
1077{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001078 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001079 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001080 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -03001081 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -03001082
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001083 tuner_dbg("%s called\n", __func__);
Michel Ludwig701672e2007-07-18 10:29:10 -03001084
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001085 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001086 case FE_OFDM:
1087 bw = p->u.ofdm.bandwidth;
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001088 /*
1089 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1090 * Both seem to require QAM firmware for OFDM decoding
1091 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1092 */
1093 if (bw == BANDWIDTH_6_MHZ)
1094 type |= QAM;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001095 break;
1096 case FE_ATSC:
1097 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001098 /* The only ATSC firmware (at least on v2.7) is D2633 */
1099 type |= ATSC | D2633;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001100 break;
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001101 /* DVB-S and pure QAM (FE_QAM) are not supported */
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001102 default:
1103 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001104 }
1105
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001106 switch (bw) {
1107 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001108 if (p->frequency < 470000000)
1109 priv->ctrl.vhfbw7 = 0;
1110 else
1111 priv->ctrl.uhfbw8 = 1;
1112 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1113 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001114 break;
1115 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001116 if (p->frequency < 470000000)
1117 priv->ctrl.vhfbw7 = 1;
1118 else
1119 priv->ctrl.uhfbw8 = 0;
1120 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1121 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001122 break;
1123 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001124 type |= DTV6;
1125 priv->ctrl.vhfbw7 = 0;
1126 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001127 break;
1128 default:
1129 tuner_err("error: bandwidth not supported.\n");
1130 };
1131
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001132 /*
1133 Selects between D2633 or D2620 firmware.
1134 It doesn't make sense for ATSC, since it should be D2633 on all cases
1135 */
1136 if (fe->ops.info.type != FE_ATSC) {
1137 switch (priv->ctrl.type) {
1138 case XC2028_D2633:
1139 type |= D2633;
1140 break;
1141 case XC2028_D2620:
1142 type |= D2620;
1143 break;
1144 case XC2028_AUTO:
1145 default:
1146 /* Zarlink seems to need D2633 */
1147 if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
1148 type |= D2633;
1149 else
1150 type |= D2620;
1151 }
1152 }
1153
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001154 /* All S-code tables need a 200kHz shift */
Andy Walls6e707b42009-06-11 07:57:50 -03001155 if (priv->ctrl.demod) {
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001156 demod = priv->ctrl.demod;
1157
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001158 /*
1159 * Newer firmwares require a 200 kHz offset only for ATSC
1160 */
1161 if (type == ATSC || priv->firm_version < 0x0302)
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001162 demod += 200;
Andy Walls6e707b42009-06-11 07:57:50 -03001163 /*
1164 * The DTV7 S-code table needs a 700 kHz shift.
Andy Walls6e707b42009-06-11 07:57:50 -03001165 *
1166 * DTV7 is only used in Australia. Germany or Italy may also
1167 * use this firmware after initialization, but a tune to a UHF
1168 * channel should then cause DTV78 to be used.
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001169 *
1170 * Unfortunately, on real-field tests, the s-code offset
1171 * didn't work as expected, as reported by
1172 * Robert Lowery <rglowery@exemail.com.au>
Andy Walls6e707b42009-06-11 07:57:50 -03001173 */
Andy Walls6e707b42009-06-11 07:57:50 -03001174 }
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001175
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001176 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001177 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001178}
1179
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001180static int xc2028_sleep(struct dvb_frontend *fe)
1181{
1182 struct xc2028_data *priv = fe->tuner_priv;
1183 int rc = 0;
1184
Devin Heitmueller93b99922009-08-03 22:52:59 -03001185 /* Avoid firmware reload on slow devices or if PM disabled */
1186 if (no_poweroff || priv->ctrl.disable_power_mgmt)
Mauro Carvalho Chehab10f201a2008-12-05 10:49:53 -03001187 return 0;
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001188
1189 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
Mauro Carvalho Chehabe278e742008-12-18 06:00:25 -03001190 if (debug > 1) {
1191 tuner_dbg("Printing sleep stack trace:\n");
1192 dump_stack();
1193 }
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001194
1195 mutex_lock(&priv->lock);
1196
1197 if (priv->firm_version < 0x0202)
1198 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1199 else
1200 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1201
1202 priv->cur_fw.type = 0; /* need firmware reload */
1203
1204 mutex_unlock(&priv->lock);
1205
1206 return rc;
1207}
Chris Pascoe45819c32007-11-19 11:41:20 -03001208
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001209static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001210{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001211 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001212
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001213 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001214
Chris Pascoeaa501be2007-11-19 04:45:38 -03001215 mutex_lock(&xc2028_list_mutex);
1216
Michael Krufkyc663d032008-04-18 21:22:50 -03001217 /* only perform final cleanup if this is the last instance */
1218 if (hybrid_tuner_report_instance_count(priv) == 1) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001219 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001220 free_firmware(priv);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001221 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001222
Michael Krufkyc663d032008-04-18 21:22:50 -03001223 if (priv)
1224 hybrid_tuner_release_state(priv);
1225
Chris Pascoeaa501be2007-11-19 04:45:38 -03001226 mutex_unlock(&xc2028_list_mutex);
1227
Michael Krufkyc663d032008-04-18 21:22:50 -03001228 fe->tuner_priv = NULL;
1229
Michel Ludwig701672e2007-07-18 10:29:10 -03001230 return 0;
1231}
1232
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001233static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001234{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001235 struct xc2028_data *priv = fe->tuner_priv;
1236
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001237 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001238
1239 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001240
1241 return 0;
1242}
1243
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001244static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001245{
1246 struct xc2028_data *priv = fe->tuner_priv;
1247 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001248 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001249
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001250 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001251
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001252 mutex_lock(&priv->lock);
1253
Chris Pascoe0a196b62007-11-19 09:29:59 -03001254 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001255 if (priv->ctrl.max_len < 9)
1256 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001257
1258 if (p->fname) {
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001259 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1260 kfree(priv->ctrl.fname);
1261 free_firmware(priv);
1262 }
1263
Chris Pascoe0a196b62007-11-19 09:29:59 -03001264 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1265 if (priv->ctrl.fname == NULL)
1266 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001267 }
1268
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001269 mutex_unlock(&priv->lock);
1270
Chris Pascoe0a196b62007-11-19 09:29:59 -03001271 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001272}
1273
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001274static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001275 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001276 .name = "Xceive XC3028",
1277 .frequency_min = 42000000,
1278 .frequency_max = 864000000,
1279 .frequency_step = 50000,
1280 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001281
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001282 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001283 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001284 .release = xc2028_dvb_release,
1285 .get_frequency = xc2028_get_frequency,
1286 .get_rf_strength = xc2028_signal,
1287 .set_params = xc2028_set_params,
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001288 .sleep = xc2028_sleep,
Michel Ludwig701672e2007-07-18 10:29:10 -03001289};
1290
Michael Krufky7972f982007-12-21 16:12:09 -03001291struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1292 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001293{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001294 struct xc2028_data *priv;
Michael Krufkyc663d032008-04-18 21:22:50 -03001295 int instance;
Michel Ludwig701672e2007-07-18 10:29:10 -03001296
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001297 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001298 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001299
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001300 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001301 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001302
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001303 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001304 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001305 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001306 }
1307
Chris Pascoeaa501be2007-11-19 04:45:38 -03001308 mutex_lock(&xc2028_list_mutex);
1309
Michael Krufkyc663d032008-04-18 21:22:50 -03001310 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1311 hybrid_tuner_instance_list,
1312 cfg->i2c_adap, cfg->i2c_addr,
1313 "xc2028");
1314 switch (instance) {
1315 case 0:
1316 /* memory allocation failure */
1317 goto fail;
1318 break;
1319 case 1:
1320 /* new tuner instance */
Chris Pascoe0a196b62007-11-19 09:29:59 -03001321 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001322
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001323 mutex_init(&priv->lock);
1324
Michael Krufkyc663d032008-04-18 21:22:50 -03001325 fe->tuner_priv = priv;
1326 break;
1327 case 2:
1328 /* existing tuner instance */
1329 fe->tuner_priv = priv;
1330 break;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001331 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001332
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001333 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001334 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001335
1336 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001337
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001338 if (cfg->ctrl)
1339 xc2028_set_config(fe, cfg->ctrl);
1340
Chris Pascoeaa501be2007-11-19 04:45:38 -03001341 mutex_unlock(&xc2028_list_mutex);
1342
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001343 return fe;
Michael Krufkyc663d032008-04-18 21:22:50 -03001344fail:
1345 mutex_unlock(&xc2028_list_mutex);
1346
1347 xc2028_dvb_release(fe);
1348 return NULL;
Michel Ludwig701672e2007-07-18 10:29:10 -03001349}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001350
Michel Ludwig701672e2007-07-18 10:29:10 -03001351EXPORT_SYMBOL(xc2028_attach);
1352
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001353MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001354MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001355MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1356MODULE_LICENSE("GPL");