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> |
| 19 | |
| 20 | #define MAX8649_DCDC_VMIN 750000 /* uV */ |
| 21 | #define MAX8649_DCDC_VMAX 1380000 /* uV */ |
| 22 | #define MAX8649_DCDC_STEP 10000 /* uV */ |
| 23 | #define MAX8649_VOL_MASK 0x3f |
| 24 | |
| 25 | /* Registers */ |
| 26 | #define MAX8649_MODE0 0x00 |
| 27 | #define MAX8649_MODE1 0x01 |
| 28 | #define MAX8649_MODE2 0x02 |
| 29 | #define MAX8649_MODE3 0x03 |
| 30 | #define MAX8649_CONTROL 0x04 |
| 31 | #define MAX8649_SYNC 0x05 |
| 32 | #define MAX8649_RAMP 0x06 |
| 33 | #define MAX8649_CHIP_ID1 0x08 |
| 34 | #define MAX8649_CHIP_ID2 0x09 |
| 35 | |
| 36 | /* Bits */ |
| 37 | #define MAX8649_EN_PD (1 << 7) |
| 38 | #define MAX8649_VID0_PD (1 << 6) |
| 39 | #define MAX8649_VID1_PD (1 << 5) |
| 40 | #define MAX8649_VID_MASK (3 << 5) |
| 41 | |
| 42 | #define MAX8649_FORCE_PWM (1 << 7) |
| 43 | #define MAX8649_SYNC_EXTCLK (1 << 6) |
| 44 | |
| 45 | #define MAX8649_EXT_MASK (3 << 6) |
| 46 | |
| 47 | #define MAX8649_RAMP_MASK (7 << 5) |
| 48 | #define MAX8649_RAMP_DOWN (1 << 1) |
| 49 | |
| 50 | struct max8649_regulator_info { |
| 51 | struct regulator_dev *regulator; |
| 52 | struct i2c_client *i2c; |
| 53 | struct device *dev; |
| 54 | struct mutex io_lock; |
| 55 | |
| 56 | int vol_reg; |
| 57 | unsigned mode:2; /* bit[1:0] = VID1, VID0 */ |
| 58 | unsigned extclk_freq:2; |
| 59 | unsigned extclk:1; |
| 60 | unsigned ramp_timing:3; |
| 61 | unsigned ramp_down:1; |
| 62 | }; |
| 63 | |
| 64 | /* I2C operations */ |
| 65 | |
| 66 | static inline int max8649_read_device(struct i2c_client *i2c, |
| 67 | int reg, int bytes, void *dest) |
| 68 | { |
| 69 | unsigned char data; |
| 70 | int ret; |
| 71 | |
| 72 | data = (unsigned char)reg; |
| 73 | ret = i2c_master_send(i2c, &data, 1); |
| 74 | if (ret < 0) |
| 75 | return ret; |
| 76 | ret = i2c_master_recv(i2c, dest, bytes); |
| 77 | if (ret < 0) |
| 78 | return ret; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static inline int max8649_write_device(struct i2c_client *i2c, |
| 83 | int reg, int bytes, void *src) |
| 84 | { |
| 85 | unsigned char buf[bytes + 1]; |
| 86 | int ret; |
| 87 | |
| 88 | buf[0] = (unsigned char)reg; |
| 89 | memcpy(&buf[1], src, bytes); |
| 90 | |
| 91 | ret = i2c_master_send(i2c, buf, bytes + 1); |
| 92 | if (ret < 0) |
| 93 | return ret; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int max8649_reg_read(struct i2c_client *i2c, int reg) |
| 98 | { |
| 99 | struct max8649_regulator_info *info = i2c_get_clientdata(i2c); |
| 100 | unsigned char data; |
| 101 | int ret; |
| 102 | |
| 103 | mutex_lock(&info->io_lock); |
| 104 | ret = max8649_read_device(i2c, reg, 1, &data); |
| 105 | mutex_unlock(&info->io_lock); |
| 106 | |
| 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | return (int)data; |
| 110 | } |
| 111 | |
| 112 | static int max8649_set_bits(struct i2c_client *i2c, int reg, |
| 113 | unsigned char mask, unsigned char data) |
| 114 | { |
| 115 | struct max8649_regulator_info *info = i2c_get_clientdata(i2c); |
| 116 | unsigned char value; |
| 117 | int ret; |
| 118 | |
| 119 | mutex_lock(&info->io_lock); |
| 120 | ret = max8649_read_device(i2c, reg, 1, &value); |
| 121 | if (ret < 0) |
| 122 | goto out; |
| 123 | value &= ~mask; |
| 124 | value |= data; |
| 125 | ret = max8649_write_device(i2c, reg, 1, &value); |
| 126 | out: |
| 127 | mutex_unlock(&info->io_lock); |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | static inline int check_range(int min_uV, int max_uV) |
| 132 | { |
| 133 | if ((min_uV < MAX8649_DCDC_VMIN) || (max_uV > MAX8649_DCDC_VMAX) |
| 134 | || (min_uV > max_uV)) |
| 135 | return -EINVAL; |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int max8649_list_voltage(struct regulator_dev *rdev, unsigned index) |
| 140 | { |
| 141 | return (MAX8649_DCDC_VMIN + index * MAX8649_DCDC_STEP); |
| 142 | } |
| 143 | |
| 144 | static int max8649_get_voltage(struct regulator_dev *rdev) |
| 145 | { |
| 146 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 147 | unsigned char data; |
| 148 | int ret; |
| 149 | |
| 150 | ret = max8649_reg_read(info->i2c, info->vol_reg); |
| 151 | if (ret < 0) |
| 152 | return ret; |
| 153 | data = (unsigned char)ret & MAX8649_VOL_MASK; |
| 154 | return max8649_list_voltage(rdev, data); |
| 155 | } |
| 156 | |
| 157 | static int max8649_set_voltage(struct regulator_dev *rdev, |
| 158 | int min_uV, int max_uV) |
| 159 | { |
| 160 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 161 | unsigned char data, mask; |
| 162 | |
| 163 | if (check_range(min_uV, max_uV)) { |
| 164 | dev_err(info->dev, "invalid voltage range (%d, %d) uV\n", |
| 165 | min_uV, max_uV); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | data = (min_uV - MAX8649_DCDC_VMIN + MAX8649_DCDC_STEP - 1) |
| 169 | / MAX8649_DCDC_STEP; |
| 170 | mask = MAX8649_VOL_MASK; |
| 171 | |
| 172 | return max8649_set_bits(info->i2c, info->vol_reg, mask, data); |
| 173 | } |
| 174 | |
| 175 | /* EN_PD means pulldown on EN input */ |
| 176 | static int max8649_enable(struct regulator_dev *rdev) |
| 177 | { |
| 178 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 179 | return max8649_set_bits(info->i2c, MAX8649_CONTROL, MAX8649_EN_PD, 0); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Applied internal pulldown resistor on EN input pin. |
| 184 | * If pulldown EN pin outside, it would be better. |
| 185 | */ |
| 186 | static int max8649_disable(struct regulator_dev *rdev) |
| 187 | { |
| 188 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 189 | return max8649_set_bits(info->i2c, MAX8649_CONTROL, MAX8649_EN_PD, |
| 190 | MAX8649_EN_PD); |
| 191 | } |
| 192 | |
| 193 | static int max8649_is_enabled(struct regulator_dev *rdev) |
| 194 | { |
| 195 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 196 | int ret; |
| 197 | |
| 198 | ret = max8649_reg_read(info->i2c, MAX8649_CONTROL); |
| 199 | if (ret < 0) |
| 200 | return ret; |
| 201 | return !((unsigned char)ret & MAX8649_EN_PD); |
| 202 | } |
| 203 | |
| 204 | static int max8649_enable_time(struct regulator_dev *rdev) |
| 205 | { |
| 206 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 207 | int voltage, rate, ret; |
| 208 | |
| 209 | /* get voltage */ |
| 210 | ret = max8649_reg_read(info->i2c, info->vol_reg); |
| 211 | if (ret < 0) |
| 212 | return ret; |
| 213 | ret &= MAX8649_VOL_MASK; |
| 214 | voltage = max8649_list_voltage(rdev, (unsigned char)ret); /* uV */ |
| 215 | |
| 216 | /* get rate */ |
| 217 | ret = max8649_reg_read(info->i2c, MAX8649_RAMP); |
| 218 | if (ret < 0) |
| 219 | return ret; |
| 220 | ret = (ret & MAX8649_RAMP_MASK) >> 5; |
| 221 | rate = (32 * 1000) >> ret; /* uV/uS */ |
| 222 | |
| 223 | return (voltage / rate); |
| 224 | } |
| 225 | |
| 226 | static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 227 | { |
| 228 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 229 | |
| 230 | switch (mode) { |
| 231 | case REGULATOR_MODE_FAST: |
| 232 | max8649_set_bits(info->i2c, info->vol_reg, MAX8649_FORCE_PWM, |
| 233 | MAX8649_FORCE_PWM); |
| 234 | break; |
| 235 | case REGULATOR_MODE_NORMAL: |
| 236 | max8649_set_bits(info->i2c, info->vol_reg, |
| 237 | MAX8649_FORCE_PWM, 0); |
| 238 | break; |
| 239 | default: |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static unsigned int max8649_get_mode(struct regulator_dev *rdev) |
| 246 | { |
| 247 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
| 248 | int ret; |
| 249 | |
| 250 | ret = max8649_reg_read(info->i2c, info->vol_reg); |
| 251 | if (ret & MAX8649_FORCE_PWM) |
| 252 | return REGULATOR_MODE_FAST; |
| 253 | return REGULATOR_MODE_NORMAL; |
| 254 | } |
| 255 | |
| 256 | static struct regulator_ops max8649_dcdc_ops = { |
| 257 | .set_voltage = max8649_set_voltage, |
| 258 | .get_voltage = max8649_get_voltage, |
| 259 | .list_voltage = max8649_list_voltage, |
| 260 | .enable = max8649_enable, |
| 261 | .disable = max8649_disable, |
| 262 | .is_enabled = max8649_is_enabled, |
| 263 | .enable_time = max8649_enable_time, |
| 264 | .set_mode = max8649_set_mode, |
| 265 | .get_mode = max8649_get_mode, |
| 266 | |
| 267 | }; |
| 268 | |
| 269 | static struct regulator_desc dcdc_desc = { |
| 270 | .name = "max8649", |
| 271 | .ops = &max8649_dcdc_ops, |
| 272 | .type = REGULATOR_VOLTAGE, |
| 273 | .n_voltages = 1 << 6, |
| 274 | .owner = THIS_MODULE, |
| 275 | }; |
| 276 | |
| 277 | static int __devinit max8649_regulator_probe(struct i2c_client *client, |
| 278 | const struct i2c_device_id *id) |
| 279 | { |
| 280 | struct max8649_platform_data *pdata = client->dev.platform_data; |
| 281 | struct max8649_regulator_info *info = NULL; |
| 282 | unsigned char data; |
| 283 | int ret; |
| 284 | |
| 285 | info = kzalloc(sizeof(struct max8649_regulator_info), GFP_KERNEL); |
| 286 | if (!info) { |
| 287 | dev_err(&client->dev, "No enough memory\n"); |
| 288 | return -ENOMEM; |
| 289 | } |
| 290 | |
| 291 | info->i2c = client; |
| 292 | info->dev = &client->dev; |
| 293 | mutex_init(&info->io_lock); |
| 294 | i2c_set_clientdata(client, info); |
| 295 | |
| 296 | info->mode = pdata->mode; |
| 297 | switch (info->mode) { |
| 298 | case 0: |
| 299 | info->vol_reg = MAX8649_MODE0; |
| 300 | break; |
| 301 | case 1: |
| 302 | info->vol_reg = MAX8649_MODE1; |
| 303 | break; |
| 304 | case 2: |
| 305 | info->vol_reg = MAX8649_MODE2; |
| 306 | break; |
| 307 | case 3: |
| 308 | info->vol_reg = MAX8649_MODE3; |
| 309 | break; |
| 310 | default: |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | ret = max8649_reg_read(info->i2c, MAX8649_CHIP_ID1); |
| 315 | if (ret < 0) { |
| 316 | dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n", |
| 317 | ret); |
| 318 | goto out; |
| 319 | } |
| 320 | dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", ret); |
| 321 | |
| 322 | /* enable VID0 & VID1 */ |
| 323 | max8649_set_bits(info->i2c, MAX8649_CONTROL, MAX8649_VID_MASK, 0); |
| 324 | |
| 325 | /* enable/disable external clock synchronization */ |
| 326 | info->extclk = pdata->extclk; |
| 327 | data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0; |
| 328 | max8649_set_bits(info->i2c, info->vol_reg, MAX8649_SYNC_EXTCLK, data); |
| 329 | if (info->extclk) { |
| 330 | /* set external clock frequency */ |
| 331 | info->extclk_freq = pdata->extclk_freq; |
| 332 | max8649_set_bits(info->i2c, MAX8649_SYNC, MAX8649_EXT_MASK, |
Axel Lin | 0f69c89 | 2010-10-01 13:56:27 +0800 | [diff] [blame] | 333 | info->extclk_freq << 6); |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if (pdata->ramp_timing) { |
| 337 | info->ramp_timing = pdata->ramp_timing; |
| 338 | max8649_set_bits(info->i2c, MAX8649_RAMP, MAX8649_RAMP_MASK, |
| 339 | info->ramp_timing << 5); |
| 340 | } |
| 341 | |
| 342 | info->ramp_down = pdata->ramp_down; |
| 343 | if (info->ramp_down) { |
| 344 | max8649_set_bits(info->i2c, MAX8649_RAMP, MAX8649_RAMP_DOWN, |
| 345 | MAX8649_RAMP_DOWN); |
| 346 | } |
| 347 | |
| 348 | info->regulator = regulator_register(&dcdc_desc, &client->dev, |
| 349 | pdata->regulator, info); |
| 350 | if (IS_ERR(info->regulator)) { |
| 351 | dev_err(info->dev, "failed to register regulator %s\n", |
| 352 | dcdc_desc.name); |
| 353 | ret = PTR_ERR(info->regulator); |
| 354 | goto out; |
| 355 | } |
| 356 | |
| 357 | dev_info(info->dev, "Max8649 regulator device is detected.\n"); |
| 358 | return 0; |
| 359 | out: |
| 360 | kfree(info); |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | static int __devexit max8649_regulator_remove(struct i2c_client *client) |
| 365 | { |
| 366 | struct max8649_regulator_info *info = i2c_get_clientdata(client); |
| 367 | |
| 368 | if (info) { |
| 369 | if (info->regulator) |
| 370 | regulator_unregister(info->regulator); |
| 371 | kfree(info); |
| 372 | } |
Haojian Zhuang | a71b797 | 2010-01-25 10:24:09 -0500 | [diff] [blame] | 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static const struct i2c_device_id max8649_id[] = { |
| 378 | { "max8649", 0 }, |
| 379 | { } |
| 380 | }; |
| 381 | MODULE_DEVICE_TABLE(i2c, max8649_id); |
| 382 | |
| 383 | static struct i2c_driver max8649_driver = { |
| 384 | .probe = max8649_regulator_probe, |
| 385 | .remove = __devexit_p(max8649_regulator_remove), |
| 386 | .driver = { |
| 387 | .name = "max8649", |
| 388 | }, |
| 389 | .id_table = max8649_id, |
| 390 | }; |
| 391 | |
| 392 | static int __init max8649_init(void) |
| 393 | { |
| 394 | return i2c_add_driver(&max8649_driver); |
| 395 | } |
| 396 | subsys_initcall(max8649_init); |
| 397 | |
| 398 | static void __exit max8649_exit(void) |
| 399 | { |
| 400 | i2c_del_driver(&max8649_driver); |
| 401 | } |
| 402 | module_exit(max8649_exit); |
| 403 | |
| 404 | /* Module information */ |
| 405 | MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver"); |
| 406 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 407 | MODULE_LICENSE("GPL"); |
| 408 | |