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