xref: /openbmc/linux/sound/ppc/keywest.c (revision e73ad388)
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 
195de4155bSJean Delvare static int keywest_probe(struct i2c_client *client,
205de4155bSJean Delvare 			 const struct i2c_device_id *id)
215de4155bSJean Delvare {
2228760c19SWolfram Sang 	keywest_probed = true;
2328760c19SWolfram Sang 	/* If instantiated via i2c-powermac, we still need to set the client */
2428760c19SWolfram Sang 	if (!keywest_ctx->client)
2528760c19SWolfram Sang 		keywest_ctx->client = client;
265de4155bSJean Delvare 	i2c_set_clientdata(client, keywest_ctx);
275de4155bSJean Delvare 	return 0;
285de4155bSJean Delvare }
295de4155bSJean Delvare 
305de4155bSJean Delvare /*
315de4155bSJean Delvare  * This is kind of a hack, best would be to turn powermac to fixed i2c
325de4155bSJean Delvare  * bus numbers and declare the sound device as part of platform
335de4155bSJean Delvare  * initialization
345de4155bSJean Delvare  */
351da177e4SLinus Torvalds static int keywest_attach_adapter(struct i2c_adapter *adapter)
361da177e4SLinus Torvalds {
375de4155bSJean Delvare 	struct i2c_board_info info;
3804a9af2eSWolfram Sang 	struct i2c_client *client;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	if (! keywest_ctx)
411da177e4SLinus Torvalds 		return -EINVAL;
421da177e4SLinus Torvalds 
43903dba1eSJean Delvare 	if (strncmp(adapter->name, "mac-io", 6))
44ac397c80SWolfram Sang 		return -EINVAL; /* ignored */
451da177e4SLinus Torvalds 
465de4155bSJean Delvare 	memset(&info, 0, sizeof(struct i2c_board_info));
4775b1a8f9SJoe Perches 	strscpy(info.type, "keywest", I2C_NAME_SIZE);
485de4155bSJean Delvare 	info.addr = keywest_ctx->addr;
4904a9af2eSWolfram Sang 	client = i2c_new_client_device(adapter, &info);
5004a9af2eSWolfram Sang 	if (IS_ERR(client))
5104a9af2eSWolfram Sang 		return PTR_ERR(client);
5204a9af2eSWolfram Sang 	keywest_ctx->client = client;
5304a9af2eSWolfram Sang 
5418c40784STakashi Iwai 	/*
5518c40784STakashi Iwai 	 * We know the driver is already loaded, so the device should be
5618c40784STakashi Iwai 	 * already bound. If not it means binding failed, and then there
5718c40784STakashi Iwai 	 * is no point in keeping the device instantiated.
5818c40784STakashi Iwai 	 */
59a7cde6d2SLars-Peter Clausen 	if (!keywest_ctx->client->dev.driver) {
6018c40784STakashi Iwai 		i2c_unregister_device(keywest_ctx->client);
6118c40784STakashi Iwai 		keywest_ctx->client = NULL;
6218c40784STakashi Iwai 		return -ENODEV;
6318c40784STakashi Iwai 	}
641da177e4SLinus Torvalds 
655de4155bSJean Delvare 	/*
665de4155bSJean Delvare 	 * Let i2c-core delete that device on driver removal.
675de4155bSJean Delvare 	 * This is safe because i2c-core holds the core_lock mutex for us.
685de4155bSJean Delvare 	 */
695de4155bSJean Delvare 	list_add_tail(&keywest_ctx->client->detected,
70a7cde6d2SLars-Peter Clausen 		      &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
711da177e4SLinus Torvalds 	return 0;
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
745de4155bSJean Delvare static int keywest_remove(struct i2c_client *client)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	if (! keywest_ctx)
771da177e4SLinus Torvalds 		return 0;
781da177e4SLinus Torvalds 	if (client == keywest_ctx->client)
791da177e4SLinus Torvalds 		keywest_ctx->client = NULL;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	return 0;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
845de4155bSJean Delvare 
855de4155bSJean Delvare static const struct i2c_device_id keywest_i2c_id[] = {
8628760c19SWolfram Sang 	{ "MAC,tas3004", 0 },		/* instantiated by i2c-powermac */
8728760c19SWolfram Sang 	{ "keywest", 0 },		/* instantiated by us if needed */
885de4155bSJean Delvare 	{ }
895de4155bSJean Delvare };
90a2bc2af6SJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
915de4155bSJean Delvare 
92a656cbf0SJean Delvare static struct i2c_driver keywest_driver = {
935de4155bSJean Delvare 	.driver = {
945de4155bSJean Delvare 		.name = "PMac Keywest Audio",
955de4155bSJean Delvare 	},
965de4155bSJean Delvare 	.probe = keywest_probe,
975de4155bSJean Delvare 	.remove = keywest_remove,
985de4155bSJean Delvare 	.id_table = keywest_i2c_id,
995de4155bSJean Delvare };
1005de4155bSJean Delvare 
1011da177e4SLinus Torvalds /* exported */
10265b29f50STakashi Iwai void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	if (keywest_ctx && keywest_ctx == i2c) {
1051da177e4SLinus Torvalds 		i2c_del_driver(&keywest_driver);
1061da177e4SLinus Torvalds 		keywest_ctx = NULL;
1071da177e4SLinus Torvalds 	}
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
11015afafc2SBill Pemberton int snd_pmac_tumbler_post_init(void)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	int err;
1131da177e4SLinus Torvalds 
114783eaf46STakashi Iwai 	if (!keywest_ctx || !keywest_ctx->client)
115783eaf46STakashi Iwai 		return -ENXIO;
116783eaf46STakashi Iwai 
117*e73ad388STakashi Iwai 	err = keywest_ctx->init_client(keywest_ctx);
118*e73ad388STakashi Iwai 	if (err < 0) {
1191da177e4SLinus Torvalds 		snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
1201da177e4SLinus Torvalds 		return err;
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds 	return 0;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /* exported */
12615afafc2SBill Pemberton int snd_pmac_keywest_init(struct pmac_keywest *i2c)
1271da177e4SLinus Torvalds {
128ac397c80SWolfram Sang 	struct i2c_adapter *adap;
129ac397c80SWolfram Sang 	int err, i = 0;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	if (keywest_ctx)
1321da177e4SLinus Torvalds 		return -EBUSY;
1331da177e4SLinus Torvalds 
134ac397c80SWolfram Sang 	adap = i2c_get_adapter(0);
135ac397c80SWolfram Sang 	if (!adap)
136ac397c80SWolfram Sang 		return -EPROBE_DEFER;
137ac397c80SWolfram Sang 
1381da177e4SLinus Torvalds 	keywest_ctx = i2c;
1391da177e4SLinus Torvalds 
140*e73ad388STakashi Iwai 	err = i2c_add_driver(&keywest_driver);
141*e73ad388STakashi Iwai 	if (err) {
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