1 #include <linux/string.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/mod_devicetable.h>
6 #include <linux/slab.h>
9 #include <asm/of_device.h>
12 * of_match_device - Tell if an of_device structure has a matching
14 * @ids: array of of device match structures to search in
15 * @dev: the of device structure to match against
17 * Used by a driver to check whether an of_device present in the
18 * system is in its list of supported devices.
20 const struct of_device_id
*of_match_device(const struct of_device_id
*matches
,
21 const struct of_device
*dev
)
25 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
28 match
&= dev
->node
->name
29 && !strcmp(matches
->name
, dev
->node
->name
);
31 match
&= dev
->node
->type
32 && !strcmp(matches
->type
, dev
->node
->type
);
33 if (matches
->compatible
[0])
34 match
&= device_is_compatible(dev
->node
,
43 static int of_platform_bus_match(struct device
*dev
, struct device_driver
*drv
)
45 struct of_device
* of_dev
= to_of_device(dev
);
46 struct of_platform_driver
* of_drv
= to_of_platform_driver(drv
);
47 const struct of_device_id
* matches
= of_drv
->match_table
;
52 return of_match_device(matches
, of_dev
) != NULL
;
55 struct of_device
*of_dev_get(struct of_device
*dev
)
61 tmp
= get_device(&dev
->dev
);
63 return to_of_device(tmp
);
68 void of_dev_put(struct of_device
*dev
)
71 put_device(&dev
->dev
);
75 static int of_device_probe(struct device
*dev
)
78 struct of_platform_driver
*drv
;
79 struct of_device
*of_dev
;
80 const struct of_device_id
*match
;
82 drv
= to_of_platform_driver(dev
->driver
);
83 of_dev
= to_of_device(dev
);
90 match
= of_match_device(drv
->match_table
, of_dev
);
92 error
= drv
->probe(of_dev
, match
);
99 static int of_device_remove(struct device
*dev
)
101 struct of_device
* of_dev
= to_of_device(dev
);
102 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
104 if (dev
->driver
&& drv
->remove
)
109 static int of_device_suspend(struct device
*dev
, pm_message_t state
)
111 struct of_device
* of_dev
= to_of_device(dev
);
112 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
115 if (dev
->driver
&& drv
->suspend
)
116 error
= drv
->suspend(of_dev
, state
);
120 static int of_device_resume(struct device
* dev
)
122 struct of_device
* of_dev
= to_of_device(dev
);
123 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
126 if (dev
->driver
&& drv
->resume
)
127 error
= drv
->resume(of_dev
);
131 struct bus_type of_platform_bus_type
= {
132 .name
= "of_platform",
133 .match
= of_platform_bus_match
,
134 .probe
= of_device_probe
,
135 .remove
= of_device_remove
,
136 .suspend
= of_device_suspend
,
137 .resume
= of_device_resume
,
140 static int __init
of_bus_driver_init(void)
142 return bus_register(&of_platform_bus_type
);
145 postcore_initcall(of_bus_driver_init
);
147 int of_register_driver(struct of_platform_driver
*drv
)
149 /* initialize common driver fields */
150 drv
->driver
.name
= drv
->name
;
151 drv
->driver
.bus
= &of_platform_bus_type
;
153 /* register with core */
154 return driver_register(&drv
->driver
);
157 void of_unregister_driver(struct of_platform_driver
*drv
)
159 driver_unregister(&drv
->driver
);
163 static ssize_t
dev_show_devspec(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
165 struct of_device
*ofdev
;
167 ofdev
= to_of_device(dev
);
168 return sprintf(buf
, "%s", ofdev
->node
->full_name
);
171 static DEVICE_ATTR(devspec
, S_IRUGO
, dev_show_devspec
, NULL
);
174 * of_release_dev - free an of device structure when all users of it are finished.
175 * @dev: device that's been disconnected
177 * Will be called only by the device core when all users of this of device are
180 void of_release_dev(struct device
*dev
)
182 struct of_device
*ofdev
;
184 ofdev
= to_of_device(dev
);
185 of_node_put(ofdev
->node
);
189 int of_device_register(struct of_device
*ofdev
)
193 BUG_ON(ofdev
->node
== NULL
);
195 rc
= device_register(&ofdev
->dev
);
199 device_create_file(&ofdev
->dev
, &dev_attr_devspec
);
204 void of_device_unregister(struct of_device
*ofdev
)
206 device_remove_file(&ofdev
->dev
, &dev_attr_devspec
);
208 device_unregister(&ofdev
->dev
);
211 struct of_device
* of_platform_device_create(struct device_node
*np
,
213 struct device
*parent
)
215 struct of_device
*dev
;
217 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
220 memset(dev
, 0, sizeof(*dev
));
222 dev
->node
= of_node_get(np
);
223 dev
->dma_mask
= 0xffffffffUL
;
224 dev
->dev
.dma_mask
= &dev
->dma_mask
;
225 dev
->dev
.parent
= parent
;
226 dev
->dev
.bus
= &of_platform_bus_type
;
227 dev
->dev
.release
= of_release_dev
;
229 strlcpy(dev
->dev
.bus_id
, bus_id
, BUS_ID_SIZE
);
231 if (of_device_register(dev
) != 0) {
239 EXPORT_SYMBOL(of_match_device
);
240 EXPORT_SYMBOL(of_platform_bus_type
);
241 EXPORT_SYMBOL(of_register_driver
);
242 EXPORT_SYMBOL(of_unregister_driver
);
243 EXPORT_SYMBOL(of_device_register
);
244 EXPORT_SYMBOL(of_device_unregister
);
245 EXPORT_SYMBOL(of_dev_get
);
246 EXPORT_SYMBOL(of_dev_put
);
247 EXPORT_SYMBOL(of_platform_device_create
);
248 EXPORT_SYMBOL(of_release_dev
);