summaryrefslogtreecommitdiff
path: root/drivers/regulator/max5970-regulator.c
blob: bc88a40a88d4cac0bbe61efc4725703b1136654e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
68
69
70
71
72
73
74
75
76
77
78
79
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
118
119
120
121
122
123
124
125
126
127
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
403
404
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
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
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
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
643
644
645
646
647
648
649
650
651
652
653
654
655
// SPDX-License-Identifier: GPL-2.0
/*
 * Device driver for regulators in MAX5970 and MAX5978 IC
 *
 * Copyright (c) 2022 9elements GmbH
 *
 * Author: Patrick Rudolph <patrick.rudolph@9elements.com>
 */

#include <linux/bitops.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/of_regulator.h>
#include <linux/platform_device.h>

#include <linux/mfd/max5970.h>

struct max5970_regulator {
	int num_switches, mon_rng, irng, shunt_micro_ohms, lim_uA;
	struct regmap *regmap;
};

enum max597x_regulator_id {
	MAX597X_SW0,
	MAX597X_SW1,
};

static int max5970_read_adc(struct regmap *regmap, int reg, long *val)
{
	u8 reg_data[2];
	int ret;

	ret = regmap_bulk_read(regmap, reg, &reg_data[0], 2);
	if (ret < 0)
		return ret;

	*val = (reg_data[0] << 2) | (reg_data[1] & 3);

	return 0;
}

static int max5970_read(struct device *dev, enum hwmon_sensor_types type,
			u32 attr, int channel, long *val)
{
	struct regulator_dev **rdevs = dev_get_drvdata(dev);
	struct max5970_regulator *ddata = rdev_get_drvdata(rdevs[channel]);
	struct regmap *regmap = ddata->regmap;
	int ret;

	switch (type) {
	case hwmon_curr:
		switch (attr) {
		case hwmon_curr_input:
			ret = max5970_read_adc(regmap, MAX5970_REG_CURRENT_H(channel), val);
			if (ret < 0)
				return ret;
			/*
			 * Calculate current from ADC value, IRNG range & shunt resistor value.
			 * ddata->irng holds the voltage corresponding to the maximum value the
			 * 10-bit ADC can measure.
			 * To obtain the output, multiply the ADC value by the IRNG range (in
			 * millivolts) and then divide it by the maximum value of the 10-bit ADC.
			 */
			*val = (*val * ddata->irng) >> 10;
			/* Convert the voltage meansurement across shunt resistor to current */
			*val = (*val * 1000) / ddata->shunt_micro_ohms;
			return 0;
		default:
			return -EOPNOTSUPP;
		}

	case hwmon_in:
		switch (attr) {
		case hwmon_in_input:
			ret = max5970_read_adc(regmap, MAX5970_REG_VOLTAGE_H(channel), val);
			if (ret < 0)
				return ret;
			/*
			 * Calculate voltage from ADC value and MON range.
			 * ddata->mon_rng holds the voltage corresponding to the maximum value the
			 * 10-bit ADC can measure.
			 * To obtain the output, multiply the ADC value by the MON range (in
			 * microvolts) and then divide it by the maximum value of the 10-bit ADC.
			 */
			*val = mul_u64_u32_shr(*val, ddata->mon_rng, 10);
			/* uV to mV */
			*val = *val / 1000;
			return 0;
		default:
			return -EOPNOTSUPP;
		}
	default:
		return -EOPNOTSUPP;
	}
}

static umode_t max5970_is_visible(const void *data,
				  enum hwmon_sensor_types type,
				  u32 attr, int channel)
{
	struct regulator_dev **rdevs = (struct regulator_dev **)data;
	struct max5970_regulator *ddata;

	if (channel >= MAX5970_NUM_SWITCHES || !rdevs[channel])
		return 0;

	ddata = rdev_get_drvdata(rdevs[channel]);

	if (channel >= ddata->num_switches)
		return 0;

	switch (type) {
	case hwmon_in:
		switch (attr) {
		case hwmon_in_input:
			return 0444;
		default:
			break;
		}
		break;
	case hwmon_curr:
		switch (attr) {
		case hwmon_curr_input:
			/* Current measurement requires knowledge of the shunt resistor value. */
			if (ddata->shunt_micro_ohms)
				return 0444;
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}
	return 0;
}

static const struct hwmon_ops max5970_hwmon_ops = {
	.is_visible = max5970_is_visible,
	.read = max5970_read,
};

static const struct hwmon_channel_info *max5970_info[] = {
	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT, HWMON_I_INPUT),
	HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT, HWMON_C_INPUT),
	NULL
};

