vexpress-regulator.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2012 ARM Limited
  4. #define DRVNAME "vexpress-regulator"
  5. #define pr_fmt(fmt) DRVNAME ": " fmt
  6. #include <linux/device.h>
  7. #include <linux/err.h>
  8. #include <linux/module.h>
  9. #include <linux/of_device.h>
  10. #include <linux/regulator/driver.h>
  11. #include <linux/regulator/machine.h>
  12. #include <linux/regulator/of_regulator.h>
  13. #include <linux/vexpress.h>
  14. static int vexpress_regulator_get_voltage(struct regulator_dev *regdev)
  15. {
  16. unsigned int uV;
  17. int err = regmap_read(regdev->regmap, 0, &uV);
  18. return err ? err : uV;
  19. }
  20. static int vexpress_regulator_set_voltage(struct regulator_dev *regdev,
  21. int min_uV, int max_uV, unsigned *selector)
  22. {
  23. return regmap_write(regdev->regmap, 0, min_uV);
  24. }
  25. static const struct regulator_ops vexpress_regulator_ops_ro = {
  26. .get_voltage = vexpress_regulator_get_voltage,
  27. };
  28. static const struct regulator_ops vexpress_regulator_ops = {
  29. .get_voltage = vexpress_regulator_get_voltage,
  30. .set_voltage = vexpress_regulator_set_voltage,
  31. };
  32. static int vexpress_regulator_probe(struct platform_device *pdev)
  33. {
  34. struct regulator_desc *desc;
  35. struct regulator_init_data *init_data;
  36. struct regulator_config config = { };
  37. struct regulator_dev *rdev;
  38. struct regmap *regmap;
  39. desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
  40. if (!desc)
  41. return -ENOMEM;
  42. regmap = devm_regmap_init_vexpress_config(&pdev->dev);
  43. if (IS_ERR(regmap))
  44. return PTR_ERR(regmap);
  45. desc->name = dev_name(&pdev->dev);
  46. desc->type = REGULATOR_VOLTAGE;
  47. desc->owner = THIS_MODULE;
  48. desc->continuous_voltage_range = true;
  49. init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
  50. desc);
  51. if (!init_data)
  52. return -EINVAL;
  53. init_data->constraints.apply_uV = 0;
  54. if (init_data->constraints.min_uV && init_data->constraints.max_uV)
  55. desc->ops = &vexpress_regulator_ops;
  56. else
  57. desc->ops = &vexpress_regulator_ops_ro;
  58. config.regmap = regmap;
  59. config.dev = &pdev->dev;
  60. config.init_data = init_data;
  61. config.of_node = pdev->dev.of_node;
  62. rdev = devm_regulator_register(&pdev->dev, desc, &config);
  63. return PTR_ERR_OR_ZERO(rdev);
  64. }
  65. static const struct of_device_id vexpress_regulator_of_match[] = {
  66. { .compatible = "arm,vexpress-volt", },
  67. { }
  68. };
  69. MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match);
  70. static struct platform_driver vexpress_regulator_driver = {
  71. .probe = vexpress_regulator_probe,
  72. .driver = {
  73. .name = DRVNAME,
  74. .of_match_table = vexpress_regulator_of_match,
  75. },
  76. };
  77. module_platform_driver(vexpress_regulator_driver);
  78. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  79. MODULE_DESCRIPTION("Versatile Express regulator");
  80. MODULE_LICENSE("GPL");
  81. MODULE_ALIAS("platform:vexpress-regulator");