xref: /OK3568_Linux_fs/kernel/drivers/regulator/fixed-helper.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/slab.h>
3*4882a593Smuzhiyun #include <linux/string.h>
4*4882a593Smuzhiyun #include <linux/platform_device.h>
5*4882a593Smuzhiyun #include <linux/regulator/machine.h>
6*4882a593Smuzhiyun #include <linux/regulator/fixed.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun struct fixed_regulator_data {
9*4882a593Smuzhiyun 	struct fixed_voltage_config cfg;
10*4882a593Smuzhiyun 	struct regulator_init_data init_data;
11*4882a593Smuzhiyun 	struct platform_device pdev;
12*4882a593Smuzhiyun };
13*4882a593Smuzhiyun 
regulator_fixed_release(struct device * dev)14*4882a593Smuzhiyun static void regulator_fixed_release(struct device *dev)
15*4882a593Smuzhiyun {
16*4882a593Smuzhiyun 	struct fixed_regulator_data *data = container_of(dev,
17*4882a593Smuzhiyun 			struct fixed_regulator_data, pdev.dev);
18*4882a593Smuzhiyun 	kfree(data->cfg.supply_name);
19*4882a593Smuzhiyun 	kfree(data);
20*4882a593Smuzhiyun }
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /**
23*4882a593Smuzhiyun  * regulator_register_fixed_name - register a no-op fixed regulator
24*4882a593Smuzhiyun  * @id: platform device id
25*4882a593Smuzhiyun  * @name: name to be used for the regulator
26*4882a593Smuzhiyun  * @supplies: consumers for this regulator
27*4882a593Smuzhiyun  * @num_supplies: number of consumers
28*4882a593Smuzhiyun  * @uv: voltage in microvolts
29*4882a593Smuzhiyun  */
regulator_register_always_on(int id,const char * name,struct regulator_consumer_supply * supplies,int num_supplies,int uv)30*4882a593Smuzhiyun struct platform_device *regulator_register_always_on(int id, const char *name,
31*4882a593Smuzhiyun 	struct regulator_consumer_supply *supplies, int num_supplies, int uv)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	struct fixed_regulator_data *data;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	data = kzalloc(sizeof(*data), GFP_KERNEL);
36*4882a593Smuzhiyun 	if (!data)
37*4882a593Smuzhiyun 		return NULL;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	data->cfg.supply_name = kstrdup(name, GFP_KERNEL);
40*4882a593Smuzhiyun 	if (!data->cfg.supply_name) {
41*4882a593Smuzhiyun 		kfree(data);
42*4882a593Smuzhiyun 		return NULL;
43*4882a593Smuzhiyun 	}
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	data->cfg.microvolts = uv;
46*4882a593Smuzhiyun 	data->cfg.enabled_at_boot = 1;
47*4882a593Smuzhiyun 	data->cfg.init_data = &data->init_data;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	data->init_data.constraints.always_on = 1;
50*4882a593Smuzhiyun 	data->init_data.consumer_supplies = supplies;
51*4882a593Smuzhiyun 	data->init_data.num_consumer_supplies = num_supplies;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	data->pdev.name = "reg-fixed-voltage";
54*4882a593Smuzhiyun 	data->pdev.id = id;
55*4882a593Smuzhiyun 	data->pdev.dev.platform_data = &data->cfg;
56*4882a593Smuzhiyun 	data->pdev.dev.release = regulator_fixed_release;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	platform_device_register(&data->pdev);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	return &data->pdev;
61*4882a593Smuzhiyun }
62