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/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
28struct 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
34struct exynos_chipid_info {
35	u32 product_id;
36	u32 revision;
37};
38
39static 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	{ "EXYNOSAUTOV9", 0xAAA80000 },
62};
63
64static const char *product_id_to_soc_id(unsigned int product_id)
65{
66	int i;
67
68	for (i = 0; i < ARRAY_SIZE(soc_ids); i++)
69		if (product_id == soc_ids[i].id)
70			return soc_ids[i].name;
71	return NULL;
72}
73
74static int exynos_chipid_get_chipid_info(struct regmap *regmap,
75		const struct exynos_chipid_variant *data,
76		struct exynos_chipid_info *soc_info)
77{
78	int ret;
79	unsigned int val, main_rev, sub_rev;
80
81	ret = regmap_read(regmap, EXYNOS_CHIPID_REG_PRO_ID, &val);
82	if (ret < 0)
83		return ret;
84	soc_info->product_id = val & EXYNOS_MASK;
85
86	if (data->rev_reg != EXYNOS_CHIPID_REG_PRO_ID) {
87		ret = regmap_read(regmap, data->rev_reg, &val);
88		if (ret < 0)
89			return ret;
90	}
91	main_rev = (val >> data->main_rev_shift) & EXYNOS_REV_PART_MASK;
92	sub_rev = (val >> data->sub_rev_shift) & EXYNOS_REV_PART_MASK;
93	soc_info->revision = (main_rev << EXYNOS_REV_PART_SHIFT) | sub_rev;
94
95	return 0;
96}
97
98static int exynos_chipid_probe(struct platform_device *pdev)
99{
100	const struct exynos_chipid_variant *drv_data;
101	struct exynos_chipid_info soc_info;
102	struct soc_device_attribute *soc_dev_attr;
103	struct soc_device *soc_dev;
104	struct device_node *root;
105	struct regmap *regmap;
106	int ret;
107
108	drv_data = of_device_get_match_data(&pdev->dev);
109	if (!drv_data)
110		return -EINVAL;
111
112	regmap = device_node_to_regmap(pdev->dev.of_node);
113	if (IS_ERR(regmap))
114		return PTR_ERR(regmap);
115
116	ret = exynos_chipid_get_chipid_info(regmap, drv_data, &soc_info);
117	if (ret < 0)
118		return ret;
119
120	soc_dev_attr = devm_kzalloc(&pdev->dev, sizeof(*soc_dev_attr),
121				    GFP_KERNEL);
122	if (!soc_dev_attr)
123		return -ENOMEM;
124
125	soc_dev_attr->family = "Samsung Exynos";
126
127	root = of_find_node_by_path("/");
128	of_property_read_string(root, "model", &soc_dev_attr->machine);
129	of_node_put(root);
130
131	soc_dev_attr->revision = devm_kasprintf(&pdev->dev, GFP_KERNEL,
132						"%x", soc_info.revision);
133	soc_dev_attr->soc_id = product_id_to_soc_id(soc_info.product_id);
134	if (!soc_dev_attr->soc_id) {
135		pr_err("Unknown SoC\n");
136		return -ENODEV;
137	}
138
139	/* please note that the actual registration will be deferred */
140	soc_dev = soc_device_register(soc_dev_attr);
141	if (IS_ERR(soc_dev))
142		return PTR_ERR(soc_dev);
143
144	ret = exynos_asv_init(&pdev->dev, regmap);
145	if (ret)
146		goto err;
147
148	platform_set_drvdata(pdev, soc_dev);
149
150	dev_info(&pdev->dev, "Exynos: CPU[%s] PRO_ID[0x%x] REV[0x%x] Detected\n",
151		 soc_dev_attr->soc_id, soc_info.product_id, soc_info.revision);
152
153	return 0;
154
155err:
156	soc_device_unregister(soc_dev);
157
158	return ret;
159}
160
161static int exynos_chipid_remove(struct platform_device *pdev)
162{
163	struct soc_device *soc_dev = platform_get_drvdata(pdev);
164
165	soc_device_unregister(soc_dev);
166
167	return 0;
168}
169
170static const struct exynos_chipid_variant exynos4210_chipid_drv_data = {
171	.rev_reg	= 0x0,
172	.main_rev_shift	= 4,
173	.sub_rev_shift	= 0,
174};
175
176static const struct exynos_chipid_variant exynos850_chipid_drv_data = {
177	.rev_reg	= 0x10,
178	.main_rev_shift	= 20,
179	.sub_rev_shift	= 16,
180};
181
182static const struct of_device_id exynos_chipid_of_device_ids[] = {
183	{
184		.compatible	= "samsung,exynos4210-chipid",
185		.data		= &exynos4210_chipid_drv_data,
186	}, {
187		.compatible	= "samsung,exynos850-chipid",
188		.data		= &exynos850_chipid_drv_data,
189	},
190	{ }
191};
192MODULE_DEVICE_TABLE(of, exynos_chipid_of_device_ids);
193
194static struct platform_driver exynos_chipid_driver = {
195	.driver = {
196		.name = "exynos-chipid",
197		.of_match_table = exynos_chipid_of_device_ids,
198	},
199	.probe	= exynos_chipid_probe,
200	.remove	= exynos_chipid_remove,
201};
202module_platform_driver(exynos_chipid_driver);
203
204MODULE_DESCRIPTION("Samsung Exynos ChipID controller and ASV driver");
205MODULE_AUTHOR("Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>");
206MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
207MODULE_AUTHOR("Pankaj Dubey <pankaj.dubey@samsung.com>");
208MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
209MODULE_LICENSE("GPL");
210