blob: 25bddf9c9fe1c6af37c1dcaa21ca6d1d1f52d701 [file] [log] [blame]
Stephen Rothwellf85ff302007-05-01 16:40:36 +10001#include <linux/string.h>
2#include <linux/kernel.h>
3#include <linux/of.h>
Stephen Rothwellf898f8d2007-05-01 16:49:51 +10004#include <linux/of_device.h>
Murali Karicheri1f5c69a2015-03-03 12:52:09 -05005#include <linux/of_address.h>
6#include <linux/of_iommu.h>
7#include <linux/dma-mapping.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +10008#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/mod_devicetable.h>
11#include <linux/slab.h>
Robin Murphy72328882017-08-31 11:32:54 +010012#include <linux/platform_device.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +100013
14#include <asm/errno.h>
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080015#include "of_private.h"
Stephen Rothwellf85ff302007-05-01 16:40:36 +100016
17/**
Grant Likely44504b22010-04-13 16:13:22 -070018 * of_match_device - Tell if a struct device matches an of_device_id list
Stephen Rothwellf85ff302007-05-01 16:40:36 +100019 * @ids: array of of device match structures to search in
20 * @dev: the of device structure to match against
21 *
Grant Likely2dc11582010-08-06 09:25:50 -060022 * Used by a driver to check whether an platform_device present in the
Stephen Rothwellf85ff302007-05-01 16:40:36 +100023 * system is in its list of supported devices.
24 */
25const struct of_device_id *of_match_device(const struct of_device_id *matches,
Grant Likely44504b22010-04-13 16:13:22 -070026 const struct device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100027{
Grant Likely8cec0e72010-06-08 07:48:17 -060028 if ((!matches) || (!dev->of_node))
Stephen Rothwellf85ff302007-05-01 16:40:36 +100029 return NULL;
Grant Likely44504b22010-04-13 16:13:22 -070030 return of_match_node(matches, dev->of_node);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100031}
32EXPORT_SYMBOL(of_match_device);
33
Grant Likely94a0cb12010-07-22 13:59:23 -060034struct platform_device *of_dev_get(struct platform_device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100035{
36 struct device *tmp;
37
38 if (!dev)
39 return NULL;
40 tmp = get_device(&dev->dev);
41 if (tmp)
Grant Likely94a0cb12010-07-22 13:59:23 -060042 return to_platform_device(tmp);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100043 else
44 return NULL;
45}
46EXPORT_SYMBOL(of_dev_get);
47
Grant Likely94a0cb12010-07-22 13:59:23 -060048void of_dev_put(struct platform_device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100049{
50 if (dev)
51 put_device(&dev->dev);
52}
53EXPORT_SYMBOL(of_dev_put);
54
Grant Likely7096d042010-10-20 11:45:13 -060055int of_device_add(struct platform_device *ofdev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100056{
Grant Likely61c7a082010-04-13 16:12:29 -070057 BUG_ON(ofdev->dev.of_node == NULL);
Jeremy Kerr6098e2e2008-10-26 21:51:25 +000058
Grant Likelyeca39302010-06-08 07:48:21 -060059 /* 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);
Andy Shevchenko6d7e3bf82017-08-25 18:33:13 +030062 ofdev->id = PLATFORM_DEVID_NONE;
Grant Likelyeca39302010-06-08 07:48:21 -060063
Zhen Lei56f2de82015-08-25 12:08:22 +080064 /*
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.
68 */
69 set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
Jeremy Kerr6098e2e2008-10-26 21:51:25 +000070
71 return device_add(&ofdev->dev);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100072}
Grant Likely7096d042010-10-20 11:45:13 -060073
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050074/**
75 * of_dma_configure - Setup DMA configuration
76 * @dev: Device to apply DMA configuration
77 * @np: Pointer to OF node having DMA configuration
78 *
79 * Try to get devices's DMA configuration from DT and update it
80 * accordingly.
81 *
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.
85 */
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +053086int of_dma_configure(struct device *dev, struct device_node *np)
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050087{
Robin Murphy72328882017-08-31 11:32:54 +010088 u64 dma_addr, paddr, size = 0;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050089 int ret;
90 bool coherent;
91 unsigned long offset;
Robin Murphy53c92d72016-04-07 18:42:05 +010092 const struct iommu_ops *iommu;
Robin Murphyee7b1f32017-08-11 17:29:56 +010093 u64 mask;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050094
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050095 ret = of_dma_get_range(np, &dma_addr, &paddr, &size);
96 if (ret < 0) {
Robin Murphy72328882017-08-31 11:32:54 +010097 /*
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.
101 */
Robin Murphyd89e2372017-10-12 16:56:14 +0100102 if (!dev->bus->force_dma)
Robin Murphy72328882017-08-31 11:32:54 +0100103 return ret == -ENODEV ? 0 : ret;
104
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500105 dma_addr = offset = 0;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500106 } else {
107 offset = PFN_DOWN(paddr - dma_addr);
Murali Karicheri0c79c812015-03-03 12:52:10 -0500108
109 /*
110 * Add a work around to treat the size as mask + 1 in case
111 * it is defined in DT as a mask.
112 */
113 if (size & 1) {
114 dev_warn(dev, "Invalid size 0x%llx for dma-range\n",
115 size);
116 size = size + 1;
117 }
118
119 if (!size) {
120 dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530121 return -EINVAL;
Murali Karicheri0c79c812015-03-03 12:52:10 -0500122 }
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500123 dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
124 }
125
Robin Murphy72328882017-08-31 11:32:54 +0100126 /*
127 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
128 * setup the correct supported mask.
129 */
130 if (!dev->coherent_dma_mask)
131 dev->coherent_dma_mask = DMA_BIT_MASK(32);
132 /*
133 * Set it to coherent_dma_mask by default if the architecture
134 * code has not set it.
135 */
136 if (!dev->dma_mask)
137 dev->dma_mask = &dev->coherent_dma_mask;
138
139 if (!size)
140 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
141
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500142 dev->dma_pfn_offset = offset;
143
Murali Karicheri9a6d7292015-03-03 14:44:57 -0600144 /*
145 * Limit coherent and dma mask based on size and default mask
146 * set by the driver.
147 */
Robin Murphyee7b1f32017-08-11 17:29:56 +0100148 mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1);
149 dev->coherent_dma_mask &= mask;
150 *dev->dma_mask &= mask;
Murali Karicheri9a6d7292015-03-03 14:44:57 -0600151
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500152 coherent = of_dma_is_coherent(np);
153 dev_dbg(dev, "device is%sdma coherent\n",
154 coherent ? " " : " not ");
155
156 iommu = of_iommu_configure(dev, np);
Sricharan Ra37b19a2017-05-27 19:17:41 +0530157 if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
158 return -EPROBE_DEFER;
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530159
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500160 dev_dbg(dev, "device is%sbehind an iommu\n",
161 iommu ? " " : " not ");
162
163 arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent);
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530164
165 return 0;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500166}
167EXPORT_SYMBOL_GPL(of_dma_configure);
168
Laurent Pinchart3f186672017-04-10 16:50:58 +0530169/**
170 * of_dma_deconfigure - Clean up DMA configuration
171 * @dev: Device for which to clean up DMA configuration
172 *
173 * Clean up all configuration performed by of_dma_configure_ops() and free all
174 * resources that have been allocated.
175 */
176void of_dma_deconfigure(struct device *dev)
177{
178 arch_teardown_dma_ops(dev);
179}
180
Grant Likely7096d042010-10-20 11:45:13 -0600181int of_device_register(struct platform_device *pdev)
182{
183 device_initialize(&pdev->dev);
184 return of_device_add(pdev);
185}
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000186EXPORT_SYMBOL(of_device_register);
187
Grant Likely94a0cb12010-07-22 13:59:23 -0600188void of_device_unregister(struct platform_device *ofdev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000189{
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000190 device_unregister(&ofdev->dev);
191}
192EXPORT_SYMBOL(of_device_unregister);
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000193
Joachim Eastwood3386e0f2015-05-06 20:09:09 +0200194const void *of_device_get_match_data(const struct device *dev)
195{
196 const struct of_device_id *match;
197
198 match = of_match_device(dev->driver->of_match_table, dev);
199 if (!match)
200 return NULL;
201
202 return match->data;
203}
204EXPORT_SYMBOL(of_device_get_match_data);
205
Rob Herring0634c292017-03-22 09:16:27 -0500206static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000207{
208 const char *compat;
Rob Herringbc575062017-07-21 15:45:32 -0500209 char *c;
210 struct property *p;
211 ssize_t csize;
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700212 ssize_t tsize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000213
Zhang Ruib9f73062014-01-14 16:46:38 +0800214 if ((!dev) || (!dev->of_node))
215 return -ENODEV;
216
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000217 /* Name & Type */
Grant Likely34a1c1e2010-06-08 07:48:13 -0600218 csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
219 dev->of_node->type);
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700220 tsize = csize;
Rob Herringbc575062017-07-21 15:45:32 -0500221 len -= csize;
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700222 if (str)
223 str += csize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000224
Rob Herringbc575062017-07-21 15:45:32 -0500225 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700226 csize = strlen(compat) + 1;
227 tsize += csize;
228 if (csize > len)
229 continue;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000230
Rob Herringbc575062017-07-21 15:45:32 -0500231 csize = snprintf(str, len, "C%s", compat);
232 for (c = str; c; ) {
233 c = strchr(c, ' ');
234 if (c)
235 *c++ = '_';
236 }
237 len -= csize;
238 str += csize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000239 }
240
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700241 return tsize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000242}
Grant Likelydd27dcd2010-06-08 07:48:12 -0600243
Stephen Boyd9c829c02016-12-28 14:56:47 -0800244int of_device_request_module(struct device *dev)
245{
246 char *str;
247 ssize_t size;
248 int ret;
249
250 size = of_device_get_modalias(dev, NULL, 0);
251 if (size < 0)
252 return size;
253
254 str = kmalloc(size + 1, GFP_KERNEL);
255 if (!str)
256 return -ENOMEM;
257
258 of_device_get_modalias(dev, str, size);
259 str[size] = '\0';
260 ret = request_module(str);
261 kfree(str);
262
263 return ret;
264}
265EXPORT_SYMBOL_GPL(of_device_request_module);
266
Grant Likelydd27dcd2010-06-08 07:48:12 -0600267/**
Rob Herring0634c292017-03-22 09:16:27 -0500268 * of_device_modalias - Fill buffer with newline terminated modalias string
269 */
270ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
271{
272 ssize_t sl = of_device_get_modalias(dev, str, len - 2);
273 if (sl < 0)
274 return sl;
Bjorn Andersson08ab58d2017-08-23 18:04:04 -0700275 if (sl > len - 2)
276 return -ENOMEM;
Rob Herring0634c292017-03-22 09:16:27 -0500277
278 str[sl++] = '\n';
279 str[sl] = 0;
280 return sl;
281}
282EXPORT_SYMBOL_GPL(of_device_modalias);
283
284/**
Grant Likelydd27dcd2010-06-08 07:48:12 -0600285 * of_device_uevent - Display OF related uevent information
286 */
Grant Likely07d57a32012-02-01 11:22:22 -0700287void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Grant Likelydd27dcd2010-06-08 07:48:12 -0600288{
289 const char *compat;
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800290 struct alias_prop *app;
Rob Herringbc575062017-07-21 15:45:32 -0500291 struct property *p;
292 int seen = 0;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600293
294 if ((!dev) || (!dev->of_node))
Grant Likely07d57a32012-02-01 11:22:22 -0700295 return;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600296
Grant Likely07d57a32012-02-01 11:22:22 -0700297 add_uevent_var(env, "OF_NAME=%s", dev->of_node->name);
Rob Herring0d638a02017-06-01 15:50:55 -0500298 add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
Grant Likely07d57a32012-02-01 11:22:22 -0700299 if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
300 add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
Grant Likelydd27dcd2010-06-08 07:48:12 -0600301
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. */
Rob Herringbc575062017-07-21 15:45:32 -0500305 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
Grant Likely07d57a32012-02-01 11:22:22 -0700306 add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
Grant Likelydd27dcd2010-06-08 07:48:12 -0600307 seen++;
308 }
Grant Likely07d57a32012-02-01 11:22:22 -0700309 add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800310
311 seen = 0;
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300312 mutex_lock(&of_mutex);
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800313 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,
316 app->alias);
317 seen++;
318 }
319 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300320 mutex_unlock(&of_mutex);
Grant Likely07d57a32012-02-01 11:22:22 -0700321}
Grant Likelydd27dcd2010-06-08 07:48:12 -0600322
Grant Likely07d57a32012-02-01 11:22:22 -0700323int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
324{
325 int sl;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600326
Grant Likely07d57a32012-02-01 11:22:22 -0700327 if ((!dev) || (!dev->of_node))
328 return -ENODEV;
329
330 /* Devicetree modalias is tricky, we add it in 2 steps */
Grant Likelydd27dcd2010-06-08 07:48:12 -0600331 if (add_uevent_var(env, "MODALIAS="))
332 return -ENOMEM;
333
Grant Likely34a1c1e2010-06-08 07:48:13 -0600334 sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
Grant Likelydd27dcd2010-06-08 07:48:12 -0600335 sizeof(env->buf) - env->buflen);
336 if (sl >= (sizeof(env->buf) - env->buflen))
337 return -ENOMEM;
338 env->buflen += sl;
339
340 return 0;
341}
Stephen Boyd7a3b7cd2016-12-28 14:56:48 -0800342EXPORT_SYMBOL_GPL(of_device_uevent_modalias);