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