1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Regulator driver for LP873X PMIC
4 *
5 * Copyright (C) 2016 Texas Instruments Incorporated - https://www.ti.com/
6 */
7
8#include <linux/bitfield.h>
9#include <linux/module.h>
10#include <linux/platform_device.h>
11#include <linux/regmap.h>
12
13#include <linux/mfd/lp873x.h>
14
15#define LP873X_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, _er, _em, \
16			 _delay, _lr, _cr)				\
17	[_id] = {							\
18		.desc = {						\
19			.name			= _name,		\
20			.supply_name		= _of "-in",		\
21			.id			= _id,			\
22			.of_match		= of_match_ptr(_of),	\
23			.regulators_node	= of_match_ptr("regulators"),\
24			.ops			= &_ops,		\
25			.n_voltages		= _n,			\
26			.type			= REGULATOR_VOLTAGE,	\
27			.owner			= THIS_MODULE,		\
28			.vsel_reg		= _vr,			\
29			.vsel_mask		= _vm,			\
30			.enable_reg		= _er,			\
31			.enable_mask		= _em,			\
32			.ramp_delay		= _delay,		\
33			.linear_ranges		= _lr,			\
34			.n_linear_ranges	= ARRAY_SIZE(_lr),	\
35			.curr_table	= lp873x_buck_uA,		\
36			.n_current_limits = ARRAY_SIZE(lp873x_buck_uA),	\
37			.csel_reg	= (_cr),			\
38			.csel_mask	= LP873X_BUCK0_CTRL_2_BUCK0_ILIM,\
39		},							\
40		.ctrl2_reg = _cr,					\
41	}
42
43struct lp873x_regulator {
44	struct regulator_desc desc;
45	unsigned int ctrl2_reg;
46};
47
48static const struct lp873x_regulator regulators[];
49
50static const struct linear_range buck0_buck1_ranges[] = {
51	REGULATOR_LINEAR_RANGE(0, 0x0, 0x13, 0),
52	REGULATOR_LINEAR_RANGE(700000, 0x14, 0x17, 10000),
53	REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000),
54	REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000),
55};
56
57static const struct linear_range ldo0_ldo1_ranges[] = {
58	REGULATOR_LINEAR_RANGE(800000, 0x0, 0x19, 100000),
59};
60
61static const unsigned int lp873x_buck_ramp_delay[] = {
62	30000, 15000, 10000, 7500, 3800, 1900, 940, 470
63};
64
65/* LP873X BUCK current limit */
66static const unsigned int lp873x_buck_uA[] = {
67	1500000, 2000000, 2500000, 3000000, 3500000, 4000000,
68};
69
70static int lp873x_buck_set_ramp_delay(struct regulator_dev *rdev,
71				      int ramp_delay)
72{
73	int id = rdev_get_id(rdev);
74	struct lp873x *lp873 = rdev_get_drvdata(rdev);
75	unsigned int reg;
76	int ret;
77
78	if (ramp_delay <= 470)
79		reg = 7;
80	else if (ramp_delay <= 940)
81		reg = 6;
82	else if (ramp_delay <= 1900)
83		reg = 5;
84	else if (ramp_delay <= 3800)
85		reg = 4;
86	else if (ramp_delay <= 7500)
87		reg = 3;
88	else if (ramp_delay <= 10000)
89		reg = 2;
90	else if (ramp_delay <= 15000)
91		reg = 1;
92	else
93		reg = 0;
94
95	ret = regmap_update_bits(lp873->regmap, regulators[id].ctrl2_reg,
96				 LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE,
97				 FIELD_PREP(LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE, reg));
98	if (ret) {
99		dev_err(lp873->dev, "SLEW RATE write failed: %d\n", ret);
100		return ret;
101	}
102
103	rdev->constraints->ramp_delay = lp873x_buck_ramp_delay[reg];
104
105	return 0;
106}
107
108/* Operations permitted on BUCK0, BUCK1 */
109static const struct regulator_ops lp873x_buck01_ops = {
110	.is_enabled		= regulator_is_enabled_regmap,
111	.enable			= regulator_enable_regmap,
112	.disable		= regulator_disable_regmap,
113	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
114	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
115	.list_voltage		= regulator_list_voltage_linear_range,
116	.map_voltage		= regulator_map_voltage_linear_range,
117	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
118	.set_ramp_delay		= lp873x_buck_set_ramp_delay,
119	.set_current_limit	= regulator_set_current_limit_regmap,
120	.get_current_limit	= regulator_get_current_limit_regmap,
121};
122
123/* Operations permitted on LDO0 and LDO1 */
124static const struct regulator_ops lp873x_ldo01_ops = {
125	.is_enabled		= regulator_is_enabled_regmap,
126	.enable			= regulator_enable_regmap,
127	.disable		= regulator_disable_regmap,
128	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
129	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
130	.list_voltage		= regulator_list_voltage_linear_range,
131	.map_voltage		= regulator_map_voltage_linear_range,
132};
133
134static const struct lp873x_regulator regulators[] = {
135	LP873X_REGULATOR("BUCK0", LP873X_BUCK_0, "buck0", lp873x_buck01_ops,
136			 256, LP873X_REG_BUCK0_VOUT,
137			 LP873X_BUCK0_VOUT_BUCK0_VSET, LP873X_REG_BUCK0_CTRL_1,
138			 LP873X_BUCK0_CTRL_1_BUCK0_EN, 10000,
139			 buck0_buck1_ranges, LP873X_REG_BUCK0_CTRL_2),
140	LP873X_REGULATOR("BUCK1", LP873X_BUCK_1, "buck1", lp873x_buck01_ops,
141			 256, LP873X_REG_BUCK1_VOUT,
142			 LP873X_BUCK1_VOUT_BUCK1_VSET, LP873X_REG_BUCK1_CTRL_1,
143			 LP873X_BUCK1_CTRL_1_BUCK1_EN, 10000,
144			 buck0_buck1_ranges, LP873X_REG_BUCK1_CTRL_2),
145	LP873X_REGULATOR("LDO0", LP873X_LDO_0, "ldo0", lp873x_ldo01_ops, 26,
146			 LP873X_REG_LDO0_VOUT, LP873X_LDO0_VOUT_LDO0_VSET,
147			 LP873X_REG_LDO0_CTRL,
148			 LP873X_LDO0_CTRL_LDO0_EN, 0, ldo0_ldo1_ranges, 0xFF),
149	LP873X_REGULATOR("LDO1", LP873X_LDO_1, "ldo1", lp873x_ldo01_ops, 26,
150			 LP873X_REG_LDO1_VOUT, LP873X_LDO1_VOUT_LDO1_VSET,
151			 LP873X_REG_LDO1_CTRL,
152			 LP873X_LDO1_CTRL_LDO1_EN, 0, ldo0_ldo1_ranges, 0xFF),
153};
154
155static int lp873x_regulator_probe(struct platform_device *pdev)
156{
157	struct lp873x *lp873 = dev_get_drvdata(pdev->dev.parent);
158	struct regulator_config config = { };
159	struct regulator_dev *rdev;
160	int i;
161
162	platform_set_drvdata(pdev, lp873);
163
164	config.dev = &pdev->dev;
165	config.dev->of_node = lp873->dev->of_node;
166	config.driver_data = lp873;
167	config.regmap = lp873->regmap;
168
169	for (i = 0; i < ARRAY_SIZE(regulators); i++) {
170		rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
171					       &config);
172		if (IS_ERR(rdev)) {
173			dev_err(lp873->dev, "failed to register %s regulator\n",
174				pdev->name);
175			return PTR_ERR(rdev);
176		}
177	}
178
179	return 0;
180}
181
182static const struct platform_device_id lp873x_regulator_id_table[] = {
183	{ "lp873x-regulator", },
184	{ /* sentinel */ }
185};
186MODULE_DEVICE_TABLE(platform, lp873x_regulator_id_table);
187
188static struct platform_driver lp873x_regulator_driver = {
189	.driver = {
190		.name = "lp873x-pmic",
191		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
192	},
193	.probe = lp873x_regulator_probe,
194	.id_table = lp873x_regulator_id_table,
195};
196module_platform_driver(lp873x_regulator_driver);
197
198MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
199MODULE_DESCRIPTION("LP873X voltage regulator driver");
200MODULE_ALIAS("platform:lp873x-pmic");
201MODULE_LICENSE("GPL v2");
202