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/nvmem-provider.h>
10 #include <linux/platform_device.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
.add_legacy_fixed_of_cells
= true;
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" },
170 MODULE_DEVICE_TABLE(of
, sdam_match_table
);
172 static struct platform_driver sdam_driver
= {
174 .name
= "qcom,spmi-sdam",
175 .of_match_table
= sdam_match_table
,
179 module_platform_driver(sdam_driver
);
181 MODULE_DESCRIPTION("QCOM SPMI SDAM driver");
182 MODULE_LICENSE("GPL v2");