static const struct hwmon_chip_info max5970_chip_info = {
	.ops = &max5970_hwmon_ops,
	.info = max5970_info,
};

static int max597x_uvp_ovp_check_mode(struct regulator_dev *rdev, int severity)
{
	int ret, reg;

	/* Status1 register contains the soft strap values sampled at POR */
	ret = regmap_read(rdev->regmap, MAX5970_REG_STATUS1, &reg);
	if (ret)
		return ret;

	/* Check soft straps match requested mode */
	if (severity == REGULATOR_SEVERITY_PROT) {
		if (STATUS1_PROT(reg) != STATUS1_PROT_SHUTDOWN)
			return -EOPNOTSUPP;

		return 0;
	}
	if (STATUS1_PROT(reg) == STATUS1_PROT_SHUTDOWN)
		return -EOPNOTSUPP;

	return 0;
}

static int max597x_set_vp(struct regulator_dev *rdev, int lim_uV, int severity,
			  bool enable, bool overvoltage)
{
	int off_h, off_l, reg, ret;
	struct max5970_regulator *data = rdev_get_drvdata(rdev);
	int channel = rdev_get_id(rdev);

	if (overvoltage) {
		if (severity == REGULATOR_SEVERITY_WARN) {
			off_h = MAX5970_REG_CH_OV_WARN_H(channel);
			off_l = MAX5970_REG_CH_OV_WARN_L(channel);
		} else {
			off_h = MAX5970_REG_CH_OV_CRIT_H(channel);
			off_l = MAX5970_REG_CH_OV_CRIT_L(channel);
		}
	} else {
		if (severity == REGULATOR_SEVERITY_WARN) {
			off_h = MAX5970_REG_CH_UV_WARN_H(channel);
			off_l = MAX5970_REG_CH_UV_WARN_L(channel);
		} else {
			off_h = MAX5970_REG_CH_UV_CRIT_H(channel);
			off_l = MAX5970_REG_CH_UV_CRIT_L(channel);
		}
	}

	if (enable)
		/* reg = ADC_MASK * (lim_uV / 1000000) / (data->mon_rng / 1000000) */
		reg = ADC_MASK * lim_uV / data->mon_rng;
	else
		reg = 0;

	ret = regmap_write(rdev->regmap, off_h, MAX5970_VAL2REG_H(reg));
	if (ret)
		return ret;

	ret = regmap_write(rdev->regmap, off_l, MAX5970_VAL2REG_L(reg));
	if (ret)
		return ret;

	return 0;
}

static int max597x_set_uvp(struct regulator_dev *rdev, int lim_uV, int severity,
			   bool enable)
{
	int ret;

	/*
	 * MAX5970 has enable control as a special value in limit reg. Can't
	 * set limit but keep feature disabled or enable W/O given limit.
	 */
	if ((lim_uV && !enable) || (!lim_uV && enable))
		return -EINVAL;

	ret = max597x_uvp_ovp_check_mode(rdev, severity);
	if (ret)
		return ret;

	return max597x_set_vp(rdev, lim_uV, severity, enable, false);
}

static int max597x_set_ovp(struct regulator_dev *rdev, int lim_uV, int severity,
			   bool enable)
{
	int ret;

	/*
	 * MAX5970 has enable control as a special value in limit reg. Can't
	 * set limit but keep feature disabled or enable W/O given limit.
	 */
	if ((lim_uV && !enable) || (!lim_uV && enable))
		return -EINVAL;

	ret = max597x_uvp_ovp_check_mode(rdev, severity);
	if (ret)
		return ret;

	return max597x_set_vp(rdev, lim_uV, severity, enable, true);
}

