2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include "maa_internal.h"
33 #define SYSFS_PWM "/sys/class/pwm"
36 * A structure representing a PWM pin
40 int pin; /**< the pin number, as known to the os. */
41 int chipid; /**< the chip id, which the pwm resides */
42 int duty_fp; /**< File pointer to duty file */
43 maa_boolean_t owner; /**< Owner of pwm context*/
48 maa_pwm_setup_duty_fp(maa_pwm_context dev)
51 snprintf(bu,MAX_SIZE, "/sys/class/pwm/pwmchip%d/pwm%d/duty_cycle", dev->chipid, dev->pin);
53 dev->duty_fp = open(bu, O_RDWR);
54 if (dev->duty_fp == -1) {
61 maa_pwm_write_period(maa_pwm_context dev, int period)
64 snprintf(bu,MAX_SIZE ,"/sys/class/pwm/pwmchip%d/pwm%d/period", dev->chipid, dev->pin);
66 int period_f = open(bu, O_RDWR);
68 fprintf(stderr, "Failed to open period for writing!\n");
69 return MAA_ERROR_INVALID_RESOURCE;
72 int length = snprintf(out, MAX_SIZE, "%d", period);
73 if (write(period_f, out, length*sizeof(char)) == -1) {
75 return MAA_ERROR_INVALID_RESOURCE;
83 maa_pwm_write_duty(maa_pwm_context dev, int duty)
85 if (dev->duty_fp == -1) {
86 maa_pwm_setup_duty_fp(dev);
89 int length = sprintf(bu, "%d", duty);
90 if (write(dev->duty_fp, bu, length * sizeof(char)) == -1)
91 return MAA_ERROR_INVALID_RESOURCE;
96 maa_pwm_get_period(maa_pwm_context dev)
99 char output[MAX_SIZE];
100 snprintf(bu,MAX_SIZE, "/sys/class/pwm/pwmchip%d/pwm%d/period", dev->chipid, dev->pin);
102 int period_f = open(bu, O_RDWR);
103 if (period_f == -1) {
104 fprintf(stderr, "Failed to open period for reading!\n");
107 off_t size = lseek(period_f, 0, SEEK_END);
108 lseek(period_f, 0, SEEK_SET);
110 read(period_f, output, size + 1);
112 int ret = strtol(output, NULL, 10);
118 maa_pwm_get_duty(maa_pwm_context dev)
120 if (dev->duty_fp == -1) {
121 maa_pwm_setup_duty_fp(dev);
123 lseek(dev->duty_fp, 0, SEEK_SET);
125 off_t size = lseek(dev->duty_fp, 0, SEEK_END);
126 lseek(dev->duty_fp, 0, SEEK_SET);
127 char output[MAX_SIZE];
128 read(dev->duty_fp, output, size+1);
130 int ret = strtol(output, NULL, 10);
135 maa_pwm_init(int pin) {
136 maa_pin_t* pinm = maa_setup_pwm(pin);
139 int chip = pinm->parent_id;
140 int pinn = pinm->pinmap;
142 return maa_pwm_init_raw(chip,pinn);
146 maa_pwm_init_raw(int chipin, int pin)
148 maa_pwm_context dev = (maa_pwm_context) malloc(sizeof(struct _pwm));
152 dev->chipid = chipin;
155 char directory[MAX_SIZE];
156 snprintf(directory, MAX_SIZE, SYSFS_PWM "/pwmchip%d/pwm%d", dev->chipid, dev->pin);
158 if (stat(directory, &dir) == 0 && S_ISDIR(dir.st_mode)) {
159 fprintf(stderr, "PWM Pin already exporting, continuing.\n");
160 dev->owner = 0; // Not Owner
162 char buffer[MAX_SIZE];
163 snprintf(buffer, MAX_SIZE, "/sys/class/pwm/pwmchip%d/export", dev->chipid);
164 int export_f = open(buffer, O_WRONLY);
165 if (export_f == -1) {
166 fprintf(stderr, "Failed to open export for writing!\n");
172 int size = snprintf(out, MAX_SIZE, "%d", dev->pin);
173 if (write(export_f, out, size*sizeof(char)) == -1) {
174 fprintf(stderr, "Failed to write to export! Potentially already enabled\n");
181 maa_pwm_setup_duty_fp(dev);
186 maa_pwm_write(maa_pwm_context dev, float percentage)
188 return maa_pwm_write_duty(dev, percentage * maa_pwm_get_period(dev));
192 maa_pwm_read(maa_pwm_context dev)
194 float output = maa_pwm_get_duty(dev) / (float) maa_pwm_get_period(dev);
199 maa_pwm_period(maa_pwm_context dev, float seconds)
201 return maa_pwm_period_ms(dev, seconds*1000);
205 maa_pwm_period_ms(maa_pwm_context dev, int ms)
207 return maa_pwm_period_us(dev, ms*1000);
211 maa_pwm_period_us(maa_pwm_context dev, int us)
213 return maa_pwm_write_period(dev, us*1000);
217 maa_pwm_pulsewidth(maa_pwm_context dev, float seconds)
219 return maa_pwm_pulsewidth_ms(dev, seconds*1000);
223 maa_pwm_pulsewidth_ms(maa_pwm_context dev, int ms)
225 return maa_pwm_pulsewidth_us(dev, ms*1000);
229 maa_pwm_pulsewidth_us(maa_pwm_context dev, int us)
231 return maa_pwm_write_duty(dev, us*1000);
235 maa_pwm_enable(maa_pwm_context dev, int enable)
244 snprintf(bu,MAX_SIZE, "/sys/class/pwm/pwmchip%d/pwm%d/enable", dev->chipid, dev->pin);
246 int enable_f = open(bu, O_RDWR);
248 if (enable_f == -1) {
249 fprintf(stderr, "Failed to open enable for writing!\n");
250 return MAA_ERROR_INVALID_RESOURCE;
253 int size = snprintf(out, sizeof(out), "%d", enable);
254 if (write(enable_f, out, size * sizeof(char)) == -1) {
255 fprintf(stderr, "Failed to write to enable!\n");
257 return MAA_ERROR_INVALID_RESOURCE;
264 maa_pwm_unexport_force(maa_pwm_context dev)
266 char filepath[MAX_SIZE];
267 snprintf(filepath, MAX_SIZE, "/sys/class/pwm/pwmchip%d/unexport", dev->chipid);
269 int unexport_f = open(filepath, O_WRONLY);
270 if (unexport_f == -1) {
271 fprintf(stderr, "Failed to open unexport for writing!\n");
272 return MAA_ERROR_INVALID_RESOURCE;
276 int size = snprintf(out, MAX_SIZE, "%d", dev->pin);
277 if (write(unexport_f, out, size*sizeof(char)) == -1) {
278 fprintf(stderr, "Failed to write to unexport!\n");
280 return MAA_ERROR_INVALID_RESOURCE;
288 maa_pwm_unexport(maa_pwm_context dev)
290 maa_pwm_enable(dev, 0);
292 return maa_pwm_unexport_force(dev);
294 return MAA_ERROR_INVALID_RESOURCE;
298 maa_pwm_close(maa_pwm_context dev)
300 maa_pwm_unexport(dev);
306 maa_pwm_owner(maa_pwm_context dev, maa_boolean_t owner_new)
309 return MAA_ERROR_INVALID_RESOURCE;
310 dev->owner = owner_new;