Merge tag 'pci-v5.20-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[linux-2.6-microblaze.git] / drivers / regulator / tps65086-regulator.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
4  *
5  * Author: Andrew F. Davis <afd@ti.com>
6  *
7  * Based on the TPS65912 driver
8  */
9
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/regulator/driver.h>
14
15 #include <linux/mfd/tps65086.h>
16
17 enum tps65086_regulators { BUCK1, BUCK2, BUCK3, BUCK4, BUCK5, BUCK6, LDOA1,
18         LDOA2, LDOA3, SWA1, SWB1, SWB2, VTT };
19
20 #define TPS65086_REGULATOR(_name, _of, _id, _nv, _vr, _vm, _er, _em, _lr, _dr, _dm)     \
21         [_id] = {                                                       \
22                 .desc = {                                               \
23                         .name                   = _name,                \
24                         .of_match               = of_match_ptr(_of),    \
25                         .regulators_node        = "regulators",         \
26                         .of_parse_cb            = tps65086_of_parse_cb, \
27                         .id                     = _id,                  \
28                         .ops                    = &reg_ops,             \
29                         .n_voltages             = _nv,                  \
30                         .type                   = REGULATOR_VOLTAGE,    \
31                         .owner                  = THIS_MODULE,          \
32                         .vsel_reg               = _vr,                  \
33                         .vsel_mask              = _vm,                  \
34                         .enable_reg             = _er,                  \
35                         .enable_mask            = _em,                  \
36                         .volt_table             = NULL,                 \
37                         .linear_ranges          = _lr,                  \
38                         .n_linear_ranges        = ARRAY_SIZE(_lr),      \
39                 },                                                      \
40                 .decay_reg = _dr,                                       \
41                 .decay_mask = _dm,                                      \
42         }
43
44 #define TPS65086_SWITCH(_name, _of, _id, _er, _em)                      \
45         [_id] = {                                                       \
46                 .desc = {                                               \
47                         .name                   = _name,                \
48                         .of_match               = of_match_ptr(_of),    \
49                         .regulators_node        = "regulators",         \
50                         .of_parse_cb            = tps65086_of_parse_cb, \
51                         .id                     = _id,                  \
52                         .ops                    = &switch_ops,          \
53                         .type                   = REGULATOR_VOLTAGE,    \
54                         .owner                  = THIS_MODULE,          \
55                         .enable_reg             = _er,                  \
56                         .enable_mask            = _em,                  \
57                 },                                                      \
58         }
59
60 struct tps65086_regulator {
61         struct regulator_desc desc;
62         unsigned int decay_reg;
63         unsigned int decay_mask;
64 };
65
66 static const struct linear_range tps65086_10mv_ranges[] = {
67         REGULATOR_LINEAR_RANGE(0, 0x0, 0x0, 0),
68         REGULATOR_LINEAR_RANGE(410000, 0x1, 0x7F, 10000),
69 };
70
71 static const struct linear_range tps65086_buck126_25mv_ranges[] = {
72         REGULATOR_LINEAR_RANGE(0, 0x0, 0x0, 0),
73         REGULATOR_LINEAR_RANGE(1000000, 0x1, 0x18, 0),
74         REGULATOR_LINEAR_RANGE(1025000, 0x19, 0x7F, 25000),
75 };
76
77 static const struct linear_range tps65086_buck345_25mv_ranges[] = {
78         REGULATOR_LINEAR_RANGE(0, 0x0, 0x0, 0),
79         REGULATOR_LINEAR_RANGE(425000, 0x1, 0x7F, 25000),
80 };
81
82 static const struct linear_range tps65086_ldoa1_ranges[] = {
83         REGULATOR_LINEAR_RANGE(1350000, 0x0, 0x0, 0),
84         REGULATOR_LINEAR_RANGE(1500000, 0x1, 0x7, 100000),
85         REGULATOR_LINEAR_RANGE(2300000, 0x8, 0xB, 100000),
86         REGULATOR_LINEAR_RANGE(2850000, 0xC, 0xD, 150000),
87         REGULATOR_LINEAR_RANGE(3300000, 0xE, 0xE, 0),
88 };
89
90 static const struct linear_range tps65086_ldoa23_ranges[] = {
91         REGULATOR_LINEAR_RANGE(700000, 0x0, 0xD, 50000),
92         REGULATOR_LINEAR_RANGE(1400000, 0xE, 0xF, 100000),
93 };
94
95 /* Operations permitted on regulators */
96 static const struct regulator_ops reg_ops = {
97         .enable                 = regulator_enable_regmap,
98         .disable                = regulator_disable_regmap,
99         .is_enabled             = regulator_is_enabled_regmap,
100         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
101         .map_voltage            = regulator_map_voltage_linear_range,
102         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
103         .list_voltage           = regulator_list_voltage_linear_range,
104 };
105
106 /* Operations permitted on load switches */
107 static const struct regulator_ops switch_ops = {
108         .enable                 = regulator_enable_regmap,
109         .disable                = regulator_disable_regmap,
110         .is_enabled             = regulator_is_enabled_regmap,
111 };
112
113 static int tps65086_of_parse_cb(struct device_node *dev,
114                                 const struct regulator_desc *desc,
115                                 struct regulator_config *config);
116
117 static struct tps65086_regulator regulators[] = {
118         TPS65086_REGULATOR("BUCK1", "buck1", BUCK1, 0x80, TPS65086_BUCK1CTRL,
119                            BUCK_VID_MASK, TPS65086_BUCK123CTRL, BIT(0),
120                            tps65086_10mv_ranges, TPS65086_BUCK1CTRL,
121                            BIT(0)),
122         TPS65086_REGULATOR("BUCK2", "buck2", BUCK2, 0x80, TPS65086_BUCK2CTRL,
123                            BUCK_VID_MASK, TPS65086_BUCK123CTRL, BIT(1),
124                            tps65086_10mv_ranges, TPS65086_BUCK2CTRL,
125                            BIT(0)),
126         TPS65086_REGULATOR("BUCK3", "buck3", BUCK3, 0x80, TPS65086_BUCK3VID,
127                            BUCK_VID_MASK, TPS65086_BUCK123CTRL, BIT(2),
128                            tps65086_10mv_ranges, TPS65086_BUCK3DECAY,
129                            BIT(0)),
130         TPS65086_REGULATOR("BUCK4", "buck4", BUCK4, 0x80, TPS65086_BUCK4VID,
131                            BUCK_VID_MASK, TPS65086_BUCK4CTRL, BIT(0),
132                            tps65086_10mv_ranges, TPS65086_BUCK4VID,
133                            BIT(0)),
134         TPS65086_REGULATOR("BUCK5", "buck5", BUCK5, 0x80, TPS65086_BUCK5VID,
135                            BUCK_VID_MASK, TPS65086_BUCK5CTRL, BIT(0),
136                            tps65086_10mv_ranges, TPS65086_BUCK5CTRL,
137                            BIT(0)),
138         TPS65086_REGULATOR("BUCK6", "buck6", BUCK6, 0x80, TPS65086_BUCK6VID,
139                            BUCK_VID_MASK, TPS65086_BUCK6CTRL, BIT(0),
140                            tps65086_10mv_ranges, TPS65086_BUCK6CTRL,
141                            BIT(0)),
142         TPS65086_REGULATOR("LDOA1", "ldoa1", LDOA1, 0xF, TPS65086_LDOA1CTRL,
143                            VDOA1_VID_MASK, TPS65086_LDOA1CTRL, BIT(0),
144                            tps65086_ldoa1_ranges, 0, 0),
145         TPS65086_REGULATOR("LDOA2", "ldoa2", LDOA2, 0x10, TPS65086_LDOA2VID,
146                            VDOA23_VID_MASK, TPS65086_LDOA2CTRL, BIT(0),
147                            tps65086_ldoa23_ranges, 0, 0),
148         TPS65086_REGULATOR("LDOA3", "ldoa3", LDOA3, 0x10, TPS65086_LDOA3VID,
149                            VDOA23_VID_MASK, TPS65086_LDOA3CTRL, BIT(0),
150                            tps65086_ldoa23_ranges, 0, 0),
151         TPS65086_SWITCH("SWA1", "swa1", SWA1, TPS65086_SWVTT_EN, BIT(5)),
152         TPS65086_SWITCH("SWB1", "swb1", SWB1, TPS65086_SWVTT_EN, BIT(6)),
153         TPS65086_SWITCH("SWB2", "swb2", SWB2, TPS65086_SWVTT_EN, BIT(7)),
154         TPS65086_SWITCH("VTT", "vtt", VTT, TPS65086_SWVTT_EN, BIT(4)),
155 };
156
157 static int tps65086_of_parse_cb(struct device_node *node,
158                                 const struct regulator_desc *desc,
159                                 struct regulator_config *config)
160 {
161         int ret;
162
163         /* Check for 25mV step mode */
164         if (of_property_read_bool(node, "ti,regulator-step-size-25mv")) {
165                 switch (desc->id) {
166                 case BUCK1:
167                 case BUCK2:
168                 case BUCK6:
169                         regulators[desc->id].desc.linear_ranges =
170                                 tps65086_buck126_25mv_ranges;
171                         regulators[desc->id].desc.n_linear_ranges =
172                                 ARRAY_SIZE(tps65086_buck126_25mv_ranges);
173                         break;
174                 case BUCK3:
175                 case BUCK4:
176                 case BUCK5:
177                         regulators[desc->id].desc.linear_ranges =
178                                 tps65086_buck345_25mv_ranges;
179                         regulators[desc->id].desc.n_linear_ranges =
180                                 ARRAY_SIZE(tps65086_buck345_25mv_ranges);
181                         break;
182                 default:
183                         dev_warn(config->dev, "25mV step mode only valid for BUCK regulators\n");
184                 }
185         }
186
187         /* Check for decay mode */
188         if (desc->id <= BUCK6 && of_property_read_bool(node, "ti,regulator-decay")) {
189                 ret = regmap_write_bits(config->regmap,
190                                         regulators[desc->id].decay_reg,
191                                         regulators[desc->id].decay_mask,
192                                         regulators[desc->id].decay_mask);
193                 if (ret) {
194                         dev_err(config->dev, "Error setting decay\n");
195                         return ret;
196                 }
197         }
198
199         return 0;
200 }
201
202 static int tps65086_regulator_probe(struct platform_device *pdev)
203 {
204         struct tps65086 *tps = dev_get_drvdata(pdev->dev.parent);
205         struct regulator_config config = { };
206         struct regulator_dev *rdev;
207         int i;
208
209         platform_set_drvdata(pdev, tps);
210
211         config.dev = &pdev->dev;
212         config.dev->of_node = tps->dev->of_node;
213         config.driver_data = tps;
214         config.regmap = tps->regmap;
215
216         for (i = 0; i < ARRAY_SIZE(regulators); i++) {
217                 rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
218                                                &config);
219                 if (IS_ERR(rdev)) {
220                         dev_err(tps->dev, "failed to register %s regulator\n",
221                                 pdev->name);
222                         return PTR_ERR(rdev);
223                 }
224         }
225
226         return 0;
227 }
228
229 static const struct platform_device_id tps65086_regulator_id_table[] = {
230         { "tps65086-regulator", },
231         { /* sentinel */ }
232 };
233 MODULE_DEVICE_TABLE(platform, tps65086_regulator_id_table);
234
235 static struct platform_driver tps65086_regulator_driver = {
236         .driver = {
237                 .name = "tps65086-regulator",
238         },
239         .probe = tps65086_regulator_probe,
240         .id_table = tps65086_regulator_id_table,
241 };
242 module_platform_driver(tps65086_regulator_driver);
243
244 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
245 MODULE_DESCRIPTION("TPS65086 Regulator driver");
246 MODULE_LICENSE("GPL v2");