soc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/of.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. #include <linux/glob.h>
  17. static DEFINE_IDA(soc_ida);
  18. /* Prototype to allow declarations of DEVICE_ATTR(<foo>) before soc_info_show */
  19. static ssize_t soc_info_show(struct device *dev, 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 const struct bus_type soc_bus_type = {
  27. .name = "soc",
  28. };
  29. static bool soc_bus_registered;
  30. static DEVICE_ATTR(machine, 0444, soc_info_show, NULL);
  31. static DEVICE_ATTR(family, 0444, soc_info_show, NULL);
  32. static DEVICE_ATTR(serial_number, 0444, soc_info_show, NULL);
  33. static DEVICE_ATTR(soc_id, 0444, soc_info_show, NULL);
  34. static DEVICE_ATTR(revision, 0444, soc_info_show, NULL);
  35. struct device *soc_device_to_device(struct soc_device *soc_dev)
  36. {
  37. return &soc_dev->dev;
  38. }
  39. static umode_t soc_attribute_mode(struct kobject *kobj,
  40. struct attribute *attr,
  41. int index)
  42. {
  43. struct device *dev = kobj_to_dev(kobj);
  44. struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
  45. if ((attr == &dev_attr_machine.attr) && soc_dev->attr->machine)
  46. return attr->mode;
  47. if ((attr == &dev_attr_family.attr) && soc_dev->attr->family)
  48. return attr->mode;
  49. if ((attr == &dev_attr_revision.attr) && soc_dev->attr->revision)
  50. return attr->mode;
  51. if ((attr == &dev_attr_serial_number.attr) && soc_dev->attr->serial_number)
  52. return attr->mode;
  53. if ((attr == &dev_attr_soc_id.attr) && soc_dev->attr->soc_id)
  54. return attr->mode;
  55. /* Unknown or unfilled attribute */
  56. return 0;
  57. }
  58. static ssize_t soc_info_show(struct device *dev, struct device_attribute *attr,
  59. char *buf)
  60. {
  61. struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
  62. const char *output;
  63. if (attr == &dev_attr_machine)
  64. output = soc_dev->attr->machine;
  65. else if (attr == &dev_attr_family)
  66. output = soc_dev->attr->family;
  67. else if (attr == &dev_attr_revision)
  68. output = soc_dev->attr->revision;
  69. else if (attr == &dev_attr_serial_number)
  70. output = soc_dev->attr->serial_number;
  71. else if (attr == &dev_attr_soc_id)
  72. output = soc_dev->attr->soc_id;
  73. else
  74. return -EINVAL;
  75. return sysfs_emit(buf, "%s\n", output);
  76. }
  77. static struct attribute *soc_attr[] = {
  78. &dev_attr_machine.attr,
  79. &dev_attr_family.attr,
  80. &dev_attr_serial_number.attr,
  81. &dev_attr_soc_id.attr,
  82. &dev_attr_revision.attr,
  83. NULL,
  84. };
  85. static const struct attribute_group soc_attr_group = {
  86. .attrs = soc_attr,
  87. .is_visible = soc_attribute_mode,
  88. };
  89. static void soc_release(struct device *dev)
  90. {
  91. struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
  92. ida_free(&soc_ida, soc_dev->soc_dev_num);
  93. kfree(soc_dev->dev.groups);
  94. kfree(soc_dev);
  95. }
  96. static void soc_device_get_machine(struct soc_device_attribute *soc_dev_attr)
  97. {
  98. struct device_node *np;
  99. if (soc_dev_attr->machine)
  100. return;
  101. np = of_find_node_by_path("/");
  102. of_property_read_string(np, "model", &soc_dev_attr->machine);
  103. of_node_put(np);
  104. }
  105. static struct soc_device_attribute *early_soc_dev_attr;
  106. struct soc_device *soc_device_register(struct soc_device_attribute *soc_dev_attr)
  107. {
  108. struct soc_device *soc_dev;
  109. const struct attribute_group **soc_attr_groups;
  110. int ret;
  111. soc_device_get_machine(soc_dev_attr);
  112. if (!soc_bus_registered) {
  113. if (early_soc_dev_attr)
  114. return ERR_PTR(-EBUSY);
  115. early_soc_dev_attr = soc_dev_attr;
  116. return NULL;
  117. }
  118. soc_dev = kzalloc(sizeof(*soc_dev), GFP_KERNEL);
  119. if (!soc_dev) {
  120. ret = -ENOMEM;
  121. goto out1;
  122. }
  123. soc_attr_groups = kcalloc(3, sizeof(*soc_attr_groups), GFP_KERNEL);
  124. if (!soc_attr_groups) {
  125. ret = -ENOMEM;
  126. goto out2;
  127. }
  128. soc_attr_groups[0] = &soc_attr_group;
  129. soc_attr_groups[1] = soc_dev_attr->custom_attr_group;
  130. /* Fetch a unique (reclaimable) SOC ID. */
  131. ret = ida_alloc(&soc_ida, GFP_KERNEL);
  132. if (ret < 0)
  133. goto out3;
  134. soc_dev->soc_dev_num = ret;
  135. soc_dev->attr = soc_dev_attr;
  136. soc_dev->dev.bus = &soc_bus_type;
  137. soc_dev->dev.groups = soc_attr_groups;
  138. soc_dev->dev.release = soc_release;
  139. dev_set_name(&soc_dev->dev, "soc%d", soc_dev->soc_dev_num);
  140. ret = device_register(&soc_dev->dev);
  141. if (ret) {
  142. put_device(&soc_dev->dev);
  143. return ERR_PTR(ret);
  144. }
  145. return soc_dev;
  146. out3:
  147. kfree(soc_attr_groups);
  148. out2:
  149. kfree(soc_dev);
  150. out1:
  151. return ERR_PTR(ret);
  152. }
  153. EXPORT_SYMBOL_GPL(soc_device_register);
  154. /* Ensure soc_dev->attr is freed after calling soc_device_unregister. */
  155. void soc_device_unregister(struct soc_device *soc_dev)
  156. {
  157. device_unregister(&soc_dev->dev);
  158. early_soc_dev_attr = NULL;
  159. }
  160. EXPORT_SYMBOL_GPL(soc_device_unregister);
  161. static int __init soc_bus_register(void)
  162. {
  163. int ret;
  164. ret = bus_register(&soc_bus_type);
  165. if (ret)
  166. return ret;
  167. soc_bus_registered = true;
  168. if (early_soc_dev_attr)
  169. return PTR_ERR(soc_device_register(early_soc_dev_attr));
  170. return 0;
  171. }
  172. core_initcall(soc_bus_register);
  173. static int soc_device_match_attr(const struct soc_device_attribute *attr,
  174. const struct soc_device_attribute *match)
  175. {
  176. if (match->machine &&
  177. (!attr->machine || !glob_match(match->machine, attr->machine)))
  178. return 0;
  179. if (match->family &&
  180. (!attr->family || !glob_match(match->family, attr->family)))
  181. return 0;
  182. if (match->revision &&
  183. (!attr->revision || !glob_match(match->revision, attr->revision)))
  184. return 0;
  185. if (match->soc_id &&
  186. (!attr->soc_id || !glob_match(match->soc_id, attr->soc_id)))
  187. return 0;
  188. return 1;
  189. }
  190. static int soc_device_match_one(struct device *dev, void *arg)
  191. {
  192. struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
  193. return soc_device_match_attr(soc_dev->attr, arg);
  194. }
  195. /*
  196. * soc_device_match - identify the SoC in the machine
  197. * @matches: zero-terminated array of possible matches
  198. *
  199. * returns the first matching entry of the argument array, or NULL
  200. * if none of them match.
  201. *
  202. * This function is meant as a helper in place of of_match_node()
  203. * in cases where either no device tree is available or the information
  204. * in a device node is insufficient to identify a particular variant
  205. * by its compatible strings or other properties. For new devices,
  206. * the DT binding should always provide unique compatible strings
  207. * that allow the use of of_match_node() instead.
  208. *
  209. * The calling function can use the .data entry of the
  210. * soc_device_attribute to pass a structure or function pointer for
  211. * each entry.
  212. */
  213. const struct soc_device_attribute *soc_device_match(
  214. const struct soc_device_attribute *matches)
  215. {
  216. int ret;
  217. if (!matches)
  218. return NULL;
  219. while (matches->machine || matches->family || matches->revision ||
  220. matches->soc_id) {
  221. ret = bus_for_each_dev(&soc_bus_type, NULL, (void *)matches,
  222. soc_device_match_one);
  223. if (ret < 0 && early_soc_dev_attr)
  224. ret = soc_device_match_attr(early_soc_dev_attr,
  225. matches);
  226. if (ret < 0)
  227. return NULL;
  228. if (ret)
  229. return matches;
  230. matches++;
  231. }
  232. return NULL;
  233. }
  234. EXPORT_SYMBOL_GPL(soc_device_match);