sysfs.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PPS generators sysfs support
  4. *
  5. * Copyright (C) 2024 Rodolfo Giometti <giometti@enneenne.com>
  6. */
  7. #include <linux/device.h>
  8. #include <linux/module.h>
  9. #include <linux/string.h>
  10. #include <linux/pps_gen_kernel.h>
  11. /*
  12. * Attribute functions
  13. */
  14. static ssize_t system_show(struct device *dev, struct device_attribute *attr,
  15. char *buf)
  16. {
  17. struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
  18. return sysfs_emit(buf, "%d\n", pps_gen->info->use_system_clock);
  19. }
  20. static DEVICE_ATTR_RO(system);
  21. static ssize_t time_show(struct device *dev, struct device_attribute *attr,
  22. char *buf)
  23. {
  24. struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
  25. struct timespec64 time;
  26. int ret;
  27. ret = pps_gen->info->get_time(pps_gen, &time);
  28. if (ret)
  29. return ret;
  30. return sysfs_emit(buf, "%llu %09lu\n", time.tv_sec, time.tv_nsec);
  31. }
  32. static DEVICE_ATTR_RO(time);
  33. static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
  34. const char *buf, size_t count)
  35. {
  36. struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
  37. bool status;
  38. int ret;
  39. ret = kstrtobool(buf, &status);
  40. if (ret)
  41. return ret;
  42. ret = pps_gen->info->enable(pps_gen, status);
  43. if (ret)
  44. return ret;
  45. pps_gen->enabled = status;
  46. return count;
  47. }
  48. static DEVICE_ATTR_WO(enable);
  49. static struct attribute *pps_gen_attrs[] = {
  50. &dev_attr_enable.attr,
  51. &dev_attr_time.attr,
  52. &dev_attr_system.attr,
  53. NULL,
  54. };
  55. static const struct attribute_group pps_gen_group = {
  56. .attrs = pps_gen_attrs,
  57. };
  58. const struct attribute_group *pps_gen_groups[] = {
  59. &pps_gen_group,
  60. NULL,
  61. };