Blame view

drivers/regulator/max8998.c 21.9 KB
5e9384c70   Krzysztof Kozlowski   regulator: maxim:...
1
2
3
4
5
6
7
  // SPDX-License-Identifier: GPL-2.0+
  //
  // max8998.c - Voltage regulator driver for the Maxim 8998
  //
  //  Copyright (C) 2009-2010 Samsung Electronics
  //  Kyungmin Park <kyungmin.park@samsung.com>
  //  Marek Szyprowski <m.szyprowski@samsung.com>
156f25285   Kyungmin Park   drivers: regulato...
8
9
10
11
12
13
14
15
16
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/i2c.h>
  #include <linux/err.h>
  #include <linux/gpio.h>
  #include <linux/slab.h>
  #include <linux/interrupt.h>
  #include <linux/mutex.h>
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
17
18
  #include <linux/of.h>
  #include <linux/of_gpio.h>
156f25285   Kyungmin Park   drivers: regulato...
19
20
  #include <linux/platform_device.h>
  #include <linux/regulator/driver.h>
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
21
  #include <linux/regulator/of_regulator.h>
156f25285   Kyungmin Park   drivers: regulato...
22
23
24
25
26
27
28
  #include <linux/mfd/max8998.h>
  #include <linux/mfd/max8998-private.h>
  
  struct max8998_data {
  	struct device		*dev;
  	struct max8998_dev	*iodev;
  	int			num_regulators;
889cd5a60   Lukasz Majewski   regulator: max899...
29
30
31
32
33
  	u8                      buck1_vol[4]; /* voltages for selection */
  	u8                      buck2_vol[2];
  	unsigned int		buck1_idx; /* index to last changed voltage */
  					   /* value in a set */
  	unsigned int		buck2_idx;
156f25285   Kyungmin Park   drivers: regulato...
34
  };
4ffea5e08   Jonathan Bakker   regulator: max899...
35
36
37
  static const unsigned int charger_current_table[] = {
  	90000, 380000, 475000, 550000, 570000, 600000, 700000, 800000,
  };
