2 * da9211-regulator.c - Regulator device driver for DA9211/DA9213/DA9215
3 * Copyright (C) 2015 Dialog Semiconductor Ltd.
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.
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.
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/of_gpio.h>
28 #include <linux/regulator/of_regulator.h>
29 #include <linux/regulator/da9211.h>
30 #include "da9211-regulator.h"
33 #define DA9211_DEVICE_ID 0x22
34 #define DA9213_DEVICE_ID 0x23
35 #define DA9215_DEVICE_ID 0x24
37 #define DA9211_BUCK_MODE_SLEEP 1
38 #define DA9211_BUCK_MODE_SYNC 2
39 #define DA9211_BUCK_MODE_AUTO 3
41 /* DA9211 REGULATOR IDs */
42 #define DA9211_ID_BUCKA 0
43 #define DA9211_ID_BUCKB 1
47 struct regmap
*regmap
;
48 struct da9211_pdata
*pdata
;
49 struct regulator_dev
*rdev
[DA9211_MAX_REGULATORS
];
55 static const struct regmap_range_cfg da9211_regmap_range
[] = {
57 .selector_reg
= DA9211_REG_PAGE_CON
,
58 .selector_mask
= DA9211_REG_PAGE_MASK
,
59 .selector_shift
= DA9211_REG_PAGE_SHIFT
,
67 static const struct regmap_config da9211_regmap_config
= {
70 .max_register
= 5 * 128,
71 .ranges
= da9211_regmap_range
,
72 .num_ranges
= ARRAY_SIZE(da9211_regmap_range
),
75 /* Default limits measured in millivolts and milliamps */
76 #define DA9211_MIN_MV 300
77 #define DA9211_MAX_MV 1570
78 #define DA9211_STEP_MV 10
80 /* Current limits for DA9211 buck (uA) indices
81 * corresponds with register values
83 static const int da9211_current_limits
[] = {
84 2000000, 2200000, 2400000, 2600000, 2800000, 3000000, 3200000, 3400000,
85 3600000, 3800000, 4000000, 4200000, 4400000, 4600000, 4800000, 5000000
87 /* Current limits for DA9213 buck (uA) indices
88 * corresponds with register values
90 static const int da9213_current_limits
[] = {
91 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
92 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
94 /* Current limits for DA9215 buck (uA) indices
95 * corresponds with register values
97 static const int da9215_current_limits
[] = {
98 4000000, 4200000, 4400000, 4600000, 4800000, 5000000, 5200000, 5400000,
99 5600000, 5800000, 6000000, 6200000, 6400000, 6600000, 6800000, 7000000
102 static unsigned int da9211_buck_get_mode(struct regulator_dev
*rdev
)
104 int id
= rdev_get_id(rdev
);
105 struct da9211
*chip
= rdev_get_drvdata(rdev
);
109 ret
= regmap_read(chip
->regmap
, DA9211_REG_BUCKA_CONF
+id
, &data
);
113 switch (data
& 0x03) {
114 case DA9211_BUCK_MODE_SYNC
:
115 mode
= REGULATOR_MODE_FAST
;
117 case DA9211_BUCK_MODE_AUTO
:
118 mode
= REGULATOR_MODE_NORMAL
;
120 case DA9211_BUCK_MODE_SLEEP
:
121 mode
= REGULATOR_MODE_STANDBY
;
128 static int da9211_buck_set_mode(struct regulator_dev
*rdev
,
131 int id
= rdev_get_id(rdev
);
132 struct da9211
*chip
= rdev_get_drvdata(rdev
);
136 case REGULATOR_MODE_FAST
:
137 val
= DA9211_BUCK_MODE_SYNC
;
139 case REGULATOR_MODE_NORMAL
:
140 val
= DA9211_BUCK_MODE_AUTO
;
142 case REGULATOR_MODE_STANDBY
:
143 val
= DA9211_BUCK_MODE_SLEEP
;
147 return regmap_update_bits(chip
->regmap
, DA9211_REG_BUCKA_CONF
+id
,
151 static int da9211_set_current_limit(struct regulator_dev
*rdev
, int min
,
154 int id
= rdev_get_id(rdev
);
155 struct da9211
*chip
= rdev_get_drvdata(rdev
);
157 const int *current_limits
;
159 switch (chip
->chip_id
) {
161 current_limits
= da9211_current_limits
;
162 max_size
= ARRAY_SIZE(da9211_current_limits
)-1;
165 current_limits
= da9213_current_limits
;
166 max_size
= ARRAY_SIZE(da9213_current_limits
)-1;
169 current_limits
= da9215_current_limits
;
170 max_size
= ARRAY_SIZE(da9215_current_limits
)-1;
176 /* search for closest to maximum */
177 for (i
= max_size
; i
>= 0; i
--) {
178 if (min
<= current_limits
[i
] &&
179 max
>= current_limits
[i
]) {
180 return regmap_update_bits(chip
->regmap
,
181 DA9211_REG_BUCK_ILIM
,
182 (0x0F << id
*4), (i
<< id
*4));
189 static int da9211_get_current_limit(struct regulator_dev
*rdev
)
191 int id
= rdev_get_id(rdev
);
192 struct da9211
*chip
= rdev_get_drvdata(rdev
);
195 const int *current_limits
;
197 switch (chip
->chip_id
) {
199 current_limits
= da9211_current_limits
;
202 current_limits
= da9213_current_limits
;
205 current_limits
= da9215_current_limits
;
211 ret
= regmap_read(chip
->regmap
, DA9211_REG_BUCK_ILIM
, &data
);
215 /* select one of 16 values: 0000 (2000mA or 3000mA)
216 * to 1111 (5000mA or 6000mA).
218 data
= (data
>> id
*4) & 0x0F;
219 return current_limits
[data
];
222 static struct regulator_ops da9211_buck_ops
= {
223 .get_mode
= da9211_buck_get_mode
,
224 .set_mode
= da9211_buck_set_mode
,
225 .enable
= regulator_enable_regmap
,
226 .disable
= regulator_disable_regmap
,
227 .is_enabled
= regulator_is_enabled_regmap
,
228 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
229 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
230 .list_voltage
= regulator_list_voltage_linear
,
231 .set_current_limit
= da9211_set_current_limit
,
232 .get_current_limit
= da9211_get_current_limit
,
235 #define DA9211_BUCK(_id) \
238 .ops = &da9211_buck_ops,\
239 .type = REGULATOR_VOLTAGE,\
240 .id = DA9211_ID_##_id,\
241 .n_voltages = (DA9211_MAX_MV - DA9211_MIN_MV) / DA9211_STEP_MV + 1,\
242 .min_uV = (DA9211_MIN_MV * 1000),\
243 .uV_step = (DA9211_STEP_MV * 1000),\
244 .enable_reg = DA9211_REG_BUCKA_CONT + DA9211_ID_##_id,\
245 .enable_mask = DA9211_BUCKA_EN,\
246 .vsel_reg = DA9211_REG_VBUCKA_A + DA9211_ID_##_id * 2,\
247 .vsel_mask = DA9211_VBUCK_MASK,\
248 .owner = THIS_MODULE,\
251 static struct regulator_desc da9211_regulators
[] = {
257 static struct of_regulator_match da9211_matches
[] = {
258 [DA9211_ID_BUCKA
] = { .name
= "BUCKA" },
259 [DA9211_ID_BUCKB
] = { .name
= "BUCKB" },
262 static struct da9211_pdata
*da9211_parse_regulators_dt(
265 struct da9211_pdata
*pdata
;
266 struct device_node
*node
;
269 node
= of_get_child_by_name(dev
->of_node
, "regulators");
271 dev_err(dev
, "regulators node not found\n");
272 return ERR_PTR(-ENODEV
);
275 num
= of_regulator_match(dev
, node
, da9211_matches
,
276 ARRAY_SIZE(da9211_matches
));
279 dev_err(dev
, "Failed to match regulators\n");
280 return ERR_PTR(-EINVAL
);
283 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
285 return ERR_PTR(-ENOMEM
);
287 pdata
->num_buck
= num
;
290 for (i
= 0; i
< ARRAY_SIZE(da9211_matches
); i
++) {
291 if (!da9211_matches
[i
].init_data
)
294 pdata
->init_data
[n
] = da9211_matches
[i
].init_data
;
295 pdata
->reg_node
[n
] = da9211_matches
[i
].of_node
;
297 of_get_named_gpio(da9211_matches
[i
].of_node
,
305 static struct da9211_pdata
*da9211_parse_regulators_dt(
308 return ERR_PTR(-ENODEV
);
312 static irqreturn_t
da9211_irq_handler(int irq
, void *data
)
314 struct da9211
*chip
= data
;
315 int reg_val
, err
, ret
= IRQ_NONE
;
317 err
= regmap_read(chip
->regmap
, DA9211_REG_EVENT_B
, ®_val
);
321 if (reg_val
& DA9211_E_OV_CURR_A
) {
322 regulator_notifier_call_chain(chip
->rdev
[0],
323 REGULATOR_EVENT_OVER_CURRENT
, NULL
);
325 err
= regmap_write(chip
->regmap
, DA9211_REG_EVENT_B
,
333 if (reg_val
& DA9211_E_OV_CURR_B
) {
334 regulator_notifier_call_chain(chip
->rdev
[1],
335 REGULATOR_EVENT_OVER_CURRENT
, NULL
);
337 err
= regmap_write(chip
->regmap
, DA9211_REG_EVENT_B
,
348 dev_err(chip
->dev
, "I2C error : %d\n", err
);
352 static int da9211_regulator_init(struct da9211
*chip
)
354 struct regulator_config config
= { };
358 ret
= regmap_read(chip
->regmap
, DA9211_REG_CONFIG_E
, &data
);
360 dev_err(chip
->dev
, "Failed to read CONFIG_E reg: %d\n", ret
);
364 data
&= DA9211_SLAVE_SEL
;
365 /* If configuration for 1/2 bucks is different between platform data
366 * and the register, driver should exit.
368 if (chip
->pdata
->num_buck
== 1 && data
== 0x00)
369 chip
->num_regulator
= 1;
370 else if (chip
->pdata
->num_buck
== 2 && data
!= 0x00)
371 chip
->num_regulator
= 2;
373 dev_err(chip
->dev
, "Configuration is mismatched\n");
377 for (i
= 0; i
< chip
->num_regulator
; i
++) {
378 config
.init_data
= chip
->pdata
->init_data
[i
];
379 config
.dev
= chip
->dev
;
380 config
.driver_data
= chip
;
381 config
.regmap
= chip
->regmap
;
382 config
.of_node
= chip
->pdata
->reg_node
[i
];
384 if (gpio_is_valid(chip
->pdata
->gpio_ren
[i
])) {
385 config
.ena_gpio
= chip
->pdata
->gpio_ren
[i
];
386 config
.ena_gpio_initialized
= true;
388 config
.ena_gpio
= -EINVAL
;
389 config
.ena_gpio_initialized
= false;
392 chip
->rdev
[i
] = devm_regulator_register(chip
->dev
,
393 &da9211_regulators
[i
], &config
);
394 if (IS_ERR(chip
->rdev
[i
])) {
396 "Failed to register DA9211 regulator\n");
397 return PTR_ERR(chip
->rdev
[i
]);
400 if (chip
->chip_irq
!= 0) {
401 ret
= regmap_update_bits(chip
->regmap
,
402 DA9211_REG_MASK_B
, DA9211_M_OV_CURR_A
<< i
, 0);
405 "Failed to update mask reg: %d\n", ret
);
415 * I2C driver interface functions
417 static int da9211_i2c_probe(struct i2c_client
*i2c
,
418 const struct i2c_device_id
*id
)
424 chip
= devm_kzalloc(&i2c
->dev
, sizeof(struct da9211
), GFP_KERNEL
);
428 chip
->dev
= &i2c
->dev
;
429 chip
->regmap
= devm_regmap_init_i2c(i2c
, &da9211_regmap_config
);
430 if (IS_ERR(chip
->regmap
)) {
431 error
= PTR_ERR(chip
->regmap
);
432 dev_err(chip
->dev
, "Failed to allocate register map: %d\n",
437 i2c_set_clientdata(i2c
, chip
);
439 chip
->pdata
= i2c
->dev
.platform_data
;
441 ret
= regmap_read(chip
->regmap
, DA9211_REG_DEVICE_ID
, &data
);
443 dev_err(chip
->dev
, "Failed to read DEVICE_ID reg: %d\n", ret
);
448 case DA9211_DEVICE_ID
:
449 chip
->chip_id
= DA9211
;
451 case DA9213_DEVICE_ID
:
452 chip
->chip_id
= DA9213
;
454 case DA9215_DEVICE_ID
:
455 chip
->chip_id
= DA9215
;
458 dev_err(chip
->dev
, "Unsupported device id = 0x%x.\n", data
);
463 chip
->pdata
= da9211_parse_regulators_dt(chip
->dev
);
465 if (IS_ERR(chip
->pdata
)) {
466 dev_err(chip
->dev
, "No regulators defined for the platform\n");
467 return PTR_ERR(chip
->pdata
);
470 chip
->chip_irq
= i2c
->irq
;
472 if (chip
->chip_irq
!= 0) {
473 ret
= devm_request_threaded_irq(chip
->dev
, chip
->chip_irq
, NULL
,
475 IRQF_TRIGGER_LOW
|IRQF_ONESHOT
,
478 dev_err(chip
->dev
, "Failed to request IRQ: %d\n",
483 dev_warn(chip
->dev
, "No IRQ configured\n");
486 ret
= da9211_regulator_init(chip
);
489 dev_err(chip
->dev
, "Failed to initialize regulator: %d\n", ret
);
494 static const struct i2c_device_id da9211_i2c_id
[] = {
500 MODULE_DEVICE_TABLE(i2c
, da9211_i2c_id
);
503 static const struct of_device_id da9211_dt_ids
[] = {
504 { .compatible
= "dlg,da9211", .data
= &da9211_i2c_id
[0] },
505 { .compatible
= "dlg,da9213", .data
= &da9211_i2c_id
[1] },
506 { .compatible
= "dlg,da9215", .data
= &da9211_i2c_id
[2] },
509 MODULE_DEVICE_TABLE(of
, da9211_dt_ids
);
512 static struct i2c_driver da9211_regulator_driver
= {
515 .of_match_table
= of_match_ptr(da9211_dt_ids
),
517 .probe
= da9211_i2c_probe
,
518 .id_table
= da9211_i2c_id
,
521 module_i2c_driver(da9211_regulator_driver
);
523 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
524 MODULE_DESCRIPTION("Regulator device driver for Dialog DA9211/DA9213/DA9215");
525 MODULE_LICENSE("GPL");