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