vexpress.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright (C) 2012 ARM Limited
  12. */
  13. #define DRVNAME "vexpress-regulator"
  14. #define pr_fmt(fmt) DRVNAME ": " fmt
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/regulator/of_regulator.h>
  22. #include <linux/vexpress.h>
  23. struct vexpress_regulator {
  24. struct regulator_desc desc;
  25. struct regulator_dev *regdev;
  26. struct vexpress_config_func *func;
  27. };
  28. static int vexpress_regulator_get_voltage(struct regulator_dev *regdev)
  29. {
  30. struct vexpress_regulator *reg = rdev_get_drvdata(regdev);
  31. u32 uV;
  32. int err = vexpress_config_read(reg->func, 0, &uV);
  33. return err ? err : uV;
  34. }
  35. static int vexpress_regulator_set_voltage(struct regulator_dev *regdev,
  36. int min_uV, int max_uV, unsigned *selector)
  37. {
  38. struct vexpress_regulator *reg = rdev_get_drvdata(regdev);
  39. return vexpress_config_write(reg->func, 0, min_uV);
  40. }
  41. static struct regulator_ops vexpress_regulator_ops_ro = {
  42. .get_voltage = vexpress_regulator_get_voltage,
  43. };
  44. static struct regulator_ops vexpress_regulator_ops = {
  45. .get_voltage = vexpress_regulator_get_voltage,
  46. .set_voltage = vexpress_regulator_set_voltage,
  47. };
  48. static int vexpress_regulator_probe(struct platform_device *pdev)
  49. {
  50. int err;
  51. struct vexpress_regulator *reg;
  52. struct regulator_init_data *init_data;
  53. struct regulator_config config = { };
  54. reg = devm_kzalloc(&pdev->dev, sizeof(*reg), GFP_KERNEL);
  55. if (!reg) {
  56. err = -ENOMEM;
  57. goto error_kzalloc;
  58. }
  59. reg->func = vexpress_config_func_get_by_dev(&pdev->dev);
  60. if (!reg->func) {
  61. err = -ENXIO;
  62. goto error_get_func;
  63. }
  64. reg->desc.name = dev_name(&pdev->dev);
  65. reg->desc.type = REGULATOR_VOLTAGE;
  66. reg->desc.owner = THIS_MODULE;
  67. reg->desc.continuous_voltage_range = true;
  68. init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node);
  69. if (!init_data) {
  70. err = -EINVAL;
  71. goto error_get_regulator_init_data;
  72. }
  73. init_data->constraints.apply_uV = 0;
  74. if (init_data->constraints.min_uV && init_data->constraints.max_uV)
  75. reg->desc.ops = &vexpress_regulator_ops;
  76. else
  77. reg->desc.ops = &vexpress_regulator_ops_ro;
  78. config.dev = &pdev->dev;
  79. config.init_data = init_data;
  80. config.driver_data = reg;
  81. config.of_node = pdev->dev.of_node;
  82. reg->regdev = regulator_register(&reg->desc, &config);
  83. if (IS_ERR(reg->regdev)) {
  84. err = PTR_ERR(reg->regdev);
  85. goto error_regulator_register;
  86. }
  87. platform_set_drvdata(pdev, reg);
  88. return 0;
  89. error_regulator_register:
  90. error_get_regulator_init_data:
  91. vexpress_config_func_put(reg->func);
  92. error_get_func:
  93. error_kzalloc:
  94. return err;
  95. }
  96. static int vexpress_regulator_remove(struct platform_device *pdev)
  97. {
  98. struct vexpress_regulator *reg = platform_get_drvdata(pdev);
  99. vexpress_config_func_put(reg->func);
  100. regulator_unregister(reg->regdev);
  101. return 0;
  102. }
  103. static struct of_device_id vexpress_regulator_of_match[] = {
  104. { .compatible = "arm,vexpress-volt", },
  105. { }
  106. };
  107. static struct platform_driver vexpress_regulator_driver = {
  108. .probe = vexpress_regulator_probe,
  109. .remove = vexpress_regulator_remove,
  110. .driver = {
  111. .name = DRVNAME,
  112. .owner = THIS_MODULE,
  113. .of_match_table = vexpress_regulator_of_match,
  114. },
  115. };
  116. module_platform_driver(vexpress_regulator_driver);
  117. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  118. MODULE_DESCRIPTION("Versatile Express regulator");
  119. MODULE_LICENSE("GPL");
  120. MODULE_ALIAS("platform:vexpress-regulator");