1 // SPDX-License-Identifier: GPL-2.0+
3 // da9211-regulator.c - Regulator device driver for DA9211/DA9212
4 // /DA9213/DA9223/DA9214/DA9224/DA9215/DA9225
5 // Copyright (C) 2015 Dialog Semiconductor Ltd.
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/regulator/machine.h>
14 #include <linux/regmap.h>
15 #include <linux/irq.h>
16 #include <linux/interrupt.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/regulator/of_regulator.h>
19 #include <linux/regulator/da9211.h>
20 #include <dt-bindings/regulator/dlg,da9211-regulator.h>
21 #include "da9211-regulator.h"
24 #define DA9211_DEVICE_ID 0x22
25 #define DA9213_DEVICE_ID 0x23
26 #define DA9215_DEVICE_ID 0x24
28 /* DA9211 REGULATOR IDs */
29 #define DA9211_ID_BUCKA 0
30 #define DA9211_ID_BUCKB 1
34 struct regmap
*regmap
;
35 struct da9211_pdata
*pdata
;
36 struct regulator_dev
*rdev
[DA9211_MAX_REGULATORS
];
42 static const struct regmap_range_cfg da9211_regmap_range
[] = {
44 .selector_reg
= DA9211_REG_PAGE_CON
,
45 .selector_mask
= DA9211_REG_PAGE_MASK
,
46 .selector_shift
= DA9211_REG_PAGE_SHIFT
,
54 static bool da9211_volatile_reg(struct device
*dev
, unsigned int reg
)
57 case DA9211_REG_STATUS_A
:
58 case DA9211_REG_STATUS_B
:
59 case DA9211_REG_EVENT_A
:
60 case DA9211_REG_EVENT_B
:
66 static const struct regmap_config da9211_regmap_config
= {
69 .max_register
= 5 * 128,
70 .volatile_reg
= da9211_volatile_reg
,
71 .cache_type
= REGCACHE_RBTREE
,
72 .ranges
= da9211_regmap_range
,
73 .num_ranges
= ARRAY_SIZE(da9211_regmap_range
),
76 /* Default limits measured in millivolts and milliamps */
77 #define DA9211_MIN_MV 300
78 #define DA9211_MAX_MV 1570
79 #define DA9211_STEP_MV 10
81 /* Current limits for DA9211 buck (uA) indices
82 * corresponds with register values
84 static const int da9211_current_limits
[] = {
85 2000000, 2200000, 2400000, 2600000, 2800000, 3000000, 3200000, 3400000,
86 3600000, 3800000, 4000000, 4200000, 4400000, 4600000, 4800000, 5000000
88 /* Current limits for DA9213 buck (uA) indices
89 * corresponds with register values
91 static const int da9213_current_limits
[] = {
92 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
93 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
95 /* Current limits for DA9215 buck (uA) indices
96 * corresponds with register values
98 static const int da9215_current_limits
[] = {
99 4000000, 4200000, 4400000, 4600000, 4800000, 5000000, 5200000, 5400000,
100 5600000, 5800000, 6000000, 6200000, 6400000, 6600000, 6800000, 7000000
103 static unsigned int da9211_map_buck_mode(unsigned int mode
)
106 case DA9211_BUCK_MODE_SLEEP
:
107 return REGULATOR_MODE_STANDBY
;
108 case DA9211_BUCK_MODE_SYNC
:
109 return REGULATOR_MODE_FAST
;
110 case DA9211_BUCK_MODE_AUTO
:
111 return REGULATOR_MODE_NORMAL
;
113 return REGULATOR_MODE_INVALID
;
117 static unsigned int da9211_buck_get_mode(struct regulator_dev
*rdev
)
119 int id
= rdev_get_id(rdev
);
120 struct da9211
*chip
= rdev_get_drvdata(rdev
);
124 ret
= regmap_read(chip
->regmap
, DA9211_REG_BUCKA_CONF
+id
, &data
);
128 switch (data
& 0x03) {
129 case DA9211_BUCK_MODE_SYNC
:
130 mode
= REGULATOR_MODE_FAST
;
132 case DA9211_BUCK_MODE_AUTO
:
133 mode
= REGULATOR_MODE_NORMAL
;
135 case DA9211_BUCK_MODE_SLEEP
:
136 mode
= REGULATOR_MODE_STANDBY
;
143 static int da9211_buck_set_mode(struct regulator_dev
*rdev
,
146 int id
= rdev_get_id(rdev
);
147 struct da9211
*chip
= rdev_get_drvdata(rdev
);
151 case REGULATOR_MODE_FAST
:
152 val
= DA9211_BUCK_MODE_SYNC
;
154 case REGULATOR_MODE_NORMAL
:
155 val
= DA9211_BUCK_MODE_AUTO
;
157 case REGULATOR_MODE_STANDBY
:
158 val
= DA9211_BUCK_MODE_SLEEP
;
162 return regmap_update_bits(chip
->regmap
, DA9211_REG_BUCKA_CONF
+id
,
166 static int da9211_set_current_limit(struct regulator_dev
*rdev
, int min
,
169 int id
= rdev_get_id(rdev
);
170 struct da9211
*chip
= rdev_get_drvdata(rdev
);
172 const int *current_limits
;
174 switch (chip
->chip_id
) {
176 current_limits
= da9211_current_limits
;
177 max_size
= ARRAY_SIZE(da9211_current_limits
)-1;
180 current_limits
= da9213_current_limits
;
181 max_size
= ARRAY_SIZE(da9213_current_limits
)-1;
184 current_limits
= da9215_current_limits
;
185 max_size
= ARRAY_SIZE(da9215_current_limits
)-1;
191 /* search for closest to maximum */
192 for (i
= max_size
; i
>= 0; i
--) {
193 if (min
<= current_limits
[i
] &&
194 max
>= current_limits
[i
]) {
195 return regmap_update_bits(chip
->regmap
,
196 DA9211_REG_BUCK_ILIM
,
197 (0x0F << id
*4), (i
<< id
*4));
204 static int da9211_get_current_limit(struct regulator_dev
*rdev
)
206 int id
= rdev_get_id(rdev
);
207 struct da9211
*chip
= rdev_get_drvdata(rdev
);
210 const int *current_limits
;
212 switch (chip
->chip_id
) {
214 current_limits
= da9211_current_limits
;
217 current_limits
= da9213_current_limits
;
220 current_limits
= da9215_current_limits
;
226 ret
= regmap_read(chip
->regmap
, DA9211_REG_BUCK_ILIM
, &data
);
230 /* select one of 16 values: 0000 (2000mA or 3000mA)
231 * to 1111 (5000mA or 6000mA).
233 data
= (data
>> id
*4) & 0x0F;
234 return current_limits
[data
];
237 static const struct regulator_ops da9211_buck_ops
= {
238 .get_mode
= da9211_buck_get_mode
,
239 .set_mode
= da9211_buck_set_mode
,
240 .enable
= regulator_enable_regmap
,
241 .disable
= regulator_disable_regmap
,
242 .is_enabled
= regulator_is_enabled_regmap
,
243 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
244 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
245 .list_voltage
= regulator_list_voltage_linear
,
246 .set_current_limit
= da9211_set_current_limit
,
247 .get_current_limit
= da9211_get_current_limit
,
250 #define DA9211_BUCK(_id) \
253 .ops = &da9211_buck_ops,\
254 .type = REGULATOR_VOLTAGE,\
255 .id = DA9211_ID_##_id,\
256 .n_voltages = (DA9211_MAX_MV - DA9211_MIN_MV) / DA9211_STEP_MV + 1,\
257 .min_uV = (DA9211_MIN_MV * 1000),\
258 .uV_step = (DA9211_STEP_MV * 1000),\
259 .enable_reg = DA9211_REG_BUCKA_CONT + DA9211_ID_##_id,\
260 .enable_mask = DA9211_BUCKA_EN,\
261 .vsel_reg = DA9211_REG_VBUCKA_A + DA9211_ID_##_id * 2,\
262 .vsel_mask = DA9211_VBUCK_MASK,\
263 .owner = THIS_MODULE,\
264 .of_map_mode = da9211_map_buck_mode,\
267 static struct regulator_desc da9211_regulators
[] = {
273 static struct of_regulator_match da9211_matches
[] = {
274 [DA9211_ID_BUCKA
] = {
276 .desc
= &da9211_regulators
[DA9211_ID_BUCKA
],
278 [DA9211_ID_BUCKB
] = {
280 .desc
= &da9211_regulators
[DA9211_ID_BUCKB
],
284 static struct da9211_pdata
*da9211_parse_regulators_dt(
287 struct da9211_pdata
*pdata
;
288 struct device_node
*node
;
291 node
= of_get_child_by_name(dev
->of_node
, "regulators");
293 dev_err(dev
, "regulators node not found\n");
294 return ERR_PTR(-ENODEV
);
297 num
= of_regulator_match(dev
, node
, da9211_matches
,
298 ARRAY_SIZE(da9211_matches
));
301 dev_err(dev
, "Failed to match regulators\n");
302 return ERR_PTR(-EINVAL
);
305 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
307 return ERR_PTR(-ENOMEM
);
309 pdata
->num_buck
= num
;
312 for (i
= 0; i
< ARRAY_SIZE(da9211_matches
); i
++) {
313 if (!da9211_matches
[i
].init_data
)
316 pdata
->init_data
[n
] = da9211_matches
[i
].init_data
;
317 pdata
->reg_node
[n
] = da9211_matches
[i
].of_node
;
318 pdata
->gpiod_ren
[n
] = devm_fwnode_gpiod_get(dev
,
319 of_fwnode_handle(pdata
->reg_node
[n
]),
322 GPIOD_FLAGS_BIT_NONEXCLUSIVE
,
324 if (IS_ERR(pdata
->gpiod_ren
[n
]))
325 pdata
->gpiod_ren
[n
] = NULL
;
332 static struct da9211_pdata
*da9211_parse_regulators_dt(
335 return ERR_PTR(-ENODEV
);
339 static irqreturn_t
da9211_irq_handler(int irq
, void *data
)
341 struct da9211
*chip
= data
;
342 int reg_val
, err
, ret
= IRQ_NONE
;
344 err
= regmap_read(chip
->regmap
, DA9211_REG_EVENT_B
, ®_val
);
348 if (reg_val
& DA9211_E_OV_CURR_A
) {
349 regulator_notifier_call_chain(chip
->rdev
[0],
350 REGULATOR_EVENT_OVER_CURRENT
, NULL
);
352 err
= regmap_write(chip
->regmap
, DA9211_REG_EVENT_B
,
360 if (reg_val
& DA9211_E_OV_CURR_B
) {
361 regulator_notifier_call_chain(chip
->rdev
[1],
362 REGULATOR_EVENT_OVER_CURRENT
, NULL
);
364 err
= regmap_write(chip
->regmap
, DA9211_REG_EVENT_B
,
375 dev_err(chip
->dev
, "I2C error : %d\n", err
);
379 static int da9211_regulator_init(struct da9211
*chip
)
381 struct regulator_config config
= { };
385 ret
= regmap_read(chip
->regmap
, DA9211_REG_CONFIG_E
, &data
);
387 dev_err(chip
->dev
, "Failed to read CONFIG_E reg: %d\n", ret
);
391 data
&= DA9211_SLAVE_SEL
;
392 /* If configuration for 1/2 bucks is different between platform data
393 * and the register, driver should exit.
395 if (chip
->pdata
->num_buck
== 1 && data
== 0x00)
396 chip
->num_regulator
= 1;
397 else if (chip
->pdata
->num_buck
== 2 && data
!= 0x00)
398 chip
->num_regulator
= 2;
400 dev_err(chip
->dev
, "Configuration is mismatched\n");
404 for (i
= 0; i
< chip
->num_regulator
; i
++) {
405 config
.init_data
= chip
->pdata
->init_data
[i
];
406 config
.dev
= chip
->dev
;
407 config
.driver_data
= chip
;
408 config
.regmap
= chip
->regmap
;
409 config
.of_node
= chip
->pdata
->reg_node
[i
];
411 if (chip
->pdata
->gpiod_ren
[i
])
412 config
.ena_gpiod
= chip
->pdata
->gpiod_ren
[i
];
414 config
.ena_gpiod
= NULL
;
417 * Hand the GPIO descriptor management over to the regulator
418 * core, remove it from GPIO devres management.
420 if (config
.ena_gpiod
)
421 devm_gpiod_unhinge(chip
->dev
, config
.ena_gpiod
);
422 chip
->rdev
[i
] = devm_regulator_register(chip
->dev
,
423 &da9211_regulators
[i
], &config
);
424 if (IS_ERR(chip
->rdev
[i
])) {
426 "Failed to register DA9211 regulator\n");
427 return PTR_ERR(chip
->rdev
[i
]);
430 if (chip
->chip_irq
!= 0) {
431 ret
= regmap_update_bits(chip
->regmap
,
432 DA9211_REG_MASK_B
, DA9211_M_OV_CURR_A
<< i
, 0);
435 "Failed to update mask reg: %d\n", ret
);
445 * I2C driver interface functions
447 static int da9211_i2c_probe(struct i2c_client
*i2c
)
453 chip
= devm_kzalloc(&i2c
->dev
, sizeof(struct da9211
), GFP_KERNEL
);
457 chip
->dev
= &i2c
->dev
;
458 chip
->regmap
= devm_regmap_init_i2c(i2c
, &da9211_regmap_config
);
459 if (IS_ERR(chip
->regmap
)) {
460 error
= PTR_ERR(chip
->regmap
);
461 dev_err(chip
->dev
, "Failed to allocate register map: %d\n",
466 i2c_set_clientdata(i2c
, chip
);
468 chip
->pdata
= i2c
->dev
.platform_data
;
470 ret
= regmap_read(chip
->regmap
, DA9211_REG_DEVICE_ID
, &data
);
472 dev_err(chip
->dev
, "Failed to read DEVICE_ID reg: %d\n", ret
);
477 case DA9211_DEVICE_ID
:
478 chip
->chip_id
= DA9211
;
480 case DA9213_DEVICE_ID
:
481 chip
->chip_id
= DA9213
;
483 case DA9215_DEVICE_ID
:
484 chip
->chip_id
= DA9215
;
487 dev_err(chip
->dev
, "Unsupported device id = 0x%x.\n", data
);
492 chip
->pdata
= da9211_parse_regulators_dt(chip
->dev
);
494 if (IS_ERR(chip
->pdata
)) {
495 dev_err(chip
->dev
, "No regulators defined for the platform\n");
496 return PTR_ERR(chip
->pdata
);
499 chip
->chip_irq
= i2c
->irq
;
501 if (chip
->chip_irq
!= 0) {
502 ret
= devm_request_threaded_irq(chip
->dev
, chip
->chip_irq
, NULL
,
504 IRQF_TRIGGER_LOW
|IRQF_ONESHOT
,
507 dev_err(chip
->dev
, "Failed to request IRQ: %d\n",
512 dev_warn(chip
->dev
, "No IRQ configured\n");
515 ret
= da9211_regulator_init(chip
);
518 dev_err(chip
->dev
, "Failed to initialize regulator: %d\n", ret
);
523 static const struct i2c_device_id da9211_i2c_id
[] = {
534 MODULE_DEVICE_TABLE(i2c
, da9211_i2c_id
);
537 static const struct of_device_id da9211_dt_ids
[] = {
538 { .compatible
= "dlg,da9211", .data
= &da9211_i2c_id
[0] },
539 { .compatible
= "dlg,da9212", .data
= &da9211_i2c_id
[1] },
540 { .compatible
= "dlg,da9213", .data
= &da9211_i2c_id
[2] },
541 { .compatible
= "dlg,da9223", .data
= &da9211_i2c_id
[3] },
542 { .compatible
= "dlg,da9214", .data
= &da9211_i2c_id
[4] },
543 { .compatible
= "dlg,da9224", .data
= &da9211_i2c_id
[5] },
544 { .compatible
= "dlg,da9215", .data
= &da9211_i2c_id
[6] },
545 { .compatible
= "dlg,da9225", .data
= &da9211_i2c_id
[7] },
548 MODULE_DEVICE_TABLE(of
, da9211_dt_ids
);
551 static struct i2c_driver da9211_regulator_driver
= {
554 .of_match_table
= of_match_ptr(da9211_dt_ids
),
556 .probe_new
= da9211_i2c_probe
,
557 .id_table
= da9211_i2c_id
,
560 module_i2c_driver(da9211_regulator_driver
);
562 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
563 MODULE_DESCRIPTION("DA9211/DA9212/DA9213/DA9223/DA9214/DA9224/DA9215/DA9225 regulator driver");
564 MODULE_LICENSE("GPL");