Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Linux driver model AC97 bus interface
0004  *
0005  * Author:  Nicolas Pitre
0006  * Created: Jan 14, 2005
0007  * Copyright:   (C) MontaVista Software Inc.
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/device.h>
0013 #include <linux/string.h>
0014 #include <sound/ac97_codec.h>
0015 
0016 /*
0017  * snd_ac97_check_id() - Reads and checks the vendor ID of the device
0018  * @ac97: The AC97 device to check
0019  * @id: The ID to compare to
0020  * @id_mask: Mask that is applied to the device ID before comparing to @id
0021  *
0022  * If @id is 0 this function returns true if the read device vendor ID is
0023  * a valid ID. If @id is non 0 this functions returns true if @id
0024  * matches the read vendor ID. Otherwise the function returns false.
0025  */
0026 static bool snd_ac97_check_id(struct snd_ac97 *ac97, unsigned int id,
0027     unsigned int id_mask)
0028 {
0029     ac97->id = ac97->bus->ops->read(ac97, AC97_VENDOR_ID1) << 16;
0030     ac97->id |= ac97->bus->ops->read(ac97, AC97_VENDOR_ID2);
0031 
0032     if (ac97->id == 0x0 || ac97->id == 0xffffffff)
0033         return false;
0034 
0035     if (id != 0 && id != (ac97->id & id_mask))
0036         return false;
0037 
0038     return true;
0039 }
0040 
0041 /**
0042  * snd_ac97_reset() - Reset AC'97 device
0043  * @ac97: The AC'97 device to reset
0044  * @try_warm: Try a warm reset first
0045  * @id: Expected device vendor ID
0046  * @id_mask: Mask that is applied to the device ID before comparing to @id
0047  *
0048  * This function resets the AC'97 device. If @try_warm is true the function
0049  * first performs a warm reset. If the warm reset is successful the function
0050  * returns 1. Otherwise or if @try_warm is false the function issues cold reset
0051  * followed by a warm reset. If this is successful the function returns 0,
0052  * otherwise a negative error code. If @id is 0 any valid device ID will be
0053  * accepted, otherwise only the ID that matches @id and @id_mask is accepted.
0054  */
0055 int snd_ac97_reset(struct snd_ac97 *ac97, bool try_warm, unsigned int id,
0056     unsigned int id_mask)
0057 {
0058     const struct snd_ac97_bus_ops *ops = ac97->bus->ops;
0059 
0060     if (try_warm && ops->warm_reset) {
0061         ops->warm_reset(ac97);
0062         if (snd_ac97_check_id(ac97, id, id_mask))
0063             return 1;
0064     }
0065 
0066     if (ops->reset)
0067         ops->reset(ac97);
0068     if (ops->warm_reset)
0069         ops->warm_reset(ac97);
0070 
0071     if (snd_ac97_check_id(ac97, id, id_mask))
0072         return 0;
0073 
0074     return -ENODEV;
0075 }
0076 EXPORT_SYMBOL_GPL(snd_ac97_reset);
0077 
0078 /*
0079  * Let drivers decide whether they want to support given codec from their
0080  * probe method. Drivers have direct access to the struct snd_ac97
0081  * structure and may  decide based on the id field amongst other things.
0082  */
0083 static int ac97_bus_match(struct device *dev, struct device_driver *drv)
0084 {
0085     return 1;
0086 }
0087 
0088 struct bus_type ac97_bus_type = {
0089     .name       = "ac97",
0090     .match      = ac97_bus_match,
0091 };
0092 
0093 static int __init ac97_bus_init(void)
0094 {
0095     return bus_register(&ac97_bus_type);
0096 }
0097 
0098 subsys_initcall(ac97_bus_init);
0099 
0100 static void __exit ac97_bus_exit(void)
0101 {
0102     bus_unregister(&ac97_bus_type);
0103 }
0104 
0105 module_exit(ac97_bus_exit);
0106 
0107 EXPORT_SYMBOL(ac97_bus_type);
0108 
0109 MODULE_LICENSE("GPL");