2 * A simple sysfs interface for the generic PWM framework
4 * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
6 * Based on previous work by Lars Poeschel <poeschel@lemonage.de>
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)
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.
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>
28 struct pwm_device *pwm;
32 static struct pwm_export *child_to_pwm_export(struct device *child)
34 return container_of(child, struct pwm_export, child);
37 static struct pwm_device *child_to_pwm_device(struct device *child)
39 struct pwm_export *export = child_to_pwm_export(child);
44 static ssize_t period_show(struct device *child,
45 struct device_attribute *attr,
48 const struct pwm_device *pwm = child_to_pwm_device(child);
50 return sprintf(buf, "%u\n", pwm_get_period(pwm));
53 static ssize_t period_store(struct device *child,
54 struct device_attribute *attr,
55 const char *buf, size_t size)
57 struct pwm_export *export = child_to_pwm_export(child);
58 struct pwm_device *pwm = export->pwm;
62 ret = kstrtouint(buf, 0, &val);
66 mutex_lock(&export->lock);
67 ret = pwm_config(pwm, pwm_get_duty_cycle(pwm), val);
68 mutex_unlock(&export->lock);
73 static ssize_t duty_cycle_show(struct device *child,
74 struct device_attribute *attr,
77 const struct pwm_device *pwm = child_to_pwm_device(child);
79 return sprintf(buf, "%u\n", pwm_get_duty_cycle(pwm));
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;
91 ret = kstrtouint(buf, 0, &val);
95 mutex_lock(&export->lock);
96 ret = pwm_config(pwm, val, pwm_get_period(pwm));
97 mutex_unlock(&export->lock);
102 static ssize_t enable_show(struct device *child,
103 struct device_attribute *attr,
106 const struct pwm_device *pwm = child_to_pwm_device(child);
108 return sprintf(buf, "%d\n", pwm_is_enabled(pwm));
111 static ssize_t enable_store(struct device *child,
112 struct device_attribute *attr,
113 const char *buf, size_t size)
115 struct pwm_export *export = child_to_pwm_export(child);
116 struct pwm_device *pwm = export->pwm;
119 ret = kstrtoint(buf, 0, &val);
123 mutex_lock(&export->lock);
130 ret = pwm_enable(pwm);
137 mutex_unlock(&export->lock);
142 static ssize_t polarity_show(struct device *child,
143 struct device_attribute *attr,
146 const struct pwm_device *pwm = child_to_pwm_device(child);
147 const char *polarity = "unknown";
149 switch (pwm_get_polarity(pwm)) {
150 case PWM_POLARITY_NORMAL:
154 case PWM_POLARITY_INVERSED:
155 polarity = "inversed";
159 return sprintf(buf, "%s\n", polarity);
162 static ssize_t polarity_store(struct device *child,
163 struct device_attribute *attr,
164 const char *buf, size_t size)
166 struct pwm_export *export = child_to_pwm_export(child);
167 struct pwm_device *pwm = export->pwm;
168 enum pwm_polarity polarity;
171 if (sysfs_streq(buf, "normal"))
172 polarity = PWM_POLARITY_NORMAL;
173 else if (sysfs_streq(buf, "inversed"))
174 polarity = PWM_POLARITY_INVERSED;
178 mutex_lock(&export->lock);
179 ret = pwm_set_polarity(pwm, polarity);
180 mutex_unlock(&export->lock);
185 static DEVICE_ATTR_RW(period);
186 static DEVICE_ATTR_RW(duty_cycle);
187 static DEVICE_ATTR_RW(enable);
188 static DEVICE_ATTR_RW(polarity);
190 static struct attribute *pwm_attrs[] = {
191 &dev_attr_period.attr,
192 &dev_attr_duty_cycle.attr,
193 &dev_attr_enable.attr,
194 &dev_attr_polarity.attr,
197 ATTRIBUTE_GROUPS(pwm);
199 static void pwm_export_release(struct device *child)
201 struct pwm_export *export = child_to_pwm_export(child);
206 static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
208 struct pwm_export *export;
211 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
214 export = kzalloc(sizeof(*export), GFP_KERNEL);
216 clear_bit(PWMF_EXPORTED, &pwm->flags);
221 mutex_init(&export->lock);
223 export->child.release = pwm_export_release;
224 export->child.parent = parent;
225 export->child.devt = MKDEV(0, 0);
226 export->child.groups = pwm_groups;
227 dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
229 ret = device_register(&export->child);
231 clear_bit(PWMF_EXPORTED, &pwm->flags);
239 static int pwm_unexport_match(struct device *child, void *data)
241 return child_to_pwm_device(child) == data;
244 static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
246 struct device *child;
248 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
251 child = device_find_child(parent, pwm, pwm_unexport_match);
255 /* for device_find_child() */
257 device_unregister(child);
263 static ssize_t export_store(struct device *parent,
264 struct device_attribute *attr,
265 const char *buf, size_t len)
267 struct pwm_chip *chip = dev_get_drvdata(parent);
268 struct pwm_device *pwm;
272 ret = kstrtouint(buf, 0, &hwpwm);
276 if (hwpwm >= chip->npwm)
279 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
283 ret = pwm_export_child(parent, pwm);
289 static DEVICE_ATTR_WO(export);
291 static ssize_t unexport_store(struct device *parent,
292 struct device_attribute *attr,
293 const char *buf, size_t len)
295 struct pwm_chip *chip = dev_get_drvdata(parent);
299 ret = kstrtouint(buf, 0, &hwpwm);
303 if (hwpwm >= chip->npwm)
306 ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
310 static DEVICE_ATTR_WO(unexport);
312 static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
315 const struct pwm_chip *chip = dev_get_drvdata(parent);
317 return sprintf(buf, "%u\n", chip->npwm);
319 static DEVICE_ATTR_RO(npwm);
321 static struct attribute *pwm_chip_attrs[] = {
322 &dev_attr_export.attr,
323 &dev_attr_unexport.attr,
327 ATTRIBUTE_GROUPS(pwm_chip);
329 static struct class pwm_class = {
331 .owner = THIS_MODULE,
332 .dev_groups = pwm_chip_groups,
335 static int pwmchip_sysfs_match(struct device *parent, const void *data)
337 return dev_get_drvdata(parent) == data;
340 void pwmchip_sysfs_export(struct pwm_chip *chip)
342 struct device *parent;
345 * If device_create() fails the pwm_chip is still usable by
346 * the kernel its just not exported.
348 parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
349 "pwmchip%d", chip->base);
350 if (IS_ERR(parent)) {
352 "device_create failed for pwm_chip sysfs export\n");
356 void pwmchip_sysfs_unexport(struct pwm_chip *chip)
358 struct device *parent;
360 parent = class_find_device(&pwm_class, NULL, chip,
361 pwmchip_sysfs_match);
363 /* for class_find_device() */
365 device_unregister(parent);
369 void pwmchip_sysfs_unexport_children(struct pwm_chip *chip)
371 struct device *parent;
374 parent = class_find_device(&pwm_class, NULL, chip,
375 pwmchip_sysfs_match);
379 for (i = 0; i < chip->npwm; i++) {
380 struct pwm_device *pwm = &chip->pwms[i];
382 if (test_bit(PWMF_EXPORTED, &pwm->flags))
383 pwm_unexport_child(parent, pwm);
389 static int __init pwm_sysfs_init(void)
391 return class_register(&pwm_class);
393 subsys_initcall(pwm_sysfs_init);