1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/i2c.h>
25#include <linux/types.h>
26#include <linux/videodev.h>
27#include <linux/init.h>
28#include <linux/errno.h>
29#include <linux/slab.h>
30#include <linux/poll.h>
31#include <linux/wait.h>
32#include <asm/uaccess.h>
33
34
35#include <media/rds.h>
36
37
38static unsigned short normal_i2c[] = {
39 0x20 >> 1,
40 0x22 >> 1,
41 I2C_CLIENT_END,
42};
43
44I2C_CLIENT_INSMOD;
45
46
47static unsigned int debug = 0;
48static unsigned int xtal = 0;
49static unsigned int rbds = 0;
50static unsigned int plvl = 0;
51static unsigned int bufblocks = 100;
52
53module_param(debug, int, 0644);
54MODULE_PARM_DESC(debug, "enable debug messages");
55module_param(xtal, int, 0);
56MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
57module_param(rbds, int, 0);
58MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0");
59module_param(plvl, int, 0);
60MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
61module_param(bufblocks, int, 0);
62MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
63
64MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
65MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
66
67MODULE_LICENSE("GPL");
68
69
70
71#define UNSET (-1U)
72#define PREFIX "saa6588: "
73#define dprintk if (debug) printk
74
75struct saa6588 {
76 struct i2c_client client;
77 struct work_struct work;
78 struct timer_list timer;
79 spinlock_t lock;
80 unsigned char *buffer;
81 unsigned int buf_size;
82 unsigned int rd_index;
83 unsigned int wr_index;
84 unsigned int block_count;
85 unsigned char last_blocknum;
86 wait_queue_head_t read_queue;
87 int data_available_for_read;
88};
89
90static struct i2c_driver driver;
91static struct i2c_client client_template;
92
93
94
95
96
97
98
99
100
101
102#define cModeStandard 0x00
103#define cModeFastPI 0x01
104#define cModeReducedRequest 0x02
105#define cModeInvalid 0x03
106
107
108#define cProcessingModeRDS 0x00
109#define cProcessingModeRBDS 0x04
110
111
112#define cErrCorrectionNone 0x00
113#define cErrCorrection2Bits 0x08
114#define cErrCorrection5Bits 0x10
115#define cErrCorrectionNoneRBDS 0x18
116
117
118#define cSyncNormal 0x00
119#define cSyncRestart 0x20
120
121
122#define cSigQualityDetectOFF 0x00
123#define cSigQualityDetectON 0x40
124
125
126#define cSigQualityTriggered 0x00
127#define cSigQualityContinous 0x80
128
129
130
131
132#define cFlywheelMaxBlocksMask 0x3F
133#define cFlywheelDefault 0x20
134
135
136#define cPauseLevel_11mV 0x00
137#define cPauseLevel_17mV 0x40
138#define cPauseLevel_27mV 0x80
139#define cPauseLevel_43mV 0xC0
140
141
142
143
144#define cQualityDetectSensMask 0x1F
145#define cQualityDetectDefault 0x0F
146
147
148#define cSelectOscFreqOFF 0x00
149#define cSelectOscFreqON 0x20
150
151
152#define cOscFreq_4332kHz 0x00
153#define cOscFreq_8664kHz 0x40
154#define cOscFreq_12996kHz 0x80
155#define cOscFreq_17328kHz 0xC0
156
157
158
159static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf)
160{
161 int i;
162
163 if (s->rd_index == s->wr_index) {
164 if (debug > 2)
165 dprintk(PREFIX "Read: buffer empty.\n");
166 return 0;
167 }
168
169 if (debug > 2) {
170 dprintk(PREFIX "Read: ");
171 for (i = s->rd_index; i < s->rd_index + 3; i++)
172 dprintk("0x%02x ", s->buffer[i]);
173 }
174
175 if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
176 return -EFAULT;
177
178 s->rd_index += 3;
179 if (s->rd_index >= s->buf_size)
180 s->rd_index = 0;
181 s->block_count--;
182
183 if (debug > 2)
184 dprintk("%d blocks total.\n", s->block_count);
185
186 return 1;
187}
188
189static void read_from_buf(struct saa6588 *s, struct rds_command *a)
190{
191 unsigned long flags;
192
193 unsigned char __user *buf_ptr = a->buffer;
194 unsigned int i;
195 unsigned int rd_blocks;
196
197 a->result = 0;
198 if (!a->buffer)
199 return;
200
201 while (!s->data_available_for_read) {
202 int ret = wait_event_interruptible(s->read_queue,
203 s->data_available_for_read);
204 if (ret == -ERESTARTSYS) {
205 a->result = -EINTR;
206 return;
207 }
208 }
209
210 spin_lock_irqsave(&s->lock, flags);
211 rd_blocks = a->block_count;
212 if (rd_blocks > s->block_count)
213 rd_blocks = s->block_count;
214
215 if (!rd_blocks) {
216 spin_unlock_irqrestore(&s->lock, flags);
217 return;
218 }
219
220 for (i = 0; i < rd_blocks; i++) {
221 if (block_to_user_buf(s, buf_ptr)) {
222 buf_ptr += 3;
223 a->result++;
224 } else
225 break;
226 }
227 a->result *= 3;
228 s->data_available_for_read = (s->block_count > 0);
229 spin_unlock_irqrestore(&s->lock, flags);
230}
231
232static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
233{
234 unsigned int i;
235
236 if (debug > 3)
237 dprintk(PREFIX "New block: ");
238
239 for (i = 0; i < 3; ++i) {
240 if (debug > 3)
241 dprintk("0x%02x ", blockbuf[i]);
242 s->buffer[s->wr_index] = blockbuf[i];
243 s->wr_index++;
244 }
245
246 if (s->wr_index >= s->buf_size)
247 s->wr_index = 0;
248
249 if (s->wr_index == s->rd_index) {
250 s->rd_index += 3;
251 if (s->rd_index >= s->buf_size)
252 s->rd_index = 0;
253 } else
254 s->block_count++;
255
256 if (debug > 3)
257 dprintk("%d blocks total.\n", s->block_count);
258}
259
260static void saa6588_i2c_poll(struct saa6588 *s)
261{
262 unsigned long flags;
263 unsigned char tmpbuf[6];
264 unsigned char blocknum;
265 unsigned char tmp;
266
267
268
269 if (6 != i2c_master_recv(&s->client, &tmpbuf[0], 6)) {
270 if (debug > 1)
271 dprintk(PREFIX "read error!\n");
272 return;
273 }
274
275 blocknum = tmpbuf[0] >> 5;
276 if (blocknum == s->last_blocknum) {
277 if (debug > 3)
278 dprintk("Saw block %d again.\n", blocknum);
279 return;
280 }
281
282 s->last_blocknum = blocknum;
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 tmp = tmpbuf[2];
302 tmpbuf[2] = tmpbuf[0];
303 tmpbuf[0] = tmp;
304
305 tmp = blocknum;
306 tmp |= blocknum << 3;
307 if ((tmpbuf[2] & 0x03) == 0x03)
308 tmp |= 0x80;
309 else if ((tmpbuf[2] & 0x03) != 0x00)
310 tmp |= 0x40;
311 tmpbuf[2] = tmp;
312
313 spin_lock_irqsave(&s->lock, flags);
314 block_to_buf(s, tmpbuf);
315 spin_unlock_irqrestore(&s->lock, flags);
316 s->data_available_for_read = 1;
317 wake_up_interruptible(&s->read_queue);
318}
319
320static void saa6588_timer(unsigned long data)
321{
322 struct saa6588 *s = (struct saa6588 *)data;
323
324 schedule_work(&s->work);
325}
326
327static void saa6588_work(struct work_struct *work)
328{
329 struct saa6588 *s = container_of(work, struct saa6588, work);
330
331 saa6588_i2c_poll(s);
332 mod_timer(&s->timer, jiffies + msecs_to_jiffies(20));
333}
334
335static int saa6588_configure(struct saa6588 *s)
336{
337 unsigned char buf[3];
338 int rc;
339
340 buf[0] = cSyncRestart;
341 if (rbds)
342 buf[0] |= cProcessingModeRBDS;
343
344 buf[1] = cFlywheelDefault;
345 switch (plvl) {
346 case 0:
347 buf[1] |= cPauseLevel_11mV;
348 break;
349 case 1:
350 buf[1] |= cPauseLevel_17mV;
351 break;
352 case 2:
353 buf[1] |= cPauseLevel_27mV;
354 break;
355 case 3:
356 buf[1] |= cPauseLevel_43mV;
357 break;
358 default:
359 break;
360 }
361
362 buf[2] = cQualityDetectDefault | cSelectOscFreqON;
363
364 switch (xtal) {
365 case 0:
366 buf[2] |= cOscFreq_4332kHz;
367 break;
368 case 1:
369 buf[2] |= cOscFreq_8664kHz;
370 break;
371 case 2:
372 buf[2] |= cOscFreq_12996kHz;
373 break;
374 case 3:
375 buf[2] |= cOscFreq_17328kHz;
376 break;
377 default:
378 break;
379 }
380
381 dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
382 buf[0], buf[1], buf[2]);
383
384 if (3 != (rc = i2c_master_send(&s->client, buf, 3)))
385 printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
386
387 return 0;
388}
389
390
391
392static int saa6588_attach(struct i2c_adapter *adap, int addr, int kind)
393{
394 struct saa6588 *s;
395 client_template.adapter = adap;
396 client_template.addr = addr;
397
398 printk(PREFIX "chip found @ 0x%x\n", addr << 1);
399
400 if (NULL == (s = kmalloc(sizeof(*s), GFP_KERNEL)))
401 return -ENOMEM;
402
403 s->buf_size = bufblocks * 3;
404
405 if (NULL == (s->buffer = kmalloc(s->buf_size, GFP_KERNEL))) {
406 kfree(s);
407 return -ENOMEM;
408 }
409 spin_lock_init(&s->lock);
410 s->client = client_template;
411 s->block_count = 0;
412 s->wr_index = 0;
413 s->rd_index = 0;
414 s->last_blocknum = 0xff;
415 init_waitqueue_head(&s->read_queue);
416 s->data_available_for_read = 0;
417 i2c_set_clientdata(&s->client, s);
418 i2c_attach_client(&s->client);
419
420 saa6588_configure(s);
421
422
423 INIT_WORK(&s->work, saa6588_work);
424 init_timer(&s->timer);
425 s->timer.function = saa6588_timer;
426 s->timer.data = (unsigned long)s;
427 schedule_work(&s->work);
428 return 0;
429}
430
431static int saa6588_probe(struct i2c_adapter *adap)
432{
433 if (adap->class & I2C_CLASS_TV_ANALOG)
434 return i2c_probe(adap, &addr_data, saa6588_attach);
435 return 0;
436}
437
438static int saa6588_detach(struct i2c_client *client)
439{
440 struct saa6588 *s = i2c_get_clientdata(client);
441
442 del_timer_sync(&s->timer);
443 flush_scheduled_work();
444
445 i2c_detach_client(client);
446 kfree(s->buffer);
447 kfree(s);
448 return 0;
449}
450
451static int saa6588_command(struct i2c_client *client, unsigned int cmd,
452 void *arg)
453{
454 struct saa6588 *s = i2c_get_clientdata(client);
455 struct rds_command *a = (struct rds_command *)arg;
456
457 switch (cmd) {
458
459 case RDS_CMD_OPEN:
460 a->result = 0;
461 break;
462
463 case RDS_CMD_CLOSE:
464 s->data_available_for_read = 1;
465 wake_up_interruptible(&s->read_queue);
466 a->result = 0;
467 break;
468
469 case RDS_CMD_READ:
470 read_from_buf(s, a);
471 break;
472
473 case RDS_CMD_POLL:
474 a->result = 0;
475 if (s->data_available_for_read) {
476 a->result |= POLLIN | POLLRDNORM;
477 }
478 poll_wait(a->instance, &s->read_queue, a->event_list);
479 break;
480
481 default:
482
483 break;
484 }
485 return 0;
486}
487
488
489
490static struct i2c_driver driver = {
491 .driver = {
492 .name = "saa6588",
493 },
494 .id = -1,
495 .attach_adapter = saa6588_probe,
496 .detach_client = saa6588_detach,
497 .command = saa6588_command,
498};
499
500static struct i2c_client client_template = {
501 .name = "saa6588",
502 .driver = &driver,
503};
504
505static int __init saa6588_init_module(void)
506{
507 return i2c_add_driver(&driver);
508}
509
510static void __exit saa6588_cleanup_module(void)
511{
512 i2c_del_driver(&driver);
513}
514
515module_init(saa6588_init_module);
516module_exit(saa6588_cleanup_module);
517
518
519
520
521
522
523
524
525