ac97_bus.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux driver model AC97 bus interface
  4. *
  5. * Author: Nicolas Pitre
  6. * Created: Jan 14, 2005
  7. * Copyright: (C) MontaVista Software Inc.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/device.h>
  12. #include <linux/string.h>
  13. #include <sound/ac97_codec.h>
  14. /*
  15. * snd_ac97_check_id() - Reads and checks the vendor ID of the device
  16. * @ac97: The AC97 device to check
  17. * @id: The ID to compare to
  18. * @id_mask: Mask that is applied to the device ID before comparing to @id
  19. *
  20. * If @id is 0 this function returns true if the read device vendor ID is
  21. * a valid ID. If @id is non 0 this functions returns true if @id
  22. * matches the read vendor ID. Otherwise the function returns false.
  23. */
  24. static bool snd_ac97_check_id(struct snd_ac97 *ac97, unsigned int id,
  25. unsigned int id_mask)
  26. {
  27. ac97->id = ac97->bus->ops->read(ac97, AC97_VENDOR_ID1) << 16;
  28. ac97->id |= ac97->bus->ops->read(ac97, AC97_VENDOR_ID2);
  29. if (ac97->id == 0x0 || ac97->id == 0xffffffff)
  30. return false;
  31. if (id != 0 && id != (ac97->id & id_mask))
  32. return false;
  33. return true;
  34. }
  35. /**
  36. * snd_ac97_reset() - Reset AC'97 device
  37. * @ac97: The AC'97 device to reset
  38. * @try_warm: Try a warm reset first
  39. * @id: Expected device vendor ID
  40. * @id_mask: Mask that is applied to the device ID before comparing to @id
  41. *
  42. * This function resets the AC'97 device. If @try_warm is true the function
  43. * first performs a warm reset. If the warm reset is successful the function
  44. * returns 1. Otherwise or if @try_warm is false the function issues cold reset
  45. * followed by a warm reset. If this is successful the function returns 0,
  46. * otherwise a negative error code. If @id is 0 any valid device ID will be
  47. * accepted, otherwise only the ID that matches @id and @id_mask is accepted.
  48. */
  49. int snd_ac97_reset(struct snd_ac97 *ac97, bool try_warm, unsigned int id,
  50. unsigned int id_mask)
  51. {
  52. const struct snd_ac97_bus_ops *ops = ac97->bus->ops;
  53. if (try_warm && ops->warm_reset) {
  54. ops->warm_reset(ac97);
  55. if (snd_ac97_check_id(ac97, id, id_mask))
  56. return 1;
  57. }
  58. if (ops->reset)
  59. ops->reset(ac97);
  60. if (ops->warm_reset)
  61. ops->warm_reset(ac97);
  62. if (snd_ac97_check_id(ac97, id, id_mask))
  63. return 0;
  64. return -ENODEV;
  65. }
  66. EXPORT_SYMBOL_GPL(snd_ac97_reset);
  67. /*
  68. * Let drivers decide whether they want to support given codec from their
  69. * probe method. Drivers have direct access to the struct snd_ac97
  70. * structure and may decide based on the id field amongst other things.
  71. */
  72. static int ac97_bus_match(struct device *dev, struct device_driver *drv)
  73. {
  74. return 1;
  75. }
  76. struct bus_type ac97_bus_type = {
  77. .name = "ac97",
  78. .match = ac97_bus_match,
  79. };
  80. static int __init ac97_bus_init(void)
  81. {
  82. return bus_register(&ac97_bus_type);
  83. }
  84. subsys_initcall(ac97_bus_init);
  85. static void __exit ac97_bus_exit(void)
  86. {
  87. bus_unregister(&ac97_bus_type);
  88. }
  89. module_exit(ac97_bus_exit);
  90. EXPORT_SYMBOL(ac97_bus_type);
  91. MODULE_LICENSE("GPL");