regulator: isl9305: Add Intersil ISL9305/H driver
[linux-block.git] / drivers / regulator / isl9305.c
CommitLineData
dec38b5c
MB
1/*
2 * isl9305 - Intersil ISL9305 DCDC regulator
3 *
4 * Copyright 2014 Linaro Ltd
5 *
6 * Author: Mark Brown <broonie@kernel.org>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <linux/module.h>
15#include <linux/err.h>
16#include <linux/i2c.h>
17#include <linux/of.h>
18#include <linux/platform_data/isl9305.h>
19#include <linux/regmap.h>
20#include <linux/regulator/driver.h>
21#include <linux/regulator/of_regulator.h>
22#include <linux/slab.h>
23
24/*
25 * Registers
26 */
27#define ISL9305_DCD1OUT 0x0
28#define ISL9305_DCD2OUT 0x1
29#define ISL9305_LDO1OUT 0x2
30#define ISL9305_LDO2OUT 0x3
31#define ISL9305_DCD_PARAMETER 0x4
32#define ISL9305_SYSTEM_PARAMETER 0x5
33#define ISL9305_DCD_SRCTL 0x6
34
35#define ISL9305_MAX_REG ISL9305_DCD_SRCTL
36
37/*
38 * DCD_PARAMETER
39 */
40#define ISL9305_DCD_PHASE 0x40
41#define ISL9305_DCD2_ULTRA 0x20
42#define ISL9305_DCD1_ULTRA 0x10
43#define ISL9305_DCD2_BLD 0x08
44#define ISL9305_DCD1_BLD 0x04
45#define ISL9305_DCD2_MODE 0x02
46#define ISL9305_DCD1_MODE 0x01
47
48/*
49 * SYSTEM_PARAMETER
50 */
51#define ISL9305_I2C_EN 0x40
52#define ISL9305_DCDPOR_MASK 0x30
53#define ISL9305_LDO2_EN 0x08
54#define ISL9305_LDO1_EN 0x04
55#define ISL9305_DCD2_EN 0x02
56#define ISL9305_DCD1_EN 0x01
57
58/*
59 * DCD_SRCTL
60 */
61#define ISL9305_DCD2SR_MASK 0xc0
62#define ISL9305_DCD1SR_MASK 0x07
63
64static const struct regulator_ops isl9305_ops = {
65 .enable = regulator_enable_regmap,
66 .disable = regulator_disable_regmap,
67 .is_enabled = regulator_is_enabled_regmap,
68 .list_voltage = regulator_list_voltage_linear,
69 .get_voltage_sel = regulator_get_voltage_sel_regmap,
70 .set_voltage_sel = regulator_set_voltage_sel_regmap,
71};
72
73static const struct regulator_desc isl9305_regulators[] = {
74 [ISL9305_DCD1] = {
75 .name = "DCD1",
76 .n_voltages = 0x70,
77 .min_uV = 825000,
78 .uV_step = 25000,
79 .vsel_reg = ISL9305_DCD1OUT,
80 .vsel_mask = 0x7f,
81 .enable_reg = ISL9305_SYSTEM_PARAMETER,
82 .enable_mask = ISL9305_DCD1_EN,
83 .supply_name = "VINDCD1",
84 .ops = &isl9305_ops,
85 },
86 [ISL9305_DCD2] = {
87 .name = "DCD2",
88 .n_voltages = 0x70,
89 .min_uV = 825000,
90 .uV_step = 25000,
91 .vsel_reg = ISL9305_DCD2OUT,
92 .vsel_mask = 0x7f,
93 .enable_reg = ISL9305_SYSTEM_PARAMETER,
94 .enable_mask = ISL9305_DCD2_EN,
95 .supply_name = "VINDCD2",
96 .ops = &isl9305_ops,
97 },
98 [ISL9305_LDO1] = {
99 .name = "LDO1",
100 .n_voltages = 0x37,
101 .min_uV = 900000,
102 .uV_step = 50000,
103 .vsel_reg = ISL9305_LDO1OUT,
104 .vsel_mask = 0x3f,
105 .enable_reg = ISL9305_SYSTEM_PARAMETER,
106 .enable_mask = ISL9305_LDO1_EN,
107 .supply_name = "VINLDO1",
108 .ops = &isl9305_ops,
109 },
110 [ISL9305_LDO2] = {
111 .name = "LDO2",
112 .n_voltages = 0x37,
113 .min_uV = 900000,
114 .uV_step = 50000,
115 .vsel_reg = ISL9305_LDO2OUT,
116 .vsel_mask = 0x3f,
117 .enable_reg = ISL9305_SYSTEM_PARAMETER,
118 .enable_mask = ISL9305_LDO2_EN,
119 .supply_name = "VINLDO2",
120 .ops = &isl9305_ops,
121 },
122};
123
124#ifdef CONFIG_OF
125static struct of_regulator_match isl9305_reg_matches[] = {
126 [ISL9305_DCD1] = { .name = "dcd1" },
127 [ISL9305_DCD2] = { .name = "dcd2" },
128 [ISL9305_LDO1] = { .name = "ldo1" },
129 [ISL9305_LDO2] = { .name = "ldo2" },
130};
131
132static struct of_regulator_match *isl9305_parse_dt(struct i2c_client *i2c)
133{
134 struct device_node *node = i2c->dev.of_node;
135 struct of_regulator_match *matches;
136 struct device_node *regs;
137 int count;
138
139 regs = of_get_child_by_name(node, "regulators");
140 if (!regs)
141 return NULL;
142
143 matches = devm_kmemdup(&i2c->dev, isl9305_reg_matches,
144 sizeof(isl9305_reg_matches), GFP_KERNEL);
145 if (!matches)
146 return NULL;
147
148 count = of_regulator_match(&i2c->dev, regs, matches,
149 ARRAY_SIZE(isl9305_reg_matches));
150 of_node_put(regs);
151 if ((count < 0) || (count > ARRAY_SIZE(isl9305_reg_matches)))
152 return NULL;
153
154 return matches;
155}
156#else
157static struct of_regulator_match *isl9305_parse_dt(struct i2c_client *i2c)
158{
159 return NULL;
160}
161#endif
162
163static const struct regmap_config isl9305_regmap = {
164 .reg_bits = 8,
165 .val_bits = 8,
166
167 .max_register = ISL9305_MAX_REG,
168 .cache_type = REGCACHE_RBTREE,
169};
170
171static int isl9305_i2c_probe(struct i2c_client *i2c,
172 const struct i2c_device_id *id)
173{
174 struct regulator_config config = { };
175 struct isl9305_pdata *pdata = i2c->dev.platform_data;
176 struct of_regulator_match *of_matches;
177 struct regulator_dev *rdev;
178 struct regmap *regmap;
179 int i, ret;
180
181 of_matches = isl9305_parse_dt(i2c);
182
183 regmap = devm_regmap_init_i2c(i2c, &isl9305_regmap);
184 if (IS_ERR(regmap)) {
185 ret = PTR_ERR(regmap);
186 dev_err(&i2c->dev, "Failed to create regmap: %d\n", ret);
187 return ret;
188 }
189
190 config.dev = &i2c->dev;
191
192 for (i = 0; i < ARRAY_SIZE(isl9305_regulators); i++) {
193 config.of_node = NULL;
194 config.init_data = NULL;
195
196 if (of_matches) {
197 config.init_data = of_matches[i].init_data;
198 config.of_node = of_matches[i].of_node;
199 }
200
201 if (!config.init_data && pdata)
202 config.init_data = pdata->init_data[i];
203
204 rdev = devm_regulator_register(&i2c->dev,
205 &isl9305_regulators[i],
206 &config);
207 if (IS_ERR(rdev)) {
208 ret = PTR_ERR(rdev);
209 dev_err(&i2c->dev, "Failed to register %s: %d\n",
210 isl9305_regulators[i].name, ret);
211 return ret;
212 }
213 }
214
215 return 0;
216}
217
218#ifdef CONFIG_OF
219static const struct of_device_id isl9305_dt_ids[] = {
220 { .compatible = "isl,isl9305" },
221 { .compatible = "isl,isl9305h" },
222 {},
223};
224#endif
225
226static const struct i2c_device_id isl9305_i2c_id[] = {
227 { "isl9305", },
228 { "isl9305h", },
229 { }
230};
231MODULE_DEVICE_TABLE(i2c, isl9305_i2c_id);
232
233static struct i2c_driver isl9305_regulator_driver = {
234 .driver = {
235 .name = "isl9305",
236 .owner = THIS_MODULE,
237 .of_match_table = of_match_ptr(isl9305_dt_ids),
238 },
239 .probe = isl9305_i2c_probe,
240 .id_table = isl9305_i2c_id,
241};
242
243module_i2c_driver(isl9305_regulator_driver);
244
245MODULE_AUTHOR("Mark Brown");
246MODULE_DESCRIPTION("Intersil ISL9305 DCDC regulator");
247MODULE_LICENSE("GPL");