fixed.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 struct regulator_ops fixed_voltage_ops = {
  41. .is_enabled = fixed_voltage_is_enabled,
  42. .enable = fixed_voltage_enable,
  43. .get_voltage = fixed_voltage_get_voltage,
  44. };
  45. static int regulator_fixed_voltage_probe(struct platform_device *pdev)
  46. {
  47. struct fixed_voltage_config *config = pdev->dev.platform_data;
  48. struct fixed_voltage_data *drvdata;
  49. int ret;
  50. drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL);
  51. if (drvdata == NULL) {
  52. ret = -ENOMEM;
  53. goto err;
  54. }
  55. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  56. if (drvdata->desc.name == NULL) {
  57. ret = -ENOMEM;
  58. goto err;
  59. }
  60. drvdata->desc.type = REGULATOR_VOLTAGE;
  61. drvdata->desc.owner = THIS_MODULE;
  62. drvdata->desc.ops = &fixed_voltage_ops,
  63. drvdata->microvolts = config->microvolts;
  64. drvdata->dev = regulator_register(&drvdata->desc, drvdata);
  65. if (IS_ERR(drvdata->dev)) {
  66. ret = PTR_ERR(drvdata->dev);
  67. goto err_name;
  68. }
  69. platform_set_drvdata(pdev, drvdata);
  70. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  71. drvdata->microvolts);
  72. return 0;
  73. err_name:
  74. kfree(drvdata->desc.name);
  75. err:
  76. kfree(drvdata);
  77. return ret;
  78. }
  79. static int regulator_fixed_voltage_remove(struct platform_device *pdev)
  80. {
  81. struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
  82. regulator_unregister(drvdata->dev);
  83. kfree(drvdata->desc.name);
  84. kfree(drvdata);
  85. return 0;
  86. }
  87. static struct platform_driver regulator_fixed_voltage_driver = {
  88. .probe = regulator_fixed_voltage_probe,
  89. .remove = regulator_fixed_voltage_remove,
  90. .driver = {
  91. .name = "reg-fixed-voltage",
  92. },
  93. };
  94. static int __init regulator_fixed_voltage_init(void)
  95. {
  96. return platform_driver_register(&regulator_fixed_voltage_driver);
  97. }
  98. module_init(regulator_fixed_voltage_init);
  99. static void __exit regulator_fixed_voltage_exit(void)
  100. {
  101. platform_driver_unregister(&regulator_fixed_voltage_driver);
  102. }
  103. module_exit(regulator_fixed_voltage_exit);
  104. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  105. MODULE_DESCRIPTION("Fixed voltage regulator");
  106. MODULE_LICENSE("GPL");