1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * PTP 1588 clock support - sysfs interface.
4 *
5 * Copyright (C) 2010 OMICRON electronics GmbH
6 * Copyright 2021 NXP
7 */
8 #include <linux/capability.h>
9 #include <linux/slab.h>
10
11 #include "ptp_private.h"
12
clock_name_show(struct device * dev,struct device_attribute * attr,char * page)13 static 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 sysfs_emit(page, "%s\n", ptp->info->name);
18 }
19 static DEVICE_ATTR_RO(clock_name);
20
21 #define PTP_SHOW_INT(name, var) \
22 static ssize_t var##_show(struct device *dev, \
23 struct device_attribute *attr, char *page) \
24 { \
25 struct ptp_clock *ptp = dev_get_drvdata(dev); \
26 return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
27 } \
28 static DEVICE_ATTR(name, 0444, var##_show, NULL);
29
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);
36
extts_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)37 static 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;
58 out:
59 return err;
60 }
61 static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
62
extts_fifo_show(struct device * dev,struct device_attribute * attr,char * page)63 static 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 /* Paired with READ_ONCE() in queue_cnt() */
83 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
84 }
85 spin_unlock_irqrestore(&queue->lock, flags);
86
87 if (!qcnt)
88 goto out;
89
90 cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
91 event.index, event.t.sec, event.t.nsec);
92 out:
93 mutex_unlock(&ptp->tsevq_mux);
94 return cnt;
95 }
96 static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
97
period_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)98 static ssize_t period_store(struct device *dev,
99 struct device_attribute *attr,
100 const char *buf, size_t count)
101 {
102 struct ptp_clock *ptp = dev_get_drvdata(dev);
103 struct ptp_clock_info *ops = ptp->info;
104 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
105 int cnt, enable, err = -EINVAL;
106
107 cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
108 &req.perout.start.sec, &req.perout.start.nsec,
109 &req.perout.period.sec, &req.perout.period.nsec);
110 if (cnt != 5)
111 goto out;
112 if (req.perout.index >= ops->n_per_out)
113 goto out;
114
115 enable = req.perout.period.sec || req.perout.period.nsec;
116 err = ops->enable(ops, &req, enable);
117 if (err)
118 goto out;
119
120 return count;
121 out:
122 return err;
123 }
124 static DEVICE_ATTR(period, 0220, NULL, period_store);
125
pps_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)126 static ssize_t pps_enable_store(struct device *dev,
127 struct device_attribute *attr,
128 const char *buf, size_t count)
129 {
130 struct ptp_clock *ptp = dev_get_drvdata(dev);
131 struct ptp_clock_info *ops = ptp->info;
132 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
133 int cnt, enable;
134 int err = -EINVAL;
135
136 if (!capable(CAP_SYS_TIME))
137 return -EPERM;
138
139 cnt = sscanf(buf, "%d", &enable);
140 if (cnt != 1)
141 goto out;
142
143 err = ops->enable(ops, &req, enable ? 1 : 0);
144 if (err)
145 goto out;
146
147 return count;
148 out:
149 return err;
150 }
151 static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
152
unregister_vclock(struct device * dev,void * data)153 static int unregister_vclock(struct device *dev, void *data)
154 {
155 struct ptp_clock *ptp = dev_get_drvdata(dev);
156 struct ptp_clock_info *info = ptp->info;
157 struct ptp_vclock *vclock;
158 u32 *num = data;
159
160 vclock = info_to_vclock(info);
161 dev_info(dev->parent, "delete virtual clock ptp%d\n",
162 vclock->clock->index);
163
164 ptp_vclock_unregister(vclock);
165 (*num)--;
166
167 /* For break. Not error. */
168 if (*num == 0)
169 return -EINVAL;
170
171 return 0;
172 }
173
n_vclocks_show(struct device * dev,struct device_attribute * attr,char * page)174 static ssize_t n_vclocks_show(struct device *dev,
175 struct device_attribute *attr, char *page)
176 {
177 struct ptp_clock *ptp = dev_get_drvdata(dev);
178 ssize_t size;
179
180 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
181 return -ERESTARTSYS;
182
183 size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->n_vclocks);
184
185 mutex_unlock(&ptp->n_vclocks_mux);
186
187 return size;
188 }
189
n_vclocks_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)190 static ssize_t n_vclocks_store(struct device *dev,
191 struct device_attribute *attr,
192 const char *buf, size_t count)
193 {
194 struct ptp_clock *ptp = dev_get_drvdata(dev);
195 struct ptp_vclock *vclock;
196 int err = -EINVAL;
197 u32 num, i;
198
199 if (kstrtou32(buf, 0, &num))
200 return err;
201
202 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
203 return -ERESTARTSYS;
204
205 if (num > ptp->max_vclocks) {
206 dev_err(dev, "max value is %d\n", ptp->max_vclocks);
207 goto out;
208 }
209
210 /* Need to create more vclocks */
211 if (num > ptp->n_vclocks) {
212 for (i = 0; i < num - ptp->n_vclocks; i++) {
213 vclock = ptp_vclock_register(ptp);
214 if (!vclock)
215 goto out;
216
217 *(ptp->vclock_index + ptp->n_vclocks + i) =
218 vclock->clock->index;
219
220 dev_info(dev, "new virtual clock ptp%d\n",
221 vclock->clock->index);
222 }
223 }
224
225 /* Need to delete vclocks */
226 if (num < ptp->n_vclocks) {
227 i = ptp->n_vclocks - num;
228 device_for_each_child_reverse(dev, &i,
229 unregister_vclock);
230
231 for (i = 1; i <= ptp->n_vclocks - num; i++)
232 *(ptp->vclock_index + ptp->n_vclocks - i) = -1;
233 }
234
235 if (num == 0)
236 dev_info(dev, "only physical clock in use now\n");
237 else
238 dev_info(dev, "guarantee physical clock free running\n");
239
240 ptp->n_vclocks = num;
241 mutex_unlock(&ptp->n_vclocks_mux);
242
243 return count;
244 out:
245 mutex_unlock(&ptp->n_vclocks_mux);
246 return err;
247 }
248 static DEVICE_ATTR_RW(n_vclocks);
249
max_vclocks_show(struct device * dev,struct device_attribute * attr,char * page)250 static ssize_t max_vclocks_show(struct device *dev,
251 struct device_attribute *attr, char *page)
252 {
253 struct ptp_clock *ptp = dev_get_drvdata(dev);
254 ssize_t size;
255
256 size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->max_vclocks);
257
258 return size;
259 }
260
max_vclocks_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)261 static ssize_t max_vclocks_store(struct device *dev,
262 struct device_attribute *attr,
263 const char *buf, size_t count)
264 {
265 struct ptp_clock *ptp = dev_get_drvdata(dev);
266 unsigned int *vclock_index;
267 int err = -EINVAL;
268 size_t size;
269 u32 max;
270
271 if (kstrtou32(buf, 0, &max) || max == 0)
272 return -EINVAL;
273
274 if (max == ptp->max_vclocks)
275 return count;
276
277 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
278 return -ERESTARTSYS;
279
280 if (max < ptp->n_vclocks)
281 goto out;
282
283 size = sizeof(int) * max;
284 vclock_index = kzalloc(size, GFP_KERNEL);
285 if (!vclock_index) {
286 err = -ENOMEM;
287 goto out;
288 }
289
290 size = sizeof(int) * ptp->n_vclocks;
291 memcpy(vclock_index, ptp->vclock_index, size);
292
293 kfree(ptp->vclock_index);
294 ptp->vclock_index = vclock_index;
295 ptp->max_vclocks = max;
296
297 mutex_unlock(&ptp->n_vclocks_mux);
298
299 return count;
300 out:
301 mutex_unlock(&ptp->n_vclocks_mux);
302 return err;
303 }
304 static DEVICE_ATTR_RW(max_vclocks);
305
306 static struct attribute *ptp_attrs[] = {
307 &dev_attr_clock_name.attr,
308
309 &dev_attr_max_adjustment.attr,
310 &dev_attr_n_alarms.attr,
311 &dev_attr_n_external_timestamps.attr,
312 &dev_attr_n_periodic_outputs.attr,
313 &dev_attr_n_programmable_pins.attr,
314 &dev_attr_pps_available.attr,
315
316 &dev_attr_extts_enable.attr,
317 &dev_attr_fifo.attr,
318 &dev_attr_period.attr,
319 &dev_attr_pps_enable.attr,
320 &dev_attr_n_vclocks.attr,
321 &dev_attr_max_vclocks.attr,
322 NULL
323 };
324
ptp_is_attribute_visible(struct kobject * kobj,struct attribute * attr,int n)325 static umode_t ptp_is_attribute_visible(struct kobject *kobj,
326 struct attribute *attr, int n)
327 {
328 struct device *dev = kobj_to_dev(kobj);
329 struct ptp_clock *ptp = dev_get_drvdata(dev);
330 struct ptp_clock_info *info = ptp->info;
331 umode_t mode = attr->mode;
332
333 if (attr == &dev_attr_extts_enable.attr ||
334 attr == &dev_attr_fifo.attr) {
335 if (!info->n_ext_ts)
336 mode = 0;
337 } else if (attr == &dev_attr_period.attr) {
338 if (!info->n_per_out)
339 mode = 0;
340 } else if (attr == &dev_attr_pps_enable.attr) {
341 if (!info->pps)
342 mode = 0;
343 } else if (attr == &dev_attr_n_vclocks.attr ||
344 attr == &dev_attr_max_vclocks.attr) {
345 if (ptp->is_virtual_clock)
346 mode = 0;
347 }
348
349 return mode;
350 }
351
352 static const struct attribute_group ptp_group = {
353 .is_visible = ptp_is_attribute_visible,
354 .attrs = ptp_attrs,
355 };
356
357 const struct attribute_group *ptp_groups[] = {
358 &ptp_group,
359 NULL
360 };
361
ptp_pin_name2index(struct ptp_clock * ptp,const char * name)362 static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
363 {
364 int i;
365 for (i = 0; i < ptp->info->n_pins; i++) {
366 if (!strcmp(ptp->info->pin_config[i].name, name))
367 return i;
368 }
369 return -1;
370 }
371
ptp_pin_show(struct device * dev,struct device_attribute * attr,char * page)372 static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
373 char *page)
374 {
375 struct ptp_clock *ptp = dev_get_drvdata(dev);
376 unsigned int func, chan;
377 int index;
378
379 index = ptp_pin_name2index(ptp, attr->attr.name);
380 if (index < 0)
381 return -EINVAL;
382
383 if (mutex_lock_interruptible(&ptp->pincfg_mux))
384 return -ERESTARTSYS;
385
386 func = ptp->info->pin_config[index].func;
387 chan = ptp->info->pin_config[index].chan;
388
389 mutex_unlock(&ptp->pincfg_mux);
390
391 return sysfs_emit(page, "%u %u\n", func, chan);
392 }
393
ptp_pin_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)394 static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
395 const char *buf, size_t count)
396 {
397 struct ptp_clock *ptp = dev_get_drvdata(dev);
398 unsigned int func, chan;
399 int cnt, err, index;
400
401 cnt = sscanf(buf, "%u %u", &func, &chan);
402 if (cnt != 2)
403 return -EINVAL;
404
405 index = ptp_pin_name2index(ptp, attr->attr.name);
406 if (index < 0)
407 return -EINVAL;
408
409 if (mutex_lock_interruptible(&ptp->pincfg_mux))
410 return -ERESTARTSYS;
411 err = ptp_set_pinfunc(ptp, index, func, chan);
412 mutex_unlock(&ptp->pincfg_mux);
413 if (err)
414 return err;
415
416 return count;
417 }
418
ptp_populate_pin_groups(struct ptp_clock * ptp)419 int ptp_populate_pin_groups(struct ptp_clock *ptp)
420 {
421 struct ptp_clock_info *info = ptp->info;
422 int err = -ENOMEM, i, n_pins = info->n_pins;
423
424 if (!n_pins)
425 return 0;
426
427 ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
428 GFP_KERNEL);
429 if (!ptp->pin_dev_attr)
430 goto no_dev_attr;
431
432 ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
433 if (!ptp->pin_attr)
434 goto no_pin_attr;
435
436 for (i = 0; i < n_pins; i++) {
437 struct device_attribute *da = &ptp->pin_dev_attr[i];
438 sysfs_attr_init(&da->attr);
439 da->attr.name = info->pin_config[i].name;
440 da->attr.mode = 0644;
441 da->show = ptp_pin_show;
442 da->store = ptp_pin_store;
443 ptp->pin_attr[i] = &da->attr;
444 }
445
446 ptp->pin_attr_group.name = "pins";
447 ptp->pin_attr_group.attrs = ptp->pin_attr;
448
449 ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
450
451 return 0;
452
453 no_pin_attr:
454 kfree(ptp->pin_dev_attr);
455 no_dev_attr:
456 return err;
457 }
458
ptp_cleanup_pin_groups(struct ptp_clock * ptp)459 void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
460 {
461 kfree(ptp->pin_attr);
462 kfree(ptp->pin_dev_attr);
463 }
464