blob: 8cd59e84816316d31441b2ff10d8b175f80137f6 [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
Richard Cochrand94ba802011-04-22 12:03:08 +02006 */
7#include <linux/capability.h>
Richard Cochran653104d2014-03-20 22:21:54 +01008#include <linux/slab.h>
Richard Cochrand94ba802011-04-22 12:03:08 +02009
10#include "ptp_private.h"
11
12static ssize_t clock_name_show(struct device *dev,
13 struct device_attribute *attr, char *page)
14{
15 struct ptp_clock *ptp = dev_get_drvdata(dev);
Yang Guang02e2ee82022-01-27 08:02:36 +080016 return sysfs_emit(page, "%s\n", ptp->info->name);
Richard Cochrand94ba802011-04-22 12:03:08 +020017}
Julia Lawall632157052016-10-29 21:37:06 +020018static DEVICE_ATTR_RO(clock_name);
Richard Cochrand94ba802011-04-22 12:03:08 +020019
Greg Kroah-Hartman34991162013-07-24 15:05:20 -070020#define PTP_SHOW_INT(name, var) \
21static ssize_t var##_show(struct device *dev, \
Richard Cochrand94ba802011-04-22 12:03:08 +020022 struct device_attribute *attr, char *page) \
23{ \
24 struct ptp_clock *ptp = dev_get_drvdata(dev); \
Greg Kroah-Hartman34991162013-07-24 15:05:20 -070025 return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
26} \
27static DEVICE_ATTR(name, 0444, var##_show, NULL);
Richard Cochrand94ba802011-04-22 12:03:08 +020028
Greg Kroah-Hartman34991162013-07-24 15:05:20 -070029PTP_SHOW_INT(max_adjustment, max_adj);
30PTP_SHOW_INT(n_alarms, n_alarm);
31PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
32PTP_SHOW_INT(n_periodic_outputs, n_per_out);
Richard Cochran653104d2014-03-20 22:21:54 +010033PTP_SHOW_INT(n_programmable_pins, n_pins);
Greg Kroah-Hartman34991162013-07-24 15:05:20 -070034PTP_SHOW_INT(pps_available, pps);
Richard Cochrand94ba802011-04-22 12:03:08 +020035
Richard Cochrand94ba802011-04-22 12:03:08 +020036static ssize_t extts_enable_store(struct device *dev,
37 struct device_attribute *attr,
38 const char *buf, size_t count)
39{
40 struct ptp_clock *ptp = dev_get_drvdata(dev);
41 struct ptp_clock_info *ops = ptp->info;
42 struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
43 int cnt, enable;
44 int err = -EINVAL;
45
46 cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
47 if (cnt != 2)
48 goto out;
49 if (req.extts.index >= ops->n_ext_ts)
50 goto out;
51
52 err = ops->enable(ops, &req, enable ? 1 : 0);
53 if (err)
54 goto out;
55
56 return count;
57out:
58 return err;
59}
Dmitry Torokhovaf59e712017-02-14 10:23:33 -080060static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
Richard Cochrand94ba802011-04-22 12:03:08 +020061
62static ssize_t extts_fifo_show(struct device *dev,
63 struct device_attribute *attr, char *page)
64{
65 struct ptp_clock *ptp = dev_get_drvdata(dev);
66 struct timestamp_event_queue *queue = &ptp->tsevq;
67 struct ptp_extts_event event;
68 unsigned long flags;
69 size_t qcnt;
70 int cnt = 0;
71
72 memset(&event, 0, sizeof(event));
73
74 if (mutex_lock_interruptible(&ptp->tsevq_mux))
75 return -ERESTARTSYS;
76
77 spin_lock_irqsave(&queue->lock, flags);
78 qcnt = queue_cnt(queue);
79 if (qcnt) {
80 event = queue->buf[queue->head];
81 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
82 }
83 spin_unlock_irqrestore(&queue->lock, flags);
84
85 if (!qcnt)
86 goto out;
87
88 cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
89 event.index, event.t.sec, event.t.nsec);
90out:
91 mutex_unlock(&ptp->tsevq_mux);
92 return cnt;
93}
Dmitry Torokhovaf59e712017-02-14 10:23:33 -080094static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
Richard Cochrand94ba802011-04-22 12:03:08 +020095
96static ssize_t period_store(struct device *dev,
97 struct device_attribute *attr,
98 const char *buf, size_t count)
99{
100 struct ptp_clock *ptp = dev_get_drvdata(dev);
101 struct ptp_clock_info *ops = ptp->info;
102 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
103 int cnt, enable, err = -EINVAL;
104
105 cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
106 &req.perout.start.sec, &req.perout.start.nsec,
107 &req.perout.period.sec, &req.perout.period.nsec);
108 if (cnt != 5)
109 goto out;
110 if (req.perout.index >= ops->n_per_out)
111 goto out;
112
113 enable = req.perout.period.sec || req.perout.period.nsec;
114 err = ops->enable(ops, &req, enable);
115 if (err)
116 goto out;
117
118 return count;
119out:
120 return err;
121}
Dmitry Torokhovaf59e712017-02-14 10:23:33 -0800122static DEVICE_ATTR(period, 0220, NULL, period_store);
Richard Cochrand94ba802011-04-22 12:03:08 +0200123
124static ssize_t pps_enable_store(struct device *dev,
125 struct device_attribute *attr,
126 const char *buf, size_t count)
127{
128 struct ptp_clock *ptp = dev_get_drvdata(dev);
129 struct ptp_clock_info *ops = ptp->info;
130 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
131 int cnt, enable;
132 int err = -EINVAL;
133
134 if (!capable(CAP_SYS_TIME))
135 return -EPERM;
136
137 cnt = sscanf(buf, "%d", &enable);
138 if (cnt != 1)
139 goto out;
140
141 err = ops->enable(ops, &req, enable ? 1 : 0);
142 if (err)
143 goto out;
144
145 return count;
146out:
147 return err;
148}
Dmitry Torokhovaf59e712017-02-14 10:23:33 -0800149static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
150
151static struct attribute *ptp_attrs[] = {
152 &dev_attr_clock_name.attr,
153
154 &dev_attr_max_adjustment.attr,
155 &dev_attr_n_alarms.attr,
156 &dev_attr_n_external_timestamps.attr,
157 &dev_attr_n_periodic_outputs.attr,
158 &dev_attr_n_programmable_pins.attr,
159 &dev_attr_pps_available.attr,
160
161 &dev_attr_extts_enable.attr,
162 &dev_attr_fifo.attr,
163 &dev_attr_period.attr,
164 &dev_attr_pps_enable.attr,
165 NULL
166};
167
168static umode_t ptp_is_attribute_visible(struct kobject *kobj,
169 struct attribute *attr, int n)
170{
171 struct device *dev = kobj_to_dev(kobj);
172 struct ptp_clock *ptp = dev_get_drvdata(dev);
173 struct ptp_clock_info *info = ptp->info;
174 umode_t mode = attr->mode;
175
176 if (attr == &dev_attr_extts_enable.attr ||
177 attr == &dev_attr_fifo.attr) {
178 if (!info->n_ext_ts)
179 mode = 0;
180 } else if (attr == &dev_attr_period.attr) {
181 if (!info->n_per_out)
182 mode = 0;
183 } else if (attr == &dev_attr_pps_enable.attr) {
184 if (!info->pps)
185 mode = 0;
186 }
187
188 return mode;
189}
190
191static const struct attribute_group ptp_group = {
192 .is_visible = ptp_is_attribute_visible,
193 .attrs = ptp_attrs,
194};
195
196const struct attribute_group *ptp_groups[] = {
197 &ptp_group,
198 NULL
199};
Richard Cochrand94ba802011-04-22 12:03:08 +0200200
Richard Cochran653104d2014-03-20 22:21:54 +0100201static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
202{
203 int i;
204 for (i = 0; i < ptp->info->n_pins; i++) {
205 if (!strcmp(ptp->info->pin_config[i].name, name))
206 return i;
207 }
208 return -1;
209}
210
211static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
212 char *page)
213{
214 struct ptp_clock *ptp = dev_get_drvdata(dev);
215 unsigned int func, chan;
216 int index;
217
218 index = ptp_pin_name2index(ptp, attr->attr.name);
219 if (index < 0)
220 return -EINVAL;
221
222 if (mutex_lock_interruptible(&ptp->pincfg_mux))
223 return -ERESTARTSYS;
224
225 func = ptp->info->pin_config[index].func;
226 chan = ptp->info->pin_config[index].chan;
227
228 mutex_unlock(&ptp->pincfg_mux);
229
Yang Guang02e2ee82022-01-27 08:02:36 +0800230 return sysfs_emit(page, "%u %u\n", func, chan);
Richard Cochran653104d2014-03-20 22:21:54 +0100231}
232
233static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
234 const char *buf, size_t count)
235{
236 struct ptp_clock *ptp = dev_get_drvdata(dev);
237 unsigned int func, chan;
238 int cnt, err, index;
239
240 cnt = sscanf(buf, "%u %u", &func, &chan);
241 if (cnt != 2)
242 return -EINVAL;
243
244 index = ptp_pin_name2index(ptp, attr->attr.name);
245 if (index < 0)
246 return -EINVAL;
247
248 if (mutex_lock_interruptible(&ptp->pincfg_mux))
249 return -ERESTARTSYS;
250 err = ptp_set_pinfunc(ptp, index, func, chan);
251 mutex_unlock(&ptp->pincfg_mux);
252 if (err)
253 return err;
254
255 return count;
256}
257
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800258int ptp_populate_pin_groups(struct ptp_clock *ptp)
Richard Cochrand94ba802011-04-22 12:03:08 +0200259{
Richard Cochran653104d2014-03-20 22:21:54 +0100260 struct ptp_clock_info *info = ptp->info;
261 int err = -ENOMEM, i, n_pins = info->n_pins;
262
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800263 if (!n_pins)
264 return 0;
265
Dmitry Torokhov6f7aa562017-02-14 10:23:32 -0800266 ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
Richard Cochran653104d2014-03-20 22:21:54 +0100267 GFP_KERNEL);
268 if (!ptp->pin_dev_attr)
269 goto no_dev_attr;
270
Dmitry Torokhov6f7aa562017-02-14 10:23:32 -0800271 ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
Richard Cochran653104d2014-03-20 22:21:54 +0100272 if (!ptp->pin_attr)
273 goto no_pin_attr;
274
275 for (i = 0; i < n_pins; i++) {
276 struct device_attribute *da = &ptp->pin_dev_attr[i];
277 sysfs_attr_init(&da->attr);
278 da->attr.name = info->pin_config[i].name;
279 da->attr.mode = 0644;
280 da->show = ptp_pin_show;
281 da->store = ptp_pin_store;
282 ptp->pin_attr[i] = &da->attr;
283 }
284
285 ptp->pin_attr_group.name = "pins";
286 ptp->pin_attr_group.attrs = ptp->pin_attr;
287
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800288 ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
289
Richard Cochran653104d2014-03-20 22:21:54 +0100290 return 0;
291
Richard Cochran653104d2014-03-20 22:21:54 +0100292no_pin_attr:
293 kfree(ptp->pin_dev_attr);
294no_dev_attr:
295 return err;
296}
297
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800298void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
Richard Cochrand94ba802011-04-22 12:03:08 +0200299{
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800300 kfree(ptp->pin_attr);
301 kfree(ptp->pin_dev_attr);
Richard Cochrand94ba802011-04-22 12:03:08 +0200302}