blob: 3732a63a2a81f58bbf808cf91008cb158afb8e97 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Liam Girdwood0ca06a02005-07-29 16:13:36 +02002/*
3 * Linux driver model AC97 bus interface
4 *
5 * Author: Nicolas Pitre
6 * Created: Jan 14, 2005
7 * Copyright: (C) MontaVista Software Inc.
Liam Girdwood0ca06a02005-07-29 16:13:36 +02008 */
9
Jaroslav Kysela5049c352005-08-22 12:19:14 +020010#include <linux/module.h>
Liam Girdwood0ca06a02005-07-29 16:13:36 +020011#include <linux/init.h>
12#include <linux/device.h>
13#include <linux/string.h>
Mark Brown96841ba2008-12-02 15:15:50 +000014#include <sound/ac97_codec.h>
Liam Girdwood0ca06a02005-07-29 16:13:36 +020015
16/*
Lars-Peter Clausen5f1d9802015-07-21 21:53:00 +020017 * 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 */
26static 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 the warm reset is successful the function
50 * returns 1. Otherwise or if @try_warm is false the function issues cold reset
51 * followed by a warm reset. If this is successful the function returns 0,
52 * otherwise a negative error code. If @id is 0 any valid device ID will be
53 * accepted, otherwise only the ID that matches @id and @id_mask is accepted.
54 */
55int snd_ac97_reset(struct snd_ac97 *ac97, bool try_warm, unsigned int id,
56 unsigned int id_mask)
57{
58 struct snd_ac97_bus_ops *ops = ac97->bus->ops;
59
60 if (try_warm && ops->warm_reset) {
61 ops->warm_reset(ac97);
62 if (snd_ac97_check_id(ac97, id, id_mask))
63 return 1;
64 }
65
66 if (ops->reset)
67 ops->reset(ac97);
68 if (ops->warm_reset)
69 ops->warm_reset(ac97);
70
71 if (snd_ac97_check_id(ac97, id, id_mask))
72 return 0;
73
74 return -ENODEV;
75}
76EXPORT_SYMBOL_GPL(snd_ac97_reset);
77
78/*
Nicolas Pitre3a91e952005-09-16 18:46:36 +020079 * Let drivers decide whether they want to support given codec from their
Jeffrin Josed0359c62010-12-06 19:27:53 +053080 * probe method. Drivers have direct access to the struct snd_ac97
81 * structure and may decide based on the id field amongst other things.
Liam Girdwood0ca06a02005-07-29 16:13:36 +020082 */
83static int ac97_bus_match(struct device *dev, struct device_driver *drv)
84{
Nicolas Pitre3a91e952005-09-16 18:46:36 +020085 return 1;
Liam Girdwood0ca06a02005-07-29 16:13:36 +020086}
87
Liam Girdwood0ca06a02005-07-29 16:13:36 +020088struct bus_type ac97_bus_type = {
89 .name = "ac97",
90 .match = ac97_bus_match,
Liam Girdwood0ca06a02005-07-29 16:13:36 +020091};
92
93static int __init ac97_bus_init(void)
94{
95 return bus_register(&ac97_bus_type);
96}
97
98subsys_initcall(ac97_bus_init);
99
100static void __exit ac97_bus_exit(void)
101{
102 bus_unregister(&ac97_bus_type);
103}
104
105module_exit(ac97_bus_exit);
106
107EXPORT_SYMBOL(ac97_bus_type);
108
109MODULE_LICENSE("GPL");