blob: 1482adafa1ad43eafa970c53b671b264c1e1a572 [file] [log] [blame]
James Ban1028a372014-07-14 13:48:45 +09001/*
2 * da9211-regulator.c - Regulator device driver for DA9211
3 * Copyright (C) 2014 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
16#include <linux/err.h>
17#include <linux/gpio.h>
18#include <linux/i2c.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/slab.h>
22#include <linux/regulator/driver.h>
23#include <linux/regulator/machine.h>
24#include <linux/regmap.h>
25#include <linux/irq.h>
26#include <linux/interrupt.h>
27#include <linux/regulator/da9211.h>
28#include "da9211-regulator.h"
29
30#define DA9211_BUCK_MODE_SLEEP 1
31#define DA9211_BUCK_MODE_SYNC 2
32#define DA9211_BUCK_MODE_AUTO 3
33
34/* DA9211 REGULATOR IDs */
35#define DA9211_ID_BUCKA 0
36#define DA9211_ID_BUCKB 1
37
38struct da9211 {
39 struct device *dev;
40 struct regmap *regmap;
41 struct da9211_pdata *pdata;
42 struct regulator_dev *rdev[DA9211_MAX_REGULATORS];
43 int num_regulator;
44 int chip_irq;
45};
46
47static const struct regmap_range_cfg da9211_regmap_range[] = {
48 {
49 .selector_reg = DA9211_REG_PAGE_CON,
50 .selector_mask = DA9211_REG_PAGE_MASK,
51 .selector_shift = DA9211_REG_PAGE_SHIFT,
52 .window_start = 0,
53 .window_len = 256,
54 .range_min = 0,
55 .range_max = 2*256,
56 },
57};
58
59static const struct regmap_config da9211_regmap_config = {
60 .reg_bits = 8,
61 .val_bits = 8,
62 .max_register = 2 * 256,
63 .ranges = da9211_regmap_range,
64 .num_ranges = ARRAY_SIZE(da9211_regmap_range),
65};
66
67/* Default limits measured in millivolts and milliamps */
68#define DA9211_MIN_MV 300
69#define DA9211_MAX_MV 1570
70#define DA9211_STEP_MV 10
71
72/* Current limits for buck (uA) indices corresponds with register values */
73static const int da9211_current_limits[] = {
74 2000000, 2200000, 2400000, 2600000, 2800000, 3000000, 3200000, 3400000,
75 3600000, 3800000, 4000000, 4200000, 4400000, 4600000, 4800000, 5000000
76};
77
78static unsigned int da9211_buck_get_mode(struct regulator_dev *rdev)
79{
80 int id = rdev_get_id(rdev);
81 struct da9211 *chip = rdev_get_drvdata(rdev);
82 unsigned int data;
83 int ret, mode = 0;
84
85 ret = regmap_read(chip->regmap, DA9211_REG_BUCKA_CONF+id, &data);
86 if (ret < 0)
87 return ret;
88
89 switch (data & 0x03) {
90 case DA9211_BUCK_MODE_SYNC:
91 mode = REGULATOR_MODE_FAST;
92 break;
93 case DA9211_BUCK_MODE_AUTO:
94 mode = REGULATOR_MODE_NORMAL;
95 break;
96 case DA9211_BUCK_MODE_SLEEP:
97 mode = REGULATOR_MODE_STANDBY;
98 break;
99 }
100
101 return mode;
102}
103
104static int da9211_buck_set_mode(struct regulator_dev *rdev,
105 unsigned int mode)
106{
107 int id = rdev_get_id(rdev);
108 struct da9211 *chip = rdev_get_drvdata(rdev);
109 int val = 0;
110
111 switch (mode) {
112 case REGULATOR_MODE_FAST:
113 val = DA9211_BUCK_MODE_SYNC;
114 break;
115 case REGULATOR_MODE_NORMAL:
116 val = DA9211_BUCK_MODE_AUTO;
117 break;
118 case REGULATOR_MODE_STANDBY:
119 val = DA9211_BUCK_MODE_SLEEP;
120 break;
121 }
122
123 return regmap_update_bits(chip->regmap, DA9211_REG_BUCKA_CONF+id,
124 0x03, val);
125}
126
127static int da9211_set_current_limit(struct regulator_dev *rdev, int min,
128 int max)
129{
130 int id = rdev_get_id(rdev);
131 struct da9211 *chip = rdev_get_drvdata(rdev);
132 int i;
133
134 /* search for closest to maximum */
135 for (i = ARRAY_SIZE(da9211_current_limits)-1; i >= 0; i--) {
136 if (min <= da9211_current_limits[i] &&
137 max >= da9211_current_limits[i]) {
138 return regmap_update_bits(chip->regmap,
139 DA9211_REG_BUCK_ILIM,
140 (0x0F << id*4), (i << id*4));
141 }
142 }
143
144 return -EINVAL;
145}
146
147static int da9211_get_current_limit(struct regulator_dev *rdev)
148{
149 int id = rdev_get_id(rdev);
150 struct da9211 *chip = rdev_get_drvdata(rdev);
151 unsigned int data;
152 int ret;
153
154 ret = regmap_read(chip->regmap, DA9211_REG_BUCK_ILIM, &data);
155 if (ret < 0)
156 return ret;
157
158 /* select one of 16 values: 0000 (2000mA) to 1111 (5000mA) */
159 data = (data >> id*4) & 0x0F;
160 return da9211_current_limits[data];
161}
162
163static struct regulator_ops da9211_buck_ops = {
164 .get_mode = da9211_buck_get_mode,
165 .set_mode = da9211_buck_set_mode,
166 .enable = regulator_enable_regmap,
167 .disable = regulator_disable_regmap,
168 .is_enabled = regulator_is_enabled_regmap,
169 .set_voltage_sel = regulator_set_voltage_sel_regmap,
170 .get_voltage_sel = regulator_get_voltage_sel_regmap,
171 .list_voltage = regulator_list_voltage_linear,
172 .set_current_limit = da9211_set_current_limit,
173 .get_current_limit = da9211_get_current_limit,
174};
175
176#define DA9211_BUCK(_id) \
177{\
178 .name = #_id,\
179 .ops = &da9211_buck_ops,\
180 .type = REGULATOR_VOLTAGE,\
181 .id = DA9211_ID_##_id,\
182 .n_voltages = (DA9211_MAX_MV - DA9211_MIN_MV) / DA9211_STEP_MV + 1,\
183 .min_uV = (DA9211_MIN_MV * 1000),\
184 .uV_step = (DA9211_STEP_MV * 1000),\
185 .enable_reg = DA9211_REG_BUCKA_CONT + DA9211_ID_##_id,\
186 .enable_mask = DA9211_BUCKA_EN,\
187 .vsel_reg = DA9211_REG_VBUCKA_A + DA9211_ID_##_id * 2,\
188 .vsel_mask = DA9211_VBUCK_MASK,\
189 .owner = THIS_MODULE,\
190}
191
192static struct regulator_desc da9211_regulators[] = {
193 DA9211_BUCK(BUCKA),
194 DA9211_BUCK(BUCKB),
195};
196
197static irqreturn_t da9211_irq_handler(int irq, void *data)
198{
199 struct da9211 *chip = data;
200 int reg_val, err, ret = IRQ_NONE;
201
202 err = regmap_read(chip->regmap, DA9211_REG_EVENT_B, &reg_val);
203 if (err < 0)
204 goto error_i2c;
205
206 if (reg_val & DA9211_E_OV_CURR_A) {
207 regulator_notifier_call_chain(chip->rdev[0],
208 REGULATOR_EVENT_OVER_CURRENT,
209 rdev_get_drvdata(chip->rdev[0]));
210
211 err = regmap_write(chip->regmap, DA9211_REG_EVENT_B,
212 DA9211_E_OV_CURR_A);
213 if (err < 0)
214 goto error_i2c;
215
216 ret = IRQ_HANDLED;
217 }
218
219 if (reg_val & DA9211_E_OV_CURR_B) {
220 regulator_notifier_call_chain(chip->rdev[1],
221 REGULATOR_EVENT_OVER_CURRENT,
222 rdev_get_drvdata(chip->rdev[1]));
223
224 err = regmap_write(chip->regmap, DA9211_REG_EVENT_B,
225 DA9211_E_OV_CURR_B);
226 if (err < 0)
227 goto error_i2c;
228
229 ret = IRQ_HANDLED;
230 }
231
232 return ret;
233
234error_i2c:
235 dev_err(chip->dev, "I2C error : %d\n", err);
236 return IRQ_NONE;
237}
238
239static int da9211_regulator_init(struct da9211 *chip)
240{
241 struct regulator_config config = { };
242 int i, ret;
243 unsigned int data;
244
245 ret = regmap_read(chip->regmap, DA9211_REG_CONFIG_E, &data);
246 if (ret < 0) {
247 dev_err(chip->dev, "Failed to read CONTROL_E reg: %d\n", ret);
Axel Lin516c1512014-07-20 19:19:35 +0800248 return ret;
James Ban1028a372014-07-14 13:48:45 +0900249 }
250
251 data &= DA9211_SLAVE_SEL;
252 /* If configuration for 1/2 bucks is different between platform data
253 * and the register, driver should exit.
254 */
255 if ((chip->pdata->num_buck == 2 && data == 0x40)
256 || (chip->pdata->num_buck == 1 && data == 0x00)) {
257 if (data == 0)
258 chip->num_regulator = 1;
259 else
260 chip->num_regulator = 2;
261 } else {
James Ban1028a372014-07-14 13:48:45 +0900262 dev_err(chip->dev, "Configuration is mismatched\n");
Axel Lin516c1512014-07-20 19:19:35 +0800263 return -EINVAL;
James Ban1028a372014-07-14 13:48:45 +0900264 }
265
266 for (i = 0; i < chip->num_regulator; i++) {
267 if (chip->pdata)
268 config.init_data =
269 &(chip->pdata->init_data[i]);
270
271 config.dev = chip->dev;
272 config.driver_data = chip;
273 config.regmap = chip->regmap;
274
275 chip->rdev[i] = devm_regulator_register(chip->dev,
276 &da9211_regulators[i], &config);
277 if (IS_ERR(chip->rdev[i])) {
278 dev_err(chip->dev,
279 "Failed to register DA9211 regulator\n");
Axel Lin516c1512014-07-20 19:19:35 +0800280 return PTR_ERR(chip->rdev[i]);
James Ban1028a372014-07-14 13:48:45 +0900281 }
282
283 if (chip->chip_irq != 0) {
284 ret = regmap_update_bits(chip->regmap,
285 DA9211_REG_MASK_B, DA9211_M_OV_CURR_A << i, 1);
286 if (ret < 0) {
287 dev_err(chip->dev,
288 "Failed to update mask reg: %d\n", ret);
Axel Lin516c1512014-07-20 19:19:35 +0800289 return ret;
James Ban1028a372014-07-14 13:48:45 +0900290 }
291 }
292 }
293
294 return 0;
James Ban1028a372014-07-14 13:48:45 +0900295}
296/*
297 * I2C driver interface functions
298 */
299static int da9211_i2c_probe(struct i2c_client *i2c,
300 const struct i2c_device_id *id)
301{
302 struct da9211 *chip;
303 int error, ret;
304
305 chip = devm_kzalloc(&i2c->dev, sizeof(struct da9211), GFP_KERNEL);
306
307 chip->dev = &i2c->dev;
308 chip->regmap = devm_regmap_init_i2c(i2c, &da9211_regmap_config);
309 if (IS_ERR(chip->regmap)) {
310 error = PTR_ERR(chip->regmap);
311 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
312 error);
313 return error;
314 }
315
316 i2c_set_clientdata(i2c, chip);
317
318 chip->pdata = i2c->dev.platform_data;
319 if (!chip->pdata) {
320 dev_err(&i2c->dev, "No platform init data supplied\n");
321 return -ENODEV;
322 }
323
324 chip->chip_irq = i2c->irq;
325
326 if (chip->chip_irq != 0) {
327 ret = devm_request_threaded_irq(chip->dev, chip->chip_irq, NULL,
328 da9211_irq_handler,
329 IRQF_TRIGGER_LOW|IRQF_ONESHOT,
330 "da9211", chip);
331 if (ret != 0) {
332 dev_err(chip->dev, "Failed to request IRQ: %d\n",
333 chip->chip_irq);
334 return ret;
335 }
336 } else {
337 dev_warn(chip->dev, "No IRQ configured\n");
338 }
339
340 ret = da9211_regulator_init(chip);
341
342 if (ret < 0)
343 dev_err(&i2c->dev, "Failed to initialize regulator: %d\n", ret);
344
345 return ret;
346}
347
James Ban1028a372014-07-14 13:48:45 +0900348static const struct i2c_device_id da9211_i2c_id[] = {
349 {"da9211", 0},
350 {},
351};
352
353MODULE_DEVICE_TABLE(i2c, da9211_i2c_id);
354
355static struct i2c_driver da9211_regulator_driver = {
356 .driver = {
357 .name = "da9211",
358 .owner = THIS_MODULE,
359 },
360 .probe = da9211_i2c_probe,
James Ban1028a372014-07-14 13:48:45 +0900361 .id_table = da9211_i2c_id,
362};
363
364module_i2c_driver(da9211_regulator_driver);
365
366MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
367MODULE_DESCRIPTION("Regulator device driver for Dialog DA9211");
368MODULE_LICENSE("GPL v2");