xref: /linux/sound/ac97_bus.c (revision c31f4aa8fed048fa70e742c4bb49bb48dc489ab3)
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 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/string.h>
14 #include <sound/ac97_codec.h>
15 
16 /*
17  * snd_ac97_check_id() - Reads and checks the vendor ID of the device
18  * @ac97: The AC97 device to check
19  * @id: The ID to compare to
20  * @id_mask: Mask that is applied to the device ID before comparing to @id
21  *
22  * If @id is 0 this function returns true if the read device vendor ID is
23  * a valid ID. If @id is non 0 this functions returns true if @id
24  * matches the read vendor ID. Otherwise the function returns false.
25  */
26 static bool snd_ac97_check_id(struct snd_ac97 *ac97, unsigned int id,
27 	unsigned int id_mask)
28 {
29 	ac97->id = ac97->bus->ops->read(ac97, AC97_VENDOR_ID1) << 16;
30 	ac97->id |= ac97->bus->ops->read(ac97, AC97_VENDOR_ID2);
31 
32 	if (ac97->id == 0x0 || ac97->id == 0xffffffff)
33 		return false;
34 
35 	if (id != 0 && id != (ac97->id & id_mask))
36 		return false;
37 
38 	return true;
39 }
40 
41 /**
42  * snd_ac97_reset() - Reset AC'97 device
43  * @ac97: The AC'97 device to reset
44  * @try_warm: Try a warm reset first
45  * @id: Expected device vendor ID
46  * @id_mask: Mask that is applied to the device ID before comparing to @id
47  *
48  * This function resets the AC'97 device. If @try_warm is true the function
49  * first performs a warm reset. If @try_warm is false the function issues
50  * cold reset followed by a warm reset. If @id is 0 any valid device ID
51  * will be accepted, otherwise only the ID that matches @id and @id_mask
52  * is accepted.
53  * Returns:
54  * * %1 - if warm reset is successful
55  * * %0 - if cold reset and warm reset is successful
56  * * %-ENODEV - if @id and @id_mask not matching
57  */
58 int snd_ac97_reset(struct snd_ac97 *ac97, bool try_warm, unsigned int id,
59 	unsigned int id_mask)
60 {
61 	const struct snd_ac97_bus_ops *ops = ac97->bus->ops;
62 
63 	if (try_warm && ops->warm_reset) {
64 		ops->warm_reset(ac97);
65 		if (snd_ac97_check_id(ac97, id, id_mask))
66 			return 1;
67 	}
68 
69 	if (ops->reset)
70 		ops->reset(ac97);
71 	if (ops->warm_reset)
72 		ops->warm_reset(ac97);
73 
74 	if (snd_ac97_check_id(ac97, id, id_mask))
75 		return 0;
76 
77 	return -ENODEV;
78 }
79 EXPORT_SYMBOL_GPL(snd_ac97_reset);
80 
81 const struct bus_type ac97_bus_type = {
82 	.name		= "ac97",
83 };
84 
85 static int __init ac97_bus_init(void)
86 {
87 	return bus_register(&ac97_bus_type);
88 }
89 
90 subsys_initcall(ac97_bus_init);
91 
92 static void __exit ac97_bus_exit(void)
93 {
94 	bus_unregister(&ac97_bus_type);
95 }
96 
97 module_exit(ac97_bus_exit);
98 
99 EXPORT_SYMBOL(ac97_bus_type);
100 
101 MODULE_DESCRIPTION("Legacy AC97 bus interface");
102 MODULE_LICENSE("GPL");
103