]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - sound/ppc/keywest.c
Merge tag 'v5.13' into next
[mirror_ubuntu-jammy-kernel.git] / sound / ppc / keywest.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * common keywest i2c layer
4 *
5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
1da177e4
LT
6 */
7
8
1da177e4
LT
9#include <linux/init.h>
10#include <linux/i2c.h>
11#include <linux/delay.h>
654e2751 12#include <linux/module.h>
1da177e4
LT
13#include <sound/core.h>
14#include "pmac.h"
15
65b29f50 16static struct pmac_keywest *keywest_ctx;
28760c19 17static bool keywest_probed;
1da177e4 18
5de4155b
JD
19static int keywest_probe(struct i2c_client *client,
20 const struct i2c_device_id *id)
21{
28760c19
WS
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;
5de4155b
JD
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 */
1da177e4
LT
35static int keywest_attach_adapter(struct i2c_adapter *adapter)
36{
5de4155b 37 struct i2c_board_info info;
04a9af2e 38 struct i2c_client *client;
1da177e4
LT
39
40 if (! keywest_ctx)
41 return -EINVAL;
42
903dba1e 43 if (strncmp(adapter->name, "mac-io", 6))
ac397c80 44 return -EINVAL; /* ignored */
1da177e4 45
5de4155b 46 memset(&info, 0, sizeof(struct i2c_board_info));
75b1a8f9 47 strscpy(info.type, "keywest", I2C_NAME_SIZE);
5de4155b 48 info.addr = keywest_ctx->addr;
04a9af2e
WS
49 client = i2c_new_client_device(adapter, &info);
50 if (IS_ERR(client))
51 return PTR_ERR(client);
52 keywest_ctx->client = client;
53
18c40784
TI
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 */
a7cde6d2 59 if (!keywest_ctx->client->dev.driver) {
18c40784
TI
60 i2c_unregister_device(keywest_ctx->client);
61 keywest_ctx->client = NULL;
62 return -ENODEV;
63 }
1da177e4 64
5de4155b
JD
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,
a7cde6d2 70 &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
1da177e4 71 return 0;
1da177e4
LT
72}
73
5de4155b 74static int keywest_remove(struct i2c_client *client)
1da177e4
LT
75{
76 if (! keywest_ctx)
77 return 0;
78 if (client == keywest_ctx->client)
79 keywest_ctx->client = NULL;
80
1da177e4
LT
81 return 0;
82}
83
5de4155b
JD
84
85static const struct i2c_device_id keywest_i2c_id[] = {
28760c19
WS
86 { "MAC,tas3004", 0 }, /* instantiated by i2c-powermac */
87 { "keywest", 0 }, /* instantiated by us if needed */
5de4155b
JD
88 { }
89};
a2bc2af6 90MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
5de4155b 91
a656cbf0 92static struct i2c_driver keywest_driver = {
5de4155b
JD
93 .driver = {
94 .name = "PMac Keywest Audio",
95 },
5de4155b
JD
96 .probe = keywest_probe,
97 .remove = keywest_remove,
98 .id_table = keywest_i2c_id,
99};
100
1da177e4 101/* exported */
65b29f50 102void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
1da177e4
LT
103{
104 if (keywest_ctx && keywest_ctx == i2c) {
105 i2c_del_driver(&keywest_driver);
106 keywest_ctx = NULL;
107 }
108}
109
15afafc2 110int snd_pmac_tumbler_post_init(void)
1da177e4
LT
111{
112 int err;
113
783eaf46
TI
114 if (!keywest_ctx || !keywest_ctx->client)
115 return -ENXIO;
116
1da177e4
LT
117 if ((err = keywest_ctx->init_client(keywest_ctx)) < 0) {
118 snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
119 return err;
120 }
121 return 0;
122}
123
124/* exported */
15afafc2 125int snd_pmac_keywest_init(struct pmac_keywest *i2c)
1da177e4 126{
ac397c80
WS
127 struct i2c_adapter *adap;
128 int err, i = 0;
1da177e4
LT
129
130 if (keywest_ctx)
131 return -EBUSY;
132
ac397c80
WS
133 adap = i2c_get_adapter(0);
134 if (!adap)
135 return -EPROBE_DEFER;
136
1da177e4
LT
137 keywest_ctx = i2c;
138
139 if ((err = i2c_add_driver(&keywest_driver))) {
140 snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
ac397c80 141 i2c_put_adapter(adap);
1da177e4
LT
142 return err;
143 }
ac397c80 144
28760c19
WS
145 /* There was already a device from i2c-powermac. Great, let's return */
146 if (keywest_probed)
147 return 0;
148
ac397c80
WS
149 /* We assume Macs have consecutive I2C bus numbers starting at 0 */
150 while (adap) {
28760c19 151 /* Scan for devices to be bound to */
ac397c80
WS
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;
1da177e4 160}