blob: c5ee3cc048eeab6aabf66206b99406bdee442db1 [file] [log] [blame]
MyungJoo Ham202f4f52010-08-20 14:43:56 +09001/*
2 * max8952.c - Voltage and current regulation for the Maxim 8952
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/i2c.h>
25#include <linux/err.h>
26#include <linux/platform_device.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/max8952.h>
MyungJoo Ham202f4f52010-08-20 14:43:56 +090029#include <linux/gpio.h>
30#include <linux/io.h>
Tomasz Figa3ff4aa92013-04-04 18:17:20 +020031#include <linux/of.h>
32#include <linux/of_gpio.h>
33#include <linux/regulator/of_regulator.h>
MyungJoo Ham202f4f52010-08-20 14:43:56 +090034#include <linux/slab.h>
35
36/* Registers */
37enum {
38 MAX8952_REG_MODE0,
39 MAX8952_REG_MODE1,
40 MAX8952_REG_MODE2,
41 MAX8952_REG_MODE3,
42 MAX8952_REG_CONTROL,
43 MAX8952_REG_SYNC,
44 MAX8952_REG_RAMP,
45 MAX8952_REG_CHIP_ID1,
46 MAX8952_REG_CHIP_ID2,
47};
48
49struct max8952_data {
50 struct i2c_client *client;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090051 struct max8952_platform_data *pdata;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090052
53 bool vid0;
54 bool vid1;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090055};
56
57static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
58{
59 int ret = i2c_smbus_read_byte_data(max8952->client, reg);
60 if (ret > 0)
61 ret &= 0xff;
62
63 return ret;
64}
65
66static int max8952_write_reg(struct max8952_data *max8952,
67 u8 reg, u8 value)
68{
69 return i2c_smbus_write_byte_data(max8952->client, reg, value);
70}
71
MyungJoo Ham202f4f52010-08-20 14:43:56 +090072static int max8952_list_voltage(struct regulator_dev *rdev,
73 unsigned int selector)
74{
75 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
76
77 if (rdev_get_id(rdev) != 0)
78 return -EINVAL;
79
Axel Linb9b49af2012-04-20 18:36:14 +080080 return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090081}
82
Axel Linb9b49af2012-04-20 18:36:14 +080083static int max8952_get_voltage_sel(struct regulator_dev *rdev)
MyungJoo Ham202f4f52010-08-20 14:43:56 +090084{
85 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
86 u8 vid = 0;
87
88 if (max8952->vid0)
89 vid += 1;
90 if (max8952->vid1)
91 vid += 2;
92
Axel Linb9b49af2012-04-20 18:36:14 +080093 return vid;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090094}
95
Axel Lin6ea67d02012-03-26 09:20:58 +080096static int max8952_set_voltage_sel(struct regulator_dev *rdev,
97 unsigned selector)
MyungJoo Ham202f4f52010-08-20 14:43:56 +090098{
99 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900100
101 if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
Jonghwan Choie2cf3132011-06-15 17:54:12 +0900102 !gpio_is_valid(max8952->pdata->gpio_vid1)) {
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900103 /* DVS not supported */
104 return -EPERM;
105 }
106
Axel Lin6ea67d02012-03-26 09:20:58 +0800107 max8952->vid0 = selector & 0x1;
108 max8952->vid1 = (selector >> 1) & 0x1;
109 gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
110 gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900111
112 return 0;
113}
114
115static struct regulator_ops max8952_ops = {
116 .list_voltage = max8952_list_voltage,
Axel Linb9b49af2012-04-20 18:36:14 +0800117 .get_voltage_sel = max8952_get_voltage_sel,
Axel Lin6ea67d02012-03-26 09:20:58 +0800118 .set_voltage_sel = max8952_set_voltage_sel,
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900119};
120
Axel Linbd6ff0d2012-04-06 08:29:49 +0800121static const struct regulator_desc regulator = {
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900122 .name = "MAX8952_VOUT",
123 .id = 0,
124 .n_voltages = MAX8952_NUM_DVS_MODE,
125 .ops = &max8952_ops,
126 .type = REGULATOR_VOLTAGE,
127 .owner = THIS_MODULE,
128};
129
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200130#ifdef CONFIG_OF
131static struct of_device_id max8952_dt_match[] = {
132 { .compatible = "maxim,max8952" },
133 {},
134};
135MODULE_DEVICE_TABLE(of, max8952_dt_match);
136
137static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
138{
139 struct max8952_platform_data *pd;
140 struct device_node *np = dev->of_node;
141 int ret;
142 int i;
143
144 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
Sachin Kamat7752d962014-02-20 14:23:08 +0530145 if (!pd)
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200146 return NULL;
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200147
148 pd->gpio_vid0 = of_get_named_gpio(np, "max8952,vid-gpios", 0);
149 pd->gpio_vid1 = of_get_named_gpio(np, "max8952,vid-gpios", 1);
150 pd->gpio_en = of_get_named_gpio(np, "max8952,en-gpio", 0);
151
152 if (of_property_read_u32(np, "max8952,default-mode", &pd->default_mode))
153 dev_warn(dev, "Default mode not specified, assuming 0\n");
154
155 ret = of_property_read_u32_array(np, "max8952,dvs-mode-microvolt",
156 pd->dvs_mode, ARRAY_SIZE(pd->dvs_mode));
157 if (ret) {
158 dev_err(dev, "max8952,dvs-mode-microvolt property not specified");
159 return NULL;
160 }
161
162 for (i = 0; i < ARRAY_SIZE(pd->dvs_mode); ++i) {
163 if (pd->dvs_mode[i] < 770000 || pd->dvs_mode[i] > 1400000) {
164 dev_err(dev, "DVS voltage %d out of range\n", i);
165 return NULL;
166 }
167 pd->dvs_mode[i] = (pd->dvs_mode[i] - 770000) / 10000;
168 }
169
170 if (of_property_read_u32(np, "max8952,sync-freq", &pd->sync_freq))
171 dev_warn(dev, "max8952,sync-freq property not specified, defaulting to 26MHz\n");
172
173 if (of_property_read_u32(np, "max8952,ramp-speed", &pd->ramp_speed))
174 dev_warn(dev, "max8952,ramp-speed property not specified, defaulting to 32mV/us\n");
175
176 pd->reg_data = of_get_regulator_init_data(dev, np);
177 if (!pd->reg_data) {
178 dev_err(dev, "Failed to parse regulator init data\n");
179 return NULL;
180 }
181
182 return pd;
183}
184#else
185static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
186{
187 return NULL;
188}
189#endif
190
Bill Pembertona5023572012-11-19 13:22:22 -0500191static int max8952_pmic_probe(struct i2c_client *client,
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900192 const struct i2c_device_id *i2c_id)
193{
194 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900195 struct max8952_platform_data *pdata = dev_get_platdata(&client->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100196 struct regulator_config config = { };
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900197 struct max8952_data *max8952;
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100198 struct regulator_dev *rdev;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900199
200 int ret = 0, err = 0;
201
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200202 if (client->dev.of_node)
203 pdata = max8952_parse_dt(&client->dev);
204
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900205 if (!pdata) {
206 dev_err(&client->dev, "Require the platform data\n");
207 return -EINVAL;
208 }
209
210 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
211 return -EIO;
212
Axel Lin372de4a2012-05-16 12:50:47 +0800213 max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data),
214 GFP_KERNEL);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900215 if (!max8952)
216 return -ENOMEM;
217
218 max8952->client = client;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900219 max8952->pdata = pdata;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900220
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100221 config.dev = &client->dev;
Tomasz Figa3ec6eb92013-04-04 18:17:19 +0200222 config.init_data = pdata->reg_data;
Mark Brownc1727082012-04-04 00:50:22 +0100223 config.driver_data = max8952;
Axel Lin71622e12013-04-25 22:15:21 +0800224 config.of_node = client->dev.of_node;
Mark Brownc1727082012-04-04 00:50:22 +0100225
Axel Linb669e0a2012-07-10 06:10:12 +0800226 config.ena_gpio = pdata->gpio_en;
Tomasz Figa3ec6eb92013-04-04 18:17:19 +0200227 if (pdata->reg_data->constraints.boot_on)
Axel Linb669e0a2012-07-10 06:10:12 +0800228 config.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
229
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100230 rdev = devm_regulator_register(&client->dev, &regulator, &config);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900231
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100232 if (IS_ERR(rdev)) {
233 ret = PTR_ERR(rdev);
234 dev_err(&client->dev, "regulator init failed (%d)\n", ret);
Axel Lin372de4a2012-05-16 12:50:47 +0800235 return ret;
Axel Linda057382010-10-25 10:11:07 +0800236 }
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900237
Axel Linc8237f02012-03-15 08:47:48 +0800238 max8952->vid0 = pdata->default_mode & 0x1;
239 max8952->vid1 = (pdata->default_mode >> 1) & 0x1;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900240
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900241 if (gpio_is_valid(pdata->gpio_vid0) &&
242 gpio_is_valid(pdata->gpio_vid1)) {
243 if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0"))
244 gpio_direction_output(pdata->gpio_vid0,
Axel Linc8237f02012-03-15 08:47:48 +0800245 (pdata->default_mode) & 0x1);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900246 else
247 err = 1;
248
249 if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1"))
250 gpio_direction_output(pdata->gpio_vid1,
Axel Linc8237f02012-03-15 08:47:48 +0800251 (pdata->default_mode >> 1) & 0x1);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900252 else {
253 if (!err)
254 gpio_free(pdata->gpio_vid0);
255 err = 2;
256 }
257
258 } else
259 err = 3;
260
261 if (err) {
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100262 dev_warn(&client->dev, "VID0/1 gpio invalid: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300263 "DVS not available.\n");
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900264 max8952->vid0 = 0;
265 max8952->vid1 = 0;
266 /* Mark invalid */
267 pdata->gpio_vid0 = -1;
268 pdata->gpio_vid1 = -1;
269
270 /* Disable Pulldown of EN only */
271 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
272
Krzysztof Kozlowskib874f412014-03-10 09:32:50 +0100273 dev_err(&client->dev, "DVS modes disabled because VID0 and VID1"
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900274 " do not have proper controls.\n");
275 } else {
276 /*
277 * Disable Pulldown on EN, VID0, VID1 to reduce
278 * leakage current of MAX8952 assuming that MAX8952
279 * is turned on (EN==1). Note that without having VID0/1
280 * properly connected, turning pulldown off can be
281 * problematic. Thus, turn this off only when they are
282 * controllable by GPIO.
283 */
284 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
285 }
286
287 max8952_write_reg(max8952, MAX8952_REG_MODE0,
288 (max8952_read_reg(max8952,
289 MAX8952_REG_MODE0) & 0xC0) |
290 (pdata->dvs_mode[0] & 0x3F));
291 max8952_write_reg(max8952, MAX8952_REG_MODE1,
292 (max8952_read_reg(max8952,
293 MAX8952_REG_MODE1) & 0xC0) |
294 (pdata->dvs_mode[1] & 0x3F));
295 max8952_write_reg(max8952, MAX8952_REG_MODE2,
296 (max8952_read_reg(max8952,
297 MAX8952_REG_MODE2) & 0xC0) |
298 (pdata->dvs_mode[2] & 0x3F));
299 max8952_write_reg(max8952, MAX8952_REG_MODE3,
300 (max8952_read_reg(max8952,
301 MAX8952_REG_MODE3) & 0xC0) |
302 (pdata->dvs_mode[3] & 0x3F));
303
304 max8952_write_reg(max8952, MAX8952_REG_SYNC,
305 (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
306 ((pdata->sync_freq & 0x3) << 6));
307 max8952_write_reg(max8952, MAX8952_REG_RAMP,
308 (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
309 ((pdata->ramp_speed & 0x7) << 5));
310
311 i2c_set_clientdata(client, max8952);
312
Axel Linda057382010-10-25 10:11:07 +0800313 return 0;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900314}
315
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500316static int max8952_pmic_remove(struct i2c_client *client)
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900317{
318 struct max8952_data *max8952 = i2c_get_clientdata(client);
319 struct max8952_platform_data *pdata = max8952->pdata;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900320
321 gpio_free(pdata->gpio_vid0);
322 gpio_free(pdata->gpio_vid1);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900323 return 0;
324}
325
326static const struct i2c_device_id max8952_ids[] = {
327 { "max8952", 0 },
328 { },
329};
330MODULE_DEVICE_TABLE(i2c, max8952_ids);
331
332static struct i2c_driver max8952_pmic_driver = {
333 .probe = max8952_pmic_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500334 .remove = max8952_pmic_remove,
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900335 .driver = {
336 .name = "max8952",
Tomasz Figa3ff4aa92013-04-04 18:17:20 +0200337 .of_match_table = of_match_ptr(max8952_dt_match),
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900338 },
339 .id_table = max8952_ids,
340};
341
342static int __init max8952_pmic_init(void)
343{
344 return i2c_add_driver(&max8952_pmic_driver);
345}
346subsys_initcall(max8952_pmic_init);
347
348static void __exit max8952_pmic_exit(void)
349{
350 i2c_del_driver(&max8952_pmic_driver);
351}
352module_exit(max8952_pmic_exit);
353
354MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
355MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
356MODULE_LICENSE("GPL");