aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/tda9840.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/media/video/tda9840.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/media/video/tda9840.c')
-rw-r--r--drivers/media/video/tda9840.c254
1 files changed, 254 insertions, 0 deletions
diff --git a/drivers/media/video/tda9840.c b/drivers/media/video/tda9840.c
new file mode 100644
index 000000000000..b5177c6f54f6
--- /dev/null
+++ b/drivers/media/video/tda9840.c
@@ -0,0 +1,254 @@
1 /*
2 tda9840 - i2c-driver for the tda9840 by SGS Thomson
3
4 Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
5
6 The tda9840 is a stereo/dual sound processor with digital
7 identification. It can be found at address 0x84 on the i2c-bus.
8
9 For detailed informations download the specifications directly
10 from SGS Thomson at http://www.st.com
11
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.
16
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.
21
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.
25 */
26
27#include <linux/module.h>
28#include <linux/ioctl.h>
29#include <linux/i2c.h>
30
31#include "tda9840.h"
32
33static int debug = 0; /* insmod parameter */
34module_param(debug, int, 0644);
35MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
36#define dprintk(args...) \
37 do { if (debug) { printk("%s: %s()[%d]: ",__stringify(KBUILD_MODNAME), __FUNCTION__, __LINE__); printk(args); } } while (0)
38
39#define SWITCH 0x00
40#define LEVEL_ADJUST 0x02
41#define STEREO_ADJUST 0x03
42#define TEST 0x04
43
44/* addresses to scan, found only at 0x42 (7-Bit) */
45static unsigned short normal_i2c[] = { I2C_TDA9840, I2C_CLIENT_END };
46static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
47
48/* magic definition of all other variables and things */
49I2C_CLIENT_INSMOD;
50
51static struct i2c_driver driver;
52static struct i2c_client client_template;
53
54static int command(struct i2c_client *client, unsigned int cmd, void *arg)
55{
56 int result;
57 int byte = *(int *)arg;
58
59 switch (cmd) {
60 case TDA9840_SWITCH:
61
62 dprintk("TDA9840_SWITCH: 0x%02x\n", byte);
63
64 if (byte != TDA9840_SET_MONO
65 && byte != TDA9840_SET_MUTE
66 && byte != TDA9840_SET_STEREO
67 && byte != TDA9840_SET_LANG1
68 && byte != TDA9840_SET_LANG2
69 && byte != TDA9840_SET_BOTH
70 && byte != TDA9840_SET_BOTH_R
71 && byte != TDA9840_SET_EXTERNAL) {
72 return -EINVAL;
73 }
74
75 result = i2c_smbus_write_byte_data(client, SWITCH, byte);
76 if (result)
77 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
78 break;
79
80 case TDA9840_LEVEL_ADJUST:
81
82 dprintk("TDA9840_LEVEL_ADJUST: %d\n", byte);
83
84 /* check for correct range */
85 if (byte > 25 || byte < -20)
86 return -EINVAL;
87
88 /* calculate actual value to set, see specs, page 18 */
89 byte /= 5;
90 if (0 < byte)
91 byte += 0x8;
92 else
93 byte = -byte;
94
95 result = i2c_smbus_write_byte_data(client, LEVEL_ADJUST, byte);
96 if (result)
97 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
98 break;
99
100 case TDA9840_STEREO_ADJUST:
101
102 dprintk("TDA9840_STEREO_ADJUST: %d\n", byte);
103
104 /* check for correct range */
105 if (byte > 25 || byte < -24)
106 return -EINVAL;
107
108 /* calculate actual value to set */
109 byte /= 5;
110 if (0 < byte)
111 byte += 0x20;
112 else
113 byte = -byte;
114
115 result = i2c_smbus_write_byte_data(client, STEREO_ADJUST, byte);
116 if (result)
117 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
118 break;
119
120 case TDA9840_DETECT: {
121 int *ret = (int *)arg;
122
123 byte = i2c_smbus_read_byte_data(client, STEREO_ADJUST);
124 if (byte == -1) {
125 dprintk("i2c_smbus_read_byte_data() failed\n");
126 return -EIO;
127 }
128
129 if (0 != (byte & 0x80)) {
130 dprintk("TDA9840_DETECT: register contents invalid\n");
131 return -EINVAL;
132 }
133
134 dprintk("TDA9840_DETECT: byte: 0x%02x\n", byte);
135 *ret = ((byte & 0x60) >> 5);
136 result = 0;
137 break;
138 }
139 case TDA9840_TEST:
140 dprintk("TDA9840_TEST: 0x%02x\n", byte);
141
142 /* mask out irrelevant bits */
143 byte &= 0x3;
144
145 result = i2c_smbus_write_byte_data(client, TEST, byte);
146 if (result)
147 dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
148 break;
149 default:
150 return -ENOIOCTLCMD;
151 }
152
153 if (result)
154 return -EIO;
155
156 return 0;
157}
158
159static int detect(struct i2c_adapter *adapter, int address, int kind)
160{
161 struct i2c_client *client;
162 int result = 0;
163
164 int byte = 0x0;
165
166 /* let's see whether this adapter can support what we need */
167 if (0 == i2c_check_functionality(adapter,
168 I2C_FUNC_SMBUS_READ_BYTE_DATA |
169 I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
170 return 0;
171 }
172
173 /* allocate memory for client structure */
174 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
175 if (0 == client) {
176 printk("not enough kernel memory\n");
177 return -ENOMEM;
178 }
179
180 /* fill client structure */
181 memcpy(client, &client_template, sizeof(struct i2c_client));
182 client->addr = address;
183 client->adapter = adapter;
184
185 /* tell the i2c layer a new client has arrived */
186 if (0 != (result = i2c_attach_client(client))) {
187 kfree(client);
188 return result;
189 }
190
191 /* set initial values for level & stereo - adjustment, mode */
192 byte = 0;
193 result = command(client, TDA9840_LEVEL_ADJUST, &byte);
194 result += command(client, TDA9840_STEREO_ADJUST, &byte);
195 byte = TDA9840_SET_MONO;
196 result = command(client, TDA9840_SWITCH, &byte);
197 if (result) {
198 dprintk("could not initialize tda9840\n");
199 return -ENODEV;
200 }
201
202 printk("tda9840: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]);
203 return 0;
204}
205
206static int attach(struct i2c_adapter *adapter)
207{
208 /* let's see whether this is a know adapter we can attach to */
209 if (adapter->id != I2C_ALGO_SAA7146) {
210 dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id);
211 return -ENODEV;
212 }
213
214 return i2c_probe(adapter, &addr_data, &detect);
215}
216
217static int detach(struct i2c_client *client)
218{
219 int ret = i2c_detach_client(client);
220 kfree(client);
221 return ret;
222}
223
224static struct i2c_driver driver = {
225 .owner = THIS_MODULE,
226 .name = "tda9840",
227 .id = I2C_DRIVERID_TDA9840,
228 .flags = I2C_DF_NOTIFY,
229 .attach_adapter = attach,
230 .detach_client = detach,
231 .command = command,
232};
233
234static struct i2c_client client_template = {
235 I2C_DEVNAME("tda9840"),
236 .driver = &driver,
237};
238
239static int __init this_module_init(void)
240{
241 return i2c_add_driver(&driver);
242}
243
244static void __exit this_module_exit(void)
245{
246 i2c_del_driver(&driver);
247}
248
249module_init(this_module_init);
250module_exit(this_module_exit);
251
252MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
253MODULE_DESCRIPTION("tda9840 driver");
254MODULE_LICENSE("GPL");