Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-microblaze.git] / drivers / soc / imx / soc-imx.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020 NXP
4  */
5
6 #include <linux/mfd/syscon.h>
7 #include <linux/of.h>
8 #include <linux/of_address.h>
9 #include <linux/regmap.h>
10 #include <linux/slab.h>
11 #include <linux/sys_soc.h>
12
13 #include <soc/imx/cpu.h>
14 #include <soc/imx/revision.h>
15
16 #define OCOTP_UID_H     0x420
17 #define OCOTP_UID_L     0x410
18
19 #define OCOTP_ULP_UID_1         0x4b0
20 #define OCOTP_ULP_UID_2         0x4c0
21 #define OCOTP_ULP_UID_3         0x4d0
22 #define OCOTP_ULP_UID_4         0x4e0
23
24 static int __init imx_soc_device_init(void)
25 {
26         struct soc_device_attribute *soc_dev_attr;
27         const char *ocotp_compat = NULL;
28         struct soc_device *soc_dev;
29         struct device_node *root;
30         struct regmap *ocotp = NULL;
31         const char *soc_id;
32         u64 soc_uid = 0;
33         u32 val;
34         int ret;
35
36         if (of_machine_is_compatible("fsl,ls1021a"))
37                 return 0;
38
39         soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
40         if (!soc_dev_attr)
41                 return -ENOMEM;
42
43         soc_dev_attr->family = "Freescale i.MX";
44
45         root = of_find_node_by_path("/");
46         ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
47         of_node_put(root);
48         if (ret)
49                 goto free_soc;
50
51         switch (__mxc_cpu_type) {
52         case MXC_CPU_MX1:
53                 soc_id = "i.MX1";
54                 break;
55         case MXC_CPU_MX21:
56                 soc_id = "i.MX21";
57                 break;
58         case MXC_CPU_MX25:
59                 soc_id = "i.MX25";
60                 break;
61         case MXC_CPU_MX27:
62                 soc_id = "i.MX27";
63                 break;
64         case MXC_CPU_MX31:
65                 soc_id = "i.MX31";
66                 break;
67         case MXC_CPU_MX35:
68                 soc_id = "i.MX35";
69                 break;
70         case MXC_CPU_MX51:
71                 soc_id = "i.MX51";
72                 break;
73         case MXC_CPU_MX53:
74                 soc_id = "i.MX53";
75                 break;
76         case MXC_CPU_IMX6SL:
77                 ocotp_compat = "fsl,imx6sl-ocotp";
78                 soc_id = "i.MX6SL";
79                 break;
80         case MXC_CPU_IMX6DL:
81                 ocotp_compat = "fsl,imx6q-ocotp";
82                 soc_id = "i.MX6DL";
83                 break;
84         case MXC_CPU_IMX6SX:
85                 ocotp_compat = "fsl,imx6sx-ocotp";
86                 soc_id = "i.MX6SX";
87                 break;
88         case MXC_CPU_IMX6Q:
89                 ocotp_compat = "fsl,imx6q-ocotp";
90                 soc_id = "i.MX6Q";
91                 break;
92         case MXC_CPU_IMX6UL:
93                 ocotp_compat = "fsl,imx6ul-ocotp";
94                 soc_id = "i.MX6UL";
95                 break;
96         case MXC_CPU_IMX6ULL:
97                 ocotp_compat = "fsl,imx6ull-ocotp";
98                 soc_id = "i.MX6ULL";
99                 break;
100         case MXC_CPU_IMX6ULZ:
101                 ocotp_compat = "fsl,imx6ull-ocotp";
102                 soc_id = "i.MX6ULZ";
103                 break;
104         case MXC_CPU_IMX6SLL:
105                 ocotp_compat = "fsl,imx6sll-ocotp";
106                 soc_id = "i.MX6SLL";
107                 break;
108         case MXC_CPU_IMX7D:
109                 ocotp_compat = "fsl,imx7d-ocotp";
110                 soc_id = "i.MX7D";
111                 break;
112         case MXC_CPU_IMX7ULP:
113                 ocotp_compat = "fsl,imx7ulp-ocotp";
114                 soc_id = "i.MX7ULP";
115                 break;
116         case MXC_CPU_VF500:
117                 ocotp_compat = "fsl,vf610-ocotp";
118                 soc_id = "VF500";
119                 break;
120         case MXC_CPU_VF510:
121                 ocotp_compat = "fsl,vf610-ocotp";
122                 soc_id = "VF510";
123                 break;
124         case MXC_CPU_VF600:
125                 ocotp_compat = "fsl,vf610-ocotp";
126                 soc_id = "VF600";
127                 break;
128         case MXC_CPU_VF610:
129                 ocotp_compat = "fsl,vf610-ocotp";
130                 soc_id = "VF610";
131                 break;
132         default:
133                 soc_id = "Unknown";
134         }
135         soc_dev_attr->soc_id = soc_id;
136
137         if (ocotp_compat) {
138                 ocotp = syscon_regmap_lookup_by_compatible(ocotp_compat);
139                 if (IS_ERR(ocotp))
140                         pr_err("%s: failed to find %s regmap!\n", __func__, ocotp_compat);
141         }
142
143         if (!IS_ERR_OR_NULL(ocotp)) {
144                 if (__mxc_cpu_type == MXC_CPU_IMX7ULP) {
145                         regmap_read(ocotp, OCOTP_ULP_UID_4, &val);
146                         soc_uid = val & 0xffff;
147                         regmap_read(ocotp, OCOTP_ULP_UID_3, &val);
148                         soc_uid <<= 16;
149                         soc_uid |= val & 0xffff;
150                         regmap_read(ocotp, OCOTP_ULP_UID_2, &val);
151                         soc_uid <<= 16;
152                         soc_uid |= val & 0xffff;
153                         regmap_read(ocotp, OCOTP_ULP_UID_1, &val);
154                         soc_uid <<= 16;
155                         soc_uid |= val & 0xffff;
156                 } else {
157                         regmap_read(ocotp, OCOTP_UID_H, &val);
158                         soc_uid = val;
159                         regmap_read(ocotp, OCOTP_UID_L, &val);
160                         soc_uid <<= 32;
161                         soc_uid |= val;
162                 }
163         }
164
165         soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%d.%d",
166                                            (imx_get_soc_revision() >> 4) & 0xf,
167                                            imx_get_soc_revision() & 0xf);
168         if (!soc_dev_attr->revision) {
169                 ret = -ENOMEM;
170                 goto free_soc;
171         }
172
173         soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", soc_uid);
174         if (!soc_dev_attr->serial_number) {
175                 ret = -ENOMEM;
176                 goto free_rev;
177         }
178
179         soc_dev = soc_device_register(soc_dev_attr);
180         if (IS_ERR(soc_dev)) {
181                 ret = PTR_ERR(soc_dev);
182                 goto free_serial_number;
183         }
184
185         return 0;
186
187 free_serial_number:
188         kfree(soc_dev_attr->serial_number);
189 free_rev:
190         kfree(soc_dev_attr->revision);
191 free_soc:
192         kfree(soc_dev_attr);
193         return ret;
194 }
195 device_initcall(imx_soc_device_init);