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