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