sdca_regmap.c 10 KB

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