soc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/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. out2:
  125. kfree(soc_dev);
  126. out1:
  127. return ERR_PTR(ret);
  128. }
  129. EXPORT_SYMBOL_GPL(soc_device_register);
  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_simple_remove(&soc_ida, soc_dev->soc_dev_num);
  134. device_unregister(&soc_dev->dev);
  135. early_soc_dev_attr = NULL;
  136. }
  137. EXPORT_SYMBOL_GPL(soc_device_unregister);
  138. static int __init soc_bus_register(void)
  139. {
  140. int ret;
  141. ret = bus_register(&soc_bus_type);
  142. if (ret)
  143. return ret;
  144. if (early_soc_dev_attr)
  145. return PTR_ERR(soc_device_register(early_soc_dev_attr));
  146. return 0;
  147. }
  148. core_initcall(soc_bus_register);
  149. static int soc_device_match_attr(const struct soc_device_attribute *attr,
  150. const struct soc_device_attribute *match)
  151. {
  152. if (match->machine &&
  153. (!attr->machine || !glob_match(match->machine, attr->machine)))
  154. return 0;
  155. if (match->family &&
  156. (!attr->family || !glob_match(match->family, attr->family)))
  157. return 0;
  158. if (match->revision &&
  159. (!attr->revision || !glob_match(match->revision, attr->revision)))
  160. return 0;
  161. if (match->soc_id &&
  162. (!attr->soc_id || !glob_match(match->soc_id, attr->soc_id)))
  163. return 0;
  164. return 1;
  165. }
  166. static int soc_device_match_one(struct device *dev, void *arg)
  167. {
  168. struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
  169. return soc_device_match_attr(soc_dev->attr, arg);
  170. }
  171. /*
  172. * soc_device_match - identify the SoC in the machine
  173. * @matches: zero-terminated array of possible matches
  174. *
  175. * returns the first matching entry of the argument array, or NULL
  176. * if none of them match.
  177. *
  178. * This function is meant as a helper in place of of_match_node()
  179. * in cases where either no device tree is available or the information
  180. * in a device node is insufficient to identify a particular variant
  181. * by its compatible strings or other properties. For new devices,
  182. * the DT binding should always provide unique compatible strings
  183. * that allow the use of of_match_node() instead.
  184. *
  185. * The calling function can use the .data entry of the
  186. * soc_device_attribute to pass a structure or function pointer for
  187. * each entry.
  188. */
  189. const struct soc_device_attribute *soc_device_match(
  190. const struct soc_device_attribute *matches)
  191. {
  192. int ret = 0;
  193. if (!matches)
  194. return NULL;
  195. while (!ret) {
  196. if (!(matches->machine || matches->family ||
  197. matches->revision || matches->soc_id))
  198. break;
  199. ret = bus_for_each_dev(&soc_bus_type, NULL, (void *)matches,
  200. soc_device_match_one);
  201. if (ret < 0 && early_soc_dev_attr)
  202. ret = soc_device_match_attr(early_soc_dev_attr,
  203. matches);
  204. if (ret < 0)
  205. return NULL;
  206. if (!ret)
  207. matches++;
  208. else
  209. return matches;
  210. }
  211. return NULL;
  212. }
  213. EXPORT_SYMBOL_GPL(soc_device_match);