Blame view

drivers/regulator/max8997-regulator.c 31.2 KB
5e9384c70   Krzysztof Kozlowski   regulator: maxim:...
1
2
3
4
5
6
7
8
  // SPDX-License-Identifier: GPL-2.0+
  //
  // max8997.c - Regulator driver for the Maxim 8997/8966
  //
  // Copyright (C) 2011 Samsung Electronics
  // MyungJoo Ham <myungjoo.ham@samsung.com>
  //
  // This driver is based on max8998.c
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
9
10
  
  #include <linux/bug.h>
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
11
12
  #include <linux/err.h>
  #include <linux/gpio.h>
77b71b370   Thomas Abraham   regulator: add de...
13
  #include <linux/of_gpio.h>
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
14
  #include <linux/slab.h>
65602c32e   Paul Gortmaker   regulator: Add mo...
15
  #include <linux/module.h>
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
16
17
18
19
20
  #include <linux/platform_device.h>
  #include <linux/regulator/driver.h>
  #include <linux/regulator/machine.h>
  #include <linux/mfd/max8997.h>
  #include <linux/mfd/max8997-private.h>
77b71b370   Thomas Abraham   regulator: add de...
21
  #include <linux/regulator/of_regulator.h>
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
22
23
24
25
26
  
  struct max8997_data {
  	struct device *dev;
  	struct max8997_dev *iodev;
  	int num_regulators;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
27
  	int ramp_delay; /* in mV/us */
6e0414a5c   MyungJoo Ham   regulator: max899...
28
29
30
  	bool buck1_gpiodvs;
  	bool buck2_gpiodvs;
  	bool buck5_gpiodvs;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
31
32
33
  	u8 buck1_vol[8];
  	u8 buck2_vol[8];
  	u8 buck5_vol[8];
6e0414a5c   MyungJoo Ham   regulator: max899...
34
  	int buck125_gpios[3];
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
35
  	int buck125_gpioindex;
6e0414a5c   MyungJoo Ham   regulator: max899...
36
  	bool ignore_gpiodvs_side_effect;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
37
38
39
  
  	u8 saved_states[MAX8997_REG_MAX];
  };
b79ca051b   Axel Lin   regulator: max899...
40
41
42
43
44
45
  static const unsigned int safeoutvolt[] = {
  	4850000,
  	4900000,
  	4950000,
  	3300000,
  };
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
46
47
  static inline void max8997_set_gpio(struct max8997_data *max8997)
  {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
48
49
50
  	int set3 = (max8997->buck125_gpioindex) & 0x1;
  	int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
  	int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
6e0414a5c   MyungJoo Ham   regulator: max899...
51
52
53
  	gpio_set_value(max8997->buck125_gpios[0], set1);
  	gpio_set_value(max8997->buck125_gpios[1], set2);
  	gpio_set_value(max8997->buck125_gpios[2], set3);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
54
55
56
57
58
59
  }
  
  struct voltage_map_desc {
  	int min;
  	int max;
  	int step;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
60
  };
bc3b7756b   Axel Lin   regulator: max899...
61
  /* Voltage maps in uV */
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
62
  static const struct voltage_map_desc ldo_voltage_map_desc = {
bc3b7756b   Axel Lin   regulator: max899...
63
  	.min = 800000,	.max = 3950000,	.step = 50000,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
64
65
66
  }; /* LDO1 ~ 18, 21 all */
  
  static const struct voltage_map_desc buck1245_voltage_map_desc = {
bc3b7756b   Axel Lin   regulator: max899...
67
  	.min = 650000,	.max = 2225000,	.step = 25000,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
68
69
70
  }; /* Buck1, 2, 4, 5 */
  
  static const struct voltage_map_desc buck37_voltage_map_desc = {
bc3b7756b   Axel Lin   regulator: max899...
71
  	.min = 750000,	.max = 3900000,	.step = 50000,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
72
  }; /* Buck3, 7 */
