Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * gpio-regulator.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2011 Heiko Stuebner <heiko@sntech.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * based on fixed.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright 2008 Wolfson Microelectronics PLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Copyright (c) 2009 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Roger Quadros <ext-roger.quadros@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * This is useful for systems with mixed controllable and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * non-controllable regulators, as well as for allowing testing on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * systems with no controllable regulators.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/regulator/machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/regulator/of_regulator.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/regulator/gpio-regulator.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) struct gpio_regulator_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct regulator_desc desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct gpio_desc **gpiods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	int nr_gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct gpio_regulator_state *states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	int nr_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static int gpio_regulator_get_value(struct regulator_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct gpio_regulator_data *data = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	for (ptr = 0; ptr < data->nr_states; ptr++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		if (data->states[ptr].gpios == data->state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			return data->states[ptr].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static int gpio_regulator_set_voltage(struct regulator_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 					int min_uV, int max_uV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 					unsigned *selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct gpio_regulator_data *data = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int ptr, target = 0, state, best_val = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	for (ptr = 0; ptr < data->nr_states; ptr++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (data->states[ptr].value < best_val &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		    data->states[ptr].value >= min_uV &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		    data->states[ptr].value <= max_uV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			target = data->states[ptr].gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			best_val = data->states[ptr].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			if (selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				*selector = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (best_val == INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	for (ptr = 0; ptr < data->nr_gpios; ptr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		state = (target & (1 << ptr)) >> ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		gpiod_set_value_cansleep(data->gpiods[ptr], state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	data->state = target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static int gpio_regulator_list_voltage(struct regulator_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				      unsigned selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct gpio_regulator_data *data = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (selector >= data->nr_states)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return data->states[selector].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 					int min_uA, int max_uA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct gpio_regulator_data *data = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int ptr, target = 0, state, best_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	for (ptr = 0; ptr < data->nr_states; ptr++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (data->states[ptr].value > best_val &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		    data->states[ptr].value >= min_uA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		    data->states[ptr].value <= max_uA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			target = data->states[ptr].gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			best_val = data->states[ptr].value;
^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) 	if (best_val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	for (ptr = 0; ptr < data->nr_gpios; ptr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		state = (target & (1 << ptr)) >> ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		gpiod_set_value_cansleep(data->gpiods[ptr], state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	data->state = target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static const struct regulator_ops gpio_regulator_voltage_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	.get_voltage = gpio_regulator_get_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.set_voltage = gpio_regulator_set_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	.list_voltage = gpio_regulator_list_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static struct gpio_regulator_config *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			     const struct regulator_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct gpio_regulator_config *config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	const char *regtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int proplen, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	int ngpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	config = devm_kzalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			sizeof(struct gpio_regulator_config),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (!config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	config->init_data = of_get_regulator_init_data(dev, np, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (!config->init_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	config->supply_name = config->init_data->constraints.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (config->init_data->constraints.boot_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		config->enabled_at_boot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * Do not use: undocumented device tree property.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 * This is kept around solely for device tree ABI stability.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (of_property_read_bool(np, "enable-at-boot"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		config->enabled_at_boot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	/* Fetch GPIO init levels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	ngpios = gpiod_count(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (ngpios > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		config->gflags = devm_kzalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 					      sizeof(enum gpiod_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 					      * ngpios,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 					      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		if (!config->gflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		for (i = 0; i < ngpios; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			ret = of_property_read_u32_index(np, "gpios-states", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 							 &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			/* Default to high per specification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				config->gflags[i] = GPIOD_OUT_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				config->gflags[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 					val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	config->ngpios = ngpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	/* Fetch states. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	proplen = of_property_count_u32_elems(np, "states");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (proplen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		dev_err(dev, "No 'states' property found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	config->states = devm_kcalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 				proplen / 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 				sizeof(struct gpio_regulator_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (!config->states)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	for (i = 0; i < proplen / 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		of_property_read_u32_index(np, "states", i * 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 					   &config->states[i].value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		of_property_read_u32_index(np, "states", i * 2 + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 					   &config->states[i].gpios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	config->nr_states = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	config->type = REGULATOR_VOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	ret = of_property_read_string(np, "regulator-type", &regtype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		if (!strncmp("voltage", regtype, 7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			config->type = REGULATOR_VOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		else if (!strncmp("current", regtype, 7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			config->type = REGULATOR_CURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			dev_warn(dev, "Unknown regulator-type '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 				 regtype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	return config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static const struct regulator_ops gpio_regulator_current_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	.get_current_limit = gpio_regulator_get_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	.set_current_limit = gpio_regulator_set_current_limit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static int gpio_regulator_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct gpio_regulator_config *config = dev_get_platdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct gpio_regulator_data *drvdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct regulator_config cfg = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct regulator_dev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	enum gpiod_flags gflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	int ptr, ret, state, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (drvdata == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		config = of_get_gpio_regulator_config(dev, np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 						      &drvdata->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		if (IS_ERR(config))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			return PTR_ERR(config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (drvdata->desc.name == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		dev_err(dev, "Failed to allocate supply name\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (!drvdata->gpiods)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	for (i = 0; i < config->ngpios; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		drvdata->gpiods[i] = devm_gpiod_get_index(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 							  NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 							  i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 							  config->gflags[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		if (IS_ERR(drvdata->gpiods[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			return PTR_ERR(drvdata->gpiods[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		/* This is good to know */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	drvdata->nr_gpios = config->ngpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	drvdata->states = devm_kmemdup(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				       config->states,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				       config->nr_states *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 				       sizeof(struct gpio_regulator_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (drvdata->states == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		dev_err(dev, "Failed to allocate state data\n");
^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) 	drvdata->nr_states = config->nr_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	drvdata->desc.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	drvdata->desc.enable_time = config->startup_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	/* handle regulator type*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	switch (config->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	case REGULATOR_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		drvdata->desc.type = REGULATOR_VOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		drvdata->desc.ops = &gpio_regulator_voltage_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		drvdata->desc.n_voltages = config->nr_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	case REGULATOR_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		drvdata->desc.type = REGULATOR_CURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		drvdata->desc.ops = &gpio_regulator_current_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		dev_err(dev, "No regulator type set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	/* build initial state from gpio init data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		if (config->gflags[ptr] == GPIOD_OUT_HIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			state |= (1 << ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	drvdata->state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	cfg.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	cfg.init_data = config->init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	cfg.driver_data = drvdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	cfg.of_node = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	 * The signal will be inverted by the GPIO core if flagged so in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	 * descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (config->enabled_at_boot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (IS_ERR(cfg.ena_gpiod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return PTR_ERR(cfg.ena_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	rdev = devm_regulator_register(dev, &drvdata->desc, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (IS_ERR(rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		ret = PTR_ERR(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		dev_err(dev, "Failed to register regulator: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	platform_set_drvdata(pdev, drvdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) #if defined(CONFIG_OF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static const struct of_device_id regulator_gpio_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	{ .compatible = "regulator-gpio", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) MODULE_DEVICE_TABLE(of, regulator_gpio_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static struct platform_driver gpio_regulator_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	.probe		= gpio_regulator_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		.name		= "gpio-regulator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		.of_match_table = of_match_ptr(regulator_gpio_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static int __init gpio_regulator_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return platform_driver_register(&gpio_regulator_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) subsys_initcall(gpio_regulator_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static void __exit gpio_regulator_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	platform_driver_unregister(&gpio_regulator_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) module_exit(gpio_regulator_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) MODULE_DESCRIPTION("gpio voltage regulator");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) MODULE_ALIAS("platform:gpio-regulator");