1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PTP 1588 clock support - sysfs interface.
5 * Copyright (C) 2010 OMICRON electronics GmbH
8 #include <linux/capability.h>
9 #include <linux/slab.h>
11 #include "ptp_private.h"
13 static ssize_t clock_name_show(struct device *dev,
14 struct device_attribute *attr, char *page)
16 struct ptp_clock *ptp = dev_get_drvdata(dev);
17 return sysfs_emit(page, "%s\n", ptp->info->name);
19 static DEVICE_ATTR_RO(clock_name);
21 static ssize_t max_phase_adjustment_show(struct device *dev,
22 struct device_attribute *attr,
25 struct ptp_clock *ptp = dev_get_drvdata(dev);
27 return snprintf(page, PAGE_SIZE - 1, "%d\n",
28 ptp->info->getmaxphase(ptp->info));
30 static DEVICE_ATTR_RO(max_phase_adjustment);
32 #define PTP_SHOW_INT(name, var) \
33 static ssize_t var##_show(struct device *dev, \
34 struct device_attribute *attr, char *page) \
36 struct ptp_clock *ptp = dev_get_drvdata(dev); \
37 return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
39 static DEVICE_ATTR(name, 0444, var##_show, NULL);
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);
48 static ssize_t extts_enable_store(struct device *dev,
49 struct device_attribute *attr,
50 const char *buf, size_t count)
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 };
58 cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
61 if (req.extts.index >= ops->n_ext_ts)
64 err = ops->enable(ops, &req, enable ? 1 : 0);
72 static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
74 static ssize_t extts_fifo_show(struct device *dev,
75 struct device_attribute *attr, char *page)
77 struct ptp_clock *ptp = dev_get_drvdata(dev);
78 struct timestamp_event_queue *queue;
79 struct ptp_extts_event event;
84 cnt = list_count_nodes(&ptp->tsevqs);
88 /* The sysfs fifo will always draw from the fist queue */
89 queue = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue,
92 memset(&event, 0, sizeof(event));
93 spin_lock_irqsave(&queue->lock, flags);
94 qcnt = queue_cnt(queue);
96 event = queue->buf[queue->head];
97 /* Paired with READ_ONCE() in queue_cnt() */
98 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
100 spin_unlock_irqrestore(&queue->lock, flags);
105 cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
106 event.index, event.t.sec, event.t.nsec);
110 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
112 static ssize_t period_store(struct device *dev,
113 struct device_attribute *attr,
114 const char *buf, size_t count)
116 struct ptp_clock *ptp = dev_get_drvdata(dev);
117 struct ptp_clock_info *ops = ptp->info;
118 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
119 int cnt, enable, err = -EINVAL;
121 cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
122 &req.perout.start.sec, &req.perout.start.nsec,
123 &req.perout.period.sec, &req.perout.period.nsec);
126 if (req.perout.index >= ops->n_per_out)
129 enable = req.perout.period.sec || req.perout.period.nsec;
130 err = ops->enable(ops, &req, enable);
138 static DEVICE_ATTR(period, 0220, NULL, period_store);
140 static ssize_t pps_enable_store(struct device *dev,
141 struct device_attribute *attr,
142 const char *buf, size_t count)
144 struct ptp_clock *ptp = dev_get_drvdata(dev);
145 struct ptp_clock_info *ops = ptp->info;
146 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
150 if (!capable(CAP_SYS_TIME))
153 cnt = sscanf(buf, "%d", &enable);
157 err = ops->enable(ops, &req, enable ? 1 : 0);
165 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
167 static int unregister_vclock(struct device *dev, void *data)
169 struct ptp_clock *ptp = dev_get_drvdata(dev);
170 struct ptp_clock_info *info = ptp->info;
171 struct ptp_vclock *vclock;
174 vclock = info_to_vclock(info);
175 dev_info(dev->parent, "delete virtual clock ptp%d\n",
176 vclock->clock->index);
178 ptp_vclock_unregister(vclock);
181 /* For break. Not error. */
188 static ssize_t n_vclocks_show(struct device *dev,
189 struct device_attribute *attr, char *page)
191 struct ptp_clock *ptp = dev_get_drvdata(dev);
194 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
197 size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->n_vclocks);
199 mutex_unlock(&ptp->n_vclocks_mux);
204 static ssize_t n_vclocks_store(struct device *dev,
205 struct device_attribute *attr,
206 const char *buf, size_t count)
208 struct ptp_clock *ptp = dev_get_drvdata(dev);
209 struct ptp_vclock *vclock;
213 if (kstrtou32(buf, 0, &num))
216 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
219 if (num > ptp->max_vclocks) {
220 dev_err(dev, "max value is %d\n", ptp->max_vclocks);
224 /* Need to create more vclocks */
225 if (num > ptp->n_vclocks) {
226 for (i = 0; i < num - ptp->n_vclocks; i++) {
227 vclock = ptp_vclock_register(ptp);
231 *(ptp->vclock_index + ptp->n_vclocks + i) =
232 vclock->clock->index;
234 dev_info(dev, "new virtual clock ptp%d\n",
235 vclock->clock->index);
239 /* Need to delete vclocks */
240 if (num < ptp->n_vclocks) {
241 i = ptp->n_vclocks - num;
242 device_for_each_child_reverse(dev, &i,
245 for (i = 1; i <= ptp->n_vclocks - num; i++)
246 *(ptp->vclock_index + ptp->n_vclocks - i) = -1;
249 /* Need to inform about changed physical clock behavior */
250 if (!ptp->has_cycles) {
252 dev_info(dev, "only physical clock in use now\n");
254 dev_info(dev, "guarantee physical clock free running\n");
257 ptp->n_vclocks = num;
258 mutex_unlock(&ptp->n_vclocks_mux);
262 mutex_unlock(&ptp->n_vclocks_mux);
265 static DEVICE_ATTR_RW(n_vclocks);
267 static ssize_t max_vclocks_show(struct device *dev,
268 struct device_attribute *attr, char *page)
270 struct ptp_clock *ptp = dev_get_drvdata(dev);
273 size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->max_vclocks);
278 static ssize_t max_vclocks_store(struct device *dev,
279 struct device_attribute *attr,
280 const char *buf, size_t count)
282 struct ptp_clock *ptp = dev_get_drvdata(dev);
283 unsigned int *vclock_index;
288 if (kstrtou32(buf, 0, &max) || max == 0)
291 if (max == ptp->max_vclocks)
294 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
297 if (max < ptp->n_vclocks)
300 size = sizeof(int) * max;
301 vclock_index = kzalloc(size, GFP_KERNEL);
307 size = sizeof(int) * ptp->n_vclocks;
308 memcpy(vclock_index, ptp->vclock_index, size);
310 kfree(ptp->vclock_index);
311 ptp->vclock_index = vclock_index;
312 ptp->max_vclocks = max;
314 mutex_unlock(&ptp->n_vclocks_mux);
318 mutex_unlock(&ptp->n_vclocks_mux);
321 static DEVICE_ATTR_RW(max_vclocks);
323 static struct attribute *ptp_attrs[] = {
324 &dev_attr_clock_name.attr,
326 &dev_attr_max_adjustment.attr,
327 &dev_attr_max_phase_adjustment.attr,
328 &dev_attr_n_alarms.attr,
329 &dev_attr_n_external_timestamps.attr,
330 &dev_attr_n_periodic_outputs.attr,
331 &dev_attr_n_programmable_pins.attr,
332 &dev_attr_pps_available.attr,
334 &dev_attr_extts_enable.attr,
336 &dev_attr_period.attr,
337 &dev_attr_pps_enable.attr,
338 &dev_attr_n_vclocks.attr,
339 &dev_attr_max_vclocks.attr,
343 static umode_t ptp_is_attribute_visible(struct kobject *kobj,
344 struct attribute *attr, int n)
346 struct device *dev = kobj_to_dev(kobj);
347 struct ptp_clock *ptp = dev_get_drvdata(dev);
348 struct ptp_clock_info *info = ptp->info;
349 umode_t mode = attr->mode;
351 if (attr == &dev_attr_extts_enable.attr ||
352 attr == &dev_attr_fifo.attr) {
355 } else if (attr == &dev_attr_period.attr) {
356 if (!info->n_per_out)
358 } else if (attr == &dev_attr_pps_enable.attr) {
361 } else if (attr == &dev_attr_n_vclocks.attr ||
362 attr == &dev_attr_max_vclocks.attr) {
363 if (ptp->is_virtual_clock)
365 } else if (attr == &dev_attr_max_phase_adjustment.attr) {
366 if (!info->adjphase || !info->getmaxphase)
373 static const struct attribute_group ptp_group = {
374 .is_visible = ptp_is_attribute_visible,
378 const struct attribute_group *ptp_groups[] = {
383 static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
386 for (i = 0; i < ptp->info->n_pins; i++) {
387 if (!strcmp(ptp->info->pin_config[i].name, name))
393 static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
396 struct ptp_clock *ptp = dev_get_drvdata(dev);
397 unsigned int func, chan;
400 index = ptp_pin_name2index(ptp, attr->attr.name);
404 if (mutex_lock_interruptible(&ptp->pincfg_mux))
407 func = ptp->info->pin_config[index].func;
408 chan = ptp->info->pin_config[index].chan;
410 mutex_unlock(&ptp->pincfg_mux);
412 return sysfs_emit(page, "%u %u\n", func, chan);
415 static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
416 const char *buf, size_t count)
418 struct ptp_clock *ptp = dev_get_drvdata(dev);
419 unsigned int func, chan;
422 cnt = sscanf(buf, "%u %u", &func, &chan);
426 index = ptp_pin_name2index(ptp, attr->attr.name);
430 if (mutex_lock_interruptible(&ptp->pincfg_mux))
432 err = ptp_set_pinfunc(ptp, index, func, chan);
433 mutex_unlock(&ptp->pincfg_mux);
440 int ptp_populate_pin_groups(struct ptp_clock *ptp)
442 struct ptp_clock_info *info = ptp->info;
443 int err = -ENOMEM, i, n_pins = info->n_pins;
448 ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
450 if (!ptp->pin_dev_attr)
453 ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
457 for (i = 0; i < n_pins; i++) {
458 struct device_attribute *da = &ptp->pin_dev_attr[i];
459 sysfs_attr_init(&da->attr);
460 da->attr.name = info->pin_config[i].name;
461 da->attr.mode = 0644;
462 da->show = ptp_pin_show;
463 da->store = ptp_pin_store;
464 ptp->pin_attr[i] = &da->attr;
467 ptp->pin_attr_group.name = "pins";
468 ptp->pin_attr_group.attrs = ptp->pin_attr;
470 ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
475 kfree(ptp->pin_dev_attr);
480 void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
482 kfree(ptp->pin_attr);
483 kfree(ptp->pin_dev_attr);