bc3b7756b   Axel Lin   regulator: max899...
73
  /* current map in uA */
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
74
  static const struct voltage_map_desc charger_current_map_desc = {
bc3b7756b   Axel Lin   regulator: max899...
75
  	.min = 200000,	.max = 950000,	.step = 50000,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
76
77
78
  };
  
  static const struct voltage_map_desc topoff_current_map_desc = {
bc3b7756b   Axel Lin   regulator: max899...
79
  	.min = 50000,	.max = 200000,	.step = 10000,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  };
  
  static const struct voltage_map_desc *reg_voltage_map[] = {
  	[MAX8997_LDO1] = &ldo_voltage_map_desc,
  	[MAX8997_LDO2] = &ldo_voltage_map_desc,
  	[MAX8997_LDO3] = &ldo_voltage_map_desc,
  	[MAX8997_LDO4] = &ldo_voltage_map_desc,
  	[MAX8997_LDO5] = &ldo_voltage_map_desc,
  	[MAX8997_LDO6] = &ldo_voltage_map_desc,
  	[MAX8997_LDO7] = &ldo_voltage_map_desc,
  	[MAX8997_LDO8] = &ldo_voltage_map_desc,
  	[MAX8997_LDO9] = &ldo_voltage_map_desc,
  	[MAX8997_LDO10] = &ldo_voltage_map_desc,
  	[MAX8997_LDO11] = &ldo_voltage_map_desc,
  	[MAX8997_LDO12] = &ldo_voltage_map_desc,
  	[MAX8997_LDO13] = &ldo_voltage_map_desc,
  	[MAX8997_LDO14] = &ldo_voltage_map_desc,
  	[MAX8997_LDO15] = &ldo_voltage_map_desc,
  	[MAX8997_LDO16] = &ldo_voltage_map_desc,
  	[MAX8997_LDO17] = &ldo_voltage_map_desc,
  	[MAX8997_LDO18] = &ldo_voltage_map_desc,
  	[MAX8997_LDO21] = &ldo_voltage_map_desc,
  	[MAX8997_BUCK1] = &buck1245_voltage_map_desc,
  	[MAX8997_BUCK2] = &buck1245_voltage_map_desc,
  	[MAX8997_BUCK3] = &buck37_voltage_map_desc,
  	[MAX8997_BUCK4] = &buck1245_voltage_map_desc,
  	[MAX8997_BUCK5] = &buck1245_voltage_map_desc,
  	[MAX8997_BUCK6] = NULL,
  	[MAX8997_BUCK7] = &buck37_voltage_map_desc,
  	[MAX8997_EN32KHZ_AP] = NULL,
  	[MAX8997_EN32KHZ_CP] = NULL,
  	[MAX8997_ENVICHG] = NULL,
  	[MAX8997_ESAFEOUT1] = NULL,
  	[MAX8997_ESAFEOUT2] = NULL,
  	[MAX8997_CHARGER_CV] = NULL,
  	[MAX8997_CHARGER] = &charger_current_map_desc,
  	[MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
  };
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
118
119
120
  static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
  		unsigned int selector)
  {
b3e1348e2   Axel Lin   regulator: Kill m...
121
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  
  	if (rid != MAX8997_CHARGER_CV)
  		goto err;
  
  	switch (selector) {
  	case 0x00:
  		return 4200000;
  	case 0x01 ... 0x0E:
  		return 4000000 + 20000 * (selector - 0x01);
  	case 0x0F:
  		return 4350000;
  	default:
  		return -EINVAL;
  	}
  err:
  	return -EINVAL;
  }
  
  static int max8997_list_voltage(struct regulator_dev *rdev,
  		unsigned int selector)
  {
  	const struct voltage_map_desc *desc;
b3e1348e2   Axel Lin   regulator: Kill m...
144
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
145
  	int val;
1aa1b9189   Dan Carpenter   regulator: max899...
146
  	if (rid < 0 || rid >= ARRAY_SIZE(reg_voltage_map))
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
147
148
149
150
151
152
153
154
155
  		return -EINVAL;
  
  	desc = reg_voltage_map[rid];
  	if (desc == NULL)
  		return -EINVAL;
  
  	val = desc->min + desc->step * selector;
  	if (val > desc->max)
  		return -EINVAL;
bc3b7756b   Axel Lin   regulator: max899...
156
  	return val;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
157
158
159
160
161
  }
  
  static int max8997_get_enable_register(struct regulator_dev *rdev,
  		int *reg, int *mask, int *pattern)
  {
b3e1348e2   Axel Lin   regulator: Kill m...
162
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  
  	switch (rid) {
  	case MAX8997_LDO1 ... MAX8997_LDO21:
  		*reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
  		*mask = 0xC0;
  		*pattern = 0xC0;
  		break;
  	case MAX8997_BUCK1:
  		*reg = MAX8997_REG_BUCK1CTRL;
  		*mask = 0x01;
  		*pattern = 0x01;
  		break;
  	case MAX8997_BUCK2:
  		*reg = MAX8997_REG_BUCK2CTRL;
  		*mask = 0x01;
  		*pattern = 0x01;
  		break;
  	case MAX8997_BUCK3:
  		*reg = MAX8997_REG_BUCK3CTRL;
  		*mask = 0x01;
  		*pattern = 0x01;
  		break;
  	case MAX8997_BUCK4:
  		*reg = MAX8997_REG_BUCK4CTRL;
  		*mask = 0x01;
  		*pattern = 0x01;
  		break;
  	case MAX8997_BUCK5:
  		*reg = MAX8997_REG_BUCK5CTRL;
  		*mask = 0x01;
  		*pattern = 0x01;
  		break;
  	case MAX8997_BUCK6:
  		*reg = MAX8997_REG_BUCK6CTRL;
  		*mask = 0x01;
  		*pattern = 0x01;
  		break;
  	case MAX8997_BUCK7:
  		*reg = MAX8997_REG_BUCK7CTRL;
  		*mask = 0x01;
  		*pattern = 0x01;
  		break;
  	case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
  		*reg = MAX8997_REG_MAINCON1;
  		*mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
  		*pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
  		break;
  	case MAX8997_ENVICHG:
  		*reg = MAX8997_REG_MBCCTRL1;
  		*mask = 0x80;
  		*pattern = 0x80;
  		break;
  	case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
  		*reg = MAX8997_REG_SAFEOUTCTRL;
  		*mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
  		*pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
  		break;
  	case MAX8997_CHARGER:
  		*reg = MAX8997_REG_MBCCTRL2;
  		*mask = 0x40;
  		*pattern = 0x40;
  		break;
  	default:
  		/* Not controllable or not exists */
  		return -EINVAL;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
228
229
230
231
232
233
234
235
236
237
238
239
240
  	}
  
  	return 0;
  }
  
  static int max8997_reg_is_enabled(struct regulator_dev *rdev)
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8997->iodev->i2c;
  	int ret, reg, mask, pattern;
  	u8 val;
  
  	ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
