4 * Copyright 2009 CompuLab, Ltd.
6 * Author: Mike Rapoport <mike@compulab.co.il>
8 * Based of virtual consumer driver:
9 * Copyright 2008 Wolfson Microelectronics PLC.
10 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
19 #include <linux/err.h>
20 #include <linux/mutex.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/userspace-consumer.h>
24 #include <linux/slab.h>
26 struct userspace_consumer_data
{
33 struct regulator_bulk_data
*supplies
;
36 static ssize_t
reg_show_name(struct device
*dev
,
37 struct device_attribute
*attr
, char *buf
)
39 struct userspace_consumer_data
*data
= dev_get_drvdata(dev
);
41 return sprintf(buf
, "%s\n", data
->name
);
44 static ssize_t
reg_show_state(struct device
*dev
,
45 struct device_attribute
*attr
, char *buf
)
47 struct userspace_consumer_data
*data
= dev_get_drvdata(dev
);
50 return sprintf(buf
, "enabled\n");
52 return sprintf(buf
, "disabled\n");
55 static ssize_t
reg_set_state(struct device
*dev
, struct device_attribute
*attr
,
56 const char *buf
, size_t count
)
58 struct userspace_consumer_data
*data
= dev_get_drvdata(dev
);
63 * sysfs_streq() doesn't need the \n's, but we add them so the strings
64 * will be shared with show_state(), above.
66 if (sysfs_streq(buf
, "enabled\n") || sysfs_streq(buf
, "1"))
68 else if (sysfs_streq(buf
, "disabled\n") || sysfs_streq(buf
, "0"))
71 dev_err(dev
, "Configuring invalid mode\n");
75 mutex_lock(&data
->lock
);
76 if (enabled
!= data
->enabled
) {
78 ret
= regulator_bulk_enable(data
->num_supplies
,
81 ret
= regulator_bulk_disable(data
->num_supplies
,
85 data
->enabled
= enabled
;
87 dev_err(dev
, "Failed to configure state: %d\n", ret
);
89 mutex_unlock(&data
->lock
);
94 static DEVICE_ATTR(name
, 0444, reg_show_name
, NULL
);
95 static DEVICE_ATTR(state
, 0644, reg_show_state
, reg_set_state
);
97 static struct attribute
*attributes
[] = {
103 static const struct attribute_group attr_group
= {
107 static int regulator_userspace_consumer_probe(struct platform_device
*pdev
)
109 struct regulator_userspace_consumer_data
*pdata
;
110 struct userspace_consumer_data
*drvdata
;
113 pdata
= pdev
->dev
.platform_data
;
117 drvdata
= kzalloc(sizeof(struct userspace_consumer_data
), GFP_KERNEL
);
121 drvdata
->name
= pdata
->name
;
122 drvdata
->num_supplies
= pdata
->num_supplies
;
123 drvdata
->supplies
= pdata
->supplies
;
125 mutex_init(&drvdata
->lock
);
127 ret
= regulator_bulk_get(&pdev
->dev
, drvdata
->num_supplies
,
130 dev_err(&pdev
->dev
, "Failed to get supplies: %d\n", ret
);
131 goto err_alloc_supplies
;
134 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &attr_group
);
136 goto err_create_attrs
;
138 if (pdata
->init_on
) {
139 ret
= regulator_bulk_enable(drvdata
->num_supplies
,
143 "Failed to set initial state: %d\n", ret
);
148 drvdata
->enabled
= pdata
->init_on
;
149 platform_set_drvdata(pdev
, drvdata
);
154 sysfs_remove_group(&pdev
->dev
.kobj
, &attr_group
);
157 regulator_bulk_free(drvdata
->num_supplies
, drvdata
->supplies
);
164 static int regulator_userspace_consumer_remove(struct platform_device
*pdev
)
166 struct userspace_consumer_data
*data
= platform_get_drvdata(pdev
);
168 sysfs_remove_group(&pdev
->dev
.kobj
, &attr_group
);
171 regulator_bulk_disable(data
->num_supplies
, data
->supplies
);
173 regulator_bulk_free(data
->num_supplies
, data
->supplies
);
179 static struct platform_driver regulator_userspace_consumer_driver
= {
180 .probe
= regulator_userspace_consumer_probe
,
181 .remove
= regulator_userspace_consumer_remove
,
183 .name
= "reg-userspace-consumer",
188 static int __init
regulator_userspace_consumer_init(void)
190 return platform_driver_register(®ulator_userspace_consumer_driver
);
192 module_init(regulator_userspace_consumer_init
);
194 static void __exit
regulator_userspace_consumer_exit(void)
196 platform_driver_unregister(®ulator_userspace_consumer_driver
);
198 module_exit(regulator_userspace_consumer_exit
);
200 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
201 MODULE_DESCRIPTION("Userspace consumer for voltage and current regulators");
202 MODULE_LICENSE("GPL");