]>
Commit | Line | Data |
---|---|---|
74426fbf RJ |
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); | |
11218bfa DX |
103 | if (ret) { |
104 | put_device(&codec->dev); | |
105 | return ret; | |
106 | } | |
74426fbf RJ |
107 | |
108 | return 0; | |
74426fbf RJ |
109 | } |
110 | ||
111 | unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv, | |
112 | unsigned int codec_num) | |
113 | { | |
114 | unsigned short vid1, vid2; | |
115 | int ret; | |
116 | ||
117 | ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1); | |
118 | vid1 = (ret & 0xffff); | |
119 | if (ret < 0) | |
120 | return 0; | |
121 | ||
122 | ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2); | |
123 | vid2 = (ret & 0xffff); | |
124 | if (ret < 0) | |
125 | return 0; | |
126 | ||
127 | dev_dbg(&adrv->adap, "%s(codec_num=%u): vendor_id=0x%08x\n", | |
128 | __func__, codec_num, AC97_ID(vid1, vid2)); | |
129 | return AC97_ID(vid1, vid2); | |
130 | } | |
131 | ||
132 | static int ac97_bus_scan(struct ac97_controller *ac97_ctrl) | |
133 | { | |
134 | int ret, i; | |
135 | unsigned int vendor_id; | |
136 | ||
137 | for (i = 0; i < AC97_BUS_MAX_CODECS; i++) { | |
138 | if (ac97_codec_find(ac97_ctrl, i)) | |
139 | continue; | |
140 | if (!(ac97_ctrl->slots_available & BIT(i))) | |
141 | continue; | |
142 | vendor_id = snd_ac97_bus_scan_one(ac97_ctrl, i); | |
143 | if (!vendor_id) | |
144 | continue; | |
145 | ||
146 | ret = ac97_codec_add(ac97_ctrl, i, vendor_id); | |
147 | if (ret < 0) | |
148 | return ret; | |
149 | } | |
150 | return 0; | |
151 | } | |
152 | ||
153 | static int ac97_bus_reset(struct ac97_controller *ac97_ctrl) | |
154 | { | |
155 | ac97_ctrl->ops->reset(ac97_ctrl); | |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
160 | /** | |
161 | * snd_ac97_codec_driver_register - register an AC97 codec driver | |
162 | * @dev: AC97 driver codec to register | |
163 | * | |
164 | * Register an AC97 codec driver to the ac97 bus driver, aka. the AC97 digital | |
165 | * controller. | |
166 | * | |
167 | * Returns 0 on success or error code | |
168 | */ | |
169 | int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv) | |
170 | { | |
171 | drv->driver.bus = &ac97_bus_type; | |
172 | return driver_register(&drv->driver); | |
173 | } | |
174 | EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_register); | |
175 | ||
176 | /** | |
177 | * snd_ac97_codec_driver_unregister - unregister an AC97 codec driver | |
178 | * @dev: AC97 codec driver to unregister | |
179 | * | |
180 | * Unregister a previously registered ac97 codec driver. | |
181 | */ | |
182 | void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv) | |
183 | { | |
184 | driver_unregister(&drv->driver); | |
185 | } | |
186 | EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_unregister); | |
187 | ||
188 | /** | |
189 | * snd_ac97_codec_get_platdata - get platform_data | |
190 | * @adev: the ac97 codec device | |
191 | * | |
192 | * For legacy platforms, in order to have platform_data in codec drivers | |
193 | * available, while ac97 device are auto-created upon probe, this retrieves the | |
194 | * platdata which was setup on ac97 controller registration. | |
195 | * | |
196 | * Returns the platform data pointer | |
197 | */ | |
198 | void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev) | |
199 | { | |
200 | struct ac97_controller *ac97_ctrl = adev->ac97_ctrl; | |
201 | ||
202 | return ac97_ctrl->codecs_pdata[adev->num]; | |
203 | } | |
204 | EXPORT_SYMBOL_GPL(snd_ac97_codec_get_platdata); | |
205 | ||
206 | static void ac97_ctrl_codecs_unregister(struct ac97_controller *ac97_ctrl) | |
207 | { | |
208 | int i; | |
209 | ||
210 | for (i = 0; i < AC97_BUS_MAX_CODECS; i++) | |
211 | if (ac97_ctrl->codecs[i]) { | |
212 | ac97_ctrl->codecs[i]->ac97_ctrl = &ac97_unbound_ctrl; | |
213 | device_unregister(&ac97_ctrl->codecs[i]->dev); | |
214 | } | |
215 | } | |
216 | ||
217 | static ssize_t cold_reset_store(struct device *dev, | |
218 | struct device_attribute *attr, const char *buf, | |
219 | size_t len) | |
220 | { | |
221 | struct ac97_controller *ac97_ctrl; | |
222 | ||
223 | mutex_lock(&ac97_controllers_mutex); | |
224 | ac97_ctrl = to_ac97_controller(dev); | |
225 | ac97_ctrl->ops->reset(ac97_ctrl); | |
226 | mutex_unlock(&ac97_controllers_mutex); | |
227 | return len; | |
228 | } | |
229 | static DEVICE_ATTR_WO(cold_reset); | |
230 | ||
231 | static ssize_t warm_reset_store(struct device *dev, | |
232 | struct device_attribute *attr, const char *buf, | |
233 | size_t len) | |
234 | { | |
235 | struct ac97_controller *ac97_ctrl; | |
236 | ||
237 | if (!dev) | |
238 | return -ENODEV; | |
239 | ||
240 | mutex_lock(&ac97_controllers_mutex); | |
241 | ac97_ctrl = to_ac97_controller(dev); | |
242 | ac97_ctrl->ops->warm_reset(ac97_ctrl); | |
243 | mutex_unlock(&ac97_controllers_mutex); | |
244 | return len; | |
245 | } | |
246 | static DEVICE_ATTR_WO(warm_reset); | |
247 | ||
248 | static struct attribute *ac97_controller_device_attrs[] = { | |
249 | &dev_attr_cold_reset.attr, | |
250 | &dev_attr_warm_reset.attr, | |
251 | NULL | |
252 | }; | |
253 | ||
254 | static struct attribute_group ac97_adapter_attr_group = { | |
255 | .name = "ac97_operations", | |
256 | .attrs = ac97_controller_device_attrs, | |
257 | }; | |
258 | ||
259 | static const struct attribute_group *ac97_adapter_groups[] = { | |
260 | &ac97_adapter_attr_group, | |
261 | NULL, | |
262 | }; | |
263 | ||
264 | static void ac97_del_adapter(struct ac97_controller *ac97_ctrl) | |
265 | { | |
266 | mutex_lock(&ac97_controllers_mutex); | |
267 | ac97_ctrl_codecs_unregister(ac97_ctrl); | |
268 | list_del(&ac97_ctrl->controllers); | |
269 | mutex_unlock(&ac97_controllers_mutex); | |
270 | ||
271 | device_unregister(&ac97_ctrl->adap); | |
272 | } | |
273 | ||
274 | static void ac97_adapter_release(struct device *dev) | |
275 | { | |
276 | struct ac97_controller *ac97_ctrl; | |
277 | ||
278 | ac97_ctrl = to_ac97_controller(dev); | |
279 | idr_remove(&ac97_adapter_idr, ac97_ctrl->nr); | |
280 | dev_dbg(&ac97_ctrl->adap, "adapter unregistered by %s\n", | |
281 | dev_name(ac97_ctrl->parent)); | |
282 | } | |
283 | ||
284 | static const struct device_type ac97_adapter_type = { | |
285 | .groups = ac97_adapter_groups, | |
286 | .release = ac97_adapter_release, | |
287 | }; | |
288 | ||
289 | static int ac97_add_adapter(struct ac97_controller *ac97_ctrl) | |
290 | { | |
291 | int ret; | |
292 | ||
293 | mutex_lock(&ac97_controllers_mutex); | |
294 | ret = idr_alloc(&ac97_adapter_idr, ac97_ctrl, 0, 0, GFP_KERNEL); | |
295 | ac97_ctrl->nr = ret; | |
296 | if (ret >= 0) { | |
297 | dev_set_name(&ac97_ctrl->adap, "ac97-%d", ret); | |
298 | ac97_ctrl->adap.type = &ac97_adapter_type; | |
299 | ac97_ctrl->adap.parent = ac97_ctrl->parent; | |
300 | ret = device_register(&ac97_ctrl->adap); | |
301 | if (ret) | |
302 | put_device(&ac97_ctrl->adap); | |
303 | } | |
304 | if (!ret) | |
305 | list_add(&ac97_ctrl->controllers, &ac97_controllers); | |
306 | mutex_unlock(&ac97_controllers_mutex); | |
307 | ||
308 | if (!ret) | |
309 | dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n", | |
310 | dev_name(ac97_ctrl->parent)); | |
311 | return ret; | |
312 | } | |
313 | ||
314 | /** | |
315 | * snd_ac97_controller_register - register an ac97 controller | |
316 | * @ops: the ac97 bus operations | |
317 | * @dev: the device providing the ac97 DC function | |
318 | * @slots_available: mask of the ac97 codecs that can be scanned and probed | |
319 | * bit0 => codec 0, bit1 => codec 1 ... bit 3 => codec 3 | |
320 | * | |
321 | * Register a digital controller which can control up to 4 ac97 codecs. This is | |
322 | * the controller side of the AC97 AC-link, while the slave side are the codecs. | |
323 | * | |
324 | * Returns a valid controller upon success, negative pointer value upon error | |
325 | */ | |
326 | struct ac97_controller *snd_ac97_controller_register( | |
327 | const struct ac97_controller_ops *ops, struct device *dev, | |
328 | unsigned short slots_available, void **codecs_pdata) | |
329 | { | |
330 | struct ac97_controller *ac97_ctrl; | |
331 | int ret, i; | |
332 | ||
333 | ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL); | |
334 | if (!ac97_ctrl) | |
335 | return ERR_PTR(-ENOMEM); | |
336 | ||
337 | for (i = 0; i < AC97_BUS_MAX_CODECS && codecs_pdata; i++) | |
338 | ac97_ctrl->codecs_pdata[i] = codecs_pdata[i]; | |
339 | ||
340 | ac97_ctrl->ops = ops; | |
341 | ac97_ctrl->slots_available = slots_available; | |
342 | ac97_ctrl->parent = dev; | |
343 | ret = ac97_add_adapter(ac97_ctrl); | |
344 | ||
345 | if (ret) | |
346 | goto err; | |
347 | ac97_bus_reset(ac97_ctrl); | |
348 | ac97_bus_scan(ac97_ctrl); | |
349 | ||
350 | return ac97_ctrl; | |
351 | err: | |
352 | kfree(ac97_ctrl); | |
353 | return ERR_PTR(ret); | |
354 | } | |
355 | EXPORT_SYMBOL_GPL(snd_ac97_controller_register); | |
356 | ||
357 | /** | |
358 | * snd_ac97_controller_unregister - unregister an ac97 controller | |
359 | * @ac97_ctrl: the device previously provided to ac97_controller_register() | |
360 | * | |
361 | */ | |
362 | void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl) | |
363 | { | |
364 | ac97_del_adapter(ac97_ctrl); | |
365 | } | |
366 | EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister); | |
367 | ||
368 | #ifdef CONFIG_PM | |
369 | static int ac97_pm_runtime_suspend(struct device *dev) | |
370 | { | |
371 | struct ac97_codec_device *codec = to_ac97_device(dev); | |
372 | int ret = pm_generic_runtime_suspend(dev); | |
373 | ||
374 | if (ret == 0 && dev->driver) { | |
375 | if (pm_runtime_is_irq_safe(dev)) | |
376 | clk_disable(codec->clk); | |
377 | else | |
378 | clk_disable_unprepare(codec->clk); | |
379 | } | |
380 | ||
381 | return ret; | |
382 | } | |
383 | ||
384 | static int ac97_pm_runtime_resume(struct device *dev) | |
385 | { | |
386 | struct ac97_codec_device *codec = to_ac97_device(dev); | |
387 | int ret; | |
388 | ||
389 | if (dev->driver) { | |
390 | if (pm_runtime_is_irq_safe(dev)) | |
391 | ret = clk_enable(codec->clk); | |
392 | else | |
393 | ret = clk_prepare_enable(codec->clk); | |
394 | if (ret) | |
395 | return ret; | |
396 | } | |
397 | ||
398 | return pm_generic_runtime_resume(dev); | |
399 | } | |
400 | #endif /* CONFIG_PM */ | |
401 | ||
402 | static const struct dev_pm_ops ac97_pm = { | |
403 | .suspend = pm_generic_suspend, | |
404 | .resume = pm_generic_resume, | |
405 | .freeze = pm_generic_freeze, | |
406 | .thaw = pm_generic_thaw, | |
407 | .poweroff = pm_generic_poweroff, | |
408 | .restore = pm_generic_restore, | |
409 | SET_RUNTIME_PM_OPS( | |
410 | ac97_pm_runtime_suspend, | |
411 | ac97_pm_runtime_resume, | |
412 | NULL) | |
413 | }; | |
414 | ||
415 | static int ac97_get_enable_clk(struct ac97_codec_device *adev) | |
416 | { | |
417 | int ret; | |
418 | ||
419 | adev->clk = clk_get(&adev->dev, "ac97_clk"); | |
420 | if (IS_ERR(adev->clk)) | |
421 | return PTR_ERR(adev->clk); | |
422 | ||
423 | ret = clk_prepare_enable(adev->clk); | |
424 | if (ret) | |
425 | clk_put(adev->clk); | |
426 | ||
427 | return ret; | |
428 | } | |
429 | ||
430 | static void ac97_put_disable_clk(struct ac97_codec_device *adev) | |
431 | { | |
432 | clk_disable_unprepare(adev->clk); | |
433 | clk_put(adev->clk); | |
434 | } | |
435 | ||
436 | static ssize_t vendor_id_show(struct device *dev, | |
437 | struct device_attribute *attr, char *buf) | |
438 | { | |
439 | struct ac97_codec_device *codec = to_ac97_device(dev); | |
440 | ||
441 | return sprintf(buf, "%08x", codec->vendor_id); | |
442 | } | |
443 | DEVICE_ATTR_RO(vendor_id); | |
444 | ||
445 | static struct attribute *ac97_dev_attrs[] = { | |
446 | &dev_attr_vendor_id.attr, | |
447 | NULL, | |
448 | }; | |
449 | ATTRIBUTE_GROUPS(ac97_dev); | |
450 | ||
451 | static int ac97_bus_match(struct device *dev, struct device_driver *drv) | |
452 | { | |
453 | struct ac97_codec_device *adev = to_ac97_device(dev); | |
454 | struct ac97_codec_driver *adrv = to_ac97_driver(drv); | |
455 | const struct ac97_id *id = adrv->id_table; | |
456 | int i = 0; | |
457 | ||
458 | if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff) | |
459 | return false; | |
460 | ||
461 | do { | |
462 | if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask)) | |
463 | return true; | |
464 | } while (id[i++].id); | |
465 | ||
466 | return false; | |
467 | } | |
468 | ||
469 | static int ac97_bus_probe(struct device *dev) | |
470 | { | |
471 | struct ac97_codec_device *adev = to_ac97_device(dev); | |
472 | struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver); | |
473 | int ret; | |
474 | ||
475 | ret = ac97_get_enable_clk(adev); | |
476 | if (ret) | |
477 | return ret; | |
478 | ||
479 | pm_runtime_get_noresume(dev); | |
480 | pm_runtime_set_active(dev); | |
481 | pm_runtime_enable(dev); | |
482 | ||
483 | ret = adrv->probe(adev); | |
484 | if (ret == 0) | |
485 | return 0; | |
486 | ||
487 | pm_runtime_disable(dev); | |
488 | pm_runtime_set_suspended(dev); | |
489 | pm_runtime_put_noidle(dev); | |
490 | ac97_put_disable_clk(adev); | |
491 | ||
492 | return ret; | |
493 | } | |
494 | ||
495 | static int ac97_bus_remove(struct device *dev) | |
496 | { | |
497 | struct ac97_codec_device *adev = to_ac97_device(dev); | |
498 | struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver); | |
499 | int ret; | |
500 | ||
501 | ret = pm_runtime_get_sync(dev); | |
bd685a96 | 502 | if (ret < 0) |
74426fbf RJ |
503 | return ret; |
504 | ||
505 | ret = adrv->remove(adev); | |
506 | pm_runtime_put_noidle(dev); | |
507 | if (ret == 0) | |
508 | ac97_put_disable_clk(adev); | |
509 | ||
29fc3347 LY |
510 | pm_runtime_disable(dev); |
511 | ||
74426fbf RJ |
512 | return ret; |
513 | } | |
514 | ||
515 | static struct bus_type ac97_bus_type = { | |
516 | .name = "ac97bus", | |
517 | .dev_groups = ac97_dev_groups, | |
518 | .match = ac97_bus_match, | |
519 | .pm = &ac97_pm, | |
520 | .probe = ac97_bus_probe, | |
521 | .remove = ac97_bus_remove, | |
522 | }; | |
523 | ||
524 | static int __init ac97_bus_init(void) | |
525 | { | |
526 | return bus_register(&ac97_bus_type); | |
527 | } | |
528 | subsys_initcall(ac97_bus_init); | |
529 | ||
530 | static void __exit ac97_bus_exit(void) | |
531 | { | |
532 | bus_unregister(&ac97_bus_type); | |
533 | } | |
534 | module_exit(ac97_bus_exit); | |
535 | ||
536 | MODULE_LICENSE("GPL"); | |
537 | MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>"); |