xref: /linux/drivers/ptp/ptp_sysfs.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PTP 1588 clock support - sysfs interface.
4  *
5  * Copyright (C) 2010 OMICRON electronics GmbH
6  * Copyright 2021 NXP
7  */
8 #include <linux/capability.h>
9 #include <linux/slab.h>
10 
11 #include "ptp_private.h"
12 
13 static ssize_t clock_name_show(struct device *dev,
14 			       struct device_attribute *attr, char *page)
15 {
16 	struct ptp_clock *ptp = dev_get_drvdata(dev);
17 	return sysfs_emit(page, "%s\n", ptp->info->name);
18 }
19 static DEVICE_ATTR_RO(clock_name);
20 
21 static ssize_t max_phase_adjustment_show(struct device *dev,
22 					 struct device_attribute *attr,
23 					 char *page)
24 {
25 	struct ptp_clock *ptp = dev_get_drvdata(dev);
26 
27 	return sysfs_emit(page, "%d\n", ptp->info->getmaxphase(ptp->info));
28 }
29 static DEVICE_ATTR_RO(max_phase_adjustment);
30 
31 #define PTP_SHOW_INT(name, var)						\
32 static ssize_t var##_show(struct device *dev,				\
33 			   struct device_attribute *attr, char *page)	\
34 {									\
35 	struct ptp_clock *ptp = dev_get_drvdata(dev);			\
36 	return sysfs_emit(page, "%d\n", ptp->info->var);	\
37 }									\
38 static DEVICE_ATTR(name, 0444, var##_show, NULL);
39 
40 PTP_SHOW_INT(max_adjustment, max_adj);
41 PTP_SHOW_INT(n_alarms, n_alarm);
42 PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
43 PTP_SHOW_INT(n_periodic_outputs, n_per_out);
44 PTP_SHOW_INT(n_programmable_pins, n_pins);
45 PTP_SHOW_INT(pps_available, pps);
46 
47 static ssize_t extts_enable_store(struct device *dev,
48 				  struct device_attribute *attr,
49 				  const char *buf, size_t count)
50 {
51 	struct ptp_clock *ptp = dev_get_drvdata(dev);
52 	struct ptp_clock_info *ops = ptp->info;
53 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
54 	int cnt, enable;
55 	int err = -EINVAL;
56 
57 	cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
58 	if (cnt != 2)
59 		goto out;
60 	if (req.extts.index >= ops->n_ext_ts)
61 		goto out;
62 
63 	err = ops->enable(ops, &req, enable ? 1 : 0);
64 	if (err)
65 		goto out;
66 
67 	return count;
68 out:
69 	return err;
70 }
71 static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
72 
73 static ssize_t extts_fifo_show(struct device *dev,
74 			       struct device_attribute *attr, char *page)
75 {
76 	struct ptp_clock *ptp = dev_get_drvdata(dev);
77 	struct timestamp_event_queue *queue;
78 	struct ptp_extts_event event;
79 	unsigned long flags;
80 	size_t qcnt;
81 	int cnt = 0;
82 
83 	cnt = list_count_nodes(&ptp->tsevqs);
84 	if (cnt <= 0)
85 		goto out;
86 
87 	/* The sysfs fifo will always draw from the fist queue */
88 	queue = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue,
89 				 qlist);
90 
91 	memset(&event, 0, sizeof(event));
92 	spin_lock_irqsave(&queue->lock, flags);
93 	qcnt = queue_cnt(queue);
94 	if (qcnt) {
95 		event = queue->buf[queue->head];
96 		/* Paired with READ_ONCE() in queue_cnt() */
97 		WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
98 	}
99 	spin_unlock_irqrestore(&queue->lock, flags);
100 
101 	if (!qcnt)
102 		goto out;
103 
104 	cnt = sysfs_emit(page, "%u %lld %u\n",
105 			 event.index, event.t.sec, event.t.nsec);
106 out:
107 	return cnt;
108 }
109 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
110 
111 static ssize_t period_store(struct device *dev,
112 			    struct device_attribute *attr,
113 			    const char *buf, size_t count)
114 {
115 	struct ptp_clock *ptp = dev_get_drvdata(dev);
116 	struct ptp_clock_info *ops = ptp->info;
117 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
118 	int cnt, enable, err = -EINVAL;
119 
120 	cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
121 		     &req.perout.start.sec, &req.perout.start.nsec,
122 		     &req.perout.period.sec, &req.perout.period.nsec);
123 	if (cnt != 5)
124 		goto out;
125 	if (req.perout.index >= ops->n_per_out)
126 		goto out;
127 
128 	enable = req.perout.period.sec || req.perout.period.nsec;
129 	err = ops->enable(ops, &req, enable);
130 	if (err)
131 		goto out;
132 
133 	return count;
134 out:
135 	return err;
136 }
137 static DEVICE_ATTR(period, 0220, NULL, period_store);
138 
139 static ssize_t pps_enable_store(struct device *dev,
140 				struct device_attribute *attr,
141 				const char *buf, size_t count)
142 {
143 	struct ptp_clock *ptp = dev_get_drvdata(dev);
144 	struct ptp_clock_info *ops = ptp->info;
145 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
146 	int cnt, enable;
147 	int err = -EINVAL;
148 
149 	if (!capable(CAP_SYS_TIME))
150 		return -EPERM;
151 
152 	cnt = sscanf(buf, "%d", &enable);
153 	if (cnt != 1)
154 		goto out;
155 
156 	err = ops->enable(ops, &req, enable ? 1 : 0);
157 	if (err)
158 		goto out;
159 
160 	return count;
161 out:
162 	return err;
163 }
164 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
165 
166 static int unregister_vclock(struct device *dev, void *data)
167 {
168 	struct ptp_clock *ptp = dev_get_drvdata(dev);
169 	struct ptp_clock_info *info = ptp->info;
170 	struct ptp_vclock *vclock;
171 	u32 *num = data;
172 
173 	vclock = info_to_vclock(info);
174 	dev_info(dev->parent, "delete virtual clock ptp%d\n",
175 		 vclock->clock->index);
176 
177 	ptp_vclock_unregister(vclock);
178 	(*num)--;
179 
180 	/* For break. Not error. */
181 	if (*num == 0)
182 		return -EINVAL;
183 
184 	return 0;
185 }
186 
187 static ssize_t n_vclocks_show(struct device *dev,
188 			      struct device_attribute *attr, char *page)
189 {
190 	struct ptp_clock *ptp = dev_get_drvdata(dev);
191 	ssize_t size;
192 
193 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
194 		return -ERESTARTSYS;
195 
196 	size = sysfs_emit(page, "%u\n", ptp->n_vclocks);
197 
198 	mutex_unlock(&ptp->n_vclocks_mux);
199 
200 	return size;
201 }
202 
203 static ssize_t n_vclocks_store(struct device *dev,
204 			       struct device_attribute *attr,
205 			       const char *buf, size_t count)
206 {
207 	struct ptp_clock *ptp = dev_get_drvdata(dev);
208 	struct ptp_vclock *vclock;
209 	int err = -EINVAL;
210 	u32 num, i;
211 
212 	if (kstrtou32(buf, 0, &num))
213 		return err;
214 
215 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
216 		return -ERESTARTSYS;
217 
218 	if (num > ptp->max_vclocks) {
219 		dev_err(dev, "max value is %d\n", ptp->max_vclocks);
220 		goto out;
221 	}
222 
223 	/* Need to create more vclocks */
224 	if (num > ptp->n_vclocks) {
225 		for (i = 0; i < num - ptp->n_vclocks; i++) {
226 			vclock = ptp_vclock_register(ptp);
227 			if (!vclock)
228 				goto out;
229 
230 			*(ptp->vclock_index + ptp->n_vclocks + i) =
231 				vclock->clock->index;
232 
233 			dev_info(dev, "new virtual clock ptp%d\n",
234 				 vclock->clock->index);
235 		}
236 	}
237 
238 	/* Need to delete vclocks */
239 	if (num < ptp->n_vclocks) {
240 		i = ptp->n_vclocks - num;
241 		device_for_each_child_reverse(dev, &i,
242 					      unregister_vclock);
243 
244 		for (i = 1; i <= ptp->n_vclocks - num; i++)
245 			*(ptp->vclock_index + ptp->n_vclocks - i) = -1;
246 	}
247 
248 	/* Need to inform about changed physical clock behavior */
249 	if (!ptp->has_cycles) {
250 		if (num == 0)
251 			dev_info(dev, "only physical clock in use now\n");
252 		else
253 			dev_info(dev, "guarantee physical clock free running\n");
254 	}
255 
256 	ptp->n_vclocks = num;
257 	mutex_unlock(&ptp->n_vclocks_mux);
258 
259 	return count;
260 out:
261 	mutex_unlock(&ptp->n_vclocks_mux);
262 	return err;
263 }
264 static DEVICE_ATTR_RW(n_vclocks);
265 
266 static ssize_t max_vclocks_show(struct device *dev,
267 				struct device_attribute *attr, char *page)
268 {
269 	struct ptp_clock *ptp = dev_get_drvdata(dev);
270 	ssize_t size;
271 
272 	size = sysfs_emit(page, "%u\n", ptp->max_vclocks);
273 
274 	return size;
275 }
276 
277 static ssize_t max_vclocks_store(struct device *dev,
278 				 struct device_attribute *attr,
279 				 const char *buf, size_t count)
280 {
281 	struct ptp_clock *ptp = dev_get_drvdata(dev);
282 	unsigned int *vclock_index;
283 	int err = -EINVAL;
284 	size_t size;
285 	u32 max;
286 
287 	if (kstrtou32(buf, 0, &max) || max == 0)
288 		return -EINVAL;
289 
290 	if (max == ptp->max_vclocks)
291 		return count;
292 
293 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
294 		return -ERESTARTSYS;
295 
296 	if (max < ptp->n_vclocks)
297 		goto out;
298 
299 	vclock_index = kcalloc(max, sizeof(int), GFP_KERNEL);
300 	if (!vclock_index) {
301 		err = -ENOMEM;
302 		goto out;
303 	}
304 
305 	size = sizeof(int) * ptp->n_vclocks;
306 	memcpy(vclock_index, ptp->vclock_index, size);
307 
308 	kfree(ptp->vclock_index);
309 	ptp->vclock_index = vclock_index;
310 	ptp->max_vclocks = max;
311 
312 	mutex_unlock(&ptp->n_vclocks_mux);
313 
314 	return count;
315 out:
316 	mutex_unlock(&ptp->n_vclocks_mux);
317 	return err;
318 }
319 static DEVICE_ATTR_RW(max_vclocks);
320 
321 static struct attribute *ptp_attrs[] = {
322 	&dev_attr_clock_name.attr,
323 
324 	&dev_attr_max_adjustment.attr,
325 	&dev_attr_max_phase_adjustment.attr,
326 	&dev_attr_n_alarms.attr,
327 	&dev_attr_n_external_timestamps.attr,
328 	&dev_attr_n_periodic_outputs.attr,
329 	&dev_attr_n_programmable_pins.attr,
330 	&dev_attr_pps_available.attr,
331 
332 	&dev_attr_extts_enable.attr,
333 	&dev_attr_fifo.attr,
334 	&dev_attr_period.attr,
335 	&dev_attr_pps_enable.attr,
336 	&dev_attr_n_vclocks.attr,
337 	&dev_attr_max_vclocks.attr,
338 	NULL
339 };
340 
341 static umode_t ptp_is_attribute_visible(struct kobject *kobj,
342 					struct attribute *attr, int n)
343 {
344 	struct device *dev = kobj_to_dev(kobj);
345 	struct ptp_clock *ptp = dev_get_drvdata(dev);
346 	struct ptp_clock_info *info = ptp->info;
347 	umode_t mode = attr->mode;
348 
349 	if (attr == &dev_attr_extts_enable.attr ||
350 	    attr == &dev_attr_fifo.attr) {
351 		if (!info->n_ext_ts)
352 			mode = 0;
353 	} else if (attr == &dev_attr_period.attr) {
354 		if (!info->n_per_out)
355 			mode = 0;
356 	} else if (attr == &dev_attr_pps_enable.attr) {
357 		if (!info->pps)
358 			mode = 0;
359 	} else if (attr == &dev_attr_n_vclocks.attr ||
360 		   attr == &dev_attr_max_vclocks.attr) {
361 		if (ptp->is_virtual_clock)
362 			mode = 0;
363 	} else if (attr == &dev_attr_max_phase_adjustment.attr) {
364 		if (!info->adjphase || !info->getmaxphase)
365 			mode = 0;
366 	}
367 
368 	return mode;
369 }
370 
371 static const struct attribute_group ptp_group = {
372 	.is_visible	= ptp_is_attribute_visible,
373 	.attrs		= ptp_attrs,
374 };
375 
376 const struct attribute_group *ptp_groups[] = {
377 	&ptp_group,
378 	NULL
379 };
380 
381 static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
382 {
383 	int i;
384 	for (i = 0; i < ptp->info->n_pins; i++) {
385 		if (!strcmp(ptp->info->pin_config[i].name, name))
386 			return i;
387 	}
388 	return -1;
389 }
390 
391 static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
392 			    char *page)
393 {
394 	struct ptp_clock *ptp = dev_get_drvdata(dev);
395 	unsigned int func, chan;
396 	int index;
397 
398 	index = ptp_pin_name2index(ptp, attr->attr.name);
399 	if (index < 0)
400 		return -EINVAL;
401 
402 	if (mutex_lock_interruptible(&ptp->pincfg_mux))
403 		return -ERESTARTSYS;
404 
405 	func = ptp->info->pin_config[index].func;
406 	chan = ptp->info->pin_config[index].chan;
407 
408 	mutex_unlock(&ptp->pincfg_mux);
409 
410 	return sysfs_emit(page, "%u %u\n", func, chan);
411 }
412 
413 static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
414 			     const char *buf, size_t count)
415 {
416 	struct ptp_clock *ptp = dev_get_drvdata(dev);
417 	unsigned int func, chan;
418 	int cnt, err, index;
419 
420 	cnt = sscanf(buf, "%u %u", &func, &chan);
421 	if (cnt != 2)
422 		return -EINVAL;
423 
424 	index = ptp_pin_name2index(ptp, attr->attr.name);
425 	if (index < 0)
426 		return -EINVAL;
427 
428 	if (mutex_lock_interruptible(&ptp->pincfg_mux))
429 		return -ERESTARTSYS;
430 	err = ptp_set_pinfunc(ptp, index, func, chan);
431 	mutex_unlock(&ptp->pincfg_mux);
432 	if (err)
433 		return err;
434 
435 	return count;
436 }
437 
438 int ptp_populate_pin_groups(struct ptp_clock *ptp)
439 {
440 	struct ptp_clock_info *info = ptp->info;
441 	int err = -ENOMEM, i, n_pins = info->n_pins;
442 
443 	if (!n_pins)
444 		return 0;
445 
446 	ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
447 				    GFP_KERNEL);
448 	if (!ptp->pin_dev_attr)
449 		goto no_dev_attr;
450 
451 	ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
452 	if (!ptp->pin_attr)
453 		goto no_pin_attr;
454 
455 	for (i = 0; i < n_pins; i++) {
456 		struct device_attribute *da = &ptp->pin_dev_attr[i];
457 		sysfs_attr_init(&da->attr);
458 		da->attr.name = info->pin_config[i].name;
459 		da->attr.mode = 0644;
460 		da->show = ptp_pin_show;
461 		da->store = ptp_pin_store;
462 		ptp->pin_attr[i] = &da->attr;
463 	}
464 
465 	ptp->pin_attr_group.name = "pins";
466 	ptp->pin_attr_group.attrs = ptp->pin_attr;
467 
468 	ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
469 
470 	return 0;
471 
472 no_pin_attr:
473 	kfree(ptp->pin_dev_attr);
474 no_dev_attr:
475 	return err;
476 }
477 
478 void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
479 {
480 	kfree(ptp->pin_attr);
481 	kfree(ptp->pin_dev_attr);
482 }
483