blob: 7a87788d3f0922399d54d88a4e35573aa5931001 [file] [log] [blame]
ChiYuan Huang5bbbfc7f2020-08-15 11:14:20 +08001// SPDX-License-Identifier: GPL-2.0+
2
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>
10
11#define RT4801_REG_VOP 0x00
12#define RT4801_REG_VON 0x01
13#define RT4801_REG_APPS 0x03
14
15#define VOUT_MASK 0x1F
16
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)
21
22#define DSV_OUT_POS 0
23#define DSV_OUT_NEG 1
24#define DSV_OUT_MAX 2
25
26#define DSVP_ENABLE BIT(0)
27#define DSVN_ENABLE BIT(1)
28#define DSVALL_ENABLE (DSVP_ENABLE | DSVN_ENABLE)
29
30struct 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];
35};
36
37static int rt4801_set_voltage_sel(struct regulator_dev *rdev, unsigned int selector)
38{
39 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
40 int id = rdev_get_id(rdev), ret;
41
42 if (priv->enable_flag & BIT(id)) {
43 ret = regulator_set_voltage_sel_regmap(rdev, selector);
44 if (ret)
45 return ret;
46 }
47
48 priv->volt_sel[id] = selector;
49 return 0;
50}
51
52static int rt4801_get_voltage_sel(struct regulator_dev *rdev)
53{
54 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
55 int id = rdev_get_id(rdev);
56
57 if (priv->enable_flag & BIT(id))
58 return regulator_get_voltage_sel_regmap(rdev);
59
60 return priv->volt_sel[id];
61}
62
63static int rt4801_enable(struct regulator_dev *rdev)
64{
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;
68
Axel Lincb2381c2021-06-03 17:49:44 +080069 if (!gpios || gpios->ndescs <= id) {
ChiYuan Huang5bbbfc7f2020-08-15 11:14:20 +080070 dev_warn(&rdev->dev, "no dedicated gpio can control\n");
71 goto bypass_gpio;
72 }
73
74 gpiod_set_value(gpios->desc[id], 1);
75
76bypass_gpio:
77 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, priv->volt_sel[id]);
78 if (ret)
79 return ret;
80
81 priv->enable_flag |= BIT(id);
82 return 0;
83}
84
85static int rt4801_disable(struct regulator_dev *rdev)
86{
87 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
88 struct gpio_descs *gpios = priv->enable_gpios;
89 int id = rdev_get_id(rdev);
90
Axel Lincb2381c2021-06-03 17:49:44 +080091 if (!gpios || gpios->ndescs <= id) {
ChiYuan Huang5bbbfc7f2020-08-15 11:14:20 +080092 dev_warn(&rdev->dev, "no dedicated gpio can control\n");
93 goto bypass_gpio;
94 }
95
96 gpiod_set_value(gpios->desc[id], 0);
97
98bypass_gpio:
99 priv->enable_flag &= ~BIT(id);
100 return 0;
101}
102
103static int rt4801_is_enabled(struct regulator_dev *rdev)
104{
105 struct rt4801_priv *priv = rdev_get_drvdata(rdev);
106 int id = rdev_get_id(rdev);
107
108 return !!(priv->enable_flag & BIT(id));
109}
110
111static 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,
118};
119
120static const struct regulator_desc rt4801_regulator_descs[] = {
121 {
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,
133 },
134 {
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,
146 },
147};
148
149static const struct regmap_config rt4801_regmap_config = {
150 .reg_bits = 8,
151 .val_bits = 8,
152 .max_register = RT4801_REG_APPS,
153};
154
155static int rt4801_probe(struct i2c_client *i2c)
156{
157 struct rt4801_priv *priv;
158 struct regmap *regmap;
159 int i;
160
161 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
162 if (!priv)
163 return -ENOMEM;
164
165 priv->dev = &i2c->dev;
166 /* bootloader will on, driver only reconfigure enable to all output high */
167 priv->enable_flag = DSVALL_ENABLE;
168
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);
173 }
174
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);
179 }
180
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;
188
189 /* initialize volt_sel variable */
190 ret = regmap_read(regmap, desc->vsel_reg, &val);
191 if (ret)
192 return ret;
193
194 priv->volt_sel[i] = val & desc->vsel_mask;
195
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);
200 }
201 }
202
203 return 0;
204}
205
ChiYuan Huangc8b2c892020-08-24 10:05:13 +0800206static const struct of_device_id __maybe_unused rt4801_of_id[] = {
ChiYuan Huang5bbbfc7f2020-08-15 11:14:20 +0800207 { .compatible = "richtek,rt4801", },
208 { },
209};
210MODULE_DEVICE_TABLE(of, rt4801_of_id);
211
212static struct i2c_driver rt4801_driver = {
213 .driver = {
214 .name = "rt4801",
215 .of_match_table = of_match_ptr(rt4801_of_id),
216 },
217 .probe_new = rt4801_probe,
218};
219module_i2c_driver(rt4801_driver);
220
221MODULE_AUTHOR("ChiYuan Hwang <cy_huang@richtek.com>");
222MODULE_DESCRIPTION("Richtek RT4801 Display Bias Driver");
223MODULE_LICENSE("GPL v2");