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)
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,
237 struct device *parent)
239 struct of_device *dev;
241 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
244 memset(dev, 0, sizeof(*dev));
246 dev->node = of_node_get(np);
247 dev->dma_mask = 0xffffffffUL;
248 dev->dev.dma_mask = &dev->dma_mask;
249 dev->dev.parent = parent;
250 dev->dev.bus = &of_platform_bus_type;
251 dev->dev.release = of_release_dev;
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);