WIP FPC-III support
[linux/fpc-iii.git] / sound / aoa / soundbus / core.c
blob002fb5bf220b7e81d58e0f9a43dc6450cdaf9cc8
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * soundbus
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 */
8 #include <linux/module.h>
9 #include "soundbus.h"
11 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
12 MODULE_LICENSE("GPL");
13 MODULE_DESCRIPTION("Apple Soundbus");
15 struct soundbus_dev *soundbus_dev_get(struct soundbus_dev *dev)
17 struct device *tmp;
19 if (!dev)
20 return NULL;
21 tmp = get_device(&dev->ofdev.dev);
22 if (tmp)
23 return to_soundbus_device(tmp);
24 else
25 return NULL;
27 EXPORT_SYMBOL_GPL(soundbus_dev_get);
29 void soundbus_dev_put(struct soundbus_dev *dev)
31 if (dev)
32 put_device(&dev->ofdev.dev);
34 EXPORT_SYMBOL_GPL(soundbus_dev_put);
36 static int soundbus_probe(struct device *dev)
38 int error = -ENODEV;
39 struct soundbus_driver *drv;
40 struct soundbus_dev *soundbus_dev;
42 drv = to_soundbus_driver(dev->driver);
43 soundbus_dev = to_soundbus_device(dev);
45 if (!drv->probe)
46 return error;
48 soundbus_dev_get(soundbus_dev);
50 error = drv->probe(soundbus_dev);
51 if (error)
52 soundbus_dev_put(soundbus_dev);
54 return error;
58 static int soundbus_uevent(struct device *dev, struct kobj_uevent_env *env)
60 struct soundbus_dev * soundbus_dev;
61 struct platform_device * of;
62 const char *compat;
63 int retval = 0;
64 int cplen, seen = 0;
66 if (!dev)
67 return -ENODEV;
69 soundbus_dev = to_soundbus_device(dev);
70 if (!soundbus_dev)
71 return -ENODEV;
73 of = &soundbus_dev->ofdev;
75 /* stuff we want to pass to /sbin/hotplug */
76 retval = add_uevent_var(env, "OF_NAME=%pOFn", of->dev.of_node);
77 if (retval)
78 return retval;
80 retval = add_uevent_var(env, "OF_TYPE=%s", of_node_get_device_type(of->dev.of_node));
81 if (retval)
82 return retval;
84 /* Since the compatible field can contain pretty much anything
85 * it's not really legal to split it out with commas. We split it
86 * up using a number of environment variables instead. */
88 compat = of_get_property(of->dev.of_node, "compatible", &cplen);
89 while (compat && cplen > 0) {
90 int tmp = env->buflen;
91 retval = add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
92 if (retval)
93 return retval;
94 compat += env->buflen - tmp;
95 cplen -= env->buflen - tmp;
96 seen += 1;
99 retval = add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
100 if (retval)
101 return retval;
102 retval = add_uevent_var(env, "MODALIAS=%s", soundbus_dev->modalias);
104 return retval;
107 static int soundbus_device_remove(struct device *dev)
109 struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
110 struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
112 if (dev->driver && drv->remove)
113 drv->remove(soundbus_dev);
114 soundbus_dev_put(soundbus_dev);
116 return 0;
119 static void soundbus_device_shutdown(struct device *dev)
121 struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
122 struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
124 if (dev->driver && drv->shutdown)
125 drv->shutdown(soundbus_dev);
128 /* soundbus_dev_attrs is declared in sysfs.c */
129 ATTRIBUTE_GROUPS(soundbus_dev);
130 static struct bus_type soundbus_bus_type = {
131 .name = "aoa-soundbus",
132 .probe = soundbus_probe,
133 .uevent = soundbus_uevent,
134 .remove = soundbus_device_remove,
135 .shutdown = soundbus_device_shutdown,
136 .dev_groups = soundbus_dev_groups,
139 int soundbus_add_one(struct soundbus_dev *dev)
141 static int devcount;
143 /* sanity checks */
144 if (!dev->attach_codec ||
145 !dev->ofdev.dev.of_node ||
146 dev->pcmname ||
147 dev->pcmid != -1) {
148 printk(KERN_ERR "soundbus: adding device failed sanity check!\n");
149 return -EINVAL;
152 dev_set_name(&dev->ofdev.dev, "soundbus:%x", ++devcount);
153 dev->ofdev.dev.bus = &soundbus_bus_type;
154 return of_device_register(&dev->ofdev);
156 EXPORT_SYMBOL_GPL(soundbus_add_one);
158 void soundbus_remove_one(struct soundbus_dev *dev)
160 of_device_unregister(&dev->ofdev);
162 EXPORT_SYMBOL_GPL(soundbus_remove_one);
164 int soundbus_register_driver(struct soundbus_driver *drv)
166 /* initialize common driver fields */
167 drv->driver.name = drv->name;
168 drv->driver.bus = &soundbus_bus_type;
170 /* register with core */
171 return driver_register(&drv->driver);
173 EXPORT_SYMBOL_GPL(soundbus_register_driver);
175 void soundbus_unregister_driver(struct soundbus_driver *drv)
177 driver_unregister(&drv->driver);
179 EXPORT_SYMBOL_GPL(soundbus_unregister_driver);
181 static int __init soundbus_init(void)
183 return bus_register(&soundbus_bus_type);
186 static void __exit soundbus_exit(void)
188 bus_unregister(&soundbus_bus_type);
191 subsys_initcall(soundbus_init);
192 module_exit(soundbus_exit);