of_i2c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/module.h>
  16. struct i2c_driver_device {
  17. char *of_device;
  18. char *i2c_type;
  19. };
  20. static struct i2c_driver_device i2c_devices[] = {
  21. };
  22. static int of_find_i2c_driver(struct device_node *node,
  23. struct i2c_board_info *info)
  24. {
  25. int i, cplen;
  26. const char *compatible;
  27. const char *p;
  28. /* 1. search for exception list entry */
  29. for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) {
  30. if (!of_device_is_compatible(node, i2c_devices[i].of_device))
  31. continue;
  32. if (strlcpy(info->type, i2c_devices[i].i2c_type,
  33. I2C_NAME_SIZE) >= I2C_NAME_SIZE)
  34. return -ENOMEM;
  35. return 0;
  36. }
  37. compatible = of_get_property(node, "compatible", &cplen);
  38. if (!compatible)
  39. return -ENODEV;
  40. /* 2. search for linux,<i2c-type> entry */
  41. p = compatible;
  42. while (cplen > 0) {
  43. if (!strncmp(p, "linux,", 6)) {
  44. p += 6;
  45. if (strlcpy(info->type, p,
  46. I2C_NAME_SIZE) >= I2C_NAME_SIZE)
  47. return -ENOMEM;
  48. return 0;
  49. }
  50. i = strlen(p) + 1;
  51. p += i;
  52. cplen -= i;
  53. }
  54. /* 3. take fist compatible entry and strip manufacturer */
  55. p = strchr(compatible, ',');
  56. if (!p)
  57. return -ENODEV;
  58. p++;
  59. if (strlcpy(info->type, p, I2C_NAME_SIZE) >= I2C_NAME_SIZE)
  60. return -ENOMEM;
  61. return 0;
  62. }
  63. void of_register_i2c_devices(struct i2c_adapter *adap,
  64. struct device_node *adap_node)
  65. {
  66. void *result;
  67. struct device_node *node;
  68. for_each_child_of_node(adap_node, node) {
  69. struct i2c_board_info info = {};
  70. const u32 *addr;
  71. int len;
  72. addr = of_get_property(node, "reg", &len);
  73. if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) {
  74. printk(KERN_ERR
  75. "of-i2c: invalid i2c device entry\n");
  76. continue;
  77. }
  78. info.irq = irq_of_parse_and_map(node, 0);
  79. if (info.irq == NO_IRQ)
  80. info.irq = -1;
  81. if (of_find_i2c_driver(node, &info) < 0) {
  82. irq_dispose_mapping(info.irq);
  83. continue;
  84. }
  85. info.addr = *addr;
  86. request_module(info.type);
  87. result = i2c_new_device(adap, &info);
  88. if (result == NULL) {
  89. printk(KERN_ERR
  90. "of-i2c: Failed to load driver for %s\n",
  91. info.type);
  92. irq_dispose_mapping(info.irq);
  93. continue;
  94. }
  95. }
  96. }
  97. EXPORT_SYMBOL(of_register_i2c_devices);
  98. MODULE_LICENSE("GPL");