gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / soundwire / bus_type.c
blob17f096dd68062a26cdb741deb043c52b2223e77d
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>
9 #include "bus.h"
11 /**
12 * sdw_get_device_id - find the matching SoundWire device id
13 * @slave: SoundWire Slave Device
14 * @drv: SoundWire Slave Driver
16 * The match is done by comparing the mfg_id and part_id from the
17 * struct sdw_device_id.
19 static const struct sdw_device_id *
20 sdw_get_device_id(struct sdw_slave *slave, struct sdw_driver *drv)
22 const struct sdw_device_id *id = drv->id_table;
24 while (id && id->mfg_id) {
25 if (slave->id.mfg_id == id->mfg_id &&
26 slave->id.part_id == id->part_id)
27 return id;
28 id++;
31 return NULL;
34 static int sdw_bus_match(struct device *dev, struct device_driver *ddrv)
36 struct sdw_slave *slave = dev_to_sdw_dev(dev);
37 struct sdw_driver *drv = drv_to_sdw_driver(ddrv);
39 return !!sdw_get_device_id(slave, drv);
42 int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size)
44 /* modalias is sdw:m<mfg_id>p<part_id> */
46 return snprintf(buf, size, "sdw:m%04Xp%04X\n",
47 slave->id.mfg_id, slave->id.part_id);
50 static int sdw_uevent(struct device *dev, struct kobj_uevent_env *env)
52 struct sdw_slave *slave = dev_to_sdw_dev(dev);
53 char modalias[32];
55 sdw_slave_modalias(slave, modalias, sizeof(modalias));
57 if (add_uevent_var(env, "MODALIAS=%s", modalias))
58 return -ENOMEM;
60 return 0;
63 struct bus_type sdw_bus_type = {
64 .name = "soundwire",
65 .match = sdw_bus_match,
66 .uevent = sdw_uevent,
68 EXPORT_SYMBOL_GPL(sdw_bus_type);
70 static int sdw_drv_probe(struct device *dev)
72 struct sdw_slave *slave = dev_to_sdw_dev(dev);
73 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
74 const struct sdw_device_id *id;
75 int ret;
77 id = sdw_get_device_id(slave, drv);
78 if (!id)
79 return -ENODEV;
81 slave->ops = drv->ops;
84 * attach to power domain but don't turn on (last arg)
86 ret = dev_pm_domain_attach(dev, false);
87 if (ret)
88 return ret;
90 ret = drv->probe(slave, id);
91 if (ret) {
92 dev_err(dev, "Probe of %s failed: %d\n", drv->name, ret);
93 dev_pm_domain_detach(dev, false);
94 return ret;
97 /* device is probed so let's read the properties now */
98 if (slave->ops && slave->ops->read_prop)
99 slave->ops->read_prop(slave);
102 * Check for valid clk_stop_timeout, use DisCo worst case value of
103 * 300ms
105 * TODO: check the timeouts and driver removal case
107 if (slave->prop.clk_stop_timeout == 0)
108 slave->prop.clk_stop_timeout = 300;
110 slave->bus->clk_stop_timeout = max_t(u32, slave->bus->clk_stop_timeout,
111 slave->prop.clk_stop_timeout);
113 slave->probed = true;
114 complete(&slave->probe_complete);
116 dev_dbg(dev, "probe complete\n");
118 return 0;
121 static int sdw_drv_remove(struct device *dev)
123 struct sdw_slave *slave = dev_to_sdw_dev(dev);
124 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
125 int ret = 0;
127 if (drv->remove)
128 ret = drv->remove(slave);
130 dev_pm_domain_detach(dev, false);
132 return ret;
135 static void sdw_drv_shutdown(struct device *dev)
137 struct sdw_slave *slave = dev_to_sdw_dev(dev);
138 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
140 if (drv->shutdown)
141 drv->shutdown(slave);
145 * __sdw_register_driver() - register a SoundWire Slave driver
146 * @drv: driver to register
147 * @owner: owning module/driver
149 * Return: zero on success, else a negative error code.
151 int __sdw_register_driver(struct sdw_driver *drv, struct module *owner)
153 drv->driver.bus = &sdw_bus_type;
155 if (!drv->probe) {
156 pr_err("driver %s didn't provide SDW probe routine\n",
157 drv->name);
158 return -EINVAL;
161 drv->driver.owner = owner;
162 drv->driver.probe = sdw_drv_probe;
164 if (drv->remove)
165 drv->driver.remove = sdw_drv_remove;
167 if (drv->shutdown)
168 drv->driver.shutdown = sdw_drv_shutdown;
170 return driver_register(&drv->driver);
172 EXPORT_SYMBOL_GPL(__sdw_register_driver);
175 * sdw_unregister_driver() - unregisters the SoundWire Slave driver
176 * @drv: driver to unregister
178 void sdw_unregister_driver(struct sdw_driver *drv)
180 driver_unregister(&drv->driver);
182 EXPORT_SYMBOL_GPL(sdw_unregister_driver);
184 static int __init sdw_bus_init(void)
186 sdw_debugfs_init();
187 return bus_register(&sdw_bus_type);
190 static void __exit sdw_bus_exit(void)
192 sdw_debugfs_exit();
193 bus_unregister(&sdw_bus_type);
196 postcore_initcall(sdw_bus_init);
197 module_exit(sdw_bus_exit);
199 MODULE_DESCRIPTION("SoundWire bus");
200 MODULE_LICENSE("GPL v2");