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