summaryrefslogtreecommitdiffstats
path: root/drivers/regulator/max14577.c
blob: 0ff5a20ac95855a0cb4e0df80cd7f09508759a54 (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
/*
 * max14577.c - Regulator driver for the Maxim 14577/77836
 *
 * Copyright (C) 2013,2014 Samsung Electronics
 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/mfd/max14577.h>
#include <linux/mfd/max14577-private.h>
#include <linux/regulator/of_regulator.h>

static int max14577_reg_is_enabled(struct regulator_dev *rdev)
{
	int rid = rdev_get_id(rdev);
	struct regmap *rmap = rdev->regmap;
	u8 reg_data;

	switch (rid) {
	case MAX14577_CHARGER:
		max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
		if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
			return 0;
		max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
		if ((reg_data & STATUS3_CGMBC_MASK) == 0)
			return 0;
		/* MBCHOSTEN and CGMBC are on */
		return 1;
	default:
		return -EINVAL;
	}
}

static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
{
	u8 reg_data;
	struct regmap *rmap = rdev->regmap;
	struct max14577 *max14577 = rdev_get_drvdata(rdev);
	const struct maxim_charger_current *limits =
		&maxim_charger_currents[max14577->dev_type];

	if (rdev_get_id(rdev) != MAX14577_CHARGER)
		return -EINVAL;

	max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);

	if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
		return limits->min;

	reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
			CHGCTRL4_MBCICHWRCH_SHIFT);
	return limits->high_start + reg_data * limits->high_step;
}

static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
		int min_uA, int max_uA)
{
	u8 reg_data;
	int ret;
	struct max14577 *max14577 = rdev_get_drvdata(rdev);
	const struct maxim_charger_current *limits =
		&maxim_charger_currents[max14577->dev_type];

	if (rdev_get_id(rdev) != MAX14577_CHARGER)
		return -EINVAL;

	ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, &reg_data);
	if (ret)
		return ret;

	return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
			CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
			reg_data);
}

static struct regulator_ops max14577_safeout_ops = {
	.is_enabled		= regulator_is_enabled_regmap,
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
	.list_voltage		= regulator_list_voltage_linear,
};

static struct regulator_ops max14577_charger_ops = {
	.is_enabled		= max14577_reg_is_enabled,
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
	.get_current_limit	= max14577_reg_get_current_limit,
	.set_current_limit	= max14577_reg_set_current_limit,
};

static const struct regulator_desc max14577_supported_regulators[] = {
	[MAX14577_SAFEOUT] = {
		.name		= "SAFEOUT",
		.id		= MAX14577_SAFEOUT,
		.ops		= &max14577_safeout_ops,
		.type		= REGULATOR_VOLTAGE,
		.owner		= THIS_MODULE,
		.n_voltages	= 1,
		.min_uV		= MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
		.enable_reg	= MAX14577_REG_CONTROL2,
		.enable_mask	= CTRL2_SFOUTORD_MASK,
	},
	[MAX14577_CHARGER] = {
		.name		= "CHARGER",
		.id		= MAX14577_CHARGER,
		.ops		= &max14577_charger_ops,
		.type		= REGULATOR_CURRENT,
		.owner		= THIS_MODULE,
		.enable_reg	= MAX14577_CHG_REG_CHG_CTRL2,
		.enable_mask	= CHGCTRL2_MBCHOSTEN_MASK,
	},
};

static struct regulator_ops max77836_ldo_ops = {
	.is_enabled		= regulator_is_enabled_regmap,
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
	.list_voltage		= regulator_list_voltage_linear,
	.map_voltage		= regulator_map_voltage_linear,
	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
	/* TODO: add .set_suspend_mode */
};

