1/*
2 * max1586.c  --  Voltage and current regulation for the Maxim 1586
3 *
4 * Copyright (C) 2008 Robert Jarzmik
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20#include <linux/module.h>
21#include <linux/err.h>
22#include <linux/i2c.h>
23#include <linux/platform_device.h>
24#include <linux/regulator/driver.h>
25#include <linux/slab.h>
26#include <linux/regulator/max1586.h>
27#include <linux/of_device.h>
28#include <linux/regulator/of_regulator.h>
29
30#define MAX1586_V3_MAX_VSEL 31
31#define MAX1586_V6_MAX_VSEL 3
32
33#define MAX1586_V3_MIN_UV   700000
34#define MAX1586_V3_MAX_UV  1475000
35
36#define MAX1586_V6_MIN_UV        0
37#define MAX1586_V6_MAX_UV  3000000
38
39#define I2C_V3_SELECT (0 << 5)
40#define I2C_V6_SELECT (1 << 5)
41
42struct max1586_data {
43	struct i2c_client *client;
44
45	/* min/max V3 voltage */
46	unsigned int min_uV;
47	unsigned int max_uV;
48
49	unsigned int v3_curr_sel;
50	unsigned int v6_curr_sel;
51};
52
53/*
54 * V6 voltage
55 * On I2C bus, sending a "x" byte to the max1586 means :
56 *   set V6 to either 0V, 1.8V, 2.5V, 3V depending on (x & 0x3)
57 * As regulator framework doesn't accept voltages to be 0V, we use 1uV.
58 */
59static const unsigned int v6_voltages_uv[] = { 1, 1800000, 2500000, 3000000 };
60
61/*
62 * V3 voltage
63 * On I2C bus, sending a "x" byte to the max1586 means :
64 *   set V3 to 0.700V + (x & 0x1f) * 0.025V
65 * This voltage can be increased by external resistors
66 * R24 and R25=100kOhm as described in the data sheet.
67 * The gain is approximately: 1 + R24/R25 + R24/185.5kOhm
68 */
69static int max1586_v3_get_voltage_sel(struct regulator_dev *rdev)
70{
71	struct max1586_data *max1586 = rdev_get_drvdata(rdev);
72
73	return max1586->v3_curr_sel;
74}
75
76static int max1586_v3_set_voltage_sel(struct regulator_dev *rdev,
77				      unsigned selector)
78{
79	struct max1586_data *max1586 = rdev_get_drvdata(rdev);
80	struct i2c_client *client = max1586->client;
81	int ret;
82	u8 v3_prog;
83
84	dev_dbg(&client->dev, "changing voltage v3 to %dmv\n",
85		regulator_list_voltage_linear(rdev, selector) / 1000);
86
87	v3_prog = I2C_V3_SELECT | (u8) selector;
88	ret = i2c_smbus_write_byte(client, v3_prog);
89	if (ret)
90		return ret;
91
92	max1586->v3_curr_sel = selector;
93
94	return 0;
95}
96
97static int max1586_v6_get_voltage_sel(struct regulator_dev *rdev)
98{
99	struct max1586_data *max1586 = rdev_get_drvdata(rdev);
100
101	return max1586->v6_curr_sel;
102}
103
104static int max1586_v6_set_voltage_sel(struct regulator_dev *rdev,
105				      unsigned int selector)
106{
107	struct max1586_data *max1586 = rdev_get_drvdata(rdev);
108	struct i2c_client *client = max1586->client;
109	u8 v6_prog;
110	int ret;
111
112	dev_dbg(&client->dev, "changing voltage v6 to %dmv\n",
113		rdev->desc->volt_table[selector] / 1000);
114
115	v6_prog = I2C_V6_SELECT | (u8) selector;
116	ret = i2c_smbus_write_byte(client, v6_prog);
117	if (ret)
118		return ret;
119
120	max1586->v6_curr_sel = selector;
121
122	return 0;
123}
124
125/*
126 * The Maxim 1586 controls V3 and V6 voltages, but offers no way of reading back
127 * the set up value.
128 */
129static struct regulator_ops max1586_v3_ops = {
130	.get_voltage_sel = max1586_v3_get_voltage_sel,
131	.set_voltage_sel = max1586_v3_set_voltage_sel,
132	.list_voltage = regulator_list_voltage_linear,
133	.map_voltage = regulator_map_voltage_linear,
134};
135
136static struct regulator_ops max1586_v6_ops = {
137	.get_voltage_sel = max1586_v6_get_voltage_sel,
138	.set_voltage_sel = max1586_v6_set_voltage_sel,
139	.list_voltage = regulator_list_voltage_table,
140};
141
142static struct regulator_desc max1586_reg[] = {
143	{
144		.name = "Output_V3",
145		.id = MAX1586_V3,
146		.ops = &max1586_v3_ops,
147		.type = REGULATOR_VOLTAGE,
148		.n_voltages = MAX1586_V3_MAX_VSEL + 1,
149		.owner = THIS_MODULE,
150	},
151	{
152		.name = "Output_V6",
153		.id = MAX1586_V6,
154		.ops = &max1586_v6_ops,
155		.type = REGULATOR_VOLTAGE,
156		.n_voltages = MAX1586_V6_MAX_VSEL + 1,
157		.volt_table = v6_voltages_uv,
158		.owner = THIS_MODULE,
159	},
160};
161
162static int of_get_max1586_platform_data(struct device *dev,
163				 struct max1586_platform_data *pdata)
164{
165	struct max1586_subdev_data *sub;
166	struct of_regulator_match rmatch[ARRAY_SIZE(max1586_reg)] = { };
167	struct device_node *np = dev->of_node;
168	int i, matched;
169
170	if (of_property_read_u32(np, "v3-gain",
171				 &pdata->v3_gain) < 0) {
172		dev_err(dev, "%s has no 'v3-gain' property\n", np->full_name);
173		return -EINVAL;
174	}
175
176	np = of_get_child_by_name(np, "regulators");
177	if (!np) {
178		dev_err(dev, "missing 'regulators' subnode in DT\n");
179		return -EINVAL;
180	}
181
182	for (i = 0; i < ARRAY_SIZE(rmatch); i++)
183		rmatch[i].name = max1586_reg[i].name;
184
185	matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch));
186	of_node_put(np);
187	/*
188	 * If matched is 0, ie. neither Output_V3 nor Output_V6 have been found,
189	 * return 0, which signals the normal situation where no subregulator is
190	 * available. This is normal because the max1586 doesn't provide any
191	 * readback support, so the subregulators can't report any status
192	 * anyway.  If matched < 0, return the error.
193	 */
194	if (matched <= 0)
195		return matched;
196
197	pdata->subdevs = devm_kzalloc(dev, sizeof(struct max1586_subdev_data) *
198						matched, GFP_KERNEL);
199	if (!pdata->subdevs)
200		return -ENOMEM;
201
202	pdata->num_subdevs = matched;
203	sub = pdata->subdevs;
204
205	for (i = 0; i < matched; i++) {
206		sub->id = i;
207		sub->name = rmatch[i].of_node->name;
208		sub->platform_data = rmatch[i].init_data;
209		sub++;
210	}
211
212	return 0;
213}
214
215static const struct of_device_id max1586_of_match[] = {
216	{ .compatible = "maxim,max1586", },
217	{},
218};
219MODULE_DEVICE_TABLE(of, max1586_of_match);
220
221static int max1586_pmic_probe(struct i2c_client *client,
222					const struct i2c_device_id *i2c_id)
223{
224	struct max1586_platform_data *pdata, pdata_of;
225	struct regulator_config config = { };
226	struct max1586_data *max1586;
227	int i, id, ret;
228	const struct of_device_id *match;
229
230	pdata = dev_get_platdata(&client->dev);
231	if (client->dev.of_node && !pdata) {
232		match = of_match_device(of_match_ptr(max1586_of_match),
233					&client->dev);
234		if (!match) {
235			dev_err(&client->dev, "Error: No device match found\n");
236			return -ENODEV;
237		}
238		ret = of_get_max1586_platform_data(&client->dev, &pdata_of);
239		if (ret < 0)
240			return ret;
241		pdata = &pdata_of;
242	}
243
244	max1586 = devm_kzalloc(&client->dev, sizeof(struct max1586_data),
245			GFP_KERNEL);
246	if (!max1586)
247		return -ENOMEM;
248
249	max1586->client = client;
250
251	if (!pdata->v3_gain)
252		return -EINVAL;
253
254	max1586->min_uV = MAX1586_V3_MIN_UV / 1000 * pdata->v3_gain / 1000;
255	max1586->max_uV = MAX1586_V3_MAX_UV / 1000 * pdata->v3_gain / 1000;
256
257	/* Set curr_sel to default voltage on power-up */
258	max1586->v3_curr_sel = 24; /* 1.3V */
259	max1586->v6_curr_sel = 0;
260
261	for (i = 0; i < pdata->num_subdevs && i <= MAX1586_V6; i++) {
262		struct regulator_dev *rdev;
263
264		id = pdata->subdevs[i].id;
265		if (!pdata->subdevs[i].platform_data)
266			continue;
267		if (id < MAX1586_V3 || id > MAX1586_V6) {
268			dev_err(&client->dev, "invalid regulator id %d\n", id);
269			return -EINVAL;
270		}
271
272		if (id == MAX1586_V3) {
273			max1586_reg[id].min_uV = max1586->min_uV;
274			max1586_reg[id].uV_step =
275					(max1586->max_uV - max1586->min_uV) /
276					MAX1586_V3_MAX_VSEL;
277		}
278
279		config.dev = &client->dev;
280		config.init_data = pdata->subdevs[i].platform_data;
281		config.driver_data = max1586;
282
283		rdev = devm_regulator_register(&client->dev,
284						  &max1586_reg[id], &config);
285		if (IS_ERR(rdev)) {
286			dev_err(&client->dev, "failed to register %s\n",
287				max1586_reg[id].name);
288			return PTR_ERR(rdev);
289		}
290	}
291
292	i2c_set_clientdata(client, max1586);
293	dev_info(&client->dev, "Maxim 1586 regulator driver loaded\n");
294	return 0;
295}
296
297static const struct i2c_device_id max1586_id[] = {
298	{ "max1586", 0 },
299	{ }
300};
301MODULE_DEVICE_TABLE(i2c, max1586_id);
302
303static struct i2c_driver max1586_pmic_driver = {
304	.probe = max1586_pmic_probe,
305	.driver		= {
306		.name	= "max1586",
307		.owner	= THIS_MODULE,
308		.of_match_table = of_match_ptr(max1586_of_match),
309	},
310	.id_table	= max1586_id,
311};
312
313static int __init max1586_pmic_init(void)
314{
315	return i2c_add_driver(&max1586_pmic_driver);
316}
317subsys_initcall(max1586_pmic_init);
318
319static void __exit max1586_pmic_exit(void)
320{
321	i2c_del_driver(&max1586_pmic_driver);
322}
323module_exit(max1586_pmic_exit);
324
325/* Module information */
326MODULE_DESCRIPTION("MAXIM 1586 voltage regulator driver");
327MODULE_AUTHOR("Robert Jarzmik");
328MODULE_LICENSE("GPL");
329