cris: Fixup compile problems
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / tda9840.c
blob2437c1a269c58e98890ecf3f91c811c5d577938f
1 /*
2 tda9840 - i2c-driver for the tda9840 by SGS Thomson
4 Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
6 The tda9840 is a stereo/dual sound processor with digital
7 identification. It can be found at address 0x84 on the i2c-bus.
9 For detailed informations download the specifications directly
10 from SGS Thomson at http://www.st.com
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/module.h>
29 #include <linux/ioctl.h>
30 #include <linux/i2c.h>
32 #include "tda9840.h"
34 static int debug; /* insmod parameter */
35 module_param(debug, int, 0644);
36 MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
38 #define dprintk(args...) \
39 do { if (debug) { printk("%s: %s()[%d]: ", KBUILD_MODNAME, __func__, __LINE__); printk(args); } } while (0)
41 #define SWITCH 0x00
42 #define LEVEL_ADJUST 0x02
43 #define STEREO_ADJUST 0x03
44 #define TEST 0x04
46 /* addresses to scan, found only at 0x42 (7-Bit) */
47 static unsigned short normal_i2c[] = { I2C_ADDR_TDA9840, I2C_CLIENT_END };
49 /* magic definition of all other variables and things */
50 I2C_CLIENT_INSMOD;
52 static struct i2c_driver driver;
53 static struct i2c_client client_template;
55 static int command(struct i2c_client *client, unsigned int cmd, void *arg)
57 int result;
58 int byte = *(int *)arg;
60 switch (cmd) {
61 case TDA9840_SWITCH:
63 dprintk("TDA9840_SWITCH: 0x%02x\n", byte);
65 if (byte != TDA9840_SET_MONO
66 && byte != TDA9840_SET_MUTE
67 && byte != TDA9840_SET_STEREO
68 && byte != TDA9840_SET_LANG1
69 && byte != TDA9840_SET_LANG2
70 && byte != TDA9840_SET_BOTH
71 && byte != TDA9840_SET_BOTH_R
72 && byte != TDA9840_SET_EXTERNAL) {
73 return -EINVAL;
76 result = i2c_smbus_write_byte_data(client, SWITCH, byte);
77 if (result)
78 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
79 break;
81 case TDA9840_LEVEL_ADJUST:
83 dprintk("TDA9840_LEVEL_ADJUST: %d\n", byte);
85 /* check for correct range */
86 if (byte > 25 || byte < -20)
87 return -EINVAL;
89 /* calculate actual value to set, see specs, page 18 */
90 byte /= 5;
91 if (0 < byte)
92 byte += 0x8;
93 else
94 byte = -byte;
96 result = i2c_smbus_write_byte_data(client, LEVEL_ADJUST, byte);
97 if (result)
98 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
99 break;
101 case TDA9840_STEREO_ADJUST:
103 dprintk("TDA9840_STEREO_ADJUST: %d\n", byte);
105 /* check for correct range */
106 if (byte > 25 || byte < -24)
107 return -EINVAL;
109 /* calculate actual value to set */
110 byte /= 5;
111 if (0 < byte)
112 byte += 0x20;
113 else
114 byte = -byte;
116 result = i2c_smbus_write_byte_data(client, STEREO_ADJUST, byte);
117 if (result)
118 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
119 break;
121 case TDA9840_DETECT: {
122 int *ret = (int *)arg;
124 byte = i2c_smbus_read_byte_data(client, STEREO_ADJUST);
125 if (byte == -1) {
126 dprintk("i2c_smbus_read_byte_data() failed\n");
127 return -EIO;
130 if (0 != (byte & 0x80)) {
131 dprintk("TDA9840_DETECT: register contents invalid\n");
132 return -EINVAL;
135 dprintk("TDA9840_DETECT: byte: 0x%02x\n", byte);
136 *ret = ((byte & 0x60) >> 5);
137 result = 0;
138 break;
140 case TDA9840_TEST:
141 dprintk("TDA9840_TEST: 0x%02x\n", byte);
143 /* mask out irrelevant bits */
144 byte &= 0x3;
146 result = i2c_smbus_write_byte_data(client, TEST, byte);
147 if (result)
148 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
149 break;
150 default:
151 return -ENOIOCTLCMD;
154 if (result)
155 return -EIO;
157 return 0;
160 static int detect(struct i2c_adapter *adapter, int address, int kind)
162 struct i2c_client *client;
163 int result = 0;
165 int byte = 0x0;
167 /* let's see whether this adapter can support what we need */
168 if (0 == i2c_check_functionality(adapter,
169 I2C_FUNC_SMBUS_READ_BYTE_DATA |
170 I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
171 return 0;
174 /* allocate memory for client structure */
175 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
176 if (!client) {
177 printk("not enough kernel memory\n");
178 return -ENOMEM;
181 /* fill client structure */
182 memcpy(client, &client_template, sizeof(struct i2c_client));
183 client->addr = address;
184 client->adapter = adapter;
186 /* tell the i2c layer a new client has arrived */
187 if (0 != (result = i2c_attach_client(client))) {
188 kfree(client);
189 return result;
192 /* set initial values for level & stereo - adjustment, mode */
193 byte = 0;
194 result = command(client, TDA9840_LEVEL_ADJUST, &byte);
195 result += command(client, TDA9840_STEREO_ADJUST, &byte);
196 byte = TDA9840_SET_MONO;
197 result = command(client, TDA9840_SWITCH, &byte);
198 if (result) {
199 dprintk("could not initialize tda9840\n");
200 return -ENODEV;
203 printk("tda9840: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]);
204 return 0;
207 static int attach(struct i2c_adapter *adapter)
209 /* let's see whether this is a know adapter we can attach to */
210 if (adapter->id != I2C_HW_SAA7146) {
211 dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id);
212 return -ENODEV;
215 return i2c_probe(adapter, &addr_data, &detect);
218 static int detach(struct i2c_client *client)
220 int ret = i2c_detach_client(client);
221 kfree(client);
222 return ret;
225 static struct i2c_driver driver = {
226 .driver = {
227 .name = "tda9840",
229 .id = I2C_DRIVERID_TDA9840,
230 .attach_adapter = attach,
231 .detach_client = detach,
232 .command = command,
235 static struct i2c_client client_template = {
236 .name = "tda9840",
237 .driver = &driver,
240 static int __init this_module_init(void)
242 return i2c_add_driver(&driver);
245 static void __exit this_module_exit(void)
247 i2c_del_driver(&driver);
250 module_init(this_module_init);
251 module_exit(this_module_exit);
253 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
254 MODULE_DESCRIPTION("tda9840 driver");
255 MODULE_LICENSE("GPL");