c245c087c   Axel Lin   regulator: max899...
241
  	if (ret)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  		return ret;
  
  	ret = max8997_read_reg(i2c, reg, &val);
  	if (ret)
  		return ret;
  
  	return (val & mask) == pattern;
  }
  
  static int max8997_reg_enable(struct regulator_dev *rdev)
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8997->iodev->i2c;
  	int ret, reg, mask, pattern;
  
  	ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
  	if (ret)
  		return ret;
  
  	return max8997_update_reg(i2c, reg, pattern, mask);
  }
  
  static int max8997_reg_disable(struct regulator_dev *rdev)
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8997->iodev->i2c;
  	int ret, reg, mask, pattern;
  
  	ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
  	if (ret)
  		return ret;
  
  	return max8997_update_reg(i2c, reg, ~pattern, mask);
  }
  
  static int max8997_get_voltage_register(struct regulator_dev *rdev,
  		int *_reg, int *_shift, int *_mask)
  {
6f43c3809   Axel Lin   regulator: Make m...
280
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
b3e1348e2   Axel Lin   regulator: Kill m...
281
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
282
283
284
285
286
287
288
289
  	int reg, shift = 0, mask = 0x3f;
  
  	switch (rid) {
  	case MAX8997_LDO1 ... MAX8997_LDO21:
  		reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
  		break;
  	case MAX8997_BUCK1:
  		reg = MAX8997_REG_BUCK1DVS1;
6f43c3809   Axel Lin   regulator: Make m...
290
291
  		if (max8997->buck1_gpiodvs)
  			reg += max8997->buck125_gpioindex;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
292
293
294
  		break;
  	case MAX8997_BUCK2:
  		reg = MAX8997_REG_BUCK2DVS1;
6f43c3809   Axel Lin   regulator: Make m...
295
296
  		if (max8997->buck2_gpiodvs)
  			reg += max8997->buck125_gpioindex;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
297
298
299
300
301
302
303
304
305
  		break;
  	case MAX8997_BUCK3:
  		reg = MAX8997_REG_BUCK3DVS;
  		break;
  	case MAX8997_BUCK4:
  		reg = MAX8997_REG_BUCK4DVS;
  		break;
  	case MAX8997_BUCK5:
  		reg = MAX8997_REG_BUCK5DVS1;
6f43c3809   Axel Lin   regulator: Make m...
306
307
  		if (max8997->buck5_gpiodvs)
  			reg += max8997->buck125_gpioindex;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
  		break;
  	case MAX8997_BUCK7:
  		reg = MAX8997_REG_BUCK7DVS;
  		break;
  	case MAX8997_ESAFEOUT1 ...  MAX8997_ESAFEOUT2:
  		reg = MAX8997_REG_SAFEOUTCTRL;
  		shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
  		mask = 0x3;
  		break;
  	case MAX8997_CHARGER_CV:
  		reg = MAX8997_REG_MBCCTRL3;
  		shift = 0;
  		mask = 0xf;
  		break;
  	case MAX8997_CHARGER:
  		reg = MAX8997_REG_MBCCTRL4;
  		shift = 0;
  		mask = 0xf;
  		break;
  	case MAX8997_CHARGER_TOPOFF:
  		reg = MAX8997_REG_MBCCTRL5;
  		shift = 0;
  		mask = 0xf;
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	*_reg = reg;
  	*_shift = shift;
  	*_mask = mask;
  
  	return 0;
  }
9e96b3a7a   Axel Lin   regulator: Conver...
342
  static int max8997_get_voltage_sel(struct regulator_dev *rdev)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
343
344
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
345
346
  	struct i2c_client *i2c = max8997->iodev->i2c;
  	int reg, shift, mask, ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
347
348
349
350
351
  	u8 val;
  
  	ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
  	if (ret)
  		return ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
352
353
354
355
356
357
  	ret = max8997_read_reg(i2c, reg, &val);
  	if (ret)
  		return ret;
  
  	val >>= shift;
  	val &= mask;
9e96b3a7a   Axel Lin   regulator: Conver...
358
  	return val;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
359
360
361
362
363
364
  }
  
  static inline int max8997_get_voltage_proper_val(
  		const struct voltage_map_desc *desc,
  		int min_vol, int max_vol)
  {
2358b7763   Axel Lin   regulator: max899...
365
  	int i;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
366
367
368
369
370
371
  
  	if (desc == NULL)
  		return -EINVAL;
  
  	if (max_vol < desc->min || min_vol > desc->max)
  		return -EINVAL;
2358b7763   Axel Lin   regulator: max899...
372
373
  	if (min_vol < desc->min)
  		min_vol = desc->min;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
374

2358b7763   Axel Lin   regulator: max899...
375
  	i = DIV_ROUND_UP(min_vol - desc->min, desc->step);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
376

bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
377
  	if (desc->min + desc->step * i > max_vol)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
378
379
380
381
382
383
384
385
386
387
  		return -EINVAL;
  
  	return i;
  }
  
  static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
  		int min_uV, int max_uV, unsigned *selector)
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8997->iodev->i2c;
b3e1348e2   Axel Lin   regulator: Kill m...
388
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
389
390
391
392
393
394
395
396
397
398
399
400
401
  	int lb, ub;
  	int reg, shift = 0, mask, ret = 0;
  	u8 val = 0x0;
  
  	if (rid != MAX8997_CHARGER_CV)
  		return -EINVAL;
  
  	ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
  	if (ret)
  		return ret;
  
  	if (max_uV < 4000000 || min_uV > 4350000)
  		return -EINVAL;
f74521ca5   MyungJoo Ham   regulator: max899...
402
403
404
  	if (min_uV <= 4000000)
  		val = 0x1;
  	else if (min_uV <= 4200000 && max_uV >= 4200000)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  		val = 0x0;
  	else {
  		lb = (min_uV - 4000001) / 20000 + 2;
  		ub = (max_uV - 4000000) / 20000 + 1;
  
  		if (lb > ub)
  			return -EINVAL;
  
  		if (lb < 0xf)
  			val = lb;
  		else {
  			if (ub >= 0xf)
  				val = 0xf;
  			else
  				return -EINVAL;
  		}
  	}
  
  	*selector = val;
  
  	ret = max8997_update_reg(i2c, reg, val << shift, mask);
  
  	return ret;
  }
  
  /*
   * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
   * BUCK1, 2, and 5 are available if they are not controlled by gpio
   */
  static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
  		int min_uV, int max_uV, unsigned *selector)
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8997->iodev->i2c;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
439
  	const struct voltage_map_desc *desc;
b3e1348e2   Axel Lin   regulator: Kill m...
440
  	int rid = rdev_get_id(rdev);
62bc4d4a7   Axel Lin   regulator: Conver...
441
  	int i, reg, shift, mask, ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
  
  	switch (rid) {
  	case MAX8997_LDO1 ... MAX8997_LDO21:
  		break;
  	case MAX8997_BUCK1 ... MAX8997_BUCK5:
  		break;
  	case MAX8997_BUCK6:
  		return -EINVAL;
  	case MAX8997_BUCK7:
  		break;
  	case MAX8997_CHARGER:
  		break;
  	case MAX8997_CHARGER_TOPOFF:
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	desc = reg_voltage_map[rid];
bc3b7756b   Axel Lin   regulator: max899...
461
  	i = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
462
463
464
465
466
467
  	if (i < 0)
  		return i;
  
  	ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
  	if (ret)
  		return ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
468
469
  	ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
  	*selector = i;
62bc4d4a7   Axel Lin   regulator: Conver...
470
471
  	return ret;
  }
24bd41090   Axel Lin   regulator: max899...
472
  static int max8997_set_voltage_buck_time_sel(struct regulator_dev *rdev,
62bc4d4a7   Axel Lin   regulator: Conver...
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
  						unsigned int old_selector,
  						unsigned int new_selector)
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  	int rid = rdev_get_id(rdev);
  	const struct voltage_map_desc *desc = reg_voltage_map[rid];
  
  	/* Delay is required only if the voltage is increasing */
  	if (old_selector >= new_selector)
  		return 0;
  
  	/* No need to delay if gpio_dvs_mode */
  	switch (rid) {
  	case MAX8997_BUCK1:
  		if (max8997->buck1_gpiodvs)
  			return 0;
  		break;
  	case MAX8997_BUCK2:
  		if (max8997->buck2_gpiodvs)
  			return 0;
  		break;
  	case MAX8997_BUCK5:
  		if (max8997->buck5_gpiodvs)
  			return 0;
  		break;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
498
  	}