156f25285   Kyungmin Park   drivers: regulato...
38
39
40
  static int max8998_get_enable_register(struct regulator_dev *rdev,
  					int *reg, int *shift)
  {
7b94791be   Axel Lin   regulator: Kill m...
41
  	int ldo = rdev_get_id(rdev);
156f25285   Kyungmin Park   drivers: regulato...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  
  	switch (ldo) {
  	case MAX8998_LDO2 ... MAX8998_LDO5:
  		*reg = MAX8998_REG_ONOFF1;
  		*shift = 3 - (ldo - MAX8998_LDO2);
  		break;
  	case MAX8998_LDO6 ... MAX8998_LDO13:
  		*reg = MAX8998_REG_ONOFF2;
  		*shift = 7 - (ldo - MAX8998_LDO6);
  		break;
  	case MAX8998_LDO14 ... MAX8998_LDO17:
  		*reg = MAX8998_REG_ONOFF3;
  		*shift = 7 - (ldo - MAX8998_LDO14);
  		break;
  	case MAX8998_BUCK1 ... MAX8998_BUCK4:
  		*reg = MAX8998_REG_ONOFF1;
  		*shift = 7 - (ldo - MAX8998_BUCK1);
  		break;
  	case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
  		*reg = MAX8998_REG_ONOFF4;
  		*shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
  		break;
  	case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
  		*reg = MAX8998_REG_CHGR2;
  		*shift = 7 - (ldo - MAX8998_ESAFEOUT1);
  		break;
4ffea5e08   Jonathan Bakker   regulator: max899...
68
69
70
71
  	case MAX8998_CHARGER:
  		*reg = MAX8998_REG_CHGR2;
  		*shift = 0;
  		break;
156f25285   Kyungmin Park   drivers: regulato...
72
73
74
75
76
77
78
79
80
81
  	default:
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
  {
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
676e02d7a   Joonyoung Shim   mfd: Use i2c_clie...
82
  	struct i2c_client *i2c = max8998->iodev->i2c;
156f25285   Kyungmin Park   drivers: regulato...
83
84
85
86
87
88
  	int ret, reg, shift = 8;
  	u8 val;
  
  	ret = max8998_get_enable_register(rdev, &reg, &shift);
  	if (ret)
  		return ret;
676e02d7a   Joonyoung Shim   mfd: Use i2c_clie...
89
  	ret = max8998_read_reg(i2c, reg, &val);
156f25285   Kyungmin Park   drivers: regulato...
90
91
92
93
94
  	if (ret)
  		return ret;
  
  	return val & (1 << shift);
  }
4ffea5e08   Jonathan Bakker   regulator: max899...
95
96
97
98
  static int max8998_ldo_is_enabled_inverted(struct regulator_dev *rdev)
  {
  	return (!max8998_ldo_is_enabled(rdev));
  }
156f25285   Kyungmin Park   drivers: regulato...
99
100
101
  static int max8998_ldo_enable(struct regulator_dev *rdev)
  {
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
676e02d7a   Joonyoung Shim   mfd: Use i2c_clie...
102
  	struct i2c_client *i2c = max8998->iodev->i2c;
156f25285   Kyungmin Park   drivers: regulato...
103
104
105
106
107
  	int reg, shift = 8, ret;
  
  	ret = max8998_get_enable_register(rdev, &reg, &shift);
  	if (ret)
  		return ret;
676e02d7a   Joonyoung Shim   mfd: Use i2c_clie...
108
  	return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
156f25285   Kyungmin Park   drivers: regulato...
109
110
111
112
113
  }
  
  static int max8998_ldo_disable(struct regulator_dev *rdev)
  {
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
676e02d7a   Joonyoung Shim   mfd: Use i2c_clie...
114
  	struct i2c_client *i2c = max8998->iodev->i2c;
156f25285   Kyungmin Park   drivers: regulato...
115
116
117
118
119
  	int reg, shift = 8, ret;
  
  	ret = max8998_get_enable_register(rdev, &reg, &shift);
  	if (ret)
  		return ret;
676e02d7a   Joonyoung Shim   mfd: Use i2c_clie...
120
  	return max8998_update_reg(i2c, reg, 0, 1<<shift);
156f25285   Kyungmin Park   drivers: regulato...
121
122
123
124
125
  }
  
  static int max8998_get_voltage_register(struct regulator_dev *rdev,
  				int *_reg, int *_shift, int *_mask)
  {
7b94791be   Axel Lin   regulator: Kill m...
126
  	int ldo = rdev_get_id(rdev);
889cd5a60   Lukasz Majewski   regulator: max899...
127
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
156f25285   Kyungmin Park   drivers: regulato...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  	int reg, shift = 0, mask = 0xff;
  
  	switch (ldo) {
  	case MAX8998_LDO2 ... MAX8998_LDO3:
  		reg = MAX8998_REG_LDO2_LDO3;
  		mask = 0xf;
  		if (ldo == MAX8998_LDO2)
  			shift = 4;
  		else
  			shift = 0;
  		break;
  	case MAX8998_LDO4 ... MAX8998_LDO7:
  		reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
  		break;
  	case MAX8998_LDO8 ... MAX8998_LDO9:
  		reg = MAX8998_REG_LDO8_LDO9;
  		mask = 0xf;
  		if (ldo == MAX8998_LDO8)
  			shift = 4;
  		else
  			shift = 0;
  		break;
  	case MAX8998_LDO10 ... MAX8998_LDO11:
  		reg = MAX8998_REG_LDO10_LDO11;
  		if (ldo == MAX8998_LDO10) {
  			shift = 5;
  			mask = 0x7;
  		} else {
  			shift = 0;
  			mask = 0x1f;
  		}
  		break;
  	case MAX8998_LDO12 ... MAX8998_LDO17:
  		reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
  		break;
  	case MAX8998_BUCK1:
889cd5a60   Lukasz Majewski   regulator: max899...
164
  		reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
156f25285   Kyungmin Park   drivers: regulato...
165
166
  		break;
  	case MAX8998_BUCK2:
889cd5a60   Lukasz Majewski   regulator: max899...
167
  		reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
156f25285   Kyungmin Park   drivers: regulato...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  		break;
  	case MAX8998_BUCK3:
  		reg = MAX8998_REG_BUCK3;
  		break;
  	case MAX8998_BUCK4:
  		reg = MAX8998_REG_BUCK4;
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	*_reg = reg;
  	*_shift = shift;
  	*_mask = mask;
  
  	return 0;
  }
7b4354b46   Axel Lin   regulator: Conver...
185
  static int max8998_get_voltage_sel(struct regulator_dev *rdev)
156f25285   Kyungmin Park   drivers: regulato...
186
187
  {
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
676e02d7a   Joonyoung Shim   mfd: Use i2c_clie...
188
  	struct i2c_client *i2c = max8998->iodev->i2c;
156f25285   Kyungmin Park   drivers: regulato...
189
190
191
192
193
194
  	int reg, shift = 0, mask, ret;
  	u8 val;
  
  	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
  	if (ret)
  		return ret;
676e02d7a   Joonyoung Shim   mfd: Use i2c_clie...
195
  	ret = max8998_read_reg(i2c, reg, &val);
156f25285   Kyungmin Park   drivers: regulato...
196
197
198
199
200
  	if (ret)
  		return ret;
  
  	val >>= shift;
  	val &= mask;
7b4354b46   Axel Lin   regulator: Conver...
201
  	return val;
156f25285   Kyungmin Park   drivers: regulato...
202
  }
baae019ef   Axel Lin   regulator: max899...
203
204
  static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
  				       unsigned selector)
9d92492fb   Lukasz Majewski   regulator: Separa...
205
206
207
  {
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8998->iodev->i2c;
baae019ef   Axel Lin   regulator: max899...
208
  	int reg, shift = 0, mask, ret;
3a93f2a9f   Mark Brown   regulator: Report...
209

9d92492fb   Lukasz Majewski   regulator: Separa...
210
211
212
  	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
  	if (ret)
  		return ret;
baae019ef   Axel Lin   regulator: max899...
213
  	ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
9d92492fb   Lukasz Majewski   regulator: Separa...
214
215
216
  
  	return ret;
  }
50f19a459   Lukasz Majewski   regulator: max899...
217
218
219
220
221
222
223
224
225
226
  static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
  {
  	gpio_set_value(gpio1, v & 0x1);
  	gpio_set_value(gpio2, (v >> 1) & 0x1);
  }
  
  static inline void buck2_gpio_set(int gpio, int v)
  {
  	gpio_set_value(gpio, v & 0x1);
  }
baae019ef   Axel Lin   regulator: max899...
227
228
  static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
  					unsigned selector)
156f25285   Kyungmin Park   drivers: regulato...
229
230
  {
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
c14727379   PaweÅ‚ Chmiel   regulator: max899...
231
  	struct max8998_platform_data *pdata = max8998->iodev->pdata;
676e02d7a   Joonyoung Shim   mfd: Use i2c_clie...
232
  	struct i2c_client *i2c = max8998->iodev->i2c;
7b94791be   Axel Lin   regulator: Kill m...
233
  	int buck = rdev_get_id(rdev);
c6163a702   Axel Lin   regulator: max899...
234
  	int reg, shift = 0, mask, ret, j;
50f19a459   Lukasz Majewski   regulator: max899...
235
  	static u8 buck1_last_val;
156f25285   Kyungmin Park   drivers: regulato...
236

156f25285   Kyungmin Park   drivers: regulato...
237
238
239
  	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
  	if (ret)
  		return ret;
50f19a459   Lukasz Majewski   regulator: max899...
240
241
242
  	switch (buck) {
  	case MAX8998_BUCK1:
  		dev_dbg(max8998->dev,
baae019ef   Axel Lin   regulator: max899...
243
244
  			"BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d
  "
85ee7a1d3   Joe Perches   treewide: cleanup...
245
246
  			"buck1_vol3:%d, buck1_vol4:%d
  ",
baae019ef   Axel Lin   regulator: max899...
247
  			selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
50f19a459   Lukasz Majewski   regulator: max899...
248
249
250
251
252
253
254
255
  			max8998->buck1_vol[2], max8998->buck1_vol[3]);
  
  		if (gpio_is_valid(pdata->buck1_set1) &&
  		    gpio_is_valid(pdata->buck1_set2)) {
  
  			/* check if requested voltage */
  			/* value is already defined */
  			for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
baae019ef   Axel Lin   regulator: max899...
256
  				if (max8998->buck1_vol[j] == selector) {
50f19a459   Lukasz Majewski   regulator: max899...
257
258
259
260
261
262
  					max8998->buck1_idx = j;
  					buck1_gpio_set(pdata->buck1_set1,
  						       pdata->buck1_set2, j);
  					goto buck1_exit;
  				}
  			}
735a3d9ef   MyungJoo Ham   regulator: Suppor...
263
264
  			if (pdata->buck_voltage_lock)
  				return -EINVAL;
50f19a459   Lukasz Majewski   regulator: max899...
265
266
267
268
269
  			/* no predefine regulator found */
  			max8998->buck1_idx = (buck1_last_val % 2) + 2;
  			dev_dbg(max8998->dev, "max8998->buck1_idx:%d
  ",
  				max8998->buck1_idx);
baae019ef   Axel Lin   regulator: max899...
270
  			max8998->buck1_vol[max8998->buck1_idx] = selector;
50f19a459   Lukasz Majewski   regulator: max899...
271
272
273
  			ret = max8998_get_voltage_register(rdev, &reg,
  							   &shift,
  							   &mask);
baae019ef   Axel Lin   regulator: max899...
274
  			ret = max8998_write_reg(i2c, reg, selector);
50f19a459   Lukasz Majewski   regulator: max899...
275
276
277
278
279
280
281
282
283
284
  			buck1_gpio_set(pdata->buck1_set1,
  				       pdata->buck1_set2, max8998->buck1_idx);
  			buck1_last_val++;
  buck1_exit:
  			dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d
  ",
  				i2c->name, gpio_get_value(pdata->buck1_set1),
  				gpio_get_value(pdata->buck1_set2));
  			break;
  		} else {
baae019ef   Axel Lin   regulator: max899...
285
  			ret = max8998_write_reg(i2c, reg, selector);
50f19a459   Lukasz Majewski   regulator: max899...
286
287
  		}
  		break;
c5a4655db   MyungJoo Ham   regulator: MAX899...
288

50f19a459   Lukasz Majewski   regulator: max899...
289
290
  	case MAX8998_BUCK2:
  		dev_dbg(max8998->dev,
baae019ef   Axel Lin   regulator: max899...
291
292
293
  			"BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d
  ",
  			selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
50f19a459   Lukasz Majewski   regulator: max899...
294
  		if (gpio_is_valid(pdata->buck2_set3)) {
735a3d9ef   MyungJoo Ham   regulator: Suppor...
295
296
297
298
  
  			/* check if requested voltage */
  			/* value is already defined */
  			for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
baae019ef   Axel Lin   regulator: max899...
299
  				if (max8998->buck2_vol[j] == selector) {
735a3d9ef   MyungJoo Ham   regulator: Suppor...
300
301
302
303
  					max8998->buck2_idx = j;
  					buck2_gpio_set(pdata->buck2_set3, j);
  					goto buck2_exit;
  				}
50f19a459   Lukasz Majewski   regulator: max899...
304
  			}
735a3d9ef   MyungJoo Ham   regulator: Suppor...
305
306
307
308
309
310
  
  			if (pdata->buck_voltage_lock)
  				return -EINVAL;
  
  			max8998_get_voltage_register(rdev,
  					&reg, &shift, &mask);
baae019ef   Axel Lin   regulator: max899...
311
312
  			ret = max8998_write_reg(i2c, reg, selector);
  			max8998->buck2_vol[max8998->buck2_idx] = selector;
735a3d9ef   MyungJoo Ham   regulator: Suppor...
313
314
  			buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
  buck2_exit:
50f19a459   Lukasz Majewski   regulator: max899...
315
316
317
318
  			dev_dbg(max8998->dev, "%s: SET3:%d
  ", i2c->name,
  				gpio_get_value(pdata->buck2_set3));
  		} else {
baae019ef   Axel Lin   regulator: max899...
319
  			ret = max8998_write_reg(i2c, reg, selector);
50f19a459   Lukasz Majewski   regulator: max899...
320
321
322
323
324
  		}
  		break;
  
  	case MAX8998_BUCK3:
  	case MAX8998_BUCK4:
baae019ef   Axel Lin   regulator: max899...
325
326
  		ret = max8998_update_reg(i2c, reg, selector<<shift,
  					 mask<<shift);
50f19a459   Lukasz Majewski   regulator: max899...
327
  		break;
c5a4655db   MyungJoo Ham   regulator: MAX899...
328
  	}
