of_i2c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * OF helpers for the I2C API
  3. *
  4. * Copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
  5. *
  6. * Based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/i2c.h>
  14. #include <linux/of.h>
  15. #include <linux/of_i2c.h>
  16. #include <linux/module.h>
  17. struct i2c_driver_device {
  18. char *of_device;
  19. char *i2c_type;
  20. };
  21. static struct i2c_driver_device i2c_devices[] = {
  22. };
  23. static int of_find_i2c_driver(struct device_node *node,
  24. struct i2c_board_info *info)
  25. {
  26. int i, cplen;
  27. const char *compatible;
  28. const char *p;
  29. /* 1. search for exception list entry */
  30. for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) {
  31. if (!of_device_is_compatible(node, i2c_devices[i].of_device))
  32. continue;
  33. if (strlcpy(info->type, i2c_devices[i].i2c_type,
  34. I2C_NAME_SIZE) >= I2C_NAME_SIZE)
  35. return -ENOMEM;
  36. return 0;
  37. }
  38. compatible = of_get_property(node, "compatible", &cplen);
  39. if (!compatible)
  40. return -ENODEV;
  41. /* 2. search for linux,<i2c-type> entry */
  42. p = compatible;
  43. while (cplen > 0) {
  44. if (!strncmp(p, "linux,", 6)) {
  45. p += 6;
  46. if (strlcpy(info->type, p,
  47. I2C_NAME_SIZE) >= I2C_NAME_SIZE)
  48. return -ENOMEM;
  49. return 0;
  50. }
  51. i = strlen(p) + 1;
  52. p += i;
  53. cplen -= i;
  54. }
  55. /* 3. take fist compatible entry and strip manufacturer */
  56. p = strchr(compatible, ',');
  57. if (!p)
  58. return -ENODEV;
  59. p++;
  60. if (strlcpy(info->type, p, I2C_NAME_SIZE) >= I2C_NAME_SIZE)
  61. return -ENOMEM;
  62. return 0;
  63. }
  64. void of_register_i2c_devices(struct i2c_adapter *adap,
  65. struct device_node *adap_node)
  66. {
  67. void *result;
  68. struct device_node *node;
  69. for_each_child_of_node(adap_node, node) {
  70. struct i2c_board_info info = {};
  71. const u32 *addr;
  72. int len;
  73. addr = of_get_property(node, "reg", &len);
  74. if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) {
  75. printk(KERN_ERR
  76. "of-i2c: invalid i2c device entry\n");
  77. continue;
  78. }
  79. info.irq = irq_of_parse_and_map(node, 0);
  80. if (info.irq == NO_IRQ)
  81. info.irq = -1;
  82. if (of_find_i2c_driver(node, &info) < 0) {
  83. irq_dispose_mapping(info.irq);
  84. continue;
  85. }
  86. info.addr = *addr;
  87. request_module(info.type);
  88. result = i2c_new_device(adap, &info);
  89. if (result == NULL) {
  90. printk(KERN_ERR
  91. "of-i2c: Failed to load driver for %s\n",
  92. info.type);
  93. irq_dispose_mapping(info.irq);
  94. continue;
  95. }
  96. }
  97. }
  98. EXPORT_SYMBOL(of_register_i2c_devices);
  99. MODULE_LICENSE("GPL");