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