bus_type.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright(c) 2015-17 Intel Corporation.
  3. #include <linux/module.h>
  4. #include <linux/mod_devicetable.h>
  5. #include <linux/pm_domain.h>
  6. #include <linux/soundwire/sdw.h>
  7. #include <linux/soundwire/sdw_type.h>
  8. /**
  9. * sdw_get_device_id - find the matching SoundWire device id
  10. * @slave: SoundWire Slave Device
  11. * @drv: SoundWire Slave Driver
  12. *
  13. * The match is done by comparing the mfg_id and part_id from the
  14. * struct sdw_device_id.
  15. */
  16. static const struct sdw_device_id *
  17. sdw_get_device_id(struct sdw_slave *slave, struct sdw_driver *drv)
  18. {
  19. const struct sdw_device_id *id = drv->id_table;
  20. while (id && id->mfg_id) {
  21. if (slave->id.mfg_id == id->mfg_id &&
  22. slave->id.part_id == id->part_id)
  23. return id;
  24. id++;
  25. }
  26. return NULL;
  27. }
  28. static int sdw_bus_match(struct device *dev, struct device_driver *ddrv)
  29. {
  30. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  31. struct sdw_driver *drv = drv_to_sdw_driver(ddrv);
  32. return !!sdw_get_device_id(slave, drv);
  33. }
  34. int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size)
  35. {
  36. /* modalias is sdw:m<mfg_id>p<part_id> */
  37. return snprintf(buf, size, "sdw:m%04Xp%04X\n",
  38. slave->id.mfg_id, slave->id.part_id);
  39. }
  40. static int sdw_uevent(struct device *dev, struct kobj_uevent_env *env)
  41. {
  42. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  43. char modalias[32];
  44. sdw_slave_modalias(slave, modalias, sizeof(modalias));
  45. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  46. return -ENOMEM;
  47. return 0;
  48. }
  49. struct bus_type sdw_bus_type = {
  50. .name = "soundwire",
  51. .match = sdw_bus_match,
  52. .uevent = sdw_uevent,
  53. };
  54. EXPORT_SYMBOL_GPL(sdw_bus_type);
  55. static int sdw_drv_probe(struct device *dev)
  56. {
  57. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  58. struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
  59. const struct sdw_device_id *id;
  60. int ret;
  61. id = sdw_get_device_id(slave, drv);
  62. if (!id)
  63. return -ENODEV;
  64. slave->ops = drv->ops;
  65. /*
  66. * attach to power domain but don't turn on (last arg)
  67. */
  68. ret = dev_pm_domain_attach(dev, false);
  69. if (ret)
  70. return ret;
  71. ret = drv->probe(slave, id);
  72. if (ret) {
  73. dev_err(dev, "Probe of %s failed: %d\n", drv->name, ret);
  74. dev_pm_domain_detach(dev, false);
  75. return ret;
  76. }
  77. /* device is probed so let's read the properties now */
  78. if (slave->ops && slave->ops->read_prop)
  79. slave->ops->read_prop(slave);
  80. /*
  81. * Check for valid clk_stop_timeout, use DisCo worst case value of
  82. * 300ms
  83. *
  84. * TODO: check the timeouts and driver removal case
  85. */
  86. if (slave->prop.clk_stop_timeout == 0)
  87. slave->prop.clk_stop_timeout = 300;
  88. slave->bus->clk_stop_timeout = max_t(u32, slave->bus->clk_stop_timeout,
  89. slave->prop.clk_stop_timeout);
  90. return 0;
  91. }
  92. static int sdw_drv_remove(struct device *dev)
  93. {
  94. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  95. struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
  96. int ret = 0;
  97. if (drv->remove)
  98. ret = drv->remove(slave);
  99. dev_pm_domain_detach(dev, false);
  100. return ret;
  101. }
  102. static void sdw_drv_shutdown(struct device *dev)
  103. {
  104. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  105. struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
  106. if (drv->shutdown)
  107. drv->shutdown(slave);
  108. }
  109. /**
  110. * __sdw_register_driver() - register a SoundWire Slave driver
  111. * @drv: driver to register
  112. * @owner: owning module/driver
  113. *
  114. * Return: zero on success, else a negative error code.
  115. */
  116. int __sdw_register_driver(struct sdw_driver *drv, struct module *owner)
  117. {
  118. drv->driver.bus = &sdw_bus_type;
  119. if (!drv->probe) {
  120. pr_err("driver %s didn't provide SDW probe routine\n",
  121. drv->name);
  122. return -EINVAL;
  123. }
  124. drv->driver.owner = owner;
  125. drv->driver.probe = sdw_drv_probe;
  126. if (drv->remove)
  127. drv->driver.remove = sdw_drv_remove;
  128. if (drv->shutdown)
  129. drv->driver.shutdown = sdw_drv_shutdown;
  130. return driver_register(&drv->driver);
  131. }
  132. EXPORT_SYMBOL_GPL(__sdw_register_driver);
  133. /**
  134. * sdw_unregister_driver() - unregisters the SoundWire Slave driver
  135. * @drv: driver to unregister
  136. */
  137. void sdw_unregister_driver(struct sdw_driver *drv)
  138. {
  139. driver_unregister(&drv->driver);
  140. }
  141. EXPORT_SYMBOL_GPL(sdw_unregister_driver);
  142. static int __init sdw_bus_init(void)
  143. {
  144. return bus_register(&sdw_bus_type);
  145. }
  146. static void __exit sdw_bus_exit(void)
  147. {
  148. bus_unregister(&sdw_bus_type);
  149. }
  150. postcore_initcall(sdw_bus_init);
  151. module_exit(sdw_bus_exit);
  152. MODULE_DESCRIPTION("SoundWire bus");
  153. MODULE_LICENSE("GPL v2");