aboutsummaryrefslogtreecommitdiffstats
path: root/sysfs.c
blob: ee7346ca550244fd1675e0e5ad34fef1e9d0bdc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* SPDX-License-Identifier: LGPL-2.1-only */
/* SPDX-FileCopyrightText: 2023 Uwe Kleine-König <u.kleine-koenig@pengutronix.de> */

#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <pwm.h>

#include "pwm-internal.h"

struct pwm_sysfs {
	struct pwm pwm;
	int dirfd;

	/* .state tracks the state assuming the PWM is enabled. */
	struct pwm_state state;
	bool enabled;
	bool cache_valid;
};

struct pwm_chip_sysfs {
	struct pwm_chip chip;
	int dirfd;
	struct pwm_sysfs *pwms[];
};

static void pwm_chip_sysfs_close(struct pwm_chip *chip)
{
	unsigned int i;
	struct pwm_chip_sysfs *chip_sysfs = container_of(chip, struct pwm_chip_sysfs, chip);

	for (i = 0; i < chip->npwm; ++i) {
		if (chip_sysfs->pwms[i]) {
			close(chip_sysfs->pwms[i]->dirfd);
			free(chip_sysfs->pwms[i]);
		}
	}
	free(chip_sysfs);
}

static struct pwm *pwm_chip_sysfs_get_pwm(struct pwm_chip *chip,
					  unsigned int offset)
{
	struct pwm_chip_sysfs *chip_sysfs = container_of(chip, struct pwm_chip_sysfs, chip);
	struct pwm_sysfs *pwm_sysfs;
	struct pwm *pwm;
#if UINT_MAX <= 4294967295U
	/* force a compiler error if buffer gets too small */
	char pwmX[14];
#endif
	int fd;

	if (chip_sysfs->pwms[offset])
		return &chip_sysfs->pwms[offset]->pwm;

	pwm_sysfs = calloc(1, sizeof(*pwm_sysfs));
	if (!pwm_sysfs)
		return NULL;
	pwm = &pwm_sysfs->pwm;

	pwm->chip = chip;

	sprintf(pwmX, "pwm%u", offset);

	pwm_sysfs->dirfd = openat(chip_sysfs->dirfd, pwmX,
				  O_PATH | O_CLOEXEC);
	if (pwm_sysfs->dirfd < 0 && errno == ENOENT) {
		int ret;

		fd = openat(chip_sysfs->dirfd, "export", O_WRONLY | O_CLOEXEC);
		if (fd < 0) {
			free(pwm_sysfs);
			return NULL;
		}

		ret = dprintf(fd, "%d\n", offset);
		if (ret < 0) {
			close(fd);
			free(pwm_sysfs);
			return NULL;
		}

		ret = close(fd);
		if (ret < 0) {
			free(pwm_sysfs);
			return NULL;
		}

		pwm_sysfs->dirfd = openat(chip_sysfs->dirfd, pwmX,
					  O_PATH | O_CLOEXEC);
	}

	if (pwm_sysfs->dirfd < 0) {
		free(pwm_sysfs);
		return NULL;
	}

	chip_sysfs->pwms[offset] = pwm_sysfs;

	pwm_sysfs->cache_valid = false;

	return pwm;
}

static int pwm_chip_sysfs_write_prop(const struct pwm_sysfs *pwm_sysfs,
					 char *propname,
					 const char *restrict format, ...)
{
	int fd;
	va_list ap;
	int ret;

	fd = openat(pwm_sysfs->dirfd, propname, O_WRONLY | O_CLOEXEC);
	if (fd < 0)
		return -1;

	va_start(ap, format);

	ret = vdprintf(fd, format, ap);

	va_end(ap);

	if (ret < 0) {
		int saved_errno = errno;

		close(fd);

		errno = saved_errno;

		return ret;
	}
	return close(fd);
}

