aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrant Likely <grant.likely@secretlab.ca>2008-07-25 22:25:13 -0400
committerGrant Likely <grant.likely@secretlab.ca>2008-07-25 22:25:13 -0400
commit3f07af494dfa6de43137dae430431c9fbf929c0c (patch)
treeee204d7cb204fbe287f2626fdeb8926adc7d4889
parent5047887caf1806f31652210df27fb62a7c43f27d (diff)
of: adapt of_find_i2c_driver() to be usable by SPI also
SPI has a similar problem as I2C in that it needs to determine an appropriate modalias value for each device node. This patch adapts the of_i2c of_find_i2c_driver() function to be usable by of_spi also. Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
-rw-r--r--drivers/of/base.c88
-rw-r--r--drivers/of/of_i2c.c64
-rw-r--r--include/linux/of.h1
3 files changed, 92 insertions, 61 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 23ffb7c0caf2..ad8ac1a8af28 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -385,3 +385,91 @@ struct device_node *of_find_matching_node(struct device_node *from,
385 return np; 385 return np;
386} 386}
387EXPORT_SYMBOL(of_find_matching_node); 387EXPORT_SYMBOL(of_find_matching_node);
388
389/**
390 * of_modalias_table: Table of explicit compatible ==> modalias mappings
391 *
392 * This table allows particulare compatible property values to be mapped
393 * to modalias strings. This is useful for busses which do not directly
394 * understand the OF device tree but are populated based on data contained
395 * within the device tree. SPI and I2C are the two current users of this
396 * table.
397 *
398 * In most cases, devices do not need to be listed in this table because
399 * the modalias value can be derived directly from the compatible table.
400 * However, if for any reason a value cannot be derived, then this table
401 * provides a method to override the implicit derivation.
402 *
403 * At the moment, a single table is used for all bus types because it is
404 * assumed that the data size is small and that the compatible values
405 * should already be distinct enough to differentiate between SPI, I2C
406 * and other devices.
407 */
408struct of_modalias_table {
409 char *of_device;
410 char *modalias;
411};
412static struct of_modalias_table of_modalias_table[] = {
413 /* Empty for now; add entries as needed */
414};
415
416/**
417 * of_modalias_node - Lookup appropriate modalias for a device node
418 * @node: pointer to a device tree node
419 * @modalias: Pointer to buffer that modalias value will be copied into
420 * @len: Length of modalias value
421 *
422 * Based on the value of the compatible property, this routine will determine
423 * an appropriate modalias value for a particular device tree node. Three
424 * separate methods are used to derive a modalias value.
425 *
426 * First method is to lookup the compatible value in of_modalias_table.
427 * Second is to look for a "linux,<modalias>" entry in the compatible list
428 * and used that for modalias. Third is to strip off the manufacturer
429 * prefix from the first compatible entry and use the remainder as modalias
430 *
431 * This routine returns 0 on success
432 */
433int of_modalias_node(struct device_node *node, char *modalias, int len)
434{
435 int i, cplen;
436 const char *compatible;
437 const char *p;
438
439 /* 1. search for exception list entry */
440 for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
441 compatible = of_modalias_table[i].of_device;
442 if (!of_device_is_compatible(node, compatible))
443 continue;
444 strlcpy(modalias, of_modalias_table[i].modalias, len);
445 return 0;
446 }
447
448 compatible = of_get_property(node, "compatible", &cplen);
449 if (!compatible)
450 return -ENODEV;
451
452 /* 2. search for linux,<modalias> entry */
453 p = compatible;
454 while (cplen > 0) {
455 if (!strncmp(p, "linux,", 6)) {
456 p += 6;
457 strlcpy(modalias, p, len);
458 return 0;
459 }
460
461 i = strlen(p) + 1;
462 p += i;
463 cplen -= i;
464 }
465
466 /* 3. take first compatible entry and strip manufacturer */
467 p = strchr(compatible, ',');
468 if (!p)
469 return -ENODEV;
470 p++;
471 strlcpy(modalias, p, len);
472 return 0;
473}
474EXPORT_SYMBOL_GPL(of_modalias_node);
475
diff --git a/drivers/of/of_i2c.c b/drivers/of/of_i2c.c
index 344e1b03dd8b..6a98dc8aa30b 100644
--- a/drivers/of/of_i2c.c
+++ b/drivers/of/of_i2c.c
@@ -16,62 +16,6 @@
16#include <linux/of_i2c.h> 16#include <linux/of_i2c.h>
17#include <linux/module.h> 17#include <linux/module.h>
18 18
19struct i2c_driver_device {
20 char *of_device;
21 char *i2c_type;
22};
23
24static struct i2c_driver_device i2c_devices[] = {
25};
26
27static int of_find_i2c_driver(struct device_node *node,
28 struct i2c_board_info *info)
29{
30 int i, cplen;
31 const char *compatible;
32 const char *p;
33
34 /* 1. search for exception list entry */
35 for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) {
36 if (!of_device_is_compatible(node, i2c_devices[i].of_device))
37 continue;
38 if (strlcpy(info->type, i2c_devices[i].i2c_type,
39 I2C_NAME_SIZE) >= I2C_NAME_SIZE)
40 return -ENOMEM;
41
42 return 0;
43 }
44
45 compatible = of_get_property(node, "compatible", &cplen);
46 if (!compatible)
47 return -ENODEV;
48
49 /* 2. search for linux,<i2c-type> entry */
50 p = compatible;
51 while (cplen > 0) {
52 if (!strncmp(p, "linux,", 6)) {
53 p += 6;
54 if (strlcpy(info->type, p,
55 I2C_NAME_SIZE) >= I2C_NAME_SIZE)
56 return -ENOMEM;
57 return 0;
58 }
59
60 i = strlen(p) + 1;
61 p += i;
62 cplen -= i;
63 }
64
65 /* 3. take fist compatible entry and strip manufacturer */
66 p = strchr(compatible, ',');
67 if (!p)
68 return -ENODEV;
69 p++;
70 if (strlcpy(info->type, p, I2C_NAME_SIZE) >= I2C_NAME_SIZE)
71 return -ENOMEM;
72 return 0;
73}
74
75void of_register_i2c_devices(struct i2c_adapter *adap, 19void of_register_i2c_devices(struct i2c_adapter *adap,
76 struct device_node *adap_node) 20 struct device_node *adap_node)
77{ 21{
@@ -83,6 +27,9 @@ void of_register_i2c_devices(struct i2c_adapter *adap,
83 const u32 *addr; 27 const u32 *addr;
84 int len; 28 int len;
85 29
30 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0)
31 continue;
32
86 addr = of_get_property(node, "reg", &len); 33 addr = of_get_property(node, "reg", &len);
87 if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) { 34 if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) {
88 printk(KERN_ERR 35 printk(KERN_ERR
@@ -92,11 +39,6 @@ void of_register_i2c_devices(struct i2c_adapter *adap,
92 39
93 info.irq = irq_of_parse_and_map(node, 0); 40 info.irq = irq_of_parse_and_map(node, 0);
94 41
95 if (of_find_i2c_driver(node, &info) < 0) {
96 irq_dispose_mapping(info.irq);
97 continue;
98 }
99
100 info.addr = *addr; 42 info.addr = *addr;
101 43
102 request_module(info.type); 44 request_module(info.type);
diff --git a/include/linux/of.h b/include/linux/of.h
index 59a61bdc98b6..79886ade070f 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -70,5 +70,6 @@ extern int of_n_addr_cells(struct device_node *np);
70extern int of_n_size_cells(struct device_node *np); 70extern int of_n_size_cells(struct device_node *np);
71extern const struct of_device_id *of_match_node( 71extern const struct of_device_id *of_match_node(
72 const struct of_device_id *matches, const struct device_node *node); 72 const struct of_device_id *matches, const struct device_node *node);
73extern int of_modalias_node(struct device_node *node, char *modalias, int len);
73 74
74#endif /* _LINUX_OF_H */ 75#endif /* _LINUX_OF_H */