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>
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>
24 #include "ac97_core.h"
27 * Protects ac97_controllers and each ac97_controller structure.
29 static DEFINE_MUTEX(ac97_controllers_mutex);
30 static DEFINE_IDR(ac97_adapter_idr);
31 static LIST_HEAD(ac97_controllers);
33 static struct bus_type ac97_bus_type;
35 static inline struct ac97_controller*
36 to_ac97_controller(struct device *ac97_adapter)
38 return container_of(ac97_adapter, struct ac97_controller, adap);
41 static int ac97_unbound_ctrl_write(struct ac97_controller *adrv, int slot,
42 unsigned short reg, unsigned short val)
47 static int ac97_unbound_ctrl_read(struct ac97_controller *adrv, int slot,
53 static const struct ac97_controller_ops ac97_unbound_ctrl_ops = {
54 .write = ac97_unbound_ctrl_write,
55 .read = ac97_unbound_ctrl_read,
58 static struct ac97_controller ac97_unbound_ctrl = {
59 .ops = &ac97_unbound_ctrl_ops,
62 static struct ac97_codec_device *
63 ac97_codec_find(struct ac97_controller *ac97_ctrl, unsigned int codec_num)
65 if (codec_num >= AC97_BUS_MAX_CODECS)
66 return ERR_PTR(-EINVAL);
68 return ac97_ctrl->codecs[codec_num];
71 static void ac97_codec_release(struct device *dev)
73 struct ac97_codec_device *adev;
74 struct ac97_controller *ac97_ctrl;
76 adev = to_ac97_device(dev);
77 ac97_ctrl = adev->ac97_ctrl;
78 ac97_ctrl->codecs[adev->num] = NULL;
82 static int ac97_codec_add(struct ac97_controller *ac97_ctrl, int idx,
83 unsigned int vendor_id)
85 struct ac97_codec_device *codec;
88 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
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;
97 codec->ac97_ctrl = ac97_ctrl;
99 device_initialize(&codec->dev);
100 dev_set_name(&codec->dev, "%s:%u", dev_name(ac97_ctrl->parent), idx);
102 ret = device_add(&codec->dev);
108 put_device(&codec->dev);
110 ac97_ctrl->codecs[idx] = NULL;
115 unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv,
116 unsigned int codec_num)
118 unsigned short vid1, vid2;
121 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1);
122 vid1 = (ret & 0xffff);
126 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2);
127 vid2 = (ret & 0xffff);
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);
136 static int ac97_bus_scan(struct ac97_controller *ac97_ctrl)
139 unsigned int vendor_id;
141 for (i = 0; i < AC97_BUS_MAX_CODECS; i++) {
142 if (ac97_codec_find(ac97_ctrl, i))
144 if (!(ac97_ctrl->slots_available & BIT(i)))
146 vendor_id = snd_ac97_bus_scan_one(ac97_ctrl, i);
150 ret = ac97_codec_add(ac97_ctrl, i, vendor_id);
157 static int ac97_bus_reset(struct ac97_controller *ac97_ctrl)
159 ac97_ctrl->ops->reset(ac97_ctrl);
165 * snd_ac97_codec_driver_register - register an AC97 codec driver
166 * @dev: AC97 driver codec to register
168 * Register an AC97 codec driver to the ac97 bus driver, aka. the AC97 digital
171 * Returns 0 on success or error code
173 int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv)
175 drv->driver.bus = &ac97_bus_type;
176 return driver_register(&drv->driver);
178 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_register);
181 * snd_ac97_codec_driver_unregister - unregister an AC97 codec driver
182 * @dev: AC97 codec driver to unregister
184 * Unregister a previously registered ac97 codec driver.
186 void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv)
188 driver_unregister(&drv->driver);
190 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_unregister);
193 * snd_ac97_codec_get_platdata - get platform_data
194 * @adev: the ac97 codec device
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.
200 * Returns the platform data pointer
202 void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev)
204 struct ac97_controller *ac97_ctrl = adev->ac97_ctrl;
206 return ac97_ctrl->codecs_pdata[adev->num];
208 EXPORT_SYMBOL_GPL(snd_ac97_codec_get_platdata);
210 static void ac97_ctrl_codecs_unregister(struct ac97_controller *ac97_ctrl)
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);
221 static ssize_t cold_reset_store(struct device *dev,
222 struct device_attribute *attr, const char *buf,
225 struct ac97_controller *ac97_ctrl;
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);
233 static DEVICE_ATTR_WO(cold_reset);
235 static ssize_t warm_reset_store(struct device *dev,
236 struct device_attribute *attr, const char *buf,
239 struct ac97_controller *ac97_ctrl;
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);
250 static DEVICE_ATTR_WO(warm_reset);
252 static struct attribute *ac97_controller_device_attrs[] = {
253 &dev_attr_cold_reset.attr,
254 &dev_attr_warm_reset.attr,
258 static struct attribute_group ac97_adapter_attr_group = {
259 .name = "ac97_operations",
260 .attrs = ac97_controller_device_attrs,
263 static const struct attribute_group *ac97_adapter_groups[] = {
264 &ac97_adapter_attr_group,
268 static void ac97_del_adapter(struct ac97_controller *ac97_ctrl)
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);
275 device_unregister(&ac97_ctrl->adap);
278 static void ac97_adapter_release(struct device *dev)
280 struct ac97_controller *ac97_ctrl;
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));
288 static const struct device_type ac97_adapter_type = {
289 .groups = ac97_adapter_groups,
290 .release = ac97_adapter_release,
293 static int ac97_add_adapter(struct ac97_controller *ac97_ctrl)
297 mutex_lock(&ac97_controllers_mutex);
298 ret = idr_alloc(&ac97_adapter_idr, ac97_ctrl, 0, 0, GFP_KERNEL);
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);
306 put_device(&ac97_ctrl->adap);
309 list_add(&ac97_ctrl->controllers, &ac97_controllers);
310 mutex_unlock(&ac97_controllers_mutex);
313 dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
314 dev_name(ac97_ctrl->parent));
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
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.
328 * Returns a valid controller upon success, negative pointer value upon error
330 struct ac97_controller *snd_ac97_controller_register(
331 const struct ac97_controller_ops *ops, struct device *dev,
332 unsigned short slots_available, void **codecs_pdata)
334 struct ac97_controller *ac97_ctrl;
337 ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL);
339 return ERR_PTR(-ENOMEM);
341 for (i = 0; i < AC97_BUS_MAX_CODECS && codecs_pdata; i++)
342 ac97_ctrl->codecs_pdata[i] = codecs_pdata[i];
344 ac97_ctrl->ops = ops;
345 ac97_ctrl->slots_available = slots_available;
346 ac97_ctrl->parent = dev;
347 ret = ac97_add_adapter(ac97_ctrl);
351 ac97_bus_reset(ac97_ctrl);
352 ac97_bus_scan(ac97_ctrl);
359 EXPORT_SYMBOL_GPL(snd_ac97_controller_register);
362 * snd_ac97_controller_unregister - unregister an ac97 controller
363 * @ac97_ctrl: the device previously provided to ac97_controller_register()
366 void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
368 ac97_del_adapter(ac97_ctrl);
370 EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister);
373 static int ac97_pm_runtime_suspend(struct device *dev)
375 struct ac97_codec_device *codec = to_ac97_device(dev);
376 int ret = pm_generic_runtime_suspend(dev);
378 if (ret == 0 && dev->driver) {
379 if (pm_runtime_is_irq_safe(dev))
380 clk_disable(codec->clk);
382 clk_disable_unprepare(codec->clk);
388 static int ac97_pm_runtime_resume(struct device *dev)
390 struct ac97_codec_device *codec = to_ac97_device(dev);
394 if (pm_runtime_is_irq_safe(dev))
395 ret = clk_enable(codec->clk);
397 ret = clk_prepare_enable(codec->clk);
402 return pm_generic_runtime_resume(dev);
404 #endif /* CONFIG_PM */
406 static 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,
414 ac97_pm_runtime_suspend,
415 ac97_pm_runtime_resume,
419 static int ac97_get_enable_clk(struct ac97_codec_device *adev)
423 adev->clk = clk_get(&adev->dev, "ac97_clk");
424 if (IS_ERR(adev->clk))
425 return PTR_ERR(adev->clk);
427 ret = clk_prepare_enable(adev->clk);
434 static void ac97_put_disable_clk(struct ac97_codec_device *adev)
436 clk_disable_unprepare(adev->clk);
440 static ssize_t vendor_id_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct ac97_codec_device *codec = to_ac97_device(dev);
445 return sprintf(buf, "%08x", codec->vendor_id);
447 DEVICE_ATTR_RO(vendor_id);
449 static struct attribute *ac97_dev_attrs[] = {
450 &dev_attr_vendor_id.attr,
453 ATTRIBUTE_GROUPS(ac97_dev);
455 static int ac97_bus_match(struct device *dev, struct device_driver *drv)
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;
462 if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
466 if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
468 } while (id[i++].id);
473 static int ac97_bus_probe(struct device *dev)
475 struct ac97_codec_device *adev = to_ac97_device(dev);
476 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
479 ret = ac97_get_enable_clk(adev);
483 pm_runtime_get_noresume(dev);
484 pm_runtime_set_active(dev);
485 pm_runtime_enable(dev);
487 ret = adrv->probe(adev);
491 pm_runtime_disable(dev);
492 pm_runtime_set_suspended(dev);
493 pm_runtime_put_noidle(dev);
494 ac97_put_disable_clk(adev);
499 static int ac97_bus_remove(struct device *dev)
501 struct ac97_codec_device *adev = to_ac97_device(dev);
502 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
505 ret = pm_runtime_get_sync(dev);
509 ret = adrv->remove(adev);
510 pm_runtime_put_noidle(dev);
512 ac97_put_disable_clk(adev);
517 static struct bus_type ac97_bus_type = {
519 .dev_groups = ac97_dev_groups,
520 .match = ac97_bus_match,
522 .probe = ac97_bus_probe,
523 .remove = ac97_bus_remove,
526 static int __init ac97_bus_init(void)
528 return bus_register(&ac97_bus_type);
530 subsys_initcall(ac97_bus_init);
532 static void __exit ac97_bus_exit(void)
534 bus_unregister(&ac97_bus_type);
536 module_exit(ac97_bus_exit);
538 MODULE_LICENSE("GPL");
539 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");