11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * common keywest i2c layer 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (c) by Takashi Iwai <tiwai@suse.de> 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/init.h> 101da177e4SLinus Torvalds #include <linux/i2c.h> 111da177e4SLinus Torvalds #include <linux/delay.h> 12654e2751STakashi Iwai #include <linux/module.h> 131da177e4SLinus Torvalds #include <sound/core.h> 141da177e4SLinus Torvalds #include "pmac.h" 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds /* 171da177e4SLinus Torvalds * we have to keep a static variable here since i2c attach_adapter 181da177e4SLinus Torvalds * callback cannot pass a private data. 191da177e4SLinus Torvalds */ 2065b29f50STakashi Iwai static struct pmac_keywest *keywest_ctx; 211da177e4SLinus Torvalds 2228760c19SWolfram Sang static bool keywest_probed; 231da177e4SLinus Torvalds 245de4155bSJean Delvare static int keywest_probe(struct i2c_client *client, 255de4155bSJean Delvare const struct i2c_device_id *id) 265de4155bSJean Delvare { 2728760c19SWolfram Sang keywest_probed = true; 2828760c19SWolfram Sang /* If instantiated via i2c-powermac, we still need to set the client */ 2928760c19SWolfram Sang if (!keywest_ctx->client) 3028760c19SWolfram Sang keywest_ctx->client = client; 315de4155bSJean Delvare i2c_set_clientdata(client, keywest_ctx); 325de4155bSJean Delvare return 0; 335de4155bSJean Delvare } 345de4155bSJean Delvare 355de4155bSJean Delvare /* 365de4155bSJean Delvare * This is kind of a hack, best would be to turn powermac to fixed i2c 375de4155bSJean Delvare * bus numbers and declare the sound device as part of platform 385de4155bSJean Delvare * initialization 395de4155bSJean Delvare */ 401da177e4SLinus Torvalds static int keywest_attach_adapter(struct i2c_adapter *adapter) 411da177e4SLinus Torvalds { 425de4155bSJean Delvare struct i2c_board_info info; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds if (! keywest_ctx) 451da177e4SLinus Torvalds return -EINVAL; 461da177e4SLinus Torvalds 47903dba1eSJean Delvare if (strncmp(adapter->name, "mac-io", 6)) 48ac397c80SWolfram Sang return -EINVAL; /* ignored */ 491da177e4SLinus Torvalds 505de4155bSJean Delvare memset(&info, 0, sizeof(struct i2c_board_info)); 515de4155bSJean Delvare strlcpy(info.type, "keywest", I2C_NAME_SIZE); 525de4155bSJean Delvare info.addr = keywest_ctx->addr; 535de4155bSJean Delvare keywest_ctx->client = i2c_new_device(adapter, &info); 5418c40784STakashi Iwai if (!keywest_ctx->client) 5518c40784STakashi Iwai return -ENODEV; 5618c40784STakashi Iwai /* 5718c40784STakashi Iwai * We know the driver is already loaded, so the device should be 5818c40784STakashi Iwai * already bound. If not it means binding failed, and then there 5918c40784STakashi Iwai * is no point in keeping the device instantiated. 6018c40784STakashi Iwai */ 61a7cde6d2SLars-Peter Clausen if (!keywest_ctx->client->dev.driver) { 6218c40784STakashi Iwai i2c_unregister_device(keywest_ctx->client); 6318c40784STakashi Iwai keywest_ctx->client = NULL; 6418c40784STakashi Iwai return -ENODEV; 6518c40784STakashi Iwai } 661da177e4SLinus Torvalds 675de4155bSJean Delvare /* 685de4155bSJean Delvare * Let i2c-core delete that device on driver removal. 695de4155bSJean Delvare * This is safe because i2c-core holds the core_lock mutex for us. 705de4155bSJean Delvare */ 715de4155bSJean Delvare list_add_tail(&keywest_ctx->client->detected, 72a7cde6d2SLars-Peter Clausen &to_i2c_driver(keywest_ctx->client->dev.driver)->clients); 731da177e4SLinus Torvalds return 0; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 765de4155bSJean Delvare static int keywest_remove(struct i2c_client *client) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds if (! keywest_ctx) 791da177e4SLinus Torvalds return 0; 801da177e4SLinus Torvalds if (client == keywest_ctx->client) 811da177e4SLinus Torvalds keywest_ctx->client = NULL; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds return 0; 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds 865de4155bSJean Delvare 875de4155bSJean Delvare static const struct i2c_device_id keywest_i2c_id[] = { 8828760c19SWolfram Sang { "MAC,tas3004", 0 }, /* instantiated by i2c-powermac */ 8928760c19SWolfram Sang { "keywest", 0 }, /* instantiated by us if needed */ 905de4155bSJean Delvare { } 915de4155bSJean Delvare }; 92a2bc2af6SJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, keywest_i2c_id); 935de4155bSJean Delvare 94a656cbf0SJean Delvare static struct i2c_driver keywest_driver = { 955de4155bSJean Delvare .driver = { 965de4155bSJean Delvare .name = "PMac Keywest Audio", 975de4155bSJean Delvare }, 985de4155bSJean Delvare .probe = keywest_probe, 995de4155bSJean Delvare .remove = keywest_remove, 1005de4155bSJean Delvare .id_table = keywest_i2c_id, 1015de4155bSJean Delvare }; 1025de4155bSJean Delvare 1031da177e4SLinus Torvalds /* exported */ 10465b29f50STakashi Iwai void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c) 1051da177e4SLinus Torvalds { 1061da177e4SLinus Torvalds if (keywest_ctx && keywest_ctx == i2c) { 1071da177e4SLinus Torvalds i2c_del_driver(&keywest_driver); 1081da177e4SLinus Torvalds keywest_ctx = NULL; 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 11215afafc2SBill Pemberton int snd_pmac_tumbler_post_init(void) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds int err; 1151da177e4SLinus Torvalds 116783eaf46STakashi Iwai if (!keywest_ctx || !keywest_ctx->client) 117783eaf46STakashi Iwai return -ENXIO; 118783eaf46STakashi Iwai 1191da177e4SLinus Torvalds if ((err = keywest_ctx->init_client(keywest_ctx)) < 0) { 1201da177e4SLinus Torvalds snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err); 1211da177e4SLinus Torvalds return err; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds return 0; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* exported */ 12715afafc2SBill Pemberton int snd_pmac_keywest_init(struct pmac_keywest *i2c) 1281da177e4SLinus Torvalds { 129ac397c80SWolfram Sang struct i2c_adapter *adap; 130ac397c80SWolfram Sang int err, i = 0; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds if (keywest_ctx) 1331da177e4SLinus Torvalds return -EBUSY; 1341da177e4SLinus Torvalds 135ac397c80SWolfram Sang adap = i2c_get_adapter(0); 136ac397c80SWolfram Sang if (!adap) 137ac397c80SWolfram Sang return -EPROBE_DEFER; 138ac397c80SWolfram Sang 1391da177e4SLinus Torvalds keywest_ctx = i2c; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds if ((err = i2c_add_driver(&keywest_driver))) { 1421da177e4SLinus Torvalds snd_printk(KERN_ERR "cannot register keywest i2c driver\n"); 143ac397c80SWolfram Sang i2c_put_adapter(adap); 1441da177e4SLinus Torvalds return err; 1451da177e4SLinus Torvalds } 146ac397c80SWolfram Sang 14728760c19SWolfram Sang /* There was already a device from i2c-powermac. Great, let's return */ 14828760c19SWolfram Sang if (keywest_probed) 14928760c19SWolfram Sang return 0; 15028760c19SWolfram Sang 151ac397c80SWolfram Sang /* We assume Macs have consecutive I2C bus numbers starting at 0 */ 152ac397c80SWolfram Sang while (adap) { 15328760c19SWolfram Sang /* Scan for devices to be bound to */ 154ac397c80SWolfram Sang err = keywest_attach_adapter(adap); 155ac397c80SWolfram Sang if (!err) 1561da177e4SLinus Torvalds return 0; 157ac397c80SWolfram Sang i2c_put_adapter(adap); 158ac397c80SWolfram Sang adap = i2c_get_adapter(++i); 159ac397c80SWolfram Sang } 160ac397c80SWolfram Sang 161ac397c80SWolfram Sang return -ENODEV; 1621da177e4SLinus Torvalds } 163