soc.c 6.7 KB

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