ARM: multi_v5_defconfig: make DaVinci part of the ARM v5 multiplatform build
[linux-2.6-microblaze.git] / drivers / regulator / tps65132-regulator.c
1 /*
2  * TI TPS65132 Regulator driver
3  *
4  * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
5  *
6  * Author: Venkat Reddy Talla <vreddytalla@nvidia.com>
7  *              Laxman Dewangan <ldewangan@nvidia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
15  * whether express or implied; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  */
19
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/i2c.h>
24 #include <linux/module.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28
29 #define TPS65132_REG_VPOS               0x00
30 #define TPS65132_REG_VNEG               0x01
31 #define TPS65132_REG_APPS_DISP_DISN     0x03
32 #define TPS65132_REG_CONTROL            0x0FF
33
34 #define TPS65132_VOUT_MASK              0x1F
35 #define TPS65132_VOUT_N_VOLTAGE         0x15
36 #define TPS65132_VOUT_VMIN              4000000
37 #define TPS65132_VOUT_VMAX              6000000
38 #define TPS65132_VOUT_STEP              100000
39
40 #define TPS65132_REG_APPS_DIS_VPOS              BIT(0)
41 #define TPS65132_REG_APPS_DIS_VNEG              BIT(1)
42
43 #define TPS65132_REGULATOR_ID_VPOS      0
44 #define TPS65132_REGULATOR_ID_VNEG      1
45 #define TPS65132_MAX_REGULATORS         2
46
47 #define TPS65132_ACT_DIS_TIME_SLACK             1000
48
49 struct tps65132_reg_pdata {
50         struct gpio_desc *en_gpiod;
51         struct gpio_desc *act_dis_gpiod;
52         unsigned int act_dis_time_us;
53         int ena_gpio_state;
54 };
55
56 struct tps65132_regulator {
57         struct device *dev;
58         struct tps65132_reg_pdata reg_pdata[TPS65132_MAX_REGULATORS];
59 };
60
61 static int tps65132_regulator_enable(struct regulator_dev *rdev)
62 {
63         struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
64         int id = rdev_get_id(rdev);
65         struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
66         int ret;
67
68         if (!IS_ERR(rpdata->en_gpiod)) {
69                 gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
70                 rpdata->ena_gpio_state = 1;
71         }
72
73         /* Hardware automatically enable discharge bit in enable */
74         if (rdev->constraints->active_discharge ==
75                         REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
76                 ret = regulator_set_active_discharge_regmap(rdev, false);
77                 if (ret < 0) {
78                         dev_err(tps->dev, "Failed to disable active discharge: %d\n",
79                                 ret);
80                         return ret;
81                 }
82         }
83
84         return 0;
85 }
86
87 static int tps65132_regulator_disable(struct regulator_dev *rdev)
88 {
89         struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
90         int id = rdev_get_id(rdev);
91         struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
92
93         if (!IS_ERR(rpdata->en_gpiod)) {
94                 gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
95                 rpdata->ena_gpio_state = 0;
96         }
97
98         if (!IS_ERR(rpdata->act_dis_gpiod)) {
99                 gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 1);
100                 usleep_range(rpdata->act_dis_time_us, rpdata->act_dis_time_us +
101                              TPS65132_ACT_DIS_TIME_SLACK);
102                 gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 0);
103         }
104
105         return 0;
106 }
107
108 static int tps65132_regulator_is_enabled(struct regulator_dev *rdev)
109 {
110         struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
111         int id = rdev_get_id(rdev);
112         struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
113
114         if (!IS_ERR(rpdata->en_gpiod))
115                 return rpdata->ena_gpio_state;
116
117         return 1;
118 }
119
120 static const struct regulator_ops tps65132_regulator_ops = {
121         .enable = tps65132_regulator_enable,
122         .disable = tps65132_regulator_disable,
123         .is_enabled = tps65132_regulator_is_enabled,
124         .list_voltage = regulator_list_voltage_linear,
125         .map_voltage = regulator_map_voltage_linear,
126         .get_voltage_sel = regulator_get_voltage_sel_regmap,
127         .set_voltage_sel = regulator_set_voltage_sel_regmap,
128         .set_active_discharge = regulator_set_active_discharge_regmap,
129 };
130
131 static int tps65132_of_parse_cb(struct device_node *np,
132                                 const struct regulator_desc *desc,
133                                 struct regulator_config *config)
134 {
135         struct tps65132_regulator *tps = config->driver_data;
136         struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[desc->id];
137         int ret;
138
139         rpdata->en_gpiod = devm_fwnode_get_index_gpiod_from_child(tps->dev,
140                                         "enable", 0, &np->fwnode, 0, "enable");
141         if (IS_ERR(rpdata->en_gpiod)) {
142                 ret = PTR_ERR(rpdata->en_gpiod);
143
144                 /* Ignore the error other than probe defer */
145                 if (ret == -EPROBE_DEFER)
146                         return ret;
147                 return 0;
148         }
149
150         rpdata->act_dis_gpiod = devm_fwnode_get_index_gpiod_from_child(
151                                         tps->dev, "active-discharge", 0,
152                                         &np->fwnode, 0, "active-discharge");
153         if (IS_ERR(rpdata->act_dis_gpiod)) {
154                 ret = PTR_ERR(rpdata->act_dis_gpiod);
155
156                 /* Ignore the error other than probe defer */
157                 if (ret == -EPROBE_DEFER)
158                         return ret;
159
160                 return 0;
161         }
162
163         ret = of_property_read_u32(np, "ti,active-discharge-time-us",
164                                    &rpdata->act_dis_time_us);
165         if (ret < 0) {
166                 dev_err(tps->dev, "Failed to read active discharge time:%d\n",
167                         ret);
168                 return ret;
169         }
170
171         return 0;
172 }
173
174 #define TPS65132_REGULATOR_DESC(_id, _name)             \
175         [TPS65132_REGULATOR_ID_##_id] = {               \
176                 .name = "tps65132-"#_name,              \
177                 .supply_name = "vin",                   \
178                 .id = TPS65132_REGULATOR_ID_##_id,      \
179                 .of_match = of_match_ptr(#_name),       \
180                 .of_parse_cb    = tps65132_of_parse_cb, \
181                 .ops = &tps65132_regulator_ops,         \
182                 .n_voltages = TPS65132_VOUT_N_VOLTAGE,  \
183                 .min_uV = TPS65132_VOUT_VMIN,           \
184                 .uV_step = TPS65132_VOUT_STEP,          \
185                 .enable_time = 500,                     \
186                 .vsel_mask = TPS65132_VOUT_MASK,        \
187                 .vsel_reg = TPS65132_REG_##_id,         \
188                 .active_discharge_off = 0,                      \
189                 .active_discharge_on = TPS65132_REG_APPS_DIS_##_id, \
190                 .active_discharge_mask = TPS65132_REG_APPS_DIS_##_id, \
191                 .active_discharge_reg = TPS65132_REG_APPS_DISP_DISN, \
192                 .type = REGULATOR_VOLTAGE,              \
193                 .owner = THIS_MODULE,                   \
194         }
195
196 static const struct regulator_desc tps_regs_desc[TPS65132_MAX_REGULATORS] = {
197         TPS65132_REGULATOR_DESC(VPOS, outp),
198         TPS65132_REGULATOR_DESC(VNEG, outn),
199 };
200
201 static const struct regmap_range tps65132_no_reg_ranges[] = {
202         regmap_reg_range(TPS65132_REG_APPS_DISP_DISN + 1,
203                          TPS65132_REG_CONTROL - 1),
204 };
205
206 static const struct regmap_access_table tps65132_no_reg_table = {
207         .no_ranges = tps65132_no_reg_ranges,
208         .n_no_ranges = ARRAY_SIZE(tps65132_no_reg_ranges),
209 };
210
211 static const struct regmap_config tps65132_regmap_config = {
212         .reg_bits       = 8,
213         .val_bits       = 8,
214         .max_register   = TPS65132_REG_CONTROL,
215         .cache_type     = REGCACHE_NONE,
216         .rd_table       = &tps65132_no_reg_table,
217         .wr_table       = &tps65132_no_reg_table,
218 };
219
220 static int tps65132_probe(struct i2c_client *client,
221                           const struct i2c_device_id *client_id)
222 {
223         struct device *dev = &client->dev;
224         struct tps65132_regulator *tps;
225         struct regulator_dev *rdev;
226         struct regmap *rmap;
227         struct regulator_config config = { };
228         int id;
229         int ret;
230
231         tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);
232         if (!tps)
233                 return -ENOMEM;
234
235         rmap = devm_regmap_init_i2c(client, &tps65132_regmap_config);
236         if (IS_ERR(rmap)) {
237                 ret = PTR_ERR(rmap);
238                 dev_err(dev, "regmap init failed: %d\n", ret);
239                 return ret;
240         }
241
242         i2c_set_clientdata(client, tps);
243         tps->dev = dev;
244
245         for (id = 0; id < TPS65132_MAX_REGULATORS; ++id) {
246                 config.regmap = rmap;
247                 config.dev = dev;
248                 config.driver_data = tps;
249
250                 rdev = devm_regulator_register(dev, &tps_regs_desc[id],
251                                                &config);
252                 if (IS_ERR(rdev)) {
253                         ret = PTR_ERR(rdev);
254                         dev_err(dev, "regulator %s register failed: %d\n",
255                                 tps_regs_desc[id].name, ret);
256                         return ret;
257                 }
258         }
259         return 0;
260 }
261
262 static const struct i2c_device_id tps65132_id[] = {
263         {.name = "tps65132",},
264         {},
265 };
266 MODULE_DEVICE_TABLE(i2c, tps65132_id);
267
268 static struct i2c_driver tps65132_i2c_driver = {
269         .driver = {
270                 .name = "tps65132",
271         },
272         .probe = tps65132_probe,
273         .id_table = tps65132_id,
274 };
275
276 module_i2c_driver(tps65132_i2c_driver);
277
278 MODULE_DESCRIPTION("tps65132 regulator driver");
279 MODULE_AUTHOR("Venkat Reddy Talla <vreddytalla@nvidia.com>");
280 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
281 MODULE_LICENSE("GPL v2");