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 snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name);
19 static DEVICE_ATTR_RO(clock_name);
21 #define PTP_SHOW_INT(name, var) \
22 static ssize_t var##_show(struct device *dev, \
23 struct device_attribute *attr, char *page) \
25 struct ptp_clock *ptp = dev_get_drvdata(dev); \
26 return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
28 static DEVICE_ATTR(name, 0444, var##_show, NULL);
30 PTP_SHOW_INT(max_adjustment, max_adj);
31 PTP_SHOW_INT(n_alarms, n_alarm);
32 PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
33 PTP_SHOW_INT(n_periodic_outputs, n_per_out);
34 PTP_SHOW_INT(n_programmable_pins, n_pins);
35 PTP_SHOW_INT(pps_available, pps);
37 static ssize_t extts_enable_store(struct device *dev,
38 struct device_attribute *attr,
39 const char *buf, size_t count)
41 struct ptp_clock *ptp = dev_get_drvdata(dev);
42 struct ptp_clock_info *ops = ptp->info;
43 struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
47 cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
50 if (req.extts.index >= ops->n_ext_ts)
53 err = ops->enable(ops, &req, enable ? 1 : 0);
61 static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
63 static ssize_t extts_fifo_show(struct device *dev,
64 struct device_attribute *attr, char *page)
66 struct ptp_clock *ptp = dev_get_drvdata(dev);
67 struct timestamp_event_queue *queue = &ptp->tsevq;
68 struct ptp_extts_event event;
73 memset(&event, 0, sizeof(event));
75 if (mutex_lock_interruptible(&ptp->tsevq_mux))
78 spin_lock_irqsave(&queue->lock, flags);
79 qcnt = queue_cnt(queue);
81 event = queue->buf[queue->head];
82 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
84 spin_unlock_irqrestore(&queue->lock, flags);
89 cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
90 event.index, event.t.sec, event.t.nsec);
92 mutex_unlock(&ptp->tsevq_mux);
95 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
97 static ssize_t period_store(struct device *dev,
98 struct device_attribute *attr,
99 const char *buf, size_t count)
101 struct ptp_clock *ptp = dev_get_drvdata(dev);
102 struct ptp_clock_info *ops = ptp->info;
103 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
104 int cnt, enable, err = -EINVAL;
106 cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
107 &req.perout.start.sec, &req.perout.start.nsec,
108 &req.perout.period.sec, &req.perout.period.nsec);
111 if (req.perout.index >= ops->n_per_out)
114 enable = req.perout.period.sec || req.perout.period.nsec;
115 err = ops->enable(ops, &req, enable);
123 static DEVICE_ATTR(period, 0220, NULL, period_store);
125 static ssize_t pps_enable_store(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t count)
129 struct ptp_clock *ptp = dev_get_drvdata(dev);
130 struct ptp_clock_info *ops = ptp->info;
131 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
135 if (!capable(CAP_SYS_TIME))
138 cnt = sscanf(buf, "%d", &enable);
142 err = ops->enable(ops, &req, enable ? 1 : 0);
150 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
152 static int unregister_vclock(struct device *dev, void *data)
154 struct ptp_clock *ptp = dev_get_drvdata(dev);
155 struct ptp_clock_info *info = ptp->info;
156 struct ptp_vclock *vclock;
159 vclock = info_to_vclock(info);
160 dev_info(dev->parent, "delete virtual clock ptp%d\n",
161 vclock->clock->index);
163 ptp_vclock_unregister(vclock);
166 /* For break. Not error. */
173 static ssize_t n_vclocks_show(struct device *dev,
174 struct device_attribute *attr, char *page)
176 struct ptp_clock *ptp = dev_get_drvdata(dev);
179 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
182 size = snprintf(page, PAGE_SIZE - 1, "%d\n", ptp->n_vclocks);
184 mutex_unlock(&ptp->n_vclocks_mux);
189 static ssize_t n_vclocks_store(struct device *dev,
190 struct device_attribute *attr,
191 const char *buf, size_t count)
193 struct ptp_clock *ptp = dev_get_drvdata(dev);
194 struct ptp_vclock *vclock;
198 if (kstrtou32(buf, 0, &num))
201 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
204 if (num > ptp->max_vclocks) {
205 dev_err(dev, "max value is %d\n", ptp->max_vclocks);
209 /* Need to create more vclocks */
210 if (num > ptp->n_vclocks) {
211 for (i = 0; i < num - ptp->n_vclocks; i++) {
212 vclock = ptp_vclock_register(ptp);
216 dev_info(dev, "new virtual clock ptp%d\n",
217 vclock->clock->index);
221 /* Need to delete vclocks */
222 if (num < ptp->n_vclocks) {
223 i = ptp->n_vclocks - num;
224 device_for_each_child_reverse(dev, &i,
229 dev_info(dev, "only physical clock in use now\n");
231 dev_info(dev, "guarantee physical clock free running\n");
233 ptp->n_vclocks = num;
234 mutex_unlock(&ptp->n_vclocks_mux);
238 mutex_unlock(&ptp->n_vclocks_mux);
241 static DEVICE_ATTR_RW(n_vclocks);
243 static ssize_t max_vclocks_show(struct device *dev,
244 struct device_attribute *attr, char *page)
246 struct ptp_clock *ptp = dev_get_drvdata(dev);
249 size = snprintf(page, PAGE_SIZE - 1, "%d\n", ptp->max_vclocks);
254 static ssize_t max_vclocks_store(struct device *dev,
255 struct device_attribute *attr,
256 const char *buf, size_t count)
258 struct ptp_clock *ptp = dev_get_drvdata(dev);
261 if (kstrtou32(buf, 0, &max) || max == 0)
264 if (max == ptp->max_vclocks)
267 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
270 if (max < ptp->n_vclocks) {
271 mutex_unlock(&ptp->n_vclocks_mux);
275 ptp->max_vclocks = max;
277 mutex_unlock(&ptp->n_vclocks_mux);
281 static DEVICE_ATTR_RW(max_vclocks);
283 static struct attribute *ptp_attrs[] = {
284 &dev_attr_clock_name.attr,
286 &dev_attr_max_adjustment.attr,
287 &dev_attr_n_alarms.attr,
288 &dev_attr_n_external_timestamps.attr,
289 &dev_attr_n_periodic_outputs.attr,
290 &dev_attr_n_programmable_pins.attr,
291 &dev_attr_pps_available.attr,
293 &dev_attr_extts_enable.attr,
295 &dev_attr_period.attr,
296 &dev_attr_pps_enable.attr,
297 &dev_attr_n_vclocks.attr,
298 &dev_attr_max_vclocks.attr,
302 static umode_t ptp_is_attribute_visible(struct kobject *kobj,
303 struct attribute *attr, int n)
305 struct device *dev = kobj_to_dev(kobj);
306 struct ptp_clock *ptp = dev_get_drvdata(dev);
307 struct ptp_clock_info *info = ptp->info;
308 umode_t mode = attr->mode;
310 if (attr == &dev_attr_extts_enable.attr ||
311 attr == &dev_attr_fifo.attr) {
314 } else if (attr == &dev_attr_period.attr) {
315 if (!info->n_per_out)
317 } else if (attr == &dev_attr_pps_enable.attr) {
320 } else if (attr == &dev_attr_n_vclocks.attr ||
321 attr == &dev_attr_max_vclocks.attr) {
322 if (ptp->is_virtual_clock)
329 static const struct attribute_group ptp_group = {
330 .is_visible = ptp_is_attribute_visible,
334 const struct attribute_group *ptp_groups[] = {
339 static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
342 for (i = 0; i < ptp->info->n_pins; i++) {
343 if (!strcmp(ptp->info->pin_config[i].name, name))
349 static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
352 struct ptp_clock *ptp = dev_get_drvdata(dev);
353 unsigned int func, chan;
356 index = ptp_pin_name2index(ptp, attr->attr.name);
360 if (mutex_lock_interruptible(&ptp->pincfg_mux))
363 func = ptp->info->pin_config[index].func;
364 chan = ptp->info->pin_config[index].chan;
366 mutex_unlock(&ptp->pincfg_mux);
368 return snprintf(page, PAGE_SIZE, "%u %u\n", func, chan);
371 static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
372 const char *buf, size_t count)
374 struct ptp_clock *ptp = dev_get_drvdata(dev);
375 unsigned int func, chan;
378 cnt = sscanf(buf, "%u %u", &func, &chan);
382 index = ptp_pin_name2index(ptp, attr->attr.name);
386 if (mutex_lock_interruptible(&ptp->pincfg_mux))
388 err = ptp_set_pinfunc(ptp, index, func, chan);
389 mutex_unlock(&ptp->pincfg_mux);
396 int ptp_populate_pin_groups(struct ptp_clock *ptp)
398 struct ptp_clock_info *info = ptp->info;
399 int err = -ENOMEM, i, n_pins = info->n_pins;
404 ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
406 if (!ptp->pin_dev_attr)
409 ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
413 for (i = 0; i < n_pins; i++) {
414 struct device_attribute *da = &ptp->pin_dev_attr[i];
415 sysfs_attr_init(&da->attr);
416 da->attr.name = info->pin_config[i].name;
417 da->attr.mode = 0644;
418 da->show = ptp_pin_show;
419 da->store = ptp_pin_store;
420 ptp->pin_attr[i] = &da->attr;
423 ptp->pin_attr_group.name = "pins";
424 ptp->pin_attr_group.attrs = ptp->pin_attr;
426 ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
431 kfree(ptp->pin_dev_attr);
436 void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
438 kfree(ptp->pin_attr);
439 kfree(ptp->pin_dev_attr);