]>
Commit | Line | Data |
---|---|---|
e6ff10f2 VW |
1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* Copyright (C) 2020 Axis Communications AB */ | |
3 | ||
4 | #include <linux/of_device.h> | |
5 | #include <linux/regulator/of_regulator.h> | |
6 | #include <linux/regulator/machine.h> | |
7 | #include <linux/regulator/driver.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/regmap.h> | |
10 | #include <linux/err.h> | |
11 | #include <linux/i2c.h> | |
86f162c9 | 12 | #include "da9121-regulator.h" |
e6ff10f2 VW |
13 | |
14 | #define DA9121_MIN_MV 300 | |
15 | #define DA9121_MAX_MV 1900 | |
16 | #define DA9121_STEP_MV 10 | |
17 | #define DA9121_MIN_SEL (DA9121_MIN_MV / DA9121_STEP_MV) | |
18 | #define DA9121_N_VOLTAGES (((DA9121_MAX_MV - DA9121_MIN_MV) / DA9121_STEP_MV) \ | |
19 | + 1 + DA9121_MIN_SEL) | |
20 | ||
21 | static const struct regmap_config da9121_regmap_config = { | |
22 | .reg_bits = 8, | |
23 | .val_bits = 8, | |
24 | }; | |
25 | ||
26 | static const struct regulator_ops da9121_buck_ops = { | |
27 | .enable = regulator_enable_regmap, | |
28 | .disable = regulator_disable_regmap, | |
29 | .is_enabled = regulator_is_enabled_regmap, | |
30 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
31 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
32 | .list_voltage = regulator_list_voltage_linear, | |
33 | }; | |
34 | ||
35 | static const struct regulator_desc da9121_reg = { | |
36 | .name = "da9121", | |
37 | .of_match = "buck1", | |
38 | .owner = THIS_MODULE, | |
39 | .ops = &da9121_buck_ops, | |
40 | .type = REGULATOR_VOLTAGE, | |
41 | .n_voltages = DA9121_N_VOLTAGES, | |
42 | .min_uV = DA9121_MIN_MV * 1000, | |
43 | .uV_step = DA9121_STEP_MV * 1000, | |
44 | .linear_min_sel = DA9121_MIN_SEL, | |
86f162c9 AW |
45 | .vsel_reg = DA9121_REG_BUCK_BUCK1_5, |
46 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
47 | .enable_reg = DA9121_REG_BUCK_BUCK1_0, | |
48 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
e6ff10f2 VW |
49 | /* Default value of BUCK_BUCK1_0.CH1_SRC_DVC_UP */ |
50 | .ramp_delay = 20000, | |
51 | /* tBUCK_EN */ | |
52 | .enable_time = 20, | |
53 | }; | |
54 | ||
55 | static const struct of_device_id da9121_dt_ids[] = { | |
56 | { .compatible = "dlg,da9121", }, | |
57 | { } | |
58 | }; | |
59 | MODULE_DEVICE_TABLE(of, da9121_dt_ids); | |
60 | ||
61 | static int da9121_i2c_probe(struct i2c_client *i2c, | |
62 | const struct i2c_device_id *id) | |
63 | { | |
64 | struct device *dev = &i2c->dev; | |
65 | struct regulator_config config = {}; | |
66 | struct regulator_dev *rdev; | |
67 | struct regmap *regmap; | |
68 | ||
69 | regmap = devm_regmap_init_i2c(i2c, &da9121_regmap_config); | |
70 | if (IS_ERR(regmap)) | |
71 | return PTR_ERR(regmap); | |
72 | ||
73 | config.dev = &i2c->dev; | |
74 | config.of_node = dev->of_node; | |
75 | config.regmap = regmap; | |
76 | ||
77 | rdev = devm_regulator_register(&i2c->dev, &da9121_reg, &config); | |
78 | if (IS_ERR(rdev)) { | |
79 | dev_err(&i2c->dev, "Failed to register da9121 regulator\n"); | |
80 | return PTR_ERR(rdev); | |
81 | } | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
86 | static const struct i2c_device_id da9121_i2c_id[] = { | |
87 | { "da9121", 0 }, | |
88 | {}, | |
89 | }; | |
90 | MODULE_DEVICE_TABLE(i2c, da9121_i2c_id); | |
91 | ||
92 | static struct i2c_driver da9121_regulator_driver = { | |
93 | .driver = { | |
94 | .name = "da9121", | |
95 | .of_match_table = of_match_ptr(da9121_dt_ids), | |
96 | }, | |
97 | .probe = da9121_i2c_probe, | |
98 | .id_table = da9121_i2c_id, | |
99 | }; | |
100 | ||
101 | module_i2c_driver(da9121_regulator_driver); | |
102 | ||
103 | MODULE_LICENSE("GPL v2"); |