blob: 0a2ffe4f38d0c45002edc9ac75d4997c7ffecea0 [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
Hans Verkuil9dd659d2007-11-04 11:03:36 -030025#define PREFIX "xc2028"
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030026
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030027static int debug;
28module_param(debug, int, 0644);
29MODULE_PARM_DESC(debug, "enable verbose debug messages");
30
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -030031static char audio_std[8];
32module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
33MODULE_PARM_DESC(audio_std,
34 "Audio standard. XC3028 audio decoder explicitly "
35 "needs to know what audio\n"
36 "standard is needed for some video standards with audio A2 or NICAM.\n"
37 "The valid values are:\n"
38 "A2\n"
39 "A2/A\n"
40 "A2/B\n"
41 "NICAM\n"
42 "NICAM/A\n"
43 "NICAM/B\n");
44
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030045static LIST_HEAD(xc2028_list);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030046/* struct for storing firmware table */
47struct firmware_description {
48 unsigned int type;
49 v4l2_std_id id;
50 unsigned char *ptr;
51 unsigned int size;
52};
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030053
54struct xc2028_data {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030055 struct list_head xc2028_list;
56 struct tuner_i2c_props i2c_props;
57 int (*tuner_callback) (void *dev,
58 int command, int arg);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030059 void *video_dev;
60 int count;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030061 __u32 frequency;
62
63 struct firmware_description *firm;
64 int firm_size;
65
66 __u16 version;
67
68 struct xc2028_ctrl ctrl;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030069
Michel Ludwig701672e2007-07-18 10:29:10 -030070 v4l2_std_id firm_type; /* video stds supported
71 by current firmware */
72 fe_bandwidth_t bandwidth; /* Firmware bandwidth:
73 6M, 7M or 8M */
74 int need_load_generic; /* The generic firmware
75 were loaded? */
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -030076
77 int max_len; /* Max firmware chunk */
78
Michel Ludwig701672e2007-07-18 10:29:10 -030079 enum tuner_mode mode;
80 struct i2c_client *i2c_client;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030081
82 struct mutex lock;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030083};
84
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -030085#define i2c_send(rc, priv, buf, size) do { \
86 rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
87 if (size != rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030088 tuner_err("i2c output error: rc = %d (should be %d)\n", \
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -030089 rc, (int)size); \
90} while (0)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030091
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -030092#define i2c_rcv(rc, priv, buf, size) do { \
93 rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
94 if (size != rc) \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -030095 tuner_err("i2c input error: rc = %d (should be %d)\n", \
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -030096 rc, (int)size); \
97} while (0)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030098
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -030099#define send_seq(priv, data...) do { \
100 int rc; \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300101 static u8 _val[] = data; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300102 if (sizeof(_val) != \
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300103 (rc = tuner_i2c_xfer_send(&priv->i2c_props, \
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300104 _val, sizeof(_val)))) { \
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300105 tuner_err("Error on line %d: %d\n", __LINE__, rc); \
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300106 return -EINVAL; \
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300107 } \
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300108 msleep(10); \
109} while (0)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300110
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300111static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300112{
113 int rc;
Mauro Carvalho Chehabb873e1a2007-11-05 08:41:50 -0300114 unsigned char buf[2];
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300115
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300116 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300117
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300118 buf[0] = reg>>8;
119 buf[1] = (unsigned char) reg;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300120
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300121 i2c_send(rc, priv, buf, 2);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300122 if (rc < 0)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300123 return rc;
124
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300125 i2c_rcv(rc, priv, buf, 2);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300126 if (rc < 0)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300127 return rc;
128
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300129 return (buf[1]) | (buf[0] << 8);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300130}
131
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300132void dump_firm_type(unsigned int type)
133{
134 if (type & BASE)
135 printk("BASE ");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300136 if (type & INIT1)
137 printk("INIT1 ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300138 if (type & F8MHZ)
139 printk("F8MHZ ");
140 if (type & MTS)
141 printk("MTS ");
142 if (type & D2620)
143 printk("D2620 ");
144 if (type & D2633)
145 printk("D2633 ");
146 if (type & DTV6)
147 printk("DTV6 ");
148 if (type & QAM)
149 printk("QAM ");
150 if (type & DTV7)
151 printk("DTV7 ");
152 if (type & DTV78)
153 printk("DTV78 ");
154 if (type & DTV8)
155 printk("DTV8 ");
156 if (type & FM)
157 printk("FM ");
158 if (type & INPUT1)
159 printk("INPUT1 ");
160 if (type & LCD)
161 printk("LCD ");
162 if (type & NOGD)
163 printk("NOGD ");
164 if (type & MONO)
165 printk("MONO ");
166 if (type & ATSC)
167 printk("ATSC ");
168 if (type & IF)
169 printk("IF ");
170 if (type & LG60)
171 printk("LG60 ");
172 if (type & ATI638)
173 printk("ATI638 ");
174 if (type & OREN538)
175 printk("OREN538 ");
176 if (type & OREN36)
177 printk("OREN36 ");
178 if (type & TOYOTA388)
179 printk("TOYOTA388 ");
180 if (type & TOYOTA794)
181 printk("TOYOTA794 ");
182 if (type & DIBCOM52)
183 printk("DIBCOM52 ");
184 if (type & ZARLINK456)
185 printk("ZARLINK456 ");
186 if (type & CHINA)
187 printk("CHINA ");
188 if (type & F6MHZ)
189 printk("F6MHZ ");
190 if (type & INPUT2)
191 printk("INPUT2 ");
192 if (type & SCODE)
193 printk("SCODE ");
194}
195
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300196static v4l2_std_id parse_audio_std_option(void)
197{
198 if (strcasecmp(audio_std, "A2"))
199 return V4L2_STD_A2;
200 if (strcasecmp(audio_std, "A2/A"))
201 return V4L2_STD_A2_A;
202 if (strcasecmp(audio_std, "A2/B"))
203 return V4L2_STD_A2_B;
204 if (strcasecmp(audio_std, "NICAM"))
205 return V4L2_STD_NICAM;
206 if (strcasecmp(audio_std, "NICAM/A"))
207 return V4L2_STD_NICAM_A;
208 if (strcasecmp(audio_std, "NICAM/B"))
209 return V4L2_STD_NICAM_B;
210
211 return 0;
212}
213
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300214static void free_firmware(struct xc2028_data *priv)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300215{
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300216 int i;
217
218 if (!priv->firm)
219 return;
220
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300221 for (i = 0; i < priv->firm_size; i++)
222 kfree(priv->firm[i].ptr);
223
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300224 kfree(priv->firm);
225
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300226 priv->firm = NULL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300227 priv->need_load_generic = 1;
228}
229
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300230static int load_all_firmwares(struct dvb_frontend *fe)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300231{
232 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300233 const struct firmware *fw = NULL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300234 unsigned char *p, *endp;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300235 int rc = 0;
236 int n, n_array;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300237 char name[33];
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300238
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300239 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300240
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300241 tuner_info("Reading firmware %s\n", priv->ctrl.fname);
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300242 rc = request_firmware(&fw, priv->ctrl.fname,
243 &priv->i2c_props.adap->dev);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300244 if (rc < 0) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300245 if (rc == -ENOENT)
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300246 tuner_err("Error: firmware %s not found.\n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300247 priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300248 else
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300249 tuner_err("Error %d while requesting firmware %s \n",
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300250 rc, priv->ctrl.fname);
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300251
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300252 return rc;
253 }
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300254 p = fw->data;
255 endp = p + fw->size;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300256
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300257 if (fw->size < sizeof(name) - 1 + 2) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300258 tuner_err("Error: firmware size is zero!\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300259 rc = -EINVAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300260 goto done;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300261 }
262
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300263 memcpy(name, p, sizeof(name) - 1);
264 name[sizeof(name) - 1] = 0;
265 p += sizeof(name) - 1;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300266
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300267 priv->version = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300268 p += 2;
269
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300270 tuner_info("Firmware: %s, ver %d.%d\n", name,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300271 priv->version >> 8, priv->version & 0xff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300272
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300273 if (p + 2 > endp)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300274 goto corrupt;
275
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300276 n_array = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300277 p += 2;
278
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300279 tuner_info("There are %d firmwares at %s\n",
280 n_array, priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300281
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300282 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300283
284 if (!fw) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300285 tuner_err("Not enough memory for reading firmware.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300286 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300287 goto done;
288 }
289
290 priv->firm_size = n_array;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300291 n = -1;
292 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300293 __u32 type, size;
294 v4l2_std_id id;
295
296 n++;
297 if (n >= n_array) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300298 tuner_err("Too much firmwares at the file\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300299 goto corrupt;
300 }
301
302 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300303 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300304 tuner_err("Firmware header is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300305 goto corrupt;
306 }
307
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300308 type = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300309 p += sizeof(type);
310
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300311 id = le64_to_cpu(*(v4l2_std_id *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300312 p += sizeof(id);
313
Michel Ludwig2fc580f2007-11-16 07:19:35 -0300314 size = le32_to_cpu(*(__u32 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300315 p += sizeof(size);
316
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300317 if ((!size) || (size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300318 tuner_err("Firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300319 dump_firm_type(type);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300320 printk("(%x), id %lx is corrupted "
321 "(size=%ld, expected %d)\n",
322 type, (unsigned long)id, endp - p, size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300323 goto corrupt;
324 }
325
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300326 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300327 if (!priv->firm[n].ptr) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300328 tuner_err("Not enough memory.\n");
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300329 rc = -ENOMEM;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300330 goto err;
331 }
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300332 tuner_info("Reading firmware type ");
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300333 dump_firm_type(type);
334 printk("(%x), id %lx, size=%d.\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300335 type, (unsigned long)id, size);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300336
337 memcpy(priv->firm[n].ptr, p, size);
338 priv->firm[n].type = type;
339 priv->firm[n].id = id;
340 priv->firm[n].size = size;
341
342 p += size;
343 }
344
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300345 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300346 tuner_err("Firmware file is incomplete!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300347 goto corrupt;
348 }
349
350 goto done;
351
352corrupt:
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300353 rc = -EINVAL;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300354 tuner_err("Error: firmware file is corrupted!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300355
356err:
357 tuner_info("Releasing loaded firmware file.\n");
358
359 free_firmware(priv);
360
361done:
362 release_firmware(fw);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300363 tuner_dbg("Firmware files loaded.\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300364
365 return rc;
366}
367
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300368static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
369 v4l2_std_id *id)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300370{
371 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300372 int i;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300373
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300374 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300375
376 if (!priv->firm) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300377 tuner_err("Error! firmware not loaded\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300378 return -EINVAL;
379 }
380
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300381 if (((type & ~SCODE) == 0) && (*id == 0))
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300382 *id = V4L2_STD_PAL;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300383
384 /* Seek for exact match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300385 for (i = 0; i < priv->firm_size; i++) {
386 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300387 goto found;
388 }
389
390 /* Seek for generic video standard match */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300391 for (i = 0; i < priv->firm_size; i++) {
392 if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300393 goto found;
394 }
395
396 /*FIXME: Would make sense to seek for type "hint" match ? */
397
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300398 i = -EINVAL;
399 goto ret;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300400
401found:
402 *id = priv->firm[i].id;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300403
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300404ret:
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300405 tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
406 if (debug) {
407 dump_firm_type(type);
408 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
409 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300410 return i;
411}
412
413static int load_firmware(struct dvb_frontend *fe, unsigned int type,
414 v4l2_std_id *id)
415{
416 struct xc2028_data *priv = fe->tuner_priv;
417 int pos, rc;
418 unsigned char *p, *endp, buf[priv->max_len];
419
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300420 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300421
422 pos = seek_firmware(fe, type, id);
423 if (pos < 0)
424 return pos;
425
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300426 tuner_info("Loading firmware for type=");
427 dump_firm_type(type);
428 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
429
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300430 p = priv->firm[pos].ptr;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300431
432 if (!p) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300433 tuner_err("Firmware pointer were freed!");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300434 return -EINVAL;
435 }
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300436 endp = p + priv->firm[pos].size;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300437
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300438 while (p < endp) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300439 __u16 size;
440
441 /* Checks if there's enough bytes to read */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300442 if (p + sizeof(size) > endp) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300443 tuner_err("Firmware chunk size is wrong\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300444 return -EINVAL;
445 }
446
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300447 size = le16_to_cpu(*(__u16 *) p);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300448 p += sizeof(size);
449
450 if (size == 0xffff)
451 return 0;
452
453 if (!size) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300454 /* Special callback command received */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300455 rc = priv->tuner_callback(priv->video_dev,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300456 XC2028_TUNER_RESET, 0);
457 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300458 tuner_err("Error at RESET code %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300459 (*p) & 0x7f);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300460 return -EINVAL;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300461 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300462 continue;
463 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300464
465 /* Checks for a sleep command */
466 if (size & 0x8000) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300467 msleep(size & 0x7fff);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300468 continue;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300469 }
470
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300471 if ((size + p > endp)) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300472 tuner_err("missing bytes: need %d, have %d\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300473 size, (int)(endp - p));
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300474 return -EINVAL;
475 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300476
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300477 buf[0] = *p;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300478 p++;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300479 size--;
480
481 /* Sends message chunks */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300482 while (size > 0) {
483 int len = (size < priv->max_len - 1) ?
484 size : priv->max_len - 1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300485
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300486 memcpy(buf + 1, p, len);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300487
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300488 i2c_send(rc, priv, buf, len + 1);
489 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300490 tuner_err("%d returned from send\n", rc);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300491 return -EINVAL;
492 }
493
494 p += len;
495 size -= len;
496 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300497 }
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300498 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300499}
500
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300501static int load_scode(struct dvb_frontend *fe, unsigned int type,
502 v4l2_std_id *id, int scode)
503{
504 struct xc2028_data *priv = fe->tuner_priv;
505 int pos, rc;
506 unsigned char *p;
507
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300508 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300509
510 pos = seek_firmware(fe, type, id);
511 if (pos < 0)
512 return pos;
513
514 p = priv->firm[pos].ptr;
515
516 if (!p) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300517 tuner_err("Firmware pointer were freed!");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300518 return -EINVAL;
519 }
520
521 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
522 return -EINVAL;
523
524 if (priv->version < 0x0202) {
525 send_seq(priv, {0x20, 0x00, 0x00, 0x00});
526 } else {
527 send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
528 }
529
530 i2c_send(rc, priv, p + 12 * scode, 12);
531
532 send_seq(priv, {0x00, 0x8c});
533
534 return 0;
535}
536
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300537static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300538 v4l2_std_id std, fe_bandwidth_t bandwidth)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300539{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300540 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300541 int rc, version, hwmodel;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300542 v4l2_std_id std0 = 0;
543 unsigned int type0 = 0, type = 0;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300544 int change_digital_bandwidth;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300545
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300546 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300547
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300548 if (!priv->firm) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300549 if (!priv->ctrl.fname) {
550 tuner_info("xc2028/3028 firmware name not set!\n");
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300551 return -EINVAL;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300552 }
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300553
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300554 rc = load_all_firmwares(fe);
555 if (rc < 0)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300556 return rc;
557 }
558
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300559 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300560 priv->mode, new_mode);
Michel Ludwig701672e2007-07-18 10:29:10 -0300561
562 /* first of all, determine whether we have switched the mode */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300563 if (new_mode != priv->mode) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300564 priv->mode = new_mode;
565 priv->need_load_generic = 1;
Michel Ludwig701672e2007-07-18 10:29:10 -0300566 }
567
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300568 change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300569 && bandwidth != priv->bandwidth) ? 1 : 0;
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300570 tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300571 bandwidth);
Michel Ludwig701672e2007-07-18 10:29:10 -0300572
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300573 if (priv->need_load_generic) {
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300574 /* Reset is needed before loading firmware */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300575 rc = priv->tuner_callback(priv->video_dev,
576 XC2028_TUNER_RESET, 0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300577 if (rc < 0)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300578 return rc;
579
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300580 type0 = BASE;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300581
582 if (priv->ctrl.type == XC2028_FIRM_MTS)
583 type0 |= MTS;
584
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300585 if (priv->bandwidth == 8)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300586 type0 |= F8MHZ;
587
588 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
589
590 rc = load_firmware(fe, type0, &std0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300591 if (rc < 0) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300592 tuner_err("Error %d while loading generic firmware\n",
593 rc);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300594 return rc;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300595 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300596
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300597 priv->need_load_generic = 0;
598 priv->firm_type = 0;
599 if (priv->mode == T_DIGITAL_TV)
600 change_digital_bandwidth = 1;
Michel Ludwig701672e2007-07-18 10:29:10 -0300601 }
602
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300603 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
Michel Ludwig701672e2007-07-18 10:29:10 -0300604
605 if (change_digital_bandwidth) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300606
607 /*FIXME: Should allow selecting between D2620 and D2633 */
608 type |= D2620;
609
610 /* FIXME: When should select a DTV78 firmware?
611 */
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300612 switch (bandwidth) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300613 case BANDWIDTH_8_MHZ:
614 type |= DTV8;
615 break;
616 case BANDWIDTH_7_MHZ:
617 type |= DTV7;
618 break;
619 case BANDWIDTH_6_MHZ:
620 /* FIXME: Should allow select also ATSC */
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300621 type |= DTV6 | QAM;
Michel Ludwig701672e2007-07-18 10:29:10 -0300622 break;
623
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300624 default:
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300625 tuner_err("error: bandwidth not supported.\n");
Michel Ludwig701672e2007-07-18 10:29:10 -0300626 };
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300627 priv->bandwidth = bandwidth;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300628 }
629
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300630 /* Load INIT1, if needed */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300631 tuner_dbg("Load init1 firmware, if exists\n");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300632 type0 = BASE | INIT1;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300633 if (priv->ctrl.type == XC2028_FIRM_MTS)
634 type0 |= MTS;
635
636 /* FIXME: Should handle errors - if INIT1 found */
637 rc = load_firmware(fe, type0, &std0);
638
639 /* FIXME: Should add support for FM radio
640 */
641
642 if (priv->ctrl.type == XC2028_FIRM_MTS)
643 type |= MTS;
644
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300645 if (priv->firm_type & std) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300646 tuner_dbg("Std-specific firmware already loaded.\n");
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300647 return 0;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300648 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300649
Mauro Carvalho Chehaba82200f2007-11-15 11:58:00 -0300650 /* Add audio hack to std mask */
651 std |= parse_audio_std_option();
652
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300653 rc = load_firmware(fe, type, &std);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300654 if (rc < 0)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300655 return rc;
656
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300657 /* Load SCODE firmware, if exists */
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300658 tuner_dbg("Trying to load scode 0\n");
Mauro Carvalho Chehabf380e1d2007-11-15 08:43:53 -0300659 type |= SCODE;
660
661 rc = load_scode(fe, type, &std, 0);
Mauro Carvalho Chehab43efe702007-11-14 19:30:28 -0300662
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300663 version = xc2028_get_reg(priv, 0x0004);
664 hwmodel = xc2028_get_reg(priv, 0x0008);
665
666 tuner_info("Device is Xceive %d version %d.%d, "
667 "firmware version %d.%d\n",
668 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
669 (version & 0xf0) >> 4, version & 0xf);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300670
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300671 priv->firm_type = std;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300672
673 return 0;
674}
675
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300676static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300677{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300678 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300679 int frq_lock, signal = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300680
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300681 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300682
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300683 mutex_lock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300684
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300685 *strength = 0;
686
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300687 /* Sync Lock Indicator */
688 frq_lock = xc2028_get_reg(priv, 0x0002);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300689 if (frq_lock <= 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300690 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300691
692 /* Frequency is locked. Return signal quality */
693
Mauro Carvalho Chehab80b52202007-11-05 09:07:13 -0300694 /* Get SNR of the video signal */
695 signal = xc2028_get_reg(priv, 0x0040);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300696
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300697 if (signal <= 0)
698 signal = frq_lock;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300699
700ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300701 mutex_unlock(&priv->lock);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300702
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300703 *strength = signal;
704
705 return 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300706}
707
708#define DIV 15625
709
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300710static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
711 enum tuner_mode new_mode,
712 v4l2_std_id std, fe_bandwidth_t bandwidth)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300713{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300714 struct xc2028_data *priv = fe->tuner_priv;
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300715 int rc = -EINVAL;
716 unsigned char buf[5];
717 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300718
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300719 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300720
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300721 mutex_lock(&priv->lock);
722
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300723 /* HACK: It seems that specific firmware need to be reloaded
724 when freq is changed */
Michel Ludwig701672e2007-07-18 10:29:10 -0300725
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300726 priv->firm_type = 0;
Michel Ludwig701672e2007-07-18 10:29:10 -0300727
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300728 /* Reset GPIO 1 */
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300729 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300730 if (rc < 0)
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300731 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300732
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300733 msleep(10);
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300734 tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300735
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300736 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300737 goto ret;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300738
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300739 if (new_mode == T_DIGITAL_TV)
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300740 offset = 2750000;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300741
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300742 div = (freq - offset + DIV / 2) / DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300743
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300744 /* CMD= Set frequency */
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300745
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300746 if (priv->version < 0x0202) {
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300747 send_seq(priv, {0x00, 0x02, 0x00, 0x00});
748 } else {
749 send_seq(priv, {0x80, 0x02, 0x00, 0x00});
750 }
751
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300752 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300753 if (rc < 0)
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300754 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300755
756 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300757
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300758 buf[0] = 0xff & (div >> 24);
759 buf[1] = 0xff & (div >> 16);
760 buf[2] = 0xff & (div >> 8);
761 buf[3] = 0xff & (div);
762 buf[4] = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300763
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300764 i2c_send(rc, priv, buf, sizeof(buf));
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300765 if (rc < 0)
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300766 goto ret;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300767 msleep(100);
768
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300769 priv->frequency = freq;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300770
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300771 tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300772 buf[1], buf[2], buf[3], buf[4],
773 freq / 1000000, (freq % 1000000) / 10000);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300774
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300775 rc = 0;
Mauro Carvalho Chehab3b205322007-09-27 18:27:03 -0300776
777ret:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300778 mutex_unlock(&priv->lock);
779
780 return rc;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300781}
782
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300783static int xc2028_set_tv_freq(struct dvb_frontend *fe,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300784 struct analog_parameters *p)
Michel Ludwig701672e2007-07-18 10:29:10 -0300785{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300786 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -0300787
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300788 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300789
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300790 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
791 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
Michel Ludwig701672e2007-07-18 10:29:10 -0300792}
793
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300794static int xc2028_set_params(struct dvb_frontend *fe,
Michel Ludwig701672e2007-07-18 10:29:10 -0300795 struct dvb_frontend_parameters *p)
796{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300797 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -0300798
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300799 tuner_dbg("%s called\n", __FUNCTION__);
Michel Ludwig701672e2007-07-18 10:29:10 -0300800
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300801 /* FIXME: Only OFDM implemented */
802 if (fe->ops.info.type != FE_OFDM) {
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300803 tuner_err("DTV type not implemented.\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300804 return -EINVAL;
805 }
Michel Ludwig701672e2007-07-18 10:29:10 -0300806
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300807 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300808 0 /* NOT USED */,
809 p->u.ofdm.bandwidth);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300810
Michel Ludwig701672e2007-07-18 10:29:10 -0300811}
812
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300813static int xc2028_dvb_release(struct dvb_frontend *fe)
Michel Ludwig701672e2007-07-18 10:29:10 -0300814{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300815 struct xc2028_data *priv = fe->tuner_priv;
Michel Ludwig701672e2007-07-18 10:29:10 -0300816
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300817 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300818
819 priv->count--;
820
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300821 if (!priv->count) {
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -0300822 list_del(&priv->xc2028_list);
823
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300824 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300825
826 free_firmware(priv);
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300827 kfree(priv);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300828 }
Michel Ludwig701672e2007-07-18 10:29:10 -0300829
830 return 0;
831}
832
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300833static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Michel Ludwig701672e2007-07-18 10:29:10 -0300834{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300835 struct xc2028_data *priv = fe->tuner_priv;
836
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300837 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300838
839 *frequency = priv->frequency;
Michel Ludwig701672e2007-07-18 10:29:10 -0300840
841 return 0;
842}
843
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300844static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300845{
846 struct xc2028_data *priv = fe->tuner_priv;
847 struct xc2028_ctrl *p = priv_cfg;
848
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300849 tuner_dbg("%s called\n", __FUNCTION__);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300850
851 priv->ctrl.type = p->type;
852
853 if (p->fname) {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300854 kfree(priv->ctrl.fname);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300855
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300856 priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300857 if (!priv->ctrl.fname)
858 return -ENOMEM;
859
860 free_firmware(priv);
861 strcpy(priv->ctrl.fname, p->fname);
862 }
863
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300864 if (p->max_len > 0)
Mauro Carvalho Chehab352fae12007-11-01 16:56:26 -0300865 priv->max_len = p->max_len;
866
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300867 return 0;
868}
869
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300870static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
Michel Ludwig701672e2007-07-18 10:29:10 -0300871 .info = {
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300872 .name = "Xceive XC3028",
873 .frequency_min = 42000000,
874 .frequency_max = 864000000,
875 .frequency_step = 50000,
876 },
Michel Ludwig701672e2007-07-18 10:29:10 -0300877
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300878 .set_config = xc2028_set_config,
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300879 .set_analog_params = xc2028_set_tv_freq,
880 .release = xc2028_dvb_release,
881 .get_frequency = xc2028_get_frequency,
882 .get_rf_strength = xc2028_signal,
883 .set_params = xc2028_set_params,
Michel Ludwig701672e2007-07-18 10:29:10 -0300884
Michel Ludwig701672e2007-07-18 10:29:10 -0300885};
886
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300887void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
Michel Ludwig701672e2007-07-18 10:29:10 -0300888{
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300889 struct xc2028_data *priv;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300890 void *video_dev;
Michel Ludwig701672e2007-07-18 10:29:10 -0300891
Mauro Carvalho Chehab83fb3402007-11-15 09:44:30 -0300892 if (debug)
893 printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300894
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300895 if (NULL == cfg->video_dev)
896 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300897
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300898 if (!fe) {
899 printk(KERN_ERR PREFIX "No frontend!\n");
900 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300901 }
902
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300903 video_dev = cfg->video_dev;
904
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300905 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300906 if (priv->video_dev == cfg->video_dev) {
907 video_dev = NULL;
908 break;
909 }
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300910 }
911
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300912 if (video_dev) {
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300913 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
914 if (priv == NULL)
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300915 return NULL;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300916
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300917 priv->bandwidth = BANDWIDTH_6_MHZ;
918 priv->need_load_generic = 1;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300919 priv->mode = T_UNINITIALIZED;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300920 priv->i2c_props.addr = cfg->i2c_addr;
921 priv->i2c_props.adap = cfg->i2c_adap;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300922 priv->video_dev = video_dev;
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300923 priv->tuner_callback = cfg->callback;
Mauro Carvalho Chehabde3fe212007-10-24 09:22:08 -0300924 priv->max_len = 13;
925
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300926 mutex_init(&priv->lock);
927
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300928 list_add_tail(&priv->xc2028_list, &xc2028_list);
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300929 }
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300930
931 fe->tuner_priv = priv;
Mauro Carvalho Chehab1808a692007-10-29 17:38:59 -0300932 priv->count++;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300933
934 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
Mauro Carvalho Chehabab0b9fc2007-11-01 17:47:42 -0300935 sizeof(xc2028_dvb_tuner_ops));
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300936
937 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
Michel Ludwig701672e2007-07-18 10:29:10 -0300938
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300939 return fe;
Michel Ludwig701672e2007-07-18 10:29:10 -0300940}
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300941
Michel Ludwig701672e2007-07-18 10:29:10 -0300942EXPORT_SYMBOL(xc2028_attach);
943
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300944MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
Mauro Carvalho Chehab983d2142007-10-29 23:44:18 -0300945MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300946MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
947MODULE_LICENSE("GPL");