Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 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> |
Pierre-Louis Bossart | bf03473 | 2019-08-21 13:58:18 -0500 | [diff] [blame] | 9 | #include "bus.h" |
Charles Keepax | 3b6c4a1 | 2023-09-20 17:04:01 +0100 | [diff] [blame] | 10 | #include "irq.h" |
Pierre-Louis Bossart | bcac590 | 2020-05-19 04:35:51 +0800 | [diff] [blame] | 11 | #include "sysfs_local.h" |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 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 * |
| 22 | sdw_get_device_id(struct sdw_slave *slave, struct sdw_driver *drv) |
| 23 | { |
Pierre-Louis Bossart | ee9173d | 2020-06-09 04:54:34 +0800 | [diff] [blame] | 24 | const struct sdw_device_id *id; |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 25 | |
Pierre-Louis Bossart | ee9173d | 2020-06-09 04:54:34 +0800 | [diff] [blame] | 26 | for (id = drv->id_table; id && id->mfg_id; id++) |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 27 | if (slave->id.mfg_id == id->mfg_id && |
Pierre-Louis Bossart | b592426 | 2020-06-09 04:54:35 +0800 | [diff] [blame] | 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)) |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 33 | return id; |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 34 | |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | static int sdw_bus_match(struct device *dev, struct device_driver *ddrv) |
| 39 | { |
Pierre-Louis Bossart | 90acca1 | 2020-05-19 01:43:19 +0800 | [diff] [blame] | 40 | struct sdw_slave *slave; |
| 41 | struct sdw_driver *drv; |
| 42 | int ret = 0; |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 43 | |
Pierre-Louis Bossart | 90acca1 | 2020-05-19 01:43:19 +0800 | [diff] [blame] | 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; |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 51 | } |
| 52 | |
Pierre-Louis Bossart | bcac590 | 2020-05-19 04:35:51 +0800 | [diff] [blame] | 53 | int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size) |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 54 | { |
Pierre-Louis Bossart | b592426 | 2020-06-09 04:54:35 +0800 | [diff] [blame] | 55 | /* modalias is sdw:m<mfg_id>p<part_id>v<version>c<class_id> */ |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 56 | |
Pierre-Louis Bossart | b592426 | 2020-06-09 04:54:35 +0800 | [diff] [blame] | 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); |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 60 | } |
| 61 | |
Greg Kroah-Hartman | 162736b | 2023-01-11 12:30:07 +0100 | [diff] [blame] | 62 | int sdw_slave_uevent(const struct device *dev, struct kobj_uevent_env *env) |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 63 | { |
Greg Kroah-Hartman | 162736b | 2023-01-11 12:30:07 +0100 | [diff] [blame] | 64 | const struct sdw_slave *slave = dev_to_sdw_dev(dev); |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 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 | struct bus_type sdw_bus_type = { |
| 76 | .name = "soundwire", |
| 77 | .match = sdw_bus_match, |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 78 | }; |
| 79 | EXPORT_SYMBOL_GPL(sdw_bus_type); |
| 80 | |
| 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; |
Pierre-Louis Bossart | 0196b52 | 2021-03-02 17:11:12 +0800 | [diff] [blame] | 86 | const char *name; |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 87 | int ret; |
| 88 | |
Srinivas Kandagatla | fcb9d73 | 2020-09-24 14:44:29 -0500 | [diff] [blame] | 89 | /* |
| 90 | * fw description is mandatory to bind |
| 91 | */ |
| 92 | if (!dev->fwnode) |
| 93 | return -ENODEV; |
| 94 | |
| 95 | if (!IS_ENABLED(CONFIG_ACPI) && !dev->of_node) |
| 96 | return -ENODEV; |
| 97 | |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 98 | id = sdw_get_device_id(slave, drv); |
| 99 | if (!id) |
| 100 | return -ENODEV; |
| 101 | |
| 102 | /* |
| 103 | * attach to power domain but don't turn on (last arg) |
| 104 | */ |
| 105 | ret = dev_pm_domain_attach(dev, false); |
Vinod Koul | 56d4fe3 | 2017-12-14 11:19:35 +0530 | [diff] [blame] | 106 | if (ret) |
| 107 | return ret; |
| 108 | |
Ulf Hansson | 29ffcc8 | 2018-04-26 10:53:09 +0200 | [diff] [blame] | 109 | ret = drv->probe(slave, id); |
| 110 | if (ret) { |
Pierre-Louis Bossart | 0196b52 | 2021-03-02 17:11:12 +0800 | [diff] [blame] | 111 | name = drv->name; |
| 112 | if (!name) |
| 113 | name = drv->driver.name; |
Pierre-Louis Bossart | bd29c00 | 2022-06-21 17:56:38 -0500 | [diff] [blame] | 114 | |
Pierre-Louis Bossart | 0196b52 | 2021-03-02 17:11:12 +0800 | [diff] [blame] | 115 | dev_err(dev, "Probe of %s failed: %d\n", name, ret); |
Ulf Hansson | 29ffcc8 | 2018-04-26 10:53:09 +0200 | [diff] [blame] | 116 | dev_pm_domain_detach(dev, false); |
| 117 | return ret; |
| 118 | } |
| 119 | |
Richard Fitzgerald | 3dca1f8 | 2023-01-23 17:25:20 +0000 | [diff] [blame] | 120 | mutex_lock(&slave->sdw_dev_lock); |
| 121 | |
Vinod Koul | 56d4fe3 | 2017-12-14 11:19:35 +0530 | [diff] [blame] | 122 | /* device is probed so let's read the properties now */ |
Pierre-Louis Bossart | bd29c00 | 2022-06-21 17:56:38 -0500 | [diff] [blame] | 123 | if (drv->ops && drv->ops->read_prop) |
| 124 | drv->ops->read_prop(slave); |
Vinod Koul | 56d4fe3 | 2017-12-14 11:19:35 +0530 | [diff] [blame] | 125 | |
Charles Keepax | 3b6c4a1 | 2023-09-20 17:04:01 +0100 | [diff] [blame] | 126 | if (slave->prop.use_domain_irq) |
| 127 | sdw_irq_create_mapping(slave); |
Lucas Tanure | 12a9512 | 2023-08-04 11:45:57 +0100 | [diff] [blame] | 128 | |
Pierre-Louis Bossart | bcac590 | 2020-05-19 04:35:51 +0800 | [diff] [blame] | 129 | /* init the sysfs as we have properties now */ |
| 130 | ret = sdw_slave_sysfs_init(slave); |
| 131 | if (ret < 0) |
| 132 | dev_warn(dev, "Slave sysfs init failed:%d\n", ret); |
| 133 | |
Vinod Koul | 56d4fe3 | 2017-12-14 11:19:35 +0530 | [diff] [blame] | 134 | /* |
| 135 | * Check for valid clk_stop_timeout, use DisCo worst case value of |
| 136 | * 300ms |
| 137 | * |
| 138 | * TODO: check the timeouts and driver removal case |
| 139 | */ |
| 140 | if (slave->prop.clk_stop_timeout == 0) |
| 141 | slave->prop.clk_stop_timeout = 300; |
| 142 | |
| 143 | slave->bus->clk_stop_timeout = max_t(u32, slave->bus->clk_stop_timeout, |
Pierre-Louis Bossart | 74a8d80 | 2019-05-01 10:57:32 -0500 | [diff] [blame] | 144 | slave->prop.clk_stop_timeout); |
Vinod Koul | 56d4fe3 | 2017-12-14 11:19:35 +0530 | [diff] [blame] | 145 | |
Pierre-Louis Bossart | 2140b66 | 2020-01-14 18:08:35 -0600 | [diff] [blame] | 146 | slave->probed = true; |
Pierre-Louis Bossart | bd29c00 | 2022-06-21 17:56:38 -0500 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * if the probe happened after the bus was started, notify the codec driver |
| 150 | * of the current hardware status to e.g. start the initialization. |
| 151 | * Errors are only logged as warnings to avoid failing the probe. |
| 152 | */ |
| 153 | if (drv->ops && drv->ops->update_status) { |
| 154 | ret = drv->ops->update_status(slave, slave->status); |
| 155 | if (ret < 0) |
| 156 | dev_warn(dev, "%s: update_status failed with status %d\n", __func__, ret); |
| 157 | } |
| 158 | |
| 159 | mutex_unlock(&slave->sdw_dev_lock); |
Pierre-Louis Bossart | 2140b66 | 2020-01-14 18:08:35 -0600 | [diff] [blame] | 160 | |
| 161 | dev_dbg(dev, "probe complete\n"); |
| 162 | |
Vinod Koul | 56d4fe3 | 2017-12-14 11:19:35 +0530 | [diff] [blame] | 163 | return 0; |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static int sdw_drv_remove(struct device *dev) |
| 167 | { |
| 168 | struct sdw_slave *slave = dev_to_sdw_dev(dev); |
| 169 | struct sdw_driver *drv = drv_to_sdw_driver(dev->driver); |
| 170 | int ret = 0; |
| 171 | |
Pierre-Louis Bossart | bd29c00 | 2022-06-21 17:56:38 -0500 | [diff] [blame] | 172 | mutex_lock(&slave->sdw_dev_lock); |
Lucas Tanure | 12a9512 | 2023-08-04 11:45:57 +0100 | [diff] [blame] | 173 | |
Pierre-Louis Bossart | bd29c00 | 2022-06-21 17:56:38 -0500 | [diff] [blame] | 174 | slave->probed = false; |
Lucas Tanure | 12a9512 | 2023-08-04 11:45:57 +0100 | [diff] [blame] | 175 | |
| 176 | if (slave->prop.use_domain_irq) |
Charles Keepax | 3b6c4a1 | 2023-09-20 17:04:01 +0100 | [diff] [blame] | 177 | sdw_irq_dispose_mapping(slave); |
Lucas Tanure | 12a9512 | 2023-08-04 11:45:57 +0100 | [diff] [blame] | 178 | |
Richard Fitzgerald | 3dca1f8 | 2023-01-23 17:25:20 +0000 | [diff] [blame] | 179 | mutex_unlock(&slave->sdw_dev_lock); |
Pierre-Louis Bossart | bd29c00 | 2022-06-21 17:56:38 -0500 | [diff] [blame] | 180 | |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 181 | if (drv->remove) |
| 182 | ret = drv->remove(slave); |
| 183 | |
| 184 | dev_pm_domain_detach(dev, false); |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static void sdw_drv_shutdown(struct device *dev) |
| 190 | { |
| 191 | struct sdw_slave *slave = dev_to_sdw_dev(dev); |
| 192 | struct sdw_driver *drv = drv_to_sdw_driver(dev->driver); |
| 193 | |
| 194 | if (drv->shutdown) |
| 195 | drv->shutdown(slave); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * __sdw_register_driver() - register a SoundWire Slave driver |
| 200 | * @drv: driver to register |
| 201 | * @owner: owning module/driver |
| 202 | * |
| 203 | * Return: zero on success, else a negative error code. |
| 204 | */ |
| 205 | int __sdw_register_driver(struct sdw_driver *drv, struct module *owner) |
| 206 | { |
Pierre-Louis Bossart | 0196b52 | 2021-03-02 17:11:12 +0800 | [diff] [blame] | 207 | const char *name; |
| 208 | |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 209 | drv->driver.bus = &sdw_bus_type; |
| 210 | |
| 211 | if (!drv->probe) { |
Pierre-Louis Bossart | 0196b52 | 2021-03-02 17:11:12 +0800 | [diff] [blame] | 212 | name = drv->name; |
| 213 | if (!name) |
| 214 | name = drv->driver.name; |
| 215 | |
| 216 | pr_err("driver %s didn't provide SDW probe routine\n", name); |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | drv->driver.owner = owner; |
| 221 | drv->driver.probe = sdw_drv_probe; |
Pierre-Louis Bossart | df64077 | 2022-06-10 09:51:05 +0800 | [diff] [blame] | 222 | drv->driver.remove = sdw_drv_remove; |
| 223 | drv->driver.shutdown = sdw_drv_shutdown; |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 224 | |
| 225 | return driver_register(&drv->driver); |
| 226 | } |
| 227 | EXPORT_SYMBOL_GPL(__sdw_register_driver); |
| 228 | |
| 229 | /** |
| 230 | * sdw_unregister_driver() - unregisters the SoundWire Slave driver |
| 231 | * @drv: driver to unregister |
| 232 | */ |
| 233 | void sdw_unregister_driver(struct sdw_driver *drv) |
| 234 | { |
| 235 | driver_unregister(&drv->driver); |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(sdw_unregister_driver); |
| 238 | |
| 239 | static int __init sdw_bus_init(void) |
| 240 | { |
Pierre-Louis Bossart | bf03473 | 2019-08-21 13:58:18 -0500 | [diff] [blame] | 241 | sdw_debugfs_init(); |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 242 | return bus_register(&sdw_bus_type); |
| 243 | } |
| 244 | |
| 245 | static void __exit sdw_bus_exit(void) |
| 246 | { |
Pierre-Louis Bossart | bf03473 | 2019-08-21 13:58:18 -0500 | [diff] [blame] | 247 | sdw_debugfs_exit(); |
Vinod Koul | 9251345 | 2017-12-14 11:19:33 +0530 | [diff] [blame] | 248 | bus_unregister(&sdw_bus_type); |
| 249 | } |
| 250 | |
| 251 | postcore_initcall(sdw_bus_init); |
| 252 | module_exit(sdw_bus_exit); |
| 253 | |
| 254 | MODULE_DESCRIPTION("SoundWire bus"); |
| 255 | MODULE_LICENSE("GPL v2"); |