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_HIGHER_ADDR_MASK 0xFF80
23 #define RSMU_HIGHER_ADDR_SHIFT 7
24 #define RSMU_LOWER_ADDR_MASK 0x7F
26 static int rsmu_read_device(struct rsmu_ddata *rsmu, u8 reg, u8 *buf, u16 bytes)
28 struct spi_device *client = to_spi_device(rsmu->dev);
29 struct spi_transfer xfer = {0};
30 struct spi_message msg;
39 xfer.bits_per_word = client->bits_per_word;
40 xfer.speed_hz = client->max_speed_hz;
42 spi_message_init(&msg);
43 spi_message_add_tail(&xfer, &msg);
46 * 4-wire SPI is a shift register, so for every byte you send,
47 * you get one back at the same time. Example read from 0xC024,
48 * which has value of 0x2D
51 * 7C 00 C0 #Set page register
52 * A4 00 #MSB is set, so this is read command
54 * XX 2D #XX is a dummy byte from sending A4 and we
55 * need to throw it away
57 ret = spi_sync(client, &msg);
59 memcpy(buf, &rsp[1], xfer.len-1);
64 static int rsmu_write_device(struct rsmu_ddata *rsmu, u8 reg, u8 *buf, u16 bytes)
66 struct spi_device *client = to_spi_device(rsmu->dev);
67 struct spi_transfer xfer = {0};
68 struct spi_message msg;
72 memcpy(&cmd[1], buf, bytes);
76 xfer.bits_per_word = client->bits_per_word;
77 xfer.speed_hz = client->max_speed_hz;
78 spi_message_init(&msg);
79 spi_message_add_tail(&xfer, &msg);
81 return spi_sync(client, &msg);
85 * 1-byte (1B) offset addressing:
86 * 16-bit register address: the lower 7 bits of the register address come
87 * from the offset addr byte and the upper 9 bits come from the page register.
89 static int rsmu_write_page_register(struct rsmu_ddata *rsmu, u16 reg)
99 page_reg = RSMU_CM_PAGE_ADDR;
100 page = reg & RSMU_HIGHER_ADDR_MASK;
101 buf[0] = (u8)(page & 0xff);
102 buf[1] = (u8)((page >> 8) & 0xff);
106 page_reg = RSMU_SABRE_PAGE_ADDR;
107 page = reg >> RSMU_HIGHER_ADDR_SHIFT;
108 buf[0] = (u8)(page & 0xff);
112 dev_err(rsmu->dev, "Unsupported RSMU device type: %d\n", rsmu->type);
116 /* Simply return if we are on the same page */
117 if (rsmu->page == page)
120 err = rsmu_write_device(rsmu, page_reg, buf, bytes);
122 dev_err(rsmu->dev, "Failed to set page offset 0x%x\n", page);
124 /* Remember the last page */
130 static int rsmu_reg_read(void *context, unsigned int reg, unsigned int *val)
132 struct rsmu_ddata *rsmu = spi_get_drvdata((struct spi_device *)context);
133 u8 addr = (u8)(reg & RSMU_LOWER_ADDR_MASK);
136 err = rsmu_write_page_register(rsmu, reg);
140 err = rsmu_read_device(rsmu, addr, (u8 *)val, 1);
142 dev_err(rsmu->dev, "Failed to read offset address 0x%x\n", addr);
147 static int rsmu_reg_write(void *context, unsigned int reg, unsigned int val)
149 struct rsmu_ddata *rsmu = spi_get_drvdata((struct spi_device *)context);
150 u8 addr = (u8)(reg & RSMU_LOWER_ADDR_MASK);
154 err = rsmu_write_page_register(rsmu, reg);
158 err = rsmu_write_device(rsmu, addr, &data, 1);
161 "Failed to write offset address 0x%x\n", addr);
166 static const struct regmap_config rsmu_cm_regmap_config = {
169 .max_register = 0xD000,
170 .reg_read = rsmu_reg_read,
171 .reg_write = rsmu_reg_write,
172 .cache_type = REGCACHE_NONE,
175 static const struct regmap_config rsmu_sabre_regmap_config = {
178 .max_register = 0x400,
179 .reg_read = rsmu_reg_read,
180 .reg_write = rsmu_reg_write,
181 .cache_type = REGCACHE_NONE,
184 static int rsmu_spi_probe(struct spi_device *client)
186 const struct spi_device_id *id = spi_get_device_id(client);
187 const struct regmap_config *cfg;
188 struct rsmu_ddata *rsmu;
191 rsmu = devm_kzalloc(&client->dev, sizeof(*rsmu), GFP_KERNEL);
195 spi_set_drvdata(client, rsmu);
197 rsmu->dev = &client->dev;
198 rsmu->type = (enum rsmu_type)id->driver_data;
200 /* Initialize regmap */
201 switch (rsmu->type) {
203 cfg = &rsmu_cm_regmap_config;
206 cfg = &rsmu_sabre_regmap_config;
209 dev_err(rsmu->dev, "Unsupported RSMU device type: %d\n", rsmu->type);
213 rsmu->regmap = devm_regmap_init(&client->dev, NULL, client, cfg);
214 if (IS_ERR(rsmu->regmap)) {
215 ret = PTR_ERR(rsmu->regmap);
216 dev_err(rsmu->dev, "Failed to allocate register map: %d\n", ret);
220 return rsmu_core_init(rsmu);
223 static void rsmu_spi_remove(struct spi_device *client)
225 struct rsmu_ddata *rsmu = spi_get_drvdata(client);
227 rsmu_core_exit(rsmu);
230 static const struct spi_device_id rsmu_spi_id[] = {
231 { "8a34000", RSMU_CM },
232 { "8a34001", RSMU_CM },
233 { "82p33810", RSMU_SABRE },
234 { "82p33811", RSMU_SABRE },
237 MODULE_DEVICE_TABLE(spi, rsmu_spi_id);
239 static const struct of_device_id rsmu_spi_of_match[] = {
240 { .compatible = "idt,8a34000", .data = (void *)RSMU_CM },
241 { .compatible = "idt,8a34001", .data = (void *)RSMU_CM },
242 { .compatible = "idt,82p33810", .data = (void *)RSMU_SABRE },
243 { .compatible = "idt,82p33811", .data = (void *)RSMU_SABRE },
246 MODULE_DEVICE_TABLE(of, rsmu_spi_of_match);
248 static struct spi_driver rsmu_spi_driver = {
251 .of_match_table = of_match_ptr(rsmu_spi_of_match),
253 .probe = rsmu_spi_probe,
254 .remove = rsmu_spi_remove,
255 .id_table = rsmu_spi_id,
258 static int __init rsmu_spi_init(void)
260 return spi_register_driver(&rsmu_spi_driver);
262 subsys_initcall(rsmu_spi_init);
264 static void __exit rsmu_spi_exit(void)
266 spi_unregister_driver(&rsmu_spi_driver);
268 module_exit(rsmu_spi_exit);
270 MODULE_DESCRIPTION("Renesas SMU SPI driver");
271 MODULE_LICENSE("GPL");