blob: 3172da847d248bfbb039162447d282ee99299abd [file] [log] [blame]
Haojian Zhuanga71b7972010-01-25 10:24:09 -05001/*
2 * Regulators driver for Maxim max8649
3 *
4 * Copyright (C) 2009-2010 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.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 version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/err.h>
14#include <linux/i2c.h>
15#include <linux/platform_device.h>
16#include <linux/regulator/driver.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Haojian Zhuanga71b7972010-01-25 10:24:09 -050018#include <linux/regulator/max8649.h>
Jonghwan Choic5b68d472011-10-24 22:26:26 +090019#include <linux/regmap.h>
Haojian Zhuanga71b7972010-01-25 10:24:09 -050020
21#define MAX8649_DCDC_VMIN 750000 /* uV */
22#define MAX8649_DCDC_VMAX 1380000 /* uV */
23#define MAX8649_DCDC_STEP 10000 /* uV */
24#define MAX8649_VOL_MASK 0x3f
25
26/* Registers */
27#define MAX8649_MODE0 0x00
28#define MAX8649_MODE1 0x01
29#define MAX8649_MODE2 0x02
30#define MAX8649_MODE3 0x03
31#define MAX8649_CONTROL 0x04
32#define MAX8649_SYNC 0x05
33#define MAX8649_RAMP 0x06
34#define MAX8649_CHIP_ID1 0x08
35#define MAX8649_CHIP_ID2 0x09
36
37/* Bits */
38#define MAX8649_EN_PD (1 << 7)
39#define MAX8649_VID0_PD (1 << 6)
40#define MAX8649_VID1_PD (1 << 5)
41#define MAX8649_VID_MASK (3 << 5)
42
43#define MAX8649_FORCE_PWM (1 << 7)
44#define MAX8649_SYNC_EXTCLK (1 << 6)
45
46#define MAX8649_EXT_MASK (3 << 6)
47
48#define MAX8649_RAMP_MASK (7 << 5)
49#define MAX8649_RAMP_DOWN (1 << 1)
50
51struct max8649_regulator_info {
Haojian Zhuanga71b7972010-01-25 10:24:09 -050052 struct device *dev;
Jonghwan Choic5b68d472011-10-24 22:26:26 +090053 struct regmap *regmap;
Haojian Zhuanga71b7972010-01-25 10:24:09 -050054
Haojian Zhuanga71b7972010-01-25 10:24:09 -050055 unsigned mode:2; /* bit[1:0] = VID1, VID0 */
56 unsigned extclk_freq:2;
57 unsigned extclk:1;
58 unsigned ramp_timing:3;
59 unsigned ramp_down:1;
60};
61
Haojian Zhuanga71b7972010-01-25 10:24:09 -050062static int max8649_enable_time(struct regulator_dev *rdev)
63{
64 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
65 int voltage, rate, ret;
Jonghwan Choic5b68d472011-10-24 22:26:26 +090066 unsigned int val;
Haojian Zhuanga71b7972010-01-25 10:24:09 -050067
68 /* get voltage */
Axel Lin817d8c22012-05-15 00:13:10 +080069 ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val);
Jonghwan Choic5b68d472011-10-24 22:26:26 +090070 if (ret != 0)
Haojian Zhuanga71b7972010-01-25 10:24:09 -050071 return ret;
Jonghwan Choic5b68d472011-10-24 22:26:26 +090072 val &= MAX8649_VOL_MASK;
Axel Lin0fb737b2012-05-15 00:14:05 +080073 voltage = regulator_list_voltage_linear(rdev, (unsigned char)val);
Haojian Zhuanga71b7972010-01-25 10:24:09 -050074
75 /* get rate */
Jonghwan Choic5b68d472011-10-24 22:26:26 +090076 ret = regmap_read(info->regmap, MAX8649_RAMP, &val);
77 if (ret != 0)
Haojian Zhuanga71b7972010-01-25 10:24:09 -050078 return ret;
Jonghwan Choic5b68d472011-10-24 22:26:26 +090079 ret = (val & MAX8649_RAMP_MASK) >> 5;
Haojian Zhuanga71b7972010-01-25 10:24:09 -050080 rate = (32 * 1000) >> ret; /* uV/uS */
81
Axel Line69c4992011-08-02 12:54:56 +080082 return DIV_ROUND_UP(voltage, rate);
Haojian Zhuanga71b7972010-01-25 10:24:09 -050083}
84
85static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode)
86{
87 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
88
89 switch (mode) {
90 case REGULATOR_MODE_FAST:
Axel Lin817d8c22012-05-15 00:13:10 +080091 regmap_update_bits(info->regmap, rdev->desc->vsel_reg,
92 MAX8649_FORCE_PWM, MAX8649_FORCE_PWM);
Haojian Zhuanga71b7972010-01-25 10:24:09 -050093 break;
94 case REGULATOR_MODE_NORMAL:
Axel Lin817d8c22012-05-15 00:13:10 +080095 regmap_update_bits(info->regmap, rdev->desc->vsel_reg,
Jonghwan Choic5b68d472011-10-24 22:26:26 +090096 MAX8649_FORCE_PWM, 0);
Haojian Zhuanga71b7972010-01-25 10:24:09 -050097 break;
98 default:
99 return -EINVAL;
100 }
101 return 0;
102}
103
104static unsigned int max8649_get_mode(struct regulator_dev *rdev)
105{
106 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900107 unsigned int val;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500108 int ret;
109
Axel Lin817d8c22012-05-15 00:13:10 +0800110 ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val);
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900111 if (ret != 0)
112 return ret;
113 if (val & MAX8649_FORCE_PWM)
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500114 return REGULATOR_MODE_FAST;
115 return REGULATOR_MODE_NORMAL;
116}
117
118static struct regulator_ops max8649_dcdc_ops = {
Axel Lin40baece2012-05-15 00:15:11 +0800119 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Axel Lin817d8c22012-05-15 00:13:10 +0800120 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lin0fb737b2012-05-15 00:14:05 +0800121 .list_voltage = regulator_list_voltage_linear,
Axel Lin40baece2012-05-15 00:15:11 +0800122 .map_voltage = regulator_map_voltage_linear,
Axel Linea88b132013-03-05 14:17:57 +0800123 .enable = regulator_enable_regmap,
124 .disable = regulator_disable_regmap,
125 .is_enabled = regulator_is_enabled_regmap,
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500126 .enable_time = max8649_enable_time,
127 .set_mode = max8649_set_mode,
128 .get_mode = max8649_get_mode,
129
130};
131
Axel Lin817d8c22012-05-15 00:13:10 +0800132static struct regulator_desc dcdc_desc = {
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500133 .name = "max8649",
134 .ops = &max8649_dcdc_ops,
135 .type = REGULATOR_VOLTAGE,
136 .n_voltages = 1 << 6,
137 .owner = THIS_MODULE,
Axel Lin817d8c22012-05-15 00:13:10 +0800138 .vsel_mask = MAX8649_VOL_MASK,
Axel Lin0fb737b2012-05-15 00:14:05 +0800139 .min_uV = MAX8649_DCDC_VMIN,
140 .uV_step = MAX8649_DCDC_STEP,
Axel Linea88b132013-03-05 14:17:57 +0800141 .enable_reg = MAX8649_CONTROL,
142 .enable_mask = MAX8649_EN_PD,
143 .enable_is_inverted = true,
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500144};
145
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900146static struct regmap_config max8649_regmap_config = {
147 .reg_bits = 8,
148 .val_bits = 8,
149};
150
Bill Pembertona5023572012-11-19 13:22:22 -0500151static int max8649_regulator_probe(struct i2c_client *client,
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500152 const struct i2c_device_id *id)
153{
Jingoo Handff91d02013-07-30 17:20:47 +0900154 struct max8649_platform_data *pdata = dev_get_platdata(&client->dev);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500155 struct max8649_regulator_info *info = NULL;
Krzysztof Kozlowskib7313b82014-03-10 09:32:47 +0100156 struct regulator_dev *regulator;
Mark Brownc1727082012-04-04 00:50:22 +0100157 struct regulator_config config = { };
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900158 unsigned int val;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500159 unsigned char data;
160 int ret;
161
Axel Lindc553a72012-04-07 23:28:28 +0800162 info = devm_kzalloc(&client->dev, sizeof(struct max8649_regulator_info),
163 GFP_KERNEL);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500164 if (!info) {
165 dev_err(&client->dev, "No enough memory\n");
166 return -ENOMEM;
167 }
168
Axel Lindc553a72012-04-07 23:28:28 +0800169 info->regmap = devm_regmap_init_i2c(client, &max8649_regmap_config);
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900170 if (IS_ERR(info->regmap)) {
171 ret = PTR_ERR(info->regmap);
172 dev_err(&client->dev, "Failed to allocate register map: %d\n", ret);
Axel Lindc553a72012-04-07 23:28:28 +0800173 return ret;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900174 }
175
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500176 info->dev = &client->dev;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500177 i2c_set_clientdata(client, info);
178
179 info->mode = pdata->mode;
180 switch (info->mode) {
181 case 0:
Axel Lin817d8c22012-05-15 00:13:10 +0800182 dcdc_desc.vsel_reg = MAX8649_MODE0;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500183 break;
184 case 1:
Axel Lin817d8c22012-05-15 00:13:10 +0800185 dcdc_desc.vsel_reg = MAX8649_MODE1;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500186 break;
187 case 2:
Axel Lin817d8c22012-05-15 00:13:10 +0800188 dcdc_desc.vsel_reg = MAX8649_MODE2;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500189 break;
190 case 3:
Axel Lin817d8c22012-05-15 00:13:10 +0800191 dcdc_desc.vsel_reg = MAX8649_MODE3;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500192 break;
193 default:
194 break;
195 }
196
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900197 ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val);
198 if (ret != 0) {
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500199 dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n",
200 ret);
Axel Lindc553a72012-04-07 23:28:28 +0800201 return ret;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500202 }
Axel Linda7de6a2012-02-06 20:37:46 +0800203 dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", val);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500204
205 /* enable VID0 & VID1 */
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900206 regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500207
208 /* enable/disable external clock synchronization */
209 info->extclk = pdata->extclk;
210 data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0;
Axel Lin817d8c22012-05-15 00:13:10 +0800211 regmap_update_bits(info->regmap, dcdc_desc.vsel_reg,
212 MAX8649_SYNC_EXTCLK, data);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500213 if (info->extclk) {
214 /* set external clock frequency */
215 info->extclk_freq = pdata->extclk_freq;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900216 regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK,
217 info->extclk_freq << 6);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500218 }
219
220 if (pdata->ramp_timing) {
221 info->ramp_timing = pdata->ramp_timing;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900222 regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK,
223 info->ramp_timing << 5);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500224 }
225
226 info->ramp_down = pdata->ramp_down;
227 if (info->ramp_down) {
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900228 regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN,
229 MAX8649_RAMP_DOWN);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500230 }
231
Mark Brownc1727082012-04-04 00:50:22 +0100232 config.dev = &client->dev;
233 config.init_data = pdata->regulator;
234 config.driver_data = info;
Haojian Zhuang61663172012-06-05 18:10:10 +0800235 config.regmap = info->regmap;
Mark Brownc1727082012-04-04 00:50:22 +0100236
Krzysztof Kozlowskib7313b82014-03-10 09:32:47 +0100237 regulator = devm_regulator_register(&client->dev, &dcdc_desc,
Sachin Kamate453f922013-09-04 11:07:57 +0530238 &config);
Krzysztof Kozlowskib7313b82014-03-10 09:32:47 +0100239 if (IS_ERR(regulator)) {
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500240 dev_err(info->dev, "failed to register regulator %s\n",
241 dcdc_desc.name);
Krzysztof Kozlowskib7313b82014-03-10 09:32:47 +0100242 return PTR_ERR(regulator);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500243 }
244
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500245 return 0;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500246}
247
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500248static const struct i2c_device_id max8649_id[] = {
249 { "max8649", 0 },
250 { }
251};
252MODULE_DEVICE_TABLE(i2c, max8649_id);
253
254static struct i2c_driver max8649_driver = {
255 .probe = max8649_regulator_probe,
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500256 .driver = {
257 .name = "max8649",
258 },
259 .id_table = max8649_id,
260};
261
262static int __init max8649_init(void)
263{
264 return i2c_add_driver(&max8649_driver);
265}
266subsys_initcall(max8649_init);
267
268static void __exit max8649_exit(void)
269{
270 i2c_del_driver(&max8649_driver);
271}
272module_exit(max8649_exit);
273
274/* Module information */
275MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
276MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
277MODULE_LICENSE("GPL");