Linux-2.6.12-rc2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / saa5246a.c
CommitLineData
1da177e4
LT
1/*
2 * Driver for the SAA5246A or SAA5281 Teletext (=Videotext) decoder chips from
3 * Philips.
4 *
5 * Only capturing of Teletext pages is tested. The videotext chips also have a
6 * TV output but my hardware doesn't use it. For this reason this driver does
7 * not support changing any TV display settings.
8 *
9 * Copyright (C) 2004 Michael Geng <linux@MichaelGeng.de>
10 *
11 * Derived from
12 *
13 * saa5249 driver
14 * Copyright (C) 1998 Richard Guenther
15 * <richard.guenther@student.uni-tuebingen.de>
16 *
17 * with changes by
18 * Alan Cox <Alan.Cox@linux.org>
19 *
20 * and
21 *
22 * vtx.c
23 * Copyright (C) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de>
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
38 * USA.
39 */
40
41#include <linux/module.h>
42#include <linux/kernel.h>
43#include <linux/sched.h>
44#include <linux/mm.h>
45#include <linux/init.h>
46#include <linux/i2c.h>
47#include <linux/videotext.h>
48#include <linux/videodev.h>
49#include "saa5246a.h"
50
51MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
52MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
53MODULE_LICENSE("GPL");
54
55struct saa5246a_device
56{
57 u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
58 int is_searching[NUM_DAUS];
59 struct i2c_client *client;
60 struct semaphore lock;
61};
62
63static struct video_device saa_template; /* Declared near bottom */
64
65/* Addresses to scan */
66static unsigned short normal_i2c[] = { I2C_ADDRESS, I2C_CLIENT_END };
67static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
68I2C_CLIENT_INSMOD;
69
70static struct i2c_client client_template;
71
72static int saa5246a_attach(struct i2c_adapter *adap, int addr, int kind)
73{
74 int pgbuf;
75 int err;
76 struct i2c_client *client;
77 struct video_device *vd;
78 struct saa5246a_device *t;
79
80 printk(KERN_INFO "saa5246a: teletext chip found.\n");
81 client=kmalloc(sizeof(*client), GFP_KERNEL);
82 if(client==NULL)
83 return -ENOMEM;
84 client_template.adapter = adap;
85 client_template.addr = addr;
86 memcpy(client, &client_template, sizeof(*client));
87 t = kmalloc(sizeof(*t), GFP_KERNEL);
88 if(t==NULL)
89 {
90 kfree(client);
91 return -ENOMEM;
92 }
93 memset(t, 0, sizeof(*t));
94 strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
95 init_MUTEX(&t->lock);
96
97 /*
98 * Now create a video4linux device
99 */
100
101 vd = video_device_alloc();
102 if(vd==NULL)
103 {
104 kfree(t);
105 kfree(client);
106 return -ENOMEM;
107 }
108 i2c_set_clientdata(client, vd);
109 memcpy(vd, &saa_template, sizeof(*vd));
110
111 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
112 {
113 memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
114 t->is_searching[pgbuf] = FALSE;
115 }
116 vd->priv=t;
117
118
119 /*
120 * Register it
121 */
122
123 if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
124 {
125 kfree(t);
126 kfree(client);
127 video_device_release(vd);
128 return err;
129 }
130 t->client = client;
131 i2c_attach_client(client);
132 return 0;
133}
134
135/*
136 * We do most of the hard work when we become a device on the i2c.
137 */
138static int saa5246a_probe(struct i2c_adapter *adap)
139{
140 if (adap->class & I2C_CLASS_TV_ANALOG)
141 return i2c_probe(adap, &addr_data, saa5246a_attach);
142 return 0;
143}
144
145static int saa5246a_detach(struct i2c_client *client)
146{
147 struct video_device *vd = i2c_get_clientdata(client);
148 i2c_detach_client(client);
149 video_unregister_device(vd);
150 kfree(vd->priv);
151 kfree(client);
152 return 0;
153}
154
155static int saa5246a_command(struct i2c_client *device, unsigned int cmd,
156 void *arg)
157{
158 return -EINVAL;
159}
160
161/*
162 * I2C interfaces
163 */
164
165static struct i2c_driver i2c_driver_videotext =
166{
167 .owner = THIS_MODULE,
168 .name = IF_NAME, /* name */
169 .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
170 .flags = I2C_DF_NOTIFY,
171 .attach_adapter = saa5246a_probe,
172 .detach_client = saa5246a_detach,
173 .command = saa5246a_command
174};
175
176static struct i2c_client client_template = {
177 .driver = &i2c_driver_videotext,
178 .name = "(unset)",
179};
180
181static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
182{
183 char buf[64];
184
185 buf[0] = reg;
186 memcpy(buf+1, data, count);
187
188 if(i2c_master_send(t->client, buf, count+1)==count+1)
189 return 0;
190 return -1;
191}
192
193static int i2c_senddata(struct saa5246a_device *t, ...)
194{
195 unsigned char buf[64];
196 int v;
197 int ct=0;
198 va_list argp;
199 va_start(argp,t);
200
201 while((v=va_arg(argp,int))!=-1)
202 buf[ct++]=v;
203 return i2c_sendbuf(t, buf[0], ct-1, buf+1);
204}
205
206