xref: /linux/drivers/soundwire/bus_type.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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 
75 const struct bus_type sdw_bus_type = {
76 	.name = "soundwire",
77 	.match = sdw_bus_match,
78 };
79 EXPORT_SYMBOL_GPL(sdw_bus_type);
80 
sdw_drv_probe(struct device * dev)81 static int sdw_drv_probe(struct device *dev)
82 {
83 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
84 	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
85 	const struct sdw_device_id *id;
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 		dev_pm_domain_detach(dev, false);
111 		return ret;
112 	}
113 
114 	mutex_lock(&slave->sdw_dev_lock);
115 
116 	/* device is probed so let's read the properties now */
117 	if (drv->ops && drv->ops->read_prop)
118 		drv->ops->read_prop(slave);
119 
120 	if (slave->prop.use_domain_irq)
121 		sdw_irq_create_mapping(slave);
122 
123 	/* init the dynamic sysfs attributes we need */
124 	ret = sdw_slave_sysfs_dpn_init(slave);
125 	if (ret < 0)
126 		dev_warn(dev, "failed to initialise sysfs: %d\n", ret);
127 
128 	/*
129 	 * Check for valid clk_stop_timeout, use DisCo worst case value of
130 	 * 300ms
131 	 *
132 	 * TODO: check the timeouts and driver removal case
133 	 */
134 	if (slave->prop.clk_stop_timeout == 0)
135 		slave->prop.clk_stop_timeout = 300;
136 
137 	slave->bus->clk_stop_timeout = max_t(u32, slave->bus->clk_stop_timeout,
138 					     slave->prop.clk_stop_timeout);
139 
140 	slave->probed = true;
141 
142 	/*
143 	 * if the probe happened after the bus was started, notify the codec driver
144 	 * of the current hardware status to e.g. start the initialization.
145 	 * Errors are only logged as warnings to avoid failing the probe.
146 	 */
147 	if (drv->ops && drv->ops->update_status) {
148 		ret = drv->ops->update_status(slave, slave->status);
149 		if (ret < 0)
150 			dev_warn(dev, "failed to update status at probe: %d\n", ret);
151 	}
152 
153 	mutex_unlock(&slave->sdw_dev_lock);
154 
155 	dev_dbg(dev, "probe complete\n");
156 
157 	return 0;
158 }
159 
sdw_drv_remove(struct device * dev)160 static int sdw_drv_remove(struct device *dev)
161 {
162 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
163 	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
164 	int ret = 0;
165 
166 	mutex_lock(&slave->sdw_dev_lock);
167 
168 	slave->probed = false;
169 
170 	if (slave->prop.use_domain_irq)
171 		sdw_irq_dispose_mapping(slave);
172 
173 	mutex_unlock(&slave->sdw_dev_lock);
174 
175 	if (drv->remove)
176 		ret = drv->remove(slave);
177 
178 	dev_pm_domain_detach(dev, false);
179 
180 	return ret;
181 }
182 
sdw_drv_shutdown(struct device * dev)183 static void sdw_drv_shutdown(struct device *dev)
184 {
185 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
186 	struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
187 
188 	if (drv->shutdown)
189 		drv->shutdown(slave);
190 }
191 
192 /**
193  * __sdw_register_driver() - register a SoundWire Slave driver
194  * @drv: driver to register
195  * @owner: owning module/driver
196  *
197  * Return: zero on success, else a negative error code.
198  */
__sdw_register_driver(struct sdw_driver * drv,struct module * owner)199 int __sdw_register_driver(struct sdw_driver *drv, struct module *owner)
200 {
201 	drv->driver.bus = &sdw_bus_type;
202 
203 	if (!drv->probe) {
204 		pr_err("driver %s didn't provide SDW probe routine\n",
205 				drv->driver.name);
206 		return -EINVAL;
207 	}
208 
209 	drv->driver.owner = owner;
210 	drv->driver.probe = sdw_drv_probe;
211 	drv->driver.remove = sdw_drv_remove;
212 	drv->driver.shutdown = sdw_drv_shutdown;
213 	drv->driver.dev_groups = sdw_attr_groups;
214 
215 	return driver_register(&drv->driver);
216 }
217 EXPORT_SYMBOL_GPL(__sdw_register_driver);
218 
219 /**
220  * sdw_unregister_driver() - unregisters the SoundWire Slave driver
221  * @drv: driver to unregister
222  */
sdw_unregister_driver(struct sdw_driver * drv)223 void sdw_unregister_driver(struct sdw_driver *drv)
224 {
225 	driver_unregister(&drv->driver);
226 }
227 EXPORT_SYMBOL_GPL(sdw_unregister_driver);
228 
sdw_bus_init(void)229 static int __init sdw_bus_init(void)
230 {
231 	sdw_debugfs_init();
232 	return bus_register(&sdw_bus_type);
233 }
234 
sdw_bus_exit(void)235 static void __exit sdw_bus_exit(void)
236 {
237 	sdw_debugfs_exit();
238 	bus_unregister(&sdw_bus_type);
239 }
240 
241 postcore_initcall(sdw_bus_init);
242 module_exit(sdw_bus_exit);
243 
244 MODULE_DESCRIPTION("SoundWire bus");
245 MODULE_LICENSE("GPL v2");
246