Merge branch 'for-linus' of git://brick.kernel.dk/data/git/linux-2.6-block
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sparc64 / kernel / of_device.c
blob238bbf6de07d1603b5ab7e225442ef1eabab2177
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>
9 #include <asm/errno.h>
10 #include <asm/of_device.h>
12 /**
13 * of_match_device - Tell if an of_device structure has a matching
14 * of_match structure
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)
24 if (!dev->node)
25 return NULL;
26 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
27 int match = 1;
28 if (matches->name[0])
29 match &= dev->node->name
30 && !strcmp(matches->name, dev->node->name);
31 if (matches->type[0])
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,
36 matches->compatible);
37 if (match)
38 return matches;
39 matches++;
41 return NULL;
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;
50 if (!matches)
51 return 0;
53 return of_match_device(matches, of_dev) != NULL;
56 struct of_device *of_dev_get(struct of_device *dev)
58 struct device *tmp;
60 if (!dev)
61 return NULL;
62 tmp = get_device(&dev->dev);
63 if (tmp)
64 return to_of_device(tmp);
65 else
66 return NULL;
69 void of_dev_put(struct of_device *dev)
71 if (dev)
72 put_device(&dev->dev);
76 static int of_device_probe(struct device *dev)
78 int error = -ENODEV;
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);
86 if (!drv->probe)
87 return error;
89 of_dev_get(of_dev);
91 match = of_match_device(drv->match_table, of_dev);
92 if (match)
93 error = drv->probe(of_dev, match);
94 if (error)
95 of_dev_put(of_dev);
97 return error;
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)
106 drv->remove(of_dev);
107 return 0;
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);
114 int error = 0;
116 if (dev->driver && drv->suspend)
117 error = drv->suspend(of_dev, state);
118 return error;
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);
125 int error = 0;
127 if (dev->driver && drv->resume)
128 error = drv->resume(of_dev);
129 return error;
132 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
134 unsigned long ret = res->start + offset;
136 if (!request_region(ret, size, name))
137 ret = 0;
139 return (void __iomem *) ret;
141 EXPORT_SYMBOL(of_ioremap);
143 void of_iounmap(void __iomem *base, unsigned long size)
145 release_region((unsigned long) base, size);
147 EXPORT_SYMBOL(of_iounmap);
149 static int node_match(struct device *dev, void *data)
151 struct of_device *op = to_of_device(dev);
152 struct device_node *dp = data;
154 return (op->node == dp);
157 struct of_device *of_find_device_by_node(struct device_node *dp)
159 struct device *dev = bus_find_device(&of_bus_type, NULL,
160 dp, node_match);
162 if (dev)
163 return to_of_device(dev);
165 return NULL;
167 EXPORT_SYMBOL(of_find_device_by_node);
169 #ifdef CONFIG_PCI
170 struct bus_type isa_bus_type = {
171 .name = "isa",
172 .match = of_platform_bus_match,
173 .probe = of_device_probe,
174 .remove = of_device_remove,
175 .suspend = of_device_suspend,
176 .resume = of_device_resume,
178 EXPORT_SYMBOL(isa_bus_type);
180 struct bus_type ebus_bus_type = {
181 .name = "ebus",
182 .match = of_platform_bus_match,
183 .probe = of_device_probe,
184 .remove = of_device_remove,
185 .suspend = of_device_suspend,
186 .resume = of_device_resume,
188 EXPORT_SYMBOL(ebus_bus_type);
189 #endif
191 #ifdef CONFIG_SBUS
192 struct bus_type sbus_bus_type = {
193 .name = "sbus",
194 .match = of_platform_bus_match,
195 .probe = of_device_probe,
196 .remove = of_device_remove,
197 .suspend = of_device_suspend,
198 .resume = of_device_resume,
200 EXPORT_SYMBOL(sbus_bus_type);
201 #endif
203 struct bus_type of_bus_type = {
204 .name = "of",
205 .match = of_platform_bus_match,
206 .probe = of_device_probe,
207 .remove = of_device_remove,
208 .suspend = of_device_suspend,
209 .resume = of_device_resume,
211 EXPORT_SYMBOL(of_bus_type);
213 static inline u64 of_read_addr(const u32 *cell, int size)
215 u64 r = 0;
216 while (size--)
217 r = (r << 32) | *(cell++);
218 return r;
221 static void __init get_cells(struct device_node *dp,
222 int *addrc, int *sizec)
224 if (addrc)
225 *addrc = of_n_addr_cells(dp);
226 if (sizec)
227 *sizec = of_n_size_cells(dp);
230 /* Max address size we deal with */
231 #define OF_MAX_ADDR_CELLS 4
233 struct of_bus {
234 const char *name;
235 const char *addr_prop_name;
236 int (*match)(struct device_node *parent);
237 void (*count_cells)(struct device_node *child,
238 int *addrc, int *sizec);
239 int (*map)(u32 *addr, const u32 *range,
240 int na, int ns, int pna);
241 unsigned int (*get_flags)(u32 *addr);
245 * Default translator (generic bus)
248 static void of_bus_default_count_cells(struct device_node *dev,
249 int *addrc, int *sizec)
251 get_cells(dev, addrc, sizec);
254 /* Make sure the least significant 64-bits are in-range. Even
255 * for 3 or 4 cell values it is a good enough approximation.
257 static int of_out_of_range(const u32 *addr, const u32 *base,
258 const u32 *size, int na, int ns)
260 u64 a = of_read_addr(addr, na);
261 u64 b = of_read_addr(base, na);
263 if (a < b)
264 return 1;
266 b += of_read_addr(size, ns);
267 if (a >= b)
268 return 1;
270 return 0;
273 static int of_bus_default_map(u32 *addr, const u32 *range,
274 int na, int ns, int pna)
276 u32 result[OF_MAX_ADDR_CELLS];
277 int i;
279 if (ns > 2) {
280 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
281 return -EINVAL;
284 if (of_out_of_range(addr, range, range + na + pna, na, ns))
285 return -EINVAL;
287 /* Start with the parent range base. */
288 memcpy(result, range + na, pna * 4);
290 /* Add in the child address offset. */
291 for (i = 0; i < na; i++)
292 result[pna - 1 - i] +=
293 (addr[na - 1 - i] -
294 range[na - 1 - i]);
296 memcpy(addr, result, pna * 4);
298 return 0;
301 static unsigned int of_bus_default_get_flags(u32 *addr)
303 return IORESOURCE_MEM;
307 * PCI bus specific translator
310 static int of_bus_pci_match(struct device_node *np)
312 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
313 /* Do not do PCI specific frobbing if the
314 * PCI bridge lacks a ranges property. We
315 * want to pass it through up to the next
316 * parent as-is, not with the PCI translate
317 * method which chops off the top address cell.
319 if (!of_find_property(np, "ranges", NULL))
320 return 0;
322 return 1;
325 return 0;
328 static void of_bus_pci_count_cells(struct device_node *np,
329 int *addrc, int *sizec)
331 if (addrc)
332 *addrc = 3;
333 if (sizec)
334 *sizec = 2;
337 static int of_bus_pci_map(u32 *addr, const u32 *range,
338 int na, int ns, int pna)
340 u32 result[OF_MAX_ADDR_CELLS];
341 int i;
343 /* Check address type match */
344 if ((addr[0] ^ range[0]) & 0x03000000)
345 return -EINVAL;
347 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
348 na - 1, ns))
349 return -EINVAL;
351 /* Start with the parent range base. */
352 memcpy(result, range + na, pna * 4);
354 /* Add in the child address offset, skipping high cell. */
355 for (i = 0; i < na - 1; i++)
356 result[pna - 1 - i] +=
357 (addr[na - 1 - i] -
358 range[na - 1 - i]);
360 memcpy(addr, result, pna * 4);
362 return 0;
365 static unsigned int of_bus_pci_get_flags(u32 *addr)
367 unsigned int flags = 0;
368 u32 w = addr[0];
370 switch((w >> 24) & 0x03) {
371 case 0x01:
372 flags |= IORESOURCE_IO;
373 case 0x02: /* 32 bits */
374 case 0x03: /* 64 bits */
375 flags |= IORESOURCE_MEM;
377 if (w & 0x40000000)
378 flags |= IORESOURCE_PREFETCH;
379 return flags;
383 * SBUS bus specific translator
386 static int of_bus_sbus_match(struct device_node *np)
388 return !strcmp(np->name, "sbus") ||
389 !strcmp(np->name, "sbi");
392 static void of_bus_sbus_count_cells(struct device_node *child,
393 int *addrc, int *sizec)
395 if (addrc)
396 *addrc = 2;
397 if (sizec)
398 *sizec = 1;
401 static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
403 return of_bus_default_map(addr, range, na, ns, pna);
406 static unsigned int of_bus_sbus_get_flags(u32 *addr)
408 return IORESOURCE_MEM;
413 * Array of bus specific translators
416 static struct of_bus of_busses[] = {
417 /* PCI */
419 .name = "pci",
420 .addr_prop_name = "assigned-addresses",
421 .match = of_bus_pci_match,
422 .count_cells = of_bus_pci_count_cells,
423 .map = of_bus_pci_map,
424 .get_flags = of_bus_pci_get_flags,
426 /* SBUS */
428 .name = "sbus",
429 .addr_prop_name = "reg",
430 .match = of_bus_sbus_match,
431 .count_cells = of_bus_sbus_count_cells,
432 .map = of_bus_sbus_map,
433 .get_flags = of_bus_sbus_get_flags,
435 /* Default */
437 .name = "default",
438 .addr_prop_name = "reg",
439 .match = NULL,
440 .count_cells = of_bus_default_count_cells,
441 .map = of_bus_default_map,
442 .get_flags = of_bus_default_get_flags,
446 static struct of_bus *of_match_bus(struct device_node *np)
448 int i;
450 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
451 if (!of_busses[i].match || of_busses[i].match(np))
452 return &of_busses[i];
453 BUG();
454 return NULL;
457 static int __init build_one_resource(struct device_node *parent,
458 struct of_bus *bus,
459 struct of_bus *pbus,
460 u32 *addr,
461 int na, int ns, int pna)
463 u32 *ranges;
464 unsigned int rlen;
465 int rone;
467 ranges = of_get_property(parent, "ranges", &rlen);
468 if (ranges == NULL || rlen == 0) {
469 u32 result[OF_MAX_ADDR_CELLS];
470 int i;
472 memset(result, 0, pna * 4);
473 for (i = 0; i < na; i++)
474 result[pna - 1 - i] =
475 addr[na - 1 - i];
477 memcpy(addr, result, pna * 4);
478 return 0;
481 /* Now walk through the ranges */
482 rlen /= 4;
483 rone = na + pna + ns;
484 for (; rlen >= rone; rlen -= rone, ranges += rone) {
485 if (!bus->map(addr, ranges, na, ns, pna))
486 return 0;
489 return 1;
492 static int __init use_1to1_mapping(struct device_node *pp)
494 char *model;
496 /* If this is on the PMU bus, don't try to translate it even
497 * if a ranges property exists.
499 if (!strcmp(pp->name, "pmu"))
500 return 1;
502 /* If we have a ranges property in the parent, use it. */
503 if (of_find_property(pp, "ranges", NULL) != NULL)
504 return 0;
506 /* If the parent is the dma node of an ISA bus, pass
507 * the translation up to the root.
509 if (!strcmp(pp->name, "dma"))
510 return 0;
512 /* Similarly for Simba PCI bridges. */
513 model = of_get_property(pp, "model", NULL);
514 if (model && !strcmp(model, "SUNW,simba"))
515 return 0;
517 return 1;
520 static int of_resource_verbose;
522 static void __init build_device_resources(struct of_device *op,
523 struct device *parent)
525 struct of_device *p_op;
526 struct of_bus *bus;
527 int na, ns;
528 int index, num_reg;
529 void *preg;
531 if (!parent)
532 return;
534 p_op = to_of_device(parent);
535 bus = of_match_bus(p_op->node);
536 bus->count_cells(op->node, &na, &ns);
538 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
539 if (!preg || num_reg == 0)
540 return;
542 /* Convert to num-cells. */
543 num_reg /= 4;
545 /* Convert to num-entries. */
546 num_reg /= na + ns;
548 /* Prevent overruning the op->resources[] array. */
549 if (num_reg > PROMREG_MAX) {
550 printk(KERN_WARNING "%s: Too many regs (%d), "
551 "limiting to %d.\n",
552 op->node->full_name, num_reg, PROMREG_MAX);
553 num_reg = PROMREG_MAX;
556 for (index = 0; index < num_reg; index++) {
557 struct resource *r = &op->resource[index];
558 u32 addr[OF_MAX_ADDR_CELLS];
559 u32 *reg = (preg + (index * ((na + ns) * 4)));
560 struct device_node *dp = op->node;
561 struct device_node *pp = p_op->node;
562 struct of_bus *pbus;
563 u64 size, result = OF_BAD_ADDR;
564 unsigned long flags;
565 int dna, dns;
566 int pna, pns;
568 size = of_read_addr(reg + na, ns);
569 flags = bus->get_flags(reg);
571 memcpy(addr, reg, na * 4);
573 if (use_1to1_mapping(pp)) {
574 result = of_read_addr(addr, na);
575 goto build_res;
578 dna = na;
579 dns = ns;
581 while (1) {
582 dp = pp;
583 pp = dp->parent;
584 if (!pp) {
585 result = of_read_addr(addr, dna);
586 break;
589 pbus = of_match_bus(pp);
590 pbus->count_cells(dp, &pna, &pns);
592 if (build_one_resource(dp, bus, pbus, addr,
593 dna, dns, pna))
594 break;
596 dna = pna;
597 dns = pns;
598 bus = pbus;
601 build_res:
602 memset(r, 0, sizeof(*r));
604 if (of_resource_verbose)
605 printk("%s reg[%d] -> %lx\n",
606 op->node->full_name, index,
607 result);
609 if (result != OF_BAD_ADDR) {
610 if (tlb_type == hypervisor)
611 result &= 0x0fffffffffffffffUL;
613 r->start = result;
614 r->end = result + size - 1;
615 r->flags = flags;
616 } else {
617 r->start = ~0UL;
618 r->end = ~0UL;
620 r->name = op->node->name;
624 static struct device_node * __init
625 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
626 u32 *imap, int imlen, u32 *imask,
627 unsigned int *irq_p)
629 struct device_node *cp;
630 unsigned int irq = *irq_p;
631 struct of_bus *bus;
632 phandle handle;
633 u32 *reg;
634 int na, num_reg, i;
636 bus = of_match_bus(pp);
637 bus->count_cells(dp, &na, NULL);
639 reg = of_get_property(dp, "reg", &num_reg);
640 if (!reg || !num_reg)
641 return NULL;
643 imlen /= ((na + 3) * 4);
644 handle = 0;
645 for (i = 0; i < imlen; i++) {
646 int j;
648 for (j = 0; j < na; j++) {
649 if ((reg[j] & imask[j]) != imap[j])
650 goto next;
652 if (imap[na] == irq) {
653 handle = imap[na + 1];
654 irq = imap[na + 2];
655 break;
658 next:
659 imap += (na + 3);
661 if (i == imlen) {
662 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
663 * properties that do not include the on-board device
664 * interrupts. Instead, the device's 'interrupts' property
665 * is already a fully specified INO value.
667 * Handle this by deciding that, if we didn't get a
668 * match in the parent's 'interrupt-map', and the
669 * parent is an IRQ translater, then use the parent as
670 * our IRQ controller.
672 if (pp->irq_trans)
673 return pp;
675 return NULL;
678 *irq_p = irq;
679 cp = of_find_node_by_phandle(handle);
681 return cp;
684 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
685 struct device_node *pp,
686 unsigned int irq)
688 struct linux_prom_pci_registers *regs;
689 unsigned int devfn, slot, ret;
691 if (irq < 1 || irq > 4)
692 return irq;
694 regs = of_get_property(dp, "reg", NULL);
695 if (!regs)
696 return irq;
698 devfn = (regs->phys_hi >> 8) & 0xff;
699 slot = (devfn >> 3) & 0x1f;
701 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
703 return ret;
706 static int of_irq_verbose;
708 static unsigned int __init build_one_device_irq(struct of_device *op,
709 struct device *parent,
710 unsigned int irq)
712 struct device_node *dp = op->node;
713 struct device_node *pp, *ip;
714 unsigned int orig_irq = irq;
716 if (irq == 0xffffffff)
717 return irq;
719 if (dp->irq_trans) {
720 irq = dp->irq_trans->irq_build(dp, irq,
721 dp->irq_trans->data);
723 if (of_irq_verbose)
724 printk("%s: direct translate %x --> %x\n",
725 dp->full_name, orig_irq, irq);
727 return irq;
730 /* Something more complicated. Walk up to the root, applying
731 * interrupt-map or bus specific translations, until we hit
732 * an IRQ translator.
734 * If we hit a bus type or situation we cannot handle, we
735 * stop and assume that the original IRQ number was in a
736 * format which has special meaning to it's immediate parent.
738 pp = dp->parent;
739 ip = NULL;
740 while (pp) {
741 void *imap, *imsk;
742 int imlen;
744 imap = of_get_property(pp, "interrupt-map", &imlen);
745 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
746 if (imap && imsk) {
747 struct device_node *iret;
748 int this_orig_irq = irq;
750 iret = apply_interrupt_map(dp, pp,
751 imap, imlen, imsk,
752 &irq);
754 if (of_irq_verbose)
755 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
756 op->node->full_name,
757 pp->full_name, this_orig_irq,
758 (iret ? iret->full_name : "NULL"), irq);
760 if (!iret)
761 break;
763 if (iret->irq_trans) {
764 ip = iret;
765 break;
767 } else {
768 if (!strcmp(pp->type, "pci") ||
769 !strcmp(pp->type, "pciex")) {
770 unsigned int this_orig_irq = irq;
772 irq = pci_irq_swizzle(dp, pp, irq);
773 if (of_irq_verbose)
774 printk("%s: PCI swizzle [%s] "
775 "%x --> %x\n",
776 op->node->full_name,
777 pp->full_name, this_orig_irq,
778 irq);
782 if (pp->irq_trans) {
783 ip = pp;
784 break;
787 dp = pp;
788 pp = pp->parent;
790 if (!ip)
791 return orig_irq;
793 irq = ip->irq_trans->irq_build(op->node, irq,
794 ip->irq_trans->data);
795 if (of_irq_verbose)
796 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
797 op->node->full_name, ip->full_name, orig_irq, irq);
799 return irq;
802 static struct of_device * __init scan_one_device(struct device_node *dp,
803 struct device *parent)
805 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
806 unsigned int *irq;
807 int len, i;
809 if (!op)
810 return NULL;
812 op->node = dp;
814 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
815 (25*1000*1000));
816 op->portid = of_getintprop_default(dp, "upa-portid", -1);
817 if (op->portid == -1)
818 op->portid = of_getintprop_default(dp, "portid", -1);
820 irq = of_get_property(dp, "interrupts", &len);
821 if (irq) {
822 memcpy(op->irqs, irq, len);
823 op->num_irqs = len / 4;
824 } else {
825 op->num_irqs = 0;
828 /* Prevent overruning the op->irqs[] array. */
829 if (op->num_irqs > PROMINTR_MAX) {
830 printk(KERN_WARNING "%s: Too many irqs (%d), "
831 "limiting to %d.\n",
832 dp->full_name, op->num_irqs, PROMINTR_MAX);
833 op->num_irqs = PROMINTR_MAX;
836 build_device_resources(op, parent);
837 for (i = 0; i < op->num_irqs; i++)
838 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
840 op->dev.parent = parent;
841 op->dev.bus = &of_bus_type;
842 if (!parent)
843 strcpy(op->dev.bus_id, "root");
844 else
845 strcpy(op->dev.bus_id, dp->path_component_name);
847 if (of_device_register(op)) {
848 printk("%s: Could not register of device.\n",
849 dp->full_name);
850 kfree(op);
851 op = NULL;
854 return op;
857 static void __init scan_tree(struct device_node *dp, struct device *parent)
859 while (dp) {
860 struct of_device *op = scan_one_device(dp, parent);
862 if (op)
863 scan_tree(dp->child, &op->dev);
865 dp = dp->sibling;
869 static void __init scan_of_devices(void)
871 struct device_node *root = of_find_node_by_path("/");
872 struct of_device *parent;
874 parent = scan_one_device(root, NULL);
875 if (!parent)
876 return;
878 scan_tree(root->child, &parent->dev);
881 static int __init of_bus_driver_init(void)
883 int err;
885 err = bus_register(&of_bus_type);
886 #ifdef CONFIG_PCI
887 if (!err)
888 err = bus_register(&isa_bus_type);
889 if (!err)
890 err = bus_register(&ebus_bus_type);
891 #endif
892 #ifdef CONFIG_SBUS
893 if (!err)
894 err = bus_register(&sbus_bus_type);
895 #endif
897 if (!err)
898 scan_of_devices();
900 return err;
903 postcore_initcall(of_bus_driver_init);
905 static int __init of_debug(char *str)
907 int val = 0;
909 get_option(&str, &val);
910 if (val & 1)
911 of_resource_verbose = 1;
912 if (val & 2)
913 of_irq_verbose = 1;
914 return 1;
917 __setup("of_debug=", of_debug);
919 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
921 /* initialize common driver fields */
922 drv->driver.name = drv->name;
923 drv->driver.bus = bus;
925 /* register with core */
926 return driver_register(&drv->driver);
929 void of_unregister_driver(struct of_platform_driver *drv)
931 driver_unregister(&drv->driver);
935 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
937 struct of_device *ofdev;
939 ofdev = to_of_device(dev);
940 return sprintf(buf, "%s", ofdev->node->full_name);
943 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
946 * of_release_dev - free an of device structure when all users of it are finished.
947 * @dev: device that's been disconnected
949 * Will be called only by the device core when all users of this of device are
950 * done.
952 void of_release_dev(struct device *dev)
954 struct of_device *ofdev;
956 ofdev = to_of_device(dev);
958 kfree(ofdev);
961 int of_device_register(struct of_device *ofdev)
963 int rc;
965 BUG_ON(ofdev->node == NULL);
967 rc = device_register(&ofdev->dev);
968 if (rc)
969 return rc;
971 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
972 if (rc)
973 device_unregister(&ofdev->dev);
975 return rc;
978 void of_device_unregister(struct of_device *ofdev)
980 device_remove_file(&ofdev->dev, &dev_attr_devspec);
981 device_unregister(&ofdev->dev);
984 struct of_device* of_platform_device_create(struct device_node *np,
985 const char *bus_id,
986 struct device *parent,
987 struct bus_type *bus)
989 struct of_device *dev;
991 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
992 if (!dev)
993 return NULL;
994 memset(dev, 0, sizeof(*dev));
996 dev->dev.parent = parent;
997 dev->dev.bus = bus;
998 dev->dev.release = of_release_dev;
1000 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
1002 if (of_device_register(dev) != 0) {
1003 kfree(dev);
1004 return NULL;
1007 return dev;
1010 EXPORT_SYMBOL(of_match_device);
1011 EXPORT_SYMBOL(of_register_driver);
1012 EXPORT_SYMBOL(of_unregister_driver);
1013 EXPORT_SYMBOL(of_device_register);
1014 EXPORT_SYMBOL(of_device_unregister);
1015 EXPORT_SYMBOL(of_dev_get);
1016 EXPORT_SYMBOL(of_dev_put);
1017 EXPORT_SYMBOL(of_platform_device_create);
1018 EXPORT_SYMBOL(of_release_dev);