soc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2011
  3. *
  4. * Author: Lee Jones <lee.jones@linaro.org> for ST-Ericsson.
  5. * License terms: GNU General Public License (GPL), version 2
  6. */
  7. #include <linux/sysfs.h>
  8. #include <linux/init.h>
  9. #include <linux/stat.h>
  10. #include <linux/slab.h>
  11. #include <linux/idr.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/sys_soc.h>
  14. #include <linux/err.h>
  15. static DEFINE_IDA(soc_ida);
  16. static ssize_t soc_info_get(struct device *dev,
  17. struct device_attribute *attr,
  18. char *buf);
  19. struct soc_device {
  20. struct device dev;
  21. struct soc_device_attribute *attr;
  22. int soc_dev_num;
  23. };
  24. static struct bus_type soc_bus_type = {
  25. .name = "soc",
  26. };
  27. static DEVICE_ATTR(machine, S_IRUGO, soc_info_get, NULL);
  28. static DEVICE_ATTR(family, S_IRUGO, soc_info_get, NULL);
  29. static DEVICE_ATTR(soc_id, S_IRUGO, soc_info_get, NULL);
  30. static DEVICE_ATTR(revision, S_IRUGO, soc_info_get, NULL);
  31. struct device *soc_device_to_device(struct soc_device *soc_dev)
  32. {
  33. return &soc_dev->dev;
  34. }
  35. static umode_t soc_attribute_mode(struct kobject *kobj,
  36. struct attribute *attr,
  37. int index)
  38. {
  39. struct device *dev = container_of(kobj, struct device, kobj);
  40. struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
  41. if ((attr == &dev_attr_machine.attr)
  42. && (soc_dev->attr->machine != NULL))
  43. return attr->mode;
  44. if ((attr == &dev_attr_family.attr)
  45. && (soc_dev->attr->family != NULL))
  46. return attr->mode;
  47. if ((attr == &dev_attr_revision.attr)
  48. && (soc_dev->attr->revision != NULL))
  49. return attr->mode;
  50. if ((attr == &dev_attr_soc_id.attr)
  51. && (soc_dev->attr->soc_id != NULL))
  52. return attr->mode;
  53. /* Unknown or unfilled attribute. */
  54. return 0;
  55. }
  56. static ssize_t soc_info_get(struct device *dev,
  57. struct device_attribute *attr,
  58. char *buf)
  59. {
  60. struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
  61. if (attr == &dev_attr_machine)
  62. return sprintf(buf, "%s\n", soc_dev->attr->machine);
  63. if (attr == &dev_attr_family)
  64. return sprintf(buf, "%s\n", soc_dev->attr->family);
  65. if (attr == &dev_attr_revision)
  66. return sprintf(buf, "%s\n", soc_dev->attr->revision);
  67. if (attr == &dev_attr_soc_id)
  68. return sprintf(buf, "%s\n", soc_dev->attr->soc_id);
  69. return -EINVAL;
  70. }
  71. static struct attribute *soc_attr[] = {
  72. &dev_attr_machine.attr,
  73. &dev_attr_family.attr,
  74. &dev_attr_soc_id.attr,
  75. &dev_attr_revision.attr,
  76. NULL,
  77. };
  78. static const struct attribute_group soc_attr_group = {
  79. .attrs = soc_attr,
  80. .is_visible = soc_attribute_mode,
  81. };
  82. static const struct attribute_group *soc_attr_groups[] = {
  83. &soc_attr_group,
  84. NULL,
  85. };
  86. static void soc_release(struct device *dev)
  87. {
  88. struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
  89. kfree(soc_dev);
  90. }
  91. struct soc_device *soc_device_register(struct soc_device_attribute *soc_dev_attr)
  92. {
  93. struct soc_device *soc_dev;
  94. int ret;
  95. soc_dev = kzalloc(sizeof(*soc_dev), GFP_KERNEL);
  96. if (!soc_dev) {
  97. ret = -ENOMEM;
  98. goto out1;
  99. }
  100. /* Fetch a unique (reclaimable) SOC ID. */
  101. ret = ida_simple_get(&soc_ida, 0, 0, GFP_KERNEL);
  102. if (ret < 0)
  103. goto out2;
  104. soc_dev->soc_dev_num = ret;
  105. soc_dev->attr = soc_dev_attr;
  106. soc_dev->dev.bus = &soc_bus_type;
  107. soc_dev->dev.groups = soc_attr_groups;
  108. soc_dev->dev.release = soc_release;
  109. dev_set_name(&soc_dev->dev, "soc%d", soc_dev->soc_dev_num);
  110. ret = device_register(&soc_dev->dev);
  111. if (ret)
  112. goto out3;
  113. return soc_dev;
  114. out3:
  115. ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
  116. out2:
  117. kfree(soc_dev);
  118. out1:
  119. return ERR_PTR(ret);
  120. }
  121. /* Ensure soc_dev->attr is freed prior to calling soc_device_unregister. */
  122. void soc_device_unregister(struct soc_device *soc_dev)
  123. {
  124. ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
  125. device_unregister(&soc_dev->dev);
  126. }
  127. static int __init soc_bus_register(void)
  128. {
  129. return bus_register(&soc_bus_type);
  130. }
  131. core_initcall(soc_bus_register);