static int max597x_set_ocp(struct regulator_dev *rdev, int lim_uA,
			   int severity, bool enable)
{
	int val, reg;
	unsigned int vthst, vthfst;

	struct max5970_regulator *data = rdev_get_drvdata(rdev);
	int rdev_id = rdev_get_id(rdev);
	/*
	 * MAX5970 doesn't has enable control for ocp.
	 * If limit is specified but enable is not set then hold the value in
	 * variable & later use it when ocp needs to be enabled.
	 */
	if (lim_uA != 0 && lim_uA != data->lim_uA)
		data->lim_uA = lim_uA;

	if (severity != REGULATOR_SEVERITY_PROT)
		return -EINVAL;

	if (enable) {

		/* Calc Vtrip threshold in uV. */
		vthst =
		    div_u64(mul_u32_u32(data->shunt_micro_ohms, data->lim_uA),
			    1000000);

		/*
		 * As recommended in datasheed, add 20% margin to avoid
		 * spurious event & passive component tolerance.
		 */
		vthst = div_u64(mul_u32_u32(vthst, 120), 100);

		/* Calc fast Vtrip threshold in uV */
		vthfst = vthst * (MAX5970_FAST2SLOW_RATIO / 100);

		if (vthfst > data->irng) {
			dev_err(&rdev->dev, "Current limit out of range\n");
			return -EINVAL;
		}
		/* Fast trip threshold to be programmed */
		val = div_u64(mul_u32_u32(0xFF, vthfst), data->irng);
	} else
		/*
		 * Since there is no option to disable ocp, set limit to max
		 * value
		 */
		val = 0xFF;

	reg = MAX5970_REG_DAC_FAST(rdev_id);

	return regmap_write(rdev->regmap, reg, val);
}

static int max597x_get_status(struct regulator_dev *rdev)
{
	int val, ret;

	ret = regmap_read(rdev->regmap, MAX5970_REG_STATUS3, &val);
	if (ret)
		return ret;

	if (val & MAX5970_STATUS3_ALERT)
		return REGULATOR_STATUS_ERROR;

	ret = regulator_is_enabled_regmap(rdev);
	if (ret < 0)
		return ret;

	if (ret)
		return REGULATOR_STATUS_ON;

	return REGULATOR_STATUS_OFF;
}

static const struct regulator_ops max597x_switch_ops = {
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
	.is_enabled = regulator_is_enabled_regmap,
	.get_status = max597x_get_status,
	.set_over_voltage_protection = max597x_set_ovp,
	.set_under_voltage_protection = max597x_set_uvp,
	.set_over_current_protection = max597x_set_ocp,
};

static int max597x_dt_parse(struct device_node *np,
			    const struct regulator_desc *desc,
			    struct regulator_config *cfg)
{
	struct max5970_regulator *data = cfg->driver_data;
	int ret = 0;

	ret =
	    of_property_read_u32(np, "shunt-resistor-micro-ohms",
				 &data->shunt_micro_ohms);
	if (ret < 0)
		dev_err(cfg->dev,
			"property 'shunt-resistor-micro-ohms' not found, err %d\n",
			ret);
	return ret;

}

#define MAX597X_SWITCH(_ID, _ereg, _chan, _supply) {     \
	.name            = #_ID,                         \
	.of_match        = of_match_ptr(#_ID),           \
	.ops             = &max597x_switch_ops,          \
	.regulators_node = of_match_ptr("regulators"),   \
	.type            = REGULATOR_VOLTAGE,            \
	.id              = MAX597X_##_ID,                \
	.owner           = THIS_MODULE,                  \
	.supply_name     = _supply,                      \
	.enable_reg      = _ereg,                        \
	.enable_mask     = CHXEN((_chan)),               \
	.of_parse_cb	 = max597x_dt_parse,		 \
}

static const struct regulator_desc regulators[] = {
	MAX597X_SWITCH(SW0, MAX5970_REG_CHXEN, 0, "vss1"),
	MAX597X_SWITCH(SW1, MAX5970_REG_CHXEN, 1, "vss2"),
};

static int max597x_regmap_read_clear(struct regmap *map, unsigned int reg,
				     unsigned int *val)
{
	int ret;

	ret = regmap_read(map, reg, val);
	if (ret)
		return ret;

	if (*val)
		return regmap_write(map, reg, *val);

	return 0;
}

