blob: 5adc552dffd58bb4fbc18eecbb164812d414fa78 [file] [log] [blame]
ChiYuan Huangb8c054a2020-09-28 15:19:43 +08001// SPDX-License-Identifier: GPL-2.0+
2
3#include <linux/delay.h>
4#include <linux/gpio/consumer.h>
5#include <linux/i2c.h>
6#include <linux/interrupt.h>
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/property.h>
10#include <linux/regmap.h>
11#include <linux/regulator/driver.h>
12
13#define RTMV20_REG_DEVINFO 0x00
14#define RTMV20_REG_PULSEDELAY 0x01
15#define RTMV20_REG_PULSEWIDTH 0x03
16#define RTMV20_REG_LDCTRL1 0x05
17#define RTMV20_REG_ESPULSEWIDTH 0x06
18#define RTMV20_REG_ESLDCTRL1 0x08
19#define RTMV20_REG_LBP 0x0A
20#define RTMV20_REG_LDCTRL2 0x0B
21#define RTMV20_REG_FSIN1CTRL1 0x0D
22#define RTMV20_REG_FSIN1CTRL3 0x0F
23#define RTMV20_REG_FSIN2CTRL1 0x10
24#define RTMV20_REG_FSIN2CTRL3 0x12
25#define RTMV20_REG_ENCTRL 0x13
26#define RTMV20_REG_STRBVSYNDLYL 0x29
27#define RTMV20_REG_LDIRQ 0x30
28#define RTMV20_REG_LDSTAT 0x40
29#define RTMV20_REG_LDMASK 0x50
30
31#define RTMV20_VID_MASK GENMASK(7, 4)
32#define RICHTEK_VID 0x80
33#define RTMV20_LDCURR_MASK GENMASK(7, 0)
34#define RTMV20_DELAY_MASK GENMASK(9, 0)
35#define RTMV20_WIDTH_MASK GENMASK(13, 0)
36#define RTMV20_WIDTH2_MASK GENMASK(7, 0)
37#define RTMV20_LBPLVL_MASK GENMASK(3, 0)
38#define RTMV20_LBPEN_MASK BIT(7)
39#define RTMV20_STROBEPOL_MASK BIT(1)
40#define RTMV20_VSYNPOL_MASK BIT(1)
41#define RTMV20_FSINEN_MASK BIT(7)
42#define RTMV20_ESEN_MASK BIT(6)
43#define RTMV20_FSINOUT_MASK BIT(2)
44#define LDENABLE_MASK (BIT(3) | BIT(0))
45
46#define OTPEVT_MASK BIT(4)
47#define SHORTEVT_MASK BIT(3)
48#define OPENEVT_MASK BIT(2)
49#define LBPEVT_MASK BIT(1)
50#define OCPEVT_MASK BIT(0)
51#define FAILEVT_MASK (SHORTEVT_MASK | OPENEVT_MASK | LBPEVT_MASK)
52
53#define RTMV20_LSW_MINUA 0
54#define RTMV20_LSW_MAXUA 6000000
55#define RTMV20_LSW_STEPUA 30000
56
57#define RTMV20_LSW_DEFAULTUA 3000000
58
59#define RTMV20_I2CRDY_TIMEUS 200
60#define RTMV20_CSRDY_TIMEUS 2000
61
62struct rtmv20_priv {
63 struct device *dev;
64 struct regmap *regmap;
65 struct gpio_desc *enable_gpio;
66 struct regulator_dev *rdev;
67};
68
69static int rtmv20_lsw_enable(struct regulator_dev *rdev)
70{
71 struct rtmv20_priv *priv = rdev_get_drvdata(rdev);
72 int ret;
73
74 gpiod_set_value(priv->enable_gpio, 1);
75
76 /* Wait for I2C can be accessed */
77 usleep_range(RTMV20_I2CRDY_TIMEUS, RTMV20_I2CRDY_TIMEUS + 100);
78
79 /* HW re-enable, disable cache only and sync regcache here */
80 regcache_cache_only(priv->regmap, false);
81 ret = regcache_sync(priv->regmap);
82 if (ret)
83 return ret;
84
85 return regulator_enable_regmap(rdev);
86}
87
88static int rtmv20_lsw_disable(struct regulator_dev *rdev)
89{
90 struct rtmv20_priv *priv = rdev_get_drvdata(rdev);
91 int ret;
92
93 ret = regulator_disable_regmap(rdev);
94 if (ret)
95 return ret;
96
97 /* Mark the regcache as dirty and cache only before HW disabled */
98 regcache_cache_only(priv->regmap, true);
99 regcache_mark_dirty(priv->regmap);
100
101 gpiod_set_value(priv->enable_gpio, 0);
102
103 return 0;
104}
105
Axel Lin86ab21c2021-05-30 20:41:00 +0800106static int rtmv20_lsw_set_current_limit(struct regulator_dev *rdev, int min_uA,
107 int max_uA)
108{
109 int sel;
110
111 if (min_uA > RTMV20_LSW_MAXUA || max_uA < RTMV20_LSW_MINUA)
112 return -EINVAL;
113
114 if (max_uA > RTMV20_LSW_MAXUA)
115 max_uA = RTMV20_LSW_MAXUA;
116
117 sel = (max_uA - RTMV20_LSW_MINUA) / RTMV20_LSW_STEPUA;
118
119 /* Ensure the selected setting is still in range */
120 if ((sel * RTMV20_LSW_STEPUA + RTMV20_LSW_MINUA) < min_uA)
121 return -EINVAL;
122
123 sel <<= ffs(rdev->desc->csel_mask) - 1;
124
125 return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
126 rdev->desc->csel_mask, sel);
127}
128
129static int rtmv20_lsw_get_current_limit(struct regulator_dev *rdev)
130{
131 unsigned int val;
132 int ret;
133
134 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
135 if (ret)
136 return ret;
137
138 val &= rdev->desc->csel_mask;
139 val >>= ffs(rdev->desc->csel_mask) - 1;
140
141 return val * RTMV20_LSW_STEPUA + RTMV20_LSW_MINUA;
142}
143
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800144static const struct regulator_ops rtmv20_regulator_ops = {
Axel Lin86ab21c2021-05-30 20:41:00 +0800145 .set_current_limit = rtmv20_lsw_set_current_limit,
146 .get_current_limit = rtmv20_lsw_get_current_limit,
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800147 .enable = rtmv20_lsw_enable,
148 .disable = rtmv20_lsw_disable,
149 .is_enabled = regulator_is_enabled_regmap,
150};
151
152static const struct regulator_desc rtmv20_lsw_desc = {
153 .name = "rtmv20,lsw",
154 .of_match = of_match_ptr("lsw"),
155 .type = REGULATOR_CURRENT,
156 .owner = THIS_MODULE,
157 .ops = &rtmv20_regulator_ops,
158 .csel_reg = RTMV20_REG_LDCTRL1,
159 .csel_mask = RTMV20_LDCURR_MASK,
160 .enable_reg = RTMV20_REG_ENCTRL,
161 .enable_mask = LDENABLE_MASK,
162 .enable_time = RTMV20_CSRDY_TIMEUS,
163};
164
165static irqreturn_t rtmv20_irq_handler(int irq, void *data)
166{
167 struct rtmv20_priv *priv = data;
168 unsigned int val;
169 int ret;
170
171 ret = regmap_read(priv->regmap, RTMV20_REG_LDIRQ, &val);
172 if (ret) {
173 dev_err(priv->dev, "Failed to get irq flags\n");
174 return IRQ_NONE;
175 }
176
177 if (val & OTPEVT_MASK)
178 regulator_notifier_call_chain(priv->rdev, REGULATOR_EVENT_OVER_TEMP, NULL);
179
180 if (val & OCPEVT_MASK)
181 regulator_notifier_call_chain(priv->rdev, REGULATOR_EVENT_OVER_CURRENT, NULL);
182
183 if (val & FAILEVT_MASK)
184 regulator_notifier_call_chain(priv->rdev, REGULATOR_EVENT_FAIL, NULL);
185
186 return IRQ_HANDLED;
187}
188
189static u32 clamp_to_selector(u32 val, u32 min, u32 max, u32 step)
190{
191 u32 retval = clamp_val(val, min, max);
192
193 return (retval - min) / step;
194}
195
196static int rtmv20_properties_init(struct rtmv20_priv *priv)
197{
198 const struct {
199 const char *name;
200 u32 def;
201 u32 min;
202 u32 max;
203 u32 step;
204 u32 addr;
205 u32 mask;
206 } props[] = {
ChiYuan Huang89a5f772020-09-30 18:08:00 +0800207 { "richtek,ld-pulse-delay-us", 0, 0, 100000, 100, RTMV20_REG_PULSEDELAY,
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800208 RTMV20_DELAY_MASK },
ChiYuan Huang89a5f772020-09-30 18:08:00 +0800209 { "richtek,ld-pulse-width-us", 1200, 0, 10000, 1, RTMV20_REG_PULSEWIDTH,
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800210 RTMV20_WIDTH_MASK },
ChiYuan Huang89a5f772020-09-30 18:08:00 +0800211 { "richtek,fsin1-delay-us", 23000, 0, 100000, 100, RTMV20_REG_FSIN1CTRL1,
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800212 RTMV20_DELAY_MASK },
ChiYuan Huang89a5f772020-09-30 18:08:00 +0800213 { "richtek,fsin1-width-us", 160, 40, 10000, 40, RTMV20_REG_FSIN1CTRL3,
214 RTMV20_WIDTH2_MASK },
215 { "richtek,fsin2-delay-us", 23000, 0, 100000, 100, RTMV20_REG_FSIN2CTRL1,
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800216 RTMV20_DELAY_MASK },
ChiYuan Huang89a5f772020-09-30 18:08:00 +0800217 { "richtek,fsin2-width-us", 160, 40, 10000, 40, RTMV20_REG_FSIN2CTRL3,
218 RTMV20_WIDTH2_MASK },
219 { "richtek,es-pulse-width-us", 1200, 0, 10000, 1, RTMV20_REG_ESPULSEWIDTH,
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800220 RTMV20_WIDTH_MASK },
ChiYuan Huang89a5f772020-09-30 18:08:00 +0800221 { "richtek,es-ld-current-microamp", 3000000, 0, 6000000, 30000,
222 RTMV20_REG_ESLDCTRL1, RTMV20_LDCURR_MASK },
223 { "richtek,lbp-level-microvolt", 2700000, 2400000, 3700000, 100000, RTMV20_REG_LBP,
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800224 RTMV20_LBPLVL_MASK },
ChiYuan Huang89a5f772020-09-30 18:08:00 +0800225 { "richtek,lbp-enable", 0, 0, 1, 1, RTMV20_REG_LBP, RTMV20_LBPEN_MASK },
226 { "richtek,strobe-polarity-high", 1, 0, 1, 1, RTMV20_REG_LDCTRL2,
227 RTMV20_STROBEPOL_MASK },
228 { "richtek,vsync-polarity-high", 1, 0, 1, 1, RTMV20_REG_LDCTRL2,
229 RTMV20_VSYNPOL_MASK },
230 { "richtek,fsin-enable", 0, 0, 1, 1, RTMV20_REG_ENCTRL, RTMV20_FSINEN_MASK },
231 { "richtek,fsin-output", 0, 0, 1, 1, RTMV20_REG_ENCTRL, RTMV20_FSINOUT_MASK },
232 { "richtek,es-enable", 0, 0, 1, 1, RTMV20_REG_ENCTRL, RTMV20_ESEN_MASK },
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800233 };
234 int i, ret;
235
236 for (i = 0; i < ARRAY_SIZE(props); i++) {
237 __be16 bval16;
238 u16 val16;
239 u32 temp;
240 int significant_bit = fls(props[i].mask);
241 int shift = ffs(props[i].mask) - 1;
242
243 if (props[i].max > 1) {
244 ret = device_property_read_u32(priv->dev, props[i].name, &temp);
245 if (ret)
246 temp = props[i].def;
247 } else
248 temp = device_property_read_bool(priv->dev, props[i].name);
249
250 temp = clamp_to_selector(temp, props[i].min, props[i].max, props[i].step);
251
252 /* If significant bit is over 8, two byte access, others one */
253 if (significant_bit > 8) {
254 ret = regmap_raw_read(priv->regmap, props[i].addr, &bval16, sizeof(bval16));
255 if (ret)
256 return ret;
257
258 val16 = be16_to_cpu(bval16);
259 val16 &= ~props[i].mask;
260 val16 |= (temp << shift);
261 bval16 = cpu_to_be16(val16);
262
263 ret = regmap_raw_write(priv->regmap, props[i].addr, &bval16,
264 sizeof(bval16));
265 } else {
266 ret = regmap_update_bits(priv->regmap, props[i].addr, props[i].mask,
267 temp << shift);
268 }
269
270 if (ret)
271 return ret;
272 }
273
274 return 0;
275}
276
277static int rtmv20_check_chip_exist(struct rtmv20_priv *priv)
278{
279 unsigned int val;
280 int ret;
281
282 ret = regmap_read(priv->regmap, RTMV20_REG_DEVINFO, &val);
283 if (ret)
284 return ret;
285
286 if ((val & RTMV20_VID_MASK) != RICHTEK_VID)
287 return -ENODEV;
288
289 return 0;
290}
291
292static bool rtmv20_is_accessible_reg(struct device *dev, unsigned int reg)
293{
294 switch (reg) {
295 case RTMV20_REG_DEVINFO ... RTMV20_REG_STRBVSYNDLYL:
296 case RTMV20_REG_LDIRQ:
297 case RTMV20_REG_LDSTAT:
298 case RTMV20_REG_LDMASK:
299 return true;
300 }
301 return false;
302}
303
304static bool rtmv20_is_volatile_reg(struct device *dev, unsigned int reg)
305{
306 if (reg == RTMV20_REG_LDIRQ || reg == RTMV20_REG_LDSTAT)
307 return true;
308 return false;
309}
310
311static const struct regmap_config rtmv20_regmap_config = {
312 .reg_bits = 8,
313 .val_bits = 8,
314 .cache_type = REGCACHE_RBTREE,
315 .max_register = RTMV20_REG_LDMASK,
316
317 .writeable_reg = rtmv20_is_accessible_reg,
318 .readable_reg = rtmv20_is_accessible_reg,
319 .volatile_reg = rtmv20_is_volatile_reg,
320};
321
322static int rtmv20_probe(struct i2c_client *i2c)
323{
324 struct rtmv20_priv *priv;
325 struct regulator_config config = {};
326 int ret;
327
328 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
329 if (!priv)
330 return -ENOMEM;
331
332 priv->dev = &i2c->dev;
333
334 /* Before regmap register, configure HW enable to make I2C accessible */
335 priv->enable_gpio = devm_gpiod_get(&i2c->dev, "enable", GPIOD_OUT_HIGH);
336 if (IS_ERR(priv->enable_gpio)) {
337 dev_err(&i2c->dev, "Failed to get enable gpio\n");
338 return PTR_ERR(priv->enable_gpio);
339 }
340
341 /* Wait for I2C can be accessed */
342 usleep_range(RTMV20_I2CRDY_TIMEUS, RTMV20_I2CRDY_TIMEUS + 100);
343
344 priv->regmap = devm_regmap_init_i2c(i2c, &rtmv20_regmap_config);
345 if (IS_ERR(priv->regmap)) {
346 dev_err(&i2c->dev, "Failed to allocate register map\n");
347 return PTR_ERR(priv->regmap);
348 }
349
350 ret = rtmv20_check_chip_exist(priv);
351 if (ret) {
352 dev_err(&i2c->dev, "Chip vendor info is not matched\n");
353 return ret;
354 }
355
356 ret = rtmv20_properties_init(priv);
357 if (ret) {
358 dev_err(&i2c->dev, "Failed to init properties\n");
359 return ret;
360 }
361
362 /*
363 * keep in shutdown mode to minimize the current consumption
364 * and also mark regcache as dirty
365 */
ChiYuan Huang6228cc82020-09-30 18:18:52 +0800366 regcache_cache_only(priv->regmap, true);
ChiYuan Huangb8c054a2020-09-28 15:19:43 +0800367 regcache_mark_dirty(priv->regmap);
368 gpiod_set_value(priv->enable_gpio, 0);
369
370 config.dev = &i2c->dev;
371 config.regmap = priv->regmap;
372 config.driver_data = priv;
373 priv->rdev = devm_regulator_register(&i2c->dev, &rtmv20_lsw_desc, &config);
374 if (IS_ERR(priv->rdev)) {
375 dev_err(&i2c->dev, "Failed to register regulator\n");
376 return PTR_ERR(priv->rdev);
377 }
378
379 /* Unmask all events before IRQ registered */
380 ret = regmap_write(priv->regmap, RTMV20_REG_LDMASK, 0);
381 if (ret)
382 return ret;
383
384 return devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, rtmv20_irq_handler,
385 IRQF_ONESHOT, dev_name(&i2c->dev), priv);
386}
387
388static int __maybe_unused rtmv20_suspend(struct device *dev)
389{
390 struct i2c_client *i2c = to_i2c_client(dev);
391
392 /*
393 * When system suspend, disable irq to prevent interrupt trigger
394 * during I2C bus suspend
395 */
396 disable_irq(i2c->irq);
397 if (device_may_wakeup(dev))
398 enable_irq_wake(i2c->irq);
399
400 return 0;
401}
402
403static int __maybe_unused rtmv20_resume(struct device *dev)
404{
405 struct i2c_client *i2c = to_i2c_client(dev);
406
407 /* Enable irq after I2C bus already resume */
408 enable_irq(i2c->irq);
409 if (device_may_wakeup(dev))
410 disable_irq_wake(i2c->irq);
411
412 return 0;
413}
414
415static SIMPLE_DEV_PM_OPS(rtmv20_pm, rtmv20_suspend, rtmv20_resume);
416
417static const struct of_device_id __maybe_unused rtmv20_of_id[] = {
418 { .compatible = "richtek,rtmv20", },
419 {}
420};
421MODULE_DEVICE_TABLE(of, rtmv20_of_id);
422
423static struct i2c_driver rtmv20_driver = {
424 .driver = {
425 .name = "rtmv20",
426 .of_match_table = of_match_ptr(rtmv20_of_id),
427 .pm = &rtmv20_pm,
428 },
429 .probe_new = rtmv20_probe,
430};
431module_i2c_driver(rtmv20_driver);
432
433MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
434MODULE_DESCRIPTION("Richtek RTMV20 Regulator Driver");
435MODULE_LICENSE("GPL v2");