1 /*
2  * Voltage regulation driver for active-semi ACT8945A PMIC
3  *
4  * Copyright (C) 2015 Atmel Corporation
5  *
6  * Author: Wenyou Yang <wenyou.yang@atmel.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  */
14 
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/machine.h>
21 
22 /**
23  * ACT8945A Global Register Map.
24  */
25 #define ACT8945A_SYS_MODE	0x00
26 #define ACT8945A_SYS_CTRL	0x01
27 #define ACT8945A_SYS_UNLK_REGS	0x0b
28 #define ACT8945A_DCDC1_VSET1	0x20
29 #define ACT8945A_DCDC1_VSET2	0x21
30 #define ACT8945A_DCDC1_CTRL	0x22
31 #define ACT8945A_DCDC2_VSET1	0x30
32 #define ACT8945A_DCDC2_VSET2	0x31
33 #define ACT8945A_DCDC2_CTRL	0x32
34 #define ACT8945A_DCDC3_VSET1	0x40
35 #define ACT8945A_DCDC3_VSET2	0x41
36 #define ACT8945A_DCDC3_CTRL	0x42
37 #define ACT8945A_LDO1_VSET	0x50
38 #define ACT8945A_LDO1_CTRL	0x51
39 #define ACT8945A_LDO2_VSET	0x54
40 #define ACT8945A_LDO2_CTRL	0x55
41 #define ACT8945A_LDO3_VSET	0x60
42 #define ACT8945A_LDO3_CTRL	0x61
43 #define ACT8945A_LDO4_VSET	0x64
44 #define ACT8945A_LDO4_CTRL	0x65
45 
46 /**
47  * Field Definitions.
48  */
49 #define ACT8945A_ENA		0x80	/* ON - [7] */
50 #define ACT8945A_VSEL_MASK	0x3F	/* VSET - [5:0] */
51 
52 /**
53  * ACT8945A Voltage Number
54  */
55 #define ACT8945A_VOLTAGE_NUM	64
56 
57 enum {
58 	ACT8945A_ID_DCDC1,
59 	ACT8945A_ID_DCDC2,
60 	ACT8945A_ID_DCDC3,
61 	ACT8945A_ID_LDO1,
62 	ACT8945A_ID_LDO2,
63 	ACT8945A_ID_LDO3,
64 	ACT8945A_ID_LDO4,
65 	ACT8945A_REG_NUM,
66 };
67 
68 static const struct regulator_linear_range act8945a_voltage_ranges[] = {
69 	REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
70 	REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
71 	REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
72 };
73 
74 static const struct regulator_ops act8945a_ops = {
75 	.list_voltage		= regulator_list_voltage_linear_range,
76 	.map_voltage		= regulator_map_voltage_linear_range,
77 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
78 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
79 	.enable			= regulator_enable_regmap,
80 	.disable		= regulator_disable_regmap,
81 	.is_enabled		= regulator_is_enabled_regmap,
82 };
83 
84 #define ACT89xx_REG(_name, _family, _id, _vsel_reg, _supply)		\
85 	[_family##_ID_##_id] = {					\
86 		.name			= _name,			\
87 		.supply_name		= _supply,			\
88 		.of_match		= of_match_ptr("REG_"#_id),	\
89 		.regulators_node	= of_match_ptr("regulators"),	\
90 		.id			= _family##_ID_##_id,		\
91 		.type			= REGULATOR_VOLTAGE,		\
92 		.ops			= &act8945a_ops,		\
93 		.n_voltages		= ACT8945A_VOLTAGE_NUM,		\
94 		.linear_ranges		= act8945a_voltage_ranges,	\
95 		.n_linear_ranges	= ARRAY_SIZE(act8945a_voltage_ranges), \
96 		.vsel_reg		= _family##_##_id##_##_vsel_reg, \
97 		.vsel_mask		= ACT8945A_VSEL_MASK,		\
98 		.enable_reg		= _family##_##_id##_CTRL,	\
99 		.enable_mask		= ACT8945A_ENA,			\
100 		.owner			= THIS_MODULE,			\
101 	}
102 
103 static const struct regulator_desc act8945a_regulators[] = {
104 	ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET1, "vp1"),
105 	ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET1, "vp2"),
106 	ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET1, "vp3"),
107 	ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
108 	ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
109 	ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
110 	ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
111 };
112 
113 static const struct regulator_desc act8945a_alt_regulators[] = {
114 	ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET2, "vp1"),
115 	ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET2, "vp2"),
116 	ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET2, "vp3"),
117 	ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
118 	ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
119 	ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
120 	ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
121 };
122 
123 static int act8945a_pmic_probe(struct platform_device *pdev)
124 {
125 	struct regulator_config config = { };
126 	const struct regulator_desc *regulators;
127 	struct regulator_dev *rdev;
128 	struct regmap *regmap;
129 	int i, num_regulators;
130 	bool voltage_select;
131 
132 	regmap = dev_get_regmap(pdev->dev.parent, NULL);
133 	if (!regmap) {
134 		dev_err(&pdev->dev,
135 			"could not retrieve regmap from parent device\n");
136 		return -EINVAL;
137 	}
138 
139 	voltage_select = of_property_read_bool(pdev->dev.parent->of_node,
140 					       "active-semi,vsel-high");
141 
142 	if (voltage_select) {
143 		regulators = act8945a_alt_regulators;
144 		num_regulators = ARRAY_SIZE(act8945a_alt_regulators);
145 	} else {
146 		regulators = act8945a_regulators;
147 		num_regulators = ARRAY_SIZE(act8945a_regulators);
148 	}
149 
150 	config.dev = &pdev->dev;
151 	config.dev->of_node = pdev->dev.parent->of_node;
152 	for (i = 0; i < num_regulators; i++) {
153 		rdev = devm_regulator_register(&pdev->dev, &regulators[i], &config);
154 		if (IS_ERR(rdev)) {
155 			dev_err(&pdev->dev,
156 				"failed to register %s regulator\n",
157 				regulators[i].name);
158 			return PTR_ERR(rdev);
159 		}
160 	}
161 
162 	/* Unlock expert registers. */
163 	return regmap_write(regmap, ACT8945A_SYS_UNLK_REGS, 0xef);
164 }
165 
166 static struct platform_driver act8945a_pmic_driver = {
167 	.driver = {
168 		.name = "act8945a-regulator",
169 	},
170 	.probe = act8945a_pmic_probe,
171 };
172 module_platform_driver(act8945a_pmic_driver);
173 
174 MODULE_DESCRIPTION("Active-semi ACT8945A voltage regulator driver");
175 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
176 MODULE_LICENSE("GPL");
177