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