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)  * userspace-consumer.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2009 CompuLab, Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Mike Rapoport <mike@compulab.co.il>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Based of virtual consumer driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *   Copyright 2008 Wolfson Microelectronics PLC.
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/regulator/userspace-consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) struct userspace_consumer_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	int num_supplies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct regulator_bulk_data *supplies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static ssize_t reg_show_name(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 			  struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct userspace_consumer_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	return sprintf(buf, "%s\n", data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static ssize_t reg_show_state(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 			  struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct userspace_consumer_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (data->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return sprintf(buf, "enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return sprintf(buf, "disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static ssize_t reg_set_state(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct userspace_consumer_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int ret;
^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) 	 * sysfs_streq() doesn't need the \n's, but we add them so the strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 * will be shared with show_state(), above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	else if (sysfs_streq(buf, "disabled\n") || sysfs_streq(buf, "0"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		dev_err(dev, "Configuring invalid mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (enabled != data->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		if (enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			ret = regulator_bulk_enable(data->num_supplies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 						    data->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			ret = regulator_bulk_disable(data->num_supplies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 						     data->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			data->enabled = enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			dev_err(dev, "Failed to configure state: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static DEVICE_ATTR(name, 0444, reg_show_name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static DEVICE_ATTR(state, 0644, reg_show_state, reg_set_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static struct attribute *attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	&dev_attr_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	&dev_attr_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static const struct attribute_group attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	.attrs	= attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int regulator_userspace_consumer_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct regulator_userspace_consumer_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct userspace_consumer_data *drvdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	pdata = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	drvdata = devm_kzalloc(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			       sizeof(struct userspace_consumer_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (drvdata == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	drvdata->name = pdata->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	drvdata->num_supplies = pdata->num_supplies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	drvdata->supplies = pdata->supplies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	mutex_init(&drvdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	ret = devm_regulator_bulk_get(&pdev->dev, drvdata->num_supplies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				      drvdata->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		dev_err(&pdev->dev, "Failed to get supplies: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ret = sysfs_create_group(&pdev->dev.kobj, &attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (pdata->init_on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		ret = regulator_bulk_enable(drvdata->num_supplies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 					    drvdata->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				"Failed to set initial state: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			goto err_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		}
^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) 	drvdata->enabled = pdata->init_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	platform_set_drvdata(pdev, drvdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) err_enable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	sysfs_remove_group(&pdev->dev.kobj, &attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int regulator_userspace_consumer_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct userspace_consumer_data *data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	sysfs_remove_group(&pdev->dev.kobj, &attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (data->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		regulator_bulk_disable(data->num_supplies, data->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static struct platform_driver regulator_userspace_consumer_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	.probe		= regulator_userspace_consumer_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	.remove		= regulator_userspace_consumer_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		.name		= "reg-userspace-consumer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) module_platform_driver(regulator_userspace_consumer_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) MODULE_DESCRIPTION("Userspace consumer for voltage and current regulators");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) MODULE_LICENSE("GPL");