summaryrefslogtreecommitdiff
path: root/drivers/soc/bcm/brcmstb/common.c
blob: 7be0374f59430d78b69ef35f6ccac343bb576f16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright © 2014 NVIDIA Corporation
 * Copyright © 2015 Broadcom Corporation
 */

#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/slab.h>
#include <linux/soc/brcmstb/brcmstb.h>
#include <linux/sys_soc.h>

struct brcmstb_soc_info {
	u32 family_id;
	u32 product_id;
};

static struct brcmstb_soc_info *soc_info;

u32 brcmstb_get_family_id(void)
{
	return soc_info ? soc_info->family_id : 0;
}
EXPORT_SYMBOL(brcmstb_get_family_id);

u32 brcmstb_get_product_id(void)
{
	return soc_info ? soc_info->product_id : 0;
}
EXPORT_SYMBOL(brcmstb_get_product_id);

static const struct of_device_id sun_top_ctrl_match[] = {
	{ .compatible = "brcm,bcm7125-sun-top-ctrl", },
	{ .compatible = "brcm,bcm7346-sun-top-ctrl", },
	{ .compatible = "brcm,bcm7358-sun-top-ctrl", },
	{ .compatible = "brcm,bcm7360-sun-top-ctrl", },
	{ .compatible = "brcm,bcm7362-sun-top-ctrl", },
	{ .compatible = "brcm,bcm7420-sun-top-ctrl", },
	{ .compatible = "brcm,bcm7425-sun-top-ctrl", },
	{ .compatible = "brcm,bcm7429-sun-top-ctrl", },
	{ .compatible = "brcm,bcm7435-sun-top-ctrl", },
	{ .compatible = "brcm,brcmstb-sun-top-ctrl", },
	{ }
};

static int __init brcmstb_soc_device_init(void)
{
	struct soc_device_attribute *soc_dev_attr;
	struct device_node *sun_top_ctrl;
	void __iomem *sun_top_ctrl_base;
	struct soc_device *soc_dev;
	int ret = 0;

	/* We could be on a multi-platform kernel, don't make this fatal but
	 * bail out early
	 */
	sun_top_ctrl = of_find_matching_node(NULL, sun_top_ctrl_match);
	if (!sun_top_ctrl)
		return 0;

	sun_top_ctrl_base = of_iomap(sun_top_ctrl, 0);
	if (!sun_top_ctrl_base) {
		ret = -ENODEV;
		goto out_put_node;
	}

	soc_info = kzalloc(sizeof(*soc_info), GFP_KERNEL);
	if (!soc_info) {
		ret = -ENOMEM;
		goto out_unmap;
	}

	soc_info->family_id = readl(sun_top_ctrl_base);
	soc_info->product_id = readl(sun_top_ctrl_base + 0x4);

	soc_dev_attr = kzalloc_obj(*soc_dev_attr);
	if (!soc_dev_attr) {
		ret = -ENOMEM;
		goto out_free_info;
	}

	soc_dev_attr->family = kasprintf(GFP_KERNEL, "%x",
					 soc_info->family_id >> 28 ?
					 soc_info->family_id >> 16 : soc_info->family_id >> 8);
	soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%x",
					 soc_info->product_id >> 28 ?
					 soc_info->product_id >> 16 : soc_info->product_id >> 8);
	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%c%d",
					 ((soc_info->product_id & 0xf0) >> 4) + 'A',
					   soc_info->product_id & 0xf);

	soc_dev = soc_device_register(soc_dev_attr);
	if (IS_ERR(soc_dev)) {
		ret = PTR_ERR(soc_dev);
		goto out_free_attr;
	}

	iounmap(sun_top_ctrl_base);
	of_node_put(sun_top_ctrl);
	return 0;

out_free_attr:
	kfree(soc_dev_attr->revision);
	kfree(soc_dev_attr->soc_id);
	kfree(soc_dev_attr->family);
	kfree(soc_dev_attr);
out_free_info:
	kfree(soc_info);
	soc_info = NULL;
out_unmap:
	iounmap(sun_top_ctrl_base);
out_put_node:
	of_node_put(sun_top_ctrl);
	return ret;
}
early_initcall(brcmstb_soc_device_init);