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