xref: /linux/sound/ac97_bus.c (revision f4b369c6fe0ceaba2da2daff8c9eb415f85926dd)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e1036502SNicolas Pitre /*
3e1036502SNicolas Pitre  * Linux driver model AC97 bus interface
4e1036502SNicolas Pitre  *
5e1036502SNicolas Pitre  * Author:	Nicolas Pitre
6e1036502SNicolas Pitre  * Created:	Jan 14, 2005
7e1036502SNicolas Pitre  * Copyright:	(C) MontaVista Software Inc.
8e1036502SNicolas Pitre  */
9e1036502SNicolas Pitre 
10e1036502SNicolas Pitre #include <linux/module.h>
11e1036502SNicolas Pitre #include <linux/init.h>
12e1036502SNicolas Pitre #include <linux/device.h>
13e1036502SNicolas Pitre #include <linux/string.h>
1496841baeSMark Brown #include <sound/ac97_codec.h>
15e1036502SNicolas Pitre 
16e1036502SNicolas Pitre /*
175f1d980eSLars-Peter Clausen  * snd_ac97_check_id() - Reads and checks the vendor ID of the device
185f1d980eSLars-Peter Clausen  * @ac97: The AC97 device to check
195f1d980eSLars-Peter Clausen  * @id: The ID to compare to
205f1d980eSLars-Peter Clausen  * @id_mask: Mask that is applied to the device ID before comparing to @id
215f1d980eSLars-Peter Clausen  *
225f1d980eSLars-Peter Clausen  * If @id is 0 this function returns true if the read device vendor ID is
235f1d980eSLars-Peter Clausen  * a valid ID. If @id is non 0 this functions returns true if @id
245f1d980eSLars-Peter Clausen  * matches the read vendor ID. Otherwise the function returns false.
255f1d980eSLars-Peter Clausen  */
snd_ac97_check_id(struct snd_ac97 * ac97,unsigned int id,unsigned int id_mask)265f1d980eSLars-Peter Clausen static bool snd_ac97_check_id(struct snd_ac97 *ac97, unsigned int id,
275f1d980eSLars-Peter Clausen 	unsigned int id_mask)
285f1d980eSLars-Peter Clausen {
295f1d980eSLars-Peter Clausen 	ac97->id = ac97->bus->ops->read(ac97, AC97_VENDOR_ID1) << 16;
305f1d980eSLars-Peter Clausen 	ac97->id |= ac97->bus->ops->read(ac97, AC97_VENDOR_ID2);
315f1d980eSLars-Peter Clausen 
325f1d980eSLars-Peter Clausen 	if (ac97->id == 0x0 || ac97->id == 0xffffffff)
335f1d980eSLars-Peter Clausen 		return false;
345f1d980eSLars-Peter Clausen 
355f1d980eSLars-Peter Clausen 	if (id != 0 && id != (ac97->id & id_mask))
365f1d980eSLars-Peter Clausen 		return false;
375f1d980eSLars-Peter Clausen 
385f1d980eSLars-Peter Clausen 	return true;
395f1d980eSLars-Peter Clausen }
405f1d980eSLars-Peter Clausen 
415f1d980eSLars-Peter Clausen /**
425f1d980eSLars-Peter Clausen  * snd_ac97_reset() - Reset AC'97 device
435f1d980eSLars-Peter Clausen  * @ac97: The AC'97 device to reset
445f1d980eSLars-Peter Clausen  * @try_warm: Try a warm reset first
455f1d980eSLars-Peter Clausen  * @id: Expected device vendor ID
465f1d980eSLars-Peter Clausen  * @id_mask: Mask that is applied to the device ID before comparing to @id
475f1d980eSLars-Peter Clausen  *
485f1d980eSLars-Peter Clausen  * This function resets the AC'97 device. If @try_warm is true the function
49*ab3bd366SHariKrishna Sagala  * first performs a warm reset. If @try_warm is false the function issues
50*ab3bd366SHariKrishna Sagala  * cold reset followed by a warm reset. If @id is 0 any valid device ID
51*ab3bd366SHariKrishna Sagala  * will be accepted, otherwise only the ID that matches @id and @id_mask
52*ab3bd366SHariKrishna Sagala  * is accepted.
53*ab3bd366SHariKrishna Sagala  * Returns:
54*ab3bd366SHariKrishna Sagala  * * %1 - if warm reset is successful
55*ab3bd366SHariKrishna Sagala  * * %0 - if cold reset and warm reset is successful
56*ab3bd366SHariKrishna Sagala  * * %-ENODEV - if @id and @id_mask not matching
575f1d980eSLars-Peter Clausen  */
snd_ac97_reset(struct snd_ac97 * ac97,bool try_warm,unsigned int id,unsigned int id_mask)585f1d980eSLars-Peter Clausen int snd_ac97_reset(struct snd_ac97 *ac97, bool try_warm, unsigned int id,
595f1d980eSLars-Peter Clausen 	unsigned int id_mask)
605f1d980eSLars-Peter Clausen {
6119260818STakashi Iwai 	const struct snd_ac97_bus_ops *ops = ac97->bus->ops;
625f1d980eSLars-Peter Clausen 
635f1d980eSLars-Peter Clausen 	if (try_warm && ops->warm_reset) {
645f1d980eSLars-Peter Clausen 		ops->warm_reset(ac97);
655f1d980eSLars-Peter Clausen 		if (snd_ac97_check_id(ac97, id, id_mask))
665f1d980eSLars-Peter Clausen 			return 1;
675f1d980eSLars-Peter Clausen 	}
685f1d980eSLars-Peter Clausen 
695f1d980eSLars-Peter Clausen 	if (ops->reset)
705f1d980eSLars-Peter Clausen 		ops->reset(ac97);
715f1d980eSLars-Peter Clausen 	if (ops->warm_reset)
725f1d980eSLars-Peter Clausen 		ops->warm_reset(ac97);
735f1d980eSLars-Peter Clausen 
745f1d980eSLars-Peter Clausen 	if (snd_ac97_check_id(ac97, id, id_mask))
755f1d980eSLars-Peter Clausen 		return 0;
765f1d980eSLars-Peter Clausen 
775f1d980eSLars-Peter Clausen 	return -ENODEV;
785f1d980eSLars-Peter Clausen }
795f1d980eSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_ac97_reset);
805f1d980eSLars-Peter Clausen 
8166e82d21SGreg Kroah-Hartman const struct bus_type ac97_bus_type = {
82e1036502SNicolas Pitre 	.name		= "ac97",
83e1036502SNicolas Pitre };
84e1036502SNicolas Pitre 
ac97_bus_init(void)85e1036502SNicolas Pitre static int __init ac97_bus_init(void)
86e1036502SNicolas Pitre {
87e1036502SNicolas Pitre 	return bus_register(&ac97_bus_type);
88e1036502SNicolas Pitre }
89e1036502SNicolas Pitre 
90e1036502SNicolas Pitre subsys_initcall(ac97_bus_init);
91e1036502SNicolas Pitre 
ac97_bus_exit(void)92e1036502SNicolas Pitre static void __exit ac97_bus_exit(void)
93e1036502SNicolas Pitre {
94e1036502SNicolas Pitre 	bus_unregister(&ac97_bus_type);
95e1036502SNicolas Pitre }
96e1036502SNicolas Pitre 
97e1036502SNicolas Pitre module_exit(ac97_bus_exit);
98e1036502SNicolas Pitre 
99e1036502SNicolas Pitre EXPORT_SYMBOL(ac97_bus_type);
100e1036502SNicolas Pitre 
10192641cc5STakashi Iwai MODULE_DESCRIPTION("Legacy AC97 bus interface");
102e1036502SNicolas Pitre MODULE_LICENSE("GPL");
103