blob: bdf506e6ae279a8fd296e2096f80eb75319b6ba8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03008 * the support into that file. So instead we have a separate driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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/init.h>
25
26#include <linux/i2c.h>
27#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030028#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <media/tuner.h>
31
32static int debug; /* insmod parameter */
33static int this_adap;
34
35static struct i2c_client client_template;
36
37/* Addresses to scan */
Jean Delvareb3d5496e2005-04-02 20:31:02 +020038static unsigned short normal_i2c[] = { 0x60, 0x61, I2C_CLIENT_END };
Jean Delvare68cc9d02005-04-02 20:04:41 +020039static unsigned short ignore = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static struct i2c_client_address_data addr_data = {
Jean Delvareb3d5496e2005-04-02 20:31:02 +020042 .normal_i2c = normal_i2c,
Jean Delvare68cc9d02005-04-02 20:04:41 +020043 .probe = &ignore,
44 .ignore = &ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
47/* ---------------------------------------------------------------------- */
48
49static unsigned char
50tuner_getstatus (struct i2c_client *c)
51{
52 unsigned char byte;
53 if (i2c_master_recv(c, &byte, 1) != 1)
54 printk(KERN_ERR "tuner-3036: I/O error.\n");
55 return byte;
56}
57
58#define TUNER_FL 0x80
59
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030060static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070061tuner_islocked (struct i2c_client *c)
62{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030063 return (tuner_getstatus(c) & TUNER_FL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66/* ---------------------------------------------------------------------- */
67
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030068static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070069set_tv_freq(struct i2c_client *c, int freq)
70{
71 u16 div = ((freq * 20) / 16);
72 unsigned long give_up = jiffies + HZ;
73 unsigned char buffer[2];
74
75 if (debug)
76 printk(KERN_DEBUG "tuner: setting frequency %dMHz, divisor %x\n", freq / 16, div);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* Select high tuning current */
79 buffer[0] = 0x29;
80 buffer[1] = 0x3e;
81
82 if (i2c_master_send(c, buffer, 2) != 2)
83 printk("tuner: i2c i/o error 1\n");
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 buffer[0] = 0x80 | ((div>>8) & 0x7f);
86 buffer[1] = div & 0xff;
87
88 if (i2c_master_send(c, buffer, 2) != 2)
89 printk("tuner: i2c i/o error 2\n");
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 while (!tuner_islocked(c) && time_before(jiffies, give_up))
92 schedule();
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (!tuner_islocked(c))
95 printk(KERN_WARNING "tuner: failed to achieve PLL lock\n");
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* Select low tuning current and engage AFC */
98 buffer[0] = 0x29;
99 buffer[1] = 0xb2;
100
101 if (i2c_master_send(c, buffer, 2) != 2)
102 printk("tuner: i2c i/o error 3\n");
103
104 if (debug)
105 printk(KERN_DEBUG "tuner: status %02x\n", tuner_getstatus(c));
106}
107
108/* ---------------------------------------------------------------------- */
109
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300110static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111tuner_attach(struct i2c_adapter *adap, int addr, int kind)
112{
113 static unsigned char buffer[] = { 0x29, 0x32, 0x2a, 0, 0x2b, 0 };
114
115 struct i2c_client *client;
116
117 if (this_adap > 0)
118 return -1;
119 this_adap++;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300120
121 client_template.adapter = adap;
122 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300125 if (client == NULL)
126 return -ENOMEM;
127 memcpy(client, &client_template, sizeof(struct i2c_client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 printk("tuner: SAB3036 found, status %02x\n", tuner_getstatus(client));
130
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300131 i2c_attach_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (i2c_master_send(client, buffer, 2) != 2)
134 printk("tuner: i2c i/o error 1\n");
135 if (i2c_master_send(client, buffer+2, 2) != 2)
136 printk("tuner: i2c i/o error 2\n");
137 if (i2c_master_send(client, buffer+4, 2) != 2)
138 printk("tuner: i2c i/o error 3\n");
139 return 0;
140}
141
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300142static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143tuner_detach(struct i2c_client *c)
144{
145 return 0;
146}
147
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300148static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
150{
151 int *iarg = (int*)arg;
152
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300153 switch (cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 {
Mauro Carvalho Chehabaf9eeed2005-07-12 13:59:06 -0700155 case VIDIOCSFREQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 set_tv_freq(client, *iarg);
157 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 default:
160 return -EINVAL;
161 }
162 return 0;
163}
164
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300165static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166tuner_probe(struct i2c_adapter *adap)
167{
168 this_adap = 0;
Jean Delvarec7a46532005-08-11 23:41:56 +0200169 if (adap->id == I2C_HW_B_LP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return i2c_probe(adap, &addr_data, tuner_attach);
171 return 0;
172}
173
174/* ----------------------------------------------------------------------- */
175
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300176static struct i2c_driver
177i2c_driver_tuner =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Laurent Riffard604f28e2005-11-26 20:43:39 +0100179 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100180 .name = "sab3036",
181 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .id = I2C_DRIVERID_SAB3036,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .attach_adapter = tuner_probe,
184 .detach_client = tuner_detach,
185 .command = tuner_command
186};
187
188static struct i2c_client client_template =
189{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300190 .driver = &i2c_driver_tuner,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .name = "SAB3036",
192};
193
194static int __init
195tuner3036_init(void)
196{
Arthur Othienod4437d32006-01-11 19:41:04 -0200197 return i2c_add_driver(&i2c_driver_tuner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200static void __exit
201tuner3036_exit(void)
202{
203 i2c_del_driver(&i2c_driver_tuner);
204}
205
206MODULE_DESCRIPTION("SAB3036 tuner driver");
207MODULE_AUTHOR("Philip Blundell <philb@gnu.org>");
208MODULE_LICENSE("GPL");
209
210module_param(debug, int, 0);
211MODULE_PARM_DESC(debug,"Enable debugging output");
212
213module_init(tuner3036_init);
214module_exit(tuner3036_exit);