1 /*
2  * Copyright (C) 2014 Linaro Ltd.
3  *
4  * Author: Linus Walleij <linus.walleij@linaro.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2, as
8  * published by the Free Software Foundation.
9  *
10  */
11 #include <linux/init.h>
12 #include <linux/io.h>
13 #include <linux/slab.h>
14 #include <linux/sys_soc.h>
15 #include <linux/platform_device.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/regmap.h>
18 #include <linux/of.h>
19 
20 /* System ID in syscon */
21 #define REALVIEW_SYS_ID_OFFSET 0x00
22 
23 static const struct of_device_id realview_soc_of_match[] = {
24 	{ .compatible = "arm,realview-eb-soc",	},
25 	{ .compatible = "arm,realview-pb1176-soc", },
26 	{ .compatible = "arm,realview-pb11mp-soc", },
27 	{ .compatible = "arm,realview-pba8-soc", },
28 	{ .compatible = "arm,realview-pbx-soc", },
29 	{ }
30 };
31 
32 static u32 realview_coreid;
33 
34 static const char *realview_board_str(u32 id)
35 {
36 	switch ((id >> 16) & 0xfff) {
37 	case 0x0147:
38 		return "HBI-0147";
39 	default:
40 		return "Unknown";
41 	}
42 }
43 
44 static const char *realview_arch_str(u32 id)
45 {
46 	switch ((id >> 8) & 0xf) {
47 	case 0x05:
48 		return "Multi-layer AXI";
49 	default:
50 		return "Unknown";
51 	}
52 }
53 
54 static ssize_t realview_get_manf(struct device *dev,
55 			      struct device_attribute *attr,
56 			      char *buf)
57 {
58 	return sprintf(buf, "%02x\n", realview_coreid >> 24);
59 }
60 
61 static struct device_attribute realview_manf_attr =
62 	__ATTR(manufacturer,  S_IRUGO, realview_get_manf,  NULL);
63 
64 static ssize_t realview_get_board(struct device *dev,
65 			      struct device_attribute *attr,
66 			      char *buf)
67 {
68 	return sprintf(buf, "%s\n", realview_board_str(realview_coreid));
69 }
70 
71 static struct device_attribute realview_board_attr =
72 	__ATTR(board,  S_IRUGO, realview_get_board,  NULL);
73 
74 static ssize_t realview_get_arch(struct device *dev,
75 			      struct device_attribute *attr,
76 			      char *buf)
77 {
78 	return sprintf(buf, "%s\n", realview_arch_str(realview_coreid));
79 }
80 
81 static struct device_attribute realview_arch_attr =
82 	__ATTR(fpga,  S_IRUGO, realview_get_arch,  NULL);
83 
84 static ssize_t realview_get_build(struct device *dev,
85 			       struct device_attribute *attr,
86 			       char *buf)
87 {
88 	return sprintf(buf, "%02x\n", (realview_coreid & 0xFF));
89 }
90 
91 static struct device_attribute realview_build_attr =
92 	__ATTR(build,  S_IRUGO, realview_get_build,  NULL);
93 
94 static int realview_soc_probe(struct platform_device *pdev)
95 {
96 	static struct regmap *syscon_regmap;
97 	struct soc_device *soc_dev;
98 	struct soc_device_attribute *soc_dev_attr;
99 	struct device_node *np = pdev->dev.of_node;
100 	int ret;
101 
102 	syscon_regmap = syscon_regmap_lookup_by_phandle(np, "regmap");
103 	if (IS_ERR(syscon_regmap))
104 		return PTR_ERR(syscon_regmap);
105 
106 	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
107 	if (!soc_dev_attr)
108 		return -ENOMEM;
109 
110 	ret = of_property_read_string(np, "compatible",
111 				      &soc_dev_attr->soc_id);
112 	if (ret)
113 		return -EINVAL;
114 
115 	soc_dev_attr->machine = "RealView";
116 	soc_dev_attr->family = "Versatile";
117 	soc_dev = soc_device_register(soc_dev_attr);
118 	if (IS_ERR(soc_dev)) {
119 		kfree(soc_dev_attr);
120 		return -ENODEV;
121 	}
122 	ret = regmap_read(syscon_regmap, REALVIEW_SYS_ID_OFFSET,
123 			  &realview_coreid);
124 	if (ret)
125 		return -ENODEV;
126 
127 	device_create_file(soc_device_to_device(soc_dev), &realview_manf_attr);
128 	device_create_file(soc_device_to_device(soc_dev), &realview_board_attr);
129 	device_create_file(soc_device_to_device(soc_dev), &realview_arch_attr);
130 	device_create_file(soc_device_to_device(soc_dev), &realview_build_attr);
131 
132 	dev_info(&pdev->dev, "RealView Syscon Core ID: 0x%08x\n",
133 		 realview_coreid);
134 	/* FIXME: add attributes for SoC to sysfs */
135 	return 0;
136 }
137 
138 static struct platform_driver realview_soc_driver = {
139 	.probe = realview_soc_probe,
140 	.driver = {
141 		.name = "realview-soc",
142 		.of_match_table = realview_soc_of_match,
143 	},
144 };
145 builtin_platform_driver(realview_soc_driver);
146