blob: 271bb736f3f55782c50bb3d2b6a73d0ddfe958b8 [file] [log] [blame]
Krzysztof Kozlowski5e9384c2018-08-07 18:18:25 +02001// SPDX-License-Identifier: GPL-2.0+
2//
3// max8998.c - Voltage regulator driver for the Maxim 8998
4//
5// Copyright (C) 2009-2010 Samsung Electronics
6// Kyungmin Park <kyungmin.park@samsung.com>
7// Marek Szyprowski <m.szyprowski@samsung.com>
Kyungmin Park156f2522010-06-16 09:04:16 +02008
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/i2c.h>
12#include <linux/err.h>
13#include <linux/gpio.h>
14#include <linux/slab.h>
15#include <linux/interrupt.h>
16#include <linux/mutex.h>
Tomasz Figaee999fb2013-06-25 16:08:10 +020017#include <linux/of.h>
18#include <linux/of_gpio.h>
Kyungmin Park156f2522010-06-16 09:04:16 +020019#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
Tomasz Figaee999fb2013-06-25 16:08:10 +020021#include <linux/regulator/of_regulator.h>
Kyungmin Park156f2522010-06-16 09:04:16 +020022#include <linux/mfd/max8998.h>
23#include <linux/mfd/max8998-private.h>
24
25struct max8998_data {
26 struct device *dev;
27 struct max8998_dev *iodev;
28 int num_regulators;
Lukasz Majewski889cd5a2010-09-27 14:32:25 +020029 u8 buck1_vol[4]; /* voltages for selection */
30 u8 buck2_vol[2];
31 unsigned int buck1_idx; /* index to last changed voltage */
32 /* value in a set */
33 unsigned int buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +020034};
35
36struct voltage_map_desc {
37 int min;
38 int max;
39 int step;
40};
41
Axel Linadf61782012-12-28 17:10:20 +080042/* Voltage maps in uV*/
Kyungmin Park156f2522010-06-16 09:04:16 +020043static const struct voltage_map_desc ldo23_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080044 .min = 800000, .step = 50000, .max = 1300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020045};
46static const struct voltage_map_desc ldo456711_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080047 .min = 1600000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020048};
49static const struct voltage_map_desc ldo8_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080050 .min = 3000000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020051};
52static const struct voltage_map_desc ldo9_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080053 .min = 2800000, .step = 100000, .max = 3100000,
Kyungmin Park156f2522010-06-16 09:04:16 +020054};
55static const struct voltage_map_desc ldo10_voltage_map_desc = {
Marek Szyprowski36a6f8c2013-01-25 09:25:40 +010056 .min = 950000, .step = 50000, .max = 1300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020057};
58static const struct voltage_map_desc ldo1213_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080059 .min = 800000, .step = 100000, .max = 3300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020060};
61static const struct voltage_map_desc ldo1415_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080062 .min = 1200000, .step = 100000, .max = 3300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020063};
64static const struct voltage_map_desc ldo1617_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080065 .min = 1600000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020066};
67static const struct voltage_map_desc buck12_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080068 .min = 750000, .step = 25000, .max = 1525000,
Kyungmin Park156f2522010-06-16 09:04:16 +020069};
70static const struct voltage_map_desc buck3_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080071 .min = 1600000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020072};
73static const struct voltage_map_desc buck4_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080074 .min = 800000, .step = 100000, .max = 2300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020075};
76
77static const struct voltage_map_desc *ldo_voltage_map[] = {
78 NULL,
79 NULL,
80 &ldo23_voltage_map_desc, /* LDO2 */
81 &ldo23_voltage_map_desc, /* LDO3 */
82 &ldo456711_voltage_map_desc, /* LDO4 */
83 &ldo456711_voltage_map_desc, /* LDO5 */
84 &ldo456711_voltage_map_desc, /* LDO6 */
85 &ldo456711_voltage_map_desc, /* LDO7 */
86 &ldo8_voltage_map_desc, /* LDO8 */
87 &ldo9_voltage_map_desc, /* LDO9 */
88 &ldo10_voltage_map_desc, /* LDO10 */
89 &ldo456711_voltage_map_desc, /* LDO11 */
90 &ldo1213_voltage_map_desc, /* LDO12 */
91 &ldo1213_voltage_map_desc, /* LDO13 */
92 &ldo1415_voltage_map_desc, /* LDO14 */
93 &ldo1415_voltage_map_desc, /* LDO15 */
94 &ldo1617_voltage_map_desc, /* LDO16 */
95 &ldo1617_voltage_map_desc, /* LDO17 */
96 &buck12_voltage_map_desc, /* BUCK1 */
97 &buck12_voltage_map_desc, /* BUCK2 */
98 &buck3_voltage_map_desc, /* BUCK3 */
99 &buck4_voltage_map_desc, /* BUCK4 */
100};
101
Kyungmin Park156f2522010-06-16 09:04:16 +0200102static int max8998_get_enable_register(struct regulator_dev *rdev,
103 int *reg, int *shift)
104{
Axel Lin7b947912012-03-01 09:27:29 +0800105 int ldo = rdev_get_id(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200106
107 switch (ldo) {
108 case MAX8998_LDO2 ... MAX8998_LDO5:
109 *reg = MAX8998_REG_ONOFF1;
110 *shift = 3 - (ldo - MAX8998_LDO2);
111 break;
112 case MAX8998_LDO6 ... MAX8998_LDO13:
113 *reg = MAX8998_REG_ONOFF2;
114 *shift = 7 - (ldo - MAX8998_LDO6);
115 break;
116 case MAX8998_LDO14 ... MAX8998_LDO17:
117 *reg = MAX8998_REG_ONOFF3;
118 *shift = 7 - (ldo - MAX8998_LDO14);
119 break;
120 case MAX8998_BUCK1 ... MAX8998_BUCK4:
121 *reg = MAX8998_REG_ONOFF1;
122 *shift = 7 - (ldo - MAX8998_BUCK1);
123 break;
124 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
125 *reg = MAX8998_REG_ONOFF4;
126 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
127 break;
128 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
129 *reg = MAX8998_REG_CHGR2;
130 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
131 break;
132 default:
133 return -EINVAL;
134 }
135
136 return 0;
137}
138
139static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
140{
141 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900142 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200143 int ret, reg, shift = 8;
144 u8 val;
145
146 ret = max8998_get_enable_register(rdev, &reg, &shift);
147 if (ret)
148 return ret;
149
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900150 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200151 if (ret)
152 return ret;
153
154 return val & (1 << shift);
155}
156
157static int max8998_ldo_enable(struct regulator_dev *rdev)
158{
159 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900160 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200161 int reg, shift = 8, ret;
162
163 ret = max8998_get_enable_register(rdev, &reg, &shift);
164 if (ret)
165 return ret;
166
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900167 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200168}
169
170static int max8998_ldo_disable(struct regulator_dev *rdev)
171{
172 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900173 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200174 int reg, shift = 8, ret;
175
176 ret = max8998_get_enable_register(rdev, &reg, &shift);
177 if (ret)
178 return ret;
179
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900180 return max8998_update_reg(i2c, reg, 0, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200181}
182
183static int max8998_get_voltage_register(struct regulator_dev *rdev,
184 int *_reg, int *_shift, int *_mask)
185{
Axel Lin7b947912012-03-01 09:27:29 +0800186 int ldo = rdev_get_id(rdev);
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200187 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200188 int reg, shift = 0, mask = 0xff;
189
190 switch (ldo) {
191 case MAX8998_LDO2 ... MAX8998_LDO3:
192 reg = MAX8998_REG_LDO2_LDO3;
193 mask = 0xf;
194 if (ldo == MAX8998_LDO2)
195 shift = 4;
196 else
197 shift = 0;
198 break;
199 case MAX8998_LDO4 ... MAX8998_LDO7:
200 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
201 break;
202 case MAX8998_LDO8 ... MAX8998_LDO9:
203 reg = MAX8998_REG_LDO8_LDO9;
204 mask = 0xf;
205 if (ldo == MAX8998_LDO8)
206 shift = 4;
207 else
208 shift = 0;
209 break;
210 case MAX8998_LDO10 ... MAX8998_LDO11:
211 reg = MAX8998_REG_LDO10_LDO11;
212 if (ldo == MAX8998_LDO10) {
213 shift = 5;
214 mask = 0x7;
215 } else {
216 shift = 0;
217 mask = 0x1f;
218 }
219 break;
220 case MAX8998_LDO12 ... MAX8998_LDO17:
221 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
222 break;
223 case MAX8998_BUCK1:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200224 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200225 break;
226 case MAX8998_BUCK2:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200227 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200228 break;
229 case MAX8998_BUCK3:
230 reg = MAX8998_REG_BUCK3;
231 break;
232 case MAX8998_BUCK4:
233 reg = MAX8998_REG_BUCK4;
234 break;
235 default:
236 return -EINVAL;
237 }
238
239 *_reg = reg;
240 *_shift = shift;
241 *_mask = mask;
242
243 return 0;
244}
245
Axel Lin7b4354b2012-04-11 13:58:09 +0800246static int max8998_get_voltage_sel(struct regulator_dev *rdev)
Kyungmin Park156f2522010-06-16 09:04:16 +0200247{
248 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900249 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200250 int reg, shift = 0, mask, ret;
251 u8 val;
252
253 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
254 if (ret)
255 return ret;
256
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900257 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200258 if (ret)
259 return ret;
260
261 val >>= shift;
262 val &= mask;
263
Axel Lin7b4354b2012-04-11 13:58:09 +0800264 return val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200265}
266
Axel Linbaae0192012-06-17 09:34:29 +0800267static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
268 unsigned selector)
Lukasz Majewski9d924922010-09-27 14:32:23 +0200269{
270 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
271 struct i2c_client *i2c = max8998->iodev->i2c;
Axel Linbaae0192012-06-17 09:34:29 +0800272 int reg, shift = 0, mask, ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000273
Lukasz Majewski9d924922010-09-27 14:32:23 +0200274 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
275 if (ret)
276 return ret;
277
Axel Linbaae0192012-06-17 09:34:29 +0800278 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
Lukasz Majewski9d924922010-09-27 14:32:23 +0200279
280 return ret;
281}
282
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200283static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
284{
285 gpio_set_value(gpio1, v & 0x1);
286 gpio_set_value(gpio2, (v >> 1) & 0x1);
287}
288
289static inline void buck2_gpio_set(int gpio, int v)
290{
291 gpio_set_value(gpio, v & 0x1);
292}
293
Axel Linbaae0192012-06-17 09:34:29 +0800294static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
295 unsigned selector)
Kyungmin Park156f2522010-06-16 09:04:16 +0200296{
297 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Paweł Chmielc1472732018-04-27 18:02:59 +0200298 struct max8998_platform_data *pdata = max8998->iodev->pdata;
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900299 struct i2c_client *i2c = max8998->iodev->i2c;
Axel Lin7b947912012-03-01 09:27:29 +0800300 int buck = rdev_get_id(rdev);
Axel Linc6163a72013-02-06 11:10:51 +0800301 int reg, shift = 0, mask, ret, j;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200302 static u8 buck1_last_val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200303
Kyungmin Park156f2522010-06-16 09:04:16 +0200304 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
305 if (ret)
306 return ret;
307
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200308 switch (buck) {
309 case MAX8998_BUCK1:
310 dev_dbg(max8998->dev,
Axel Linbaae0192012-06-17 09:34:29 +0800311 "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
Joe Perches85ee7a12011-04-23 20:38:19 -0700312 "buck1_vol3:%d, buck1_vol4:%d\n",
Axel Linbaae0192012-06-17 09:34:29 +0800313 selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200314 max8998->buck1_vol[2], max8998->buck1_vol[3]);
315
316 if (gpio_is_valid(pdata->buck1_set1) &&
317 gpio_is_valid(pdata->buck1_set2)) {
318
319 /* check if requested voltage */
320 /* value is already defined */
321 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
Axel Linbaae0192012-06-17 09:34:29 +0800322 if (max8998->buck1_vol[j] == selector) {
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200323 max8998->buck1_idx = j;
324 buck1_gpio_set(pdata->buck1_set1,
325 pdata->buck1_set2, j);
326 goto buck1_exit;
327 }
328 }
329
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100330 if (pdata->buck_voltage_lock)
331 return -EINVAL;
332
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200333 /* no predefine regulator found */
334 max8998->buck1_idx = (buck1_last_val % 2) + 2;
335 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
336 max8998->buck1_idx);
Axel Linbaae0192012-06-17 09:34:29 +0800337 max8998->buck1_vol[max8998->buck1_idx] = selector;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200338 ret = max8998_get_voltage_register(rdev, &reg,
339 &shift,
340 &mask);
Axel Linbaae0192012-06-17 09:34:29 +0800341 ret = max8998_write_reg(i2c, reg, selector);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200342 buck1_gpio_set(pdata->buck1_set1,
343 pdata->buck1_set2, max8998->buck1_idx);
344 buck1_last_val++;
345buck1_exit:
346 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
347 i2c->name, gpio_get_value(pdata->buck1_set1),
348 gpio_get_value(pdata->buck1_set2));
349 break;
350 } else {
Axel Linbaae0192012-06-17 09:34:29 +0800351 ret = max8998_write_reg(i2c, reg, selector);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200352 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200353 break;
354
355 case MAX8998_BUCK2:
356 dev_dbg(max8998->dev,
Axel Linbaae0192012-06-17 09:34:29 +0800357 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
358 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200359 if (gpio_is_valid(pdata->buck2_set3)) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100360
361 /* check if requested voltage */
362 /* value is already defined */
363 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
Axel Linbaae0192012-06-17 09:34:29 +0800364 if (max8998->buck2_vol[j] == selector) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100365 max8998->buck2_idx = j;
366 buck2_gpio_set(pdata->buck2_set3, j);
367 goto buck2_exit;
368 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200369 }
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100370
371 if (pdata->buck_voltage_lock)
372 return -EINVAL;
373
374 max8998_get_voltage_register(rdev,
375 &reg, &shift, &mask);
Axel Linbaae0192012-06-17 09:34:29 +0800376 ret = max8998_write_reg(i2c, reg, selector);
377 max8998->buck2_vol[max8998->buck2_idx] = selector;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100378 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
379buck2_exit:
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200380 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
381 gpio_get_value(pdata->buck2_set3));
382 } else {
Axel Linbaae0192012-06-17 09:34:29 +0800383 ret = max8998_write_reg(i2c, reg, selector);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200384 }
385 break;
386
387 case MAX8998_BUCK3:
388 case MAX8998_BUCK4:
Axel Linbaae0192012-06-17 09:34:29 +0800389 ret = max8998_update_reg(i2c, reg, selector<<shift,
390 mask<<shift);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200391 break;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200392 }
393
Axel Lin276909d2012-04-11 13:59:14 +0800394 return ret;
395}
396
397static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
398 unsigned int old_selector,
399 unsigned int new_selector)
400{
401 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
402 struct i2c_client *i2c = max8998->iodev->i2c;
403 const struct voltage_map_desc *desc;
404 int buck = rdev_get_id(rdev);
405 u8 val = 0;
406 int difference, ret;
407
408 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
409 return -EINVAL;
410
411 desc = ldo_voltage_map[buck];
412
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200413 /* Voltage stabilization */
Axel Lin276909d2012-04-11 13:59:14 +0800414 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
415 if (ret)
416 return ret;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200417
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200418 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
419 /* MAX8998 has ENRAMP bit implemented, so test it*/
420 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
Axel Lin276909d2012-04-11 13:59:14 +0800421 return 0;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200422
Axel Linadf61782012-12-28 17:10:20 +0800423 difference = (new_selector - old_selector) * desc->step / 1000;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200424 if (difference > 0)
Axel Lin81d0a6a2013-01-09 19:34:57 +0800425 return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200426
Axel Lin276909d2012-04-11 13:59:14 +0800427 return 0;
Kyungmin Park156f2522010-06-16 09:04:16 +0200428}
429
430static struct regulator_ops max8998_ldo_ops = {
Axel Lin9a0fbb62012-06-17 09:33:07 +0800431 .list_voltage = regulator_list_voltage_linear,
Axel Linbaae0192012-06-17 09:34:29 +0800432 .map_voltage = regulator_map_voltage_linear,
Kyungmin Park156f2522010-06-16 09:04:16 +0200433 .is_enabled = max8998_ldo_is_enabled,
434 .enable = max8998_ldo_enable,
435 .disable = max8998_ldo_disable,
Axel Lin7b4354b2012-04-11 13:58:09 +0800436 .get_voltage_sel = max8998_get_voltage_sel,
Axel Linbaae0192012-06-17 09:34:29 +0800437 .set_voltage_sel = max8998_set_voltage_ldo_sel,
Kyungmin Park156f2522010-06-16 09:04:16 +0200438};
439
440static struct regulator_ops max8998_buck_ops = {
Axel Lin9a0fbb62012-06-17 09:33:07 +0800441 .list_voltage = regulator_list_voltage_linear,
Axel Linbaae0192012-06-17 09:34:29 +0800442 .map_voltage = regulator_map_voltage_linear,
Kyungmin Park156f2522010-06-16 09:04:16 +0200443 .is_enabled = max8998_ldo_is_enabled,
444 .enable = max8998_ldo_enable,
445 .disable = max8998_ldo_disable,
Axel Lin7b4354b2012-04-11 13:58:09 +0800446 .get_voltage_sel = max8998_get_voltage_sel,
Axel Linbaae0192012-06-17 09:34:29 +0800447 .set_voltage_sel = max8998_set_voltage_buck_sel,
Axel Lin276909d2012-04-11 13:59:14 +0800448 .set_voltage_time_sel = max8998_set_voltage_buck_time_sel,
Kyungmin Park156f2522010-06-16 09:04:16 +0200449};
450
451static struct regulator_ops max8998_others_ops = {
452 .is_enabled = max8998_ldo_is_enabled,
453 .enable = max8998_ldo_enable,
454 .disable = max8998_ldo_disable,
Kyungmin Park156f2522010-06-16 09:04:16 +0200455};
456
457static struct regulator_desc regulators[] = {
458 {
459 .name = "LDO2",
460 .id = MAX8998_LDO2,
461 .ops = &max8998_ldo_ops,
462 .type = REGULATOR_VOLTAGE,
463 .owner = THIS_MODULE,
464 }, {
465 .name = "LDO3",
466 .id = MAX8998_LDO3,
467 .ops = &max8998_ldo_ops,
468 .type = REGULATOR_VOLTAGE,
469 .owner = THIS_MODULE,
470 }, {
471 .name = "LDO4",
472 .id = MAX8998_LDO4,
473 .ops = &max8998_ldo_ops,
474 .type = REGULATOR_VOLTAGE,
475 .owner = THIS_MODULE,
476 }, {
477 .name = "LDO5",
478 .id = MAX8998_LDO5,
479 .ops = &max8998_ldo_ops,
480 .type = REGULATOR_VOLTAGE,
481 .owner = THIS_MODULE,
482 }, {
483 .name = "LDO6",
484 .id = MAX8998_LDO6,
485 .ops = &max8998_ldo_ops,
486 .type = REGULATOR_VOLTAGE,
487 .owner = THIS_MODULE,
488 }, {
489 .name = "LDO7",
490 .id = MAX8998_LDO7,
491 .ops = &max8998_ldo_ops,
492 .type = REGULATOR_VOLTAGE,
493 .owner = THIS_MODULE,
494 }, {
495 .name = "LDO8",
496 .id = MAX8998_LDO8,
497 .ops = &max8998_ldo_ops,
498 .type = REGULATOR_VOLTAGE,
499 .owner = THIS_MODULE,
500 }, {
501 .name = "LDO9",
502 .id = MAX8998_LDO9,
503 .ops = &max8998_ldo_ops,
504 .type = REGULATOR_VOLTAGE,
505 .owner = THIS_MODULE,
506 }, {
507 .name = "LDO10",
508 .id = MAX8998_LDO10,
509 .ops = &max8998_ldo_ops,
510 .type = REGULATOR_VOLTAGE,
511 .owner = THIS_MODULE,
512 }, {
513 .name = "LDO11",
514 .id = MAX8998_LDO11,
515 .ops = &max8998_ldo_ops,
516 .type = REGULATOR_VOLTAGE,
517 .owner = THIS_MODULE,
518 }, {
519 .name = "LDO12",
520 .id = MAX8998_LDO12,
521 .ops = &max8998_ldo_ops,
522 .type = REGULATOR_VOLTAGE,
523 .owner = THIS_MODULE,
524 }, {
525 .name = "LDO13",
526 .id = MAX8998_LDO13,
527 .ops = &max8998_ldo_ops,
528 .type = REGULATOR_VOLTAGE,
529 .owner = THIS_MODULE,
530 }, {
531 .name = "LDO14",
532 .id = MAX8998_LDO14,
533 .ops = &max8998_ldo_ops,
534 .type = REGULATOR_VOLTAGE,
535 .owner = THIS_MODULE,
536 }, {
537 .name = "LDO15",
538 .id = MAX8998_LDO15,
539 .ops = &max8998_ldo_ops,
540 .type = REGULATOR_VOLTAGE,
541 .owner = THIS_MODULE,
542 }, {
543 .name = "LDO16",
544 .id = MAX8998_LDO16,
545 .ops = &max8998_ldo_ops,
546 .type = REGULATOR_VOLTAGE,
547 .owner = THIS_MODULE,
548 }, {
549 .name = "LDO17",
550 .id = MAX8998_LDO17,
551 .ops = &max8998_ldo_ops,
552 .type = REGULATOR_VOLTAGE,
553 .owner = THIS_MODULE,
554 }, {
555 .name = "BUCK1",
556 .id = MAX8998_BUCK1,
557 .ops = &max8998_buck_ops,
558 .type = REGULATOR_VOLTAGE,
559 .owner = THIS_MODULE,
560 }, {
561 .name = "BUCK2",
562 .id = MAX8998_BUCK2,
563 .ops = &max8998_buck_ops,
564 .type = REGULATOR_VOLTAGE,
565 .owner = THIS_MODULE,
566 }, {
567 .name = "BUCK3",
568 .id = MAX8998_BUCK3,
569 .ops = &max8998_buck_ops,
570 .type = REGULATOR_VOLTAGE,
571 .owner = THIS_MODULE,
572 }, {
573 .name = "BUCK4",
574 .id = MAX8998_BUCK4,
575 .ops = &max8998_buck_ops,
576 .type = REGULATOR_VOLTAGE,
577 .owner = THIS_MODULE,
578 }, {
Tomasz Figaee999fb2013-06-25 16:08:10 +0200579 .name = "EN32KHz-AP",
Kyungmin Park156f2522010-06-16 09:04:16 +0200580 .id = MAX8998_EN32KHZ_AP,
581 .ops = &max8998_others_ops,
582 .type = REGULATOR_VOLTAGE,
583 .owner = THIS_MODULE,
584 }, {
Tomasz Figaee999fb2013-06-25 16:08:10 +0200585 .name = "EN32KHz-CP",
Kyungmin Park156f2522010-06-16 09:04:16 +0200586 .id = MAX8998_EN32KHZ_CP,
587 .ops = &max8998_others_ops,
588 .type = REGULATOR_VOLTAGE,
589 .owner = THIS_MODULE,
590 }, {
591 .name = "ENVICHG",
592 .id = MAX8998_ENVICHG,
593 .ops = &max8998_others_ops,
594 .type = REGULATOR_VOLTAGE,
595 .owner = THIS_MODULE,
596 }, {
597 .name = "ESAFEOUT1",
598 .id = MAX8998_ESAFEOUT1,
599 .ops = &max8998_others_ops,
600 .type = REGULATOR_VOLTAGE,
601 .owner = THIS_MODULE,
602 }, {
603 .name = "ESAFEOUT2",
604 .id = MAX8998_ESAFEOUT2,
605 .ops = &max8998_others_ops,
606 .type = REGULATOR_VOLTAGE,
607 .owner = THIS_MODULE,
608 }
609};
610
Tomasz Figaee999fb2013-06-25 16:08:10 +0200611static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
612 struct max8998_platform_data *pdata,
613 struct device_node *pmic_np)
614{
615 int gpio;
616
617 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
618 if (!gpio_is_valid(gpio)) {
619 dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
620 return -EINVAL;
621 }
622 pdata->buck1_set1 = gpio;
623
624 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
625 if (!gpio_is_valid(gpio)) {
626 dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
627 return -EINVAL;
628 }
629 pdata->buck1_set2 = gpio;
630
631 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
632 if (!gpio_is_valid(gpio)) {
633 dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
634 return -EINVAL;
635 }
636 pdata->buck2_set3 = gpio;
637
638 return 0;
639}
640
641static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
642 struct max8998_platform_data *pdata)
643{
644 struct device_node *pmic_np = iodev->dev->of_node;
645 struct device_node *regulators_np, *reg_np;
646 struct max8998_regulator_data *rdata;
647 unsigned int i;
648 int ret;
649
650 regulators_np = of_get_child_by_name(pmic_np, "regulators");
651 if (!regulators_np) {
652 dev_err(iodev->dev, "could not find regulators sub-node\n");
653 return -EINVAL;
654 }
655
656 /* count the number of regulators to be supported in pmic */
657 pdata->num_regulators = of_get_child_count(regulators_np);
658
Kees Cooka86854d2018-06-12 14:07:58 -0700659 rdata = devm_kcalloc(iodev->dev,
660 pdata->num_regulators, sizeof(*rdata),
661 GFP_KERNEL);
Sachin Kamat58c95372014-02-17 14:33:35 +0530662 if (!rdata) {
663 of_node_put(regulators_np);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200664 return -ENOMEM;
Sachin Kamat58c95372014-02-17 14:33:35 +0530665 }
Tomasz Figaee999fb2013-06-25 16:08:10 +0200666
667 pdata->regulators = rdata;
668 for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
669 reg_np = of_get_child_by_name(regulators_np,
670 regulators[i].name);
671 if (!reg_np)
672 continue;
673
674 rdata->id = regulators[i].id;
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100675 rdata->initdata = of_get_regulator_init_data(iodev->dev,
676 reg_np,
677 &regulators[i]);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200678 rdata->reg_node = reg_np;
679 ++rdata;
680 }
681 pdata->num_regulators = rdata - pdata->regulators;
682
Sachin Kamat58c95372014-02-17 14:33:35 +0530683 of_node_put(reg_np);
684 of_node_put(regulators_np);
685
Tomasz Figaee999fb2013-06-25 16:08:10 +0200686 ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
687 if (ret)
688 return -EINVAL;
689
690 if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
691 pdata->buck_voltage_lock = true;
692
693 ret = of_property_read_u32(pmic_np,
694 "max8998,pmic-buck1-default-dvs-idx",
695 &pdata->buck1_default_idx);
696 if (!ret && pdata->buck1_default_idx >= 4) {
697 pdata->buck1_default_idx = 0;
698 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
699 }
700
701 ret = of_property_read_u32(pmic_np,
702 "max8998,pmic-buck2-default-dvs-idx",
703 &pdata->buck2_default_idx);
704 if (!ret && pdata->buck2_default_idx >= 2) {
705 pdata->buck2_default_idx = 0;
706 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
707 }
708
709 ret = of_property_read_u32_array(pmic_np,
710 "max8998,pmic-buck1-dvs-voltage",
711 pdata->buck1_voltage,
712 ARRAY_SIZE(pdata->buck1_voltage));
713 if (ret) {
714 dev_err(iodev->dev, "buck1 voltages not specified\n");
715 return -EINVAL;
716 }
717
718 ret = of_property_read_u32_array(pmic_np,
719 "max8998,pmic-buck2-dvs-voltage",
720 pdata->buck2_voltage,
721 ARRAY_SIZE(pdata->buck2_voltage));
722 if (ret) {
723 dev_err(iodev->dev, "buck2 voltages not specified\n");
724 return -EINVAL;
725 }
726
727 return 0;
728}
729
Bill Pembertona5023572012-11-19 13:22:22 -0500730static int max8998_pmic_probe(struct platform_device *pdev)
Kyungmin Park156f2522010-06-16 09:04:16 +0200731{
732 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200733 struct max8998_platform_data *pdata = iodev->pdata;
Mark Brownc1727082012-04-04 00:50:22 +0100734 struct regulator_config config = { };
Axel Lin8a221df2014-03-09 15:32:19 +0800735 struct regulator_dev *rdev;
Kyungmin Park156f2522010-06-16 09:04:16 +0200736 struct max8998_data *max8998;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200737 struct i2c_client *i2c;
Axel Lin8a221df2014-03-09 15:32:19 +0800738 int i, ret;
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200739 unsigned int v;
Kyungmin Park156f2522010-06-16 09:04:16 +0200740
741 if (!pdata) {
742 dev_err(pdev->dev.parent, "No platform init data supplied\n");
743 return -ENODEV;
744 }
745
Tomasz Figaee999fb2013-06-25 16:08:10 +0200746 if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
747 ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
748 if (ret)
749 return ret;
750 }
751
Axel Lin0f80ea12012-04-19 09:47:37 +0800752 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
753 GFP_KERNEL);
Kyungmin Park156f2522010-06-16 09:04:16 +0200754 if (!max8998)
755 return -ENOMEM;
756
Axel Lin747cc852010-08-27 16:37:34 +0800757 max8998->dev = &pdev->dev;
Kyungmin Park156f2522010-06-16 09:04:16 +0200758 max8998->iodev = iodev;
Axel Linc356cbc2010-08-22 15:26:49 +0800759 max8998->num_regulators = pdata->num_regulators;
Kyungmin Park156f2522010-06-16 09:04:16 +0200760 platform_set_drvdata(pdev, max8998);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200761 i2c = max8998->iodev->i2c;
762
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100763 max8998->buck1_idx = pdata->buck1_default_idx;
764 max8998->buck2_idx = pdata->buck2_default_idx;
765
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200766 /* NOTE: */
767 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
768 /* will be displayed */
769
770 /* Check if MAX8998 voltage selection GPIOs are defined */
771 if (gpio_is_valid(pdata->buck1_set1) &&
772 gpio_is_valid(pdata->buck1_set2)) {
773 /* Check if SET1 is not equal to 0 */
774 if (!pdata->buck1_set1) {
Thiago Farina9df19a52013-02-23 00:52:35 -0300775 dev_err(&pdev->dev,
776 "MAX8998 SET1 GPIO defined as 0 !\n");
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200777 WARN_ON(!pdata->buck1_set1);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530778 return -EIO;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200779 }
780 /* Check if SET2 is not equal to 0 */
781 if (!pdata->buck1_set2) {
Thiago Farina9df19a52013-02-23 00:52:35 -0300782 dev_err(&pdev->dev,
783 "MAX8998 SET2 GPIO defined as 0 !\n");
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200784 WARN_ON(!pdata->buck1_set2);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530785 return -EIO;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200786 }
787
788 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
789 gpio_direction_output(pdata->buck1_set1,
790 max8998->buck1_idx & 0x1);
791
792
793 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
794 gpio_direction_output(pdata->buck1_set2,
795 (max8998->buck1_idx >> 1) & 0x1);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200796
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200797 /* Set predefined values for BUCK1 registers */
798 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
799 i = 0;
800 while (buck12_voltage_map_desc.min +
801 buck12_voltage_map_desc.step*i
802 < pdata->buck1_voltage[v])
803 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200804
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200805 max8998->buck1_vol[v] = i;
806 ret = max8998_write_reg(i2c,
807 MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
808 if (ret)
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530809 return ret;
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200810 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200811 }
812
813 if (gpio_is_valid(pdata->buck2_set3)) {
814 /* Check if SET3 is not equal to 0 */
815 if (!pdata->buck2_set3) {
Thiago Farina9df19a52013-02-23 00:52:35 -0300816 dev_err(&pdev->dev,
817 "MAX8998 SET3 GPIO defined as 0 !\n");
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200818 WARN_ON(!pdata->buck2_set3);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530819 return -EIO;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200820 }
821 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
822 gpio_direction_output(pdata->buck2_set3,
823 max8998->buck2_idx & 0x1);
824
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200825 /* Set predefined values for BUCK2 registers */
826 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
827 i = 0;
828 while (buck12_voltage_map_desc.min +
829 buck12_voltage_map_desc.step*i
830 < pdata->buck2_voltage[v])
831 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200832
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200833 max8998->buck2_vol[v] = i;
834 ret = max8998_write_reg(i2c,
835 MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
836 if (ret)
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530837 return ret;
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200838 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200839 }
Kyungmin Park156f2522010-06-16 09:04:16 +0200840
841 for (i = 0; i < pdata->num_regulators; i++) {
842 const struct voltage_map_desc *desc;
843 int id = pdata->regulators[i].id;
844 int index = id - MAX8998_LDO2;
845
846 desc = ldo_voltage_map[id];
847 if (desc && regulators[index].ops != &max8998_others_ops) {
848 int count = (desc->max - desc->min) / desc->step + 1;
Axel Lin9a0fbb62012-06-17 09:33:07 +0800849
Kyungmin Park156f2522010-06-16 09:04:16 +0200850 regulators[index].n_voltages = count;
Axel Linadf61782012-12-28 17:10:20 +0800851 regulators[index].min_uV = desc->min;
852 regulators[index].uV_step = desc->step;
Kyungmin Park156f2522010-06-16 09:04:16 +0200853 }
Mark Brownc1727082012-04-04 00:50:22 +0100854
855 config.dev = max8998->dev;
Tomasz Figaee999fb2013-06-25 16:08:10 +0200856 config.of_node = pdata->regulators[i].reg_node;
Mark Brownc1727082012-04-04 00:50:22 +0100857 config.init_data = pdata->regulators[i].initdata;
858 config.driver_data = max8998;
859
Axel Lin8a221df2014-03-09 15:32:19 +0800860 rdev = devm_regulator_register(&pdev->dev, &regulators[index],
861 &config);
862 if (IS_ERR(rdev)) {
863 ret = PTR_ERR(rdev);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200864 dev_err(max8998->dev, "regulator %s init failed (%d)\n",
865 regulators[index].name, ret);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530866 return ret;
Kyungmin Park156f2522010-06-16 09:04:16 +0200867 }
868 }
869
870
871 return 0;
Kyungmin Park156f2522010-06-16 09:04:16 +0200872}
873
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900874static const struct platform_device_id max8998_pmic_id[] = {
875 { "max8998-pmic", TYPE_MAX8998 },
876 { "lp3974-pmic", TYPE_LP3974 },
877 { }
878};
Axel Lina51b9072011-03-26 23:28:42 +0800879MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900880
Kyungmin Park156f2522010-06-16 09:04:16 +0200881static struct platform_driver max8998_pmic_driver = {
882 .driver = {
883 .name = "max8998-pmic",
Kyungmin Park156f2522010-06-16 09:04:16 +0200884 },
885 .probe = max8998_pmic_probe,
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900886 .id_table = max8998_pmic_id,
Kyungmin Park156f2522010-06-16 09:04:16 +0200887};
888
889static int __init max8998_pmic_init(void)
890{
891 return platform_driver_register(&max8998_pmic_driver);
892}
893subsys_initcall(max8998_pmic_init);
894
895static void __exit max8998_pmic_cleanup(void)
896{
897 platform_driver_unregister(&max8998_pmic_driver);
898}
899module_exit(max8998_pmic_cleanup);
900
901MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
902MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
903MODULE_LICENSE("GPL");