1 // SPDX-License-Identifier: GPL-2.0+
3 * I2C driver for Renesas Synchronization Management Unit (SMU) devices.
5 * Copyright (C) 2021 Integrated Device Technology, Inc., a Renesas Company.
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/mfd/core.h>
12 #include <linux/mfd/rsmu.h>
13 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
21 * 32-bit register address: the lower 8 bits of the register address come
22 * from the offset addr byte and the upper 24 bits come from the page register.
24 #define RSMU_CM_PAGE_ADDR 0xFC
25 #define RSMU_CM_PAGE_MASK 0xFFFFFF00
26 #define RSMU_CM_ADDRESS_MASK 0x000000FF
29 * 15-bit register address: the lower 7 bits of the register address come
30 * from the offset addr byte and the upper 8 bits come from the page register.
32 #define RSMU_SABRE_PAGE_ADDR 0x7F
33 #define RSMU_SABRE_PAGE_WINDOW 128
35 static const struct regmap_range_cfg rsmu_sabre_range_cfg[] = {
39 .selector_reg = RSMU_SABRE_PAGE_ADDR,
40 .selector_mask = 0xFF,
43 .window_len = RSMU_SABRE_PAGE_WINDOW,
47 static bool rsmu_sabre_volatile_reg(struct device *dev, unsigned int reg)
50 case RSMU_SABRE_PAGE_ADDR:
57 static int rsmu_read_device(struct rsmu_ddata *rsmu, u8 reg, u8 *buf, u16 bytes)
59 struct i2c_client *client = to_i2c_client(rsmu->dev);
60 struct i2c_msg msg[2];
63 msg[0].addr = client->addr;
68 msg[1].addr = client->addr;
69 msg[1].flags = I2C_M_RD;
73 cnt = i2c_transfer(client->adapter, msg, 2);
76 dev_err(rsmu->dev, "i2c_transfer failed at addr: %04x!", reg);
78 } else if (cnt != 2) {
80 "i2c_transfer sent only %d of 2 messages", cnt);
87 static int rsmu_write_device(struct rsmu_ddata *rsmu, u8 reg, u8 *buf, u16 bytes)
89 struct i2c_client *client = to_i2c_client(rsmu->dev);
90 u8 msg[RSMU_MAX_WRITE_COUNT + 1]; /* 1 Byte added for the device register */
93 if (bytes > RSMU_MAX_WRITE_COUNT)
97 memcpy(&msg[1], buf, bytes);
99 cnt = i2c_master_send(client, msg, bytes + 1);
102 dev_err(&client->dev,
103 "i2c_master_send failed at addr: %04x!", reg);
110 static int rsmu_write_page_register(struct rsmu_ddata *rsmu, u32 reg)
112 u32 page = reg & RSMU_CM_PAGE_MASK;
116 /* Do not modify offset register for none-scsr registers */
117 if (reg < RSMU_CM_SCSR_BASE)
120 /* Simply return if we are on the same page */
121 if (rsmu->page == page)
125 buf[1] = (u8)((page >> 8) & 0xFF);
126 buf[2] = (u8)((page >> 16) & 0xFF);
127 buf[3] = (u8)((page >> 24) & 0xFF);
129 err = rsmu_write_device(rsmu, RSMU_CM_PAGE_ADDR, buf, sizeof(buf));
131 dev_err(rsmu->dev, "Failed to set page offset 0x%x\n", page);
133 /* Remember the last page */
139 static int rsmu_reg_read(void *context, unsigned int reg, unsigned int *val)
141 struct rsmu_ddata *rsmu = i2c_get_clientdata((struct i2c_client *)context);
142 u8 addr = (u8)(reg & RSMU_CM_ADDRESS_MASK);
145 err = rsmu_write_page_register(rsmu, reg);
149 err = rsmu_read_device(rsmu, addr, (u8 *)val, 1);
151 dev_err(rsmu->dev, "Failed to read offset address 0x%x\n", addr);
156 static int rsmu_reg_write(void *context, unsigned int reg, unsigned int val)
158 struct rsmu_ddata *rsmu = i2c_get_clientdata((struct i2c_client *)context);
159 u8 addr = (u8)(reg & RSMU_CM_ADDRESS_MASK);
163 err = rsmu_write_page_register(rsmu, reg);
167 err = rsmu_write_device(rsmu, addr, &data, 1);
170 "Failed to write offset address 0x%x\n", addr);
175 static const struct regmap_config rsmu_cm_regmap_config = {
178 .max_register = 0x20120000,
179 .reg_read = rsmu_reg_read,
180 .reg_write = rsmu_reg_write,
181 .cache_type = REGCACHE_NONE,
184 static const struct regmap_config rsmu_sabre_regmap_config = {
187 .max_register = 0x400,
188 .ranges = rsmu_sabre_range_cfg,
189 .num_ranges = ARRAY_SIZE(rsmu_sabre_range_cfg),
190 .volatile_reg = rsmu_sabre_volatile_reg,
191 .cache_type = REGCACHE_RBTREE,
192 .can_multi_write = true,
195 static const struct regmap_config rsmu_sl_regmap_config = {
198 .reg_format_endian = REGMAP_ENDIAN_BIG,
199 .max_register = 0x340,
200 .cache_type = REGCACHE_NONE,
201 .can_multi_write = true,
204 static int rsmu_i2c_probe(struct i2c_client *client)
206 const struct i2c_device_id *id = i2c_client_get_device_id(client);
207 const struct regmap_config *cfg;
208 struct rsmu_ddata *rsmu;
211 rsmu = devm_kzalloc(&client->dev, sizeof(*rsmu), GFP_KERNEL);
215 i2c_set_clientdata(client, rsmu);
217 rsmu->dev = &client->dev;
218 rsmu->type = (enum rsmu_type)id->driver_data;
220 switch (rsmu->type) {
222 cfg = &rsmu_cm_regmap_config;
225 cfg = &rsmu_sabre_regmap_config;
228 cfg = &rsmu_sl_regmap_config;
231 dev_err(rsmu->dev, "Unsupported RSMU device type: %d\n", rsmu->type);
235 if (rsmu->type == RSMU_CM)
236 rsmu->regmap = devm_regmap_init(&client->dev, NULL, client, cfg);
238 rsmu->regmap = devm_regmap_init_i2c(client, cfg);
239 if (IS_ERR(rsmu->regmap)) {
240 ret = PTR_ERR(rsmu->regmap);
241 dev_err(rsmu->dev, "Failed to allocate register map: %d\n", ret);
245 return rsmu_core_init(rsmu);
248 static void rsmu_i2c_remove(struct i2c_client *client)
250 struct rsmu_ddata *rsmu = i2c_get_clientdata(client);
252 rsmu_core_exit(rsmu);
255 static const struct i2c_device_id rsmu_i2c_id[] = {
256 { "8a34000", RSMU_CM },
257 { "8a34001", RSMU_CM },
258 { "82p33810", RSMU_SABRE },
259 { "82p33811", RSMU_SABRE },
260 { "8v19n850", RSMU_SL },
261 { "8v19n851", RSMU_SL },
264 MODULE_DEVICE_TABLE(i2c, rsmu_i2c_id);
266 static const struct of_device_id rsmu_i2c_of_match[] = {
267 { .compatible = "idt,8a34000", .data = (void *)RSMU_CM },
268 { .compatible = "idt,8a34001", .data = (void *)RSMU_CM },
269 { .compatible = "idt,82p33810", .data = (void *)RSMU_SABRE },
270 { .compatible = "idt,82p33811", .data = (void *)RSMU_SABRE },
271 { .compatible = "idt,8v19n850", .data = (void *)RSMU_SL },
272 { .compatible = "idt,8v19n851", .data = (void *)RSMU_SL },
275 MODULE_DEVICE_TABLE(of, rsmu_i2c_of_match);
277 static struct i2c_driver rsmu_i2c_driver = {
280 .of_match_table = of_match_ptr(rsmu_i2c_of_match),
282 .probe = rsmu_i2c_probe,
283 .remove = rsmu_i2c_remove,
284 .id_table = rsmu_i2c_id,
287 static int __init rsmu_i2c_init(void)
289 return i2c_add_driver(&rsmu_i2c_driver);
291 subsys_initcall(rsmu_i2c_init);
293 static void __exit rsmu_i2c_exit(void)
295 i2c_del_driver(&rsmu_i2c_driver);
297 module_exit(rsmu_i2c_exit);
299 MODULE_DESCRIPTION("Renesas SMU I2C driver");
300 MODULE_LICENSE("GPL");