1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
8 #include <linux/module.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
)
21 tmp
= get_device(&dev
->ofdev
.dev
);
23 return to_soundbus_device(tmp
);
27 EXPORT_SYMBOL_GPL(soundbus_dev_get
);
29 void soundbus_dev_put(struct soundbus_dev
*dev
)
32 put_device(&dev
->ofdev
.dev
);
34 EXPORT_SYMBOL_GPL(soundbus_dev_put
);
36 static int soundbus_probe(struct device
*dev
)
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
);
48 soundbus_dev_get(soundbus_dev
);
50 error
= drv
->probe(soundbus_dev
);
52 soundbus_dev_put(soundbus_dev
);
58 static int soundbus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
60 struct soundbus_dev
* soundbus_dev
;
61 struct platform_device
* of
;
69 soundbus_dev
= to_soundbus_device(dev
);
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
);
80 retval
= add_uevent_var(env
, "OF_TYPE=%s", of_node_get_device_type(of
->dev
.of_node
));
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
);
94 compat
+= env
->buflen
- tmp
;
95 cplen
-= env
->buflen
- tmp
;
99 retval
= add_uevent_var(env
, "OF_COMPATIBLE_N=%d", seen
);
102 retval
= add_uevent_var(env
, "MODALIAS=%s", soundbus_dev
->modalias
);
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
);
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
)
144 if (!dev
->attach_codec
||
145 !dev
->ofdev
.dev
.of_node
||
148 printk(KERN_ERR
"soundbus: adding device failed sanity check!\n");
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
);