1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/bitops.h>
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/soundwire/sdw_registers.h>
#include <linux/types.h>
#include <sound/sdca.h>
#include <sound/sdca_function.h>
#include <sound/sdca_regmap.h>
static struct sdca_entity *
function_find_entity(struct sdca_function_data *function, unsigned int reg)
{
int i;
for (i = 0; i < function->num_entities; i++)
if (SDW_SDCA_CTL_ENT(reg) == function->entities[i].id)
return &function->entities[i];
return NULL;
}
static struct sdca_control *
entity_find_control(struct sdca_entity *entity, unsigned int reg)
{
int i;
for (i = 0; i < entity->num_controls; i++) {
if (SDW_SDCA_CTL_CSEL(reg) == entity->controls[i].sel)
return &entity->controls[i];
}
return NULL;
}
static struct sdca_control *
function_find_control(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_entity *entity;
entity = function_find_entity(function, reg);
if (!entity)
return NULL;
return entity_find_control(entity, reg);
}
/**
* sdca_regmap_readable - return if a given SDCA Control is readable
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns true if the register is readable.
*/
bool sdca_regmap_readable(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return false;
control = function_find_control(function, reg);
if (!control)
return false;
if (!(BIT(SDW_SDCA_CTL_CNUM(reg)) & control->cn_list))
return false;
switch (control->mode) {
case SDCA_ACCESS_MODE_RW:
case SDCA_ACCESS_MODE_RO:
case SDCA_ACCESS_MODE_RW1S:
case SDCA_ACCESS_MODE_RW1C:
if (SDW_SDCA_NEXT_CTL(0) & reg)
return false;
fallthrough;
case SDCA_ACCESS_MODE_DUAL:
/* No access to registers marked solely for device use */
return control->layers & ~SDCA_ACCESS_LAYER_DEVICE;
default:
return false;
}
}
EXPORT_SYMBOL_NS(sdca_regmap_readable, "SND_SOC_SDCA");
/**
* sdca_regmap_writeable - return if a given SDCA Control is writeable
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns true if the register is writeable.
*/
bool sdca_regmap_writeable(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return false;
control = function_find_control(function, reg);
if (!control)
return false;
if (!(BIT(SDW_SDCA_CTL_CNUM(reg)) & control->cn_list))
return false;
switch (control->mode) {
case SDCA_ACCESS_MODE_RW:
case SDCA_ACCESS_MODE_RW1S:
case SDCA_ACCESS_MODE_RW1C:
if (SDW_SDCA_NEXT_CTL(0) & reg)
return false;
fallthrough;
case SDCA_ACCESS_MODE_DUAL:
/* No access to registers marked solely for device use */
return control->layers & ~SDCA_ACCESS_LAYER_DEVICE;
default:
return false;
}
}
EXPORT_SYMBOL_NS(sdca_regmap_writeable, "SND_SOC_SDCA");
/**
* sdca_regmap_volatile - return if a given SDCA Control is volatile
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns true if the register is volatile.
*/
bool sdca_regmap_volatile(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return false;
control = function_find_control(function, reg);
if (!control)
return false;
return control->is_volatile;
}
EXPORT_SYMBOL_NS(sdca_regmap_volatile, "SND_SOC_SDCA");
/**
* sdca_regmap_deferrable - return if a given SDCA Control is deferrable
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns true if the register is deferrable.
*/
bool sdca_regmap_deferrable(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return false;
control = function_find_control(function, reg);
if (!control)
return false;
return control->deferrable;
}
EXPORT_SYMBOL_NS(sdca_regmap_deferrable, "SND_SOC_SDCA");
/**
* sdca_regmap_mbq_size - return size in bytes of a given SDCA Control
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns the size in bytes of the Control.
*/
int sdca_regmap_mbq_size(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return -EINVAL;
control = function_find_control(function, reg);
if (!control)
return -EINVAL;
return clamp_val(control->nbits / BITS_PER_BYTE, sizeof(u8), sizeof(u32));
}
EXPORT_SYMBOL_NS(sdca_regmap_mbq_size, "SND_SOC_SDCA");
/**
* sdca_regmap_count_constants - count the number of DisCo constant Controls
* @dev: Pointer to the device.
* @function: Pointer to the Function information, to be parsed.
*
* This function returns the number of DisCo constant Controls present
* in a function. Typically this information will be used to populate
* the regmap defaults array, allowing drivers to access the values of
* DisCo constants as any other physical register.
*
* Return: Returns number of DisCo constant controls, or a negative error
* code on failure.
*/
int sdca_regmap_count_constants(struct device *dev,
struct sdca_function_data *function)
{
int nconsts = 0;
int i, j;
for (i = 0; i < function->num_entities; i++) {
struct sdca_entity *entity = &function->entities[i];
for (j = 0; j < entity->num_controls; j++) {
if (entity->controls[j].mode == SDCA_ACCESS_MODE_DC)
nconsts += hweight64(entity->controls[j].cn_list);
}
}
return nconsts;
}
EXPORT_SYMBOL_NS(sdca_regmap_count_constants, "SND_SOC_SDCA");
/**
* sdca_regmap_populate_constants - fill an array with DisCo constant values
* @dev: Pointer to the device.
* @function: Pointer to the Function information, to be parsed.
* @consts: Pointer to the array which should be filled with the DisCo
* constant values.
*
* This function will populate a regmap struct reg_default array with
* the values of the DisCo constants for a given Function. This
* allows to access the values of DisCo constants the same as any
* other physical register.
*
* Return: Returns the number of constants populated on success, a negative
* error code on failure.
*/
int sdca_regmap_populate_constants(struct device *dev,
struct sdca_function_data *function,
struct reg_default *consts)
{
int i, j, k, l;
for (i = 0, k = 0; i < function->num_entities; i++) {
struct sdca_entity *entity = &function->entities[i];
for (j = 0; j < entity->num_controls; j++) {
struct sdca_control *control = &entity->controls[j];
int cn;
if (control->mode != SDCA_ACCESS_MODE_DC)
continue;
l = 0;
for_each_set_bit(cn, (unsigned long *)&control->cn_list,
BITS_PER_TYPE(control->cn_list)) {
consts[k].reg = SDW_SDCA_CTL(function->desc->adr,
entity->id,
control->sel, cn);
consts[k].def = control->values[l];
k++;
l++;
}
}
}
return k;
}
EXPORT_SYMBOL_NS(sdca_regmap_populate_constants, "SND_SOC_SDCA");
/**
* sdca_regmap_write_defaults - write out DisCo defaults to device
* @dev: Pointer to the device.
* @regmap: Pointer to the Function register map.
* @function: Pointer to the Function information, to be parsed.
*
* This function will write out to the hardware all the DisCo default and
* fixed value controls. This will cause them to be populated into the cache,
* and subsequent handling can be done through a cache sync.
*
* Return: Returns zero on success, and a negative error code on failure.
*/
int sdca_regmap_write_defaults(struct device *dev, struct regmap *regmap,
struct sdca_function_data *function)
{
int i, j, k;
int ret;
for (i = 0; i < function->num_entities; i++) {
struct sdca_entity *entity = &function->entities[i];
for (j = 0; j < entity->num_controls; j++) {
struct sdca_control *control = &entity->controls[j];
int cn;
if (control->mode == SDCA_ACCESS_MODE_DC)
continue;
if (!control->has_default && !control->has_fixed)
continue;
k = 0;
for_each_set_bit(cn, (unsigned long *)&control->cn_list,
BITS_PER_TYPE(control->cn_list)) {
unsigned int reg;
reg = SDW_SDCA_CTL(function->desc->adr, entity->id,
control->sel, cn);
ret = regmap_write(regmap, reg, control->values[k]);
if (ret)
return ret;
k++;
}
}
}
return 0;
}
EXPORT_SYMBOL_NS(sdca_regmap_write_defaults, "SND_SOC_SDCA");
|