blob: 41b92dc2f011a3541c59e5a8248cf6f05eae4aa8 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Richard Cochrand94ba802011-04-22 12:03:08 +02002/*
3 * PTP 1588 clock support - sysfs interface.
4 *
5 * Copyright (C) 2010 OMICRON electronics GmbH
Yangbo Lu73f37062021-06-30 16:11:53 +08006 * Copyright 2021 NXP
Richard Cochrand94ba802011-04-22 12:03:08 +02007 */
8#include <linux/capability.h>
Richard Cochran653104d2014-03-20 22:21:54 +01009#include <linux/slab.h>
Richard Cochrand94ba802011-04-22 12:03:08 +020010
11#include "ptp_private.h"
12
13static 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 snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name);
18}
Julia Lawall632157052016-10-29 21:37:06 +020019static DEVICE_ATTR_RO(clock_name);
Richard Cochrand94ba802011-04-22 12:03:08 +020020
Greg Kroah-Hartman34991162013-07-24 15:05:20 -070021#define PTP_SHOW_INT(name, var) \
22static ssize_t var##_show(struct device *dev, \
Richard Cochrand94ba802011-04-22 12:03:08 +020023 struct device_attribute *attr, char *page) \
24{ \
25 struct ptp_clock *ptp = dev_get_drvdata(dev); \
Greg Kroah-Hartman34991162013-07-24 15:05:20 -070026 return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
27} \
28static DEVICE_ATTR(name, 0444, var##_show, NULL);
Richard Cochrand94ba802011-04-22 12:03:08 +020029
Greg Kroah-Hartman34991162013-07-24 15:05:20 -070030PTP_SHOW_INT(max_adjustment, max_adj);
31PTP_SHOW_INT(n_alarms, n_alarm);
32PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
33PTP_SHOW_INT(n_periodic_outputs, n_per_out);
Richard Cochran653104d2014-03-20 22:21:54 +010034PTP_SHOW_INT(n_programmable_pins, n_pins);
Greg Kroah-Hartman34991162013-07-24 15:05:20 -070035PTP_SHOW_INT(pps_available, pps);
Richard Cochrand94ba802011-04-22 12:03:08 +020036
Richard Cochrand94ba802011-04-22 12:03:08 +020037static ssize_t extts_enable_store(struct device *dev,
38 struct device_attribute *attr,
39 const char *buf, size_t count)
40{
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 };
44 int cnt, enable;
45 int err = -EINVAL;
46
47 cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
48 if (cnt != 2)
49 goto out;
50 if (req.extts.index >= ops->n_ext_ts)
51 goto out;
52
53 err = ops->enable(ops, &req, enable ? 1 : 0);
54 if (err)
55 goto out;
56
57 return count;
58out:
59 return err;
60}
Dmitry Torokhovaf59e712017-02-14 10:23:33 -080061static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
Richard Cochrand94ba802011-04-22 12:03:08 +020062
63static ssize_t extts_fifo_show(struct device *dev,
64 struct device_attribute *attr, char *page)
65{
66 struct ptp_clock *ptp = dev_get_drvdata(dev);
67 struct timestamp_event_queue *queue = &ptp->tsevq;
68 struct ptp_extts_event event;
69 unsigned long flags;
70 size_t qcnt;
71 int cnt = 0;
72
73 memset(&event, 0, sizeof(event));
74
75 if (mutex_lock_interruptible(&ptp->tsevq_mux))
76 return -ERESTARTSYS;
77
78 spin_lock_irqsave(&queue->lock, flags);
79 qcnt = queue_cnt(queue);
80 if (qcnt) {
81 event = queue->buf[queue->head];
82 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
83 }
84 spin_unlock_irqrestore(&queue->lock, flags);
85
86 if (!qcnt)
87 goto out;
88
89 cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
90 event.index, event.t.sec, event.t.nsec);
91out:
92 mutex_unlock(&ptp->tsevq_mux);
93 return cnt;
94}
Dmitry Torokhovaf59e712017-02-14 10:23:33 -080095static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
Richard Cochrand94ba802011-04-22 12:03:08 +020096
97static ssize_t period_store(struct device *dev,
98 struct device_attribute *attr,
99 const char *buf, size_t count)
100{
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;
105
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);
109 if (cnt != 5)
110 goto out;
111 if (req.perout.index >= ops->n_per_out)
112 goto out;
113
114 enable = req.perout.period.sec || req.perout.period.nsec;
115 err = ops->enable(ops, &req, enable);
116 if (err)
117 goto out;
118
119 return count;
120out:
121 return err;
122}
Dmitry Torokhovaf59e712017-02-14 10:23:33 -0800123static DEVICE_ATTR(period, 0220, NULL, period_store);
Richard Cochrand94ba802011-04-22 12:03:08 +0200124
125static ssize_t pps_enable_store(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t count)
128{
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 };
132 int cnt, enable;
133 int err = -EINVAL;
134
135 if (!capable(CAP_SYS_TIME))
136 return -EPERM;
137
138 cnt = sscanf(buf, "%d", &enable);
139 if (cnt != 1)
140 goto out;
141
142 err = ops->enable(ops, &req, enable ? 1 : 0);
143 if (err)
144 goto out;
145
146 return count;
147out:
148 return err;
149}
Dmitry Torokhovaf59e712017-02-14 10:23:33 -0800150static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
151
Yangbo Lu73f37062021-06-30 16:11:53 +0800152static int unregister_vclock(struct device *dev, void *data)
153{
154 struct ptp_clock *ptp = dev_get_drvdata(dev);
155 struct ptp_clock_info *info = ptp->info;
156 struct ptp_vclock *vclock;
Vinicius Costa Gomesd329e412021-08-06 18:15:46 -0700157 u32 *num = data;
Yangbo Lu73f37062021-06-30 16:11:53 +0800158
159 vclock = info_to_vclock(info);
160 dev_info(dev->parent, "delete virtual clock ptp%d\n",
161 vclock->clock->index);
162
163 ptp_vclock_unregister(vclock);
164 (*num)--;
165
166 /* For break. Not error. */
167 if (*num == 0)
168 return -EINVAL;
169
170 return 0;
171}
172
173static ssize_t n_vclocks_show(struct device *dev,
174 struct device_attribute *attr, char *page)
175{
176 struct ptp_clock *ptp = dev_get_drvdata(dev);
177 ssize_t size;
178
179 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
180 return -ERESTARTSYS;
181
Yangbo Luf6a175c2021-07-05 17:46:17 +0800182 size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->n_vclocks);
Yangbo Lu73f37062021-06-30 16:11:53 +0800183
184 mutex_unlock(&ptp->n_vclocks_mux);
185
186 return size;
187}
188
189static ssize_t n_vclocks_store(struct device *dev,
190 struct device_attribute *attr,
191 const char *buf, size_t count)
192{
193 struct ptp_clock *ptp = dev_get_drvdata(dev);
194 struct ptp_vclock *vclock;
195 int err = -EINVAL;
196 u32 num, i;
197
198 if (kstrtou32(buf, 0, &num))
199 return err;
200
201 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
202 return -ERESTARTSYS;
203
204 if (num > ptp->max_vclocks) {
205 dev_err(dev, "max value is %d\n", ptp->max_vclocks);
206 goto out;
207 }
208
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);
213 if (!vclock)
214 goto out;
215
Yangbo Lu44c494c2021-06-30 16:11:54 +0800216 *(ptp->vclock_index + ptp->n_vclocks + i) =
217 vclock->clock->index;
218
Yangbo Lu73f37062021-06-30 16:11:53 +0800219 dev_info(dev, "new virtual clock ptp%d\n",
220 vclock->clock->index);
221 }
222 }
223
224 /* Need to delete vclocks */
225 if (num < ptp->n_vclocks) {
226 i = ptp->n_vclocks - num;
227 device_for_each_child_reverse(dev, &i,
228 unregister_vclock);
Yangbo Lu44c494c2021-06-30 16:11:54 +0800229
230 for (i = 1; i <= ptp->n_vclocks - num; i++)
231 *(ptp->vclock_index + ptp->n_vclocks - i) = -1;
Yangbo Lu73f37062021-06-30 16:11:53 +0800232 }
233
234 if (num == 0)
235 dev_info(dev, "only physical clock in use now\n");
236 else
237 dev_info(dev, "guarantee physical clock free running\n");
238
239 ptp->n_vclocks = num;
240 mutex_unlock(&ptp->n_vclocks_mux);
241
242 return count;
243out:
244 mutex_unlock(&ptp->n_vclocks_mux);
245 return err;
246}
247static DEVICE_ATTR_RW(n_vclocks);
248
249static ssize_t max_vclocks_show(struct device *dev,
250 struct device_attribute *attr, char *page)
251{
252 struct ptp_clock *ptp = dev_get_drvdata(dev);
253 ssize_t size;
254
Yangbo Luf6a175c2021-07-05 17:46:17 +0800255 size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->max_vclocks);
Yangbo Lu73f37062021-06-30 16:11:53 +0800256
257 return size;
258}
259
260static ssize_t max_vclocks_store(struct device *dev,
261 struct device_attribute *attr,
262 const char *buf, size_t count)
263{
264 struct ptp_clock *ptp = dev_get_drvdata(dev);
Yangbo Lu44c494c2021-06-30 16:11:54 +0800265 unsigned int *vclock_index;
266 int err = -EINVAL;
267 size_t size;
Yangbo Lu73f37062021-06-30 16:11:53 +0800268 u32 max;
269
270 if (kstrtou32(buf, 0, &max) || max == 0)
271 return -EINVAL;
272
273 if (max == ptp->max_vclocks)
274 return count;
275
276 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
277 return -ERESTARTSYS;
278
Yangbo Lu44c494c2021-06-30 16:11:54 +0800279 if (max < ptp->n_vclocks)
280 goto out;
281
282 size = sizeof(int) * max;
283 vclock_index = kzalloc(size, GFP_KERNEL);
284 if (!vclock_index) {
285 err = -ENOMEM;
286 goto out;
Yangbo Lu73f37062021-06-30 16:11:53 +0800287 }
288
Yangbo Lu44c494c2021-06-30 16:11:54 +0800289 size = sizeof(int) * ptp->n_vclocks;
290 memcpy(vclock_index, ptp->vclock_index, size);
291
292 kfree(ptp->vclock_index);
293 ptp->vclock_index = vclock_index;
Yangbo Lu73f37062021-06-30 16:11:53 +0800294 ptp->max_vclocks = max;
295
296 mutex_unlock(&ptp->n_vclocks_mux);
297
298 return count;
Yangbo Lu44c494c2021-06-30 16:11:54 +0800299out:
300 mutex_unlock(&ptp->n_vclocks_mux);
301 return err;
Yangbo Lu73f37062021-06-30 16:11:53 +0800302}
303static DEVICE_ATTR_RW(max_vclocks);
304
Dmitry Torokhovaf59e712017-02-14 10:23:33 -0800305static struct attribute *ptp_attrs[] = {
306 &dev_attr_clock_name.attr,
307
308 &dev_attr_max_adjustment.attr,
309 &dev_attr_n_alarms.attr,
310 &dev_attr_n_external_timestamps.attr,
311 &dev_attr_n_periodic_outputs.attr,
312 &dev_attr_n_programmable_pins.attr,
313 &dev_attr_pps_available.attr,
314
315 &dev_attr_extts_enable.attr,
316 &dev_attr_fifo.attr,
317 &dev_attr_period.attr,
318 &dev_attr_pps_enable.attr,
Yangbo Lu73f37062021-06-30 16:11:53 +0800319 &dev_attr_n_vclocks.attr,
320 &dev_attr_max_vclocks.attr,
Dmitry Torokhovaf59e712017-02-14 10:23:33 -0800321 NULL
322};
323
324static umode_t ptp_is_attribute_visible(struct kobject *kobj,
325 struct attribute *attr, int n)
326{
327 struct device *dev = kobj_to_dev(kobj);
328 struct ptp_clock *ptp = dev_get_drvdata(dev);
329 struct ptp_clock_info *info = ptp->info;
330 umode_t mode = attr->mode;
331
332 if (attr == &dev_attr_extts_enable.attr ||
333 attr == &dev_attr_fifo.attr) {
334 if (!info->n_ext_ts)
335 mode = 0;
336 } else if (attr == &dev_attr_period.attr) {
337 if (!info->n_per_out)
338 mode = 0;
339 } else if (attr == &dev_attr_pps_enable.attr) {
340 if (!info->pps)
341 mode = 0;
Yangbo Lu73f37062021-06-30 16:11:53 +0800342 } else if (attr == &dev_attr_n_vclocks.attr ||
343 attr == &dev_attr_max_vclocks.attr) {
344 if (ptp->is_virtual_clock)
345 mode = 0;
Dmitry Torokhovaf59e712017-02-14 10:23:33 -0800346 }
347
348 return mode;
349}
350
351static const struct attribute_group ptp_group = {
352 .is_visible = ptp_is_attribute_visible,
353 .attrs = ptp_attrs,
354};
355
356const struct attribute_group *ptp_groups[] = {
357 &ptp_group,
358 NULL
359};
Richard Cochrand94ba802011-04-22 12:03:08 +0200360
Richard Cochran653104d2014-03-20 22:21:54 +0100361static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
362{
363 int i;
364 for (i = 0; i < ptp->info->n_pins; i++) {
365 if (!strcmp(ptp->info->pin_config[i].name, name))
366 return i;
367 }
368 return -1;
369}
370
371static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
372 char *page)
373{
374 struct ptp_clock *ptp = dev_get_drvdata(dev);
375 unsigned int func, chan;
376 int index;
377
378 index = ptp_pin_name2index(ptp, attr->attr.name);
379 if (index < 0)
380 return -EINVAL;
381
382 if (mutex_lock_interruptible(&ptp->pincfg_mux))
383 return -ERESTARTSYS;
384
385 func = ptp->info->pin_config[index].func;
386 chan = ptp->info->pin_config[index].chan;
387
388 mutex_unlock(&ptp->pincfg_mux);
389
390 return snprintf(page, PAGE_SIZE, "%u %u\n", func, chan);
391}
392
393static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t count)
395{
396 struct ptp_clock *ptp = dev_get_drvdata(dev);
397 unsigned int func, chan;
398 int cnt, err, index;
399
400 cnt = sscanf(buf, "%u %u", &func, &chan);
401 if (cnt != 2)
402 return -EINVAL;
403
404 index = ptp_pin_name2index(ptp, attr->attr.name);
405 if (index < 0)
406 return -EINVAL;
407
408 if (mutex_lock_interruptible(&ptp->pincfg_mux))
409 return -ERESTARTSYS;
410 err = ptp_set_pinfunc(ptp, index, func, chan);
411 mutex_unlock(&ptp->pincfg_mux);
412 if (err)
413 return err;
414
415 return count;
416}
417
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800418int ptp_populate_pin_groups(struct ptp_clock *ptp)
Richard Cochrand94ba802011-04-22 12:03:08 +0200419{
Richard Cochran653104d2014-03-20 22:21:54 +0100420 struct ptp_clock_info *info = ptp->info;
421 int err = -ENOMEM, i, n_pins = info->n_pins;
422
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800423 if (!n_pins)
424 return 0;
425
Dmitry Torokhov6f7aa562017-02-14 10:23:32 -0800426 ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
Richard Cochran653104d2014-03-20 22:21:54 +0100427 GFP_KERNEL);
428 if (!ptp->pin_dev_attr)
429 goto no_dev_attr;
430
Dmitry Torokhov6f7aa562017-02-14 10:23:32 -0800431 ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
Richard Cochran653104d2014-03-20 22:21:54 +0100432 if (!ptp->pin_attr)
433 goto no_pin_attr;
434
435 for (i = 0; i < n_pins; i++) {
436 struct device_attribute *da = &ptp->pin_dev_attr[i];
437 sysfs_attr_init(&da->attr);
438 da->attr.name = info->pin_config[i].name;
439 da->attr.mode = 0644;
440 da->show = ptp_pin_show;
441 da->store = ptp_pin_store;
442 ptp->pin_attr[i] = &da->attr;
443 }
444
445 ptp->pin_attr_group.name = "pins";
446 ptp->pin_attr_group.attrs = ptp->pin_attr;
447
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800448 ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
449
Richard Cochran653104d2014-03-20 22:21:54 +0100450 return 0;
451
Richard Cochran653104d2014-03-20 22:21:54 +0100452no_pin_attr:
453 kfree(ptp->pin_dev_attr);
454no_dev_attr:
455 return err;
456}
457
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800458void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
Richard Cochrand94ba802011-04-22 12:03:08 +0200459{
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800460 kfree(ptp->pin_attr);
461 kfree(ptp->pin_dev_attr);
Richard Cochrand94ba802011-04-22 12:03:08 +0200462}