1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Regulator driver for Ricoh RN5T618 PMIC
4  *
5  * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
6  */
7 
8 #include <linux/mfd/rn5t618.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/of_regulator.h>
15 
16 static const struct regulator_ops rn5t618_reg_ops = {
17 	.enable			= regulator_enable_regmap,
18 	.disable		= regulator_disable_regmap,
19 	.is_enabled		= regulator_is_enabled_regmap,
20 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
21 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
22 	.list_voltage		= regulator_list_voltage_linear,
23 };
24 
25 #define REG(rid, ereg, emask, vreg, vmask, min, max, step)		\
26 	{								\
27 		.name		= #rid,					\
28 		.of_match	= of_match_ptr(#rid),			\
29 		.regulators_node = of_match_ptr("regulators"),		\
30 		.id		= RN5T618_##rid,			\
31 		.type		= REGULATOR_VOLTAGE,			\
32 		.owner		= THIS_MODULE,				\
33 		.ops		= &rn5t618_reg_ops,			\
34 		.n_voltages	= ((max) - (min)) / (step) + 1,		\
35 		.min_uV		= (min),				\
36 		.uV_step	= (step),				\
37 		.enable_reg	= RN5T618_##ereg,			\
38 		.enable_mask	= (emask),				\
39 		.vsel_reg	= RN5T618_##vreg,			\
40 		.vsel_mask	= (vmask),				\
41 	}
42 
43 static const struct regulator_desc rn5t567_regulators[] = {
44 	/* DCDC */
45 	REG(DCDC1, DC1CTL, BIT(0), DC1DAC, 0xff, 600000, 3500000, 12500),
46 	REG(DCDC2, DC2CTL, BIT(0), DC2DAC, 0xff, 600000, 3500000, 12500),
47 	REG(DCDC3, DC3CTL, BIT(0), DC3DAC, 0xff, 600000, 3500000, 12500),
48 	REG(DCDC4, DC4CTL, BIT(0), DC4DAC, 0xff, 600000, 3500000, 12500),
49 	/* LDO */
50 	REG(LDO1, LDOEN1, BIT(0), LDO1DAC, 0x7f, 900000, 3500000, 25000),
51 	REG(LDO2, LDOEN1, BIT(1), LDO2DAC, 0x7f, 900000, 3500000, 25000),
52 	REG(LDO3, LDOEN1, BIT(2), LDO3DAC, 0x7f, 600000, 3500000, 25000),
53 	REG(LDO4, LDOEN1, BIT(3), LDO4DAC, 0x7f, 900000, 3500000, 25000),
54 	REG(LDO5, LDOEN1, BIT(4), LDO5DAC, 0x7f, 900000, 3500000, 25000),
55 	/* LDO RTC */
56 	REG(LDORTC1, LDOEN2, BIT(4), LDORTCDAC, 0x7f, 1200000, 3500000, 25000),
57 	REG(LDORTC2, LDOEN2, BIT(5), LDORTC2DAC, 0x7f, 900000, 3500000, 25000),
58 };
59 
60 static const struct regulator_desc rn5t618_regulators[] = {
61 	/* DCDC */
62 	REG(DCDC1, DC1CTL, BIT(0), DC1DAC, 0xff, 600000, 3500000, 12500),
63 	REG(DCDC2, DC2CTL, BIT(0), DC2DAC, 0xff, 600000, 3500000, 12500),
64 	REG(DCDC3, DC3CTL, BIT(0), DC3DAC, 0xff, 600000, 3500000, 12500),
65 	/* LDO */
66 	REG(LDO1, LDOEN1, BIT(0), LDO1DAC, 0x7f, 900000, 3500000, 25000),
67 	REG(LDO2, LDOEN1, BIT(1), LDO2DAC, 0x7f, 900000, 3500000, 25000),
68 	REG(LDO3, LDOEN1, BIT(2), LDO3DAC, 0x7f, 600000, 3500000, 25000),
69 	REG(LDO4, LDOEN1, BIT(3), LDO4DAC, 0x7f, 900000, 3500000, 25000),
70 	REG(LDO5, LDOEN1, BIT(4), LDO5DAC, 0x7f, 900000, 3500000, 25000),
71 	/* LDO RTC */
72 	REG(LDORTC1, LDOEN2, BIT(4), LDORTCDAC, 0x7f, 1700000, 3500000, 25000),
73 	REG(LDORTC2, LDOEN2, BIT(5), LDORTC2DAC, 0x7f, 900000, 3500000, 25000),
74 };
75 
76 static const struct regulator_desc rc5t619_regulators[] = {
77 	/* DCDC */
78 	REG(DCDC1, DC1CTL, BIT(0), DC1DAC, 0xff, 600000, 3500000, 12500),
79 	REG(DCDC2, DC2CTL, BIT(0), DC2DAC, 0xff, 600000, 3500000, 12500),
80 	REG(DCDC3, DC3CTL, BIT(0), DC3DAC, 0xff, 600000, 3500000, 12500),
81 	REG(DCDC4, DC4CTL, BIT(0), DC4DAC, 0xff, 600000, 3500000, 12500),
82 	REG(DCDC5, DC5CTL, BIT(0), DC5DAC, 0xff, 600000, 3500000, 12500),
83 	/* LDO */
84 	REG(LDO1, LDOEN1, BIT(0), LDO1DAC, 0x7f, 900000, 3500000, 25000),
85 	REG(LDO2, LDOEN1, BIT(1), LDO2DAC, 0x7f, 900000, 3500000, 25000),
86 	REG(LDO3, LDOEN1, BIT(2), LDO3DAC, 0x7f, 900000, 3500000, 25000),
87 	REG(LDO4, LDOEN1, BIT(3), LDO4DAC, 0x7f, 900000, 3500000, 25000),
88 	REG(LDO5, LDOEN1, BIT(4), LDO5DAC, 0x7f, 600000, 3500000, 25000),
89 	REG(LDO6, LDOEN1, BIT(5), LDO6DAC, 0x7f, 600000, 3500000, 25000),
90 	REG(LDO7, LDOEN1, BIT(6), LDO7DAC, 0x7f, 900000, 3500000, 25000),
91 	REG(LDO8, LDOEN1, BIT(7), LDO8DAC, 0x7f, 900000, 3500000, 25000),
92 	REG(LDO9, LDOEN2, BIT(0), LDO9DAC, 0x7f, 900000, 3500000, 25000),
93 	REG(LDO10, LDOEN2, BIT(1), LDO10DAC, 0x7f, 900000, 3500000, 25000),
94 	/* LDO RTC */
95 	REG(LDORTC1, LDOEN2, BIT(4), LDORTCDAC, 0x7f, 1700000, 3500000, 25000),
96 	REG(LDORTC2, LDOEN2, BIT(5), LDORTC2DAC, 0x7f, 900000, 3500000, 25000),
97 };
98 
99 static int rn5t618_regulator_probe(struct platform_device *pdev)
100 {
101 	struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
102 	struct regulator_config config = { };
103 	struct regulator_dev *rdev;
104 	const struct regulator_desc *regulators;
105 	int i;
106 	int num_regulators = 0;
107 
108 	switch (rn5t618->variant) {
109 	case RN5T567:
110 		regulators = rn5t567_regulators;
111 		num_regulators = ARRAY_SIZE(rn5t567_regulators);
112 		break;
113 	case RN5T618:
114 		regulators = rn5t618_regulators;
115 		num_regulators = ARRAY_SIZE(rn5t618_regulators);
116 		break;
117 	case RC5T619:
118 		regulators = rc5t619_regulators;
119 		num_regulators = ARRAY_SIZE(rc5t619_regulators);
120 		break;
121 	default:
122 		return -EINVAL;
123 	}
124 
125 	config.dev = pdev->dev.parent;
126 	config.regmap = rn5t618->regmap;
127 
128 	for (i = 0; i < num_regulators; i++) {
129 		rdev = devm_regulator_register(&pdev->dev,
130 					       &regulators[i],
131 					       &config);
132 		if (IS_ERR(rdev)) {
133 			dev_err(&pdev->dev, "failed to register %s regulator\n",
134 				regulators[i].name);
135 			return PTR_ERR(rdev);
136 		}
137 	}
138 
139 	return 0;
140 }
141 
142 static struct platform_driver rn5t618_regulator_driver = {
143 	.probe = rn5t618_regulator_probe,
144 	.driver = {
145 		.name	= "rn5t618-regulator",
146 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
147 	},
148 };
149 
150 module_platform_driver(rn5t618_regulator_driver);
151 
152 MODULE_ALIAS("platform:rn5t618-regulator");
153 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
154 MODULE_DESCRIPTION("RN5T618 regulator driver");
155 MODULE_LICENSE("GPL v2");
156