vexpress-regulator.c 2.7 KB

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