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