aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sound/ac97/codec.h118
-rw-r--r--include/sound/ac97/compat.h20
-rw-r--r--include/sound/ac97/controller.h85
-rw-r--r--sound/ac97/Kconfig19
-rw-r--r--sound/ac97/Makefile8
-rw-r--r--sound/ac97/ac97_core.h16
-rw-r--r--sound/ac97/bus.c539
-rw-r--r--sound/ac97/codec.c15
-rw-r--r--sound/ac97/snd_ac97_compat.c108
9 files changed, 928 insertions, 0 deletions
diff --git a/include/sound/ac97/codec.h b/include/sound/ac97/codec.h
new file mode 100644
index 000000000000..ec04be9ab119
--- /dev/null
+++ b/include/sound/ac97/codec.h
@@ -0,0 +1,118 @@
1/*
2 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#ifndef __SOUND_AC97_CODEC2_H
9#define __SOUND_AC97_CODEC2_H
10
11#include <linux/device.h>
12
13#define AC97_ID(vendor_id1, vendor_id2) \
14 ((((vendor_id1) & 0xffff) << 16) | ((vendor_id2) & 0xffff))
15#define AC97_DRIVER_ID(vendor_id1, vendor_id2, mask_id1, mask_id2, _data) \
16 { .id = (((vendor_id1) & 0xffff) << 16) | ((vendor_id2) & 0xffff), \
17 .mask = (((mask_id1) & 0xffff) << 16) | ((mask_id2) & 0xffff), \
18 .data = (_data) }
19
20struct ac97_controller;
21struct clk;
22
23/**
24 * struct ac97_id - matches a codec device and driver on an ac97 bus
25 * @id: The significant bits if the codec vendor ID1 and ID2
26 * @mask: Bitmask specifying which bits of the id field are significant when
27 * matching. A driver binds to a device when :
28 * ((vendorID1 << 8 | vendorID2) & (mask_id1 << 8 | mask_id2)) == id.
29 * @data: Private data used by the driver.
30 */
31struct ac97_id {
32 unsigned int id;
33 unsigned int mask;
34 void *data;
35};
36
37/**
38 * ac97_codec_device - a ac97 codec
39 * @dev: the core device
40 * @vendor_id: the vendor_id of the codec, as sensed on the AC-link
41 * @num: the codec number, 0 is primary, 1 is first slave, etc ...
42 * @clk: the clock BIT_CLK provided by the codec
43 * @ac97_ctrl: ac97 digital controller on the same AC-link
44 *
45 * This is the device instantiated for each codec living on a AC-link. There are
46 * normally 0 to 4 codec devices per AC-link, and all of them are controlled by
47 * an AC97 digital controller.
48 */
49struct ac97_codec_device {
50 struct device dev;
51 unsigned int vendor_id;
52 unsigned int num;
53 struct clk *clk;
54 struct ac97_controller *ac97_ctrl;
55};
56
57/**
58 * ac97_codec_driver - a ac97 codec driver
59 * @driver: the device driver structure
60 * @probe: the function called when a ac97_codec_device is matched
61 * @remove: the function called when the device is unbound/removed
62 * @shutdown: shutdown function (might be NULL)
63 * @id_table: ac97 vendor_id match table, { } member terminated
64 */
65struct ac97_codec_driver {
66 struct device_driver driver;
67 int (*probe)(struct ac97_codec_device *);
68 int (*remove)(struct ac97_codec_device *);
69 void (*shutdown)(struct ac97_codec_device *);
70 const struct ac97_id *id_table;
71};
72
73static inline struct ac97_codec_device *to_ac97_device(struct device *d)
74{
75 return container_of(d, struct ac97_codec_device, dev);
76}
77
78static inline struct ac97_codec_driver *to_ac97_driver(struct device_driver *d)
79{
80 return container_of(d, struct ac97_codec_driver, driver);
81}
82
83#if IS_ENABLED(CONFIG_AC97_BUS_NEW)
84int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv);
85void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv);
86#else
87static inline int
88snd_ac97_codec_driver_register(struct ac97_codec_driver *drv)
89{
90 return 0;
91}
92static inline void
93snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv)
94{
95}
96#endif
97
98
99static inline struct device *
100ac97_codec_dev2dev(struct ac97_codec_device *adev)
101{
102 return &adev->dev;
103}
104
105static inline void *ac97_get_drvdata(struct ac97_codec_device *adev)
106{
107 return dev_get_drvdata(ac97_codec_dev2dev(adev));
108}
109
110static inline void ac97_set_drvdata(struct ac97_codec_device *adev,
111 void *data)
112{
113 dev_set_drvdata(ac97_codec_dev2dev(adev), data);
114}
115
116void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev);
117
118#endif
diff --git a/include/sound/ac97/compat.h b/include/sound/ac97/compat.h
new file mode 100644
index 000000000000..1351cba40048
--- /dev/null
+++ b/include/sound/ac97/compat.h
@@ -0,0 +1,20 @@
1/*
2 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This file is for backward compatibility with snd_ac97 structure and its
9 * multiple usages, such as the snd_ac97_bus and snd_ac97_build_ops.
10 *
11 */
12#ifndef AC97_COMPAT_H
13#define AC97_COMPAT_H
14
15#include <sound/ac97_codec.h>
16
17struct snd_ac97 *snd_ac97_compat_alloc(struct ac97_codec_device *adev);
18void snd_ac97_compat_release(struct snd_ac97 *ac97);
19
20#endif
diff --git a/include/sound/ac97/controller.h b/include/sound/ac97/controller.h
new file mode 100644
index 000000000000..b36ecdd64f14
--- /dev/null
+++ b/include/sound/ac97/controller.h
@@ -0,0 +1,85 @@
1/*
2 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#ifndef AC97_CONTROLLER_H
9#define AC97_CONTROLLER_H
10
11#include <linux/device.h>
12#include <linux/list.h>
13
14#define AC97_BUS_MAX_CODECS 4
15#define AC97_SLOTS_AVAILABLE_ALL 0xf
16
17struct ac97_controller_ops;
18
19/**
20 * struct ac97_controller - The AC97 controller of the AC-Link
21 * @ops: the AC97 operations.
22 * @controllers: linked list of all existing controllers.
23 * @adap: the shell device ac97-%d, ie. ac97 adapter
24 * @nr: the number of the shell device
25 * @slots_available: the mask of accessible/scanable codecs.
26 * @parent: the device providing the AC97 controller.
27 * @codecs: the 4 possible AC97 codecs (NULL if none found).
28 * @codecs_pdata: platform_data for each codec (NULL if no pdata).
29 *
30 * This structure is internal to AC97 bus, and should not be used by the
31 * controllers themselves, excepting for using @dev.
32 */
33struct ac97_controller {
34 const struct ac97_controller_ops *ops;
35 struct list_head controllers;
36 struct device adap;
37 int nr;
38 unsigned short slots_available;
39 struct device *parent;
40 struct ac97_codec_device *codecs[AC97_BUS_MAX_CODECS];
41 void *codecs_pdata[AC97_BUS_MAX_CODECS];
42};
43
44/**
45 * struct ac97_controller_ops - The AC97 operations
46 * @reset: Cold reset of the AC97 AC-Link.
47 * @warm_reset: Warm reset of the AC97 AC-Link.
48 * @read: Read of a single AC97 register.
49 * Returns the register value or a negative error code.
50 * @write: Write of a single AC97 register.
51 *
52 * These are the basic operation an AC97 controller must provide for an AC97
53 * access functions. Amongst these, all but the last 2 are mandatory.
54 * The slot number is also known as the AC97 codec number, between 0 and 3.
55 */
56struct ac97_controller_ops {
57 void (*reset)(struct ac97_controller *adrv);
58 void (*warm_reset)(struct ac97_controller *adrv);
59 int (*write)(struct ac97_controller *adrv, int slot,
60 unsigned short reg, unsigned short val);
61 int (*read)(struct ac97_controller *adrv, int slot, unsigned short reg);
62};
63
64#if IS_ENABLED(CONFIG_AC97_BUS_NEW)
65struct ac97_controller *snd_ac97_controller_register(
66 const struct ac97_controller_ops *ops, struct device *dev,
67 unsigned short slots_available, void **codecs_pdata);
68void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl);
69#else
70static inline struct ac97_controller *
71snd_ac97_controller_register(const struct ac97_controller_ops *ops,
72 struct device *dev,
73 unsigned short slots_available,
74 void **codecs_pdata)
75{
76 return ERR_PTR(-ENODEV);
77}
78
79static inline void
80snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
81{
82}
83#endif
84
85#endif
diff --git a/sound/ac97/Kconfig b/sound/ac97/Kconfig
new file mode 100644
index 000000000000..f8a64e15e5bf
--- /dev/null
+++ b/sound/ac97/Kconfig
@@ -0,0 +1,19 @@
1#
2# AC97 configuration
3#
4
5
6config AC97_BUS_NEW
7 tristate
8 select AC97
9 help
10 This is the new AC97 bus type, successor of AC97_BUS. The ported
11 drivers which benefit from the AC97 automatic probing should "select"
12 this instead of the AC97_BUS.
13 Say Y here if you want to have AC97 devices, which are sound oriented
14 devices around an AC-Link.
15
16config AC97_BUS_COMPAT
17 bool
18 depends on AC97_BUS_NEW
19 depends on !AC97_BUS
diff --git a/sound/ac97/Makefile b/sound/ac97/Makefile
new file mode 100644
index 000000000000..f9c2640bfb59
--- /dev/null
+++ b/sound/ac97/Makefile
@@ -0,0 +1,8 @@
1#
2# make for AC97 bus drivers
3#
4
5obj-$(CONFIG_AC97_BUS_NEW) += ac97.o
6
7ac97-y += bus.o codec.o
8ac97-$(CONFIG_AC97_BUS_COMPAT) += snd_ac97_compat.o
diff --git a/sound/ac97/ac97_core.h b/sound/ac97/ac97_core.h
new file mode 100644
index 000000000000..08441a4fda7c
--- /dev/null
+++ b/sound/ac97/ac97_core.h
@@ -0,0 +1,16 @@
1/*
2 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9unsigned int snd_ac97_bus_scan_one(struct ac97_controller *ac97,
10 unsigned int codec_num);
11
12static inline bool ac97_ids_match(unsigned int id1, unsigned int id2,
13 unsigned int mask)
14{
15 return (id1 & mask) == (id2 & mask);
16}
diff --git a/sound/ac97/bus.c b/sound/ac97/bus.c
new file mode 100644
index 000000000000..31f858eceffc
--- /dev/null
+++ b/sound/ac97/bus.c
@@ -0,0 +1,539 @@
1/*
2 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/module.h>
10#include <linux/bitops.h>
11#include <linux/clk.h>
12#include <linux/device.h>
13#include <linux/idr.h>
14#include <linux/list.h>
15#include <linux/mutex.h>
16#include <linux/pm.h>
17#include <linux/pm_runtime.h>
18#include <linux/slab.h>
19#include <linux/sysfs.h>
20#include <sound/ac97/codec.h>
21#include <sound/ac97/controller.h>
22#include <sound/ac97/regs.h>
23
24#include "ac97_core.h"
25
26/*
27 * Protects ac97_controllers and each ac97_controller structure.
28 */
29static DEFINE_MUTEX(ac97_controllers_mutex);
30static DEFINE_IDR(ac97_adapter_idr);
31static LIST_HEAD(ac97_controllers);
32
33static struct bus_type ac97_bus_type;
34
35static inline struct ac97_controller*
36to_ac97_controller(struct device *ac97_adapter)
37{
38 return container_of(ac97_adapter, struct ac97_controller, adap);
39}
40
41static int ac97_unbound_ctrl_write(struct ac97_controller *adrv, int slot,
42 unsigned short reg, unsigned short val)
43{
44 return -ENODEV;
45}
46
47static int ac97_unbound_ctrl_read(struct ac97_controller *adrv, int slot,
48 unsigned short reg)
49{
50 return -ENODEV;
51}
52
53static const struct ac97_controller_ops ac97_unbound_ctrl_ops = {
54 .write = ac97_unbound_ctrl_write,
55 .read = ac97_unbound_ctrl_read,
56};
57
58static struct ac97_controller ac97_unbound_ctrl = {
59 .ops = &ac97_unbound_ctrl_ops,
60};
61
62static struct ac97_codec_device *
63ac97_codec_find(struct ac97_controller *ac97_ctrl, unsigned int codec_num)
64{
65 if (codec_num >= AC97_BUS_MAX_CODECS)
66 return ERR_PTR(-EINVAL);
67
68 return ac97_ctrl->codecs[codec_num];
69}
70
71static void ac97_codec_release(struct device *dev)
72{
73 struct ac97_codec_device *adev;
74 struct ac97_controller *ac97_ctrl;
75
76 adev = to_ac97_device(dev);
77 ac97_ctrl = adev->ac97_ctrl;
78 ac97_ctrl->codecs[adev->num] = NULL;
79 kfree(adev);
80}
81
82static int ac97_codec_add(struct ac97_controller *ac97_ctrl, int idx,
83 unsigned int vendor_id)
84{
85 struct ac97_codec_device *codec;
86 int ret;
87
88 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
89 if (!codec)
90 return -ENOMEM;
91 ac97_ctrl->codecs[idx] = codec;
92 codec->vendor_id = vendor_id;
93 codec->dev.release = ac97_codec_release;
94 codec->dev.bus = &ac97_bus_type;
95 codec->dev.parent = &ac97_ctrl->adap;
96 codec->num = idx;
97 codec->ac97_ctrl = ac97_ctrl;
98
99 device_initialize(&codec->dev);
100 dev_set_name(&codec->dev, "%s:%u", dev_name(ac97_ctrl->parent), idx);
101
102 ret = device_add(&codec->dev);
103 if (ret)
104 goto err_free_codec;
105
106 return 0;
107err_free_codec:
108 put_device(&codec->dev);
109 kfree(codec);
110 ac97_ctrl->codecs[idx] = NULL;
111
112 return ret;
113}
114
115unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv,
116 unsigned int codec_num)
117{
118 unsigned short vid1, vid2;
119 int ret;
120
121 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1);
122 vid1 = (ret & 0xffff);
123 if (ret < 0)
124 return 0;
125
126 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2);
127 vid2 = (ret & 0xffff);
128 if (ret < 0)
129 return 0;
130
131 dev_dbg(&adrv->adap, "%s(codec_num=%u): vendor_id=0x%08x\n",
132 __func__, codec_num, AC97_ID(vid1, vid2));
133 return AC97_ID(vid1, vid2);
134}
135
136static int ac97_bus_scan(struct ac97_controller *ac97_ctrl)
137{
138 int ret, i;
139 unsigned int vendor_id;
140
141 for (i = 0; i < AC97_BUS_MAX_CODECS; i++) {
142 if (ac97_codec_find(ac97_ctrl, i))
143 continue;
144 if (!(ac97_ctrl->slots_available & BIT(i)))
145 continue;
146 vendor_id = snd_ac97_bus_scan_one(ac97_ctrl, i);
147 if (!vendor_id)
148 continue;
149
150 ret = ac97_codec_add(ac97_ctrl, i, vendor_id);
151 if (ret < 0)
152 return ret;
153 }
154 return 0;
155}
156
157static int ac97_bus_reset(struct ac97_controller *ac97_ctrl)
158{
159 ac97_ctrl->ops->reset(ac97_ctrl);
160
161 return 0;
162}
163
164/**
165 * snd_ac97_codec_driver_register - register an AC97 codec driver
166 * @dev: AC97 driver codec to register
167 *
168 * Register an AC97 codec driver to the ac97 bus driver, aka. the AC97 digital
169 * controller.
170 *
171 * Returns 0 on success or error code
172 */
173int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv)
174{
175 drv->driver.bus = &ac97_bus_type;
176 return driver_register(&drv->driver);
177}
178EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_register);
179
180/**
181 * snd_ac97_codec_driver_unregister - unregister an AC97 codec driver
182 * @dev: AC97 codec driver to unregister
183 *
184 * Unregister a previously registered ac97 codec driver.
185 */
186void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv)
187{
188 driver_unregister(&drv->driver);
189}
190EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_unregister);
191
192/**
193 * snd_ac97_codec_get_platdata - get platform_data
194 * @adev: the ac97 codec device
195 *
196 * For legacy platforms, in order to have platform_data in codec drivers
197 * available, while ac97 device are auto-created upon probe, this retrieves the
198 * platdata which was setup on ac97 controller registration.
199 *
200 * Returns the platform data pointer
201 */
202void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev)
203{
204 struct ac97_controller *ac97_ctrl = adev->ac97_ctrl;
205
206 return ac97_ctrl->codecs_pdata[adev->num];
207}
208EXPORT_SYMBOL_GPL(snd_ac97_codec_get_platdata);
209
210static void ac97_ctrl_codecs_unregister(struct ac97_controller *ac97_ctrl)
211{
212 int i;
213
214 for (i = 0; i < AC97_BUS_MAX_CODECS; i++)
215 if (ac97_ctrl->codecs[i]) {
216 ac97_ctrl->codecs[i]->ac97_ctrl = &ac97_unbound_ctrl;
217 device_unregister(&ac97_ctrl->codecs[i]->dev);
218 }
219}
220
221static ssize_t cold_reset_store(struct device *dev,
222 struct device_attribute *attr, const char *buf,
223 size_t len)
224{
225 struct ac97_controller *ac97_ctrl;
226
227 mutex_lock(&ac97_controllers_mutex);
228 ac97_ctrl = to_ac97_controller(dev);
229 ac97_ctrl->ops->reset(ac97_ctrl);
230 mutex_unlock(&ac97_controllers_mutex);
231 return len;
232}
233static DEVICE_ATTR_WO(cold_reset);
234
235static ssize_t warm_reset_store(struct device *dev,
236 struct device_attribute *attr, const char *buf,
237 size_t len)
238{
239 struct ac97_controller *ac97_ctrl;
240
241 if (!dev)
242 return -ENODEV;
243
244 mutex_lock(&ac97_controllers_mutex);
245 ac97_ctrl = to_ac97_controller(dev);
246 ac97_ctrl->ops->warm_reset(ac97_ctrl);
247 mutex_unlock(&ac97_controllers_mutex);
248 return len;
249}
250static DEVICE_ATTR_WO(warm_reset);
251
252static struct attribute *ac97_controller_device_attrs[] = {
253 &dev_attr_cold_reset.attr,
254 &dev_attr_warm_reset.attr,
255 NULL
256};
257
258static struct attribute_group ac97_adapter_attr_group = {
259 .name = "ac97_operations",
260 .attrs = ac97_controller_device_attrs,
261};
262
263static const struct attribute_group *ac97_adapter_groups[] = {
264 &ac97_adapter_attr_group,
265 NULL,
266};
267
268static void ac97_del_adapter(struct ac97_controller *ac97_ctrl)
269{
270 mutex_lock(&ac97_controllers_mutex);
271 ac97_ctrl_codecs_unregister(ac97_ctrl);
272 list_del(&ac97_ctrl->controllers);
273 mutex_unlock(&ac97_controllers_mutex);
274
275 device_unregister(&ac97_ctrl->adap);
276}
277
278static void ac97_adapter_release(struct device *dev)
279{
280 struct ac97_controller *ac97_ctrl;
281
282 ac97_ctrl = to_ac97_controller(dev);
283 idr_remove(&ac97_adapter_idr, ac97_ctrl->nr);
284 dev_dbg(&ac97_ctrl->adap, "adapter unregistered by %s\n",
285 dev_name(ac97_ctrl->parent));
286}
287
288static const struct device_type ac97_adapter_type = {
289 .groups = ac97_adapter_groups,
290 .release = ac97_adapter_release,
291};
292
293static int ac97_add_adapter(struct ac97_controller *ac97_ctrl)
294{
295 int ret;
296
297 mutex_lock(&ac97_controllers_mutex);
298 ret = idr_alloc(&ac97_adapter_idr, ac97_ctrl, 0, 0, GFP_KERNEL);
299 ac97_ctrl->nr = ret;
300 if (ret >= 0) {
301 dev_set_name(&ac97_ctrl->adap, "ac97-%d", ret);
302 ac97_ctrl->adap.type = &ac97_adapter_type;
303 ac97_ctrl->adap.parent = ac97_ctrl->parent;
304 ret = device_register(&ac97_ctrl->adap);
305 if (ret)
306 put_device(&ac97_ctrl->adap);
307 }
308 if (!ret)
309 list_add(&ac97_ctrl->controllers, &ac97_controllers);
310 mutex_unlock(&ac97_controllers_mutex);
311
312 if (!ret)
313 dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
314 dev_name(ac97_ctrl->parent));
315 return ret;
316}
317
318/**
319 * snd_ac97_controller_register - register an ac97 controller
320 * @ops: the ac97 bus operations
321 * @dev: the device providing the ac97 DC function
322 * @slots_available: mask of the ac97 codecs that can be scanned and probed
323 * bit0 => codec 0, bit1 => codec 1 ... bit 3 => codec 3
324 *
325 * Register a digital controller which can control up to 4 ac97 codecs. This is
326 * the controller side of the AC97 AC-link, while the slave side are the codecs.
327 *
328 * Returns a valid controller upon success, negative pointer value upon error
329 */
330struct ac97_controller *snd_ac97_controller_register(
331 const struct ac97_controller_ops *ops, struct device *dev,
332 unsigned short slots_available, void **codecs_pdata)
333{
334 struct ac97_controller *ac97_ctrl;
335 int ret, i;
336
337 ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL);
338 if (!ac97_ctrl)
339 return ERR_PTR(-ENOMEM);
340
341 for (i = 0; i < AC97_BUS_MAX_CODECS && codecs_pdata; i++)
342 ac97_ctrl->codecs_pdata[i] = codecs_pdata[i];
343
344 ac97_ctrl->ops = ops;
345 ac97_ctrl->slots_available = slots_available;
346 ac97_ctrl->parent = dev;
347 ret = ac97_add_adapter(ac97_ctrl);
348
349 if (ret)
350 goto err;
351 ac97_bus_reset(ac97_ctrl);
352 ac97_bus_scan(ac97_ctrl);
353
354 return ac97_ctrl;
355err:
356 kfree(ac97_ctrl);
357 return ERR_PTR(ret);
358}
359EXPORT_SYMBOL_GPL(snd_ac97_controller_register);
360
361/**
362 * snd_ac97_controller_unregister - unregister an ac97 controller
363 * @ac97_ctrl: the device previously provided to ac97_controller_register()
364 *
365 */
366void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
367{
368 ac97_del_adapter(ac97_ctrl);
369}
370EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister);
371
372#ifdef CONFIG_PM
373static int ac97_pm_runtime_suspend(struct device *dev)
374{
375 struct ac97_codec_device *codec = to_ac97_device(dev);
376 int ret = pm_generic_runtime_suspend(dev);
377
378 if (ret == 0 && dev->driver) {
379 if (pm_runtime_is_irq_safe(dev))
380 clk_disable(codec->clk);
381 else
382 clk_disable_unprepare(codec->clk);
383 }
384
385 return ret;
386}
387
388static int ac97_pm_runtime_resume(struct device *dev)
389{
390 struct ac97_codec_device *codec = to_ac97_device(dev);
391 int ret;
392
393 if (dev->driver) {
394 if (pm_runtime_is_irq_safe(dev))
395 ret = clk_enable(codec->clk);
396 else
397 ret = clk_prepare_enable(codec->clk);
398 if (ret)
399 return ret;
400 }
401
402 return pm_generic_runtime_resume(dev);
403}
404#endif /* CONFIG_PM */
405
406static const struct dev_pm_ops ac97_pm = {
407 .suspend = pm_generic_suspend,
408 .resume = pm_generic_resume,
409 .freeze = pm_generic_freeze,
410 .thaw = pm_generic_thaw,
411 .poweroff = pm_generic_poweroff,
412 .restore = pm_generic_restore,
413 SET_RUNTIME_PM_OPS(
414 ac97_pm_runtime_suspend,
415 ac97_pm_runtime_resume,
416 NULL)
417};
418
419static int ac97_get_enable_clk(struct ac97_codec_device *adev)
420{
421 int ret;
422
423 adev->clk = clk_get(&adev->dev, "ac97_clk");
424 if (IS_ERR(adev->clk))
425 return PTR_ERR(adev->clk);
426
427 ret = clk_prepare_enable(adev->clk);
428 if (ret)
429 clk_put(adev->clk);
430
431 return ret;
432}
433
434static void ac97_put_disable_clk(struct ac97_codec_device *adev)
435{
436 clk_disable_unprepare(adev->clk);
437 clk_put(adev->clk);
438}
439
440static ssize_t vendor_id_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
443 struct ac97_codec_device *codec = to_ac97_device(dev);
444
445 return sprintf(buf, "%08x", codec->vendor_id);
446}
447DEVICE_ATTR_RO(vendor_id);
448
449static struct attribute *ac97_dev_attrs[] = {
450 &dev_attr_vendor_id.attr,
451 NULL,
452};
453ATTRIBUTE_GROUPS(ac97_dev);
454
455static int ac97_bus_match(struct device *dev, struct device_driver *drv)
456{
457 struct ac97_codec_device *adev = to_ac97_device(dev);
458 struct ac97_codec_driver *adrv = to_ac97_driver(drv);
459 const struct ac97_id *id = adrv->id_table;
460 int i = 0;
461
462 if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
463 return false;
464
465 do {
466 if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
467 return true;
468 } while (id[i++].id);
469
470 return false;
471}
472
473static int ac97_bus_probe(struct device *dev)
474{
475 struct ac97_codec_device *adev = to_ac97_device(dev);
476 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
477 int ret;
478
479 ret = ac97_get_enable_clk(adev);
480 if (ret)
481 return ret;
482
483 pm_runtime_get_noresume(dev);
484 pm_runtime_set_active(dev);
485 pm_runtime_enable(dev);
486
487 ret = adrv->probe(adev);
488 if (ret == 0)
489 return 0;
490
491 pm_runtime_disable(dev);
492 pm_runtime_set_suspended(dev);
493 pm_runtime_put_noidle(dev);
494 ac97_put_disable_clk(adev);
495
496 return ret;
497}
498
499static int ac97_bus_remove(struct device *dev)
500{
501 struct ac97_codec_device *adev = to_ac97_device(dev);
502 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
503 int ret;
504
505 ret = pm_runtime_get_sync(dev);
506 if (ret)
507 return ret;
508
509 ret = adrv->remove(adev);
510 pm_runtime_put_noidle(dev);
511 if (ret == 0)
512 ac97_put_disable_clk(adev);
513
514 return ret;
515}
516
517static struct bus_type ac97_bus_type = {
518 .name = "ac97bus",
519 .dev_groups = ac97_dev_groups,
520 .match = ac97_bus_match,
521 .pm = &ac97_pm,
522 .probe = ac97_bus_probe,
523 .remove = ac97_bus_remove,
524};
525
526static int __init ac97_bus_init(void)
527{
528 return bus_register(&ac97_bus_type);
529}
530subsys_initcall(ac97_bus_init);
531
532static void __exit ac97_bus_exit(void)
533{
534 bus_unregister(&ac97_bus_type);
535}
536module_exit(ac97_bus_exit);
537
538MODULE_LICENSE("GPL");
539MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
diff --git a/sound/ac97/codec.c b/sound/ac97/codec.c
new file mode 100644
index 000000000000..a835f03744bf
--- /dev/null
+++ b/sound/ac97/codec.c
@@ -0,0 +1,15 @@
1/*
2 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <sound/ac97_codec.h>
10#include <sound/ac97/codec.h>
11#include <sound/ac97/controller.h>
12#include <linux/device.h>
13#include <linux/slab.h>
14#include <sound/soc.h> /* For compat_ac97_* */
15
diff --git a/sound/ac97/snd_ac97_compat.c b/sound/ac97/snd_ac97_compat.c
new file mode 100644
index 000000000000..61544e0d8de4
--- /dev/null
+++ b/sound/ac97/snd_ac97_compat.c
@@ -0,0 +1,108 @@
1/*
2 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/list.h>
10#include <linux/slab.h>
11#include <sound/ac97/codec.h>
12#include <sound/ac97/compat.h>
13#include <sound/ac97/controller.h>
14#include <sound/soc.h>
15
16#include "ac97_core.h"
17
18static void compat_ac97_reset(struct snd_ac97 *ac97)
19{
20 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
21 struct ac97_controller *actrl = adev->ac97_ctrl;
22
23 if (actrl->ops->reset)
24 actrl->ops->reset(actrl);
25}
26
27static void compat_ac97_warm_reset(struct snd_ac97 *ac97)
28{
29 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
30 struct ac97_controller *actrl = adev->ac97_ctrl;
31
32 if (actrl->ops->warm_reset)
33 actrl->ops->warm_reset(actrl);
34}
35
36static void compat_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
37 unsigned short val)
38{
39 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
40 struct ac97_controller *actrl = adev->ac97_ctrl;
41
42 actrl->ops->write(actrl, ac97->num, reg, val);
43}
44
45static unsigned short compat_ac97_read(struct snd_ac97 *ac97,
46 unsigned short reg)
47{
48 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
49 struct ac97_controller *actrl = adev->ac97_ctrl;
50
51 return actrl->ops->read(actrl, ac97->num, reg);
52}
53
54static struct snd_ac97_bus_ops compat_snd_ac97_bus_ops = {
55 .reset = compat_ac97_reset,
56 .warm_reset = compat_ac97_warm_reset,
57 .write = compat_ac97_write,
58 .read = compat_ac97_read,
59};
60
61static struct snd_ac97_bus compat_soc_ac97_bus = {
62 .ops = &compat_snd_ac97_bus_ops,
63};
64
65struct snd_ac97 *snd_ac97_compat_alloc(struct ac97_codec_device *adev)
66{
67 struct snd_ac97 *ac97;
68
69 ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
70 if (ac97 == NULL)
71 return ERR_PTR(-ENOMEM);
72
73 ac97->dev = adev->dev;
74 ac97->private_data = adev;
75 ac97->bus = &compat_soc_ac97_bus;
76 return ac97;
77}
78EXPORT_SYMBOL_GPL(snd_ac97_compat_alloc);
79
80void snd_ac97_compat_release(struct snd_ac97 *ac97)
81{
82 kfree(ac97);
83}
84EXPORT_SYMBOL_GPL(snd_ac97_compat_release);
85
86int snd_ac97_reset(struct snd_ac97 *ac97, bool try_warm, unsigned int id,
87 unsigned int id_mask)
88{
89 struct ac97_codec_device *adev = to_ac97_device(ac97->private_data);
90 struct ac97_controller *actrl = adev->ac97_ctrl;
91 unsigned int scanned;
92
93 if (try_warm) {
94 compat_ac97_warm_reset(ac97);
95 scanned = snd_ac97_bus_scan_one(actrl, adev->num);
96 if (ac97_ids_match(scanned, adev->vendor_id, id_mask))
97 return 1;
98 }
99
100 compat_ac97_reset(ac97);
101 compat_ac97_warm_reset(ac97);
102 scanned = snd_ac97_bus_scan_one(actrl, adev->num);
103 if (ac97_ids_match(scanned, adev->vendor_id, id_mask))
104 return 0;
105
106 return -ENODEV;
107}
108EXPORT_SYMBOL_GPL(snd_ac97_reset);