xref: /openbmc/linux/sound/ppc/keywest.c (revision 5de4155b)
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 <linux/slab.h>
261da177e4SLinus Torvalds #include <sound/core.h>
271da177e4SLinus Torvalds #include "pmac.h"
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * we have to keep a static variable here since i2c attach_adapter
311da177e4SLinus Torvalds  * callback cannot pass a private data.
321da177e4SLinus Torvalds  */
3365b29f50STakashi Iwai static struct pmac_keywest *keywest_ctx;
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #ifndef i2c_device_name
371da177e4SLinus Torvalds #define i2c_device_name(x)	((x)->name)
381da177e4SLinus Torvalds #endif
391da177e4SLinus Torvalds 
405de4155bSJean Delvare static int keywest_probe(struct i2c_client *client,
415de4155bSJean Delvare 			 const struct i2c_device_id *id)
425de4155bSJean Delvare {
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 
591da177e4SLinus Torvalds 	if (strncmp(i2c_device_name(adapter), "mac-io", 6))
601da177e4SLinus Torvalds 		return 0; /* 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);
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,
725de4155bSJean Delvare 		      &keywest_ctx->client->driver->clients);
731da177e4SLinus Torvalds 	return 0;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
765de4155bSJean Delvare static int keywest_remove(struct i2c_client *client)
771da177e4SLinus Torvalds {
785de4155bSJean Delvare 	i2c_set_clientdata(client, NULL);
791da177e4SLinus Torvalds 	if (! keywest_ctx)
801da177e4SLinus Torvalds 		return 0;
811da177e4SLinus Torvalds 	if (client == keywest_ctx->client)
821da177e4SLinus Torvalds 		keywest_ctx->client = NULL;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	return 0;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
875de4155bSJean Delvare 
885de4155bSJean Delvare static const struct i2c_device_id keywest_i2c_id[] = {
895de4155bSJean Delvare 	{ "keywest", 0 },
905de4155bSJean Delvare 	{ }
915de4155bSJean Delvare };
925de4155bSJean Delvare 
935de4155bSJean Delvare struct i2c_driver keywest_driver = {
945de4155bSJean Delvare 	.driver = {
955de4155bSJean Delvare 		.name = "PMac Keywest Audio",
965de4155bSJean Delvare 	},
975de4155bSJean Delvare 	.attach_adapter = keywest_attach_adapter,
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 
1121da177e4SLinus Torvalds int __init 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 */
12765b29f50STakashi Iwai int __init snd_pmac_keywest_init(struct pmac_keywest *i2c)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	int err;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	if (keywest_ctx)
1321da177e4SLinus Torvalds 		return -EBUSY;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	keywest_ctx = i2c;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	if ((err = i2c_add_driver(&keywest_driver))) {
1371da177e4SLinus Torvalds 		snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
1381da177e4SLinus Torvalds 		return err;
1391da177e4SLinus Torvalds 	}
1401da177e4SLinus Torvalds 	return 0;
1411da177e4SLinus Torvalds }
142