static const struct regulator_desc max77836_supported_regulators[] = {
	[MAX14577_SAFEOUT] = {
		.name		= "SAFEOUT",
		.id		= MAX14577_SAFEOUT,
		.ops		= &max14577_safeout_ops,
		.type		= REGULATOR_VOLTAGE,
		.owner		= THIS_MODULE,
		.n_voltages	= 1,
		.min_uV		= MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
		.enable_reg	= MAX14577_REG_CONTROL2,
		.enable_mask	= CTRL2_SFOUTORD_MASK,
	},
	[MAX14577_CHARGER] = {
		.name		= "CHARGER",
		.id		= MAX14577_CHARGER,
		.ops		= &max14577_charger_ops,
		.type		= REGULATOR_CURRENT,
		.owner		= THIS_MODULE,
		.enable_reg	= MAX14577_CHG_REG_CHG_CTRL2,
		.enable_mask	= CHGCTRL2_MBCHOSTEN_MASK,
	},
	[MAX77836_LDO1] = {
		.name		= "LDO1",
		.id		= MAX77836_LDO1,
		.ops		= &max77836_ldo_ops,
		.type		= REGULATOR_VOLTAGE,
		.owner		= THIS_MODULE,
		.n_voltages	= MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM,
		.min_uV		= MAX77836_REGULATOR_LDO_VOLTAGE_MIN,
		.uV_step	= MAX77836_REGULATOR_LDO_VOLTAGE_STEP,
		.enable_reg	= MAX77836_LDO_REG_CNFG1_LDO1,
		.enable_mask	= MAX77836_CNFG1_LDO_PWRMD_MASK,
		.vsel_reg	= MAX77836_LDO_REG_CNFG1_LDO1,
		.vsel_mask	= MAX77836_CNFG1_LDO_TV_MASK,
	},
	[MAX77836_LDO2] = {
		.name		= "LDO2",
		.id		= MAX77836_LDO2,
		.ops		= &max77836_ldo_ops,
		.type		= REGULATOR_VOLTAGE,
		.owner		= THIS_MODULE,
		.n_voltages	= MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM,
		.min_uV		= MAX77836_REGULATOR_LDO_VOLTAGE_MIN,
		.uV_step	= MAX77836_REGULATOR_LDO_VOLTAGE_STEP,
		.enable_reg	= MAX77836_LDO_REG_CNFG1_LDO2,
		.enable_mask	= MAX77836_CNFG1_LDO_PWRMD_MASK,
		.vsel_reg	= MAX77836_LDO_REG_CNFG1_LDO2,
		.vsel_mask	= MAX77836_CNFG1_LDO_TV_MASK,
	},
};

#ifdef CONFIG_OF
static struct of_regulator_match max14577_regulator_matches[] = {
	{ .name	= "SAFEOUT", },
	{ .name = "CHARGER", },
};

static struct of_regulator_match max77836_regulator_matches[] = {
	{ .name	= "SAFEOUT", },
	{ .name = "CHARGER", },
	{ .name = "LDO1", },
	{ .name = "LDO2", },
};

static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev,
		enum maxim_device_type dev_type)
{
	int ret;
	struct device_node *np;
	struct of_regulator_match *regulator_matches;
	unsigned int regulator_matches_size;

	np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
	if (!np) {
		dev_err(&pdev->dev, "Failed to get child OF node for regulators\n");
		return -EINVAL;
	}

	switch (dev_type) {
	case MAXIM_DEVICE_TYPE_MAX77836:
		regulator_matches = max77836_regulator_matches;
		regulator_matches_size = ARRAY_SIZE(max77836_regulator_matches);
		break;
	case MAXIM_DEVICE_TYPE_MAX14577:
	default:
		regulator_matches = max14577_regulator_matches;
		regulator_matches_size = ARRAY_SIZE(max14577_regulator_matches);
	}

	ret = of_regulator_match(&pdev->dev, np, regulator_matches,
			regulator_matches_size);
	if (ret < 0)
		dev_err(&pdev->dev, "Error parsing regulator init data: %d\n", ret);
	else
		ret = 0;

	of_node_put(np);

	return ret;
}

static inline struct regulator_init_data *match_init_data(int index,
		enum maxim_device_type dev_type)
{
	switch (dev_type) {
	case MAXIM_DEVICE_TYPE_MAX77836:
		return max77836_regulator_matches[index].init_data;

	case MAXIM_DEVICE_TYPE_MAX14577:
	default:
		return max14577_regulator_matches[index].init_data;
	}
}

