| 1 | |
| 2 | |
| 3 |
|
| 4 | #include <linux/module.h>
|
| 5 | #include <linux/pm_domain.h>
|
| 6 | #include <linux/soundwire/sdw.h>
|
| 7 | #include <linux/soundwire/sdw_type.h>
|
| 8 | #include "bus.h"
|
| 9 | #include "irq.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 *
|
| 21 | sdw_get_device_id(struct sdw_slave *slave, const 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 |
|
| 37 | static int sdw_bus_match(struct device *dev, const struct device_driver *ddrv)
|
| 38 | {
|
| 39 | struct sdw_slave *slave;
|
| 40 | const 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 |
|
| 52 | int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size)
|
| 53 | {
|
| 54 |
|
| 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 |
|
| 61 | int sdw_slave_uevent(const struct device *dev, struct kobj_uevent_env *env)
|
| 62 | {
|
| 63 | const 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 | static int sdw_bus_probe(struct device *dev)
|
| 75 | {
|
| 76 | struct sdw_slave *slave = dev_to_sdw_dev(dev);
|
| 77 | struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
|
| 78 | const struct sdw_device_id *id;
|
| 79 | int ret;
|
| 80 |
|
| 81 | |
| 82 | * fw description is mandatory to bind
|
| 83 |
|
| 84 | if (!dev->fwnode)
|
| 85 | return -ENODEV;
|
| 86 |
|
| 87 | if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node)
|
| 88 | return -ENODEV;
|
| 89 |
|
| 90 | id = sdw_get_device_id(slave, drv);
|
| 91 | if (!id)
|
| 92 | return -ENODEV;
|
| 93 |
|
| 94 | |
| 95 | * attach to power domain but don't turn on (last arg)
|
| 96 |
|
| 97 | ret = dev_pm_domain_attach(dev, 0);
|
| 98 | if (ret)
|
| 99 | return ret;
|
| 100 |
|
| 101 | ret = ida_alloc_max(&slave->bus->slave_ida, SDW_FW_MAX_DEVICES - 1, GFP_KERNEL);
|
| 102 | if (ret < 0) {
|
| 103 | dev_err(dev, "Failed to allocated ID: %d\n", ret);
|
| 104 | return ret;
|
| 105 | }
|
| 106 | slave->index = ret;
|
| 107 |
|
| 108 |
|
| 109 | sdw_irq_create_mapping(slave);
|
| 110 |
|
| 111 | ret = drv->probe(slave, id);
|
| 112 | if (ret) {
|
| 113 | ida_free(&slave->bus->slave_ida, slave->index);
|
| 114 | return ret;
|
| 115 | }
|
| 116 |
|
| 117 | mutex_lock(&slave->sdw_dev_lock);
|
| 118 |
|
| 119 |
|
| 120 | if (drv->ops && drv->ops->read_prop)
|
| 121 | drv->ops->read_prop(slave);
|
| 122 |
|
| 123 |
|
| 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 |
|
| 160 | static void sdw_bus_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 |
|
| 165 | mutex_lock(&slave->sdw_dev_lock);
|
| 166 |
|
| 167 | slave->probed = false;
|
| 168 |
|
| 169 | mutex_unlock(&slave->sdw_dev_lock);
|
| 170 |
|
| 171 | if (drv->remove)
|
| 172 | drv->remove(slave);
|
| 173 |
|
| 174 | ida_free(&slave->bus->slave_ida, slave->index);
|
| 175 | }
|
| 176 |
|
| 177 | static void sdw_bus_shutdown(struct device *dev)
|
| 178 | {
|
| 179 | struct sdw_slave *slave = dev_to_sdw_dev(dev);
|
| 180 | struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
|
| 181 |
|
| 182 | if (dev->driver && drv->shutdown)
|
| 183 | drv->shutdown(slave);
|
| 184 | }
|
| 185 |
|
| 186 | const struct bus_type sdw_bus_type = {
|
| 187 | .name = "soundwire",
|
| 188 | .match = sdw_bus_match,
|
| 189 | .probe = sdw_bus_probe,
|
| 190 | .remove = sdw_bus_remove,
|
| 191 | .shutdown = sdw_bus_shutdown,
|
| 192 | };
|
| 193 | EXPORT_SYMBOL_GPL(sdw_bus_type);
|
| 194 |
|
| 195 | |
| 196 | * __sdw_register_driver() - register a SoundWire Slave driver
|
| 197 | * @drv: driver to register
|
| 198 | * @owner: owning module/driver
|
| 199 | *
|
| 200 | * Return: zero on success, else a negative error code.
|
| 201 |
|
| 202 | int __sdw_register_driver(struct sdw_driver *drv, struct module *owner)
|
| 203 | {
|
| 204 | drv->driver.bus = &sdw_bus_type;
|
| 205 |
|
| 206 | if (!drv->probe) {
|
| 207 | pr_err("driver %s didn't provide SDW probe routine\n",
|
| 208 | drv->driver.name);
|
| 209 | return -EINVAL;
|
| 210 | }
|
| 211 |
|
| 212 | drv->driver.owner = owner;
|
| 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 |
|
| 223 | void sdw_unregister_driver(struct sdw_driver *drv)
|
| 224 | {
|
| 225 | driver_unregister(&drv->driver);
|
| 226 | }
|
| 227 | EXPORT_SYMBOL_GPL(sdw_unregister_driver);
|
| 228 |
|
| 229 | static int __init sdw_bus_init(void)
|
| 230 | {
|
| 231 | sdw_debugfs_init();
|
| 232 | return bus_register(&sdw_bus_type);
|
| 233 | }
|
| 234 |
|
| 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");
|