1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * common keywest i2c layer 4 * 5 * Copyright (c) by Takashi Iwai <tiwai@suse.de> 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 16 static struct pmac_keywest *keywest_ctx; 17 static bool keywest_probed; 18 19 static int keywest_probe(struct i2c_client *client) 20 { 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); 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 */ 34 static 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; /* ignored */ 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 * 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 */ 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 return 0; 65 } 66 67 static void keywest_remove(struct i2c_client *client) 68 { 69 if (! keywest_ctx) 70 return; 71 if (client == keywest_ctx->client) 72 keywest_ctx->client = NULL; 73 } 74 75 76 static const struct i2c_device_id keywest_i2c_id[] = { 77 { "MAC,tas3004" }, /* instantiated by i2c-powermac */ 78 { "keywest" }, /* instantiated by us if needed */ 79 { } 80 }; 81 MODULE_DEVICE_TABLE(i2c, keywest_i2c_id); 82 83 static struct i2c_driver keywest_driver = { 84 .driver = { 85 .name = "PMac Keywest Audio", 86 }, 87 .probe = keywest_probe, 88 .remove = keywest_remove, 89 .id_table = keywest_i2c_id, 90 }; 91 92 /* exported */ 93 void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c) 94 { 95 if (keywest_ctx && keywest_ctx == i2c) { 96 i2c_unregister_device(keywest_ctx->client); 97 i2c_del_driver(&keywest_driver); 98 keywest_ctx = NULL; 99 } 100 } 101 102 int snd_pmac_tumbler_post_init(void) 103 { 104 int err; 105 106 if (!keywest_ctx || !keywest_ctx->client) 107 return -ENXIO; 108 109 err = keywest_ctx->init_client(keywest_ctx); 110 if (err < 0) { 111 dev_err(&keywest_ctx->client->dev, 112 "tumbler: %i :cannot initialize the MCS\n", err); 113 return err; 114 } 115 return 0; 116 } 117 118 /* exported */ 119 int snd_pmac_keywest_init(struct pmac_keywest *i2c) 120 { 121 struct i2c_adapter *adap; 122 int err, i = 0; 123 124 if (keywest_ctx) 125 return -EBUSY; 126 127 adap = i2c_get_adapter(0); 128 if (!adap) 129 return -EPROBE_DEFER; 130 131 keywest_ctx = i2c; 132 133 err = i2c_add_driver(&keywest_driver); 134 if (err) { 135 dev_err(&i2c->client->dev, "cannot register keywest i2c driver\n"); 136 i2c_put_adapter(adap); 137 return err; 138 } 139 140 /* There was already a device from i2c-powermac. Great, let's return */ 141 if (keywest_probed) 142 return 0; 143 144 /* We assume Macs have consecutive I2C bus numbers starting at 0 */ 145 while (adap) { 146 /* Scan for devices to be bound to */ 147 err = keywest_attach_adapter(adap); 148 if (!err) 149 return 0; 150 i2c_put_adapter(adap); 151 adap = i2c_get_adapter(++i); 152 } 153 154 return -ENODEV; 155 } 156