static inline struct device_node *match_of_node(int index,
		enum maxim_device_type dev_type)
{
	switch (dev_type) {
	case MAXIM_DEVICE_TYPE_MAX77836:
		return max77836_regulator_matches[index].of_node;

	case MAXIM_DEVICE_TYPE_MAX14577:
	default:
		return max14577_regulator_matches[index].of_node;
	}
}
#else /* CONFIG_OF */
static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev,
		enum maxim_device_type dev_type)
{
	return 0;
}
static inline struct regulator_init_data *match_init_data(int index,
		enum maxim_device_type dev_type)
{
	return NULL;
}

static inline struct device_node *match_of_node(int index,
		enum maxim_device_type dev_type)
{
	return NULL;
}
#endif /* CONFIG_OF */

/**
 * Registers for regulators of max77836 use different I2C slave addresses so
 * different regmaps must be used for them.
 *
 * Returns proper regmap for accessing regulator passed by id.
 */
static struct regmap *max14577_get_regmap(struct max14577 *max14577,
		int reg_id)
{
	switch (max14577->dev_type) {
	case MAXIM_DEVICE_TYPE_MAX77836:
		switch (reg_id) {
		case MAX77836_SAFEOUT ... MAX77836_CHARGER:
			return max14577->regmap;
		default:
			/* MAX77836_LDO1 ... MAX77836_LDO2 */
			return max14577->regmap_pmic;
		}

	case MAXIM_DEVICE_TYPE_MAX14577:
	default:
		return max14577->regmap;
	}
}

static int max14577_regulator_probe(struct platform_device *pdev)
{
	struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
	struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
	int i, ret;
	struct regulator_config config = {};
	const struct regulator_desc *supported_regulators;
	unsigned int supported_regulators_size;
	enum maxim_device_type dev_type = max14577->dev_type;

	ret = max14577_regulator_dt_parse_pdata(pdev, dev_type);
	if (ret)
		return ret;

	switch (dev_type) {
	case MAXIM_DEVICE_TYPE_MAX77836:
		supported_regulators = max77836_supported_regulators;
		supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators);
		break;
	case MAXIM_DEVICE_TYPE_MAX14577:
	default:
		supported_regulators = max14577_supported_regulators;
		supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators);
	}

	config.dev = &pdev->dev;
	config.driver_data = max14577;

	for (i = 0; i < supported_regulators_size; i++) {
		struct regulator_dev *regulator;
		/*
		 * Index of supported_regulators[] is also the id and must
		 * match index of pdata->regulators[].
		 */
		if (pdata && pdata->regulators) {
			config.init_data = pdata->regulators[i].initdata;
			config.of_node = pdata->regulators[i].of_node;
		} else {
			config.init_data = match_init_data(i, dev_type);
			config.of_node = match_of_node(i, dev_type);
		}
		config.regmap = max14577_get_regmap(max14577,
				supported_regulators[i].id);

		regulator = devm_regulator_register(&pdev->dev,
				&supported_regulators[i], &config);
		if (IS_ERR(regulator)) {
			ret = PTR_ERR(regulator);
			dev_err(&pdev->dev,
					"Regulator init failed for %d/%s with error: %d\n",
					i, supported_regulators[i].name, ret);
			return ret;
		}
	}

	return ret;
}

static const struct platform_device_id max14577_regulator_id[] = {
	{ "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, },
	{ "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, },
	{ }
};
MODULE_DEVICE_TABLE(platform, max14577_regulator_id);

static struct platform_driver max14577_regulator_driver = {
	.driver = {
		   .owner = THIS_MODULE,
		   .name = "max14577-regulator",
		   },
	.probe		= max14577_regulator_probe,
	.id_table	= max14577_regulator_id,
};

static int __init max14577_regulator_init(void)
{
	BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM);
	BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM);

	BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN +
			(MAX77836_REGULATOR_LDO_VOLTAGE_STEP *
			  (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) !=
			MAX77836_REGULATOR_LDO_VOLTAGE_MAX);

	return platform_driver_register(&max14577_regulator_driver);
}
subsys_initcall(max14577_regulator_init);

static void __exit max14577_regulator_exit(void)
{
	platform_driver_unregister(&max14577_regulator_driver);
}
module_exit(max14577_regulator_exit);

MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:max14577-regulator");
OpenPOWER on IntegriCloud