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_select_max_voltage(struct regulator_dev *rdev, 271 int min_uV, int max_uV) 272 { 273 u16 vsel; 274 275 if (max_uV < 600000 || max_uV > 1800000) 276 return -EINVAL; 277 278 vsel = ((max_uV - 600000) / 12500) + 8; 279 280 if (wm831x_buckv_list_voltage(rdev, vsel) < min_uV || 281 wm831x_buckv_list_voltage(rdev, vsel) < max_uV) 282 return -EINVAL; 283 284 return vsel; 285 } 286 287 static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state) 288 { 289 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 290 291 if (state == dcdc->dvs_gpio_state) 292 return 0; 293 294 dcdc->dvs_gpio_state = state; 295 gpio_set_value(dcdc->dvs_gpio, state); 296 297 /* Should wait for DVS state change to be asserted if we have 298 * a GPIO for it, for now assume the device is configured 299 * for the fastest possible transition. 300 */ 301 302 return 0; 303 } 304 305 static int wm831x_buckv_set_voltage(struct regulator_dev *rdev, 306 int min_uV, int max_uV, unsigned *selector) 307 { 308 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 309 struct wm831x *wm831x = dcdc->wm831x; 310 int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 311 int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL; 312 int vsel, ret; 313 314 vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV); 315 if (vsel < 0) 316 return vsel; 317 318 *selector = vsel; 319 320 /* If this value is already set then do a GPIO update if we can */ 321 if (dcdc->dvs_gpio && dcdc->on_vsel == vsel) 322 return wm831x_buckv_set_dvs(rdev, 0); 323 324 if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel) 325 return wm831x_buckv_set_dvs(rdev, 1); 326 327 /* Always set the ON status to the minimum voltage */ 328 ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel); 329 if (ret < 0) 330 return ret; 331 dcdc->on_vsel = vsel; 332 333 if (!dcdc->dvs_gpio) 334 return ret; 335 336 /* Kick the voltage transition now */ 337 ret = wm831x_buckv_set_dvs(rdev, 0); 338 if (ret < 0) 339 return ret; 340 341 /* Set the high voltage as the DVS voltage. This is optimised 342 * for CPUfreq usage, most processors will keep the maximum 343 * voltage constant and lower the minimum with the frequency. */ 344 vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV); 345 if (vsel < 0) { 346 /* This should never happen - at worst the same vsel 347 * should be chosen */ 348 WARN_ON(vsel < 0); 349 return 0; 350 } 351 352 /* Don't bother if it's the same VSEL we're already using */ 353 if (vsel == dcdc->on_vsel) 354 return 0; 355 356 ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel); 357 if (ret == 0) 358 dcdc->dvs_vsel = vsel; 359 else 360 dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n", 361 ret); 362 363 return 0; 364 } 365 366 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev, 367 int uV) 368 { 369 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 370 struct wm831x *wm831x = dcdc->wm831x; 371 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; 372 int vsel; 373 374 vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV); 375 if (vsel < 0) 376 return vsel; 377 378 return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel); 379 } 380 381 static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev) 382 { 383 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 384 385 if (dcdc->dvs_gpio && dcdc->dvs_gpio_state) 386 return dcdc->dvs_vsel; 387 else 388 return dcdc->on_vsel; 389 } 390 391 /* Current limit options */ 392 static u16 wm831x_dcdc_ilim[] = { 393 125, 250, 375, 500, 625, 750, 875, 1000 394 }; 395 396 static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev, 397 int min_uA, int max_uA) 398 { 399 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 400 struct wm831x *wm831x = dcdc->wm831x; 401 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; 402 int i; 403 404 for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) { 405 if (max_uA <= wm831x_dcdc_ilim[i]) 406 break; 407 } 408 if (i == ARRAY_SIZE(wm831x_dcdc_ilim)) 409 return -EINVAL; 410 411 return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i); 412 } 413 414 static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev) 415 { 416 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 417 struct wm831x *wm831x = dcdc->wm831x; 418 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; 419 int val; 420 421 val = wm831x_reg_read(wm831x, reg); 422 if (val < 0) 423 return val; 424 425 return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK]; 426 } 427 428 static struct regulator_ops wm831x_buckv_ops = { 429 .set_voltage = wm831x_buckv_set_voltage, 430 .get_voltage_sel = wm831x_buckv_get_voltage_sel, 431 .list_voltage = wm831x_buckv_list_voltage, 432 .set_suspend_voltage = wm831x_buckv_set_suspend_voltage, 433 .set_current_limit = wm831x_buckv_set_current_limit, 434 .get_current_limit = wm831x_buckv_get_current_limit, 435 436 .is_enabled = wm831x_dcdc_is_enabled, 437 .enable = wm831x_dcdc_enable, 438 .disable = wm831x_dcdc_disable, 439 .get_status = wm831x_dcdc_get_status, 440 .get_mode = wm831x_dcdc_get_mode, 441 .set_mode = wm831x_dcdc_set_mode, 442 .set_suspend_mode = wm831x_dcdc_set_suspend_mode, 443 }; 444 445 /* 446 * Set up DVS control. We just log errors since we can still run 447 * (with reduced performance) if we fail. 448 */ 449 static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc, 450 struct wm831x_buckv_pdata *pdata) 451 { 452 struct wm831x *wm831x = dcdc->wm831x; 453 int ret; 454 u16 ctrl; 455 456 if (!pdata || !pdata->dvs_gpio) 457 return; 458 459 ret = gpio_request(pdata->dvs_gpio, "DCDC DVS"); 460 if (ret < 0) { 461 dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n", 462 dcdc->name, ret); 463 return; 464 } 465 466 /* gpiolib won't let us read the GPIO status so pick the higher 467 * of the two existing voltages so we take it as platform data. 468 */ 469 dcdc->dvs_gpio_state = pdata->dvs_init_state; 470 471 ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state); 472 if (ret < 0) { 473 dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n", 474 dcdc->name, ret); 475 gpio_free(pdata->dvs_gpio); 476 return; 477 } 478 479 dcdc->dvs_gpio = pdata->dvs_gpio; 480 481 switch (pdata->dvs_control_src) { 482 case 1: 483 ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT; 484 break; 485 case 2: 486 ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT; 487 break; 488 default: 489 dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n", 490 pdata->dvs_control_src, dcdc->name); 491 return; 492 } 493 494 /* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL 495 * to make bootstrapping a bit smoother. 496 */ 497 if (!dcdc->dvs_vsel) { 498 ret = wm831x_set_bits(wm831x, 499 dcdc->base + WM831X_DCDC_DVS_CONTROL, 500 WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel); 501 if (ret == 0) 502 dcdc->dvs_vsel = dcdc->on_vsel; 503 else 504 dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n", 505 ret); 506 } 507 508 ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL, 509 WM831X_DC1_DVS_SRC_MASK, ctrl); 510 if (ret < 0) { 511 dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n", 512 dcdc->name, ret); 513 } 514 } 515 516 static __devinit int wm831x_buckv_probe(struct platform_device *pdev) 517 { 518 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 519 struct wm831x_pdata *pdata = wm831x->dev->platform_data; 520 int id; 521 struct wm831x_dcdc *dcdc; 522 struct resource *res; 523 int ret, irq; 524 525 if (pdata && pdata->wm831x_num) 526 id = (pdata->wm831x_num * 10) + 1; 527 else 528 id = 0; 529 id = pdev->id - id; 530 531 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); 532 533 if (pdata == NULL || pdata->dcdc[id] == NULL) 534 return -ENODEV; 535 536 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL); 537 if (dcdc == NULL) { 538 dev_err(&pdev->dev, "Unable to allocate private data\n"); 539 return -ENOMEM; 540 } 541 542 dcdc->wm831x = wm831x; 543 544 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 545 if (res == NULL) { 546 dev_err(&pdev->dev, "No I/O resource\n"); 547 ret = -EINVAL; 548 goto err; 549 } 550 dcdc->base = res->start; 551 552 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); 553 dcdc->desc.name = dcdc->name; 554 dcdc->desc.id = id; 555 dcdc->desc.type = REGULATOR_VOLTAGE; 556 dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1; 557 dcdc->desc.ops = &wm831x_buckv_ops; 558 dcdc->desc.owner = THIS_MODULE; 559 560 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG); 561 if (ret < 0) { 562 dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret); 563 goto err; 564 } 565 dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK; 566 567 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL); 568 if (ret < 0) { 569 dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret); 570 goto err; 571 } 572 dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK; 573 574 if (pdata->dcdc[id]) 575 wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data); 576 577 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev, 578 pdata->dcdc[id], dcdc); 579 if (IS_ERR(dcdc->regulator)) { 580 ret = PTR_ERR(dcdc->regulator); 581 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", 582 id + 1, ret); 583 goto err; 584 } 585 586 irq = platform_get_irq_byname(pdev, "UV"); 587 ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq, 588 IRQF_TRIGGER_RISING, dcdc->name, dcdc); 589 if (ret != 0) { 590 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", 591 irq, ret); 592 goto err_regulator; 593 } 594 595 irq = platform_get_irq_byname(pdev, "HC"); 596 ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq, 597 IRQF_TRIGGER_RISING, dcdc->name, dcdc); 598 if (ret != 0) { 599 dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n", 600 irq, ret); 601 goto err_uv; 602 } 603 604 platform_set_drvdata(pdev, dcdc); 605 606 return 0; 607 608 err_uv: 609 free_irq(platform_get_irq_byname(pdev, "UV"), dcdc); 610 err_regulator: 611 regulator_unregister(dcdc->regulator); 612 err: 613 if (dcdc->dvs_gpio) 614 gpio_free(dcdc->dvs_gpio); 615 kfree(dcdc); 616 return ret; 617 } 618 619 static __devexit int wm831x_buckv_remove(struct platform_device *pdev) 620 { 621 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); 622 623 platform_set_drvdata(pdev, NULL); 624 625 free_irq(platform_get_irq_byname(pdev, "HC"), dcdc); 626 free_irq(platform_get_irq_byname(pdev, "UV"), dcdc); 627 regulator_unregister(dcdc->regulator); 628 if (dcdc->dvs_gpio) 629 gpio_free(dcdc->dvs_gpio); 630 kfree(dcdc); 631 632 return 0; 633 } 634 635 static struct platform_driver wm831x_buckv_driver = { 636 .probe = wm831x_buckv_probe, 637 .remove = __devexit_p(wm831x_buckv_remove), 638 .driver = { 639 .name = "wm831x-buckv", 640 .owner = THIS_MODULE, 641 }, 642 }; 643 644 /* 645 * BUCKP specifics 646 */ 647 648 static int wm831x_buckp_list_voltage(struct regulator_dev *rdev, 649 unsigned selector) 650 { 651 if (selector <= WM831X_BUCKP_MAX_SELECTOR) 652 return 850000 + (selector * 25000); 653 else 654 return -EINVAL; 655 } 656 657 static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg, 658 int min_uV, int max_uV, int *selector) 659 { 660 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 661 struct wm831x *wm831x = dcdc->wm831x; 662 u16 vsel; 663 664 if (min_uV <= 34000000) 665 vsel = (min_uV - 850000) / 25000; 666 else 667 return -EINVAL; 668 669 if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV) 670 return -EINVAL; 671 672 *selector = vsel; 673 674 return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel); 675 } 676 677 static int wm831x_buckp_set_voltage(struct regulator_dev *rdev, 678 int min_uV, int max_uV, 679 unsigned *selector) 680 { 681 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 682 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 683 684 return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV, 685 selector); 686 } 687 688 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, 689 int uV) 690 { 691 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 692 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; 693 unsigned selector; 694 695 return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV, &selector); 696 } 697 698 static int wm831x_buckp_get_voltage_sel(struct regulator_dev *rdev) 699 { 700 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 701 struct wm831x *wm831x = dcdc->wm831x; 702 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 703 int val; 704 705 val = wm831x_reg_read(wm831x, reg); 706 if (val < 0) 707 return val; 708 709 return val & WM831X_DC3_ON_VSEL_MASK; 710 } 711 712 static struct regulator_ops wm831x_buckp_ops = { 713 .set_voltage = wm831x_buckp_set_voltage, 714 .get_voltage_sel = wm831x_buckp_get_voltage_sel, 715 .list_voltage = wm831x_buckp_list_voltage, 716 .set_suspend_voltage = wm831x_buckp_set_suspend_voltage, 717 718 .is_enabled = wm831x_dcdc_is_enabled, 719 .enable = wm831x_dcdc_enable, 720 .disable = wm831x_dcdc_disable, 721 .get_status = wm831x_dcdc_get_status, 722 .get_mode = wm831x_dcdc_get_mode, 723 .set_mode = wm831x_dcdc_set_mode, 724 .set_suspend_mode = wm831x_dcdc_set_suspend_mode, 725 }; 726 727 static __devinit int wm831x_buckp_probe(struct platform_device *pdev) 728 { 729 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 730 struct wm831x_pdata *pdata = wm831x->dev->platform_data; 731 int id; 732 struct wm831x_dcdc *dcdc; 733 struct resource *res; 734 int ret, irq; 735 736 if (pdata && pdata->wm831x_num) 737 id = (pdata->wm831x_num * 10) + 1; 738 else 739 id = 0; 740 id = pdev->id - id; 741 742 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); 743 744 if (pdata == NULL || pdata->dcdc[id] == NULL) 745 return -ENODEV; 746 747 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL); 748 if (dcdc == NULL) { 749 dev_err(&pdev->dev, "Unable to allocate private data\n"); 750 return -ENOMEM; 751 } 752 753 dcdc->wm831x = wm831x; 754 755 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 756 if (res == NULL) { 757 dev_err(&pdev->dev, "No I/O resource\n"); 758 ret = -EINVAL; 759 goto err; 760 } 761 dcdc->base = res->start; 762 763 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); 764 dcdc->desc.name = dcdc->name; 765 dcdc->desc.id = id; 766 dcdc->desc.type = REGULATOR_VOLTAGE; 767 dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1; 768 dcdc->desc.ops = &wm831x_buckp_ops; 769 dcdc->desc.owner = THIS_MODULE; 770 771 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev, 772 pdata->dcdc[id], dcdc); 773 if (IS_ERR(dcdc->regulator)) { 774 ret = PTR_ERR(dcdc->regulator); 775 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", 776 id + 1, ret); 777 goto err; 778 } 779 780 irq = platform_get_irq_byname(pdev, "UV"); 781 ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq, 782 IRQF_TRIGGER_RISING, dcdc->name, dcdc); 783 if (ret != 0) { 784 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", 785 irq, ret); 786 goto err_regulator; 787 } 788 789 platform_set_drvdata(pdev, dcdc); 790 791 return 0; 792 793 err_regulator: 794 regulator_unregister(dcdc->regulator); 795 err: 796 kfree(dcdc); 797 return ret; 798 } 799 800 static __devexit int wm831x_buckp_remove(struct platform_device *pdev) 801 { 802 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); 803 804 platform_set_drvdata(pdev, NULL); 805 806 free_irq(platform_get_irq_byname(pdev, "UV"), dcdc); 807 regulator_unregister(dcdc->regulator); 808 kfree(dcdc); 809 810 return 0; 811 } 812 813 static struct platform_driver wm831x_buckp_driver = { 814 .probe = wm831x_buckp_probe, 815 .remove = __devexit_p(wm831x_buckp_remove), 816 .driver = { 817 .name = "wm831x-buckp", 818 .owner = THIS_MODULE, 819 }, 820 }; 821 822 /* 823 * DCDC boost convertors 824 */ 825 826 static int wm831x_boostp_get_status(struct regulator_dev *rdev) 827 { 828 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 829 struct wm831x *wm831x = dcdc->wm831x; 830 int ret; 831 832 /* First, check for errors */ 833 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS); 834 if (ret < 0) 835 return ret; 836 837 if (ret & (1 << rdev_get_id(rdev))) { 838 dev_dbg(wm831x->dev, "DCDC%d under voltage\n", 839 rdev_get_id(rdev) + 1); 840 return REGULATOR_STATUS_ERROR; 841 } 842 843 /* Is the regulator on? */ 844 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS); 845 if (ret < 0) 846 return ret; 847 if (ret & (1 << rdev_get_id(rdev))) 848 return REGULATOR_STATUS_ON; 849 else 850 return REGULATOR_STATUS_OFF; 851 } 852 853 static struct regulator_ops wm831x_boostp_ops = { 854 .get_status = wm831x_boostp_get_status, 855 856 .is_enabled = wm831x_dcdc_is_enabled, 857 .enable = wm831x_dcdc_enable, 858 .disable = wm831x_dcdc_disable, 859 }; 860 861 static __devinit int wm831x_boostp_probe(struct platform_device *pdev) 862 { 863 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 864 struct wm831x_pdata *pdata = wm831x->dev->platform_data; 865 int id = pdev->id % ARRAY_SIZE(pdata->dcdc); 866 struct wm831x_dcdc *dcdc; 867 struct resource *res; 868 int ret, irq; 869 870 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); 871 872 if (pdata == NULL || pdata->dcdc[id] == NULL) 873 return -ENODEV; 874 875 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL); 876 if (dcdc == NULL) { 877 dev_err(&pdev->dev, "Unable to allocate private data\n"); 878 return -ENOMEM; 879 } 880 881 dcdc->wm831x = wm831x; 882 883 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 884 if (res == NULL) { 885 dev_err(&pdev->dev, "No I/O resource\n"); 886 ret = -EINVAL; 887 goto err; 888 } 889 dcdc->base = res->start; 890 891 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); 892 dcdc->desc.name = dcdc->name; 893 dcdc->desc.id = id; 894 dcdc->desc.type = REGULATOR_VOLTAGE; 895 dcdc->desc.ops = &wm831x_boostp_ops; 896 dcdc->desc.owner = THIS_MODULE; 897 898 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev, 899 pdata->dcdc[id], dcdc); 900 if (IS_ERR(dcdc->regulator)) { 901 ret = PTR_ERR(dcdc->regulator); 902 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", 903 id + 1, ret); 904 goto err; 905 } 906 907 irq = platform_get_irq_byname(pdev, "UV"); 908 ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq, 909 IRQF_TRIGGER_RISING, dcdc->name, 910 dcdc); 911 if (ret != 0) { 912 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", 913 irq, ret); 914 goto err_regulator; 915 } 916 917 platform_set_drvdata(pdev, dcdc); 918 919 return 0; 920 921 err_regulator: 922 regulator_unregister(dcdc->regulator); 923 err: 924 kfree(dcdc); 925 return ret; 926 } 927 928 static __devexit int wm831x_boostp_remove(struct platform_device *pdev) 929 { 930 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); 931 932 platform_set_drvdata(pdev, NULL); 933 934 free_irq(platform_get_irq_byname(pdev, "UV"), dcdc); 935 regulator_unregister(dcdc->regulator); 936 kfree(dcdc); 937 938 return 0; 939 } 940 941 static struct platform_driver wm831x_boostp_driver = { 942 .probe = wm831x_boostp_probe, 943 .remove = __devexit_p(wm831x_boostp_remove), 944 .driver = { 945 .name = "wm831x-boostp", 946 .owner = THIS_MODULE, 947 }, 948 }; 949 950 /* 951 * External Power Enable 952 * 953 * These aren't actually DCDCs but look like them in hardware so share 954 * code. 955 */ 956 957 #define WM831X_EPE_BASE 6 958 959 static struct regulator_ops wm831x_epe_ops = { 960 .is_enabled = wm831x_dcdc_is_enabled, 961 .enable = wm831x_dcdc_enable, 962 .disable = wm831x_dcdc_disable, 963 .get_status = wm831x_dcdc_get_status, 964 }; 965 966 static __devinit int wm831x_epe_probe(struct platform_device *pdev) 967 { 968 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 969 struct wm831x_pdata *pdata = wm831x->dev->platform_data; 970 int id = pdev->id % ARRAY_SIZE(pdata->epe); 971 struct wm831x_dcdc *dcdc; 972 int ret; 973 974 dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1); 975 976 if (pdata == NULL || pdata->epe[id] == NULL) 977 return -ENODEV; 978 979 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL); 980 if (dcdc == NULL) { 981 dev_err(&pdev->dev, "Unable to allocate private data\n"); 982 return -ENOMEM; 983 } 984 985 dcdc->wm831x = wm831x; 986 987 /* For current parts this is correct; probably need to revisit 988 * in future. 989 */ 990 snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1); 991 dcdc->desc.name = dcdc->name; 992 dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */ 993 dcdc->desc.ops = &wm831x_epe_ops; 994 dcdc->desc.type = REGULATOR_VOLTAGE; 995 dcdc->desc.owner = THIS_MODULE; 996 997 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev, 998 pdata->epe[id], dcdc); 999 if (IS_ERR(dcdc->regulator)) { 1000 ret = PTR_ERR(dcdc->regulator); 1001 dev_err(wm831x->dev, "Failed to register EPE%d: %d\n", 1002 id + 1, ret); 1003 goto err; 1004 } 1005 1006 platform_set_drvdata(pdev, dcdc); 1007 1008 return 0; 1009 1010 err: 1011 kfree(dcdc); 1012 return ret; 1013 } 1014 1015 static __devexit int wm831x_epe_remove(struct platform_device *pdev) 1016 { 1017 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); 1018 1019 platform_set_drvdata(pdev, NULL); 1020 1021 regulator_unregister(dcdc->regulator); 1022 kfree(dcdc); 1023 1024 return 0; 1025 } 1026 1027 static struct platform_driver wm831x_epe_driver = { 1028 .probe = wm831x_epe_probe, 1029 .remove = __devexit_p(wm831x_epe_remove), 1030 .driver = { 1031 .name = "wm831x-epe", 1032 .owner = THIS_MODULE, 1033 }, 1034 }; 1035 1036 static int __init wm831x_dcdc_init(void) 1037 { 1038 int ret; 1039 ret = platform_driver_register(&wm831x_buckv_driver); 1040 if (ret != 0) 1041 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret); 1042 1043 ret = platform_driver_register(&wm831x_buckp_driver); 1044 if (ret != 0) 1045 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret); 1046 1047 ret = platform_driver_register(&wm831x_boostp_driver); 1048 if (ret != 0) 1049 pr_err("Failed to register WM831x BOOST driver: %d\n", ret); 1050 1051 ret = platform_driver_register(&wm831x_epe_driver); 1052 if (ret != 0) 1053 pr_err("Failed to register WM831x EPE driver: %d\n", ret); 1054 1055 return 0; 1056 } 1057 subsys_initcall(wm831x_dcdc_init); 1058 1059 static void __exit wm831x_dcdc_exit(void) 1060 { 1061 platform_driver_unregister(&wm831x_epe_driver); 1062 platform_driver_unregister(&wm831x_boostp_driver); 1063 platform_driver_unregister(&wm831x_buckp_driver); 1064 platform_driver_unregister(&wm831x_buckv_driver); 1065 } 1066 module_exit(wm831x_dcdc_exit); 1067 1068 /* Module information */ 1069 MODULE_AUTHOR("Mark Brown"); 1070 MODULE_DESCRIPTION("WM831x DC-DC convertor driver"); 1071 MODULE_LICENSE("GPL"); 1072 MODULE_ALIAS("platform:wm831x-buckv"); 1073 MODULE_ALIAS("platform:wm831x-buckp"); 1074 MODULE_ALIAS("platform:wm831x-epe"); 1075