1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright(c) 2015-17 Intel Corporation.
3
4 #include <linux/module.h>
5 #include <linux/mod_devicetable.h>
6 #include <linux/pm_domain.h>
7 #include <linux/soundwire/sdw.h>
8 #include <linux/soundwire/sdw_type.h>
9 #include "bus.h"
10 #include "sysfs_local.h"
11
12 /**
13 * sdw_get_device_id - find the matching SoundWire device id
14 * @slave: SoundWire Slave Device
15 * @drv: SoundWire Slave Driver
16 *
17 * The match is done by comparing the mfg_id and part_id from the
18 * struct sdw_device_id.
19 */
20 static const struct sdw_device_id *
sdw_get_device_id(struct sdw_slave * slave,struct sdw_driver * drv)21 sdw_get_device_id(struct sdw_slave *slave, struct sdw_driver *drv)
22 {
23 const struct sdw_device_id *id;
24
25 for (id = drv->id_table; id && id->mfg_id; id++)
26 if (slave->id.mfg_id == id->mfg_id &&
27 slave->id.part_id == id->part_id &&
28 (!id->sdw_version ||
29 slave->id.sdw_version == id->sdw_version) &&
30 (!id->class_id ||
31 slave->id.class_id == id->class_id))
32 return id;
33
34 return NULL;
35 }
36
sdw_bus_match(struct device * dev,struct device_driver * ddrv)37 static int sdw_bus_match(struct device *dev, struct device_driver *ddrv)
38 {
39 struct sdw_slave *slave;
40 struct sdw_driver *drv;
41 int ret = 0;
42
43 if (is_sdw_slave(dev)) {
44 slave = dev_to_sdw_dev(dev);
45 drv = drv_to_sdw_driver(ddrv);
46
47 ret = !!sdw_get_device_id(slave, drv);
48 }
49 return ret;
50 }
51
sdw_slave_modalias(const struct sdw_slave * slave,char * buf,size_t size)52 int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size)
53 {
54 /* modalias is sdw:m<mfg_id>p<part_id>v<version>c<class_id> */
55
56 return snprintf(buf, size, "sdw:m%04Xp%04Xv%02Xc%02X\n",
57 slave->id.mfg_id, slave->id.part_id,
58 slave->id.sdw_version, slave->id.class_id);
59 }
60
sdw_slave_uevent(struct device * dev,struct kobj_uevent_env * env)61 int sdw_slave_uevent(struct device *dev, struct kobj_uevent_env *env)
62 {
63 struct sdw_slave *slave = dev_to_sdw_dev(dev);
64 char modalias[32];
65
66 sdw_slave_modalias(slave, modalias, sizeof(modalias));
67
68 if (add_uevent_var(env, "MODALIAS=%s", modalias))
69 return -ENOMEM;
70
71 return 0;
72 }
73
74 struct bus_type sdw_bus_type = {
75 .name = "soundwire",
76 .match = sdw_bus_match,
77 };
78 EXPORT_SYMBOL_GPL(sdw_bus_type);
79
sdw_drv_probe(struct device * dev)80 static int sdw_drv_probe(struct device *dev)
81 {
82 struct sdw_slave *slave = dev_to_sdw_dev(dev);
83 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
84 const struct sdw_device_id *id;
85 const char *name;
86 int ret;
87
88 /*
89 * fw description is mandatory to bind
90 */
91 if (!dev->fwnode)
92 return -ENODEV;
93
94 if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
95 return -ENODEV;
96
97 id = sdw_get_device_id(slave, drv);
98 if (!id)
99 return -ENODEV;
100
101 /*
102 * attach to power domain but don't turn on (last arg)
103 */
104 ret = dev_pm_domain_attach(dev, false);
105 if (ret)
106 return ret;
107
108 ret = drv->probe(slave, id);
109 if (ret) {
110 name = drv->name;
111 if (!name)
112 name = drv->driver.name;
113
114 dev_err(dev, "Probe of %s failed: %d\n", name, ret);
115 dev_pm_domain_detach(dev, false);
116 return ret;
117 }
118
119 mutex_lock(&slave->sdw_dev_lock);
120
121 /* device is probed so let's read the properties now */
122 if (drv->ops && drv->ops->read_prop)
123 drv->ops->read_prop(slave);
124
125 /* init the sysfs as we have properties now */
126 ret = sdw_slave_sysfs_init(slave);
127 if (ret < 0)
128 dev_warn(dev, "Slave sysfs init failed:%d\n", ret);
129
130 /*
131 * Check for valid clk_stop_timeout, use DisCo worst case value of
132 * 300ms
133 *
134 * TODO: check the timeouts and driver removal case
135 */
136 if (slave->prop.clk_stop_timeout == 0)
137 slave->prop.clk_stop_timeout = 300;
138
139 slave->bus->clk_stop_timeout = max_t(u32, slave->bus->clk_stop_timeout,
140 slave->prop.clk_stop_timeout);
141
142 slave->probed = true;
143
144 /*
145 * if the probe happened after the bus was started, notify the codec driver
146 * of the current hardware status to e.g. start the initialization.
147 * Errors are only logged as warnings to avoid failing the probe.
148 */
149 if (drv->ops && drv->ops->update_status) {
150 ret = drv->ops->update_status(slave, slave->status);
151 if (ret < 0)
152 dev_warn(dev, "%s: update_status failed with status %d\n", __func__, ret);
153 }
154
155 mutex_unlock(&slave->sdw_dev_lock);
156
157 dev_dbg(dev, "probe complete\n");
158
159 return 0;
160 }
161
sdw_drv_remove(struct device * dev)162 static int sdw_drv_remove(struct device *dev)
163 {
164 struct sdw_slave *slave = dev_to_sdw_dev(dev);
165 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
166 int ret = 0;
167
168 mutex_lock(&slave->sdw_dev_lock);
169 slave->probed = false;
170 mutex_unlock(&slave->sdw_dev_lock);
171
172 if (drv->remove)
173 ret = drv->remove(slave);
174
175 dev_pm_domain_detach(dev, false);
176
177 return ret;
178 }
179
sdw_drv_shutdown(struct device * dev)180 static void sdw_drv_shutdown(struct device *dev)
181 {
182 struct sdw_slave *slave = dev_to_sdw_dev(dev);
183 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
184
185 if (drv->shutdown)
186 drv->shutdown(slave);
187 }
188
189 /**
190 * __sdw_register_driver() - register a SoundWire Slave driver
191 * @drv: driver to register
192 * @owner: owning module/driver
193 *
194 * Return: zero on success, else a negative error code.
195 */
__sdw_register_driver(struct sdw_driver * drv,struct module * owner)196 int __sdw_register_driver(struct sdw_driver *drv, struct module *owner)
197 {
198 const char *name;
199
200 drv->driver.bus = &sdw_bus_type;
201
202 if (!drv->probe) {
203 name = drv->name;
204 if (!name)
205 name = drv->driver.name;
206
207 pr_err("driver %s didn't provide SDW probe routine\n", name);
208 return -EINVAL;
209 }
210
211 drv->driver.owner = owner;
212 drv->driver.probe = sdw_drv_probe;
213 drv->driver.remove = sdw_drv_remove;
214 drv->driver.shutdown = sdw_drv_shutdown;
215
216 return driver_register(&drv->driver);
217 }
218 EXPORT_SYMBOL_GPL(__sdw_register_driver);
219
220 /**
221 * sdw_unregister_driver() - unregisters the SoundWire Slave driver
222 * @drv: driver to unregister
223 */
sdw_unregister_driver(struct sdw_driver * drv)224 void sdw_unregister_driver(struct sdw_driver *drv)
225 {
226 driver_unregister(&drv->driver);
227 }
228 EXPORT_SYMBOL_GPL(sdw_unregister_driver);
229
sdw_bus_init(void)230 static int __init sdw_bus_init(void)
231 {
232 sdw_debugfs_init();
233 return bus_register(&sdw_bus_type);
234 }
235
sdw_bus_exit(void)236 static void __exit sdw_bus_exit(void)
237 {
238 sdw_debugfs_exit();
239 bus_unregister(&sdw_bus_type);
240 }
241
242 postcore_initcall(sdw_bus_init);
243 module_exit(sdw_bus_exit);
244
245 MODULE_DESCRIPTION("SoundWire bus");
246 MODULE_LICENSE("GPL v2");
247