Linux Audio

Check our new training course

Loading...
v6.8
  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
 
 
 
 
 
 
 
 
 
 
 
 
 35static 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
 47static 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
 57static 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
 87static 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
110static 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
139static 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
156static 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
175static 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
184static 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
195static 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
204static 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
248static 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
255static 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};
264MODULE_DEVICE_TABLE(i2c, rsmu_i2c_id);
265
266static 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};
275MODULE_DEVICE_TABLE(of, rsmu_i2c_of_match);
276
277static struct i2c_driver rsmu_i2c_driver = {
278	.driver = {
279		.name = "rsmu-i2c",
280		.of_match_table = rsmu_i2c_of_match,
281	},
282	.probe = rsmu_i2c_probe,
283	.remove	= rsmu_i2c_remove,
284	.id_table = rsmu_i2c_id,
285};
286
287static int __init rsmu_i2c_init(void)
288{
289	return i2c_add_driver(&rsmu_i2c_driver);
290}
291subsys_initcall(rsmu_i2c_init);
292
293static void __exit rsmu_i2c_exit(void)
294{
295	i2c_del_driver(&rsmu_i2c_driver);
296}
297module_exit(rsmu_i2c_exit);
298
299MODULE_DESCRIPTION("Renesas SMU I2C driver");
300MODULE_LICENSE("GPL");
v6.2
  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 * 16-bit register address: the lower 8 bits of the register address come
 22 * from the offset addr byte and the upper 8 bits come from the page register.
 23 */
 24#define	RSMU_CM_PAGE_ADDR		0xFD
 25#define	RSMU_CM_PAGE_WINDOW		256
 
 26
 27/*
 28 * 15-bit register address: the lower 7 bits of the register address come
 29 * from the offset addr byte and the upper 8 bits come from the page register.
 30 */
 31#define	RSMU_SABRE_PAGE_ADDR		0x7F
 32#define	RSMU_SABRE_PAGE_WINDOW		128
 33
 34static const struct regmap_range_cfg rsmu_cm_range_cfg[] = {
 35	{
 36		.range_min = 0,
 37		.range_max = 0xD000,
 38		.selector_reg = RSMU_CM_PAGE_ADDR,
 39		.selector_mask = 0xFF,
 40		.selector_shift = 0,
 41		.window_start = 0,
 42		.window_len = RSMU_CM_PAGE_WINDOW,
 43	}
 44};
 45
 46static const struct regmap_range_cfg rsmu_sabre_range_cfg[] = {
 47	{
 48		.range_min = 0,
 49		.range_max = 0x400,
 50		.selector_reg = RSMU_SABRE_PAGE_ADDR,
 51		.selector_mask = 0xFF,
 52		.selector_shift = 0,
 53		.window_start = 0,
 54		.window_len = RSMU_SABRE_PAGE_WINDOW,
 55	}
 56};
 57
 58static bool rsmu_cm_volatile_reg(struct device *dev, unsigned int reg)
 59{
 60	switch (reg) {
 61	case RSMU_CM_PAGE_ADDR:
 62		return false;
 63	default:
 64		return true;
 65	}
 66}
 67
 68static bool rsmu_sabre_volatile_reg(struct device *dev, unsigned int reg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 69{
 70	switch (reg) {
 71	case RSMU_SABRE_PAGE_ADDR:
 72		return false;
 73	default:
 74		return true;
 
 
 
 
 
 
 
 
 
 
 
 75	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 76}
 77
 78static const struct regmap_config rsmu_cm_regmap_config = {
 79	.reg_bits = 8,
 80	.val_bits = 8,
 81	.max_register = 0xD000,
 82	.ranges = rsmu_cm_range_cfg,
 83	.num_ranges = ARRAY_SIZE(rsmu_cm_range_cfg),
 84	.volatile_reg = rsmu_cm_volatile_reg,
 85	.cache_type = REGCACHE_RBTREE,
 86	.can_multi_write = true,
 87};
 88
 89static const struct regmap_config rsmu_sabre_regmap_config = {
 90	.reg_bits = 8,
 91	.val_bits = 8,
 92	.max_register = 0x400,
 93	.ranges = rsmu_sabre_range_cfg,
 94	.num_ranges = ARRAY_SIZE(rsmu_sabre_range_cfg),
 95	.volatile_reg = rsmu_sabre_volatile_reg,
 96	.cache_type = REGCACHE_RBTREE,
 97	.can_multi_write = true,
 98};
 99
100static const struct regmap_config rsmu_sl_regmap_config = {
101	.reg_bits = 16,
102	.val_bits = 8,
103	.reg_format_endian = REGMAP_ENDIAN_BIG,
104	.max_register = 0x339,
105	.cache_type = REGCACHE_NONE,
106	.can_multi_write = true,
107};
108
109static int rsmu_i2c_probe(struct i2c_client *client)
110{
111	const struct i2c_device_id *id = i2c_client_get_device_id(client);
112	const struct regmap_config *cfg;
113	struct rsmu_ddata *rsmu;
114	int ret;
115
116	rsmu = devm_kzalloc(&client->dev, sizeof(*rsmu), GFP_KERNEL);
117	if (!rsmu)
118		return -ENOMEM;
119
120	i2c_set_clientdata(client, rsmu);
121
122	rsmu->dev = &client->dev;
123	rsmu->type = (enum rsmu_type)id->driver_data;
124
125	switch (rsmu->type) {
126	case RSMU_CM:
127		cfg = &rsmu_cm_regmap_config;
128		break;
129	case RSMU_SABRE:
130		cfg = &rsmu_sabre_regmap_config;
131		break;
132	case RSMU_SL:
133		cfg = &rsmu_sl_regmap_config;
134		break;
135	default:
136		dev_err(rsmu->dev, "Unsupported RSMU device type: %d\n", rsmu->type);
137		return -ENODEV;
138	}
139	rsmu->regmap = devm_regmap_init_i2c(client, cfg);
 
 
 
 
140	if (IS_ERR(rsmu->regmap)) {
141		ret = PTR_ERR(rsmu->regmap);
142		dev_err(rsmu->dev, "Failed to allocate register map: %d\n", ret);
143		return ret;
144	}
145
146	return rsmu_core_init(rsmu);
147}
148
149static void rsmu_i2c_remove(struct i2c_client *client)
150{
151	struct rsmu_ddata *rsmu = i2c_get_clientdata(client);
152
153	rsmu_core_exit(rsmu);
154}
155
156static const struct i2c_device_id rsmu_i2c_id[] = {
157	{ "8a34000",  RSMU_CM },
158	{ "8a34001",  RSMU_CM },
159	{ "82p33810", RSMU_SABRE },
160	{ "82p33811", RSMU_SABRE },
161	{ "8v19n850", RSMU_SL },
162	{ "8v19n851", RSMU_SL },
163	{}
164};
165MODULE_DEVICE_TABLE(i2c, rsmu_i2c_id);
166
167static const struct of_device_id rsmu_i2c_of_match[] = {
168	{ .compatible = "idt,8a34000",  .data = (void *)RSMU_CM },
169	{ .compatible = "idt,8a34001",  .data = (void *)RSMU_CM },
170	{ .compatible = "idt,82p33810", .data = (void *)RSMU_SABRE },
171	{ .compatible = "idt,82p33811", .data = (void *)RSMU_SABRE },
172	{ .compatible = "idt,8v19n850", .data = (void *)RSMU_SL },
173	{ .compatible = "idt,8v19n851", .data = (void *)RSMU_SL },
174	{}
175};
176MODULE_DEVICE_TABLE(of, rsmu_i2c_of_match);
177
178static struct i2c_driver rsmu_i2c_driver = {
179	.driver = {
180		.name = "rsmu-i2c",
181		.of_match_table = of_match_ptr(rsmu_i2c_of_match),
182	},
183	.probe_new = rsmu_i2c_probe,
184	.remove	= rsmu_i2c_remove,
185	.id_table = rsmu_i2c_id,
186};
187
188static int __init rsmu_i2c_init(void)
189{
190	return i2c_add_driver(&rsmu_i2c_driver);
191}
192subsys_initcall(rsmu_i2c_init);
193
194static void __exit rsmu_i2c_exit(void)
195{
196	i2c_del_driver(&rsmu_i2c_driver);
197}
198module_exit(rsmu_i2c_exit);
199
200MODULE_DESCRIPTION("Renesas SMU I2C driver");
201MODULE_LICENSE("GPL");