blob: b5ee3ebfcfca03e2f9fd5973cd5d72b52d1c5f53 [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
Miroslav Slugen304bce42011-12-11 20:19:34 -030027/* Registers (Write-only) */
28#define XREG_INIT 0x00
29#define XREG_RF_FREQ 0x02
30#define XREG_POWER_DOWN 0x08
31
32/* Registers (Read-only) */
33#define XREG_FREQ_ERROR 0x01
34#define XREG_LOCK 0x02
35#define XREG_VERSION 0x04
36#define XREG_PRODUCT_ID 0x08
37#define XREG_HSYNC_FREQ 0x10
38#define XREG_FRAME_LINES 0x20
39#define XREG_SNR 0x40
40
41#define XREG_ADC_ENV 0x0100
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -030042
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030043static int debug;
44module_param(debug, int, 0644);
45MODULE_PARM_DESC(debug, "enable verbose debug messages");
46
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -030047static int no_poweroff;
48module_param(no_poweroff, int, 0644);
Devin Heitmueller49008772009-04-28 16:22:47 -030049MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -030050 "1 keep device energized and with tuner ready all the times.\n"
51 " Faster, but consumes more power and keeps the device hotter\n");
52
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -030053static char audio_std[8];
54module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
55MODULE_PARM_DESC(audio_std,
56 "Audio standard. XC3028 audio decoder explicitly "
57 "needs to know what audio\n"
58 "standard is needed for some video standards with audio A2 or NICAM.\n"
59 "The valid values are:\n"
60 "A2\n"
61 "A2/A\n"
62 "A2/B\n"
63 "NICAM\n"
64 "NICAM/A\n"
65 "NICAM/B\n");
66
Samuel Ortiz4327b772009-05-27 00:49:33 +020067static char firmware_name[30];
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -030068module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
69MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
70 "default firmware name\n");
71
Michael Krufkyc663d032008-04-18 21:22:50 -030072static LIST_HEAD(hybrid_tuner_instance_list);
Chris Pascoeaa501be2007-11-19 04:45:38 -030073static DEFINE_MUTEX(xc2028_list_mutex);
74
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030075/* struct for storing firmware table */
76struct firmware_description {
77 unsigned int type;
78 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030079 __u16 int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030080 unsigned char *ptr;
81 unsigned int size;
82};
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030083
Chris Pascoee0f0b372007-11-19 11:22:03 -030084struct firmware_properties {
85 unsigned int type;
86 v4l2_std_id id;
87 v4l2_std_id std_req;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030088 __u16 int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -030089 unsigned int scode_table;
90 int scode_nr;
91};
92
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030093struct xc2028_data {
Michael Krufkyc663d032008-04-18 21:22:50 -030094 struct list_head hybrid_tuner_instance_list;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030095 struct tuner_i2c_props i2c_props;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030096 __u32 frequency;
97
98 struct firmware_description *firm;
99 int firm_size;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300100 __u16 firm_version;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300101
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300102 __u16 hwmodel;
103 __u16 hwvers;
104
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300105 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300106
Chris Pascoee0f0b372007-11-19 11:22:03 -0300107 struct firmware_properties cur_fw;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300108
109 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300110};
111
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300112#define i2c_send(priv, buf, size) ({ \
113 int _rc; \
114 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
115 if (size != _rc) \
116 tuner_info("i2c output error: rc = %d (should be %d)\n",\
117 _rc, (int)size); \
Devin Heitmueller70ca3c42010-01-19 01:38:45 -0300118 if (priv->ctrl.msleep) \
119 msleep(priv->ctrl.msleep); \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300120 _rc; \
121})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300122
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300123#define i2c_rcv(priv, buf, size) ({ \
124 int _rc; \
125 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
126 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300127 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300128 _rc, (int)size); \
129 _rc; \
130})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300131
Chris Pascoe7d58d112007-11-19 04:31:58 -0300132#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
133 int _rc; \
134 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
135 ibuf, isize); \
136 if (isize != _rc) \
137 tuner_err("i2c input error: rc = %d (should be %d)\n", \
138 _rc, (int)isize); \
Devin Heitmueller70ca3c42010-01-19 01:38:45 -0300139 if (priv->ctrl.msleep) \
140 msleep(priv->ctrl.msleep); \
Chris Pascoe7d58d112007-11-19 04:31:58 -0300141 _rc; \
142})
143
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300144#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300145 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300146 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300147 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300148 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300149 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300150 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
Devin Heitmueller70ca3c42010-01-19 01:38:45 -0300151 } else if (priv->ctrl.msleep) \
Mauro Carvalho Chehabe5cc2bf2008-01-08 11:26:59 -0300152 msleep(priv->ctrl.msleep); \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300153 _rc; \
154})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300155
Devin Heitmueller83244022008-04-17 21:41:16 -0300156static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300157{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300158 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300159 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300160
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300161 tuner_dbg("%s %04x called\n", __func__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300162
Chris Pascoe7d58d112007-11-19 04:31:58 -0300163 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300164 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300165
Chris Pascoe7d58d112007-11-19 04:31:58 -0300166 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
167 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300168
Chris Pascoe7d58d112007-11-19 04:31:58 -0300169 *val = (ibuf[1]) | (ibuf[0] << 8);
170 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300171}
172
Chris Pascoee0262682007-12-02 06:30:50 -0300173#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Adrian Bunk29bec0b2008-04-22 14:41:45 -0300174static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300175{
176 if (type & BASE)
177 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300178 if (type & INIT1)
179 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300180 if (type & F8MHZ)
181 printk("F8MHZ ");
182 if (type & MTS)
183 printk("MTS ");
184 if (type & D2620)
185 printk("D2620 ");
186 if (type & D2633)
187 printk("D2633 ");
188 if (type & DTV6)
189 printk("DTV6 ");
190 if (type & QAM)
191 printk("QAM ");
192 if (type & DTV7)
193 printk("DTV7 ");
194 if (type & DTV78)
195 printk("DTV78 ");
196 if (type & DTV8)
197 printk("DTV8 ");
198 if (type & FM)
199 printk("FM ");
200 if (type & INPUT1)
201 printk("INPUT1 ");
202 if (type & LCD)
203 printk("LCD ");
204 if (type & NOGD)
205 printk("NOGD ");
206 if (type & MONO)
207 printk("MONO ");
208 if (type & ATSC)
209 printk("ATSC ");
210 if (type & IF)
211 printk("IF ");
212 if (type & LG60)
213 printk("LG60 ");
214 if (type & ATI638)
215 printk("ATI638 ");
216 if (type & OREN538)
217 printk("OREN538 ");
218 if (type & OREN36)
219 printk("OREN36 ");
220 if (type & TOYOTA388)
221 printk("TOYOTA388 ");
222 if (type & TOYOTA794)
223 printk("TOYOTA794 ");
224 if (type & DIBCOM52)
225 printk("DIBCOM52 ");
226 if (type & ZARLINK456)
227 printk("ZARLINK456 ");
228 if (type & CHINA)
229 printk("CHINA ");
230 if (type & F6MHZ)
231 printk("F6MHZ ");
232 if (type & INPUT2)
233 printk("INPUT2 ");
234 if (type & SCODE)
235 printk("SCODE ");
Chris Pascoee0262682007-12-02 06:30:50 -0300236 if (type & HAS_IF)
237 printk("HAS_IF_%d ", int_freq);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300238}
239
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300240static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300241{
Chris Pascoee155d902007-11-19 04:16:47 -0300242 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300243 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300244 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300245 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300246 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300247 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300248 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300249 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300250 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300251 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300252 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300253 return V4L2_STD_NICAM_B;
254
255 return 0;
256}
257
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300258static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300259{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300260 int i;
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -0300261 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300262
263 if (!priv->firm)
264 return;
265
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300266 for (i = 0; i < priv->firm_size; i++)
267 kfree(priv->firm[i].ptr);
268
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300269 kfree(priv->firm);
270
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300271 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300272 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300273
274 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300275}
276
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300277static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300278{
279 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300280 const struct firmware *fw = NULL;
David Woodhousec63e87e2008-05-24 00:13:34 +0100281 const unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300282 int rc = 0;
283 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300284 char name[33];
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300285 char *fname;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300286
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300287 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300288
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300289 if (!firmware_name[0])
290 fname = priv->ctrl.fname;
291 else
292 fname = firmware_name;
293
294 tuner_dbg("Reading firmware %s\n", fname);
Jean Delvaree9785252009-04-26 05:43:59 -0300295 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300296 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300297 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300298 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300299 fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300300 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300301 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300302 rc, fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300303
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300304 return rc;
305 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300306 p = fw->data;
307 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300308
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300309 if (fw->size < sizeof(name) - 1 + 2 + 2) {
310 tuner_err("Error: firmware file %s has invalid size!\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300311 fname);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300312 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300313 }
314
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300315 memcpy(name, p, sizeof(name) - 1);
316 name[sizeof(name) - 1] = 0;
317 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300318
Al Viro84a9f332008-06-22 14:19:29 -0300319 priv->firm_version = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300320 p += 2;
321
Al Viro84a9f332008-06-22 14:19:29 -0300322 n_array = get_unaligned_le16(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300323 p += 2;
324
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300325 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
Mauro Carvalho Chehab5c913c02008-04-22 14:46:24 -0300326 n_array, fname, name,
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300327 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300328
Thomas Meyer1b7acf02011-11-29 17:08:00 -0300329 priv->firm = kcalloc(n_array, sizeof(*priv->firm), GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300330 if (priv->firm == NULL) {
331 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300332 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300333 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300334 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300335 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300336
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300337 n = -1;
338 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300339 __u32 type, size;
340 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300341 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300342
343 n++;
344 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300345 tuner_err("More firmware images in file than "
346 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300347 goto corrupt;
348 }
349
350 /* Checks if there's enough bytes to read */
Al Viro84a9f332008-06-22 14:19:29 -0300351 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
352 goto header;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300353
Al Viro84a9f332008-06-22 14:19:29 -0300354 type = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300355 p += sizeof(type);
356
Al Viro84a9f332008-06-22 14:19:29 -0300357 id = get_unaligned_le64(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300358 p += sizeof(id);
359
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300360 if (type & HAS_IF) {
Al Viro84a9f332008-06-22 14:19:29 -0300361 int_freq = get_unaligned_le16(p);
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300362 p += sizeof(int_freq);
Al Viro84a9f332008-06-22 14:19:29 -0300363 if (endp - p < sizeof(size))
364 goto header;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300365 }
366
Al Viro84a9f332008-06-22 14:19:29 -0300367 size = get_unaligned_le32(p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300368 p += sizeof(size);
369
Al Viro84a9f332008-06-22 14:19:29 -0300370 if (!size || size > endp - p) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300371 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300372 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300373 printk("(%x), id %llx is corrupted "
374 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300375 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300376 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300377 goto corrupt;
378 }
379
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300380 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300381 if (priv->firm[n].ptr == NULL) {
382 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300383 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300384 goto err;
385 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300386 tuner_dbg("Reading firmware type ");
387 if (debug) {
Chris Pascoee0262682007-12-02 06:30:50 -0300388 dump_firm_type_and_int_freq(type, int_freq);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300389 printk("(%x), id %llx, size=%d.\n",
Chris Pascoee0262682007-12-02 06:30:50 -0300390 type, (unsigned long long)id, size);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300391 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300392
393 memcpy(priv->firm[n].ptr, p, size);
394 priv->firm[n].type = type;
395 priv->firm[n].id = id;
396 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300397 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300398
399 p += size;
400 }
401
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300402 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300403 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300404 goto corrupt;
405 }
406
407 goto done;
408
Al Viro84a9f332008-06-22 14:19:29 -0300409header:
410 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300411corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300412 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300413 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300414
415err:
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300416 tuner_info("Releasing partially loaded firmware file.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300417 free_firmware(priv);
418
419done:
420 release_firmware(fw);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300421 if (rc == 0)
422 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300423
424 return rc;
425}
426
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300427static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
428 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300429{
430 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoeb1535292007-11-19 10:04:06 -0300431 int i, best_i = -1, best_nr_matches = 0;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300432 unsigned int type_mask = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300433
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300434 tuner_dbg("%s called, want type=", __func__);
Chris Pascoeb1535292007-11-19 10:04:06 -0300435 if (debug) {
436 dump_firm_type(type);
437 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
438 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300439
440 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300441 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300442 return -EINVAL;
443 }
444
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300445 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300446 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300447
Chris Pascoee0f0b372007-11-19 11:22:03 -0300448 if (type & BASE)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300449 type_mask = BASE_TYPES;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300450 else if (type & SCODE) {
Chris Pascoee0f0b372007-11-19 11:22:03 -0300451 type &= SCODE_TYPES;
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300452 type_mask = SCODE_TYPES & ~HAS_IF;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300453 } else if (type & DTV_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300454 type_mask = DTV_TYPES;
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300455 else if (type & STD_SPECIFIC_TYPES)
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300456 type_mask = STD_SPECIFIC_TYPES;
457
458 type &= type_mask;
459
Harvey Harrison8367fe22008-04-25 01:28:10 -0300460 if (!(type & SCODE))
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300461 type_mask = ~0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300462
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300463 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300464 for (i = 0; i < priv->firm_size; i++) {
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300465 if ((type == (priv->firm[i].type & type_mask)) &&
Chris Pascoeef207fe2007-12-02 09:30:55 -0300466 (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300467 goto found;
468 }
469
470 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300471 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300472 v4l2_std_id match_mask;
473 int nr_matches;
474
Mauro Carvalho Chehab33e53162008-04-21 06:58:48 -0300475 if (type != (priv->firm[i].type & type_mask))
Chris Pascoeb1535292007-11-19 10:04:06 -0300476 continue;
477
478 match_mask = *id & priv->firm[i].id;
479 if (!match_mask)
480 continue;
481
482 if ((*id & match_mask) == *id)
483 goto found; /* Supports all the requested standards */
484
485 nr_matches = hweight64(match_mask);
486 if (nr_matches > best_nr_matches) {
487 best_nr_matches = nr_matches;
488 best_i = i;
489 }
490 }
491
492 if (best_nr_matches > 0) {
493 tuner_dbg("Selecting best matching firmware (%d bits) for "
494 "type=", best_nr_matches);
495 dump_firm_type(type);
496 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
497 i = best_i;
498 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300499 }
500
501 /*FIXME: Would make sense to seek for type "hint" match ? */
502
Chris Pascoeb1535292007-11-19 10:04:06 -0300503 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300504 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300505
506found:
507 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300508
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300509ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300510 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300511 if (debug) {
512 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300513 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300514 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300515 return i;
516}
517
Michael Krufkyd7cba042008-09-12 13:31:45 -0300518static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
519{
520 struct xc2028_data *priv = fe->tuner_priv;
521
522 /* analog side (tuner-core) uses i2c_adap->algo_data.
523 * digital side is not guaranteed to have algo_data defined.
524 *
525 * digital side will always have fe->dvb defined.
526 * analog side (tuner-core) doesn't (yet) define fe->dvb.
527 */
528
529 return (!fe->callback) ? -EINVAL :
530 fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
531 fe->dvb->priv : priv->i2c_props.adap->algo_data,
532 DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
533}
534
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300535static int load_firmware(struct dvb_frontend *fe, unsigned int type,
536 v4l2_std_id *id)
537{
538 struct xc2028_data *priv = fe->tuner_priv;
539 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300540 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300541
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300542 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300543
544 pos = seek_firmware(fe, type, id);
545 if (pos < 0)
546 return pos;
547
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300548 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300549 dump_firm_type(priv->firm[pos].type);
550 printk("(%x), id %016llx.\n", priv->firm[pos].type,
551 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300552
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300553 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300554 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300555
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300556 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300557 __u16 size;
558
559 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300560 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300561 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300562 return -EINVAL;
563 }
564
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300565 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300566 p += sizeof(size);
567
568 if (size == 0xffff)
569 return 0;
570
571 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300572 /* Special callback command received */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300573 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300574 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300575 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300576 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300577 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300578 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300579 continue;
580 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300581 if (size >= 0xff00) {
582 switch (size) {
583 case 0xff00:
Michael Krufkyd7cba042008-09-12 13:31:45 -0300584 rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
Michel Ludwig5403bba2007-11-16 07:49:49 -0300585 if (rc < 0) {
586 tuner_err("Error at RESET code %d\n",
587 (*p) & 0x7f);
588 return -EINVAL;
589 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300590 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300591 default:
592 tuner_info("Invalid RESET code %d\n",
593 size & 0x7f);
594 return -EINVAL;
595
596 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300597 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300598 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300599
600 /* Checks for a sleep command */
601 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300602 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300603 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300604 }
605
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300606 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300607 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300608 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300609 return -EINVAL;
610 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300611
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300612 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300613 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300614 size--;
615
616 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300617 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300618 int len = (size < priv->ctrl.max_len - 1) ?
619 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300620
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300621 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300622
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300623 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300624 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300625 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300626 return -EINVAL;
627 }
628
629 p += len;
630 size -= len;
631 }
Thierry Reding4d37ece2011-08-04 04:13:59 -0300632
633 /* silently fail if the frontend doesn't support I2C flush */
634 rc = do_tuner_callback(fe, XC2028_I2C_FLUSH, 0);
635 if ((rc < 0) && (rc != -EINVAL)) {
636 tuner_err("error executing flush: %d\n", rc);
637 return rc;
638 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300639 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300640 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300641}
642
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300643static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300644 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300645{
646 struct xc2028_data *priv = fe->tuner_priv;
647 int pos, rc;
648 unsigned char *p;
649
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300650 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300651
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300652 if (!int_freq) {
653 pos = seek_firmware(fe, type, id);
654 if (pos < 0)
655 return pos;
656 } else {
657 for (pos = 0; pos < priv->firm_size; pos++) {
658 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300659 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300660 break;
661 }
662 if (pos == priv->firm_size)
663 return -ENOENT;
664 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300665
666 p = priv->firm[pos].ptr;
667
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300668 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300669 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
670 return -EINVAL;
671 p += 12 * scode;
672 } else {
673 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
674 * has a 2-byte size header in the firmware format. */
675 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
676 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
677 return -EINVAL;
678 p += 14 * scode + 2;
679 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300680
Chris Pascoed7b22c52007-11-19 10:12:45 -0300681 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300682 dump_firm_type_and_int_freq(priv->firm[pos].type,
683 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300684 printk("(%x), id %016llx.\n", priv->firm[pos].type,
685 (unsigned long long)*id);
686
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300687 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300688 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
689 else
690 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
691 if (rc < 0)
692 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300693
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300694 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300695 if (rc < 0)
696 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300697
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300698 rc = send_seq(priv, {0x00, 0x8c});
699 if (rc < 0)
700 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300701
702 return 0;
703}
704
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300705static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300706 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300707{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300708 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300709 struct firmware_properties new_fw;
Alina Friedrichsenb8bc77d2011-01-23 12:27:05 -0300710 int rc = 0, retry_count = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300711 u16 version, hwmodel;
712 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300713
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300714 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300715
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300716 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300717 if (!priv->ctrl.fname) {
718 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300719 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300720 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300721
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300722 rc = load_all_firmwares(fe);
723 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300724 return rc;
725 }
726
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300727 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300728 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300729
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300730retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300731 new_fw.type = type;
732 new_fw.id = std;
733 new_fw.std_req = std;
734 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
735 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300736 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300737
738 tuner_dbg("checking firmware, user requested type=");
739 if (debug) {
740 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300741 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300742 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300743 if (!int_freq) {
744 printk("scode_tbl ");
745 dump_firm_type(priv->ctrl.scode_table);
746 printk("(%x), ", priv->ctrl.scode_table);
747 } else
748 printk("int_freq %d, ", new_fw.int_freq);
749 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300750 }
751
752 /* No need to reload base firmware if it matches */
753 if (((BASE | new_fw.type) & BASE_TYPES) ==
754 (priv->cur_fw.type & BASE_TYPES)) {
755 tuner_dbg("BASE firmware not changed.\n");
756 goto skip_base;
757 }
758
759 /* Updating BASE - forget about all currently loaded firmware */
760 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
761
762 /* Reset is needed before loading firmware */
Michael Krufkyd7cba042008-09-12 13:31:45 -0300763 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300764 if (rc < 0)
765 goto fail;
766
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300767 /* BASE firmwares are all std0 */
768 std0 = 0;
769 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300770 if (rc < 0) {
771 tuner_err("Error %d while loading base firmware\n",
772 rc);
773 goto fail;
774 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300775
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300776 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300777 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300778
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300779 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300780 if (rc == -ENOENT)
781 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
782 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300783 if (rc < 0 && rc != -ENOENT) {
784 tuner_err("Error %d while loading init1 firmware\n",
785 rc);
786 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300787 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300788
Chris Pascoee0f0b372007-11-19 11:22:03 -0300789skip_base:
790 /*
791 * No need to reload standard specific firmware if base firmware
792 * was not reloaded and requested video standards have not changed.
793 */
794 if (priv->cur_fw.type == (BASE | new_fw.type) &&
795 priv->cur_fw.std_req == std) {
796 tuner_dbg("Std-specific firmware already loaded.\n");
797 goto skip_std_specific;
798 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300799
Chris Pascoee0f0b372007-11-19 11:22:03 -0300800 /* Reloading std-specific firmware forces a SCODE update */
801 priv->cur_fw.scode_table = 0;
802
Chris Pascoee0f0b372007-11-19 11:22:03 -0300803 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300804 if (rc == -ENOENT)
805 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
806
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300807 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300808 goto fail;
809
810skip_std_specific:
811 if (priv->cur_fw.scode_table == new_fw.scode_table &&
812 priv->cur_fw.scode_nr == new_fw.scode_nr) {
813 tuner_dbg("SCODE firmware already loaded.\n");
814 goto check_device;
815 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300816
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300817 if (new_fw.type & FM)
818 goto check_device;
819
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300820 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300821 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300822
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300823 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
824 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300825
Chris Pascoee0f0b372007-11-19 11:22:03 -0300826check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300827 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
828 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
829 tuner_err("Unable to read tuner registers.\n");
830 goto fail;
831 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300832
Devin Heitmuellerb37f2d62008-04-21 07:02:09 -0300833 tuner_dbg("Device is Xceive %d version %d.%d, "
834 "firmware version %d.%d\n",
835 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
836 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300837
Mauro Carvalho Chehab0fb84ce2008-12-02 08:30:16 -0300838
839 if (priv->ctrl.read_not_reliable)
840 goto read_not_reliable;
841
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300842 /* Check firmware version against what we downloaded. */
843 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
Mauro Carvalho Chehab2d5024a2009-09-14 10:23:20 -0300844 if (!priv->ctrl.read_not_reliable) {
845 tuner_err("Incorrect readback of firmware version.\n");
846 goto fail;
847 } else {
848 tuner_err("Returned an incorrect version. However, "
849 "read is not reliable enough. Ignoring it.\n");
850 hwmodel = 3028;
851 }
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300852 }
853
854 /* Check that the tuner hardware model remains consistent over time. */
855 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
856 priv->hwmodel = hwmodel;
857 priv->hwvers = version & 0xff00;
858 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
859 priv->hwvers != (version & 0xff00)) {
860 tuner_err("Read invalid device hardware information - tuner "
861 "hung?\n");
862 goto fail;
863 }
864
Mauro Carvalho Chehab0fb84ce2008-12-02 08:30:16 -0300865read_not_reliable:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300866 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
867
868 /*
869 * By setting BASE in cur_fw.type only after successfully loading all
870 * firmwares, we can:
871 * 1. Identify that BASE firmware with type=0 has been loaded;
872 * 2. Tell whether BASE firmware was just changed the next time through.
873 */
874 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300875
876 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300877
878fail:
879 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Alina Friedrichsenb8bc77d2011-01-23 12:27:05 -0300880 if (retry_count < 8) {
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300881 msleep(50);
Alina Friedrichsenb8bc77d2011-01-23 12:27:05 -0300882 retry_count++;
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300883 tuner_dbg("Retrying firmware load\n");
884 goto retry;
885 }
886
Chris Pascoee0f0b372007-11-19 11:22:03 -0300887 if (rc == -ENOENT)
888 rc = -EINVAL;
889 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300890}
891
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300892static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300893{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300894 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300895 u16 frq_lock, signal = 0;
896 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300897
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300898 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300899
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300900 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300901
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300902 /* Sync Lock Indicator */
Miroslav Slugen304bce42011-12-11 20:19:34 -0300903 rc = xc2028_get_reg(priv, XREG_LOCK, &frq_lock);
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300904 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300905 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300906
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300907 /* Frequency is locked */
908 if (frq_lock == 1)
Mauro Carvalho Chehab884b0512011-11-29 13:33:52 -0300909 signal = 1 << 11;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300910
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300911 /* Get SNR of the video signal */
Miroslav Slugen304bce42011-12-11 20:19:34 -0300912 rc = xc2028_get_reg(priv, XREG_SNR, &signal);
Chris Pascoe7d58d112007-11-19 04:31:58 -0300913 if (rc < 0)
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300914 goto ret;
915
916 /* Use both frq_lock and signal to generate the result */
917 signal = signal || ((signal & 0x07) << 12);
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300918
919ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300920 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300921
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300922 *strength = signal;
923
Mauro Carvalho Chehabb0166ab2008-04-24 11:19:55 -0300924 tuner_dbg("signal strength is %d\n", signal);
925
Chris Pascoe7d58d112007-11-19 04:31:58 -0300926 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300927}
928
929#define DIV 15625
930
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300931static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Hans Verkuilaa40d192011-03-06 09:24:32 -0300932 enum v4l2_tuner_type new_type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300933 unsigned int type,
934 v4l2_std_id std,
935 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300936{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300937 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300938 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300939 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300940 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300941
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300942 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300943
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300944 mutex_lock(&priv->lock);
945
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300946 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300947
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300948 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300949 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300950
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300951 /* On some cases xc2028 can disable video output, if
952 * very weak signals are received. By sending a soft
953 * reset, this is re-enabled. So, it is better to always
954 * send a soft reset before changing channels, to be sure
955 * that xc2028 will be in a safe state.
956 * Maybe this might also be needed for DTV.
957 */
Mauro Carvalho Chehabfd34cb02011-08-31 15:12:45 -0300958 switch (new_type) {
959 case V4L2_TUNER_ANALOG_TV:
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300960 rc = send_seq(priv, {0x00, 0x00});
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300961
Mauro Carvalho Chehabfd34cb02011-08-31 15:12:45 -0300962 /* Analog mode requires offset = 0 */
963 break;
964 case V4L2_TUNER_RADIO:
965 /* Radio mode requires offset = 0 */
966 break;
967 case V4L2_TUNER_DIGITAL_TV:
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200968 /*
969 * Digital modes require an offset to adjust to the
970 * proper frequency. The offset depends on what
971 * firmware version is used.
972 */
973
974 /*
975 * Adjust to the center frequency. This is calculated by the
976 * formula: offset = 1.25MHz - BW/2
977 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
978 * further adjustment to get the frequency center on VHF
979 */
Gianluca Gennari98ab8552012-01-04 15:17:19 -0300980
981 /*
982 * The firmware DTV78 used to work fine in UHF band (8 MHz
983 * bandwidth) but not at all in VHF band (7 MHz bandwidth).
984 * The real problem was connected to the formula used to
985 * calculate the center frequency offset in VHF band.
986 * In fact, removing the 500KHz adjustment fixed the problem.
987 * This is coherent to what was implemented for the DTV7
988 * firmware.
989 * In the end, now the center frequency is the same for all 3
990 * firmwares (DTV7, DTV8, DTV78) and doesn't depend on channel
991 * bandwidth.
992 */
993
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300994 if (priv->cur_fw.type & DTV6)
995 offset = 1750000;
Gianluca Gennari98ab8552012-01-04 15:17:19 -0300996 else /* DTV7 or DTV8 or DTV78 */
Mauro Carvalho Chehab0a863972009-06-01 12:18:10 -0300997 offset = 2750000;
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -0200998
999 /*
1000 * xc3028 additional "magic"
1001 * Depending on the firmware version, it needs some adjustments
1002 * to properly centralize the frequency. This seems to be
1003 * needed to compensate the SCODE table adjustments made by
1004 * newer firmwares
1005 */
1006
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001007 /*
1008 * The proper adjustment would be to do it at s-code table.
1009 * However, this didn't work, as reported by
1010 * Robert Lowery <rglowery@exemail.com.au>
1011 */
1012
Gianluca Gennari98ab8552012-01-04 15:17:19 -03001013#if 0
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001014 /*
1015 * Still need tests for XC3028L (firmware 3.2 or upper)
1016 * So, for now, let's just comment the per-firmware
1017 * version of this change. Reports with xc3028l working
1018 * with and without the lines bellow are welcome
1019 */
1020
1021 if (priv->firm_version < 0x0302) {
1022 if (priv->cur_fw.type & DTV7)
1023 offset += 500000;
1024 } else {
1025 if (priv->cur_fw.type & DTV7)
1026 offset -= 300000;
1027 else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
1028 offset += 200000;
1029 }
1030#endif
Chris Pascoea44f1c42007-11-19 06:35:26 -03001031 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -03001032
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001033 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -03001034
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001035 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001036 if (priv->firm_version < 0x0202)
Miroslav Slugen304bce42011-12-11 20:19:34 -03001037 rc = send_seq(priv, {0x00, XREG_RF_FREQ, 0x00, 0x00});
Chris Pascoe47cc5b72007-11-19 04:14:23 -03001038 else
Miroslav Slugen304bce42011-12-11 20:19:34 -03001039 rc = send_seq(priv, {0x80, XREG_RF_FREQ, 0x00, 0x00});
Chris Pascoe47cc5b72007-11-19 04:14:23 -03001040 if (rc < 0)
1041 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001042
Mauro Carvalho Chehab1fe873692008-04-22 14:45:20 -03001043 /* Return code shouldn't be checked.
1044 The reset CLK is needed only with tm6000.
1045 Driver should work fine even if this fails.
1046 */
Devin Heitmueller70ca3c42010-01-19 01:38:45 -03001047 if (priv->ctrl.msleep)
1048 msleep(priv->ctrl.msleep);
Michael Krufkyd7cba042008-09-12 13:31:45 -03001049 do_tuner_callback(fe, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001050
1051 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -03001052
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001053 buf[0] = 0xff & (div >> 24);
1054 buf[1] = 0xff & (div >> 16);
1055 buf[2] = 0xff & (div >> 8);
1056 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001057
Chris Pascoe47cc5b72007-11-19 04:14:23 -03001058 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001059 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001060 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001061 msleep(100);
1062
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001063 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001064
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -03001065 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
1066 buf[0], buf[1], buf[2], buf[3],
1067 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001068
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001069 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -03001070
1071ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001072 mutex_unlock(&priv->lock);
1073
1074 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001075}
1076
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001077static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001078 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -03001079{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001080 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001081 unsigned int type=0;
1082
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001083 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -03001084
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001085 if (p->mode == V4L2_TUNER_RADIO) {
1086 type |= FM;
1087 if (priv->ctrl.input1)
1088 type |= INPUT1;
1089 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab437f5fa2011-02-21 21:03:59 -03001090 V4L2_TUNER_RADIO, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -03001091 }
1092
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -03001093 /* if std is not defined, choose one */
1094 if (!p->std)
1095 p->std = V4L2_STD_MN;
1096
1097 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001098 if (!(p->std & V4L2_STD_MN))
1099 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -03001100
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001101 /* Add audio hack to std mask */
1102 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001103
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001104 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab437f5fa2011-02-21 21:03:59 -03001105 V4L2_TUNER_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -03001106}
1107
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03001108static int xc2028_set_params(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001109{
Mauro Carvalho Chehab506cd712011-12-21 08:53:22 -03001110 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1111 u32 delsys = c->delivery_system;
1112 u32 bw = c->bandwidth_hz;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001113 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001114 unsigned int type=0;
Chris Pascoead35ce92007-12-02 06:36:42 -03001115 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -03001116
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001117 tuner_dbg("%s called\n", __func__);
Michel Ludwig701672e2007-07-18 10:29:10 -03001118
Mauro Carvalho Chehab506cd712011-12-21 08:53:22 -03001119 switch (delsys) {
1120 case SYS_DVBT:
1121 case SYS_DVBT2:
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001122 /*
1123 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1124 * Both seem to require QAM firmware for OFDM decoding
1125 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1126 */
Mauro Carvalho Chehab506cd712011-12-21 08:53:22 -03001127 if (bw <= 6000000)
Mauro Carvalho Chehaba1014d72009-06-01 11:46:08 -03001128 type |= QAM;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001129
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001130 switch (priv->ctrl.type) {
1131 case XC2028_D2633:
1132 type |= D2633;
1133 break;
1134 case XC2028_D2620:
1135 type |= D2620;
1136 break;
1137 case XC2028_AUTO:
1138 default:
1139 /* Zarlink seems to need D2633 */
1140 if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
1141 type |= D2633;
1142 else
1143 type |= D2620;
1144 }
Mauro Carvalho Chehab506cd712011-12-21 08:53:22 -03001145 break;
1146 case SYS_ATSC:
1147 /* The only ATSC firmware (at least on v2.7) is D2633 */
1148 type |= ATSC | D2633;
1149 break;
1150 /* DVB-S and pure QAM (FE_QAM) are not supported */
1151 default:
1152 return -EINVAL;
1153 }
1154
1155 if (bw <= 6000000) {
1156 type |= DTV6;
1157 priv->ctrl.vhfbw7 = 0;
1158 priv->ctrl.uhfbw8 = 0;
1159 } else if (bw <= 7000000) {
1160 if (c->frequency < 470000000)
1161 priv->ctrl.vhfbw7 = 1;
1162 else
1163 priv->ctrl.uhfbw8 = 0;
1164 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1165 type |= F8MHZ;
1166 } else {
1167 if (c->frequency < 470000000)
1168 priv->ctrl.vhfbw7 = 0;
1169 else
1170 priv->ctrl.uhfbw8 = 1;
1171 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1172 type |= F8MHZ;
Mauro Carvalho Chehab0975fc62008-09-28 02:24:44 -03001173 }
1174
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001175 /* All S-code tables need a 200kHz shift */
Andy Walls6e707b42009-06-11 07:57:50 -03001176 if (priv->ctrl.demod) {
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001177 demod = priv->ctrl.demod;
1178
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001179 /*
1180 * Newer firmwares require a 200 kHz offset only for ATSC
1181 */
1182 if (type == ATSC || priv->firm_version < 0x0302)
Mauro Carvalho Chehab7d350282010-02-19 20:08:06 -02001183 demod += 200;
Andy Walls6e707b42009-06-11 07:57:50 -03001184 /*
1185 * The DTV7 S-code table needs a 700 kHz shift.
Andy Walls6e707b42009-06-11 07:57:50 -03001186 *
1187 * DTV7 is only used in Australia. Germany or Italy may also
1188 * use this firmware after initialization, but a tune to a UHF
1189 * channel should then cause DTV78 to be used.
Mauro Carvalho Chehab7f2199c2010-02-19 02:45:00 -02001190 *
1191 * Unfortunately, on real-field tests, the s-code offset
1192 * didn't work as expected, as reported by
1193 * Robert Lowery <rglowery@exemail.com.au>
Andy Walls6e707b42009-06-11 07:57:50 -03001194 */
Andy Walls6e707b42009-06-11 07:57:50 -03001195 }
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001196
Mauro Carvalho Chehab506cd712011-12-21 08:53:22 -03001197 return generic_set_freq(fe, c->frequency,
Mauro Carvalho Chehab437f5fa2011-02-21 21:03:59 -03001198 V4L2_TUNER_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001199}
1200
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001201static int xc2028_sleep(struct dvb_frontend *fe)
1202{
1203 struct xc2028_data *priv = fe->tuner_priv;
1204 int rc = 0;
1205
Devin Heitmueller93b99922009-08-03 22:52:59 -03001206 /* Avoid firmware reload on slow devices or if PM disabled */
1207 if (no_poweroff || priv->ctrl.disable_power_mgmt)
Mauro Carvalho Chehab10f201a2008-12-05 10:49:53 -03001208 return 0;
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001209
1210 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
Mauro Carvalho Chehabe278e742008-12-18 06:00:25 -03001211 if (debug > 1) {
1212 tuner_dbg("Printing sleep stack trace:\n");
1213 dump_stack();
1214 }
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001215
1216 mutex_lock(&priv->lock);
1217
1218 if (priv->firm_version < 0x0202)
Miroslav Slugen304bce42011-12-11 20:19:34 -03001219 rc = send_seq(priv, {0x00, XREG_POWER_DOWN, 0x00, 0x00});
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001220 else
Miroslav Slugen304bce42011-12-11 20:19:34 -03001221 rc = send_seq(priv, {0x80, XREG_POWER_DOWN, 0x00, 0x00});
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001222
1223 priv->cur_fw.type = 0; /* need firmware reload */
1224
1225 mutex_unlock(&priv->lock);
1226
1227 return rc;
1228}
Chris Pascoe45819c32007-11-19 11:41:20 -03001229
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001230static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001231{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001232 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001233
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001234 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001235
Chris Pascoeaa501be2007-11-19 04:45:38 -03001236 mutex_lock(&xc2028_list_mutex);
1237
Michael Krufkyc663d032008-04-18 21:22:50 -03001238 /* only perform final cleanup if this is the last instance */
1239 if (hybrid_tuner_report_instance_count(priv) == 1) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001240 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001241 free_firmware(priv);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001242 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001243
Michael Krufkyc663d032008-04-18 21:22:50 -03001244 if (priv)
1245 hybrid_tuner_release_state(priv);
1246
Chris Pascoeaa501be2007-11-19 04:45:38 -03001247 mutex_unlock(&xc2028_list_mutex);
1248
Michael Krufkyc663d032008-04-18 21:22:50 -03001249 fe->tuner_priv = NULL;
1250
Michel Ludwig701672e2007-07-18 10:29:10 -03001251 return 0;
1252}
1253
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001254static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001255{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001256 struct xc2028_data *priv = fe->tuner_priv;
1257
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001258 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001259
1260 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001261
1262 return 0;
1263}
1264
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001265static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001266{
1267 struct xc2028_data *priv = fe->tuner_priv;
1268 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001269 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001270
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001271 tuner_dbg("%s called\n", __func__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001272
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001273 mutex_lock(&priv->lock);
1274
Chris Pascoe0a196b62007-11-19 09:29:59 -03001275 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001276 if (priv->ctrl.max_len < 9)
1277 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001278
1279 if (p->fname) {
Mauro Carvalho Chehab92b75ab2008-04-17 21:40:53 -03001280 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1281 kfree(priv->ctrl.fname);
1282 free_firmware(priv);
1283 }
1284
Chris Pascoe0a196b62007-11-19 09:29:59 -03001285 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1286 if (priv->ctrl.fname == NULL)
1287 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001288 }
1289
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001290 mutex_unlock(&priv->lock);
1291
Chris Pascoe0a196b62007-11-19 09:29:59 -03001292 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001293}
1294
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001295static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001296 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001297 .name = "Xceive XC3028",
1298 .frequency_min = 42000000,
1299 .frequency_max = 864000000,
1300 .frequency_step = 50000,
1301 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001302
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001303 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001304 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001305 .release = xc2028_dvb_release,
1306 .get_frequency = xc2028_get_frequency,
1307 .get_rf_strength = xc2028_signal,
1308 .set_params = xc2028_set_params,
Mauro Carvalho Chehab74a89b22008-12-05 10:31:16 -03001309 .sleep = xc2028_sleep,
Michel Ludwig701672e2007-07-18 10:29:10 -03001310};
1311
Michael Krufky7972f982007-12-21 16:12:09 -03001312struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1313 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001314{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001315 struct xc2028_data *priv;
Michael Krufkyc663d032008-04-18 21:22:50 -03001316 int instance;
Michel Ludwig701672e2007-07-18 10:29:10 -03001317
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001318 if (debug)
Michael Krufky27566652008-04-22 14:41:53 -03001319 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001320
Mauro Carvalho Chehabb412ba72008-04-22 14:46:11 -03001321 if (NULL == cfg)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001322 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001323
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001324 if (!fe) {
Michael Krufky27566652008-04-22 14:41:53 -03001325 printk(KERN_ERR "xc2028: No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001326 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001327 }
1328
Chris Pascoeaa501be2007-11-19 04:45:38 -03001329 mutex_lock(&xc2028_list_mutex);
1330
Michael Krufkyc663d032008-04-18 21:22:50 -03001331 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1332 hybrid_tuner_instance_list,
1333 cfg->i2c_adap, cfg->i2c_addr,
1334 "xc2028");
1335 switch (instance) {
1336 case 0:
1337 /* memory allocation failure */
1338 goto fail;
1339 break;
1340 case 1:
1341 /* new tuner instance */
Chris Pascoe0a196b62007-11-19 09:29:59 -03001342 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001343
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001344 mutex_init(&priv->lock);
1345
Michael Krufkyc663d032008-04-18 21:22:50 -03001346 fe->tuner_priv = priv;
1347 break;
1348 case 2:
1349 /* existing tuner instance */
1350 fe->tuner_priv = priv;
1351 break;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001352 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001353
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001354 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001355 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001356
1357 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001358
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001359 if (cfg->ctrl)
1360 xc2028_set_config(fe, cfg->ctrl);
1361
Chris Pascoeaa501be2007-11-19 04:45:38 -03001362 mutex_unlock(&xc2028_list_mutex);
1363
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001364 return fe;
Michael Krufkyc663d032008-04-18 21:22:50 -03001365fail:
1366 mutex_unlock(&xc2028_list_mutex);
1367
1368 xc2028_dvb_release(fe);
1369 return NULL;
Michel Ludwig701672e2007-07-18 10:29:10 -03001370}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001371
Michel Ludwig701672e2007-07-18 10:29:10 -03001372EXPORT_SYMBOL(xc2028_attach);
1373
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001374MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001375MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001376MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1377MODULE_LICENSE("GPL");