276909d35   Axel Lin   regulator: Conver...
329
330
331
332
333
334
335
336
337
  	return ret;
  }
  
  static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
  					     unsigned int old_selector,
  					     unsigned int new_selector)
  {
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8998->iodev->i2c;
276909d35   Axel Lin   regulator: Conver...
338
339
340
341
342
343
  	int buck = rdev_get_id(rdev);
  	u8 val = 0;
  	int difference, ret;
  
  	if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
  		return -EINVAL;
50f19a459   Lukasz Majewski   regulator: max899...
344
  	/* Voltage stabilization */
276909d35   Axel Lin   regulator: Conver...
345
346
347
  	ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
  	if (ret)
  		return ret;
50f19a459   Lukasz Majewski   regulator: max899...
348
349
350
351
  
  	/* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
  	/* MAX8998 has ENRAMP bit implemented, so test it*/
  	if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
276909d35   Axel Lin   regulator: Conver...
352
  		return 0;
50f19a459   Lukasz Majewski   regulator: max899...
353

0dceab333   Axel Lin   regulator: max899...
354
  	difference = (new_selector - old_selector) * rdev->desc->uV_step / 1000;
50f19a459   Lukasz Majewski   regulator: max899...
355
  	if (difference > 0)
81d0a6ae7   Axel Lin   regulator: max899...
356
  		return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
50f19a459   Lukasz Majewski   regulator: max899...
357

276909d35   Axel Lin   regulator: Conver...
358
  	return 0;
156f25285   Kyungmin Park   drivers: regulato...
359
  }
