xref: /openbmc/linux/sound/ac97/bus.c (revision 98ddec80)
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/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>
23 
24 #include "ac97_core.h"
25 
26 /*
27  * Protects ac97_controllers and each ac97_controller structure.
28  */
29 static DEFINE_MUTEX(ac97_controllers_mutex);
30 static DEFINE_IDR(ac97_adapter_idr);
31 static LIST_HEAD(ac97_controllers);
32 
33 static struct bus_type ac97_bus_type;
34 
35 static inline struct ac97_controller*
36 to_ac97_controller(struct device *ac97_adapter)
37 {
38 	return container_of(ac97_adapter, struct ac97_controller, adap);
39 }
40 
41 static int ac97_unbound_ctrl_write(struct ac97_controller *adrv, int slot,
42 		     unsigned short reg, unsigned short val)
43 {
44 	return -ENODEV;
45 }
46 
47 static int ac97_unbound_ctrl_read(struct ac97_controller *adrv, int slot,
48 				  unsigned short reg)
49 {
50 	return -ENODEV;
51 }
52 
53 static const struct ac97_controller_ops ac97_unbound_ctrl_ops = {
54 	.write = ac97_unbound_ctrl_write,
55 	.read = ac97_unbound_ctrl_read,
56 };
57 
58 static struct ac97_controller ac97_unbound_ctrl = {
59 	.ops = &ac97_unbound_ctrl_ops,
60 };
61 
62 static struct ac97_codec_device *
63 ac97_codec_find(struct ac97_controller *ac97_ctrl, unsigned int codec_num)
64 {
65 	if (codec_num >= AC97_BUS_MAX_CODECS)
66 		return ERR_PTR(-EINVAL);
67 
68 	return ac97_ctrl->codecs[codec_num];
69 }
70 
71 static void ac97_codec_release(struct device *dev)
72 {
73 	struct ac97_codec_device *adev;
74 	struct ac97_controller *ac97_ctrl;
75 
76 	adev = to_ac97_device(dev);
77 	ac97_ctrl = adev->ac97_ctrl;
78 	ac97_ctrl->codecs[adev->num] = NULL;
79 	kfree(adev);
80 }
81 
82 static int ac97_codec_add(struct ac97_controller *ac97_ctrl, int idx,
83 		   unsigned int vendor_id)
84 {
85 	struct ac97_codec_device *codec;
86 	int ret;
87 
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;
98 
99 	device_initialize(&codec->dev);
100 	dev_set_name(&codec->dev, "%s:%u", dev_name(ac97_ctrl->parent), idx);
101 
102 	ret = device_add(&codec->dev);
103 	if (ret)
104 		goto err_free_codec;
105 
106 	return 0;
107 err_free_codec:
108 	put_device(&codec->dev);
109 	kfree(codec);
110 	ac97_ctrl->codecs[idx] = NULL;
111 
112 	return ret;
113 }
114 
115 unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv,
116 				   unsigned int codec_num)
117 {
118 	unsigned short vid1, vid2;
119 	int ret;
120 
121 	ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1);
122 	vid1 = (ret & 0xffff);
123 	if (ret < 0)
124 		return 0;
125 
126 	ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2);
127 	vid2 = (ret & 0xffff);
128 	if (ret < 0)
129 		return 0;
130 
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);
134 }
135 
136 static int ac97_bus_scan(struct ac97_controller *ac97_ctrl)
137 {
138 	int ret, i;
139 	unsigned int vendor_id;
140 
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;
149 
150 		ret = ac97_codec_add(ac97_ctrl, i, vendor_id);
151 		if (ret < 0)
152 			return ret;
153 	}
154 	return 0;
155 }
156 
157 static int ac97_bus_reset(struct ac97_controller *ac97_ctrl)
158 {
159 	ac97_ctrl->ops->reset(ac97_ctrl);
160 
161 	return 0;
162 }
163 
164 /**
165  * snd_ac97_codec_driver_register - register an AC97 codec driver
166  * @dev: AC97 driver codec to register
167  *
168  * Register an AC97 codec driver to the ac97 bus driver, aka. the AC97 digital
169  * controller.
170  *
171  * Returns 0 on success or error code
172  */
173 int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv)
174 {
175 	drv->driver.bus = &ac97_bus_type;
176 	return driver_register(&drv->driver);
177 }
178 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_register);
179 
180 /**
181  * snd_ac97_codec_driver_unregister - unregister an AC97 codec driver
182  * @dev: AC97 codec driver to unregister
183  *
184  * Unregister a previously registered ac97 codec driver.
185  */
186 void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv)
187 {
188 	driver_unregister(&drv->driver);
189 }
190 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_unregister);
191 
192 /**
193  * snd_ac97_codec_get_platdata - get platform_data
194  * @adev: the ac97 codec device
195  *
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.
199  *
200  * Returns the platform data pointer
201  */
202 void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev)
203 {
204 	struct ac97_controller *ac97_ctrl = adev->ac97_ctrl;
205 
206 	return ac97_ctrl->codecs_pdata[adev->num];
207 }
208 EXPORT_SYMBOL_GPL(snd_ac97_codec_get_platdata);
209 
210 static void ac97_ctrl_codecs_unregister(struct ac97_controller *ac97_ctrl)
211 {
212 	int i;
213 
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);
218 		}
219 }
220 
221 static ssize_t cold_reset_store(struct device *dev,
222 				struct device_attribute *attr, const char *buf,
223 				size_t len)
224 {
225 	struct ac97_controller *ac97_ctrl;
226 
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;
232 }
233 static DEVICE_ATTR_WO(cold_reset);
234 
235 static ssize_t warm_reset_store(struct device *dev,
236 				struct device_attribute *attr, const char *buf,
237 				size_t len)
238 {
239 	struct ac97_controller *ac97_ctrl;
240 
241 	if (!dev)
242 		return -ENODEV;
243 
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;
249 }
250 static DEVICE_ATTR_WO(warm_reset);
251 
252 static struct attribute *ac97_controller_device_attrs[] = {
253 	&dev_attr_cold_reset.attr,
254 	&dev_attr_warm_reset.attr,
255 	NULL
256 };
257 
258 static struct attribute_group ac97_adapter_attr_group = {
259 	.name	= "ac97_operations",
260 	.attrs	= ac97_controller_device_attrs,
261 };
262 
263 static const struct attribute_group *ac97_adapter_groups[] = {
264 	&ac97_adapter_attr_group,
265 	NULL,
266 };
267 
268 static void ac97_del_adapter(struct ac97_controller *ac97_ctrl)
269 {
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);
274 
275 	device_unregister(&ac97_ctrl->adap);
276 }
277 
278 static void ac97_adapter_release(struct device *dev)
279 {
280 	struct ac97_controller *ac97_ctrl;
281 
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));
286 }
287 
288 static const struct device_type ac97_adapter_type = {
289 	.groups		= ac97_adapter_groups,
290 	.release	= ac97_adapter_release,
291 };
292 
293 static int ac97_add_adapter(struct ac97_controller *ac97_ctrl)
294 {
295 	int ret;
296 
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);
307 	}
308 	if (!ret)
309 		list_add(&ac97_ctrl->controllers, &ac97_controllers);
310 	mutex_unlock(&ac97_controllers_mutex);
311 
312 	if (!ret)
313 		dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
314 			dev_name(ac97_ctrl->parent));
315 	return ret;
316 }
317 
318 /**
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
324  *
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.
327  *
328  * Returns a valid controller upon success, negative pointer value upon error
329  */
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)
333 {
334 	struct ac97_controller *ac97_ctrl;
335 	int ret, i;
336 
337 	ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL);
338 	if (!ac97_ctrl)
339 		return ERR_PTR(-ENOMEM);
340 
341 	for (i = 0; i < AC97_BUS_MAX_CODECS && codecs_pdata; i++)
342 		ac97_ctrl->codecs_pdata[i] = codecs_pdata[i];
343 
344 	ac97_ctrl->ops = ops;
345 	ac97_ctrl->slots_available = slots_available;
346 	ac97_ctrl->parent = dev;
347 	ret = ac97_add_adapter(ac97_ctrl);
348 
349 	if (ret)
350 		goto err;
351 	ac97_bus_reset(ac97_ctrl);
352 	ac97_bus_scan(ac97_ctrl);
353 
354 	return ac97_ctrl;
355 err:
356 	kfree(ac97_ctrl);
357 	return ERR_PTR(ret);
358 }
359 EXPORT_SYMBOL_GPL(snd_ac97_controller_register);
360 
361 /**
362  * snd_ac97_controller_unregister - unregister an ac97 controller
363  * @ac97_ctrl: the device previously provided to ac97_controller_register()
364  *
365  */
366 void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
367 {
368 	ac97_del_adapter(ac97_ctrl);
369 }
370 EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister);
371 
372 #ifdef CONFIG_PM
373 static int ac97_pm_runtime_suspend(struct device *dev)
374 {
375 	struct ac97_codec_device *codec = to_ac97_device(dev);
376 	int ret = pm_generic_runtime_suspend(dev);
377 
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);
383 	}
384 
385 	return ret;
386 }
387 
388 static int ac97_pm_runtime_resume(struct device *dev)
389 {
390 	struct ac97_codec_device *codec = to_ac97_device(dev);
391 	int ret;
392 
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;
400 	}
401 
402 	return pm_generic_runtime_resume(dev);
403 }
404 #endif /* CONFIG_PM */
405 
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)
417 };
418 
419 static int ac97_get_enable_clk(struct ac97_codec_device *adev)
420 {
421 	int ret;
422 
423 	adev->clk = clk_get(&adev->dev, "ac97_clk");
424 	if (IS_ERR(adev->clk))
425 		return PTR_ERR(adev->clk);
426 
427 	ret = clk_prepare_enable(adev->clk);
428 	if (ret)
429 		clk_put(adev->clk);
430 
431 	return ret;
432 }
433 
434 static void ac97_put_disable_clk(struct ac97_codec_device *adev)
435 {
436 	clk_disable_unprepare(adev->clk);
437 	clk_put(adev->clk);
438 }
439 
440 static ssize_t vendor_id_show(struct device *dev,
441 			      struct device_attribute *attr, char *buf)
442 {
443 	struct ac97_codec_device *codec = to_ac97_device(dev);
444 
445 	return sprintf(buf, "%08x", codec->vendor_id);
446 }
447 DEVICE_ATTR_RO(vendor_id);
448 
449 static struct attribute *ac97_dev_attrs[] = {
450 	&dev_attr_vendor_id.attr,
451 	NULL,
452 };
453 ATTRIBUTE_GROUPS(ac97_dev);
454 
455 static int ac97_bus_match(struct device *dev, struct device_driver *drv)
456 {
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;
461 
462 	if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
463 		return false;
464 
465 	do {
466 		if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
467 			return true;
468 	} while (id[i++].id);
469 
470 	return false;
471 }
472 
473 static int ac97_bus_probe(struct device *dev)
474 {
475 	struct ac97_codec_device *adev = to_ac97_device(dev);
476 	struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
477 	int ret;
478 
479 	ret = ac97_get_enable_clk(adev);
480 	if (ret)
481 		return ret;
482 
483 	pm_runtime_get_noresume(dev);
484 	pm_runtime_set_active(dev);
485 	pm_runtime_enable(dev);
486 
487 	ret = adrv->probe(adev);
488 	if (ret == 0)
489 		return 0;
490 
491 	pm_runtime_disable(dev);
492 	pm_runtime_set_suspended(dev);
493 	pm_runtime_put_noidle(dev);
494 	ac97_put_disable_clk(adev);
495 
496 	return ret;
497 }
498 
499 static int ac97_bus_remove(struct device *dev)
500 {
501 	struct ac97_codec_device *adev = to_ac97_device(dev);
502 	struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
503 	int ret;
504 
505 	ret = pm_runtime_get_sync(dev);
506 	if (ret)
507 		return ret;
508 
509 	ret = adrv->remove(adev);
510 	pm_runtime_put_noidle(dev);
511 	if (ret == 0)
512 		ac97_put_disable_clk(adev);
513 
514 	return ret;
515 }
516 
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,
524 };
525 
526 static int __init ac97_bus_init(void)
527 {
528 	return bus_register(&ac97_bus_type);
529 }
530 subsys_initcall(ac97_bus_init);
531 
532 static void __exit ac97_bus_exit(void)
533 {
534 	bus_unregister(&ac97_bus_type);
535 }
536 module_exit(ac97_bus_exit);
537 
538 MODULE_LICENSE("GPL");
539 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
540