0001
0002
0003
0004
0005 #include <linux/acpi.h>
0006 #include <linux/bits.h>
0007 #include <linux/init.h>
0008 #include <linux/mutex.h>
0009 #include <linux/platform_profile.h>
0010 #include <linux/sysfs.h>
0011
0012 static struct platform_profile_handler *cur_profile;
0013 static DEFINE_MUTEX(profile_lock);
0014
0015 static const char * const profile_names[] = {
0016 [PLATFORM_PROFILE_LOW_POWER] = "low-power",
0017 [PLATFORM_PROFILE_COOL] = "cool",
0018 [PLATFORM_PROFILE_QUIET] = "quiet",
0019 [PLATFORM_PROFILE_BALANCED] = "balanced",
0020 [PLATFORM_PROFILE_BALANCED_PERFORMANCE] = "balanced-performance",
0021 [PLATFORM_PROFILE_PERFORMANCE] = "performance",
0022 };
0023 static_assert(ARRAY_SIZE(profile_names) == PLATFORM_PROFILE_LAST);
0024
0025 static ssize_t platform_profile_choices_show(struct device *dev,
0026 struct device_attribute *attr,
0027 char *buf)
0028 {
0029 int len = 0;
0030 int err, i;
0031
0032 err = mutex_lock_interruptible(&profile_lock);
0033 if (err)
0034 return err;
0035
0036 if (!cur_profile) {
0037 mutex_unlock(&profile_lock);
0038 return -ENODEV;
0039 }
0040
0041 for_each_set_bit(i, cur_profile->choices, PLATFORM_PROFILE_LAST) {
0042 if (len == 0)
0043 len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
0044 else
0045 len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
0046 }
0047 len += sysfs_emit_at(buf, len, "\n");
0048 mutex_unlock(&profile_lock);
0049 return len;
0050 }
0051
0052 static ssize_t platform_profile_show(struct device *dev,
0053 struct device_attribute *attr,
0054 char *buf)
0055 {
0056 enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
0057 int err;
0058
0059 err = mutex_lock_interruptible(&profile_lock);
0060 if (err)
0061 return err;
0062
0063 if (!cur_profile) {
0064 mutex_unlock(&profile_lock);
0065 return -ENODEV;
0066 }
0067
0068 err = cur_profile->profile_get(cur_profile, &profile);
0069 mutex_unlock(&profile_lock);
0070 if (err)
0071 return err;
0072
0073
0074 if (WARN_ON((profile < 0) || (profile >= ARRAY_SIZE(profile_names))))
0075 return -EIO;
0076
0077 return sysfs_emit(buf, "%s\n", profile_names[profile]);
0078 }
0079
0080 static ssize_t platform_profile_store(struct device *dev,
0081 struct device_attribute *attr,
0082 const char *buf, size_t count)
0083 {
0084 int err, i;
0085
0086 err = mutex_lock_interruptible(&profile_lock);
0087 if (err)
0088 return err;
0089
0090 if (!cur_profile) {
0091 mutex_unlock(&profile_lock);
0092 return -ENODEV;
0093 }
0094
0095
0096 i = sysfs_match_string(profile_names, buf);
0097 if (i < 0) {
0098 mutex_unlock(&profile_lock);
0099 return -EINVAL;
0100 }
0101
0102
0103 if (!test_bit(i, cur_profile->choices)) {
0104 mutex_unlock(&profile_lock);
0105 return -EOPNOTSUPP;
0106 }
0107
0108 err = cur_profile->profile_set(cur_profile, i);
0109 if (!err)
0110 sysfs_notify(acpi_kobj, NULL, "platform_profile");
0111
0112 mutex_unlock(&profile_lock);
0113 if (err)
0114 return err;
0115 return count;
0116 }
0117
0118 static DEVICE_ATTR_RO(platform_profile_choices);
0119 static DEVICE_ATTR_RW(platform_profile);
0120
0121 static struct attribute *platform_profile_attrs[] = {
0122 &dev_attr_platform_profile_choices.attr,
0123 &dev_attr_platform_profile.attr,
0124 NULL
0125 };
0126
0127 static const struct attribute_group platform_profile_group = {
0128 .attrs = platform_profile_attrs
0129 };
0130
0131 void platform_profile_notify(void)
0132 {
0133 if (!cur_profile)
0134 return;
0135 sysfs_notify(acpi_kobj, NULL, "platform_profile");
0136 }
0137 EXPORT_SYMBOL_GPL(platform_profile_notify);
0138
0139 int platform_profile_register(struct platform_profile_handler *pprof)
0140 {
0141 int err;
0142
0143 mutex_lock(&profile_lock);
0144
0145 if (cur_profile) {
0146 mutex_unlock(&profile_lock);
0147 return -EEXIST;
0148 }
0149
0150
0151 if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
0152 !pprof->profile_set || !pprof->profile_get) {
0153 mutex_unlock(&profile_lock);
0154 return -EINVAL;
0155 }
0156
0157 err = sysfs_create_group(acpi_kobj, &platform_profile_group);
0158 if (err) {
0159 mutex_unlock(&profile_lock);
0160 return err;
0161 }
0162
0163 cur_profile = pprof;
0164 mutex_unlock(&profile_lock);
0165 return 0;
0166 }
0167 EXPORT_SYMBOL_GPL(platform_profile_register);
0168
0169 int platform_profile_remove(void)
0170 {
0171 sysfs_remove_group(acpi_kobj, &platform_profile_group);
0172
0173 mutex_lock(&profile_lock);
0174 cur_profile = NULL;
0175 mutex_unlock(&profile_lock);
0176 return 0;
0177 }
0178 EXPORT_SYMBOL_GPL(platform_profile_remove);
0179
0180 MODULE_AUTHOR("Mark Pearson <markpearson@lenovo.com>");
0181 MODULE_LICENSE("GPL");