2 * Driver for the SAA5246A videotext decoder chip from Philips.
4 * Only capturing of videotext pages is tested. The SAA5246A chip also has
5 * a TV output but my hardware doesn't use it. For this reason this driver
6 * does not support changing any TV display settings.
8 * Copyright (C) 2004 Michael Geng <linux@MichaelGeng.de>
13 * Copyright (C) 1998 Richard Guenther
14 * <richard.guenther@student.uni-tuebingen.de>
17 * Alan Cox <Alan.Cox@linux.org>
22 * Copyright (C) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de>
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/sched.h>
44 #include <linux/init.h>
45 #include <linux/i2c.h>
46 #include <linux/videotext.h>
47 #include <linux/videodev.h>
50 struct saa5246a_device
52 u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
53 int is_searching[NUM_DAUS];
54 struct i2c_client *client;
55 struct semaphore lock;
58 static struct video_device saa_template; /* Declared near bottom */
60 /* Addresses to scan */
61 static unsigned short normal_i2c[] = { I2C_ADDRESS, I2C_CLIENT_END };
62 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
63 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
64 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
65 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
66 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
67 static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
69 static struct i2c_client_address_data addr_data = {
70 normal_i2c, normal_i2c_range,
76 static struct i2c_client client_template;
78 static int saa5246a_attach(struct i2c_adapter *adap, int addr, int kind)
82 struct i2c_client *client;
83 struct video_device *vd;
84 struct saa5246a_device *t;
86 printk(KERN_INFO "saa5246a: teletext chip found.\n");
87 client=kmalloc(sizeof(*client), GFP_KERNEL);
90 client_template.adapter = adap;
91 client_template.addr = addr;
92 memcpy(client, &client_template, sizeof(*client));
93 t = kmalloc(sizeof(*t), GFP_KERNEL);
99 memset(t, 0, sizeof(*t));
100 strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
101 init_MUTEX(&t->lock);
104 * Now create a video4linux device
107 vd = video_device_alloc();
114 i2c_set_clientdata(client, vd);
115 memcpy(vd, &saa_template, sizeof(*vd));
117 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
119 memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
120 t->is_searching[pgbuf] = FALSE;
129 if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
133 video_device_release(vd);
137 i2c_attach_client(client);
142 * We do most of the hard work when we become a device on the i2c.
144 static int saa5246a_probe(struct i2c_adapter *adap)
146 if (adap->class & I2C_CLASS_TV_ANALOG)
147 return i2c_probe(adap, &addr_data, saa5246a_attach);
151 static int saa5246a_detach(struct i2c_client *client)
153 struct video_device *vd = i2c_get_clientdata(client);
154 i2c_detach_client(client);
155 video_unregister_device(vd);
161 static int saa5246a_command(struct i2c_client *device, unsigned int cmd,
171 static struct i2c_driver i2c_driver_videotext =
173 .owner = THIS_MODULE,
174 .name = IF_NAME, /* name */
175 .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
176 .flags = I2C_DF_NOTIFY,
177 .attach_adapter = saa5246a_probe,
178 .detach_client = saa5246a_detach,
179 .command = saa5246a_command
182 static struct i2c_client client_template = {
184 .driver = &i2c_driver_videotext,
188 static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
193 memcpy(buf+1, data, count);
195 if(i2c_master_send(t->client, buf, count+1)==count+1)
200 static int i2c_senddata(struct saa5246a_device *t, ...)
202 unsigned char buf[64];
208 while((v=va_arg(argp,int))!=-1)
210 return i2c_sendbuf(t, buf[0], ct-1, buf+1);
213 /* Get count number of bytes from I²C-device at address adr, store them in buf.
214 * Start & stop handshaking is done by this routine, ack will be sent after the
215 * last byte to inhibit further sending of data. If uaccess is TRUE, data is
216 * written to user-space with put_user. Returns -1 if I²C-device didn't send
217 * acknowledge, 0 otherwise
219 static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
221 if(i2c_master_recv(t->client, buf, count)!=count)
226 /* When a page is found then the not FOUND bit in one of the status registers
227 * of the SAA5264A chip is cleared. Unfortunately this bit is not set
228 * automatically when a new page is requested. Instead this function must be
229 * called after a page has been requested.
231 * Return value: 0 if successful
233 static int saa5246a_clear_found_bit(struct saa5246a_device *t,
234 unsigned char dau_no)
236 unsigned char row_25_column_8;
238 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
241 R8_DO_NOT_CLEAR_MEMORY,
248 i2c_getdata(t, 1, &row_25_column_8))
252 row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
253 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
256 R8_DO_NOT_CLEAR_MEMORY,
272 /* Requests one videotext page as described in req. The fields of req are
273 * checked and an error is returned if something is invalid.
275 * Return value: 0 if successful
277 static int saa5246a_request_page(struct saa5246a_device *t,
280 if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
282 if (req->pagemask & PGMASK_PAGE)
283 if (req->page < 0 || req->page > PAGE_MAX)
285 if (req->pagemask & PGMASK_HOUR)
286 if (req->hour < 0 || req->hour > HOUR_MAX)
288 if (req->pagemask & PGMASK_MINUTE)
289 if (req->minute < 0 || req->minute > MINUTE_MAX)
291 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
294 if (i2c_senddata(t, SAA5246A_REGISTER_R2,
296 R2_IN_R3_SELECT_PAGE_HUNDREDS |
299 R2_HAMMING_CHECK_OFF,
301 HUNDREDS_OF_PAGE(req->page) |
303 (req->pagemask & PG_HUND ?
304 R3_PAGE_HUNDREDS_DO_CARE :
305 R3_PAGE_HUNDREDS_DO_NOT_CARE),
307 TENS_OF_PAGE(req->page) |
308 (req->pagemask & PG_TEN ?
309 R3_PAGE_TENS_DO_CARE :
310 R3_PAGE_TENS_DO_NOT_CARE),
312 UNITS_OF_PAGE(req->page) |
313 (req->pagemask & PG_UNIT ?
314 R3_PAGE_UNITS_DO_CARE :
315 R3_PAGE_UNITS_DO_NOT_CARE),
317 TENS_OF_HOUR(req->hour) |
318 (req->pagemask & HR_TEN ?
319 R3_HOURS_TENS_DO_CARE :
320 R3_HOURS_TENS_DO_NOT_CARE),
322 UNITS_OF_HOUR(req->hour) |
323 (req->pagemask & HR_UNIT ?
324 R3_HOURS_UNITS_DO_CARE :
325 R3_HOURS_UNITS_DO_NOT_CARE),
327 TENS_OF_MINUTE(req->minute) |
328 (req->pagemask & MIN_TEN ?
329 R3_MINUTES_TENS_DO_CARE :
330 R3_MINUTES_TENS_DO_NOT_CARE),
332 UNITS_OF_MINUTE(req->minute) |
333 (req->pagemask & MIN_UNIT ?
334 R3_MINUTES_UNITS_DO_CARE :
335 R3_MINUTES_UNITS_DO_NOT_CARE),
337 COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
339 R2_IN_R3_SELECT_PAGE_HUNDREDS |
342 R2_HAMMING_CHECK_OFF,
344 HUNDREDS_OF_PAGE(req->page) |
346 (req->pagemask & PG_HUND ?
347 R3_PAGE_HUNDREDS_DO_CARE :
348 R3_PAGE_HUNDREDS_DO_NOT_CARE),
355 t->is_searching[req->pgbuf] = TRUE;
359 /* This routine decodes the page number from the infobits contained in line 25.
362 * infobits: must be bits 0 to 9 of column 25
364 * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
366 static inline int saa5246a_extract_pagenum_from_infobits(
367 unsigned char infobits[10])
369 int page_hundreds, page_tens, page_units;
371 page_units = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
372 page_tens = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
373 page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
375 /* page 0x.. means page 8.. */
376 if (page_hundreds == 0)
379 return((page_hundreds << 8) | (page_tens << 4) | page_units);
382 /* Decodes the hour from the infobits contained in line 25.
385 * infobits: must be bits 0 to 9 of column 25
387 * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
389 static inline int saa5246a_extract_hour_from_infobits(
390 unsigned char infobits[10])
392 int hour_tens, hour_units;
394 hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
395 hour_tens = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
397 return((hour_tens << 4) | hour_units);
400 /* Decodes the minutes from the infobits contained in line 25.
403 * infobits: must be bits 0 to 9 of column 25
405 * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
407 static inline int saa5246a_extract_minutes_from_infobits(
408 unsigned char infobits[10])
410 int minutes_tens, minutes_units;
412 minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
413 minutes_tens = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
415 return((minutes_tens << 4) | minutes_units);
418 /* Reads the status bits contained in the first 10 columns of the first line
419 * and extracts the information into info.
421 * Return value: 0 if successful
423 static inline int saa5246a_get_status(struct saa5246a_device *t,
424 vtx_pageinfo_t *info, unsigned char dau_no)
426 unsigned char infobits[10];
429 if (dau_no >= NUM_DAUS)
432 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
435 R8_DO_NOT_CLEAR_MEMORY,
442 i2c_getdata(t, 10, infobits))
447 info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
448 info->hour = saa5246a_extract_hour_from_infobits(infobits);
449 info->minute = saa5246a_extract_minutes_from_infobits(infobits);
450 info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
451 info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
452 info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
453 info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
454 info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
455 info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
456 info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
457 info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
458 info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
459 info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
460 info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
462 for (column = 0; column <= 7; column++) {
463 if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
468 if (!info->hamming && !info->notfound)
469 t->is_searching[dau_no] = FALSE;
473 /* Reads 1 videotext page buffer of the SAA5246A.
475 * req is used both as input and as output. It contains information which part
476 * must be read. The videotext page is copied into req->buffer.
478 * Return value: 0 if successful
480 static inline int saa5246a_get_page(struct saa5246a_device *t,
485 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
486 req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
488 /* Read "normal" part of page */
489 end = min(req->end, VTX_PAGESIZE - 1);
490 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
493 R8_DO_NOT_CLEAR_MEMORY,
500 i2c_getdata(t, end - req->start + 1, req->buffer))
504 /* Always get the time from buffer 4, since this stupid SAA5246A only
505 * updates the currently displayed buffer...
507 if (REQ_CONTAINS_TIME(req))
509 start = max(req->start, POS_TIME_START);
510 end = min(req->end, POS_TIME_END);
511 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
513 R8_ACTIVE_CHAPTER_4 |
514 R8_DO_NOT_CLEAR_MEMORY,
521 i2c_getdata(t, end - start + 1,
522 req->buffer + start - req->start))
527 /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
528 if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf])
530 start = max(req->start, POS_HEADER_START);
531 end = min(req->end, POS_HEADER_END);
532 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
534 R8_ACTIVE_CHAPTER_4 |
535 R8_DO_NOT_CLEAR_MEMORY,
542 i2c_getdata(t, end - start + 1,
543 req->buffer + start - req->start))
552 /* Stops the acquisition circuit given in dau_no. The page buffer associated
553 * with this acquisition circuit will no more be updated. The other daus are
556 * Return value: 0 if successful
558 static inline int saa5246a_stop_dau(struct saa5246a_device *t,
559 unsigned char dau_no)
561 if (dau_no >= NUM_DAUS)
563 if (i2c_senddata(t, SAA5246A_REGISTER_R2,
565 R2_IN_R3_SELECT_PAGE_HUNDREDS |
568 R2_HAMMING_CHECK_OFF,
572 R3_PAGE_HUNDREDS_DO_NOT_CARE,
578 t->is_searching[dau_no] = FALSE;
582 /* Handles ioctls defined in videotext.h
584 * Returns 0 if successful
586 static int do_saa5246a_ioctl(struct inode *inode, struct file *file,
587 unsigned int cmd, void *arg)
589 struct video_device *vd = video_devdata(file);
590 struct saa5246a_device *t=vd->priv;
595 vtx_info_t *info = arg;
597 info->version_major = MAJOR_VERSION;
598 info->version_minor = MINOR_VERSION;
599 info->numpages = NUM_DAUS;
605 vtx_pagereq_t *req = arg;
607 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
609 memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
615 vtx_pagereq_t *req = arg;
617 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
619 return(saa5246a_clear_found_bit(t, req->pgbuf));
624 vtx_pagereq_t *req = arg;
626 return(saa5246a_request_page(t, req));
631 vtx_pagereq_t *req = arg;
635 if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
637 if(copy_to_user(req->buffer, &info,
638 sizeof(vtx_pageinfo_t)))
645 vtx_pagereq_t *req = arg;
647 return(saa5246a_get_page(t, req));
652 vtx_pagereq_t *req = arg;
654 return(saa5246a_stop_dau(t, req->pgbuf));
669 /* I do not know what "virtual mode" means */
679 static int saa5246a_ioctl(struct inode *inode, struct file *file,
680 unsigned int cmd, unsigned long arg)
682 struct video_device *vd = video_devdata(file);
683 struct saa5246a_device *t = vd->priv;
687 err = video_usercopy(inode, file, cmd, arg, do_saa5246a_ioctl);
692 static int saa5246a_open(struct inode *inode, struct file *file)
694 struct video_device *vd = video_devdata(file);
695 struct saa5246a_device *t = vd->priv;
698 err = video_exclusive_open(inode,file);
702 if (t->client==NULL) {
707 if (i2c_senddata(t, SAA5246A_REGISTER_R0,
710 R0_PLL_TIME_CONSTANT_LONG |
711 R0_ENABLE_nODD_EVEN_OUTPUT |
713 R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
715 R0_NO_AUTOMATIC_FASTEXT_PROMPT,
717 R1_NON_INTERLACED_312_312_LINES |
719 R1_EXTENDED_PACKET_DISABLE |
721 R1_8_BITS_NO_PARITY |
725 i2c_senddata(t, SAA5246A_REGISTER_R4,
727 /* We do not care much for the TV display but nevertheless we
728 * need the currently displayed page later because only on that
729 * page the time is updated. */
741 video_exclusive_release(inode,file);
745 static int saa5246a_release(struct inode *inode, struct file *file)
747 struct video_device *vd = video_devdata(file);
748 struct saa5246a_device *t = vd->priv;
750 /* Stop all acquisition circuits. */
751 i2c_senddata(t, SAA5246A_REGISTER_R1,
753 R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
755 R1_EXTENDED_PACKET_DISABLE |
757 R1_8_BITS_NO_PARITY |
761 video_exclusive_release(inode,file);
765 static int __init init_saa_5246a (void)
767 printk(KERN_INFO "SAA5246A driver (" IF_NAME
768 " interface) for VideoText version %d.%d\n",
769 MAJOR_VERSION, MINOR_VERSION);
770 return i2c_add_driver(&i2c_driver_videotext);
773 static void __exit cleanup_saa_5246a (void)
775 i2c_del_driver(&i2c_driver_videotext);
778 module_init(init_saa_5246a);
779 module_exit(cleanup_saa_5246a);
781 static struct file_operations saa_fops = {
782 .owner = THIS_MODULE,
783 .open = saa5246a_open,
784 .release = saa5246a_release,
785 .ioctl = saa5246a_ioctl,
789 static struct video_device saa_template =
791 .owner = THIS_MODULE,
793 .type = VID_TYPE_TELETEXT,
794 .hardware = VID_HARDWARE_SAA5249,
796 .release = video_device_release,
800 MODULE_LICENSE("GPL");