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