62bc4d4a7   Axel Lin   regulator: Conver...
499
500
501
502
503
504
  	switch (rid) {
  	case MAX8997_BUCK1:
  	case MAX8997_BUCK2:
  	case MAX8997_BUCK4:
  	case MAX8997_BUCK5:
  		return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
bc3b7756b   Axel Lin   regulator: max899...
505
  				    max8997->ramp_delay * 1000);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
506
  	}
62bc4d4a7   Axel Lin   regulator: Conver...
507
  	return 0;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
508
509
510
511
512
513
514
515
516
517
518
519
520
521
  }
  
  /*
   * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
   *
   * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
   * of one of the bucks may affect that of another buck, which is the side
   * effect of the change (set_voltage). This function examines the GPIO-DVS
   * configurations and checks whether such side-effect exists.
   */
  static int max8997_assess_side_effect(struct regulator_dev *rdev,
  		u8 new_val, int *best)
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
b3e1348e2   Axel Lin   regulator: Kill m...
522
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
  	u8 *buckx_val[3];
  	bool buckx_gpiodvs[3];
  	int side_effect[8];
  	int min_side_effect = INT_MAX;
  	int i;
  
  	*best = -1;
  
  	switch (rid) {
  	case MAX8997_BUCK1:
  		rid = 0;
  		break;
  	case MAX8997_BUCK2:
  		rid = 1;
  		break;
  	case MAX8997_BUCK5:
  		rid = 2;
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	buckx_val[0] = max8997->buck1_vol;
  	buckx_val[1] = max8997->buck2_vol;
  	buckx_val[2] = max8997->buck5_vol;
6e0414a5c   MyungJoo Ham   regulator: max899...
548
549
550
  	buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
  	buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
  	buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
  
  	for (i = 0; i < 8; i++) {
  		int others;
  
  		if (new_val != (buckx_val[rid])[i]) {
  			side_effect[i] = -1;
  			continue;
  		}
  
  		side_effect[i] = 0;
  		for (others = 0; others < 3; others++) {
  			int diff;
  
  			if (others == rid)
  				continue;
  			if (buckx_gpiodvs[others] == false)
  				continue; /* Not affected */
  			diff = (buckx_val[others])[i] -
  				(buckx_val[others])[max8997->buck125_gpioindex];
  			if (diff > 0)
  				side_effect[i] += diff;
  			else if (diff < 0)
  				side_effect[i] -= diff;
  		}
  		if (side_effect[i] == 0) {
  			*best = i;
  			return 0; /* NO SIDE EFFECT! Use This! */
  		}
  		if (side_effect[i] < min_side_effect) {
  			min_side_effect = side_effect[i];
  			*best = i;
  		}
  	}
  
  	if (*best == -1)
  		return -EINVAL;
  
  	return side_effect[*best];
  }
  
  /*
   * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
   * max8997_set_voltage_ldobuck to do the job.
   */
  static int max8997_set_voltage_buck(struct regulator_dev *rdev,
  		int min_uV, int max_uV, unsigned *selector)
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
b3e1348e2   Axel Lin   regulator: Kill m...
599
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
600
601
602
  	const struct voltage_map_desc *desc;
  	int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
  	bool gpio_dvs_mode = false;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
603
604
605
606
607
608
  
  	if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
  		return -EINVAL;
  
  	switch (rid) {
  	case MAX8997_BUCK1:
6e0414a5c   MyungJoo Ham   regulator: max899...
609
  		if (max8997->buck1_gpiodvs)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
610
611
612
  			gpio_dvs_mode = true;
  		break;
  	case MAX8997_BUCK2:
6e0414a5c   MyungJoo Ham   regulator: max899...
613
  		if (max8997->buck2_gpiodvs)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
614
615
616
  			gpio_dvs_mode = true;
  		break;
  	case MAX8997_BUCK5:
6e0414a5c   MyungJoo Ham   regulator: max899...
617
  		if (max8997->buck5_gpiodvs)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
618
619
620
621
622
623
624
625
626
  			gpio_dvs_mode = true;
  		break;
  	}
  
  	if (!gpio_dvs_mode)
  		return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
  						selector);
  
  	desc = reg_voltage_map[rid];
bc3b7756b   Axel Lin   regulator: max899...
627
  	new_val = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
  	if (new_val < 0)
  		return new_val;
  
  	tmp_dmg = INT_MAX;
  	tmp_idx = -1;
  	tmp_val = -1;
  	do {
  		damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
  		if (damage == 0)
  			goto out;
  
  		if (tmp_dmg > damage) {
  			tmp_idx = new_idx;
  			tmp_val = new_val;
  			tmp_dmg = damage;
  		}
  
  		new_val++;
f55205f4d   Axel Lin   regulator: Fix th...
646
  	} while (desc->min + desc->step * new_val <= desc->max);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
647
648
649
  
  	new_idx = tmp_idx;
  	new_val = tmp_val;
6e0414a5c   MyungJoo Ham   regulator: max899...
650
  	if (max8997->ignore_gpiodvs_side_effect == false)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
651
  		return -EINVAL;
c4e3b1442   Jingoo Han   regulator: max899...
652
653
654
655
  	dev_warn(&rdev->dev,
  		"MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:  %d -> %d
  ",
  		max8997->buck125_gpioindex, tmp_idx);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
656
657
658
659
660
661
662
663
664
665
666
  
  out:
  	if (new_idx < 0 || new_val < 0)
  		return -EINVAL;
  
  	max8997->buck125_gpioindex = new_idx;
  	max8997_set_gpio(max8997);
  	*selector = new_val;
  
  	return 0;
  }
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
667
  /* For SAFEOUT1 and SAFEOUT2 */
b79ca051b   Axel Lin   regulator: max899...
668
669
  static int max8997_set_voltage_safeout_sel(struct regulator_dev *rdev,
  					   unsigned selector)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
670
671
672
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8997->iodev->i2c;
b3e1348e2   Axel Lin   regulator: Kill m...
673
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
674
  	int reg, shift = 0, mask, ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
675
676
677
  
  	if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
  		return -EINVAL;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
678
679
680
  	ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
  	if (ret)
  		return ret;
b79ca051b   Axel Lin   regulator: max899...
681
  	return max8997_update_reg(i2c, reg, selector << shift, mask << shift);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
