xref: /linux/sound/ppc/keywest.c (revision 460ea8980511c01c1551012b9a6ec6a06d02da59)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * common keywest i2c layer
4  *
5  * Copyright (c) by Takashi Iwai <tiwai@suse.de>
6  */
7 
8 
9 #include <linux/init.h>
10 #include <linux/i2c.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <sound/core.h>
14 #include "pmac.h"
15 
16 static struct pmac_keywest *keywest_ctx;
17 static bool keywest_probed;
18 
19 static int keywest_probe(struct i2c_client *client)
20 {
21 	keywest_probed = true;
22 	/* If instantiated via i2c-powermac, we still need to set the client */
23 	if (!keywest_ctx->client)
24 		keywest_ctx->client = client;
25 	i2c_set_clientdata(client, keywest_ctx);
26 	return 0;
27 }
28 
29 /*
30  * This is kind of a hack, best would be to turn powermac to fixed i2c
31  * bus numbers and declare the sound device as part of platform
32  * initialization
33  */
34 static int keywest_attach_adapter(struct i2c_adapter *adapter)
35 {
36 	struct i2c_board_info info;
37 	struct i2c_client *client;
38 
39 	if (! keywest_ctx)
40 		return -EINVAL;
41 
42 	if (strncmp(adapter->name, "mac-io", 6))
43 		return -EINVAL; /* ignored */
44 
45 	memset(&info, 0, sizeof(struct i2c_board_info));
46 	strscpy(info.type, "keywest", I2C_NAME_SIZE);
47 	info.addr = keywest_ctx->addr;
48 	client = i2c_new_client_device(adapter, &info);
49 	if (IS_ERR(client))
50 		return PTR_ERR(client);
51 	keywest_ctx->client = client;
52 
53 	/*
54 	 * We know the driver is already loaded, so the device should be
55 	 * already bound. If not it means binding failed, and then there
56 	 * is no point in keeping the device instantiated.
57 	 */
58 	if (!keywest_ctx->client->dev.driver) {
59 		i2c_unregister_device(keywest_ctx->client);
60 		keywest_ctx->client = NULL;
61 		return -ENODEV;
62 	}
63 
64 	/*
65 	 * Let i2c-core delete that device on driver removal.
66 	 * This is safe because i2c-core holds the core_lock mutex for us.
67 	 */
68 	list_add_tail(&keywest_ctx->client->detected,
69 		      &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
70 	return 0;
71 }
72 
73 static void keywest_remove(struct i2c_client *client)
74 {
75 	if (! keywest_ctx)
76 		return;
77 	if (client == keywest_ctx->client)
78 		keywest_ctx->client = NULL;
79 }
80 
81 
82 static const struct i2c_device_id keywest_i2c_id[] = {
83 	{ "MAC,tas3004", 0 },		/* instantiated by i2c-powermac */
84 	{ "keywest", 0 },		/* instantiated by us if needed */
85 	{ }
86 };
87 MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
88 
89 static struct i2c_driver keywest_driver = {
90 	.driver = {
91 		.name = "PMac Keywest Audio",
92 	},
93 	.probe = keywest_probe,
94 	.remove = keywest_remove,
95 	.id_table = keywest_i2c_id,
96 };
97 
98 /* exported */
99 void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
100 {
101 	if (keywest_ctx && keywest_ctx == i2c) {
102 		i2c_del_driver(&keywest_driver);
103 		keywest_ctx = NULL;
104 	}
105 }
106 
107 int snd_pmac_tumbler_post_init(void)
108 {
109 	int err;
110 
111 	if (!keywest_ctx || !keywest_ctx->client)
112 		return -ENXIO;
113 
114 	err = keywest_ctx->init_client(keywest_ctx);
115 	if (err < 0) {
116 		snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
117 		return err;
118 	}
119 	return 0;
120 }
121 
122 /* exported */
123 int snd_pmac_keywest_init(struct pmac_keywest *i2c)
124 {
125 	struct i2c_adapter *adap;
126 	int err, i = 0;
127 
128 	if (keywest_ctx)
129 		return -EBUSY;
130 
131 	adap = i2c_get_adapter(0);
132 	if (!adap)
133 		return -EPROBE_DEFER;
134 
135 	keywest_ctx = i2c;
136 
137 	err = i2c_add_driver(&keywest_driver);
138 	if (err) {
139 		snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
140 		i2c_put_adapter(adap);
141 		return err;
142 	}
143 
144 	/* There was already a device from i2c-powermac. Great, let's return */
145 	if (keywest_probed)
146 		return 0;
147 
148 	/* We assume Macs have consecutive I2C bus numbers starting at 0 */
149 	while (adap) {
150 		/* Scan for devices to be bound to */
151 		err = keywest_attach_adapter(adap);
152 		if (!err)
153 			return 0;
154 		i2c_put_adapter(adap);
155 		adap = i2c_get_adapter(++i);
156 	}
157 
158 	return -ENODEV;
159 }
160