xref: /openbmc/linux/drivers/regulator/max8649.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a71b797fSHaojian Zhuang /*
3a71b797fSHaojian Zhuang  * Regulators driver for Maxim max8649
4a71b797fSHaojian Zhuang  *
5a71b797fSHaojian Zhuang  * Copyright (C) 2009-2010 Marvell International Ltd.
6a71b797fSHaojian Zhuang  *      Haojian Zhuang <haojian.zhuang@marvell.com>
7a71b797fSHaojian Zhuang  */
8a71b797fSHaojian Zhuang #include <linux/kernel.h>
9a71b797fSHaojian Zhuang #include <linux/module.h>
10a71b797fSHaojian Zhuang #include <linux/err.h>
11a71b797fSHaojian Zhuang #include <linux/i2c.h>
12a71b797fSHaojian Zhuang #include <linux/platform_device.h>
13a71b797fSHaojian Zhuang #include <linux/regulator/driver.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15a71b797fSHaojian Zhuang #include <linux/regulator/max8649.h>
16c5b68d47SJonghwan Choi #include <linux/regmap.h>
17a71b797fSHaojian Zhuang 
18a71b797fSHaojian Zhuang #define MAX8649_DCDC_VMIN	750000		/* uV */
19a71b797fSHaojian Zhuang #define MAX8649_DCDC_VMAX	1380000		/* uV */
20a71b797fSHaojian Zhuang #define MAX8649_DCDC_STEP	10000		/* uV */
21a71b797fSHaojian Zhuang #define MAX8649_VOL_MASK	0x3f
22a71b797fSHaojian Zhuang 
23a71b797fSHaojian Zhuang /* Registers */
24a71b797fSHaojian Zhuang #define MAX8649_MODE0		0x00
25a71b797fSHaojian Zhuang #define MAX8649_MODE1		0x01
26a71b797fSHaojian Zhuang #define MAX8649_MODE2		0x02
27a71b797fSHaojian Zhuang #define MAX8649_MODE3		0x03
28a71b797fSHaojian Zhuang #define MAX8649_CONTROL		0x04
29a71b797fSHaojian Zhuang #define MAX8649_SYNC		0x05
30a71b797fSHaojian Zhuang #define MAX8649_RAMP		0x06
31a71b797fSHaojian Zhuang #define MAX8649_CHIP_ID1	0x08
32a71b797fSHaojian Zhuang #define MAX8649_CHIP_ID2	0x09
33a71b797fSHaojian Zhuang 
34a71b797fSHaojian Zhuang /* Bits */
35a71b797fSHaojian Zhuang #define MAX8649_EN_PD		(1 << 7)
36a71b797fSHaojian Zhuang #define MAX8649_VID0_PD		(1 << 6)
37a71b797fSHaojian Zhuang #define MAX8649_VID1_PD		(1 << 5)
38a71b797fSHaojian Zhuang #define MAX8649_VID_MASK	(3 << 5)
39a71b797fSHaojian Zhuang 
40a71b797fSHaojian Zhuang #define MAX8649_FORCE_PWM	(1 << 7)
41a71b797fSHaojian Zhuang #define MAX8649_SYNC_EXTCLK	(1 << 6)
42a71b797fSHaojian Zhuang 
43a71b797fSHaojian Zhuang #define MAX8649_EXT_MASK	(3 << 6)
44a71b797fSHaojian Zhuang 
45a71b797fSHaojian Zhuang #define MAX8649_RAMP_MASK	(7 << 5)
46a71b797fSHaojian Zhuang #define MAX8649_RAMP_DOWN	(1 << 1)
47a71b797fSHaojian Zhuang 
48a71b797fSHaojian Zhuang struct max8649_regulator_info {
49a71b797fSHaojian Zhuang 	struct device		*dev;
50c5b68d47SJonghwan Choi 	struct regmap		*regmap;
51a71b797fSHaojian Zhuang 
52a71b797fSHaojian Zhuang 	unsigned	mode:2;	/* bit[1:0] = VID1, VID0 */
53a71b797fSHaojian Zhuang 	unsigned	extclk_freq:2;
54a71b797fSHaojian Zhuang 	unsigned	extclk:1;
55a71b797fSHaojian Zhuang 	unsigned	ramp_timing:3;
56a71b797fSHaojian Zhuang 	unsigned	ramp_down:1;
57a71b797fSHaojian Zhuang };
58a71b797fSHaojian Zhuang 
max8649_enable_time(struct regulator_dev * rdev)59a71b797fSHaojian Zhuang static int max8649_enable_time(struct regulator_dev *rdev)
60a71b797fSHaojian Zhuang {
61a71b797fSHaojian Zhuang 	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
62a71b797fSHaojian Zhuang 	int voltage, rate, ret;
63c5b68d47SJonghwan Choi 	unsigned int val;
64a71b797fSHaojian Zhuang 
65a71b797fSHaojian Zhuang 	/* get voltage */
66817d8c2dSAxel Lin 	ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val);
67c5b68d47SJonghwan Choi 	if (ret != 0)
68a71b797fSHaojian Zhuang 		return ret;
69c5b68d47SJonghwan Choi 	val &= MAX8649_VOL_MASK;
700fb737b5SAxel Lin 	voltage = regulator_list_voltage_linear(rdev, (unsigned char)val);
71a71b797fSHaojian Zhuang 
72a71b797fSHaojian Zhuang 	/* get rate */
73c5b68d47SJonghwan Choi 	ret = regmap_read(info->regmap, MAX8649_RAMP, &val);
74c5b68d47SJonghwan Choi 	if (ret != 0)
75a71b797fSHaojian Zhuang 		return ret;
76c5b68d47SJonghwan Choi 	ret = (val & MAX8649_RAMP_MASK) >> 5;
77a71b797fSHaojian Zhuang 	rate = (32 * 1000) >> ret;	/* uV/uS */
78a71b797fSHaojian Zhuang 
79e69c4997SAxel Lin 	return DIV_ROUND_UP(voltage, rate);
80a71b797fSHaojian Zhuang }
81a71b797fSHaojian Zhuang 
max8649_set_mode(struct regulator_dev * rdev,unsigned int mode)82a71b797fSHaojian Zhuang static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode)
83a71b797fSHaojian Zhuang {
84a71b797fSHaojian Zhuang 	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
85a71b797fSHaojian Zhuang 
86a71b797fSHaojian Zhuang 	switch (mode) {
87a71b797fSHaojian Zhuang 	case REGULATOR_MODE_FAST:
88817d8c2dSAxel Lin 		regmap_update_bits(info->regmap, rdev->desc->vsel_reg,
89817d8c2dSAxel Lin 				   MAX8649_FORCE_PWM, MAX8649_FORCE_PWM);
90a71b797fSHaojian Zhuang 		break;
91a71b797fSHaojian Zhuang 	case REGULATOR_MODE_NORMAL:
92817d8c2dSAxel Lin 		regmap_update_bits(info->regmap, rdev->desc->vsel_reg,
93a71b797fSHaojian Zhuang 				   MAX8649_FORCE_PWM, 0);
94a71b797fSHaojian Zhuang 		break;
95a71b797fSHaojian Zhuang 	default:
96a71b797fSHaojian Zhuang 		return -EINVAL;
97a71b797fSHaojian Zhuang 	}
98a71b797fSHaojian Zhuang 	return 0;
99a71b797fSHaojian Zhuang }
100a71b797fSHaojian Zhuang 
max8649_get_mode(struct regulator_dev * rdev)101a71b797fSHaojian Zhuang static unsigned int max8649_get_mode(struct regulator_dev *rdev)
102a71b797fSHaojian Zhuang {
103a71b797fSHaojian Zhuang 	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
104c5b68d47SJonghwan Choi 	unsigned int val;
105a71b797fSHaojian Zhuang 	int ret;
106a71b797fSHaojian Zhuang 
107817d8c2dSAxel Lin 	ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val);
108c5b68d47SJonghwan Choi 	if (ret != 0)
109c5b68d47SJonghwan Choi 		return ret;
110c5b68d47SJonghwan Choi 	if (val & MAX8649_FORCE_PWM)
111a71b797fSHaojian Zhuang 		return REGULATOR_MODE_FAST;
112a71b797fSHaojian Zhuang 	return REGULATOR_MODE_NORMAL;
113a71b797fSHaojian Zhuang }
114a71b797fSHaojian Zhuang 
115035f3324SKrzysztof Kozlowski static const struct regulator_ops max8649_dcdc_ops = {
11640baeceeSAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
117817d8c2dSAxel Lin 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
1180fb737b5SAxel Lin 	.list_voltage	= regulator_list_voltage_linear,
11940baeceeSAxel Lin 	.map_voltage	= regulator_map_voltage_linear,
120ea88b132SAxel Lin 	.enable		= regulator_enable_regmap,
121ea88b132SAxel Lin 	.disable	= regulator_disable_regmap,
122ea88b132SAxel Lin 	.is_enabled	= regulator_is_enabled_regmap,
123a71b797fSHaojian Zhuang 	.enable_time	= max8649_enable_time,
124a71b797fSHaojian Zhuang 	.set_mode	= max8649_set_mode,
125a71b797fSHaojian Zhuang 	.get_mode	= max8649_get_mode,
126a71b797fSHaojian Zhuang 
127a71b797fSHaojian Zhuang };
128a71b797fSHaojian Zhuang 
129817d8c2dSAxel Lin static struct regulator_desc dcdc_desc = {
130a71b797fSHaojian Zhuang 	.name		= "max8649",
131a71b797fSHaojian Zhuang 	.ops		= &max8649_dcdc_ops,
132a71b797fSHaojian Zhuang 	.type		= REGULATOR_VOLTAGE,
133a71b797fSHaojian Zhuang 	.n_voltages	= 1 << 6,
134a71b797fSHaojian Zhuang 	.owner		= THIS_MODULE,
135817d8c2dSAxel Lin 	.vsel_mask	= MAX8649_VOL_MASK,
1360fb737b5SAxel Lin 	.min_uV		= MAX8649_DCDC_VMIN,
1370fb737b5SAxel Lin 	.uV_step	= MAX8649_DCDC_STEP,
138ea88b132SAxel Lin 	.enable_reg	= MAX8649_CONTROL,
139ea88b132SAxel Lin 	.enable_mask	= MAX8649_EN_PD,
140ea88b132SAxel Lin 	.enable_is_inverted = true,
141a71b797fSHaojian Zhuang };
142a71b797fSHaojian Zhuang 
143035f3324SKrzysztof Kozlowski static const struct regmap_config max8649_regmap_config = {
144c5b68d47SJonghwan Choi 	.reg_bits = 8,
145c5b68d47SJonghwan Choi 	.val_bits = 8,
146c5b68d47SJonghwan Choi };
147c5b68d47SJonghwan Choi 
max8649_regulator_probe(struct i2c_client * client)1483cf44173SUwe Kleine-König static int max8649_regulator_probe(struct i2c_client *client)
149a71b797fSHaojian Zhuang {
150dff91d0bSJingoo Han 	struct max8649_platform_data *pdata = dev_get_platdata(&client->dev);
151a71b797fSHaojian Zhuang 	struct max8649_regulator_info *info = NULL;
152b7313b89SKrzysztof Kozlowski 	struct regulator_dev *regulator;
153c172708dSMark Brown 	struct regulator_config config = { };
154c5b68d47SJonghwan Choi 	unsigned int val;
155a71b797fSHaojian Zhuang 	unsigned char data;
156a71b797fSHaojian Zhuang 	int ret;
157a71b797fSHaojian Zhuang 
158dc553a79SAxel Lin 	info = devm_kzalloc(&client->dev, sizeof(struct max8649_regulator_info),
159dc553a79SAxel Lin 			    GFP_KERNEL);
1606e27e996SJingoo Han 	if (!info)
161a71b797fSHaojian Zhuang 		return -ENOMEM;
162a71b797fSHaojian Zhuang 
163dc553a79SAxel Lin 	info->regmap = devm_regmap_init_i2c(client, &max8649_regmap_config);
164c5b68d47SJonghwan Choi 	if (IS_ERR(info->regmap)) {
165c5b68d47SJonghwan Choi 		ret = PTR_ERR(info->regmap);
166c5b68d47SJonghwan Choi 		dev_err(&client->dev, "Failed to allocate register map: %d\n", ret);
167dc553a79SAxel Lin 		return ret;
168c5b68d47SJonghwan Choi 	}
169c5b68d47SJonghwan Choi 
170a71b797fSHaojian Zhuang 	info->dev = &client->dev;
171a71b797fSHaojian Zhuang 	i2c_set_clientdata(client, info);
172a71b797fSHaojian Zhuang 
173a71b797fSHaojian Zhuang 	info->mode = pdata->mode;
174a71b797fSHaojian Zhuang 	switch (info->mode) {
175a71b797fSHaojian Zhuang 	case 0:
176817d8c2dSAxel Lin 		dcdc_desc.vsel_reg = MAX8649_MODE0;
177a71b797fSHaojian Zhuang 		break;
178a71b797fSHaojian Zhuang 	case 1:
179817d8c2dSAxel Lin 		dcdc_desc.vsel_reg = MAX8649_MODE1;
180a71b797fSHaojian Zhuang 		break;
181a71b797fSHaojian Zhuang 	case 2:
182817d8c2dSAxel Lin 		dcdc_desc.vsel_reg = MAX8649_MODE2;
183a71b797fSHaojian Zhuang 		break;
184a71b797fSHaojian Zhuang 	case 3:
185817d8c2dSAxel Lin 		dcdc_desc.vsel_reg = MAX8649_MODE3;
186a71b797fSHaojian Zhuang 		break;
187a71b797fSHaojian Zhuang 	default:
188a71b797fSHaojian Zhuang 		break;
189a71b797fSHaojian Zhuang 	}
190a71b797fSHaojian Zhuang 
191c5b68d47SJonghwan Choi 	ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val);
192c5b68d47SJonghwan Choi 	if (ret != 0) {
193a71b797fSHaojian Zhuang 		dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n",
194a71b797fSHaojian Zhuang 			ret);
195dc553a79SAxel Lin 		return ret;
196a71b797fSHaojian Zhuang 	}
197da7de6a1SAxel Lin 	dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", val);
198a71b797fSHaojian Zhuang 
199a71b797fSHaojian Zhuang 	/* enable VID0 & VID1 */
200c5b68d47SJonghwan Choi 	regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0);
201a71b797fSHaojian Zhuang 
202a71b797fSHaojian Zhuang 	/* enable/disable external clock synchronization */
203a71b797fSHaojian Zhuang 	info->extclk = pdata->extclk;
204a71b797fSHaojian Zhuang 	data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0;
205817d8c2dSAxel Lin 	regmap_update_bits(info->regmap, dcdc_desc.vsel_reg,
206817d8c2dSAxel Lin 			   MAX8649_SYNC_EXTCLK, data);
207a71b797fSHaojian Zhuang 	if (info->extclk) {
208a71b797fSHaojian Zhuang 		/* set external clock frequency */
209a71b797fSHaojian Zhuang 		info->extclk_freq = pdata->extclk_freq;
210c5b68d47SJonghwan Choi 		regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK,
2110f69c897SAxel Lin 				   info->extclk_freq << 6);
212a71b797fSHaojian Zhuang 	}
213a71b797fSHaojian Zhuang 
214a71b797fSHaojian Zhuang 	if (pdata->ramp_timing) {
215a71b797fSHaojian Zhuang 		info->ramp_timing = pdata->ramp_timing;
216c5b68d47SJonghwan Choi 		regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK,
217a71b797fSHaojian Zhuang 				   info->ramp_timing << 5);
218a71b797fSHaojian Zhuang 	}
219a71b797fSHaojian Zhuang 
220a71b797fSHaojian Zhuang 	info->ramp_down = pdata->ramp_down;
221a71b797fSHaojian Zhuang 	if (info->ramp_down) {
222c5b68d47SJonghwan Choi 		regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN,
223a71b797fSHaojian Zhuang 				   MAX8649_RAMP_DOWN);
224a71b797fSHaojian Zhuang 	}
225a71b797fSHaojian Zhuang 
226c172708dSMark Brown 	config.dev = &client->dev;
227c172708dSMark Brown 	config.init_data = pdata->regulator;
228c172708dSMark Brown 	config.driver_data = info;
22961663171SHaojian Zhuang 	config.regmap = info->regmap;
230c172708dSMark Brown 
231b7313b89SKrzysztof Kozlowski 	regulator = devm_regulator_register(&client->dev, &dcdc_desc,
232e453f92eSSachin Kamat 						  &config);
233b7313b89SKrzysztof Kozlowski 	if (IS_ERR(regulator)) {
234a71b797fSHaojian Zhuang 		dev_err(info->dev, "failed to register regulator %s\n",
235a71b797fSHaojian Zhuang 			dcdc_desc.name);
236b7313b89SKrzysztof Kozlowski 		return PTR_ERR(regulator);
237a71b797fSHaojian Zhuang 	}
238a71b797fSHaojian Zhuang 
239a71b797fSHaojian Zhuang 	return 0;
240a71b797fSHaojian Zhuang }
241a71b797fSHaojian Zhuang 
242a71b797fSHaojian Zhuang static const struct i2c_device_id max8649_id[] = {
243a71b797fSHaojian Zhuang 	{ "max8649", 0 },
244a71b797fSHaojian Zhuang 	{ }
245a71b797fSHaojian Zhuang };
246a71b797fSHaojian Zhuang MODULE_DEVICE_TABLE(i2c, max8649_id);
247a71b797fSHaojian Zhuang 
248a71b797fSHaojian Zhuang static struct i2c_driver max8649_driver = {
2493cf44173SUwe Kleine-König 	.probe		= max8649_regulator_probe,
250a71b797fSHaojian Zhuang 	.driver		= {
251a71b797fSHaojian Zhuang 		.name	= "max8649",
252*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
253a71b797fSHaojian Zhuang 	},
254a71b797fSHaojian Zhuang 	.id_table	= max8649_id,
255a71b797fSHaojian Zhuang };
256a71b797fSHaojian Zhuang 
max8649_init(void)257a71b797fSHaojian Zhuang static int __init max8649_init(void)
258a71b797fSHaojian Zhuang {
259a71b797fSHaojian Zhuang 	return i2c_add_driver(&max8649_driver);
260a71b797fSHaojian Zhuang }
261a71b797fSHaojian Zhuang subsys_initcall(max8649_init);
262a71b797fSHaojian Zhuang 
max8649_exit(void)263a71b797fSHaojian Zhuang static void __exit max8649_exit(void)
264a71b797fSHaojian Zhuang {
265a71b797fSHaojian Zhuang 	i2c_del_driver(&max8649_driver);
266a71b797fSHaojian Zhuang }
267a71b797fSHaojian Zhuang module_exit(max8649_exit);
268a71b797fSHaojian Zhuang 
269a71b797fSHaojian Zhuang /* Module information */
270a71b797fSHaojian Zhuang MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
271a71b797fSHaojian Zhuang MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
272a71b797fSHaojian Zhuang MODULE_LICENSE("GPL");
273