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