vexpress-regulator.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 regmap *regmap;
  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 = regmap_read(reg->regmap, 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 regmap_write(reg->regmap, 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. struct vexpress_regulator *reg;
  51. struct regulator_init_data *init_data;
  52. struct regulator_config config = { };
  53. reg = devm_kzalloc(&pdev->dev, sizeof(*reg), GFP_KERNEL);
  54. if (!reg)
  55. return -ENOMEM;
  56. reg->regmap = devm_regmap_init_vexpress_config(&pdev->dev);
  57. if (IS_ERR(reg->regmap))
  58. return PTR_ERR(reg->regmap);
  59. reg->desc.name = dev_name(&pdev->dev);
  60. reg->desc.type = REGULATOR_VOLTAGE;
  61. reg->desc.owner = THIS_MODULE;
  62. reg->desc.continuous_voltage_range = true;
  63. init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
  64. &reg->desc);
  65. if (!init_data)
  66. return -EINVAL;
  67. init_data->constraints.apply_uV = 0;
  68. if (init_data->constraints.min_uV && init_data->constraints.max_uV)
  69. reg->desc.ops = &vexpress_regulator_ops;
  70. else
  71. reg->desc.ops = &vexpress_regulator_ops_ro;
  72. config.dev = &pdev->dev;
  73. config.init_data = init_data;
  74. config.driver_data = reg;
  75. config.of_node = pdev->dev.of_node;
  76. reg->regdev = devm_regulator_register(&pdev->dev, &reg->desc, &config);
  77. if (IS_ERR(reg->regdev))
  78. return PTR_ERR(reg->regdev);
  79. platform_set_drvdata(pdev, reg);
  80. return 0;
  81. }
  82. static const struct of_device_id vexpress_regulator_of_match[] = {
  83. { .compatible = "arm,vexpress-volt", },
  84. { }
  85. };
  86. MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match);
  87. static struct platform_driver vexpress_regulator_driver = {
  88. .probe = vexpress_regulator_probe,
  89. .driver = {
  90. .name = DRVNAME,
  91. .of_match_table = vexpress_regulator_of_match,
  92. },
  93. };
  94. module_platform_driver(vexpress_regulator_driver);
  95. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  96. MODULE_DESCRIPTION("Versatile Express regulator");
  97. MODULE_LICENSE("GPL");
  98. MODULE_ALIAS("platform:vexpress-regulator");