bolero-cdc-utils.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Copyright (c) 2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/regmap.h>
  14. #include "bolero-cdc.h"
  15. #include "internal.h"
  16. #define REG_BYTES 2
  17. #define VAL_BYTES 1
  18. const u16 macro_id_base_offset[MAX_MACRO] = {
  19. TX_START_OFFSET,
  20. RX_START_OFFSET,
  21. WSA_START_OFFSET,
  22. VA_START_OFFSET,
  23. };
  24. int bolero_get_macro_id(bool va_no_dec_flag, u16 reg)
  25. {
  26. if (reg >= TX_START_OFFSET
  27. && reg <= TX_MAX_OFFSET)
  28. return TX_MACRO;
  29. if (reg >= RX_START_OFFSET
  30. && reg <= RX_MAX_OFFSET)
  31. return RX_MACRO;
  32. if (reg >= WSA_START_OFFSET
  33. && reg <= WSA_MAX_OFFSET)
  34. return WSA_MACRO;
  35. if (!va_no_dec_flag &&
  36. (reg >= VA_START_OFFSET &&
  37. reg <= VA_MAX_OFFSET))
  38. return VA_MACRO;
  39. if (va_no_dec_flag &&
  40. (reg >= VA_START_OFFSET &&
  41. reg <= VA_TOP_MAX_OFFSET))
  42. return VA_MACRO;
  43. return -EINVAL;
  44. }
  45. static int regmap_bus_read(void *context, const void *reg, size_t reg_size,
  46. void *val, size_t val_size)
  47. {
  48. struct device *dev = context;
  49. struct bolero_priv *priv = dev_get_drvdata(dev);
  50. u16 *reg_p;
  51. u16 __reg;
  52. int macro_id, i;
  53. u8 temp = 0;
  54. int ret = -EINVAL;
  55. if (!priv) {
  56. dev_err(dev, "%s: priv is NULL\n", __func__);
  57. return ret;
  58. }
  59. if (!reg || !val) {
  60. dev_err(dev, "%s: reg or val is NULL\n", __func__);
  61. return ret;
  62. }
  63. if (reg_size != REG_BYTES) {
  64. dev_err(dev, "%s: register size %zd bytes, not supported\n",
  65. __func__, reg_size);
  66. return ret;
  67. }
  68. reg_p = (u16 *)reg;
  69. macro_id = bolero_get_macro_id(priv->va_without_decimation,
  70. reg_p[0]);
  71. if (macro_id < 0 || !priv->macros_supported[macro_id]) {
  72. dev_err_ratelimited(dev,
  73. "%s: Unsupported macro %d or reg 0x%x is invalid\n",
  74. __func__, macro_id, reg_p[0]);
  75. return ret;
  76. }
  77. mutex_lock(&priv->io_lock);
  78. for (i = 0; i < val_size; i++) {
  79. __reg = reg_p[i] - macro_id_base_offset[macro_id];
  80. ret = priv->read_dev(priv, macro_id, __reg, &temp);
  81. if (ret < 0) {
  82. dev_err_ratelimited(dev,
  83. "%s: Codec read failed (%d), reg: 0x%x, size:%zd\n",
  84. __func__, ret, reg_p[i], val_size);
  85. break;
  86. }
  87. ((u8 *)val)[i] = temp;
  88. dev_dbg(dev, "%s: Read 0x%02x from reg 0x%x\n",
  89. __func__, temp, reg_p[i]);
  90. }
  91. mutex_unlock(&priv->io_lock);
  92. return ret;
  93. }
  94. static int regmap_bus_gather_write(void *context,
  95. const void *reg, size_t reg_size,
  96. const void *val, size_t val_size)
  97. {
  98. struct device *dev = context;
  99. struct bolero_priv *priv = dev_get_drvdata(dev);
  100. u16 *reg_p;
  101. u16 __reg;
  102. int macro_id, i;
  103. int ret = -EINVAL;
  104. if (!priv) {
  105. dev_err(dev, "%s: priv is NULL\n", __func__);
  106. return ret;
  107. }
  108. if (!reg || !val) {
  109. dev_err(dev, "%s: reg or val is NULL\n", __func__);
  110. return ret;
  111. }
  112. if (reg_size != REG_BYTES) {
  113. dev_err(dev, "%s: register size %zd bytes, not supported\n",
  114. __func__, reg_size);
  115. return ret;
  116. }
  117. reg_p = (u16 *)reg;
  118. macro_id = bolero_get_macro_id(priv->va_without_decimation,
  119. reg_p[0]);
  120. if (macro_id < 0 || !priv->macros_supported[macro_id]) {
  121. dev_err_ratelimited(dev,
  122. "%s: Unsupported macro-id %d or reg 0x%x is invalid\n",
  123. __func__, macro_id, reg_p[0]);
  124. return ret;
  125. }
  126. mutex_lock(&priv->io_lock);
  127. for (i = 0; i < val_size; i++) {
  128. __reg = reg_p[i] - macro_id_base_offset[macro_id];
  129. ret = priv->write_dev(priv, macro_id, __reg, ((u8 *)val)[i]);
  130. if (ret < 0) {
  131. dev_err_ratelimited(dev,
  132. "%s: Codec write failed (%d), reg:0x%x, size:%zd\n",
  133. __func__, ret, reg_p[i], val_size);
  134. break;
  135. }
  136. dev_dbg(dev, "Write %02x to reg 0x%x\n", ((u8 *)val)[i],
  137. reg_p[i]);
  138. }
  139. mutex_unlock(&priv->io_lock);
  140. return ret;
  141. }
  142. static int regmap_bus_write(void *context, const void *data, size_t count)
  143. {
  144. struct device *dev = context;
  145. struct bolero_priv *priv = dev_get_drvdata(dev);
  146. if (!priv)
  147. return -EINVAL;
  148. if (count < REG_BYTES) {
  149. dev_err(dev, "%s: count %zd bytes < %d, not supported\n",
  150. __func__, count, REG_BYTES);
  151. return -EINVAL;
  152. }
  153. return regmap_bus_gather_write(context, data, REG_BYTES,
  154. data + REG_BYTES,
  155. count - REG_BYTES);
  156. }
  157. static struct regmap_bus regmap_bus_config = {
  158. .write = regmap_bus_write,
  159. .gather_write = regmap_bus_gather_write,
  160. .read = regmap_bus_read,
  161. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  162. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  163. };
  164. struct regmap *bolero_regmap_init(struct device *dev,
  165. const struct regmap_config *config)
  166. {
  167. return devm_regmap_init(dev, &regmap_bus_config, dev, config);
  168. }