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