1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Rockchip Electronics Co., Ltd
5 * Based on kernel drivers/regulator/pwm-regulator.c
6 * Copyright (C) 2014 - STMicroelectronics Inc.
7 * Author: Lee Jones <lee.jones@linaro.org>
14 #include <power/regulator.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 struct pwm_regulator_info {
19 /* pwm id corresponding to the PWM driver */
21 /* the period of one PWM cycle */
24 * the polarity of one PWM
26 * 1: inverted polarity
30 /* initialize voltage of regulator */
32 /* the maximum voltage of regulator */
34 /* the minimum voltage of regulator */
36 /* the current voltage of regulator */
40 static int pwm_regulator_enable(struct udevice *dev, bool enable)
42 struct pwm_regulator_info *priv = dev_get_priv(dev);
44 return pwm_set_enable(priv->pwm, priv->pwm_id, enable);
47 static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
49 struct pwm_regulator_info *priv = dev_get_priv(dev);
50 int min_uV = priv->min_voltage;
51 int max_uV = priv->max_voltage;
52 int diff = max_uV - min_uV;
54 return ((req_uV * 100) - (min_uV * 100)) / diff;
57 static int pwm_regulator_get_voltage(struct udevice *dev)
59 struct pwm_regulator_info *priv = dev_get_priv(dev);
64 static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
66 struct pwm_regulator_info *priv = dev_get_priv(dev);
70 duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
72 ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity);
74 dev_err(dev, "Failed to init PWM\n");
78 ret = pwm_set_config(priv->pwm, priv->pwm_id,
79 priv->period_ns, (priv->period_ns / 100) * duty_cycle);
81 dev_err(dev, "Failed to configure PWM\n");
85 priv->volt_uV = uvolt;
90 static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
92 struct pwm_regulator_info *priv = dev_get_priv(dev);
93 struct ofnode_phandle_args args;
96 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args);
98 debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
102 priv->period_ns = args.args[1];
103 priv->polarity = args.args[2];
105 priv->init_voltage = dev_read_u32_default(dev, "regulator-init-microvolt", -1);
106 if (priv->init_voltage < 0) {
107 printf("Cannot find regulator pwm init_voltage\n");
111 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
113 debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
120 static int pwm_regulator_probe(struct udevice *dev)
122 struct pwm_regulator_info *priv = dev_get_priv(dev);
123 struct dm_regulator_uclass_platdata *uc_pdata;
125 uc_pdata = dev_get_uclass_platdata(dev);
127 uc_pdata->type = REGULATOR_TYPE_BUCK;
128 uc_pdata->mode_count = 0;
129 priv->max_voltage = uc_pdata->max_uV;
130 priv->min_voltage = uc_pdata->min_uV;
132 if (priv->init_voltage)
133 pwm_regulator_set_voltage(dev, priv->init_voltage);
138 static const struct dm_regulator_ops pwm_regulator_ops = {
139 .get_value = pwm_regulator_get_voltage,
140 .set_value = pwm_regulator_set_voltage,
141 .set_enable = pwm_regulator_enable,
144 static const struct udevice_id pwm_regulator_ids[] = {
145 { .compatible = "pwm-regulator" },
149 U_BOOT_DRIVER(pwm_regulator) = {
150 .name = "pwm_regulator",
151 .id = UCLASS_REGULATOR,
152 .ops = &pwm_regulator_ops,
153 .probe = pwm_regulator_probe,
154 .of_match = pwm_regulator_ids,
155 .ofdata_to_platdata = pwm_regulator_ofdata_to_platdata,
156 .priv_auto_alloc_size = sizeof(struct pwm_regulator_info),