1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017, 2020 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 platform_device
*pdev
;
22 struct regmap
*regmap
;
23 struct nvmem_config sdam_config
;
28 /* read only register offsets */
29 static const u8 sdam_ro_map
[] = {
35 static bool sdam_is_valid(struct sdam_chip
*sdam
, unsigned int offset
,
38 unsigned int sdam_mem_end
= SDAM_MEM_START
+ sdam
->size
- 1;
43 if (offset
>= SDAM_MEM_START
&& offset
<= sdam_mem_end
44 && (offset
+ len
- 1) <= sdam_mem_end
)
46 else if ((offset
== SDAM_PBS_TRIG_SET
|| offset
== SDAM_PBS_TRIG_CLR
)
53 static bool sdam_is_ro(unsigned int offset
, size_t len
)
57 for (i
= 0; i
< ARRAY_SIZE(sdam_ro_map
); i
++)
58 if (offset
<= sdam_ro_map
[i
] && (offset
+ len
) > sdam_ro_map
[i
])
64 static int sdam_read(void *priv
, unsigned int offset
, void *val
,
67 struct sdam_chip
*sdam
= priv
;
68 struct device
*dev
= &sdam
->pdev
->dev
;
71 if (!sdam_is_valid(sdam
, offset
, bytes
)) {
72 dev_err(dev
, "Invalid SDAM offset %#x len=%zd\n",
77 rc
= regmap_bulk_read(sdam
->regmap
, sdam
->base
+ offset
, val
, bytes
);
79 dev_err(dev
, "Failed to read SDAM offset %#x len=%zd, rc=%d\n",
85 static int sdam_write(void *priv
, unsigned int offset
, void *val
,
88 struct sdam_chip
*sdam
= priv
;
89 struct device
*dev
= &sdam
->pdev
->dev
;
92 if (!sdam_is_valid(sdam
, offset
, bytes
)) {
93 dev_err(dev
, "Invalid SDAM offset %#x len=%zd\n",
98 if (sdam_is_ro(offset
, bytes
)) {
99 dev_err(dev
, "Invalid write offset %#x len=%zd\n",
104 rc
= regmap_bulk_write(sdam
->regmap
, sdam
->base
+ offset
, val
, bytes
);
106 dev_err(dev
, "Failed to write SDAM offset %#x len=%zd, rc=%d\n",
112 static int sdam_probe(struct platform_device
*pdev
)
114 struct sdam_chip
*sdam
;
115 struct nvmem_device
*nvmem
;
119 sdam
= devm_kzalloc(&pdev
->dev
, sizeof(*sdam
), GFP_KERNEL
);
123 sdam
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
125 dev_err(&pdev
->dev
, "Failed to get regmap handle\n");
129 rc
= of_property_read_u32(pdev
->dev
.of_node
, "reg", &sdam
->base
);
131 dev_err(&pdev
->dev
, "Failed to get SDAM base, rc=%d\n", rc
);
135 rc
= regmap_read(sdam
->regmap
, sdam
->base
+ SDAM_SIZE
, &val
);
137 dev_err(&pdev
->dev
, "Failed to read SDAM_SIZE rc=%d\n", rc
);
140 sdam
->size
= val
* 32;
142 sdam
->sdam_config
.dev
= &pdev
->dev
;
143 sdam
->sdam_config
.name
= "spmi_sdam";
144 sdam
->sdam_config
.id
= NVMEM_DEVID_AUTO
;
145 sdam
->sdam_config
.owner
= THIS_MODULE
,
146 sdam
->sdam_config
.stride
= 1;
147 sdam
->sdam_config
.word_size
= 1;
148 sdam
->sdam_config
.reg_read
= sdam_read
;
149 sdam
->sdam_config
.reg_write
= sdam_write
;
150 sdam
->sdam_config
.priv
= sdam
;
152 nvmem
= devm_nvmem_register(&pdev
->dev
, &sdam
->sdam_config
);
155 "Failed to register SDAM nvmem device rc=%ld\n",
160 "SDAM base=%#x size=%u registered successfully\n",
161 sdam
->base
, sdam
->size
);
166 static const struct of_device_id sdam_match_table
[] = {
167 { .compatible
= "qcom,spmi-sdam" },
171 static struct platform_driver sdam_driver
= {
173 .name
= "qcom,spmi-sdam",
174 .of_match_table
= sdam_match_table
,
179 static int __init
sdam_init(void)
181 return platform_driver_register(&sdam_driver
);
183 subsys_initcall(sdam_init
);
185 static void __exit
sdam_exit(void)
187 return platform_driver_unregister(&sdam_driver
);
189 module_exit(sdam_exit
);
191 MODULE_DESCRIPTION("QCOM SPMI SDAM driver");
192 MODULE_LICENSE("GPL v2");