1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * common keywest i2c layer
5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
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>
16 static struct pmac_keywest *keywest_ctx;
17 static bool keywest_probed;
19 static int keywest_probe(struct i2c_client *client)
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;
25 i2c_set_clientdata(client, keywest_ctx);
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
34 static int keywest_attach_adapter(struct i2c_adapter *adapter)
36 struct i2c_board_info info;
37 struct i2c_client *client;
42 if (strncmp(adapter->name, "mac-io", 6))
43 return -EINVAL; /* ignored */
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);
50 return PTR_ERR(client);
51 keywest_ctx->client = client;
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.
58 if (!keywest_ctx->client->dev.driver) {
59 i2c_unregister_device(keywest_ctx->client);
60 keywest_ctx->client = NULL;
65 * Let i2c-core delete that device on driver removal.
66 * This is safe because i2c-core holds the core_lock mutex for us.
68 list_add_tail(&keywest_ctx->client->detected,
69 &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
73 static void keywest_remove(struct i2c_client *client)
77 if (client == keywest_ctx->client)
78 keywest_ctx->client = NULL;
82 static const struct i2c_device_id keywest_i2c_id[] = {
83 { "MAC,tas3004", 0 }, /* instantiated by i2c-powermac */
84 { "keywest", 0 }, /* instantiated by us if needed */
87 MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
89 static struct i2c_driver keywest_driver = {
91 .name = "PMac Keywest Audio",
93 .probe = keywest_probe,
94 .remove = keywest_remove,
95 .id_table = keywest_i2c_id,
99 void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
101 if (keywest_ctx && keywest_ctx == i2c) {
102 i2c_del_driver(&keywest_driver);
107 int snd_pmac_tumbler_post_init(void)
111 if (!keywest_ctx || !keywest_ctx->client)
114 err = keywest_ctx->init_client(keywest_ctx);
116 snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
123 int snd_pmac_keywest_init(struct pmac_keywest *i2c)
125 struct i2c_adapter *adap;
131 adap = i2c_get_adapter(0);
133 return -EPROBE_DEFER;
137 err = i2c_add_driver(&keywest_driver);
139 snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
140 i2c_put_adapter(adap);
144 /* There was already a device from i2c-powermac. Great, let's return */
148 /* We assume Macs have consecutive I2C bus numbers starting at 0 */
150 /* Scan for devices to be bound to */
151 err = keywest_attach_adapter(adap);
154 i2c_put_adapter(adap);
155 adap = i2c_get_adapter(++i);