682
  }
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
683
684
685
686
687
  static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
  {
  	struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8997->iodev->i2c;
  	int ret, reg, mask, pattern;
b3e1348e2   Axel Lin   regulator: Kill m...
688
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
  
  	ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
  	if (ret)
  		return ret;
  
  	max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
  
  	if (rid == MAX8997_LDO1 ||
  			rid == MAX8997_LDO10 ||
  			rid == MAX8997_LDO21) {
  		dev_dbg(&rdev->dev, "Conditional Power-Off for %s
  ",
  				rdev->desc->name);
  		return max8997_update_reg(i2c, reg, 0x40, mask);
  	}
  
  	dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)
  ",
  			rdev->desc->name, max8997->saved_states[rid] & mask,
  			(~pattern) & mask);
  	return max8997_update_reg(i2c, reg, ~pattern, mask);
  }
9ed84d24d   Rikard Falkeborn   regulator: max899...
711
  static const struct regulator_ops max8997_ldo_ops = {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
712
713
714
715
  	.list_voltage		= max8997_list_voltage,
  	.is_enabled		= max8997_reg_is_enabled,
  	.enable			= max8997_reg_enable,
  	.disable		= max8997_reg_disable,
9e96b3a7a   Axel Lin   regulator: Conver...
716
  	.get_voltage_sel	= max8997_get_voltage_sel,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
717
  	.set_voltage		= max8997_set_voltage_ldobuck,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
718
719
  	.set_suspend_disable	= max8997_reg_disable_suspend,
  };
9ed84d24d   Rikard Falkeborn   regulator: max899...
720
  static const struct regulator_ops max8997_buck_ops = {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
721
722
723
724
  	.list_voltage		= max8997_list_voltage,
  	.is_enabled		= max8997_reg_is_enabled,
  	.enable			= max8997_reg_enable,
  	.disable		= max8997_reg_disable,
9e96b3a7a   Axel Lin   regulator: Conver...
725
  	.get_voltage_sel	= max8997_get_voltage_sel,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
726
  	.set_voltage		= max8997_set_voltage_buck,
24bd41090   Axel Lin   regulator: max899...
727
  	.set_voltage_time_sel	= max8997_set_voltage_buck_time_sel,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
728
729
  	.set_suspend_disable	= max8997_reg_disable_suspend,
  };
9ed84d24d   Rikard Falkeborn   regulator: max899...
730
  static const struct regulator_ops max8997_fixedvolt_ops = {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
731
732
733
734
  	.list_voltage		= max8997_list_voltage,
  	.is_enabled		= max8997_reg_is_enabled,
  	.enable			= max8997_reg_enable,
  	.disable		= max8997_reg_disable,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
735
736
  	.set_suspend_disable	= max8997_reg_disable_suspend,
  };
9ed84d24d   Rikard Falkeborn   regulator: max899...
737
  static const struct regulator_ops max8997_safeout_ops = {
b79ca051b   Axel Lin   regulator: max899...
738
  	.list_voltage		= regulator_list_voltage_table,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
739
740
741
  	.is_enabled		= max8997_reg_is_enabled,
  	.enable			= max8997_reg_enable,
  	.disable		= max8997_reg_disable,
9e96b3a7a   Axel Lin   regulator: Conver...
742
  	.get_voltage_sel	= max8997_get_voltage_sel,
b79ca051b   Axel Lin   regulator: max899...
743
  	.set_voltage_sel	= max8997_set_voltage_safeout_sel,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
744
745
  	.set_suspend_disable	= max8997_reg_disable_suspend,
  };
9ed84d24d   Rikard Falkeborn   regulator: max899...
746
  static const struct regulator_ops max8997_fixedstate_ops = {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
747
  	.list_voltage		= max8997_list_voltage_charger_cv,
9e96b3a7a   Axel Lin   regulator: Conver...
748
  	.get_voltage_sel	= max8997_get_voltage_sel,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
749
750
  	.set_voltage		= max8997_set_voltage_charger_cv,
  };
9e96b3a7a   Axel Lin   regulator: Conver...
751
752
  static int max8997_set_current_limit(struct regulator_dev *rdev,
  				     int min_uA, int max_uA)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
753
754
  {
  	unsigned dummy;
9e96b3a7a   Axel Lin   regulator: Conver...
755
  	int rid = rdev_get_id(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
756

9e96b3a7a   Axel Lin   regulator: Conver...
757
758
  	if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
  		return -EINVAL;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
759

9e96b3a7a   Axel Lin   regulator: Conver...
760
761
  	/* Reuse max8997_set_voltage_ldobuck to set current_limit. */
  	return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
762
  }
9e96b3a7a   Axel Lin   regulator: Conver...
763
764
765
766
767
768
769
770
771
772
773
774
775
776
  static int max8997_get_current_limit(struct regulator_dev *rdev)
  {
  	int sel, rid = rdev_get_id(rdev);
  
  	if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
  		return -EINVAL;
  
  	sel = max8997_get_voltage_sel(rdev);
  	if (sel < 0)
  		return sel;
  
  	/* Reuse max8997_list_voltage to get current_limit. */
  	return max8997_list_voltage(rdev, sel);
  }
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
777

9ed84d24d   Rikard Falkeborn   regulator: max899...
778
  static const struct regulator_ops max8997_charger_ops = {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
779
780
781
  	.is_enabled		= max8997_reg_is_enabled,
  	.enable			= max8997_reg_enable,
  	.disable		= max8997_reg_disable,
9e96b3a7a   Axel Lin   regulator: Conver...
782
783
  	.get_current_limit	= max8997_get_current_limit,
  	.set_current_limit	= max8997_set_current_limit,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
784
  };
9ed84d24d   Rikard Falkeborn   regulator: max899...
785
  static const struct regulator_ops max8997_charger_fixedstate_ops = {
9e96b3a7a   Axel Lin   regulator: Conver...
786
787
  	.get_current_limit	= max8997_get_current_limit,
  	.set_current_limit	= max8997_set_current_limit,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
788
  };
4533f80ef   Axel Lin   regulator: Add MA...
789
790
791
792
  #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
  	.name		= #_name,		\
  	.id		= MAX8997_##_name,	\
  	.ops		= &_ops,		\
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
793
794
795
  	.type		= REGULATOR_VOLTAGE,	\
  	.owner		= THIS_MODULE,		\
  }
