regmap-swr.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/device.h>
  7. #include <linux/slab.h>
  8. #include <linux/mutex.h>
  9. #include <linux/regmap.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <soc/soundwire.h>
  13. #include <soc/internal.h>
  14. static int regmap_swr_gather_write(void *context,
  15. const void *reg, size_t reg_size,
  16. const void *val, size_t val_len)
  17. {
  18. struct device *dev = context;
  19. struct swr_device *swr = to_swr_device(dev);
  20. struct regmap *map = dev_get_regmap(dev, NULL);
  21. size_t addr_bytes;
  22. size_t val_bytes;
  23. int i, ret = 0;
  24. u16 reg_addr = 0;
  25. u8 *value;
  26. if (map == NULL) {
  27. dev_err_ratelimited(dev, "%s: regmap is NULL\n", __func__);
  28. return -EINVAL;
  29. }
  30. addr_bytes = map->format.reg_bytes;
  31. if (swr == NULL) {
  32. dev_err_ratelimited(dev, "%s: swr device is NULL\n", __func__);
  33. return -EINVAL;
  34. }
  35. if (reg_size != addr_bytes) {
  36. dev_err_ratelimited(dev, "%s: reg size %zd bytes not supported\n",
  37. __func__, reg_size);
  38. return -EINVAL;
  39. }
  40. reg_addr = *(u16 *)reg;
  41. val_bytes = map->format.val_bytes;
  42. /* val_len = val_bytes * val_count */
  43. for (i = 0; i < (val_len / val_bytes); i++) {
  44. value = (u8 *)val + (val_bytes * i);
  45. ret = swr_write(swr, swr->dev_num, (reg_addr + i), value);
  46. if (ret < 0) {
  47. dev_err_ratelimited(dev, "%s: write reg 0x%x failed, err %d\n",
  48. __func__, (reg_addr + i), ret);
  49. break;
  50. }
  51. }
  52. return ret;
  53. }
  54. static int regmap_swr_raw_multi_reg_write(void *context, const void *data,
  55. size_t count)
  56. {
  57. struct device *dev = context;
  58. struct swr_device *swr = to_swr_device(dev);
  59. struct regmap *map = dev_get_regmap(dev, NULL);
  60. size_t addr_bytes;
  61. size_t val_bytes;
  62. size_t pad_bytes;
  63. size_t num_regs;
  64. int i = 0;
  65. int ret = 0;
  66. u16 *reg;
  67. u8 *val;
  68. u8 *buf;
  69. if (swr == NULL) {
  70. dev_err_ratelimited(dev, "%s: swr device is NULL\n", __func__);
  71. return -EINVAL;
  72. }
  73. if (map == NULL) {
  74. dev_err_ratelimited(dev, "%s: regmap is NULL\n", __func__);
  75. return -EINVAL;
  76. }
  77. addr_bytes = map->format.reg_bytes;
  78. val_bytes = map->format.val_bytes;
  79. pad_bytes = map->format.pad_bytes;
  80. if (addr_bytes + val_bytes + pad_bytes == 0) {
  81. dev_err_ratelimited(dev, "%s: sum of addr, value and pad is 0\n", __func__);
  82. return -EINVAL;
  83. }
  84. num_regs = count / (addr_bytes + val_bytes + pad_bytes);
  85. reg = kcalloc(num_regs, sizeof(u16), GFP_KERNEL);
  86. if (!reg)
  87. return -ENOMEM;
  88. val = kcalloc(num_regs, sizeof(u8), GFP_KERNEL);
  89. if (!val) {
  90. ret = -ENOMEM;
  91. goto mem_fail;
  92. }
  93. buf = (u8 *)data;
  94. for (i = 0; i < num_regs; i++) {
  95. reg[i] = *(u16 *)buf;
  96. buf += (map->format.reg_bytes + map->format.pad_bytes);
  97. val[i] = *buf;
  98. buf += map->format.val_bytes;
  99. }
  100. ret = swr_bulk_write(swr, swr->dev_num, reg, val, num_regs);
  101. if (ret)
  102. dev_err_ratelimited(dev, "%s: multi reg write failed\n", __func__);
  103. kfree(val);
  104. mem_fail:
  105. kfree(reg);
  106. return ret;
  107. }
  108. static int regmap_swr_write(void *context, const void *data, size_t count)
  109. {
  110. struct device *dev = context;
  111. struct regmap *map = dev_get_regmap(dev, NULL);
  112. size_t addr_bytes;
  113. size_t val_bytes;
  114. size_t pad_bytes;
  115. if (map == NULL) {
  116. dev_err_ratelimited(dev, "%s: regmap is NULL\n", __func__);
  117. return -EINVAL;
  118. }
  119. addr_bytes = map->format.reg_bytes;
  120. val_bytes = map->format.val_bytes;
  121. pad_bytes = map->format.pad_bytes;
  122. WARN_ON(count < addr_bytes);
  123. if (count > (addr_bytes + val_bytes + pad_bytes))
  124. return regmap_swr_raw_multi_reg_write(context, data, count);
  125. else
  126. return regmap_swr_gather_write(context, data, addr_bytes,
  127. (data + addr_bytes),
  128. (count - addr_bytes));
  129. }
  130. static int regmap_swr_read(void *context,
  131. const void *reg, size_t reg_size,
  132. void *val, size_t val_size)
  133. {
  134. struct device *dev = context;
  135. struct swr_device *swr = to_swr_device(dev);
  136. struct regmap *map = dev_get_regmap(dev, NULL);
  137. size_t addr_bytes;
  138. int ret = 0;
  139. u16 reg_addr = 0;
  140. if (map == NULL) {
  141. dev_err_ratelimited(dev, "%s: regmap is NULL\n", __func__);
  142. return -EINVAL;
  143. }
  144. addr_bytes = map->format.reg_bytes;
  145. if (swr == NULL) {
  146. dev_err_ratelimited(dev, "%s: swr is NULL\n", __func__);
  147. return -EINVAL;
  148. }
  149. if (reg_size != addr_bytes) {
  150. dev_err_ratelimited(dev, "%s: register size %zd bytes not supported\n",
  151. __func__, reg_size);
  152. return -EINVAL;
  153. }
  154. reg_addr = *(u16 *)reg;
  155. ret = swr_read(swr, swr->dev_num, reg_addr, val, val_size);
  156. if (ret < 0)
  157. dev_err_ratelimited(dev, "%s: codec reg 0x%x read failed %d\n",
  158. __func__, reg_addr, ret);
  159. return ret;
  160. }
  161. static struct regmap_bus regmap_swr = {
  162. .write = regmap_swr_write,
  163. .gather_write = regmap_swr_gather_write,
  164. .read = regmap_swr_read,
  165. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  166. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  167. };
  168. struct regmap *__regmap_init_swr(struct swr_device *swr,
  169. const struct regmap_config *config,
  170. struct lock_class_key *lock_key,
  171. const char *lock_name)
  172. {
  173. return __regmap_init(&swr->dev, &regmap_swr, &swr->dev, config,
  174. lock_key, lock_name);
  175. }
  176. EXPORT_SYMBOL(__regmap_init_swr);
  177. struct regmap *__devm_regmap_init_swr(struct swr_device *swr,
  178. const struct regmap_config *config,
  179. struct lock_class_key *lock_key,
  180. const char *lock_name)
  181. {
  182. return __devm_regmap_init(&swr->dev, &regmap_swr, &swr->dev, config,
  183. lock_key, lock_name);
  184. }
  185. EXPORT_SYMBOL(__devm_regmap_init_swr);
  186. MODULE_LICENSE("GPL v2");