blob: d2479bba75bfd49b689716f95fdb240dfd339445 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Robert Jarzmik74426fb2017-09-02 21:54:04 +02002/*
3 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
Robert Jarzmik74426fb2017-09-02 21:54:04 +02004 */
5
6#include <linux/list.h>
7#include <linux/slab.h>
8#include <sound/ac97/codec.h>
9#include <sound/ac97/compat.h>
10#include <sound/ac97/controller.h>
11#include <sound/soc.h>
12
13#include "ac97_core.h"
14
Lihua Yaoc7b81702018-08-15 20:59:46 +080015static void compat_ac97_release(struct device *dev)
16{
17 kfree(to_ac97_t(dev));
18}
19
Robert Jarzmik74426fb2017-09-02 21:54:04 +020020static void compat_ac97_reset(struct snd_ac97 *ac97)
21{
22 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
23 struct ac97_controller *actrl = adev->ac97_ctrl;
24
25 if (actrl->ops->reset)
26 actrl->ops->reset(actrl);
27}
28
29static void compat_ac97_warm_reset(struct snd_ac97 *ac97)
30{
31 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
32 struct ac97_controller *actrl = adev->ac97_ctrl;
33
34 if (actrl->ops->warm_reset)
35 actrl->ops->warm_reset(actrl);
36}
37
38static void compat_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
39 unsigned short val)
40{
41 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
42 struct ac97_controller *actrl = adev->ac97_ctrl;
43
44 actrl->ops->write(actrl, ac97->num, reg, val);
45}
46
47static unsigned short compat_ac97_read(struct snd_ac97 *ac97,
48 unsigned short reg)
49{
50 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
51 struct ac97_controller *actrl = adev->ac97_ctrl;
52
53 return actrl->ops->read(actrl, ac97->num, reg);
54}
55
Takashi Iwai33c83aa2020-01-03 09:16:39 +010056static const struct snd_ac97_bus_ops compat_snd_ac97_bus_ops = {
Robert Jarzmik74426fb2017-09-02 21:54:04 +020057 .reset = compat_ac97_reset,
58 .warm_reset = compat_ac97_warm_reset,
59 .write = compat_ac97_write,
60 .read = compat_ac97_read,
61};
62
63static struct snd_ac97_bus compat_soc_ac97_bus = {
64 .ops = &compat_snd_ac97_bus_ops,
65};
66
67struct snd_ac97 *snd_ac97_compat_alloc(struct ac97_codec_device *adev)
68{
69 struct snd_ac97 *ac97;
Lihua Yaoc7b81702018-08-15 20:59:46 +080070 int ret;
Robert Jarzmik74426fb2017-09-02 21:54:04 +020071
72 ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
73 if (ac97 == NULL)
74 return ERR_PTR(-ENOMEM);
75
Robert Jarzmik74426fb2017-09-02 21:54:04 +020076 ac97->private_data = adev;
77 ac97->bus = &compat_soc_ac97_bus;
Lihua Yaoc7b81702018-08-15 20:59:46 +080078
79 ac97->dev.parent = &adev->dev;
80 ac97->dev.release = compat_ac97_release;
81 dev_set_name(&ac97->dev, "%s-compat", dev_name(&adev->dev));
82 ret = device_register(&ac97->dev);
83 if (ret) {
84 put_device(&ac97->dev);
85 return ERR_PTR(ret);
86 }
87
Robert Jarzmik74426fb2017-09-02 21:54:04 +020088 return ac97;
89}
90EXPORT_SYMBOL_GPL(snd_ac97_compat_alloc);
91
92void snd_ac97_compat_release(struct snd_ac97 *ac97)
93{
Lihua Yaoc7b81702018-08-15 20:59:46 +080094 device_unregister(&ac97->dev);
Robert Jarzmik74426fb2017-09-02 21:54:04 +020095}
96EXPORT_SYMBOL_GPL(snd_ac97_compat_release);
97
98int snd_ac97_reset(struct snd_ac97 *ac97, bool try_warm, unsigned int id,
99 unsigned int id_mask)
100{
101 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
102 struct ac97_controller *actrl = adev->ac97_ctrl;
103 unsigned int scanned;
104
105 if (try_warm) {
106 compat_ac97_warm_reset(ac97);
107 scanned = snd_ac97_bus_scan_one(actrl, adev->num);
108 if (ac97_ids_match(scanned, adev->vendor_id, id_mask))
109 return 1;
110 }
111
112 compat_ac97_reset(ac97);
113 compat_ac97_warm_reset(ac97);
114 scanned = snd_ac97_bus_scan_one(actrl, adev->num);
115 if (ac97_ids_match(scanned, adev->vendor_id, id_mask))
116 return 0;
117
118 return -ENODEV;
119}
120EXPORT_SYMBOL_GPL(snd_ac97_reset);