4533f80ef   Axel Lin   regulator: Add MA...
796
797
798
799
800
801
  
  #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
  	.name		= #_name,		\
  	.id		= MAX8997_##_name,	\
  	.ops		= &_ops,		\
  	.type		= REGULATOR_CURRENT,	\
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
802
803
804
805
  	.owner		= THIS_MODULE,		\
  }
  
  static struct regulator_desc regulators[] = {
4533f80ef   Axel Lin   regulator: Add MA...
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
  	MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
  	MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
  	MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
  	MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
  	MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
  	MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
  	MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
  	MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
  	MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
  	MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
  	MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
  	MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
  	MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
  	MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
  	MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
  	MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
  				  max8997_charger_fixedstate_ops),
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
841
  };
77b71b370   Thomas Abraham   regulator: add de...
842
  #ifdef CONFIG_OF
b4895e2ca   Axel Lin   regulator: max899...
843
  static int max8997_pmic_dt_parse_dvs_gpio(struct platform_device *pdev,
77b71b370   Thomas Abraham   regulator: add de...
844
845
846
847
848
849
850
851
852
  			struct max8997_platform_data *pdata,
  			struct device_node *pmic_np)
  {
  	int i, gpio;
  
  	for (i = 0; i < 3; i++) {
  		gpio = of_get_named_gpio(pmic_np,
  					"max8997,pmic-buck125-dvs-gpios", i);
  		if (!gpio_is_valid(gpio)) {
b4895e2ca   Axel Lin   regulator: max899...
853
854
  			dev_err(&pdev->dev, "invalid gpio[%d]: %d
  ", i, gpio);
77b71b370   Thomas Abraham   regulator: add de...
855
856
857
858
859
860
  			return -EINVAL;
  		}
  		pdata->buck125_gpios[i] = gpio;
  	}
  	return 0;
  }
b4895e2ca   Axel Lin   regulator: max899...
861
  static int max8997_pmic_dt_parse_pdata(struct platform_device *pdev,
77b71b370   Thomas Abraham   regulator: add de...
862
863
  					struct max8997_platform_data *pdata)
  {
b4895e2ca   Axel Lin   regulator: max899...
864
  	struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
77b71b370   Thomas Abraham   regulator: add de...
865
866
867
  	struct device_node *pmic_np, *regulators_np, *reg_np;
  	struct max8997_regulator_data *rdata;
  	unsigned int i, dvs_voltage_nr = 1, ret;
b8b27a44d   Guodong Xu   regulator: remove...
868
  	pmic_np = iodev->dev->of_node;
77b71b370   Thomas Abraham   regulator: add de...
869
  	if (!pmic_np) {
b4895e2ca   Axel Lin   regulator: max899...
870
871
  		dev_err(&pdev->dev, "could not find pmic sub-node
  ");
77b71b370   Thomas Abraham   regulator: add de...
872
873
  		return -ENODEV;
  	}
c0a353631   Sachin Kamat   regulator: max899...
874
  	regulators_np = of_get_child_by_name(pmic_np, "regulators");
77b71b370   Thomas Abraham   regulator: add de...
875
  	if (!regulators_np) {
b4895e2ca   Axel Lin   regulator: max899...
876
877
  		dev_err(&pdev->dev, "could not find regulators sub-node
  ");
77b71b370   Thomas Abraham   regulator: add de...
878
879
880
881
  		return -EINVAL;
  	}
  
  	/* count the number of regulators to be supported in pmic */
ee70b2584   Axel Lin   regulator: max899...
882
  	pdata->num_regulators = of_get_child_count(regulators_np);
77b71b370   Thomas Abraham   regulator: add de...
883

a86854d0c   Kees Cook   treewide: devm_kz...
884
885
886
  	rdata = devm_kcalloc(&pdev->dev,
  			     pdata->num_regulators, sizeof(*rdata),
  			     GFP_KERNEL);
77b71b370   Thomas Abraham   regulator: add de...
887
  	if (!rdata) {
c92f5dd2c   Axel Lin   regulator: Add mi...
888
  		of_node_put(regulators_np);
77b71b370   Thomas Abraham   regulator: add de...
889
890
891
892
893
894
  		return -ENOMEM;
  	}
  
  	pdata->regulators = rdata;
  	for_each_child_of_node(regulators_np, reg_np) {
  		for (i = 0; i < ARRAY_SIZE(regulators); i++)
c32569e35   Rob Herring   regulator: Use of...
895
  			if (of_node_name_eq(reg_np, regulators[i].name))
77b71b370   Thomas Abraham   regulator: add de...
896
897
898
  				break;
  
  		if (i == ARRAY_SIZE(regulators)) {
0c9721a5d   Rob Herring   regulator: Conver...
899
900
901
  			dev_warn(&pdev->dev, "don't know how to configure regulator %pOFn
  ",
  				 reg_np);
77b71b370   Thomas Abraham   regulator: add de...
902
903
904
905
  			continue;
  		}
  
  		rdata->id = i;
b4895e2ca   Axel Lin   regulator: max899...
906
  		rdata->initdata = of_get_regulator_init_data(&pdev->dev,
072e78b12   Javier Martinez Canillas   regulator: of: Ad...
907
908
  							     reg_np,
  							     &regulators[i]);
77b71b370   Thomas Abraham   regulator: add de...
909
910
911
  		rdata->reg_node = reg_np;
  		rdata++;
  	}
c92f5dd2c   Axel Lin   regulator: Add mi...
912
  	of_node_put(regulators_np);
77b71b370   Thomas Abraham   regulator: add de...
913
914
915
916
917
918
919
920
921
922
923
924
  
  	if (of_get_property(pmic_np, "max8997,pmic-buck1-uses-gpio-dvs", NULL))
  		pdata->buck1_gpiodvs = true;
  
  	if (of_get_property(pmic_np, "max8997,pmic-buck2-uses-gpio-dvs", NULL))
  		pdata->buck2_gpiodvs = true;
  
  	if (of_get_property(pmic_np, "max8997,pmic-buck5-uses-gpio-dvs", NULL))
  		pdata->buck5_gpiodvs = true;
  
  	if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
  						pdata->buck5_gpiodvs) {
b4895e2ca   Axel Lin   regulator: max899...
925
  		ret = max8997_pmic_dt_parse_dvs_gpio(pdev, pdata, pmic_np);
77b71b370   Thomas Abraham   regulator: add de...
926
927
928
929
930
931
932
933
934
935
  		if (ret)
  			return -EINVAL;
  
  		if (of_property_read_u32(pmic_np,
  				"max8997,pmic-buck125-default-dvs-idx",
  				&pdata->buck125_default_idx)) {
  			pdata->buck125_default_idx = 0;
  		} else {
  			if (pdata->buck125_default_idx >= 8) {
  				pdata->buck125_default_idx = 0;
b4895e2ca   Axel Lin   regulator: max899...
936
937
  				dev_info(&pdev->dev, "invalid value for default dvs index, using 0 instead
  ");
77b71b370   Thomas Abraham   regulator: add de...
938
939
940
941
942
943
944
945
946
947
948
949
950
  			}
  		}
  
  		if (of_get_property(pmic_np,
  			"max8997,pmic-ignore-gpiodvs-side-effect", NULL))
  			pdata->ignore_gpiodvs_side_effect = true;
  
  		dvs_voltage_nr = 8;
  	}
  
  	if (of_property_read_u32_array(pmic_np,
  				"max8997,pmic-buck1-dvs-voltage",
  				pdata->buck1_voltage, dvs_voltage_nr)) {
b4895e2ca   Axel Lin   regulator: max899...
951
952
  		dev_err(&pdev->dev, "buck1 voltages not specified
  ");
77b71b370   Thomas Abraham   regulator: add de...
953
954
955
956
957
958
  		return -EINVAL;
  	}
  
  	if (of_property_read_u32_array(pmic_np,
  				"max8997,pmic-buck2-dvs-voltage",
  				pdata->buck2_voltage, dvs_voltage_nr)) {
b4895e2ca   Axel Lin   regulator: max899...
959
960
  		dev_err(&pdev->dev, "buck2 voltages not specified
  ");
77b71b370   Thomas Abraham   regulator: add de...
961
962
963
964
965
966
  		return -EINVAL;
  	}
  
  	if (of_property_read_u32_array(pmic_np,
  				"max8997,pmic-buck5-dvs-voltage",
  				pdata->buck5_voltage, dvs_voltage_nr)) {
b4895e2ca   Axel Lin   regulator: max899...
967
968
  		dev_err(&pdev->dev, "buck5 voltages not specified
  ");
77b71b370   Thomas Abraham   regulator: add de...
969
970
971
972
973
974
  		return -EINVAL;
  	}
  
  	return 0;
  }
  #else
b4895e2ca   Axel Lin   regulator: max899...
975
  static int max8997_pmic_dt_parse_pdata(struct platform_device *pdev,
77b71b370   Thomas Abraham   regulator: add de...
976
977
978
979
980
  					struct max8997_platform_data *pdata)
  {
  	return 0;
  }
  #endif /* CONFIG_OF */
a5023574d   Bill Pemberton   regulator: remove...
981
  static int max8997_pmic_probe(struct platform_device *pdev)
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
982
983
  {
  	struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
77b71b370   Thomas Abraham   regulator: add de...
984
  	struct max8997_platform_data *pdata = iodev->pdata;
c172708d3   Mark Brown   regulator: core: ...
985
  	struct regulator_config config = { };
ad78bba42   Axel Lin   regulator: max899...
986
  	struct regulator_dev *rdev;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
987
988
  	struct max8997_data *max8997;
  	struct i2c_client *i2c;
ad78bba42   Axel Lin   regulator: max899...
989
  	int i, ret, nr_dvs;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
990
  	u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
a2f07305e   Axel Lin   regulator: max899...
991
992
993
  	if (!pdata) {
  		dev_err(&pdev->dev, "No platform init data supplied.
  ");
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
994
995
  		return -ENODEV;
  	}
77b71b370   Thomas Abraham   regulator: add de...
996
  	if (iodev->dev->of_node) {
b4895e2ca   Axel Lin   regulator: max899...
997
  		ret = max8997_pmic_dt_parse_pdata(pdev, pdata);
77b71b370   Thomas Abraham   regulator: add de...
998
999
1000
  		if (ret)
  			return ret;
  	}
8ae5767ba   Axel Lin   regulator: max899...
1001
1002
  	max8997 = devm_kzalloc(&pdev->dev, sizeof(struct max8997_data),
  			       GFP_KERNEL);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1003
1004
  	if (!max8997)
  		return -ENOMEM;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1005
1006
1007
1008
1009
1010
1011
  	max8997->dev = &pdev->dev;
  	max8997->iodev = iodev;
  	max8997->num_regulators = pdata->num_regulators;
  	platform_set_drvdata(pdev, max8997);
  	i2c = max8997->iodev->i2c;
  
  	max8997->buck125_gpioindex = pdata->buck125_default_idx;
6e0414a5c   MyungJoo Ham   regulator: max899...
1012
1013
1014
1015
1016
  	max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
  	max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
  	max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
  	memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
  	max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1017

068a8c823   Thomas Abraham   regulator: max899...
1018
1019
1020
1021
  	nr_dvs = (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
  			pdata->buck5_gpiodvs) ? 8 : 1;
  
  	for (i = 0; i < nr_dvs; i++) {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1022
1023
1024
  		max8997->buck1_vol[i] = ret =
  			max8997_get_voltage_proper_val(
  					&buck1245_voltage_map_desc,
bc3b7756b   Axel Lin   regulator: max899...
1025
1026
  					pdata->buck1_voltage[i],
  					pdata->buck1_voltage[i] +
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1027
1028
  					buck1245_voltage_map_desc.step);
  		if (ret < 0)
4177f95b8   Sachin Kamat   regulator: max899...
1029
  			return ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1030
1031
1032
1033
  
  		max8997->buck2_vol[i] = ret =
  			max8997_get_voltage_proper_val(
  					&buck1245_voltage_map_desc,
bc3b7756b   Axel Lin   regulator: max899...
1034
1035
  					pdata->buck2_voltage[i],
  					pdata->buck2_voltage[i] +
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1036
1037
  					buck1245_voltage_map_desc.step);
  		if (ret < 0)
4177f95b8   Sachin Kamat   regulator: max899...
1038
  			return ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1039
1040
1041
1042
  
  		max8997->buck5_vol[i] = ret =
  			max8997_get_voltage_proper_val(
  					&buck1245_voltage_map_desc,
bc3b7756b   Axel Lin   regulator: max899...
1043
1044
  					pdata->buck5_voltage[i],
  					pdata->buck5_voltage[i] +
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1045
1046
  					buck1245_voltage_map_desc.step);
  		if (ret < 0)
4177f95b8   Sachin Kamat   regulator: max899...
1047
  			return ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
  
  		if (max_buck1 < max8997->buck1_vol[i])
  			max_buck1 = max8997->buck1_vol[i];
  		if (max_buck2 < max8997->buck2_vol[i])
  			max_buck2 = max8997->buck2_vol[i];
  		if (max_buck5 < max8997->buck5_vol[i])
  			max_buck5 = max8997->buck5_vol[i];
  	}
  
  	/* For the safety, set max voltage before setting up */
  	for (i = 0; i < 8; i++) {
ecb9c4f59   Axel Lin   regulator: Remove...
1059
  		max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1060
  				max_buck1, 0x3f);
ecb9c4f59   Axel Lin   regulator: Remove...
1061
  		max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1062
  				max_buck2, 0x3f);
ecb9c4f59   Axel Lin   regulator: Remove...
1063
  		max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1064
1065
  				max_buck5, 0x3f);
  	}
11ec7bf00   Thomas Abraham   regulator: max899...
1066
  	/* Initialize all the DVS related BUCK registers */
068a8c823   Thomas Abraham   regulator: max899...
1067
  	for (i = 0; i < nr_dvs; i++) {
11ec7bf00   Thomas Abraham   regulator: max899...
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
  		max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
  				max8997->buck1_vol[i],
  				0x3f);
  		max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
  				max8997->buck2_vol[i],
  				0x3f);
  		max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
  				max8997->buck5_vol[i],
  				0x3f);
  	}
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1078
1079
1080
1081
1082
1083
  	/*
  	 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
  	 * If at least one of them cares, set gpios.
  	 */
  	if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
  			pdata->buck5_gpiodvs) {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1084
1085
1086
1087
1088
1089
  
  		if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
  				!gpio_is_valid(pdata->buck125_gpios[1]) ||
  				!gpio_is_valid(pdata->buck125_gpios[2])) {
  			dev_err(&pdev->dev, "GPIO NOT VALID
  ");
4177f95b8   Sachin Kamat   regulator: max899...
1090
  			return -EINVAL;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1091
  		}
8fa25eda8   Axel Lin   regulator: max899...
1092
1093
1094
  		ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[0],
  					"MAX8997 SET1");
  		if (ret)
4177f95b8   Sachin Kamat   regulator: max899...
1095
  			return ret;
8fa25eda8   Axel Lin   regulator: max899...
1096
1097
1098
1099
  
  		ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[1],
  					"MAX8997 SET2");
  		if (ret)
