| .. | .. |
|---|
| 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 | | - */ |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
|---|
| 2 | +// |
|---|
| 3 | +// wm831x-dcdc.c -- DC-DC buck converter driver for the WM831x series |
|---|
| 4 | +// |
|---|
| 5 | +// Copyright 2009 Wolfson Microelectronics PLC. |
|---|
| 6 | +// |
|---|
| 7 | +// Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
|---|
| 13 | 8 | |
|---|
| 14 | 9 | #include <linux/module.h> |
|---|
| 15 | 10 | #include <linux/moduleparam.h> |
|---|
| .. | .. |
|---|
| 20 | 15 | #include <linux/platform_device.h> |
|---|
| 21 | 16 | #include <linux/regulator/driver.h> |
|---|
| 22 | 17 | #include <linux/regulator/machine.h> |
|---|
| 23 | | -#include <linux/gpio.h> |
|---|
| 18 | +#include <linux/gpio/consumer.h> |
|---|
| 24 | 19 | #include <linux/slab.h> |
|---|
| 25 | 20 | |
|---|
| 26 | 21 | #include <linux/mfd/wm831x/core.h> |
|---|
| .. | .. |
|---|
| 55 | 50 | int base; |
|---|
| 56 | 51 | struct wm831x *wm831x; |
|---|
| 57 | 52 | struct regulator_dev *regulator; |
|---|
| 58 | | - int dvs_gpio; |
|---|
| 53 | + struct gpio_desc *dvs_gpiod; |
|---|
| 59 | 54 | int dvs_gpio_state; |
|---|
| 60 | 55 | int on_vsel; |
|---|
| 61 | 56 | int dvs_vsel; |
|---|
| .. | .. |
|---|
| 205 | 200 | * BUCKV specifics |
|---|
| 206 | 201 | */ |
|---|
| 207 | 202 | |
|---|
| 208 | | -static int wm831x_buckv_list_voltage(struct regulator_dev *rdev, |
|---|
| 209 | | - unsigned selector) |
|---|
| 210 | | -{ |
|---|
| 211 | | - if (selector <= 0x8) |
|---|
| 212 | | - return 600000; |
|---|
| 213 | | - if (selector <= WM831X_BUCKV_MAX_SELECTOR) |
|---|
| 214 | | - return 600000 + ((selector - 0x8) * 12500); |
|---|
| 215 | | - return -EINVAL; |
|---|
| 216 | | -} |
|---|
| 217 | | - |
|---|
| 218 | | -static int wm831x_buckv_map_voltage(struct regulator_dev *rdev, |
|---|
| 219 | | - int min_uV, int max_uV) |
|---|
| 220 | | -{ |
|---|
| 221 | | - u16 vsel; |
|---|
| 222 | | - |
|---|
| 223 | | - if (min_uV < 600000) |
|---|
| 224 | | - vsel = 0; |
|---|
| 225 | | - else if (min_uV <= 1800000) |
|---|
| 226 | | - vsel = DIV_ROUND_UP(min_uV - 600000, 12500) + 8; |
|---|
| 227 | | - else |
|---|
| 228 | | - return -EINVAL; |
|---|
| 229 | | - |
|---|
| 230 | | - if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV) |
|---|
| 231 | | - return -EINVAL; |
|---|
| 232 | | - |
|---|
| 233 | | - return vsel; |
|---|
| 234 | | -} |
|---|
| 203 | +static const struct linear_range wm831x_buckv_ranges[] = { |
|---|
| 204 | + REGULATOR_LINEAR_RANGE(600000, 0, 0x7, 0), |
|---|
| 205 | + REGULATOR_LINEAR_RANGE(600000, 0x8, 0x68, 12500), |
|---|
| 206 | +}; |
|---|
| 235 | 207 | |
|---|
| 236 | 208 | static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state) |
|---|
| 237 | 209 | { |
|---|
| .. | .. |
|---|
| 241 | 213 | return 0; |
|---|
| 242 | 214 | |
|---|
| 243 | 215 | dcdc->dvs_gpio_state = state; |
|---|
| 244 | | - gpio_set_value(dcdc->dvs_gpio, state); |
|---|
| 216 | + gpiod_set_value(dcdc->dvs_gpiod, state); |
|---|
| 245 | 217 | |
|---|
| 246 | 218 | /* Should wait for DVS state change to be asserted if we have |
|---|
| 247 | 219 | * a GPIO for it, for now assume the device is configured |
|---|
| .. | .. |
|---|
| 261 | 233 | int ret; |
|---|
| 262 | 234 | |
|---|
| 263 | 235 | /* If this value is already set then do a GPIO update if we can */ |
|---|
| 264 | | - if (dcdc->dvs_gpio && dcdc->on_vsel == vsel) |
|---|
| 236 | + if (dcdc->dvs_gpiod && dcdc->on_vsel == vsel) |
|---|
| 265 | 237 | return wm831x_buckv_set_dvs(rdev, 0); |
|---|
| 266 | 238 | |
|---|
| 267 | | - if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel) |
|---|
| 239 | + if (dcdc->dvs_gpiod && dcdc->dvs_vsel == vsel) |
|---|
| 268 | 240 | return wm831x_buckv_set_dvs(rdev, 1); |
|---|
| 269 | 241 | |
|---|
| 270 | 242 | /* Always set the ON status to the minimum voltage */ |
|---|
| .. | .. |
|---|
| 273 | 245 | return ret; |
|---|
| 274 | 246 | dcdc->on_vsel = vsel; |
|---|
| 275 | 247 | |
|---|
| 276 | | - if (!dcdc->dvs_gpio) |
|---|
| 248 | + if (!dcdc->dvs_gpiod) |
|---|
| 277 | 249 | return ret; |
|---|
| 278 | 250 | |
|---|
| 279 | 251 | /* Kick the voltage transition now */ |
|---|
| .. | .. |
|---|
| 309 | 281 | u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; |
|---|
| 310 | 282 | int vsel; |
|---|
| 311 | 283 | |
|---|
| 312 | | - vsel = wm831x_buckv_map_voltage(rdev, uV, uV); |
|---|
| 284 | + vsel = regulator_map_voltage_linear_range(rdev, uV, uV); |
|---|
| 313 | 285 | if (vsel < 0) |
|---|
| 314 | 286 | return vsel; |
|---|
| 315 | 287 | |
|---|
| .. | .. |
|---|
| 320 | 292 | { |
|---|
| 321 | 293 | struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
|---|
| 322 | 294 | |
|---|
| 323 | | - if (dcdc->dvs_gpio && dcdc->dvs_gpio_state) |
|---|
| 295 | + if (dcdc->dvs_gpiod && dcdc->dvs_gpio_state) |
|---|
| 324 | 296 | return dcdc->dvs_vsel; |
|---|
| 325 | 297 | else |
|---|
| 326 | 298 | return dcdc->on_vsel; |
|---|
| .. | .. |
|---|
| 331 | 303 | 125000, 250000, 375000, 500000, 625000, 750000, 875000, 1000000 |
|---|
| 332 | 304 | }; |
|---|
| 333 | 305 | |
|---|
| 334 | | -static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev, |
|---|
| 335 | | - int min_uA, int max_uA) |
|---|
| 336 | | -{ |
|---|
| 337 | | - struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
|---|
| 338 | | - struct wm831x *wm831x = dcdc->wm831x; |
|---|
| 339 | | - u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; |
|---|
| 340 | | - int i; |
|---|
| 341 | | - |
|---|
| 342 | | - for (i = ARRAY_SIZE(wm831x_dcdc_ilim) - 1; i >= 0; i--) { |
|---|
| 343 | | - if ((min_uA <= wm831x_dcdc_ilim[i]) && |
|---|
| 344 | | - (wm831x_dcdc_ilim[i] <= max_uA)) |
|---|
| 345 | | - return wm831x_set_bits(wm831x, reg, |
|---|
| 346 | | - WM831X_DC1_HC_THR_MASK, |
|---|
| 347 | | - i << WM831X_DC1_HC_THR_SHIFT); |
|---|
| 348 | | - } |
|---|
| 349 | | - |
|---|
| 350 | | - return -EINVAL; |
|---|
| 351 | | -} |
|---|
| 352 | | - |
|---|
| 353 | | -static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev) |
|---|
| 354 | | -{ |
|---|
| 355 | | - struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
|---|
| 356 | | - struct wm831x *wm831x = dcdc->wm831x; |
|---|
| 357 | | - u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; |
|---|
| 358 | | - int val; |
|---|
| 359 | | - |
|---|
| 360 | | - val = wm831x_reg_read(wm831x, reg); |
|---|
| 361 | | - if (val < 0) |
|---|
| 362 | | - return val; |
|---|
| 363 | | - |
|---|
| 364 | | - val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT; |
|---|
| 365 | | - return wm831x_dcdc_ilim[val]; |
|---|
| 366 | | -} |
|---|
| 367 | | - |
|---|
| 368 | 306 | static const struct regulator_ops wm831x_buckv_ops = { |
|---|
| 369 | 307 | .set_voltage_sel = wm831x_buckv_set_voltage_sel, |
|---|
| 370 | 308 | .get_voltage_sel = wm831x_buckv_get_voltage_sel, |
|---|
| 371 | | - .list_voltage = wm831x_buckv_list_voltage, |
|---|
| 372 | | - .map_voltage = wm831x_buckv_map_voltage, |
|---|
| 309 | + .list_voltage = regulator_list_voltage_linear_range, |
|---|
| 310 | + .map_voltage = regulator_map_voltage_linear_range, |
|---|
| 373 | 311 | .set_suspend_voltage = wm831x_buckv_set_suspend_voltage, |
|---|
| 374 | | - .set_current_limit = wm831x_buckv_set_current_limit, |
|---|
| 375 | | - .get_current_limit = wm831x_buckv_get_current_limit, |
|---|
| 312 | + .set_current_limit = regulator_set_current_limit_regmap, |
|---|
| 313 | + .get_current_limit = regulator_get_current_limit_regmap, |
|---|
| 376 | 314 | |
|---|
| 377 | 315 | .is_enabled = regulator_is_enabled_regmap, |
|---|
| 378 | 316 | .enable = regulator_enable_regmap, |
|---|
| .. | .. |
|---|
| 395 | 333 | int ret; |
|---|
| 396 | 334 | u16 ctrl; |
|---|
| 397 | 335 | |
|---|
| 398 | | - if (!pdata || !pdata->dvs_gpio) |
|---|
| 336 | + if (!pdata) |
|---|
| 399 | 337 | return; |
|---|
| 400 | 338 | |
|---|
| 401 | 339 | /* gpiolib won't let us read the GPIO status so pick the higher |
|---|
| .. | .. |
|---|
| 403 | 341 | */ |
|---|
| 404 | 342 | dcdc->dvs_gpio_state = pdata->dvs_init_state; |
|---|
| 405 | 343 | |
|---|
| 406 | | - ret = devm_gpio_request_one(&pdev->dev, pdata->dvs_gpio, |
|---|
| 407 | | - dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0, |
|---|
| 408 | | - "DCDC DVS"); |
|---|
| 409 | | - if (ret < 0) { |
|---|
| 410 | | - dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n", |
|---|
| 411 | | - dcdc->name, ret); |
|---|
| 344 | + dcdc->dvs_gpiod = devm_gpiod_get(&pdev->dev, "dvs", |
|---|
| 345 | + dcdc->dvs_gpio_state ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW); |
|---|
| 346 | + if (IS_ERR(dcdc->dvs_gpiod)) { |
|---|
| 347 | + dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %ld\n", |
|---|
| 348 | + dcdc->name, PTR_ERR(dcdc->dvs_gpiod)); |
|---|
| 412 | 349 | return; |
|---|
| 413 | 350 | } |
|---|
| 414 | | - |
|---|
| 415 | | - dcdc->dvs_gpio = pdata->dvs_gpio; |
|---|
| 416 | 351 | |
|---|
| 417 | 352 | switch (pdata->dvs_control_src) { |
|---|
| 418 | 353 | case 1: |
|---|
| .. | .. |
|---|
| 492 | 427 | dcdc->desc.id = id; |
|---|
| 493 | 428 | dcdc->desc.type = REGULATOR_VOLTAGE; |
|---|
| 494 | 429 | dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1; |
|---|
| 430 | + dcdc->desc.linear_ranges = wm831x_buckv_ranges; |
|---|
| 431 | + dcdc->desc.n_linear_ranges = ARRAY_SIZE(wm831x_buckv_ranges); |
|---|
| 495 | 432 | dcdc->desc.ops = &wm831x_buckv_ops; |
|---|
| 496 | 433 | dcdc->desc.owner = THIS_MODULE; |
|---|
| 497 | 434 | dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; |
|---|
| 498 | 435 | dcdc->desc.enable_mask = 1 << id; |
|---|
| 436 | + dcdc->desc.csel_reg = dcdc->base + WM831X_DCDC_CONTROL_2; |
|---|
| 437 | + dcdc->desc.csel_mask = WM831X_DC1_HC_THR_MASK; |
|---|
| 438 | + dcdc->desc.n_current_limits = ARRAY_SIZE(wm831x_dcdc_ilim); |
|---|
| 439 | + dcdc->desc.curr_table = wm831x_dcdc_ilim; |
|---|
| 499 | 440 | |
|---|
| 500 | 441 | ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG); |
|---|
| 501 | 442 | if (ret < 0) { |
|---|