0b0c0bd81   kbuild test robot   regulator: max899...
360
361
  static int max8998_set_current_limit(struct regulator_dev *rdev,
  				     int min_uA, int max_uA)
4ffea5e08   Jonathan Bakker   regulator: max899...
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  {
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8998->iodev->i2c;
  	unsigned int n_currents = rdev->desc->n_current_limits;
  	int i, sel = -1;
  
  	if (n_currents == 0)
  		return -EINVAL;
  
  	if (rdev->desc->curr_table) {
  		const unsigned int *curr_table = rdev->desc->curr_table;
  		bool ascend = curr_table[n_currents - 1] > curr_table[0];
  
  		/* search for closest to maximum */
  		if (ascend) {
  			for (i = n_currents - 1; i >= 0; i--) {
  				if (min_uA <= curr_table[i] &&
  				    curr_table[i] <= max_uA) {
  					sel = i;
  					break;
  				}
  			}
  		} else {
  			for (i = 0; i < n_currents; i++) {
  				if (min_uA <= curr_table[i] &&
  				    curr_table[i] <= max_uA) {
  					sel = i;
  					break;
  				}
  			}
  		}
  	}
  
  	if (sel < 0)
  		return -EINVAL;
  
  	sel <<= ffs(rdev->desc->csel_mask) - 1;
  
  	return max8998_update_reg(i2c, rdev->desc->csel_reg,
  				  sel, rdev->desc->csel_mask);
  }
36f69fa96   Lee Jones   regulator: max899...
403
  static int max8998_get_current_limit(struct regulator_dev *rdev)
4ffea5e08   Jonathan Bakker   regulator: max899...
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  {
  	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  	struct i2c_client *i2c = max8998->iodev->i2c;
  	u8 val;
  	int ret;
  
  	ret = max8998_read_reg(i2c, rdev->desc->csel_reg, &val);
  	if (ret != 0)
  		return ret;
  
  	val &= rdev->desc->csel_mask;
  	val >>= ffs(rdev->desc->csel_mask) - 1;
  
  	if (rdev->desc->curr_table) {
  		if (val >= rdev->desc->n_current_limits)
  			return -EINVAL;
  
  		return rdev->desc->curr_table[val];
  	}
  
  	return -EINVAL;
  }
7d695f20c   Axel Lin   regulator: max899...
426
  static const struct regulator_ops max8998_ldo_ops = {
9a0fbb627   Axel Lin   regulator: max899...
427
  	.list_voltage		= regulator_list_voltage_linear,
baae019ef   Axel Lin   regulator: max899...
428
  	.map_voltage		= regulator_map_voltage_linear,
156f25285   Kyungmin Park   drivers: regulato...
429
430
431
  	.is_enabled		= max8998_ldo_is_enabled,
  	.enable			= max8998_ldo_enable,
  	.disable		= max8998_ldo_disable,
7b4354b46   Axel Lin   regulator: Conver...
432
  	.get_voltage_sel	= max8998_get_voltage_sel,
baae019ef   Axel Lin   regulator: max899...
433
  	.set_voltage_sel	= max8998_set_voltage_ldo_sel,
156f25285   Kyungmin Park   drivers: regulato...
434
  };