4177f95b8   Sachin Kamat   regulator: max899...
1100
  			return ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1101

8fa25eda8   Axel Lin   regulator: max899...
1102
  		ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[2],
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1103
  				"MAX8997 SET3");
8fa25eda8   Axel Lin   regulator: max899...
1104
  		if (ret)
4177f95b8   Sachin Kamat   regulator: max899...
1105
  			return ret;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
  
  		gpio_direction_output(pdata->buck125_gpios[0],
  				(max8997->buck125_gpioindex >> 2)
  				& 0x1); /* SET1 */
  		gpio_direction_output(pdata->buck125_gpios[1],
  				(max8997->buck125_gpioindex >> 1)
  				& 0x1); /* SET2 */
  		gpio_direction_output(pdata->buck125_gpios[2],
  				(max8997->buck125_gpioindex >> 0)
  				& 0x1); /* SET3 */
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1116
1117
1118
1119
1120
1121
1122
1123
1124
  	}
  
  	/* DVS-GPIO disabled */
  	max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
  			(1 << 1) : (0 << 1), 1 << 1);
  	max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
  			(1 << 1) : (0 << 1), 1 << 1);
  	max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
  			(1 << 1) : (0 << 1), 1 << 1);
dbb48e7c3   Tushar Behera   regulator: MAX899...
1125
1126
1127
  	/* Misc Settings */
  	max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
  	max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1128
