Merge tag 'dmaengine-4.20-rc1' of git://git.infradead.org/users/vkoul/slave-dma
[sfrench/cifs-2.6.git] / drivers / regulator / max8952.c
1 /*
2  * max8952.c - Voltage and current regulation for the Maxim 8952
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  * MyungJoo Ham <myungjoo.ham@samsung.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/i2c.h>
25 #include <linux/err.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/max8952.h>
29 #include <linux/gpio.h>
30 #include <linux/gpio/consumer.h>
31 #include <linux/io.h>
32 #include <linux/of.h>
33 #include <linux/of_gpio.h>
34 #include <linux/regulator/of_regulator.h>
35 #include <linux/slab.h>
36
37 /* Registers */
38 enum {
39         MAX8952_REG_MODE0,
40         MAX8952_REG_MODE1,
41         MAX8952_REG_MODE2,
42         MAX8952_REG_MODE3,
43         MAX8952_REG_CONTROL,
44         MAX8952_REG_SYNC,
45         MAX8952_REG_RAMP,
46         MAX8952_REG_CHIP_ID1,
47         MAX8952_REG_CHIP_ID2,
48 };
49
50 struct max8952_data {
51         struct i2c_client       *client;
52         struct max8952_platform_data *pdata;
53
54         bool vid0;
55         bool vid1;
56 };
57
58 static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
59 {
60         int ret = i2c_smbus_read_byte_data(max8952->client, reg);
61
62         if (ret > 0)
63                 ret &= 0xff;
64
65         return ret;
66 }
67
68 static int max8952_write_reg(struct max8952_data *max8952,
69                 u8 reg, u8 value)
70 {
71         return i2c_smbus_write_byte_data(max8952->client, reg, value);
72 }
73
74 static int max8952_list_voltage(struct regulator_dev *rdev,
75                 unsigned int selector)
76 {
77         struct max8952_data *max8952 = rdev_get_drvdata(rdev);
78
79         if (rdev_get_id(rdev) != 0)
80                 return -EINVAL;
81
82         return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000;
83 }
84
85 static int max8952_get_voltage_sel(struct regulator_dev *rdev)
86 {
87         struct max8952_data *max8952 = rdev_get_drvdata(rdev);
88         u8 vid = 0;
89
90         if (max8952->vid0)
91                 vid += 1;
92         if (max8952->vid1)
93                 vid += 2;
94
95         return vid;
96 }
97
98 static int max8952_set_voltage_sel(struct regulator_dev *rdev,
99                                    unsigned selector)
100 {
101         struct max8952_data *max8952 = rdev_get_drvdata(rdev);
102
103         if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
104                         !gpio_is_valid(max8952->pdata->gpio_vid1)) {
105                 /* DVS not supported */
106                 return -EPERM;
107         }
108
109         max8952->vid0 = selector & 0x1;
110         max8952->vid1 = (selector >> 1) & 0x1;
111         gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
112         gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
113
114         return 0;
115 }
116
117 static const struct regulator_ops max8952_ops = {
118         .list_voltage           = max8952_list_voltage,
119         .get_voltage_sel        = max8952_get_voltage_sel,
120         .set_voltage_sel        = max8952_set_voltage_sel,
121 };
122
123 static const struct regulator_desc regulator = {
124         .name           = "MAX8952_VOUT",
125         .id             = 0,
126         .n_voltages     = MAX8952_NUM_DVS_MODE,
127         .ops            = &max8952_ops,
128         .type           = REGULATOR_VOLTAGE,
129         .owner          = THIS_MODULE,
130 };
131
132 #ifdef CONFIG_OF
133 static const struct of_device_id max8952_dt_match[] = {
134         { .compatible = "maxim,max8952" },
135         {},
136 };
137 MODULE_DEVICE_TABLE(of, max8952_dt_match);
138
139 static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
140 {
141         struct max8952_platform_data *pd;
142         struct device_node *np = dev->of_node;
143         int ret;
144         int i;
145
146         pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
147         if (!pd)
148                 return NULL;
149
150         pd->gpio_vid0 = of_get_named_gpio(np, "max8952,vid-gpios", 0);
151         pd->gpio_vid1 = of_get_named_gpio(np, "max8952,vid-gpios", 1);
152
153         if (of_property_read_u32(np, "max8952,default-mode", &pd->default_mode))
154                 dev_warn(dev, "Default mode not specified, assuming 0\n");
155
156         ret = of_property_read_u32_array(np, "max8952,dvs-mode-microvolt",
157                                         pd->dvs_mode, ARRAY_SIZE(pd->dvs_mode));
158         if (ret) {
159                 dev_err(dev, "max8952,dvs-mode-microvolt property not specified");
160                 return NULL;
161         }
162
163         for (i = 0; i < ARRAY_SIZE(pd->dvs_mode); ++i) {
164                 if (pd->dvs_mode[i] < 770000 || pd->dvs_mode[i] > 1400000) {
165                         dev_err(dev, "DVS voltage %d out of range\n", i);
166                         return NULL;
167                 }
168                 pd->dvs_mode[i] = (pd->dvs_mode[i] - 770000) / 10000;
169         }
170
171         if (of_property_read_u32(np, "max8952,sync-freq", &pd->sync_freq))
172                 dev_warn(dev, "max8952,sync-freq property not specified, defaulting to 26MHz\n");
173
174         if (of_property_read_u32(np, "max8952,ramp-speed", &pd->ramp_speed))
175                 dev_warn(dev, "max8952,ramp-speed property not specified, defaulting to 32mV/us\n");
176
177         pd->reg_data = of_get_regulator_init_data(dev, np, &regulator);
178         if (!pd->reg_data) {
179                 dev_err(dev, "Failed to parse regulator init data\n");
180                 return NULL;
181         }
182
183         return pd;
184 }
185 #else
186 static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
187 {
188         return NULL;
189 }
190 #endif
191
192 static int max8952_pmic_probe(struct i2c_client *client,
193                 const struct i2c_device_id *i2c_id)
194 {
195         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
196         struct max8952_platform_data *pdata = dev_get_platdata(&client->dev);
197         struct regulator_config config = { };
198         struct max8952_data *max8952;
199         struct regulator_dev *rdev;
200         struct gpio_desc *gpiod;
201         enum gpiod_flags gflags;
202
203         int ret = 0, err = 0;
204
205         if (client->dev.of_node)
206                 pdata = max8952_parse_dt(&client->dev);
207
208         if (!pdata) {
209                 dev_err(&client->dev, "Require the platform data\n");
210                 return -EINVAL;
211         }
212
213         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
214                 return -EIO;
215
216         max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data),
217                                GFP_KERNEL);
218         if (!max8952)
219                 return -ENOMEM;
220
221         max8952->client = client;
222         max8952->pdata = pdata;
223
224         config.dev = &client->dev;
225         config.init_data = pdata->reg_data;
226         config.driver_data = max8952;
227         config.of_node = client->dev.of_node;
228
229         if (pdata->reg_data->constraints.boot_on)
230                 gflags = GPIOD_OUT_HIGH;
231         else
232                 gflags = GPIOD_OUT_LOW;
233         gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
234         gpiod = devm_gpiod_get_optional(&client->dev,
235                                         "max8952,en",
236                                         gflags);
237         if (IS_ERR(gpiod))
238                 return PTR_ERR(gpiod);
239         if (gpiod)
240                 config.ena_gpiod = gpiod;
241
242         rdev = devm_regulator_register(&client->dev, &regulator, &config);
243         if (IS_ERR(rdev)) {
244                 ret = PTR_ERR(rdev);
245                 dev_err(&client->dev, "regulator init failed (%d)\n", ret);
246                 return ret;
247         }
248
249         max8952->vid0 = pdata->default_mode & 0x1;
250         max8952->vid1 = (pdata->default_mode >> 1) & 0x1;
251
252         if (gpio_is_valid(pdata->gpio_vid0) &&
253                         gpio_is_valid(pdata->gpio_vid1)) {
254                 unsigned long gpio_flags;
255
256                 gpio_flags = max8952->vid0 ?
257                              GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
258                 if (devm_gpio_request_one(&client->dev, pdata->gpio_vid0,
259                                           gpio_flags, "MAX8952 VID0"))
260                         err = 1;
261
262                 gpio_flags = max8952->vid1 ?
263                              GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
264                 if (devm_gpio_request_one(&client->dev, pdata->gpio_vid1,
265                                           gpio_flags, "MAX8952 VID1"))
266                         err = 2;
267         } else
268                 err = 3;
269
270         if (err) {
271                 dev_warn(&client->dev, "VID0/1 gpio invalid: "
272                                 "DVS not available.\n");
273                 max8952->vid0 = 0;
274                 max8952->vid1 = 0;
275                 /* Mark invalid */
276                 pdata->gpio_vid0 = -1;
277                 pdata->gpio_vid1 = -1;
278
279                 /* Disable Pulldown of EN only */
280                 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
281
282                 dev_err(&client->dev, "DVS modes disabled because VID0 and VID1"
283                                 " do not have proper controls.\n");
284         } else {
285                 /*
286                  * Disable Pulldown on EN, VID0, VID1 to reduce
287                  * leakage current of MAX8952 assuming that MAX8952
288                  * is turned on (EN==1). Note that without having VID0/1
289                  * properly connected, turning pulldown off can be
290                  * problematic. Thus, turn this off only when they are
291                  * controllable by GPIO.
292                  */
293                 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
294         }
295
296         max8952_write_reg(max8952, MAX8952_REG_MODE0,
297                         (max8952_read_reg(max8952,
298                                           MAX8952_REG_MODE0) & 0xC0) |
299                         (pdata->dvs_mode[0] & 0x3F));
300         max8952_write_reg(max8952, MAX8952_REG_MODE1,
301                         (max8952_read_reg(max8952,
302                                           MAX8952_REG_MODE1) & 0xC0) |
303                         (pdata->dvs_mode[1] & 0x3F));
304         max8952_write_reg(max8952, MAX8952_REG_MODE2,
305                         (max8952_read_reg(max8952,
306                                           MAX8952_REG_MODE2) & 0xC0) |
307                         (pdata->dvs_mode[2] & 0x3F));
308         max8952_write_reg(max8952, MAX8952_REG_MODE3,
309                         (max8952_read_reg(max8952,
310                                           MAX8952_REG_MODE3) & 0xC0) |
311                         (pdata->dvs_mode[3] & 0x3F));
312
313         max8952_write_reg(max8952, MAX8952_REG_SYNC,
314                         (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
315                         ((pdata->sync_freq & 0x3) << 6));
316         max8952_write_reg(max8952, MAX8952_REG_RAMP,
317                         (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
318                         ((pdata->ramp_speed & 0x7) << 5));
319
320         i2c_set_clientdata(client, max8952);
321
322         return 0;
323 }
324
325 static const struct i2c_device_id max8952_ids[] = {
326         { "max8952", 0 },
327         { },
328 };
329 MODULE_DEVICE_TABLE(i2c, max8952_ids);
330
331 static struct i2c_driver max8952_pmic_driver = {
332         .probe          = max8952_pmic_probe,
333         .driver         = {
334                 .name   = "max8952",
335                 .of_match_table = of_match_ptr(max8952_dt_match),
336         },
337         .id_table       = max8952_ids,
338 };
339
340 static int __init max8952_pmic_init(void)
341 {
342         return i2c_add_driver(&max8952_pmic_driver);
343 }
344 subsys_initcall(max8952_pmic_init);
345
346 static void __exit max8952_pmic_exit(void)
347 {
348         i2c_del_driver(&max8952_pmic_driver);
349 }
350 module_exit(max8952_pmic_exit);
351
352 MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
353 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
354 MODULE_LICENSE("GPL");