static int max597x_irq_handler(int irq, struct regulator_irq_data *rid,
			       unsigned long *dev_mask)
{
	struct regulator_err_state *stat;
	struct max5970_regulator *d = (struct max5970_regulator *)rid->data;
	int val, ret, i;

	ret = max597x_regmap_read_clear(d->regmap, MAX5970_REG_FAULT0, &val);
	if (ret)
		return REGULATOR_FAILED_RETRY;

	*dev_mask = 0;
	for (i = 0; i < d->num_switches; i++) {
		stat = &rid->states[i];
		stat->notifs = 0;
		stat->errors = 0;
	}

	for (i = 0; i < d->num_switches; i++) {
		stat = &rid->states[i];

		if (val & UV_STATUS_CRIT(i)) {
			*dev_mask |= 1 << i;
			stat->notifs |= REGULATOR_EVENT_UNDER_VOLTAGE;
			stat->errors |= REGULATOR_ERROR_UNDER_VOLTAGE;
		} else if (val & UV_STATUS_WARN(i)) {
			*dev_mask |= 1 << i;
			stat->notifs |= REGULATOR_EVENT_UNDER_VOLTAGE_WARN;
			stat->errors |= REGULATOR_ERROR_UNDER_VOLTAGE_WARN;
		}
	}

	ret = max597x_regmap_read_clear(d->regmap, MAX5970_REG_FAULT1, &val);
	if (ret)
		return REGULATOR_FAILED_RETRY;

	for (i = 0; i < d->num_switches; i++) {
		stat = &rid->states[i];

		if (val & OV_STATUS_CRIT(i)) {
			*dev_mask |= 1 << i;
			stat->notifs |= REGULATOR_EVENT_REGULATION_OUT;
			stat->errors |= REGULATOR_ERROR_REGULATION_OUT;
		} else if (val & OV_STATUS_WARN(i)) {
			*dev_mask |= 1 << i;
			stat->notifs |= REGULATOR_EVENT_OVER_VOLTAGE_WARN;
			stat->errors |= REGULATOR_ERROR_OVER_VOLTAGE_WARN;
		}
	}

	ret = max597x_regmap_read_clear(d->regmap, MAX5970_REG_FAULT2, &val);
	if (ret)
		return REGULATOR_FAILED_RETRY;

	for (i = 0; i < d->num_switches; i++) {
		stat = &rid->states[i];

		if (val & OC_STATUS_WARN(i)) {
			*dev_mask |= 1 << i;
			stat->notifs |= REGULATOR_EVENT_OVER_CURRENT_WARN;
			stat->errors |= REGULATOR_ERROR_OVER_CURRENT_WARN;
		}
	}

	ret = regmap_read(d->regmap, MAX5970_REG_STATUS0, &val);
	if (ret)
		return REGULATOR_FAILED_RETRY;

	for (i = 0; i < d->num_switches; i++) {
		stat = &rid->states[i];

		if ((val & MAX5970_CB_IFAULTF(i))
		    || (val & MAX5970_CB_IFAULTS(i))) {
			*dev_mask |= 1 << i;
			stat->notifs |=
			    REGULATOR_EVENT_OVER_CURRENT |
			    REGULATOR_EVENT_DISABLE;
			stat->errors |=
			    REGULATOR_ERROR_OVER_CURRENT | REGULATOR_ERROR_FAIL;

			/* Clear the sub-IRQ status */
			regulator_disable_regmap(stat->rdev);
		}
	}
	return 0;
}

static int max597x_adc_range(struct regmap *regmap, const int ch,
			     u32 *irng, u32 *mon_rng)
{
	unsigned int reg;
	int ret;

	/* Decode current ADC range */
	ret = regmap_read(regmap, MAX5970_REG_STATUS2, &reg);
	if (ret)
		return ret;
	switch (MAX5970_IRNG(reg, ch)) {
	case 0:
		*irng = 100000;	/* 100 mV */
		break;
	case 1:
		*irng = 50000;	/* 50 mV */
		break;
	case 2:
		*irng = 25000;	/* 25 mV */
		break;
	default:
		return -EINVAL;
	}

	/* Decode current voltage monitor range */
	ret = regmap_read(regmap, MAX5970_REG_MON_RANGE, &reg);
	if (ret)
		return ret;

	*mon_rng = MAX5970_MON_MAX_RANGE_UV >> MAX5970_MON(reg, ch);

	return 0;
}

