2 * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
4 * http://www.active-semi.com/products/power-management-units/act88xx/
6 * Copyright (C) 2013 Atmel Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
22 #include <linux/err.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/act8865.h>
27 #include <linux/of_device.h>
28 #include <linux/regulator/of_regulator.h>
29 #include <linux/regmap.h>
32 * ACT8865 Global Register Map.
34 #define ACT8865_SYS_MODE 0x00
35 #define ACT8865_SYS_CTRL 0x01
36 #define ACT8865_DCDC1_VSET1 0x20
37 #define ACT8865_DCDC1_VSET2 0x21
38 #define ACT8865_DCDC1_CTRL 0x22
39 #define ACT8865_DCDC2_VSET1 0x30
40 #define ACT8865_DCDC2_VSET2 0x31
41 #define ACT8865_DCDC2_CTRL 0x32
42 #define ACT8865_DCDC3_VSET1 0x40
43 #define ACT8865_DCDC3_VSET2 0x41
44 #define ACT8865_DCDC3_CTRL 0x42
45 #define ACT8865_LDO1_VSET 0x50
46 #define ACT8865_LDO1_CTRL 0x51
47 #define ACT8865_LDO2_VSET 0x54
48 #define ACT8865_LDO2_CTRL 0x55
49 #define ACT8865_LDO3_VSET 0x60
50 #define ACT8865_LDO3_CTRL 0x61
51 #define ACT8865_LDO4_VSET 0x64
52 #define ACT8865_LDO4_CTRL 0x65
57 #define ACT8865_ENA 0x80 /* ON - [7] */
58 #define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
61 * ACT8865 voltage number
63 #define ACT8865_VOLTAGE_NUM 64
66 struct regmap
*regmap
;
69 static const struct regmap_config act8865_regmap_config
= {
74 static const struct regulator_linear_range act8865_voltage_ranges
[] = {
75 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
76 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
77 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
80 static struct regulator_ops act8865_ops
= {
81 .list_voltage
= regulator_list_voltage_linear_range
,
82 .map_voltage
= regulator_map_voltage_linear_range
,
83 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
84 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
85 .enable
= regulator_enable_regmap
,
86 .disable
= regulator_disable_regmap
,
87 .is_enabled
= regulator_is_enabled_regmap
,
90 #define ACT88xx_REG(_name, _family, _id, _vsel_reg) \
91 [_family##_ID_##_id] = { \
93 .id = _family##_ID_##_id, \
94 .type = REGULATOR_VOLTAGE, \
95 .ops = &act8865_ops, \
96 .n_voltages = ACT8865_VOLTAGE_NUM, \
97 .linear_ranges = act8865_voltage_ranges, \
98 .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
99 .vsel_reg = _family##_##_id##_##_vsel_reg, \
100 .vsel_mask = ACT8865_VSEL_MASK, \
101 .enable_reg = _family##_##_id##_CTRL, \
102 .enable_mask = ACT8865_ENA, \
103 .owner = THIS_MODULE, \
106 static const struct regulator_desc act8865_regulators
[] = {
107 ACT88xx_REG("DCDC_REG1", ACT8865
, DCDC1
, VSET1
),
108 ACT88xx_REG("DCDC_REG2", ACT8865
, DCDC2
, VSET1
),
109 ACT88xx_REG("DCDC_REG3", ACT8865
, DCDC3
, VSET1
),
110 ACT88xx_REG("LDO_REG1", ACT8865
, LDO1
, VSET
),
111 ACT88xx_REG("LDO_REG2", ACT8865
, LDO2
, VSET
),
112 ACT88xx_REG("LDO_REG3", ACT8865
, LDO3
, VSET
),
113 ACT88xx_REG("LDO_REG4", ACT8865
, LDO4
, VSET
),
117 static const struct of_device_id act8865_dt_ids
[] = {
118 { .compatible
= "active-semi,act8865", .data
= (void *)ACT8865
},
121 MODULE_DEVICE_TABLE(of
, act8865_dt_ids
);
123 static struct of_regulator_match act8865_matches
[] = {
124 [ACT8865_ID_DCDC1
] = { .name
= "DCDC_REG1"},
125 [ACT8865_ID_DCDC2
] = { .name
= "DCDC_REG2"},
126 [ACT8865_ID_DCDC3
] = { .name
= "DCDC_REG3"},
127 [ACT8865_ID_LDO1
] = { .name
= "LDO_REG1"},
128 [ACT8865_ID_LDO2
] = { .name
= "LDO_REG2"},
129 [ACT8865_ID_LDO3
] = { .name
= "LDO_REG3"},
130 [ACT8865_ID_LDO4
] = { .name
= "LDO_REG4"},
133 static int act8865_pdata_from_dt(struct device
*dev
,
134 struct device_node
**of_node
,
135 struct act8865_platform_data
*pdata
,
136 struct of_regulator_match
*matches
,
140 struct device_node
*np
;
141 struct act8865_regulator_data
*regulator
;
143 np
= of_get_child_by_name(dev
->of_node
, "regulators");
145 dev_err(dev
, "missing 'regulators' subnode in DT\n");
149 matched
= of_regulator_match(dev
, np
, matches
, num_matches
);
154 pdata
->regulators
= devm_kzalloc(dev
,
155 sizeof(struct act8865_regulator_data
) *
156 num_matches
, GFP_KERNEL
);
157 if (!pdata
->regulators
)
160 pdata
->num_regulators
= num_matches
;
161 regulator
= pdata
->regulators
;
163 for (i
= 0; i
< num_matches
; i
++) {
165 regulator
->name
= matches
[i
].name
;
166 regulator
->platform_data
= matches
[i
].init_data
;
167 of_node
[i
] = matches
[i
].of_node
;
174 static inline int act8865_pdata_from_dt(struct device
*dev
,
175 struct device_node
**of_node
,
176 struct act8865_platform_data
*pdata
)
182 static struct regulator_init_data
183 *act8865_get_init_data(int id
, struct act8865_platform_data
*pdata
)
190 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
191 if (pdata
->regulators
[i
].id
== id
)
192 return pdata
->regulators
[i
].platform_data
;
198 static int act8865_pmic_probe(struct i2c_client
*client
,
199 const struct i2c_device_id
*i2c_id
)
201 static const struct regulator_desc
*regulators
;
202 struct act8865_platform_data pdata_of
, *pdata
;
203 struct of_regulator_match
*matches
;
204 struct device
*dev
= &client
->dev
;
205 struct device_node
**of_node
;
206 int i
, ret
, num_regulators
;
207 struct act8865
*act8865
;
210 pdata
= dev_get_platdata(dev
);
212 if (dev
->of_node
&& !pdata
) {
213 const struct of_device_id
*id
;
215 id
= of_match_device(of_match_ptr(act8865_dt_ids
), dev
);
219 type
= (unsigned long) id
->data
;
221 type
= i2c_id
->driver_data
;
226 matches
= act8865_matches
;
227 regulators
= act8865_regulators
;
228 num_regulators
= ARRAY_SIZE(act8865_regulators
);
231 dev_err(dev
, "invalid device id %lu\n", type
);
235 of_node
= devm_kzalloc(dev
, sizeof(struct device_node
*) *
236 num_regulators
, GFP_KERNEL
);
240 if (dev
->of_node
&& !pdata
) {
241 ret
= act8865_pdata_from_dt(dev
, of_node
, &pdata_of
, matches
,
249 if (pdata
->num_regulators
> num_regulators
) {
250 dev_err(dev
, "too many regulators: %d\n",
251 pdata
->num_regulators
);
255 act8865
= devm_kzalloc(dev
, sizeof(struct act8865
), GFP_KERNEL
);
259 act8865
->regmap
= devm_regmap_init_i2c(client
, &act8865_regmap_config
);
260 if (IS_ERR(act8865
->regmap
)) {
261 ret
= PTR_ERR(act8865
->regmap
);
262 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
267 /* Finally register devices */
268 for (i
= 0; i
< num_regulators
; i
++) {
269 const struct regulator_desc
*desc
= ®ulators
[i
];
270 struct regulator_config config
= { };
271 struct regulator_dev
*rdev
;
274 config
.init_data
= act8865_get_init_data(desc
->id
, pdata
);
275 config
.of_node
= of_node
[i
];
276 config
.driver_data
= act8865
;
277 config
.regmap
= act8865
->regmap
;
279 rdev
= devm_regulator_register(&client
->dev
, desc
, &config
);
281 dev_err(dev
, "failed to register %s\n", desc
->name
);
282 return PTR_ERR(rdev
);
286 i2c_set_clientdata(client
, act8865
);
287 devm_kfree(dev
, of_node
);
292 static const struct i2c_device_id act8865_ids
[] = {
293 { .name
= "act8865", .driver_data
= ACT8865
},
296 MODULE_DEVICE_TABLE(i2c
, act8865_ids
);
298 static struct i2c_driver act8865_pmic_driver
= {
301 .owner
= THIS_MODULE
,
303 .probe
= act8865_pmic_probe
,
304 .id_table
= act8865_ids
,
307 module_i2c_driver(act8865_pmic_driver
);
309 MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
310 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
311 MODULE_LICENSE("GPL v2");