7d695f20c   Axel Lin   regulator: max899...
435
  static const struct regulator_ops max8998_buck_ops = {
9a0fbb627   Axel Lin   regulator: max899...
436
  	.list_voltage		= regulator_list_voltage_linear,
baae019ef   Axel Lin   regulator: max899...
437
  	.map_voltage		= regulator_map_voltage_linear,
156f25285   Kyungmin Park   drivers: regulato...
438
439
440
  	.is_enabled		= max8998_ldo_is_enabled,
  	.enable			= max8998_ldo_enable,
  	.disable		= max8998_ldo_disable,
7b4354b46   Axel Lin   regulator: Conver...
441
  	.get_voltage_sel	= max8998_get_voltage_sel,
baae019ef   Axel Lin   regulator: max899...
442
  	.set_voltage_sel	= max8998_set_voltage_buck_sel,
276909d35   Axel Lin   regulator: Conver...
443
  	.set_voltage_time_sel	= max8998_set_voltage_buck_time_sel,
156f25285   Kyungmin Park   drivers: regulato...
444
  };
4ffea5e08   Jonathan Bakker   regulator: max899...
445
446
447
448
449
450
451
452
  static const struct regulator_ops max8998_charger_ops = {
  	.set_current_limit	= max8998_set_current_limit,
  	.get_current_limit	= max8998_get_current_limit,
  	.is_enabled		= max8998_ldo_is_enabled_inverted,
  	/* Swapped as register is inverted */
  	.enable			= max8998_ldo_disable,
  	.disable		= max8998_ldo_enable,
  };
7d695f20c   Axel Lin   regulator: max899...
453
  static const struct regulator_ops max8998_others_ops = {
156f25285   Kyungmin Park   drivers: regulato...
454
455
456
  	.is_enabled		= max8998_ldo_is_enabled,
  	.enable			= max8998_ldo_enable,
  	.disable		= max8998_ldo_disable,
156f25285   Kyungmin Park   drivers: regulato...
457
  };
0dceab333   Axel Lin   regulator: max899...
458
459
460
461
462
463
464
465
466
467
  #define MAX8998_LINEAR_REG(_name, _ops, _min, _step, _max) \
  	{ \
  		.name = #_name, \
  		.id = MAX8998_##_name, \
  		.ops = _ops, \
  		.min_uV = (_min), \
  		.uV_step = (_step), \
  		.n_voltages = ((_max) - (_min)) / (_step) + 1, \
  		.type = REGULATOR_VOLTAGE, \
  		.owner = THIS_MODULE, \
156f25285   Kyungmin Park   drivers: regulato...
468
  	}
0dceab333   Axel Lin   regulator: max899...
469

4ffea5e08   Jonathan Bakker   regulator: max899...
470
471
472
473
474
475
476
477
478
479
480
481
  #define MAX8998_CURRENT_REG(_name, _ops, _table, _reg, _mask) \
  	{ \
  		.name = #_name, \
  		.id = MAX8998_##_name, \
  		.ops = _ops, \
  		.curr_table = _table, \
  		.n_current_limits = ARRAY_SIZE(_table), \
  		.csel_reg = _reg, \
  		.csel_mask = _mask, \
  		.type = REGULATOR_CURRENT, \
  		.owner = THIS_MODULE, \
  	}
