GNU Linux-libre 4.19.264-gnu1
[releases.git] / sound / ac97 / bus.c
1 /*
2  * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
3  *
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  */
8
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/of.h>
17 #include <linux/pm.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>
24
25 #include "ac97_core.h"
26
27 /*
28  * Protects ac97_controllers and each ac97_controller structure.
29  */
30 static DEFINE_MUTEX(ac97_controllers_mutex);
31 static DEFINE_IDR(ac97_adapter_idr);
32 static LIST_HEAD(ac97_controllers);
33
34 static struct bus_type ac97_bus_type;
35
36 static inline struct ac97_controller*
37 to_ac97_controller(struct device *ac97_adapter)
38 {
39         return container_of(ac97_adapter, struct ac97_controller, adap);
40 }
41
42 static int ac97_unbound_ctrl_write(struct ac97_controller *adrv, int slot,
43                      unsigned short reg, unsigned short val)
44 {
45         return -ENODEV;
46 }
47
48 static int ac97_unbound_ctrl_read(struct ac97_controller *adrv, int slot,
49                                   unsigned short reg)
50 {
51         return -ENODEV;
52 }
53
54 static const struct ac97_controller_ops ac97_unbound_ctrl_ops = {
55         .write = ac97_unbound_ctrl_write,
56         .read = ac97_unbound_ctrl_read,
57 };
58
59 static struct ac97_controller ac97_unbound_ctrl = {
60         .ops = &ac97_unbound_ctrl_ops,
61 };
62
63 static struct ac97_codec_device *
64 ac97_codec_find(struct ac97_controller *ac97_ctrl, unsigned int codec_num)
65 {
66         if (codec_num >= AC97_BUS_MAX_CODECS)
67                 return ERR_PTR(-EINVAL);
68
69         return ac97_ctrl->codecs[codec_num];
70 }
71
72 static struct device_node *
73 ac97_of_get_child_device(struct ac97_controller *ac97_ctrl, int idx,
74                          unsigned int vendor_id)
75 {
76         struct device_node *node;
77         u32 reg;
78         char compat[] = "ac97,0000,0000";
79
80         snprintf(compat, sizeof(compat), "ac97,%04x,%04x",
81                  vendor_id >> 16, vendor_id & 0xffff);
82
83         for_each_child_of_node(ac97_ctrl->parent->of_node, node) {
84                 if ((idx != of_property_read_u32(node, "reg", &reg)) ||
85                     !of_device_is_compatible(node, compat))
86                         continue;
87                 return node;
88         }
89
90         return NULL;
91 }
92
93 static void ac97_codec_release(struct device *dev)
94 {
95         struct ac97_codec_device *adev;
96         struct ac97_controller *ac97_ctrl;
97
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);
102         kfree(adev);
103 }
104
105 static int ac97_codec_add(struct ac97_controller *ac97_ctrl, int idx,
106                    unsigned int vendor_id)
107 {
108         struct ac97_codec_device *codec;
109         int ret;
110
111         codec = kzalloc(sizeof(*codec), GFP_KERNEL);
112         if (!codec)
113                 return -ENOMEM;
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;
119         codec->num = idx;
120         codec->ac97_ctrl = ac97_ctrl;
121
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,
125                                                       vendor_id);
126
127         ret = device_add(&codec->dev);
128         if (ret) {
129                 put_device(&codec->dev);
130                 return ret;
131         }
132
133         return 0;
134 }
135
136 unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv,
137                                    unsigned int codec_num)
138 {
139         unsigned short vid1, vid2;
140         int ret;
141
142         ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1);
143         vid1 = (ret & 0xffff);
144         if (ret < 0)
145                 return 0;
146
147         ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2);
148         vid2 = (ret & 0xffff);
149         if (ret < 0)
150                 return 0;
151
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);
155 }
156
157 static int ac97_bus_scan(struct ac97_controller *ac97_ctrl)
158 {
159         int ret, i;
160         unsigned int vendor_id;
161
162         for (i = 0; i < AC97_BUS_MAX_CODECS; i++) {
163                 if (ac97_codec_find(ac97_ctrl, i))
164                         continue;
165                 if (!(ac97_ctrl->slots_available & BIT(i)))
166                         continue;
167                 vendor_id = snd_ac97_bus_scan_one(ac97_ctrl, i);
168                 if (!vendor_id)
169                         continue;
170
171                 ret = ac97_codec_add(ac97_ctrl, i, vendor_id);
172                 if (ret < 0)
173                         return ret;
174         }
175         return 0;
176 }
177
178 static int ac97_bus_reset(struct ac97_controller *ac97_ctrl)
179 {
180         ac97_ctrl->ops->reset(ac97_ctrl);
181
182         return 0;
183 }
184
185 /**
186  * snd_ac97_codec_driver_register - register an AC97 codec driver
187  * @dev: AC97 driver codec to register
188  *
189  * Register an AC97 codec driver to the ac97 bus driver, aka. the AC97 digital
190  * controller.
191  *
192  * Returns 0 on success or error code
193  */
194 int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv)
195 {
196         drv->driver.bus = &ac97_bus_type;
197         return driver_register(&drv->driver);
198 }
199 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_register);
200
201 /**
202  * snd_ac97_codec_driver_unregister - unregister an AC97 codec driver
203  * @dev: AC97 codec driver to unregister
204  *
205  * Unregister a previously registered ac97 codec driver.
206  */
207 void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv)
208 {
209         driver_unregister(&drv->driver);
210 }
211 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_unregister);
212
213 /**
214  * snd_ac97_codec_get_platdata - get platform_data
215  * @adev: the ac97 codec device
216  *
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.
220  *
221  * Returns the platform data pointer
222  */
223 void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev)
224 {
225         struct ac97_controller *ac97_ctrl = adev->ac97_ctrl;
226
227         return ac97_ctrl->codecs_pdata[adev->num];
228 }
229 EXPORT_SYMBOL_GPL(snd_ac97_codec_get_platdata);
230
231 static void ac97_ctrl_codecs_unregister(struct ac97_controller *ac97_ctrl)
232 {
233         int i;
234
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);
239                 }
240 }
241
242 static ssize_t cold_reset_store(struct device *dev,
243                                 struct device_attribute *attr, const char *buf,
244                                 size_t len)
245 {
246         struct ac97_controller *ac97_ctrl;
247
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);
252         return len;
253 }
254 static DEVICE_ATTR_WO(cold_reset);
255
256 static ssize_t warm_reset_store(struct device *dev,
257                                 struct device_attribute *attr, const char *buf,
258                                 size_t len)
259 {
260         struct ac97_controller *ac97_ctrl;
261
262         if (!dev)
263                 return -ENODEV;
264
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);
269         return len;
270 }
271 static DEVICE_ATTR_WO(warm_reset);
272
273 static struct attribute *ac97_controller_device_attrs[] = {
274         &dev_attr_cold_reset.attr,
275         &dev_attr_warm_reset.attr,
276         NULL
277 };
278
279 static struct attribute_group ac97_adapter_attr_group = {
280         .name   = "ac97_operations",
281         .attrs  = ac97_controller_device_attrs,
282 };
283
284 static const struct attribute_group *ac97_adapter_groups[] = {
285         &ac97_adapter_attr_group,
286         NULL,
287 };
288
289 static void ac97_del_adapter(struct ac97_controller *ac97_ctrl)
290 {
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);
295
296         device_unregister(&ac97_ctrl->adap);
297 }
298
299 static void ac97_adapter_release(struct device *dev)
300 {
301         struct ac97_controller *ac97_ctrl;
302
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));
307 }
308
309 static const struct device_type ac97_adapter_type = {
310         .groups         = ac97_adapter_groups,
311         .release        = ac97_adapter_release,
312 };
313
314 static int ac97_add_adapter(struct ac97_controller *ac97_ctrl)
315 {
316         int ret;
317
318         mutex_lock(&ac97_controllers_mutex);
319         ret = idr_alloc(&ac97_adapter_idr, ac97_ctrl, 0, 0, GFP_KERNEL);
320         ac97_ctrl->nr = ret;
321         if (ret >= 0) {
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);
326                 if (ret)
327                         put_device(&ac97_ctrl->adap);
328         }
329         if (!ret)
330                 list_add(&ac97_ctrl->controllers, &ac97_controllers);
331         mutex_unlock(&ac97_controllers_mutex);
332
333         if (!ret)
334                 dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
335                         dev_name(ac97_ctrl->parent));
336         return ret;
337 }
338
339 /**
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
345  *
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.
348  *
349  * Returns a valid controller upon success, negative pointer value upon error
350  */
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)
354 {
355         struct ac97_controller *ac97_ctrl;
356         int ret, i;
357
358         ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL);
359         if (!ac97_ctrl)
360                 return ERR_PTR(-ENOMEM);
361
362         for (i = 0; i < AC97_BUS_MAX_CODECS && codecs_pdata; i++)
363                 ac97_ctrl->codecs_pdata[i] = codecs_pdata[i];
364
365         ac97_ctrl->ops = ops;
366         ac97_ctrl->slots_available = slots_available;
367         ac97_ctrl->parent = dev;
368         ret = ac97_add_adapter(ac97_ctrl);
369
370         if (ret)
371                 goto err;
372         ac97_bus_reset(ac97_ctrl);
373         ac97_bus_scan(ac97_ctrl);
374
375         return ac97_ctrl;
376 err:
377         kfree(ac97_ctrl);
378         return ERR_PTR(ret);
379 }
380 EXPORT_SYMBOL_GPL(snd_ac97_controller_register);
381
382 /**
383  * snd_ac97_controller_unregister - unregister an ac97 controller
384  * @ac97_ctrl: the device previously provided to ac97_controller_register()
385  *
386  */
387 void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
388 {
389         ac97_del_adapter(ac97_ctrl);
390 }
391 EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister);
392
393 #ifdef CONFIG_PM
394 static int ac97_pm_runtime_suspend(struct device *dev)
395 {
396         struct ac97_codec_device *codec = to_ac97_device(dev);
397         int ret = pm_generic_runtime_suspend(dev);
398
399         if (ret == 0 && dev->driver) {
400                 if (pm_runtime_is_irq_safe(dev))
401                         clk_disable(codec->clk);
402                 else
403                         clk_disable_unprepare(codec->clk);
404         }
405
406         return ret;
407 }
408
409 static int ac97_pm_runtime_resume(struct device *dev)
410 {
411         struct ac97_codec_device *codec = to_ac97_device(dev);
412         int ret;
413
414         if (dev->driver) {
415                 if (pm_runtime_is_irq_safe(dev))
416                         ret = clk_enable(codec->clk);
417                 else
418                         ret = clk_prepare_enable(codec->clk);
419                 if (ret)
420                         return ret;
421         }
422
423         return pm_generic_runtime_resume(dev);
424 }
425 #endif /* CONFIG_PM */
426
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,
434         SET_RUNTIME_PM_OPS(
435                 ac97_pm_runtime_suspend,
436                 ac97_pm_runtime_resume,
437                 NULL)
438 };
439
440 static int ac97_get_enable_clk(struct ac97_codec_device *adev)
441 {
442         int ret;
443
444         adev->clk = clk_get(&adev->dev, "ac97_clk");
445         if (IS_ERR(adev->clk))
446                 return PTR_ERR(adev->clk);
447
448         ret = clk_prepare_enable(adev->clk);
449         if (ret)
450                 clk_put(adev->clk);
451
452         return ret;
453 }
454
455 static void ac97_put_disable_clk(struct ac97_codec_device *adev)
456 {
457         clk_disable_unprepare(adev->clk);
458         clk_put(adev->clk);
459 }
460
461 static ssize_t vendor_id_show(struct device *dev,
462                               struct device_attribute *attr, char *buf)
463 {
464         struct ac97_codec_device *codec = to_ac97_device(dev);
465
466         return sprintf(buf, "%08x", codec->vendor_id);
467 }
468 DEVICE_ATTR_RO(vendor_id);
469
470 static struct attribute *ac97_dev_attrs[] = {
471         &dev_attr_vendor_id.attr,
472         NULL,
473 };
474 ATTRIBUTE_GROUPS(ac97_dev);
475
476 static int ac97_bus_match(struct device *dev, struct device_driver *drv)
477 {
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;
481         int i = 0;
482
483         if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
484                 return false;
485
486         do {
487                 if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
488                         return true;
489         } while (id[i++].id);
490
491         return false;
492 }
493
494 static int ac97_bus_probe(struct device *dev)
495 {
496         struct ac97_codec_device *adev = to_ac97_device(dev);
497         struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
498         int ret;
499
500         ret = ac97_get_enable_clk(adev);
501         if (ret)
502                 return ret;
503
504         pm_runtime_get_noresume(dev);
505         pm_runtime_set_active(dev);
506         pm_runtime_enable(dev);
507
508         ret = adrv->probe(adev);
509         if (ret == 0)
510                 return 0;
511
512         pm_runtime_disable(dev);
513         pm_runtime_set_suspended(dev);
514         pm_runtime_put_noidle(dev);
515         ac97_put_disable_clk(adev);
516
517         return ret;
518 }
519
520 static int ac97_bus_remove(struct device *dev)
521 {
522         struct ac97_codec_device *adev = to_ac97_device(dev);
523         struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
524         int ret;
525
526         ret = pm_runtime_resume_and_get(dev);
527         if (ret < 0)
528                 return ret;
529
530         ret = adrv->remove(adev);
531         pm_runtime_put_noidle(dev);
532         if (ret == 0)
533                 ac97_put_disable_clk(adev);
534
535         pm_runtime_disable(dev);
536
537         return ret;
538 }
539
540 static struct bus_type ac97_bus_type = {
541         .name           = "ac97bus",
542         .dev_groups     = ac97_dev_groups,
543         .match          = ac97_bus_match,
544         .pm             = &ac97_pm,
545         .probe          = ac97_bus_probe,
546         .remove         = ac97_bus_remove,
547 };
548
549 static int __init ac97_bus_init(void)
550 {
551         return bus_register(&ac97_bus_type);
552 }
553 subsys_initcall(ac97_bus_init);
554
555 static void __exit ac97_bus_exit(void)
556 {
557         bus_unregister(&ac97_bus_type);
558 }
559 module_exit(ac97_bus_exit);
560
561 MODULE_LICENSE("GPL");
562 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");