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