1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A simple sysfs interface for the generic PWM framework
5 * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
7 * Based on previous work by Lars Poeschel <poeschel@lemonage.de>
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>
19 struct pwm_device *pwm;
23 static struct pwm_export *child_to_pwm_export(struct device *child)
25 return container_of(child, struct pwm_export, child);
28 static struct pwm_device *child_to_pwm_device(struct device *child)
30 struct pwm_export *export = child_to_pwm_export(child);
35 static ssize_t period_show(struct device *child,
36 struct device_attribute *attr,
39 const struct pwm_device *pwm = child_to_pwm_device(child);
40 struct pwm_state state;
42 pwm_get_state(pwm, &state);
44 return sprintf(buf, "%u\n", state.period);
47 static ssize_t period_store(struct device *child,
48 struct device_attribute *attr,
49 const char *buf, size_t size)
51 struct pwm_export *export = child_to_pwm_export(child);
52 struct pwm_device *pwm = export->pwm;
53 struct pwm_state state;
57 ret = kstrtouint(buf, 0, &val);
61 mutex_lock(&export->lock);
62 pwm_get_state(pwm, &state);
64 ret = pwm_apply_state(pwm, &state);
65 mutex_unlock(&export->lock);
70 static ssize_t duty_cycle_show(struct device *child,
71 struct device_attribute *attr,
74 const struct pwm_device *pwm = child_to_pwm_device(child);
75 struct pwm_state state;
77 pwm_get_state(pwm, &state);
79 return sprintf(buf, "%u\n", state.duty_cycle);
82 static ssize_t duty_cycle_store(struct device *child,
83 struct device_attribute *attr,
84 const char *buf, size_t size)
86 struct pwm_export *export = child_to_pwm_export(child);
87 struct pwm_device *pwm = export->pwm;
88 struct pwm_state state;
92 ret = kstrtouint(buf, 0, &val);
96 mutex_lock(&export->lock);
97 pwm_get_state(pwm, &state);
98 state.duty_cycle = val;
99 ret = pwm_apply_state(pwm, &state);
100 mutex_unlock(&export->lock);
105 static ssize_t enable_show(struct device *child,
106 struct device_attribute *attr,
109 const struct pwm_device *pwm = child_to_pwm_device(child);
110 struct pwm_state state;
112 pwm_get_state(pwm, &state);
114 return sprintf(buf, "%d\n", state.enabled);
117 static ssize_t enable_store(struct device *child,
118 struct device_attribute *attr,
119 const char *buf, size_t size)
121 struct pwm_export *export = child_to_pwm_export(child);
122 struct pwm_device *pwm = export->pwm;
123 struct pwm_state state;
126 ret = kstrtoint(buf, 0, &val);
130 mutex_lock(&export->lock);
132 pwm_get_state(pwm, &state);
136 state.enabled = false;
139 state.enabled = true;
146 ret = pwm_apply_state(pwm, &state);
149 mutex_unlock(&export->lock);
153 static ssize_t polarity_show(struct device *child,
154 struct device_attribute *attr,
157 const struct pwm_device *pwm = child_to_pwm_device(child);
158 const char *polarity = "unknown";
159 struct pwm_state state;
161 pwm_get_state(pwm, &state);
163 switch (state.polarity) {
164 case PWM_POLARITY_NORMAL:
168 case PWM_POLARITY_INVERSED:
169 polarity = "inversed";
173 return sprintf(buf, "%s\n", polarity);
176 static ssize_t polarity_store(struct device *child,
177 struct device_attribute *attr,
178 const char *buf, size_t size)
180 struct pwm_export *export = child_to_pwm_export(child);
181 struct pwm_device *pwm = export->pwm;
182 enum pwm_polarity polarity;
183 struct pwm_state state;
186 if (sysfs_streq(buf, "normal"))
187 polarity = PWM_POLARITY_NORMAL;
188 else if (sysfs_streq(buf, "inversed"))
189 polarity = PWM_POLARITY_INVERSED;
193 mutex_lock(&export->lock);
194 pwm_get_state(pwm, &state);
195 state.polarity = polarity;
196 ret = pwm_apply_state(pwm, &state);
197 mutex_unlock(&export->lock);
202 static ssize_t capture_show(struct device *child,
203 struct device_attribute *attr,
206 struct pwm_device *pwm = child_to_pwm_device(child);
207 struct pwm_capture result;
210 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
214 return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
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);
221 static DEVICE_ATTR_RO(capture);
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,
228 &dev_attr_capture.attr,
231 ATTRIBUTE_GROUPS(pwm);
233 static void pwm_export_release(struct device *child)
235 struct pwm_export *export = child_to_pwm_export(child);
240 static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
242 struct pwm_export *export;
246 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
249 export = kzalloc(sizeof(*export), GFP_KERNEL);
251 clear_bit(PWMF_EXPORTED, &pwm->flags);
256 mutex_init(&export->lock);
258 export->child.release = pwm_export_release;
259 export->child.parent = parent;
260 export->child.devt = MKDEV(0, 0);
261 export->child.groups = pwm_groups;
262 dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
264 ret = device_register(&export->child);
266 clear_bit(PWMF_EXPORTED, &pwm->flags);
267 put_device(&export->child);
271 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
273 kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
279 static int pwm_unexport_match(struct device *child, void *data)
281 return child_to_pwm_device(child) == data;
284 static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
286 struct device *child;
289 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
292 child = device_find_child(parent, pwm, pwm_unexport_match);
296 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
298 kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
301 /* for device_find_child() */
303 device_unregister(child);
309 static ssize_t export_store(struct device *parent,
310 struct device_attribute *attr,
311 const char *buf, size_t len)
313 struct pwm_chip *chip = dev_get_drvdata(parent);
314 struct pwm_device *pwm;
318 ret = kstrtouint(buf, 0, &hwpwm);
322 if (hwpwm >= chip->npwm)
325 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
329 ret = pwm_export_child(parent, pwm);
335 static DEVICE_ATTR_WO(export);
337 static ssize_t unexport_store(struct device *parent,
338 struct device_attribute *attr,
339 const char *buf, size_t len)
341 struct pwm_chip *chip = dev_get_drvdata(parent);
345 ret = kstrtouint(buf, 0, &hwpwm);
349 if (hwpwm >= chip->npwm)
352 ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
356 static DEVICE_ATTR_WO(unexport);
358 static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
361 const struct pwm_chip *chip = dev_get_drvdata(parent);
363 return sprintf(buf, "%u\n", chip->npwm);
365 static DEVICE_ATTR_RO(npwm);
367 static struct attribute *pwm_chip_attrs[] = {
368 &dev_attr_export.attr,
369 &dev_attr_unexport.attr,
373 ATTRIBUTE_GROUPS(pwm_chip);
375 static struct class pwm_class = {
377 .owner = THIS_MODULE,
378 .dev_groups = pwm_chip_groups,
381 static int pwmchip_sysfs_match(struct device *parent, const void *data)
383 return dev_get_drvdata(parent) == data;
386 void pwmchip_sysfs_export(struct pwm_chip *chip)
388 struct device *parent;
391 * If device_create() fails the pwm_chip is still usable by
392 * the kernel it's just not exported.
394 parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
395 "pwmchip%d", chip->base);
396 if (IS_ERR(parent)) {
398 "device_create failed for pwm_chip sysfs export\n");
402 void pwmchip_sysfs_unexport(struct pwm_chip *chip)
404 struct device *parent;
407 parent = class_find_device(&pwm_class, NULL, chip,
408 pwmchip_sysfs_match);
412 for (i = 0; i < chip->npwm; i++) {
413 struct pwm_device *pwm = &chip->pwms[i];
415 if (test_bit(PWMF_EXPORTED, &pwm->flags))
416 pwm_unexport_child(parent, pwm);
420 device_unregister(parent);
423 static int __init pwm_sysfs_init(void)
425 return class_register(&pwm_class);
427 subsys_initcall(pwm_sysfs_init);