1f16861b1SSeth Forshee // SPDX-License-Identifier: GPL-2.0
2f16861b1SSeth Forshee //
3f16861b1SSeth Forshee // Regulators driver for Dialog Semiconductor DA903x
4f16861b1SSeth Forshee //
5f16861b1SSeth Forshee // Copyright (C) 2006-2008 Marvell International Ltd.
6f16861b1SSeth Forshee // Copyright (C) 2008 Compulab Ltd.
7f16861b1SSeth Forshee 
8f16861b1SSeth Forshee #include <linux/kernel.h>
9f16861b1SSeth Forshee #include <linux/init.h>
10f16861b1SSeth Forshee #include <linux/err.h>
11f16861b1SSeth Forshee #include <linux/module.h>
12f16861b1SSeth Forshee #include <linux/platform_device.h>
13f16861b1SSeth Forshee #include <linux/regulator/driver.h>
14f16861b1SSeth Forshee #include <linux/regulator/machine.h>
15f16861b1SSeth Forshee #include <linux/mfd/da903x.h>
16f16861b1SSeth Forshee 
17f16861b1SSeth Forshee /* DA9030 Registers */
18f16861b1SSeth Forshee #define DA9030_INVAL		(-1)
19f16861b1SSeth Forshee #define DA9030_LDO1011		(0x10)
20f16861b1SSeth Forshee #define DA9030_LDO15		(0x11)
21f16861b1SSeth Forshee #define DA9030_LDO1416		(0x12)
22f16861b1SSeth Forshee #define DA9030_LDO1819		(0x13)
23f16861b1SSeth Forshee #define DA9030_LDO17		(0x14)
24f16861b1SSeth Forshee #define DA9030_BUCK2DVM1	(0x15)
25f16861b1SSeth Forshee #define DA9030_BUCK2DVM2	(0x16)
26f16861b1SSeth Forshee #define DA9030_RCTL11		(0x17)
27f16861b1SSeth Forshee #define DA9030_RCTL21		(0x18)
28f16861b1SSeth Forshee #define DA9030_LDO1		(0x90)
29f16861b1SSeth Forshee #define DA9030_LDO23		(0x91)
30f16861b1SSeth Forshee #define DA9030_LDO45		(0x92)
31f16861b1SSeth Forshee #define DA9030_LDO6		(0x93)
32f16861b1SSeth Forshee #define DA9030_LDO78		(0x94)
33f16861b1SSeth Forshee #define DA9030_LDO912		(0x95)
34f16861b1SSeth Forshee #define DA9030_BUCK		(0x96)
35f16861b1SSeth Forshee #define DA9030_RCTL12		(0x97)
36f16861b1SSeth Forshee #define DA9030_RCTL22		(0x98)
37f16861b1SSeth Forshee #define DA9030_LDO_UNLOCK	(0xa0)
38f16861b1SSeth Forshee #define DA9030_LDO_UNLOCK_MASK	(0xe0)
39f16861b1SSeth Forshee #define DA9034_OVER1		(0x10)
40f16861b1SSeth Forshee 
41f16861b1SSeth Forshee /* DA9034 Registers */
42f16861b1SSeth Forshee #define DA9034_INVAL		(-1)
43f16861b1SSeth Forshee #define DA9034_OVER2		(0x11)
44f16861b1SSeth Forshee #define DA9034_OVER3		(0x12)
45f16861b1SSeth Forshee #define DA9034_LDO643		(0x13)
46f16861b1SSeth Forshee #define DA9034_LDO987		(0x14)
47f16861b1SSeth Forshee #define DA9034_LDO1110		(0x15)
48f16861b1SSeth Forshee #define DA9034_LDO1312		(0x16)
49f16861b1SSeth Forshee #define DA9034_LDO1514		(0x17)
50f16861b1SSeth Forshee #define DA9034_VCC1		(0x20)
51f16861b1SSeth Forshee #define DA9034_ADTV1		(0x23)
52f16861b1SSeth Forshee #define DA9034_ADTV2		(0x24)
53f16861b1SSeth Forshee #define DA9034_AVRC		(0x25)
54f16861b1SSeth Forshee #define DA9034_CDTV1		(0x26)
55f16861b1SSeth Forshee #define DA9034_CDTV2		(0x27)
56f16861b1SSeth Forshee #define DA9034_CVRC		(0x28)
57f16861b1SSeth Forshee #define DA9034_SDTV1		(0x29)
58f16861b1SSeth Forshee #define DA9034_SDTV2		(0x2a)
59f16861b1SSeth Forshee #define DA9034_SVRC		(0x2b)
60f16861b1SSeth Forshee #define DA9034_MDTV1		(0x32)
61f16861b1SSeth Forshee #define DA9034_MDTV2		(0x33)
62f16861b1SSeth Forshee #define DA9034_MVRC		(0x34)
63f16861b1SSeth Forshee 
64f16861b1SSeth Forshee /* DA9035 Registers. DA9034 Registers are comptabile to DA9035. */
65f16861b1SSeth Forshee #define DA9035_OVER3		(0x12)
66f16861b1SSeth Forshee #define DA9035_VCC2		(0x1f)
67f16861b1SSeth Forshee #define DA9035_3DTV1		(0x2c)
68f16861b1SSeth Forshee #define DA9035_3DTV2		(0x2d)
69f16861b1SSeth Forshee #define DA9035_3VRC		(0x2e)
70f16861b1SSeth Forshee #define DA9035_AUTOSKIP		(0x2f)
71f16861b1SSeth Forshee 
72f16861b1SSeth Forshee struct da903x_regulator_info {
73f16861b1SSeth Forshee 	struct regulator_desc desc;
74f16861b1SSeth Forshee 
75f16861b1SSeth Forshee 	int	max_uV;
76f16861b1SSeth Forshee 	int	vol_reg;
77f16861b1SSeth Forshee 	int	vol_shift;
78f16861b1SSeth Forshee 	int	vol_nbits;
79f16861b1SSeth Forshee 	int	update_reg;
80f16861b1SSeth Forshee 	int	update_bit;
81f16861b1SSeth Forshee 	int	enable_reg;
82f16861b1SSeth Forshee 	int	enable_bit;
83f16861b1SSeth Forshee };
84f16861b1SSeth Forshee 
to_da903x_dev(struct regulator_dev * rdev)85f16861b1SSeth Forshee static inline struct device *to_da903x_dev(struct regulator_dev *rdev)
86f16861b1SSeth Forshee {
87f16861b1SSeth Forshee 	return rdev_get_dev(rdev)->parent->parent;
88f16861b1SSeth Forshee }
89f16861b1SSeth Forshee 
check_range(struct da903x_regulator_info * info,int min_uV,int max_uV)90f16861b1SSeth Forshee static inline int check_range(struct da903x_regulator_info *info,
91f16861b1SSeth Forshee 				int min_uV, int max_uV)
92f16861b1SSeth Forshee {
93f16861b1SSeth Forshee 	if (min_uV < info->desc.min_uV || min_uV > info->max_uV)
94f16861b1SSeth Forshee 		return -EINVAL;
95f16861b1SSeth Forshee 
96f16861b1SSeth Forshee 	return 0;
97f16861b1SSeth Forshee }
98f16861b1SSeth Forshee 
99f16861b1SSeth Forshee /* DA9030/DA9034 common operations */
da903x_set_voltage_sel(struct regulator_dev * rdev,unsigned selector)100f16861b1SSeth Forshee static int da903x_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
101f16861b1SSeth Forshee {
102f16861b1SSeth Forshee 	struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
103f16861b1SSeth Forshee 	struct device *da9034_dev = to_da903x_dev(rdev);
104f16861b1SSeth Forshee 	uint8_t val, mask;
105f16861b1SSeth Forshee 
106f16861b1SSeth Forshee 	if (rdev->desc->n_voltages == 1)
107f16861b1SSeth Forshee 		return -EINVAL;
108f16861b1SSeth Forshee 
109f16861b1SSeth Forshee 	val = selector << info->vol_shift;
110f16861b1SSeth Forshee 	mask = ((1 << info->vol_nbits) - 1)  << info->vol_shift;
111f16861b1SSeth Forshee 
112f16861b1SSeth Forshee 	return da903x_update(da9034_dev, info->vol_reg, val, mask);
113f16861b1SSeth Forshee }
114f16861b1SSeth Forshee 
da903x_get_voltage_sel(struct regulator_dev * rdev)115f16861b1SSeth Forshee static int da903x_get_voltage_sel(struct regulator_dev *rdev)
116f16861b1SSeth Forshee {
117f16861b1SSeth Forshee 	struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
118f16861b1SSeth Forshee 	struct device *da9034_dev = to_da903x_dev(rdev);
119f16861b1SSeth Forshee 	uint8_t val, mask;
120f16861b1SSeth Forshee 	int ret;
121f16861b1SSeth Forshee 
122f16861b1SSeth Forshee 	if (rdev->desc->n_voltages == 1)
123f16861b1SSeth Forshee 		return 0;
124f16861b1SSeth Forshee 
125f16861b1SSeth Forshee 	ret = da903x_read(da9034_dev, info->vol_reg, &val);
126f16861b1SSeth Forshee 	if (ret)
127f16861b1SSeth Forshee 		return ret;
128f16861b1SSeth Forshee 
129f16861b1SSeth Forshee 	mask = ((1 << info->vol_nbits) - 1)  << info->vol_shift;
130f16861b1SSeth Forshee 	val = (val & mask) >> info->vol_shift;
131f16861b1SSeth Forshee 
132f16861b1SSeth Forshee 	return val;
133f16861b1SSeth Forshee }
134f16861b1SSeth Forshee 
da903x_enable(struct regulator_dev * rdev)135f16861b1SSeth Forshee static int da903x_enable(struct regulator_dev *rdev)
136f16861b1SSeth Forshee {
137f16861b1SSeth Forshee 	struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
138f16861b1SSeth Forshee 	struct device *da9034_dev = to_da903x_dev(rdev);
139f16861b1SSeth Forshee 
140f16861b1SSeth Forshee 	return da903x_set_bits(da9034_dev, info->enable_reg,
141f16861b1SSeth Forshee 					1 << info->enable_bit);
142f16861b1SSeth Forshee }
143f16861b1SSeth Forshee 
da903x_disable(struct regulator_dev * rdev)144f16861b1SSeth Forshee static int da903x_disable(struct regulator_dev *rdev)
145f16861b1SSeth Forshee {
146f16861b1SSeth Forshee 	struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
147f16861b1SSeth Forshee 	struct device *da9034_dev = to_da903x_dev(rdev);
148f16861b1SSeth Forshee 
149f16861b1SSeth Forshee 	return da903x_clr_bits(da9034_dev, info->enable_reg,
150f16861b1SSeth Forshee 					1 << info->enable_bit);
151f16861b1SSeth Forshee }
152f16861b1SSeth Forshee 
da903x_is_enabled(struct regulator_dev * rdev)153f16861b1SSeth Forshee static int da903x_is_enabled(struct regulator_dev *rdev)
154f16861b1SSeth Forshee {
155f16861b1SSeth Forshee 	struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
156f16861b1SSeth Forshee 	struct device *da9034_dev = to_da903x_dev(rdev);
157f16861b1SSeth Forshee 	uint8_t reg_val;
158f16861b1SSeth Forshee 	int ret;
159f16861b1SSeth Forshee 
160f16861b1SSeth Forshee 	ret = da903x_read(da9034_dev, info->enable_reg, &reg_val);
161f16861b1SSeth Forshee 	if (ret)
162f16861b1SSeth Forshee 		return ret;
163f16861b1SSeth Forshee 
164f16861b1SSeth Forshee 	return !!(reg_val & (1 << info->enable_bit));
165f16861b1SSeth Forshee }
166f16861b1SSeth Forshee 
167f16861b1SSeth Forshee /* DA9030 specific operations */
da9030_set_ldo1_15_voltage_sel(struct regulator_dev * rdev,unsigned selector)168f16861b1SSeth Forshee static int da9030_set_ldo1_15_voltage_sel(struct regulator_dev *rdev,
169f16861b1SSeth Forshee 					  unsigned selector)
170f16861b1SSeth Forshee {
171f16861b1SSeth Forshee 	struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
172f16861b1SSeth Forshee 	struct device *da903x_dev = to_da903x_dev(rdev);
173f16861b1SSeth Forshee 	uint8_t val, mask;
174f16861b1SSeth Forshee 	int ret;
175f16861b1SSeth Forshee 
176f16861b1SSeth Forshee 	val = selector << info->vol_shift;
177f16861b1SSeth Forshee 	mask = ((1 << info->vol_nbits) - 1)  << info->vol_shift;
178f16861b1SSeth Forshee 	val |= DA9030_LDO_UNLOCK; /* have to set UNLOCK bits */
179f16861b1SSeth Forshee 	mask |= DA9030_LDO_UNLOCK_MASK;
180f16861b1SSeth Forshee 
181f16861b1SSeth Forshee 	/* write twice */
182f16861b1SSeth Forshee 	ret = da903x_update(da903x_dev, info->vol_reg, val, mask);
183f16861b1SSeth Forshee 	if (ret)
184f16861b1SSeth Forshee 		return ret;
185f16861b1SSeth Forshee 
186f16861b1SSeth Forshee 	return da903x_update(da903x_dev, info->vol_reg, val, mask);
187f16861b1SSeth Forshee }
188f16861b1SSeth Forshee 
da9030_map_ldo14_voltage(struct regulator_dev * rdev,int min_uV,int max_uV)189f16861b1SSeth Forshee static int da9030_map_ldo14_voltage(struct regulator_dev *rdev,
190f16861b1SSeth Forshee 				    int min_uV, int max_uV)
191f16861b1SSeth Forshee {
192f16861b1SSeth Forshee 	struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
193f16861b1SSeth Forshee 	int thresh, sel;
194f16861b1SSeth Forshee 
195f16861b1SSeth Forshee 	if (check_range(info, min_uV, max_uV)) {
196f16861b1SSeth Forshee 		pr_err("invalid voltage range (%d, %d) uV\n", min_uV, max_uV);
197f16861b1SSeth Forshee 		return -EINVAL;
198f16861b1SSeth Forshee 	}
199f16861b1SSeth Forshee 
200f16861b1SSeth Forshee 	thresh = (info->max_uV + info->desc.min_uV) / 2;
201f16861b1SSeth Forshee 	if (min_uV < thresh) {
202f16861b1SSeth Forshee 		sel = DIV_ROUND_UP(thresh - min_uV, info->desc.uV_step);
203f16861b1SSeth Forshee 		sel |= 0x4;
204f16861b1SSeth Forshee 	} else {
205f16861b1SSeth Forshee 		sel = DIV_ROUND_UP(min_uV - thresh, info->desc.uV_step);
206f16861b1SSeth Forshee 	}
207f16861b1SSeth Forshee 
208f16861b1SSeth Forshee 	return sel;
209f16861b1SSeth Forshee }
210f16861b1SSeth Forshee 
da9030_list_ldo14_voltage(struct regulator_dev * rdev,unsigned selector)211f16861b1SSeth Forshee static int da9030_list_ldo14_voltage(struct regulator_dev *rdev,
212f16861b1SSeth Forshee 				     unsigned selector)
213f16861b1SSeth Forshee {
214f16861b1SSeth Forshee 	struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
215f16861b1SSeth Forshee 	int volt;
216f16861b1SSeth Forshee 
217f16861b1SSeth Forshee 	if (selector & 0x4)
218f16861b1SSeth Forshee 		volt = rdev->desc->min_uV +
219f16861b1SSeth Forshee 		       rdev->desc->uV_step * (3 - (selector & ~0x4));
220f16861b1SSeth Forshee 	else
221f16861b1SSeth Forshee 		volt = (info->max_uV + rdev->desc->min_uV) / 2 +
222f16861b1SSeth Forshee 		       rdev->desc->uV_step * (selector & ~0x4);
223f16861b1SSeth Forshee 
224f16861b1SSeth Forshee 	if (volt > info->max_uV)
225f16861b1SSeth Forshee 		return -EINVAL;
226f16861b1SSeth Forshee 
227f16861b1SSeth Forshee 	return volt;
228f16861b1SSeth Forshee }
229f16861b1SSeth Forshee 
230f16861b1SSeth Forshee /* DA9034 specific operations */
da9034_set_dvc_voltage_sel(struct regulator_dev * rdev,unsigned selector)231f16861b1SSeth Forshee static int da9034_set_dvc_voltage_sel(struct regulator_dev *rdev,
232f16861b1SSeth Forshee 				      unsigned selector)
233f16861b1SSeth Forshee {
234f16861b1SSeth Forshee 	struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
235f16861b1SSeth Forshee 	struct device *da9034_dev = to_da903x_dev(rdev);
236f16861b1SSeth Forshee 	uint8_t val, mask;
237f16861b1SSeth Forshee 	int ret;
238f16861b1SSeth Forshee 
239f16861b1SSeth Forshee 	val = selector << info->vol_shift;
240f16861b1SSeth Forshee 	mask = ((1 << info->vol_nbits) - 1)  << info->vol_shift;
241f16861b1SSeth Forshee 
242f16861b1SSeth Forshee 	ret = da903x_update(da9034_dev, info->vol_reg, val, mask);
243f16861b1SSeth Forshee 	if (ret)
244f16861b1SSeth Forshee 		return ret;
245f16861b1SSeth Forshee 
246f16861b1SSeth Forshee 	ret = da903x_set_bits(da9034_dev, info->update_reg,
247f16861b1SSeth Forshee 					1 << info->update_bit);
248f16861b1SSeth Forshee 	return ret;
249f16861b1SSeth Forshee }
250f16861b1SSeth Forshee 
251f16861b1SSeth Forshee static const struct linear_range da9034_ldo12_ranges[] = {
252f16861b1SSeth Forshee 	REGULATOR_LINEAR_RANGE(1700000, 0, 7, 50000),
253f16861b1SSeth Forshee 	REGULATOR_LINEAR_RANGE(2700000, 8, 15, 50000),
254f16861b1SSeth Forshee };
255f16861b1SSeth Forshee 
256f16861b1SSeth Forshee static const struct regulator_ops da903x_regulator_ldo_ops = {
257f16861b1SSeth Forshee 	.set_voltage_sel = da903x_set_voltage_sel,
258f16861b1SSeth Forshee 	.get_voltage_sel = da903x_get_voltage_sel,
259f16861b1SSeth Forshee 	.list_voltage	= regulator_list_voltage_linear,
260f16861b1SSeth Forshee 	.map_voltage	= regulator_map_voltage_linear,
261f16861b1SSeth Forshee 	.enable		= da903x_enable,
262f16861b1SSeth Forshee 	.disable	= da903x_disable,
263f16861b1SSeth Forshee 	.is_enabled	= da903x_is_enabled,
264f16861b1SSeth Forshee };
265f16861b1SSeth Forshee 
266f16861b1SSeth Forshee /* NOTE: this is dedicated for the insane DA9030 LDO14 */
267f16861b1SSeth Forshee static const struct regulator_ops da9030_regulator_ldo14_ops = {
268f16861b1SSeth Forshee 	.set_voltage_sel = da903x_set_voltage_sel,
269f16861b1SSeth Forshee 	.get_voltage_sel = da903x_get_voltage_sel,
270f16861b1SSeth Forshee 	.list_voltage	= da9030_list_ldo14_voltage,
271f16861b1SSeth Forshee 	.map_voltage	= da9030_map_ldo14_voltage,
272f16861b1SSeth Forshee 	.enable		= da903x_enable,
273f16861b1SSeth Forshee 	.disable	= da903x_disable,
274f16861b1SSeth Forshee 	.is_enabled	= da903x_is_enabled,
275f16861b1SSeth Forshee };
276f16861b1SSeth Forshee 
277f16861b1SSeth Forshee /* NOTE: this is dedicated for the DA9030 LDO1 and LDO15 that have locks  */
278f16861b1SSeth Forshee static const struct regulator_ops da9030_regulator_ldo1_15_ops = {
279f16861b1SSeth Forshee 	.set_voltage_sel = da9030_set_ldo1_15_voltage_sel,
280f16861b1SSeth Forshee 	.get_voltage_sel = da903x_get_voltage_sel,
281f16861b1SSeth Forshee 	.list_voltage	= regulator_list_voltage_linear,
282f16861b1SSeth Forshee 	.map_voltage	= regulator_map_voltage_linear,
283f16861b1SSeth Forshee 	.enable		= da903x_enable,
284f16861b1SSeth Forshee 	.disable	= da903x_disable,
285f16861b1SSeth Forshee 	.is_enabled	= da903x_is_enabled,
286f16861b1SSeth Forshee };
287f16861b1SSeth Forshee 
288f16861b1SSeth Forshee static const struct regulator_ops da9034_regulator_dvc_ops = {
289f16861b1SSeth Forshee 	.set_voltage_sel = da9034_set_dvc_voltage_sel,
290f16861b1SSeth Forshee 	.get_voltage_sel = da903x_get_voltage_sel,
291f16861b1SSeth Forshee 	.list_voltage	= regulator_list_voltage_linear,
292f16861b1SSeth Forshee 	.map_voltage	= regulator_map_voltage_linear,
293f16861b1SSeth Forshee 	.enable		= da903x_enable,
294f16861b1SSeth Forshee 	.disable	= da903x_disable,
295f16861b1SSeth Forshee 	.is_enabled	= da903x_is_enabled,
296f16861b1SSeth Forshee };
297f16861b1SSeth Forshee 
298f16861b1SSeth Forshee /* NOTE: this is dedicated for the insane LDO12 */
299f16861b1SSeth Forshee static const struct regulator_ops da9034_regulator_ldo12_ops = {
300f16861b1SSeth Forshee 	.set_voltage_sel = da903x_set_voltage_sel,
301f16861b1SSeth Forshee 	.get_voltage_sel = da903x_get_voltage_sel,
302f16861b1SSeth Forshee 	.list_voltage	= regulator_list_voltage_linear_range,
303f16861b1SSeth Forshee 	.map_voltage	= regulator_map_voltage_linear_range,
304f16861b1SSeth Forshee 	.enable		= da903x_enable,
305f16861b1SSeth Forshee 	.disable	= da903x_disable,
306f16861b1SSeth Forshee 	.is_enabled	= da903x_is_enabled,
307f16861b1SSeth Forshee };
308f16861b1SSeth Forshee 
309f16861b1SSeth Forshee #define DA903x_LDO(_pmic, _id, min, max, step, vreg, shift, nbits, ereg, ebit)	\
310f16861b1SSeth Forshee {									\
311f16861b1SSeth Forshee 	.desc	= {							\
312f16861b1SSeth Forshee 		.name	= "LDO" #_id,					\
313f16861b1SSeth Forshee 		.ops	= &da903x_regulator_ldo_ops,			\
314f16861b1SSeth Forshee 		.type	= REGULATOR_VOLTAGE,				\
315f16861b1SSeth Forshee 		.id	= _pmic##_ID_LDO##_id,				\
316f16861b1SSeth Forshee 		.n_voltages = (step) ? ((max - min) / step + 1) : 1,	\
317f16861b1SSeth Forshee 		.owner	= THIS_MODULE,					\
318f16861b1SSeth Forshee 		.min_uV	 = (min) * 1000,				\
319f16861b1SSeth Forshee 		.uV_step = (step) * 1000,				\
320f16861b1SSeth Forshee 	},								\
321f16861b1SSeth Forshee 	.max_uV		= (max) * 1000,					\
322f16861b1SSeth Forshee 	.vol_reg	= _pmic##_##vreg,				\
323f16861b1SSeth Forshee 	.vol_shift	= (shift),					\
324f16861b1SSeth Forshee 	.vol_nbits	= (nbits),					\
325f16861b1SSeth Forshee 	.enable_reg	= _pmic##_##ereg,				\
326f16861b1SSeth Forshee 	.enable_bit	= (ebit),					\
327f16861b1SSeth Forshee }
328f16861b1SSeth Forshee 
329f16861b1SSeth Forshee #define DA903x_DVC(_pmic, _id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
330f16861b1SSeth Forshee {									\
331f16861b1SSeth Forshee 	.desc	= {							\
332f16861b1SSeth Forshee 		.name	= #_id,						\
333f16861b1SSeth Forshee 		.ops	= &da9034_regulator_dvc_ops,			\
334f16861b1SSeth Forshee 		.type	= REGULATOR_VOLTAGE,				\
335f16861b1SSeth Forshee 		.id	= _pmic##_ID_##_id,				\
336f16861b1SSeth Forshee 		.n_voltages = (step) ? ((max - min) / step + 1) : 1,	\
337f16861b1SSeth Forshee 		.owner	= THIS_MODULE,					\
338f16861b1SSeth Forshee 		.min_uV = (min) * 1000,					\
339f16861b1SSeth Forshee 		.uV_step = (step) * 1000,				\
340f16861b1SSeth Forshee 	},								\
341f16861b1SSeth Forshee 	.max_uV		= (max) * 1000,					\
342f16861b1SSeth Forshee 	.vol_reg	= _pmic##_##vreg,				\
343f16861b1SSeth Forshee 	.vol_shift	= (0),						\
344f16861b1SSeth Forshee 	.vol_nbits	= (nbits),					\
345f16861b1SSeth Forshee 	.update_reg	= _pmic##_##ureg,				\
346f16861b1SSeth Forshee 	.update_bit	= (ubit),					\
347f16861b1SSeth Forshee 	.enable_reg	= _pmic##_##ereg,				\
348f16861b1SSeth Forshee 	.enable_bit	= (ebit),					\
349f16861b1SSeth Forshee }
350f16861b1SSeth Forshee 
351f16861b1SSeth Forshee #define DA9034_LDO(_id, min, max, step, vreg, shift, nbits, ereg, ebit)	\
352f16861b1SSeth Forshee 	DA903x_LDO(DA9034, _id, min, max, step, vreg, shift, nbits, ereg, ebit)
353f16861b1SSeth Forshee 
354f16861b1SSeth Forshee #define DA9030_LDO(_id, min, max, step, vreg, shift, nbits, ereg, ebit)	\
355f16861b1SSeth Forshee 	DA903x_LDO(DA9030, _id, min, max, step, vreg, shift, nbits, ereg, ebit)
356f16861b1SSeth Forshee 
357f16861b1SSeth Forshee #define DA9030_DVC(_id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
358f16861b1SSeth Forshee 	DA903x_DVC(DA9030, _id, min, max, step, vreg, nbits, ureg, ubit, \
359f16861b1SSeth Forshee 		   ereg, ebit)
360f16861b1SSeth Forshee 
361f16861b1SSeth Forshee #define DA9034_DVC(_id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
362f16861b1SSeth Forshee 	DA903x_DVC(DA9034, _id, min, max, step, vreg, nbits, ureg, ubit, \
363f16861b1SSeth Forshee 		   ereg, ebit)
364f16861b1SSeth Forshee 
365f16861b1SSeth Forshee #define DA9035_DVC(_id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
366f16861b1SSeth Forshee 	DA903x_DVC(DA9035, _id, min, max, step, vreg, nbits, ureg, ubit, \
367f16861b1SSeth Forshee 		   ereg, ebit)
368f16861b1SSeth Forshee 
369f16861b1SSeth Forshee static struct da903x_regulator_info da903x_regulator_info[] = {
370f16861b1SSeth Forshee 	/* DA9030 */
371f16861b1SSeth Forshee 	DA9030_DVC(BUCK2, 850, 1625, 25, BUCK2DVM1, 5, BUCK2DVM1, 7, RCTL11, 0),
372f16861b1SSeth Forshee 
373f16861b1SSeth Forshee 	DA9030_LDO( 1, 1200, 3200, 100,    LDO1, 0, 5, RCTL12, 1),
374f16861b1SSeth Forshee 	DA9030_LDO( 2, 1800, 3200, 100,   LDO23, 0, 4, RCTL12, 2),
375f16861b1SSeth Forshee 	DA9030_LDO( 3, 1800, 3200, 100,   LDO23, 4, 4, RCTL12, 3),
376f16861b1SSeth Forshee 	DA9030_LDO( 4, 1800, 3200, 100,   LDO45, 0, 4, RCTL12, 4),
377f16861b1SSeth Forshee 	DA9030_LDO( 5, 1800, 3200, 100,   LDO45, 4, 4, RCTL12, 5),
378f16861b1SSeth Forshee 	DA9030_LDO( 6, 1800, 3200, 100,    LDO6, 0, 4, RCTL12, 6),
379f16861b1SSeth Forshee 	DA9030_LDO( 7, 1800, 3200, 100,   LDO78, 0, 4, RCTL12, 7),
380f16861b1SSeth Forshee 	DA9030_LDO( 8, 1800, 3200, 100,   LDO78, 4, 4, RCTL22, 0),
381f16861b1SSeth Forshee 	DA9030_LDO( 9, 1800, 3200, 100,  LDO912, 0, 4, RCTL22, 1),
382f16861b1SSeth Forshee 	DA9030_LDO(10, 1800, 3200, 100, LDO1011, 0, 4, RCTL22, 2),
383f16861b1SSeth Forshee 	DA9030_LDO(11, 1800, 3200, 100, LDO1011, 4, 4, RCTL22, 3),
384f16861b1SSeth Forshee 	DA9030_LDO(12, 1800, 3200, 100,  LDO912, 4, 4, RCTL22, 4),
385f16861b1SSeth Forshee 	DA9030_LDO(14, 2760, 2940,  30, LDO1416, 0, 3, RCTL11, 4),
386f16861b1SSeth Forshee 	DA9030_LDO(15, 1100, 2650,  50,	  LDO15, 0, 5, RCTL11, 5),
387f16861b1SSeth Forshee 	DA9030_LDO(16, 1100, 2650,  50, LDO1416, 3, 5, RCTL11, 6),
388f16861b1SSeth Forshee 	DA9030_LDO(17, 1800, 3200, 100,   LDO17, 0, 4, RCTL11, 7),
389f16861b1SSeth Forshee 	DA9030_LDO(18, 1800, 3200, 100, LDO1819, 0, 4, RCTL21, 2),
390f16861b1SSeth Forshee 	DA9030_LDO(19, 1800, 3200, 100, LDO1819, 4, 4, RCTL21, 1),
391f16861b1SSeth Forshee 	DA9030_LDO(13, 2100, 2100, 0, INVAL, 0, 0, RCTL11, 3), /* fixed @2.1V */
392f16861b1SSeth Forshee 
393f16861b1SSeth Forshee 	/* DA9034 */
394f16861b1SSeth Forshee 	DA9034_DVC(BUCK1, 725, 1500, 25, ADTV2, 5, VCC1, 0, OVER1, 0),
395f16861b1SSeth Forshee 	DA9034_DVC(BUCK2, 725, 1500, 25, CDTV2, 5, VCC1, 2, OVER1, 1),
396f16861b1SSeth Forshee 	DA9034_DVC(LDO2,  725, 1500, 25, SDTV2, 5, VCC1, 4, OVER1, 2),
397f16861b1SSeth Forshee 	DA9034_DVC(LDO1, 1700, 2075, 25, MDTV1, 4, VCC1, 6, OVER3, 4),
398f16861b1SSeth Forshee 
399f16861b1SSeth Forshee 	DA9034_LDO( 3, 1800, 3300, 100,  LDO643, 0, 4, OVER3, 5),
400f16861b1SSeth Forshee 	DA9034_LDO( 4, 1800, 2900,1100,  LDO643, 4, 1, OVER3, 6),
401f16861b1SSeth Forshee 	DA9034_LDO( 6, 2500, 2850,  50,  LDO643, 5, 3, OVER2, 0),
402f16861b1SSeth Forshee 	DA9034_LDO( 7, 2700, 3050,  50,  LDO987, 0, 3, OVER2, 1),
403f16861b1SSeth Forshee 	DA9034_LDO( 8, 2700, 2850,  50,  LDO987, 3, 2, OVER2, 2),
404f16861b1SSeth Forshee 	DA9034_LDO( 9, 2700, 3050,  50,  LDO987, 5, 3, OVER2, 3),
405f16861b1SSeth Forshee 	DA9034_LDO(10, 2700, 3050,  50, LDO1110, 0, 3, OVER2, 4),
406f16861b1SSeth Forshee 	DA9034_LDO(11, 1800, 3300, 100, LDO1110, 4, 4, OVER2, 5),
407f16861b1SSeth Forshee 	DA9034_LDO(12, 1700, 3050,  50, LDO1312, 0, 4, OVER3, 6),
408f16861b1SSeth Forshee 	DA9034_LDO(13, 1800, 3300, 100, LDO1312, 4, 4, OVER2, 7),
409f16861b1SSeth Forshee 	DA9034_LDO(14, 1800, 3300, 100, LDO1514, 0, 4, OVER3, 0),
410f16861b1SSeth Forshee 	DA9034_LDO(15, 1800, 3300, 100, LDO1514, 4, 4, OVER3, 1),
411f16861b1SSeth Forshee 	DA9034_LDO(5, 3100, 3100, 0, INVAL, 0, 0, OVER3, 7), /* fixed @3.1V */
412f16861b1SSeth Forshee 
413f16861b1SSeth Forshee 	/* DA9035 */
414f16861b1SSeth Forshee 	DA9035_DVC(BUCK3, 1800, 2200, 100, 3DTV1, 3, VCC2, 0, OVER3, 3),
415f16861b1SSeth Forshee };
416f16861b1SSeth Forshee 
find_regulator_info(int id)417f16861b1SSeth Forshee static inline struct da903x_regulator_info *find_regulator_info(int id)
418f16861b1SSeth Forshee {
419f16861b1SSeth Forshee 	struct da903x_regulator_info *ri;
420f16861b1SSeth Forshee 	int i;
421f16861b1SSeth Forshee 
422f16861b1SSeth Forshee 	for (i = 0; i < ARRAY_SIZE(da903x_regulator_info); i++) {
423f16861b1SSeth Forshee 		ri = &da903x_regulator_info[i];
424f16861b1SSeth Forshee 		if (ri->desc.id == id)
425f16861b1SSeth Forshee 			return ri;
426f16861b1SSeth Forshee 	}
427f16861b1SSeth Forshee 	return NULL;
428f16861b1SSeth Forshee }
429f16861b1SSeth Forshee 
da903x_regulator_probe(struct platform_device * pdev)430f16861b1SSeth Forshee static int da903x_regulator_probe(struct platform_device *pdev)
431f16861b1SSeth Forshee {
432f16861b1SSeth Forshee 	struct da903x_regulator_info *ri = NULL;
433f16861b1SSeth Forshee 	struct regulator_dev *rdev;
434f16861b1SSeth Forshee 	struct regulator_config config = { };
435f16861b1SSeth Forshee 
436f16861b1SSeth Forshee 	ri = find_regulator_info(pdev->id);
437f16861b1SSeth Forshee 	if (ri == NULL) {
438f16861b1SSeth Forshee 		dev_err(&pdev->dev, "invalid regulator ID specified\n");
439f16861b1SSeth Forshee 		return -EINVAL;
440f16861b1SSeth Forshee 	}
441f16861b1SSeth Forshee 
442f16861b1SSeth Forshee 	/* Workaround for the weird LDO12 voltage setting */
443f16861b1SSeth Forshee 	if (ri->desc.id == DA9034_ID_LDO12) {
444f16861b1SSeth Forshee 		ri->desc.ops = &da9034_regulator_ldo12_ops;
445f16861b1SSeth Forshee 		ri->desc.n_voltages = 16;
446f16861b1SSeth Forshee 		ri->desc.linear_ranges = da9034_ldo12_ranges;
447f16861b1SSeth Forshee 		ri->desc.n_linear_ranges = ARRAY_SIZE(da9034_ldo12_ranges);
448f16861b1SSeth Forshee 	}
449f16861b1SSeth Forshee 
450f16861b1SSeth Forshee 	if (ri->desc.id == DA9030_ID_LDO14)
451f16861b1SSeth Forshee 		ri->desc.ops = &da9030_regulator_ldo14_ops;
452f16861b1SSeth Forshee 
453f16861b1SSeth Forshee 	if (ri->desc.id == DA9030_ID_LDO1 || ri->desc.id == DA9030_ID_LDO15)
454f16861b1SSeth Forshee 		ri->desc.ops = &da9030_regulator_ldo1_15_ops;
455f16861b1SSeth Forshee 
456f16861b1SSeth Forshee 	config.dev = &pdev->dev;
457f16861b1SSeth Forshee 	config.init_data = dev_get_platdata(&pdev->dev);
458f16861b1SSeth Forshee 	config.driver_data = ri;
459f16861b1SSeth Forshee 
460f16861b1SSeth Forshee 	rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
461f16861b1SSeth Forshee 	if (IS_ERR(rdev)) {
462f16861b1SSeth Forshee 		dev_err(&pdev->dev, "failed to register regulator %s\n",
463f16861b1SSeth Forshee 				ri->desc.name);
464f16861b1SSeth Forshee 		return PTR_ERR(rdev);
465f16861b1SSeth Forshee 	}
466f16861b1SSeth Forshee 
467f16861b1SSeth Forshee 	platform_set_drvdata(pdev, rdev);
468f16861b1SSeth Forshee 	return 0;
469f16861b1SSeth Forshee }
470f16861b1SSeth Forshee 
471f16861b1SSeth Forshee static struct platform_driver da903x_regulator_driver = {
472f16861b1SSeth Forshee 	.driver	= {
473f16861b1SSeth Forshee 		.name	= "da903x-regulator",
474*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
475f16861b1SSeth Forshee 	},
476f16861b1SSeth Forshee 	.probe		= da903x_regulator_probe,
477f16861b1SSeth Forshee };
478f16861b1SSeth Forshee 
da903x_regulator_init(void)479f16861b1SSeth Forshee static int __init da903x_regulator_init(void)
480f16861b1SSeth Forshee {
481f16861b1SSeth Forshee 	return platform_driver_register(&da903x_regulator_driver);
482f16861b1SSeth Forshee }
483f16861b1SSeth Forshee subsys_initcall(da903x_regulator_init);
484f16861b1SSeth Forshee 
da903x_regulator_exit(void)485f16861b1SSeth Forshee static void __exit da903x_regulator_exit(void)
486f16861b1SSeth Forshee {
487f16861b1SSeth Forshee 	platform_driver_unregister(&da903x_regulator_driver);
488f16861b1SSeth Forshee }
489f16861b1SSeth Forshee module_exit(da903x_regulator_exit);
490f16861b1SSeth Forshee 
491f16861b1SSeth Forshee MODULE_LICENSE("GPL");
492f16861b1SSeth Forshee MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"
493f16861b1SSeth Forshee 	      "Mike Rapoport <mike@compulab.co.il>");
494f16861b1SSeth Forshee MODULE_DESCRIPTION("Regulator Driver for Dialog Semiconductor DA903X PMIC");
495f16861b1SSeth Forshee MODULE_ALIAS("platform:da903x-regulator");
496