xref: /openbmc/linux/drivers/regulator/isl9305.c (revision be709d48)
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 
64 static 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 
73 static const struct regulator_desc isl9305_regulators[] = {
74 	[ISL9305_DCD1] = {
75 		.name =		"DCD1",
76 		.of_match =	of_match_ptr("dcd1"),
77 		.regulators_node = of_match_ptr("regulators"),
78 		.n_voltages =	0x70,
79 		.min_uV =	825000,
80 		.uV_step =	25000,
81 		.vsel_reg =	ISL9305_DCD1OUT,
82 		.vsel_mask =	0x7f,
83 		.enable_reg =	ISL9305_SYSTEM_PARAMETER,
84 		.enable_mask =	ISL9305_DCD1_EN,
85 		.supply_name =	"VINDCD1",
86 		.ops =		&isl9305_ops,
87 		.owner =	THIS_MODULE,
88 	},
89 	[ISL9305_DCD2] = {
90 		.name =		"DCD2",
91 		.of_match =	of_match_ptr("dcd2"),
92 		.regulators_node = of_match_ptr("regulators"),
93 		.n_voltages =	0x70,
94 		.min_uV =	825000,
95 		.uV_step =	25000,
96 		.vsel_reg =	ISL9305_DCD2OUT,
97 		.vsel_mask =	0x7f,
98 		.enable_reg =	ISL9305_SYSTEM_PARAMETER,
99 		.enable_mask =	ISL9305_DCD2_EN,
100 		.supply_name =	"VINDCD2",
101 		.ops =		&isl9305_ops,
102 		.owner =	THIS_MODULE,
103 	},
104 	[ISL9305_LDO1] = {
105 		.name =		"LDO1",
106 		.of_match =	of_match_ptr("ldo1"),
107 		.regulators_node = of_match_ptr("regulators"),
108 		.n_voltages =	0x37,
109 		.min_uV =	900000,
110 		.uV_step =	50000,
111 		.vsel_reg =	ISL9305_LDO1OUT,
112 		.vsel_mask =	0x3f,
113 		.enable_reg =	ISL9305_SYSTEM_PARAMETER,
114 		.enable_mask =	ISL9305_LDO1_EN,
115 		.supply_name =	"VINLDO1",
116 		.ops =		&isl9305_ops,
117 		.owner =	THIS_MODULE,
118 	},
119 	[ISL9305_LDO2] = {
120 		.name =		"LDO2",
121 		.of_match =	of_match_ptr("ldo2"),
122 		.regulators_node = of_match_ptr("regulators"),
123 		.n_voltages =	0x37,
124 		.min_uV =	900000,
125 		.uV_step =	50000,
126 		.vsel_reg =	ISL9305_LDO2OUT,
127 		.vsel_mask =	0x3f,
128 		.enable_reg =	ISL9305_SYSTEM_PARAMETER,
129 		.enable_mask =	ISL9305_LDO2_EN,
130 		.supply_name =	"VINLDO2",
131 		.ops =		&isl9305_ops,
132 		.owner =	THIS_MODULE,
133 	},
134 };
135 
136 static const struct regmap_config isl9305_regmap = {
137 	.reg_bits = 8,
138 	.val_bits = 8,
139 
140 	.max_register = ISL9305_MAX_REG,
141 	.cache_type = REGCACHE_RBTREE,
142 };
143 
144 static int isl9305_i2c_probe(struct i2c_client *i2c,
145 			     const struct i2c_device_id *id)
146 {
147 	struct regulator_config config = { };
148 	struct isl9305_pdata *pdata = i2c->dev.platform_data;
149 	struct regulator_dev *rdev;
150 	struct regmap *regmap;
151 	int i, ret;
152 
153 	regmap = devm_regmap_init_i2c(i2c, &isl9305_regmap);
154 	if (IS_ERR(regmap)) {
155 		ret = PTR_ERR(regmap);
156 		dev_err(&i2c->dev, "Failed to create regmap: %d\n", ret);
157 		return ret;
158 	}
159 
160 	config.dev = &i2c->dev;
161 
162 	for (i = 0; i < ARRAY_SIZE(isl9305_regulators); i++) {
163 		if (pdata)
164 			config.init_data = pdata->init_data[i];
165 		else
166 			config.init_data = NULL;
167 
168 		rdev = devm_regulator_register(&i2c->dev,
169 					       &isl9305_regulators[i],
170 					       &config);
171 		if (IS_ERR(rdev)) {
172 			ret = PTR_ERR(rdev);
173 			dev_err(&i2c->dev, "Failed to register %s: %d\n",
174 				isl9305_regulators[i].name, ret);
175 			return ret;
176 		}
177 	}
178 
179 	return 0;
180 }
181 
182 #ifdef CONFIG_OF
183 static const struct of_device_id isl9305_dt_ids[] = {
184 	{ .compatible = "isl,isl9305" }, /* for backward compat., don't use */
185 	{ .compatible = "isil,isl9305" },
186 	{ .compatible = "isl,isl9305h" }, /* for backward compat., don't use */
187 	{ .compatible = "isil,isl9305h" },
188 	{},
189 };
190 MODULE_DEVICE_TABLE(of, isl9305_dt_ids);
191 #endif
192 
193 static const struct i2c_device_id isl9305_i2c_id[] = {
194 	{ "isl9305", },
195 	{ "isl9305h", },
196 	{ }
197 };
198 MODULE_DEVICE_TABLE(i2c, isl9305_i2c_id);
199 
200 static struct i2c_driver isl9305_regulator_driver = {
201 	.driver = {
202 		.name = "isl9305",
203 		.of_match_table	= of_match_ptr(isl9305_dt_ids),
204 	},
205 	.probe = isl9305_i2c_probe,
206 	.id_table = isl9305_i2c_id,
207 };
208 
209 module_i2c_driver(isl9305_regulator_driver);
210 
211 MODULE_AUTHOR("Mark Brown");
212 MODULE_DESCRIPTION("Intersil ISL9305 DCDC regulator");
213 MODULE_LICENSE("GPL");
214