Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Regulators driver for Maxim max8649 |
| 4 | * |
| 5 | * Copyright (C) 2009-2010 Marvell International Ltd. |
| 6 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/regulator/driver.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 15 | #include <linux/regulator/max8649.h> |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 16 | #include <linux/regmap.h> |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 17 | |
| 18 | #define MAX8649_DCDC_VMIN 750000 /* uV */ |
| 19 | #define MAX8649_DCDC_VMAX 1380000 /* uV */ |
| 20 | #define MAX8649_DCDC_STEP 10000 /* uV */ |
| 21 | #define MAX8649_VOL_MASK 0x3f |
| 22 | |
| 23 | /* Registers */ |
| 24 | #define MAX8649_MODE0 0x00 |
| 25 | #define MAX8649_MODE1 0x01 |
| 26 | #define MAX8649_MODE2 0x02 |
| 27 | #define MAX8649_MODE3 0x03 |
| 28 | #define MAX8649_CONTROL 0x04 |
| 29 | #define MAX8649_SYNC 0x05 |
| 30 | #define MAX8649_RAMP 0x06 |
| 31 | #define MAX8649_CHIP_ID1 0x08 |
| 32 | #define MAX8649_CHIP_ID2 0x09 |
| 33 | |
| 34 | /* Bits */ |
| 35 | #define MAX8649_EN_PD (1 << 7) |
| 36 | #define MAX8649_VID0_PD (1 << 6) |
| 37 | #define MAX8649_VID1_PD (1 << 5) |
| 38 | #define MAX8649_VID_MASK (3 << 5) |
| 39 | |
| 40 | #define MAX8649_FORCE_PWM (1 << 7) |
| 41 | #define MAX8649_SYNC_EXTCLK (1 << 6) |
| 42 | |
| 43 | #define MAX8649_EXT_MASK (3 << 6) |
| 44 | |
| 45 | #define MAX8649_RAMP_MASK (7 << 5) |
| 46 | #define MAX8649_RAMP_DOWN (1 << 1) |
| 47 | |
| 48 | struct max8649_regulator_info { |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 49 | struct device *dev; |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 50 | struct regmap *regmap; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 51 | |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 52 | unsigned mode:2; /* bit[1:0] = VID1, VID0 */ |
| 53 | unsigned extclk_freq:2; |
| 54 | unsigned extclk:1; |
| 55 | unsigned ramp_timing:3; |
| 56 | unsigned ramp_down:1; |
| 57 | }; |
| 58 | |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 59 | static int max8649_enable_time(struct regulator_dev *rdev) |
| 60 | { |
| 61 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 62 | int voltage, rate, ret; |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 63 | unsigned int val; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 64 | |
| 65 | /* get voltage */ |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 66 | ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val); |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 67 | if (ret != 0) |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 68 | return ret; |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 69 | val &= MAX8649_VOL_MASK; |
Axel Lin | 0fb737b | 2012-05-15 00:14:05 +0800 | [diff] [blame] | 70 | voltage = regulator_list_voltage_linear(rdev, (unsigned char)val); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 71 | |
| 72 | /* get rate */ |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 73 | ret = regmap_read(info->regmap, MAX8649_RAMP, &val); |
| 74 | if (ret != 0) |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 75 | return ret; |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 76 | ret = (val & MAX8649_RAMP_MASK) >> 5; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 77 | rate = (32 * 1000) >> ret; /* uV/uS */ |
| 78 | |
Axel Lin | e69c499 | 2011-08-02 12:54:56 +0800 | [diff] [blame] | 79 | return DIV_ROUND_UP(voltage, rate); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 83 | { |
| 84 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 85 | |
| 86 | switch (mode) { |
| 87 | case REGULATOR_MODE_FAST: |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 88 | regmap_update_bits(info->regmap, rdev->desc->vsel_reg, |
| 89 | MAX8649_FORCE_PWM, MAX8649_FORCE_PWM); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 90 | break; |
| 91 | case REGULATOR_MODE_NORMAL: |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 92 | regmap_update_bits(info->regmap, rdev->desc->vsel_reg, |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 93 | MAX8649_FORCE_PWM, 0); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 94 | break; |
| 95 | default: |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static unsigned int max8649_get_mode(struct regulator_dev *rdev) |
| 102 | { |
| 103 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 104 | unsigned int val; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 105 | int ret; |
| 106 | |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 107 | ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val); |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 108 | if (ret != 0) |
| 109 | return ret; |
| 110 | if (val & MAX8649_FORCE_PWM) |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 111 | return REGULATOR_MODE_FAST; |
| 112 | return REGULATOR_MODE_NORMAL; |
| 113 | } |
| 114 | |
Krzysztof Kozlowski | 035f332 | 2015-01-05 10:05:47 +0100 | [diff] [blame] | 115 | static const struct regulator_ops max8649_dcdc_ops = { |
Axel Lin | 40baece | 2012-05-15 00:15:11 +0800 | [diff] [blame] | 116 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 117 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
Axel Lin | 0fb737b | 2012-05-15 00:14:05 +0800 | [diff] [blame] | 118 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 40baece | 2012-05-15 00:15:11 +0800 | [diff] [blame] | 119 | .map_voltage = regulator_map_voltage_linear, |
Axel Lin | ea88b13 | 2013-03-05 14:17:57 +0800 | [diff] [blame] | 120 | .enable = regulator_enable_regmap, |
| 121 | .disable = regulator_disable_regmap, |
| 122 | .is_enabled = regulator_is_enabled_regmap, |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 123 | .enable_time = max8649_enable_time, |
| 124 | .set_mode = max8649_set_mode, |
| 125 | .get_mode = max8649_get_mode, |
| 126 | |
| 127 | }; |
| 128 | |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 129 | static struct regulator_desc dcdc_desc = { |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 130 | .name = "max8649", |
| 131 | .ops = &max8649_dcdc_ops, |
| 132 | .type = REGULATOR_VOLTAGE, |
| 133 | .n_voltages = 1 << 6, |
| 134 | .owner = THIS_MODULE, |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 135 | .vsel_mask = MAX8649_VOL_MASK, |
Axel Lin | 0fb737b | 2012-05-15 00:14:05 +0800 | [diff] [blame] | 136 | .min_uV = MAX8649_DCDC_VMIN, |
| 137 | .uV_step = MAX8649_DCDC_STEP, |
Axel Lin | ea88b13 | 2013-03-05 14:17:57 +0800 | [diff] [blame] | 138 | .enable_reg = MAX8649_CONTROL, |
| 139 | .enable_mask = MAX8649_EN_PD, |
| 140 | .enable_is_inverted = true, |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 141 | }; |
| 142 | |
Krzysztof Kozlowski | 035f332 | 2015-01-05 10:05:47 +0100 | [diff] [blame] | 143 | static const struct regmap_config max8649_regmap_config = { |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 144 | .reg_bits = 8, |
| 145 | .val_bits = 8, |
| 146 | }; |
| 147 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 148 | static int max8649_regulator_probe(struct i2c_client *client, |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 149 | const struct i2c_device_id *id) |
| 150 | { |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 151 | struct max8649_platform_data *pdata = dev_get_platdata(&client->dev); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 152 | struct max8649_regulator_info *info = NULL; |
Krzysztof Kozlowski | b7313b8 | 2014-03-10 09:32:47 +0100 | [diff] [blame] | 153 | struct regulator_dev *regulator; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 154 | struct regulator_config config = { }; |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 155 | unsigned int val; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 156 | unsigned char data; |
| 157 | int ret; |
| 158 | |
Axel Lin | dc553a7 | 2012-04-07 23:28:28 +0800 | [diff] [blame] | 159 | info = devm_kzalloc(&client->dev, sizeof(struct max8649_regulator_info), |
| 160 | GFP_KERNEL); |
Jingoo Han | 6e27e99 | 2014-06-02 15:29:57 +0900 | [diff] [blame] | 161 | if (!info) |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 162 | return -ENOMEM; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 163 | |
Axel Lin | dc553a7 | 2012-04-07 23:28:28 +0800 | [diff] [blame] | 164 | info->regmap = devm_regmap_init_i2c(client, &max8649_regmap_config); |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 165 | if (IS_ERR(info->regmap)) { |
| 166 | ret = PTR_ERR(info->regmap); |
| 167 | dev_err(&client->dev, "Failed to allocate register map: %d\n", ret); |
Axel Lin | dc553a7 | 2012-04-07 23:28:28 +0800 | [diff] [blame] | 168 | return ret; |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 169 | } |
| 170 | |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 171 | info->dev = &client->dev; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 172 | i2c_set_clientdata(client, info); |
| 173 | |
| 174 | info->mode = pdata->mode; |
| 175 | switch (info->mode) { |
| 176 | case 0: |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 177 | dcdc_desc.vsel_reg = MAX8649_MODE0; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 178 | break; |
| 179 | case 1: |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 180 | dcdc_desc.vsel_reg = MAX8649_MODE1; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 181 | break; |
| 182 | case 2: |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 183 | dcdc_desc.vsel_reg = MAX8649_MODE2; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 184 | break; |
| 185 | case 3: |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 186 | dcdc_desc.vsel_reg = MAX8649_MODE3; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 187 | break; |
| 188 | default: |
| 189 | break; |
| 190 | } |
| 191 | |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 192 | ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val); |
| 193 | if (ret != 0) { |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 194 | dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n", |
| 195 | ret); |
Axel Lin | dc553a7 | 2012-04-07 23:28:28 +0800 | [diff] [blame] | 196 | return ret; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 197 | } |
Axel Lin | da7de6a | 2012-02-06 20:37:46 +0800 | [diff] [blame] | 198 | dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", val); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 199 | |
| 200 | /* enable VID0 & VID1 */ |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 201 | regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 202 | |
| 203 | /* enable/disable external clock synchronization */ |
| 204 | info->extclk = pdata->extclk; |
| 205 | data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0; |
Axel Lin | 817d8c2 | 2012-05-15 00:13:10 +0800 | [diff] [blame] | 206 | regmap_update_bits(info->regmap, dcdc_desc.vsel_reg, |
| 207 | MAX8649_SYNC_EXTCLK, data); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 208 | if (info->extclk) { |
| 209 | /* set external clock frequency */ |
| 210 | info->extclk_freq = pdata->extclk_freq; |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 211 | regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK, |
| 212 | info->extclk_freq << 6); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (pdata->ramp_timing) { |
| 216 | info->ramp_timing = pdata->ramp_timing; |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 217 | regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK, |
| 218 | info->ramp_timing << 5); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | info->ramp_down = pdata->ramp_down; |
| 222 | if (info->ramp_down) { |
Jonghwan Choi | c5b68d47 | 2011-10-24 22:26:26 +0900 | [diff] [blame] | 223 | regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN, |
| 224 | MAX8649_RAMP_DOWN); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 227 | config.dev = &client->dev; |
| 228 | config.init_data = pdata->regulator; |
| 229 | config.driver_data = info; |
Haojian Zhuang | 6166317 | 2012-06-05 18:10:10 +0800 | [diff] [blame] | 230 | config.regmap = info->regmap; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 231 | |
Krzysztof Kozlowski | b7313b8 | 2014-03-10 09:32:47 +0100 | [diff] [blame] | 232 | regulator = devm_regulator_register(&client->dev, &dcdc_desc, |
Sachin Kamat | e453f92 | 2013-09-04 11:07:57 +0530 | [diff] [blame] | 233 | &config); |
Krzysztof Kozlowski | b7313b8 | 2014-03-10 09:32:47 +0100 | [diff] [blame] | 234 | if (IS_ERR(regulator)) { |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 235 | dev_err(info->dev, "failed to register regulator %s\n", |
| 236 | dcdc_desc.name); |
Krzysztof Kozlowski | b7313b8 | 2014-03-10 09:32:47 +0100 | [diff] [blame] | 237 | return PTR_ERR(regulator); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 240 | return 0; |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 241 | } |
| 242 | |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 243 | static const struct i2c_device_id max8649_id[] = { |
| 244 | { "max8649", 0 }, |
| 245 | { } |
| 246 | }; |
| 247 | MODULE_DEVICE_TABLE(i2c, max8649_id); |
| 248 | |
| 249 | static struct i2c_driver max8649_driver = { |
| 250 | .probe = max8649_regulator_probe, |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 251 | .driver = { |
| 252 | .name = "max8649", |
| 253 | }, |
| 254 | .id_table = max8649_id, |
| 255 | }; |
| 256 | |
| 257 | static int __init max8649_init(void) |
| 258 | { |
| 259 | return i2c_add_driver(&max8649_driver); |
| 260 | } |
| 261 | subsys_initcall(max8649_init); |
| 262 | |
| 263 | static void __exit max8649_exit(void) |
| 264 | { |
| 265 | i2c_del_driver(&max8649_driver); |
| 266 | } |
| 267 | module_exit(max8649_exit); |
| 268 | |
| 269 | /* Module information */ |
| 270 | MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver"); |
| 271 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 272 | MODULE_LICENSE("GPL"); |