61513bb0fae9b439f6122f5688605868c51d092d
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / tuner-3036.c
1 /*
2  * Driver for Philips SAB3036 "CITAC" tuner control chip.
3  *
4  * Author: Phil Blundell <philb@gnu.org>
5  *
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. 
9  *
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.
14  */
15
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/version.h>
25 #include <linux/init.h>
26
27 #include <linux/i2c.h>
28 #include <linux/videodev.h>
29
30 #include <media/tuner.h>
31
32 static int debug;       /* insmod parameter */
33 static int this_adap;
34
35 static struct i2c_client client_template;
36
37 /* Addresses to scan */
38 static unsigned short normal_i2c[] = {I2C_CLIENT_END};
39 static unsigned short normal_i2c_range[] = {0x60, 0x61, I2C_CLIENT_END};
40 static unsigned short probe[2]        = { I2C_CLIENT_END, I2C_CLIENT_END };
41 static unsigned short probe_range[2]  = { I2C_CLIENT_END, I2C_CLIENT_END };
42 static unsigned short ignore[2]       = { I2C_CLIENT_END, I2C_CLIENT_END };
43 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
44 static unsigned short force[2]        = { I2C_CLIENT_END, I2C_CLIENT_END };
45
46 static struct i2c_client_address_data addr_data = {
47         normal_i2c, normal_i2c_range, 
48         probe, probe_range, 
49         ignore, ignore_range, 
50         force
51 };
52
53 /* ---------------------------------------------------------------------- */
54
55 static unsigned char
56 tuner_getstatus (struct i2c_client *c)
57 {
58         unsigned char byte;
59         if (i2c_master_recv(c, &byte, 1) != 1)
60                 printk(KERN_ERR "tuner-3036: I/O error.\n");
61         return byte;
62 }
63
64 #define TUNER_FL        0x80
65
66 static int 
67 tuner_islocked (struct i2c_client *c)
68 {
69         return (tuner_getstatus(c) & TUNER_FL);
70 }
71
72 /* ---------------------------------------------------------------------- */
73
74 static void 
75 set_tv_freq(struct i2c_client *c, int freq)
76 {
77         u16 div = ((freq * 20) / 16);
78         unsigned long give_up = jiffies + HZ;
79         unsigned char buffer[2];
80
81         if (debug)
82                 printk(KERN_DEBUG "tuner: setting frequency %dMHz, divisor %x\n", freq / 16, div);
83         
84         /* Select high tuning current */
85         buffer[0] = 0x29;
86         buffer[1] = 0x3e;
87
88         if (i2c_master_send(c, buffer, 2) != 2)
89                 printk("tuner: i2c i/o error 1\n");
90         
91         buffer[0] = 0x80 | ((div>>8) & 0x7f);
92         buffer[1] = div & 0xff;
93
94         if (i2c_master_send(c, buffer, 2) != 2)
95                 printk("tuner: i2c i/o error 2\n");
96         
97         while (!tuner_islocked(c) && time_before(jiffies, give_up))
98                 schedule();
99                
100         if (!tuner_islocked(c))
101                 printk(KERN_WARNING "tuner: failed to achieve PLL lock\n");
102         
103         /* Select low tuning current and engage AFC */
104         buffer[0] = 0x29;
105         buffer[1] = 0xb2;
106
107         if (i2c_master_send(c, buffer, 2) != 2)
108                 printk("tuner: i2c i/o error 3\n");
109
110         if (debug)
111                 printk(KERN_DEBUG "tuner: status %02x\n", tuner_getstatus(c));
112 }
113
114 /* ---------------------------------------------------------------------- */
115
116 static int 
117 tuner_attach(struct i2c_adapter *adap, int addr, int kind)
118 {
119         static unsigned char buffer[] = { 0x29, 0x32, 0x2a, 0, 0x2b, 0 };
120
121         struct i2c_client *client;
122
123         if (this_adap > 0)
124                 return -1;
125         this_adap++;
126         
127         client_template.adapter = adap;
128         client_template.addr = addr;
129
130         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
131         if (client == NULL)
132                 return -ENOMEM;
133         memcpy(client, &client_template, sizeof(struct i2c_client));
134
135         printk("tuner: SAB3036 found, status %02x\n", tuner_getstatus(client));
136
137         i2c_attach_client(client);
138         MOD_INC_USE_COUNT;
139
140         if (i2c_master_send(client, buffer, 2) != 2)
141                 printk("tuner: i2c i/o error 1\n");
142         if (i2c_master_send(client, buffer+2, 2) != 2)
143                 printk("tuner: i2c i/o error 2\n");
144         if (i2c_master_send(client, buffer+4, 2) != 2)
145                 printk("tuner: i2c i/o error 3\n");
146         return 0;
147 }
148
149 static int 
150 tuner_detach(struct i2c_client *c)
151 {
152         MOD_DEC_USE_COUNT;
153         return 0;
154 }
155
156 static int 
157 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
158 {
159         int *iarg = (int*)arg;
160
161         switch (cmd) 
162         {
163                 case TUNER_SET_TVFREQ:
164                         set_tv_freq(client, *iarg);
165                         break;
166             
167                 default:
168                         return -EINVAL;
169         }
170         return 0;
171 }
172
173 static int 
174 tuner_probe(struct i2c_adapter *adap)
175 {
176         this_adap = 0;
177         if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_LP))
178                 return i2c_probe(adap, &addr_data, tuner_attach);
179         return 0;
180 }
181
182 /* ----------------------------------------------------------------------- */
183
184 static struct i2c_driver 
185 i2c_driver_tuner = 
186 {
187         .owner          =       THIS_MODULE,
188         .name           =       "sab3036",
189         .id             =       I2C_DRIVERID_SAB3036,
190         .flags          =       I2C_DF_NOTIFY,
191         .attach_adapter =       tuner_probe,
192         .detach_client  =       tuner_detach,
193         .command        =       tuner_command
194 };
195
196 static struct i2c_client client_template =
197 {
198         .id             = -1,
199         .driver         = &i2c_driver_tuner,
200         .dev            = {
201                 .name   = "SAB3036",
202         },
203 };
204
205 int __init
206 tuner3036_init(void)
207 {
208         i2c_add_driver(&i2c_driver_tuner);
209         return 0;
210 }
211
212 void __exit
213 tuner3036_exit(void)
214 {
215         i2c_del_driver(&i2c_driver_tuner);
216 }
217
218 MODULE_DESCRIPTION("SAB3036 tuner driver");
219 MODULE_AUTHOR("Philip Blundell <philb@gnu.org>");
220 MODULE_LICENSE("GPL");
221
222 MODULE_PARM(debug,"i");
223 MODULE_PARM_DESC(debug,"Enable debugging output");
224
225 module_init(tuner3036_init);
226 module_exit(tuner3036_exit);