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