fmdrv_tx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * FM Driver for Connectivity chip of Texas Instruments.
  3. * This sub-module of FM driver implements FM TX functionality.
  4. *
  5. * Copyright (C) 2011 Texas Instruments
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/delay.h>
  18. #include "fmdrv.h"
  19. #include "fmdrv_common.h"
  20. #include "fmdrv_tx.h"
  21. int fm_tx_set_stereo_mono(struct fmdev *fmdev, u16 mode)
  22. {
  23. u16 payload;
  24. int ret;
  25. if (fmdev->tx_data.aud_mode == mode)
  26. return 0;
  27. fmdbg("stereo mode: %d\n", mode);
  28. /* Set Stereo/Mono mode */
  29. payload = (1 - mode);
  30. ret = fmc_send_cmd(fmdev, MONO_SET, REG_WR, &payload,
  31. sizeof(payload), NULL, NULL);
  32. if (ret < 0)
  33. return ret;
  34. fmdev->tx_data.aud_mode = mode;
  35. return ret;
  36. }
  37. static int set_rds_text(struct fmdev *fmdev, u8 *rds_text)
  38. {
  39. u16 payload;
  40. int ret;
  41. ret = fmc_send_cmd(fmdev, RDS_DATA_SET, REG_WR, rds_text,
  42. strlen(rds_text), NULL, NULL);
  43. if (ret < 0)
  44. return ret;
  45. /* Scroll mode */
  46. payload = (u16)0x1;
  47. ret = fmc_send_cmd(fmdev, DISPLAY_MODE, REG_WR, &payload,
  48. sizeof(payload), NULL, NULL);
  49. if (ret < 0)
  50. return ret;
  51. return 0;
  52. }
  53. static int set_rds_data_mode(struct fmdev *fmdev, u8 mode)
  54. {
  55. u16 payload;
  56. int ret;
  57. /* Setting unique PI TODO: how unique? */
  58. payload = (u16)0xcafe;
  59. ret = fmc_send_cmd(fmdev, PI_SET, REG_WR, &payload,
  60. sizeof(payload), NULL, NULL);
  61. if (ret < 0)
  62. return ret;
  63. /* Set decoder id */
  64. payload = (u16)0xa;
  65. ret = fmc_send_cmd(fmdev, DI_SET, REG_WR, &payload,
  66. sizeof(payload), NULL, NULL);
  67. if (ret < 0)
  68. return ret;
  69. /* TODO: RDS_MODE_GET? */
  70. return 0;
  71. }
  72. static int set_rds_len(struct fmdev *fmdev, u8 type, u16 len)
  73. {
  74. u16 payload;
  75. int ret;
  76. len |= type << 8;
  77. payload = len;
  78. ret = fmc_send_cmd(fmdev, RDS_CONFIG_DATA_SET, REG_WR, &payload,
  79. sizeof(payload), NULL, NULL);
  80. if (ret < 0)
  81. return ret;
  82. /* TODO: LENGTH_GET? */
  83. return 0;
  84. }
  85. int fm_tx_set_rds_mode(struct fmdev *fmdev, u8 rds_en_dis)
  86. {
  87. u16 payload;
  88. int ret;
  89. u8 rds_text[] = "Zoom2\n";
  90. fmdbg("rds_en_dis:%d(E:%d, D:%d)\n", rds_en_dis,
  91. FM_RDS_ENABLE, FM_RDS_DISABLE);
  92. if (rds_en_dis == FM_RDS_ENABLE) {
  93. /* Set RDS length */
  94. set_rds_len(fmdev, 0, strlen(rds_text));
  95. /* Set RDS text */
  96. set_rds_text(fmdev, rds_text);
  97. /* Set RDS mode */
  98. set_rds_data_mode(fmdev, 0x0);
  99. }
  100. /* Send command to enable RDS */
  101. if (rds_en_dis == FM_RDS_ENABLE)
  102. payload = 0x01;
  103. else
  104. payload = 0x00;
  105. ret = fmc_send_cmd(fmdev, RDS_DATA_ENB, REG_WR, &payload,
  106. sizeof(payload), NULL, NULL);
  107. if (ret < 0)
  108. return ret;
  109. if (rds_en_dis == FM_RDS_ENABLE) {
  110. /* Set RDS length */
  111. set_rds_len(fmdev, 0, strlen(rds_text));
  112. /* Set RDS text */
  113. set_rds_text(fmdev, rds_text);
  114. }
  115. fmdev->tx_data.rds.flag = rds_en_dis;
  116. return 0;
  117. }
  118. int fm_tx_set_radio_text(struct fmdev *fmdev, u8 *rds_text, u8 rds_type)
  119. {
  120. u16 payload;
  121. int ret;
  122. if (fmdev->curr_fmmode != FM_MODE_TX)
  123. return -EPERM;
  124. fm_tx_set_rds_mode(fmdev, 0);
  125. /* Set RDS length */
  126. set_rds_len(fmdev, rds_type, strlen(rds_text));
  127. /* Set RDS text */
  128. set_rds_text(fmdev, rds_text);
  129. /* Set RDS mode */
  130. set_rds_data_mode(fmdev, 0x0);
  131. payload = 1;
  132. ret = fmc_send_cmd(fmdev, RDS_DATA_ENB, REG_WR, &payload,
  133. sizeof(payload), NULL, NULL);
  134. if (ret < 0)
  135. return ret;
  136. return 0;
  137. }
  138. int fm_tx_set_af(struct fmdev *fmdev, u32 af)
  139. {
  140. u16 payload;
  141. int ret;
  142. if (fmdev->curr_fmmode != FM_MODE_TX)
  143. return -EPERM;
  144. fmdbg("AF: %d\n", af);
  145. af = (af - 87500) / 100;
  146. payload = (u16)af;
  147. ret = fmc_send_cmd(fmdev, TA_SET, REG_WR, &payload,
  148. sizeof(payload), NULL, NULL);
  149. if (ret < 0)
  150. return ret;
  151. return 0;
  152. }
  153. int fm_tx_set_region(struct fmdev *fmdev, u8 region)
  154. {
  155. u16 payload;
  156. int ret;
  157. if (region != FM_BAND_EUROPE_US && region != FM_BAND_JAPAN) {
  158. fmerr("Invalid band\n");
  159. return -EINVAL;
  160. }
  161. /* Send command to set the band */
  162. payload = (u16)region;
  163. ret = fmc_send_cmd(fmdev, TX_BAND_SET, REG_WR, &payload,
  164. sizeof(payload), NULL, NULL);
  165. if (ret < 0)
  166. return ret;
  167. return 0;
  168. }
  169. int fm_tx_set_mute_mode(struct fmdev *fmdev, u8 mute_mode_toset)
  170. {
  171. u16 payload;
  172. int ret;
  173. fmdbg("tx: mute mode %d\n", mute_mode_toset);
  174. payload = mute_mode_toset;
  175. ret = fmc_send_cmd(fmdev, MUTE, REG_WR, &payload,
  176. sizeof(payload), NULL, NULL);
  177. if (ret < 0)
  178. return ret;
  179. return 0;
  180. }
  181. /* Set TX Audio I/O */
  182. static int set_audio_io(struct fmdev *fmdev)
  183. {
  184. struct fmtx_data *tx = &fmdev->tx_data;
  185. u16 payload;
  186. int ret;
  187. /* Set Audio I/O Enable */
  188. payload = tx->audio_io;
  189. ret = fmc_send_cmd(fmdev, AUDIO_IO_SET, REG_WR, &payload,
  190. sizeof(payload), NULL, NULL);
  191. if (ret < 0)
  192. return ret;
  193. /* TODO: is audio set? */
  194. return 0;
  195. }
  196. /* Start TX Transmission */
  197. static int enable_xmit(struct fmdev *fmdev, u8 new_xmit_state)
  198. {
  199. struct fmtx_data *tx = &fmdev->tx_data;
  200. unsigned long timeleft;
  201. u16 payload;
  202. int ret;
  203. /* Enable POWER_ENB interrupts */
  204. payload = FM_POW_ENB_EVENT;
  205. ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
  206. sizeof(payload), NULL, NULL);
  207. if (ret < 0)
  208. return ret;
  209. /* Set Power Enable */
  210. payload = new_xmit_state;
  211. ret = fmc_send_cmd(fmdev, POWER_ENB_SET, REG_WR, &payload,
  212. sizeof(payload), NULL, NULL);
  213. if (ret < 0)
  214. return ret;
  215. /* Wait for Power Enabled */
  216. init_completion(&fmdev->maintask_comp);
  217. timeleft = wait_for_completion_timeout(&fmdev->maintask_comp,
  218. FM_DRV_TX_TIMEOUT);
  219. if (!timeleft) {
  220. fmerr("Timeout(%d sec),didn't get tune ended interrupt\n",
  221. jiffies_to_msecs(FM_DRV_TX_TIMEOUT) / 1000);
  222. return -ETIMEDOUT;
  223. }
  224. set_bit(FM_CORE_TX_XMITING, &fmdev->flag);
  225. tx->xmit_state = new_xmit_state;
  226. return 0;
  227. }
  228. /* Set TX power level */
  229. int fm_tx_set_pwr_lvl(struct fmdev *fmdev, u8 new_pwr_lvl)
  230. {
  231. u16 payload;
  232. struct fmtx_data *tx = &fmdev->tx_data;
  233. int ret;
  234. if (fmdev->curr_fmmode != FM_MODE_TX)
  235. return -EPERM;
  236. fmdbg("tx: pwr_level_to_set %ld\n", (long int)new_pwr_lvl);
  237. /* If the core isn't ready update global variable */
  238. if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
  239. tx->pwr_lvl = new_pwr_lvl;
  240. return 0;
  241. }
  242. /* Set power level: Application will specify power level value in
  243. * units of dB/uV, whereas range and step are specific to FM chip.
  244. * For TI's WL chips, convert application specified power level value
  245. * to chip specific value by subtracting 122 from it. Refer to TI FM
  246. * data sheet for details.
  247. * */
  248. payload = (FM_PWR_LVL_HIGH - new_pwr_lvl);
  249. ret = fmc_send_cmd(fmdev, POWER_LEV_SET, REG_WR, &payload,
  250. sizeof(payload), NULL, NULL);
  251. if (ret < 0)
  252. return ret;
  253. /* TODO: is the power level set? */
  254. tx->pwr_lvl = new_pwr_lvl;
  255. return 0;
  256. }
  257. /*
  258. * Sets FM TX pre-emphasis filter value (OFF, 50us, or 75us)
  259. * Convert V4L2 specified filter values to chip specific filter values.
  260. */
  261. int fm_tx_set_preemph_filter(struct fmdev *fmdev, u32 preemphasis)
  262. {
  263. struct fmtx_data *tx = &fmdev->tx_data;
  264. u16 payload;
  265. int ret;
  266. if (fmdev->curr_fmmode != FM_MODE_TX)
  267. return -EPERM;
  268. switch (preemphasis) {
  269. case V4L2_PREEMPHASIS_DISABLED:
  270. payload = FM_TX_PREEMPH_OFF;
  271. break;
  272. case V4L2_PREEMPHASIS_50_uS:
  273. payload = FM_TX_PREEMPH_50US;
  274. break;
  275. case V4L2_PREEMPHASIS_75_uS:
  276. payload = FM_TX_PREEMPH_75US;
  277. break;
  278. }
  279. ret = fmc_send_cmd(fmdev, PREMPH_SET, REG_WR, &payload,
  280. sizeof(payload), NULL, NULL);
  281. if (ret < 0)
  282. return ret;
  283. tx->preemph = payload;
  284. return ret;
  285. }
  286. /* Get the TX tuning capacitor value.*/
  287. int fm_tx_get_tune_cap_val(struct fmdev *fmdev)
  288. {
  289. u16 curr_val;
  290. u32 resp_len;
  291. int ret;
  292. if (fmdev->curr_fmmode != FM_MODE_TX)
  293. return -EPERM;
  294. ret = fmc_send_cmd(fmdev, READ_FMANT_TUNE_VALUE, REG_RD,
  295. NULL, sizeof(curr_val), &curr_val, &resp_len);
  296. if (ret < 0)
  297. return ret;
  298. curr_val = be16_to_cpu((__force __be16)curr_val);
  299. return curr_val;
  300. }
  301. /* Set TX Frequency */
  302. int fm_tx_set_freq(struct fmdev *fmdev, u32 freq_to_set)
  303. {
  304. struct fmtx_data *tx = &fmdev->tx_data;
  305. u16 payload, chanl_index;
  306. int ret;
  307. if (test_bit(FM_CORE_TX_XMITING, &fmdev->flag)) {
  308. enable_xmit(fmdev, 0);
  309. clear_bit(FM_CORE_TX_XMITING, &fmdev->flag);
  310. }
  311. /* Enable FR, BL interrupts */
  312. payload = (FM_FR_EVENT | FM_BL_EVENT);
  313. ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
  314. sizeof(payload), NULL, NULL);
  315. if (ret < 0)
  316. return ret;
  317. tx->tx_frq = (unsigned long)freq_to_set;
  318. fmdbg("tx: freq_to_set %ld\n", (long int)tx->tx_frq);
  319. chanl_index = freq_to_set / 10;
  320. /* Set current tuner channel */
  321. payload = chanl_index;
  322. ret = fmc_send_cmd(fmdev, CHANL_SET, REG_WR, &payload,
  323. sizeof(payload), NULL, NULL);
  324. if (ret < 0)
  325. return ret;
  326. fm_tx_set_pwr_lvl(fmdev, tx->pwr_lvl);
  327. fm_tx_set_preemph_filter(fmdev, tx->preemph);
  328. tx->audio_io = 0x01; /* I2S */
  329. set_audio_io(fmdev);
  330. enable_xmit(fmdev, 0x01); /* Enable transmission */
  331. tx->aud_mode = FM_STEREO_MODE;
  332. tx->rds.flag = FM_RDS_DISABLE;
  333. return 0;
  334. }