WIP FPC-III support
[linux/fpc-iii.git] / drivers / regulator / rt4801-regulator.c
blob2055a9cb13ba549ddcd940035e41fb8437b2468f
1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/gpio/consumer.h>
4 #include <linux/i2c.h>
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/of.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)
22 #define DSV_OUT_POS 0
23 #define DSV_OUT_NEG 1
24 #define DSV_OUT_MAX 2
26 #define DSVP_ENABLE BIT(0)
27 #define DSVN_ENABLE BIT(1)
28 #define DSVALL_ENABLE (DSVP_ENABLE | DSVN_ENABLE)
30 struct rt4801_priv {
31 struct device *dev;
32 struct gpio_descs *enable_gpios;
33 unsigned int enable_flag;
34 unsigned int volt_sel[DSV_OUT_MAX];
37 static int rt4801_set_voltage_sel(struct regulator_dev *rdev, unsigned int selector)
39 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
40 int id = rdev_get_id(rdev), ret;
42 if (priv->enable_flag & BIT(id)) {
43 ret = regulator_set_voltage_sel_regmap(rdev, selector);
44 if (ret)
45 return ret;
48 priv->volt_sel[id] = selector;
49 return 0;
52 static int rt4801_get_voltage_sel(struct regulator_dev *rdev)
54 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
55 int id = rdev_get_id(rdev);
57 if (priv->enable_flag & BIT(id))
58 return regulator_get_voltage_sel_regmap(rdev);
60 return priv->volt_sel[id];
63 static int rt4801_enable(struct regulator_dev *rdev)
65 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
66 struct gpio_descs *gpios = priv->enable_gpios;
67 int id = rdev_get_id(rdev), ret;
69 if (gpios->ndescs <= id) {
70 dev_warn(&rdev->dev, "no dedicated gpio can control\n");
71 goto bypass_gpio;
74 gpiod_set_value(gpios->desc[id], 1);
76 bypass_gpio:
77 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, priv->volt_sel[id]);
78 if (ret)
79 return ret;
81 priv->enable_flag |= BIT(id);
82 return 0;
85 static int rt4801_disable(struct regulator_dev *rdev)
87 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
88 struct gpio_descs *gpios = priv->enable_gpios;
89 int id = rdev_get_id(rdev);
91 if (gpios->ndescs <= id) {
92 dev_warn(&rdev->dev, "no dedicated gpio can control\n");
93 goto bypass_gpio;
96 gpiod_set_value(gpios->desc[id], 0);
98 bypass_gpio:
99 priv->enable_flag &= ~BIT(id);
100 return 0;
103 static int rt4801_is_enabled(struct regulator_dev *rdev)
105 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
106 int id = rdev_get_id(rdev);
108 return !!(priv->enable_flag & BIT(id));
111 static const struct regulator_ops rt4801_regulator_ops = {
112 .list_voltage = regulator_list_voltage_linear,
113 .set_voltage_sel = rt4801_set_voltage_sel,
114 .get_voltage_sel = rt4801_get_voltage_sel,
115 .enable = rt4801_enable,
116 .disable = rt4801_disable,
117 .is_enabled = rt4801_is_enabled,
120 static const struct regulator_desc rt4801_regulator_descs[] = {
122 .name = "DSVP",
123 .ops = &rt4801_regulator_ops,
124 .of_match = of_match_ptr("DSVP"),
125 .type = REGULATOR_VOLTAGE,
126 .id = DSV_OUT_POS,
127 .min_uV = MIN_UV,
128 .uV_step = STEP_UV,
129 .n_voltages = N_VOLTAGES,
130 .owner = THIS_MODULE,
131 .vsel_reg = RT4801_REG_VOP,
132 .vsel_mask = VOUT_MASK,
135 .name = "DSVN",
136 .ops = &rt4801_regulator_ops,
137 .of_match = of_match_ptr("DSVN"),
138 .type = REGULATOR_VOLTAGE,
139 .id = DSV_OUT_NEG,
140 .min_uV = MIN_UV,
141 .uV_step = STEP_UV,
142 .n_voltages = N_VOLTAGES,
143 .owner = THIS_MODULE,
144 .vsel_reg = RT4801_REG_VON,
145 .vsel_mask = VOUT_MASK,
149 static const struct regmap_config rt4801_regmap_config = {
150 .reg_bits = 8,
151 .val_bits = 8,
152 .max_register = RT4801_REG_APPS,
155 static int rt4801_probe(struct i2c_client *i2c)
157 struct rt4801_priv *priv;
158 struct regmap *regmap;
159 int i;
161 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
162 if (!priv)
163 return -ENOMEM;
165 priv->dev = &i2c->dev;
166 /* bootloader will on, driver only reconfigure enable to all output high */
167 priv->enable_flag = DSVALL_ENABLE;
169 regmap = devm_regmap_init_i2c(i2c, &rt4801_regmap_config);
170 if (IS_ERR(regmap)) {
171 dev_err(&i2c->dev, "Failed to init regmap\n");
172 return PTR_ERR(regmap);
175 priv->enable_gpios = devm_gpiod_get_array_optional(&i2c->dev, "enable", GPIOD_OUT_HIGH);
176 if (IS_ERR(priv->enable_gpios)) {
177 dev_err(&i2c->dev, "Failed to get gpios\n");
178 return PTR_ERR(priv->enable_gpios);
181 for (i = 0; i < DSV_OUT_MAX; i++) {
182 const struct regulator_desc *desc = rt4801_regulator_descs + i;
183 struct regulator_config config = { .dev = &i2c->dev, .driver_data = priv,
184 .regmap = regmap, };
185 struct regulator_dev *rdev;
186 unsigned int val;
187 int ret;
189 /* initialize volt_sel variable */
190 ret = regmap_read(regmap, desc->vsel_reg, &val);
191 if (ret)
192 return ret;
194 priv->volt_sel[i] = val & desc->vsel_mask;
196 rdev = devm_regulator_register(&i2c->dev, desc, &config);
197 if (IS_ERR(rdev)) {
198 dev_err(&i2c->dev, "Failed to register [%d] regulator\n", i);
199 return PTR_ERR(rdev);
203 return 0;
206 static const struct of_device_id __maybe_unused rt4801_of_id[] = {
207 { .compatible = "richtek,rt4801", },
208 { },
210 MODULE_DEVICE_TABLE(of, rt4801_of_id);
212 static struct i2c_driver rt4801_driver = {
213 .driver = {
214 .name = "rt4801",
215 .of_match_table = of_match_ptr(rt4801_of_id),
217 .probe_new = rt4801_probe,
219 module_i2c_driver(rt4801_driver);
221 MODULE_AUTHOR("ChiYuan Hwang <cy_huang@richtek.com>");
222 MODULE_DESCRIPTION("Richtek RT4801 Display Bias Driver");
223 MODULE_LICENSE("GPL v2");