H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * A simple sysfs interface for the generic PWM framework |
| 3 | * |
| 4 | * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com> |
| 5 | * |
| 6 | * Based on previous work by Lars Poeschel <poeschel@lemonage.de> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/kdev_t.h> |
| 24 | #include <linux/pwm.h> |
| 25 | |
| 26 | struct pwm_export { |
| 27 | struct device child; |
| 28 | struct pwm_device *pwm; |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 29 | struct mutex lock; |
Yoshihiro Shimoda | 7fd4edc | 2019-05-31 18:55:00 +0900 | [diff] [blame^] | 30 | struct pwm_state suspend; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static struct pwm_export *child_to_pwm_export(struct device *child) |
| 34 | { |
| 35 | return container_of(child, struct pwm_export, child); |
| 36 | } |
| 37 | |
| 38 | static struct pwm_device *child_to_pwm_device(struct device *child) |
| 39 | { |
| 40 | struct pwm_export *export = child_to_pwm_export(child); |
| 41 | |
| 42 | return export->pwm; |
| 43 | } |
| 44 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 45 | static ssize_t period_show(struct device *child, |
| 46 | struct device_attribute *attr, |
| 47 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 48 | { |
| 49 | const struct pwm_device *pwm = child_to_pwm_device(child); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 50 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 51 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 52 | pwm_get_state(pwm, &state); |
| 53 | |
| 54 | return sprintf(buf, "%u\n", state.period); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 57 | static ssize_t period_store(struct device *child, |
| 58 | struct device_attribute *attr, |
| 59 | const char *buf, size_t size) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 60 | { |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 61 | struct pwm_export *export = child_to_pwm_export(child); |
| 62 | struct pwm_device *pwm = export->pwm; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 63 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 64 | unsigned int val; |
| 65 | int ret; |
| 66 | |
| 67 | ret = kstrtouint(buf, 0, &val); |
| 68 | if (ret) |
| 69 | return ret; |
| 70 | |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 71 | mutex_lock(&export->lock); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 72 | pwm_get_state(pwm, &state); |
| 73 | state.period = val; |
| 74 | ret = pwm_apply_state(pwm, &state); |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 75 | mutex_unlock(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 76 | |
| 77 | return ret ? : size; |
| 78 | } |
| 79 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 80 | static ssize_t duty_cycle_show(struct device *child, |
| 81 | struct device_attribute *attr, |
| 82 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 83 | { |
| 84 | const struct pwm_device *pwm = child_to_pwm_device(child); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 85 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 86 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 87 | pwm_get_state(pwm, &state); |
| 88 | |
| 89 | return sprintf(buf, "%u\n", state.duty_cycle); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 92 | static ssize_t duty_cycle_store(struct device *child, |
| 93 | struct device_attribute *attr, |
| 94 | const char *buf, size_t size) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 95 | { |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 96 | struct pwm_export *export = child_to_pwm_export(child); |
| 97 | struct pwm_device *pwm = export->pwm; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 98 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 99 | unsigned int val; |
| 100 | int ret; |
| 101 | |
| 102 | ret = kstrtouint(buf, 0, &val); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 106 | mutex_lock(&export->lock); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 107 | pwm_get_state(pwm, &state); |
| 108 | state.duty_cycle = val; |
| 109 | ret = pwm_apply_state(pwm, &state); |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 110 | mutex_unlock(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 111 | |
| 112 | return ret ? : size; |
| 113 | } |
| 114 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 115 | static ssize_t enable_show(struct device *child, |
| 116 | struct device_attribute *attr, |
| 117 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 118 | { |
| 119 | const struct pwm_device *pwm = child_to_pwm_device(child); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 120 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 121 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 122 | pwm_get_state(pwm, &state); |
| 123 | |
| 124 | return sprintf(buf, "%d\n", state.enabled); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 127 | static ssize_t enable_store(struct device *child, |
| 128 | struct device_attribute *attr, |
| 129 | const char *buf, size_t size) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 130 | { |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 131 | struct pwm_export *export = child_to_pwm_export(child); |
| 132 | struct pwm_device *pwm = export->pwm; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 133 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 134 | int val, ret; |
| 135 | |
| 136 | ret = kstrtoint(buf, 0, &val); |
| 137 | if (ret) |
| 138 | return ret; |
| 139 | |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 140 | mutex_lock(&export->lock); |
| 141 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 142 | pwm_get_state(pwm, &state); |
| 143 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 144 | switch (val) { |
| 145 | case 0: |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 146 | state.enabled = false; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 147 | break; |
| 148 | case 1: |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 149 | state.enabled = true; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 150 | break; |
| 151 | default: |
| 152 | ret = -EINVAL; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 153 | goto unlock; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Ryo Kodama | fe5aa34 | 2016-06-08 10:58:23 +0900 | [diff] [blame] | 156 | ret = pwm_apply_state(pwm, &state); |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 157 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 158 | unlock: |
| 159 | mutex_unlock(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 160 | return ret ? : size; |
| 161 | } |
| 162 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 163 | static ssize_t polarity_show(struct device *child, |
| 164 | struct device_attribute *attr, |
| 165 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 166 | { |
| 167 | const struct pwm_device *pwm = child_to_pwm_device(child); |
Thierry Reding | 5a063d8 | 2015-07-20 09:56:05 +0200 | [diff] [blame] | 168 | const char *polarity = "unknown"; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 169 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 170 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 171 | pwm_get_state(pwm, &state); |
| 172 | |
| 173 | switch (state.polarity) { |
Thierry Reding | 5a063d8 | 2015-07-20 09:56:05 +0200 | [diff] [blame] | 174 | case PWM_POLARITY_NORMAL: |
| 175 | polarity = "normal"; |
| 176 | break; |
| 177 | |
| 178 | case PWM_POLARITY_INVERSED: |
| 179 | polarity = "inversed"; |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | return sprintf(buf, "%s\n", polarity); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 186 | static ssize_t polarity_store(struct device *child, |
| 187 | struct device_attribute *attr, |
| 188 | const char *buf, size_t size) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 189 | { |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 190 | struct pwm_export *export = child_to_pwm_export(child); |
| 191 | struct pwm_device *pwm = export->pwm; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 192 | enum pwm_polarity polarity; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 193 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 194 | int ret; |
| 195 | |
| 196 | if (sysfs_streq(buf, "normal")) |
| 197 | polarity = PWM_POLARITY_NORMAL; |
| 198 | else if (sysfs_streq(buf, "inversed")) |
| 199 | polarity = PWM_POLARITY_INVERSED; |
| 200 | else |
| 201 | return -EINVAL; |
| 202 | |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 203 | mutex_lock(&export->lock); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 204 | pwm_get_state(pwm, &state); |
| 205 | state.polarity = polarity; |
| 206 | ret = pwm_apply_state(pwm, &state); |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 207 | mutex_unlock(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 208 | |
| 209 | return ret ? : size; |
| 210 | } |
| 211 | |
Lee Jones | 1a366fe | 2016-06-08 10:21:25 +0100 | [diff] [blame] | 212 | static ssize_t capture_show(struct device *child, |
| 213 | struct device_attribute *attr, |
| 214 | char *buf) |
| 215 | { |
| 216 | struct pwm_device *pwm = child_to_pwm_device(child); |
| 217 | struct pwm_capture result; |
| 218 | int ret; |
| 219 | |
| 220 | ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); |
| 221 | if (ret) |
| 222 | return ret; |
| 223 | |
| 224 | return sprintf(buf, "%u %u\n", result.period, result.duty_cycle); |
| 225 | } |
| 226 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 227 | static DEVICE_ATTR_RW(period); |
| 228 | static DEVICE_ATTR_RW(duty_cycle); |
| 229 | static DEVICE_ATTR_RW(enable); |
| 230 | static DEVICE_ATTR_RW(polarity); |
Lee Jones | 1a366fe | 2016-06-08 10:21:25 +0100 | [diff] [blame] | 231 | static DEVICE_ATTR_RO(capture); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 232 | |
| 233 | static struct attribute *pwm_attrs[] = { |
| 234 | &dev_attr_period.attr, |
| 235 | &dev_attr_duty_cycle.attr, |
| 236 | &dev_attr_enable.attr, |
| 237 | &dev_attr_polarity.attr, |
Lee Jones | 1a366fe | 2016-06-08 10:21:25 +0100 | [diff] [blame] | 238 | &dev_attr_capture.attr, |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 239 | NULL |
| 240 | }; |
Axel Lin | 6ca142a | 2013-12-04 18:29:53 +0800 | [diff] [blame] | 241 | ATTRIBUTE_GROUPS(pwm); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 242 | |
| 243 | static void pwm_export_release(struct device *child) |
| 244 | { |
| 245 | struct pwm_export *export = child_to_pwm_export(child); |
| 246 | |
| 247 | kfree(export); |
| 248 | } |
| 249 | |
| 250 | static int pwm_export_child(struct device *parent, struct pwm_device *pwm) |
| 251 | { |
| 252 | struct pwm_export *export; |
Fabrice Gasnier | 552c02e | 2018-10-01 15:23:57 +0200 | [diff] [blame] | 253 | char *pwm_prop[2]; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 254 | int ret; |
| 255 | |
| 256 | if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) |
| 257 | return -EBUSY; |
| 258 | |
| 259 | export = kzalloc(sizeof(*export), GFP_KERNEL); |
| 260 | if (!export) { |
| 261 | clear_bit(PWMF_EXPORTED, &pwm->flags); |
| 262 | return -ENOMEM; |
| 263 | } |
| 264 | |
| 265 | export->pwm = pwm; |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 266 | mutex_init(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 267 | |
| 268 | export->child.release = pwm_export_release; |
| 269 | export->child.parent = parent; |
| 270 | export->child.devt = MKDEV(0, 0); |
Axel Lin | 6ca142a | 2013-12-04 18:29:53 +0800 | [diff] [blame] | 271 | export->child.groups = pwm_groups; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 272 | dev_set_name(&export->child, "pwm%u", pwm->hwpwm); |
| 273 | |
| 274 | ret = device_register(&export->child); |
| 275 | if (ret) { |
| 276 | clear_bit(PWMF_EXPORTED, &pwm->flags); |
Arvind Yadav | 8bbf5b4 | 2018-03-08 15:27:37 +0530 | [diff] [blame] | 277 | put_device(&export->child); |
| 278 | export = NULL; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 279 | return ret; |
| 280 | } |
Fabrice Gasnier | 552c02e | 2018-10-01 15:23:57 +0200 | [diff] [blame] | 281 | pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); |
| 282 | pwm_prop[1] = NULL; |
| 283 | kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop); |
| 284 | kfree(pwm_prop[0]); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int pwm_unexport_match(struct device *child, void *data) |
| 290 | { |
| 291 | return child_to_pwm_device(child) == data; |
| 292 | } |
| 293 | |
| 294 | static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm) |
| 295 | { |
| 296 | struct device *child; |
Fabrice Gasnier | 552c02e | 2018-10-01 15:23:57 +0200 | [diff] [blame] | 297 | char *pwm_prop[2]; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 298 | |
| 299 | if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) |
| 300 | return -ENODEV; |
| 301 | |
| 302 | child = device_find_child(parent, pwm, pwm_unexport_match); |
| 303 | if (!child) |
| 304 | return -ENODEV; |
| 305 | |
Fabrice Gasnier | 552c02e | 2018-10-01 15:23:57 +0200 | [diff] [blame] | 306 | pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); |
| 307 | pwm_prop[1] = NULL; |
| 308 | kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop); |
| 309 | kfree(pwm_prop[0]); |
| 310 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 311 | /* for device_find_child() */ |
| 312 | put_device(child); |
| 313 | device_unregister(child); |
| 314 | pwm_put(pwm); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 319 | static ssize_t export_store(struct device *parent, |
| 320 | struct device_attribute *attr, |
| 321 | const char *buf, size_t len) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 322 | { |
| 323 | struct pwm_chip *chip = dev_get_drvdata(parent); |
| 324 | struct pwm_device *pwm; |
| 325 | unsigned int hwpwm; |
| 326 | int ret; |
| 327 | |
| 328 | ret = kstrtouint(buf, 0, &hwpwm); |
| 329 | if (ret < 0) |
| 330 | return ret; |
| 331 | |
| 332 | if (hwpwm >= chip->npwm) |
| 333 | return -ENODEV; |
| 334 | |
| 335 | pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); |
| 336 | if (IS_ERR(pwm)) |
| 337 | return PTR_ERR(pwm); |
| 338 | |
| 339 | ret = pwm_export_child(parent, pwm); |
| 340 | if (ret < 0) |
| 341 | pwm_put(pwm); |
| 342 | |
| 343 | return ret ? : len; |
| 344 | } |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 345 | static DEVICE_ATTR_WO(export); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 346 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 347 | static ssize_t unexport_store(struct device *parent, |
| 348 | struct device_attribute *attr, |
| 349 | const char *buf, size_t len) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 350 | { |
| 351 | struct pwm_chip *chip = dev_get_drvdata(parent); |
| 352 | unsigned int hwpwm; |
| 353 | int ret; |
| 354 | |
| 355 | ret = kstrtouint(buf, 0, &hwpwm); |
| 356 | if (ret < 0) |
| 357 | return ret; |
| 358 | |
| 359 | if (hwpwm >= chip->npwm) |
| 360 | return -ENODEV; |
| 361 | |
| 362 | ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]); |
| 363 | |
| 364 | return ret ? : len; |
| 365 | } |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 366 | static DEVICE_ATTR_WO(unexport); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 367 | |
Greg Kroah-Hartman | 9da0175 | 2013-07-24 15:05:39 -0700 | [diff] [blame] | 368 | static ssize_t npwm_show(struct device *parent, struct device_attribute *attr, |
| 369 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 370 | { |
| 371 | const struct pwm_chip *chip = dev_get_drvdata(parent); |
| 372 | |
| 373 | return sprintf(buf, "%u\n", chip->npwm); |
| 374 | } |
Greg Kroah-Hartman | 9da0175 | 2013-07-24 15:05:39 -0700 | [diff] [blame] | 375 | static DEVICE_ATTR_RO(npwm); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 376 | |
Greg Kroah-Hartman | 9da0175 | 2013-07-24 15:05:39 -0700 | [diff] [blame] | 377 | static struct attribute *pwm_chip_attrs[] = { |
| 378 | &dev_attr_export.attr, |
| 379 | &dev_attr_unexport.attr, |
| 380 | &dev_attr_npwm.attr, |
| 381 | NULL, |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 382 | }; |
Greg Kroah-Hartman | 9da0175 | 2013-07-24 15:05:39 -0700 | [diff] [blame] | 383 | ATTRIBUTE_GROUPS(pwm_chip); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 384 | |
Yoshihiro Shimoda | 7fd4edc | 2019-05-31 18:55:00 +0900 | [diff] [blame^] | 385 | /* takes export->lock on success */ |
| 386 | static struct pwm_export *pwm_class_get_state(struct device *parent, |
| 387 | struct pwm_device *pwm, |
| 388 | struct pwm_state *state) |
| 389 | { |
| 390 | struct device *child; |
| 391 | struct pwm_export *export; |
| 392 | |
| 393 | if (!test_bit(PWMF_EXPORTED, &pwm->flags)) |
| 394 | return NULL; |
| 395 | |
| 396 | child = device_find_child(parent, pwm, pwm_unexport_match); |
| 397 | if (!child) |
| 398 | return NULL; |
| 399 | |
| 400 | export = child_to_pwm_export(child); |
| 401 | put_device(child); /* for device_find_child() */ |
| 402 | |
| 403 | mutex_lock(&export->lock); |
| 404 | pwm_get_state(pwm, state); |
| 405 | |
| 406 | return export; |
| 407 | } |
| 408 | |
| 409 | static int pwm_class_apply_state(struct pwm_export *export, |
| 410 | struct pwm_device *pwm, |
| 411 | struct pwm_state *state) |
| 412 | { |
| 413 | int ret = pwm_apply_state(pwm, state); |
| 414 | |
| 415 | /* release lock taken in pwm_class_get_state */ |
| 416 | mutex_unlock(&export->lock); |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm) |
| 422 | { |
| 423 | struct pwm_chip *chip = dev_get_drvdata(parent); |
| 424 | unsigned int i; |
| 425 | int ret = 0; |
| 426 | |
| 427 | for (i = 0; i < npwm; i++) { |
| 428 | struct pwm_device *pwm = &chip->pwms[i]; |
| 429 | struct pwm_state state; |
| 430 | struct pwm_export *export; |
| 431 | |
| 432 | export = pwm_class_get_state(parent, pwm, &state); |
| 433 | if (!export) |
| 434 | continue; |
| 435 | |
| 436 | state.enabled = export->suspend.enabled; |
| 437 | ret = pwm_class_apply_state(export, pwm, &state); |
| 438 | if (ret < 0) |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | static int __maybe_unused pwm_class_suspend(struct device *parent) |
| 446 | { |
| 447 | struct pwm_chip *chip = dev_get_drvdata(parent); |
| 448 | unsigned int i; |
| 449 | int ret = 0; |
| 450 | |
| 451 | for (i = 0; i < chip->npwm; i++) { |
| 452 | struct pwm_device *pwm = &chip->pwms[i]; |
| 453 | struct pwm_state state; |
| 454 | struct pwm_export *export; |
| 455 | |
| 456 | export = pwm_class_get_state(parent, pwm, &state); |
| 457 | if (!export) |
| 458 | continue; |
| 459 | |
| 460 | export->suspend = state; |
| 461 | state.enabled = false; |
| 462 | ret = pwm_class_apply_state(export, pwm, &state); |
| 463 | if (ret < 0) { |
| 464 | /* |
| 465 | * roll back the PWM devices that were disabled by |
| 466 | * this suspend function. |
| 467 | */ |
| 468 | pwm_class_resume_npwm(parent, i); |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | return ret; |
| 474 | } |
| 475 | |
| 476 | static int __maybe_unused pwm_class_resume(struct device *parent) |
| 477 | { |
| 478 | struct pwm_chip *chip = dev_get_drvdata(parent); |
| 479 | |
| 480 | return pwm_class_resume_npwm(parent, chip->npwm); |
| 481 | } |
| 482 | |
| 483 | static SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume); |
| 484 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 485 | static struct class pwm_class = { |
Thierry Reding | 412820d | 2015-07-20 09:58:09 +0200 | [diff] [blame] | 486 | .name = "pwm", |
| 487 | .owner = THIS_MODULE, |
| 488 | .dev_groups = pwm_chip_groups, |
Yoshihiro Shimoda | 7fd4edc | 2019-05-31 18:55:00 +0900 | [diff] [blame^] | 489 | .pm = &pwm_class_pm_ops, |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | static int pwmchip_sysfs_match(struct device *parent, const void *data) |
| 493 | { |
| 494 | return dev_get_drvdata(parent) == data; |
| 495 | } |
| 496 | |
| 497 | void pwmchip_sysfs_export(struct pwm_chip *chip) |
| 498 | { |
| 499 | struct device *parent; |
| 500 | |
| 501 | /* |
| 502 | * If device_create() fails the pwm_chip is still usable by |
Uwe Kleine-König | 9ff0667 | 2019-03-12 10:25:47 +0100 | [diff] [blame] | 503 | * the kernel it's just not exported. |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 504 | */ |
| 505 | parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip, |
| 506 | "pwmchip%d", chip->base); |
| 507 | if (IS_ERR(parent)) { |
| 508 | dev_warn(chip->dev, |
| 509 | "device_create failed for pwm_chip sysfs export\n"); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | void pwmchip_sysfs_unexport(struct pwm_chip *chip) |
| 514 | { |
| 515 | struct device *parent; |
David Hsu | 0733424 | 2016-08-09 14:57:46 -0700 | [diff] [blame] | 516 | unsigned int i; |
| 517 | |
| 518 | parent = class_find_device(&pwm_class, NULL, chip, |
| 519 | pwmchip_sysfs_match); |
| 520 | if (!parent) |
| 521 | return; |
| 522 | |
| 523 | for (i = 0; i < chip->npwm; i++) { |
| 524 | struct pwm_device *pwm = &chip->pwms[i]; |
| 525 | |
| 526 | if (test_bit(PWMF_EXPORTED, &pwm->flags)) |
| 527 | pwm_unexport_child(parent, pwm); |
| 528 | } |
Johan Hovold | 0e1614a | 2016-11-01 11:46:39 +0100 | [diff] [blame] | 529 | |
| 530 | put_device(parent); |
Phong Hoang | 347ab94 | 2019-03-19 19:40:08 +0900 | [diff] [blame] | 531 | device_unregister(parent); |
David Hsu | 0733424 | 2016-08-09 14:57:46 -0700 | [diff] [blame] | 532 | } |
| 533 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 534 | static int __init pwm_sysfs_init(void) |
| 535 | { |
| 536 | return class_register(&pwm_class); |
| 537 | } |
| 538 | subsys_initcall(pwm_sysfs_init); |