1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017, 2020-2021, The Linux Foundation. All rights reserved.
6 #include <linux/device.h>
7 #include <linux/module.h>
9 #include <linux/of_platform.h>
10 #include <linux/nvmem-provider.h>
11 #include <linux/regmap.h>
13 #define SDAM_MEM_START 0x40
14 #define REGISTER_MAP_ID 0x40
15 #define REGISTER_MAP_VERSION 0x41
16 #define SDAM_SIZE 0x44
17 #define SDAM_PBS_TRIG_SET 0xE5
18 #define SDAM_PBS_TRIG_CLR 0xE6
21 struct regmap *regmap;
22 struct nvmem_config sdam_config;
27 /* read only register offsets */
28 static const u8 sdam_ro_map[] = {
34 static bool sdam_is_valid(struct sdam_chip *sdam, unsigned int offset,
37 unsigned int sdam_mem_end = SDAM_MEM_START + sdam->size - 1;
42 if (offset >= SDAM_MEM_START && offset <= sdam_mem_end
43 && (offset + len - 1) <= sdam_mem_end)
45 else if ((offset == SDAM_PBS_TRIG_SET || offset == SDAM_PBS_TRIG_CLR)
52 static bool sdam_is_ro(unsigned int offset, size_t len)
56 for (i = 0; i < ARRAY_SIZE(sdam_ro_map); i++)
57 if (offset <= sdam_ro_map[i] && (offset + len) > sdam_ro_map[i])
63 static int sdam_read(void *priv, unsigned int offset, void *val,
66 struct sdam_chip *sdam = priv;
67 struct device *dev = sdam->sdam_config.dev;
70 if (!sdam_is_valid(sdam, offset, bytes)) {
71 dev_err(dev, "Invalid SDAM offset %#x len=%zd\n",
76 rc = regmap_bulk_read(sdam->regmap, sdam->base + offset, val, bytes);
78 dev_err(dev, "Failed to read SDAM offset %#x len=%zd, rc=%d\n",
84 static int sdam_write(void *priv, unsigned int offset, void *val,
87 struct sdam_chip *sdam = priv;
88 struct device *dev = sdam->sdam_config.dev;
91 if (!sdam_is_valid(sdam, offset, bytes)) {
92 dev_err(dev, "Invalid SDAM offset %#x len=%zd\n",
97 if (sdam_is_ro(offset, bytes)) {
98 dev_err(dev, "Invalid write offset %#x len=%zd\n",
103 rc = regmap_bulk_write(sdam->regmap, sdam->base + offset, val, bytes);
105 dev_err(dev, "Failed to write SDAM offset %#x len=%zd, rc=%d\n",
111 static int sdam_probe(struct platform_device *pdev)
113 struct sdam_chip *sdam;
114 struct nvmem_device *nvmem;
118 sdam = devm_kzalloc(&pdev->dev, sizeof(*sdam), GFP_KERNEL);
122 sdam->regmap = dev_get_regmap(pdev->dev.parent, NULL);
124 dev_err(&pdev->dev, "Failed to get regmap handle\n");
128 rc = of_property_read_u32(pdev->dev.of_node, "reg", &sdam->base);
130 dev_err(&pdev->dev, "Failed to get SDAM base, rc=%d\n", rc);
134 rc = regmap_read(sdam->regmap, sdam->base + SDAM_SIZE, &val);
136 dev_err(&pdev->dev, "Failed to read SDAM_SIZE rc=%d\n", rc);
139 sdam->size = val * 32;
141 sdam->sdam_config.dev = &pdev->dev;
142 sdam->sdam_config.name = "spmi_sdam";
143 sdam->sdam_config.id = NVMEM_DEVID_AUTO;
144 sdam->sdam_config.owner = THIS_MODULE;
145 sdam->sdam_config.stride = 1;
146 sdam->sdam_config.word_size = 1;
147 sdam->sdam_config.reg_read = sdam_read;
148 sdam->sdam_config.reg_write = sdam_write;
149 sdam->sdam_config.priv = sdam;
151 nvmem = devm_nvmem_register(&pdev->dev, &sdam->sdam_config);
154 "Failed to register SDAM nvmem device rc=%ld\n",
159 "SDAM base=%#x size=%u registered successfully\n",
160 sdam->base, sdam->size);
165 static const struct of_device_id sdam_match_table[] = {
166 { .compatible = "qcom,spmi-sdam" },
170 static struct platform_driver sdam_driver = {
172 .name = "qcom,spmi-sdam",
173 .of_match_table = sdam_match_table,
178 static int __init sdam_init(void)
180 return platform_driver_register(&sdam_driver);
182 subsys_initcall(sdam_init);
184 static void __exit sdam_exit(void)
186 return platform_driver_unregister(&sdam_driver);
188 module_exit(sdam_exit);
190 MODULE_DESCRIPTION("QCOM SPMI SDAM driver");
191 MODULE_LICENSE("GPL v2");