fixed.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, &pdev->dev,
  65. config->init_data, drvdata);
  66. if (IS_ERR(drvdata->dev)) {
  67. ret = PTR_ERR(drvdata->dev);
  68. goto err_name;
  69. }
  70. platform_set_drvdata(pdev, drvdata);
  71. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  72. drvdata->microvolts);
  73. return 0;
  74. err_name:
  75. kfree(drvdata->desc.name);
  76. err:
  77. kfree(drvdata);
  78. return ret;
  79. }
  80. static int regulator_fixed_voltage_remove(struct platform_device *pdev)
  81. {
  82. struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
  83. regulator_unregister(drvdata->dev);
  84. kfree(drvdata->desc.name);
  85. kfree(drvdata);
  86. return 0;
  87. }
  88. static struct platform_driver regulator_fixed_voltage_driver = {
  89. .probe = regulator_fixed_voltage_probe,
  90. .remove = regulator_fixed_voltage_remove,
  91. .driver = {
  92. .name = "reg-fixed-voltage",
  93. },
  94. };
  95. static int __init regulator_fixed_voltage_init(void)
  96. {
  97. return platform_driver_register(&regulator_fixed_voltage_driver);
  98. }
  99. module_init(regulator_fixed_voltage_init);
  100. static void __exit regulator_fixed_voltage_exit(void)
  101. {
  102. platform_driver_unregister(&regulator_fixed_voltage_driver);
  103. }
  104. module_exit(regulator_fixed_voltage_exit);
  105. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  106. MODULE_DESCRIPTION("Fixed voltage regulator");
  107. MODULE_LICENSE("GPL");