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>
7 #include <asm/of_device.h>
10 * of_match_device - Tell if an of_device structure has a matching
12 * @ids: array of of device match structures to search in
13 * @dev: the of device structure to match against
15 * Used by a driver to check whether an of_device present in the
16 * system is in its list of supported devices.
18 const struct of_match
* of_match_device(const struct of_match
*matches
,
19 const struct of_device
*dev
)
23 while (matches
->name
|| matches
->type
|| matches
->compatible
) {
25 if (matches
->name
&& matches
->name
!= OF_ANY_MATCH
)
26 match
&= dev
->node
->name
27 && !strcmp(matches
->name
, dev
->node
->name
);
28 if (matches
->type
&& matches
->type
!= OF_ANY_MATCH
)
29 match
&= dev
->node
->type
30 && !strcmp(matches
->type
, dev
->node
->type
);
31 if (matches
->compatible
&& matches
->compatible
!= OF_ANY_MATCH
)
32 match
&= device_is_compatible(dev
->node
,
41 static int of_platform_bus_match(struct device
*dev
, struct device_driver
*drv
)
43 struct of_device
* of_dev
= to_of_device(dev
);
44 struct of_platform_driver
* of_drv
= to_of_platform_driver(drv
);
45 const struct of_match
* matches
= of_drv
->match_table
;
50 return of_match_device(matches
, of_dev
) != NULL
;
53 struct of_device
*of_dev_get(struct of_device
*dev
)
59 tmp
= get_device(&dev
->dev
);
61 return to_of_device(tmp
);
66 void of_dev_put(struct of_device
*dev
)
69 put_device(&dev
->dev
);
73 static int of_device_probe(struct device
*dev
)
76 struct of_platform_driver
*drv
;
77 struct of_device
*of_dev
;
78 const struct of_match
*match
;
80 drv
= to_of_platform_driver(dev
->driver
);
81 of_dev
= to_of_device(dev
);
88 match
= of_match_device(drv
->match_table
, of_dev
);
90 error
= drv
->probe(of_dev
, match
);
97 static int of_device_remove(struct device
*dev
)
99 struct of_device
* of_dev
= to_of_device(dev
);
100 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
102 if (dev
->driver
&& drv
->remove
)
107 static int of_device_suspend(struct device
*dev
, pm_message_t state
)
109 struct of_device
* of_dev
= to_of_device(dev
);
110 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
113 if (dev
->driver
&& drv
->suspend
)
114 error
= drv
->suspend(of_dev
, state
);
118 static int of_device_resume(struct device
* dev
)
120 struct of_device
* of_dev
= to_of_device(dev
);
121 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
124 if (dev
->driver
&& drv
->resume
)
125 error
= drv
->resume(of_dev
);
129 struct bus_type of_platform_bus_type
= {
130 .name
= "of_platform",
131 .match
= of_platform_bus_match
,
132 .suspend
= of_device_suspend
,
133 .resume
= of_device_resume
,
136 static int __init
of_bus_driver_init(void)
138 return bus_register(&of_platform_bus_type
);
141 postcore_initcall(of_bus_driver_init
);
143 int of_register_driver(struct of_platform_driver
*drv
)
147 /* initialize common driver fields */
148 drv
->driver
.name
= drv
->name
;
149 drv
->driver
.bus
= &of_platform_bus_type
;
150 drv
->driver
.probe
= of_device_probe
;
151 drv
->driver
.remove
= of_device_remove
;
153 /* register with core */
154 count
= driver_register(&drv
->driver
);
155 return count
? count
: 1;
158 void of_unregister_driver(struct of_platform_driver
*drv
)
160 driver_unregister(&drv
->driver
);
164 static ssize_t
dev_show_devspec(struct device
*dev
, char *buf
)
166 struct of_device
*ofdev
;
168 ofdev
= to_of_device(dev
);
169 return sprintf(buf
, "%s", ofdev
->node
->full_name
);
172 static DEVICE_ATTR(devspec
, S_IRUGO
, dev_show_devspec
, NULL
);
175 * of_release_dev - free an of device structure when all users of it are finished.
176 * @dev: device that's been disconnected
178 * Will be called only by the device core when all users of this of device are
181 void of_release_dev(struct device
*dev
)
183 struct of_device
*ofdev
;
185 ofdev
= to_of_device(dev
);
189 int of_device_register(struct of_device
*ofdev
)
192 struct of_device
**odprop
;
194 BUG_ON(ofdev
->node
== NULL
);
196 odprop
= (struct of_device
**)get_property(ofdev
->node
, "linux,device", NULL
);
198 struct property
*new_prop
;
200 new_prop
= kmalloc(sizeof(struct property
) + sizeof(struct of_device
*),
202 if (new_prop
== NULL
)
204 new_prop
->name
= "linux,device";
205 new_prop
->length
= sizeof(sizeof(struct of_device
*));
206 new_prop
->value
= (unsigned char *)&new_prop
[1];
207 odprop
= (struct of_device
**)new_prop
->value
;
209 prom_add_property(ofdev
->node
, new_prop
);
213 rc
= device_register(&ofdev
->dev
);
217 device_create_file(&ofdev
->dev
, &dev_attr_devspec
);
222 void of_device_unregister(struct of_device
*ofdev
)
224 struct of_device
**odprop
;
226 device_remove_file(&ofdev
->dev
, &dev_attr_devspec
);
228 odprop
= (struct of_device
**)get_property(ofdev
->node
, "linux,device", NULL
);
232 device_unregister(&ofdev
->dev
);
235 struct of_device
* of_platform_device_create(struct device_node
*np
, const char *bus_id
)
237 struct of_device
*dev
;
240 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
243 memset(dev
, 0, sizeof(*dev
));
246 dev
->dma_mask
= 0xffffffffUL
;
247 dev
->dev
.dma_mask
= &dev
->dma_mask
;
248 dev
->dev
.parent
= NULL
;
249 dev
->dev
.bus
= &of_platform_bus_type
;
250 dev
->dev
.release
= of_release_dev
;
252 reg
= (u32
*)get_property(np
, "reg", NULL
);
253 strlcpy(dev
->dev
.bus_id
, bus_id
, BUS_ID_SIZE
);
255 if (of_device_register(dev
) != 0) {
263 EXPORT_SYMBOL(of_match_device
);
264 EXPORT_SYMBOL(of_platform_bus_type
);
265 EXPORT_SYMBOL(of_register_driver
);
266 EXPORT_SYMBOL(of_unregister_driver
);
267 EXPORT_SYMBOL(of_device_register
);
268 EXPORT_SYMBOL(of_device_unregister
);
269 EXPORT_SYMBOL(of_dev_get
);
270 EXPORT_SYMBOL(of_dev_put
);
271 EXPORT_SYMBOL(of_platform_device_create
);
272 EXPORT_SYMBOL(of_release_dev
);