blob: 3a487255482c13073f031e16113b3d91c44a1008 [file] [log] [blame]
Matti Vaittinenba087992018-05-30 11:43:43 +03001// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 ROHM Semiconductors
3// bd71837-regulator.c ROHM BD71837MWV regulator driver
4
Matti Vaittinenc9dc4cf2018-06-28 14:22:23 +03005#include <linux/delay.h>
Matti Vaittinenba087992018-05-30 11:43:43 +03006#include <linux/err.h>
Matti Vaittinenc9dc4cf2018-06-28 14:22:23 +03007#include <linux/gpio.h>
Matti Vaittinenba087992018-05-30 11:43:43 +03008#include <linux/interrupt.h>
Matti Vaittinenc9dc4cf2018-06-28 14:22:23 +03009#include <linux/kernel.h>
10#include <linux/mfd/bd71837.h>
11#include <linux/module.h>
Matti Vaittinenba087992018-05-30 11:43:43 +030012#include <linux/platform_device.h>
13#include <linux/regulator/driver.h>
14#include <linux/regulator/machine.h>
Matti Vaittinenba087992018-05-30 11:43:43 +030015#include <linux/regulator/of_regulator.h>
Matti Vaittinenc9dc4cf2018-06-28 14:22:23 +030016#include <linux/slab.h>
Matti Vaittinenba087992018-05-30 11:43:43 +030017
18struct bd71837_pmic {
19 struct regulator_desc descs[BD71837_REGULATOR_CNT];
20 struct bd71837 *mfd;
21 struct platform_device *pdev;
22 struct regulator_dev *rdev[BD71837_REGULATOR_CNT];
23};
24
25/*
26 * BUCK1/2/3/4
27 * BUCK1RAMPRATE[1:0] BUCK1 DVS ramp rate setting
28 * 00: 10.00mV/usec 10mV 1uS
29 * 01: 5.00mV/usec 10mV 2uS
30 * 10: 2.50mV/usec 10mV 4uS
31 * 11: 1.25mV/usec 10mV 8uS
32 */
33static int bd71837_buck1234_set_ramp_delay(struct regulator_dev *rdev,
34 int ramp_delay)
35{
36 struct bd71837_pmic *pmic = rdev_get_drvdata(rdev);
37 struct bd71837 *mfd = pmic->mfd;
38 int id = rdev->desc->id;
39 unsigned int ramp_value = BUCK_RAMPRATE_10P00MV;
40
Matti Vaittinenc9dc4cf2018-06-28 14:22:23 +030041 dev_dbg(&pmic->pdev->dev, "Buck[%d] Set Ramp = %d\n", id + 1,
Matti Vaittinenba087992018-05-30 11:43:43 +030042 ramp_delay);
43 switch (ramp_delay) {
44 case 1 ... 1250:
45 ramp_value = BUCK_RAMPRATE_1P25MV;
46 break;
47 case 1251 ... 2500:
48 ramp_value = BUCK_RAMPRATE_2P50MV;
49 break;
50 case 2501 ... 5000:
51 ramp_value = BUCK_RAMPRATE_5P00MV;
52 break;
53 case 5001 ... 10000:
54 ramp_value = BUCK_RAMPRATE_10P00MV;
55 break;
56 default:
57 ramp_value = BUCK_RAMPRATE_10P00MV;
58 dev_err(&pmic->pdev->dev,
59 "%s: ramp_delay: %d not supported, setting 10000mV//us\n",
60 rdev->desc->name, ramp_delay);
61 }
62
63 return regmap_update_bits(mfd->regmap, BD71837_REG_BUCK1_CTRL + id,
64 BUCK_RAMPRATE_MASK, ramp_value << 6);
65}
66
67/* Bucks 1 to 4 support DVS. PWM mode is used when voltage is changed.
68 * Bucks 5 to 8 and LDOs can use PFM and must be disabled when voltage
69 * is changed. Hence we return -EBUSY for these if voltage is changed
70 * when BUCK/LDO is enabled.
71 */
72static int bd71837_set_voltage_sel_restricted(struct regulator_dev *rdev,
73 unsigned int sel)
74{
Axel Linffdc4982018-06-27 20:40:14 +080075 if (regulator_is_enabled_regmap(rdev))
76 return -EBUSY;
Matti Vaittinenba087992018-05-30 11:43:43 +030077
Axel Linffdc4982018-06-27 20:40:14 +080078 return regulator_set_voltage_sel_regmap(rdev, sel);
Matti Vaittinenba087992018-05-30 11:43:43 +030079}
80
81static struct regulator_ops bd71837_ldo_regulator_ops = {
82 .enable = regulator_enable_regmap,
83 .disable = regulator_disable_regmap,
84 .is_enabled = regulator_is_enabled_regmap,
85 .list_voltage = regulator_list_voltage_linear_range,
86 .set_voltage_sel = bd71837_set_voltage_sel_restricted,
87 .get_voltage_sel = regulator_get_voltage_sel_regmap,
88};
89
90static struct regulator_ops bd71837_ldo_regulator_nolinear_ops = {
91 .enable = regulator_enable_regmap,
92 .disable = regulator_disable_regmap,
93 .is_enabled = regulator_is_enabled_regmap,
94 .list_voltage = regulator_list_voltage_table,
95 .set_voltage_sel = bd71837_set_voltage_sel_restricted,
96 .get_voltage_sel = regulator_get_voltage_sel_regmap,
97};
98
99static struct regulator_ops bd71837_buck_regulator_ops = {
100 .enable = regulator_enable_regmap,
101 .disable = regulator_disable_regmap,
102 .is_enabled = regulator_is_enabled_regmap,
103 .list_voltage = regulator_list_voltage_linear_range,
104 .set_voltage_sel = bd71837_set_voltage_sel_restricted,
105 .get_voltage_sel = regulator_get_voltage_sel_regmap,
106 .set_voltage_time_sel = regulator_set_voltage_time_sel,
107};
108
109static struct regulator_ops bd71837_buck_regulator_nolinear_ops = {
110 .enable = regulator_enable_regmap,
111 .disable = regulator_disable_regmap,
112 .is_enabled = regulator_is_enabled_regmap,
113 .list_voltage = regulator_list_voltage_table,
114 .set_voltage_sel = bd71837_set_voltage_sel_restricted,
115 .get_voltage_sel = regulator_get_voltage_sel_regmap,
116 .set_voltage_time_sel = regulator_set_voltage_time_sel,
117};
118
119static struct regulator_ops bd71837_buck1234_regulator_ops = {
120 .enable = regulator_enable_regmap,
121 .disable = regulator_disable_regmap,
122 .is_enabled = regulator_is_enabled_regmap,
123 .list_voltage = regulator_list_voltage_linear_range,
124 .set_voltage_sel = regulator_set_voltage_sel_regmap,
125 .get_voltage_sel = regulator_get_voltage_sel_regmap,
126 .set_voltage_time_sel = regulator_set_voltage_time_sel,
127 .set_ramp_delay = bd71837_buck1234_set_ramp_delay,
128};
129
130/*
131 * BUCK1/2/3/4
132 * 0.70 to 1.30V (10mV step)
133 */
134static const struct regulator_linear_range bd71837_buck1234_voltage_ranges[] = {
135 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x3C, 10000),
136 REGULATOR_LINEAR_RANGE(1300000, 0x3D, 0x3F, 0),
137};
138
139/*
140 * BUCK5
141 * 0.9V to 1.35V ()
142 */
143static const struct regulator_linear_range bd71837_buck5_voltage_ranges[] = {
144 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x03, 100000),
145 REGULATOR_LINEAR_RANGE(1050000, 0x04, 0x05, 50000),
146 REGULATOR_LINEAR_RANGE(1200000, 0x06, 0x07, 150000),
147};
148
149/*
150 * BUCK6
151 * 3.0V to 3.3V (step 100mV)
152 */
153static const struct regulator_linear_range bd71837_buck6_voltage_ranges[] = {
154 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
155};
156
157/*
158 * BUCK7
159 * 000 = 1.605V
160 * 001 = 1.695V
161 * 010 = 1.755V
162 * 011 = 1.8V (Initial)
163 * 100 = 1.845V
164 * 101 = 1.905V
165 * 110 = 1.95V
166 * 111 = 1.995V
167 */
168static const unsigned int buck_7_volts[] = {
169 1605000, 1695000, 1755000, 1800000, 1845000, 1905000, 1950000, 1995000
170};
171
172/*
173 * BUCK8
174 * 0.8V to 1.40V (step 10mV)
175 */
176static const struct regulator_linear_range bd71837_buck8_voltage_ranges[] = {
177 REGULATOR_LINEAR_RANGE(800000, 0x00, 0x3C, 10000),
178 REGULATOR_LINEAR_RANGE(1400000, 0x3D, 0x3F, 0),
179};
180
181/*
182 * LDO1
183 * 3.0 to 3.3V (100mV step)
184 */
185static const struct regulator_linear_range bd71837_ldo1_voltage_ranges[] = {
186 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
187};
188
189/*
190 * LDO2
191 * 0.8 or 0.9V
192 */
Axel Linadb78a82018-06-27 20:40:13 +0800193static const unsigned int ldo_2_volts[] = {
Matti Vaittinenba087992018-05-30 11:43:43 +0300194 900000, 800000
195};
196
197/*
198 * LDO3
199 * 1.8 to 3.3V (100mV step)
200 */
201static const struct regulator_linear_range bd71837_ldo3_voltage_ranges[] = {
202 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
203};
204
205/*
206 * LDO4
207 * 0.9 to 1.8V (100mV step)
208 */
209static const struct regulator_linear_range bd71837_ldo4_voltage_ranges[] = {
210 REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
211 REGULATOR_LINEAR_RANGE(1800000, 0x0A, 0x0F, 0),
212};
213
214/*
215 * LDO5
216 * 1.8 to 3.3V (100mV step)
217 */
218static const struct regulator_linear_range bd71837_ldo5_voltage_ranges[] = {
219 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
220};
221
222/*
223 * LDO6
224 * 0.9 to 1.8V (100mV step)
225 */
226static const struct regulator_linear_range bd71837_ldo6_voltage_ranges[] = {
227 REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
228 REGULATOR_LINEAR_RANGE(1800000, 0x0A, 0x0F, 0),
229};
230
231/*
232 * LDO7
233 * 1.8 to 3.3V (100mV step)
234 */
235static const struct regulator_linear_range bd71837_ldo7_voltage_ranges[] = {
236 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
237};
238
239static const struct regulator_desc bd71837_regulators[] = {
240 {
241 .name = "buck1",
242 .of_match = of_match_ptr("BUCK1"),
243 .regulators_node = of_match_ptr("regulators"),
244 .id = BD71837_BUCK1,
245 .ops = &bd71837_buck1234_regulator_ops,
246 .type = REGULATOR_VOLTAGE,
247 .n_voltages = BD71837_BUCK1_VOLTAGE_NUM,
248 .linear_ranges = bd71837_buck1234_voltage_ranges,
249 .n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
250 .vsel_reg = BD71837_REG_BUCK1_VOLT_RUN,
251 .vsel_mask = BUCK1_RUN_MASK,
252 .enable_reg = BD71837_REG_BUCK1_CTRL,
253 .enable_mask = BD71837_BUCK_EN,
254 .owner = THIS_MODULE,
255 },
256 {
257 .name = "buck2",
258 .of_match = of_match_ptr("BUCK2"),
259 .regulators_node = of_match_ptr("regulators"),
260 .id = BD71837_BUCK2,
261 .ops = &bd71837_buck1234_regulator_ops,
262 .type = REGULATOR_VOLTAGE,
263 .n_voltages = BD71837_BUCK2_VOLTAGE_NUM,
264 .linear_ranges = bd71837_buck1234_voltage_ranges,
265 .n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
266 .vsel_reg = BD71837_REG_BUCK2_VOLT_RUN,
267 .vsel_mask = BUCK2_RUN_MASK,
268 .enable_reg = BD71837_REG_BUCK2_CTRL,
269 .enable_mask = BD71837_BUCK_EN,
270 .owner = THIS_MODULE,
271 },
272 {
273 .name = "buck3",
274 .of_match = of_match_ptr("BUCK3"),
275 .regulators_node = of_match_ptr("regulators"),
276 .id = BD71837_BUCK3,
277 .ops = &bd71837_buck1234_regulator_ops,
278 .type = REGULATOR_VOLTAGE,
279 .n_voltages = BD71837_BUCK3_VOLTAGE_NUM,
280 .linear_ranges = bd71837_buck1234_voltage_ranges,
281 .n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
282 .vsel_reg = BD71837_REG_BUCK3_VOLT_RUN,
283 .vsel_mask = BUCK3_RUN_MASK,
284 .enable_reg = BD71837_REG_BUCK3_CTRL,
285 .enable_mask = BD71837_BUCK_EN,
286 .owner = THIS_MODULE,
287 },
288 {
289 .name = "buck4",
290 .of_match = of_match_ptr("BUCK4"),
291 .regulators_node = of_match_ptr("regulators"),
292 .id = BD71837_BUCK4,
293 .ops = &bd71837_buck1234_regulator_ops,
294 .type = REGULATOR_VOLTAGE,
295 .n_voltages = BD71837_BUCK4_VOLTAGE_NUM,
296 .linear_ranges = bd71837_buck1234_voltage_ranges,
297 .n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
298 .vsel_reg = BD71837_REG_BUCK4_VOLT_RUN,
299 .vsel_mask = BUCK4_RUN_MASK,
300 .enable_reg = BD71837_REG_BUCK4_CTRL,
301 .enable_mask = BD71837_BUCK_EN,
302 .owner = THIS_MODULE,
303 },
304 {
305 .name = "buck5",
306 .of_match = of_match_ptr("BUCK5"),
307 .regulators_node = of_match_ptr("regulators"),
308 .id = BD71837_BUCK5,
309 .ops = &bd71837_buck_regulator_ops,
310 .type = REGULATOR_VOLTAGE,
311 .n_voltages = BD71837_BUCK5_VOLTAGE_NUM,
312 .linear_ranges = bd71837_buck5_voltage_ranges,
313 .n_linear_ranges = ARRAY_SIZE(bd71837_buck5_voltage_ranges),
314 .vsel_reg = BD71837_REG_BUCK5_VOLT,
315 .vsel_mask = BUCK5_MASK,
316 .enable_reg = BD71837_REG_BUCK5_CTRL,
317 .enable_mask = BD71837_BUCK_EN,
318 .owner = THIS_MODULE,
319 },
320 {
321 .name = "buck6",
322 .of_match = of_match_ptr("BUCK6"),
323 .regulators_node = of_match_ptr("regulators"),
324 .id = BD71837_BUCK6,
325 .ops = &bd71837_buck_regulator_ops,
326 .type = REGULATOR_VOLTAGE,
327 .n_voltages = BD71837_BUCK6_VOLTAGE_NUM,
328 .linear_ranges = bd71837_buck6_voltage_ranges,
329 .n_linear_ranges = ARRAY_SIZE(bd71837_buck6_voltage_ranges),
330 .vsel_reg = BD71837_REG_BUCK6_VOLT,
331 .vsel_mask = BUCK6_MASK,
332 .enable_reg = BD71837_REG_BUCK6_CTRL,
333 .enable_mask = BD71837_BUCK_EN,
334 .owner = THIS_MODULE,
335 },
336 {
337 .name = "buck7",
338 .of_match = of_match_ptr("BUCK7"),
339 .regulators_node = of_match_ptr("regulators"),
340 .id = BD71837_BUCK7,
341 .ops = &bd71837_buck_regulator_nolinear_ops,
342 .type = REGULATOR_VOLTAGE,
343 .volt_table = &buck_7_volts[0],
344 .n_voltages = ARRAY_SIZE(buck_7_volts),
345 .vsel_reg = BD71837_REG_BUCK7_VOLT,
346 .vsel_mask = BUCK7_MASK,
347 .enable_reg = BD71837_REG_BUCK7_CTRL,
348 .enable_mask = BD71837_BUCK_EN,
349 .owner = THIS_MODULE,
350 },
351 {
352 .name = "buck8",
353 .of_match = of_match_ptr("BUCK8"),
354 .regulators_node = of_match_ptr("regulators"),
355 .id = BD71837_BUCK8,
356 .ops = &bd71837_buck_regulator_ops,
357 .type = REGULATOR_VOLTAGE,
358 .n_voltages = BD71837_BUCK8_VOLTAGE_NUM,
359 .linear_ranges = bd71837_buck8_voltage_ranges,
360 .n_linear_ranges = ARRAY_SIZE(bd71837_buck8_voltage_ranges),
361 .vsel_reg = BD71837_REG_BUCK8_VOLT,
362 .vsel_mask = BUCK8_MASK,
363 .enable_reg = BD71837_REG_BUCK8_CTRL,
364 .enable_mask = BD71837_BUCK_EN,
365 .owner = THIS_MODULE,
366 },
367 {
368 .name = "ldo1",
369 .of_match = of_match_ptr("LDO1"),
370 .regulators_node = of_match_ptr("regulators"),
371 .id = BD71837_LDO1,
372 .ops = &bd71837_ldo_regulator_ops,
373 .type = REGULATOR_VOLTAGE,
374 .n_voltages = BD71837_LDO1_VOLTAGE_NUM,
375 .linear_ranges = bd71837_ldo1_voltage_ranges,
376 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo1_voltage_ranges),
377 .vsel_reg = BD71837_REG_LDO1_VOLT,
378 .vsel_mask = LDO1_MASK,
379 .enable_reg = BD71837_REG_LDO1_VOLT,
380 .enable_mask = BD71837_LDO_EN,
381 .owner = THIS_MODULE,
382 },
383 {
384 .name = "ldo2",
385 .of_match = of_match_ptr("LDO2"),
386 .regulators_node = of_match_ptr("regulators"),
387 .id = BD71837_LDO2,
388 .ops = &bd71837_ldo_regulator_nolinear_ops,
389 .type = REGULATOR_VOLTAGE,
390 .volt_table = &ldo_2_volts[0],
391 .vsel_reg = BD71837_REG_LDO2_VOLT,
392 .vsel_mask = LDO2_MASK,
393 .n_voltages = ARRAY_SIZE(ldo_2_volts),
394 .n_voltages = BD71837_LDO2_VOLTAGE_NUM,
395 .enable_reg = BD71837_REG_LDO2_VOLT,
396 .enable_mask = BD71837_LDO_EN,
397 .owner = THIS_MODULE,
398 },
399 {
400 .name = "ldo3",
401 .of_match = of_match_ptr("LDO3"),
402 .regulators_node = of_match_ptr("regulators"),
403 .id = BD71837_LDO3,
404 .ops = &bd71837_ldo_regulator_ops,
405 .type = REGULATOR_VOLTAGE,
406 .n_voltages = BD71837_LDO3_VOLTAGE_NUM,
407 .linear_ranges = bd71837_ldo3_voltage_ranges,
408 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo3_voltage_ranges),
409 .vsel_reg = BD71837_REG_LDO3_VOLT,
410 .vsel_mask = LDO3_MASK,
411 .enable_reg = BD71837_REG_LDO3_VOLT,
412 .enable_mask = BD71837_LDO_EN,
413 .owner = THIS_MODULE,
414 },
415 {
416 .name = "ldo4",
417 .of_match = of_match_ptr("LDO4"),
418 .regulators_node = of_match_ptr("regulators"),
419 .id = BD71837_LDO4,
420 .ops = &bd71837_ldo_regulator_ops,
421 .type = REGULATOR_VOLTAGE,
422 .n_voltages = BD71837_LDO4_VOLTAGE_NUM,
423 .linear_ranges = bd71837_ldo4_voltage_ranges,
424 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo4_voltage_ranges),
425 .vsel_reg = BD71837_REG_LDO4_VOLT,
426 .vsel_mask = LDO4_MASK,
427 .enable_reg = BD71837_REG_LDO4_VOLT,
428 .enable_mask = BD71837_LDO_EN,
429 .owner = THIS_MODULE,
430 },
431 {
432 .name = "ldo5",
433 .of_match = of_match_ptr("LDO5"),
434 .regulators_node = of_match_ptr("regulators"),
435 .id = BD71837_LDO5,
436 .ops = &bd71837_ldo_regulator_ops,
437 .type = REGULATOR_VOLTAGE,
438 .n_voltages = BD71837_LDO5_VOLTAGE_NUM,
439 .linear_ranges = bd71837_ldo5_voltage_ranges,
440 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo5_voltage_ranges),
441 /* LDO5 is supplied by buck6 */
442 .supply_name = "buck6",
443 .vsel_reg = BD71837_REG_LDO5_VOLT,
444 .vsel_mask = LDO5_MASK,
445 .enable_reg = BD71837_REG_LDO5_VOLT,
446 .enable_mask = BD71837_LDO_EN,
447 .owner = THIS_MODULE,
448 },
449 {
450 .name = "ldo6",
451 .of_match = of_match_ptr("LDO6"),
452 .regulators_node = of_match_ptr("regulators"),
453 .id = BD71837_LDO6,
454 .ops = &bd71837_ldo_regulator_ops,
455 .type = REGULATOR_VOLTAGE,
456 .n_voltages = BD71837_LDO6_VOLTAGE_NUM,
457 .linear_ranges = bd71837_ldo6_voltage_ranges,
458 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo6_voltage_ranges),
459 /* LDO6 is supplied by buck7 */
460 .supply_name = "buck7",
461 .vsel_reg = BD71837_REG_LDO6_VOLT,
462 .vsel_mask = LDO6_MASK,
463 .enable_reg = BD71837_REG_LDO6_VOLT,
464 .enable_mask = BD71837_LDO_EN,
465 .owner = THIS_MODULE,
466 },
467 {
468 .name = "ldo7",
469 .of_match = of_match_ptr("LDO7"),
470 .regulators_node = of_match_ptr("regulators"),
471 .id = BD71837_LDO7,
472 .ops = &bd71837_ldo_regulator_ops,
473 .type = REGULATOR_VOLTAGE,
474 .n_voltages = BD71837_LDO7_VOLTAGE_NUM,
475 .linear_ranges = bd71837_ldo7_voltage_ranges,
476 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo7_voltage_ranges),
477 .vsel_reg = BD71837_REG_LDO7_VOLT,
478 .vsel_mask = LDO7_MASK,
479 .enable_reg = BD71837_REG_LDO7_VOLT,
480 .enable_mask = BD71837_LDO_EN,
481 .owner = THIS_MODULE,
482 },
483};
484
485struct reg_init {
486 unsigned int reg;
487 unsigned int mask;
488};
489
490static int bd71837_probe(struct platform_device *pdev)
491{
492 struct bd71837_pmic *pmic;
493 struct bd71837_board *pdata;
494 struct regulator_config config = { 0 };
495 struct reg_init pmic_regulator_inits[] = {
496 {
497 .reg = BD71837_REG_BUCK1_CTRL,
498 .mask = BD71837_BUCK_SEL,
499 }, {
500 .reg = BD71837_REG_BUCK2_CTRL,
501 .mask = BD71837_BUCK_SEL,
502 }, {
503 .reg = BD71837_REG_BUCK3_CTRL,
504 .mask = BD71837_BUCK_SEL,
505 }, {
506 .reg = BD71837_REG_BUCK4_CTRL,
507 .mask = BD71837_BUCK_SEL,
508 }, {
509 .reg = BD71837_REG_BUCK5_CTRL,
510 .mask = BD71837_BUCK_SEL,
511 }, {
512 .reg = BD71837_REG_BUCK6_CTRL,
513 .mask = BD71837_BUCK_SEL,
514 }, {
515 .reg = BD71837_REG_BUCK7_CTRL,
516 .mask = BD71837_BUCK_SEL,
517 }, {
518 .reg = BD71837_REG_BUCK8_CTRL,
519 .mask = BD71837_BUCK_SEL,
520 }, {
521 .reg = BD71837_REG_LDO1_VOLT,
522 .mask = BD71837_LDO_SEL,
523 }, {
524 .reg = BD71837_REG_LDO2_VOLT,
525 .mask = BD71837_LDO_SEL,
526 }, {
527 .reg = BD71837_REG_LDO3_VOLT,
528 .mask = BD71837_LDO_SEL,
529 }, {
530 .reg = BD71837_REG_LDO4_VOLT,
531 .mask = BD71837_LDO_SEL,
532 }, {
533 .reg = BD71837_REG_LDO5_VOLT,
534 .mask = BD71837_LDO_SEL,
535 }, {
536 .reg = BD71837_REG_LDO6_VOLT,
537 .mask = BD71837_LDO_SEL,
538 }, {
539 .reg = BD71837_REG_LDO7_VOLT,
540 .mask = BD71837_LDO_SEL,
541 }
542 };
543
544 int i, err;
545
Matti Vaittinenc9dc4cf2018-06-28 14:22:23 +0300546 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
Matti Vaittinenba087992018-05-30 11:43:43 +0300547 if (!pmic)
548 return -ENOMEM;
549
550 memcpy(pmic->descs, bd71837_regulators, sizeof(pmic->descs));
551
552 pmic->pdev = pdev;
553 pmic->mfd = dev_get_drvdata(pdev->dev.parent);
554
555 if (!pmic->mfd) {
556 dev_err(&pdev->dev, "No MFD driver data\n");
557 err = -EINVAL;
558 goto err;
559 }
560 platform_set_drvdata(pdev, pmic);
561 pdata = dev_get_platdata(pmic->mfd->dev);
562
563 /* Register LOCK release */
564 err = regmap_update_bits(pmic->mfd->regmap, BD71837_REG_REGLOCK,
565 (REGLOCK_PWRSEQ | REGLOCK_VREG), 0);
566 if (err) {
567 dev_err(&pmic->pdev->dev, "Failed to unlock PMIC (%d)\n", err);
568 goto err;
569 } else {
Matti Vaittinenc9dc4cf2018-06-28 14:22:23 +0300570 dev_dbg(&pmic->pdev->dev, "Unlocked lock register 0x%x\n",
571 BD71837_REG_REGLOCK);
Matti Vaittinenba087992018-05-30 11:43:43 +0300572 }
573
574 for (i = 0; i < ARRAY_SIZE(pmic_regulator_inits); i++) {
575
576 struct regulator_desc *desc;
577 struct regulator_dev *rdev;
578
579 desc = &pmic->descs[i];
580
581 if (pdata)
582 config.init_data = pdata->init_data[i];
583
584 config.dev = pdev->dev.parent;
585 config.driver_data = pmic;
586 config.regmap = pmic->mfd->regmap;
587
588 rdev = devm_regulator_register(&pdev->dev, desc, &config);
589 if (IS_ERR(rdev)) {
590 dev_err(pmic->mfd->dev,
591 "failed to register %s regulator\n",
592 desc->name);
593 err = PTR_ERR(rdev);
594 goto err;
595 }
596 /* Regulator register gets the regulator constraints and
597 * applies them (set_machine_constraints). This should have
598 * turned the control register(s) to correct values and we
599 * can now switch the control from PMIC state machine to the
600 * register interface
601 */
602 err = regmap_update_bits(pmic->mfd->regmap,
603 pmic_regulator_inits[i].reg,
604 pmic_regulator_inits[i].mask,
605 0xFFFFFFFF);
606 if (err) {
607 dev_err(&pmic->pdev->dev,
608 "Failed to write BUCK/LDO SEL bit for (%s)\n",
609 desc->name);
610 goto err;
611 }
612
613 pmic->rdev[i] = rdev;
614 }
615
Matti Vaittinenba087992018-05-30 11:43:43 +0300616err:
617 return err;
618}
619
620static struct platform_driver bd71837_regulator = {
621 .driver = {
622 .name = "bd71837-pmic",
Matti Vaittinenba087992018-05-30 11:43:43 +0300623 },
624 .probe = bd71837_probe,
625};
626
627module_platform_driver(bd71837_regulator);
628
629MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
630MODULE_DESCRIPTION("BD71837 voltage regulator driver");
631MODULE_LICENSE("GPL");