blob: ac7f3e1d32f469d3e371cb190dce2dd786b45115 [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;
53 unsigned char *ptr;
54 unsigned int size;
55};
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030056
57struct xc2028_data {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030058 struct list_head xc2028_list;
59 struct tuner_i2c_props i2c_props;
60 int (*tuner_callback) (void *dev,
61 int command, int arg);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030062 void *video_dev;
63 int count;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030064 __u32 frequency;
65
66 struct firmware_description *firm;
67 int firm_size;
68
69 __u16 version;
70
71 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030072
Michel Ludwig701672e2007-07-18 10:29:10 -030073 v4l2_std_id firm_type; /* video stds supported
74 by current firmware */
75 fe_bandwidth_t bandwidth; /* Firmware bandwidth:
76 6M, 7M or 8M */
77 int need_load_generic; /* The generic firmware
78 were loaded? */
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030079
80 int max_len; /* Max firmware chunk */
81
Michel Ludwig701672e2007-07-18 10:29:10 -030082 enum tuner_mode mode;
83 struct i2c_client *i2c_client;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030084
85 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030086};
87
Chris Pascoe47cc5b72007-11-19 04:14:23 -030088#define i2c_send(priv, buf, size) ({ \
89 int _rc; \
90 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
91 if (size != _rc) \
92 tuner_info("i2c output error: rc = %d (should be %d)\n",\
93 _rc, (int)size); \
94 _rc; \
95})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030096
Chris Pascoe47cc5b72007-11-19 04:14:23 -030097#define i2c_rcv(priv, buf, size) ({ \
98 int _rc; \
99 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
100 if (size != _rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300101 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300102 _rc, (int)size); \
103 _rc; \
104})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300105
Chris Pascoe7d58d112007-11-19 04:31:58 -0300106#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
107 int _rc; \
108 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
109 ibuf, isize); \
110 if (isize != _rc) \
111 tuner_err("i2c input error: rc = %d (should be %d)\n", \
112 _rc, (int)isize); \
113 _rc; \
114})
115
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300116#define send_seq(priv, data...) ({ \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300117 static u8 _val[] = data; \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300118 int _rc; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300119 if (sizeof(_val) != \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300120 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300121 _val, sizeof(_val)))) { \
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300122 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
123 } else \
124 msleep(10); \
125 _rc; \
126})
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300127
Chris Pascoe7d58d112007-11-19 04:31:58 -0300128static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300129{
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300130 unsigned char buf[2];
Chris Pascoe7d58d112007-11-19 04:31:58 -0300131 unsigned char ibuf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300132
Chris Pascoe7d58d112007-11-19 04:31:58 -0300133 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300134
Chris Pascoe7d58d112007-11-19 04:31:58 -0300135 buf[0] = reg >> 8;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300136 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300137
Chris Pascoe7d58d112007-11-19 04:31:58 -0300138 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
139 return -EIO;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300140
Chris Pascoe7d58d112007-11-19 04:31:58 -0300141 *val = (ibuf[1]) | (ibuf[0] << 8);
142 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300143}
144
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300145void dump_firm_type(unsigned int type)
146{
147 if (type & BASE)
148 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300149 if (type & INIT1)
150 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300151 if (type & F8MHZ)
152 printk("F8MHZ ");
153 if (type & MTS)
154 printk("MTS ");
155 if (type & D2620)
156 printk("D2620 ");
157 if (type & D2633)
158 printk("D2633 ");
159 if (type & DTV6)
160 printk("DTV6 ");
161 if (type & QAM)
162 printk("QAM ");
163 if (type & DTV7)
164 printk("DTV7 ");
165 if (type & DTV78)
166 printk("DTV78 ");
167 if (type & DTV8)
168 printk("DTV8 ");
169 if (type & FM)
170 printk("FM ");
171 if (type & INPUT1)
172 printk("INPUT1 ");
173 if (type & LCD)
174 printk("LCD ");
175 if (type & NOGD)
176 printk("NOGD ");
177 if (type & MONO)
178 printk("MONO ");
179 if (type & ATSC)
180 printk("ATSC ");
181 if (type & IF)
182 printk("IF ");
183 if (type & LG60)
184 printk("LG60 ");
185 if (type & ATI638)
186 printk("ATI638 ");
187 if (type & OREN538)
188 printk("OREN538 ");
189 if (type & OREN36)
190 printk("OREN36 ");
191 if (type & TOYOTA388)
192 printk("TOYOTA388 ");
193 if (type & TOYOTA794)
194 printk("TOYOTA794 ");
195 if (type & DIBCOM52)
196 printk("DIBCOM52 ");
197 if (type & ZARLINK456)
198 printk("ZARLINK456 ");
199 if (type & CHINA)
200 printk("CHINA ");
201 if (type & F6MHZ)
202 printk("F6MHZ ");
203 if (type & INPUT2)
204 printk("INPUT2 ");
205 if (type & SCODE)
206 printk("SCODE ");
207}
208
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300209static v4l2_std_id parse_audio_std_option(void)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300210{
Chris Pascoee155d902007-11-19 04:16:47 -0300211 if (strcasecmp(audio_std, "A2") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300212 return V4L2_STD_A2;
Chris Pascoee155d902007-11-19 04:16:47 -0300213 if (strcasecmp(audio_std, "A2/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300214 return V4L2_STD_A2_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300215 if (strcasecmp(audio_std, "A2/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300216 return V4L2_STD_A2_B;
Chris Pascoee155d902007-11-19 04:16:47 -0300217 if (strcasecmp(audio_std, "NICAM") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300218 return V4L2_STD_NICAM;
Chris Pascoee155d902007-11-19 04:16:47 -0300219 if (strcasecmp(audio_std, "NICAM/A") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300220 return V4L2_STD_NICAM_A;
Chris Pascoee155d902007-11-19 04:16:47 -0300221 if (strcasecmp(audio_std, "NICAM/B") == 0)
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300222 return V4L2_STD_NICAM_B;
223
224 return 0;
225}
226
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300227static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300228{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300229 int i;
230
231 if (!priv->firm)
232 return;
233
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300234 for (i = 0; i < priv->firm_size; i++)
235 kfree(priv->firm[i].ptr);
236
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300237 kfree(priv->firm);
238
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300239 priv->firm = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300240 priv->need_load_generic = 1;
241}
242
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300243static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300244{
245 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300246 const struct firmware *fw = NULL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300247 unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300248 int rc = 0;
249 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300250 char name[33];
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300251
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300252 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300253
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300254 tuner_info("Reading firmware %s\n", priv->ctrl.fname);
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300255 rc = request_firmware(&fw, priv->ctrl.fname,
256 &priv->i2c_props.adap->dev);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300257 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300258 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300259 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300260 priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300261 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300262 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300263 rc, priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300264
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300265 return rc;
266 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300267 p = fw->data;
268 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300269
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300270 if (fw->size < sizeof(name) - 1 + 2) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300271 tuner_err("Error: firmware size is zero!\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300272 rc = -EINVAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300273 goto done;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300274 }
275
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300276 memcpy(name, p, sizeof(name) - 1);
277 name[sizeof(name) - 1] = 0;
278 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300279
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300280 priv->version = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300281 p += 2;
282
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300283 tuner_info("Firmware: %s, ver %d.%d\n", name,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300284 priv->version >> 8, priv->version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300285
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300286 if (p + 2 > endp)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300287 goto corrupt;
288
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300289 n_array = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300290 p += 2;
291
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300292 tuner_info("There are %d firmwares at %s\n",
293 n_array, priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300294
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300295 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300296
297 if (!fw) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300298 tuner_err("Not enough memory for reading firmware.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300299 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300300 goto done;
301 }
302
303 priv->firm_size = n_array;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300304 n = -1;
305 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300306 __u32 type, size;
307 v4l2_std_id id;
308
309 n++;
310 if (n >= n_array) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300311 tuner_err("Too much firmwares at the file\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300312 goto corrupt;
313 }
314
315 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300316 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300317 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300318 goto corrupt;
319 }
320
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300321 type = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300322 p += sizeof(type);
323
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300324 id = le64_to_cpu(*(v4l2_std_id *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300325 p += sizeof(id);
326
Michel Ludwig2fc580f2007-11-16 07:19:35 -0300327 size = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300328 p += sizeof(size);
329
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300330 if ((!size) || (size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300331 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300332 dump_firm_type(type);
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300333 printk("(%x), id %llx is corrupted "
334 "(size=%d, expected %d)\n",
Chris Pascoe91240dd2007-11-19 04:38:53 -0300335 type, (unsigned long long)id,
Mauro Carvalho Chehabef8c1882007-11-16 16:28:21 -0300336 (unsigned)(endp - p), size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300337 goto corrupt;
338 }
339
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300340 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300341 if (!priv->firm[n].ptr) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300342 tuner_err("Not enough memory.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300343 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300344 goto err;
345 }
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300346 tuner_info("Reading firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300347 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300348 printk("(%x), id %llx, size=%d.\n",
349 type, (unsigned long long)id, size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300350
351 memcpy(priv->firm[n].ptr, p, size);
352 priv->firm[n].type = type;
353 priv->firm[n].id = id;
354 priv->firm[n].size = size;
355
356 p += size;
357 }
358
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300359 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300360 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300361 goto corrupt;
362 }
363
364 goto done;
365
366corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300367 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300368 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300369
370err:
371 tuner_info("Releasing loaded firmware file.\n");
372
373 free_firmware(priv);
374
375done:
376 release_firmware(fw);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300377 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300378
379 return rc;
380}
381
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300382static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
383 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300384{
385 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300386 int i;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300387
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300388 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300389
390 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300391 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300392 return -EINVAL;
393 }
394
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300395 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300396 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300397
398 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300399 for (i = 0; i < priv->firm_size; i++) {
400 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300401 goto found;
402 }
403
404 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300405 for (i = 0; i < priv->firm_size; i++) {
406 if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300407 goto found;
408 }
409
410 /*FIXME: Would make sense to seek for type "hint" match ? */
411
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300412 i = -EINVAL;
413 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300414
415found:
416 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300417
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300418ret:
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300419 tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
420 if (debug) {
421 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300422 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300423 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300424 return i;
425}
426
427static int load_firmware(struct dvb_frontend *fe, unsigned int type,
428 v4l2_std_id *id)
429{
430 struct xc2028_data *priv = fe->tuner_priv;
431 int pos, rc;
432 unsigned char *p, *endp, buf[priv->max_len];
433
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300434 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300435
436 pos = seek_firmware(fe, type, id);
437 if (pos < 0)
438 return pos;
439
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300440 tuner_info("Loading firmware for type=");
441 dump_firm_type(type);
Chris Pascoe91240dd2007-11-19 04:38:53 -0300442 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300443
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300444 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300445
446 if (!p) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300447 tuner_err("Firmware pointer were freed!");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300448 return -EINVAL;
449 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300450 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300451
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300452 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300453 __u16 size;
454
455 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300456 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300457 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300458 return -EINVAL;
459 }
460
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300461 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300462 p += sizeof(size);
463
464 if (size == 0xffff)
465 return 0;
466
467 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300468 /* Special callback command received */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300469 rc = priv->tuner_callback(priv->video_dev,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300470 XC2028_TUNER_RESET, 0);
471 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300472 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300473 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300474 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300475 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300476 continue;
477 }
Michel Ludwig5403bba2007-11-16 07:49:49 -0300478 if (size >= 0xff00) {
479 switch (size) {
480 case 0xff00:
481 rc = priv->tuner_callback(priv->video_dev,
482 XC2028_RESET_CLK, 0);
483 if (rc < 0) {
484 tuner_err("Error at RESET code %d\n",
485 (*p) & 0x7f);
486 return -EINVAL;
487 }
488 default:
489 tuner_info("Invalid RESET code %d\n",
490 size & 0x7f);
491 return -EINVAL;
492
493 }
Mauro Carvalho Chehab2d4c0ac2007-11-16 09:43:19 -0300494 continue;
Michel Ludwig5403bba2007-11-16 07:49:49 -0300495 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300496
497 /* Checks for a sleep command */
498 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300499 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300500 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300501 }
502
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300503 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300504 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300505 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300506 return -EINVAL;
507 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300508
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300509 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300510 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300511 size--;
512
513 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300514 while (size > 0) {
515 int len = (size < priv->max_len - 1) ?
516 size : priv->max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300517
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300518 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300519
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300520 rc = i2c_send(priv, buf, len + 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300521 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300522 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300523 return -EINVAL;
524 }
525
526 p += len;
527 size -= len;
528 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300529 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300530 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300531}
532
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300533static int load_scode(struct dvb_frontend *fe, unsigned int type,
534 v4l2_std_id *id, int scode)
535{
536 struct xc2028_data *priv = fe->tuner_priv;
537 int pos, rc;
538 unsigned char *p;
539
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300540 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300541
542 pos = seek_firmware(fe, type, id);
543 if (pos < 0)
544 return pos;
545
546 p = priv->firm[pos].ptr;
547
548 if (!p) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300549 tuner_err("Firmware pointer were freed!");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300550 return -EINVAL;
551 }
552
553 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
554 return -EINVAL;
555
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300556 if (priv->version < 0x0202)
557 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
558 else
559 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
560 if (rc < 0)
561 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300562
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300563 rc = i2c_send(priv, p + 12 * scode, 12);
564 if (rc < 0)
565 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300566
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300567 rc = send_seq(priv, {0x00, 0x8c});
568 if (rc < 0)
569 return -EIO;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300570
571 return 0;
572}
573
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300574static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300575 v4l2_std_id std, fe_bandwidth_t bandwidth)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300576{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300577 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300578 int rc;
579 u16 version, hwmodel;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300580 v4l2_std_id std0 = 0;
581 unsigned int type0 = 0, type = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300582 int change_digital_bandwidth;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300583
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300584 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300585
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300586 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300587 if (!priv->ctrl.fname) {
588 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300589 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300590 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300591
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300592 rc = load_all_firmwares(fe);
593 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300594 return rc;
595 }
596
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300597 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300598 priv->mode, new_mode);
Michel Ludwig701672e2007-07-18 10:29:10 -0300599
600 /* first of all, determine whether we have switched the mode */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300601 if (new_mode != priv->mode) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300602 priv->mode = new_mode;
603 priv->need_load_generic = 1;
Michel Ludwig701672e2007-07-18 10:29:10 -0300604 }
605
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300606 change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300607 && bandwidth != priv->bandwidth) ? 1 : 0;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300608 tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300609 bandwidth);
Michel Ludwig701672e2007-07-18 10:29:10 -0300610
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300611 if (priv->need_load_generic) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300612 /* Reset is needed before loading firmware */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300613 rc = priv->tuner_callback(priv->video_dev,
614 XC2028_TUNER_RESET, 0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300615 if (rc < 0)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300616 return rc;
617
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300618 type0 = BASE;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300619
620 if (priv->ctrl.type == XC2028_FIRM_MTS)
621 type0 |= MTS;
622
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300623 if (priv->bandwidth == 8)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300624 type0 |= F8MHZ;
625
626 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
627
628 rc = load_firmware(fe, type0, &std0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300629 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300630 tuner_err("Error %d while loading generic firmware\n",
631 rc);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300632 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300633 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300634
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300635 priv->need_load_generic = 0;
636 priv->firm_type = 0;
637 if (priv->mode == T_DIGITAL_TV)
638 change_digital_bandwidth = 1;
Michel Ludwig701672e2007-07-18 10:29:10 -0300639 }
640
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300641 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
Michel Ludwig701672e2007-07-18 10:29:10 -0300642
643 if (change_digital_bandwidth) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300644
645 /*FIXME: Should allow selecting between D2620 and D2633 */
646 type |= D2620;
647
648 /* FIXME: When should select a DTV78 firmware?
649 */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300650 switch (bandwidth) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300651 case BANDWIDTH_8_MHZ:
652 type |= DTV8;
653 break;
654 case BANDWIDTH_7_MHZ:
655 type |= DTV7;
656 break;
657 case BANDWIDTH_6_MHZ:
658 /* FIXME: Should allow select also ATSC */
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300659 type |= DTV6 | QAM;
Michel Ludwig701672e2007-07-18 10:29:10 -0300660 break;
661
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300662 default:
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300663 tuner_err("error: bandwidth not supported.\n");
Michel Ludwig701672e2007-07-18 10:29:10 -0300664 };
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300665 priv->bandwidth = bandwidth;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300666 }
667
Michel Ludwig5403bba2007-11-16 07:49:49 -0300668 if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
669 return 0;
670
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300671 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300672 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300673 type0 = BASE | INIT1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300674 if (priv->ctrl.type == XC2028_FIRM_MTS)
675 type0 |= MTS;
676
677 /* FIXME: Should handle errors - if INIT1 found */
678 rc = load_firmware(fe, type0, &std0);
679
680 /* FIXME: Should add support for FM radio
681 */
682
683 if (priv->ctrl.type == XC2028_FIRM_MTS)
684 type |= MTS;
685
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300686 if (priv->firm_type & std) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300687 tuner_dbg("Std-specific firmware already loaded.\n");
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300688 return 0;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300689 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300690
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300691 /* Add audio hack to std mask */
692 std |= parse_audio_std_option();
693
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300694 rc = load_firmware(fe, type, &std);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300695 if (rc < 0)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300696 return rc;
697
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300698 /* Load SCODE firmware, if exists */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300699 tuner_dbg("Trying to load scode 0\n");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300700 type |= SCODE;
701
702 rc = load_scode(fe, type, &std, 0);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300703
Chris Pascoe7d58d112007-11-19 04:31:58 -0300704 xc2028_get_reg(priv, 0x0004, &version);
705 xc2028_get_reg(priv, 0x0008, &hwmodel);
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300706
707 tuner_info("Device is Xceive %d version %d.%d, "
708 "firmware version %d.%d\n",
709 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
710 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300711
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300712 priv->firm_type = std;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300713
714 return 0;
715}
716
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300717static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300718{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300719 struct xc2028_data *priv = fe->tuner_priv;
Chris Pascoe7d58d112007-11-19 04:31:58 -0300720 u16 frq_lock, signal = 0;
721 int rc;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300722
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300723 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300724
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300725 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300726
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300727 /* Sync Lock Indicator */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300728 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
729 if (rc < 0 || frq_lock == 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300730 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300731
732 /* Frequency is locked. Return signal quality */
733
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300734 /* Get SNR of the video signal */
Chris Pascoe7d58d112007-11-19 04:31:58 -0300735 rc = xc2028_get_reg(priv, 0x0040, &signal);
736 if (rc < 0)
737 signal = -frq_lock;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300738
739ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300740 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300741
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300742 *strength = signal;
743
Chris Pascoe7d58d112007-11-19 04:31:58 -0300744 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300745}
746
747#define DIV 15625
748
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300749static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
750 enum tuner_mode new_mode,
751 v4l2_std_id std, fe_bandwidth_t bandwidth)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300752{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300753 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300754 int rc = -EINVAL;
755 unsigned char buf[5];
756 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300757
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300758 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300759
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300760 mutex_lock(&priv->lock);
761
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300762 /* HACK: It seems that specific firmware need to be reloaded
763 when freq is changed */
Michel Ludwig701672e2007-07-18 10:29:10 -0300764
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300765 priv->firm_type = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -0300766
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300767 /* Reset GPIO 1 */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300768 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300769 if (rc < 0)
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300770 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300771
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300772 msleep(10);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300773 tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300774
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300775 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300776 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300777
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300778 if (new_mode == T_DIGITAL_TV)
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300779 offset = 2750000;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300780
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300781 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300782
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300783 /* CMD= Set frequency */
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300784
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300785 if (priv->version < 0x0202)
786 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
787 else
788 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
789 if (rc < 0)
790 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300791
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300792 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300793 if (rc < 0)
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300794 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300795
796 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300797
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300798 buf[0] = 0xff & (div >> 24);
799 buf[1] = 0xff & (div >> 16);
800 buf[2] = 0xff & (div >> 8);
801 buf[3] = 0xff & (div);
802 buf[4] = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300803
Chris Pascoe47cc5b72007-11-19 04:14:23 -0300804 rc = i2c_send(priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300805 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300806 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300807 msleep(100);
808
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300809 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300810
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300811 tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300812 buf[1], buf[2], buf[3], buf[4],
813 freq / 1000000, (freq % 1000000) / 10000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300814
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300815 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300816
817ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300818 mutex_unlock(&priv->lock);
819
820 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300821}
822
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300823static int xc2028_set_tv_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300824 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300825{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300826 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -0300827
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300828 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300829
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300830 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
831 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
Michel Ludwig701672e2007-07-18 10:29:10 -0300832}
833
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300834static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -0300835 struct dvb_frontend_parameters *p)
836{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300837 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -0300838
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300839 tuner_dbg("%s called\n", __FUNCTION__);
Michel Ludwig701672e2007-07-18 10:29:10 -0300840
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300841 /* FIXME: Only OFDM implemented */
842 if (fe->ops.info.type != FE_OFDM) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300843 tuner_err("DTV type not implemented.\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300844 return -EINVAL;
845 }
Michel Ludwig701672e2007-07-18 10:29:10 -0300846
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300847 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300848 0 /* NOT USED */,
849 p->u.ofdm.bandwidth);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300850
Michel Ludwig701672e2007-07-18 10:29:10 -0300851}
852
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300853static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -0300854{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300855 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -0300856
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300857 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300858
Chris Pascoeaa501be2007-11-19 04:45:38 -0300859 mutex_lock(&xc2028_list_mutex);
860
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300861 priv->count--;
862
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300863 if (!priv->count) {
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -0300864 list_del(&priv->xc2028_list);
865
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300866 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300867
868 free_firmware(priv);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300869 kfree(priv);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300870 }
Michel Ludwig701672e2007-07-18 10:29:10 -0300871
Chris Pascoeaa501be2007-11-19 04:45:38 -0300872 mutex_unlock(&xc2028_list_mutex);
873
Michel Ludwig701672e2007-07-18 10:29:10 -0300874 return 0;
875}
876
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300877static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -0300878{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300879 struct xc2028_data *priv = fe->tuner_priv;
880
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300881 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300882
883 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -0300884
885 return 0;
886}
887
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300888static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300889{
890 struct xc2028_data *priv = fe->tuner_priv;
891 struct xc2028_ctrl *p = priv_cfg;
892
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300893 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300894
895 priv->ctrl.type = p->type;
896
897 if (p->fname) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300898 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300899
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300900 priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300901 if (!priv->ctrl.fname)
902 return -ENOMEM;
903
904 free_firmware(priv);
905 strcpy(priv->ctrl.fname, p->fname);
906 }
907
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300908 if (p->max_len > 0)
Mauro Carvalho Chehab352fae12007-11-01 16:56:26 -0300909 priv->max_len = p->max_len;
910
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300911 return 0;
912}
913
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300914static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -0300915 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300916 .name = "Xceive XC3028",
917 .frequency_min = 42000000,
918 .frequency_max = 864000000,
919 .frequency_step = 50000,
920 },
Michel Ludwig701672e2007-07-18 10:29:10 -0300921
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300922 .set_config = xc2028_set_config,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300923 .set_analog_params = xc2028_set_tv_freq,
924 .release = xc2028_dvb_release,
925 .get_frequency = xc2028_get_frequency,
926 .get_rf_strength = xc2028_signal,
927 .set_params = xc2028_set_params,
Michel Ludwig701672e2007-07-18 10:29:10 -0300928
Michel Ludwig701672e2007-07-18 10:29:10 -0300929};
930
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300931void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -0300932{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300933 struct xc2028_data *priv;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300934 void *video_dev;
Michel Ludwig701672e2007-07-18 10:29:10 -0300935
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300936 if (debug)
Chris Pascoe3157ece2007-11-19 04:34:29 -0300937 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300938
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300939 if (NULL == cfg->video_dev)
940 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300941
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300942 if (!fe) {
Chris Pascoe3157ece2007-11-19 04:34:29 -0300943 printk(KERN_ERR PREFIX ": No frontend!\n");
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300944 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300945 }
946
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300947 video_dev = cfg->video_dev;
948
Chris Pascoeaa501be2007-11-19 04:45:38 -0300949 mutex_lock(&xc2028_list_mutex);
950
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300951 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300952 if (priv->video_dev == cfg->video_dev) {
953 video_dev = NULL;
954 break;
955 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300956 }
957
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300958 if (video_dev) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300959 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Chris Pascoeaa501be2007-11-19 04:45:38 -0300960 if (priv == NULL) {
961 mutex_unlock(&xc2028_list_mutex);
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300962 return NULL;
Chris Pascoeaa501be2007-11-19 04:45:38 -0300963 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300964
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300965 priv->bandwidth = BANDWIDTH_6_MHZ;
966 priv->need_load_generic = 1;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300967 priv->mode = T_UNINITIALIZED;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300968 priv->i2c_props.addr = cfg->i2c_addr;
969 priv->i2c_props.adap = cfg->i2c_adap;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300970 priv->video_dev = video_dev;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300971 priv->tuner_callback = cfg->callback;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300972 priv->max_len = 13;
973
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300974 mutex_init(&priv->lock);
975
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300976 list_add_tail(&priv->xc2028_list, &xc2028_list);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300977 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300978
979 fe->tuner_priv = priv;
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -0300980 priv->count++;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300981
982 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300983 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300984
985 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -0300986
Chris Pascoeaa501be2007-11-19 04:45:38 -0300987 mutex_unlock(&xc2028_list_mutex);
988
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300989 return fe;
Michel Ludwig701672e2007-07-18 10:29:10 -0300990}
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300991
Michel Ludwig701672e2007-07-18 10:29:10 -0300992EXPORT_SYMBOL(xc2028_attach);
993
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300994MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -0300995MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300996MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
997MODULE_LICENSE("GPL");