1
2
3
4
5
6
7
8
9#include <linux/init.h>
10#include <linux/i2c.h>
11#include <linux/delay.h>
12#include <linux/module.h>
13#include <sound/core.h>
14#include "pmac.h"
15
16static struct pmac_keywest *keywest_ctx;
17static bool keywest_probed;
18
19static int keywest_probe(struct i2c_client *client)
20{
21 keywest_probed = true;
22
23 if (!keywest_ctx->client)
24 keywest_ctx->client = client;
25 i2c_set_clientdata(client, keywest_ctx);
26 return 0;
27}
28
29
30
31
32
33
34static int keywest_attach_adapter(struct i2c_adapter *adapter)
35{
36 struct i2c_board_info info;
37 struct i2c_client *client;
38
39 if (! keywest_ctx)
40 return -EINVAL;
41
42 if (strncmp(adapter->name, "mac-io", 6))
43 return -EINVAL;
44
45 memset(&info, 0, sizeof(struct i2c_board_info));
46 strscpy(info.type, "keywest", I2C_NAME_SIZE);
47 info.addr = keywest_ctx->addr;
48 client = i2c_new_client_device(adapter, &info);
49 if (IS_ERR(client))
50 return PTR_ERR(client);
51 keywest_ctx->client = client;
52
53
54
55
56
57
58 if (!keywest_ctx->client->dev.driver) {
59 i2c_unregister_device(keywest_ctx->client);
60 keywest_ctx->client = NULL;
61 return -ENODEV;
62 }
63
64
65
66
67
68 list_add_tail(&keywest_ctx->client->detected,
69 &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
70 return 0;
71}
72
73static void keywest_remove(struct i2c_client *client)
74{
75 if (! keywest_ctx)
76 return;
77 if (client == keywest_ctx->client)
78 keywest_ctx->client = NULL;
79}
80
81
82static const struct i2c_device_id keywest_i2c_id[] = {
83 { "MAC,tas3004", 0 },
84 { "keywest", 0 },
85 { }
86};
87MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
88
89static struct i2c_driver keywest_driver = {
90 .driver = {
91 .name = "PMac Keywest Audio",
92 },
93 .probe = keywest_probe,
94 .remove = keywest_remove,
95 .id_table = keywest_i2c_id,
96};
97
98
99void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
100{
101 if (keywest_ctx && keywest_ctx == i2c) {
102 i2c_del_driver(&keywest_driver);
103 keywest_ctx = NULL;
104 }
105}
106
107int snd_pmac_tumbler_post_init(void)
108{
109 int err;
110
111 if (!keywest_ctx || !keywest_ctx->client)
112 return -ENXIO;
113
114 err = keywest_ctx->init_client(keywest_ctx);
115 if (err < 0) {
116 snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
117 return err;
118 }
119 return 0;
120}
121
122
123int snd_pmac_keywest_init(struct pmac_keywest *i2c)
124{
125 struct i2c_adapter *adap;
126 int err, i = 0;
127
128 if (keywest_ctx)
129 return -EBUSY;
130
131 adap = i2c_get_adapter(0);
132 if (!adap)
133 return -EPROBE_DEFER;
134
135 keywest_ctx = i2c;
136
137 err = i2c_add_driver(&keywest_driver);
138 if (err) {
139 snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
140 i2c_put_adapter(adap);
141 return err;
142 }
143
144
145 if (keywest_probed)
146 return 0;
147
148
149 while (adap) {
150
151 err = keywest_attach_adapter(adap);
152 if (!err)
153 return 0;
154 i2c_put_adapter(adap);
155 adap = i2c_get_adapter(++i);
156 }
157
158 return -ENODEV;
159}
160