Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  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/interrupt.h>
 26#include <linux/irq.h>
 27#include <linux/slab.h>
 28#include <linux/regulator/driver.h>
 29#include <linux/regulator/machine.h>
 30#include <linux/regulator/of_regulator.h>
 31#include <linux/regmap.h>
 32
 33#include "da9210-regulator.h"
 34
 35struct da9210 {
 36	struct regulator_dev *rdev;
 37	struct regmap *regmap;
 38};
 39
 40static const struct regmap_config da9210_regmap_config = {
 41	.reg_bits = 8,
 42	.val_bits = 8,
 43};
 44
 45static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
 46				    int max_uA);
 47static int da9210_get_current_limit(struct regulator_dev *rdev);
 48
 49static const struct regulator_ops da9210_buck_ops = {
 50	.enable = regulator_enable_regmap,
 51	.disable = regulator_disable_regmap,
 52	.is_enabled = regulator_is_enabled_regmap,
 53	.set_voltage_sel = regulator_set_voltage_sel_regmap,
 54	.get_voltage_sel = regulator_get_voltage_sel_regmap,
 55	.list_voltage = regulator_list_voltage_linear,
 56	.set_current_limit = da9210_set_current_limit,
 57	.get_current_limit = da9210_get_current_limit,
 58};
 59
 60/* Default limits measured in millivolts and milliamps */
 61#define DA9210_MIN_MV		300
 62#define DA9210_MAX_MV		1570
 63#define DA9210_STEP_MV		10
 64
 65/* Current limits for buck (uA) indices corresponds with register values */
 66static const int da9210_buck_limits[] = {
 67	1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000,
 68	3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000
 69};
 70
 71static const struct regulator_desc da9210_reg = {
 72	.name = "DA9210",
 73	.id = 0,
 74	.ops = &da9210_buck_ops,
 75	.type = REGULATOR_VOLTAGE,
 76	.n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1,
 77	.min_uV = (DA9210_MIN_MV * 1000),
 78	.uV_step = (DA9210_STEP_MV * 1000),
 79	.vsel_reg = DA9210_REG_VBUCK_A,
 80	.vsel_mask = DA9210_VBUCK_MASK,
 81	.enable_reg = DA9210_REG_BUCK_CONT,
 82	.enable_mask = DA9210_BUCK_EN,
 83	.owner = THIS_MODULE,
 84};
 85
 86static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
 87				    int max_uA)
 88{
 89	struct da9210 *chip = rdev_get_drvdata(rdev);
 90	unsigned int sel;
 91	int i;
 92
 93	/* search for closest to maximum */
 94	for (i = ARRAY_SIZE(da9210_buck_limits)-1; i >= 0; i--) {
 95		if (min_uA <= da9210_buck_limits[i] &&
 96		    max_uA >= da9210_buck_limits[i]) {
 97			sel = i;
 98			sel = sel << DA9210_BUCK_ILIM_SHIFT;
 99			return regmap_update_bits(chip->regmap,
100						  DA9210_REG_BUCK_ILIM,
101						  DA9210_BUCK_ILIM_MASK, sel);
102		}
103	}
104
105	return -EINVAL;
106}
107
108static int da9210_get_current_limit(struct regulator_dev *rdev)
109{
110	struct da9210 *chip = rdev_get_drvdata(rdev);
111	unsigned int data;
112	unsigned int sel;
113	int ret;
114
115	ret = regmap_read(chip->regmap, DA9210_REG_BUCK_ILIM, &data);
116	if (ret < 0)
117		return ret;
118
119	/* select one of 16 values: 0000 (1600mA) to 1111 (4600mA) */
120	sel = (data & DA9210_BUCK_ILIM_MASK) >> DA9210_BUCK_ILIM_SHIFT;
121
122	return da9210_buck_limits[sel];
123}
124
125static irqreturn_t da9210_irq_handler(int irq, void *data)
126{
127	struct da9210 *chip = data;
128	unsigned int val, handled = 0;
129	int error, ret = IRQ_NONE;
130
131	error = regmap_read(chip->regmap, DA9210_REG_EVENT_B, &val);
132	if (error < 0)
133		goto error_i2c;
134
135	mutex_lock(&chip->rdev->mutex);
136
137	if (val & DA9210_E_OVCURR) {
138		regulator_notifier_call_chain(chip->rdev,
139					      REGULATOR_EVENT_OVER_CURRENT,
140					      NULL);
141		handled |= DA9210_E_OVCURR;
142	}
143	if (val & DA9210_E_NPWRGOOD) {
144		regulator_notifier_call_chain(chip->rdev,
145					      REGULATOR_EVENT_UNDER_VOLTAGE,
146					      NULL);
147		handled |= DA9210_E_NPWRGOOD;
148	}
149	if (val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT)) {
150		regulator_notifier_call_chain(chip->rdev,
151					      REGULATOR_EVENT_OVER_TEMP, NULL);
152		handled |= val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT);
153	}
154	if (val & DA9210_E_VMAX) {
155		regulator_notifier_call_chain(chip->rdev,
156					      REGULATOR_EVENT_REGULATION_OUT,
157					      NULL);
158		handled |= DA9210_E_VMAX;
159	}
160
161	mutex_unlock(&chip->rdev->mutex);
162
163	if (handled) {
164		/* Clear handled events */
165		error = regmap_write(chip->regmap, DA9210_REG_EVENT_B, handled);
166		if (error < 0)
167			goto error_i2c;
168
169		ret = IRQ_HANDLED;
170	}
171
172	return ret;
173
174error_i2c:
175	dev_err(regmap_get_device(chip->regmap), "I2C error : %d\n", error);
176	return ret;
177}
178
179/*
180 * I2C driver interface functions
181 */
182static int da9210_i2c_probe(struct i2c_client *i2c,
183			    const struct i2c_device_id *id)
184{
185	struct da9210 *chip;
186	struct device *dev = &i2c->dev;
187	struct da9210_pdata *pdata = dev_get_platdata(dev);
188	struct regulator_dev *rdev = NULL;
189	struct regulator_config config = { };
190	int error;
191
192	chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL);
193	if (!chip)
194		return -ENOMEM;
195
196	chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config);
197	if (IS_ERR(chip->regmap)) {
198		error = PTR_ERR(chip->regmap);
199		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
200			error);
201		return error;
202	}
203
204	config.dev = &i2c->dev;
205	config.init_data = pdata ? &pdata->da9210_constraints :
206		of_get_regulator_init_data(dev, dev->of_node, &da9210_reg);
207	config.driver_data = chip;
208	config.regmap = chip->regmap;
209	config.of_node = dev->of_node;
210
211	/* Mask all interrupt sources to deassert interrupt line */
212	error = regmap_write(chip->regmap, DA9210_REG_MASK_A, ~0);
213	if (!error)
214		error = regmap_write(chip->regmap, DA9210_REG_MASK_B, ~0);
215	if (error) {
216		dev_err(&i2c->dev, "Failed to write to mask reg: %d\n", error);
217		return error;
218	}
219
220	rdev = devm_regulator_register(&i2c->dev, &da9210_reg, &config);
221	if (IS_ERR(rdev)) {
222		dev_err(&i2c->dev, "Failed to register DA9210 regulator\n");
223		return PTR_ERR(rdev);
224	}
225
226	chip->rdev = rdev;
227	if (i2c->irq) {
228		error = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
229						  da9210_irq_handler,
230						  IRQF_TRIGGER_LOW |
231						  IRQF_ONESHOT | IRQF_SHARED,
232						  "da9210", chip);
233		if (error) {
234			dev_err(&i2c->dev, "Failed to request IRQ%u: %d\n",
235				i2c->irq, error);
236			return error;
237		}
238
239		error = regmap_update_bits(chip->regmap, DA9210_REG_MASK_B,
240					 DA9210_M_OVCURR | DA9210_M_NPWRGOOD |
241					 DA9210_M_TEMP_WARN |
242					 DA9210_M_TEMP_CRIT | DA9210_M_VMAX, 0);
243		if (error < 0) {
244			dev_err(&i2c->dev, "Failed to update mask reg: %d\n",
245				error);
246			return error;
247		}
248	} else {
249		dev_warn(&i2c->dev, "No IRQ configured\n");
250	}
251
252	i2c_set_clientdata(i2c, chip);
253
254	return 0;
255}
256
257static const struct i2c_device_id da9210_i2c_id[] = {
258	{"da9210", 0},
259	{},
260};
261
262MODULE_DEVICE_TABLE(i2c, da9210_i2c_id);
263
264static struct i2c_driver da9210_regulator_driver = {
265	.driver = {
266		.name = "da9210",
267	},
268	.probe = da9210_i2c_probe,
269	.id_table = da9210_i2c_id,
270};
271
272module_i2c_driver(da9210_regulator_driver);
273
274MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
275MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210");
276MODULE_LICENSE("GPL v2");