Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0+
  2//
  3// da9210-regulator.c - Regulator device driver for DA9210
  4// Copyright (C) 2013  Dialog Semiconductor Ltd.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  5
  6#include <linux/err.h>
  7#include <linux/i2c.h>
  8#include <linux/module.h>
  9#include <linux/interrupt.h>
 10#include <linux/irq.h>
 11#include <linux/regulator/driver.h>
 12#include <linux/regulator/machine.h>
 13#include <linux/of_device.h>
 14#include <linux/regulator/of_regulator.h>
 15#include <linux/regmap.h>
 16
 17#include "da9210-regulator.h"
 18
 19struct da9210 {
 20	struct regulator_dev *rdev;
 21	struct regmap *regmap;
 22};
 23
 24static const struct regmap_config da9210_regmap_config = {
 25	.reg_bits = 8,
 26	.val_bits = 8,
 27};
 28
 29static const struct regulator_ops da9210_buck_ops = {
 
 
 
 
 30	.enable = regulator_enable_regmap,
 31	.disable = regulator_disable_regmap,
 32	.is_enabled = regulator_is_enabled_regmap,
 33	.set_voltage_sel = regulator_set_voltage_sel_regmap,
 34	.get_voltage_sel = regulator_get_voltage_sel_regmap,
 35	.list_voltage = regulator_list_voltage_linear,
 36	.set_current_limit = regulator_set_current_limit_regmap,
 37	.get_current_limit = regulator_get_current_limit_regmap,
 38};
 39
 40/* Default limits measured in millivolts and milliamps */
 41#define DA9210_MIN_MV		300
 42#define DA9210_MAX_MV		1570
 43#define DA9210_STEP_MV		10
 44
 45/* Current limits for buck (uA) indices corresponds with register values */
 46static const unsigned int da9210_buck_limits[] = {
 47	1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000,
 48	3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000
 49};
 50
 51static const struct regulator_desc da9210_reg = {
 52	.name = "DA9210",
 53	.id = 0,
 54	.ops = &da9210_buck_ops,
 55	.type = REGULATOR_VOLTAGE,
 56	.n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1,
 57	.min_uV = (DA9210_MIN_MV * 1000),
 58	.uV_step = (DA9210_STEP_MV * 1000),
 59	.vsel_reg = DA9210_REG_VBUCK_A,
 60	.vsel_mask = DA9210_VBUCK_MASK,
 61	.enable_reg = DA9210_REG_BUCK_CONT,
 62	.enable_mask = DA9210_BUCK_EN,
 63	.owner = THIS_MODULE,
 64	.curr_table = da9210_buck_limits,
 65	.n_current_limits = ARRAY_SIZE(da9210_buck_limits),
 66	.csel_reg = DA9210_REG_BUCK_ILIM,
 67	.csel_mask = DA9210_BUCK_ILIM_MASK,
 68};
 69
 70static irqreturn_t da9210_irq_handler(int irq, void *data)
 
 71{
 72	struct da9210 *chip = data;
 73	unsigned int val, handled = 0;
 74	int error, ret = IRQ_NONE;
 75
 76	error = regmap_read(chip->regmap, DA9210_REG_EVENT_B, &val);
 77	if (error < 0)
 78		goto error_i2c;
 79
 80	regulator_lock(chip->rdev);
 81
 82	if (val & DA9210_E_OVCURR) {
 83		regulator_notifier_call_chain(chip->rdev,
 84					      REGULATOR_EVENT_OVER_CURRENT,
 85					      NULL);
 86		handled |= DA9210_E_OVCURR;
 87	}
 88	if (val & DA9210_E_NPWRGOOD) {
 89		regulator_notifier_call_chain(chip->rdev,
 90					      REGULATOR_EVENT_UNDER_VOLTAGE,
 91					      NULL);
 92		handled |= DA9210_E_NPWRGOOD;
 93	}
 94	if (val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT)) {
 95		regulator_notifier_call_chain(chip->rdev,
 96					      REGULATOR_EVENT_OVER_TEMP, NULL);
 97		handled |= val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT);
 98	}
 99	if (val & DA9210_E_VMAX) {
100		regulator_notifier_call_chain(chip->rdev,
101					      REGULATOR_EVENT_REGULATION_OUT,
102					      NULL);
103		handled |= DA9210_E_VMAX;
104	}
105
106	regulator_unlock(chip->rdev);
107
108	if (handled) {
109		/* Clear handled events */
110		error = regmap_write(chip->regmap, DA9210_REG_EVENT_B, handled);
111		if (error < 0)
112			goto error_i2c;
113
114		ret = IRQ_HANDLED;
115	}
 
 
 
 
 
 
 
 
116
117	return ret;
 
