| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 1 | /* | 
|  | 2 | * userspace-consumer.c | 
|  | 3 | * | 
|  | 4 | * Copyright 2009 CompuLab, Ltd. | 
|  | 5 | * | 
|  | 6 | * Author: Mike Rapoport <mike@compulab.co.il> | 
|  | 7 | * | 
|  | 8 | * Based of virtual consumer driver: | 
|  | 9 | *   Copyright 2008 Wolfson Microelectronics PLC. | 
|  | 10 | *   Author: Mark Brown <broonie@opensource.wolfsonmicro.com> | 
|  | 11 | * | 
|  | 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. | 
|  | 16 | * | 
|  | 17 | */ | 
|  | 18 |  | 
|  | 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 |  | 
|  | 25 | struct userspace_consumer_data { | 
|  | 26 | const char *name; | 
|  | 27 |  | 
|  | 28 | struct mutex lock; | 
|  | 29 | bool enabled; | 
|  | 30 |  | 
|  | 31 | int num_supplies; | 
|  | 32 | struct regulator_bulk_data *supplies; | 
|  | 33 | }; | 
|  | 34 |  | 
| Liam Girdwood | 7c31499 | 2009-04-28 12:01:16 +0100 | [diff] [blame] | 35 | static ssize_t reg_show_name(struct device *dev, | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 36 | struct device_attribute *attr, char *buf) | 
|  | 37 | { | 
|  | 38 | struct userspace_consumer_data *data = dev_get_drvdata(dev); | 
|  | 39 |  | 
|  | 40 | return sprintf(buf, "%s\n", data->name); | 
|  | 41 | } | 
|  | 42 |  | 
| Liam Girdwood | 7c31499 | 2009-04-28 12:01:16 +0100 | [diff] [blame] | 43 | static ssize_t reg_show_state(struct device *dev, | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 44 | struct device_attribute *attr, char *buf) | 
|  | 45 | { | 
|  | 46 | struct userspace_consumer_data *data = dev_get_drvdata(dev); | 
|  | 47 |  | 
|  | 48 | if (data->enabled) | 
|  | 49 | return sprintf(buf, "enabled\n"); | 
|  | 50 |  | 
|  | 51 | return sprintf(buf, "disabled\n"); | 
|  | 52 | } | 
|  | 53 |  | 
| Liam Girdwood | 7c31499 | 2009-04-28 12:01:16 +0100 | [diff] [blame] | 54 | static ssize_t reg_set_state(struct device *dev, struct device_attribute *attr, | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 55 | const char *buf, size_t count) | 
|  | 56 | { | 
|  | 57 | struct userspace_consumer_data *data = dev_get_drvdata(dev); | 
|  | 58 | bool enabled; | 
|  | 59 | int ret; | 
|  | 60 |  | 
|  | 61 | /* | 
|  | 62 | * sysfs_streq() doesn't need the \n's, but we add them so the strings | 
|  | 63 | * will be shared with show_state(), above. | 
|  | 64 | */ | 
|  | 65 | if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1")) | 
|  | 66 | enabled = true; | 
|  | 67 | else if (sysfs_streq(buf, "disabled\n") || sysfs_streq(buf, "0")) | 
|  | 68 | enabled = false; | 
|  | 69 | else { | 
|  | 70 | dev_err(dev, "Configuring invalid mode\n"); | 
|  | 71 | return count; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | mutex_lock(&data->lock); | 
|  | 75 | if (enabled != data->enabled) { | 
|  | 76 | if (enabled) | 
|  | 77 | ret = regulator_bulk_enable(data->num_supplies, | 
|  | 78 | data->supplies); | 
|  | 79 | else | 
|  | 80 | ret = regulator_bulk_disable(data->num_supplies, | 
|  | 81 | data->supplies); | 
|  | 82 |  | 
|  | 83 | if (ret == 0) | 
|  | 84 | data->enabled = enabled; | 
|  | 85 | else | 
|  | 86 | dev_err(dev, "Failed to configure state: %d\n", ret); | 
|  | 87 | } | 
|  | 88 | mutex_unlock(&data->lock); | 
|  | 89 |  | 
|  | 90 | return count; | 
|  | 91 | } | 
|  | 92 |  | 
| Liam Girdwood | 7c31499 | 2009-04-28 12:01:16 +0100 | [diff] [blame] | 93 | static DEVICE_ATTR(name, 0444, reg_show_name, NULL); | 
|  | 94 | static DEVICE_ATTR(state, 0644, reg_show_state, reg_set_state); | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 95 |  | 
| Felipe Balbi | e9d6269 | 2009-08-10 09:05:13 +0300 | [diff] [blame] | 96 | static struct attribute *attributes[] = { | 
|  | 97 | &dev_attr_name.attr, | 
|  | 98 | &dev_attr_state.attr, | 
|  | 99 | NULL, | 
|  | 100 | }; | 
|  | 101 |  | 
|  | 102 | static const struct attribute_group attr_group = { | 
|  | 103 | .attrs	= attributes, | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 104 | }; | 
|  | 105 |  | 
|  | 106 | static int regulator_userspace_consumer_probe(struct platform_device *pdev) | 
|  | 107 | { | 
|  | 108 | struct regulator_userspace_consumer_data *pdata; | 
|  | 109 | struct userspace_consumer_data *drvdata; | 
| Felipe Balbi | e9d6269 | 2009-08-10 09:05:13 +0300 | [diff] [blame] | 110 | int ret; | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 111 |  | 
|  | 112 | pdata = pdev->dev.platform_data; | 
|  | 113 | if (!pdata) | 
|  | 114 | return -EINVAL; | 
|  | 115 |  | 
|  | 116 | drvdata = kzalloc(sizeof(struct userspace_consumer_data), GFP_KERNEL); | 
|  | 117 | if (drvdata == NULL) | 
|  | 118 | return -ENOMEM; | 
|  | 119 |  | 
|  | 120 | drvdata->name = pdata->name; | 
|  | 121 | drvdata->num_supplies = pdata->num_supplies; | 
|  | 122 | drvdata->supplies = pdata->supplies; | 
|  | 123 |  | 
|  | 124 | mutex_init(&drvdata->lock); | 
|  | 125 |  | 
|  | 126 | ret = regulator_bulk_get(&pdev->dev, drvdata->num_supplies, | 
|  | 127 | drvdata->supplies); | 
|  | 128 | if (ret) { | 
|  | 129 | dev_err(&pdev->dev, "Failed to get supplies: %d\n", ret); | 
|  | 130 | goto err_alloc_supplies; | 
|  | 131 | } | 
|  | 132 |  | 
| Felipe Balbi | e9d6269 | 2009-08-10 09:05:13 +0300 | [diff] [blame] | 133 | ret = sysfs_create_group(&pdev->dev.kobj, &attr_group); | 
|  | 134 | if (ret != 0) | 
|  | 135 | goto err_create_attrs; | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 136 |  | 
| Felipe Balbi | e9d6269 | 2009-08-10 09:05:13 +0300 | [diff] [blame] | 137 | if (pdata->init_on) { | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 138 | ret = regulator_bulk_enable(drvdata->num_supplies, | 
|  | 139 | drvdata->supplies); | 
| Felipe Balbi | e9d6269 | 2009-08-10 09:05:13 +0300 | [diff] [blame] | 140 | if (ret) { | 
|  | 141 | dev_err(&pdev->dev, | 
|  | 142 | "Failed to set initial state: %d\n", ret); | 
|  | 143 | goto err_enable; | 
|  | 144 | } | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
| Felipe Balbi | e9d6269 | 2009-08-10 09:05:13 +0300 | [diff] [blame] | 147 | drvdata->enabled = pdata->init_on; | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 148 | platform_set_drvdata(pdev, drvdata); | 
|  | 149 |  | 
|  | 150 | return 0; | 
|  | 151 |  | 
| Felipe Balbi | e9d6269 | 2009-08-10 09:05:13 +0300 | [diff] [blame] | 152 | err_enable: | 
|  | 153 | sysfs_remove_group(&pdev->dev.kobj, &attr_group); | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 154 |  | 
| Felipe Balbi | e9d6269 | 2009-08-10 09:05:13 +0300 | [diff] [blame] | 155 | err_create_attrs: | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 156 | regulator_bulk_free(drvdata->num_supplies, drvdata->supplies); | 
|  | 157 |  | 
|  | 158 | err_alloc_supplies: | 
|  | 159 | kfree(drvdata); | 
|  | 160 | return ret; | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | static int regulator_userspace_consumer_remove(struct platform_device *pdev) | 
|  | 164 | { | 
|  | 165 | struct userspace_consumer_data *data = platform_get_drvdata(pdev); | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 166 |  | 
| Felipe Balbi | e9d6269 | 2009-08-10 09:05:13 +0300 | [diff] [blame] | 167 | sysfs_remove_group(&pdev->dev.kobj, &attr_group); | 
| Mike Rapoport | 1d98ccc | 2009-04-26 16:49:39 +0300 | [diff] [blame] | 168 |  | 
|  | 169 | if (data->enabled) | 
|  | 170 | regulator_bulk_disable(data->num_supplies, data->supplies); | 
|  | 171 |  | 
|  | 172 | regulator_bulk_free(data->num_supplies, data->supplies); | 
|  | 173 | kfree(data); | 
|  | 174 |  | 
|  | 175 | return 0; | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | static struct platform_driver regulator_userspace_consumer_driver = { | 
|  | 179 | .probe		= regulator_userspace_consumer_probe, | 
|  | 180 | .remove		= regulator_userspace_consumer_remove, | 
|  | 181 | .driver		= { | 
|  | 182 | .name		= "reg-userspace-consumer", | 
|  | 183 | }, | 
|  | 184 | }; | 
|  | 185 |  | 
|  | 186 |  | 
|  | 187 | static int __init regulator_userspace_consumer_init(void) | 
|  | 188 | { | 
|  | 189 | return platform_driver_register(®ulator_userspace_consumer_driver); | 
|  | 190 | } | 
|  | 191 | module_init(regulator_userspace_consumer_init); | 
|  | 192 |  | 
|  | 193 | static void __exit regulator_userspace_consumer_exit(void) | 
|  | 194 | { | 
|  | 195 | platform_driver_unregister(®ulator_userspace_consumer_driver); | 
|  | 196 | } | 
|  | 197 | module_exit(regulator_userspace_consumer_exit); | 
|  | 198 |  | 
|  | 199 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); | 
|  | 200 | MODULE_DESCRIPTION("Userspace consumer for voltage and current regulators"); | 
|  | 201 | MODULE_LICENSE("GPL"); |