2 * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
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.
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>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/sysfs.h>
21 #include <sound/ac97/codec.h>
22 #include <sound/ac97/controller.h>
23 #include <sound/ac97/regs.h>
25 #include "ac97_core.h"
28 * Protects ac97_controllers and each ac97_controller structure.
30 static DEFINE_MUTEX(ac97_controllers_mutex);
31 static DEFINE_IDR(ac97_adapter_idr);
32 static LIST_HEAD(ac97_controllers);
34 static struct bus_type ac97_bus_type;
36 static inline struct ac97_controller*
37 to_ac97_controller(struct device *ac97_adapter)
39 return container_of(ac97_adapter, struct ac97_controller, adap);
42 static int ac97_unbound_ctrl_write(struct ac97_controller *adrv, int slot,
43 unsigned short reg, unsigned short val)
48 static int ac97_unbound_ctrl_read(struct ac97_controller *adrv, int slot,
54 static const struct ac97_controller_ops ac97_unbound_ctrl_ops = {
55 .write = ac97_unbound_ctrl_write,
56 .read = ac97_unbound_ctrl_read,
59 static struct ac97_controller ac97_unbound_ctrl = {
60 .ops = &ac97_unbound_ctrl_ops,
63 static struct ac97_codec_device *
64 ac97_codec_find(struct ac97_controller *ac97_ctrl, unsigned int codec_num)
66 if (codec_num >= AC97_BUS_MAX_CODECS)
67 return ERR_PTR(-EINVAL);
69 return ac97_ctrl->codecs[codec_num];
72 static struct device_node *
73 ac97_of_get_child_device(struct ac97_controller *ac97_ctrl, int idx,
74 unsigned int vendor_id)
76 struct device_node *node;
78 char compat[] = "ac97,0000,0000";
80 snprintf(compat, sizeof(compat), "ac97,%04x,%04x",
81 vendor_id >> 16, vendor_id & 0xffff);
83 for_each_child_of_node(ac97_ctrl->parent->of_node, node) {
84 if ((idx != of_property_read_u32(node, "reg", ®)) ||
85 !of_device_is_compatible(node, compat))
93 static void ac97_codec_release(struct device *dev)
95 struct ac97_codec_device *adev;
96 struct ac97_controller *ac97_ctrl;
98 adev = to_ac97_device(dev);
99 ac97_ctrl = adev->ac97_ctrl;
100 ac97_ctrl->codecs[adev->num] = NULL;
101 of_node_put(dev->of_node);
105 static int ac97_codec_add(struct ac97_controller *ac97_ctrl, int idx,
106 unsigned int vendor_id)
108 struct ac97_codec_device *codec;
111 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
114 ac97_ctrl->codecs[idx] = codec;
115 codec->vendor_id = vendor_id;
116 codec->dev.release = ac97_codec_release;
117 codec->dev.bus = &ac97_bus_type;
118 codec->dev.parent = &ac97_ctrl->adap;
120 codec->ac97_ctrl = ac97_ctrl;
122 device_initialize(&codec->dev);
123 dev_set_name(&codec->dev, "%s:%u", dev_name(ac97_ctrl->parent), idx);
124 codec->dev.of_node = ac97_of_get_child_device(ac97_ctrl, idx,
127 ret = device_add(&codec->dev);
129 put_device(&codec->dev);
136 unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv,
137 unsigned int codec_num)
139 unsigned short vid1, vid2;
142 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1);
143 vid1 = (ret & 0xffff);
147 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2);
148 vid2 = (ret & 0xffff);
152 dev_dbg(&adrv->adap, "%s(codec_num=%u): vendor_id=0x%08x\n",
153 __func__, codec_num, AC97_ID(vid1, vid2));
154 return AC97_ID(vid1, vid2);
157 static int ac97_bus_scan(struct ac97_controller *ac97_ctrl)
160 unsigned int vendor_id;
162 for (i = 0; i < AC97_BUS_MAX_CODECS; i++) {
163 if (ac97_codec_find(ac97_ctrl, i))
165 if (!(ac97_ctrl->slots_available & BIT(i)))
167 vendor_id = snd_ac97_bus_scan_one(ac97_ctrl, i);
171 ret = ac97_codec_add(ac97_ctrl, i, vendor_id);
178 static int ac97_bus_reset(struct ac97_controller *ac97_ctrl)
180 ac97_ctrl->ops->reset(ac97_ctrl);
186 * snd_ac97_codec_driver_register - register an AC97 codec driver
187 * @dev: AC97 driver codec to register
189 * Register an AC97 codec driver to the ac97 bus driver, aka. the AC97 digital
192 * Returns 0 on success or error code
194 int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv)
196 drv->driver.bus = &ac97_bus_type;
197 return driver_register(&drv->driver);
199 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_register);
202 * snd_ac97_codec_driver_unregister - unregister an AC97 codec driver
203 * @dev: AC97 codec driver to unregister
205 * Unregister a previously registered ac97 codec driver.
207 void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv)
209 driver_unregister(&drv->driver);
211 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_unregister);
214 * snd_ac97_codec_get_platdata - get platform_data
215 * @adev: the ac97 codec device
217 * For legacy platforms, in order to have platform_data in codec drivers
218 * available, while ac97 device are auto-created upon probe, this retrieves the
219 * platdata which was setup on ac97 controller registration.
221 * Returns the platform data pointer
223 void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev)
225 struct ac97_controller *ac97_ctrl = adev->ac97_ctrl;
227 return ac97_ctrl->codecs_pdata[adev->num];
229 EXPORT_SYMBOL_GPL(snd_ac97_codec_get_platdata);
231 static void ac97_ctrl_codecs_unregister(struct ac97_controller *ac97_ctrl)
235 for (i = 0; i < AC97_BUS_MAX_CODECS; i++)
236 if (ac97_ctrl->codecs[i]) {
237 ac97_ctrl->codecs[i]->ac97_ctrl = &ac97_unbound_ctrl;
238 device_unregister(&ac97_ctrl->codecs[i]->dev);
242 static ssize_t cold_reset_store(struct device *dev,
243 struct device_attribute *attr, const char *buf,
246 struct ac97_controller *ac97_ctrl;
248 mutex_lock(&ac97_controllers_mutex);
249 ac97_ctrl = to_ac97_controller(dev);
250 ac97_ctrl->ops->reset(ac97_ctrl);
251 mutex_unlock(&ac97_controllers_mutex);
254 static DEVICE_ATTR_WO(cold_reset);
256 static ssize_t warm_reset_store(struct device *dev,
257 struct device_attribute *attr, const char *buf,
260 struct ac97_controller *ac97_ctrl;
265 mutex_lock(&ac97_controllers_mutex);
266 ac97_ctrl = to_ac97_controller(dev);
267 ac97_ctrl->ops->warm_reset(ac97_ctrl);
268 mutex_unlock(&ac97_controllers_mutex);
271 static DEVICE_ATTR_WO(warm_reset);
273 static struct attribute *ac97_controller_device_attrs[] = {
274 &dev_attr_cold_reset.attr,
275 &dev_attr_warm_reset.attr,
279 static struct attribute_group ac97_adapter_attr_group = {
280 .name = "ac97_operations",
281 .attrs = ac97_controller_device_attrs,
284 static const struct attribute_group *ac97_adapter_groups[] = {
285 &ac97_adapter_attr_group,
289 static void ac97_del_adapter(struct ac97_controller *ac97_ctrl)
291 mutex_lock(&ac97_controllers_mutex);
292 ac97_ctrl_codecs_unregister(ac97_ctrl);
293 list_del(&ac97_ctrl->controllers);
294 mutex_unlock(&ac97_controllers_mutex);
296 device_unregister(&ac97_ctrl->adap);
299 static void ac97_adapter_release(struct device *dev)
301 struct ac97_controller *ac97_ctrl;
303 ac97_ctrl = to_ac97_controller(dev);
304 idr_remove(&ac97_adapter_idr, ac97_ctrl->nr);
305 dev_dbg(&ac97_ctrl->adap, "adapter unregistered by %s\n",
306 dev_name(ac97_ctrl->parent));
309 static const struct device_type ac97_adapter_type = {
310 .groups = ac97_adapter_groups,
311 .release = ac97_adapter_release,
314 static int ac97_add_adapter(struct ac97_controller *ac97_ctrl)
318 mutex_lock(&ac97_controllers_mutex);
319 ret = idr_alloc(&ac97_adapter_idr, ac97_ctrl, 0, 0, GFP_KERNEL);
322 dev_set_name(&ac97_ctrl->adap, "ac97-%d", ret);
323 ac97_ctrl->adap.type = &ac97_adapter_type;
324 ac97_ctrl->adap.parent = ac97_ctrl->parent;
325 ret = device_register(&ac97_ctrl->adap);
327 put_device(&ac97_ctrl->adap);
330 list_add(&ac97_ctrl->controllers, &ac97_controllers);
331 mutex_unlock(&ac97_controllers_mutex);
334 dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
335 dev_name(ac97_ctrl->parent));
340 * snd_ac97_controller_register - register an ac97 controller
341 * @ops: the ac97 bus operations
342 * @dev: the device providing the ac97 DC function
343 * @slots_available: mask of the ac97 codecs that can be scanned and probed
344 * bit0 => codec 0, bit1 => codec 1 ... bit 3 => codec 3
346 * Register a digital controller which can control up to 4 ac97 codecs. This is
347 * the controller side of the AC97 AC-link, while the slave side are the codecs.
349 * Returns a valid controller upon success, negative pointer value upon error
351 struct ac97_controller *snd_ac97_controller_register(
352 const struct ac97_controller_ops *ops, struct device *dev,
353 unsigned short slots_available, void **codecs_pdata)
355 struct ac97_controller *ac97_ctrl;
358 ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL);
360 return ERR_PTR(-ENOMEM);
362 for (i = 0; i < AC97_BUS_MAX_CODECS && codecs_pdata; i++)
363 ac97_ctrl->codecs_pdata[i] = codecs_pdata[i];
365 ac97_ctrl->ops = ops;
366 ac97_ctrl->slots_available = slots_available;
367 ac97_ctrl->parent = dev;
368 ret = ac97_add_adapter(ac97_ctrl);
372 ac97_bus_reset(ac97_ctrl);
373 ac97_bus_scan(ac97_ctrl);
380 EXPORT_SYMBOL_GPL(snd_ac97_controller_register);
383 * snd_ac97_controller_unregister - unregister an ac97 controller
384 * @ac97_ctrl: the device previously provided to ac97_controller_register()
387 void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
389 ac97_del_adapter(ac97_ctrl);
391 EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister);
394 static int ac97_pm_runtime_suspend(struct device *dev)
396 struct ac97_codec_device *codec = to_ac97_device(dev);
397 int ret = pm_generic_runtime_suspend(dev);
399 if (ret == 0 && dev->driver) {
400 if (pm_runtime_is_irq_safe(dev))
401 clk_disable(codec->clk);
403 clk_disable_unprepare(codec->clk);
409 static int ac97_pm_runtime_resume(struct device *dev)
411 struct ac97_codec_device *codec = to_ac97_device(dev);
415 if (pm_runtime_is_irq_safe(dev))
416 ret = clk_enable(codec->clk);
418 ret = clk_prepare_enable(codec->clk);
423 return pm_generic_runtime_resume(dev);
425 #endif /* CONFIG_PM */
427 static const struct dev_pm_ops ac97_pm = {
428 .suspend = pm_generic_suspend,
429 .resume = pm_generic_resume,
430 .freeze = pm_generic_freeze,
431 .thaw = pm_generic_thaw,
432 .poweroff = pm_generic_poweroff,
433 .restore = pm_generic_restore,
435 ac97_pm_runtime_suspend,
436 ac97_pm_runtime_resume,
440 static int ac97_get_enable_clk(struct ac97_codec_device *adev)
444 adev->clk = clk_get(&adev->dev, "ac97_clk");
445 if (IS_ERR(adev->clk))
446 return PTR_ERR(adev->clk);
448 ret = clk_prepare_enable(adev->clk);
455 static void ac97_put_disable_clk(struct ac97_codec_device *adev)
457 clk_disable_unprepare(adev->clk);
461 static ssize_t vendor_id_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
464 struct ac97_codec_device *codec = to_ac97_device(dev);
466 return sprintf(buf, "%08x", codec->vendor_id);
468 DEVICE_ATTR_RO(vendor_id);
470 static struct attribute *ac97_dev_attrs[] = {
471 &dev_attr_vendor_id.attr,
474 ATTRIBUTE_GROUPS(ac97_dev);
476 static int ac97_bus_match(struct device *dev, struct device_driver *drv)
478 struct ac97_codec_device *adev = to_ac97_device(dev);
479 struct ac97_codec_driver *adrv = to_ac97_driver(drv);
480 const struct ac97_id *id = adrv->id_table;
483 if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
487 if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
489 } while (id[i++].id);
494 static int ac97_bus_probe(struct device *dev)
496 struct ac97_codec_device *adev = to_ac97_device(dev);
497 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
500 ret = ac97_get_enable_clk(adev);
504 pm_runtime_get_noresume(dev);
505 pm_runtime_set_active(dev);
506 pm_runtime_enable(dev);
508 ret = adrv->probe(adev);
512 pm_runtime_disable(dev);
513 pm_runtime_set_suspended(dev);
514 pm_runtime_put_noidle(dev);
515 ac97_put_disable_clk(adev);
520 static int ac97_bus_remove(struct device *dev)
522 struct ac97_codec_device *adev = to_ac97_device(dev);
523 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
526 ret = pm_runtime_resume_and_get(dev);
530 ret = adrv->remove(adev);
531 pm_runtime_put_noidle(dev);
533 ac97_put_disable_clk(adev);
535 pm_runtime_disable(dev);
540 static struct bus_type ac97_bus_type = {
542 .dev_groups = ac97_dev_groups,
543 .match = ac97_bus_match,
545 .probe = ac97_bus_probe,
546 .remove = ac97_bus_remove,
549 static int __init ac97_bus_init(void)
551 return bus_register(&ac97_bus_type);
553 subsys_initcall(ac97_bus_init);
555 static void __exit ac97_bus_exit(void)
557 bus_unregister(&ac97_bus_type);
559 module_exit(ac97_bus_exit);
561 MODULE_LICENSE("GPL");
562 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");