1 #include <linux/string.h>
2 #include <linux/kernel.h>
4 #include <linux/of_device.h>
5 #include <linux/of_address.h>
6 #include <linux/of_iommu.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
14 #include <asm/errno.h>
15 #include "of_private.h"
18 * of_match_device - Tell if a struct device matches an of_device_id list
19 * @ids: array of of device match structures to search in
20 * @dev: the of device structure to match against
22 * Used by a driver to check whether an platform_device present in the
23 * system is in its list of supported devices.
25 const struct of_device_id *of_match_device(const struct of_device_id *matches,
26 const struct device *dev)
28 if ((!matches) || (!dev->of_node))
30 return of_match_node(matches, dev->of_node);
32 EXPORT_SYMBOL(of_match_device);
34 struct platform_device *of_dev_get(struct platform_device *dev)
40 tmp = get_device(&dev->dev);
42 return to_platform_device(tmp);
46 EXPORT_SYMBOL(of_dev_get);
48 void of_dev_put(struct platform_device *dev)
51 put_device(&dev->dev);
53 EXPORT_SYMBOL(of_dev_put);
55 int of_device_add(struct platform_device *ofdev)
57 BUG_ON(ofdev->dev.of_node == NULL);
59 /* name and id have to be set so that the platform bus doesn't get
60 * confused on matching */
61 ofdev->name = dev_name(&ofdev->dev);
62 ofdev->id = PLATFORM_DEVID_NONE;
65 * If this device has not binding numa node in devicetree, that is
66 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
67 * device is on the same node as the parent.
69 set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
71 return device_add(&ofdev->dev);
75 * of_dma_configure - Setup DMA configuration
76 * @dev: Device to apply DMA configuration
77 * @np: Pointer to OF node having DMA configuration
79 * Try to get devices's DMA configuration from DT and update it
82 * If platform code needs to use its own special DMA configuration, it
83 * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
84 * to fix up DMA configuration.
86 int of_dma_configure(struct device *dev, struct device_node *np)
88 u64 dma_addr, paddr, size = 0;
92 const struct iommu_ops *iommu;
95 ret = of_dma_get_range(np, &dma_addr, &paddr, &size);
98 * For legacy reasons, we have to assume some devices need
99 * DMA configuration regardless of whether "dma-ranges" is
100 * correctly specified or not.
102 if (!dev->bus->force_dma)
103 return ret == -ENODEV ? 0 : ret;
105 dma_addr = offset = 0;
107 offset = PFN_DOWN(paddr - dma_addr);
110 * Add a work around to treat the size as mask + 1 in case
111 * it is defined in DT as a mask.
114 dev_warn(dev, "Invalid size 0x%llx for dma-range\n",
120 dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
123 dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
127 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
128 * setup the correct supported mask.
130 if (!dev->coherent_dma_mask)
131 dev->coherent_dma_mask = DMA_BIT_MASK(32);
133 * Set it to coherent_dma_mask by default if the architecture
134 * code has not set it.
137 dev->dma_mask = &dev->coherent_dma_mask;
140 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
142 dev->dma_pfn_offset = offset;
145 * Limit coherent and dma mask based on size and default mask
148 mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1);
149 dev->coherent_dma_mask &= mask;
150 *dev->dma_mask &= mask;
152 coherent = of_dma_is_coherent(np);
153 dev_dbg(dev, "device is%sdma coherent\n",
154 coherent ? " " : " not ");
156 iommu = of_iommu_configure(dev, np);
157 if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
158 return -EPROBE_DEFER;
160 dev_dbg(dev, "device is%sbehind an iommu\n",
161 iommu ? " " : " not ");
163 arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent);
167 EXPORT_SYMBOL_GPL(of_dma_configure);
170 * of_dma_deconfigure - Clean up DMA configuration
171 * @dev: Device for which to clean up DMA configuration
173 * Clean up all configuration performed by of_dma_configure_ops() and free all
174 * resources that have been allocated.
176 void of_dma_deconfigure(struct device *dev)
178 arch_teardown_dma_ops(dev);
181 int of_device_register(struct platform_device *pdev)
183 device_initialize(&pdev->dev);
184 return of_device_add(pdev);
186 EXPORT_SYMBOL(of_device_register);
188 void of_device_unregister(struct platform_device *ofdev)
190 device_unregister(&ofdev->dev);
192 EXPORT_SYMBOL(of_device_unregister);
194 const void *of_device_get_match_data(const struct device *dev)
196 const struct of_device_id *match;
198 match = of_match_device(dev->driver->of_match_table, dev);
204 EXPORT_SYMBOL(of_device_get_match_data);
206 static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
214 if ((!dev) || (!dev->of_node))
218 csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
225 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
226 csize = strlen(compat) + 1;
231 csize = snprintf(str, len, "C%s", compat);
244 int of_device_request_module(struct device *dev)
250 size = of_device_get_modalias(dev, NULL, 0);
254 str = kmalloc(size + 1, GFP_KERNEL);
258 of_device_get_modalias(dev, str, size);
260 ret = request_module(str);
265 EXPORT_SYMBOL_GPL(of_device_request_module);
268 * of_device_modalias - Fill buffer with newline terminated modalias string
270 ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
272 ssize_t sl = of_device_get_modalias(dev, str, len - 2);
282 EXPORT_SYMBOL_GPL(of_device_modalias);
285 * of_device_uevent - Display OF related uevent information
287 void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
290 struct alias_prop *app;
294 if ((!dev) || (!dev->of_node))
297 add_uevent_var(env, "OF_NAME=%s", dev->of_node->name);
298 add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
299 if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
300 add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
302 /* Since the compatible field can contain pretty much anything
303 * it's not really legal to split it out with commas. We split it
304 * up using a number of environment variables instead. */
305 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
306 add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
309 add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
312 mutex_lock(&of_mutex);
313 list_for_each_entry(app, &aliases_lookup, link) {
314 if (dev->of_node == app->np) {
315 add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
320 mutex_unlock(&of_mutex);
323 int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
327 if ((!dev) || (!dev->of_node))
330 /* Devicetree modalias is tricky, we add it in 2 steps */
331 if (add_uevent_var(env, "MODALIAS="))
334 sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
335 sizeof(env->buf) - env->buflen);
336 if (sl >= (sizeof(env->buf) - env->buflen))
342 EXPORT_SYMBOL_GPL(of_device_uevent_modalias);