macio_sysfs.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/stat.h>
  4. #include <asm/macio.h>
  5. #define macio_config_of_attr(field, format_string) \
  6. static ssize_t \
  7. field##_show (struct device *dev, struct device_attribute *attr, \
  8. char *buf) \
  9. { \
  10. struct macio_dev *mdev = to_macio_device (dev); \
  11. return sprintf (buf, format_string, mdev->ofdev.dev.of_node->field); \
  12. } \
  13. static DEVICE_ATTR_RO(field);
  14. static ssize_t
  15. compatible_show (struct device *dev, struct device_attribute *attr, char *buf)
  16. {
  17. struct platform_device *of;
  18. const char *compat;
  19. int cplen;
  20. int length = 0;
  21. of = &to_macio_device (dev)->ofdev;
  22. compat = of_get_property(of->dev.of_node, "compatible", &cplen);
  23. if (!compat) {
  24. *buf = '\0';
  25. return 0;
  26. }
  27. while (cplen > 0) {
  28. int l;
  29. length += sprintf (buf, "%s\n", compat);
  30. buf += length;
  31. l = strlen (compat) + 1;
  32. compat += l;
  33. cplen -= l;
  34. }
  35. return length;
  36. }
  37. static DEVICE_ATTR_RO(compatible);
  38. static ssize_t modalias_show (struct device *dev, struct device_attribute *attr,
  39. char *buf)
  40. {
  41. return of_device_modalias(dev, buf, PAGE_SIZE);
  42. }
  43. static ssize_t devspec_show(struct device *dev,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. struct platform_device *ofdev;
  47. ofdev = to_platform_device(dev);
  48. return sprintf(buf, "%pOF\n", ofdev->dev.of_node);
  49. }
  50. static DEVICE_ATTR_RO(modalias);
  51. static DEVICE_ATTR_RO(devspec);
  52. macio_config_of_attr (name, "%s\n");
  53. macio_config_of_attr (type, "%s\n");
  54. static struct attribute *macio_dev_attrs[] = {
  55. &dev_attr_name.attr,
  56. &dev_attr_type.attr,
  57. &dev_attr_compatible.attr,
  58. &dev_attr_modalias.attr,
  59. &dev_attr_devspec.attr,
  60. NULL,
  61. };
  62. static const struct attribute_group macio_dev_group = {
  63. .attrs = macio_dev_attrs,
  64. };
  65. const struct attribute_group *macio_dev_groups[] = {
  66. &macio_dev_group,
  67. NULL,
  68. };