1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/gpio/consumer.h>
5 #include <linux/kernel.h>
6 #include <linux/module.h>
8 #include <linux/regmap.h>
9 #include <linux/regulator/driver.h>
11 #define RT4801_REG_VOP 0x00
12 #define RT4801_REG_VON 0x01
13 #define RT4801_REG_APPS 0x03
15 #define VOUT_MASK 0x1F
17 #define MIN_UV 4000000
18 #define STEP_UV 100000
19 #define MAX_UV 6000000
20 #define N_VOLTAGES ((MAX_UV - MIN_UV) / STEP_UV + 1)
26 #define DSVP_ENABLE BIT(0)
27 #define DSVN_ENABLE BIT(1)
28 #define DSVALL_ENABLE (DSVP_ENABLE | DSVN_ENABLE)
32 struct gpio_desc *enable_gpios[DSV_OUT_MAX];
33 unsigned int enable_flag;
34 unsigned int volt_sel[DSV_OUT_MAX];
37 static int rt4801_of_parse_cb(struct device_node *np,
38 const struct regulator_desc *desc,
39 struct regulator_config *config)
41 struct rt4801_priv *priv = config->driver_data;
44 if (priv->enable_gpios[id]) {
45 dev_warn(priv->dev, "duplicated enable-gpios property\n");
48 priv->enable_gpios[id] = devm_fwnode_gpiod_get_index(priv->dev,
53 if (IS_ERR(priv->enable_gpios[id]))
54 priv->enable_gpios[id] = NULL;
59 static int rt4801_set_voltage_sel(struct regulator_dev *rdev, unsigned int selector)
61 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
62 int id = rdev_get_id(rdev), ret;
64 if (priv->enable_flag & BIT(id)) {
65 ret = regulator_set_voltage_sel_regmap(rdev, selector);
70 priv->volt_sel[id] = selector;
74 static int rt4801_get_voltage_sel(struct regulator_dev *rdev)
76 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
77 int id = rdev_get_id(rdev);
79 if (priv->enable_flag & BIT(id))
80 return regulator_get_voltage_sel_regmap(rdev);
82 return priv->volt_sel[id];
85 static int rt4801_enable(struct regulator_dev *rdev)
87 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
88 int id = rdev_get_id(rdev), ret;
90 if (!priv->enable_gpios[id]) {
91 dev_warn(&rdev->dev, "no dedicated gpio can control\n");
95 gpiod_set_value(priv->enable_gpios[id], 1);
98 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, priv->volt_sel[id]);
102 priv->enable_flag |= BIT(id);
106 static int rt4801_disable(struct regulator_dev *rdev)
108 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
109 int id = rdev_get_id(rdev);
111 if (!priv->enable_gpios[id]) {
112 dev_warn(&rdev->dev, "no dedicated gpio can control\n");
116 gpiod_set_value(priv->enable_gpios[id], 0);
119 priv->enable_flag &= ~BIT(id);
123 static int rt4801_is_enabled(struct regulator_dev *rdev)
125 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
126 int id = rdev_get_id(rdev);
128 return !!(priv->enable_flag & BIT(id));
131 static const struct regulator_ops rt4801_regulator_ops = {
132 .list_voltage = regulator_list_voltage_linear,
133 .set_voltage_sel = rt4801_set_voltage_sel,
134 .get_voltage_sel = rt4801_get_voltage_sel,
135 .enable = rt4801_enable,
136 .disable = rt4801_disable,
137 .is_enabled = rt4801_is_enabled,
140 static const struct regulator_desc rt4801_regulator_descs[] = {
143 .ops = &rt4801_regulator_ops,
144 .of_match = of_match_ptr("DSVP"),
145 .of_parse_cb = rt4801_of_parse_cb,
146 .type = REGULATOR_VOLTAGE,
150 .n_voltages = N_VOLTAGES,
151 .owner = THIS_MODULE,
152 .vsel_reg = RT4801_REG_VOP,
153 .vsel_mask = VOUT_MASK,
157 .ops = &rt4801_regulator_ops,
158 .of_match = of_match_ptr("DSVN"),
159 .of_parse_cb = rt4801_of_parse_cb,
160 .type = REGULATOR_VOLTAGE,
164 .n_voltages = N_VOLTAGES,
165 .owner = THIS_MODULE,
166 .vsel_reg = RT4801_REG_VON,
167 .vsel_mask = VOUT_MASK,
171 static const struct regmap_config rt4801_regmap_config = {
174 .max_register = RT4801_REG_APPS,
177 static int rt4801_probe(struct i2c_client *i2c)
179 struct rt4801_priv *priv;
180 struct regmap *regmap;
183 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
187 priv->dev = &i2c->dev;
188 /* bootloader will on, driver only reconfigure enable to all output high */
189 priv->enable_flag = DSVALL_ENABLE;
191 regmap = devm_regmap_init_i2c(i2c, &rt4801_regmap_config);
192 if (IS_ERR(regmap)) {
193 dev_err(&i2c->dev, "Failed to init regmap\n");
194 return PTR_ERR(regmap);
197 for (i = 0; i < DSV_OUT_MAX; i++) {
198 priv->enable_gpios[i] = devm_gpiod_get_index_optional(&i2c->dev,
202 if (IS_ERR(priv->enable_gpios[i])) {
203 dev_err(&i2c->dev, "Failed to get gpios\n");
204 return PTR_ERR(priv->enable_gpios[i]);
208 for (i = 0; i < DSV_OUT_MAX; i++) {
209 const struct regulator_desc *desc = rt4801_regulator_descs + i;
210 struct regulator_config config = { .dev = &i2c->dev, .driver_data = priv,
212 struct regulator_dev *rdev;
216 /* initialize volt_sel variable */
217 ret = regmap_read(regmap, desc->vsel_reg, &val);
221 priv->volt_sel[i] = val & desc->vsel_mask;
223 rdev = devm_regulator_register(&i2c->dev, desc, &config);
225 dev_err(&i2c->dev, "Failed to register [%d] regulator\n", i);
226 return PTR_ERR(rdev);
233 static const struct of_device_id __maybe_unused rt4801_of_id[] = {
234 { .compatible = "richtek,rt4801", },
237 MODULE_DEVICE_TABLE(of, rt4801_of_id);
239 static struct i2c_driver rt4801_driver = {
242 .of_match_table = of_match_ptr(rt4801_of_id),
244 .probe_new = rt4801_probe,
246 module_i2c_driver(rt4801_driver);
248 MODULE_AUTHOR("ChiYuan Hwang <cy_huang@richtek.com>");
249 MODULE_DESCRIPTION("Richtek RT4801 Display Bias Driver");
250 MODULE_LICENSE("GPL v2");