Linux Audio

Check our new training course

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