1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright(c) 2015-17 Intel Corporation.
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>
10 #include "sysfs_local.h"
13 * sdw_get_device_id - find the matching SoundWire device id
14 * @slave: SoundWire Slave Device
15 * @drv: SoundWire Slave Driver
17 * The match is done by comparing the mfg_id and part_id from the
18 * struct sdw_device_id.
20 static const struct sdw_device_id *
21 sdw_get_device_id(struct sdw_slave *slave, struct sdw_driver *drv)
23 const struct sdw_device_id *id;
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 &&
29 slave->id.sdw_version == id->sdw_version) &&
31 slave->id.class_id == id->class_id))
37 static int sdw_bus_match(struct device *dev, struct device_driver *ddrv)
39 struct sdw_slave *slave;
40 struct sdw_driver *drv;
43 if (is_sdw_slave(dev)) {
44 slave = dev_to_sdw_dev(dev);
45 drv = drv_to_sdw_driver(ddrv);
47 ret = !!sdw_get_device_id(slave, drv);
52 int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size)
54 /* modalias is sdw:m<mfg_id>p<part_id>v<version>c<class_id> */
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);
61 int sdw_slave_uevent(struct device *dev, struct kobj_uevent_env *env)
63 struct sdw_slave *slave = dev_to_sdw_dev(dev);
66 sdw_slave_modalias(slave, modalias, sizeof(modalias));
68 if (add_uevent_var(env, "MODALIAS=%s", modalias))
74 struct bus_type sdw_bus_type = {
76 .match = sdw_bus_match,
78 EXPORT_SYMBOL_GPL(sdw_bus_type);
80 static int sdw_drv_probe(struct device *dev)
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;
89 * fw description is mandatory to bind
94 if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
97 id = sdw_get_device_id(slave, drv);
101 slave->ops = drv->ops;
104 * attach to power domain but don't turn on (last arg)
106 ret = dev_pm_domain_attach(dev, false);
110 ret = drv->probe(slave, id);
114 name = drv->driver.name;
115 dev_err(dev, "Probe of %s failed: %d\n", name, ret);
116 dev_pm_domain_detach(dev, false);
120 /* device is probed so let's read the properties now */
121 if (slave->ops && slave->ops->read_prop)
122 slave->ops->read_prop(slave);
124 /* init the sysfs as we have properties now */
125 ret = sdw_slave_sysfs_init(slave);
127 dev_warn(dev, "Slave sysfs init failed:%d\n", ret);
130 * Check for valid clk_stop_timeout, use DisCo worst case value of
133 * TODO: check the timeouts and driver removal case
135 if (slave->prop.clk_stop_timeout == 0)
136 slave->prop.clk_stop_timeout = 300;
138 slave->bus->clk_stop_timeout = max_t(u32, slave->bus->clk_stop_timeout,
139 slave->prop.clk_stop_timeout);
141 slave->probed = true;
142 complete(&slave->probe_complete);
144 dev_dbg(dev, "probe complete\n");
149 static int sdw_drv_remove(struct device *dev)
151 struct sdw_slave *slave = dev_to_sdw_dev(dev);
152 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
156 ret = drv->remove(slave);
158 dev_pm_domain_detach(dev, false);
163 static void sdw_drv_shutdown(struct device *dev)
165 struct sdw_slave *slave = dev_to_sdw_dev(dev);
166 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
169 drv->shutdown(slave);
173 * __sdw_register_driver() - register a SoundWire Slave driver
174 * @drv: driver to register
175 * @owner: owning module/driver
177 * Return: zero on success, else a negative error code.
179 int __sdw_register_driver(struct sdw_driver *drv, struct module *owner)
183 drv->driver.bus = &sdw_bus_type;
188 name = drv->driver.name;
190 pr_err("driver %s didn't provide SDW probe routine\n", name);
194 drv->driver.owner = owner;
195 drv->driver.probe = sdw_drv_probe;
198 drv->driver.remove = sdw_drv_remove;
201 drv->driver.shutdown = sdw_drv_shutdown;
203 return driver_register(&drv->driver);
205 EXPORT_SYMBOL_GPL(__sdw_register_driver);
208 * sdw_unregister_driver() - unregisters the SoundWire Slave driver
209 * @drv: driver to unregister
211 void sdw_unregister_driver(struct sdw_driver *drv)
213 driver_unregister(&drv->driver);
215 EXPORT_SYMBOL_GPL(sdw_unregister_driver);
217 static int __init sdw_bus_init(void)
220 return bus_register(&sdw_bus_type);
223 static void __exit sdw_bus_exit(void)
226 bus_unregister(&sdw_bus_type);
229 postcore_initcall(sdw_bus_init);
230 module_exit(sdw_bus_exit);
232 MODULE_DESCRIPTION("SoundWire bus");
233 MODULE_LICENSE("GPL v2");