blob: 8f4fdf70f7eb3867984e0136009f7e59b4e888a4 [file] [log] [blame]
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001/* tuner-xc2028
2 *
3 * Copyright (c) 2007 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>
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030018#include "tuner-i2c.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030019#include "tuner-xc2028.h"
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030020#include "tuner-xc2028-types.h"
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030021
Michel Ludwig701672e2007-07-18 10:29:10 -030022#include <linux/dvb/frontend.h>
23#include "dvb_frontend.h"
24
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -030025
Hans Verkuil9dd659d2007-11-04 11:03:36 -030026#define PREFIX "xc2028"
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -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 Chehaba82200f2007-11-15 11:58:00 -030032static char audio_std[8];
33module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
34MODULE_PARM_DESC(audio_std,
35 "Audio standard. XC3028 audio decoder explicitly "
36 "needs to know what audio\n"
37 "standard is needed for some video standards with audio A2 or NICAM.\n"
38 "The valid values are:\n"
39 "A2\n"
40 "A2/A\n"
41 "A2/B\n"
42 "NICAM\n"
43 "NICAM/A\n"
44 "NICAM/B\n");
45
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030046static LIST_HEAD(xc2028_list);
Chris Pascoeaa501be2007-11-19 04:45:38 -030047static DEFINE_MUTEX(xc2028_list_mutex);
48
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030049/* struct for storing firmware table */
50struct firmware_description {
51 unsigned int type;
52 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030053 __u16 int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030054 unsigned char *ptr;
55 unsigned int size;
56};
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030057
Chris Pascoee0f0b372007-11-19 11:22:03 -030058struct firmware_properties {
59 unsigned int type;
60 v4l2_std_id id;
61 v4l2_std_id std_req;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -030062 __u16 int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -030063 unsigned int scode_table;
64 int scode_nr;
65};
66
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030067struct xc2028_data {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030068 struct list_head xc2028_list;
69 struct tuner_i2c_props i2c_props;
70 int (*tuner_callback) (void *dev,
71 int command, int arg);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030072 void *video_dev;
73 int count;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030074 __u32 frequency;
75
76 struct firmware_description *firm;
77 int firm_size;
Chris Pascoe06fd82d2007-11-19 06:06:08 -030078 __u16 firm_version;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030079
Chris Pascoe8bf799a2007-11-19 11:35:45 -030080 __u16 hwmodel;
81 __u16 hwvers;
82
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030083 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030084
Chris Pascoee0f0b372007-11-19 11:22:03 -030085 struct firmware_properties cur_fw;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030086
87 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030088};
89
Chris Pascoe47cc5b72007-11-19 04:14:23 -030090#define i2c_send(priv, buf, size) ({ \
91 int _rc; \
92 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
93 if (size != _rc) \
94 tuner_info("i2c output error: rc = %d (should be %d)\n",\
95 _rc, (int)size); \
96 _rc; \
97})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030098
Chris Pascoe47cc5b72007-11-19 04:14:23 -030099#define i2c_rcv(priv, buf, size) ({ \
100 int _rc; \
101 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
102 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300103 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300104 _rc, (int)size); \
105 _rc; \
106})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300107
Chris Pascoe7d58d112007-11-19 04:31:58 -0300108#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
109 int _rc; \
110 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
111 ibuf, isize); \
112 if (isize != _rc) \
113 tuner_err("i2c input error: rc = %d (should be %d)\n", \
114 _rc, (int)isize); \
115 _rc; \
116})
117
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300118#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300119 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300120 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300121 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300122 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300123 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300124 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
125 } else \
126 msleep(10); \
127 _rc; \
128})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300129
Chris Pascoe7d58d112007-11-19 04:31:58 -0300130static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300131{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300132 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300133 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300134
Chris Pascoe7d58d112007-11-19 04:31:58 -0300135 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300136
Chris Pascoe7d58d112007-11-19 04:31:58 -0300137 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300138 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300139
Chris Pascoe7d58d112007-11-19 04:31:58 -0300140 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
141 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300142
Chris Pascoe7d58d112007-11-19 04:31:58 -0300143 *val = (ibuf[1]) | (ibuf[0] << 8);
144 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300145}
146
Chris Pascoee0262682007-12-02 06:30:50 -0300147#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Adrian Bunk29bec0b2008-04-22 14:41:45 -0300148static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300149{
150 if (type & BASE)
151 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300152 if (type & INIT1)
153 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300154 if (type & F8MHZ)
155 printk("F8MHZ ");
156 if (type & MTS)
157 printk("MTS ");
158 if (type & D2620)
159 printk("D2620 ");
160 if (type & D2633)
161 printk("D2633 ");
162 if (type & DTV6)
163 printk("DTV6 ");
164 if (type & QAM)
165 printk("QAM ");
166 if (type & DTV7)
167 printk("DTV7 ");
168 if (type & DTV78)
169 printk("DTV78 ");
170 if (type & DTV8)
171 printk("DTV8 ");
172 if (type & FM)
173 printk("FM ");
174 if (type & INPUT1)
175 printk("INPUT1 ");
176 if (type & LCD)
177 printk("LCD ");
178 if (type & NOGD)
179 printk("NOGD ");
180 if (type & MONO)
181 printk("MONO ");
182 if (type & ATSC)
183 printk("ATSC ");
184 if (type & IF)
185 printk("IF ");
186 if (type & LG60)
187 printk("LG60 ");
188 if (type & ATI638)
189 printk("ATI638 ");
190 if (type & OREN538)
191 printk("OREN538 ");
192 if (type & OREN36)
193 printk("OREN36 ");
194 if (type & TOYOTA388)
195 printk("TOYOTA388 ");
196 if (type & TOYOTA794)
197 printk("TOYOTA794 ");
198 if (type & DIBCOM52)
199 printk("DIBCOM52 ");
200 if (type & ZARLINK456)
201 printk("ZARLINK456 ");
202 if (type & CHINA)
203 printk("CHINA ");
204 if (type & F6MHZ)
205 printk("F6MHZ ");
206 if (type & INPUT2)
207 printk("INPUT2 ");
208 if (type & SCODE)
209 printk("SCODE ");
Chris Pascoee0262682007-12-02 06:30:50 -0300210 if (type & HAS_IF)
211 printk("HAS_IF_%d ", int_freq);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300212}
213
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300214static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300215{
Chris Pascoee155d902007-11-19 04:16:47 -0300216 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300217 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300218 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300219 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300220 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300221 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300222 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300223 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300224 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300225 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300226 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300227 return V4L2_STD_NICAM_B;
228
229 return 0;
230}
231
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300232static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300233{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300234 int i;
235
236 if (!priv->firm)
237 return;
238
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300239 for (i = 0; i < priv->firm_size; i++)
240 kfree(priv->firm[i].ptr);
241
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300242 kfree(priv->firm);
243
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300244 priv->firm = NULL;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300245 priv->firm_size = 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300246
247 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300248}
249
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300250static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300251{
252 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300253 const struct firmware *fw = NULL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300254 unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300255 int rc = 0;
256 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300257 char name[33];
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300258
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300259 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300260
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300261 tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300262 rc = request_firmware(&fw, priv->ctrl.fname,
263 &priv->i2c_props.adap->dev);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300264 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300265 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300266 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300267 priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300268 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300269 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300270 rc, priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300271
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300272 return rc;
273 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300274 p = fw->data;
275 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300276
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300277 if (fw->size < sizeof(name) - 1 + 2 + 2) {
278 tuner_err("Error: firmware file %s has invalid size!\n",
279 priv->ctrl.fname);
280 goto corrupt;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300281 }
282
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300283 memcpy(name, p, sizeof(name) - 1);
284 name[sizeof(name) - 1] = 0;
285 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300286
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300287 priv->firm_version = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300288 p += 2;
289
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300290 n_array = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300291 p += 2;
292
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300293 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
294 n_array, priv->ctrl.fname, name,
295 priv->firm_version >> 8, priv->firm_version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300296
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300297 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300298 if (priv->firm == NULL) {
299 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300300 rc = -ENOMEM;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300301 goto err;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300302 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300303 priv->firm_size = n_array;
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300304
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300305 n = -1;
306 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300307 __u32 type, size;
308 v4l2_std_id id;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300309 __u16 int_freq = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300310
311 n++;
312 if (n >= n_array) {
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300313 tuner_err("More firmware images in file than "
314 "were expected!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300315 goto corrupt;
316 }
317
318 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300319 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300320 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300321 goto corrupt;
322 }
323
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300324 type = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300325 p += sizeof(type);
326
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300327 id = le64_to_cpu(*(v4l2_std_id *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300328 p += sizeof(id);
329
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300330 if (type & HAS_IF) {
331 int_freq = le16_to_cpu(*(__u16 *) p);
332 p += sizeof(int_freq);
333 }
334
Michel Ludwig2fc580f2007-11-16 07:19:35 -0300335 size = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300336 p += sizeof(size);
337
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300338 if ((!size) || (size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300339 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300340 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300341 printk("(%x), id %llx is corrupted "
342 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300343 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300344 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300345 goto corrupt;
346 }
347
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300348 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300349 if (priv->firm[n].ptr == NULL) {
350 tuner_err("Not enough memory to load firmware file.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300351 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300352 goto err;
353 }
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300354 tuner_dbg("Reading firmware type ");
355 if (debug) {
Chris Pascoee0262682007-12-02 06:30:50 -0300356 dump_firm_type_and_int_freq(type, int_freq);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300357 printk("(%x), id %llx, size=%d.\n",
Chris Pascoee0262682007-12-02 06:30:50 -0300358 type, (unsigned long long)id, size);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300359 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300360
361 memcpy(priv->firm[n].ptr, p, size);
362 priv->firm[n].type = type;
363 priv->firm[n].id = id;
364 priv->firm[n].size = size;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300365 priv->firm[n].int_freq = int_freq;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300366
367 p += size;
368 }
369
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300370 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300371 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300372 goto corrupt;
373 }
374
375 goto done;
376
377corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300378 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300379 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300380
381err:
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300382 tuner_info("Releasing partially loaded firmware file.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300383 free_firmware(priv);
384
385done:
386 release_firmware(fw);
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300387 if (rc == 0)
388 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300389
390 return rc;
391}
392
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300393static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
394 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300395{
396 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoeb1535292007-11-19 10:04:06 -0300397 int i, best_i = -1, best_nr_matches = 0;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300398 unsigned int ign_firm_type_mask = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300399
Chris Pascoeb1535292007-11-19 10:04:06 -0300400 tuner_dbg("%s called, want type=", __FUNCTION__);
401 if (debug) {
402 dump_firm_type(type);
403 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
404 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300405
406 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300407 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300408 return -EINVAL;
409 }
410
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300411 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300412 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300413
Chris Pascoee0f0b372007-11-19 11:22:03 -0300414 if (type & BASE)
415 type &= BASE_TYPES;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300416 else if (type & SCODE) {
Chris Pascoee0f0b372007-11-19 11:22:03 -0300417 type &= SCODE_TYPES;
Chris Pascoeef207fe2007-12-02 09:30:55 -0300418 ign_firm_type_mask = HAS_IF;
419 } else if (type & DTV_TYPES)
Chris Pascoe11a9eff2007-11-19 23:18:36 -0300420 type &= DTV_TYPES;
421 else if (type & STD_SPECIFIC_TYPES)
422 type &= STD_SPECIFIC_TYPES;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300423
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300424 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300425 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeef207fe2007-12-02 09:30:55 -0300426 if ((type == (priv->firm[i].type & ~ign_firm_type_mask)) &&
427 (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300428 goto found;
429 }
430
431 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300432 for (i = 0; i < priv->firm_size; i++) {
Chris Pascoeb1535292007-11-19 10:04:06 -0300433 v4l2_std_id match_mask;
434 int nr_matches;
435
Chris Pascoeef207fe2007-12-02 09:30:55 -0300436 if (type != (priv->firm[i].type & ~ign_firm_type_mask))
Chris Pascoeb1535292007-11-19 10:04:06 -0300437 continue;
438
439 match_mask = *id & priv->firm[i].id;
440 if (!match_mask)
441 continue;
442
443 if ((*id & match_mask) == *id)
444 goto found; /* Supports all the requested standards */
445
446 nr_matches = hweight64(match_mask);
447 if (nr_matches > best_nr_matches) {
448 best_nr_matches = nr_matches;
449 best_i = i;
450 }
451 }
452
453 if (best_nr_matches > 0) {
454 tuner_dbg("Selecting best matching firmware (%d bits) for "
455 "type=", best_nr_matches);
456 dump_firm_type(type);
457 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
458 i = best_i;
459 goto found;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300460 }
461
462 /*FIXME: Would make sense to seek for type "hint" match ? */
463
Chris Pascoeb1535292007-11-19 10:04:06 -0300464 i = -ENOENT;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300465 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300466
467found:
468 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300469
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300470ret:
Chris Pascoeb1535292007-11-19 10:04:06 -0300471 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300472 if (debug) {
473 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300474 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300475 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300476 return i;
477}
478
479static int load_firmware(struct dvb_frontend *fe, unsigned int type,
480 v4l2_std_id *id)
481{
482 struct xc2028_data *priv = fe->tuner_priv;
483 int pos, rc;
Chris Pascoe0a196b62007-11-19 09:29:59 -0300484 unsigned char *p, *endp, buf[priv->ctrl.max_len];
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300485
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300486 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300487
488 pos = seek_firmware(fe, type, id);
489 if (pos < 0)
490 return pos;
491
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300492 tuner_info("Loading firmware for type=");
Chris Pascoeb1535292007-11-19 10:04:06 -0300493 dump_firm_type(priv->firm[pos].type);
494 printk("(%x), id %016llx.\n", priv->firm[pos].type,
495 (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300496
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300497 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300498 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300499
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300500 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300501 __u16 size;
502
503 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300504 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300505 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300506 return -EINVAL;
507 }
508
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300509 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300510 p += sizeof(size);
511
512 if (size == 0xffff)
513 return 0;
514
515 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300516 /* Special callback command received */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300517 rc = priv->tuner_callback(priv->video_dev,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300518 XC2028_TUNER_RESET, 0);
519 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300520 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300521 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300522 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300523 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300524 continue;
525 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300526 if (size >= 0xff00) {
527 switch (size) {
528 case 0xff00:
529 rc = priv->tuner_callback(priv->video_dev,
530 XC2028_RESET_CLK, 0);
531 if (rc < 0) {
532 tuner_err("Error at RESET code %d\n",
533 (*p) & 0x7f);
534 return -EINVAL;
535 }
Chris Pascoeb32f9fb2007-11-19 04:53:50 -0300536 break;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300537 default:
538 tuner_info("Invalid RESET code %d\n",
539 size & 0x7f);
540 return -EINVAL;
541
542 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300543 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300544 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300545
546 /* Checks for a sleep command */
547 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300548 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300549 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300550 }
551
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300552 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300553 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300554 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300555 return -EINVAL;
556 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300557
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300558 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300559 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300560 size--;
561
562 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300563 while (size > 0) {
Chris Pascoe0a196b62007-11-19 09:29:59 -0300564 int len = (size < priv->ctrl.max_len - 1) ?
565 size : priv->ctrl.max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300566
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300567 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300568
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300569 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300570 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300571 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300572 return -EINVAL;
573 }
574
575 p += len;
576 size -= len;
577 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300578 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300579 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300580}
581
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300582static int load_scode(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300583 v4l2_std_id *id, __u16 int_freq, int scode)
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300584{
585 struct xc2028_data *priv = fe->tuner_priv;
586 int pos, rc;
587 unsigned char *p;
588
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300589 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300590
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300591 if (!int_freq) {
592 pos = seek_firmware(fe, type, id);
593 if (pos < 0)
594 return pos;
595 } else {
596 for (pos = 0; pos < priv->firm_size; pos++) {
597 if ((priv->firm[pos].int_freq == int_freq) &&
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300598 (priv->firm[pos].type & HAS_IF))
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300599 break;
600 }
601 if (pos == priv->firm_size)
602 return -ENOENT;
603 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300604
605 p = priv->firm[pos].ptr;
606
Chris Pascoe9ca01e72007-12-02 06:54:17 -0300607 if (priv->firm[pos].type & HAS_IF) {
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300608 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
609 return -EINVAL;
610 p += 12 * scode;
611 } else {
612 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
613 * has a 2-byte size header in the firmware format. */
614 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
615 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
616 return -EINVAL;
617 p += 14 * scode + 2;
618 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300619
Chris Pascoed7b22c52007-11-19 10:12:45 -0300620 tuner_info("Loading SCODE for type=");
Chris Pascoee0262682007-12-02 06:30:50 -0300621 dump_firm_type_and_int_freq(priv->firm[pos].type,
622 priv->firm[pos].int_freq);
Chris Pascoed7b22c52007-11-19 10:12:45 -0300623 printk("(%x), id %016llx.\n", priv->firm[pos].type,
624 (unsigned long long)*id);
625
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300626 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300627 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
628 else
629 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
630 if (rc < 0)
631 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300632
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300633 rc = i2c_send(priv, p, 12);
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300634 if (rc < 0)
635 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300636
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300637 rc = send_seq(priv, {0x00, 0x8c});
638 if (rc < 0)
639 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300640
641 return 0;
642}
643
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300644static int check_firmware(struct dvb_frontend *fe, unsigned int type,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300645 v4l2_std_id std, __u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300646{
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300647 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300648 struct firmware_properties new_fw;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300649 int rc = 0, is_retry = 0;
650 u16 version, hwmodel;
651 v4l2_std_id std0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300652
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300653 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300654
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300655 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300656 if (!priv->ctrl.fname) {
657 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300658 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300659 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300660
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300661 rc = load_all_firmwares(fe);
662 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300663 return rc;
664 }
665
Mauro Carvalho Chehab0f6dac12008-01-05 16:47:16 -0300666 if (priv->ctrl.mts && !(type & FM))
Chris Pascoee0f0b372007-11-19 11:22:03 -0300667 type |= MTS;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300668
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300669retry:
Chris Pascoee0f0b372007-11-19 11:22:03 -0300670 new_fw.type = type;
671 new_fw.id = std;
672 new_fw.std_req = std;
673 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
674 new_fw.scode_nr = 0;
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300675 new_fw.int_freq = int_freq;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300676
677 tuner_dbg("checking firmware, user requested type=");
678 if (debug) {
679 dump_firm_type(new_fw.type);
Chris Pascoee0262682007-12-02 06:30:50 -0300680 printk("(%x), id %016llx, ", new_fw.type,
Chris Pascoee0f0b372007-11-19 11:22:03 -0300681 (unsigned long long)new_fw.std_req);
Chris Pascoee0262682007-12-02 06:30:50 -0300682 if (!int_freq) {
683 printk("scode_tbl ");
684 dump_firm_type(priv->ctrl.scode_table);
685 printk("(%x), ", priv->ctrl.scode_table);
686 } else
687 printk("int_freq %d, ", new_fw.int_freq);
688 printk("scode_nr %d\n", new_fw.scode_nr);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300689 }
690
691 /* No need to reload base firmware if it matches */
692 if (((BASE | new_fw.type) & BASE_TYPES) ==
693 (priv->cur_fw.type & BASE_TYPES)) {
694 tuner_dbg("BASE firmware not changed.\n");
695 goto skip_base;
696 }
697
698 /* Updating BASE - forget about all currently loaded firmware */
699 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
700
701 /* Reset is needed before loading firmware */
702 rc = priv->tuner_callback(priv->video_dev,
703 XC2028_TUNER_RESET, 0);
704 if (rc < 0)
705 goto fail;
706
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300707 /* BASE firmwares are all std0 */
708 std0 = 0;
709 rc = load_firmware(fe, BASE | new_fw.type, &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300710 if (rc < 0) {
711 tuner_err("Error %d while loading base firmware\n",
712 rc);
713 goto fail;
714 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300715
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300716 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300717 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300718
Chris Pascoe47bd5bc2007-11-19 23:11:37 -0300719 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
Chris Pascoe1ad0b792007-11-19 23:43:13 -0300720 if (rc == -ENOENT)
721 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
722 &std0);
Chris Pascoee0f0b372007-11-19 11:22:03 -0300723 if (rc < 0 && rc != -ENOENT) {
724 tuner_err("Error %d while loading init1 firmware\n",
725 rc);
726 goto fail;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300727 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300728
Chris Pascoee0f0b372007-11-19 11:22:03 -0300729skip_base:
730 /*
731 * No need to reload standard specific firmware if base firmware
732 * was not reloaded and requested video standards have not changed.
733 */
734 if (priv->cur_fw.type == (BASE | new_fw.type) &&
735 priv->cur_fw.std_req == std) {
736 tuner_dbg("Std-specific firmware already loaded.\n");
737 goto skip_std_specific;
738 }
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300739
Chris Pascoee0f0b372007-11-19 11:22:03 -0300740 /* Reloading std-specific firmware forces a SCODE update */
741 priv->cur_fw.scode_table = 0;
742
Chris Pascoee0f0b372007-11-19 11:22:03 -0300743 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Mauro Carvalho Chehabcca83792007-11-22 11:47:18 -0300744 if (rc == -ENOENT)
745 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
746
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300747 if (rc < 0)
Chris Pascoee0f0b372007-11-19 11:22:03 -0300748 goto fail;
749
750skip_std_specific:
751 if (priv->cur_fw.scode_table == new_fw.scode_table &&
752 priv->cur_fw.scode_nr == new_fw.scode_nr) {
753 tuner_dbg("SCODE firmware already loaded.\n");
754 goto check_device;
755 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300756
Mauro Carvalho Chehab40ae91a2008-02-14 01:52:48 -0300757 if (new_fw.type & FM)
758 goto check_device;
759
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300760 /* Load SCODE firmware, if exists */
Chris Pascoee0f0b372007-11-19 11:22:03 -0300761 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300762
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300763 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
764 new_fw.int_freq, new_fw.scode_nr);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300765
Chris Pascoee0f0b372007-11-19 11:22:03 -0300766check_device:
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300767 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
768 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
769 tuner_err("Unable to read tuner registers.\n");
770 goto fail;
771 }
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300772
773 tuner_info("Device is Xceive %d version %d.%d, "
774 "firmware version %d.%d\n",
775 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
776 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300777
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300778 /* Check firmware version against what we downloaded. */
779 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
780 tuner_err("Incorrect readback of firmware version.\n");
781 goto fail;
782 }
783
784 /* Check that the tuner hardware model remains consistent over time. */
785 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
786 priv->hwmodel = hwmodel;
787 priv->hwvers = version & 0xff00;
788 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
789 priv->hwvers != (version & 0xff00)) {
790 tuner_err("Read invalid device hardware information - tuner "
791 "hung?\n");
792 goto fail;
793 }
794
Chris Pascoee0f0b372007-11-19 11:22:03 -0300795 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
796
797 /*
798 * By setting BASE in cur_fw.type only after successfully loading all
799 * firmwares, we can:
800 * 1. Identify that BASE firmware with type=0 has been loaded;
801 * 2. Tell whether BASE firmware was just changed the next time through.
802 */
803 priv->cur_fw.type |= BASE;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300804
805 return 0;
Chris Pascoee0f0b372007-11-19 11:22:03 -0300806
807fail:
808 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
Chris Pascoe8bf799a2007-11-19 11:35:45 -0300809 if (!is_retry) {
810 msleep(50);
811 is_retry = 1;
812 tuner_dbg("Retrying firmware load\n");
813 goto retry;
814 }
815
Chris Pascoee0f0b372007-11-19 11:22:03 -0300816 if (rc == -ENOENT)
817 rc = -EINVAL;
818 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300819}
820
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300821static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300822{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300823 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300824 u16 frq_lock, signal = 0;
825 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300826
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300827 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300828
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300829 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300830
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300831 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300832 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
833 if (rc < 0 || frq_lock == 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300834 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300835
836 /* Frequency is locked. Return signal quality */
837
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300838 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300839 rc = xc2028_get_reg(priv, 0x0040, &signal);
840 if (rc < 0)
841 signal = -frq_lock;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300842
843ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300844 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300845
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300846 *strength = signal;
847
Chris Pascoe7d58d112007-11-19 04:31:58 -0300848 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300849}
850
851#define DIV 15625
852
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300853static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300854 enum tuner_mode new_mode,
855 unsigned int type,
856 v4l2_std_id std,
857 u16 int_freq)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300858{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300859 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300860 int rc = -EINVAL;
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300861 unsigned char buf[4];
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300862 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300863
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300864 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300865
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300866 mutex_lock(&priv->lock);
867
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300868 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300869
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300870 if (check_firmware(fe, type, std, int_freq) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300871 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300872
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300873 /* On some cases xc2028 can disable video output, if
874 * very weak signals are received. By sending a soft
875 * reset, this is re-enabled. So, it is better to always
876 * send a soft reset before changing channels, to be sure
877 * that xc2028 will be in a safe state.
878 * Maybe this might also be needed for DTV.
879 */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300880 if (new_mode == T_ANALOG_TV) {
Mauro Carvalho Chehab2800ae92007-11-22 12:48:04 -0300881 rc = send_seq(priv, {0x00, 0x00});
Michael Krufkyd536c9d2007-12-18 10:42:33 -0300882 } else if (priv->cur_fw.type & ATSC) {
883 offset = 1750000;
884 } else {
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300885 offset = 2750000;
Chris Pascoe897b8422007-12-02 09:39:18 -0300886 /*
887 * We must adjust the offset by 500kHz in two cases in order
888 * to correctly center the IF output:
889 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
890 * selected and a 7MHz channel is tuned;
891 * 2) When tuning a VHF channel with DTV78 firmware.
892 */
893 if (((priv->cur_fw.type & DTV7) &&
894 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
895 ((priv->cur_fw.type & DTV78) && freq < 470000000))
Chris Pascoea44f1c42007-11-19 06:35:26 -0300896 offset -= 500000;
897 }
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300898
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300899 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300900
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300901 /* CMD= Set frequency */
Chris Pascoe06fd82d2007-11-19 06:06:08 -0300902 if (priv->firm_version < 0x0202)
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300903 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
904 else
905 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
906 if (rc < 0)
907 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300908
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300909 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300910 if (rc < 0)
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300911 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300912
913 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300914
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300915 buf[0] = 0xff & (div >> 24);
916 buf[1] = 0xff & (div >> 16);
917 buf[2] = 0xff & (div >> 8);
918 buf[3] = 0xff & (div);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300919
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300920 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300921 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300922 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300923 msleep(100);
924
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300925 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300926
Chris Pascoe2ce4b3a2007-11-19 06:20:17 -0300927 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
928 buf[0], buf[1], buf[2], buf[3],
929 freq / 1000000, (freq % 1000000) / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300930
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300931 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300932
933ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300934 mutex_unlock(&priv->lock);
935
936 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300937}
938
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300939static int xc2028_set_analog_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300940 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300941{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300942 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300943 unsigned int type=0;
944
945 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabc71d4bc2007-11-22 11:47:18 -0300946
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300947 if (p->mode == V4L2_TUNER_RADIO) {
948 type |= FM;
949 if (priv->ctrl.input1)
950 type |= INPUT1;
951 return generic_set_freq(fe, (625l * p->frequency) / 10,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300952 T_ANALOG_TV, type, 0, 0);
Mauro Carvalho Chehabd74cb252007-11-24 10:20:15 -0300953 }
954
Mauro Carvalho Chehaba5e9fe12007-11-23 11:36:18 -0300955 /* if std is not defined, choose one */
956 if (!p->std)
957 p->std = V4L2_STD_MN;
958
959 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300960 if (!(p->std & V4L2_STD_MN))
961 type |= F8MHZ;
Michel Ludwig701672e2007-07-18 10:29:10 -0300962
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300963 /* Add audio hack to std mask */
964 p->std |= parse_audio_std_option();
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300965
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300966 return generic_set_freq(fe, 62500l * p->frequency,
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -0300967 T_ANALOG_TV, type, p->std, 0);
Michel Ludwig701672e2007-07-18 10:29:10 -0300968}
969
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300970static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -0300971 struct dvb_frontend_parameters *p)
972{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300973 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300974 unsigned int type=0;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300975 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
Chris Pascoead35ce92007-12-02 06:36:42 -0300976 u16 demod = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -0300977
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300978 tuner_dbg("%s called\n", __FUNCTION__);
Michel Ludwig701672e2007-07-18 10:29:10 -0300979
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -0300980 if (priv->ctrl.d2633)
981 type |= D2633;
982 else
983 type |= D2620;
984
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300985 switch(fe->ops.info.type) {
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300986 case FE_OFDM:
987 bw = p->u.ofdm.bandwidth;
988 break;
989 case FE_QAM:
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -0300990 tuner_info("WARN: There are some reports that "
991 "QAM 6 MHz doesn't work.\n"
992 "If this works for you, please report by "
993 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -0300994 bw = BANDWIDTH_6_MHZ;
995 type |= QAM;
996 break;
997 case FE_ATSC:
998 bw = BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -0300999 /* The only ATSC firmware (at least on v2.7) is D2633,
1000 so overrides ctrl->d2633 */
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001001 type |= ATSC| D2633;
Mauro Carvalho Chehab4bfae522007-12-16 09:24:30 -03001002 type &= ~D2620;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001003 break;
Mauro Carvalho Chehab5c156482007-12-02 00:02:18 -03001004 /* DVB-S is not supported */
1005 default:
1006 return -EINVAL;
Mauro Carvalho Chehabd04aa542007-11-24 10:47:03 -03001007 }
1008
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001009 switch (bw) {
1010 case BANDWIDTH_8_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001011 if (p->frequency < 470000000)
1012 priv->ctrl.vhfbw7 = 0;
1013 else
1014 priv->ctrl.uhfbw8 = 1;
1015 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1016 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001017 break;
1018 case BANDWIDTH_7_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001019 if (p->frequency < 470000000)
1020 priv->ctrl.vhfbw7 = 1;
1021 else
1022 priv->ctrl.uhfbw8 = 0;
1023 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1024 type |= F8MHZ;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001025 break;
1026 case BANDWIDTH_6_MHZ:
Chris Pascoe3dfefc52007-12-02 10:07:06 -03001027 type |= DTV6;
1028 priv->ctrl.vhfbw7 = 0;
1029 priv->ctrl.uhfbw8 = 0;
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001030 break;
1031 default:
1032 tuner_err("error: bandwidth not supported.\n");
1033 };
1034
Mauro Carvalho Chehab66c2d532007-11-25 19:26:36 -03001035 /* All S-code tables need a 200kHz shift */
1036 if (priv->ctrl.demod)
Chris Pascoead35ce92007-12-02 06:36:42 -03001037 demod = priv->ctrl.demod + 200;
Mauro Carvalho Chehabb542dfd2007-11-24 11:07:12 -03001038
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001039 return generic_set_freq(fe, p->frequency,
Chris Pascoead35ce92007-12-02 06:36:42 -03001040 T_DIGITAL_TV, type, 0, demod);
Michel Ludwig701672e2007-07-18 10:29:10 -03001041}
1042
Chris Pascoe45819c32007-11-19 11:41:20 -03001043static int xc2028_sleep(struct dvb_frontend *fe)
1044{
1045 struct xc2028_data *priv = fe->tuner_priv;
1046 int rc = 0;
1047
1048 tuner_dbg("%s called\n", __FUNCTION__);
1049
1050 mutex_lock(&priv->lock);
1051
1052 if (priv->firm_version < 0x0202)
1053 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1054 else
1055 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1056
1057 priv->cur_fw.type = 0; /* need firmware reload */
1058
1059 mutex_unlock(&priv->lock);
1060
1061 return rc;
1062}
1063
1064
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001065static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -03001066{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001067 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -03001068
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001069 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001070
Chris Pascoeaa501be2007-11-19 04:45:38 -03001071 mutex_lock(&xc2028_list_mutex);
1072
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001073 priv->count--;
1074
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001075 if (!priv->count) {
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001076 list_del(&priv->xc2028_list);
1077
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001078 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001079
1080 free_firmware(priv);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001081 kfree(priv);
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001082 fe->tuner_priv = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001083 }
Michel Ludwig701672e2007-07-18 10:29:10 -03001084
Chris Pascoeaa501be2007-11-19 04:45:38 -03001085 mutex_unlock(&xc2028_list_mutex);
1086
Michel Ludwig701672e2007-07-18 10:29:10 -03001087 return 0;
1088}
1089
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001090static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -03001091{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001092 struct xc2028_data *priv = fe->tuner_priv;
1093
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001094 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001095
1096 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -03001097
1098 return 0;
1099}
1100
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001101static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001102{
1103 struct xc2028_data *priv = fe->tuner_priv;
1104 struct xc2028_ctrl *p = priv_cfg;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001105 int rc = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001106
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001107 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001108
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001109 mutex_lock(&priv->lock);
1110
Chris Pascoe0a196b62007-11-19 09:29:59 -03001111 kfree(priv->ctrl.fname);
1112 free_firmware(priv);
1113
1114 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1115 priv->ctrl.fname = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001116
1117 if (p->fname) {
Chris Pascoe0a196b62007-11-19 09:29:59 -03001118 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1119 if (priv->ctrl.fname == NULL)
1120 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001121 }
1122
Chris Pascoe0a196b62007-11-19 09:29:59 -03001123 if (priv->ctrl.max_len < 9)
1124 priv->ctrl.max_len = 13;
Mauro Carvalho Chehab352fae12007-11-01 16:56:26 -03001125
Chris Pascoe06fd82d2007-11-19 06:06:08 -03001126 mutex_unlock(&priv->lock);
1127
Chris Pascoe0a196b62007-11-19 09:29:59 -03001128 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001129}
1130
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001131static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -03001132 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001133 .name = "Xceive XC3028",
1134 .frequency_min = 42000000,
1135 .frequency_max = 864000000,
1136 .frequency_step = 50000,
1137 },
Michel Ludwig701672e2007-07-18 10:29:10 -03001138
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001139 .set_config = xc2028_set_config,
Mauro Carvalho Chehab00deff12007-11-24 10:13:42 -03001140 .set_analog_params = xc2028_set_analog_freq,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001141 .release = xc2028_dvb_release,
1142 .get_frequency = xc2028_get_frequency,
1143 .get_rf_strength = xc2028_signal,
1144 .set_params = xc2028_set_params,
Chris Pascoe45819c32007-11-19 11:41:20 -03001145 .sleep = xc2028_sleep,
Michel Ludwig701672e2007-07-18 10:29:10 -03001146
Michel Ludwig701672e2007-07-18 10:29:10 -03001147};
1148
Michael Krufky7972f982007-12-21 16:12:09 -03001149struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1150 struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -03001151{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001152 struct xc2028_data *priv;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001153 void *video_dev;
Michel Ludwig701672e2007-07-18 10:29:10 -03001154
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -03001155 if (debug)
Chris Pascoe3157ece2007-11-19 04:34:29 -03001156 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001157
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001158 if (NULL == cfg || NULL == cfg->video_dev)
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001159 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001160
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001161 if (!fe) {
Chris Pascoe3157ece2007-11-19 04:34:29 -03001162 printk(KERN_ERR PREFIX ": No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001163 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001164 }
1165
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001166 video_dev = cfg->video_dev;
1167
Chris Pascoeaa501be2007-11-19 04:45:38 -03001168 mutex_lock(&xc2028_list_mutex);
1169
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001170 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001171 if (priv->video_dev == cfg->video_dev) {
1172 video_dev = NULL;
1173 break;
1174 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001175 }
1176
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001177 if (video_dev) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001178 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Chris Pascoeaa501be2007-11-19 04:45:38 -03001179 if (priv == NULL) {
1180 mutex_unlock(&xc2028_list_mutex);
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001181 return NULL;
Chris Pascoeaa501be2007-11-19 04:45:38 -03001182 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001183
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001184 priv->i2c_props.addr = cfg->i2c_addr;
1185 priv->i2c_props.adap = cfg->i2c_adap;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001186 priv->video_dev = video_dev;
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001187 priv->tuner_callback = cfg->callback;
Chris Pascoe0a196b62007-11-19 09:29:59 -03001188 priv->ctrl.max_len = 13;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -03001189
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001190 mutex_init(&priv->lock);
1191
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001192 list_add_tail(&priv->xc2028_list, &xc2028_list);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001193 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001194
1195 fe->tuner_priv = priv;
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -03001196 priv->count++;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001197
1198 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -03001199 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001200
1201 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -03001202
Mauro Carvalho Chehab71a2ee32007-11-22 12:19:37 -03001203 if (cfg->ctrl)
1204 xc2028_set_config(fe, cfg->ctrl);
1205
Chris Pascoeaa501be2007-11-19 04:45:38 -03001206 mutex_unlock(&xc2028_list_mutex);
1207
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001208 return fe;
Michel Ludwig701672e2007-07-18 10:29:10 -03001209}
Michel Ludwiga37b4c92007-11-16 07:46:14 -03001210
Michel Ludwig701672e2007-07-18 10:29:10 -03001211EXPORT_SYMBOL(xc2028_attach);
1212
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001213MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -03001214MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -03001215MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1216MODULE_LICENSE("GPL");