aqt1000-utils.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright (c) 2016-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/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/regmap.h>
  16. #include <linux/delay.h>
  17. #include <linux/sched.h>
  18. #include "aqt1000.h"
  19. #include "aqt1000-utils.h"
  20. #define REG_BYTES 2
  21. #define VAL_BYTES 1
  22. /*
  23. * Page Register Address that APP Proc uses to
  24. * access codec registers is identified as 0x00
  25. */
  26. #define PAGE_REG_ADDR 0x00
  27. static int aqt_page_write(struct aqt1000 *aqt, unsigned short *reg)
  28. {
  29. int ret = 0;
  30. unsigned short c_reg, reg_addr;
  31. u8 pg_num, prev_pg_num;
  32. c_reg = *reg;
  33. pg_num = c_reg >> 8;
  34. reg_addr = c_reg & 0xff;
  35. if (aqt->prev_pg_valid) {
  36. prev_pg_num = aqt->prev_pg;
  37. if (prev_pg_num != pg_num) {
  38. ret = aqt->write_dev(
  39. aqt, PAGE_REG_ADDR,
  40. (void *) &pg_num, 1);
  41. if (ret < 0)
  42. dev_err(aqt->dev,
  43. "%s: page write error, pg_num: 0x%x\n",
  44. __func__, pg_num);
  45. else {
  46. aqt->prev_pg = pg_num;
  47. dev_dbg(aqt->dev, "%s: Page 0x%x Write to 0x00\n",
  48. __func__, pg_num);
  49. }
  50. }
  51. } else {
  52. ret = aqt->write_dev(
  53. aqt, PAGE_REG_ADDR, (void *) &pg_num, 1);
  54. if (ret < 0)
  55. dev_err(aqt->dev,
  56. "%s: page write error, pg_num: 0x%x\n",
  57. __func__, pg_num);
  58. else {
  59. aqt->prev_pg = pg_num;
  60. aqt->prev_pg_valid = true;
  61. dev_dbg(aqt->dev, "%s: Page 0x%x Write to 0x00\n",
  62. __func__, pg_num);
  63. }
  64. }
  65. *reg = reg_addr;
  66. return ret;
  67. }
  68. static int regmap_bus_read(void *context, const void *reg, size_t reg_size,
  69. void *val, size_t val_size)
  70. {
  71. struct device *dev = context;
  72. struct aqt1000 *aqt = dev_get_drvdata(dev);
  73. unsigned short c_reg, rreg;
  74. int ret, i;
  75. if (!aqt) {
  76. dev_err(dev, "%s: aqt is NULL\n", __func__);
  77. return -EINVAL;
  78. }
  79. if (!reg || !val) {
  80. dev_err(dev, "%s: reg or val is NULL\n", __func__);
  81. return -EINVAL;
  82. }
  83. if (reg_size != REG_BYTES) {
  84. dev_err(dev, "%s: register size %zd bytes, not supported\n",
  85. __func__, reg_size);
  86. return -EINVAL;
  87. }
  88. mutex_lock(&aqt->io_lock);
  89. c_reg = *(u16 *)reg;
  90. rreg = c_reg;
  91. ret = aqt_page_write(aqt, &c_reg);
  92. if (ret)
  93. goto err;
  94. ret = aqt->read_dev(aqt, c_reg, val, val_size);
  95. if (ret < 0)
  96. dev_err(dev, "%s: Codec read failed (%d), reg: 0x%x, size:%zd\n",
  97. __func__, ret, rreg, val_size);
  98. else {
  99. for (i = 0; i < val_size; i++)
  100. dev_dbg(dev, "%s: Read 0x%02x from 0x%x\n",
  101. __func__, ((u8 *)val)[i], rreg + i);
  102. }
  103. err:
  104. mutex_unlock(&aqt->io_lock);
  105. return ret;
  106. }
  107. static int regmap_bus_gather_write(void *context,
  108. const void *reg, size_t reg_size,
  109. const void *val, size_t val_size)
  110. {
  111. struct device *dev = context;
  112. struct aqt1000 *aqt = dev_get_drvdata(dev);
  113. unsigned short c_reg, rreg;
  114. int ret, i;
  115. if (!aqt) {
  116. dev_err(dev, "%s: aqt is NULL\n", __func__);
  117. return -EINVAL;
  118. }
  119. if (!reg || !val) {
  120. dev_err(dev, "%s: reg or val is NULL\n", __func__);
  121. return -EINVAL;
  122. }
  123. if (reg_size != REG_BYTES) {
  124. dev_err(dev, "%s: register size %zd bytes, not supported\n",
  125. __func__, reg_size);
  126. return -EINVAL;
  127. }
  128. mutex_lock(&aqt->io_lock);
  129. c_reg = *(u16 *)reg;
  130. rreg = c_reg;
  131. ret = aqt_page_write(aqt, &c_reg);
  132. if (ret)
  133. goto err;
  134. for (i = 0; i < val_size; i++)
  135. dev_dbg(dev, "Write %02x to 0x%x\n", ((u8 *)val)[i],
  136. rreg + i);
  137. ret = aqt->write_dev(aqt, c_reg, (void *) val, val_size);
  138. if (ret < 0)
  139. dev_err(dev, "%s: Codec write failed (%d), reg:0x%x, size:%zd\n",
  140. __func__, ret, rreg, val_size);
  141. err:
  142. mutex_unlock(&aqt->io_lock);
  143. return ret;
  144. }
  145. static int regmap_bus_write(void *context, const void *data, size_t count)
  146. {
  147. struct device *dev = context;
  148. struct aqt1000 *aqt = dev_get_drvdata(dev);
  149. if (!aqt)
  150. return -EINVAL;
  151. WARN_ON(count < REG_BYTES);
  152. return regmap_bus_gather_write(context, data, REG_BYTES,
  153. data + REG_BYTES,
  154. count - REG_BYTES);
  155. }
  156. static struct regmap_bus regmap_bus_config = {
  157. .write = regmap_bus_write,
  158. .gather_write = regmap_bus_gather_write,
  159. .read = regmap_bus_read,
  160. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  161. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  162. };
  163. /*
  164. * aqt1000_regmap_init:
  165. * Initialize aqt1000 register map
  166. *
  167. * @dev: pointer to wcd device
  168. * @config: pointer to register map config
  169. *
  170. * Returns pointer to regmap structure for success
  171. * or NULL in case of failure.
  172. */
  173. struct regmap *aqt1000_regmap_init(struct device *dev,
  174. const struct regmap_config *config)
  175. {
  176. return devm_regmap_init(dev, &regmap_bus_config, dev, config);
  177. }
  178. EXPORT_SYMBOL(aqt1000_regmap_init);