ALSA: mark all struct bus_type as const
[linux-2.6-block.git] / sound / ac97 / bus.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
74426fbf
RJ
2/*
3 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
74426fbf
RJ
4 */
5
6#include <linux/module.h>
7#include <linux/bitops.h>
8#include <linux/clk.h>
9#include <linux/device.h>
10#include <linux/idr.h>
11#include <linux/list.h>
12#include <linux/mutex.h>
2225a3e6 13#include <linux/of.h>
74426fbf
RJ
14#include <linux/pm.h>
15#include <linux/pm_runtime.h>
16#include <linux/slab.h>
17#include <linux/sysfs.h>
18#include <sound/ac97/codec.h>
19#include <sound/ac97/controller.h>
20#include <sound/ac97/regs.h>
21
22#include "ac97_core.h"
23
24/*
25 * Protects ac97_controllers and each ac97_controller structure.
26 */
27static DEFINE_MUTEX(ac97_controllers_mutex);
28static DEFINE_IDR(ac97_adapter_idr);
29static LIST_HEAD(ac97_controllers);
30
74426fbf
RJ
31static inline struct ac97_controller*
32to_ac97_controller(struct device *ac97_adapter)
33{
34 return container_of(ac97_adapter, struct ac97_controller, adap);
35}
36
37static int ac97_unbound_ctrl_write(struct ac97_controller *adrv, int slot,
38 unsigned short reg, unsigned short val)
39{
40 return -ENODEV;
41}
42
43static int ac97_unbound_ctrl_read(struct ac97_controller *adrv, int slot,
44 unsigned short reg)
45{
46 return -ENODEV;
47}
48
49static const struct ac97_controller_ops ac97_unbound_ctrl_ops = {
50 .write = ac97_unbound_ctrl_write,
51 .read = ac97_unbound_ctrl_read,
52};
53
54static struct ac97_controller ac97_unbound_ctrl = {
55 .ops = &ac97_unbound_ctrl_ops,
56};
57
58static struct ac97_codec_device *
59ac97_codec_find(struct ac97_controller *ac97_ctrl, unsigned int codec_num)
60{
61 if (codec_num >= AC97_BUS_MAX_CODECS)
62 return ERR_PTR(-EINVAL);
63
64 return ac97_ctrl->codecs[codec_num];
65}
66
2225a3e6
RJ
67static struct device_node *
68ac97_of_get_child_device(struct ac97_controller *ac97_ctrl, int idx,
69 unsigned int vendor_id)
70{
71 struct device_node *node;
72 u32 reg;
73 char compat[] = "ac97,0000,0000";
74
75 snprintf(compat, sizeof(compat), "ac97,%04x,%04x",
76 vendor_id >> 16, vendor_id & 0xffff);
77
78 for_each_child_of_node(ac97_ctrl->parent->of_node, node) {
79 if ((idx != of_property_read_u32(node, "reg", &reg)) ||
80 !of_device_is_compatible(node, compat))
81 continue;
31d2350d 82 return node;
2225a3e6
RJ
83 }
84
85 return NULL;
86}
87
74426fbf
RJ
88static void ac97_codec_release(struct device *dev)
89{
90 struct ac97_codec_device *adev;
91 struct ac97_controller *ac97_ctrl;
92
93 adev = to_ac97_device(dev);
94 ac97_ctrl = adev->ac97_ctrl;
95 ac97_ctrl->codecs[adev->num] = NULL;
2225a3e6 96 of_node_put(dev->of_node);
74426fbf
RJ
97 kfree(adev);
98}
99
100static int ac97_codec_add(struct ac97_controller *ac97_ctrl, int idx,
101 unsigned int vendor_id)
102{
103 struct ac97_codec_device *codec;
104 int ret;
105
106 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
107 if (!codec)
108 return -ENOMEM;
109 ac97_ctrl->codecs[idx] = codec;
110 codec->vendor_id = vendor_id;
111 codec->dev.release = ac97_codec_release;
112 codec->dev.bus = &ac97_bus_type;
113 codec->dev.parent = &ac97_ctrl->adap;
114 codec->num = idx;
115 codec->ac97_ctrl = ac97_ctrl;
116
117 device_initialize(&codec->dev);
118 dev_set_name(&codec->dev, "%s:%u", dev_name(ac97_ctrl->parent), idx);
2225a3e6
RJ
119 codec->dev.of_node = ac97_of_get_child_device(ac97_ctrl, idx,
120 vendor_id);
74426fbf
RJ
121
122 ret = device_add(&codec->dev);
607975b3
DX
123 if (ret) {
124 put_device(&codec->dev);
125 return ret;
126 }
74426fbf
RJ
127
128 return 0;
74426fbf
RJ
129}
130
131unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv,
132 unsigned int codec_num)
133{
134 unsigned short vid1, vid2;
135 int ret;
136
137 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1);
138 vid1 = (ret & 0xffff);
139 if (ret < 0)
140 return 0;
141
142 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2);
143 vid2 = (ret & 0xffff);
144 if (ret < 0)
145 return 0;
146
147 dev_dbg(&adrv->adap, "%s(codec_num=%u): vendor_id=0x%08x\n",
148 __func__, codec_num, AC97_ID(vid1, vid2));
149 return AC97_ID(vid1, vid2);
150}
151
152static int ac97_bus_scan(struct ac97_controller *ac97_ctrl)
153{
154 int ret, i;
155 unsigned int vendor_id;
156
157 for (i = 0; i < AC97_BUS_MAX_CODECS; i++) {
158 if (ac97_codec_find(ac97_ctrl, i))
159 continue;
160 if (!(ac97_ctrl->slots_available & BIT(i)))
161 continue;
162 vendor_id = snd_ac97_bus_scan_one(ac97_ctrl, i);
163 if (!vendor_id)
164 continue;
165
166 ret = ac97_codec_add(ac97_ctrl, i, vendor_id);
167 if (ret < 0)
168 return ret;
169 }
170 return 0;
171}
172
173static int ac97_bus_reset(struct ac97_controller *ac97_ctrl)
174{
175 ac97_ctrl->ops->reset(ac97_ctrl);
176
177 return 0;
178}
179
180/**
181 * snd_ac97_codec_driver_register - register an AC97 codec driver
182 * @dev: AC97 driver codec to register
183 *
184 * Register an AC97 codec driver to the ac97 bus driver, aka. the AC97 digital
185 * controller.
186 *
187 * Returns 0 on success or error code
188 */
189int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv)
190{
191 drv->driver.bus = &ac97_bus_type;
192 return driver_register(&drv->driver);
193}
194EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_register);
195
196/**
197 * snd_ac97_codec_driver_unregister - unregister an AC97 codec driver
198 * @dev: AC97 codec driver to unregister
199 *
200 * Unregister a previously registered ac97 codec driver.
201 */
202void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv)
203{
204 driver_unregister(&drv->driver);
205}
206EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_unregister);
207
208/**
209 * snd_ac97_codec_get_platdata - get platform_data
210 * @adev: the ac97 codec device
211 *
212 * For legacy platforms, in order to have platform_data in codec drivers
213 * available, while ac97 device are auto-created upon probe, this retrieves the
214 * platdata which was setup on ac97 controller registration.
215 *
216 * Returns the platform data pointer
217 */
218void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev)
219{
220 struct ac97_controller *ac97_ctrl = adev->ac97_ctrl;
221
222 return ac97_ctrl->codecs_pdata[adev->num];
223}
224EXPORT_SYMBOL_GPL(snd_ac97_codec_get_platdata);
225
226static void ac97_ctrl_codecs_unregister(struct ac97_controller *ac97_ctrl)
227{
228 int i;
229
230 for (i = 0; i < AC97_BUS_MAX_CODECS; i++)
231 if (ac97_ctrl->codecs[i]) {
232 ac97_ctrl->codecs[i]->ac97_ctrl = &ac97_unbound_ctrl;
233 device_unregister(&ac97_ctrl->codecs[i]->dev);
234 }
235}
236
237static ssize_t cold_reset_store(struct device *dev,
238 struct device_attribute *attr, const char *buf,
239 size_t len)
240{
241 struct ac97_controller *ac97_ctrl;
242
243 mutex_lock(&ac97_controllers_mutex);
244 ac97_ctrl = to_ac97_controller(dev);
245 ac97_ctrl->ops->reset(ac97_ctrl);
246 mutex_unlock(&ac97_controllers_mutex);
247 return len;
248}
249static DEVICE_ATTR_WO(cold_reset);
250
251static ssize_t warm_reset_store(struct device *dev,
252 struct device_attribute *attr, const char *buf,
253 size_t len)
254{
255 struct ac97_controller *ac97_ctrl;
256
257 if (!dev)
258 return -ENODEV;
259
260 mutex_lock(&ac97_controllers_mutex);
261 ac97_ctrl = to_ac97_controller(dev);
262 ac97_ctrl->ops->warm_reset(ac97_ctrl);
263 mutex_unlock(&ac97_controllers_mutex);
264 return len;
265}
266static DEVICE_ATTR_WO(warm_reset);
267
268static struct attribute *ac97_controller_device_attrs[] = {
269 &dev_attr_cold_reset.attr,
270 &dev_attr_warm_reset.attr,
271 NULL
272};
273
fa2e5a64 274static const struct attribute_group ac97_adapter_attr_group = {
74426fbf
RJ
275 .name = "ac97_operations",
276 .attrs = ac97_controller_device_attrs,
277};
278
279static const struct attribute_group *ac97_adapter_groups[] = {
280 &ac97_adapter_attr_group,
281 NULL,
282};
283
284static void ac97_del_adapter(struct ac97_controller *ac97_ctrl)
285{
286 mutex_lock(&ac97_controllers_mutex);
287 ac97_ctrl_codecs_unregister(ac97_ctrl);
288 list_del(&ac97_ctrl->controllers);
289 mutex_unlock(&ac97_controllers_mutex);
290
291 device_unregister(&ac97_ctrl->adap);
292}
293
294static void ac97_adapter_release(struct device *dev)
295{
296 struct ac97_controller *ac97_ctrl;
297
298 ac97_ctrl = to_ac97_controller(dev);
299 idr_remove(&ac97_adapter_idr, ac97_ctrl->nr);
300 dev_dbg(&ac97_ctrl->adap, "adapter unregistered by %s\n",
301 dev_name(ac97_ctrl->parent));
302}
303
304static const struct device_type ac97_adapter_type = {
305 .groups = ac97_adapter_groups,
306 .release = ac97_adapter_release,
307};
308
309static int ac97_add_adapter(struct ac97_controller *ac97_ctrl)
310{
311 int ret;
312
313 mutex_lock(&ac97_controllers_mutex);
314 ret = idr_alloc(&ac97_adapter_idr, ac97_ctrl, 0, 0, GFP_KERNEL);
315 ac97_ctrl->nr = ret;
316 if (ret >= 0) {
317 dev_set_name(&ac97_ctrl->adap, "ac97-%d", ret);
318 ac97_ctrl->adap.type = &ac97_adapter_type;
319 ac97_ctrl->adap.parent = ac97_ctrl->parent;
320 ret = device_register(&ac97_ctrl->adap);
321 if (ret)
322 put_device(&ac97_ctrl->adap);
323 }
324 if (!ret)
325 list_add(&ac97_ctrl->controllers, &ac97_controllers);
326 mutex_unlock(&ac97_controllers_mutex);
327
328 if (!ret)
329 dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
330 dev_name(ac97_ctrl->parent));
331 return ret;
332}
333
334/**
335 * snd_ac97_controller_register - register an ac97 controller
336 * @ops: the ac97 bus operations
337 * @dev: the device providing the ac97 DC function
338 * @slots_available: mask of the ac97 codecs that can be scanned and probed
339 * bit0 => codec 0, bit1 => codec 1 ... bit 3 => codec 3
340 *
341 * Register a digital controller which can control up to 4 ac97 codecs. This is
342 * the controller side of the AC97 AC-link, while the slave side are the codecs.
343 *
344 * Returns a valid controller upon success, negative pointer value upon error
345 */
346struct ac97_controller *snd_ac97_controller_register(
347 const struct ac97_controller_ops *ops, struct device *dev,
348 unsigned short slots_available, void **codecs_pdata)
349{
350 struct ac97_controller *ac97_ctrl;
351 int ret, i;
352
353 ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL);
354 if (!ac97_ctrl)
355 return ERR_PTR(-ENOMEM);
356
357 for (i = 0; i < AC97_BUS_MAX_CODECS && codecs_pdata; i++)
358 ac97_ctrl->codecs_pdata[i] = codecs_pdata[i];
359
360 ac97_ctrl->ops = ops;
361 ac97_ctrl->slots_available = slots_available;
362 ac97_ctrl->parent = dev;
363 ret = ac97_add_adapter(ac97_ctrl);
364
365 if (ret)
366 goto err;
367 ac97_bus_reset(ac97_ctrl);
368 ac97_bus_scan(ac97_ctrl);
369
370 return ac97_ctrl;
371err:
372 kfree(ac97_ctrl);
373 return ERR_PTR(ret);
374}
375EXPORT_SYMBOL_GPL(snd_ac97_controller_register);
376
377/**
378 * snd_ac97_controller_unregister - unregister an ac97 controller
379 * @ac97_ctrl: the device previously provided to ac97_controller_register()
380 *
381 */
382void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
383{
384 ac97_del_adapter(ac97_ctrl);
385}
386EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister);
387
388#ifdef CONFIG_PM
389static int ac97_pm_runtime_suspend(struct device *dev)
390{
391 struct ac97_codec_device *codec = to_ac97_device(dev);
392 int ret = pm_generic_runtime_suspend(dev);
393
394 if (ret == 0 && dev->driver) {
395 if (pm_runtime_is_irq_safe(dev))
396 clk_disable(codec->clk);
397 else
398 clk_disable_unprepare(codec->clk);
399 }
400
401 return ret;
402}
403
404static int ac97_pm_runtime_resume(struct device *dev)
405{
406 struct ac97_codec_device *codec = to_ac97_device(dev);
407 int ret;
408
409 if (dev->driver) {
410 if (pm_runtime_is_irq_safe(dev))
411 ret = clk_enable(codec->clk);
412 else
413 ret = clk_prepare_enable(codec->clk);
414 if (ret)
415 return ret;
416 }
417
418 return pm_generic_runtime_resume(dev);
419}
420#endif /* CONFIG_PM */
421
422static const struct dev_pm_ops ac97_pm = {
423 .suspend = pm_generic_suspend,
424 .resume = pm_generic_resume,
425 .freeze = pm_generic_freeze,
426 .thaw = pm_generic_thaw,
427 .poweroff = pm_generic_poweroff,
428 .restore = pm_generic_restore,
429 SET_RUNTIME_PM_OPS(
430 ac97_pm_runtime_suspend,
431 ac97_pm_runtime_resume,
432 NULL)
433};
434
435static int ac97_get_enable_clk(struct ac97_codec_device *adev)
436{
437 int ret;
438
439 adev->clk = clk_get(&adev->dev, "ac97_clk");
440 if (IS_ERR(adev->clk))
441 return PTR_ERR(adev->clk);
442
443 ret = clk_prepare_enable(adev->clk);
444 if (ret)
445 clk_put(adev->clk);
446
447 return ret;
448}
449
450static void ac97_put_disable_clk(struct ac97_codec_device *adev)
451{
452 clk_disable_unprepare(adev->clk);
453 clk_put(adev->clk);
454}
455
456static ssize_t vendor_id_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
458{
459 struct ac97_codec_device *codec = to_ac97_device(dev);
460
7450320e 461 return sysfs_emit(buf, "%08x", codec->vendor_id);
74426fbf 462}
d28dc3d8 463static DEVICE_ATTR_RO(vendor_id);
74426fbf
RJ
464
465static struct attribute *ac97_dev_attrs[] = {
466 &dev_attr_vendor_id.attr,
467 NULL,
468};
469ATTRIBUTE_GROUPS(ac97_dev);
470
471static int ac97_bus_match(struct device *dev, struct device_driver *drv)
472{
473 struct ac97_codec_device *adev = to_ac97_device(dev);
474 struct ac97_codec_driver *adrv = to_ac97_driver(drv);
475 const struct ac97_id *id = adrv->id_table;
476 int i = 0;
477
478 if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
479 return false;
480
481 do {
482 if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
483 return true;
484 } while (id[i++].id);
485
486 return false;
487}
488
489static int ac97_bus_probe(struct device *dev)
490{
491 struct ac97_codec_device *adev = to_ac97_device(dev);
492 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
493 int ret;
494
495 ret = ac97_get_enable_clk(adev);
496 if (ret)
497 return ret;
498
499 pm_runtime_get_noresume(dev);
500 pm_runtime_set_active(dev);
501 pm_runtime_enable(dev);
502
503 ret = adrv->probe(adev);
504 if (ret == 0)
505 return 0;
506
507 pm_runtime_disable(dev);
508 pm_runtime_set_suspended(dev);
509 pm_runtime_put_noidle(dev);
510 ac97_put_disable_clk(adev);
511
512 return ret;
513}
514
fc7a6209 515static void ac97_bus_remove(struct device *dev)
74426fbf
RJ
516{
517 struct ac97_codec_device *adev = to_ac97_device(dev);
518 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
519 int ret;
520
a38e9330 521 ret = pm_runtime_resume_and_get(dev);
d15ec0b4 522 if (ret < 0)
fc7a6209 523 return;
74426fbf 524
7351324c 525 adrv->remove(adev);
74426fbf 526 pm_runtime_put_noidle(dev);
7351324c 527 ac97_put_disable_clk(adev);
74426fbf 528
250ea7c5 529 pm_runtime_disable(dev);
74426fbf
RJ
530}
531
532static struct bus_type ac97_bus_type = {
533 .name = "ac97bus",
534 .dev_groups = ac97_dev_groups,
535 .match = ac97_bus_match,
536 .pm = &ac97_pm,
537 .probe = ac97_bus_probe,
538 .remove = ac97_bus_remove,
539};
540
541static int __init ac97_bus_init(void)
542{
543 return bus_register(&ac97_bus_type);
544}
545subsys_initcall(ac97_bus_init);
546
547static void __exit ac97_bus_exit(void)
548{
549 bus_unregister(&ac97_bus_type);
550}
551module_exit(ac97_bus_exit);
552
553MODULE_LICENSE("GPL");
554MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");