regmap-swr.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 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. #define ADDR_BYTES (2)
  14. #define ADDR_BYTES_4 (4)
  15. #define VAL_BYTES (1)
  16. #define PAD_BYTES (0)
  17. #define SCP1_ADDRESS_VAL_MASK (0x7f800000)
  18. #define SCP2_ADDRESS_VAL_MASK (0x007f8000)
  19. #define BIT_WIDTH_CHECK_MASK (0xffff0000)
  20. #define SCP1_ADDRESS_VAL_SHIFT (23)
  21. #define SCP2_ADDRESS_VAL_SHIFT (15)
  22. #define SCP1_ADDRESS (0X48)
  23. #define SCP2_ADDRESS (0X49)
  24. #define SDCA_READ_WRITE_BIT (0x8000)
  25. u8 g_scp1_val;
  26. u8 g_scp2_val;
  27. static DEFINE_MUTEX(swr_rw_lock);
  28. static int regmap_swr_reg_address_get(struct swr_device *swr,
  29. u16 *reg_addr, const void *reg, size_t reg_size)
  30. {
  31. u8 scp1_val = 0, scp2_val = 0;
  32. u32 temp = 0;
  33. int ret = 0;
  34. if (reg_size == ADDR_BYTES_4) {
  35. temp = (*(u32 *)reg) & SCP1_ADDRESS_VAL_MASK;
  36. scp1_val = temp >> SCP1_ADDRESS_VAL_SHIFT;
  37. temp = (*(u32 *)reg) & SCP2_ADDRESS_VAL_MASK;
  38. scp2_val = temp >> SCP2_ADDRESS_VAL_SHIFT;
  39. if (scp1_val || scp2_val) {
  40. if (scp1_val != g_scp1_val) {
  41. ret = swr_write(swr, swr->dev_num, SCP1_ADDRESS, &scp1_val);
  42. if (ret < 0) {
  43. dev_err(&swr->dev, "%s: write reg scp1_address failed, err %d\n",
  44. __func__, ret);
  45. return ret;
  46. }
  47. g_scp1_val = scp1_val;
  48. }
  49. if (scp2_val != g_scp2_val) {
  50. ret = swr_write(swr, swr->dev_num, SCP2_ADDRESS, &scp2_val);
  51. if (ret < 0) {
  52. dev_err(&swr->dev, "%s: write reg scp2_address failed, err %d\n",
  53. __func__, ret);
  54. return ret;
  55. }
  56. g_scp2_val = scp2_val;
  57. }
  58. *reg_addr = (*(u16 *)reg | SDCA_READ_WRITE_BIT);
  59. dev_dbg(&swr->dev, "%s: reg: 0x%x, scp1_val: 0x%x, scp2_val: 0x%x, reg_addr: 0x%x\n",
  60. __func__, *(u32 *)reg, scp1_val, scp2_val, *reg_addr);
  61. } else {
  62. *reg_addr = *(u16 *)reg;
  63. }
  64. } else {
  65. *reg_addr = *(u16 *)reg;
  66. }
  67. return ret;
  68. }
  69. static int regmap_swr_gather_write(void *context,
  70. const void *reg, size_t reg_size,
  71. const void *val, size_t val_len)
  72. {
  73. struct device *dev = context;
  74. struct swr_device *swr = to_swr_device(dev);
  75. struct regmap *map = dev_get_regmap(dev, NULL);
  76. int i, ret = 0;
  77. u16 reg_addr = 0;
  78. u8 *value;
  79. if (map == NULL) {
  80. dev_err_ratelimited(dev, "%s: regmap is NULL\n", __func__);
  81. return -EINVAL;
  82. }
  83. if (swr == NULL) {
  84. dev_err_ratelimited(dev, "%s: swr device is NULL\n", __func__);
  85. return -EINVAL;
  86. }
  87. if ((reg_size != ADDR_BYTES) && (reg_size != ADDR_BYTES_4)) {
  88. dev_err_ratelimited(dev, "%s: reg size %zd bytes not supported\n",
  89. __func__, reg_size);
  90. return -EINVAL;
  91. }
  92. mutex_lock(&swr_rw_lock);
  93. ret = regmap_swr_reg_address_get(swr, &reg_addr, reg, reg_size);
  94. if (ret < 0) {
  95. mutex_unlock(&swr_rw_lock);
  96. return ret;
  97. }
  98. /* val_len = VAL_BYTES * val_count */
  99. for (i = 0; i < (val_len / VAL_BYTES); i++) {
  100. value = (u8 *)val + (VAL_BYTES * i);
  101. ret = swr_write(swr, swr->dev_num, (reg_addr + i), value);
  102. if (ret < 0) {
  103. dev_err_ratelimited(dev, "%s: write reg 0x%x failed, err %d\n",
  104. __func__, (reg_addr + i), ret);
  105. break;
  106. }
  107. dev_dbg(dev, "%s: dev_num: 0x%x, gather write reg: 0x%x, value: 0x%x\n",
  108. __func__, swr->dev_num, (reg_addr + i), *value);
  109. }
  110. mutex_unlock(&swr_rw_lock);
  111. return ret;
  112. }
  113. static int regmap_swr_raw_multi_reg_write(void *context, const void *data,
  114. size_t count)
  115. {
  116. struct device *dev = context;
  117. struct swr_device *swr = to_swr_device(dev);
  118. struct regmap *map = dev_get_regmap(dev, NULL);
  119. size_t num_regs;
  120. int i = 0;
  121. int ret = 0;
  122. u16 *reg;
  123. u8 *val;
  124. u8 *buf;
  125. if (swr == NULL) {
  126. dev_err_ratelimited(dev, "%s: swr device is NULL\n", __func__);
  127. return -EINVAL;
  128. }
  129. if (map == NULL) {
  130. dev_err_ratelimited(dev, "%s: regmap is NULL\n", __func__);
  131. return -EINVAL;
  132. }
  133. if (ADDR_BYTES + VAL_BYTES + PAD_BYTES == 0) {
  134. dev_err_ratelimited(dev, "%s: sum of addr, value and pad is 0\n", __func__);
  135. return -EINVAL;
  136. }
  137. num_regs = count / (ADDR_BYTES + VAL_BYTES + PAD_BYTES);
  138. reg = kcalloc(num_regs, sizeof(u16), GFP_KERNEL);
  139. if (!reg)
  140. return -ENOMEM;
  141. val = kcalloc(num_regs, sizeof(u8), GFP_KERNEL);
  142. if (!val) {
  143. ret = -ENOMEM;
  144. goto mem_fail;
  145. }
  146. buf = (u8 *)data;
  147. for (i = 0; i < num_regs; i++) {
  148. reg[i] = *(u16 *)buf;
  149. buf += (ADDR_BYTES + PAD_BYTES);
  150. val[i] = *buf;
  151. buf += VAL_BYTES;
  152. }
  153. ret = swr_bulk_write(swr, swr->dev_num, reg, val, num_regs);
  154. if (ret)
  155. dev_err_ratelimited(dev, "%s: multi reg write failed\n", __func__);
  156. kfree(val);
  157. mem_fail:
  158. kfree(reg);
  159. return ret;
  160. }
  161. static int regmap_swr_write(void *context, const void *data, size_t count)
  162. {
  163. struct device *dev = context;
  164. struct swr_device *swr = to_swr_device(dev);
  165. struct regmap *map = dev_get_regmap(dev, NULL);
  166. int addr_bytes = 0;
  167. if (map == NULL) {
  168. dev_err_ratelimited(dev, "%s: regmap is NULL\n", __func__);
  169. return -EINVAL;
  170. }
  171. if (swr == NULL) {
  172. dev_err_ratelimited(dev, "%s: swr is NULL\n", __func__);
  173. return -EINVAL;
  174. }
  175. addr_bytes = (swr->paging_support ? ADDR_BYTES_4 : ADDR_BYTES);
  176. WARN_ON(count < addr_bytes);
  177. if (count > (addr_bytes + VAL_BYTES + PAD_BYTES))
  178. return regmap_swr_raw_multi_reg_write(context, data, count);
  179. else
  180. return regmap_swr_gather_write(context, data, addr_bytes,
  181. (data + addr_bytes),
  182. (count - addr_bytes));
  183. }
  184. static int regmap_swr_read(void *context,
  185. const void *reg, size_t reg_size,
  186. void *val, size_t val_size)
  187. {
  188. struct device *dev = context;
  189. struct swr_device *swr = to_swr_device(dev);
  190. struct regmap *map = dev_get_regmap(dev, NULL);
  191. int ret = 0;
  192. u16 reg_addr = 0;
  193. if (map == NULL) {
  194. dev_err_ratelimited(dev, "%s: regmap is NULL\n", __func__);
  195. return -EINVAL;
  196. }
  197. if (swr == NULL) {
  198. dev_err_ratelimited(dev, "%s: swr is NULL\n", __func__);
  199. return -EINVAL;
  200. }
  201. if ((reg_size != ADDR_BYTES) && (reg_size != ADDR_BYTES_4)) {
  202. dev_err_ratelimited(dev, "%s: reg size %zd bytes not supported\n",
  203. __func__, reg_size);
  204. return -EINVAL;
  205. }
  206. mutex_lock(&swr_rw_lock);
  207. ret = regmap_swr_reg_address_get(swr, &reg_addr, reg, reg_size);
  208. if (ret < 0) {
  209. dev_err_ratelimited(dev,
  210. "%s: regmap_swr_reg_address_get failed, reg: 0x%x\n",
  211. __func__, *(u32 *)reg);
  212. mutex_unlock(&swr_rw_lock);
  213. return ret;
  214. }
  215. ret = swr_read(swr, swr->dev_num, reg_addr, val, val_size);
  216. if (ret < 0)
  217. dev_err_ratelimited(dev, "%s: codec reg 0x%x read failed %d\n",
  218. __func__, reg_addr, ret);
  219. mutex_unlock(&swr_rw_lock);
  220. return ret;
  221. }
  222. static struct regmap_bus regmap_swr = {
  223. .write = regmap_swr_write,
  224. .gather_write = regmap_swr_gather_write,
  225. .read = regmap_swr_read,
  226. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  227. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  228. };
  229. struct regmap *__regmap_init_swr(struct swr_device *swr,
  230. const struct regmap_config *config,
  231. struct lock_class_key *lock_key,
  232. const char *lock_name)
  233. {
  234. return __regmap_init(&swr->dev, &regmap_swr, &swr->dev, config,
  235. lock_key, lock_name);
  236. }
  237. EXPORT_SYMBOL(__regmap_init_swr);
  238. struct regmap *__devm_regmap_init_swr(struct swr_device *swr,
  239. const struct regmap_config *config,
  240. struct lock_class_key *lock_key,
  241. const char *lock_name)
  242. {
  243. return __devm_regmap_init(&swr->dev, &regmap_swr, &swr->dev, config,
  244. lock_key, lock_name);
  245. }
  246. EXPORT_SYMBOL(__devm_regmap_init_swr);
  247. MODULE_LICENSE("GPL v2");