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