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>
8 #include <asm/of_device.h>
11 * of_match_device - Tell if an of_device structure has a matching
13 * @ids: array of of device match structures to search in
14 * @dev: the of device structure to match against
16 * Used by a driver to check whether an of_device present in the
17 * system is in its list of supported devices.
19 const struct of_device_id * of_match_device(const struct of_device_id *matches,
20 const struct of_device *dev)
24 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
27 match &= dev->node->name
28 && !strcmp(matches->name, dev->node->name);
30 match &= dev->node->type
31 && !strcmp(matches->type, dev->node->type);
32 if (matches->compatible[0])
33 match &= device_is_compatible(dev->node,
42 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
44 struct of_device * of_dev = to_of_device(dev);
45 struct of_platform_driver * of_drv = to_of_platform_driver(drv);
46 const struct of_device_id * matches = of_drv->match_table;
51 return of_match_device(matches, of_dev) != NULL;
54 struct of_device *of_dev_get(struct of_device *dev)
60 tmp = get_device(&dev->dev);
62 return to_of_device(tmp);
67 void of_dev_put(struct of_device *dev)
70 put_device(&dev->dev);
74 static int of_device_probe(struct device *dev)
77 struct of_platform_driver *drv;
78 struct of_device *of_dev;
79 const struct of_device_id *match;
81 drv = to_of_platform_driver(dev->driver);
82 of_dev = to_of_device(dev);
89 match = of_match_device(drv->match_table, of_dev);
91 error = drv->probe(of_dev, match);
98 static int of_device_remove(struct device *dev)
100 struct of_device * of_dev = to_of_device(dev);
101 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
103 if (dev->driver && drv->remove)
108 static int of_device_suspend(struct device *dev, pm_message_t state)
110 struct of_device * of_dev = to_of_device(dev);
111 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
114 if (dev->driver && drv->suspend)
115 error = drv->suspend(of_dev, state);
119 static int of_device_resume(struct device * dev)
121 struct of_device * of_dev = to_of_device(dev);
122 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
125 if (dev->driver && drv->resume)
126 error = drv->resume(of_dev);
130 struct bus_type of_platform_bus_type = {
131 .name = "of_platform",
132 .match = of_platform_bus_match,
133 .suspend = of_device_suspend,
134 .resume = of_device_resume,
137 static int __init of_bus_driver_init(void)
139 return bus_register(&of_platform_bus_type);
142 postcore_initcall(of_bus_driver_init);
144 int of_register_driver(struct of_platform_driver *drv)
148 /* initialize common driver fields */
149 drv->driver.name = drv->name;
150 drv->driver.bus = &of_platform_bus_type;
151 drv->driver.probe = of_device_probe;
152 drv->driver.remove = of_device_remove;
154 /* register with core */
155 count = driver_register(&drv->driver);
156 return count ? count : 1;
159 void of_unregister_driver(struct of_platform_driver *drv)
161 driver_unregister(&drv->driver);
165 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
167 struct of_device *ofdev;
169 ofdev = to_of_device(dev);
170 return sprintf(buf, "%s", ofdev->node->full_name);
173 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
176 * of_release_dev - free an of device structure when all users of it are finished.
177 * @dev: device that's been disconnected
179 * Will be called only by the device core when all users of this of device are
182 void of_release_dev(struct device *dev)
184 struct of_device *ofdev;
186 ofdev = to_of_device(dev);
187 of_node_put(ofdev->node);
191 int of_device_register(struct of_device *ofdev)
194 struct of_device **odprop;
196 BUG_ON(ofdev->node == NULL);
198 odprop = (struct of_device **)get_property(ofdev->node, "linux,device", NULL);
200 struct property *new_prop;
202 new_prop = kmalloc(sizeof(struct property) + sizeof(struct of_device *),
204 if (new_prop == NULL)
206 new_prop->name = "linux,device";
207 new_prop->length = sizeof(sizeof(struct of_device *));
208 new_prop->value = (unsigned char *)&new_prop[1];
209 odprop = (struct of_device **)new_prop->value;
211 prom_add_property(ofdev->node, new_prop);
215 rc = device_register(&ofdev->dev);
219 device_create_file(&ofdev->dev, &dev_attr_devspec);
224 void of_device_unregister(struct of_device *ofdev)
226 struct of_device **odprop;
228 device_remove_file(&ofdev->dev, &dev_attr_devspec);
230 odprop = (struct of_device **)get_property(ofdev->node, "linux,device", NULL);
234 device_unregister(&ofdev->dev);
237 struct of_device* of_platform_device_create(struct device_node *np,
239 struct device *parent)
241 struct of_device *dev;
244 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
247 memset(dev, 0, sizeof(*dev));
249 dev->node = of_node_get(np);
250 dev->dma_mask = 0xffffffffUL;
251 dev->dev.dma_mask = &dev->dma_mask;
252 dev->dev.parent = parent;
253 dev->dev.bus = &of_platform_bus_type;
254 dev->dev.release = of_release_dev;
256 reg = (u32 *)get_property(np, "reg", NULL);
257 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
259 if (of_device_register(dev) != 0) {
267 EXPORT_SYMBOL(of_match_device);
268 EXPORT_SYMBOL(of_platform_bus_type);
269 EXPORT_SYMBOL(of_register_driver);
270 EXPORT_SYMBOL(of_unregister_driver);
271 EXPORT_SYMBOL(of_device_register);
272 EXPORT_SYMBOL(of_device_unregister);
273 EXPORT_SYMBOL(of_dev_get);
274 EXPORT_SYMBOL(of_dev_put);
275 EXPORT_SYMBOL(of_platform_device_create);
276 EXPORT_SYMBOL(of_release_dev);