static int pwm_chip_sysfs_apply_state(struct pwm *pwm,
				      const struct pwm_state *state)
{
	struct pwm_sysfs *pwm_sysfs = container_of(pwm, struct pwm_sysfs, pwm);
	int ret;

	/* period = 0 is interpreted as disabled */
	if (state->period) {
		if (!pwm_sysfs->cache_valid ||
		    (state->duty_offset < state->period - state->duty_cycle) !=
		     (pwm_sysfs->state.duty_offset < pwm_sysfs->state.period - pwm_sysfs->state.duty_cycle)) {
			if (state->duty_offset < state->period - state->duty_cycle) {
				ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "polarity", "normal\n");
				if (ret)
					return ret;

				pwm_sysfs->state.duty_offset = 0;
			} else {
				ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "polarity", "inversed\n");
				if (ret)
					return ret;

				pwm_sysfs->state.duty_offset = state->period - state->duty_cycle;
			}
		}

		/*
		 * Ensure that we never hit duty_cycle > period. As updating
		 * period and duty_cycle cannot be done in a single step write
		 * period first if period increases and write duty_cycle first
		 * if period decreases.
		 */
		if (!pwm_sysfs->cache_valid ||
		    pwm_sysfs->state.period <= state->period) {
			if (!pwm_sysfs->cache_valid ||
			    pwm_sysfs->state.period != state->period) {
				ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "period",
								"%" PRIu64 "\n", state->period);
				if (ret)
					return ret;
				pwm_sysfs->state.period = state->period;
			}

			if (!pwm_sysfs->cache_valid ||
			    pwm_sysfs->state.duty_cycle != state->duty_cycle) {
				ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "duty_cycle",
								"%" PRIu64 "\n", state->duty_cycle);
				if (ret)
					return ret;
				pwm_sysfs->state.duty_cycle = state->duty_cycle;
			}
		} else {
			if (!!pwm_sysfs->cache_valid ||
			    pwm_sysfs->state.duty_cycle != state->duty_cycle) {
				ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "duty_cycle",
								"%" PRIu64 "\n", state->duty_cycle);
				if (ret)
					return ret;
				pwm_sysfs->state.duty_cycle = state->duty_cycle;
			}

			/*
			 * It's already known that
			 * pwm_sysfs->state.period > state->period, so
			 * no need to check for pwm_sysfs->state.period != state->period
			 */
			ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "period",
							"%" PRIu64 "\n", state->period);
			if (ret)
				return ret;
			pwm_sysfs->state.period = state->period;
		}

		if (!pwm_sysfs->cache_valid || !pwm_sysfs->enabled) {
			ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "enable", "1\n");
			if (ret)
				return ret;
			pwm_sysfs->enabled = true;
		}
		pwm_sysfs->cache_valid = true;
	} else {
		if (!pwm_sysfs->enabled) {
			ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "enable", "0\n");
			if (ret)
				return ret;
			pwm_sysfs->enabled = false;
		}
	}

	return 0;
}

static int pwm_chip_sysfs_get_state(struct pwm *pwm, struct pwm_state *state)
{
	(void)pwm; (void)state;
	errno = EIO;
	return -1;
}

struct pwm_chip *pwm_chip_sysfs_open_by_number(unsigned int num)
{
	struct pwm_chip_sysfs *chip_sysfs;
	struct pwm_chip *chip;
	long npwm;
	int dirfd, fd;
	ssize_t ret;
	char buf[128];

	ret = snprintf(buf, sizeof(buf), "/sys/class/pwm/pwmchip%d", num);
	if (ret < 0)
		return NULL;
	if ((size_t)ret >= sizeof(buf)) {
		/* huh */
		errno = EINVAL;
		return NULL;
	}

	dirfd = open(buf, O_PATH | O_CLOEXEC);
	if (dirfd < 0)
		return NULL;

	fd = openat(dirfd, "npwm", O_RDONLY | O_CLOEXEC);
	if (fd < 0) {
		close(dirfd);
		return NULL;
	}

	ret = read(fd, buf, sizeof(buf));
	close(fd);
	if (ret < 0) {
		close(dirfd);
		return NULL;
	}

	if (ret == 0 || buf[ret - 1] != '\n') {
		close(dirfd);
		errno = EINVAL;
		return NULL;
	}

	errno = 0;
	npwm = strtol(buf, NULL, 10);

	if (errno) {
		close(dirfd);
		return NULL;
	}

	if (npwm < 0 || npwm > 128) {
		close(dirfd);
		errno = EINVAL;
		return NULL;
	}

	chip_sysfs = calloc(1, sizeof(*chip_sysfs) + npwm * sizeof(chip_sysfs->pwms[0]));
	if (!chip_sysfs) {
		close(dirfd);
		return NULL;
	}

	chip = &chip_sysfs->chip;
	chip->close = pwm_chip_sysfs_close;
	chip->get_pwm = pwm_chip_sysfs_get_pwm;
	chip->apply_state = pwm_chip_sysfs_apply_state;
	chip->get_state = pwm_chip_sysfs_get_state;
	chip->npwm = npwm;

	chip_sysfs->dirfd = dirfd;

	return chip;
}