1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
5 * Copyright (c) 2020 Krzysztof Kozlowski <[email protected]>
6 *
7 * Exynos - CHIP ID support
8 * Author: Pankaj Dubey <[email protected]>
9 * Author: Bartlomiej Zolnierkiewicz <[email protected]>
10 * Author: Krzysztof Kozlowski <[email protected]>
11 *
12 * Samsung Exynos SoC Adaptive Supply Voltage and Chip ID support
13 */
14
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/soc/samsung/exynos-chipid.h>
24 #include <linux/sys_soc.h>
25
26 #include "exynos-asv.h"
27
28 struct exynos_chipid_variant {
29 unsigned int rev_reg; /* revision register offset */
30 unsigned int main_rev_shift; /* main revision offset in rev_reg */
31 unsigned int sub_rev_shift; /* sub revision offset in rev_reg */
32 };
33
34 struct exynos_chipid_info {
35 u32 product_id;
36 u32 revision;
37 };
38
39 static const struct exynos_soc_id {
40 const char *name;
41 unsigned int id;
42 } soc_ids[] = {
43 /* List ordered by SoC name */
44 /* Compatible with: samsung,exynos4210-chipid */
45 { "EXYNOS3250", 0xE3472000 },
46 { "EXYNOS4210", 0x43200000 }, /* EVT0 revision */
47 { "EXYNOS4210", 0x43210000 },
48 { "EXYNOS4212", 0x43220000 },
49 { "EXYNOS4412", 0xE4412000 },
50 { "EXYNOS5250", 0x43520000 },
51 { "EXYNOS5260", 0xE5260000 },
52 { "EXYNOS5410", 0xE5410000 },
53 { "EXYNOS5420", 0xE5420000 },
54 { "EXYNOS5433", 0xE5433000 },
55 { "EXYNOS5440", 0xE5440000 },
56 { "EXYNOS5800", 0xE5422000 },
57 { "EXYNOS7420", 0xE7420000 },
58 /* Compatible with: samsung,exynos850-chipid */
59 { "EXYNOS7885", 0xE7885000 },
60 { "EXYNOS850", 0xE3830000 },
61 { "EXYNOS8895", 0xE8895000 },
62 { "EXYNOS9810", 0xE9810000 },
63 { "EXYNOS990", 0xE9830000 },
64 { "EXYNOSAUTOV9", 0xAAA80000 },
65 { "EXYNOSAUTOV920", 0x0A920000 },
66 };
67
product_id_to_soc_id(unsigned int product_id)68 static const char *product_id_to_soc_id(unsigned int product_id)
69 {
70 int i;
71
72 for (i = 0; i < ARRAY_SIZE(soc_ids); i++)
73 if (product_id == soc_ids[i].id)
74 return soc_ids[i].name;
75 return NULL;
76 }
77
exynos_chipid_get_chipid_info(struct regmap * regmap,const struct exynos_chipid_variant * data,struct exynos_chipid_info * soc_info)78 static int exynos_chipid_get_chipid_info(struct regmap *regmap,
79 const struct exynos_chipid_variant *data,
80 struct exynos_chipid_info *soc_info)
81 {
82 int ret;
83 unsigned int val, main_rev, sub_rev;
84
85 ret = regmap_read(regmap, EXYNOS_CHIPID_REG_PRO_ID, &val);
86 if (ret < 0)
87 return ret;
88 soc_info->product_id = val & EXYNOS_MASK;
89
90 if (data->rev_reg != EXYNOS_CHIPID_REG_PRO_ID) {
91 ret = regmap_read(regmap, data->rev_reg, &val);
92 if (ret < 0)
93 return ret;
94 }
95 main_rev = (val >> data->main_rev_shift) & EXYNOS_REV_PART_MASK;
96 sub_rev = (val >> data->sub_rev_shift) & EXYNOS_REV_PART_MASK;
97 soc_info->revision = (main_rev << EXYNOS_REV_PART_SHIFT) | sub_rev;
98
99 return 0;
100 }
101
exynos_chipid_probe(struct platform_device * pdev)102 static int exynos_chipid_probe(struct platform_device *pdev)
103 {
104 const struct exynos_chipid_variant *drv_data;
105 struct exynos_chipid_info soc_info;
106 struct soc_device_attribute *soc_dev_attr;
107 struct soc_device *soc_dev;
108 struct device_node *root;
109 struct regmap *regmap;
110 int ret;
111
112 drv_data = of_device_get_match_data(&pdev->dev);
113 if (!drv_data)
114 return -EINVAL;
115
116 regmap = device_node_to_regmap(pdev->dev.of_node);
117 if (IS_ERR(regmap))
118 return PTR_ERR(regmap);
119
120 ret = exynos_chipid_get_chipid_info(regmap, drv_data, &soc_info);
121 if (ret < 0)
122 return ret;
123
124 soc_dev_attr = devm_kzalloc(&pdev->dev, sizeof(*soc_dev_attr),
125 GFP_KERNEL);
126 if (!soc_dev_attr)
127 return -ENOMEM;
128
129 soc_dev_attr->family = "Samsung Exynos";
130
131 root = of_find_node_by_path("/");
132 of_property_read_string(root, "model", &soc_dev_attr->machine);
133 of_node_put(root);
134
135 soc_dev_attr->revision = devm_kasprintf(&pdev->dev, GFP_KERNEL,
136 "%x", soc_info.revision);
137 if (!soc_dev_attr->revision)
138 return -ENOMEM;
139 soc_dev_attr->soc_id = product_id_to_soc_id(soc_info.product_id);
140 if (!soc_dev_attr->soc_id) {
141 pr_err("Unknown SoC\n");
142 return -ENODEV;
143 }
144
145 /* please note that the actual registration will be deferred */
146 soc_dev = soc_device_register(soc_dev_attr);
147 if (IS_ERR(soc_dev))
148 return PTR_ERR(soc_dev);
149
150 ret = exynos_asv_init(&pdev->dev, regmap);
151 if (ret)
152 goto err;
153
154 platform_set_drvdata(pdev, soc_dev);
155
156 dev_info(&pdev->dev, "Exynos: CPU[%s] PRO_ID[0x%x] REV[0x%x] Detected\n",
157 soc_dev_attr->soc_id, soc_info.product_id, soc_info.revision);
158
159 return 0;
160
161 err:
162 soc_device_unregister(soc_dev);
163
164 return ret;
165 }
166
exynos_chipid_remove(struct platform_device * pdev)167 static void exynos_chipid_remove(struct platform_device *pdev)
168 {
169 struct soc_device *soc_dev = platform_get_drvdata(pdev);
170
171 soc_device_unregister(soc_dev);
172 }
173
174 static const struct exynos_chipid_variant exynos4210_chipid_drv_data = {
175 .rev_reg = 0x0,
176 .main_rev_shift = 4,
177 .sub_rev_shift = 0,
178 };
179
180 static const struct exynos_chipid_variant exynos850_chipid_drv_data = {
181 .rev_reg = 0x10,
182 .main_rev_shift = 20,
183 .sub_rev_shift = 16,
184 };
185
186 static const struct of_device_id exynos_chipid_of_device_ids[] = {
187 {
188 .compatible = "samsung,exynos4210-chipid",
189 .data = &exynos4210_chipid_drv_data,
190 }, {
191 .compatible = "samsung,exynos850-chipid",
192 .data = &exynos850_chipid_drv_data,
193 },
194 { }
195 };
196 MODULE_DEVICE_TABLE(of, exynos_chipid_of_device_ids);
197
198 static struct platform_driver exynos_chipid_driver = {
199 .driver = {
200 .name = "exynos-chipid",
201 .of_match_table = exynos_chipid_of_device_ids,
202 },
203 .probe = exynos_chipid_probe,
204 .remove = exynos_chipid_remove,
205 };
206 module_platform_driver(exynos_chipid_driver);
207
208 MODULE_DESCRIPTION("Samsung Exynos ChipID controller and ASV driver");
209 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz <[email protected]>");
210 MODULE_AUTHOR("Krzysztof Kozlowski <[email protected]>");
211 MODULE_AUTHOR("Pankaj Dubey <[email protected]>");
212 MODULE_AUTHOR("Sylwester Nawrocki <[email protected]>");
213 MODULE_LICENSE("GPL");
214