Commit | Line | Data |
---|---|---|
10b89ee3 MCC |
1 | /* |
2 | Driver for SAA6588 RDS decoder | |
3 | ||
4 | (c) 2005 Hans J. Koch | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
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 | ||
fa3fcceb | 34 | #include <media/rds.h> |
b5ffc223 HV |
35 | #include <media/v4l2-common.h> |
36 | #include <media/v4l2-i2c-drv-legacy.h> | |
10b89ee3 MCC |
37 | |
38 | /* Addresses to scan */ | |
39 | static unsigned short normal_i2c[] = { | |
40 | 0x20 >> 1, | |
41 | 0x22 >> 1, | |
42 | I2C_CLIENT_END, | |
43 | }; | |
44 | ||
45 | I2C_CLIENT_INSMOD; | |
46 | ||
47 | /* insmod options */ | |
ff699e6b DSL |
48 | static unsigned int debug; |
49 | static unsigned int xtal; | |
50 | static unsigned int rbds; | |
51 | static unsigned int plvl; | |
10b89ee3 MCC |
52 | static unsigned int bufblocks = 100; |
53 | ||
9b565eb7 | 54 | module_param(debug, int, 0644); |
10b89ee3 | 55 | MODULE_PARM_DESC(debug, "enable debug messages"); |
9b565eb7 | 56 | module_param(xtal, int, 0); |
10b89ee3 | 57 | MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0"); |
9b565eb7 | 58 | module_param(rbds, int, 0); |
10b89ee3 | 59 | MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0"); |
9b565eb7 | 60 | module_param(plvl, int, 0); |
10b89ee3 | 61 | MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0"); |
9b565eb7 | 62 | module_param(bufblocks, int, 0); |
10b89ee3 MCC |
63 | MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100"); |
64 | ||
65 | MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder"); | |
66 | MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>"); | |
67 | ||
68 | MODULE_LICENSE("GPL"); | |
69 | ||
70 | /* ---------------------------------------------------------------------- */ | |
71 | ||
72 | #define UNSET (-1U) | |
73 | #define PREFIX "saa6588: " | |
74 | #define dprintk if (debug) printk | |
75 | ||
76 | struct saa6588 { | |
b5ffc223 | 77 | struct i2c_client *client; |
10b89ee3 MCC |
78 | struct work_struct work; |
79 | struct timer_list timer; | |
80 | spinlock_t lock; | |
81 | unsigned char *buffer; | |
82 | unsigned int buf_size; | |
83 | unsigned int rd_index; | |
84 | unsigned int wr_index; | |
85 | unsigned int block_count; | |
86 | unsigned char last_blocknum; | |
87 | wait_queue_head_t read_queue; | |
88 | int data_available_for_read; | |
89 | }; | |
90 | ||
10b89ee3 MCC |
91 | /* ---------------------------------------------------------------------- */ |
92 | ||
93 | /* | |
94 | * SAA6588 defines | |
95 | */ | |
96 | ||
97 | /* Initialization and mode control byte (0w) */ | |
98 | ||
99 | /* bit 0+1 (DAC0/DAC1) */ | |
100 | #define cModeStandard 0x00 | |
101 | #define cModeFastPI 0x01 | |
102 | #define cModeReducedRequest 0x02 | |
103 | #define cModeInvalid 0x03 | |
104 | ||
105 | /* bit 2 (RBDS) */ | |
106 | #define cProcessingModeRDS 0x00 | |
107 | #define cProcessingModeRBDS 0x04 | |
108 | ||
109 | /* bit 3+4 (SYM0/SYM1) */ | |
110 | #define cErrCorrectionNone 0x00 | |
111 | #define cErrCorrection2Bits 0x08 | |
112 | #define cErrCorrection5Bits 0x10 | |
113 | #define cErrCorrectionNoneRBDS 0x18 | |
114 | ||
115 | /* bit 5 (NWSY) */ | |
116 | #define cSyncNormal 0x00 | |
117 | #define cSyncRestart 0x20 | |
118 | ||
119 | /* bit 6 (TSQD) */ | |
120 | #define cSigQualityDetectOFF 0x00 | |
121 | #define cSigQualityDetectON 0x40 | |
122 | ||
123 | /* bit 7 (SQCM) */ | |
124 | #define cSigQualityTriggered 0x00 | |
125 | #define cSigQualityContinous 0x80 | |
126 | ||
127 | /* Pause level and flywheel control byte (1w) */ | |
128 | ||
129 | /* bits 0..5 (FEB0..FEB5) */ | |
130 | #define cFlywheelMaxBlocksMask 0x3F | |
131 | #define cFlywheelDefault 0x20 | |
132 | ||
133 | /* bits 6+7 (PL0/PL1) */ | |
134 | #define cPauseLevel_11mV 0x00 | |
135 | #define cPauseLevel_17mV 0x40 | |
136 | #define cPauseLevel_27mV 0x80 | |
137 | #define cPauseLevel_43mV 0xC0 | |
138 | ||
139 | /* Pause time/oscillator frequency/quality detector control byte (1w) */ | |
140 | ||
141 | /* bits 0..4 (SQS0..SQS4) */ | |
142 | #define cQualityDetectSensMask 0x1F | |
143 | #define cQualityDetectDefault 0x0F | |
144 | ||
145 | /* bit 5 (SOSC) */ | |
146 | #define cSelectOscFreqOFF 0x00 | |
147 | #define cSelectOscFreqON 0x20 | |
148 | ||
149 | /* bit 6+7 (PTF0/PTF1) */ | |
150 | #define cOscFreq_4332kHz 0x00 | |
151 | #define cOscFreq_8664kHz 0x40 | |
152 | #define cOscFreq_12996kHz 0x80 | |
153 | #define cOscFreq_17328kHz 0xC0 | |
154 | ||
155 | /* ---------------------------------------------------------------------- */ | |
156 | ||
ae8aed03 | 157 | static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf) |
10b89ee3 MCC |
158 | { |
159 | int i; | |
160 | ||
161 | if (s->rd_index == s->wr_index) { | |
162 | if (debug > 2) | |
163 | dprintk(PREFIX "Read: buffer empty.\n"); | |
164 | return 0; | |
165 | } | |
166 | ||
167 | if (debug > 2) { | |
168 | dprintk(PREFIX "Read: "); | |
169 | for (i = s->rd_index; i < s->rd_index + 3; i++) | |
170 | dprintk("0x%02x ", s->buffer[i]); | |
171 | } | |
172 | ||
173 | if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3)) | |
174 | return -EFAULT; | |
175 | ||
176 | s->rd_index += 3; | |
177 | if (s->rd_index >= s->buf_size) | |
178 | s->rd_index = 0; | |
179 | s->block_count--; | |
180 | ||
181 | if (debug > 2) | |
182 | dprintk("%d blocks total.\n", s->block_count); | |
183 | ||
184 | return 1; | |
185 | } | |
186 | ||
187 | static void read_from_buf(struct saa6588 *s, struct rds_command *a) | |
188 | { | |
189 | unsigned long flags; | |
190 | ||
ae8aed03 | 191 | unsigned char __user *buf_ptr = a->buffer; |
10b89ee3 MCC |
192 | unsigned int i; |
193 | unsigned int rd_blocks; | |
194 | ||
195 | a->result = 0; | |
196 | if (!a->buffer) | |
197 | return; | |
198 | ||
199 | while (!s->data_available_for_read) { | |
200 | int ret = wait_event_interruptible(s->read_queue, | |
201 | s->data_available_for_read); | |
202 | if (ret == -ERESTARTSYS) { | |
203 | a->result = -EINTR; | |
204 | return; | |
205 | } | |
206 | } | |
207 | ||
208 | spin_lock_irqsave(&s->lock, flags); | |
209 | rd_blocks = a->block_count; | |
210 | if (rd_blocks > s->block_count) | |
211 | rd_blocks = s->block_count; | |
212 | ||
a5bbc7d9 IS |
213 | if (!rd_blocks) { |
214 | spin_unlock_irqrestore(&s->lock, flags); | |
10b89ee3 | 215 | return; |
a5bbc7d9 | 216 | } |
10b89ee3 MCC |
217 | |
218 | for (i = 0; i < rd_blocks; i++) { | |
219 | if (block_to_user_buf(s, buf_ptr)) { | |
220 | buf_ptr += 3; | |
221 | a->result++; | |
222 | } else | |
223 | break; | |
224 | } | |
225 | a->result *= 3; | |
226 | s->data_available_for_read = (s->block_count > 0); | |
227 | spin_unlock_irqrestore(&s->lock, flags); | |
228 | } | |
229 | ||
230 | static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf) | |
231 | { | |
232 | unsigned int i; | |
233 | ||
234 | if (debug > 3) | |
235 | dprintk(PREFIX "New block: "); | |
236 | ||
237 | for (i = 0; i < 3; ++i) { | |
238 | if (debug > 3) | |
239 | dprintk("0x%02x ", blockbuf[i]); | |
240 | s->buffer[s->wr_index] = blockbuf[i]; | |
241 | s->wr_index++; | |
242 | } | |
243 | ||
244 | if (s->wr_index >= s->buf_size) | |
245 | s->wr_index = 0; | |
246 | ||
247 | if (s->wr_index == s->rd_index) { | |
6c3d67ab | 248 | s->rd_index += 3; |
10b89ee3 MCC |
249 | if (s->rd_index >= s->buf_size) |
250 | s->rd_index = 0; | |
251 | } else | |
252 | s->block_count++; | |
253 | ||
254 | if (debug > 3) | |
255 | dprintk("%d blocks total.\n", s->block_count); | |
256 | } | |
257 | ||
258 | static void saa6588_i2c_poll(struct saa6588 *s) | |
259 | { | |
260 | unsigned long flags; | |
261 | unsigned char tmpbuf[6]; | |
262 | unsigned char blocknum; | |
263 | unsigned char tmp; | |
264 | ||
265 | /* Although we only need 3 bytes, we have to read at least 6. | |
266 | SAA6588 returns garbage otherwise */ | |
b5ffc223 | 267 | if (6 != i2c_master_recv(s->client, &tmpbuf[0], 6)) { |
10b89ee3 MCC |
268 | if (debug > 1) |
269 | dprintk(PREFIX "read error!\n"); | |
270 | return; | |
271 | } | |
272 | ||
273 | blocknum = tmpbuf[0] >> 5; | |
274 | if (blocknum == s->last_blocknum) { | |
275 | if (debug > 3) | |
276 | dprintk("Saw block %d again.\n", blocknum); | |
277 | return; | |
278 | } | |
279 | ||
280 | s->last_blocknum = blocknum; | |
281 | ||
282 | /* | |
283 | Byte order according to v4l2 specification: | |
284 | ||
285 | Byte 0: Least Significant Byte of RDS Block | |
286 | Byte 1: Most Significant Byte of RDS Block | |
287 | Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error | |
288 | occurred during reception of this block. | |
289 | Bit 6: Corrected bit. Indicates that an error was | |
290 | corrected for this data block. | |
291 | Bits 5-3: Received Offset. Indicates the offset received | |
292 | by the sync system. | |
293 | Bits 2-0: Offset Name. Indicates the offset applied to this data. | |
294 | ||
295 | SAA6588 byte order is Status-MSB-LSB, so we have to swap the | |
296 | first and the last of the 3 bytes block. | |
297 | */ | |
298 | ||
299 | tmp = tmpbuf[2]; | |
300 | tmpbuf[2] = tmpbuf[0]; | |
301 | tmpbuf[0] = tmp; | |
302 | ||
303 | tmp = blocknum; | |
304 | tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */ | |
305 | if ((tmpbuf[2] & 0x03) == 0x03) | |
306 | tmp |= 0x80; /* uncorrectable error */ | |
307 | else if ((tmpbuf[2] & 0x03) != 0x00) | |
308 | tmp |= 0x40; /* corrected error */ | |
309 | tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */ | |
310 | ||
311 | spin_lock_irqsave(&s->lock, flags); | |
312 | block_to_buf(s, tmpbuf); | |
313 | spin_unlock_irqrestore(&s->lock, flags); | |
314 | s->data_available_for_read = 1; | |
315 | wake_up_interruptible(&s->read_queue); | |
316 | } | |
317 | ||
318 | static void saa6588_timer(unsigned long data) | |
319 | { | |
320 | struct saa6588 *s = (struct saa6588 *)data; | |
321 | ||
322 | schedule_work(&s->work); | |
323 | } | |
324 | ||
c4028958 | 325 | static void saa6588_work(struct work_struct *work) |
10b89ee3 | 326 | { |
c4028958 | 327 | struct saa6588 *s = container_of(work, struct saa6588, work); |
10b89ee3 MCC |
328 | |
329 | saa6588_i2c_poll(s); | |
e222f834 | 330 | mod_timer(&s->timer, jiffies + msecs_to_jiffies(20)); |
10b89ee3 MCC |
331 | } |
332 | ||
333 | static int saa6588_configure(struct saa6588 *s) | |
334 | { | |
335 | unsigned char buf[3]; | |
336 | int rc; | |
337 | ||
338 | buf[0] = cSyncRestart; | |
339 | if (rbds) | |
340 | buf[0] |= cProcessingModeRBDS; | |
341 | ||
342 | buf[1] = cFlywheelDefault; | |
343 | switch (plvl) { | |
344 | case 0: | |
345 | buf[1] |= cPauseLevel_11mV; | |
346 | break; | |
347 | case 1: | |
348 | buf[1] |= cPauseLevel_17mV; | |
349 | break; | |
350 | case 2: | |
351 | buf[1] |= cPauseLevel_27mV; | |
352 | break; | |
353 | case 3: | |
354 | buf[1] |= cPauseLevel_43mV; | |
355 | break; | |
356 | default: /* nothing */ | |
357 | break; | |
358 | } | |
359 | ||
360 | buf[2] = cQualityDetectDefault | cSelectOscFreqON; | |
361 | ||
362 | switch (xtal) { | |
363 | case 0: | |
364 | buf[2] |= cOscFreq_4332kHz; | |
365 | break; | |
366 | case 1: | |
367 | buf[2] |= cOscFreq_8664kHz; | |
368 | break; | |
369 | case 2: | |
370 | buf[2] |= cOscFreq_12996kHz; | |
371 | break; | |
372 | case 3: | |
373 | buf[2] |= cOscFreq_17328kHz; | |
374 | break; | |
375 | default: /* nothing */ | |
376 | break; | |
377 | } | |
378 | ||
379 | dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n", | |
380 | buf[0], buf[1], buf[2]); | |
381 | ||
b5ffc223 HV |
382 | rc = i2c_master_send(s->client, buf, 3); |
383 | if (rc != 3) | |
10b89ee3 MCC |
384 | printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc); |
385 | ||
386 | return 0; | |
387 | } | |
388 | ||
389 | /* ---------------------------------------------------------------------- */ | |
390 | ||
b5ffc223 HV |
391 | static int saa6588_probe(struct i2c_client *client, |
392 | const struct i2c_device_id *id) | |
10b89ee3 MCC |
393 | { |
394 | struct saa6588 *s; | |
10b89ee3 | 395 | |
b5ffc223 HV |
396 | v4l_info(client, "saa6588 found @ 0x%x (%s)\n", |
397 | client->addr << 1, client->adapter->name); | |
10b89ee3 | 398 | |
b5ffc223 HV |
399 | s = kzalloc(sizeof(*s), GFP_KERNEL); |
400 | if (s == NULL) | |
10b89ee3 MCC |
401 | return -ENOMEM; |
402 | ||
b5ffc223 | 403 | s->client = client; |
10b89ee3 MCC |
404 | s->buf_size = bufblocks * 3; |
405 | ||
b5ffc223 HV |
406 | s->buffer = kmalloc(s->buf_size, GFP_KERNEL); |
407 | if (s->buffer == NULL) { | |
10b89ee3 MCC |
408 | kfree(s); |
409 | return -ENOMEM; | |
410 | } | |
588005e1 | 411 | spin_lock_init(&s->lock); |
10b89ee3 MCC |
412 | s->block_count = 0; |
413 | s->wr_index = 0; | |
414 | s->rd_index = 0; | |
415 | s->last_blocknum = 0xff; | |
416 | init_waitqueue_head(&s->read_queue); | |
417 | s->data_available_for_read = 0; | |
b5ffc223 | 418 | i2c_set_clientdata(client, s); |
10b89ee3 MCC |
419 | |
420 | saa6588_configure(s); | |
421 | ||
422 | /* start polling via eventd */ | |
c4028958 | 423 | INIT_WORK(&s->work, saa6588_work); |
10b89ee3 MCC |
424 | init_timer(&s->timer); |
425 | s->timer.function = saa6588_timer; | |
426 | s->timer.data = (unsigned long)s; | |
427 | schedule_work(&s->work); | |
10b89ee3 MCC |
428 | return 0; |
429 | } | |
430 | ||
b5ffc223 | 431 | static int saa6588_remove(struct i2c_client *client) |
10b89ee3 MCC |
432 | { |
433 | struct saa6588 *s = i2c_get_clientdata(client); | |
434 | ||
435 | del_timer_sync(&s->timer); | |
436 | flush_scheduled_work(); | |
437 | ||
10b89ee3 MCC |
438 | kfree(s->buffer); |
439 | kfree(s); | |
440 | return 0; | |
441 | } | |
442 | ||
443 | static int saa6588_command(struct i2c_client *client, unsigned int cmd, | |
444 | void *arg) | |
445 | { | |
446 | struct saa6588 *s = i2c_get_clientdata(client); | |
447 | struct rds_command *a = (struct rds_command *)arg; | |
448 | ||
449 | switch (cmd) { | |
450 | /* --- open() for /dev/radio --- */ | |
451 | case RDS_CMD_OPEN: | |
452 | a->result = 0; /* return error if chip doesn't work ??? */ | |
453 | break; | |
454 | /* --- close() for /dev/radio --- */ | |
455 | case RDS_CMD_CLOSE: | |
456 | s->data_available_for_read = 1; | |
457 | wake_up_interruptible(&s->read_queue); | |
458 | a->result = 0; | |
459 | break; | |
460 | /* --- read() for /dev/radio --- */ | |
461 | case RDS_CMD_READ: | |
462 | read_from_buf(s, a); | |
463 | break; | |
464 | /* --- poll() for /dev/radio --- */ | |
465 | case RDS_CMD_POLL: | |
466 | a->result = 0; | |
467 | if (s->data_available_for_read) { | |
468 | a->result |= POLLIN | POLLRDNORM; | |
469 | } | |
470 | poll_wait(a->instance, &s->read_queue, a->event_list); | |
471 | break; | |
472 | ||
473 | default: | |
474 | /* nothing */ | |
475 | break; | |
476 | } | |
477 | return 0; | |
478 | } | |
479 | ||
480 | /* ----------------------------------------------------------------------- */ | |
481 | ||
b5ffc223 HV |
482 | static const struct i2c_device_id saa6588_id[] = { |
483 | { "saa6588", 0 }, | |
484 | { } | |
10b89ee3 | 485 | }; |
b5ffc223 | 486 | MODULE_DEVICE_TABLE(i2c, saa6588_id); |
10b89ee3 | 487 | |
b5ffc223 | 488 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { |
10b89ee3 | 489 | .name = "saa6588", |
b5ffc223 HV |
490 | .command = saa6588_command, |
491 | .probe = saa6588_probe, | |
492 | .remove = saa6588_remove, | |
493 | .legacy_class = I2C_CLASS_TV_ANALOG, | |
494 | .id_table = saa6588_id, | |
10b89ee3 | 495 | }; |