blob: b63f941990d512b2a3c94e1af9d26f6a92863648 [file] [log] [blame]
Kever Yang8bbe1cc2016-09-23 15:57:19 +08001/*
2 * Copyright (C) 2016 Rockchip Electronics Co., Ltd
3 *
4 * Based on kernel drivers/regulator/pwm-regulator.c
5 * Copyright (C) 2014 - STMicroelectronics Inc.
6 * Author: Lee Jones <lee.jones@linaro.org>
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <common.h>
12#include <dm.h>
13#include <errno.h>
14#include <pwm.h>
15#include <power/regulator.h>
16#include <libfdt.h>
17#include <fdt_support.h>
18#include <fdtdec.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
22struct pwm_regulator_info {
23 /* pwm id corresponding to the PWM driver */
24 int pwm_id;
25 /* the period of one PWM cycle */
26 int period_ns;
Kever Yang4c087422017-04-24 10:27:49 +080027 /*
28 * the polarity of one PWM
29 * 0: normal polarity
30 * 1: inverted polarity
31 */
32 bool polarity;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080033 struct udevice *pwm;
34 /* initialize voltage of regulator */
Mark Kettenis416b6c52017-05-13 20:17:05 +020035 int init_voltage;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080036 /* the maximum voltage of regulator */
Mark Kettenis416b6c52017-05-13 20:17:05 +020037 int max_voltage;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080038 /* the minimum voltage of regulator */
Mark Kettenis416b6c52017-05-13 20:17:05 +020039 int min_voltage;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080040 /* the current voltage of regulator */
Mark Kettenis416b6c52017-05-13 20:17:05 +020041 int volt_uV;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080042};
43
44static int pwm_regulator_enable(struct udevice *dev, bool enable)
45{
46 struct pwm_regulator_info *priv = dev_get_priv(dev);
47
48 return pwm_set_enable(priv->pwm, priv->pwm_id, enable);
49}
50
51static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
52{
53 struct pwm_regulator_info *priv = dev_get_priv(dev);
54 int min_uV = priv->min_voltage;
55 int max_uV = priv->max_voltage;
56 int diff = max_uV - min_uV;
57
Kever Yang4c087422017-04-24 10:27:49 +080058 return ((req_uV * 100) - (min_uV * 100)) / diff;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080059}
60
61static int pwm_regulator_get_voltage(struct udevice *dev)
62{
63 struct pwm_regulator_info *priv = dev_get_priv(dev);
64
65 return priv->volt_uV;
66}
67
68static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
69{
70 struct pwm_regulator_info *priv = dev_get_priv(dev);
71 int duty_cycle;
72 int ret = 0;
73
74 duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
75
Kever Yang4c087422017-04-24 10:27:49 +080076 ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity);
77 if (ret) {
78 dev_err(dev, "Failed to init PWM\n");
79 return ret;
80 }
81
Kever Yang8bbe1cc2016-09-23 15:57:19 +080082 ret = pwm_set_config(priv->pwm, priv->pwm_id,
Kever Yang6ea3e5d2017-07-19 19:54:21 +080083 priv->period_ns, (priv->period_ns / 100) * duty_cycle);
Kever Yang8bbe1cc2016-09-23 15:57:19 +080084 if (ret) {
85 dev_err(dev, "Failed to configure PWM\n");
86 return ret;
87 }
88
Kever Yang8bbe1cc2016-09-23 15:57:19 +080089 priv->volt_uV = uvolt;
Kever Yangcba88f12017-07-19 19:54:22 +080090
Kever Yang8bbe1cc2016-09-23 15:57:19 +080091 return ret;
92}
93
94static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
95{
96 struct pwm_regulator_info *priv = dev_get_priv(dev);
97 struct fdtdec_phandle_args args;
98 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070099 int node = dev_of_offset(dev);
Kever Yang8bbe1cc2016-09-23 15:57:19 +0800100 int ret;
101
102 ret = fdtdec_parse_phandle_with_args(blob, node, "pwms", "#pwm-cells",
103 0, 0, &args);
104 if (ret) {
105 debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
106 return ret;
107 }
Kever Yang8bbe1cc2016-09-23 15:57:19 +0800108
109 priv->period_ns = args.args[1];
Kever Yang4c087422017-04-24 10:27:49 +0800110 priv->polarity = args.args[2];
Kever Yang8bbe1cc2016-09-23 15:57:19 +0800111
112 priv->init_voltage = fdtdec_get_int(blob, node,
113 "regulator-init-microvolt", -1);
114 if (priv->init_voltage < 0) {
115 printf("Cannot find regulator pwm init_voltage\n");
116 return -EINVAL;
117 }
118
119 ret = uclass_get_device_by_of_offset(UCLASS_PWM, args.node, &priv->pwm);
120 if (ret) {
121 debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
122 return ret;
123 }
124
125 return 0;
126}
127
128static int pwm_regulator_probe(struct udevice *dev)
129{
130 struct pwm_regulator_info *priv = dev_get_priv(dev);
131 struct dm_regulator_uclass_platdata *uc_pdata;
132
133 uc_pdata = dev_get_uclass_platdata(dev);
134
135 uc_pdata->type = REGULATOR_TYPE_BUCK;
136 uc_pdata->mode_count = 0;
137 priv->max_voltage = uc_pdata->max_uV;
138 priv->min_voltage = uc_pdata->min_uV;
139
140 if (priv->init_voltage)
141 pwm_regulator_set_voltage(dev, priv->init_voltage);
142
Kever Yang8bbe1cc2016-09-23 15:57:19 +0800143 return 0;
144}
145
146static const struct dm_regulator_ops pwm_regulator_ops = {
147 .get_value = pwm_regulator_get_voltage,
148 .set_value = pwm_regulator_set_voltage,
149 .set_enable = pwm_regulator_enable,
150};
151
152static const struct udevice_id pwm_regulator_ids[] = {
153 { .compatible = "pwm-regulator" },
154 { }
155};
156
157U_BOOT_DRIVER(pwm_regulator) = {
158 .name = "pwm_regulator",
159 .id = UCLASS_REGULATOR,
160 .ops = &pwm_regulator_ops,
161 .probe = pwm_regulator_probe,
162 .of_match = pwm_regulator_ids,
163 .ofdata_to_platdata = pwm_regulator_ofdata_to_platdata,
164 .priv_auto_alloc_size = sizeof(struct pwm_regulator_info),
165};