static int max597x_setup_irq(struct device *dev,
			     int irq,
			     struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES],
			     int num_switches, struct max5970_regulator *data)
{
	struct regulator_irq_desc max597x_notif = {
		.name = "max597x-irq",
		.map_event = max597x_irq_handler,
		.data = data,
	};
	int errs = REGULATOR_ERROR_UNDER_VOLTAGE |
	    REGULATOR_ERROR_UNDER_VOLTAGE_WARN |
	    REGULATOR_ERROR_OVER_VOLTAGE_WARN |
	    REGULATOR_ERROR_REGULATION_OUT |
	    REGULATOR_ERROR_OVER_CURRENT |
	    REGULATOR_ERROR_OVER_CURRENT_WARN | REGULATOR_ERROR_FAIL;
	void *irq_helper;

	/* Register notifiers - can fail if IRQ is not given */
	irq_helper = devm_regulator_irq_helper(dev, &max597x_notif,
					       irq, 0, errs, NULL,
					       &rdevs[0], num_switches);
	if (IS_ERR(irq_helper)) {
		if (PTR_ERR(irq_helper) == -EPROBE_DEFER)
			return -EPROBE_DEFER;

		dev_warn(dev, "IRQ disabled %pe\n", irq_helper);
	}

	return 0;
}

static int max597x_regulator_probe(struct platform_device *pdev)
{
	struct max5970_data *max597x;
	struct regmap *regmap = dev_get_regmap(pdev->dev.parent, NULL);
	struct max5970_regulator *data;
	struct i2c_client *i2c = to_i2c_client(pdev->dev.parent);
	struct regulator_config config = { };
	struct regulator_dev *rdev;
	struct regulator_dev **rdevs = NULL;
	struct device *hwmon_dev;
	int num_switches;
	int ret, i;

	if (!regmap)
		return -EPROBE_DEFER;

	max597x = devm_kzalloc(&i2c->dev, sizeof(struct max5970_data), GFP_KERNEL);
	if (!max597x)
		return -ENOMEM;

	rdevs = devm_kcalloc(&i2c->dev, MAX5970_NUM_SWITCHES, sizeof(struct regulator_dev *),
			     GFP_KERNEL);
	if (!rdevs)
		return -ENOMEM;

	i2c_set_clientdata(i2c, max597x);

	if (of_device_is_compatible(i2c->dev.of_node, "maxim,max5978"))
		max597x->num_switches = MAX5978_NUM_SWITCHES;
	else if (of_device_is_compatible(i2c->dev.of_node, "maxim,max5970"))
		max597x->num_switches = MAX5970_NUM_SWITCHES;
	else
		return -ENODEV;

	num_switches = max597x->num_switches;

	for (i = 0; i < num_switches; i++) {
		data =
		    devm_kzalloc(&i2c->dev, sizeof(struct max5970_regulator),
				 GFP_KERNEL);
		if (!data)
			return -ENOMEM;

		data->num_switches = num_switches;
		data->regmap = regmap;

		ret = max597x_adc_range(regmap, i, &max597x->irng[i], &max597x->mon_rng[i]);
		if (ret < 0)
			return ret;

		data->irng = max597x->irng[i];
		data->mon_rng = max597x->mon_rng[i];

		config.dev = &i2c->dev;
		config.driver_data = (void *)data;
		config.regmap = data->regmap;
		rdev = devm_regulator_register(&i2c->dev,
					       &regulators[i], &config);
		if (IS_ERR(rdev)) {
			dev_err(&i2c->dev, "failed to register regulator %s\n",
				regulators[i].name);
			return PTR_ERR(rdev);
		}
		rdevs[i] = rdev;
		max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms;
	}

	if (IS_REACHABLE(CONFIG_HWMON)) {
		hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970", rdevs,
								 &max5970_chip_info, NULL);
		if (IS_ERR(hwmon_dev)) {
			return dev_err_probe(&i2c->dev, PTR_ERR(hwmon_dev),
					     "Unable to register hwmon device\n");
		}
	}

	if (i2c->irq) {
		ret =
		    max597x_setup_irq(&i2c->dev, i2c->irq, rdevs, num_switches,
				      data);
		if (ret) {
			dev_err(&i2c->dev, "IRQ setup failed");
			return ret;
		}
	}

	return ret;
}

static struct platform_driver max597x_regulator_driver = {
	.driver = {
		.name = "max5970-regulator",
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
	},
	.probe = max597x_regulator_probe,
};

module_platform_driver(max597x_regulator_driver);


MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>");
MODULE_DESCRIPTION("MAX5970_hot-swap controller driver");
MODULE_LICENSE("GPL v2");