269 lines
6.3 KiB
C
269 lines
6.3 KiB
C
/*
|
|
* drivers/power/axp/axp-virtual.c
|
|
* (C) Copyright 2010-2016
|
|
* Allwinner Technology Co., Ltd. <www.allwinnertech.com>
|
|
* Pannan <pannan@allwinnertech.com>
|
|
*
|
|
* virtual driver of axp
|
|
*
|
|
* 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/regulator/driver.h>
|
|
#include <linux/platform_device.h>
|
|
#include "axp-virtual.h"
|
|
|
|
static void update_voltage_constraints(struct virtual_consumer_data *data)
|
|
{
|
|
int ret = 0;
|
|
struct regulator *regu;
|
|
|
|
regu = regulator_get(NULL, data->regu_name);
|
|
if (IS_ERR(regu)) {
|
|
pr_err("%s: regulator get %s failed\n",
|
|
__func__, data->regu_name);
|
|
return;
|
|
}
|
|
|
|
if (data->min_uv && data->max_uv && data->min_uv <= data->max_uv) {
|
|
ret = regulator_set_voltage(regu, data->min_uv, data->max_uv);
|
|
if (ret != 0) {
|
|
pr_err("regulator_set_voltage() failed: %d\n", ret);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (data->min_uv && data->max_uv) {
|
|
ret = regu->rdev->desc->ops->enable(regu->rdev);
|
|
if (ret != 0)
|
|
pr_err("regulator_enable() failed: %d\n", ret);
|
|
}
|
|
|
|
if (!(data->min_uv && data->max_uv)) {
|
|
ret = regu->rdev->desc->ops->disable(regu->rdev);
|
|
if (ret != 0)
|
|
pr_err("regulator_disable() failed: %d\n", ret);
|
|
}
|
|
|
|
regulator_put(regu);
|
|
}
|
|
|
|
static void update_current_limit_constraints(struct virtual_consumer_data *data)
|
|
{
|
|
int ret;
|
|
struct regulator *regu;
|
|
|
|
regu = regulator_get(NULL, data->regu_name);
|
|
if (IS_ERR(regu)) {
|
|
pr_err("%s: regulator get %s failed\n",
|
|
__func__, data->regu_name);
|
|
return;
|
|
}
|
|
|
|
if (data->max_ua && data->min_ua <= data->max_ua) {
|
|
ret = regulator_set_current_limit(regu,
|
|
data->min_ua, data->max_ua);
|
|
if (ret != 0) {
|
|
pr_err("regulator_set_current_limit failed\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (data->max_ua && !data->enabled) {
|
|
ret = regulator_enable(regu);
|
|
if (ret == 0)
|
|
data->enabled = 1;
|
|
else
|
|
pr_err("regulator_enable() failed: %d\n", ret);
|
|
}
|
|
|
|
if (!(data->min_ua && data->max_ua) && data->enabled) {
|
|
ret = regulator_disable(regu);
|
|
if (ret == 0)
|
|
data->enabled = 0;
|
|
else
|
|
pr_err("regulator_disable() failed: %d\n", ret);
|
|
}
|
|
|
|
regulator_put(regu);
|
|
}
|
|
|
|
static ssize_t show_min_uv(struct device *dev,
|
|
struct device_attribute *attr, char *buf)
|
|
{
|
|
struct virtual_consumer_data *data = dev_get_drvdata(dev);
|
|
struct regulator *regu;
|
|
|
|
regu = regulator_get(NULL, data->regu_name);
|
|
if (IS_ERR(regu))
|
|
return sprintf(buf, "%s: failed\n", __func__);
|
|
|
|
data->min_uv = regulator_get_voltage(regu);
|
|
regulator_put(regu);
|
|
|
|
return sprintf(buf, "%d\n", data->min_uv);
|
|
}
|
|
|
|
static ssize_t set_min_uv(struct device *dev, struct device_attribute *attr,
|
|
const char *buf, size_t count)
|
|
{
|
|
struct virtual_consumer_data *data = dev_get_drvdata(dev);
|
|
int val, err;
|
|
|
|
err = kstrtoint(buf, 10, &val);
|
|
if (err)
|
|
return err;
|
|
|
|
data->max_uv = (data->max_uv >= data->min_uv) ?
|
|
data->max_uv : data->min_uv;
|
|
|
|
mutex_lock(&data->lock);
|
|
data->min_uv = val;
|
|
update_voltage_constraints(data);
|
|
mutex_unlock(&data->lock);
|
|
|
|
return count;
|
|
}
|
|
|
|
static ssize_t show_max_uv(struct device *dev,
|
|
struct device_attribute *attr, char *buf)
|
|
{
|
|
struct virtual_consumer_data *data = dev_get_drvdata(dev);
|
|
|
|
return sprintf(buf, "%d\n", data->max_uv);
|
|
}
|
|
|
|
static ssize_t set_max_uv(struct device *dev, struct device_attribute *attr,
|
|
const char *buf, size_t count)
|
|
{
|
|
struct virtual_consumer_data *data = dev_get_drvdata(dev);
|
|
struct regulator *regu;
|
|
int val, err;
|
|
|
|
err = kstrtoint(buf, 10, &val);
|
|
if (err)
|
|
return err;
|
|
|
|
regu = regulator_get(NULL, data->regu_name);
|
|
if (IS_ERR(regu))
|
|
return (long)regu;
|
|
|
|
data->min_uv = regulator_get_voltage(regu);
|
|
|
|
mutex_lock(&data->lock);
|
|
data->max_uv = val;
|
|
update_voltage_constraints(data);
|
|
mutex_unlock(&data->lock);
|
|
|
|
return count;
|
|
}
|
|
|
|
static ssize_t show_min_ua(struct device *dev,
|
|
struct device_attribute *attr, char *buf)
|
|
{
|
|
struct virtual_consumer_data *data = dev_get_drvdata(dev);
|
|
|
|
return sprintf(buf, "%d\n", data->min_ua);
|
|
}
|
|
|
|
static ssize_t set_min_ua(struct device *dev, struct device_attribute *attr,
|
|
const char *buf, size_t count)
|
|
{
|
|
struct virtual_consumer_data *data = dev_get_drvdata(dev);
|
|
int val, err;
|
|
|
|
err = kstrtoint(buf, 10, &val);
|
|
if (err)
|
|
return err;
|
|
|
|
mutex_lock(&data->lock);
|
|
data->min_ua = val;
|
|
update_current_limit_constraints(data);
|
|
mutex_unlock(&data->lock);
|
|
|
|
return count;
|
|
}
|
|
|
|
static ssize_t show_max_ua(struct device *dev,
|
|
struct device_attribute *attr, char *buf)
|
|
{
|
|
struct virtual_consumer_data *data = dev_get_drvdata(dev);
|
|
|
|
return sprintf(buf, "%d\n", data->max_ua);
|
|
}
|
|
|
|
static ssize_t set_max_ua(struct device *dev, struct device_attribute *attr,
|
|
const char *buf, size_t count)
|
|
{
|
|
struct virtual_consumer_data *data = dev_get_drvdata(dev);
|
|
int val, err;
|
|
|
|
err = kstrtoint(buf, 10, &val);
|
|
if (err)
|
|
return err;
|
|
|
|
mutex_lock(&data->lock);
|
|
data->max_ua = val;
|
|
update_current_limit_constraints(data);
|
|
mutex_unlock(&data->lock);
|
|
|
|
return count;
|
|
}
|
|
|
|
static DEVICE_ATTR(min_microvolts, 0644, show_min_uv, set_min_uv);
|
|
static DEVICE_ATTR(max_microvolts, 0644, show_max_uv, set_max_uv);
|
|
static DEVICE_ATTR(min_microamps, 0644, show_min_ua, set_min_ua);
|
|
static DEVICE_ATTR(max_microamps, 0644, show_max_ua, set_max_ua);
|
|
|
|
struct device_attribute *attributes_virtual[] = {
|
|
&dev_attr_min_microvolts,
|
|
&dev_attr_max_microvolts,
|
|
&dev_attr_min_microamps,
|
|
&dev_attr_max_microamps,
|
|
};
|
|
|
|
int axp_add_virtual_regulator_devices(struct platform_device *pdev, int size,
|
|
struct axp_virtual_dev_mapping *mapping)
|
|
{
|
|
int i, j, ret = 0;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
pdev[i].name = mapping[i].device_name;
|
|
pdev[i].id = -1;
|
|
pdev[i].dev.platform_data = mapping[i].regulator_name;
|
|
ret = platform_device_register(&pdev[i]);
|
|
if (ret)
|
|
goto failed;
|
|
}
|
|
|
|
return 0;
|
|
|
|
failed:
|
|
for (j = i - 1; j >= 0; j--)
|
|
platform_device_unregister(&pdev[j]);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void axp_init_virtual_regulator_drivers(struct platform_driver *pdrv, int size,
|
|
struct axp_virtual_dev_mapping *mapping,
|
|
int (*_probe)(struct platform_device *),
|
|
int (*_remove)(struct platform_device *))
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
pdrv[i].probe = _probe;
|
|
pdrv[i].remove = _remove;
|
|
pdrv[i].driver.name = mapping[i].device_name;
|
|
}
|
|
}
|
|
|
|
MODULE_DESCRIPTION("Virtual regulator driver of axp");
|
|
MODULE_AUTHOR("pannan");
|
|
MODULE_LICENSE("GPL");
|