]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * common keywest i2c layer | |
4 | * | |
5 | * Copyright (c) by Takashi Iwai <tiwai@suse.de> | |
1da177e4 LT |
6 | */ |
7 | ||
8 | ||
1da177e4 LT |
9 | #include <linux/init.h> |
10 | #include <linux/i2c.h> | |
11 | #include <linux/delay.h> | |
654e2751 | 12 | #include <linux/module.h> |
1da177e4 LT |
13 | #include <sound/core.h> |
14 | #include "pmac.h" | |
15 | ||
16 | /* | |
17 | * we have to keep a static variable here since i2c attach_adapter | |
18 | * callback cannot pass a private data. | |
19 | */ | |
65b29f50 | 20 | static struct pmac_keywest *keywest_ctx; |
1da177e4 | 21 | |
28760c19 | 22 | static bool keywest_probed; |
1da177e4 | 23 | |
5de4155b JD |
24 | static int keywest_probe(struct i2c_client *client, |
25 | const struct i2c_device_id *id) | |
26 | { | |
28760c19 WS |
27 | keywest_probed = true; |
28 | /* If instantiated via i2c-powermac, we still need to set the client */ | |
29 | if (!keywest_ctx->client) | |
30 | keywest_ctx->client = client; | |
5de4155b JD |
31 | i2c_set_clientdata(client, keywest_ctx); |
32 | return 0; | |
33 | } | |
34 | ||
35 | /* | |
36 | * This is kind of a hack, best would be to turn powermac to fixed i2c | |
37 | * bus numbers and declare the sound device as part of platform | |
38 | * initialization | |
39 | */ | |
1da177e4 LT |
40 | static int keywest_attach_adapter(struct i2c_adapter *adapter) |
41 | { | |
5de4155b | 42 | struct i2c_board_info info; |
04a9af2e | 43 | struct i2c_client *client; |
1da177e4 LT |
44 | |
45 | if (! keywest_ctx) | |
46 | return -EINVAL; | |
47 | ||
903dba1e | 48 | if (strncmp(adapter->name, "mac-io", 6)) |
ac397c80 | 49 | return -EINVAL; /* ignored */ |
1da177e4 | 50 | |
5de4155b JD |
51 | memset(&info, 0, sizeof(struct i2c_board_info)); |
52 | strlcpy(info.type, "keywest", I2C_NAME_SIZE); | |
53 | info.addr = keywest_ctx->addr; | |
04a9af2e WS |
54 | client = i2c_new_client_device(adapter, &info); |
55 | if (IS_ERR(client)) | |
56 | return PTR_ERR(client); | |
57 | keywest_ctx->client = client; | |
58 | ||
18c40784 TI |
59 | /* |
60 | * We know the driver is already loaded, so the device should be | |
61 | * already bound. If not it means binding failed, and then there | |
62 | * is no point in keeping the device instantiated. | |
63 | */ | |
a7cde6d2 | 64 | if (!keywest_ctx->client->dev.driver) { |
18c40784 TI |
65 | i2c_unregister_device(keywest_ctx->client); |
66 | keywest_ctx->client = NULL; | |
67 | return -ENODEV; | |
68 | } | |
1da177e4 | 69 | |
5de4155b JD |
70 | /* |
71 | * Let i2c-core delete that device on driver removal. | |
72 | * This is safe because i2c-core holds the core_lock mutex for us. | |
73 | */ | |
74 | list_add_tail(&keywest_ctx->client->detected, | |
a7cde6d2 | 75 | &to_i2c_driver(keywest_ctx->client->dev.driver)->clients); |
1da177e4 | 76 | return 0; |
1da177e4 LT |
77 | } |
78 | ||
5de4155b | 79 | static int keywest_remove(struct i2c_client *client) |
1da177e4 LT |
80 | { |
81 | if (! keywest_ctx) | |
82 | return 0; | |
83 | if (client == keywest_ctx->client) | |
84 | keywest_ctx->client = NULL; | |
85 | ||
1da177e4 LT |
86 | return 0; |
87 | } | |
88 | ||
5de4155b JD |
89 | |
90 | static const struct i2c_device_id keywest_i2c_id[] = { | |
28760c19 WS |
91 | { "MAC,tas3004", 0 }, /* instantiated by i2c-powermac */ |
92 | { "keywest", 0 }, /* instantiated by us if needed */ | |
5de4155b JD |
93 | { } |
94 | }; | |
a2bc2af6 | 95 | MODULE_DEVICE_TABLE(i2c, keywest_i2c_id); |
5de4155b | 96 | |
a656cbf0 | 97 | static struct i2c_driver keywest_driver = { |
5de4155b JD |
98 | .driver = { |
99 | .name = "PMac Keywest Audio", | |
100 | }, | |
5de4155b JD |
101 | .probe = keywest_probe, |
102 | .remove = keywest_remove, | |
103 | .id_table = keywest_i2c_id, | |
104 | }; | |
105 | ||
1da177e4 | 106 | /* exported */ |
65b29f50 | 107 | void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c) |
1da177e4 LT |
108 | { |
109 | if (keywest_ctx && keywest_ctx == i2c) { | |
110 | i2c_del_driver(&keywest_driver); | |
111 | keywest_ctx = NULL; | |
112 | } | |
113 | } | |
114 | ||
15afafc2 | 115 | int snd_pmac_tumbler_post_init(void) |
1da177e4 LT |
116 | { |
117 | int err; | |
118 | ||
783eaf46 TI |
119 | if (!keywest_ctx || !keywest_ctx->client) |
120 | return -ENXIO; | |
121 | ||
1da177e4 LT |
122 | if ((err = keywest_ctx->init_client(keywest_ctx)) < 0) { |
123 | snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err); | |
124 | return err; | |
125 | } | |
126 | return 0; | |
127 | } | |
128 | ||
129 | /* exported */ | |
15afafc2 | 130 | int snd_pmac_keywest_init(struct pmac_keywest *i2c) |
1da177e4 | 131 | { |
ac397c80 WS |
132 | struct i2c_adapter *adap; |
133 | int err, i = 0; | |
1da177e4 LT |
134 | |
135 | if (keywest_ctx) | |
136 | return -EBUSY; | |
137 | ||
ac397c80 WS |
138 | adap = i2c_get_adapter(0); |
139 | if (!adap) | |
140 | return -EPROBE_DEFER; | |
141 | ||
1da177e4 LT |
142 | keywest_ctx = i2c; |
143 | ||
144 | if ((err = i2c_add_driver(&keywest_driver))) { | |
145 | snd_printk(KERN_ERR "cannot register keywest i2c driver\n"); | |
ac397c80 | 146 | i2c_put_adapter(adap); |
1da177e4 LT |
147 | return err; |
148 | } | |
ac397c80 | 149 | |
28760c19 WS |
150 | /* There was already a device from i2c-powermac. Great, let's return */ |
151 | if (keywest_probed) | |
152 | return 0; | |
153 | ||
ac397c80 WS |
154 | /* We assume Macs have consecutive I2C bus numbers starting at 0 */ |
155 | while (adap) { | |
28760c19 | 156 | /* Scan for devices to be bound to */ |
ac397c80 WS |
157 | err = keywest_attach_adapter(adap); |
158 | if (!err) | |
159 | return 0; | |
160 | i2c_put_adapter(adap); | |
161 | adap = i2c_get_adapter(++i); | |
162 | } | |
163 | ||
164 | return -ENODEV; | |
1da177e4 | 165 | } |