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 typedef int (*rsmu_rw_device
)(struct rsmu_ddata
*rsmu
, u8 reg
, u8
*buf
, u8 bytes
);
37 static const struct regmap_range_cfg rsmu_sabre_range_cfg
[] = {
41 .selector_reg
= RSMU_SABRE_PAGE_ADDR
,
42 .selector_mask
= 0xFF,
45 .window_len
= RSMU_SABRE_PAGE_WINDOW
,
49 static bool rsmu_sabre_volatile_reg(struct device
*dev
, unsigned int reg
)
52 case RSMU_SABRE_PAGE_ADDR
:
59 static int rsmu_smbus_i2c_write_device(struct rsmu_ddata
*rsmu
, u8 reg
, u8
*buf
, u8 bytes
)
61 struct i2c_client
*client
= to_i2c_client(rsmu
->dev
);
63 return i2c_smbus_write_i2c_block_data(client
, reg
, bytes
, buf
);
66 static int rsmu_smbus_i2c_read_device(struct rsmu_ddata
*rsmu
, u8 reg
, u8
*buf
, u8 bytes
)
68 struct i2c_client
*client
= to_i2c_client(rsmu
->dev
);
71 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, bytes
, buf
);
80 static int rsmu_i2c_read_device(struct rsmu_ddata
*rsmu
, u8 reg
, u8
*buf
, u8 bytes
)
82 struct i2c_client
*client
= to_i2c_client(rsmu
->dev
);
83 struct i2c_msg msg
[2];
86 msg
[0].addr
= client
->addr
;
91 msg
[1].addr
= client
->addr
;
92 msg
[1].flags
= I2C_M_RD
;
96 cnt
= i2c_transfer(client
->adapter
, msg
, 2);
99 dev_err(rsmu
->dev
, "i2c_transfer failed at addr: %04x!", reg
);
101 } else if (cnt
!= 2) {
103 "i2c_transfer sent only %d of 2 messages", cnt
);
110 static int rsmu_i2c_write_device(struct rsmu_ddata
*rsmu
, u8 reg
, u8
*buf
, u8 bytes
)
112 struct i2c_client
*client
= to_i2c_client(rsmu
->dev
);
113 /* we add 1 byte for device register */
114 u8 msg
[RSMU_MAX_WRITE_COUNT
+ 1];
117 if (bytes
> RSMU_MAX_WRITE_COUNT
)
121 memcpy(&msg
[1], buf
, bytes
);
123 cnt
= i2c_master_send(client
, msg
, bytes
+ 1);
126 dev_err(&client
->dev
,
127 "i2c_master_send failed at addr: %04x!", reg
);
134 static int rsmu_write_page_register(struct rsmu_ddata
*rsmu
, u32 reg
,
135 rsmu_rw_device rsmu_write_device
)
137 u32 page
= reg
& RSMU_CM_PAGE_MASK
;
141 /* Do not modify offset register for none-scsr registers */
142 if (reg
< RSMU_CM_SCSR_BASE
)
145 /* Simply return if we are on the same page */
146 if (rsmu
->page
== page
)
150 buf
[1] = (u8
)((page
>> 8) & 0xFF);
151 buf
[2] = (u8
)((page
>> 16) & 0xFF);
152 buf
[3] = (u8
)((page
>> 24) & 0xFF);
154 err
= rsmu_write_device(rsmu
, RSMU_CM_PAGE_ADDR
, buf
, sizeof(buf
));
156 dev_err(rsmu
->dev
, "Failed to set page offset 0x%x\n", page
);
158 /* Remember the last page */
164 static int rsmu_i2c_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
166 struct rsmu_ddata
*rsmu
= i2c_get_clientdata((struct i2c_client
*)context
);
167 u8 addr
= (u8
)(reg
& RSMU_CM_ADDRESS_MASK
);
170 err
= rsmu_write_page_register(rsmu
, reg
, rsmu_i2c_write_device
);
174 err
= rsmu_i2c_read_device(rsmu
, addr
, (u8
*)val
, 1);
176 dev_err(rsmu
->dev
, "Failed to read offset address 0x%x\n", addr
);
181 static int rsmu_i2c_reg_write(void *context
, unsigned int reg
, unsigned int val
)
183 struct rsmu_ddata
*rsmu
= i2c_get_clientdata((struct i2c_client
*)context
);
184 u8 addr
= (u8
)(reg
& RSMU_CM_ADDRESS_MASK
);
188 err
= rsmu_write_page_register(rsmu
, reg
, rsmu_i2c_write_device
);
192 err
= rsmu_i2c_write_device(rsmu
, addr
, &data
, 1);
195 "Failed to write offset address 0x%x\n", addr
);
200 static int rsmu_smbus_i2c_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
202 struct rsmu_ddata
*rsmu
= i2c_get_clientdata((struct i2c_client
*)context
);
203 u8 addr
= (u8
)(reg
& RSMU_CM_ADDRESS_MASK
);
206 err
= rsmu_write_page_register(rsmu
, reg
, rsmu_smbus_i2c_write_device
);
210 err
= rsmu_smbus_i2c_read_device(rsmu
, addr
, (u8
*)val
, 1);
212 dev_err(rsmu
->dev
, "Failed to read offset address 0x%x\n", addr
);
217 static int rsmu_smbus_i2c_reg_write(void *context
, unsigned int reg
, unsigned int val
)
219 struct rsmu_ddata
*rsmu
= i2c_get_clientdata((struct i2c_client
*)context
);
220 u8 addr
= (u8
)(reg
& RSMU_CM_ADDRESS_MASK
);
224 err
= rsmu_write_page_register(rsmu
, reg
, rsmu_smbus_i2c_write_device
);
228 err
= rsmu_smbus_i2c_write_device(rsmu
, addr
, &data
, 1);
231 "Failed to write offset address 0x%x\n", addr
);
236 static const struct regmap_config rsmu_i2c_cm_regmap_config
= {
239 .max_register
= 0x20120000,
240 .reg_read
= rsmu_i2c_reg_read
,
241 .reg_write
= rsmu_i2c_reg_write
,
242 .cache_type
= REGCACHE_NONE
,
245 static const struct regmap_config rsmu_smbus_i2c_cm_regmap_config
= {
248 .max_register
= 0x20120000,
249 .reg_read
= rsmu_smbus_i2c_reg_read
,
250 .reg_write
= rsmu_smbus_i2c_reg_write
,
251 .cache_type
= REGCACHE_NONE
,
254 static const struct regmap_config rsmu_sabre_regmap_config
= {
257 .max_register
= 0x400,
258 .ranges
= rsmu_sabre_range_cfg
,
259 .num_ranges
= ARRAY_SIZE(rsmu_sabre_range_cfg
),
260 .volatile_reg
= rsmu_sabre_volatile_reg
,
261 .cache_type
= REGCACHE_MAPLE
,
262 .can_multi_write
= true,
265 static const struct regmap_config rsmu_sl_regmap_config
= {
268 .reg_format_endian
= REGMAP_ENDIAN_BIG
,
269 .max_register
= 0x340,
270 .cache_type
= REGCACHE_NONE
,
271 .can_multi_write
= true,
274 static int rsmu_i2c_probe(struct i2c_client
*client
)
276 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
277 const struct regmap_config
*cfg
;
278 struct rsmu_ddata
*rsmu
;
281 rsmu
= devm_kzalloc(&client
->dev
, sizeof(*rsmu
), GFP_KERNEL
);
285 i2c_set_clientdata(client
, rsmu
);
287 rsmu
->dev
= &client
->dev
;
288 rsmu
->type
= (enum rsmu_type
)id
->driver_data
;
290 switch (rsmu
->type
) {
292 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
293 cfg
= &rsmu_i2c_cm_regmap_config
;
294 } else if (i2c_check_functionality(client
->adapter
,
295 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
296 cfg
= &rsmu_smbus_i2c_cm_regmap_config
;
298 dev_err(rsmu
->dev
, "Unsupported i2c adapter\n");
303 cfg
= &rsmu_sabre_regmap_config
;
306 cfg
= &rsmu_sl_regmap_config
;
309 dev_err(rsmu
->dev
, "Unsupported RSMU device type: %d\n", rsmu
->type
);
313 if (rsmu
->type
== RSMU_CM
)
314 rsmu
->regmap
= devm_regmap_init(&client
->dev
, NULL
, client
, cfg
);
316 rsmu
->regmap
= devm_regmap_init_i2c(client
, cfg
);
318 if (IS_ERR(rsmu
->regmap
)) {
319 ret
= PTR_ERR(rsmu
->regmap
);
320 dev_err(rsmu
->dev
, "Failed to allocate register map: %d\n", ret
);
324 return rsmu_core_init(rsmu
);
327 static void rsmu_i2c_remove(struct i2c_client
*client
)
329 struct rsmu_ddata
*rsmu
= i2c_get_clientdata(client
);
331 rsmu_core_exit(rsmu
);
334 static const struct i2c_device_id rsmu_i2c_id
[] = {
335 { "8a34000", RSMU_CM
},
336 { "8a34001", RSMU_CM
},
337 { "82p33810", RSMU_SABRE
},
338 { "82p33811", RSMU_SABRE
},
339 { "8v19n850", RSMU_SL
},
340 { "8v19n851", RSMU_SL
},
343 MODULE_DEVICE_TABLE(i2c
, rsmu_i2c_id
);
345 static const struct of_device_id rsmu_i2c_of_match
[] = {
346 { .compatible
= "idt,8a34000", .data
= (void *)RSMU_CM
},
347 { .compatible
= "idt,8a34001", .data
= (void *)RSMU_CM
},
348 { .compatible
= "idt,82p33810", .data
= (void *)RSMU_SABRE
},
349 { .compatible
= "idt,82p33811", .data
= (void *)RSMU_SABRE
},
350 { .compatible
= "idt,8v19n850", .data
= (void *)RSMU_SL
},
351 { .compatible
= "idt,8v19n851", .data
= (void *)RSMU_SL
},
354 MODULE_DEVICE_TABLE(of
, rsmu_i2c_of_match
);
356 static struct i2c_driver rsmu_i2c_driver
= {
359 .of_match_table
= rsmu_i2c_of_match
,
361 .probe
= rsmu_i2c_probe
,
362 .remove
= rsmu_i2c_remove
,
363 .id_table
= rsmu_i2c_id
,
366 static int __init
rsmu_i2c_init(void)
368 return i2c_add_driver(&rsmu_i2c_driver
);
370 subsys_initcall(rsmu_i2c_init
);
372 static void __exit
rsmu_i2c_exit(void)
374 i2c_del_driver(&rsmu_i2c_driver
);
376 module_exit(rsmu_i2c_exit
);
378 MODULE_DESCRIPTION("Renesas SMU I2C driver");
379 MODULE_LICENSE("GPL");