GNU Linux-libre 4.9.287-gnu1
[releases.git] / drivers / soc / bcm / brcmstb / common.c
1 /*
2  * Copyright © 2014 NVIDIA Corporation
3  * Copyright © 2015 Broadcom Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/io.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/slab.h>
19 #include <linux/soc/brcmstb/brcmstb.h>
20 #include <linux/sys_soc.h>
21
22 #include <soc/brcmstb/common.h>
23
24 static u32 family_id;
25 static u32 product_id;
26
27 static const struct of_device_id brcmstb_machine_match[] = {
28         { .compatible = "brcm,brcmstb", },
29         { }
30 };
31
32 bool soc_is_brcmstb(void)
33 {
34         const struct of_device_id *match;
35         struct device_node *root;
36
37         root = of_find_node_by_path("/");
38         if (!root)
39                 return false;
40
41         match = of_match_node(brcmstb_machine_match, root);
42         of_node_put(root);
43
44         return match != NULL;
45 }
46
47 static const struct of_device_id sun_top_ctrl_match[] = {
48         { .compatible = "brcm,brcmstb-sun-top-ctrl", },
49         { }
50 };
51
52 static int __init brcmstb_soc_device_init(void)
53 {
54         struct soc_device_attribute *soc_dev_attr;
55         struct soc_device *soc_dev;
56         struct device_node *sun_top_ctrl;
57         void __iomem *sun_top_ctrl_base;
58         int ret = 0;
59
60         sun_top_ctrl = of_find_matching_node(NULL, sun_top_ctrl_match);
61         if (!sun_top_ctrl)
62                 return -ENODEV;
63
64         sun_top_ctrl_base = of_iomap(sun_top_ctrl, 0);
65         if (!sun_top_ctrl_base)
66                 return -ENODEV;
67
68         family_id = readl(sun_top_ctrl_base);
69         product_id = readl(sun_top_ctrl_base + 0x4);
70
71         soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
72         if (!soc_dev_attr) {
73                 ret = -ENOMEM;
74                 goto out;
75         }
76
77         soc_dev_attr->family = kasprintf(GFP_KERNEL, "%x",
78                                          family_id >> 28 ?
79                                          family_id >> 16 : family_id >> 8);
80         soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%x",
81                                          product_id >> 28 ?
82                                          product_id >> 16 : product_id >> 8);
83         soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%c%d",
84                                          ((product_id & 0xf0) >> 4) + 'A',
85                                            product_id & 0xf);
86
87         soc_dev = soc_device_register(soc_dev_attr);
88         if (IS_ERR(soc_dev)) {
89                 kfree(soc_dev_attr->family);
90                 kfree(soc_dev_attr->soc_id);
91                 kfree(soc_dev_attr->revision);
92                 kfree(soc_dev_attr);
93                 ret = -ENODEV;
94                 goto out;
95         }
96
97         return 0;
98
99 out:
100         iounmap(sun_top_ctrl_base);
101         return ret;
102 }
103 arch_initcall(brcmstb_soc_device_init);