soc.c 4.1 KB

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