1 // SPDX-License-Identifier: GPL-2.0+
3 * SPI driver for Renesas Synchronization Management Unit (SMU) devices.
5 * Copyright (C) 2021 Integrated Device Technology, Inc., a Renesas Company.
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/mfd/core.h>
11 #include <linux/mfd/rsmu.h>
12 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
16 #include <linux/spi/spi.h>
20 #define RSMU_CM_PAGE_ADDR 0x7C
21 #define RSMU_SABRE_PAGE_ADDR 0x7F
22 #define RSMU_PAGE_MASK 0xFFFFFF80
23 #define RSMU_ADDR_MASK 0x7F
25 static int rsmu_read_device(struct rsmu_ddata *rsmu, u8 reg, u8 *buf, u16 bytes)
27 struct spi_device *client = to_spi_device(rsmu->dev);
28 struct spi_transfer xfer = {0};
29 struct spi_message msg;
30 u8 cmd[RSMU_MAX_READ_COUNT + 1] = {0};
31 u8 rsp[RSMU_MAX_READ_COUNT + 1] = {0};
34 if (bytes > RSMU_MAX_READ_COUNT)
41 xfer.bits_per_word = client->bits_per_word;
42 xfer.speed_hz = client->max_speed_hz;
44 spi_message_init(&msg);
45 spi_message_add_tail(&xfer, &msg);
48 * 4-wire SPI is a shift register, so for every byte you send,
49 * you get one back at the same time. Example read from 0xC024,
50 * which has value of 0x2D
53 * 7C 00 C0 #Set page register
54 * A4 00 #MSB is set, so this is read command
56 * XX 2D #XX is a dummy byte from sending A4 and we
57 * need to throw it away
59 ret = spi_sync(client, &msg);
61 memcpy(buf, &rsp[1], xfer.len-1);
66 static int rsmu_write_device(struct rsmu_ddata *rsmu, u8 reg, u8 *buf, u16 bytes)
68 struct spi_device *client = to_spi_device(rsmu->dev);
69 struct spi_transfer xfer = {0};
70 struct spi_message msg;
71 u8 cmd[RSMU_MAX_WRITE_COUNT + 1] = {0};
73 if (bytes > RSMU_MAX_WRITE_COUNT)
77 memcpy(&cmd[1], buf, bytes);
81 xfer.bits_per_word = client->bits_per_word;
82 xfer.speed_hz = client->max_speed_hz;
83 spi_message_init(&msg);
84 spi_message_add_tail(&xfer, &msg);
86 return spi_sync(client, &msg);
90 * 1-byte (1B) offset addressing:
91 * 16-bit register address: the lower 7 bits of the register address come
92 * from the offset addr byte and the upper 9 bits come from the page register.
94 static int rsmu_write_page_register(struct rsmu_ddata *rsmu, u32 reg)
102 switch (rsmu->type) {
104 /* Do not modify page register for none-scsr registers */
105 if (reg < RSMU_CM_SCSR_BASE)
107 page_reg = RSMU_CM_PAGE_ADDR;
108 page = reg & RSMU_PAGE_MASK;
109 buf[0] = (u8)(page & 0xff);
110 buf[1] = (u8)((page >> 8) & 0xff);
111 buf[2] = (u8)((page >> 16) & 0xff);
112 buf[3] = (u8)((page >> 24) & 0xff);
116 /* Do not modify page register if reg is page register itself */
117 if ((reg & RSMU_ADDR_MASK) == RSMU_ADDR_MASK)
119 page_reg = RSMU_SABRE_PAGE_ADDR;
120 page = reg & RSMU_PAGE_MASK;
121 /* The three page bits are located in the single Page Register */
122 buf[0] = (u8)((page >> 7) & 0x7);
126 dev_err(rsmu->dev, "Unsupported RSMU device type: %d\n", rsmu->type);
130 /* Simply return if we are on the same page */
131 if (rsmu->page == page)
134 err = rsmu_write_device(rsmu, page_reg, buf, bytes);
136 dev_err(rsmu->dev, "Failed to set page offset 0x%x\n", page);
138 /* Remember the last page */
144 static int rsmu_reg_read(void *context, unsigned int reg, unsigned int *val)
146 struct rsmu_ddata *rsmu = spi_get_drvdata((struct spi_device *)context);
147 u8 addr = (u8)(reg & RSMU_ADDR_MASK);
150 err = rsmu_write_page_register(rsmu, reg);
154 err = rsmu_read_device(rsmu, addr, (u8 *)val, 1);
156 dev_err(rsmu->dev, "Failed to read offset address 0x%x\n", addr);
161 static int rsmu_reg_write(void *context, unsigned int reg, unsigned int val)
163 struct rsmu_ddata *rsmu = spi_get_drvdata((struct spi_device *)context);
164 u8 addr = (u8)(reg & RSMU_ADDR_MASK);
168 err = rsmu_write_page_register(rsmu, reg);
172 err = rsmu_write_device(rsmu, addr, &data, 1);
175 "Failed to write offset address 0x%x\n", addr);
180 static const struct regmap_config rsmu_cm_regmap_config = {
183 .max_register = 0x20120000,
184 .reg_read = rsmu_reg_read,
185 .reg_write = rsmu_reg_write,
186 .cache_type = REGCACHE_NONE,
189 static const struct regmap_config rsmu_sabre_regmap_config = {
192 .max_register = 0x400,
193 .reg_read = rsmu_reg_read,
194 .reg_write = rsmu_reg_write,
195 .cache_type = REGCACHE_NONE,
198 static int rsmu_spi_probe(struct spi_device *client)
200 const struct spi_device_id *id = spi_get_device_id(client);
201 const struct regmap_config *cfg;
202 struct rsmu_ddata *rsmu;
205 rsmu = devm_kzalloc(&client->dev, sizeof(*rsmu), GFP_KERNEL);
209 spi_set_drvdata(client, rsmu);
211 rsmu->dev = &client->dev;
212 rsmu->type = (enum rsmu_type)id->driver_data;
214 /* Initialize regmap */
215 switch (rsmu->type) {
217 cfg = &rsmu_cm_regmap_config;
220 cfg = &rsmu_sabre_regmap_config;
223 dev_err(rsmu->dev, "Unsupported RSMU device type: %d\n", rsmu->type);
227 rsmu->regmap = devm_regmap_init(&client->dev, NULL, client, cfg);
228 if (IS_ERR(rsmu->regmap)) {
229 ret = PTR_ERR(rsmu->regmap);
230 dev_err(rsmu->dev, "Failed to allocate register map: %d\n", ret);
234 return rsmu_core_init(rsmu);
237 static void rsmu_spi_remove(struct spi_device *client)
239 struct rsmu_ddata *rsmu = spi_get_drvdata(client);
241 rsmu_core_exit(rsmu);
244 static const struct spi_device_id rsmu_spi_id[] = {
245 { "8a34000", RSMU_CM },
246 { "8a34001", RSMU_CM },
247 { "82p33810", RSMU_SABRE },
248 { "82p33811", RSMU_SABRE },
251 MODULE_DEVICE_TABLE(spi, rsmu_spi_id);
253 static const struct of_device_id rsmu_spi_of_match[] = {
254 { .compatible = "idt,8a34000", .data = (void *)RSMU_CM },
255 { .compatible = "idt,8a34001", .data = (void *)RSMU_CM },
256 { .compatible = "idt,82p33810", .data = (void *)RSMU_SABRE },
257 { .compatible = "idt,82p33811", .data = (void *)RSMU_SABRE },
260 MODULE_DEVICE_TABLE(of, rsmu_spi_of_match);
262 static struct spi_driver rsmu_spi_driver = {
265 .of_match_table = of_match_ptr(rsmu_spi_of_match),
267 .probe = rsmu_spi_probe,
268 .remove = rsmu_spi_remove,
269 .id_table = rsmu_spi_id,
272 static int __init rsmu_spi_init(void)
274 return spi_register_driver(&rsmu_spi_driver);
276 subsys_initcall(rsmu_spi_init);
278 static void __exit rsmu_spi_exit(void)
280 spi_unregister_driver(&rsmu_spi_driver);
282 module_exit(rsmu_spi_exit);
284 MODULE_DESCRIPTION("Renesas SMU SPI driver");
285 MODULE_LICENSE("GPL");