fixed.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * fixed.c
  3. *
  4. * Copyright 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This is useful for systems with mixed controllable and
  14. * non-controllable regulators, as well as for allowing testing on
  15. * systems with no controllable regulators.
  16. */
  17. #include <linux/err.h>
  18. #include <linux/mutex.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/fixed.h>
  22. struct fixed_voltage_data {
  23. struct regulator_desc desc;
  24. struct regulator_dev *dev;
  25. int microvolts;
  26. };
  27. static int fixed_voltage_is_enabled(struct regulator_dev *dev)
  28. {
  29. return 1;
  30. }
  31. static int fixed_voltage_enable(struct regulator_dev *dev)
  32. {
  33. return 0;
  34. }
  35. static int fixed_voltage_get_voltage(struct regulator_dev *dev)
  36. {
  37. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  38. return data->microvolts;
  39. }
  40. static int fixed_voltage_list_voltage(struct regulator_dev *dev,
  41. unsigned selector)
  42. {
  43. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  44. if (selector != 0)
  45. return -EINVAL;
  46. return data->microvolts;
  47. }
  48. static struct regulator_ops fixed_voltage_ops = {
  49. .is_enabled = fixed_voltage_is_enabled,
  50. .enable = fixed_voltage_enable,
  51. .get_voltage = fixed_voltage_get_voltage,
  52. .list_voltage = fixed_voltage_list_voltage,
  53. };
  54. static int regulator_fixed_voltage_probe(struct platform_device *pdev)
  55. {
  56. struct fixed_voltage_config *config = pdev->dev.platform_data;
  57. struct fixed_voltage_data *drvdata;
  58. int ret;
  59. drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL);
  60. if (drvdata == NULL) {
  61. ret = -ENOMEM;
  62. goto err;
  63. }
  64. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  65. if (drvdata->desc.name == NULL) {
  66. ret = -ENOMEM;
  67. goto err;
  68. }
  69. drvdata->desc.type = REGULATOR_VOLTAGE;
  70. drvdata->desc.owner = THIS_MODULE;
  71. drvdata->desc.ops = &fixed_voltage_ops;
  72. drvdata->desc.n_voltages = 1;
  73. drvdata->microvolts = config->microvolts;
  74. drvdata->dev = regulator_register(&drvdata->desc, &pdev->dev,
  75. config->init_data, drvdata);
  76. if (IS_ERR(drvdata->dev)) {
  77. ret = PTR_ERR(drvdata->dev);
  78. goto err_name;
  79. }
  80. platform_set_drvdata(pdev, drvdata);
  81. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  82. drvdata->microvolts);
  83. return 0;
  84. err_name:
  85. kfree(drvdata->desc.name);
  86. err:
  87. kfree(drvdata);
  88. return ret;
  89. }
  90. static int regulator_fixed_voltage_remove(struct platform_device *pdev)
  91. {
  92. struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
  93. regulator_unregister(drvdata->dev);
  94. kfree(drvdata->desc.name);
  95. kfree(drvdata);
  96. return 0;
  97. }
  98. static struct platform_driver regulator_fixed_voltage_driver = {
  99. .probe = regulator_fixed_voltage_probe,
  100. .remove = regulator_fixed_voltage_remove,
  101. .driver = {
  102. .name = "reg-fixed-voltage",
  103. },
  104. };
  105. static int __init regulator_fixed_voltage_init(void)
  106. {
  107. return platform_driver_register(&regulator_fixed_voltage_driver);
  108. }
  109. subsys_initcall(regulator_fixed_voltage_init);
  110. static void __exit regulator_fixed_voltage_exit(void)
  111. {
  112. platform_driver_unregister(&regulator_fixed_voltage_driver);
  113. }
  114. module_exit(regulator_fixed_voltage_exit);
  115. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  116. MODULE_DESCRIPTION("Fixed voltage regulator");
  117. MODULE_LICENSE("GPL");
  118. MODULE_ALIAS("platform:reg-fixed-voltage");