xref: /OK3568_Linux_fs/kernel/drivers/regulator/mp8865-regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Regulator driver for mp8865
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for  more details.
12  */
13 
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/of_regulator.h>
20 
21 #define MP8865_MAX_VSEL			0x7f
22 #define VOL_MIN_IDX			0x00
23 #define VOL_MAX_IDX			0x7f
24 #define MP8865_ENABLE_TIME		150
25 
26 /* Register definitions */
27 #define MP8865_VOUT_REG			0
28 #define MP8865_SYSCNTL_REG		1
29 #define MP8865_ID_REG			2
30 #define MP8865_STATUS_REG		3
31 #define MP8865_MAX_REG			4
32 
33 #define MP8865_VOUT_MASK		0x7f
34 #define MP8865_VBOOT_MASK		0x80
35 
36 #define MP8865_ENABLE			0x80
37 #define MP8865_GO_BIT			0x40
38 #define MP8865_SLEW_RATE_MASK		0x38
39 #define MP8865_SWITCH_FRE_MASK		0x06
40 #define MP8865_PFM_MODE			0X01
41 
42 /* mp8865 chip information */
43 struct mp8865_chip {
44 	struct device *dev;
45 	struct regulator_desc desc;
46 	struct regulator_dev  *rdev;
47 	struct regmap *regmap;
48 };
49 
50 struct mp8865_platform_data {
51 	struct regulator_init_data *mp8865_init_data;
52 	struct device_node *of_node;
53 };
54 
55 static const struct linear_range mp8865_voltage_ranges[] = {
56 	REGULATOR_LINEAR_RANGE(600000, VOL_MIN_IDX, VOL_MAX_IDX, 10000),
57 };
58 
mp8865_set_voltage(struct regulator_dev * rdev,unsigned sel)59 static int mp8865_set_voltage(struct regulator_dev *rdev, unsigned sel)
60 {
61 	int ret;
62 
63 	ret = regmap_update_bits(rdev->regmap, MP8865_SYSCNTL_REG,
64 				 MP8865_GO_BIT, MP8865_GO_BIT);
65 	if (ret)
66 		return ret;
67 
68 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
69 	return regmap_write(rdev->regmap, MP8865_VOUT_REG, sel);
70 }
71 
is_write_reg(struct device * dev,unsigned int reg)72 static bool is_write_reg(struct device *dev, unsigned int reg)
73 {
74 	return (reg < MP8865_ID_REG) ? true : false;
75 }
76 
is_volatile_reg(struct device * dev,unsigned int reg)77 static bool is_volatile_reg(struct device *dev, unsigned int reg)
78 {
79 	return (reg == MP8865_STATUS_REG) ? true : false;
80 }
81 
82 static const struct regmap_config mp8865_regmap_config = {
83 	.reg_bits = 8,
84 	.val_bits = 8,
85 	.writeable_reg = is_write_reg,
86 	.volatile_reg = is_volatile_reg,
87 	.max_register = MP8865_MAX_REG,
88 	.cache_type = REGCACHE_RBTREE,
89 };
90 
91 static struct regulator_ops mp8865_dcdc_ops = {
92 	.list_voltage = regulator_list_voltage_linear_range,
93 	.map_voltage = regulator_map_voltage_linear_range,
94 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
95 	.set_voltage_sel = mp8865_set_voltage,
96 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
97 	.enable = regulator_enable_regmap,
98 	.disable = regulator_disable_regmap,
99 	.is_enabled = regulator_is_enabled_regmap,
100 };
101 
102 static const struct of_device_id mp8865_of_match[] = {
103 	{.compatible =  "mps,mp8865", .data = (void *)0},
104 	{},
105 };
106 MODULE_DEVICE_TABLE(of, mp8865_of_match);
107 
108 static struct of_regulator_match mp8865_matches = {
109 	.name = "mp8865_dcdc1",
110 	.driver_data = (void *)0,
111 };
112 
113 static struct mp8865_platform_data *
mp8865_parse_dt(struct i2c_client * client,struct of_regulator_match ** mp8865_reg_matches)114 	mp8865_parse_dt(struct i2c_client *client,
115 			struct of_regulator_match **mp8865_reg_matches)
116 {
117 	struct mp8865_platform_data *pdata;
118 	struct device_node *np, *regulators;
119 	int ret;
120 
121 	pdata = devm_kzalloc(&client->dev, sizeof(struct mp8865_platform_data),
122 			     GFP_KERNEL);
123 	if (!pdata)
124 		return NULL;
125 
126 	np = of_node_get(client->dev.of_node);
127 	regulators = of_find_node_by_name(np, "regulators");
128 	if (!regulators) {
129 		dev_err(&client->dev, "regulator node not found\n");
130 		return NULL;
131 	}
132 
133 	ret = of_regulator_match(&client->dev, regulators, &mp8865_matches, 1);
134 	if (ret < 0) {
135 		dev_err(&client->dev, "Error parsing regulators init data\n");
136 		return NULL;
137 	}
138 	*mp8865_reg_matches = &mp8865_matches;
139 	of_node_put(regulators);
140 
141 	pdata->mp8865_init_data = mp8865_matches.init_data;
142 	pdata->of_node = mp8865_matches.of_node;
143 
144 	return pdata;
145 }
146 
mp8865_probe(struct i2c_client * client,const struct i2c_device_id * id)147 static int mp8865_probe(struct i2c_client *client,
148 			const struct i2c_device_id *id)
149 {
150 	struct mp8865_chip *mp8865;
151 	struct mp8865_platform_data *pdata;
152 	struct of_regulator_match *mp8865_reg_matches = NULL;
153 	struct regulator_dev *sy_rdev;
154 	struct regulator_config config;
155 	struct regulator_desc *rdesc;
156 	int ret;
157 
158 	mp8865 = devm_kzalloc(&client->dev, sizeof(struct mp8865_chip),
159 			      GFP_KERNEL);
160 	if (!mp8865)
161 		return -ENOMEM;
162 	pdata = dev_get_platdata(&client->dev);
163 	if (!pdata)
164 		pdata = mp8865_parse_dt(client, &mp8865_reg_matches);
165 
166 	if (!pdata || !pdata->mp8865_init_data) {
167 		dev_err(&client->dev, "Platform data not found!\n");
168 		return -ENODEV;
169 	}
170 
171 	mp8865->dev = &client->dev;
172 	mp8865->regmap = devm_regmap_init_i2c(client, &mp8865_regmap_config);
173 	if (IS_ERR(mp8865->regmap)) {
174 		dev_err(&client->dev, "Failed to allocate regmap!\n");
175 		return PTR_ERR(mp8865->regmap);
176 	}
177 
178 	i2c_set_clientdata(client, mp8865);
179 
180 	config.dev = mp8865->dev;
181 	config.driver_data = mp8865;
182 	config.init_data = pdata->mp8865_init_data;
183 	config.of_node = pdata->of_node;
184 	config.regmap = mp8865->regmap;
185 
186 	rdesc = &mp8865->desc;
187 	rdesc->name = "mp8865_dcdc1",
188 	rdesc->id = 0,
189 	rdesc->ops = &mp8865_dcdc_ops,
190 	rdesc->n_voltages = MP8865_MAX_VSEL + 1,
191 	rdesc->linear_ranges = mp8865_voltage_ranges,
192 	rdesc->n_linear_ranges = ARRAY_SIZE(mp8865_voltage_ranges),
193 	rdesc->vsel_reg = MP8865_VOUT_REG;
194 	rdesc->vsel_mask = MP8865_VOUT_MASK;
195 	rdesc->enable_reg = MP8865_SYSCNTL_REG;
196 	rdesc->enable_mask = MP8865_ENABLE;
197 	rdesc->enable_time = MP8865_ENABLE_TIME;
198 	rdesc->type = REGULATOR_VOLTAGE,
199 	rdesc->owner = THIS_MODULE;
200 
201 	/* set slew_rate 16mV/uS */
202 	ret = regmap_update_bits(mp8865->regmap, MP8865_SYSCNTL_REG,
203 				 MP8865_SLEW_RATE_MASK, 0x10);
204 	if (ret) {
205 		dev_err(mp8865->dev, "failed to set slew_rate\n");
206 		return ret;
207 	}
208 
209 	/* set switch_frequency 1.1MHz */
210 	ret = regmap_update_bits(mp8865->regmap, MP8865_SYSCNTL_REG,
211 				 MP8865_SWITCH_FRE_MASK, 0x40);
212 	if (ret) {
213 		dev_err(mp8865->dev, "failed to set switch_frequency\n");
214 		return ret;
215 	}
216 
217 	sy_rdev = devm_regulator_register(mp8865->dev, &mp8865->desc,
218 					  &config);
219 	if (IS_ERR(sy_rdev)) {
220 		dev_err(mp8865->dev, "failed to register regulator\n");
221 		return PTR_ERR(sy_rdev);
222 	}
223 
224 	dev_info(mp8865->dev, "mp8865 register successful\n");
225 
226 	return 0;
227 }
228 
229 static const struct i2c_device_id mp8865_id[] = {
230 	{ .name = "mps,mp8865", },
231 	{  },
232 };
233 MODULE_DEVICE_TABLE(i2c, mp8865_id);
234 
235 static struct i2c_driver mp8865_driver = {
236 	.driver = {
237 		.name = "mp8865",
238 		.of_match_table = of_match_ptr(mp8865_of_match),
239 		.owner = THIS_MODULE,
240 	},
241 	.probe    = mp8865_probe,
242 	.id_table = mp8865_id,
243 };
244 module_i2c_driver(mp8865_driver);
245 
246 MODULE_AUTHOR("Zain Wang <zain.wang@rock-chips.com>");
247 MODULE_DESCRIPTION("mp8865 voltage regulator driver");
248 MODULE_LICENSE("GPL v2");
249