]> git.sur5r.net Git - u-boot/blob - drivers/power/regulator/pwm_regulator.c
dm: core: Replace of_offset with accessor
[u-boot] / drivers / power / regulator / pwm_regulator.c
1 /*
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
20 DECLARE_GLOBAL_DATA_PTR;
21
22 struct 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;
27         struct udevice *pwm;
28         /* initialize voltage of regulator */
29         unsigned int init_voltage;
30         /* the maximum voltage of regulator */
31         unsigned int max_voltage;
32         /* the minimum voltage of regulator */
33         unsigned int min_voltage;
34         /* the current voltage of regulator */
35         unsigned int volt_uV;
36 };
37
38 static int pwm_regulator_enable(struct udevice *dev, bool enable)
39 {
40         struct pwm_regulator_info *priv = dev_get_priv(dev);
41
42         return pwm_set_enable(priv->pwm, priv->pwm_id, enable);
43 }
44
45 static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
46 {
47         struct pwm_regulator_info *priv = dev_get_priv(dev);
48         int min_uV = priv->min_voltage;
49         int max_uV = priv->max_voltage;
50         int diff = max_uV - min_uV;
51
52         return 100 - (((req_uV * 100) - (min_uV * 100)) / diff);
53 }
54
55 static int pwm_regulator_get_voltage(struct udevice *dev)
56 {
57         struct pwm_regulator_info *priv = dev_get_priv(dev);
58
59         return priv->volt_uV;
60 }
61
62 static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
63 {
64         struct pwm_regulator_info *priv = dev_get_priv(dev);
65         int duty_cycle;
66         int ret = 0;
67
68         duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
69
70         ret = pwm_set_config(priv->pwm, priv->pwm_id,
71                         (priv->period_ns / 100) * duty_cycle, priv->period_ns);
72         if (ret) {
73                 dev_err(dev, "Failed to configure PWM\n");
74                 return ret;
75         }
76
77         ret = pwm_set_enable(priv->pwm, priv->pwm_id, true);
78         if (ret) {
79                 dev_err(dev, "Failed to enable PWM\n");
80                 return ret;
81         }
82         priv->volt_uV = uvolt;
83         return ret;
84 }
85
86 static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
87 {
88         struct pwm_regulator_info *priv = dev_get_priv(dev);
89         struct fdtdec_phandle_args args;
90         const void *blob = gd->fdt_blob;
91         int node = dev_of_offset(dev);
92         int ret;
93
94         ret = fdtdec_parse_phandle_with_args(blob, node, "pwms", "#pwm-cells",
95                                              0, 0, &args);
96         if (ret) {
97                 debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
98                 return ret;
99         }
100         /* TODO: pwm_id here from device tree if needed */
101
102         priv->period_ns = args.args[1];
103
104         priv->init_voltage = fdtdec_get_int(blob, node,
105                         "regulator-init-microvolt", -1);
106         if (priv->init_voltage < 0) {
107                 printf("Cannot find regulator pwm init_voltage\n");
108                 return -EINVAL;
109         }
110
111         ret = uclass_get_device_by_of_offset(UCLASS_PWM, args.node, &priv->pwm);
112         if (ret) {
113                 debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
114                 return ret;
115         }
116
117         return 0;
118 }
119
120 static int pwm_regulator_probe(struct udevice *dev)
121 {
122         struct pwm_regulator_info *priv = dev_get_priv(dev);
123         struct dm_regulator_uclass_platdata *uc_pdata;
124
125         uc_pdata = dev_get_uclass_platdata(dev);
126
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;
131
132         if (priv->init_voltage)
133                 pwm_regulator_set_voltage(dev, priv->init_voltage);
134
135         pwm_regulator_enable(dev, 1);
136
137         return 0;
138 }
139
140 static const struct dm_regulator_ops pwm_regulator_ops = {
141         .get_value  = pwm_regulator_get_voltage,
142         .set_value  = pwm_regulator_set_voltage,
143         .set_enable = pwm_regulator_enable,
144 };
145
146 static const struct udevice_id pwm_regulator_ids[] = {
147         { .compatible = "pwm-regulator" },
148         { }
149 };
150
151 U_BOOT_DRIVER(pwm_regulator) = {
152         .name = "pwm_regulator",
153         .id = UCLASS_REGULATOR,
154         .ops = &pwm_regulator_ops,
155         .probe = pwm_regulator_probe,
156         .of_match = pwm_regulator_ids,
157         .ofdata_to_platdata     = pwm_regulator_ofdata_to_platdata,
158         .priv_auto_alloc_size   = sizeof(struct pwm_regulator_info),
159 };