^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) ST-Ericsson SA 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: Lee Jones <lee.jones@linaro.org> for ST-Ericsson.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sys_soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/glob.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static DEFINE_IDA(soc_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* Prototype to allow declarations of DEVICE_ATTR(<foo>) before soc_info_show */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static ssize_t soc_info_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) char *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct soc_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct soc_device_attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int soc_dev_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static struct bus_type soc_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .name = "soc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static DEVICE_ATTR(machine, 0444, soc_info_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static DEVICE_ATTR(family, 0444, soc_info_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static DEVICE_ATTR(serial_number, 0444, soc_info_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static DEVICE_ATTR(soc_id, 0444, soc_info_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static DEVICE_ATTR(revision, 0444, soc_info_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct device *soc_device_to_device(struct soc_device *soc_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return &soc_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static umode_t soc_attribute_mode(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if ((attr == &dev_attr_machine.attr) && soc_dev->attr->machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if ((attr == &dev_attr_family.attr) && soc_dev->attr->family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if ((attr == &dev_attr_revision.attr) && soc_dev->attr->revision)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if ((attr == &dev_attr_serial_number.attr) && soc_dev->attr->serial_number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if ((attr == &dev_attr_soc_id.attr) && soc_dev->attr->soc_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* Unknown or unfilled attribute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static ssize_t soc_info_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const char *output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (attr == &dev_attr_machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) output = soc_dev->attr->machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) else if (attr == &dev_attr_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) output = soc_dev->attr->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) else if (attr == &dev_attr_revision)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) output = soc_dev->attr->revision;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) else if (attr == &dev_attr_serial_number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) output = soc_dev->attr->serial_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) else if (attr == &dev_attr_soc_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) output = soc_dev->attr->soc_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return sysfs_emit(buf, "%s\n", output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static struct attribute *soc_attr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) &dev_attr_machine.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) &dev_attr_family.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) &dev_attr_serial_number.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) &dev_attr_soc_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) &dev_attr_revision.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static const struct attribute_group soc_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .attrs = soc_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .is_visible = soc_attribute_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static void soc_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) kfree(soc_dev->dev.groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) kfree(soc_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static struct soc_device_attribute *early_soc_dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct soc_device *soc_device_register(struct soc_device_attribute *soc_dev_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct soc_device *soc_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) const struct attribute_group **soc_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!soc_bus_type.p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (early_soc_dev_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) early_soc_dev_attr = soc_dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) soc_dev = kzalloc(sizeof(*soc_dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!soc_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) soc_attr_groups = kcalloc(3, sizeof(*soc_attr_groups), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!soc_attr_groups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) soc_attr_groups[0] = &soc_attr_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) soc_attr_groups[1] = soc_dev_attr->custom_attr_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Fetch a unique (reclaimable) SOC ID. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ret = ida_simple_get(&soc_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) goto out3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) soc_dev->soc_dev_num = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) soc_dev->attr = soc_dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) soc_dev->dev.bus = &soc_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) soc_dev->dev.groups = soc_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) soc_dev->dev.release = soc_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) dev_set_name(&soc_dev->dev, "soc%d", soc_dev->soc_dev_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ret = device_register(&soc_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) put_device(&soc_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return soc_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) out3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) kfree(soc_attr_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) kfree(soc_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) EXPORT_SYMBOL_GPL(soc_device_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* Ensure soc_dev->attr is freed prior to calling soc_device_unregister. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) void soc_device_unregister(struct soc_device *soc_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) device_unregister(&soc_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) early_soc_dev_attr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) EXPORT_SYMBOL_GPL(soc_device_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int __init soc_bus_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = bus_register(&soc_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (early_soc_dev_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return PTR_ERR(soc_device_register(early_soc_dev_attr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) core_initcall(soc_bus_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int soc_device_match_attr(const struct soc_device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) const struct soc_device_attribute *match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (match->machine &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) (!attr->machine || !glob_match(match->machine, attr->machine)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (match->family &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) (!attr->family || !glob_match(match->family, attr->family)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (match->revision &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) (!attr->revision || !glob_match(match->revision, attr->revision)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (match->soc_id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) (!attr->soc_id || !glob_match(match->soc_id, attr->soc_id)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int soc_device_match_one(struct device *dev, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return soc_device_match_attr(soc_dev->attr, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * soc_device_match - identify the SoC in the machine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * @matches: zero-terminated array of possible matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * returns the first matching entry of the argument array, or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * if none of them match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * This function is meant as a helper in place of of_match_node()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * in cases where either no device tree is available or the information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * in a device node is insufficient to identify a particular variant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * by its compatible strings or other properties. For new devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * the DT binding should always provide unique compatible strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * that allow the use of of_match_node() instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * The calling function can use the .data entry of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * soc_device_attribute to pass a structure or function pointer for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * each entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) const struct soc_device_attribute *soc_device_match(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) const struct soc_device_attribute *matches)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!matches)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) while (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!(matches->machine || matches->family ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) matches->revision || matches->soc_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ret = bus_for_each_dev(&soc_bus_type, NULL, (void *)matches,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) soc_device_match_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (ret < 0 && early_soc_dev_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) ret = soc_device_match_attr(early_soc_dev_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) matches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return matches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) EXPORT_SYMBOL_GPL(soc_device_match);