Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / ptp / ptp_sysfs.c
blob6b1b8f57cd9510f269c86dd89a7a74f277f6916b
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * PTP 1588 clock support - sysfs interface.
5 * Copyright (C) 2010 OMICRON electronics GmbH
6 * Copyright 2021 NXP
7 */
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,
23 char *page)
25 struct ptp_clock *ptp = dev_get_drvdata(dev);
27 return sysfs_emit(page, "%d\n", ptp->info->getmaxphase(ptp->info));
29 static DEVICE_ATTR_RO(max_phase_adjustment);
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);
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);
47 static ssize_t extts_enable_store(struct device *dev,
48 struct device_attribute *attr,
49 const char *buf, size_t count)
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;
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;
63 err = ops->enable(ops, &req, enable ? 1 : 0);
64 if (err)
65 goto out;
67 return count;
68 out:
69 return err;
71 static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
73 static ssize_t extts_fifo_show(struct device *dev,
74 struct device_attribute *attr, char *page)
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;
83 cnt = list_count_nodes(&ptp->tsevqs);
84 if (cnt <= 0)
85 goto out;
87 /* The sysfs fifo will always draw from the fist queue */
88 queue = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue,
89 qlist);
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);
99 spin_unlock_irqrestore(&queue->lock, flags);
101 if (!qcnt)
102 goto out;
104 cnt = sysfs_emit(page, "%u %lld %u\n",
105 event.index, event.t.sec, event.t.nsec);
106 out:
107 return cnt;
109 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
111 static ssize_t period_store(struct device *dev,
112 struct device_attribute *attr,
113 const char *buf, size_t count)
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;
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;
128 enable = req.perout.period.sec || req.perout.period.nsec;
129 err = ops->enable(ops, &req, enable);
130 if (err)
131 goto out;
133 return count;
134 out:
135 return err;
137 static DEVICE_ATTR(period, 0220, NULL, period_store);
139 static ssize_t pps_enable_store(struct device *dev,
140 struct device_attribute *attr,
141 const char *buf, size_t count)
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;
149 if (!capable(CAP_SYS_TIME))
150 return -EPERM;
152 cnt = sscanf(buf, "%d", &enable);
153 if (cnt != 1)
154 goto out;
156 err = ops->enable(ops, &req, enable ? 1 : 0);
157 if (err)
158 goto out;
160 return count;
161 out:
162 return err;
164 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
166 static int unregister_vclock(struct device *dev, void *data)
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;
173 vclock = info_to_vclock(info);
174 dev_info(dev->parent, "delete virtual clock ptp%d\n",
175 vclock->clock->index);
177 ptp_vclock_unregister(vclock);
178 (*num)--;
180 /* For break. Not error. */
181 if (*num == 0)
182 return -EINVAL;
184 return 0;
187 static ssize_t n_vclocks_show(struct device *dev,
188 struct device_attribute *attr, char *page)
190 struct ptp_clock *ptp = dev_get_drvdata(dev);
191 ssize_t size;
193 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
194 return -ERESTARTSYS;
196 size = sysfs_emit(page, "%u\n", ptp->n_vclocks);
198 mutex_unlock(&ptp->n_vclocks_mux);
200 return size;
203 static ssize_t n_vclocks_store(struct device *dev,
204 struct device_attribute *attr,
205 const char *buf, size_t count)
207 struct ptp_clock *ptp = dev_get_drvdata(dev);
208 struct ptp_vclock *vclock;
209 int err = -EINVAL;
210 u32 num, i;
212 if (kstrtou32(buf, 0, &num))
213 return err;
215 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
216 return -ERESTARTSYS;
218 if (num > ptp->max_vclocks) {
219 dev_err(dev, "max value is %d\n", ptp->max_vclocks);
220 goto out;
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;
230 *(ptp->vclock_index + ptp->n_vclocks + i) =
231 vclock->clock->index;
233 dev_info(dev, "new virtual clock ptp%d\n",
234 vclock->clock->index);
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);
244 for (i = 1; i <= ptp->n_vclocks - num; i++)
245 *(ptp->vclock_index + ptp->n_vclocks - i) = -1;
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");
256 ptp->n_vclocks = num;
257 mutex_unlock(&ptp->n_vclocks_mux);
259 return count;
260 out:
261 mutex_unlock(&ptp->n_vclocks_mux);
262 return err;
264 static DEVICE_ATTR_RW(n_vclocks);
266 static ssize_t max_vclocks_show(struct device *dev,
267 struct device_attribute *attr, char *page)
269 struct ptp_clock *ptp = dev_get_drvdata(dev);
270 ssize_t size;
272 size = sysfs_emit(page, "%u\n", ptp->max_vclocks);
274 return size;
277 static ssize_t max_vclocks_store(struct device *dev,
278 struct device_attribute *attr,
279 const char *buf, size_t count)
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;
287 if (kstrtou32(buf, 0, &max) || max == 0)
288 return -EINVAL;
290 if (max == ptp->max_vclocks)
291 return count;
293 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
294 return -ERESTARTSYS;
296 if (max < ptp->n_vclocks)
297 goto out;
299 vclock_index = kcalloc(max, sizeof(int), GFP_KERNEL);
300 if (!vclock_index) {
301 err = -ENOMEM;
302 goto out;
305 size = sizeof(int) * ptp->n_vclocks;
306 memcpy(vclock_index, ptp->vclock_index, size);
308 kfree(ptp->vclock_index);
309 ptp->vclock_index = vclock_index;
310 ptp->max_vclocks = max;
312 mutex_unlock(&ptp->n_vclocks_mux);
314 return count;
315 out:
316 mutex_unlock(&ptp->n_vclocks_mux);
317 return err;
319 static DEVICE_ATTR_RW(max_vclocks);
321 static struct attribute *ptp_attrs[] = {
322 &dev_attr_clock_name.attr,
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,
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
341 static umode_t ptp_is_attribute_visible(struct kobject *kobj,
342 struct attribute *attr, int n)
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;
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;
368 return mode;
371 static const struct attribute_group ptp_group = {
372 .is_visible = ptp_is_attribute_visible,
373 .attrs = ptp_attrs,
376 const struct attribute_group *ptp_groups[] = {
377 &ptp_group,
378 NULL
381 static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
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;
388 return -1;
391 static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
392 char *page)
394 struct ptp_clock *ptp = dev_get_drvdata(dev);
395 unsigned int func, chan;
396 int index;
398 index = ptp_pin_name2index(ptp, attr->attr.name);
399 if (index < 0)
400 return -EINVAL;
402 if (mutex_lock_interruptible(&ptp->pincfg_mux))
403 return -ERESTARTSYS;
405 func = ptp->info->pin_config[index].func;
406 chan = ptp->info->pin_config[index].chan;
408 mutex_unlock(&ptp->pincfg_mux);
410 return sysfs_emit(page, "%u %u\n", func, chan);
413 static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
414 const char *buf, size_t count)
416 struct ptp_clock *ptp = dev_get_drvdata(dev);
417 unsigned int func, chan;
418 int cnt, err, index;
420 cnt = sscanf(buf, "%u %u", &func, &chan);
421 if (cnt != 2)
422 return -EINVAL;
424 index = ptp_pin_name2index(ptp, attr->attr.name);
425 if (index < 0)
426 return -EINVAL;
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;
435 return count;
438 int ptp_populate_pin_groups(struct ptp_clock *ptp)
440 struct ptp_clock_info *info = ptp->info;
441 int err = -ENOMEM, i, n_pins = info->n_pins;
443 if (!n_pins)
444 return 0;
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;
451 ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
452 if (!ptp->pin_attr)
453 goto no_pin_attr;
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;
465 ptp->pin_attr_group.name = "pins";
466 ptp->pin_attr_group.attrs = ptp->pin_attr;
468 ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
470 return 0;
472 no_pin_attr:
473 kfree(ptp->pin_dev_attr);
474 no_dev_attr:
475 return err;
478 void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
480 kfree(ptp->pin_attr);
481 kfree(ptp->pin_dev_attr);