regmap-swr.c 5.5 KB

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