^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Regulator driver for PWM Regulators
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2014 - STMicroelectronics Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Lee Jones <lee.jones@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/regulator/machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/regulator/of_regulator.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pwm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct pwm_continuous_reg_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int min_uV_dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int max_uV_dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned int dutycycle_unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct pwm_regulator_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* Shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct pwm_device *pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* Voltage table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct pwm_voltages *duty_cycle_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Continuous mode info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct pwm_continuous_reg_data continuous;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* regulator descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct regulator_desc desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Enable GPIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct gpio_desc *enb_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* Init voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int init_uv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct pwm_voltages {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned int uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned int dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int req_min_uV, int req_max_uV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned int *selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Voltage table call-backs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static void pwm_regulator_init_state(struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct pwm_state pwm_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned int dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pwm_get_state(drvdata->pwm, &pwm_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) for (i = 0; i < rdev->desc->n_voltages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) drvdata->state = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (drvdata->state < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) pwm_regulator_init_state(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return drvdata->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unsigned selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct pwm_state pstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pwm_init_state(drvdata->pwm, &pstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) pwm_set_relative_duty_cycle(&pstate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) drvdata->duty_cycle_table[selector].dutycycle, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ret = pwm_apply_state(drvdata->pwm, &pstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) drvdata->state = selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) unsigned selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (selector >= rdev->desc->n_voltages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return drvdata->duty_cycle_table[selector].uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int pwm_regulator_enable(struct regulator_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (drvdata->init_uv && !pwm_get_duty_cycle(drvdata->pwm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pwm_regulator_set_voltage(dev, drvdata->init_uv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) drvdata->init_uv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return pwm_enable(drvdata->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int pwm_regulator_disable(struct regulator_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) pwm_disable(drvdata->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static int pwm_regulator_is_enabled(struct regulator_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return pwm_is_enabled(drvdata->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int min_uV = rdev->constraints->min_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int max_uV = rdev->constraints->max_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int diff_uV = max_uV - min_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct pwm_state pstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) unsigned int diff_duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) unsigned int voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) pwm_get_state(drvdata->pwm, &pstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * The dutycycle for min_uV might be greater than the one for max_uV.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * This is happening when the user needs an inversed polarity, but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * PWM device does not support inversing it in hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (max_uV_duty < min_uV_duty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) voltage = min_uV_duty - voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) diff_duty = min_uV_duty - max_uV_duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) voltage = voltage - min_uV_duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) diff_duty = max_uV_duty - min_uV_duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return voltage + min_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int req_min_uV, int req_max_uV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) unsigned int *selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int min_uV = rdev->constraints->min_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int max_uV = rdev->constraints->max_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int diff_uV = max_uV - min_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct pwm_state pstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned int diff_duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) unsigned int dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) pwm_init_state(drvdata->pwm, &pstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * The dutycycle for min_uV might be greater than the one for max_uV.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * This is happening when the user needs an inversed polarity, but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * PWM device does not support inversing it in hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (max_uV_duty < min_uV_duty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) diff_duty = min_uV_duty - max_uV_duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) diff_duty = max_uV_duty - min_uV_duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) diff_duty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) diff_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (max_uV_duty < min_uV_duty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) dutycycle = min_uV_duty - dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dutycycle = min_uV_duty + dutycycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) ret = pwm_apply_state(drvdata->pwm, &pstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static const struct regulator_ops pwm_regulator_voltage_table_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) .set_voltage_sel = pwm_regulator_set_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) .get_voltage_sel = pwm_regulator_get_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) .list_voltage = pwm_regulator_list_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) .map_voltage = regulator_map_voltage_iterate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) .enable = pwm_regulator_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) .disable = pwm_regulator_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) .is_enabled = pwm_regulator_is_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static const struct regulator_ops pwm_regulator_voltage_continuous_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) .get_voltage = pwm_regulator_get_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .set_voltage = pwm_regulator_set_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .enable = pwm_regulator_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) .disable = pwm_regulator_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) .is_enabled = pwm_regulator_is_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static const struct regulator_desc pwm_regulator_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) .name = "pwm-regulator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) .type = REGULATOR_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) .supply_name = "pwm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int pwm_regulator_init_table(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct pwm_regulator_data *drvdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct device_node *np = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct pwm_voltages *duty_cycle_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) unsigned int length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) of_find_property(np, "voltage-table", &length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if ((length < sizeof(*duty_cycle_table)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) (length % sizeof(*duty_cycle_table))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (!duty_cycle_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ret = of_property_read_u32_array(np, "voltage-table",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) (u32 *)duty_cycle_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) length / sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) drvdata->state = -ENOTRECOVERABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) drvdata->duty_cycle_table = duty_cycle_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) drvdata->desc.ops = &pwm_regulator_voltage_table_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int pwm_regulator_init_continuous(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct pwm_regulator_data *drvdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) u32 dutycycle_range[2] = { 0, 100 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) u32 dutycycle_unit = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) drvdata->desc.continuous_voltage_range = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) of_property_read_u32_array(pdev->dev.of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) "pwm-dutycycle-range",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) dutycycle_range, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) &dutycycle_unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (dutycycle_range[0] > dutycycle_unit ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) dutycycle_range[1] > dutycycle_unit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) drvdata->continuous.dutycycle_unit = dutycycle_unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int pwm_regulator_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) const struct regulator_init_data *init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct pwm_regulator_data *drvdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct regulator_dev *regulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct regulator_config config = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct device_node *np = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) enum gpiod_flags gpio_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) u32 init_uv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (!np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) dev_err(&pdev->dev, "Device Tree node missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!drvdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (of_find_property(np, "voltage-table", NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) ret = pwm_regulator_init_table(pdev, drvdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ret = pwm_regulator_init_continuous(pdev, drvdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (!of_property_read_u32(np, "regulator-init-microvolt", &init_uv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) drvdata->init_uv = init_uv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) init_data = of_get_regulator_init_data(&pdev->dev, np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) &drvdata->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (!init_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) config.of_node = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) config.dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) config.driver_data = drvdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) config.init_data = init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (IS_ERR(drvdata->pwm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ret = PTR_ERR(drvdata->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (ret == -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) dev_dbg(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) "Failed to get PWM, deferring probe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (init_data->constraints.boot_on || init_data->constraints.always_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) gpio_flags = GPIOD_OUT_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) gpio_flags = GPIOD_OUT_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) gpio_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (IS_ERR(drvdata->enb_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ret = PTR_ERR(drvdata->enb_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) ret = pwm_adjust_config(drvdata->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) regulator = devm_regulator_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) &drvdata->desc, &config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (IS_ERR(regulator)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ret = PTR_ERR(regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) drvdata->desc.name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static const struct of_device_id __maybe_unused pwm_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) { .compatible = "pwm-regulator" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) MODULE_DEVICE_TABLE(of, pwm_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static struct platform_driver pwm_regulator_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) .name = "pwm-regulator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) .of_match_table = of_match_ptr(pwm_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .probe = pwm_regulator_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) module_platform_driver(pwm_regulator_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) MODULE_DESCRIPTION("PWM Regulator Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) MODULE_ALIAS("platform:pwm-regulator");