intel_th: Don't touch switch routing in host mode
[linux/fpc-iii.git] / sound / ac97 / bus.c
blob31f858eceffcb29937a05b143e920f6b8b8abfef
1 /*
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.
7 */
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>
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)
44 return -ENODEV;
47 static int ac97_unbound_ctrl_read(struct ac97_controller *adrv, int slot,
48 unsigned short reg)
50 return -ENODEV;
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;
79 kfree(adev);
82 static int ac97_codec_add(struct ac97_controller *ac97_ctrl, int idx,
83 unsigned int vendor_id)
85 struct ac97_codec_device *codec;
86 int ret;
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;
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);
103 if (ret)
104 goto err_free_codec;
106 return 0;
107 err_free_codec:
108 put_device(&codec->dev);
109 kfree(codec);
110 ac97_ctrl->codecs[idx] = NULL;
112 return ret;
115 unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv,
116 unsigned int codec_num)
118 unsigned short vid1, vid2;
119 int ret;
121 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1);
122 vid1 = (ret & 0xffff);
123 if (ret < 0)
124 return 0;
126 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2);
127 vid2 = (ret & 0xffff);
128 if (ret < 0)
129 return 0;
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)
138 int ret, i;
139 unsigned int vendor_id;
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;
150 ret = ac97_codec_add(ac97_ctrl, i, vendor_id);
151 if (ret < 0)
152 return ret;
154 return 0;
157 static int ac97_bus_reset(struct ac97_controller *ac97_ctrl)
159 ac97_ctrl->ops->reset(ac97_ctrl);
161 return 0;
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
169 * controller.
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)
212 int i;
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,
223 size_t len)
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);
231 return len;
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,
237 size_t len)
239 struct ac97_controller *ac97_ctrl;
241 if (!dev)
242 return -ENODEV;
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;
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,
255 NULL
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,
265 NULL,
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)
295 int ret;
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);
308 if (!ret)
309 list_add(&ac97_ctrl->controllers, &ac97_controllers);
310 mutex_unlock(&ac97_controllers_mutex);
312 if (!ret)
313 dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
314 dev_name(ac97_ctrl->parent));
315 return ret;
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;
335 int ret, i;
337 ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL);
338 if (!ac97_ctrl)
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);
349 if (ret)
350 goto err;
351 ac97_bus_reset(ac97_ctrl);
352 ac97_bus_scan(ac97_ctrl);
354 return ac97_ctrl;
355 err:
356 kfree(ac97_ctrl);
357 return ERR_PTR(ret);
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);
372 #ifdef CONFIG_PM
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);
381 else
382 clk_disable_unprepare(codec->clk);
385 return ret;
388 static int ac97_pm_runtime_resume(struct device *dev)
390 struct ac97_codec_device *codec = to_ac97_device(dev);
391 int ret;
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;
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,
413 SET_RUNTIME_PM_OPS(
414 ac97_pm_runtime_suspend,
415 ac97_pm_runtime_resume,
416 NULL)
419 static int ac97_get_enable_clk(struct ac97_codec_device *adev)
421 int ret;
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);
428 if (ret)
429 clk_put(adev->clk);
431 return ret;
434 static void ac97_put_disable_clk(struct ac97_codec_device *adev)
436 clk_disable_unprepare(adev->clk);
437 clk_put(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,
451 NULL,
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;
460 int i = 0;
462 if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
463 return false;
465 do {
466 if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
467 return true;
468 } while (id[i++].id);
470 return false;
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);
477 int ret;
479 ret = ac97_get_enable_clk(adev);
480 if (ret)
481 return ret;
483 pm_runtime_get_noresume(dev);
484 pm_runtime_set_active(dev);
485 pm_runtime_enable(dev);
487 ret = adrv->probe(adev);
488 if (ret == 0)
489 return 0;
491 pm_runtime_disable(dev);
492 pm_runtime_set_suspended(dev);
493 pm_runtime_put_noidle(dev);
494 ac97_put_disable_clk(adev);
496 return ret;
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);
503 int ret;
505 ret = pm_runtime_get_sync(dev);
506 if (ret)
507 return ret;
509 ret = adrv->remove(adev);
510 pm_runtime_put_noidle(dev);
511 if (ret == 0)
512 ac97_put_disable_clk(adev);
514 return ret;
517 static 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,
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>");