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