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