voltage/current regulator consumer interface
Quoted 2 lines · Mike Rapoport
It is the most simple and straight forward solution. However, I have several questions and I cannot answer them myself:
Some initial thoughts:
Quoted 1 line
Should the driver handle single or several supplies at once?
I'd expect that it should be able to cope with that - a lot of hardware takes multiple supplies.
Quoted 3 lines
If the driver handles several supplies how to define attributes per-supply? What attributes will be exposed? Would it be simple 'state' that can be
enabled or disabled? Or the entire set of micorvolts/microamps etc?
I'd expect that if you're getting into much more than a simple enable for the entire device it'd be getting to the point where it's worth considering writing an actual driver for the device. That said, it's a fairly natural extension to support more fine grained stuff if someone does actually end up wanting it.
Quoted 4 lines
- Extend regulator framework so that
regulator_consumer_supplyentities will gain their own representation in sysfs. Depending on machine constraints the supply attributes can be either read-only or writable. I haven't dedicated much thought to it, so a cannot state any pros and cons.
I'd expect making them writable to be very fragile if there is an active consumer driver - I'd expect reference counts to get lost with enable and disable and I'd expect drivers managing voltages and currents to get confused if their own configuration gets changed under them. The core already needs to arbitrate between multiple users so it seems to make sense to have userspace represented as other users are to the core.
Being able to read back the consumer status doesn't seem unreasonable, though.
Hi,
Recently there was a brief discussion on linux-arm-kernel list [1] about controlling voltage regulator state in cases when there is no consumer device for a particular regulator.
I have some thoughts but I'd like to know people opinion before I start implementation.
Problem
-------
The regulator framework API provides ability to control the state of voltage/current regulators from within the kernel. Usually the regulator supplies power to a device and device driver or some hooks to the platform code from the device driver manipulate the regulator state. However, the regulator framework does not have userspace ABI that allows regulator state modifications. Lack of this ABI prevents fine-grained control for power consumption of devices such as GPS trancievers and GSM modems. Moreover, in SoC based systems it is possible to switch on/off power to entire subsystem when it is not used.
Possible solutions
------------------
- Add a platform_driver similar to drivers/regulator/virtual.c that will expose writable attributes to sysfs thus allowing userspace to control regulator state. It is the most simple and straight forward solution. However, I have several questions and I cannot answer them myself:
Should the driver handle single or several supplies at once?
If the driver handles single supply isn't it a waste of memory to have aplatform_device per supply?
If the driver handles several supplies how to define attributes per-supply? What attributes will be exposed? Would it be simple 'state' that can be
enabled or disabled? Or the entire set of micorvolts/microamps etc?
- Extend regulator framework so that regulator_consumer_supply entities will gain their own representation in sysfs. Depending on machine constraints the supply attributes can be either read-only or writable. I haven't dedicated much thought to it, so a cannot state any pros and cons.
Any feedback is appreciated.
--
[1] http://lists.arm.linux.org.uk/lurker/message/20090413.125359.613f85ad.en.html
Signature · 2 lines
Sincerely yours, Mike.
Quoted 16 lines · Mark Brown
Quoted 2 lines · Mike Rapoport
It is the most simple and straight forward solution. However, I have several questions and I cannot answer them myself:
Some initial thoughts:
Quoted 1 line
Should the driver handle single or several supplies at once?
I'd expect that it should be able to cope with that - a lot of hardware takes multiple supplies.
Quoted 3 lines
If the driver handles several supplies how to define attributes per-supply? What attributes will be exposed? Would it be simple 'state' that can be
enabled or disabled? Or the entire set of micorvolts/microamps etc?
Quoted 5 lines
I'd expect that if you're getting into much more than a simple enable for the entire device it'd be getting to the point where it's worth considering writing an actual driver for the device. That said, it's a fairly natural extension to support more fine grained stuff if someone does actually end up wanting it.
I've managed to create some preliminary "line-consumer" driver. I don't really like the name, but I couldn't think of something better and "virtual" is already taken :)
Feedback is appreciated.
drivers/regulator/line-consumer.c new file +211 -0
@@ -0,0 +1,211 @@ +/* +* line-consumer.c +* +* Copyright 2009 CompuLab, Ltd.. +* +* Author: Mike Rapoport <[email protected]> +* +* Based of virtual consumer driver: +* Copyright 2008 Wolfson Microelectronics PLC. +* Author: Mark Brown <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of the +* License, or (at your option) any later version. +* +*/ + +#include <linux/err.h> +#include <linux/mutex.h> +#include <linux/platform_device.h> +#include <linux/regulator/consumer.h> +#include <linux/regulator/line-consumer.h> + +struct line_consumer_data { +const char *name; + +struct mutex lock; +bool enabled; + +int num_supplies; +struct regulator_bulk_data *supplies; +}; + +static ssize_t show_name (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct line_consumer_data *data =dev_get_drvdata (dev); + +return sprintf (buf,"%s\n" , data->name); +} + +static ssize_t show_state (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct line_consumer_data *data =dev_get_drvdata (dev); + +if (data->enabled) +return sprintf (buf,"enabled\n" ); + +return sprintf (buf,"disabled\n" ); +} + +static ssize_t set_state (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct line_consumer_data *data =dev_get_drvdata (dev); +bool enabled; +int ret; + +/* +* sysfs_streq() doesn't need the \n's, but we add them so the strings +* will be shared with show_state(), above. +*/ +if (sysfs_streq (buf,"enabled\n" ) ==0 ) + enabled = true; +else if (sysfs_streq (buf,"disabled\n" ) ==0 ) + enabled = false; +else { +dev_err (dev,"Configuring invalid mode\n" ); +return count; + } + +mutex_lock (&data->lock); +if (enabled != data->enabled) { +if (enabled) + ret =regulator_bulk_enable (data->num_supplies, + data->supplies); +else + ret =regulator_bulk_disable (data->num_supplies, + data->supplies); + +if (ret ==0 ) + data->enabled = enabled; +else +dev_err (dev,"Failed to configure state: %d\n" , ret); + } +mutex_unlock (&data->lock); + +return count; +} + +static DEVICE_ATTR(name, 0444, show_name, NULL); +static DEVICE_ATTR(state, 0666, show_state, set_state); + +static struct device_attribute *attributes[] = { + &dev_attr_name, + &dev_attr_state, +}; + +static int regulator_line_consumer_probe (struct platform_device *pdev) +{ +struct regulator_line_consumer_data *pdata; +struct line_consumer_data *drvdata; +int ret, i; + + pdata = pdev->dev.platform_data; +if (!pdata) +return -EINVAL ; + + drvdata =kzalloc (sizeof (struct line_consumer_data ),GFP_KERNEL ); +if (drvdata ==NULL ) +return -ENOMEM ; + + drvdata->name = pdata->name; + drvdata->num_supplies = pdata->num_supplies; + + drvdata->supplies =kzalloc (sizeof (struct regulator_bulk_data ) * + drvdata->num_supplies, +GFP_KERNEL ); +if (drvdata->supplies ==NULL ) { + ret = -ENOMEM ; + goto err_alloc_supplies; + } + +mutex_init (&drvdata->lock); + +for (i =0 ; i < drvdata->num_supplies; i++) + drvdata->supplies[i].supply = pdata->supplies[i].supply; + + ret =regulator_bulk_get (&pdev->dev, drvdata->num_supplies, + drvdata->supplies); +if (ret) { +dev_err (&pdev->dev,"Failed to get supplies: %d\n" , ret); + goto err_alloc_supplies; + } + +for (i =0 ; i <ARRAY_SIZE (attributes); i++) { + ret =device_create_file (&pdev->dev, attributes[i]); +if (ret !=0 ) + goto err_create_attrs; + } + +if (pdata->init_on) + ret =regulator_bulk_enable (drvdata->num_supplies, + drvdata->supplies); +else + ret =regulator_bulk_disable (drvdata->num_supplies, + drvdata->supplies); + + drvdata->enabled = pdata->init_on; + +if (ret) { +dev_err (&pdev->dev,"Failed to set initial state: %d\n" , ret); + goto err_create_attrs; + } + +platform_set_drvdata (pdev, drvdata); + +return 0 ; + +err_create_attrs: +for (i =0 ; i <ARRAY_SIZE (attributes); i++) +device_remove_file (&pdev->dev, attributes[i]); + +regulator_bulk_free (drvdata->num_supplies, drvdata->supplies); + +err_alloc_supplies: +kfree (drvdata); +return ret; +} + +static int regulator_line_consumer_remove (struct platform_device *pdev) +{ +struct line_consumer_data *drvdata =platform_get_drvdata (pdev); +int i; + +for (i =0 ; i <ARRAY_SIZE (attributes); i++) +device_remove_file (&pdev->dev, attributes[i]); + +regulator_bulk_free (drvdata->num_supplies, drvdata->supplies); +kfree (drvdata->supplies); +kfree (drvdata); + +return 0 ; +} + +static struct platform_driver regulator_line_consumer_driver = { + .probe = regulator_line_consumer_probe, + .remove = regulator_line_consumer_remove, + .driver = { + .name ="reg-line-consumer" , + }, +}; + + +static int __init regulator_line_consumer_init (void ) +{ +return platform_driver_register (®ulator_line_consumer_driver); +} +module_init (regulator_line_consumer_init); + +static void __exit regulator_line_consumer_exit(void) +{ +platform_driver_unregister (®ulator_line_consumer_driver); +} +module_exit (regulator_line_consumer_exit); + +MODULE_AUTHOR ("Mike Rapoport <[email protected]>" ); +MODULE_DESCRIPTION ("Line consumer for voltage and current regulators" ); +MODULE_LICENSE ("GPL" );
include/linux/regulator/line-consumer.h new file +25 -0
@@ -0,0 +1,25 @@ +#ifndef __REGULATOR_PLATFORM_CONSUMER_H_ +#define __REGULATOR_PLATFORM_CONSUMER_H_ + +struct regulator_consumer_supply ; + +/** +* struct regulator_line_consumer_data - line consumer +* initialisation data. +* +* @name: Name for the consumer line +* @num_supplies: Number of supplies feeding the line +* @supplies: Supplies configuration. +* @init_on: Set if the regulators supplying the line should be +* enabled during initialisation +*/ +struct regulator_line_consumer_data { +const char *name; + +int num_supplies; +struct regulator_consumer_supply *supplies; + +bool init_on; +}; + +#endif /* __REGULATOR_PLATFORM_CONSUMER_H_ */
Signature · 2 lines
Sincerely yours, Mike.
Quoted 3 lines · Mike Rapoport
I've managed to create some preliminary "line-consumer" driver. I don't really like the name, but I couldn't think of something better and "virtual" is already taken :)
"userspace" or "user"? The goal here is to let userspace applications be consumers.
Quoted 5 lines
drivers/regulator/line-consumer.c new file +5
-0+static ssize_t set_state (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct line_consumer_data *data =dev_get_drvdata (dev); +bool enabled;
drivers/regulator/line-consumer.c line 68
Quoted 10 lines
drivers/regulator/line-consumer.c new file +10
-0+int ret; + +/* +* sysfs_streq() doesn't need the \n's, but we add them so the strings +* will be shared with show_state(), above. +*/ +if (sysfs_streq (buf,"enabled\n" ) ==0 ) + enabled = true; +else if (sysfs_streq (buf,"disabled\n" ) ==0 ) + enabled = false;
I'd be inclined to also accept 1 and 0 here.
drivers/regulator/line-consumer.c line 94
Quoted 2 lines
drivers/regulator/line-consumer.c new file +2
-0+static DEVICE_ATTR(name, 0444, show_name, NULL); +static DEVICE_ATTR(state, 0666, show_state, set_state);
Permissions for set_state() should probably be tighter?
drivers/regulator/line-consumer.c line 142
Quoted 5 lines
drivers/regulator/line-consumer.c new file +5
-0+for (i =0 ; i <ARRAY_SIZE (attributes); i++) { + ret =device_create_file (&pdev->dev, attributes[i]); +if (ret !=0 ) + goto err_create_attrs; + }
device_add_attributes()?
drivers/regulator/line-consumer.c line 149
Quoted 6 lines
drivers/regulator/line-consumer.c new file +6
-0+if (pdata->init_on) + ret =regulator_bulk_enable (drvdata->num_supplies, + drvdata->supplies); +else + ret =regulator_bulk_disable (drvdata->num_supplies, + drvdata->supplies);
The disable case will lead to unbalanced enables and disables, loosing a reference to the supply. Just don't do anything if the supplies should not be enabled.
Quoted 4 lines
drivers/regulator/line-consumer.c new file +4
-0+static int regulator_line_consumer_remove (struct platform_device *pdev) +{ +struct line_consumer_data *drvdata =platform_get_drvdata (pdev); +int i;
drivers/regulator/line-consumer.c line 186
Quoted 10 lines
drivers/regulator/line-consumer.c new file +10
-0+ +for (i =0 ; i <ARRAY_SIZE (attributes); i++) +device_remove_file (&pdev->dev, attributes[i]); + +regulator_bulk_free (drvdata->num_supplies, drvdata->supplies); +kfree (drvdata->supplies); +kfree (drvdata); + +return 0 ; +}
Hrm. You probably want to disable the supplies if you weren't using them here.
Quoted 8 lines · Mark Brown
Quoted 3 lines · Mike Rapoport
I've managed to create some preliminary "line-consumer" driver. I don't really like the name, but I couldn't think of something better and "virtual" is already taken :)
"userspace" or "user"? The goal here is to let userspace applications be consumers.
I don't quite understand your intention here. Do you mean that it would bebetter to use a character device rather than sysfs?
Quoted 65 lines
Quoted 5 lines
drivers/regulator/line-consumer.c new file +5
-0+static ssize_t set_state (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct line_consumer_data *data =dev_get_drvdata (dev); +bool enabled;drivers/regulator/line-consumer.c line 68
Quoted 10 lines
drivers/regulator/line-consumer.c new file +10
-0+int ret; + +/* +* sysfs_streq() doesn't need the \n's, but we add them so the strings +* will be shared with show_state(), above. +*/ +if (sysfs_streq (buf,"enabled\n" ) ==0 ) + enabled = true; +else if (sysfs_streq (buf,"disabled\n" ) ==0 ) + enabled = false;I'd be inclined to also accept 1 and 0 here.
drivers/regulator/line-consumer.c line 94
Quoted 2 lines
drivers/regulator/line-consumer.c new file +2
-0+static DEVICE_ATTR(name, 0444, show_name, NULL); +static DEVICE_ATTR(state, 0666, show_state, set_state);Permissions for
set_state()should probably be tighter?drivers/regulator/line-consumer.c line 142
Quoted 5 lines
drivers/regulator/line-consumer.c new file +5
-0+for (i =0 ; i <ARRAY_SIZE (attributes); i++) { + ret =device_create_file (&pdev->dev, attributes[i]); +if (ret !=0 ) + goto err_create_attrs; + }
device_add_attributes()?drivers/regulator/line-consumer.c line 149
Quoted 6 lines
drivers/regulator/line-consumer.c new file +6
-0+if (pdata->init_on) + ret =regulator_bulk_enable (drvdata->num_supplies, + drvdata->supplies); +else + ret =regulator_bulk_disable (drvdata->num_supplies, + drvdata->supplies);The disable case will lead to unbalanced enables and disables, loosing a reference to the supply. Just don't do anything if the supplies should not be enabled.
Quoted 4 lines
drivers/regulator/line-consumer.c new file +4
-0+static int regulator_line_consumer_remove (struct platform_device *pdev) +{ +struct line_consumer_data *drvdata =platform_get_drvdata (pdev); +int i;drivers/regulator/line-consumer.c line 186
Quoted 10 lines
drivers/regulator/line-consumer.c new file +10
-0+ +for (i =0 ; i <ARRAY_SIZE (attributes); i++) +device_remove_file (&pdev->dev, attributes[i]); + +regulator_bulk_free (drvdata->num_supplies, drvdata->supplies); +kfree (drvdata->supplies); +kfree (drvdata); + +return 0 ; +}Hrm. You probably want to disable the supplies if you weren't using them here.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 2 lines
Sincerely yours, Mike.
Quoted 7 lines · Mike Rapoport
Quoted 5 lines · Mark Brown
Quoted 3 lines · Mike Rapoport
I've managed to create some preliminary "line-consumer" driver. I don't really like the name, but I couldn't think of something better and "virtual" is already taken :)
Quoted 5 lines
Quoted 2 lines
"userspace" or "user"? The goal here is to let userspace applications be consumers.
I don't quite understand your intention here. Do you mean that it would bebetter to use a character device rather than sysfs?
I'm talking about the name.
Quoted 8 lines · Mark Brown
Quoted 6 lines · Mike Rapoport
Quoted 5 lines · Mark Brown
Quoted 3 lines · Mike Rapoport
I've managed to create some preliminary "line-consumer" driver. I don't really like the name, but I couldn't think of something better and "virtual" is already taken :)
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
"userspace" or "user"? The goal here is to let userspace applications be consumers.
I don't quite understand your intention here. Do you mean that it would bebetter to use a character device rather than sysfs?
I'm talking about the name.
As I said, I could not invent something better. Any idea?
Quoted 6 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 2 lines
Sincerely yours, Mike.
Quoted 8 lines · Mike Rapoport
Quoted 4 lines · Mark Brown
Quoted 2 lines
Quoted 2 lines
"userspace" or "user"? The goal here is to let userspace applications be consumers.
I'm talking about the name.
As I said, I could not invent something better. Any idea?
Those are my ideas :)
Quoted 7 lines · Mark Brown
Quoted 6 lines · Mike Rapoport
Quoted 4 lines · Mark Brown
Quoted 2 lines
Quoted 2 lines
"userspace" or "user"? The goal here is to let userspace applications be consumers.
I'm talking about the name.
What about "line-supply"?
Quoted 4 lines
Quoted 1 line
As I said, I could not invent something better. Any idea?
Those are my ideas :)
Signature · 2 lines
Sincerely yours, Mike.
Quoted 1 line · Mike Rapoport
What about "line-supply"?
In power management line normally refers to the core, unregulated power bus for the system and a line supply would normally be something like a wall wart that supplies this directly when it's connected.