2 * Regulator driver for mp8865
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/of_regulator.h>
21 #define MP8865_MAX_VSEL 0x7f
22 #define VOL_MIN_IDX 0x00
23 #define VOL_MAX_IDX 0x7f
25 /* Register definitions */
26 #define MP8865_VOUT_REG 0
27 #define MP8865_SYSCNTL_REG 1
28 #define MP8865_ID_REG 2
29 #define MP8865_STATUS_REG 3
31 #define MP8865_VOUT_MASK 0x7f
32 #define MP8865_VBOOT_MASK 0x80
34 #define MP8865_ENABLE 0x80
35 #define MP8865_GO_BIT 0x40
36 #define MP8865_SLEW_RATE_MASK 0x1c
37 #define MP8865_SWITCH_FRE_MASK 0x06
38 #define MP8865_PFM_MODE 0X01
40 /* mp8865 chip information */
43 struct regulator_desc desc;
44 struct regulator_dev *rdev;
45 struct regmap *regmap;
48 struct mp8865_platform_data {
49 struct regulator_init_data *mp8865_init_data;
50 struct device_node *of_node;
53 static const struct regulator_linear_range mp8865_voltage_ranges[] = {
54 REGULATOR_LINEAR_RANGE(600000, VOL_MIN_IDX, VOL_MAX_IDX, 10000),
57 static int mp8865_set_voltage(struct regulator_dev *rdev, unsigned sel)
61 ret = regmap_update_bits(rdev->regmap, MP8865_SYSCNTL_REG,
62 MP8865_GO_BIT, MP8865_GO_BIT);
66 sel <<= ffs(rdev->desc->vsel_mask) - 1;
67 return regmap_write(rdev->regmap, MP8865_VOUT_REG, sel);
70 static const struct regmap_config mp8865_regmap_config = {
75 static struct regulator_ops mp8865_dcdc_ops = {
76 .list_voltage = regulator_list_voltage_linear_range,
77 .map_voltage = regulator_map_voltage_linear_range,
78 .get_voltage_sel = regulator_get_voltage_sel_regmap,
79 .set_voltage_sel = mp8865_set_voltage,
80 .enable = regulator_enable_regmap,
81 .disable = regulator_disable_regmap,
82 .is_enabled = regulator_is_enabled_regmap,
85 static const struct of_device_id mp8865_of_match[] = {
86 {.compatible = "mps,mp8865", .data = (void *)0},
89 MODULE_DEVICE_TABLE(of, mp8865_of_match);
91 static struct of_regulator_match mp8865_matches = {
92 .name = "mp8865_dcdc1",
93 .driver_data = (void *)0,
96 static struct mp8865_platform_data *
97 mp8865_parse_dt(struct i2c_client *client,
98 struct of_regulator_match **mp8865_reg_matches)
100 struct mp8865_platform_data *pdata;
101 struct device_node *np, *regulators;
104 pdata = devm_kzalloc(&client->dev, sizeof(struct mp8865_platform_data),
109 np = of_node_get(client->dev.of_node);
110 regulators = of_find_node_by_name(np, "regulators");
112 dev_err(&client->dev, "regulator node not found\n");
116 ret = of_regulator_match(&client->dev, regulators, &mp8865_matches, 1);
118 dev_err(&client->dev, "Error parsing regulators init data\n");
121 *mp8865_reg_matches = &mp8865_matches;
122 of_node_put(regulators);
124 pdata->mp8865_init_data = mp8865_matches.init_data;
125 pdata->of_node = mp8865_matches.of_node;
130 static int mp8865_probe(struct i2c_client *client,
131 const struct i2c_device_id *id)
133 struct mp8865_chip *mp8865;
134 struct mp8865_platform_data *pdata;
135 struct of_regulator_match *mp8865_reg_matches = NULL;
136 struct regulator_dev *sy_rdev;
137 struct regulator_config config;
138 struct regulator_desc *rdesc;
140 mp8865 = devm_kzalloc(&client->dev, sizeof(struct mp8865_chip),
144 pdata = dev_get_platdata(&client->dev);
146 pdata = mp8865_parse_dt(client, &mp8865_reg_matches);
148 if (!pdata || !pdata->mp8865_init_data) {
149 dev_err(&client->dev, "Platform data not found!\n");
153 mp8865->dev = &client->dev;
154 mp8865->regmap = devm_regmap_init_i2c(client, &mp8865_regmap_config);
155 if (IS_ERR(mp8865->regmap)) {
156 dev_err(&client->dev, "Failed to allocate regmap!\n");
157 return PTR_ERR(mp8865->regmap);
160 i2c_set_clientdata(client, mp8865);
162 config.dev = mp8865->dev;
163 config.driver_data = mp8865;
164 config.init_data = pdata->mp8865_init_data;
165 config.of_node = pdata->of_node;
166 config.regmap = mp8865->regmap;
168 rdesc = &mp8865->desc;
169 rdesc->name = "mp8865_dcdc1",
171 rdesc->ops = &mp8865_dcdc_ops,
172 rdesc->n_voltages = MP8865_MAX_VSEL + 1,
173 rdesc->linear_ranges = mp8865_voltage_ranges,
174 rdesc->n_linear_ranges = ARRAY_SIZE(mp8865_voltage_ranges),
175 rdesc->vsel_reg = MP8865_VOUT_REG;
176 rdesc->vsel_mask = MP8865_VOUT_MASK;
177 rdesc->enable_reg = MP8865_SYSCNTL_REG;
178 rdesc->enable_mask = MP8865_ENABLE;
179 rdesc->type = REGULATOR_VOLTAGE,
180 rdesc->owner = THIS_MODULE;
182 sy_rdev = devm_regulator_register(mp8865->dev, &mp8865->desc,
184 if (IS_ERR(sy_rdev)) {
185 dev_err(mp8865->dev, "failed to register regulator\n");
186 return PTR_ERR(sy_rdev);
189 dev_info(mp8865->dev, "mp8865 register successful\n");
194 static const struct i2c_device_id mp8865_id[] = {
195 { .name = "mps,mp8865", },
198 MODULE_DEVICE_TABLE(i2c, mp8865_id);
200 static struct i2c_driver mp8865_driver = {
203 .of_match_table = of_match_ptr(mp8865_of_match),
204 .owner = THIS_MODULE,
206 .probe = mp8865_probe,
207 .id_table = mp8865_id,
209 module_i2c_driver(mp8865_driver);
211 MODULE_AUTHOR("Zain Wang <zain.wang@rock-chips.com>");
212 MODULE_DESCRIPTION("mp8865 voltage regulator driver");
213 MODULE_LICENSE("GPL v2");