xref: /openbmc/linux/drivers/mfd/rsmu_i2c.c (revision d7955ce4)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * I2C driver for Renesas Synchronization Management Unit (SMU) devices.
4  *
5  * Copyright (C) 2021 Integrated Device Technology, Inc., a Renesas Company.
6  */
7 
8 #include <linux/i2c.h>
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>
14 #include <linux/of.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 
18 #include "rsmu.h"
19 
20 /*
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.
23  */
24 #define	RSMU_CM_PAGE_ADDR		0xFC
25 #define RSMU_CM_PAGE_MASK		0xFFFFFF00
26 #define RSMU_CM_ADDRESS_MASK		0x000000FF
27 
28 /*
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.
31  */
32 #define	RSMU_SABRE_PAGE_ADDR		0x7F
33 #define	RSMU_SABRE_PAGE_WINDOW		128
34 
35 static const struct regmap_range_cfg rsmu_sabre_range_cfg[] = {
36 	{
37 		.range_min = 0,
38 		.range_max = 0x400,
39 		.selector_reg = RSMU_SABRE_PAGE_ADDR,
40 		.selector_mask = 0xFF,
41 		.selector_shift = 0,
42 		.window_start = 0,
43 		.window_len = RSMU_SABRE_PAGE_WINDOW,
44 	}
45 };
46 
47 static bool rsmu_sabre_volatile_reg(struct device *dev, unsigned int reg)
48 {
49 	switch (reg) {
50 	case RSMU_SABRE_PAGE_ADDR:
51 		return false;
52 	default:
53 		return true;
54 	}
55 }
56 
57 static int rsmu_read_device(struct rsmu_ddata *rsmu, u8 reg, u8 *buf, u16 bytes)
58 {
59 	struct i2c_client *client = to_i2c_client(rsmu->dev);
60 	struct i2c_msg msg[2];
61 	int cnt;
62 
63 	msg[0].addr = client->addr;
64 	msg[0].flags = 0;
65 	msg[0].len = 1;
66 	msg[0].buf = &reg;
67 
68 	msg[1].addr = client->addr;
69 	msg[1].flags = I2C_M_RD;
70 	msg[1].len = bytes;
71 	msg[1].buf = buf;
72 
73 	cnt = i2c_transfer(client->adapter, msg, 2);
74 
75 	if (cnt < 0) {
76 		dev_err(rsmu->dev, "i2c_transfer failed at addr: %04x!", reg);
77 		return cnt;
78 	} else if (cnt != 2) {
79 		dev_err(rsmu->dev,
80 			"i2c_transfer sent only %d of 2 messages", cnt);
81 		return -EIO;
82 	}
83 
84 	return 0;
85 }
86 
87 static int rsmu_write_device(struct rsmu_ddata *rsmu, u8 reg, u8 *buf, u16 bytes)
88 {
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 */
91 	int cnt;
92 
93 	if (bytes > RSMU_MAX_WRITE_COUNT)
94 		return -EINVAL;
95 
96 	msg[0] = reg;
97 	memcpy(&msg[1], buf, bytes);
98 
99 	cnt = i2c_master_send(client, msg, bytes + 1);
100 
101 	if (cnt < 0) {
102 		dev_err(&client->dev,
103 			"i2c_master_send failed at addr: %04x!", reg);
104 		return cnt;
105 	}
106 
107 	return 0;
108 }
109 
110 static int rsmu_write_page_register(struct rsmu_ddata *rsmu, u32 reg)
111 {
112 	u32 page = reg & RSMU_CM_PAGE_MASK;
113 	u8 buf[4];
114 	int err;
115 
116 	/* Do not modify offset register for none-scsr registers */
117 	if (reg < RSMU_CM_SCSR_BASE)
118 		return 0;
119 
120 	/* Simply return if we are on the same page */
121 	if (rsmu->page == page)
122 		return 0;
123 
124 	buf[0] = 0x0;
125 	buf[1] = (u8)((page >> 8) & 0xFF);
126 	buf[2] = (u8)((page >> 16) & 0xFF);
127 	buf[3] = (u8)((page >> 24) & 0xFF);
128 
129 	err = rsmu_write_device(rsmu, RSMU_CM_PAGE_ADDR, buf, sizeof(buf));
130 	if (err)
131 		dev_err(rsmu->dev, "Failed to set page offset 0x%x\n", page);
132 	else
133 		/* Remember the last page */
134 		rsmu->page = page;
135 
136 	return err;
137 }
138 
139 static int rsmu_reg_read(void *context, unsigned int reg, unsigned int *val)
140 {
141 	struct rsmu_ddata *rsmu = i2c_get_clientdata((struct i2c_client *)context);
142 	u8 addr = (u8)(reg & RSMU_CM_ADDRESS_MASK);
143 	int err;
144 
145 	err = rsmu_write_page_register(rsmu, reg);
146 	if (err)
147 		return err;
148 
149 	err = rsmu_read_device(rsmu, addr, (u8 *)val, 1);
150 	if (err)
151 		dev_err(rsmu->dev, "Failed to read offset address 0x%x\n", addr);
152 
153 	return err;
154 }
155 
156 static int rsmu_reg_write(void *context, unsigned int reg, unsigned int val)
157 {
158 	struct rsmu_ddata *rsmu = i2c_get_clientdata((struct i2c_client *)context);
159 	u8 addr = (u8)(reg & RSMU_CM_ADDRESS_MASK);
160 	u8 data = (u8)val;
161 	int err;
162 
163 	err = rsmu_write_page_register(rsmu, reg);
164 	if (err)
165 		return err;
166 
167 	err = rsmu_write_device(rsmu, addr, &data, 1);
168 	if (err)
169 		dev_err(rsmu->dev,
170 			"Failed to write offset address 0x%x\n", addr);
171 
172 	return err;
173 }
174 
175 static const struct regmap_config rsmu_cm_regmap_config = {
176 	.reg_bits = 32,
177 	.val_bits = 8,
178 	.max_register = 0x20120000,
179 	.reg_read = rsmu_reg_read,
180 	.reg_write = rsmu_reg_write,
181 	.cache_type = REGCACHE_NONE,
182 };
183 
184 static const struct regmap_config rsmu_sabre_regmap_config = {
185 	.reg_bits = 8,
186 	.val_bits = 8,
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,
193 };
194 
195 static const struct regmap_config rsmu_sl_regmap_config = {
196 	.reg_bits = 16,
197 	.val_bits = 8,
198 	.reg_format_endian = REGMAP_ENDIAN_BIG,
199 	.max_register = 0x340,
200 	.cache_type = REGCACHE_NONE,
201 	.can_multi_write = true,
202 };
203 
204 static int rsmu_i2c_probe(struct i2c_client *client)
205 {
206 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
207 	const struct regmap_config *cfg;
208 	struct rsmu_ddata *rsmu;
209 	int ret;
210 
211 	rsmu = devm_kzalloc(&client->dev, sizeof(*rsmu), GFP_KERNEL);
212 	if (!rsmu)
213 		return -ENOMEM;
214 
215 	i2c_set_clientdata(client, rsmu);
216 
217 	rsmu->dev = &client->dev;
218 	rsmu->type = (enum rsmu_type)id->driver_data;
219 
220 	switch (rsmu->type) {
221 	case RSMU_CM:
222 		cfg = &rsmu_cm_regmap_config;
223 		break;
224 	case RSMU_SABRE:
225 		cfg = &rsmu_sabre_regmap_config;
226 		break;
227 	case RSMU_SL:
228 		cfg = &rsmu_sl_regmap_config;
229 		break;
230 	default:
231 		dev_err(rsmu->dev, "Unsupported RSMU device type: %d\n", rsmu->type);
232 		return -ENODEV;
233 	}
234 
235 	if (rsmu->type == RSMU_CM)
236 		rsmu->regmap = devm_regmap_init(&client->dev, NULL, client, cfg);
237 	else
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);
242 		return ret;
243 	}
244 
245 	return rsmu_core_init(rsmu);
246 }
247 
248 static void rsmu_i2c_remove(struct i2c_client *client)
249 {
250 	struct rsmu_ddata *rsmu = i2c_get_clientdata(client);
251 
252 	rsmu_core_exit(rsmu);
253 }
254 
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 },
262 	{}
263 };
264 MODULE_DEVICE_TABLE(i2c, rsmu_i2c_id);
265 
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 },
273 	{}
274 };
275 MODULE_DEVICE_TABLE(of, rsmu_i2c_of_match);
276 
277 static struct i2c_driver rsmu_i2c_driver = {
278 	.driver = {
279 		.name = "rsmu-i2c",
280 		.of_match_table = of_match_ptr(rsmu_i2c_of_match),
281 	},
282 	.probe = rsmu_i2c_probe,
283 	.remove	= rsmu_i2c_remove,
284 	.id_table = rsmu_i2c_id,
285 };
286 
287 static int __init rsmu_i2c_init(void)
288 {
289 	return i2c_add_driver(&rsmu_i2c_driver);
290 }
291 subsys_initcall(rsmu_i2c_init);
292 
293 static void __exit rsmu_i2c_exit(void)
294 {
295 	i2c_del_driver(&rsmu_i2c_driver);
296 }
297 module_exit(rsmu_i2c_exit);
298 
299 MODULE_DESCRIPTION("Renesas SMU I2C driver");
300 MODULE_LICENSE("GPL");
301