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>
15 #include <linux/libfdt.h>
16 #include <fdt_support.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 struct pwm_regulator_info {
22 /* pwm id corresponding to the PWM driver */
24 /* the period of one PWM cycle */
27 * the polarity of one PWM
29 * 1: inverted polarity
33 /* initialize voltage of regulator */
35 /* the maximum voltage of regulator */
37 /* the minimum voltage of regulator */
39 /* the current voltage of regulator */
43 static int pwm_regulator_enable(struct udevice *dev, bool enable)
45 struct pwm_regulator_info *priv = dev_get_priv(dev);
47 return pwm_set_enable(priv->pwm, priv->pwm_id, enable);
50 static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
52 struct pwm_regulator_info *priv = dev_get_priv(dev);
53 int min_uV = priv->min_voltage;
54 int max_uV = priv->max_voltage;
55 int diff = max_uV - min_uV;
57 return ((req_uV * 100) - (min_uV * 100)) / diff;
60 static int pwm_regulator_get_voltage(struct udevice *dev)
62 struct pwm_regulator_info *priv = dev_get_priv(dev);
67 static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
69 struct pwm_regulator_info *priv = dev_get_priv(dev);
73 duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
75 ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity);
77 dev_err(dev, "Failed to init PWM\n");
81 ret = pwm_set_config(priv->pwm, priv->pwm_id,
82 priv->period_ns, (priv->period_ns / 100) * duty_cycle);
84 dev_err(dev, "Failed to configure PWM\n");
88 priv->volt_uV = uvolt;
93 static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
95 struct pwm_regulator_info *priv = dev_get_priv(dev);
96 struct fdtdec_phandle_args args;
97 const void *blob = gd->fdt_blob;
98 int node = dev_of_offset(dev);
101 ret = fdtdec_parse_phandle_with_args(blob, node, "pwms", "#pwm-cells",
104 debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
108 priv->period_ns = args.args[1];
109 priv->polarity = args.args[2];
111 priv->init_voltage = fdtdec_get_int(blob, node,
112 "regulator-init-microvolt", -1);
113 if (priv->init_voltage < 0) {
114 printf("Cannot find regulator pwm init_voltage\n");
118 ret = uclass_get_device_by_of_offset(UCLASS_PWM, args.node, &priv->pwm);
120 debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
127 static int pwm_regulator_probe(struct udevice *dev)
129 struct pwm_regulator_info *priv = dev_get_priv(dev);
130 struct dm_regulator_uclass_platdata *uc_pdata;
132 uc_pdata = dev_get_uclass_platdata(dev);
134 uc_pdata->type = REGULATOR_TYPE_BUCK;
135 uc_pdata->mode_count = 0;
136 priv->max_voltage = uc_pdata->max_uV;
137 priv->min_voltage = uc_pdata->min_uV;
139 if (priv->init_voltage)
140 pwm_regulator_set_voltage(dev, priv->init_voltage);
145 static const struct dm_regulator_ops pwm_regulator_ops = {
146 .get_value = pwm_regulator_get_voltage,
147 .set_value = pwm_regulator_set_voltage,
148 .set_enable = pwm_regulator_enable,
151 static const struct udevice_id pwm_regulator_ids[] = {
152 { .compatible = "pwm-regulator" },
156 U_BOOT_DRIVER(pwm_regulator) = {
157 .name = "pwm_regulator",
158 .id = UCLASS_REGULATOR,
159 .ops = &pwm_regulator_ops,
160 .probe = pwm_regulator_probe,
161 .of_match = pwm_regulator_ids,
162 .ofdata_to_platdata = pwm_regulator_ofdata_to_platdata,
163 .priv_auto_alloc_size = sizeof(struct pwm_regulator_info),