xref: /linux/drivers/soundwire/bus_type.c (revision d3b402c5a2d47f51eb0581da1a7b142f82cb10d1)
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 "irq.h"
11 #include "sysfs_local.h"
12 
13 /**
14  * sdw_get_device_id - find the matching SoundWire device id
15  * @slave: SoundWire Slave Device
16  * @drv: SoundWire Slave Driver
17  *
18  * The match is done by comparing the mfg_id and part_id from the
19  * struct sdw_device_id.
20  */
21 static const struct sdw_device_id *
sdw_get_device_id(struct sdw_slave * slave,const struct sdw_driver * drv)22 sdw_get_device_id(struct sdw_slave *slave, const struct sdw_driver *drv)
23 {
24 	const struct sdw_device_id *id;
25 
26 	for (id = drv->id_table; id && id->mfg_id; id++)
27 		if (slave->id.mfg_id == id->mfg_id &&
28 		    slave->id.part_id == id->part_id  &&
29 		    (!id->sdw_version ||
30 		     slave->id.sdw_version == id->sdw_version) &&
31 		    (!id->class_id ||
32 		     slave->id.class_id == id->class_id))
33 			return id;
34 
35 	return NULL;
36 }
37 
sdw_bus_match(struct device * dev,const struct device_driver * ddrv)38 static int sdw_bus_match(struct device *dev, const struct device_driver *ddrv)
39 {
40 	struct sdw_slave *slave;
41 	const struct sdw_driver *drv;
42 	int ret = 0;
43 
44 	if (is_sdw_slave(dev)) {
45 		slave = dev_to_sdw_dev(dev);
46 		drv = drv_to_sdw_driver(ddrv);
47 
48 		ret = !!sdw_get_device_id(slave, drv);
49 	}
50 	return ret;
51 }
52 
sdw_slave_modalias(const struct sdw_slave * slave,char * buf,size_t size)53 int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size)
54 {
55 	/* modalias is sdw:m<mfg_id>p<part_id>v<version>c<class_id> */
56 
57 	return snprintf(buf, size, "sdw:m%04Xp%04Xv%02Xc%02X\n",
58 			slave->id.mfg_id, slave->id.part_id,
59 			slave->id.sdw_version, slave->id.class_id);
60 }
61 
sdw_slave_uevent(const struct device * dev,struct kobj_uevent_env * env)62 int sdw_slave_uevent(const struct device *dev, struct kobj_uevent_env *env)
63 {
64 	const struct sdw_slave *slave = dev_to_sdw_dev(dev);
65 	char modalias[32];
66 
67 	sdw_slave_modalias(slave, modalias, sizeof(modalias));
68 
69 	if (add_uevent_var(env, "MODALIAS=%s", modalias))
70 		return -ENOMEM;
71 
72 	return 0;
73 }
74 
sdw_bus_probe(struct device * dev)75 static int sdw_bus_probe(struct device *dev)
76 {
77 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
78 	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
79 	const struct sdw_device_id *id;
80 	int ret;
81 
82 	/*
83 	 * fw description is mandatory to bind
84 	 */
85 	if (!dev->fwnode)
86 		return -ENODEV;
87 
88 	if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
89 		return -ENODEV;
90 
91 	id = sdw_get_device_id(slave, drv);
92 	if (!id)
93 		return -ENODEV;
94 
95 	/*
96 	 * attach to power domain but don't turn on (last arg)
97 	 */
98 	ret = dev_pm_domain_attach(dev, 0);
99 	if (ret)
100 		return ret;
101 
102 	ret = ida_alloc_max(&slave->bus->slave_ida, SDW_FW_MAX_DEVICES - 1, GFP_KERNEL);
103 	if (ret < 0) {
104 		dev_err(dev, "Failed to allocated ID: %d\n", ret);
105 		return ret;
106 	}
107 	slave->index = ret;
108 
109 	ret = drv->probe(slave, id);
110 	if (ret) {
111 		ida_free(&slave->bus->slave_ida, slave->index);
112 		return ret;
113 	}
114 
115 	mutex_lock(&slave->sdw_dev_lock);
116 
117 	/* device is probed so let's read the properties now */
118 	if (drv->ops && drv->ops->read_prop)
119 		drv->ops->read_prop(slave);
120 
121 	if (slave->prop.use_domain_irq)
122 		sdw_irq_create_mapping(slave);
123 
124 	/* init the dynamic sysfs attributes we need */
125 	ret = sdw_slave_sysfs_dpn_init(slave);
126 	if (ret < 0)
127 		dev_warn(dev, "failed to initialise sysfs: %d\n", ret);
128 
129 	/*
130 	 * Check for valid clk_stop_timeout, use DisCo worst case value of
131 	 * 300ms
132 	 *
133 	 * TODO: check the timeouts and driver removal case
134 	 */
135 	if (slave->prop.clk_stop_timeout == 0)
136 		slave->prop.clk_stop_timeout = 300;
137 
138 	slave->bus->clk_stop_timeout = max_t(u32, slave->bus->clk_stop_timeout,
139 					     slave->prop.clk_stop_timeout);
140 
141 	slave->probed = true;
142 
143 	/*
144 	 * if the probe happened after the bus was started, notify the codec driver
145 	 * of the current hardware status to e.g. start the initialization.
146 	 * Errors are only logged as warnings to avoid failing the probe.
147 	 */
148 	if (drv->ops && drv->ops->update_status) {
149 		ret = drv->ops->update_status(slave, slave->status);
150 		if (ret < 0)
151 			dev_warn(dev, "failed to update status at probe: %d\n", ret);
152 	}
153 
154 	mutex_unlock(&slave->sdw_dev_lock);
155 
156 	dev_dbg(dev, "probe complete\n");
157 
158 	return 0;
159 }
160 
sdw_bus_remove(struct device * dev)161 static void sdw_bus_remove(struct device *dev)
162 {
163 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
164 	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
165 
166 	mutex_lock(&slave->sdw_dev_lock);
167 
168 	slave->probed = false;
169 
170 	mutex_unlock(&slave->sdw_dev_lock);
171 
172 	if (drv->remove)
173 		drv->remove(slave);
174 
175 	ida_free(&slave->bus->slave_ida, slave->index);
176 }
177 
sdw_bus_shutdown(struct device * dev)178 static void sdw_bus_shutdown(struct device *dev)
179 {
180 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
181 	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
182 
183 	if (dev->driver && drv->shutdown)
184 		drv->shutdown(slave);
185 }
186 
187 const struct bus_type sdw_bus_type = {
188 	.name = "soundwire",
189 	.match = sdw_bus_match,
190 	.probe = sdw_bus_probe,
191 	.remove = sdw_bus_remove,
192 	.shutdown = sdw_bus_shutdown,
193 };
194 EXPORT_SYMBOL_GPL(sdw_bus_type);
195 
196 /**
197  * __sdw_register_driver() - register a SoundWire Slave driver
198  * @drv: driver to register
199  * @owner: owning module/driver
200  *
201  * Return: zero on success, else a negative error code.
202  */
__sdw_register_driver(struct sdw_driver * drv,struct module * owner)203 int __sdw_register_driver(struct sdw_driver *drv, struct module *owner)
204 {
205 	drv->driver.bus = &sdw_bus_type;
206 
207 	if (!drv->probe) {
208 		pr_err("driver %s didn't provide SDW probe routine\n",
209 				drv->driver.name);
210 		return -EINVAL;
211 	}
212 
213 	drv->driver.owner = owner;
214 	drv->driver.dev_groups = sdw_attr_groups;
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