xref: /openbmc/linux/sound/ppc/keywest.c (revision c894ec01)
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 
1665b29f50STakashi Iwai static struct pmac_keywest *keywest_ctx;
1728760c19SWolfram Sang static bool keywest_probed;
181da177e4SLinus Torvalds 
keywest_probe(struct i2c_client * client)191cf47b04SUwe Kleine-König static int keywest_probe(struct i2c_client *client)
205de4155bSJean Delvare {
2128760c19SWolfram Sang 	keywest_probed = true;
2228760c19SWolfram Sang 	/* If instantiated via i2c-powermac, we still need to set the client */
2328760c19SWolfram Sang 	if (!keywest_ctx->client)
2428760c19SWolfram Sang 		keywest_ctx->client = client;
255de4155bSJean Delvare 	i2c_set_clientdata(client, keywest_ctx);
265de4155bSJean Delvare 	return 0;
275de4155bSJean Delvare }
285de4155bSJean Delvare 
295de4155bSJean Delvare /*
305de4155bSJean Delvare  * This is kind of a hack, best would be to turn powermac to fixed i2c
315de4155bSJean Delvare  * bus numbers and declare the sound device as part of platform
325de4155bSJean Delvare  * initialization
335de4155bSJean Delvare  */
keywest_attach_adapter(struct i2c_adapter * adapter)341da177e4SLinus Torvalds static int keywest_attach_adapter(struct i2c_adapter *adapter)
351da177e4SLinus Torvalds {
365de4155bSJean Delvare 	struct i2c_board_info info;
3704a9af2eSWolfram Sang 	struct i2c_client *client;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	if (! keywest_ctx)
401da177e4SLinus Torvalds 		return -EINVAL;
411da177e4SLinus Torvalds 
42903dba1eSJean Delvare 	if (strncmp(adapter->name, "mac-io", 6))
43ac397c80SWolfram Sang 		return -EINVAL; /* ignored */
441da177e4SLinus Torvalds 
455de4155bSJean Delvare 	memset(&info, 0, sizeof(struct i2c_board_info));
4675b1a8f9SJoe Perches 	strscpy(info.type, "keywest", I2C_NAME_SIZE);
475de4155bSJean Delvare 	info.addr = keywest_ctx->addr;
4804a9af2eSWolfram Sang 	client = i2c_new_client_device(adapter, &info);
4904a9af2eSWolfram Sang 	if (IS_ERR(client))
5004a9af2eSWolfram Sang 		return PTR_ERR(client);
5104a9af2eSWolfram Sang 	keywest_ctx->client = client;
5204a9af2eSWolfram Sang 
5318c40784STakashi Iwai 	/*
5418c40784STakashi Iwai 	 * We know the driver is already loaded, so the device should be
5518c40784STakashi Iwai 	 * already bound. If not it means binding failed, and then there
5618c40784STakashi Iwai 	 * is no point in keeping the device instantiated.
5718c40784STakashi Iwai 	 */
58a7cde6d2SLars-Peter Clausen 	if (!keywest_ctx->client->dev.driver) {
5918c40784STakashi Iwai 		i2c_unregister_device(keywest_ctx->client);
6018c40784STakashi Iwai 		keywest_ctx->client = NULL;
6118c40784STakashi Iwai 		return -ENODEV;
6218c40784STakashi Iwai 	}
631da177e4SLinus Torvalds 
645de4155bSJean Delvare 	/*
655de4155bSJean Delvare 	 * Let i2c-core delete that device on driver removal.
665de4155bSJean Delvare 	 * This is safe because i2c-core holds the core_lock mutex for us.
675de4155bSJean Delvare 	 */
685de4155bSJean Delvare 	list_add_tail(&keywest_ctx->client->detected,
69a7cde6d2SLars-Peter Clausen 		      &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
701da177e4SLinus Torvalds 	return 0;
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds 
keywest_remove(struct i2c_client * client)73ed5c2f5fSUwe Kleine-König static void keywest_remove(struct i2c_client *client)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	if (! keywest_ctx)
76ed5c2f5fSUwe Kleine-König 		return;
771da177e4SLinus Torvalds 	if (client == keywest_ctx->client)
781da177e4SLinus Torvalds 		keywest_ctx->client = NULL;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
815de4155bSJean Delvare 
825de4155bSJean Delvare static const struct i2c_device_id keywest_i2c_id[] = {
8328760c19SWolfram Sang 	{ "MAC,tas3004", 0 },		/* instantiated by i2c-powermac */
8428760c19SWolfram Sang 	{ "keywest", 0 },		/* instantiated by us if needed */
855de4155bSJean Delvare 	{ }
865de4155bSJean Delvare };
87a2bc2af6SJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
885de4155bSJean Delvare 
89a656cbf0SJean Delvare static struct i2c_driver keywest_driver = {
905de4155bSJean Delvare 	.driver = {
915de4155bSJean Delvare 		.name = "PMac Keywest Audio",
925de4155bSJean Delvare 	},
93*c894ec01SUwe Kleine-König 	.probe = keywest_probe,
945de4155bSJean Delvare 	.remove = keywest_remove,
955de4155bSJean Delvare 	.id_table = keywest_i2c_id,
965de4155bSJean Delvare };
975de4155bSJean Delvare 
981da177e4SLinus Torvalds /* exported */
snd_pmac_keywest_cleanup(struct pmac_keywest * i2c)9965b29f50STakashi Iwai void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	if (keywest_ctx && keywest_ctx == i2c) {
1021da177e4SLinus Torvalds 		i2c_del_driver(&keywest_driver);
1031da177e4SLinus Torvalds 		keywest_ctx = NULL;
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
snd_pmac_tumbler_post_init(void)10715afafc2SBill Pemberton int snd_pmac_tumbler_post_init(void)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	int err;
1101da177e4SLinus Torvalds 
111783eaf46STakashi Iwai 	if (!keywest_ctx || !keywest_ctx->client)
112783eaf46STakashi Iwai 		return -ENXIO;
113783eaf46STakashi Iwai 
114e73ad388STakashi Iwai 	err = keywest_ctx->init_client(keywest_ctx);
115e73ad388STakashi Iwai 	if (err < 0) {
1161da177e4SLinus Torvalds 		snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
1171da177e4SLinus Torvalds 		return err;
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 	return 0;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /* exported */
snd_pmac_keywest_init(struct pmac_keywest * i2c)12315afafc2SBill Pemberton int snd_pmac_keywest_init(struct pmac_keywest *i2c)
1241da177e4SLinus Torvalds {
125ac397c80SWolfram Sang 	struct i2c_adapter *adap;
126ac397c80SWolfram Sang 	int err, i = 0;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	if (keywest_ctx)
1291da177e4SLinus Torvalds 		return -EBUSY;
1301da177e4SLinus Torvalds 
131ac397c80SWolfram Sang 	adap = i2c_get_adapter(0);
132ac397c80SWolfram Sang 	if (!adap)
133ac397c80SWolfram Sang 		return -EPROBE_DEFER;
134ac397c80SWolfram Sang 
1351da177e4SLinus Torvalds 	keywest_ctx = i2c;
1361da177e4SLinus Torvalds 
137e73ad388STakashi Iwai 	err = i2c_add_driver(&keywest_driver);
138e73ad388STakashi Iwai 	if (err) {
1391da177e4SLinus Torvalds 		snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
140ac397c80SWolfram Sang 		i2c_put_adapter(adap);
1411da177e4SLinus Torvalds 		return err;
1421da177e4SLinus Torvalds 	}
143ac397c80SWolfram Sang 
14428760c19SWolfram Sang 	/* There was already a device from i2c-powermac. Great, let's return */
14528760c19SWolfram Sang 	if (keywest_probed)
14628760c19SWolfram Sang 		return 0;
14728760c19SWolfram Sang 
148ac397c80SWolfram Sang 	/* We assume Macs have consecutive I2C bus numbers starting at 0 */
149ac397c80SWolfram Sang 	while (adap) {
15028760c19SWolfram Sang 		/* Scan for devices to be bound to */
151ac397c80SWolfram Sang 		err = keywest_attach_adapter(adap);
152ac397c80SWolfram Sang 		if (!err)
1531da177e4SLinus Torvalds 			return 0;
154ac397c80SWolfram Sang 		i2c_put_adapter(adap);
155ac397c80SWolfram Sang 		adap = i2c_get_adapter(++i);
156ac397c80SWolfram Sang 	}
157ac397c80SWolfram Sang 
158ac397c80SWolfram Sang 	return -ENODEV;
1591da177e4SLinus Torvalds }
160