of_regulator.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * OF helpers for regulator framework
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Rajendra Nayak <rnayak@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/of.h>
  14. #include <linux/regulator/machine.h>
  15. static void of_get_regulation_constraints(struct device_node *np,
  16. struct regulator_init_data **init_data)
  17. {
  18. const __be32 *min_uV, *max_uV, *uV_offset;
  19. const __be32 *min_uA, *max_uA;
  20. struct regulation_constraints *constraints = &(*init_data)->constraints;
  21. constraints->name = of_get_property(np, "regulator-name", NULL);
  22. min_uV = of_get_property(np, "regulator-min-microvolt", NULL);
  23. if (min_uV)
  24. constraints->min_uV = be32_to_cpu(*min_uV);
  25. max_uV = of_get_property(np, "regulator-max-microvolt", NULL);
  26. if (max_uV)
  27. constraints->max_uV = be32_to_cpu(*max_uV);
  28. /* Voltage change possible? */
  29. if (constraints->min_uV != constraints->max_uV)
  30. constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  31. uV_offset = of_get_property(np, "regulator-microvolt-offset", NULL);
  32. if (uV_offset)
  33. constraints->uV_offset = be32_to_cpu(*uV_offset);
  34. min_uA = of_get_property(np, "regulator-min-microamp", NULL);
  35. if (min_uA)
  36. constraints->min_uA = be32_to_cpu(*min_uA);
  37. max_uA = of_get_property(np, "regulator-max-microamp", NULL);
  38. if (max_uA)
  39. constraints->max_uA = be32_to_cpu(*max_uA);
  40. /* Current change possible? */
  41. if (constraints->min_uA != constraints->max_uA)
  42. constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
  43. if (of_find_property(np, "regulator-boot-on", NULL))
  44. constraints->boot_on = true;
  45. if (of_find_property(np, "regulator-always-on", NULL))
  46. constraints->always_on = true;
  47. else /* status change should be possible if not always on. */
  48. constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
  49. }
  50. /**
  51. * of_get_regulator_init_data - extract regulator_init_data structure info
  52. * @dev: device requesting for regulator_init_data
  53. *
  54. * Populates regulator_init_data structure by extracting data from device
  55. * tree node, returns a pointer to the populated struture or NULL if memory
  56. * alloc fails.
  57. */
  58. struct regulator_init_data *of_get_regulator_init_data(struct device *dev)
  59. {
  60. struct regulator_init_data *init_data;
  61. if (!dev->of_node)
  62. return NULL;
  63. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  64. if (!init_data)
  65. return NULL; /* Out of memory? */
  66. of_get_regulation_constraints(dev->of_node, &init_data);
  67. return init_data;
  68. }