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 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) \
35 struct ptp_clock *ptp = dev_get_drvdata(dev); \
36 return sysfs_emit(page, "%d\n", ptp->info->var); \
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
};
57 cnt
= sscanf(buf
, "%u %d", &req
.extts
.index
, &enable
);
60 if (req
.extts
.index
>= ops
->n_ext_ts
)
63 err
= ops
->enable(ops
, &req
, enable
? 1 : 0);
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
;
83 cnt
= list_count_nodes(&ptp
->tsevqs
);
87 /* The sysfs fifo will always draw from the fist queue */
88 queue
= list_first_entry(&ptp
->tsevqs
, struct timestamp_event_queue
,
91 memset(&event
, 0, sizeof(event
));
92 spin_lock_irqsave(&queue
->lock
, flags
);
93 qcnt
= queue_cnt(queue
);
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
);
104 cnt
= sysfs_emit(page
, "%u %lld %u\n",
105 event
.index
, event
.t
.sec
, event
.t
.nsec
);
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
);
125 if (req
.perout
.index
>= ops
->n_per_out
)
128 enable
= req
.perout
.period
.sec
|| req
.perout
.period
.nsec
;
129 err
= ops
->enable(ops
, &req
, enable
);
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
};
149 if (!capable(CAP_SYS_TIME
))
152 cnt
= sscanf(buf
, "%d", &enable
);
156 err
= ops
->enable(ops
, &req
, enable
? 1 : 0);
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
;
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
);
180 /* For break. Not error. */
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
);
193 if (mutex_lock_interruptible(&ptp
->n_vclocks_mux
))
196 size
= sysfs_emit(page
, "%u\n", ptp
->n_vclocks
);
198 mutex_unlock(&ptp
->n_vclocks_mux
);
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
;
212 if (kstrtou32(buf
, 0, &num
))
215 if (mutex_lock_interruptible(&ptp
->n_vclocks_mux
))
218 if (num
> ptp
->max_vclocks
) {
219 dev_err(dev
, "max value is %d\n", ptp
->max_vclocks
);
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
);
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
,
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
) {
251 dev_info(dev
, "only physical clock in use now\n");
253 dev_info(dev
, "guarantee physical clock free running\n");
256 ptp
->n_vclocks
= num
;
257 mutex_unlock(&ptp
->n_vclocks_mux
);
261 mutex_unlock(&ptp
->n_vclocks_mux
);
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
);
272 size
= sysfs_emit(page
, "%u\n", ptp
->max_vclocks
);
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
;
287 if (kstrtou32(buf
, 0, &max
) || max
== 0)
290 if (max
== ptp
->max_vclocks
)
293 if (mutex_lock_interruptible(&ptp
->n_vclocks_mux
))
296 if (max
< ptp
->n_vclocks
)
299 vclock_index
= kcalloc(max
, sizeof(int), GFP_KERNEL
);
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
);
316 mutex_unlock(&ptp
->n_vclocks_mux
);
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
,
334 &dev_attr_period
.attr
,
335 &dev_attr_pps_enable
.attr
,
336 &dev_attr_n_vclocks
.attr
,
337 &dev_attr_max_vclocks
.attr
,
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
) {
353 } else if (attr
== &dev_attr_period
.attr
) {
354 if (!info
->n_per_out
)
356 } else if (attr
== &dev_attr_pps_enable
.attr
) {
359 } else if (attr
== &dev_attr_n_vclocks
.attr
||
360 attr
== &dev_attr_max_vclocks
.attr
) {
361 if (ptp
->is_virtual_clock
)
363 } else if (attr
== &dev_attr_max_phase_adjustment
.attr
) {
364 if (!info
->adjphase
|| !info
->getmaxphase
)
371 static const struct attribute_group ptp_group
= {
372 .is_visible
= ptp_is_attribute_visible
,
376 const struct attribute_group
*ptp_groups
[] = {
381 static int ptp_pin_name2index(struct ptp_clock
*ptp
, const char *name
)
384 for (i
= 0; i
< ptp
->info
->n_pins
; i
++) {
385 if (!strcmp(ptp
->info
->pin_config
[i
].name
, name
))
391 static ssize_t
ptp_pin_show(struct device
*dev
, struct device_attribute
*attr
,
394 struct ptp_clock
*ptp
= dev_get_drvdata(dev
);
395 unsigned int func
, chan
;
398 index
= ptp_pin_name2index(ptp
, attr
->attr
.name
);
402 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
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
;
420 cnt
= sscanf(buf
, "%u %u", &func
, &chan
);
424 index
= ptp_pin_name2index(ptp
, attr
->attr
.name
);
428 if (mutex_lock_interruptible(&ptp
->pincfg_mux
))
430 err
= ptp_set_pinfunc(ptp
, index
, func
, chan
);
431 mutex_unlock(&ptp
->pincfg_mux
);
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
;
446 ptp
->pin_dev_attr
= kcalloc(n_pins
, sizeof(*ptp
->pin_dev_attr
),
448 if (!ptp
->pin_dev_attr
)
451 ptp
->pin_attr
= kcalloc(1 + n_pins
, sizeof(*ptp
->pin_attr
), GFP_KERNEL
);
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
;
473 kfree(ptp
->pin_dev_attr
);
478 void ptp_cleanup_pin_groups(struct ptp_clock
*ptp
)
480 kfree(ptp
->pin_attr
);
481 kfree(ptp
->pin_dev_attr
);