V4L/DVB (3916): AverMedia 6 Eyes AVS6EYES support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / tuner-3036.c
blob74ab48c09c6a448a745f3469ae08cdc9c1adf373
1 /*
2 * Driver for Philips SAB3036 "CITAC" tuner control chip.
4 * Author: Phil Blundell <philb@gnu.org>
6 * The SAB3036 is just about different enough from the chips that
7 * tuner.c copes with to make it not worth the effort to crowbar
8 * the support into that file. So instead we have a separate driver.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/timer.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
26 #include <linux/i2c.h>
27 #include <linux/videodev.h>
29 #include <media/tuner.h>
31 static int debug; /* insmod parameter */
32 static int this_adap;
34 static struct i2c_client client_template;
36 /* Addresses to scan */
37 static unsigned short normal_i2c[] = { 0x60, 0x61, I2C_CLIENT_END };
38 static unsigned short ignore = I2C_CLIENT_END;
40 static struct i2c_client_address_data addr_data = {
41 .normal_i2c = normal_i2c,
42 .probe = &ignore,
43 .ignore = &ignore,
46 /* ---------------------------------------------------------------------- */
48 static unsigned char
49 tuner_getstatus (struct i2c_client *c)
51 unsigned char byte;
52 if (i2c_master_recv(c, &byte, 1) != 1)
53 printk(KERN_ERR "tuner-3036: I/O error.\n");
54 return byte;
57 #define TUNER_FL 0x80
59 static int
60 tuner_islocked (struct i2c_client *c)
62 return (tuner_getstatus(c) & TUNER_FL);
65 /* ---------------------------------------------------------------------- */
67 static void
68 set_tv_freq(struct i2c_client *c, int freq)
70 u16 div = ((freq * 20) / 16);
71 unsigned long give_up = jiffies + HZ;
72 unsigned char buffer[2];
74 if (debug)
75 printk(KERN_DEBUG "tuner: setting frequency %dMHz, divisor %x\n", freq / 16, div);
77 /* Select high tuning current */
78 buffer[0] = 0x29;
79 buffer[1] = 0x3e;
81 if (i2c_master_send(c, buffer, 2) != 2)
82 printk("tuner: i2c i/o error 1\n");
84 buffer[0] = 0x80 | ((div>>8) & 0x7f);
85 buffer[1] = div & 0xff;
87 if (i2c_master_send(c, buffer, 2) != 2)
88 printk("tuner: i2c i/o error 2\n");
90 while (!tuner_islocked(c) && time_before(jiffies, give_up))
91 schedule();
93 if (!tuner_islocked(c))
94 printk(KERN_WARNING "tuner: failed to achieve PLL lock\n");
96 /* Select low tuning current and engage AFC */
97 buffer[0] = 0x29;
98 buffer[1] = 0xb2;
100 if (i2c_master_send(c, buffer, 2) != 2)
101 printk("tuner: i2c i/o error 3\n");
103 if (debug)
104 printk(KERN_DEBUG "tuner: status %02x\n", tuner_getstatus(c));
107 /* ---------------------------------------------------------------------- */
109 static int
110 tuner_attach(struct i2c_adapter *adap, int addr, int kind)
112 static unsigned char buffer[] = { 0x29, 0x32, 0x2a, 0, 0x2b, 0 };
114 struct i2c_client *client;
116 if (this_adap > 0)
117 return -1;
118 this_adap++;
120 client_template.adapter = adap;
121 client_template.addr = addr;
123 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
124 if (client == NULL)
125 return -ENOMEM;
126 memcpy(client, &client_template, sizeof(struct i2c_client));
128 printk("tuner: SAB3036 found, status %02x\n", tuner_getstatus(client));
130 i2c_attach_client(client);
132 if (i2c_master_send(client, buffer, 2) != 2)
133 printk("tuner: i2c i/o error 1\n");
134 if (i2c_master_send(client, buffer+2, 2) != 2)
135 printk("tuner: i2c i/o error 2\n");
136 if (i2c_master_send(client, buffer+4, 2) != 2)
137 printk("tuner: i2c i/o error 3\n");
138 return 0;
141 static int
142 tuner_detach(struct i2c_client *c)
144 return 0;
147 static int
148 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
150 int *iarg = (int*)arg;
152 switch (cmd)
154 case VIDIOCSFREQ:
155 set_tv_freq(client, *iarg);
156 break;
158 default:
159 return -EINVAL;
161 return 0;
164 static int
165 tuner_probe(struct i2c_adapter *adap)
167 this_adap = 0;
168 if (adap->id == I2C_HW_B_LP)
169 return i2c_probe(adap, &addr_data, tuner_attach);
170 return 0;
173 /* ----------------------------------------------------------------------- */
175 static struct i2c_driver
176 i2c_driver_tuner =
178 .driver = {
179 .name = "sab3036",
181 .id = I2C_DRIVERID_SAB3036,
182 .attach_adapter = tuner_probe,
183 .detach_client = tuner_detach,
184 .command = tuner_command
187 static struct i2c_client client_template =
189 .driver = &i2c_driver_tuner,
190 .name = "SAB3036",
193 static int __init
194 tuner3036_init(void)
196 return i2c_add_driver(&i2c_driver_tuner);
199 static void __exit
200 tuner3036_exit(void)
202 i2c_del_driver(&i2c_driver_tuner);
205 MODULE_DESCRIPTION("SAB3036 tuner driver");
206 MODULE_AUTHOR("Philip Blundell <philb@gnu.org>");
207 MODULE_LICENSE("GPL");
209 module_param(debug, int, 0);
210 MODULE_PARM_DESC(debug,"Enable debugging output");
212 module_init(tuner3036_init);
213 module_exit(tuner3036_exit);