1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2019 five technologies GmbH
4 // Author: Markus Reichl <m.reichl@fivetechno.de>
6 #include <linux/module.h>
9 #include <linux/regulator/driver.h>
10 #include <linux/regmap.h>
13 #define VOL_MIN_IDX 0x00
14 #define VOL_MAX_IDX 0x7ff
16 /* Register definitions */
17 #define MP8859_VOUT_L_REG 0 //3 lo Bits
18 #define MP8859_VOUT_H_REG 1 //8 hi Bits
19 #define MP8859_VOUT_GO_REG 2
20 #define MP8859_IOUT_LIM_REG 3
21 #define MP8859_CTL1_REG 4
22 #define MP8859_CTL2_REG 5
23 #define MP8859_RESERVED1_REG 6
24 #define MP8859_RESERVED2_REG 7
25 #define MP8859_RESERVED3_REG 8
26 #define MP8859_STATUS_REG 9
27 #define MP8859_INTERRUPT_REG 0x0A
28 #define MP8859_MASK_REG 0x0B
29 #define MP8859_ID1_REG 0x0C
30 #define MP8859_MFR_ID_REG 0x27
31 #define MP8859_DEV_ID_REG 0x28
32 #define MP8859_IC_REV_REG 0x29
34 #define MP8859_MAX_REG 0x29
36 #define MP8859_GO_BIT 0x01
39 static int mp8859_set_voltage_sel(struct regulator_dev *rdev, unsigned int sel)
43 ret = regmap_write(rdev->regmap, MP8859_VOUT_L_REG, sel & 0x7);
47 ret = regmap_write(rdev->regmap, MP8859_VOUT_H_REG, sel >> 3);
51 ret = regmap_update_bits(rdev->regmap, MP8859_VOUT_GO_REG,
56 static int mp8859_get_voltage_sel(struct regulator_dev *rdev)
62 ret = regmap_read(rdev->regmap, MP8859_VOUT_H_REG, &val_tmp);
68 ret = regmap_read(rdev->regmap, MP8859_VOUT_L_REG, &val_tmp);
72 val |= val_tmp & 0x07;
76 static const struct linear_range mp8859_dcdc_ranges[] = {
77 REGULATOR_LINEAR_RANGE(0, VOL_MIN_IDX, VOL_MAX_IDX, 10000),
80 static const struct regmap_config mp8859_regmap = {
83 .max_register = MP8859_MAX_REG,
84 .cache_type = REGCACHE_RBTREE,
87 static const struct regulator_ops mp8859_ops = {
88 .set_voltage_sel = mp8859_set_voltage_sel,
89 .get_voltage_sel = mp8859_get_voltage_sel,
90 .list_voltage = regulator_list_voltage_linear_range,
93 static const struct regulator_desc mp8859_regulators[] = {
96 .type = REGULATOR_VOLTAGE,
97 .name = "mp8859_dcdc",
99 .of_match = of_match_ptr("mp8859_dcdc"),
100 .n_voltages = VOL_MAX_IDX + 1,
101 .linear_ranges = mp8859_dcdc_ranges,
102 .n_linear_ranges = 1,
104 .owner = THIS_MODULE,
108 static int mp8859_i2c_probe(struct i2c_client *i2c)
111 struct regulator_config config = {.dev = &i2c->dev};
112 struct regmap *regmap = devm_regmap_init_i2c(i2c, &mp8859_regmap);
113 struct regulator_dev *rdev;
115 if (IS_ERR(regmap)) {
116 ret = PTR_ERR(regmap);
117 dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
120 rdev = devm_regulator_register(&i2c->dev, &mp8859_regulators[0],
125 dev_err(&i2c->dev, "failed to register %s: %d\n",
126 mp8859_regulators[0].name, ret);
132 static const struct of_device_id mp8859_dt_id[] __maybe_unused = {
133 {.compatible = "mps,mp8859"},
136 MODULE_DEVICE_TABLE(of, mp8859_dt_id);
138 static const struct i2c_device_id mp8859_i2c_id[] = {
142 MODULE_DEVICE_TABLE(i2c, mp8859_i2c_id);
144 static struct i2c_driver mp8859_regulator_driver = {
147 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
148 .of_match_table = of_match_ptr(mp8859_dt_id),
150 .probe = mp8859_i2c_probe,
151 .id_table = mp8859_i2c_id,
154 module_i2c_driver(mp8859_regulator_driver);
156 MODULE_DESCRIPTION("Monolithic Power Systems MP8859 voltage regulator driver");
157 MODULE_AUTHOR("Markus Reichl <m.reichl@fivetechno.de>");
158 MODULE_LICENSE("GPL v2");