1 #include <linux/config.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
10 #include <asm/of_device.h>
13 * of_match_device - Tell if an of_device structure has a matching
15 * @ids: array of of device match structures to search in
16 * @dev: the of device structure to match against
18 * Used by a driver to check whether an of_device present in the
19 * system is in its list of supported devices.
21 const struct of_device_id
*of_match_device(const struct of_device_id
*matches
,
22 const struct of_device
*dev
)
26 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
29 match
&= dev
->node
->name
30 && !strcmp(matches
->name
, dev
->node
->name
);
32 match
&= dev
->node
->type
33 && !strcmp(matches
->type
, dev
->node
->type
);
34 if (matches
->compatible
[0])
35 match
&= of_device_is_compatible(dev
->node
,
44 static int of_platform_bus_match(struct device
*dev
, struct device_driver
*drv
)
46 struct of_device
* of_dev
= to_of_device(dev
);
47 struct of_platform_driver
* of_drv
= to_of_platform_driver(drv
);
48 const struct of_device_id
* matches
= of_drv
->match_table
;
53 return of_match_device(matches
, of_dev
) != NULL
;
56 struct of_device
*of_dev_get(struct of_device
*dev
)
62 tmp
= get_device(&dev
->dev
);
64 return to_of_device(tmp
);
69 void of_dev_put(struct of_device
*dev
)
72 put_device(&dev
->dev
);
76 static int of_device_probe(struct device
*dev
)
79 struct of_platform_driver
*drv
;
80 struct of_device
*of_dev
;
81 const struct of_device_id
*match
;
83 drv
= to_of_platform_driver(dev
->driver
);
84 of_dev
= to_of_device(dev
);
91 match
= of_match_device(drv
->match_table
, of_dev
);
93 error
= drv
->probe(of_dev
, match
);
100 static int of_device_remove(struct device
*dev
)
102 struct of_device
* of_dev
= to_of_device(dev
);
103 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
105 if (dev
->driver
&& drv
->remove
)
110 static int of_device_suspend(struct device
*dev
, pm_message_t state
)
112 struct of_device
* of_dev
= to_of_device(dev
);
113 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
116 if (dev
->driver
&& drv
->suspend
)
117 error
= drv
->suspend(of_dev
, state
);
121 static int of_device_resume(struct device
* dev
)
123 struct of_device
* of_dev
= to_of_device(dev
);
124 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
127 if (dev
->driver
&& drv
->resume
)
128 error
= drv
->resume(of_dev
);
133 struct bus_type ebus_bus_type
= {
135 .match
= of_platform_bus_match
,
136 .probe
= of_device_probe
,
137 .remove
= of_device_remove
,
138 .suspend
= of_device_suspend
,
139 .resume
= of_device_resume
,
144 struct bus_type sbus_bus_type
= {
146 .match
= of_platform_bus_match
,
147 .probe
= of_device_probe
,
148 .remove
= of_device_remove
,
149 .suspend
= of_device_suspend
,
150 .resume
= of_device_resume
,
154 static int __init
of_bus_driver_init(void)
160 err
= bus_register(&ebus_bus_type
);
164 err
= bus_register(&sbus_bus_type
);
169 postcore_initcall(of_bus_driver_init
);
171 int of_register_driver(struct of_platform_driver
*drv
, struct bus_type
*bus
)
173 /* initialize common driver fields */
174 drv
->driver
.name
= drv
->name
;
175 drv
->driver
.bus
= bus
;
177 /* register with core */
178 return driver_register(&drv
->driver
);
181 void of_unregister_driver(struct of_platform_driver
*drv
)
183 driver_unregister(&drv
->driver
);
187 static ssize_t
dev_show_devspec(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
189 struct of_device
*ofdev
;
191 ofdev
= to_of_device(dev
);
192 return sprintf(buf
, "%s", ofdev
->node
->full_name
);
195 static DEVICE_ATTR(devspec
, S_IRUGO
, dev_show_devspec
, NULL
);
198 * of_release_dev - free an of device structure when all users of it are finished.
199 * @dev: device that's been disconnected
201 * Will be called only by the device core when all users of this of device are
204 void of_release_dev(struct device
*dev
)
206 struct of_device
*ofdev
;
208 ofdev
= to_of_device(dev
);
213 int of_device_register(struct of_device
*ofdev
)
217 BUG_ON(ofdev
->node
== NULL
);
219 rc
= device_register(&ofdev
->dev
);
223 device_create_file(&ofdev
->dev
, &dev_attr_devspec
);
228 void of_device_unregister(struct of_device
*ofdev
)
230 device_remove_file(&ofdev
->dev
, &dev_attr_devspec
);
231 device_unregister(&ofdev
->dev
);
234 struct of_device
* of_platform_device_create(struct device_node
*np
,
236 struct device
*parent
,
237 struct bus_type
*bus
)
239 struct of_device
*dev
;
241 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
244 memset(dev
, 0, sizeof(*dev
));
246 dev
->dev
.parent
= parent
;
248 dev
->dev
.release
= of_release_dev
;
250 strlcpy(dev
->dev
.bus_id
, bus_id
, BUS_ID_SIZE
);
252 if (of_device_register(dev
) != 0) {
260 EXPORT_SYMBOL(of_match_device
);
261 EXPORT_SYMBOL(of_register_driver
);
262 EXPORT_SYMBOL(of_unregister_driver
);
263 EXPORT_SYMBOL(of_device_register
);
264 EXPORT_SYMBOL(of_device_unregister
);
265 EXPORT_SYMBOL(of_dev_get
);
266 EXPORT_SYMBOL(of_dev_put
);
267 EXPORT_SYMBOL(of_platform_device_create
);
268 EXPORT_SYMBOL(of_release_dev
);