xref: /openbmc/linux/sound/ppc/keywest.c (revision a2bc2af6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * common keywest i2c layer
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) by Takashi Iwai <tiwai@suse.de>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
81da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
91da177e4SLinus Torvalds  *   (at your option) any later version.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
121da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
131da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141da177e4SLinus Torvalds  *   GNU General Public License for more details.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
171da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
181da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <linux/init.h>
231da177e4SLinus Torvalds #include <linux/i2c.h>
241da177e4SLinus Torvalds #include <linux/delay.h>
251da177e4SLinus Torvalds #include <sound/core.h>
261da177e4SLinus Torvalds #include "pmac.h"
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  * we have to keep a static variable here since i2c attach_adapter
301da177e4SLinus Torvalds  * callback cannot pass a private data.
311da177e4SLinus Torvalds  */
3265b29f50STakashi Iwai static struct pmac_keywest *keywest_ctx;
331da177e4SLinus Torvalds 
3428760c19SWolfram Sang static bool keywest_probed;
351da177e4SLinus Torvalds 
365de4155bSJean Delvare static int keywest_probe(struct i2c_client *client,
375de4155bSJean Delvare 			 const struct i2c_device_id *id)
385de4155bSJean Delvare {
3928760c19SWolfram Sang 	keywest_probed = true;
4028760c19SWolfram Sang 	/* If instantiated via i2c-powermac, we still need to set the client */
4128760c19SWolfram Sang 	if (!keywest_ctx->client)
4228760c19SWolfram Sang 		keywest_ctx->client = client;
435de4155bSJean Delvare 	i2c_set_clientdata(client, keywest_ctx);
445de4155bSJean Delvare 	return 0;
455de4155bSJean Delvare }
465de4155bSJean Delvare 
475de4155bSJean Delvare /*
485de4155bSJean Delvare  * This is kind of a hack, best would be to turn powermac to fixed i2c
495de4155bSJean Delvare  * bus numbers and declare the sound device as part of platform
505de4155bSJean Delvare  * initialization
515de4155bSJean Delvare  */
521da177e4SLinus Torvalds static int keywest_attach_adapter(struct i2c_adapter *adapter)
531da177e4SLinus Torvalds {
545de4155bSJean Delvare 	struct i2c_board_info info;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	if (! keywest_ctx)
571da177e4SLinus Torvalds 		return -EINVAL;
581da177e4SLinus Torvalds 
59903dba1eSJean Delvare 	if (strncmp(adapter->name, "mac-io", 6))
60ac397c80SWolfram Sang 		return -EINVAL; /* ignored */
611da177e4SLinus Torvalds 
625de4155bSJean Delvare 	memset(&info, 0, sizeof(struct i2c_board_info));
635de4155bSJean Delvare 	strlcpy(info.type, "keywest", I2C_NAME_SIZE);
645de4155bSJean Delvare 	info.addr = keywest_ctx->addr;
655de4155bSJean Delvare 	keywest_ctx->client = i2c_new_device(adapter, &info);
6618c40784STakashi Iwai 	if (!keywest_ctx->client)
6718c40784STakashi Iwai 		return -ENODEV;
6818c40784STakashi Iwai 	/*
6918c40784STakashi Iwai 	 * We know the driver is already loaded, so the device should be
7018c40784STakashi Iwai 	 * already bound. If not it means binding failed, and then there
7118c40784STakashi Iwai 	 * is no point in keeping the device instantiated.
7218c40784STakashi Iwai 	 */
73a7cde6d2SLars-Peter Clausen 	if (!keywest_ctx->client->dev.driver) {
7418c40784STakashi Iwai 		i2c_unregister_device(keywest_ctx->client);
7518c40784STakashi Iwai 		keywest_ctx->client = NULL;
7618c40784STakashi Iwai 		return -ENODEV;
7718c40784STakashi Iwai 	}
781da177e4SLinus Torvalds 
795de4155bSJean Delvare 	/*
805de4155bSJean Delvare 	 * Let i2c-core delete that device on driver removal.
815de4155bSJean Delvare 	 * This is safe because i2c-core holds the core_lock mutex for us.
825de4155bSJean Delvare 	 */
835de4155bSJean Delvare 	list_add_tail(&keywest_ctx->client->detected,
84a7cde6d2SLars-Peter Clausen 		      &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
851da177e4SLinus Torvalds 	return 0;
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
885de4155bSJean Delvare static int keywest_remove(struct i2c_client *client)
891da177e4SLinus Torvalds {
901da177e4SLinus Torvalds 	if (! keywest_ctx)
911da177e4SLinus Torvalds 		return 0;
921da177e4SLinus Torvalds 	if (client == keywest_ctx->client)
931da177e4SLinus Torvalds 		keywest_ctx->client = NULL;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	return 0;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
985de4155bSJean Delvare 
995de4155bSJean Delvare static const struct i2c_device_id keywest_i2c_id[] = {
10028760c19SWolfram Sang 	{ "MAC,tas3004", 0 },		/* instantiated by i2c-powermac */
10128760c19SWolfram Sang 	{ "keywest", 0 },		/* instantiated by us if needed */
1025de4155bSJean Delvare 	{ }
1035de4155bSJean Delvare };
104a2bc2af6SJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
1055de4155bSJean Delvare 
106a656cbf0SJean Delvare static struct i2c_driver keywest_driver = {
1075de4155bSJean Delvare 	.driver = {
1085de4155bSJean Delvare 		.name = "PMac Keywest Audio",
1095de4155bSJean Delvare 	},
1105de4155bSJean Delvare 	.probe = keywest_probe,
1115de4155bSJean Delvare 	.remove = keywest_remove,
1125de4155bSJean Delvare 	.id_table = keywest_i2c_id,
1135de4155bSJean Delvare };
1145de4155bSJean Delvare 
1151da177e4SLinus Torvalds /* exported */
11665b29f50STakashi Iwai void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	if (keywest_ctx && keywest_ctx == i2c) {
1191da177e4SLinus Torvalds 		i2c_del_driver(&keywest_driver);
1201da177e4SLinus Torvalds 		keywest_ctx = NULL;
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
12415afafc2SBill Pemberton int snd_pmac_tumbler_post_init(void)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	int err;
1271da177e4SLinus Torvalds 
128783eaf46STakashi Iwai 	if (!keywest_ctx || !keywest_ctx->client)
129783eaf46STakashi Iwai 		return -ENXIO;
130783eaf46STakashi Iwai 
1311da177e4SLinus Torvalds 	if ((err = keywest_ctx->init_client(keywest_ctx)) < 0) {
1321da177e4SLinus Torvalds 		snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
1331da177e4SLinus Torvalds 		return err;
1341da177e4SLinus Torvalds 	}
1351da177e4SLinus Torvalds 	return 0;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /* exported */
13915afafc2SBill Pemberton int snd_pmac_keywest_init(struct pmac_keywest *i2c)
1401da177e4SLinus Torvalds {
141ac397c80SWolfram Sang 	struct i2c_adapter *adap;
142ac397c80SWolfram Sang 	int err, i = 0;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	if (keywest_ctx)
1451da177e4SLinus Torvalds 		return -EBUSY;
1461da177e4SLinus Torvalds 
147ac397c80SWolfram Sang 	adap = i2c_get_adapter(0);
148ac397c80SWolfram Sang 	if (!adap)
149ac397c80SWolfram Sang 		return -EPROBE_DEFER;
150ac397c80SWolfram Sang 
1511da177e4SLinus Torvalds 	keywest_ctx = i2c;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	if ((err = i2c_add_driver(&keywest_driver))) {
1541da177e4SLinus Torvalds 		snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
155ac397c80SWolfram Sang 		i2c_put_adapter(adap);
1561da177e4SLinus Torvalds 		return err;
1571da177e4SLinus Torvalds 	}
158ac397c80SWolfram Sang 
15928760c19SWolfram Sang 	/* There was already a device from i2c-powermac. Great, let's return */
16028760c19SWolfram Sang 	if (keywest_probed)
16128760c19SWolfram Sang 		return 0;
16228760c19SWolfram Sang 
163ac397c80SWolfram Sang 	/* We assume Macs have consecutive I2C bus numbers starting at 0 */
164ac397c80SWolfram Sang 	while (adap) {
16528760c19SWolfram Sang 		/* Scan for devices to be bound to */
166ac397c80SWolfram Sang 		err = keywest_attach_adapter(adap);
167ac397c80SWolfram Sang 		if (!err)
1681da177e4SLinus Torvalds 			return 0;
169ac397c80SWolfram Sang 		i2c_put_adapter(adap);
170ac397c80SWolfram Sang 		adap = i2c_get_adapter(++i);
171ac397c80SWolfram Sang 	}
172ac397c80SWolfram Sang 
173ac397c80SWolfram Sang 	return -ENODEV;
1741da177e4SLinus Torvalds }
175