1 /* 2 * wm831x-dcdc.c -- DC-DC buck convertor driver for the WM831x series 3 * 4 * Copyright 2009 Wolfson Microelectronics PLC. 5 * 6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2 of the License, or (at your 11 * option) any later version. 12 */ 13 14 #include <linux/module.h> 15 #include <linux/moduleparam.h> 16 #include <linux/init.h> 17 #include <linux/bitops.h> 18 #include <linux/err.h> 19 #include <linux/i2c.h> 20 #include <linux/platform_device.h> 21 #include <linux/regulator/driver.h> 22 #include <linux/regulator/machine.h> 23 #include <linux/gpio.h> 24 25 #include <linux/mfd/wm831x/core.h> 26 #include <linux/mfd/wm831x/regulator.h> 27 #include <linux/mfd/wm831x/pdata.h> 28 29 #define WM831X_BUCKV_MAX_SELECTOR 0x68 30 #define WM831X_BUCKP_MAX_SELECTOR 0x66 31 32 #define WM831X_DCDC_MODE_FAST 0 33 #define WM831X_DCDC_MODE_NORMAL 1 34 #define WM831X_DCDC_MODE_IDLE 2 35 #define WM831X_DCDC_MODE_STANDBY 3 36 37 #define WM831X_DCDC_MAX_NAME 6 38 39 /* Register offsets in control block */ 40 #define WM831X_DCDC_CONTROL_1 0 41 #define WM831X_DCDC_CONTROL_2 1 42 #define WM831X_DCDC_ON_CONFIG 2 43 #define WM831X_DCDC_SLEEP_CONTROL 3 44 #define WM831X_DCDC_DVS_CONTROL 4 45 46 /* 47 * Shared 48 */ 49 50 struct wm831x_dcdc { 51 char name[WM831X_DCDC_MAX_NAME]; 52 struct regulator_desc desc; 53 int base; 54 struct wm831x *wm831x; 55 struct regulator_dev *regulator; 56 int dvs_gpio; 57 int dvs_gpio_state; 58 int on_vsel; 59 int dvs_vsel; 60 }; 61 62 static int wm831x_dcdc_is_enabled(struct regulator_dev *rdev) 63 { 64 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 65 struct wm831x *wm831x = dcdc->wm831x; 66 int mask = 1 << rdev_get_id(rdev); 67 int reg; 68 69 reg = wm831x_reg_read(wm831x, WM831X_DCDC_ENABLE); 70 if (reg < 0) 71 return reg; 72 73 if (reg & mask) 74 return 1; 75 else 76 return 0; 77 } 78 79 static int wm831x_dcdc_enable(struct regulator_dev *rdev) 80 { 81 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 82 struct wm831x *wm831x = dcdc->wm831x; 83 int mask = 1 << rdev_get_id(rdev); 84 85 return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, mask); 86 } 87 88 static int wm831x_dcdc_disable(struct regulator_dev *rdev) 89 { 90 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 91 struct wm831x *wm831x = dcdc->wm831x; 92 int mask = 1 << rdev_get_id(rdev); 93 94 return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, 0); 95 } 96 97 static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev) 98 99 { 100 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 101 struct wm831x *wm831x = dcdc->wm831x; 102 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 103 int val; 104 105 val = wm831x_reg_read(wm831x, reg); 106 if (val < 0) 107 return val; 108 109 val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT; 110 111 switch (val) { 112 case WM831X_DCDC_MODE_FAST: 113 return REGULATOR_MODE_FAST; 114 case WM831X_DCDC_MODE_NORMAL: 115 return REGULATOR_MODE_NORMAL; 116 case WM831X_DCDC_MODE_STANDBY: 117 return REGULATOR_MODE_STANDBY; 118 case WM831X_DCDC_MODE_IDLE: 119 return REGULATOR_MODE_IDLE; 120 default: 121 BUG(); 122 } 123 } 124 125 static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg, 126 unsigned int mode) 127 { 128 int val; 129 130 switch (mode) { 131 case REGULATOR_MODE_FAST: 132 val = WM831X_DCDC_MODE_FAST; 133 break; 134 case REGULATOR_MODE_NORMAL: 135 val = WM831X_DCDC_MODE_NORMAL; 136 break; 137 case REGULATOR_MODE_STANDBY: 138 val = WM831X_DCDC_MODE_STANDBY; 139 break; 140 case REGULATOR_MODE_IDLE: 141 val = WM831X_DCDC_MODE_IDLE; 142 break; 143 default: 144 return -EINVAL; 145 } 146 147 return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK, 148 val << WM831X_DC1_ON_MODE_SHIFT); 149 } 150 151 static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode) 152 { 153 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 154 struct wm831x *wm831x = dcdc->wm831x; 155 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 156 157 return wm831x_dcdc_set_mode_int(wm831x, reg, mode); 158 } 159 160 static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev, 161 unsigned int mode) 162 { 163 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 164 struct wm831x *wm831x = dcdc->wm831x; 165 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; 166 167 return wm831x_dcdc_set_mode_int(wm831x, reg, mode); 168 } 169 170 static int wm831x_dcdc_get_status(struct regulator_dev *rdev) 171 { 172 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 173 struct wm831x *wm831x = dcdc->wm831x; 174 int ret; 175 176 /* First, check for errors */ 177 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS); 178 if (ret < 0) 179 return ret; 180 181 if (ret & (1 << rdev_get_id(rdev))) { 182 dev_dbg(wm831x->dev, "DCDC%d under voltage\n", 183 rdev_get_id(rdev) + 1); 184 return REGULATOR_STATUS_ERROR; 185 } 186 187 /* DCDC1 and DCDC2 can additionally detect high voltage/current */ 188 if (rdev_get_id(rdev) < 2) { 189 if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) { 190 dev_dbg(wm831x->dev, "DCDC%d over voltage\n", 191 rdev_get_id(rdev) + 1); 192 return REGULATOR_STATUS_ERROR; 193 } 194 195 if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) { 196 dev_dbg(wm831x->dev, "DCDC%d over current\n", 197 rdev_get_id(rdev) + 1); 198 return REGULATOR_STATUS_ERROR; 199 } 200 } 201 202 /* Is the regulator on? */ 203 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS); 204 if (ret < 0) 205 return ret; 206 if (!(ret & (1 << rdev_get_id(rdev)))) 207 return REGULATOR_STATUS_OFF; 208 209 /* TODO: When we handle hardware control modes so we can report the 210 * current mode. */ 211 return REGULATOR_STATUS_ON; 212 } 213 214 static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data) 215 { 216 struct wm831x_dcdc *dcdc = data; 217 218 regulator_notifier_call_chain(dcdc->regulator, 219 REGULATOR_EVENT_UNDER_VOLTAGE, 220 NULL); 221 222 return IRQ_HANDLED; 223 } 224 225 static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data) 226 { 227 struct wm831x_dcdc *dcdc = data; 228 229 regulator_notifier_call_chain(dcdc->regulator, 230 REGULATOR_EVENT_OVER_CURRENT, 231 NULL); 232 233 return IRQ_HANDLED; 234 } 235 236 /* 237 * BUCKV specifics 238 */ 239 240 static int wm831x_buckv_list_voltage(struct regulator_dev *rdev, 241 unsigned selector) 242 { 243 if (selector <= 0x8) 244 return 600000; 245 if (selector <= WM831X_BUCKV_MAX_SELECTOR) 246 return 600000 + ((selector - 0x8) * 12500); 247 return -EINVAL; 248 } 249 250 static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev, 251 int min_uV, int max_uV) 252 { 253 u16 vsel; 254 255 if (min_uV < 600000) 256 vsel = 0; 257 else if (min_uV <= 1800000) 258 vsel = ((min_uV - 600000) / 12500) + 8; 259 else 260 return -EINVAL; 261 262 if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV) 263 return -EINVAL; 264 265 return vsel; 266 } 267 268 static int wm831x_buckv_select_max_voltage(struct regulator_dev *rdev, 269 int min_uV, int max_uV) 270 { 271 u16 vsel; 272 273 if (max_uV < 600000 || max_uV > 1800000) 274 return -EINVAL; 275 276 vsel = ((max_uV - 600000) / 12500) + 8; 277 278 if (wm831x_buckv_list_voltage(rdev, vsel) < min_uV || 279 wm831x_buckv_list_voltage(rdev, vsel) < max_uV) 280 return -EINVAL; 281 282 return vsel; 283 } 284 285 static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state) 286 { 287 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 288 289 if (state == dcdc->dvs_gpio_state) 290 return 0; 291 292 dcdc->dvs_gpio_state = state; 293 gpio_set_value(dcdc->dvs_gpio, state); 294 295 /* Should wait for DVS state change to be asserted if we have 296 * a GPIO for it, for now assume the device is configured 297 * for the fastest possible transition. 298 */ 299 300 return 0; 301 } 302 303 static int wm831x_buckv_set_voltage(struct regulator_dev *rdev, 304 int min_uV, int max_uV) 305 { 306 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 307 struct wm831x *wm831x = dcdc->wm831x; 308 int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 309 int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL; 310 int vsel, ret; 311 312 vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV); 313 if (vsel < 0) 314 return vsel; 315 316 /* If this value is already set then do a GPIO update if we can */ 317 if (dcdc->dvs_gpio && dcdc->on_vsel == vsel) 318 return wm831x_buckv_set_dvs(rdev, 0); 319 320 if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel) 321 return wm831x_buckv_set_dvs(rdev, 1); 322 323 /* Always set the ON status to the minimum voltage */ 324 ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel); 325 if (ret < 0) 326 return ret; 327 dcdc->on_vsel = vsel; 328 329 if (!dcdc->dvs_gpio) 330 return ret; 331 332 /* Kick the voltage transition now */ 333 ret = wm831x_buckv_set_dvs(rdev, 0); 334 if (ret < 0) 335 return ret; 336 337 /* Set the high voltage as the DVS voltage. This is optimised 338 * for CPUfreq usage, most processors will keep the maximum 339 * voltage constant and lower the minimum with the frequency. */ 340 vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV); 341 if (vsel < 0) { 342 /* This should never happen - at worst the same vsel 343 * should be chosen */ 344 WARN_ON(vsel < 0); 345 return 0; 346 } 347 348 /* Don't bother if it's the same VSEL we're already using */ 349 if (vsel == dcdc->on_vsel) 350 return 0; 351 352 ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel); 353 if (ret == 0) 354 dcdc->dvs_vsel = vsel; 355 else 356 dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n", 357 ret); 358 359 return 0; 360 } 361 362 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev, 363 int uV) 364 { 365 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 366 struct wm831x *wm831x = dcdc->wm831x; 367 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; 368 int vsel; 369 370 vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV); 371 if (vsel < 0) 372 return vsel; 373 374 return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel); 375 } 376 377 static int wm831x_buckv_get_voltage(struct regulator_dev *rdev) 378 { 379 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 380 381 if (dcdc->dvs_gpio && dcdc->dvs_gpio_state) 382 return wm831x_buckv_list_voltage(rdev, dcdc->dvs_vsel); 383 else 384 return wm831x_buckv_list_voltage(rdev, dcdc->on_vsel); 385 } 386 387 /* Current limit options */ 388 static u16 wm831x_dcdc_ilim[] = { 389 125, 250, 375, 500, 625, 750, 875, 1000 390 }; 391 392 static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev, 393 int min_uA, int max_uA) 394 { 395 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 396 struct wm831x *wm831x = dcdc->wm831x; 397 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; 398 int i; 399 400 for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) { 401 if (max_uA <= wm831x_dcdc_ilim[i]) 402 break; 403 } 404 if (i == ARRAY_SIZE(wm831x_dcdc_ilim)) 405 return -EINVAL; 406 407 return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i); 408 } 409 410 static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev) 411 { 412 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 413 struct wm831x *wm831x = dcdc->wm831x; 414 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; 415 int val; 416 417 val = wm831x_reg_read(wm831x, reg); 418 if (val < 0) 419 return val; 420 421 return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK]; 422 } 423 424 static struct regulator_ops wm831x_buckv_ops = { 425 .set_voltage = wm831x_buckv_set_voltage, 426 .get_voltage = wm831x_buckv_get_voltage, 427 .list_voltage = wm831x_buckv_list_voltage, 428 .set_suspend_voltage = wm831x_buckv_set_suspend_voltage, 429 .set_current_limit = wm831x_buckv_set_current_limit, 430 .get_current_limit = wm831x_buckv_get_current_limit, 431 432 .is_enabled = wm831x_dcdc_is_enabled, 433 .enable = wm831x_dcdc_enable, 434 .disable = wm831x_dcdc_disable, 435 .get_status = wm831x_dcdc_get_status, 436 .get_mode = wm831x_dcdc_get_mode, 437 .set_mode = wm831x_dcdc_set_mode, 438 .set_suspend_mode = wm831x_dcdc_set_suspend_mode, 439 }; 440 441 /* 442 * Set up DVS control. We just log errors since we can still run 443 * (with reduced performance) if we fail. 444 */ 445 static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc, 446 struct wm831x_buckv_pdata *pdata) 447 { 448 struct wm831x *wm831x = dcdc->wm831x; 449 int ret; 450 u16 ctrl; 451 452 if (!pdata || !pdata->dvs_gpio) 453 return; 454 455 switch (pdata->dvs_control_src) { 456 case 1: 457 ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT; 458 break; 459 case 2: 460 ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT; 461 break; 462 default: 463 dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n", 464 pdata->dvs_control_src, dcdc->name); 465 return; 466 } 467 468 ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL, 469 WM831X_DC1_DVS_SRC_MASK, ctrl); 470 if (ret < 0) { 471 dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n", 472 dcdc->name, ret); 473 return; 474 } 475 476 ret = gpio_request(pdata->dvs_gpio, "DCDC DVS"); 477 if (ret < 0) { 478 dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n", 479 dcdc->name, ret); 480 return; 481 } 482 483 /* gpiolib won't let us read the GPIO status so pick the higher 484 * of the two existing voltages so we take it as platform data. 485 */ 486 dcdc->dvs_gpio_state = pdata->dvs_init_state; 487 488 ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state); 489 if (ret < 0) { 490 dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n", 491 dcdc->name, ret); 492 gpio_free(pdata->dvs_gpio); 493 return; 494 } 495 496 dcdc->dvs_gpio = pdata->dvs_gpio; 497 } 498 499 static __devinit int wm831x_buckv_probe(struct platform_device *pdev) 500 { 501 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 502 struct wm831x_pdata *pdata = wm831x->dev->platform_data; 503 int id = pdev->id % ARRAY_SIZE(pdata->dcdc); 504 struct wm831x_dcdc *dcdc; 505 struct resource *res; 506 int ret, irq; 507 508 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); 509 510 if (pdata == NULL || pdata->dcdc[id] == NULL) 511 return -ENODEV; 512 513 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL); 514 if (dcdc == NULL) { 515 dev_err(&pdev->dev, "Unable to allocate private data\n"); 516 return -ENOMEM; 517 } 518 519 dcdc->wm831x = wm831x; 520 521 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 522 if (res == NULL) { 523 dev_err(&pdev->dev, "No I/O resource\n"); 524 ret = -EINVAL; 525 goto err; 526 } 527 dcdc->base = res->start; 528 529 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); 530 dcdc->desc.name = dcdc->name; 531 dcdc->desc.id = id; 532 dcdc->desc.type = REGULATOR_VOLTAGE; 533 dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1; 534 dcdc->desc.ops = &wm831x_buckv_ops; 535 dcdc->desc.owner = THIS_MODULE; 536 537 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG); 538 if (ret < 0) { 539 dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret); 540 goto err; 541 } 542 dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK; 543 544 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG); 545 if (ret < 0) { 546 dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret); 547 goto err; 548 } 549 dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK; 550 551 if (pdata->dcdc[id]) 552 wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data); 553 554 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev, 555 pdata->dcdc[id], dcdc); 556 if (IS_ERR(dcdc->regulator)) { 557 ret = PTR_ERR(dcdc->regulator); 558 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", 559 id + 1, ret); 560 goto err; 561 } 562 563 irq = platform_get_irq_byname(pdev, "UV"); 564 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq, 565 IRQF_TRIGGER_RISING, dcdc->name, 566 dcdc); 567 if (ret != 0) { 568 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", 569 irq, ret); 570 goto err_regulator; 571 } 572 573 irq = platform_get_irq_byname(pdev, "HC"); 574 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_oc_irq, 575 IRQF_TRIGGER_RISING, dcdc->name, 576 dcdc); 577 if (ret != 0) { 578 dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n", 579 irq, ret); 580 goto err_uv; 581 } 582 583 platform_set_drvdata(pdev, dcdc); 584 585 return 0; 586 587 err_uv: 588 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc); 589 err_regulator: 590 regulator_unregister(dcdc->regulator); 591 err: 592 if (dcdc->dvs_gpio) 593 gpio_free(dcdc->dvs_gpio); 594 kfree(dcdc); 595 return ret; 596 } 597 598 static __devexit int wm831x_buckv_remove(struct platform_device *pdev) 599 { 600 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); 601 struct wm831x *wm831x = dcdc->wm831x; 602 603 platform_set_drvdata(pdev, NULL); 604 605 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "HC"), dcdc); 606 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc); 607 regulator_unregister(dcdc->regulator); 608 if (dcdc->dvs_gpio) 609 gpio_free(dcdc->dvs_gpio); 610 kfree(dcdc); 611 612 return 0; 613 } 614 615 static struct platform_driver wm831x_buckv_driver = { 616 .probe = wm831x_buckv_probe, 617 .remove = __devexit_p(wm831x_buckv_remove), 618 .driver = { 619 .name = "wm831x-buckv", 620 .owner = THIS_MODULE, 621 }, 622 }; 623 624 /* 625 * BUCKP specifics 626 */ 627 628 static int wm831x_buckp_list_voltage(struct regulator_dev *rdev, 629 unsigned selector) 630 { 631 if (selector <= WM831X_BUCKP_MAX_SELECTOR) 632 return 850000 + (selector * 25000); 633 else 634 return -EINVAL; 635 } 636 637 static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg, 638 int min_uV, int max_uV) 639 { 640 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 641 struct wm831x *wm831x = dcdc->wm831x; 642 u16 vsel; 643 644 if (min_uV <= 34000000) 645 vsel = (min_uV - 850000) / 25000; 646 else 647 return -EINVAL; 648 649 if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV) 650 return -EINVAL; 651 652 return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel); 653 } 654 655 static int wm831x_buckp_set_voltage(struct regulator_dev *rdev, 656 int min_uV, int max_uV) 657 { 658 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 659 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 660 661 return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV); 662 } 663 664 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, 665 int uV) 666 { 667 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 668 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; 669 670 return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV); 671 } 672 673 static int wm831x_buckp_get_voltage(struct regulator_dev *rdev) 674 { 675 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 676 struct wm831x *wm831x = dcdc->wm831x; 677 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 678 int val; 679 680 val = wm831x_reg_read(wm831x, reg); 681 if (val < 0) 682 return val; 683 684 return wm831x_buckp_list_voltage(rdev, val & WM831X_DC3_ON_VSEL_MASK); 685 } 686 687 static struct regulator_ops wm831x_buckp_ops = { 688 .set_voltage = wm831x_buckp_set_voltage, 689 .get_voltage = wm831x_buckp_get_voltage, 690 .list_voltage = wm831x_buckp_list_voltage, 691 .set_suspend_voltage = wm831x_buckp_set_suspend_voltage, 692 693 .is_enabled = wm831x_dcdc_is_enabled, 694 .enable = wm831x_dcdc_enable, 695 .disable = wm831x_dcdc_disable, 696 .get_status = wm831x_dcdc_get_status, 697 .get_mode = wm831x_dcdc_get_mode, 698 .set_mode = wm831x_dcdc_set_mode, 699 .set_suspend_mode = wm831x_dcdc_set_suspend_mode, 700 }; 701 702 static __devinit int wm831x_buckp_probe(struct platform_device *pdev) 703 { 704 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 705 struct wm831x_pdata *pdata = wm831x->dev->platform_data; 706 int id = pdev->id % ARRAY_SIZE(pdata->dcdc); 707 struct wm831x_dcdc *dcdc; 708 struct resource *res; 709 int ret, irq; 710 711 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); 712 713 if (pdata == NULL || pdata->dcdc[id] == NULL) 714 return -ENODEV; 715 716 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL); 717 if (dcdc == NULL) { 718 dev_err(&pdev->dev, "Unable to allocate private data\n"); 719 return -ENOMEM; 720 } 721 722 dcdc->wm831x = wm831x; 723 724 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 725 if (res == NULL) { 726 dev_err(&pdev->dev, "No I/O resource\n"); 727 ret = -EINVAL; 728 goto err; 729 } 730 dcdc->base = res->start; 731 732 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); 733 dcdc->desc.name = dcdc->name; 734 dcdc->desc.id = id; 735 dcdc->desc.type = REGULATOR_VOLTAGE; 736 dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1; 737 dcdc->desc.ops = &wm831x_buckp_ops; 738 dcdc->desc.owner = THIS_MODULE; 739 740 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev, 741 pdata->dcdc[id], dcdc); 742 if (IS_ERR(dcdc->regulator)) { 743 ret = PTR_ERR(dcdc->regulator); 744 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", 745 id + 1, ret); 746 goto err; 747 } 748 749 irq = platform_get_irq_byname(pdev, "UV"); 750 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq, 751 IRQF_TRIGGER_RISING, dcdc->name, 752 dcdc); 753 if (ret != 0) { 754 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", 755 irq, ret); 756 goto err_regulator; 757 } 758 759 platform_set_drvdata(pdev, dcdc); 760 761 return 0; 762 763 err_regulator: 764 regulator_unregister(dcdc->regulator); 765 err: 766 kfree(dcdc); 767 return ret; 768 } 769 770 static __devexit int wm831x_buckp_remove(struct platform_device *pdev) 771 { 772 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); 773 struct wm831x *wm831x = dcdc->wm831x; 774 775 platform_set_drvdata(pdev, NULL); 776 777 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc); 778 regulator_unregister(dcdc->regulator); 779 kfree(dcdc); 780 781 return 0; 782 } 783 784 static struct platform_driver wm831x_buckp_driver = { 785 .probe = wm831x_buckp_probe, 786 .remove = __devexit_p(wm831x_buckp_remove), 787 .driver = { 788 .name = "wm831x-buckp", 789 .owner = THIS_MODULE, 790 }, 791 }; 792 793 /* 794 * DCDC boost convertors 795 */ 796 797 static int wm831x_boostp_get_status(struct regulator_dev *rdev) 798 { 799 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 800 struct wm831x *wm831x = dcdc->wm831x; 801 int ret; 802 803 /* First, check for errors */ 804 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS); 805 if (ret < 0) 806 return ret; 807 808 if (ret & (1 << rdev_get_id(rdev))) { 809 dev_dbg(wm831x->dev, "DCDC%d under voltage\n", 810 rdev_get_id(rdev) + 1); 811 return REGULATOR_STATUS_ERROR; 812 } 813 814 /* Is the regulator on? */ 815 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS); 816 if (ret < 0) 817 return ret; 818 if (ret & (1 << rdev_get_id(rdev))) 819 return REGULATOR_STATUS_ON; 820 else 821 return REGULATOR_STATUS_OFF; 822 } 823 824 static struct regulator_ops wm831x_boostp_ops = { 825 .get_status = wm831x_boostp_get_status, 826 827 .is_enabled = wm831x_dcdc_is_enabled, 828 .enable = wm831x_dcdc_enable, 829 .disable = wm831x_dcdc_disable, 830 }; 831 832 static __devinit int wm831x_boostp_probe(struct platform_device *pdev) 833 { 834 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 835 struct wm831x_pdata *pdata = wm831x->dev->platform_data; 836 int id = pdev->id % ARRAY_SIZE(pdata->dcdc); 837 struct wm831x_dcdc *dcdc; 838 struct resource *res; 839 int ret, irq; 840 841 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); 842 843 if (pdata == NULL || pdata->dcdc[id] == NULL) 844 return -ENODEV; 845 846 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL); 847 if (dcdc == NULL) { 848 dev_err(&pdev->dev, "Unable to allocate private data\n"); 849 return -ENOMEM; 850 } 851 852 dcdc->wm831x = wm831x; 853 854 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 855 if (res == NULL) { 856 dev_err(&pdev->dev, "No I/O resource\n"); 857 ret = -EINVAL; 858 goto err; 859 } 860 dcdc->base = res->start; 861 862 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); 863 dcdc->desc.name = dcdc->name; 864 dcdc->desc.id = id; 865 dcdc->desc.type = REGULATOR_VOLTAGE; 866 dcdc->desc.ops = &wm831x_boostp_ops; 867 dcdc->desc.owner = THIS_MODULE; 868 869 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev, 870 pdata->dcdc[id], dcdc); 871 if (IS_ERR(dcdc->regulator)) { 872 ret = PTR_ERR(dcdc->regulator); 873 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", 874 id + 1, ret); 875 goto err; 876 } 877 878 irq = platform_get_irq_byname(pdev, "UV"); 879 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq, 880 IRQF_TRIGGER_RISING, dcdc->name, 881 dcdc); 882 if (ret != 0) { 883 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", 884 irq, ret); 885 goto err_regulator; 886 } 887 888 platform_set_drvdata(pdev, dcdc); 889 890 return 0; 891 892 err_regulator: 893 regulator_unregister(dcdc->regulator); 894 err: 895 kfree(dcdc); 896 return ret; 897 } 898 899 static __devexit int wm831x_boostp_remove(struct platform_device *pdev) 900 { 901 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); 902 struct wm831x *wm831x = dcdc->wm831x; 903 904 platform_set_drvdata(pdev, NULL); 905 906 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc); 907 regulator_unregister(dcdc->regulator); 908 kfree(dcdc); 909 910 return 0; 911 } 912 913 static struct platform_driver wm831x_boostp_driver = { 914 .probe = wm831x_boostp_probe, 915 .remove = __devexit_p(wm831x_boostp_remove), 916 .driver = { 917 .name = "wm831x-boostp", 918 .owner = THIS_MODULE, 919 }, 920 }; 921 922 /* 923 * External Power Enable 924 * 925 * These aren't actually DCDCs but look like them in hardware so share 926 * code. 927 */ 928 929 #define WM831X_EPE_BASE 6 930 931 static struct regulator_ops wm831x_epe_ops = { 932 .is_enabled = wm831x_dcdc_is_enabled, 933 .enable = wm831x_dcdc_enable, 934 .disable = wm831x_dcdc_disable, 935 .get_status = wm831x_dcdc_get_status, 936 }; 937 938 static __devinit int wm831x_epe_probe(struct platform_device *pdev) 939 { 940 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 941 struct wm831x_pdata *pdata = wm831x->dev->platform_data; 942 int id = pdev->id % ARRAY_SIZE(pdata->epe); 943 struct wm831x_dcdc *dcdc; 944 int ret; 945 946 dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1); 947 948 if (pdata == NULL || pdata->epe[id] == NULL) 949 return -ENODEV; 950 951 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL); 952 if (dcdc == NULL) { 953 dev_err(&pdev->dev, "Unable to allocate private data\n"); 954 return -ENOMEM; 955 } 956 957 dcdc->wm831x = wm831x; 958 959 /* For current parts this is correct; probably need to revisit 960 * in future. 961 */ 962 snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1); 963 dcdc->desc.name = dcdc->name; 964 dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */ 965 dcdc->desc.ops = &wm831x_epe_ops; 966 dcdc->desc.type = REGULATOR_VOLTAGE; 967 dcdc->desc.owner = THIS_MODULE; 968 969 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev, 970 pdata->epe[id], dcdc); 971 if (IS_ERR(dcdc->regulator)) { 972 ret = PTR_ERR(dcdc->regulator); 973 dev_err(wm831x->dev, "Failed to register EPE%d: %d\n", 974 id + 1, ret); 975 goto err; 976 } 977 978 platform_set_drvdata(pdev, dcdc); 979 980 return 0; 981 982 err: 983 kfree(dcdc); 984 return ret; 985 } 986 987 static __devexit int wm831x_epe_remove(struct platform_device *pdev) 988 { 989 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); 990 991 platform_set_drvdata(pdev, NULL); 992 993 regulator_unregister(dcdc->regulator); 994 kfree(dcdc); 995 996 return 0; 997 } 998 999 static struct platform_driver wm831x_epe_driver = { 1000 .probe = wm831x_epe_probe, 1001 .remove = __devexit_p(wm831x_epe_remove), 1002 .driver = { 1003 .name = "wm831x-epe", 1004 .owner = THIS_MODULE, 1005 }, 1006 }; 1007 1008 static int __init wm831x_dcdc_init(void) 1009 { 1010 int ret; 1011 ret = platform_driver_register(&wm831x_buckv_driver); 1012 if (ret != 0) 1013 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret); 1014 1015 ret = platform_driver_register(&wm831x_buckp_driver); 1016 if (ret != 0) 1017 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret); 1018 1019 ret = platform_driver_register(&wm831x_boostp_driver); 1020 if (ret != 0) 1021 pr_err("Failed to register WM831x BOOST driver: %d\n", ret); 1022 1023 ret = platform_driver_register(&wm831x_epe_driver); 1024 if (ret != 0) 1025 pr_err("Failed to register WM831x EPE driver: %d\n", ret); 1026 1027 return 0; 1028 } 1029 subsys_initcall(wm831x_dcdc_init); 1030 1031 static void __exit wm831x_dcdc_exit(void) 1032 { 1033 platform_driver_unregister(&wm831x_epe_driver); 1034 platform_driver_unregister(&wm831x_boostp_driver); 1035 platform_driver_unregister(&wm831x_buckp_driver); 1036 platform_driver_unregister(&wm831x_buckv_driver); 1037 } 1038 module_exit(wm831x_dcdc_exit); 1039 1040 /* Module information */ 1041 MODULE_AUTHOR("Mark Brown"); 1042 MODULE_DESCRIPTION("WM831x DC-DC convertor driver"); 1043 MODULE_LICENSE("GPL"); 1044 MODULE_ALIAS("platform:wm831x-buckv"); 1045 MODULE_ALIAS("platform:wm831x-buckp"); 1046