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