1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun // Copyright (C) 2018 ROHM Semiconductors
3*4882a593Smuzhiyun // bd70528-regulator.c ROHM BD70528MWV regulator driver
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/delay.h>
6*4882a593Smuzhiyun #include <linux/err.h>
7*4882a593Smuzhiyun #include <linux/interrupt.h>
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/mfd/rohm-bd70528.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/of.h>
12*4882a593Smuzhiyun #include <linux/platform_device.h>
13*4882a593Smuzhiyun #include <linux/regmap.h>
14*4882a593Smuzhiyun #include <linux/regulator/driver.h>
15*4882a593Smuzhiyun #include <linux/regulator/machine.h>
16*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define BUCK_RAMPRATE_250MV 0
20*4882a593Smuzhiyun #define BUCK_RAMPRATE_125MV 1
21*4882a593Smuzhiyun #define BUCK_RAMP_MAX 250
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun static const struct linear_range bd70528_buck1_volts[] = {
24*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(1200000, 0x00, 0x1, 600000),
25*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(2750000, 0x2, 0xf, 50000),
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun static const struct linear_range bd70528_buck2_volts[] = {
28*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(1200000, 0x00, 0x1, 300000),
29*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(1550000, 0x2, 0xd, 50000),
30*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(3000000, 0xe, 0xf, 300000),
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun static const struct linear_range bd70528_buck3_volts[] = {
33*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(800000, 0x00, 0xd, 50000),
34*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(1800000, 0xe, 0xf, 0),
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* All LDOs have same voltage ranges */
38*4882a593Smuzhiyun static const struct linear_range bd70528_ldo_volts[] = {
39*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(1650000, 0x0, 0x07, 50000),
40*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(2100000, 0x8, 0x0f, 100000),
41*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(2850000, 0x10, 0x19, 50000),
42*4882a593Smuzhiyun REGULATOR_LINEAR_RANGE(3300000, 0x19, 0x1f, 0),
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /* Also both LEDs support same voltages */
46*4882a593Smuzhiyun static const unsigned int led_volts[] = {
47*4882a593Smuzhiyun 20000, 30000
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun
bd70528_set_ramp_delay(struct regulator_dev * rdev,int ramp_delay)50*4882a593Smuzhiyun static int bd70528_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun if (ramp_delay > 0 && ramp_delay <= BUCK_RAMP_MAX) {
53*4882a593Smuzhiyun unsigned int ramp_value = BUCK_RAMPRATE_250MV;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun if (ramp_delay <= 125)
56*4882a593Smuzhiyun ramp_value = BUCK_RAMPRATE_125MV;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
59*4882a593Smuzhiyun BD70528_MASK_BUCK_RAMP,
60*4882a593Smuzhiyun ramp_value << BD70528_SIFT_BUCK_RAMP);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun dev_err(&rdev->dev, "%s: ramp_delay: %d not supported\n",
63*4882a593Smuzhiyun rdev->desc->name, ramp_delay);
64*4882a593Smuzhiyun return -EINVAL;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
bd70528_led_set_voltage_sel(struct regulator_dev * rdev,unsigned int sel)67*4882a593Smuzhiyun static int bd70528_led_set_voltage_sel(struct regulator_dev *rdev,
68*4882a593Smuzhiyun unsigned int sel)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun int ret;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun ret = regulator_is_enabled_regmap(rdev);
73*4882a593Smuzhiyun if (ret < 0)
74*4882a593Smuzhiyun return ret;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun if (ret == 0)
77*4882a593Smuzhiyun return regulator_set_voltage_sel_regmap(rdev, sel);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun dev_err(&rdev->dev,
80*4882a593Smuzhiyun "LED voltage change not allowed when led is enabled\n");
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun return -EBUSY;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun static const struct regulator_ops bd70528_buck_ops = {
86*4882a593Smuzhiyun .enable = regulator_enable_regmap,
87*4882a593Smuzhiyun .disable = regulator_disable_regmap,
88*4882a593Smuzhiyun .is_enabled = regulator_is_enabled_regmap,
89*4882a593Smuzhiyun .list_voltage = regulator_list_voltage_linear_range,
90*4882a593Smuzhiyun .set_voltage_sel = regulator_set_voltage_sel_regmap,
91*4882a593Smuzhiyun .get_voltage_sel = regulator_get_voltage_sel_regmap,
92*4882a593Smuzhiyun .set_voltage_time_sel = regulator_set_voltage_time_sel,
93*4882a593Smuzhiyun .set_ramp_delay = bd70528_set_ramp_delay,
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun static const struct regulator_ops bd70528_ldo_ops = {
97*4882a593Smuzhiyun .enable = regulator_enable_regmap,
98*4882a593Smuzhiyun .disable = regulator_disable_regmap,
99*4882a593Smuzhiyun .is_enabled = regulator_is_enabled_regmap,
100*4882a593Smuzhiyun .list_voltage = regulator_list_voltage_linear_range,
101*4882a593Smuzhiyun .set_voltage_sel = regulator_set_voltage_sel_regmap,
102*4882a593Smuzhiyun .get_voltage_sel = regulator_get_voltage_sel_regmap,
103*4882a593Smuzhiyun .set_voltage_time_sel = regulator_set_voltage_time_sel,
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun static const struct regulator_ops bd70528_led_ops = {
107*4882a593Smuzhiyun .enable = regulator_enable_regmap,
108*4882a593Smuzhiyun .disable = regulator_disable_regmap,
109*4882a593Smuzhiyun .is_enabled = regulator_is_enabled_regmap,
110*4882a593Smuzhiyun .list_voltage = regulator_list_voltage_table,
111*4882a593Smuzhiyun .set_voltage_sel = bd70528_led_set_voltage_sel,
112*4882a593Smuzhiyun .get_voltage_sel = regulator_get_voltage_sel_regmap,
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun static const struct regulator_desc bd70528_desc[] = {
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun .name = "buck1",
118*4882a593Smuzhiyun .of_match = of_match_ptr("BUCK1"),
119*4882a593Smuzhiyun .regulators_node = of_match_ptr("regulators"),
120*4882a593Smuzhiyun .id = BD70528_BUCK1,
121*4882a593Smuzhiyun .ops = &bd70528_buck_ops,
122*4882a593Smuzhiyun .type = REGULATOR_VOLTAGE,
123*4882a593Smuzhiyun .linear_ranges = bd70528_buck1_volts,
124*4882a593Smuzhiyun .n_linear_ranges = ARRAY_SIZE(bd70528_buck1_volts),
125*4882a593Smuzhiyun .n_voltages = BD70528_BUCK_VOLTS,
126*4882a593Smuzhiyun .enable_reg = BD70528_REG_BUCK1_EN,
127*4882a593Smuzhiyun .enable_mask = BD70528_MASK_RUN_EN,
128*4882a593Smuzhiyun .vsel_reg = BD70528_REG_BUCK1_VOLT,
129*4882a593Smuzhiyun .vsel_mask = BD70528_MASK_BUCK_VOLT,
130*4882a593Smuzhiyun .owner = THIS_MODULE,
131*4882a593Smuzhiyun },
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun .name = "buck2",
134*4882a593Smuzhiyun .of_match = of_match_ptr("BUCK2"),
135*4882a593Smuzhiyun .regulators_node = of_match_ptr("regulators"),
136*4882a593Smuzhiyun .id = BD70528_BUCK2,
137*4882a593Smuzhiyun .ops = &bd70528_buck_ops,
138*4882a593Smuzhiyun .type = REGULATOR_VOLTAGE,
139*4882a593Smuzhiyun .linear_ranges = bd70528_buck2_volts,
140*4882a593Smuzhiyun .n_linear_ranges = ARRAY_SIZE(bd70528_buck2_volts),
141*4882a593Smuzhiyun .n_voltages = BD70528_BUCK_VOLTS,
142*4882a593Smuzhiyun .enable_reg = BD70528_REG_BUCK2_EN,
143*4882a593Smuzhiyun .enable_mask = BD70528_MASK_RUN_EN,
144*4882a593Smuzhiyun .vsel_reg = BD70528_REG_BUCK2_VOLT,
145*4882a593Smuzhiyun .vsel_mask = BD70528_MASK_BUCK_VOLT,
146*4882a593Smuzhiyun .owner = THIS_MODULE,
147*4882a593Smuzhiyun },
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun .name = "buck3",
150*4882a593Smuzhiyun .of_match = of_match_ptr("BUCK3"),
151*4882a593Smuzhiyun .regulators_node = of_match_ptr("regulators"),
152*4882a593Smuzhiyun .id = BD70528_BUCK3,
153*4882a593Smuzhiyun .ops = &bd70528_buck_ops,
154*4882a593Smuzhiyun .type = REGULATOR_VOLTAGE,
155*4882a593Smuzhiyun .linear_ranges = bd70528_buck3_volts,
156*4882a593Smuzhiyun .n_linear_ranges = ARRAY_SIZE(bd70528_buck3_volts),
157*4882a593Smuzhiyun .n_voltages = BD70528_BUCK_VOLTS,
158*4882a593Smuzhiyun .enable_reg = BD70528_REG_BUCK3_EN,
159*4882a593Smuzhiyun .enable_mask = BD70528_MASK_RUN_EN,
160*4882a593Smuzhiyun .vsel_reg = BD70528_REG_BUCK3_VOLT,
161*4882a593Smuzhiyun .vsel_mask = BD70528_MASK_BUCK_VOLT,
162*4882a593Smuzhiyun .owner = THIS_MODULE,
163*4882a593Smuzhiyun },
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun .name = "ldo1",
166*4882a593Smuzhiyun .of_match = of_match_ptr("LDO1"),
167*4882a593Smuzhiyun .regulators_node = of_match_ptr("regulators"),
168*4882a593Smuzhiyun .id = BD70528_LDO1,
169*4882a593Smuzhiyun .ops = &bd70528_ldo_ops,
170*4882a593Smuzhiyun .type = REGULATOR_VOLTAGE,
171*4882a593Smuzhiyun .linear_ranges = bd70528_ldo_volts,
172*4882a593Smuzhiyun .n_linear_ranges = ARRAY_SIZE(bd70528_ldo_volts),
173*4882a593Smuzhiyun .n_voltages = BD70528_LDO_VOLTS,
174*4882a593Smuzhiyun .enable_reg = BD70528_REG_LDO1_EN,
175*4882a593Smuzhiyun .enable_mask = BD70528_MASK_RUN_EN,
176*4882a593Smuzhiyun .vsel_reg = BD70528_REG_LDO1_VOLT,
177*4882a593Smuzhiyun .vsel_mask = BD70528_MASK_LDO_VOLT,
178*4882a593Smuzhiyun .owner = THIS_MODULE,
179*4882a593Smuzhiyun },
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun .name = "ldo2",
182*4882a593Smuzhiyun .of_match = of_match_ptr("LDO2"),
183*4882a593Smuzhiyun .regulators_node = of_match_ptr("regulators"),
184*4882a593Smuzhiyun .id = BD70528_LDO2,
185*4882a593Smuzhiyun .ops = &bd70528_ldo_ops,
186*4882a593Smuzhiyun .type = REGULATOR_VOLTAGE,
187*4882a593Smuzhiyun .linear_ranges = bd70528_ldo_volts,
188*4882a593Smuzhiyun .n_linear_ranges = ARRAY_SIZE(bd70528_ldo_volts),
189*4882a593Smuzhiyun .n_voltages = BD70528_LDO_VOLTS,
190*4882a593Smuzhiyun .enable_reg = BD70528_REG_LDO2_EN,
191*4882a593Smuzhiyun .enable_mask = BD70528_MASK_RUN_EN,
192*4882a593Smuzhiyun .vsel_reg = BD70528_REG_LDO2_VOLT,
193*4882a593Smuzhiyun .vsel_mask = BD70528_MASK_LDO_VOLT,
194*4882a593Smuzhiyun .owner = THIS_MODULE,
195*4882a593Smuzhiyun },
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun .name = "ldo3",
198*4882a593Smuzhiyun .of_match = of_match_ptr("LDO3"),
199*4882a593Smuzhiyun .regulators_node = of_match_ptr("regulators"),
200*4882a593Smuzhiyun .id = BD70528_LDO3,
201*4882a593Smuzhiyun .ops = &bd70528_ldo_ops,
202*4882a593Smuzhiyun .type = REGULATOR_VOLTAGE,
203*4882a593Smuzhiyun .linear_ranges = bd70528_ldo_volts,
204*4882a593Smuzhiyun .n_linear_ranges = ARRAY_SIZE(bd70528_ldo_volts),
205*4882a593Smuzhiyun .n_voltages = BD70528_LDO_VOLTS,
206*4882a593Smuzhiyun .enable_reg = BD70528_REG_LDO3_EN,
207*4882a593Smuzhiyun .enable_mask = BD70528_MASK_RUN_EN,
208*4882a593Smuzhiyun .vsel_reg = BD70528_REG_LDO3_VOLT,
209*4882a593Smuzhiyun .vsel_mask = BD70528_MASK_LDO_VOLT,
210*4882a593Smuzhiyun .owner = THIS_MODULE,
211*4882a593Smuzhiyun },
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun .name = "ldo_led1",
214*4882a593Smuzhiyun .of_match = of_match_ptr("LDO_LED1"),
215*4882a593Smuzhiyun .regulators_node = of_match_ptr("regulators"),
216*4882a593Smuzhiyun .id = BD70528_LED1,
217*4882a593Smuzhiyun .ops = &bd70528_led_ops,
218*4882a593Smuzhiyun .type = REGULATOR_VOLTAGE,
219*4882a593Smuzhiyun .volt_table = &led_volts[0],
220*4882a593Smuzhiyun .n_voltages = ARRAY_SIZE(led_volts),
221*4882a593Smuzhiyun .enable_reg = BD70528_REG_LED_EN,
222*4882a593Smuzhiyun .enable_mask = BD70528_MASK_LED1_EN,
223*4882a593Smuzhiyun .vsel_reg = BD70528_REG_LED_VOLT,
224*4882a593Smuzhiyun .vsel_mask = BD70528_MASK_LED1_VOLT,
225*4882a593Smuzhiyun .owner = THIS_MODULE,
226*4882a593Smuzhiyun },
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun .name = "ldo_led2",
229*4882a593Smuzhiyun .of_match = of_match_ptr("LDO_LED2"),
230*4882a593Smuzhiyun .regulators_node = of_match_ptr("regulators"),
231*4882a593Smuzhiyun .id = BD70528_LED2,
232*4882a593Smuzhiyun .ops = &bd70528_led_ops,
233*4882a593Smuzhiyun .type = REGULATOR_VOLTAGE,
234*4882a593Smuzhiyun .volt_table = &led_volts[0],
235*4882a593Smuzhiyun .n_voltages = ARRAY_SIZE(led_volts),
236*4882a593Smuzhiyun .enable_reg = BD70528_REG_LED_EN,
237*4882a593Smuzhiyun .enable_mask = BD70528_MASK_LED2_EN,
238*4882a593Smuzhiyun .vsel_reg = BD70528_REG_LED_VOLT,
239*4882a593Smuzhiyun .vsel_mask = BD70528_MASK_LED2_VOLT,
240*4882a593Smuzhiyun .owner = THIS_MODULE,
241*4882a593Smuzhiyun },
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun };
244*4882a593Smuzhiyun
bd70528_probe(struct platform_device * pdev)245*4882a593Smuzhiyun static int bd70528_probe(struct platform_device *pdev)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun struct rohm_regmap_dev *bd70528;
248*4882a593Smuzhiyun int i;
249*4882a593Smuzhiyun struct regulator_config config = {
250*4882a593Smuzhiyun .dev = pdev->dev.parent,
251*4882a593Smuzhiyun };
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun bd70528 = dev_get_drvdata(pdev->dev.parent);
254*4882a593Smuzhiyun if (!bd70528) {
255*4882a593Smuzhiyun dev_err(&pdev->dev, "No MFD driver data\n");
256*4882a593Smuzhiyun return -EINVAL;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun config.regmap = bd70528->regmap;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(bd70528_desc); i++) {
262*4882a593Smuzhiyun struct regulator_dev *rdev;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun rdev = devm_regulator_register(&pdev->dev, &bd70528_desc[i],
265*4882a593Smuzhiyun &config);
266*4882a593Smuzhiyun if (IS_ERR(rdev)) {
267*4882a593Smuzhiyun dev_err(&pdev->dev,
268*4882a593Smuzhiyun "failed to register %s regulator\n",
269*4882a593Smuzhiyun bd70528_desc[i].name);
270*4882a593Smuzhiyun return PTR_ERR(rdev);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun static struct platform_driver bd70528_regulator = {
277*4882a593Smuzhiyun .driver = {
278*4882a593Smuzhiyun .name = "bd70528-pmic"
279*4882a593Smuzhiyun },
280*4882a593Smuzhiyun .probe = bd70528_probe,
281*4882a593Smuzhiyun };
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun module_platform_driver(bd70528_regulator);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
286*4882a593Smuzhiyun MODULE_DESCRIPTION("BD70528 voltage regulator driver");
287*4882a593Smuzhiyun MODULE_LICENSE("GPL");
288*4882a593Smuzhiyun MODULE_ALIAS("platform:bd70528-pmic");
289