soc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2011
  4. *
  5. * Author: Lee Jones <lee.jones@linaro.org> for ST-Ericsson.
  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. #include <linux/glob.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. static struct soc_device_attribute *early_soc_dev_attr;
  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. if (!soc_bus_type.p) {
  98. if (early_soc_dev_attr)
  99. return ERR_PTR(-EBUSY);
  100. early_soc_dev_attr = soc_dev_attr;
  101. return NULL;
  102. }
  103. soc_dev = kzalloc(sizeof(*soc_dev), GFP_KERNEL);
  104. if (!soc_dev) {
  105. ret = -ENOMEM;
  106. goto out1;
  107. }
  108. /* Fetch a unique (reclaimable) SOC ID. */
  109. ret = ida_simple_get(&soc_ida, 0, 0, GFP_KERNEL);
  110. if (ret < 0)
  111. goto out2;
  112. soc_dev->soc_dev_num = ret;
  113. soc_dev->attr = soc_dev_attr;
  114. soc_dev->dev.bus = &soc_bus_type;
  115. soc_dev->dev.groups = soc_attr_groups;
  116. soc_dev->dev.release = soc_release;
  117. dev_set_name(&soc_dev->dev, "soc%d", soc_dev->soc_dev_num);
  118. ret = device_register(&soc_dev->dev);
  119. if (ret)
  120. goto out3;
  121. return soc_dev;
  122. out3:
  123. ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
  124. put_device(&soc_dev->dev);
  125. soc_dev = NULL;
  126. out2:
  127. kfree(soc_dev);
  128. out1:
  129. return ERR_PTR(ret);
  130. }
  131. EXPORT_SYMBOL_GPL(soc_device_register);
  132. /* Ensure soc_dev->attr is freed prior to calling soc_device_unregister. */
  133. void soc_device_unregister(struct soc_device *soc_dev)
  134. {
  135. ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
  136. device_unregister(&soc_dev->dev);
  137. early_soc_dev_attr = NULL;
  138. }
  139. EXPORT_SYMBOL_GPL(soc_device_unregister);
  140. static int __init soc_bus_register(void)
  141. {
  142. int ret;
  143. ret = bus_register(&soc_bus_type);
  144. if (ret)
  145. return ret;
  146. if (early_soc_dev_attr)
  147. return PTR_ERR(soc_device_register(early_soc_dev_attr));
  148. return 0;
  149. }
  150. core_initcall(soc_bus_register);
  151. static int soc_device_match_attr(const struct soc_device_attribute *attr,
  152. const struct soc_device_attribute *match)
  153. {
  154. if (match->machine &&
  155. (!attr->machine || !glob_match(match->machine, attr->machine)))
  156. return 0;
  157. if (match->family &&
  158. (!attr->family || !glob_match(match->family, attr->family)))
  159. return 0;
  160. if (match->revision &&
  161. (!attr->revision || !glob_match(match->revision, attr->revision)))
  162. return 0;
  163. if (match->soc_id &&
  164. (!attr->soc_id || !glob_match(match->soc_id, attr->soc_id)))
  165. return 0;
  166. return 1;
  167. }
  168. static int soc_device_match_one(struct device *dev, void *arg)
  169. {
  170. struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
  171. return soc_device_match_attr(soc_dev->attr, arg);
  172. }
  173. /*
  174. * soc_device_match - identify the SoC in the machine
  175. * @matches: zero-terminated array of possible matches
  176. *
  177. * returns the first matching entry of the argument array, or NULL
  178. * if none of them match.
  179. *
  180. * This function is meant as a helper in place of of_match_node()
  181. * in cases where either no device tree is available or the information
  182. * in a device node is insufficient to identify a particular variant
  183. * by its compatible strings or other properties. For new devices,
  184. * the DT binding should always provide unique compatible strings
  185. * that allow the use of of_match_node() instead.
  186. *
  187. * The calling function can use the .data entry of the
  188. * soc_device_attribute to pass a structure or function pointer for
  189. * each entry.
  190. */
  191. const struct soc_device_attribute *soc_device_match(
  192. const struct soc_device_attribute *matches)
  193. {
  194. int ret = 0;
  195. if (!matches)
  196. return NULL;
  197. while (!ret) {
  198. if (!(matches->machine || matches->family ||
  199. matches->revision || matches->soc_id))
  200. break;
  201. ret = bus_for_each_dev(&soc_bus_type, NULL, (void *)matches,
  202. soc_device_match_one);
  203. if (ret < 0 && early_soc_dev_attr)
  204. ret = soc_device_match_attr(early_soc_dev_attr,
  205. matches);
  206. if (ret < 0)
  207. return NULL;
  208. if (!ret)
  209. matches++;
  210. else
  211. return matches;
  212. }
  213. return NULL;
  214. }
  215. EXPORT_SYMBOL_GPL(soc_device_match);