xref: /openbmc/linux/drivers/ptp/ptp_sysfs.c (revision c67ce71d)
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 		/* Paired with READ_ONCE() in queue_cnt() */
94 		WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
95 	}
96 	spin_unlock_irqrestore(&queue->lock, flags);
97 
98 	if (!qcnt)
99 		goto out;
100 
101 	cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
102 		       event.index, event.t.sec, event.t.nsec);
103 out:
104 	mutex_unlock(&ptp->tsevq_mux);
105 	return cnt;
106 }
107 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
108 
109 static ssize_t period_store(struct device *dev,
110 			    struct device_attribute *attr,
111 			    const char *buf, size_t count)
112 {
113 	struct ptp_clock *ptp = dev_get_drvdata(dev);
114 	struct ptp_clock_info *ops = ptp->info;
115 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
116 	int cnt, enable, err = -EINVAL;
117 
118 	cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
119 		     &req.perout.start.sec, &req.perout.start.nsec,
120 		     &req.perout.period.sec, &req.perout.period.nsec);
121 	if (cnt != 5)
122 		goto out;
123 	if (req.perout.index >= ops->n_per_out)
124 		goto out;
125 
126 	enable = req.perout.period.sec || req.perout.period.nsec;
127 	err = ops->enable(ops, &req, enable);
128 	if (err)
129 		goto out;
130 
131 	return count;
132 out:
133 	return err;
134 }
135 static DEVICE_ATTR(period, 0220, NULL, period_store);
136 
137 static ssize_t pps_enable_store(struct device *dev,
138 				struct device_attribute *attr,
139 				const char *buf, size_t count)
140 {
141 	struct ptp_clock *ptp = dev_get_drvdata(dev);
142 	struct ptp_clock_info *ops = ptp->info;
143 	struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
144 	int cnt, enable;
145 	int err = -EINVAL;
146 
147 	if (!capable(CAP_SYS_TIME))
148 		return -EPERM;
149 
150 	cnt = sscanf(buf, "%d", &enable);
151 	if (cnt != 1)
152 		goto out;
153 
154 	err = ops->enable(ops, &req, enable ? 1 : 0);
155 	if (err)
156 		goto out;
157 
158 	return count;
159 out:
160 	return err;
161 }
162 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
163 
164 static int unregister_vclock(struct device *dev, void *data)
165 {
166 	struct ptp_clock *ptp = dev_get_drvdata(dev);
167 	struct ptp_clock_info *info = ptp->info;
168 	struct ptp_vclock *vclock;
169 	u32 *num = data;
170 
171 	vclock = info_to_vclock(info);
172 	dev_info(dev->parent, "delete virtual clock ptp%d\n",
173 		 vclock->clock->index);
174 
175 	ptp_vclock_unregister(vclock);
176 	(*num)--;
177 
178 	/* For break. Not error. */
179 	if (*num == 0)
180 		return -EINVAL;
181 
182 	return 0;
183 }
184 
185 static ssize_t n_vclocks_show(struct device *dev,
186 			      struct device_attribute *attr, char *page)
187 {
188 	struct ptp_clock *ptp = dev_get_drvdata(dev);
189 	ssize_t size;
190 
191 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
192 		return -ERESTARTSYS;
193 
194 	size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->n_vclocks);
195 
196 	mutex_unlock(&ptp->n_vclocks_mux);
197 
198 	return size;
199 }
200 
201 static ssize_t n_vclocks_store(struct device *dev,
202 			       struct device_attribute *attr,
203 			       const char *buf, size_t count)
204 {
205 	struct ptp_clock *ptp = dev_get_drvdata(dev);
206 	struct ptp_vclock *vclock;
207 	int err = -EINVAL;
208 	u32 num, i;
209 
210 	if (kstrtou32(buf, 0, &num))
211 		return err;
212 
213 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
214 		return -ERESTARTSYS;
215 
216 	if (num > ptp->max_vclocks) {
217 		dev_err(dev, "max value is %d\n", ptp->max_vclocks);
218 		goto out;
219 	}
220 
221 	/* Need to create more vclocks */
222 	if (num > ptp->n_vclocks) {
223 		for (i = 0; i < num - ptp->n_vclocks; i++) {
224 			vclock = ptp_vclock_register(ptp);
225 			if (!vclock)
226 				goto out;
227 
228 			*(ptp->vclock_index + ptp->n_vclocks + i) =
229 				vclock->clock->index;
230 
231 			dev_info(dev, "new virtual clock ptp%d\n",
232 				 vclock->clock->index);
233 		}
234 	}
235 
236 	/* Need to delete vclocks */
237 	if (num < ptp->n_vclocks) {
238 		i = ptp->n_vclocks - num;
239 		device_for_each_child_reverse(dev, &i,
240 					      unregister_vclock);
241 
242 		for (i = 1; i <= ptp->n_vclocks - num; i++)
243 			*(ptp->vclock_index + ptp->n_vclocks - i) = -1;
244 	}
245 
246 	/* Need to inform about changed physical clock behavior */
247 	if (!ptp->has_cycles) {
248 		if (num == 0)
249 			dev_info(dev, "only physical clock in use now\n");
250 		else
251 			dev_info(dev, "guarantee physical clock free running\n");
252 	}
253 
254 	ptp->n_vclocks = num;
255 	mutex_unlock(&ptp->n_vclocks_mux);
256 
257 	return count;
258 out:
259 	mutex_unlock(&ptp->n_vclocks_mux);
260 	return err;
261 }
262 static DEVICE_ATTR_RW(n_vclocks);
263 
264 static ssize_t max_vclocks_show(struct device *dev,
265 				struct device_attribute *attr, char *page)
266 {
267 	struct ptp_clock *ptp = dev_get_drvdata(dev);
268 	ssize_t size;
269 
270 	size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->max_vclocks);
271 
272 	return size;
273 }
274 
275 static ssize_t max_vclocks_store(struct device *dev,
276 				 struct device_attribute *attr,
277 				 const char *buf, size_t count)
278 {
279 	struct ptp_clock *ptp = dev_get_drvdata(dev);
280 	unsigned int *vclock_index;
281 	int err = -EINVAL;
282 	size_t size;
283 	u32 max;
284 
285 	if (kstrtou32(buf, 0, &max) || max == 0)
286 		return -EINVAL;
287 
288 	if (max == ptp->max_vclocks)
289 		return count;
290 
291 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
292 		return -ERESTARTSYS;
293 
294 	if (max < ptp->n_vclocks)
295 		goto out;
296 
297 	vclock_index = kcalloc(max, sizeof(int), 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