sp_params.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <dsp/q6audio-v2.h>
  6. #include <dsp/q6afe-v2.h>
  7. #include <linux/msm_audio_calibration.h>
  8. #include <dsp/sp_params.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. /* export or show spk params at /sys/class/spk_params/cal_data */
  13. #define SPK_PARAMS "spk_params"
  14. #define CLASS_NAME "cal_data"
  15. #define BUF_SZ 20
  16. #define Q27 (1<<27)
  17. #define Q22 (1<<22)
  18. struct afe_spk_ctl {
  19. struct class *p_class;
  20. struct device *p_dev;
  21. struct afe_sp_rx_tmax_xmax_logging_param xt_logging;
  22. int32_t max_temperature_rd[SP_V2_NUM_MAX_SPKR];
  23. };
  24. struct afe_spk_ctl this_afe_spk;
  25. static ssize_t sp_count_exceeded_temperature_l_show(struct device *dev,
  26. struct device_attribute *attr,
  27. char *buf)
  28. {
  29. ssize_t ret = 0;
  30. ret = snprintf(buf, BUF_SZ, "%d\n",
  31. this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_1]);
  32. this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_1] = 0;
  33. return ret;
  34. }
  35. static DEVICE_ATTR(count_exceeded_temperature, 0644,
  36. sp_count_exceeded_temperature_l_show, NULL);
  37. static ssize_t sp_count_exceeded_temperature_r_show(struct device *dev,
  38. struct device_attribute *attr,
  39. char *buf)
  40. {
  41. ssize_t ret = 0;
  42. ret = snprintf(buf, BUF_SZ, "%d\n",
  43. this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_2]);
  44. this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_2] = 0;
  45. return ret;
  46. }
  47. static DEVICE_ATTR(count_exceeded_temperature_r, 0644,
  48. sp_count_exceeded_temperature_r_show, NULL);
  49. static ssize_t sp_count_exceeded_excursion_l_show(struct device *dev,
  50. struct device_attribute *attr,
  51. char *buf)
  52. {
  53. ssize_t ret = 0;
  54. ret = snprintf(buf, BUF_SZ, "%d\n",
  55. this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_1]);
  56. this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_1] = 0;
  57. return ret;
  58. }
  59. static DEVICE_ATTR(count_exceeded_excursion, 0644,
  60. sp_count_exceeded_excursion_l_show, NULL);
  61. static ssize_t sp_count_exceeded_excursion_r_show(struct device *dev,
  62. struct device_attribute *attr,
  63. char *buf)
  64. {
  65. ssize_t ret = 0;
  66. ret = snprintf(buf, BUF_SZ, "%d\n",
  67. this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_2]);
  68. this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_2] = 0;
  69. return ret;
  70. }
  71. static DEVICE_ATTR(count_exceeded_excursion_r, 0644,
  72. sp_count_exceeded_excursion_r_show, NULL);
  73. static ssize_t sp_max_excursion_l_show(struct device *dev,
  74. struct device_attribute *attr,
  75. char *buf)
  76. {
  77. ssize_t ret = 0;
  78. int32_t ex_val_frac;
  79. float ex_val;
  80. int32_t ex_q27 = this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_1];
  81. ex_val = (ex_q27 * 1.0)/Q27;
  82. ex_val_frac = ex_val * 100;
  83. ret = snprintf(buf, BUF_SZ, "%d.%02d\n", 0, ex_val_frac);
  84. this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_1] = 0;
  85. return ret;
  86. }
  87. static DEVICE_ATTR(max_excursion, 0644, sp_max_excursion_l_show, NULL);
  88. static ssize_t sp_max_excursion_r_show(struct device *dev,
  89. struct device_attribute *attr,
  90. char *buf)
  91. {
  92. ssize_t ret = 0;
  93. int32_t ex_val_frac;
  94. float ex_val;
  95. int32_t ex_q27 = this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_2];
  96. ex_val = (ex_q27 * 1.0)/Q27;
  97. ex_val_frac = ex_val * 100;
  98. ret = snprintf(buf, BUF_SZ, "%d.%02d\n", 0, ex_val_frac);
  99. this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_2] = 0;
  100. return ret;
  101. }
  102. static DEVICE_ATTR(max_excursion_r, 0644, sp_max_excursion_r_show, NULL);
  103. static ssize_t sp_max_temperature_l_show(struct device *dev,
  104. struct device_attribute *attr,
  105. char *buf)
  106. {
  107. ssize_t ret = 0;
  108. ret = snprintf(buf, BUF_SZ, "%d\n",
  109. this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_1]/Q22);
  110. this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_1] = 0;
  111. return ret;
  112. }
  113. static DEVICE_ATTR(max_temperature, 0644, sp_max_temperature_l_show, NULL);
  114. static ssize_t sp_max_temperature_r_show(struct device *dev,
  115. struct device_attribute *attr,
  116. char *buf)
  117. {
  118. ssize_t ret = 0;
  119. ret = snprintf(buf, BUF_SZ, "%d\n",
  120. this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_2]/Q22);
  121. this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_2] = 0;
  122. return ret;
  123. }
  124. static DEVICE_ATTR(max_temperature_r, 0644, sp_max_temperature_r_show, NULL);
  125. static ssize_t sp_max_temperature_rd_l_show(struct device *dev,
  126. struct device_attribute *attr,
  127. char *buf)
  128. {
  129. return snprintf(buf, BUF_SZ, "%d\n",
  130. this_afe_spk.max_temperature_rd[SP_V2_SPKR_1]/Q22);
  131. }
  132. static DEVICE_ATTR(max_temperature_rd, 0644,
  133. sp_max_temperature_rd_l_show, NULL);
  134. static ssize_t sp_max_temperature_rd_r_show(struct device *dev,
  135. struct device_attribute *attr,
  136. char *buf)
  137. {
  138. return snprintf(buf, BUF_SZ, "%d\n",
  139. this_afe_spk.max_temperature_rd[SP_V2_SPKR_2]/Q22);
  140. }
  141. static DEVICE_ATTR(max_temperature_rd_r, 0644,
  142. sp_max_temperature_rd_r_show, NULL);
  143. static ssize_t q6afe_initial_cal_show(struct device *dev,
  144. struct device_attribute *attr,
  145. char *buf)
  146. {
  147. return snprintf(buf, BUF_SZ, "%d\n", afe_get_spk_initial_cal());
  148. }
  149. static ssize_t q6afe_initial_cal_store(struct device *dev,
  150. struct device_attribute *attr,
  151. const char *buf, size_t size)
  152. {
  153. int initial_cal = 0;
  154. if (!kstrtou32(buf, 0, &initial_cal)) {
  155. initial_cal = initial_cal > 0 ? 1 : 0;
  156. if (initial_cal == afe_get_spk_initial_cal())
  157. dev_dbg(dev, "%s: same value already present\n",
  158. __func__);
  159. else
  160. afe_set_spk_initial_cal(initial_cal);
  161. }
  162. return size;
  163. }
  164. static DEVICE_ATTR(initial_cal, 0644,
  165. q6afe_initial_cal_show, q6afe_initial_cal_store);
  166. static ssize_t q6afe_v_vali_flag_show(struct device *dev,
  167. struct device_attribute *attr,
  168. char *buf)
  169. {
  170. return snprintf(buf, BUF_SZ, "%d\n", afe_get_spk_v_vali_flag());
  171. }
  172. static ssize_t q6afe_v_vali_flag_store(struct device *dev,
  173. struct device_attribute *attr,
  174. const char *buf, size_t size)
  175. {
  176. int v_vali_flag = 0;
  177. if (!kstrtou32(buf, 0, &v_vali_flag)) {
  178. v_vali_flag = v_vali_flag > 0 ? 1 : 0;
  179. if (v_vali_flag == afe_get_spk_v_vali_flag())
  180. dev_dbg(dev, "%s: same value already present\n",
  181. __func__);
  182. else
  183. afe_set_spk_v_vali_flag(v_vali_flag);
  184. }
  185. return size;
  186. }
  187. static DEVICE_ATTR(v_vali_flag, 0644,
  188. q6afe_v_vali_flag_show, q6afe_v_vali_flag_store);
  189. static ssize_t q6afe_spk_r0_l_show(struct device *dev,
  190. struct device_attribute *attr,
  191. char *buf)
  192. {
  193. int r0[SP_V2_NUM_MAX_SPKRS];
  194. afe_get_spk_r0(r0);
  195. return snprintf(buf, BUF_SZ, "%d\n", r0[SP_V2_SPKR_1]);
  196. }
  197. static DEVICE_ATTR(spk_r0, 0644, q6afe_spk_r0_l_show, NULL);
  198. static ssize_t q6afe_spk_t0_l_show(struct device *dev,
  199. struct device_attribute *attr,
  200. char *buf)
  201. {
  202. int t0[SP_V2_NUM_MAX_SPKRS];
  203. afe_get_spk_t0(t0);
  204. return snprintf(buf, BUF_SZ, "%d\n", t0[SP_V2_SPKR_1]);
  205. }
  206. static DEVICE_ATTR(spk_t0, 0644, q6afe_spk_t0_l_show, NULL);
  207. static ssize_t q6afe_spk_r0_r_show(struct device *dev,
  208. struct device_attribute *attr,
  209. char *buf)
  210. {
  211. int r0[SP_V2_NUM_MAX_SPKRS];
  212. afe_get_spk_r0(r0);
  213. return snprintf(buf, BUF_SZ, "%d\n", r0[SP_V2_SPKR_2]);
  214. }
  215. static DEVICE_ATTR(spk_r0_r, 0644, q6afe_spk_r0_r_show, NULL);
  216. static ssize_t q6afe_spk_t0_r_show(struct device *dev,
  217. struct device_attribute *attr,
  218. char *buf)
  219. {
  220. int t0[SP_V2_NUM_MAX_SPKRS];
  221. afe_get_spk_t0(t0);
  222. return snprintf(buf, BUF_SZ, "%d\n", t0[SP_V2_SPKR_2]);
  223. }
  224. static DEVICE_ATTR(spk_t0_r, 0644, q6afe_spk_t0_r_show, NULL);
  225. static ssize_t q6afe_spk_v_vali_l_show(struct device *dev,
  226. struct device_attribute *attr,
  227. char *buf)
  228. {
  229. int v_vali_sts[SP_V2_NUM_MAX_SPKRS];
  230. afe_get_spk_v_vali_sts(v_vali_sts);
  231. return snprintf(buf, BUF_SZ, "%d\n", v_vali_sts[SP_V2_SPKR_1]);
  232. }
  233. static DEVICE_ATTR(spk_v_vali_status, 0644, q6afe_spk_v_vali_l_show, NULL);
  234. static ssize_t q6afe_spk_v_vali_r_show(struct device *dev,
  235. struct device_attribute *attr,
  236. char *buf)
  237. {
  238. int v_vali_sts[SP_V2_NUM_MAX_SPKRS];
  239. afe_get_spk_v_vali_sts(v_vali_sts);
  240. return snprintf(buf, BUF_SZ, "%d\n", v_vali_sts[SP_V2_SPKR_2]);
  241. }
  242. static DEVICE_ATTR(spk_v_vali_r_status, 0644, q6afe_spk_v_vali_r_show, NULL);
  243. static struct attribute *afe_spk_cal_attr[] = {
  244. &dev_attr_max_excursion.attr,
  245. &dev_attr_max_excursion_r.attr,
  246. &dev_attr_max_temperature.attr,
  247. &dev_attr_max_temperature_r.attr,
  248. &dev_attr_count_exceeded_excursion.attr,
  249. &dev_attr_count_exceeded_excursion_r.attr,
  250. &dev_attr_count_exceeded_temperature.attr,
  251. &dev_attr_count_exceeded_temperature_r.attr,
  252. &dev_attr_max_temperature_rd.attr,
  253. &dev_attr_max_temperature_rd_r.attr,
  254. &dev_attr_initial_cal.attr,
  255. &dev_attr_spk_r0.attr,
  256. &dev_attr_spk_t0.attr,
  257. &dev_attr_spk_r0_r.attr,
  258. &dev_attr_spk_t0_r.attr,
  259. &dev_attr_v_vali_flag.attr,
  260. &dev_attr_spk_v_vali_status.attr,
  261. &dev_attr_spk_v_vali_r_status.attr,
  262. NULL,
  263. };
  264. static struct attribute_group afe_spk_cal_attr_grp = {
  265. .attrs = afe_spk_cal_attr,
  266. };
  267. /**
  268. * afe_get_sp_xt_logging_data -
  269. * to get excursion logging data from DSP
  270. *
  271. * @port: AFE port ID
  272. *
  273. * Returns 0 on success or error on failure
  274. */
  275. int afe_get_sp_xt_logging_data(u16 port_id)
  276. {
  277. int ret = 0;
  278. struct afe_sp_rx_tmax_xmax_logging_param xt_logging_data;
  279. ret = afe_get_sp_rx_tmax_xmax_logging_data(&xt_logging_data, port_id);
  280. if (ret) {
  281. pr_err("%s Excursion logging fail\n", __func__);
  282. return ret;
  283. }
  284. /* storing max sp param value */
  285. if (this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_1] <
  286. xt_logging_data.max_temperature[SP_V2_SPKR_1])
  287. this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_1] =
  288. xt_logging_data.max_temperature[SP_V2_SPKR_1];
  289. if (this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_2] <
  290. xt_logging_data.max_temperature[SP_V2_SPKR_2])
  291. this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_2] =
  292. xt_logging_data.max_temperature[SP_V2_SPKR_2];
  293. /* update temp for max_temperature_rd node */
  294. if (this_afe_spk.max_temperature_rd[SP_V2_SPKR_1] <
  295. xt_logging_data.max_temperature[SP_V2_SPKR_1])
  296. this_afe_spk.max_temperature_rd[SP_V2_SPKR_1] =
  297. xt_logging_data.max_temperature[SP_V2_SPKR_1];
  298. if (this_afe_spk.max_temperature_rd[SP_V2_SPKR_2] <
  299. xt_logging_data.max_temperature[SP_V2_SPKR_2])
  300. this_afe_spk.max_temperature_rd[SP_V2_SPKR_2] =
  301. xt_logging_data.max_temperature[SP_V2_SPKR_2];
  302. if (this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_1] <
  303. xt_logging_data.max_excursion[SP_V2_SPKR_1])
  304. this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_1] =
  305. xt_logging_data.max_excursion[SP_V2_SPKR_1];
  306. if (this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_2] <
  307. xt_logging_data.max_excursion[SP_V2_SPKR_2])
  308. this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_2] =
  309. xt_logging_data.max_excursion[SP_V2_SPKR_2];
  310. if (this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_1] <
  311. xt_logging_data.count_exceeded_temperature[SP_V2_SPKR_1])
  312. this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_1]
  313. += xt_logging_data.count_exceeded_temperature[SP_V2_SPKR_1];
  314. if (this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_2] <
  315. xt_logging_data.count_exceeded_temperature[SP_V2_SPKR_2])
  316. this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_2]
  317. += xt_logging_data.count_exceeded_temperature[SP_V2_SPKR_2];
  318. if (this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_1] <
  319. xt_logging_data.count_exceeded_excursion[SP_V2_SPKR_1])
  320. this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_1]
  321. += xt_logging_data.count_exceeded_excursion[SP_V2_SPKR_1];
  322. if (this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_2] <
  323. xt_logging_data.count_exceeded_excursion[SP_V2_SPKR_2])
  324. this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_2]
  325. += xt_logging_data.count_exceeded_excursion[SP_V2_SPKR_2];
  326. return ret;
  327. }
  328. EXPORT_SYMBOL(afe_get_sp_xt_logging_data);
  329. int __init spk_params_init(void)
  330. {
  331. /* initialize xt param value with 0 */
  332. this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_1] = 0;
  333. this_afe_spk.xt_logging.max_temperature[SP_V2_SPKR_2] = 0;
  334. this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_1] = 0;
  335. this_afe_spk.xt_logging.max_excursion[SP_V2_SPKR_2] = 0;
  336. this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_1] = 0;
  337. this_afe_spk.xt_logging.count_exceeded_temperature[SP_V2_SPKR_2] = 0;
  338. this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_1] = 0;
  339. this_afe_spk.xt_logging.count_exceeded_excursion[SP_V2_SPKR_2] = 0;
  340. this_afe_spk.p_class = class_create(THIS_MODULE, SPK_PARAMS);
  341. if (this_afe_spk.p_class) {
  342. this_afe_spk.p_dev = device_create(this_afe_spk.p_class, NULL,
  343. 1, NULL, CLASS_NAME);
  344. if (!IS_ERR(this_afe_spk.p_dev)) {
  345. if (sysfs_create_group(&this_afe_spk.p_dev->kobj,
  346. &afe_spk_cal_attr_grp))
  347. pr_err("%s: Failed to create sysfs group\n",
  348. __func__);
  349. }
  350. }
  351. return 0;
  352. }
  353. void spk_params_exit(void)
  354. {
  355. pr_debug("%s\n", __func__);
  356. }