blob: ccd5da63cdf266a4c1046e78f4e4e4b7728cefc8 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
MyungJoo Ham202f4f52010-08-20 14:43:56 +09002/*
3 * max8952.c - Voltage and current regulation for the Maxim 8952
4 *
5 * Copyright (C) 2010 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
MyungJoo Ham202f4f52010-08-20 14:43:56 +09007 */
8
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/i2c.h>
12#include <linux/err.h>
13#include <linux/platform_device.h>
14#include <linux/regulator/driver.h>
15#include <linux/regulator/max8952.h>
Linus Walleijd7a261c2018-05-14 10:06:29 +020016#include <linux/gpio/consumer.h>
MyungJoo Ham202f4f52010-08-20 14:43:56 +090017#include <linux/io.h>
Tomasz Figa3ff4aa92013-04-04 18:17:20 +020018#include <linux/of.h>
Tomasz Figa3ff4aa92013-04-04 18:17:20 +020019#include <linux/regulator/of_regulator.h>
MyungJoo Ham202f4f52010-08-20 14:43:56 +090020#include <linux/slab.h>
21
22/* Registers */
23enum {
24 MAX8952_REG_MODE0,
25 MAX8952_REG_MODE1,
26 MAX8952_REG_MODE2,
27 MAX8952_REG_MODE3,
28 MAX8952_REG_CONTROL,
29 MAX8952_REG_SYNC,
30 MAX8952_REG_RAMP,
31 MAX8952_REG_CHIP_ID1,
32 MAX8952_REG_CHIP_ID2,
33};
34
35struct max8952_data {
36 struct i2c_client *client;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090037 struct max8952_platform_data *pdata;
Linus Walleijfd742ea2019-06-09 13:48:12 +020038 struct gpio_desc *vid0_gpiod;
39 struct gpio_desc *vid1_gpiod;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090040 bool vid0;
41 bool vid1;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090042};
43
44static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
45{
46 int ret = i2c_smbus_read_byte_data(max8952->client, reg);
Sachin Kamata5f8f962014-03-18 15:53:04 +053047
MyungJoo Ham202f4f52010-08-20 14:43:56 +090048 if (ret > 0)
49 ret &= 0xff;
50
51 return ret;
52}
53
54static int max8952_write_reg(struct max8952_data *max8952,
55 u8 reg, u8 value)
56{
57 return i2c_smbus_write_byte_data(max8952->client, reg, value);
58}
59
MyungJoo Ham202f4f52010-08-20 14:43:56 +090060static int max8952_list_voltage(struct regulator_dev *rdev,
61 unsigned int selector)
62{
63 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
64
65 if (rdev_get_id(rdev) != 0)
66 return -EINVAL;
67
Axel Linb9b49af2012-04-20 18:36:14 +080068 return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090069}
70
Axel Linb9b49af2012-04-20 18:36:14 +080071static int max8952_get_voltage_sel(struct regulator_dev *rdev)
MyungJoo Ham202f4f52010-08-20 14:43:56 +090072{
73 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
74 u8 vid = 0;
75
76 if (max8952->vid0)
77 vid += 1;
78 if (max8952->vid1)
79 vid += 2;
80
Axel Linb9b49af2012-04-20 18:36:14 +080081 return vid;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090082}
83
Axel Lin6ea67d02012-03-26 09:20:58 +080084static int max8952_set_voltage_sel(struct regulator_dev *rdev,
85 unsigned selector)
MyungJoo Ham202f4f52010-08-20 14:43:56 +090086{
87 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
MyungJoo Ham202f4f52010-08-20 14:43:56 +090088
Linus Walleijfd742ea2019-06-09 13:48:12 +020089 if (!max8952->vid0_gpiod || !max8952->vid1_gpiod) {
MyungJoo Ham202f4f52010-08-20 14:43:56 +090090 /* DVS not supported */
91 return -EPERM;
92 }
93
Axel Lin6ea67d02012-03-26 09:20:58 +080094 max8952->vid0 = selector & 0x1;
95 max8952->vid1 = (selector >> 1) & 0x1;
Linus Walleijfd742ea2019-06-09 13:48:12 +020096 gpiod_set_value(max8952->vid0_gpiod, max8952->vid0);
97 gpiod_set_value(max8952->vid1_gpiod, max8952->vid1);
MyungJoo Ham202f4f52010-08-20 14:43:56 +090098
99 return 0;
100}
101
Bhumika Goyal6e09f4a2017-01-28 20:21:04 +0530102static const struct regulator_ops max8952_ops = {
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900103 .list_voltage = max8952_list_voltage,
Axel Linb9b49af2012-04-20 18:36:14 +0800104 .get_voltage_sel = max8952_get_voltage_sel,
Axel Lin6ea67d02012-03-26 09:20:58 +0800105 .set_voltage_sel = max8952_set_voltage_sel,
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900106};
107
Axel Linbd6ff0d2012-04-06 08:29:49 +0800108static const struct regulator_desc regulator = {
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900109 .name = "MAX8952_VOUT",
110 .id = 0,
111 .n_voltages = MAX8952_NUM_DVS_MODE,
112 .ops = &max8952_ops,
113 .type = REGULATOR_VOLTAGE,
114 .owner = THIS_MODULE,
115};
116
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200117#ifdef CONFIG_OF
Jingoo Hancfe6e332014-05-07 17:06:34 +0900118static const struct of_device_id max8952_dt_match[] = {
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200119 { .compatible = "maxim,max8952" },
120 {},
121};
122MODULE_DEVICE_TABLE(of, max8952_dt_match);
123
124static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
125{
126 struct max8952_platform_data *pd;
127 struct device_node *np = dev->of_node;
128 int ret;
129 int i;
130
131 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
Sachin Kamat7752d962014-02-20 14:23:08 +0530132 if (!pd)
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200133 return NULL;
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200134
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200135 if (of_property_read_u32(np, "max8952,default-mode", &pd->default_mode))
136 dev_warn(dev, "Default mode not specified, assuming 0\n");
137
138 ret = of_property_read_u32_array(np, "max8952,dvs-mode-microvolt",
139 pd->dvs_mode, ARRAY_SIZE(pd->dvs_mode));
140 if (ret) {
141 dev_err(dev, "max8952,dvs-mode-microvolt property not specified");
142 return NULL;
143 }
144
145 for (i = 0; i < ARRAY_SIZE(pd->dvs_mode); ++i) {
146 if (pd->dvs_mode[i] < 770000 || pd->dvs_mode[i] > 1400000) {
147 dev_err(dev, "DVS voltage %d out of range\n", i);
148 return NULL;
149 }
150 pd->dvs_mode[i] = (pd->dvs_mode[i] - 770000) / 10000;
151 }
152
153 if (of_property_read_u32(np, "max8952,sync-freq", &pd->sync_freq))
154 dev_warn(dev, "max8952,sync-freq property not specified, defaulting to 26MHz\n");
155
156 if (of_property_read_u32(np, "max8952,ramp-speed", &pd->ramp_speed))
157 dev_warn(dev, "max8952,ramp-speed property not specified, defaulting to 32mV/us\n");
158
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100159 pd->reg_data = of_get_regulator_init_data(dev, np, &regulator);
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200160 if (!pd->reg_data) {
161 dev_err(dev, "Failed to parse regulator init data\n");
162 return NULL;
163 }
164
165 return pd;
166}
167#else
168static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
169{
170 return NULL;
171}
172#endif
173
Bill Pembertona5023572012-11-19 13:22:22 -0500174static int max8952_pmic_probe(struct i2c_client *client,
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900175 const struct i2c_device_id *i2c_id)
176{
Wolfram Sang6b960922019-06-08 12:56:04 +0200177 struct i2c_adapter *adapter = client->adapter;
Jingoo Handff91d02013-07-30 17:20:47 +0900178 struct max8952_platform_data *pdata = dev_get_platdata(&client->dev);
Mark Brownc172708d2012-04-04 00:50:22 +0100179 struct regulator_config config = { };
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900180 struct max8952_data *max8952;
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100181 struct regulator_dev *rdev;
Linus Walleijd7a261c2018-05-14 10:06:29 +0200182 struct gpio_desc *gpiod;
183 enum gpiod_flags gflags;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900184
Linus Walleijfd742ea2019-06-09 13:48:12 +0200185 int ret = 0;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900186
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200187 if (client->dev.of_node)
188 pdata = max8952_parse_dt(&client->dev);
189
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900190 if (!pdata) {
191 dev_err(&client->dev, "Require the platform data\n");
192 return -EINVAL;
193 }
194
195 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
196 return -EIO;
197
Axel Lin372de4a2012-05-16 12:50:47 +0800198 max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data),
199 GFP_KERNEL);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900200 if (!max8952)
201 return -ENOMEM;
202
203 max8952->client = client;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900204 max8952->pdata = pdata;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900205
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100206 config.dev = &client->dev;
Tomasz Figa3ec6eb92013-04-04 18:17:19 +0200207 config.init_data = pdata->reg_data;
Mark Brownc172708d2012-04-04 00:50:22 +0100208 config.driver_data = max8952;
Axel Lin71622e12013-04-25 22:15:21 +0800209 config.of_node = client->dev.of_node;
Mark Brownc172708d2012-04-04 00:50:22 +0100210
Tomasz Figa3ec6eb92013-04-04 18:17:19 +0200211 if (pdata->reg_data->constraints.boot_on)
Linus Walleijd7a261c2018-05-14 10:06:29 +0200212 gflags = GPIOD_OUT_HIGH;
213 else
214 gflags = GPIOD_OUT_LOW;
Linus Walleij63239e42018-10-15 11:02:40 +0200215 gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
Linus Walleij894077d2018-12-06 13:43:41 +0100216 /*
217 * Do not use devm* here: the regulator core takes over the
218 * lifecycle management of the GPIO descriptor.
219 */
220 gpiod = gpiod_get_optional(&client->dev,
221 "max8952,en",
222 gflags);
Linus Walleijd7a261c2018-05-14 10:06:29 +0200223 if (IS_ERR(gpiod))
224 return PTR_ERR(gpiod);
225 if (gpiod)
226 config.ena_gpiod = gpiod;
Axel Linb669e0a2012-07-10 06:10:12 +0800227
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100228 rdev = devm_regulator_register(&client->dev, &regulator, &config);
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100229 if (IS_ERR(rdev)) {
230 ret = PTR_ERR(rdev);
231 dev_err(&client->dev, "regulator init failed (%d)\n", ret);
Axel Lin372de4a2012-05-16 12:50:47 +0800232 return ret;
Axel Linda057382010-10-25 10:11:07 +0800233 }
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900234
Axel Linc8237f02012-03-15 08:47:48 +0800235 max8952->vid0 = pdata->default_mode & 0x1;
236 max8952->vid1 = (pdata->default_mode >> 1) & 0x1;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900237
Linus Walleijfd742ea2019-06-09 13:48:12 +0200238 /* Fetch vid0 and vid1 GPIOs if available */
239 gflags = max8952->vid0 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
240 max8952->vid0_gpiod = devm_gpiod_get_index_optional(&client->dev,
241 "max8952,vid",
242 0, gflags);
243 if (IS_ERR(max8952->vid0_gpiod))
244 return PTR_ERR(max8952->vid0_gpiod);
245 gflags = max8952->vid1 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
246 max8952->vid1_gpiod = devm_gpiod_get_index_optional(&client->dev,
247 "max8952,vid",
248 1, gflags);
249 if (IS_ERR(max8952->vid1_gpiod))
250 return PTR_ERR(max8952->vid1_gpiod);
Axel Lind7da1522014-06-16 10:12:25 +0800251
Linus Walleijfd742ea2019-06-09 13:48:12 +0200252 /* If either VID GPIO is missing just disable this */
253 if (!max8952->vid0_gpiod || !max8952->vid1_gpiod) {
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100254 dev_warn(&client->dev, "VID0/1 gpio invalid: "
Linus Walleijfd742ea2019-06-09 13:48:12 +0200255 "DVS not available.\n");
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900256 max8952->vid0 = 0;
257 max8952->vid1 = 0;
Linus Walleijfd742ea2019-06-09 13:48:12 +0200258 /* Make sure if we have any descriptors they get set to low */
259 if (max8952->vid0_gpiod)
260 gpiod_set_value(max8952->vid0_gpiod, 0);
261 if (max8952->vid1_gpiod)
262 gpiod_set_value(max8952->vid1_gpiod, 0);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900263
264 /* Disable Pulldown of EN only */
265 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
266
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100267 dev_err(&client->dev, "DVS modes disabled because VID0 and VID1"
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900268 " do not have proper controls.\n");
269 } else {
270 /*
271 * Disable Pulldown on EN, VID0, VID1 to reduce
272 * leakage current of MAX8952 assuming that MAX8952
273 * is turned on (EN==1). Note that without having VID0/1
274 * properly connected, turning pulldown off can be
275 * problematic. Thus, turn this off only when they are
276 * controllable by GPIO.
277 */
278 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
279 }
280
281 max8952_write_reg(max8952, MAX8952_REG_MODE0,
282 (max8952_read_reg(max8952,
283 MAX8952_REG_MODE0) & 0xC0) |
284 (pdata->dvs_mode[0] & 0x3F));
285 max8952_write_reg(max8952, MAX8952_REG_MODE1,
286 (max8952_read_reg(max8952,
287 MAX8952_REG_MODE1) & 0xC0) |
288 (pdata->dvs_mode[1] & 0x3F));
289 max8952_write_reg(max8952, MAX8952_REG_MODE2,
290 (max8952_read_reg(max8952,
291 MAX8952_REG_MODE2) & 0xC0) |
292 (pdata->dvs_mode[2] & 0x3F));
293 max8952_write_reg(max8952, MAX8952_REG_MODE3,
294 (max8952_read_reg(max8952,
295 MAX8952_REG_MODE3) & 0xC0) |
296 (pdata->dvs_mode[3] & 0x3F));
297
298 max8952_write_reg(max8952, MAX8952_REG_SYNC,
299 (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
300 ((pdata->sync_freq & 0x3) << 6));
301 max8952_write_reg(max8952, MAX8952_REG_RAMP,
302 (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
303 ((pdata->ramp_speed & 0x7) << 5));
304
305 i2c_set_clientdata(client, max8952);
306
Axel Linda057382010-10-25 10:11:07 +0800307 return 0;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900308}
309
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900310static const struct i2c_device_id max8952_ids[] = {
311 { "max8952", 0 },
312 { },
313};
314MODULE_DEVICE_TABLE(i2c, max8952_ids);
315
316static struct i2c_driver max8952_pmic_driver = {
317 .probe = max8952_pmic_probe,
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900318 .driver = {
319 .name = "max8952",
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200320 .of_match_table = of_match_ptr(max8952_dt_match),
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900321 },
322 .id_table = max8952_ids,
323};
324
325static int __init max8952_pmic_init(void)
326{
327 return i2c_add_driver(&max8952_pmic_driver);
328}
329subsys_initcall(max8952_pmic_init);
330
331static void __exit max8952_pmic_exit(void)
332{
333 i2c_del_driver(&max8952_pmic_driver);
334}
335module_exit(max8952_pmic_exit);
336
337MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
338MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
339MODULE_LICENSE("GPL");