Thomas Gleixner | 3e0a4e8 | 2019-05-23 11:14:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 2 | /* |
| 3 | * A simple sysfs interface for the generic PWM framework |
| 4 | * |
| 5 | * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com> |
| 6 | * |
| 7 | * Based on previous work by Lars Poeschel <poeschel@lemonage.de> |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/mutex.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/kdev_t.h> |
| 15 | #include <linux/pwm.h> |
| 16 | |
| 17 | struct pwm_export { |
| 18 | struct device child; |
| 19 | struct pwm_device *pwm; |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 20 | struct mutex lock; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | static struct pwm_export *child_to_pwm_export(struct device *child) |
| 24 | { |
| 25 | return container_of(child, struct pwm_export, child); |
| 26 | } |
| 27 | |
| 28 | static struct pwm_device *child_to_pwm_device(struct device *child) |
| 29 | { |
| 30 | struct pwm_export *export = child_to_pwm_export(child); |
| 31 | |
| 32 | return export->pwm; |
| 33 | } |
| 34 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 35 | static ssize_t period_show(struct device *child, |
| 36 | struct device_attribute *attr, |
| 37 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 38 | { |
| 39 | const struct pwm_device *pwm = child_to_pwm_device(child); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 40 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 41 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 42 | pwm_get_state(pwm, &state); |
| 43 | |
| 44 | return sprintf(buf, "%u\n", state.period); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 47 | static ssize_t period_store(struct device *child, |
| 48 | struct device_attribute *attr, |
| 49 | const char *buf, size_t size) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 50 | { |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 51 | struct pwm_export *export = child_to_pwm_export(child); |
| 52 | struct pwm_device *pwm = export->pwm; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 53 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 54 | unsigned int val; |
| 55 | int ret; |
| 56 | |
| 57 | ret = kstrtouint(buf, 0, &val); |
| 58 | if (ret) |
| 59 | return ret; |
| 60 | |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 61 | mutex_lock(&export->lock); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 62 | pwm_get_state(pwm, &state); |
| 63 | state.period = val; |
| 64 | ret = pwm_apply_state(pwm, &state); |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 65 | mutex_unlock(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 66 | |
| 67 | return ret ? : size; |
| 68 | } |
| 69 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 70 | static ssize_t duty_cycle_show(struct device *child, |
| 71 | struct device_attribute *attr, |
| 72 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 73 | { |
| 74 | const struct pwm_device *pwm = child_to_pwm_device(child); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 75 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 76 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 77 | pwm_get_state(pwm, &state); |
| 78 | |
| 79 | return sprintf(buf, "%u\n", state.duty_cycle); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 82 | static ssize_t duty_cycle_store(struct device *child, |
| 83 | struct device_attribute *attr, |
| 84 | const char *buf, size_t size) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 85 | { |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 86 | struct pwm_export *export = child_to_pwm_export(child); |
| 87 | struct pwm_device *pwm = export->pwm; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 88 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 89 | unsigned int val; |
| 90 | int ret; |
| 91 | |
| 92 | ret = kstrtouint(buf, 0, &val); |
| 93 | if (ret) |
| 94 | return ret; |
| 95 | |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 96 | mutex_lock(&export->lock); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 97 | pwm_get_state(pwm, &state); |
| 98 | state.duty_cycle = val; |
| 99 | ret = pwm_apply_state(pwm, &state); |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 100 | mutex_unlock(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 101 | |
| 102 | return ret ? : size; |
| 103 | } |
| 104 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 105 | static ssize_t enable_show(struct device *child, |
| 106 | struct device_attribute *attr, |
| 107 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 108 | { |
| 109 | const struct pwm_device *pwm = child_to_pwm_device(child); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 110 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 111 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 112 | pwm_get_state(pwm, &state); |
| 113 | |
| 114 | return sprintf(buf, "%d\n", state.enabled); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 117 | static ssize_t enable_store(struct device *child, |
| 118 | struct device_attribute *attr, |
| 119 | const char *buf, size_t size) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 120 | { |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 121 | struct pwm_export *export = child_to_pwm_export(child); |
| 122 | struct pwm_device *pwm = export->pwm; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 123 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 124 | int val, ret; |
| 125 | |
| 126 | ret = kstrtoint(buf, 0, &val); |
| 127 | if (ret) |
| 128 | return ret; |
| 129 | |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 130 | mutex_lock(&export->lock); |
| 131 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 132 | pwm_get_state(pwm, &state); |
| 133 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 134 | switch (val) { |
| 135 | case 0: |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 136 | state.enabled = false; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 137 | break; |
| 138 | case 1: |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 139 | state.enabled = true; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 140 | break; |
| 141 | default: |
| 142 | ret = -EINVAL; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 143 | goto unlock; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Ryo Kodama | fe5aa34 | 2016-06-08 10:58:23 +0900 | [diff] [blame] | 146 | ret = pwm_apply_state(pwm, &state); |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 147 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 148 | unlock: |
| 149 | mutex_unlock(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 150 | return ret ? : size; |
| 151 | } |
| 152 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 153 | static ssize_t polarity_show(struct device *child, |
| 154 | struct device_attribute *attr, |
| 155 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 156 | { |
| 157 | const struct pwm_device *pwm = child_to_pwm_device(child); |
Thierry Reding | 5a063d8 | 2015-07-20 09:56:05 +0200 | [diff] [blame] | 158 | const char *polarity = "unknown"; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 159 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 160 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 161 | pwm_get_state(pwm, &state); |
| 162 | |
| 163 | switch (state.polarity) { |
Thierry Reding | 5a063d8 | 2015-07-20 09:56:05 +0200 | [diff] [blame] | 164 | case PWM_POLARITY_NORMAL: |
| 165 | polarity = "normal"; |
| 166 | break; |
| 167 | |
| 168 | case PWM_POLARITY_INVERSED: |
| 169 | polarity = "inversed"; |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | return sprintf(buf, "%s\n", polarity); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 176 | static ssize_t polarity_store(struct device *child, |
| 177 | struct device_attribute *attr, |
| 178 | const char *buf, size_t size) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 179 | { |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 180 | struct pwm_export *export = child_to_pwm_export(child); |
| 181 | struct pwm_device *pwm = export->pwm; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 182 | enum pwm_polarity polarity; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 183 | struct pwm_state state; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 184 | int ret; |
| 185 | |
| 186 | if (sysfs_streq(buf, "normal")) |
| 187 | polarity = PWM_POLARITY_NORMAL; |
| 188 | else if (sysfs_streq(buf, "inversed")) |
| 189 | polarity = PWM_POLARITY_INVERSED; |
| 190 | else |
| 191 | return -EINVAL; |
| 192 | |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 193 | mutex_lock(&export->lock); |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 194 | pwm_get_state(pwm, &state); |
| 195 | state.polarity = polarity; |
| 196 | ret = pwm_apply_state(pwm, &state); |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 197 | mutex_unlock(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 198 | |
| 199 | return ret ? : size; |
| 200 | } |
| 201 | |
Lee Jones | 1a366fe | 2016-06-08 10:21:25 +0100 | [diff] [blame] | 202 | static ssize_t capture_show(struct device *child, |
| 203 | struct device_attribute *attr, |
| 204 | char *buf) |
| 205 | { |
| 206 | struct pwm_device *pwm = child_to_pwm_device(child); |
| 207 | struct pwm_capture result; |
| 208 | int ret; |
| 209 | |
| 210 | ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | |
| 214 | return sprintf(buf, "%u %u\n", result.period, result.duty_cycle); |
| 215 | } |
| 216 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 217 | static DEVICE_ATTR_RW(period); |
| 218 | static DEVICE_ATTR_RW(duty_cycle); |
| 219 | static DEVICE_ATTR_RW(enable); |
| 220 | static DEVICE_ATTR_RW(polarity); |
Lee Jones | 1a366fe | 2016-06-08 10:21:25 +0100 | [diff] [blame] | 221 | static DEVICE_ATTR_RO(capture); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 222 | |
| 223 | static struct attribute *pwm_attrs[] = { |
| 224 | &dev_attr_period.attr, |
| 225 | &dev_attr_duty_cycle.attr, |
| 226 | &dev_attr_enable.attr, |
| 227 | &dev_attr_polarity.attr, |
Lee Jones | 1a366fe | 2016-06-08 10:21:25 +0100 | [diff] [blame] | 228 | &dev_attr_capture.attr, |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 229 | NULL |
| 230 | }; |
Axel Lin | 6ca142a | 2013-12-04 18:29:53 +0800 | [diff] [blame] | 231 | ATTRIBUTE_GROUPS(pwm); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 232 | |
| 233 | static void pwm_export_release(struct device *child) |
| 234 | { |
| 235 | struct pwm_export *export = child_to_pwm_export(child); |
| 236 | |
| 237 | kfree(export); |
| 238 | } |
| 239 | |
| 240 | static int pwm_export_child(struct device *parent, struct pwm_device *pwm) |
| 241 | { |
| 242 | struct pwm_export *export; |
Fabrice Gasnier | 552c02e | 2018-10-01 15:23:57 +0200 | [diff] [blame] | 243 | char *pwm_prop[2]; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 244 | int ret; |
| 245 | |
| 246 | if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) |
| 247 | return -EBUSY; |
| 248 | |
| 249 | export = kzalloc(sizeof(*export), GFP_KERNEL); |
| 250 | if (!export) { |
| 251 | clear_bit(PWMF_EXPORTED, &pwm->flags); |
| 252 | return -ENOMEM; |
| 253 | } |
| 254 | |
| 255 | export->pwm = pwm; |
Boris BREZILLON | 459a25a | 2016-03-30 22:03:27 +0200 | [diff] [blame] | 256 | mutex_init(&export->lock); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 257 | |
| 258 | export->child.release = pwm_export_release; |
| 259 | export->child.parent = parent; |
| 260 | export->child.devt = MKDEV(0, 0); |
Axel Lin | 6ca142a | 2013-12-04 18:29:53 +0800 | [diff] [blame] | 261 | export->child.groups = pwm_groups; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 262 | dev_set_name(&export->child, "pwm%u", pwm->hwpwm); |
| 263 | |
| 264 | ret = device_register(&export->child); |
| 265 | if (ret) { |
| 266 | clear_bit(PWMF_EXPORTED, &pwm->flags); |
Arvind Yadav | 8bbf5b4 | 2018-03-08 15:27:37 +0530 | [diff] [blame] | 267 | put_device(&export->child); |
| 268 | export = NULL; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 269 | return ret; |
| 270 | } |
Fabrice Gasnier | 552c02e | 2018-10-01 15:23:57 +0200 | [diff] [blame] | 271 | pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); |
| 272 | pwm_prop[1] = NULL; |
| 273 | kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop); |
| 274 | kfree(pwm_prop[0]); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int pwm_unexport_match(struct device *child, void *data) |
| 280 | { |
| 281 | return child_to_pwm_device(child) == data; |
| 282 | } |
| 283 | |
| 284 | static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm) |
| 285 | { |
| 286 | struct device *child; |
Fabrice Gasnier | 552c02e | 2018-10-01 15:23:57 +0200 | [diff] [blame] | 287 | char *pwm_prop[2]; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 288 | |
| 289 | if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) |
| 290 | return -ENODEV; |
| 291 | |
| 292 | child = device_find_child(parent, pwm, pwm_unexport_match); |
| 293 | if (!child) |
| 294 | return -ENODEV; |
| 295 | |
Fabrice Gasnier | 552c02e | 2018-10-01 15:23:57 +0200 | [diff] [blame] | 296 | pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); |
| 297 | pwm_prop[1] = NULL; |
| 298 | kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop); |
| 299 | kfree(pwm_prop[0]); |
| 300 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 301 | /* for device_find_child() */ |
| 302 | put_device(child); |
| 303 | device_unregister(child); |
| 304 | pwm_put(pwm); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 309 | static ssize_t export_store(struct device *parent, |
| 310 | struct device_attribute *attr, |
| 311 | const char *buf, size_t len) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 312 | { |
| 313 | struct pwm_chip *chip = dev_get_drvdata(parent); |
| 314 | struct pwm_device *pwm; |
| 315 | unsigned int hwpwm; |
| 316 | int ret; |
| 317 | |
| 318 | ret = kstrtouint(buf, 0, &hwpwm); |
| 319 | if (ret < 0) |
| 320 | return ret; |
| 321 | |
| 322 | if (hwpwm >= chip->npwm) |
| 323 | return -ENODEV; |
| 324 | |
| 325 | pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); |
| 326 | if (IS_ERR(pwm)) |
| 327 | return PTR_ERR(pwm); |
| 328 | |
| 329 | ret = pwm_export_child(parent, pwm); |
| 330 | if (ret < 0) |
| 331 | pwm_put(pwm); |
| 332 | |
| 333 | return ret ? : len; |
| 334 | } |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 335 | static DEVICE_ATTR_WO(export); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 336 | |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 337 | static ssize_t unexport_store(struct device *parent, |
| 338 | struct device_attribute *attr, |
| 339 | const char *buf, size_t len) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 340 | { |
| 341 | struct pwm_chip *chip = dev_get_drvdata(parent); |
| 342 | unsigned int hwpwm; |
| 343 | int ret; |
| 344 | |
| 345 | ret = kstrtouint(buf, 0, &hwpwm); |
| 346 | if (ret < 0) |
| 347 | return ret; |
| 348 | |
| 349 | if (hwpwm >= chip->npwm) |
| 350 | return -ENODEV; |
| 351 | |
| 352 | ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]); |
| 353 | |
| 354 | return ret ? : len; |
| 355 | } |
Olliver Schinagl | 65cdc69 | 2015-10-26 22:32:37 +0100 | [diff] [blame] | 356 | static DEVICE_ATTR_WO(unexport); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 357 | |
Greg Kroah-Hartman | 9da0175 | 2013-07-24 15:05:39 -0700 | [diff] [blame] | 358 | static ssize_t npwm_show(struct device *parent, struct device_attribute *attr, |
| 359 | char *buf) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 360 | { |
| 361 | const struct pwm_chip *chip = dev_get_drvdata(parent); |
| 362 | |
| 363 | return sprintf(buf, "%u\n", chip->npwm); |
| 364 | } |
Greg Kroah-Hartman | 9da0175 | 2013-07-24 15:05:39 -0700 | [diff] [blame] | 365 | static DEVICE_ATTR_RO(npwm); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 366 | |
Greg Kroah-Hartman | 9da0175 | 2013-07-24 15:05:39 -0700 | [diff] [blame] | 367 | static struct attribute *pwm_chip_attrs[] = { |
| 368 | &dev_attr_export.attr, |
| 369 | &dev_attr_unexport.attr, |
| 370 | &dev_attr_npwm.attr, |
| 371 | NULL, |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 372 | }; |
Greg Kroah-Hartman | 9da0175 | 2013-07-24 15:05:39 -0700 | [diff] [blame] | 373 | ATTRIBUTE_GROUPS(pwm_chip); |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 374 | |
| 375 | static struct class pwm_class = { |
Thierry Reding | 412820d | 2015-07-20 09:58:09 +0200 | [diff] [blame] | 376 | .name = "pwm", |
| 377 | .owner = THIS_MODULE, |
| 378 | .dev_groups = pwm_chip_groups, |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | static int pwmchip_sysfs_match(struct device *parent, const void *data) |
| 382 | { |
| 383 | return dev_get_drvdata(parent) == data; |
| 384 | } |
| 385 | |
| 386 | void pwmchip_sysfs_export(struct pwm_chip *chip) |
| 387 | { |
| 388 | struct device *parent; |
| 389 | |
| 390 | /* |
| 391 | * 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] | 392 | * the kernel it's just not exported. |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 393 | */ |
| 394 | parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip, |
| 395 | "pwmchip%d", chip->base); |
| 396 | if (IS_ERR(parent)) { |
| 397 | dev_warn(chip->dev, |
| 398 | "device_create failed for pwm_chip sysfs export\n"); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void pwmchip_sysfs_unexport(struct pwm_chip *chip) |
| 403 | { |
| 404 | struct device *parent; |
David Hsu | 0733424 | 2016-08-09 14:57:46 -0700 | [diff] [blame] | 405 | unsigned int i; |
| 406 | |
| 407 | parent = class_find_device(&pwm_class, NULL, chip, |
| 408 | pwmchip_sysfs_match); |
| 409 | if (!parent) |
| 410 | return; |
| 411 | |
| 412 | for (i = 0; i < chip->npwm; i++) { |
| 413 | struct pwm_device *pwm = &chip->pwms[i]; |
| 414 | |
| 415 | if (test_bit(PWMF_EXPORTED, &pwm->flags)) |
| 416 | pwm_unexport_child(parent, pwm); |
| 417 | } |
Johan Hovold | 0e1614a | 2016-11-01 11:46:39 +0100 | [diff] [blame] | 418 | |
| 419 | put_device(parent); |
Phong Hoang | 347ab94 | 2019-03-19 19:40:08 +0900 | [diff] [blame] | 420 | device_unregister(parent); |
David Hsu | 0733424 | 2016-08-09 14:57:46 -0700 | [diff] [blame] | 421 | } |
| 422 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 423 | static int __init pwm_sysfs_init(void) |
| 424 | { |
| 425 | return class_register(&pwm_class); |
| 426 | } |
| 427 | subsys_initcall(pwm_sysfs_init); |