blob: 5e2c0aed5472a9c962d138354f0d2df5726c90dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/module.h>
2#include <linux/slab.h>
3#include <linux/proc_fs.h>
4#include <linux/ioport.h>
5#include <linux/sysctl.h>
6#include <linux/types.h>
7#include <linux/i2c.h>
8#include <linux/init.h>
9#include <linux/soundcard.h>
10#include <asm/uaccess.h>
11#include <asm/errno.h>
12#include <asm/io.h>
13#include <asm/prom.h>
14
15#include "tas_common.h"
16
17#define CALL0(proc) \
18 do { \
19 struct tas_data_t *self; \
20 if (!tas_client || driver_hooks == NULL) \
21 return -1; \
22 self = dev_get_drvdata(&tas_client->dev); \
23 if (driver_hooks->proc) \
24 return driver_hooks->proc(self); \
25 else \
26 return -EINVAL; \
27 } while (0)
28
29#define CALL(proc,arg...) \
30 do { \
31 struct tas_data_t *self; \
32 if (!tas_client || driver_hooks == NULL) \
33 return -1; \
34 self = dev_get_drvdata(&tas_client->dev); \
35 if (driver_hooks->proc) \
36 return driver_hooks->proc(self, ## arg); \
37 else \
38 return -EINVAL; \
39 } while (0)
40
41
42static u8 tas_i2c_address = 0x34;
43static struct i2c_client *tas_client;
44static struct device_node* tas_node;
45
46static int tas_attach_adapter(struct i2c_adapter *);
47static int tas_detach_client(struct i2c_client *);
48
49struct i2c_driver tas_driver = {
Laurent Riffardd74cdab2005-11-26 20:46:32 +010050 .driver = {
Laurent Riffardd74cdab2005-11-26 20:46:32 +010051 .name = "tas",
52 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .attach_adapter = tas_attach_adapter,
54 .detach_client = tas_detach_client,
55};
56
57struct tas_driver_hooks_t *driver_hooks;
58
59int
60tas_register_driver(struct tas_driver_hooks_t *hooks)
61{
62 driver_hooks = hooks;
63 return 0;
64}
65
66int
67tas_get_mixer_level(int mixer, uint *level)
68{
69 CALL(get_mixer_level,mixer,level);
70}
71
72int
73tas_set_mixer_level(int mixer,uint level)
74{
75 CALL(set_mixer_level,mixer,level);
76}
77
78int
79tas_enter_sleep(void)
80{
81 CALL0(enter_sleep);
82}
83
84int
85tas_leave_sleep(void)
86{
87 CALL0(leave_sleep);
88}
89
90int
91tas_supported_mixers(void)
92{
93 CALL0(supported_mixers);
94}
95
96int
97tas_mixer_is_stereo(int mixer)
98{
99 CALL(mixer_is_stereo,mixer);
100}
101
102int
103tas_stereo_mixers(void)
104{
105 CALL0(stereo_mixers);
106}
107
108int
109tas_output_device_change(int device_id,int layout_id,int speaker_id)
110{
111 CALL(output_device_change,device_id,layout_id,speaker_id);
112}
113
114int
115tas_device_ioctl(u_int cmd, u_long arg)
116{
117 CALL(device_ioctl,cmd,arg);
118}
119
120int
121tas_post_init(void)
122{
123 CALL0(post_init);
124}
125
126static int
127tas_detect_client(struct i2c_adapter *adapter, int address)
128{
129 static const char *client_name = "tas Digital Equalizer";
130 struct i2c_client *new_client;
131 int rc = -ENODEV;
132
133 if (!driver_hooks) {
134 printk(KERN_ERR "tas_detect_client called with no hooks !\n");
135 return -ENODEV;
136 }
137
Robert P. J. Day3159f062007-02-14 00:33:16 -0800138 new_client = kzalloc(sizeof(*new_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (!new_client)
140 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 new_client->addr = address;
143 new_client->adapter = adapter;
144 new_client->driver = &tas_driver;
145 strlcpy(new_client->name, client_name, DEVICE_NAME_SIZE);
146
147 if (driver_hooks->init(new_client))
148 goto bail;
149
150 /* Tell the i2c layer a new client has arrived */
151 if (i2c_attach_client(new_client)) {
152 driver_hooks->uninit(dev_get_drvdata(&new_client->dev));
153 goto bail;
154 }
155
156 tas_client = new_client;
157 return 0;
158 bail:
159 tas_client = NULL;
160 kfree(new_client);
161 return rc;
162}
163
164static int
165tas_attach_adapter(struct i2c_adapter *adapter)
166{
167 if (!strncmp(adapter->name, "mac-io", 6))
168 return tas_detect_client(adapter, tas_i2c_address);
169 return 0;
170}
171
172static int
173tas_detach_client(struct i2c_client *client)
174{
175 if (client == tas_client) {
176 driver_hooks->uninit(dev_get_drvdata(&client->dev));
177
178 i2c_detach_client(client);
179 kfree(client);
180 }
181 return 0;
182}
183
184void
185tas_cleanup(void)
186{
187 i2c_del_driver(&tas_driver);
188}
189
190int __init
191tas_init(int driver_id, const char *driver_name)
192{
Stephen Rothwella7edd0e2007-04-03 10:52:17 +1000193 const u32* paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 printk(KERN_INFO "tas driver [%s])\n", driver_name);
196
Benjamin Herrenschmidt4d6c5882006-04-21 15:04:22 +1000197#ifndef CONFIG_I2C_POWERMAC
198 request_module("i2c-powermac");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#endif
200 tas_node = find_devices("deq");
201 if (tas_node == NULL)
202 return -ENODEV;
Stephen Rothwella7edd0e2007-04-03 10:52:17 +1000203 paddr = get_property(tas_node, "i2c-address", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (paddr) {
205 tas_i2c_address = (*paddr) >> 1;
206 printk(KERN_INFO "using i2c address: 0x%x from device-tree\n",
207 tas_i2c_address);
208 } else
209 printk(KERN_INFO "using i2c address: 0x%x (default)\n",
210 tas_i2c_address);
211
212 return i2c_add_driver(&tas_driver);
213}