Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Regulators driver for Maxim max8925 |
| 3 | * |
| 4 | * Copyright (C) 2009 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> |
Paul Gortmaker | 65602c3 | 2011-07-17 16:28:23 -0400 | [diff] [blame] | 12 | #include <linux/module.h> |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/regulator/driver.h> |
| 18 | #include <linux/regulator/machine.h> |
| 19 | #include <linux/mfd/max8925.h> |
| 20 | |
| 21 | #define SD1_DVM_VMIN 850000 |
| 22 | #define SD1_DVM_VMAX 1000000 |
| 23 | #define SD1_DVM_STEP 50000 |
| 24 | #define SD1_DVM_SHIFT 5 /* SDCTL1 bit5 */ |
| 25 | #define SD1_DVM_EN 6 /* SDV1 bit 6 */ |
| 26 | |
Kevin Liu | 65364e9 | 2012-01-04 15:12:04 +0800 | [diff] [blame] | 27 | /* bit definitions in LDO control registers */ |
| 28 | #define LDO_SEQ_I2C 0x7 /* Power U/D by i2c */ |
| 29 | #define LDO_SEQ_MASK 0x7 /* Power U/D sequence mask */ |
| 30 | #define LDO_SEQ_SHIFT 2 /* Power U/D sequence offset */ |
| 31 | #define LDO_I2C_EN 0x1 /* Enable by i2c */ |
| 32 | #define LDO_I2C_EN_MASK 0x1 /* Enable mask by i2c */ |
| 33 | #define LDO_I2C_EN_SHIFT 0 /* Enable offset by i2c */ |
Haojian Zhuang | db13c04 | 2011-04-18 22:04:09 +0800 | [diff] [blame] | 34 | |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 35 | struct max8925_regulator_info { |
| 36 | struct regulator_desc desc; |
| 37 | struct regulator_dev *regulator; |
| 38 | struct i2c_client *i2c; |
| 39 | struct max8925_chip *chip; |
| 40 | |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 41 | int max_uV; |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 42 | int vol_reg; |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 43 | int enable_reg; |
| 44 | }; |
| 45 | |
| 46 | static inline int check_range(struct max8925_regulator_info *info, |
| 47 | int min_uV, int max_uV) |
| 48 | { |
Axel Lin | 4093e73 | 2012-05-14 10:35:40 +0800 | [diff] [blame^] | 49 | if (min_uV < info->desc.min_uV || min_uV > info->max_uV) |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 50 | return -EINVAL; |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 55 | static int max8925_set_voltage(struct regulator_dev *rdev, |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 56 | int min_uV, int max_uV, unsigned int *selector) |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 57 | { |
| 58 | struct max8925_regulator_info *info = rdev_get_drvdata(rdev); |
| 59 | unsigned char data, mask; |
| 60 | |
| 61 | if (check_range(info, min_uV, max_uV)) { |
| 62 | dev_err(info->chip->dev, "invalid voltage range (%d, %d) uV\n", |
| 63 | min_uV, max_uV); |
| 64 | return -EINVAL; |
| 65 | } |
Axel Lin | 4093e73 | 2012-05-14 10:35:40 +0800 | [diff] [blame^] | 66 | data = DIV_ROUND_UP(min_uV - info->desc.min_uV, info->desc.uV_step); |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 67 | *selector = data; |
Axel Lin | 7f52ba7 | 2012-04-02 09:16:08 +0800 | [diff] [blame] | 68 | mask = rdev->desc->n_voltages - 1; |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 69 | |
| 70 | return max8925_set_bits(info->i2c, info->vol_reg, mask, data); |
| 71 | } |
| 72 | |
Axel Lin | 3c8983b | 2012-05-14 10:34:50 +0800 | [diff] [blame] | 73 | static int max8925_get_voltage_sel(struct regulator_dev *rdev) |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 74 | { |
| 75 | struct max8925_regulator_info *info = rdev_get_drvdata(rdev); |
| 76 | unsigned char data, mask; |
| 77 | int ret; |
| 78 | |
| 79 | ret = max8925_reg_read(info->i2c, info->vol_reg); |
| 80 | if (ret < 0) |
| 81 | return ret; |
Axel Lin | 7f52ba7 | 2012-04-02 09:16:08 +0800 | [diff] [blame] | 82 | mask = rdev->desc->n_voltages - 1; |
| 83 | data = ret & mask; |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 84 | |
Axel Lin | 3c8983b | 2012-05-14 10:34:50 +0800 | [diff] [blame] | 85 | return data; |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static int max8925_enable(struct regulator_dev *rdev) |
| 89 | { |
| 90 | struct max8925_regulator_info *info = rdev_get_drvdata(rdev); |
| 91 | |
| 92 | return max8925_set_bits(info->i2c, info->enable_reg, |
Kevin Liu | 65364e9 | 2012-01-04 15:12:04 +0800 | [diff] [blame] | 93 | LDO_SEQ_MASK << LDO_SEQ_SHIFT | |
| 94 | LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT, |
| 95 | LDO_SEQ_I2C << LDO_SEQ_SHIFT | |
| 96 | LDO_I2C_EN << LDO_I2C_EN_SHIFT); |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static int max8925_disable(struct regulator_dev *rdev) |
| 100 | { |
| 101 | struct max8925_regulator_info *info = rdev_get_drvdata(rdev); |
| 102 | |
| 103 | return max8925_set_bits(info->i2c, info->enable_reg, |
Kevin Liu | 65364e9 | 2012-01-04 15:12:04 +0800 | [diff] [blame] | 104 | LDO_SEQ_MASK << LDO_SEQ_SHIFT | |
| 105 | LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT, |
| 106 | LDO_SEQ_I2C << LDO_SEQ_SHIFT); |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int max8925_is_enabled(struct regulator_dev *rdev) |
| 110 | { |
| 111 | struct max8925_regulator_info *info = rdev_get_drvdata(rdev); |
Kevin Liu | 65364e9 | 2012-01-04 15:12:04 +0800 | [diff] [blame] | 112 | int ldo_seq, ret; |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 113 | |
Haojian Zhuang | 4b99fef | 2010-04-06 06:19:15 -0400 | [diff] [blame] | 114 | ret = max8925_reg_read(info->i2c, info->enable_reg); |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 115 | if (ret < 0) |
| 116 | return ret; |
Kevin Liu | 65364e9 | 2012-01-04 15:12:04 +0800 | [diff] [blame] | 117 | ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK; |
| 118 | if (ldo_seq != LDO_SEQ_I2C) |
| 119 | return 1; |
| 120 | else |
| 121 | return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT); |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV) |
| 125 | { |
| 126 | struct max8925_regulator_info *info = rdev_get_drvdata(rdev); |
| 127 | unsigned char data, mask; |
| 128 | |
| 129 | if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX) |
| 130 | return -EINVAL; |
| 131 | |
Axel Lin | 5db6d09 | 2012-03-13 16:09:28 +0800 | [diff] [blame] | 132 | data = DIV_ROUND_UP(uV - SD1_DVM_VMIN, SD1_DVM_STEP); |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 133 | data <<= SD1_DVM_SHIFT; |
| 134 | mask = 3 << SD1_DVM_SHIFT; |
| 135 | |
| 136 | return max8925_set_bits(info->i2c, info->enable_reg, mask, data); |
| 137 | } |
| 138 | |
| 139 | static int max8925_set_dvm_enable(struct regulator_dev *rdev) |
| 140 | { |
| 141 | struct max8925_regulator_info *info = rdev_get_drvdata(rdev); |
| 142 | |
| 143 | return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, |
| 144 | 1 << SD1_DVM_EN); |
| 145 | } |
| 146 | |
| 147 | static int max8925_set_dvm_disable(struct regulator_dev *rdev) |
| 148 | { |
| 149 | struct max8925_regulator_info *info = rdev_get_drvdata(rdev); |
| 150 | |
| 151 | return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0); |
| 152 | } |
| 153 | |
| 154 | static struct regulator_ops max8925_regulator_sdv_ops = { |
Axel Lin | 4093e73 | 2012-05-14 10:35:40 +0800 | [diff] [blame^] | 155 | .list_voltage = regulator_list_voltage_linear, |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 156 | .set_voltage = max8925_set_voltage, |
Axel Lin | 3c8983b | 2012-05-14 10:34:50 +0800 | [diff] [blame] | 157 | .get_voltage_sel = max8925_get_voltage_sel, |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 158 | .enable = max8925_enable, |
| 159 | .disable = max8925_disable, |
| 160 | .is_enabled = max8925_is_enabled, |
| 161 | .set_suspend_voltage = max8925_set_dvm_voltage, |
| 162 | .set_suspend_enable = max8925_set_dvm_enable, |
| 163 | .set_suspend_disable = max8925_set_dvm_disable, |
| 164 | }; |
| 165 | |
| 166 | static struct regulator_ops max8925_regulator_ldo_ops = { |
Axel Lin | 4093e73 | 2012-05-14 10:35:40 +0800 | [diff] [blame^] | 167 | .list_voltage = regulator_list_voltage_linear, |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 168 | .set_voltage = max8925_set_voltage, |
Axel Lin | 3c8983b | 2012-05-14 10:34:50 +0800 | [diff] [blame] | 169 | .get_voltage_sel = max8925_get_voltage_sel, |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 170 | .enable = max8925_enable, |
| 171 | .disable = max8925_disable, |
| 172 | .is_enabled = max8925_is_enabled, |
| 173 | }; |
| 174 | |
| 175 | #define MAX8925_SDV(_id, min, max, step) \ |
| 176 | { \ |
| 177 | .desc = { \ |
| 178 | .name = "SDV" #_id, \ |
| 179 | .ops = &max8925_regulator_sdv_ops, \ |
| 180 | .type = REGULATOR_VOLTAGE, \ |
| 181 | .id = MAX8925_ID_SD##_id, \ |
| 182 | .owner = THIS_MODULE, \ |
Axel Lin | 7f52ba7 | 2012-04-02 09:16:08 +0800 | [diff] [blame] | 183 | .n_voltages = 64, \ |
Axel Lin | 4093e73 | 2012-05-14 10:35:40 +0800 | [diff] [blame^] | 184 | .min_uV = min * 1000, \ |
| 185 | .uV_step = step * 1000, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 186 | }, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 187 | .max_uV = max * 1000, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 188 | .vol_reg = MAX8925_SDV##_id, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 189 | .enable_reg = MAX8925_SDCTL##_id, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | #define MAX8925_LDO(_id, min, max, step) \ |
| 193 | { \ |
| 194 | .desc = { \ |
| 195 | .name = "LDO" #_id, \ |
| 196 | .ops = &max8925_regulator_ldo_ops, \ |
| 197 | .type = REGULATOR_VOLTAGE, \ |
| 198 | .id = MAX8925_ID_LDO##_id, \ |
| 199 | .owner = THIS_MODULE, \ |
Axel Lin | 7f52ba7 | 2012-04-02 09:16:08 +0800 | [diff] [blame] | 200 | .n_voltages = 64, \ |
Axel Lin | 4093e73 | 2012-05-14 10:35:40 +0800 | [diff] [blame^] | 201 | .min_uV = min * 1000, \ |
| 202 | .uV_step = step * 1000, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 203 | }, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 204 | .max_uV = max * 1000, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 205 | .vol_reg = MAX8925_LDOVOUT##_id, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 206 | .enable_reg = MAX8925_LDOCTL##_id, \ |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static struct max8925_regulator_info max8925_regulator_info[] = { |
| 210 | MAX8925_SDV(1, 637.5, 1425, 12.5), |
| 211 | MAX8925_SDV(2, 650, 2225, 25), |
| 212 | MAX8925_SDV(3, 750, 3900, 50), |
| 213 | |
| 214 | MAX8925_LDO(1, 750, 3900, 50), |
| 215 | MAX8925_LDO(2, 650, 2250, 25), |
| 216 | MAX8925_LDO(3, 650, 2250, 25), |
| 217 | MAX8925_LDO(4, 750, 3900, 50), |
| 218 | MAX8925_LDO(5, 750, 3900, 50), |
| 219 | MAX8925_LDO(6, 750, 3900, 50), |
| 220 | MAX8925_LDO(7, 750, 3900, 50), |
| 221 | MAX8925_LDO(8, 750, 3900, 50), |
| 222 | MAX8925_LDO(9, 750, 3900, 50), |
| 223 | MAX8925_LDO(10, 750, 3900, 50), |
| 224 | MAX8925_LDO(11, 750, 3900, 50), |
| 225 | MAX8925_LDO(12, 750, 3900, 50), |
| 226 | MAX8925_LDO(13, 750, 3900, 50), |
| 227 | MAX8925_LDO(14, 750, 3900, 50), |
| 228 | MAX8925_LDO(15, 750, 3900, 50), |
| 229 | MAX8925_LDO(16, 750, 3900, 50), |
| 230 | MAX8925_LDO(17, 650, 2250, 25), |
| 231 | MAX8925_LDO(18, 650, 2250, 25), |
| 232 | MAX8925_LDO(19, 750, 3900, 50), |
| 233 | MAX8925_LDO(20, 750, 3900, 50), |
| 234 | }; |
| 235 | |
Dmitry Torokhov | 3fff6c0 | 2010-02-23 23:38:55 -0800 | [diff] [blame] | 236 | static struct max8925_regulator_info * __devinit find_regulator_info(int id) |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 237 | { |
| 238 | struct max8925_regulator_info *ri; |
| 239 | int i; |
| 240 | |
| 241 | for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) { |
| 242 | ri = &max8925_regulator_info[i]; |
| 243 | if (ri->desc.id == id) |
| 244 | return ri; |
| 245 | } |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | static int __devinit max8925_regulator_probe(struct platform_device *pdev) |
| 250 | { |
| 251 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 252 | struct max8925_platform_data *pdata = chip->dev->platform_data; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 253 | struct regulator_config config = { }; |
Dmitry Torokhov | 3fff6c0 | 2010-02-23 23:38:55 -0800 | [diff] [blame] | 254 | struct max8925_regulator_info *ri; |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 255 | struct regulator_dev *rdev; |
| 256 | |
| 257 | ri = find_regulator_info(pdev->id); |
| 258 | if (ri == NULL) { |
| 259 | dev_err(&pdev->dev, "invalid regulator ID specified\n"); |
| 260 | return -EINVAL; |
| 261 | } |
| 262 | ri->i2c = chip->i2c; |
| 263 | ri->chip = chip; |
| 264 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 265 | config.dev = &pdev->dev; |
| 266 | config.init_data = pdata->regulator[pdev->id]; |
| 267 | config.driver_data = ri; |
| 268 | |
| 269 | rdev = regulator_register(&ri->desc, &config); |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 270 | if (IS_ERR(rdev)) { |
| 271 | dev_err(&pdev->dev, "failed to register regulator %s\n", |
| 272 | ri->desc.name); |
| 273 | return PTR_ERR(rdev); |
| 274 | } |
| 275 | |
| 276 | platform_set_drvdata(pdev, rdev); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int __devexit max8925_regulator_remove(struct platform_device *pdev) |
| 281 | { |
| 282 | struct regulator_dev *rdev = platform_get_drvdata(pdev); |
| 283 | |
Dmitry Torokhov | 3fff6c0 | 2010-02-23 23:38:55 -0800 | [diff] [blame] | 284 | platform_set_drvdata(pdev, NULL); |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 285 | regulator_unregister(rdev); |
Dmitry Torokhov | 3fff6c0 | 2010-02-23 23:38:55 -0800 | [diff] [blame] | 286 | |
Haojian Zhuang | 08ff6f2 | 2010-01-11 13:10:08 +0100 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static struct platform_driver max8925_regulator_driver = { |
| 291 | .driver = { |
| 292 | .name = "max8925-regulator", |
| 293 | .owner = THIS_MODULE, |
| 294 | }, |
| 295 | .probe = max8925_regulator_probe, |
| 296 | .remove = __devexit_p(max8925_regulator_remove), |
| 297 | }; |
| 298 | |
| 299 | static int __init max8925_regulator_init(void) |
| 300 | { |
| 301 | return platform_driver_register(&max8925_regulator_driver); |
| 302 | } |
| 303 | subsys_initcall(max8925_regulator_init); |
| 304 | |
| 305 | static void __exit max8925_regulator_exit(void) |
| 306 | { |
| 307 | platform_driver_unregister(&max8925_regulator_driver); |
| 308 | } |
| 309 | module_exit(max8925_regulator_exit); |
| 310 | |
| 311 | MODULE_LICENSE("GPL"); |
| 312 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 313 | MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC"); |
| 314 | MODULE_ALIAS("platform:max8925-regulator"); |