0dceab333   Axel Lin   regulator: max899...
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  #define MAX8998_OTHERS_REG(_name, _id) \
  	{ \
  		.name = #_name, \
  		.id = _id, \
  		.ops = &max8998_others_ops, \
  		.type = REGULATOR_VOLTAGE, \
  		.owner = THIS_MODULE, \
  	}
  
  static const struct regulator_desc regulators[] = {
  	MAX8998_LINEAR_REG(LDO2, &max8998_ldo_ops, 800000, 50000, 1300000),
  	MAX8998_LINEAR_REG(LDO3, &max8998_ldo_ops, 800000, 50000, 1300000),
  	MAX8998_LINEAR_REG(LDO4, &max8998_ldo_ops, 1600000, 100000, 3600000),
  	MAX8998_LINEAR_REG(LDO5, &max8998_ldo_ops, 1600000, 100000, 3600000),
  	MAX8998_LINEAR_REG(LDO6, &max8998_ldo_ops, 1600000, 100000, 3600000),
  	MAX8998_LINEAR_REG(LDO7, &max8998_ldo_ops, 1600000, 100000, 3600000),
  	MAX8998_LINEAR_REG(LDO8, &max8998_ldo_ops, 3000000, 100000, 3600000),
  	MAX8998_LINEAR_REG(LDO9, &max8998_ldo_ops, 2800000, 100000, 3100000),
  	MAX8998_LINEAR_REG(LDO10, &max8998_ldo_ops, 950000, 50000, 1300000),
  	MAX8998_LINEAR_REG(LDO11, &max8998_ldo_ops, 1600000, 100000, 3600000),
  	MAX8998_LINEAR_REG(LDO12, &max8998_ldo_ops, 800000, 100000, 3300000),
  	MAX8998_LINEAR_REG(LDO13, &max8998_ldo_ops, 800000, 100000, 3300000),
  	MAX8998_LINEAR_REG(LDO14, &max8998_ldo_ops, 1200000, 100000, 3300000),
  	MAX8998_LINEAR_REG(LDO15, &max8998_ldo_ops, 1200000, 100000, 3300000),
  	MAX8998_LINEAR_REG(LDO16, &max8998_ldo_ops, 1600000, 100000, 3600000),
  	MAX8998_LINEAR_REG(LDO17, &max8998_ldo_ops, 1600000, 100000, 3600000),
  	MAX8998_LINEAR_REG(BUCK1, &max8998_buck_ops, 750000, 25000, 1525000),
  	MAX8998_LINEAR_REG(BUCK2, &max8998_buck_ops, 750000, 25000, 1525000),
  	MAX8998_LINEAR_REG(BUCK3, &max8998_buck_ops, 1600000, 100000, 3600000),
  	MAX8998_LINEAR_REG(BUCK4, &max8998_buck_ops, 800000, 100000, 2300000),
  	MAX8998_OTHERS_REG(EN32KHz-AP, MAX8998_EN32KHZ_AP),
  	MAX8998_OTHERS_REG(EN32KHz-CP, MAX8998_EN32KHZ_CP),
  	MAX8998_OTHERS_REG(ENVICHG, MAX8998_ENVICHG),
  	MAX8998_OTHERS_REG(ESAFEOUT1, MAX8998_ESAFEOUT1),
  	MAX8998_OTHERS_REG(ESAFEOUT2, MAX8998_ESAFEOUT2),
4ffea5e08   Jonathan Bakker   regulator: max899...
517
518
  	MAX8998_CURRENT_REG(CHARGER, &max8998_charger_ops,
  			    charger_current_table, MAX8998_REG_CHGR1, 0x7),
156f25285   Kyungmin Park   drivers: regulato...
519
  };
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
520
521
522
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
  static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
  			struct max8998_platform_data *pdata,
  			struct device_node *pmic_np)
  {
  	int gpio;
  
  	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
  	if (!gpio_is_valid(gpio)) {
  		dev_err(iodev->dev, "invalid buck1 gpio[0]: %d
  ", gpio);
  		return -EINVAL;
  	}
  	pdata->buck1_set1 = gpio;
  
  	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
  	if (!gpio_is_valid(gpio)) {
  		dev_err(iodev->dev, "invalid buck1 gpio[1]: %d
  ", gpio);
  		return -EINVAL;
  	}
  	pdata->buck1_set2 = gpio;
  
  	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
  	if (!gpio_is_valid(gpio)) {
  		dev_err(iodev->dev, "invalid buck 2 gpio: %d
  ", gpio);
  		return -EINVAL;
  	}
  	pdata->buck2_set3 = gpio;
  
  	return 0;
  }
  
  static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
  					struct max8998_platform_data *pdata)
  {
  	struct device_node *pmic_np = iodev->dev->of_node;
  	struct device_node *regulators_np, *reg_np;
  	struct max8998_regulator_data *rdata;
  	unsigned int i;
  	int ret;
  
  	regulators_np = of_get_child_by_name(pmic_np, "regulators");
  	if (!regulators_np) {
  		dev_err(iodev->dev, "could not find regulators sub-node
  ");
  		return -EINVAL;
  	}
  
  	/* count the number of regulators to be supported in pmic */
  	pdata->num_regulators = of_get_child_count(regulators_np);
a86854d0c   Kees Cook   treewide: devm_kz...
571
572
573
  	rdata = devm_kcalloc(iodev->dev,
  			     pdata->num_regulators, sizeof(*rdata),
  			     GFP_KERNEL);
58c953779   Sachin Kamat   regulator: max899...
574
575
  	if (!rdata) {
  		of_node_put(regulators_np);
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
576
  		return -ENOMEM;
58c953779   Sachin Kamat   regulator: max899...
577
  	}
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
578
579
580
581
582
583
584
585
586
  
  	pdata->regulators = rdata;
  	for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
  		reg_np = of_get_child_by_name(regulators_np,
  							regulators[i].name);
  		if (!reg_np)
  			continue;
  
  		rdata->id = regulators[i].id;
072e78b12   Javier Martinez Canillas   regulator: of: Ad...
587
588
589
  		rdata->initdata = of_get_regulator_init_data(iodev->dev,
  							     reg_np,
  							     &regulators[i]);
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
590
591
592
593
  		rdata->reg_node = reg_np;
  		++rdata;
  	}
  	pdata->num_regulators = rdata - pdata->regulators;
58c953779   Sachin Kamat   regulator: max899...
594
595
  	of_node_put(reg_np);
  	of_node_put(regulators_np);
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
  	ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
  	if (ret)
  		return -EINVAL;
  
  	if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
  		pdata->buck_voltage_lock = true;
  
  	ret = of_property_read_u32(pmic_np,
  					"max8998,pmic-buck1-default-dvs-idx",
  					&pdata->buck1_default_idx);
  	if (!ret && pdata->buck1_default_idx >= 4) {
  		pdata->buck1_default_idx = 0;
  		dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead
  ");
  	}
  
  	ret = of_property_read_u32(pmic_np,
  					"max8998,pmic-buck2-default-dvs-idx",
  					&pdata->buck2_default_idx);
  	if (!ret && pdata->buck2_default_idx >= 2) {
  		pdata->buck2_default_idx = 0;
  		dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead
  ");
  	}
  
  	ret = of_property_read_u32_array(pmic_np,
  					"max8998,pmic-buck1-dvs-voltage",
  					pdata->buck1_voltage,
  					ARRAY_SIZE(pdata->buck1_voltage));
  	if (ret) {
  		dev_err(iodev->dev, "buck1 voltages not specified
  ");
  		return -EINVAL;
  	}
  
  	ret = of_property_read_u32_array(pmic_np,
  					"max8998,pmic-buck2-dvs-voltage",
  					pdata->buck2_voltage,
  					ARRAY_SIZE(pdata->buck2_voltage));
  	if (ret) {
  		dev_err(iodev->dev, "buck2 voltages not specified
  ");
  		return -EINVAL;
  	}
  
  	return 0;
  }
a5023574d   Bill Pemberton   regulator: remove...
643
  static int max8998_pmic_probe(struct platform_device *pdev)
156f25285   Kyungmin Park   drivers: regulato...
644
645
  {
  	struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
646
  	struct max8998_platform_data *pdata = iodev->pdata;
c172708d3   Mark Brown   regulator: core: ...
647
  	struct regulator_config config = { };
8a221df69   Axel Lin   regulator: max899...
648
  	struct regulator_dev *rdev;
156f25285   Kyungmin Park   drivers: regulato...
649
  	struct max8998_data *max8998;
50f19a459   Lukasz Majewski   regulator: max899...
650
  	struct i2c_client *i2c;
8a221df69   Axel Lin   regulator: max899...
651
  	int i, ret;
4280e0b42   Tomasz Figa   regulator: max899...
652
  	unsigned int v;
156f25285   Kyungmin Park   drivers: regulato...
653
654
655
656
657
658
  
  	if (!pdata) {
  		dev_err(pdev->dev.parent, "No platform init data supplied
  ");
  		return -ENODEV;
  	}
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
659
660
661
662
663
  	if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
  		ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
  		if (ret)
  			return ret;
  	}
0f80ea149   Axel Lin   regulator: max899...
664
665
  	max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
  			       GFP_KERNEL);
156f25285   Kyungmin Park   drivers: regulato...
666
667
  	if (!max8998)
  		return -ENOMEM;
747cc851d   Axel Lin   regulator: set ma...
668
  	max8998->dev = &pdev->dev;
156f25285   Kyungmin Park   drivers: regulato...
669
  	max8998->iodev = iodev;
c356cbc2d   Axel Lin   regulator: max899...
670
  	max8998->num_regulators = pdata->num_regulators;
156f25285   Kyungmin Park   drivers: regulato...
671
  	platform_set_drvdata(pdev, max8998);
50f19a459   Lukasz Majewski   regulator: max899...
672
  	i2c = max8998->iodev->i2c;
735a3d9ef   MyungJoo Ham   regulator: Suppor...
673
674
  	max8998->buck1_idx = pdata->buck1_default_idx;
  	max8998->buck2_idx = pdata->buck2_default_idx;
50f19a459   Lukasz Majewski   regulator: max899...
675
676
677
678
679
680
681
682
683
  	/* NOTE: */
  	/* For unused GPIO NOT marked as -1 (thereof equal to 0)  WARN_ON */
  	/* will be displayed */
  
  	/* Check if MAX8998 voltage selection GPIOs are defined */
  	if (gpio_is_valid(pdata->buck1_set1) &&
  	    gpio_is_valid(pdata->buck1_set2)) {
  		/* Check if SET1 is not equal to 0 */
  		if (!pdata->buck1_set1) {
9df19a559   Thiago Farina   regulators: max89...
684
685
686
  			dev_err(&pdev->dev,
  				"MAX8998 SET1 GPIO defined as 0 !
  ");
50f19a459   Lukasz Majewski   regulator: max899...
687
  			WARN_ON(!pdata->buck1_set1);
8d491bf42   Sachin Kamat   regulator: max899...
688
  			return -EIO;
50f19a459   Lukasz Majewski   regulator: max899...
689
690
691
  		}
  		/* Check if SET2 is not equal to 0 */
  		if (!pdata->buck1_set2) {
9df19a559   Thiago Farina   regulators: max89...
692
693
694
  			dev_err(&pdev->dev,
  				"MAX8998 SET2 GPIO defined as 0 !
  ");
50f19a459   Lukasz Majewski   regulator: max899...
695
  			WARN_ON(!pdata->buck1_set2);
8d491bf42   Sachin Kamat   regulator: max899...
696
  			return -EIO;
50f19a459   Lukasz Majewski   regulator: max899...
697
698
699
700
701
702
703
704
705
706
  		}
  
  		gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
  		gpio_direction_output(pdata->buck1_set1,
  				      max8998->buck1_idx & 0x1);
  
  
  		gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
  		gpio_direction_output(pdata->buck1_set2,
  				      (max8998->buck1_idx >> 1) & 0x1);
50f19a459   Lukasz Majewski   regulator: max899...
707

4280e0b42   Tomasz Figa   regulator: max899...
708
709
  		/* Set predefined values for BUCK1 registers */
  		for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
0dceab333   Axel Lin   regulator: max899...
710
  			int index = MAX8998_BUCK1 - MAX8998_LDO2;
4280e0b42   Tomasz Figa   regulator: max899...
711
  			i = 0;
0dceab333   Axel Lin   regulator: max899...
712
713
  			while (regulators[index].min_uV +
  			       regulators[index].uV_step * i
4280e0b42   Tomasz Figa   regulator: max899...
714
715
716
717
718
719
720
  			       < pdata->buck1_voltage[v])
  				i++;
  
  			max8998->buck1_vol[v] = i;
  			ret = max8998_write_reg(i2c,
  					MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
  			if (ret)
8d491bf42   Sachin Kamat   regulator: max899...
721
  				return ret;
4280e0b42   Tomasz Figa   regulator: max899...
722
  		}
50f19a459   Lukasz Majewski   regulator: max899...
723
724
725
726
727
  	}
  
  	if (gpio_is_valid(pdata->buck2_set3)) {
  		/* Check if SET3 is not equal to 0 */
  		if (!pdata->buck2_set3) {
9df19a559   Thiago Farina   regulators: max89...
728
729
730
  			dev_err(&pdev->dev,
  				"MAX8998 SET3 GPIO defined as 0 !
  ");
50f19a459   Lukasz Majewski   regulator: max899...
731
  			WARN_ON(!pdata->buck2_set3);
8d491bf42   Sachin Kamat   regulator: max899...
732
  			return -EIO;
50f19a459   Lukasz Majewski   regulator: max899...
733
734
735
736
  		}
  		gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
  		gpio_direction_output(pdata->buck2_set3,
  				      max8998->buck2_idx & 0x1);
4280e0b42   Tomasz Figa   regulator: max899...
737
738
  		/* Set predefined values for BUCK2 registers */
  		for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
0dceab333   Axel Lin   regulator: max899...
739
  			int index = MAX8998_BUCK2 - MAX8998_LDO2;
4280e0b42   Tomasz Figa   regulator: max899...
740
  			i = 0;
0dceab333   Axel Lin   regulator: max899...
741
742
  			while (regulators[index].min_uV +
  			       regulators[index].uV_step * i
4280e0b42   Tomasz Figa   regulator: max899...
743
744
745
746
747
748
749
  			       < pdata->buck2_voltage[v])
  				i++;
  
  			max8998->buck2_vol[v] = i;
  			ret = max8998_write_reg(i2c,
  					MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
  			if (ret)
8d491bf42   Sachin Kamat   regulator: max899...
750
  				return ret;
4280e0b42   Tomasz Figa   regulator: max899...
751
  		}
50f19a459   Lukasz Majewski   regulator: max899...
752
  	}
156f25285   Kyungmin Park   drivers: regulato...
753
754
  
  	for (i = 0; i < pdata->num_regulators; i++) {
0dceab333   Axel Lin   regulator: max899...
755
  		int index = pdata->regulators[i].id - MAX8998_LDO2;
c172708d3   Mark Brown   regulator: core: ...
756
757
  
  		config.dev = max8998->dev;
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
758
  		config.of_node = pdata->regulators[i].reg_node;
c172708d3   Mark Brown   regulator: core: ...
759
760
  		config.init_data = pdata->regulators[i].initdata;
  		config.driver_data = max8998;
8a221df69   Axel Lin   regulator: max899...
761
762
763
764
  		rdev = devm_regulator_register(&pdev->dev, &regulators[index],
  					       &config);
  		if (IS_ERR(rdev)) {
  			ret = PTR_ERR(rdev);
ee999fb3f   Tomasz Figa   mfd: max8998: Add...
765
766
767
  			dev_err(max8998->dev, "regulator %s init failed (%d)
  ",
  						regulators[index].name, ret);
8d491bf42   Sachin Kamat   regulator: max899...
768
  			return ret;
156f25285   Kyungmin Park   drivers: regulato...
769
770
  		}
  	}
156f25285   Kyungmin Park   drivers: regulato...
771
  	return 0;
156f25285   Kyungmin Park   drivers: regulato...
772
  }
337ce5d1c   MyungJoo Ham   mfd: Support LP39...
773
774
775
776
777
  static const struct platform_device_id max8998_pmic_id[] = {
  	{ "max8998-pmic", TYPE_MAX8998 },
  	{ "lp3974-pmic", TYPE_LP3974 },
  	{ }
  };
a51b907b2   Axel Lin   regulator: Add MO...
778
  MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
337ce5d1c   MyungJoo Ham   mfd: Support LP39...
779

156f25285   Kyungmin Park   drivers: regulato...
780
781
782
  static struct platform_driver max8998_pmic_driver = {
  	.driver = {
  		.name = "max8998-pmic",
156f25285   Kyungmin Park   drivers: regulato...
783
784
  	},
  	.probe = max8998_pmic_probe,
337ce5d1c   MyungJoo Ham   mfd: Support LP39...
785
  	.id_table = max8998_pmic_id,
156f25285   Kyungmin Park   drivers: regulato...
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
  };
  
  static int __init max8998_pmic_init(void)
  {
  	return platform_driver_register(&max8998_pmic_driver);
  }
  subsys_initcall(max8998_pmic_init);
  
  static void __exit max8998_pmic_cleanup(void)
  {
  	platform_driver_unregister(&max8998_pmic_driver);
  }
  module_exit(max8998_pmic_cleanup);
  
  MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
  MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  MODULE_LICENSE("GPL");