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