1129
1130
1131
1132
  	for (i = 0; i < pdata->num_regulators; i++) {
  		const struct voltage_map_desc *desc;
  		int id = pdata->regulators[i].id;
  
  		desc = reg_voltage_map[id];
b79ca051b   Axel Lin   regulator: max899...
1133
  		if (desc) {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1134
1135
  			regulators[id].n_voltages =
  				(desc->max - desc->min) / desc->step + 1;
b79ca051b   Axel Lin   regulator: max899...
1136
1137
1138
1139
  		} else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2) {
  			regulators[id].volt_table = safeoutvolt;
  			regulators[id].n_voltages = ARRAY_SIZE(safeoutvolt);
  		} else if (id == MAX8997_CHARGER_CV) {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1140
  			regulators[id].n_voltages = 16;
b79ca051b   Axel Lin   regulator: max899...
1141
  		}
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1142

c172708d3   Mark Brown   regulator: core: ...
1143
1144
1145
  		config.dev = max8997->dev;
  		config.init_data = pdata->regulators[i].initdata;
  		config.driver_data = max8997;
77b71b370   Thomas Abraham   regulator: add de...
1146
  		config.of_node = pdata->regulators[i].reg_node;
c172708d3   Mark Brown   regulator: core: ...
1147

ad78bba42   Axel Lin   regulator: max899...
1148
1149
1150
  		rdev = devm_regulator_register(&pdev->dev, &regulators[id],
  					       &config);
  		if (IS_ERR(rdev)) {
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1151
1152
1153
  			dev_err(max8997->dev, "regulator init failed for %d
  ",
  					id);
ad78bba42   Axel Lin   regulator: max899...
1154
  			return PTR_ERR(rdev);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1155
1156
  		}
  	}
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1157
  	return 0;
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1158
1159
1160
1161
1162
1163
  }
  
  static const struct platform_device_id max8997_pmic_id[] = {
  	{ "max8997-pmic", 0},
  	{ },
  };
a51b907b2   Axel Lin   regulator: Add MO...
1164
  MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1165
1166
1167
1168
  
  static struct platform_driver max8997_pmic_driver = {
  	.driver = {
  		.name = "max8997-pmic",
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1169
1170
  	},
  	.probe = max8997_pmic_probe,
bd6ca2cf5   MyungJoo Ham   regulator: MAX899...
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
  	.id_table = max8997_pmic_id,
  };
  
  static int __init max8997_pmic_init(void)
  {
  	return platform_driver_register(&max8997_pmic_driver);
  }
  subsys_initcall(max8997_pmic_init);
  
  static void __exit max8997_pmic_cleanup(void)
  {
  	platform_driver_unregister(&max8997_pmic_driver);
  }
  module_exit(max8997_pmic_cleanup);
  
  MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
  MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  MODULE_LICENSE("GPL");