vivid-radio-common.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * vivid-radio-common.c - common radio rx/tx support functions.
  3. *
  4. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. #include <linux/errno.h>
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include <linux/videodev2.h>
  23. #include "vivid-core.h"
  24. #include "vivid-ctrls.h"
  25. #include "vivid-radio-common.h"
  26. #include "vivid-rds-gen.h"
  27. /*
  28. * These functions are shared between the vivid receiver and transmitter
  29. * since both use the same frequency bands.
  30. */
  31. const struct v4l2_frequency_band vivid_radio_bands[TOT_BANDS] = {
  32. /* Band FM */
  33. {
  34. .type = V4L2_TUNER_RADIO,
  35. .index = 0,
  36. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  37. V4L2_TUNER_CAP_FREQ_BANDS,
  38. .rangelow = FM_FREQ_RANGE_LOW,
  39. .rangehigh = FM_FREQ_RANGE_HIGH,
  40. .modulation = V4L2_BAND_MODULATION_FM,
  41. },
  42. /* Band AM */
  43. {
  44. .type = V4L2_TUNER_RADIO,
  45. .index = 1,
  46. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
  47. .rangelow = AM_FREQ_RANGE_LOW,
  48. .rangehigh = AM_FREQ_RANGE_HIGH,
  49. .modulation = V4L2_BAND_MODULATION_AM,
  50. },
  51. /* Band SW */
  52. {
  53. .type = V4L2_TUNER_RADIO,
  54. .index = 2,
  55. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
  56. .rangelow = SW_FREQ_RANGE_LOW,
  57. .rangehigh = SW_FREQ_RANGE_HIGH,
  58. .modulation = V4L2_BAND_MODULATION_AM,
  59. },
  60. };
  61. /*
  62. * Initialize the RDS generator. If we can loop, then the RDS generator
  63. * is set up with the values from the RDS TX controls, otherwise it
  64. * will fill in standard values using one of two alternates.
  65. */
  66. void vivid_radio_rds_init(struct vivid_dev *dev)
  67. {
  68. struct vivid_rds_gen *rds = &dev->rds_gen;
  69. bool alt = dev->radio_rx_rds_use_alternates;
  70. /* Do nothing, blocks will be filled by the transmitter */
  71. if (dev->radio_rds_loop && !dev->radio_tx_rds_controls)
  72. return;
  73. if (dev->radio_rds_loop) {
  74. v4l2_ctrl_lock(dev->radio_tx_rds_pi);
  75. rds->picode = dev->radio_tx_rds_pi->cur.val;
  76. rds->pty = dev->radio_tx_rds_pty->cur.val;
  77. rds->mono_stereo = dev->radio_tx_rds_mono_stereo->cur.val;
  78. rds->art_head = dev->radio_tx_rds_art_head->cur.val;
  79. rds->compressed = dev->radio_tx_rds_compressed->cur.val;
  80. rds->dyn_pty = dev->radio_tx_rds_dyn_pty->cur.val;
  81. rds->ta = dev->radio_tx_rds_ta->cur.val;
  82. rds->tp = dev->radio_tx_rds_tp->cur.val;
  83. rds->ms = dev->radio_tx_rds_ms->cur.val;
  84. strlcpy(rds->psname,
  85. dev->radio_tx_rds_psname->p_cur.p_char,
  86. sizeof(rds->psname));
  87. strlcpy(rds->radiotext,
  88. dev->radio_tx_rds_radiotext->p_cur.p_char + alt * 64,
  89. sizeof(rds->radiotext));
  90. v4l2_ctrl_unlock(dev->radio_tx_rds_pi);
  91. } else {
  92. vivid_rds_gen_fill(rds, dev->radio_rx_freq, alt);
  93. }
  94. if (dev->radio_rx_rds_controls) {
  95. v4l2_ctrl_s_ctrl(dev->radio_rx_rds_pty, rds->pty);
  96. v4l2_ctrl_s_ctrl(dev->radio_rx_rds_ta, rds->ta);
  97. v4l2_ctrl_s_ctrl(dev->radio_rx_rds_tp, rds->tp);
  98. v4l2_ctrl_s_ctrl(dev->radio_rx_rds_ms, rds->ms);
  99. v4l2_ctrl_s_ctrl_string(dev->radio_rx_rds_psname, rds->psname);
  100. v4l2_ctrl_s_ctrl_string(dev->radio_rx_rds_radiotext, rds->radiotext);
  101. if (!dev->radio_rds_loop)
  102. dev->radio_rx_rds_use_alternates = !dev->radio_rx_rds_use_alternates;
  103. }
  104. vivid_rds_generate(rds);
  105. }
  106. /*
  107. * Calculate the emulated signal quality taking into account the frequency
  108. * the transmitter is using.
  109. */
  110. static void vivid_radio_calc_sig_qual(struct vivid_dev *dev)
  111. {
  112. int mod = 16000;
  113. int delta = 800;
  114. int sig_qual, sig_qual_tx = mod;
  115. /*
  116. * For SW and FM there is a channel every 1000 kHz, for AM there is one
  117. * every 100 kHz.
  118. */
  119. if (dev->radio_rx_freq <= AM_FREQ_RANGE_HIGH) {
  120. mod /= 10;
  121. delta /= 10;
  122. }
  123. sig_qual = (dev->radio_rx_freq + delta) % mod - delta;
  124. if (dev->has_radio_tx)
  125. sig_qual_tx = dev->radio_rx_freq - dev->radio_tx_freq;
  126. if (abs(sig_qual_tx) <= abs(sig_qual)) {
  127. sig_qual = sig_qual_tx;
  128. /*
  129. * Zero the internal rds buffer if we are going to loop
  130. * rds blocks.
  131. */
  132. if (!dev->radio_rds_loop && !dev->radio_tx_rds_controls)
  133. memset(dev->rds_gen.data, 0,
  134. sizeof(dev->rds_gen.data));
  135. dev->radio_rds_loop = dev->radio_rx_freq >= FM_FREQ_RANGE_LOW;
  136. } else {
  137. dev->radio_rds_loop = false;
  138. }
  139. if (dev->radio_rx_freq <= AM_FREQ_RANGE_HIGH)
  140. sig_qual *= 10;
  141. dev->radio_rx_sig_qual = sig_qual;
  142. }
  143. int vivid_radio_g_frequency(struct file *file, const unsigned *pfreq, struct v4l2_frequency *vf)
  144. {
  145. if (vf->tuner != 0)
  146. return -EINVAL;
  147. vf->frequency = *pfreq;
  148. return 0;
  149. }
  150. int vivid_radio_s_frequency(struct file *file, unsigned *pfreq, const struct v4l2_frequency *vf)
  151. {
  152. struct vivid_dev *dev = video_drvdata(file);
  153. unsigned freq;
  154. unsigned band;
  155. if (vf->tuner != 0)
  156. return -EINVAL;
  157. if (vf->frequency >= (FM_FREQ_RANGE_LOW + SW_FREQ_RANGE_HIGH) / 2)
  158. band = BAND_FM;
  159. else if (vf->frequency <= (AM_FREQ_RANGE_HIGH + SW_FREQ_RANGE_LOW) / 2)
  160. band = BAND_AM;
  161. else
  162. band = BAND_SW;
  163. freq = clamp_t(u32, vf->frequency, vivid_radio_bands[band].rangelow,
  164. vivid_radio_bands[band].rangehigh);
  165. *pfreq = freq;
  166. /*
  167. * For both receiver and transmitter recalculate the signal quality
  168. * (since that depends on both frequencies) and re-init the rds
  169. * generator.
  170. */
  171. vivid_radio_calc_sig_qual(dev);
  172. vivid_radio_rds_init(dev);
  173. return 0;
  174. }