regmap-sdw-mbq.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright(c) 2020 Intel Corporation.
  3. #include <linux/bits.h>
  4. #include <linux/delay.h>
  5. #include <linux/device.h>
  6. #include <linux/errno.h>
  7. #include <linux/iopoll.h>
  8. #include <linux/module.h>
  9. #include <linux/regmap.h>
  10. #include <linux/soundwire/sdw.h>
  11. #include <linux/soundwire/sdw_registers.h>
  12. #include <sound/sdca_function.h>
  13. #include "internal.h"
  14. struct regmap_mbq_context {
  15. struct device *dev;
  16. struct sdw_slave *sdw;
  17. bool (*readable_reg)(struct device *dev, unsigned int reg);
  18. struct regmap_sdw_mbq_cfg cfg;
  19. int val_size;
  20. };
  21. static int regmap_sdw_mbq_size(struct regmap_mbq_context *ctx, unsigned int reg)
  22. {
  23. int size = ctx->val_size;
  24. if (ctx->cfg.mbq_size) {
  25. size = ctx->cfg.mbq_size(ctx->dev, reg);
  26. if (!size || size > ctx->val_size)
  27. return -EINVAL;
  28. }
  29. return size;
  30. }
  31. static bool regmap_sdw_mbq_deferrable(struct regmap_mbq_context *ctx, unsigned int reg)
  32. {
  33. if (ctx->cfg.deferrable)
  34. return ctx->cfg.deferrable(ctx->dev, reg);
  35. return false;
  36. }
  37. static int regmap_sdw_mbq_poll_busy(struct sdw_slave *slave, unsigned int reg,
  38. struct regmap_mbq_context *ctx)
  39. {
  40. struct device *dev = ctx->dev;
  41. int val, ret = 0;
  42. dev_dbg(dev, "Deferring transaction for 0x%x\n", reg);
  43. reg = SDW_SDCA_CTL(SDW_SDCA_CTL_FUNC(reg), 0,
  44. SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0);
  45. if (ctx->readable_reg(dev, reg)) {
  46. ret = read_poll_timeout(sdw_read_no_pm, val,
  47. val < 0 || !(val & SDCA_CTL_ENTITY_0_FUNCTION_BUSY),
  48. ctx->cfg.timeout_us, ctx->cfg.retry_us,
  49. false, slave, reg);
  50. if (val < 0)
  51. return val;
  52. if (ret)
  53. dev_err(dev, "Function busy timed out 0x%x: %d\n", reg, val);
  54. } else {
  55. fsleep(ctx->cfg.timeout_us);
  56. }
  57. return ret;
  58. }
  59. static int regmap_sdw_mbq_write_impl(struct sdw_slave *slave,
  60. unsigned int reg, unsigned int val,
  61. int mbq_size, bool deferrable)
  62. {
  63. int shift = mbq_size * BITS_PER_BYTE;
  64. int ret;
  65. while (--mbq_size > 0) {
  66. shift -= BITS_PER_BYTE;
  67. ret = sdw_write_no_pm(slave, SDW_SDCA_MBQ_CTL(reg),
  68. (val >> shift) & 0xff);
  69. if (ret < 0)
  70. return ret;
  71. }
  72. ret = sdw_write_no_pm(slave, reg, val & 0xff);
  73. if (deferrable && ret == -ENODATA)
  74. return -EAGAIN;
  75. return ret;
  76. }
  77. static int regmap_sdw_mbq_write(void *context, unsigned int reg, unsigned int val)
  78. {
  79. struct regmap_mbq_context *ctx = context;
  80. struct sdw_slave *slave = ctx->sdw;
  81. bool deferrable = regmap_sdw_mbq_deferrable(ctx, reg);
  82. int mbq_size = regmap_sdw_mbq_size(ctx, reg);
  83. int ret;
  84. if (mbq_size < 0)
  85. return mbq_size;
  86. /*
  87. * Technically the spec does allow a device to set itself to busy for
  88. * internal reasons, but since it doesn't provide any information on
  89. * how to handle timeouts in that case, for now the code will only
  90. * process a single wait/timeout on function busy and a single retry
  91. * of the transaction.
  92. */
  93. ret = regmap_sdw_mbq_write_impl(slave, reg, val, mbq_size, deferrable);
  94. if (ret == -EAGAIN) {
  95. ret = regmap_sdw_mbq_poll_busy(slave, reg, ctx);
  96. if (ret)
  97. return ret;
  98. ret = regmap_sdw_mbq_write_impl(slave, reg, val, mbq_size, false);
  99. }
  100. return ret;
  101. }
  102. static int regmap_sdw_mbq_read_impl(struct sdw_slave *slave,
  103. unsigned int reg, unsigned int *val,
  104. int mbq_size, bool deferrable)
  105. {
  106. int shift = BITS_PER_BYTE;
  107. int read;
  108. read = sdw_read_no_pm(slave, reg);
  109. if (read < 0) {
  110. if (deferrable && read == -ENODATA)
  111. return -EAGAIN;
  112. return read;
  113. }
  114. *val = read;
  115. while (--mbq_size > 0) {
  116. read = sdw_read_no_pm(slave, SDW_SDCA_MBQ_CTL(reg));
  117. if (read < 0)
  118. return read;
  119. *val |= read << shift;
  120. shift += BITS_PER_BYTE;
  121. }
  122. return 0;
  123. }
  124. static int regmap_sdw_mbq_read(void *context, unsigned int reg, unsigned int *val)
  125. {
  126. struct regmap_mbq_context *ctx = context;
  127. struct sdw_slave *slave = ctx->sdw;
  128. bool deferrable = regmap_sdw_mbq_deferrable(ctx, reg);
  129. int mbq_size = regmap_sdw_mbq_size(ctx, reg);
  130. int ret;
  131. if (mbq_size < 0)
  132. return mbq_size;
  133. /*
  134. * Technically the spec does allow a device to set itself to busy for
  135. * internal reasons, but since it doesn't provide any information on
  136. * how to handle timeouts in that case, for now the code will only
  137. * process a single wait/timeout on function busy and a single retry
  138. * of the transaction.
  139. */
  140. ret = regmap_sdw_mbq_read_impl(slave, reg, val, mbq_size, deferrable);
  141. if (ret == -EAGAIN) {
  142. ret = regmap_sdw_mbq_poll_busy(slave, reg, ctx);
  143. if (ret)
  144. return ret;
  145. ret = regmap_sdw_mbq_read_impl(slave, reg, val, mbq_size, false);
  146. }
  147. return ret;
  148. }
  149. static const struct regmap_bus regmap_sdw_mbq = {
  150. .reg_read = regmap_sdw_mbq_read,
  151. .reg_write = regmap_sdw_mbq_write,
  152. .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
  153. .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
  154. };
  155. static int regmap_sdw_mbq_config_check(const struct regmap_config *config)
  156. {
  157. if (config->val_bits > (sizeof(unsigned int) * BITS_PER_BYTE))
  158. return -ENOTSUPP;
  159. /* Registers are 32 bits wide */
  160. if (config->reg_bits != 32)
  161. return -ENOTSUPP;
  162. if (config->pad_bits != 0)
  163. return -ENOTSUPP;
  164. return 0;
  165. }
  166. static struct regmap_mbq_context *
  167. regmap_sdw_mbq_gen_context(struct device *dev,
  168. struct sdw_slave *sdw,
  169. const struct regmap_config *config,
  170. const struct regmap_sdw_mbq_cfg *mbq_config)
  171. {
  172. struct regmap_mbq_context *ctx;
  173. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  174. if (!ctx)
  175. return ERR_PTR(-ENOMEM);
  176. ctx->dev = dev;
  177. ctx->sdw = sdw;
  178. if (mbq_config)
  179. ctx->cfg = *mbq_config;
  180. ctx->val_size = config->val_bits / BITS_PER_BYTE;
  181. ctx->readable_reg = config->readable_reg;
  182. return ctx;
  183. }
  184. struct regmap *__regmap_init_sdw_mbq(struct device *dev, struct sdw_slave *sdw,
  185. const struct regmap_config *config,
  186. const struct regmap_sdw_mbq_cfg *mbq_config,
  187. struct lock_class_key *lock_key,
  188. const char *lock_name)
  189. {
  190. struct regmap_mbq_context *ctx;
  191. int ret;
  192. ret = regmap_sdw_mbq_config_check(config);
  193. if (ret)
  194. return ERR_PTR(ret);
  195. ctx = regmap_sdw_mbq_gen_context(dev, sdw, config, mbq_config);
  196. if (IS_ERR(ctx))
  197. return ERR_CAST(ctx);
  198. return __regmap_init(dev, &regmap_sdw_mbq, ctx,
  199. config, lock_key, lock_name);
  200. }
  201. EXPORT_SYMBOL_GPL(__regmap_init_sdw_mbq);
  202. struct regmap *__devm_regmap_init_sdw_mbq(struct device *dev, struct sdw_slave *sdw,
  203. const struct regmap_config *config,
  204. const struct regmap_sdw_mbq_cfg *mbq_config,
  205. struct lock_class_key *lock_key,
  206. const char *lock_name)
  207. {
  208. struct regmap_mbq_context *ctx;
  209. int ret;
  210. ret = regmap_sdw_mbq_config_check(config);
  211. if (ret)
  212. return ERR_PTR(ret);
  213. ctx = regmap_sdw_mbq_gen_context(dev, sdw, config, mbq_config);
  214. if (IS_ERR(ctx))
  215. return ERR_CAST(ctx);
  216. return __devm_regmap_init(dev, &regmap_sdw_mbq, ctx,
  217. config, lock_key, lock_name);
  218. }
  219. EXPORT_SYMBOL_GPL(__devm_regmap_init_sdw_mbq);
  220. MODULE_DESCRIPTION("regmap SoundWire MBQ Module");
  221. MODULE_LICENSE("GPL");