blob: 3acbaa04e1b3b3c6a4711211b5bf9acfe1e1dbe8 [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 }
Thierry Reding4d37ece2011-08-04 04:13:59 -0300617
618 /* silently fail if the frontend doesn't support I2C flush */
619 rc = do_tuner_callback(fe, XC2028_I2C_FLUSH, 0);
620 if ((rc < 0) && (rc != -EINVAL)) {
621 tuner_err("error executing flush: %d\n", rc);
622 return rc;
623 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300624 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300625 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300626}
627
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300628static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300629 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300630{
631 struct xc2028_data *priv = fe->tuner_priv;
632 int pos, rc;
633 unsigned char *p;
634
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300635 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300636
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300637 if (!int_freq) {
638 pos = seek_firmware(fe, type, id);
639 if (pos < 0)
640 return pos;
641 } else {
642 for (pos = 0; pos < priv->firm_size; pos++) {
643 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300644 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300645 break;
646 }
647 if (pos == priv->firm_size)
648 return -ENOENT;
649 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300650
651 p = priv->firm[pos].ptr;
652
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300653 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300654 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
655 return -EINVAL;
656 p += 12 * scode;
657 } else {
658 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
659 * has a 2-byte size header in the firmware format. */
660 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
661 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
662 return -EINVAL;
663 p += 14 * scode + 2;
664 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300665
Chris Pascoed7b22c52007-11-19 10:12:45 -0300666 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300667 dump_firm_type_and_int_freq(priv->firm[pos].type,
668 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300669 printk("(%x), id %016llx.\n", priv->firm[pos].type,
670 (unsigned long long)*id);
671
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300672 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300673 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
674 else
675 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
676 if (rc < 0)
677 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300678
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300679 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300680 if (rc < 0)
681 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300682
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300683 rc = send_seq(priv, {0x00, 0x8c});
684 if (rc < 0)
685 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300686
687 return 0;
688}
689
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300690static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300691 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300692{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300693 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300694 struct firmware_properties new_fw;
Alina Friedrichsenb8bc77d2011-01-23 12:27:05 -0300695 int rc = 0, retry_count = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300696 u16 version, hwmodel;
697 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300698
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300699 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300700
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300701 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300702 if (!priv->ctrl.fname) {
703 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300704 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300705 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300706
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300707 rc = load_all_firmwares(fe);
708 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300709 return rc;
710 }
711
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300712 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300713 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300714
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300715retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300716 new_fw.type = type;
717 new_fw.id = std;
718 new_fw.std_req = std;
719 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
720 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300721 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300722
723 tuner_dbg("checking firmware, user requested type=");
724 if (debug) {
725 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300726 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300727 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300728 if (!int_freq) {
729 printk("scode_tbl ");
730 dump_firm_type(priv->ctrl.scode_table);
731 printk("(%x), ", priv->ctrl.scode_table);
732 } else
733 printk("int_freq %d, ", new_fw.int_freq);
734 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300735 }
736
737 /* No need to reload base firmware if it matches */
738 if (((BASE | new_fw.type) & BASE_TYPES) ==
739 (priv->cur_fw.type & BASE_TYPES)) {
740 tuner_dbg("BASE firmware not changed.\n");
741 goto skip_base;
742 }
743
744 /* Updating BASE - forget about all currently loaded firmware */
745 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
746
747 /* Reset is needed before loading firmware */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300748 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300749 if (rc < 0)
750 goto fail;
751
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300752 /* BASE firmwares are all std0 */
753 std0 = 0;
754 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300755 if (rc < 0) {
756 tuner_err("Error %d while loading base firmware\n",
757 rc);
758 goto fail;
759 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300760
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300761 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300762 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300763
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300764 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300765 if (rc == -ENOENT)
766 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
767 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300768 if (rc < 0 && rc != -ENOENT) {
769 tuner_err("Error %d while loading init1 firmware\n",
770 rc);
771 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300772 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300773
Chris Pascoee0f0b372007-11-19 11:22:03 -0300774skip_base:
775 /*
776 * No need to reload standard specific firmware if base firmware
777 * was not reloaded and requested video standards have not changed.
778 */
779 if (priv->cur_fw.type == (BASE | new_fw.type) &&
780 priv->cur_fw.std_req == std) {
781 tuner_dbg("Std-specific firmware already loaded.\n");
782 goto skip_std_specific;
783 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300784
Chris Pascoee0f0b372007-11-19 11:22:03 -0300785 /* Reloading std-specific firmware forces a SCODE update */
786 priv->cur_fw.scode_table = 0;
787
Chris Pascoee0f0b372007-11-19 11:22:03 -0300788 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300789 if (rc == -ENOENT)
790 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
791
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300792 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300793 goto fail;
794
795skip_std_specific:
796 if (priv->cur_fw.scode_table == new_fw.scode_table &&
797 priv->cur_fw.scode_nr == new_fw.scode_nr) {
798 tuner_dbg("SCODE firmware already loaded.\n");
799 goto check_device;
800 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300801
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300802 if (new_fw.type & FM)
803 goto check_device;
804
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300805 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300806 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300807
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300808 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
809 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300810
Chris Pascoee0f0b372007-11-19 11:22:03 -0300811check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300812 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
813 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
814 tuner_err("Unable to read tuner registers.\n");
815 goto fail;
816 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300817
Devin Heitmuellerb37f2d62008-04-21 07:02:09 -0300818 tuner_dbg("Device is Xceive %d version %d.%d, "
819 "firmware version %d.%d\n",
820 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
821 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300822
Mauro Carvalho Chehab0fb84ce2008-12-02 08:30:16 -0300823
824 if (priv->ctrl.read_not_reliable)
825 goto read_not_reliable;
826
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300827 /* Check firmware version against what we downloaded. */
828 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
Mauro Carvalho Chehab2d5024a2009-09-14 10:23:20 -0300829 if (!priv->ctrl.read_not_reliable) {
830 tuner_err("Incorrect readback of firmware version.\n");
831 goto fail;
832 } else {
833 tuner_err("Returned an incorrect version. However, "
834 "read is not reliable enough. Ignoring it.\n");
835 hwmodel = 3028;
836 }
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300837 }
838
839 /* Check that the tuner hardware model remains consistent over time. */
840 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
841 priv->hwmodel = hwmodel;
842 priv->hwvers = version & 0xff00;
843 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
844 priv->hwvers != (version & 0xff00)) {
845 tuner_err("Read invalid device hardware information - tuner "
846 "hung?\n");
847 goto fail;
848 }
849
Mauro Carvalho Chehab0fb84ce2008-12-02 08:30:16 -0300850read_not_reliable:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300851 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
852
853 /*
854 * By setting BASE in cur_fw.type only after successfully loading all
855 * firmwares, we can:
856 * 1. Identify that BASE firmware with type=0 has been loaded;
857 * 2. Tell whether BASE firmware was just changed the next time through.
858 */
859 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300860
861 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300862
863fail:
864 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Alina Friedrichsenb8bc77d2011-01-23 12:27:05 -0300865 if (retry_count < 8) {
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300866 msleep(50);
Alina Friedrichsenb8bc77d2011-01-23 12:27:05 -0300867 retry_count++;
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300868 tuner_dbg("Retrying firmware load\n");
869 goto retry;
870 }
871
Chris Pascoee0f0b372007-11-19 11:22:03 -0300872 if (rc == -ENOENT)
873 rc = -EINVAL;
874 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300875}
876
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300877static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300878{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300879 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300880 u16 frq_lock, signal = 0;
881 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300882
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300883 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300884
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300885 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300886
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300887 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300888 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300889 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300890 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300891
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300892 /* Frequency is locked */
893 if (frq_lock == 1)
894 signal = 32768;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300895
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300896 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300897 rc = xc2028_get_reg(priv, 0x0040, &signal);
898 if (rc < 0)
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300899 goto ret;
900
901 /* Use both frq_lock and signal to generate the result */
902 signal = signal || ((signal & 0x07) << 12);
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300903
904ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300905 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300906
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300907 *strength = signal;
908
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300909 tuner_dbg("signal strength is %d\n", signal);
910
Chris Pascoe7d58d112007-11-19 04:31:58 -0300911 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300912}
913
914#define DIV 15625
915
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300916static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Hans Verkuilaa40d192011-03-06 09:24:32 -0300917 enum v4l2_tuner_type new_type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300918 unsigned int type,
919 v4l2_std_id std,
920 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300921{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300922 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300923 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300924 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300925 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300926
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300927 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300928
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300929 mutex_lock(&priv->lock);
930
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300931 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300932
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300933 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300934 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300935
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300936 /* On some cases xc2028 can disable video output, if
937 * very weak signals are received. By sending a soft
938 * reset, this is re-enabled. So, it is better to always
939 * send a soft reset before changing channels, to be sure
940 * that xc2028 will be in a safe state.
941 * Maybe this might also be needed for DTV.
942 */
Mauro Carvalho Chehabfd34cb02011-08-31 15:12:45 -0300943 switch (new_type) {
944 case V4L2_TUNER_ANALOG_TV:
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300945 rc = send_seq(priv, {0x00, 0x00});
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300946
Mauro Carvalho Chehabfd34cb02011-08-31 15:12:45 -0300947 /* Analog mode requires offset = 0 */
948 break;
949 case V4L2_TUNER_RADIO:
950 /* Radio mode requires offset = 0 */
951 break;
952 case V4L2_TUNER_DIGITAL_TV:
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200953 /*
954 * Digital modes require an offset to adjust to the
955 * proper frequency. The offset depends on what
956 * firmware version is used.
957 */
958
959 /*
960 * Adjust to the center frequency. This is calculated by the
961 * formula: offset = 1.25MHz - BW/2
962 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
963 * further adjustment to get the frequency center on VHF
964 */
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300965 if (priv->cur_fw.type & DTV6)
966 offset = 1750000;
967 else if (priv->cur_fw.type & DTV7)
968 offset = 2250000;
969 else /* DTV8 or DTV78 */
970 offset = 2750000;
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300971 if ((priv->cur_fw.type & DTV78) && freq < 470000000)
Chris Pascoea44f1c42007-11-19 06:35:26 -0300972 offset -= 500000;
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200973
974 /*
975 * xc3028 additional "magic"
976 * Depending on the firmware version, it needs some adjustments
977 * to properly centralize the frequency. This seems to be
978 * needed to compensate the SCODE table adjustments made by
979 * newer firmwares
980 */
981
982#if 1
983 /*
984 * The proper adjustment would be to do it at s-code table.
985 * However, this didn't work, as reported by
986 * Robert Lowery <rglowery@exemail.com.au>
987 */
988
989 if (priv->cur_fw.type & DTV7)
990 offset += 500000;
991
992#else
993 /*
994 * Still need tests for XC3028L (firmware 3.2 or upper)
995 * So, for now, let's just comment the per-firmware
996 * version of this change. Reports with xc3028l working
997 * with and without the lines bellow are welcome
998 */
999
1000 if (priv->firm_version < 0x0302) {
1001 if (priv->cur_fw.type & DTV7)
1002 offset += 500000;
1003 } else {
1004 if (priv->cur_fw.type & DTV7)
1005 offset -= 300000;
1006 else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
1007 offset += 200000;
1008 }
1009#endif
Chris Pascoea44f1c42007-11-19 06:35:26 -03001010 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -03001011
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001012 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -03001013
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001014 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001015 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -03001016 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
1017 else
1018 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
1019 if (rc < 0)
1020 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001021
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -03001022 /* Return code shouldn't be checked.
1023 The reset CLK is needed only with tm6000.
1024 Driver should work fine even if this fails.
1025 */
Devin Heitmueller70ca3c42010-01-19 01:38:45 -03001026 if (priv->ctrl.msleep)
1027 msleep(priv->ctrl.msleep);
Michael Krufkyd7cba042008-09-12 13:31:45 -03001028 do_tuner_callback(fe, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001029
1030 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -03001031
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001032 buf[0] = 0xff & (div >> 24);
1033 buf[1] = 0xff & (div >> 16);
1034 buf[2] = 0xff & (div >> 8);
1035 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001036
Chris Pascoe47cc5b72007-11-19 04:14:23 -03001037 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001038 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001039 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001040 msleep(100);
1041
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001042 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001043
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -03001044 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
1045 buf[0], buf[1], buf[2], buf[3],
1046 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001047
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001048 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001049
1050ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001051 mutex_unlock(&priv->lock);
1052
1053 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001054}
1055
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001056static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001057 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -03001058{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001059 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001060 unsigned int type=0;
1061
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001062 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -03001063
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001064 if (p->mode == V4L2_TUNER_RADIO) {
1065 type |= FM;
1066 if (priv->ctrl.input1)
1067 type |= INPUT1;
1068 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab437f5fa2011-02-21 21:03:59 -03001069 V4L2_TUNER_RADIO, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001070 }
1071
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -03001072 /* if std is not defined, choose one */
1073 if (!p->std)
1074 p->std = V4L2_STD_MN;
1075
1076 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001077 if (!(p->std & V4L2_STD_MN))
1078 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -03001079
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001080 /* Add audio hack to std mask */
1081 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001082
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001083 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab437f5fa2011-02-21 21:03:59 -03001084 V4L2_TUNER_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -03001085}
1086
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001087static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -03001088 struct dvb_frontend_parameters *p)
1089{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001090 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001091 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001092 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -03001093 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -03001094
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001095 tuner_dbg("%s called\n", __func__);
Michel Ludwig701672e2007-07-18 10:29:10 -03001096
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001097 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001098 case FE_OFDM:
1099 bw = p->u.ofdm.bandwidth;
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001100 /*
1101 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1102 * Both seem to require QAM firmware for OFDM decoding
1103 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1104 */
1105 if (bw == BANDWIDTH_6_MHZ)
1106 type |= QAM;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001107 break;
1108 case FE_ATSC:
1109 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001110 /* The only ATSC firmware (at least on v2.7) is D2633 */
1111 type |= ATSC | D2633;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001112 break;
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001113 /* DVB-S and pure QAM (FE_QAM) are not supported */
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001114 default:
1115 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001116 }
1117
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001118 switch (bw) {
1119 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001120 if (p->frequency < 470000000)
1121 priv->ctrl.vhfbw7 = 0;
1122 else
1123 priv->ctrl.uhfbw8 = 1;
1124 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1125 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001126 break;
1127 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001128 if (p->frequency < 470000000)
1129 priv->ctrl.vhfbw7 = 1;
1130 else
1131 priv->ctrl.uhfbw8 = 0;
1132 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1133 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001134 break;
1135 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001136 type |= DTV6;
1137 priv->ctrl.vhfbw7 = 0;
1138 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001139 break;
1140 default:
1141 tuner_err("error: bandwidth not supported.\n");
1142 };
1143
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001144 /*
1145 Selects between D2633 or D2620 firmware.
1146 It doesn't make sense for ATSC, since it should be D2633 on all cases
1147 */
1148 if (fe->ops.info.type != FE_ATSC) {
1149 switch (priv->ctrl.type) {
1150 case XC2028_D2633:
1151 type |= D2633;
1152 break;
1153 case XC2028_D2620:
1154 type |= D2620;
1155 break;
1156 case XC2028_AUTO:
1157 default:
1158 /* Zarlink seems to need D2633 */
1159 if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
1160 type |= D2633;
1161 else
1162 type |= D2620;
1163 }
1164 }
1165
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001166 /* All S-code tables need a 200kHz shift */
Andy Walls6e707b42009-06-11 07:57:50 -03001167 if (priv->ctrl.demod) {
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001168 demod = priv->ctrl.demod;
1169
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001170 /*
1171 * Newer firmwares require a 200 kHz offset only for ATSC
1172 */
1173 if (type == ATSC || priv->firm_version < 0x0302)
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001174 demod += 200;
Andy Walls6e707b42009-06-11 07:57:50 -03001175 /*
1176 * The DTV7 S-code table needs a 700 kHz shift.
Andy Walls6e707b42009-06-11 07:57:50 -03001177 *
1178 * DTV7 is only used in Australia. Germany or Italy may also
1179 * use this firmware after initialization, but a tune to a UHF
1180 * channel should then cause DTV78 to be used.
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001181 *
1182 * Unfortunately, on real-field tests, the s-code offset
1183 * didn't work as expected, as reported by
1184 * Robert Lowery <rglowery@exemail.com.au>
Andy Walls6e707b42009-06-11 07:57:50 -03001185 */
Andy Walls6e707b42009-06-11 07:57:50 -03001186 }
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001187
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001188 return generic_set_freq(fe, p->frequency,
Mauro Carvalho Chehab437f5fa2011-02-21 21:03:59 -03001189 V4L2_TUNER_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001190}
1191
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001192static int xc2028_sleep(struct dvb_frontend *fe)
1193{
1194 struct xc2028_data *priv = fe->tuner_priv;
1195 int rc = 0;
1196
Devin Heitmueller93b99922009-08-03 22:52:59 -03001197 /* Avoid firmware reload on slow devices or if PM disabled */
1198 if (no_poweroff || priv->ctrl.disable_power_mgmt)
Mauro Carvalho Chehab10f201a2008-12-05 10:49:53 -03001199 return 0;
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001200
1201 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
Mauro Carvalho Chehabe278e742008-12-18 06:00:25 -03001202 if (debug > 1) {
1203 tuner_dbg("Printing sleep stack trace:\n");
1204 dump_stack();
1205 }
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001206
1207 mutex_lock(&priv->lock);
1208
1209 if (priv->firm_version < 0x0202)
1210 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1211 else
1212 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1213
1214 priv->cur_fw.type = 0; /* need firmware reload */
1215
1216 mutex_unlock(&priv->lock);
1217
1218 return rc;
1219}
Chris Pascoe45819c32007-11-19 11:41:20 -03001220
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001221static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001222{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001223 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001224
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001225 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001226
Chris Pascoeaa501be2007-11-19 04:45:38 -03001227 mutex_lock(&xc2028_list_mutex);
1228
Michael Krufkyc663d032008-04-18 21:22:50 -03001229 /* only perform final cleanup if this is the last instance */
1230 if (hybrid_tuner_report_instance_count(priv) == 1) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001231 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001232 free_firmware(priv);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001233 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001234
Michael Krufkyc663d032008-04-18 21:22:50 -03001235 if (priv)
1236 hybrid_tuner_release_state(priv);
1237
Chris Pascoeaa501be2007-11-19 04:45:38 -03001238 mutex_unlock(&xc2028_list_mutex);
1239
Michael Krufkyc663d032008-04-18 21:22:50 -03001240 fe->tuner_priv = NULL;
1241
Michel Ludwig701672e2007-07-18 10:29:10 -03001242 return 0;
1243}
1244
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001245static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001246{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001247 struct xc2028_data *priv = fe->tuner_priv;
1248
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001249 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001250
1251 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001252
1253 return 0;
1254}
1255
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001256static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001257{
1258 struct xc2028_data *priv = fe->tuner_priv;
1259 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001260 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001261
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001262 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001263
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001264 mutex_lock(&priv->lock);
1265
Chris Pascoe0a196b62007-11-19 09:29:59 -03001266 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001267 if (priv->ctrl.max_len < 9)
1268 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001269
1270 if (p->fname) {
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001271 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1272 kfree(priv->ctrl.fname);
1273 free_firmware(priv);
1274 }
1275
Chris Pascoe0a196b62007-11-19 09:29:59 -03001276 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1277 if (priv->ctrl.fname == NULL)
1278 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001279 }
1280
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001281 mutex_unlock(&priv->lock);
1282
Chris Pascoe0a196b62007-11-19 09:29:59 -03001283 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001284}
1285
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001286static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001287 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001288 .name = "Xceive XC3028",
1289 .frequency_min = 42000000,
1290 .frequency_max = 864000000,
1291 .frequency_step = 50000,
1292 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001293
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001294 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001295 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001296 .release = xc2028_dvb_release,
1297 .get_frequency = xc2028_get_frequency,
1298 .get_rf_strength = xc2028_signal,
1299 .set_params = xc2028_set_params,
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001300 .sleep = xc2028_sleep,
Michel Ludwig701672e2007-07-18 10:29:10 -03001301};
1302
Michael Krufky7972f982007-12-21 16:12:09 -03001303struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1304 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001305{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001306 struct xc2028_data *priv;
Michael Krufkyc663d032008-04-18 21:22:50 -03001307 int instance;
Michel Ludwig701672e2007-07-18 10:29:10 -03001308
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001309 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001310 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001311
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001312 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001313 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001314
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001315 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001316 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001317 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001318 }
1319
Chris Pascoeaa501be2007-11-19 04:45:38 -03001320 mutex_lock(&xc2028_list_mutex);
1321
Michael Krufkyc663d032008-04-18 21:22:50 -03001322 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1323 hybrid_tuner_instance_list,
1324 cfg->i2c_adap, cfg->i2c_addr,
1325 "xc2028");
1326 switch (instance) {
1327 case 0:
1328 /* memory allocation failure */
1329 goto fail;
1330 break;
1331 case 1:
1332 /* new tuner instance */
Chris Pascoe0a196b62007-11-19 09:29:59 -03001333 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001334
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001335 mutex_init(&priv->lock);
1336
Michael Krufkyc663d032008-04-18 21:22:50 -03001337 fe->tuner_priv = priv;
1338 break;
1339 case 2:
1340 /* existing tuner instance */
1341 fe->tuner_priv = priv;
1342 break;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001343 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001344
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001345 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001346 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001347
1348 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001349
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001350 if (cfg->ctrl)
1351 xc2028_set_config(fe, cfg->ctrl);
1352
Chris Pascoeaa501be2007-11-19 04:45:38 -03001353 mutex_unlock(&xc2028_list_mutex);
1354
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001355 return fe;
Michael Krufkyc663d032008-04-18 21:22:50 -03001356fail:
1357 mutex_unlock(&xc2028_list_mutex);
1358
1359 xc2028_dvb_release(fe);
1360 return NULL;
Michel Ludwig701672e2007-07-18 10:29:10 -03001361}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001362
Michel Ludwig701672e2007-07-18 10:29:10 -03001363EXPORT_SYMBOL(xc2028_attach);
1364
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001365MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001366MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001367MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1368MODULE_LICENSE("GPL");