soc.c 3.9 KB

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