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