adaptive_mipi_v2.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) Samsung Electronics Co., Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/sdp/adaptive_mipi_v2.h>
  11. #include <linux/sdp/adaptive_mipi_v2_cp_info.h>
  12. #include <linux/sdp/sdp_debug.h>
  13. #include <linux/notifier.h>
  14. static struct adaptive_mipi_v2_table_element *get_matched_table_element(
  15. struct band_info *band_info,
  16. struct adaptive_mipi_v2_table_element *table,
  17. int table_size)
  18. {
  19. struct adaptive_mipi_v2_table_element *element;
  20. int i;
  21. for (i = 0; i < table_size; i++) {
  22. element = &table[i];
  23. if (band_info->rat == element->rat &&
  24. band_info->band == element->band &&
  25. band_info->channel >= element->from_ch &&
  26. band_info->channel <= element->end_ch)
  27. return element;
  28. }
  29. return NULL;
  30. }
  31. static int get_optimal_osc_clock(struct cp_info *cp_msg,
  32. struct adaptive_mipi_v2_info *info)
  33. {
  34. struct adaptive_mipi_v2_table_element *element;
  35. struct band_info *band_msg;
  36. int i;
  37. if (info->osc_table_size == 0)
  38. return INV_OSC_CLK;
  39. for (i = 0; i < cp_msg->cell_count; i++) {
  40. band_msg = &cp_msg->infos[i];
  41. element = get_matched_table_element(band_msg,
  42. info->osc_table,
  43. info->osc_table_size);
  44. if (element)
  45. return info->osc_clocks_khz[ALTERNATIVE_OSC_ID];
  46. }
  47. return info->osc_clocks_khz[DEFAULT_OSC_ID];
  48. }
  49. static int get_status_weight(int connection_status)
  50. {
  51. if (connection_status == STATUS_PRIMARY_SERVING)
  52. return WEIGHT_PRIMARY_SERVING;
  53. else if (connection_status == STATUS_SECONDARY_SERVING)
  54. return WEIGHT_SECONDARY_SERVING;
  55. sdp_err(NULL, "invalid connection_status: %d\n",
  56. connection_status);
  57. return -EINVAL;
  58. }
  59. static int get_signal_weight(int sinr)
  60. {
  61. if (sinr == DEFAULT_WEAK_SIGNAL)
  62. return WEIGHT_WEAK_SIGNAL;
  63. if (sinr < JUDGE_STRONG_SIGNAL)
  64. return WEIGHT_WEAK_SIGNAL;
  65. return WEIGHT_STRONG_SIGNAL;
  66. }
  67. static int update_ratings(int connection_status, int sinr,
  68. int *ratings, int ratings_size)
  69. {
  70. int status_weight = get_status_weight(connection_status);
  71. int signal_weight = get_signal_weight(sinr);
  72. int i;
  73. if (status_weight < 0 || signal_weight < 0)
  74. return -EINVAL;
  75. for (i = 0; i < ratings_size; i++)
  76. ratings[i] *= status_weight * signal_weight;
  77. return 0;
  78. }
  79. static int get_band_id(u32 bandwidth_khz)
  80. {
  81. return bandwidth_khz <= 10000 ? BANDWIDTH_10M_IDX : BANDWIDTH_20M_IDX;
  82. }
  83. static int get_min_score_clock_id(int *total_score, int score_size)
  84. {
  85. int min_id = 0;
  86. int min_score = total_score[0];
  87. int i;
  88. for (i = 1; i < score_size; i++) {
  89. if (min_score > total_score[i]) {
  90. min_score = total_score[i];
  91. min_id = i;
  92. }
  93. }
  94. return min_id;
  95. }
  96. static int get_optimal_mipi_clock(struct cp_info *cp_msg,
  97. struct adaptive_mipi_v2_info *info)
  98. {
  99. struct adaptive_mipi_v2_table_element *element;
  100. struct band_info *band_msg;
  101. int band_id;
  102. int ratings[MAX_MIPI_FREQ_CNT] = {0, };
  103. int total_score[MAX_MIPI_FREQ_CNT] = {0, };
  104. int min_id;
  105. int i, j;
  106. int ret;
  107. sdp_info(info->ctx, "cell_count: %d\n", cp_msg->cell_count);
  108. for (i = 0; i < cp_msg->cell_count; i++) {
  109. band_msg = &cp_msg->infos[i];
  110. band_id = get_band_id(band_msg->bandwidth);
  111. element = get_matched_table_element(band_msg,
  112. info->mipi_table[band_id],
  113. info->mipi_table_size[band_id]);
  114. if (!element)
  115. continue;
  116. memcpy(ratings, element->mipi_clocks_rating, sizeof(ratings));
  117. ret = update_ratings(band_msg->connection_status, band_msg->sinr,
  118. ratings, info->mipi_clocks_size);
  119. if (ret < 0)
  120. continue;
  121. for (j = 0; j < info->mipi_clocks_size; j++)
  122. total_score[j] += ratings[j];
  123. sdp_info(info->ctx, "[%d] band_info: {%d,%d,%d,%d,%d,%d}, score: {%d,%d,%d,%d} \n",
  124. i, band_msg->rat, band_msg->band, band_msg->channel,
  125. band_msg->connection_status, band_msg->bandwidth, band_msg->sinr,
  126. total_score[0], total_score[1], total_score[2], total_score[3]);
  127. }
  128. min_id = get_min_score_clock_id(total_score, info->mipi_clocks_size);
  129. return info->mipi_clocks_kbps[min_id];
  130. }
  131. static int sdp_adaptive_mipi_v2_ril_notifier_callback(struct notifier_block *nb,
  132. unsigned long size, void *buf)
  133. {
  134. struct adaptive_mipi_v2_info *info =
  135. container_of(nb, struct adaptive_mipi_v2_info, ril_nb);
  136. struct dev_ril_bridge_msg *ril_msg = buf;
  137. struct cp_info *cp_msg;
  138. int mipi_clk_kbps;
  139. int osc_clk_khz;
  140. int ret;
  141. if (!nb || !ril_msg) {
  142. sdp_err(NULL, "null arg\n");
  143. return NOTIFY_BAD;
  144. }
  145. if (ril_msg->dev_id != IPC_SYSTEM_CP_ADAPTIVE_MIPI_INFO) {
  146. sdp_dbg(info->ctx, "unmatched sub cmd: %d\n", ril_msg->dev_id);
  147. return NOTIFY_DONE;
  148. }
  149. cp_msg = (struct cp_info *)ril_msg->data;
  150. if (ril_msg->data_len < sizeof(cp_msg->cell_count)) {
  151. return NOTIFY_BAD;
  152. }
  153. if (cp_msg->cell_count > MAX_BAND || cp_msg->cell_count <= 0) {
  154. sdp_err(info->ctx, "invalid cell_count (%d)\n", cp_msg->cell_count);
  155. return NOTIFY_BAD;
  156. }
  157. if (ril_msg->data_len < sizeof(cp_msg->cell_count) + cp_msg->cell_count * sizeof(*cp_msg->infos)) {
  158. sdp_err(info->ctx, "invalid data_len (%d) - cell_count(%d)\n", ril_msg->data_len, cp_msg->cell_count);
  159. return NOTIFY_BAD;
  160. }
  161. mipi_clk_kbps = get_optimal_mipi_clock(cp_msg, info);
  162. if (mipi_clk_kbps < 0)
  163. return NOTIFY_DONE;
  164. osc_clk_khz = get_optimal_osc_clock(cp_msg, info);
  165. sdp_info(info->ctx, "mipi_clk_kbps: %d, osc_clk_khz: %d\n", mipi_clk_kbps, osc_clk_khz);
  166. ret = info->funcs->apply_freq(mipi_clk_kbps, osc_clk_khz, info->ctx);
  167. if (ret < 0) {
  168. sdp_err(info->ctx, "fail to set freq: ret=%d\n", ret);
  169. return NOTIFY_DONE;
  170. }
  171. return NOTIFY_DONE;
  172. }
  173. static int validate_info(struct adaptive_mipi_v2_info *info)
  174. {
  175. int i;
  176. if (!info) {
  177. sdp_err(NULL, "null info\n");
  178. return -ENODEV;
  179. }
  180. if (info->mipi_clocks_size == 0 ||
  181. info->mipi_clocks_size > MAX_MIPI_FREQ_CNT) {
  182. sdp_err(info->ctx, "invalid mipi_clocks_size (%d)\n",
  183. info->mipi_clocks_size);
  184. return -EINVAL;
  185. }
  186. if (!info->funcs || !info->funcs->apply_freq) {
  187. sdp_err(info->ctx, "no callback\n");
  188. return -EINVAL;
  189. }
  190. for (i = 0; i < MAX_BANDWIDTH_IDX; i++) {
  191. if (!info->mipi_table[i]) {
  192. sdp_err(info->ctx, "no mipi table[%d]\n", i);
  193. return -EINVAL;
  194. }
  195. }
  196. return 0;
  197. }
  198. int sdp_init_adaptive_mipi_v2(struct adaptive_mipi_v2_info *info)
  199. {
  200. int ret;
  201. if (validate_info(info) < 0)
  202. return -EINVAL;
  203. info->ril_nb.notifier_call = sdp_adaptive_mipi_v2_ril_notifier_callback;
  204. ret = register_dev_ril_bridge_event_notifier(&info->ril_nb);
  205. if (ret < 0) {
  206. sdp_err(info->ctx, "failed to register ril notifier(%d)\n", ret);
  207. return ret;
  208. }
  209. sdp_info(info->ctx, "initialized sdp adaptive mipi v2\n");
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(sdp_init_adaptive_mipi_v2);
  213. int sdp_cleanup_adaptive_mipi_v2(struct adaptive_mipi_v2_info *info)
  214. {
  215. int ret;
  216. if (!info) {
  217. sdp_err(NULL, "null info\n");
  218. return -ENODEV;
  219. }
  220. ret = unregister_dev_ril_bridge_event_notifier(&info->ril_nb);
  221. if (ret < 0) {
  222. sdp_err(info->ctx, "failed to unregister ril notifier(%d)\n", ret);
  223. return ret;
  224. }
  225. sdp_info(info->ctx, "cleanup sdp adaptive mipi v2\n");
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(sdp_cleanup_adaptive_mipi_v2);
  229. MODULE_DESCRIPTION("sdp adaptive mipi v2");
  230. MODULE_LICENSE("GPL");