118
119error_i2c:
120	dev_err(regmap_get_device(chip->regmap), "I2C error : %d\n", error);
121	return ret;
122}
123
124/*
125 * I2C driver interface functions
126 */
127
128static const struct of_device_id da9210_dt_ids[] = {
129	{ .compatible = "dlg,da9210", },
130	{ }
131};
132MODULE_DEVICE_TABLE(of, da9210_dt_ids);
133
134static int da9210_i2c_probe(struct i2c_client *i2c)
135{
136	struct da9210 *chip;
137	struct device *dev = &i2c->dev;
138	struct da9210_pdata *pdata = dev_get_platdata(dev);
139	struct regulator_dev *rdev = NULL;
140	struct regulator_config config = { };
141	int error;
142	const struct of_device_id *match;
143
144	if (i2c->dev.of_node && !pdata) {
145		match = of_match_device(of_match_ptr(da9210_dt_ids),
146						&i2c->dev);
147		if (!match) {
148			dev_err(&i2c->dev, "Error: No device match found\n");
149			return -ENODEV;
150		}
151	}
152
153	chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL);
154	if (!chip)
155		return -ENOMEM;
156
157	chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config);
158	if (IS_ERR(chip->regmap)) {
159		error = PTR_ERR(chip->regmap);
160		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
161			error);
162		return error;
163	}
164
165	config.dev = &i2c->dev;
166	config.init_data = pdata ? &pdata->da9210_constraints :
167		of_get_regulator_init_data(dev, dev->of_node, &da9210_reg);
168	config.driver_data = chip;
169	config.regmap = chip->regmap;
170	config.of_node = dev->of_node;
171
172	/* Mask all interrupt sources to deassert interrupt line */
173	error = regmap_write(chip->regmap, DA9210_REG_MASK_A, ~0);
174	if (!error)
175		error = regmap_write(chip->regmap, DA9210_REG_MASK_B, ~0);
176	if (error) {
177		dev_err(&i2c->dev, "Failed to write to mask reg: %d\n", error);
178		return error;
179	}
180
181	rdev = devm_regulator_register(&i2c->dev, &da9210_reg, &config);
182	if (IS_ERR(rdev)) {
183		dev_err(&i2c->dev, "Failed to register DA9210 regulator\n");
184		return PTR_ERR(rdev);
185	}
186
187	chip->rdev = rdev;
188	if (i2c->irq) {
189		error = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
190						  da9210_irq_handler,
191						  IRQF_TRIGGER_LOW |
192						  IRQF_ONESHOT | IRQF_SHARED,
193						  "da9210", chip);
194		if (error) {
195			dev_err(&i2c->dev, "Failed to request IRQ%u: %d\n",
196				i2c->irq, error);
197			return error;
198		}
199
200		error = regmap_update_bits(chip->regmap, DA9210_REG_MASK_B,
201					 DA9210_M_OVCURR | DA9210_M_NPWRGOOD |
202					 DA9210_M_TEMP_WARN |
203					 DA9210_M_TEMP_CRIT | DA9210_M_VMAX, 0);
204		if (error < 0) {
205			dev_err(&i2c->dev, "Failed to update mask reg: %d\n",
206				error);
207			return error;
208		}
209	} else {
210		dev_warn(&i2c->dev, "No IRQ configured\n");
211	}
212
213	i2c_set_clientdata(i2c, chip);
214
215	return 0;
216}
217
218static const struct i2c_device_id da9210_i2c_id[] = {
219	{"da9210", 0},
220	{},
221};
222
223MODULE_DEVICE_TABLE(i2c, da9210_i2c_id);
224
225static struct i2c_driver da9210_regulator_driver = {
226	.driver = {
227		.name = "da9210",
228		.of_match_table = of_match_ptr(da9210_dt_ids),
229	},
230	.probe_new = da9210_i2c_probe,
231	.id_table = da9210_i2c_id,
232};
233
234module_i2c_driver(da9210_regulator_driver);
235
236MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
237MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210");
238MODULE_LICENSE("GPL v2");
v3.15
  1/*
  2 * da9210-regulator.c - Regulator device driver for DA9210
  3 * Copyright (C) 2013  Dialog Semiconductor Ltd.
  4 *
  5 * This library is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU Library General Public
  7 * License as published by the Free Software Foundation; either
  8 * version 2 of the License, or (at your option) any later version.
  9 *
 10 * This library is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13 * Library General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU Library General Public
 16 * License along with this library; if not, write to the
 17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 18 * Boston, MA  02110-1301, USA.
 19 */
 20
 21#include <linux/err.h>
 22#include <linux/i2c.h>
 23#include <linux/module.h>
 24#include <linux/init.h>
 25#include <linux/slab.h>
 26#include <linux/regulator/driver.h>
 27#include <linux/regulator/machine.h>
 
 28#include <linux/regulator/of_regulator.h>
 29#include <linux/regmap.h>
 30
 31#include "da9210-regulator.h"
 32
 33struct da9210 {
 34	struct regulator_dev *rdev;
 35	struct regmap *regmap;
 36};
 37
 38static const struct regmap_config da9210_regmap_config = {
 39	.reg_bits = 8,
 40	.val_bits = 8,
 41};
 42
 43static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
 44				    int max_uA);
 45static int da9210_get_current_limit(struct regulator_dev *rdev);
 46
 47static struct regulator_ops da9210_buck_ops = {
 48	.enable = regulator_enable_regmap,
 49	.disable = regulator_disable_regmap,
 50	.is_enabled = regulator_is_enabled_regmap,
 51	.set_voltage_sel = regulator_set_voltage_sel_regmap,
 52	.get_voltage_sel = regulator_get_voltage_sel_regmap,
 53	.list_voltage = regulator_list_voltage_linear,
 54	.set_current_limit = da9210_set_current_limit,
 55	.get_current_limit = da9210_get_current_limit,
 56};
 57
 58/* Default limits measured in millivolts and milliamps */
 59#define DA9210_MIN_MV		300
 60#define DA9210_MAX_MV		1570
 61#define DA9210_STEP_MV		10
 62
 63/* Current limits for buck (uA) indices corresponds with register values */
 64static const int da9210_buck_limits[] = {
 65	1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000,
 66	3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000
 67};
 68
 69static const struct regulator_desc da9210_reg = {
 70	.name = "DA9210",
 71	.id = 0,
 72	.ops = &da9210_buck_ops,
 73	.type = REGULATOR_VOLTAGE,
 74	.n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1,
 75	.min_uV = (DA9210_MIN_MV * 1000),
 76	.uV_step = (DA9210_STEP_MV * 1000),
 77	.vsel_reg = DA9210_REG_VBUCK_A,
 78	.vsel_mask = DA9210_VBUCK_MASK,
 79	.enable_reg = DA9210_REG_BUCK_CONT,
 80	.enable_mask = DA9210_BUCK_EN,
 81	.owner = THIS_MODULE,
 
 
 
 
 82};
 83
 84static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
 85				    int max_uA)
 86{
 87	struct da9210 *chip = rdev_get_drvdata(rdev);
 88	unsigned int sel;
 89	int i;
 90
 91	/* search for closest to maximum */
 92	for (i = ARRAY_SIZE(da9210_buck_limits)-1; i >= 0; i--) {
 93		if (min_uA <= da9210_buck_limits[i] &&
 94		    max_uA >= da9210_buck_limits[i]) {
 95			sel = i;
 96			sel = sel << DA9210_BUCK_ILIM_SHIFT;
 97			return regmap_update_bits(chip->regmap,
 98						  DA9210_REG_BUCK_ILIM,
 99						  DA9210_BUCK_ILIM_MASK, sel);
100		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101	}
102
103	return -EINVAL;
104}
 
 
 
 
 
105
106static int da9210_get_current_limit(struct regulator_dev *rdev)
107{
108	struct da9210 *chip = rdev_get_drvdata(rdev);
109	unsigned int data;
110	unsigned int sel;
111	int ret;
112
113	ret = regmap_read(chip->regmap, DA9210_REG_BUCK_ILIM, &data);
114	if (ret < 0)
115		return ret;
116
117	/* select one of 16 values: 0000 (1600mA) to 1111 (4600mA) */
118	sel = (data & DA9210_BUCK_ILIM_MASK) >> DA9210_BUCK_ILIM_SHIFT;
119
120	return da9210_buck_limits[sel];
 
 
121}
122
123/*
124 * I2C driver interface functions
125 */
126static int da9210_i2c_probe(struct i2c_client *i2c,
127			    const struct i2c_device_id *id)
 
 
 
 
 
 
128{
129	struct da9210 *chip;
130	struct device *dev = &i2c->dev;
131	struct da9210_pdata *pdata = dev_get_platdata(dev);
132	struct regulator_dev *rdev = NULL;
133	struct regulator_config config = { };
134	int error;
 
 
 
 
 
 
 
 
 
 
135
136	chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL);
137	if (!chip)
138		return -ENOMEM;
139
140	chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config);
141	if (IS_ERR(chip->regmap)) {
142		error = PTR_ERR(chip->regmap);
143		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
144			error);
145		return error;
146	}
147
148	config.dev = &i2c->dev;
149	config.init_data = pdata ? &pdata->da9210_constraints :
150		of_get_regulator_init_data(dev, dev->of_node);
151	config.driver_data = chip;
152	config.regmap = chip->regmap;
153	config.of_node = dev->of_node;
154
 
 
 
 
 
 
 
 
 
155	rdev = devm_regulator_register(&i2c->dev, &da9210_reg, &config);
156	if (IS_ERR(rdev)) {
157		dev_err(&i2c->dev, "Failed to register DA9210 regulator\n");
158		return PTR_ERR(rdev);
159	}
160
161	chip->rdev = rdev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
163	i2c_set_clientdata(i2c, chip);
164
165	return 0;
166}
167
168static const struct i2c_device_id da9210_i2c_id[] = {
169	{"da9210", 0},
170	{},
171};
172
173MODULE_DEVICE_TABLE(i2c, da9210_i2c_id);
174
175static struct i2c_driver da9210_regulator_driver = {
176	.driver = {
177		.name = "da9210",
178		.owner = THIS_MODULE,
179	},
180	.probe = da9210_i2c_probe,
181	.id_table = da9210_i2c_id,
182};
183
184module_i2c_driver(da9210_regulator_driver);
185
186MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
187MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210");
188MODULE_LICENSE("GPL v2");