PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/media/radio/radio-wl1273.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 2177 lines | 1636 code | 428 blank | 113 comment | 397 complexity | 4026bb3910fa59395bc4d966f0cbeb2b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Driver for the Texas Instruments WL1273 FM radio.
  3. *
  4. * Copyright (C) 2011 Nokia Corporation
  5. * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as 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. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/firmware.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mfd/wl1273-core.h>
  24. #include <linux/slab.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-ctrls.h>
  27. #include <media/v4l2-device.h>
  28. #include <media/v4l2-ioctl.h>
  29. #define DRIVER_DESC "Wl1273 FM Radio"
  30. #define WL1273_POWER_SET_OFF 0
  31. #define WL1273_POWER_SET_FM BIT(0)
  32. #define WL1273_POWER_SET_RDS BIT(1)
  33. #define WL1273_POWER_SET_RETENTION BIT(4)
  34. #define WL1273_PUPD_SET_OFF 0x00
  35. #define WL1273_PUPD_SET_ON 0x01
  36. #define WL1273_PUPD_SET_RETENTION 0x10
  37. #define WL1273_FREQ(x) (x * 10000 / 625)
  38. #define WL1273_INV_FREQ(x) (x * 625 / 10000)
  39. /*
  40. * static int radio_nr - The number of the radio device
  41. *
  42. * The default is 0.
  43. */
  44. static int radio_nr;
  45. module_param(radio_nr, int, 0);
  46. MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");
  47. struct wl1273_device {
  48. char *bus_type;
  49. u8 forbidden;
  50. unsigned int preemphasis;
  51. unsigned int spacing;
  52. unsigned int tx_power;
  53. unsigned int rx_frequency;
  54. unsigned int tx_frequency;
  55. unsigned int rangelow;
  56. unsigned int rangehigh;
  57. unsigned int band;
  58. bool stereo;
  59. /* RDS */
  60. unsigned int rds_on;
  61. wait_queue_head_t read_queue;
  62. struct mutex lock; /* for serializing fm radio operations */
  63. struct completion busy;
  64. unsigned char *buffer;
  65. unsigned int buf_size;
  66. unsigned int rd_index;
  67. unsigned int wr_index;
  68. /* Selected interrupts */
  69. u16 irq_flags;
  70. u16 irq_received;
  71. struct v4l2_ctrl_handler ctrl_handler;
  72. struct v4l2_device v4l2dev;
  73. struct video_device videodev;
  74. struct device *dev;
  75. struct wl1273_core *core;
  76. struct file *owner;
  77. char *write_buf;
  78. unsigned int rds_users;
  79. };
  80. #define WL1273_IRQ_MASK (WL1273_FR_EVENT | \
  81. WL1273_POW_ENB_EVENT)
  82. /*
  83. * static unsigned int rds_buf - the number of RDS buffer blocks used.
  84. *
  85. * The default number is 100.
  86. */
  87. static unsigned int rds_buf = 100;
  88. module_param(rds_buf, uint, 0);
  89. MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");
  90. static int wl1273_fm_write_fw(struct wl1273_core *core,
  91. __u8 *fw, int len)
  92. {
  93. struct i2c_client *client = core->client;
  94. struct i2c_msg msg;
  95. int i, r = 0;
  96. msg.addr = client->addr;
  97. msg.flags = 0;
  98. for (i = 0; i <= len; i++) {
  99. msg.len = fw[0];
  100. msg.buf = fw + 1;
  101. fw += msg.len + 1;
  102. dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
  103. r = i2c_transfer(client->adapter, &msg, 1);
  104. if (r < 0 && i < len + 1)
  105. break;
  106. }
  107. dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
  108. dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
  109. /* Last transfer always fails. */
  110. if (i == len || r == 1)
  111. r = 0;
  112. return r;
  113. }
  114. #define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status)
  115. #define WL1273_RDS_CORRECTABLE_ERROR (1 << 3)
  116. #define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4)
  117. static int wl1273_fm_rds(struct wl1273_device *radio)
  118. {
  119. struct wl1273_core *core = radio->core;
  120. struct i2c_client *client = core->client;
  121. u16 val;
  122. u8 b0 = WL1273_RDS_DATA_GET, status;
  123. struct v4l2_rds_data rds = { 0, 0, 0 };
  124. struct i2c_msg msg[] = {
  125. {
  126. .addr = client->addr,
  127. .flags = 0,
  128. .buf = &b0,
  129. .len = 1,
  130. },
  131. {
  132. .addr = client->addr,
  133. .flags = I2C_M_RD,
  134. .buf = (u8 *) &rds,
  135. .len = sizeof(rds),
  136. }
  137. };
  138. int r;
  139. if (core->mode != WL1273_MODE_RX)
  140. return 0;
  141. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  142. if (r)
  143. return r;
  144. if ((val & 0x01) == 0) {
  145. /* RDS decoder not synchronized */
  146. return -EAGAIN;
  147. }
  148. /* copy all four RDS blocks to internal buffer */
  149. do {
  150. r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  151. if (r != ARRAY_SIZE(msg)) {
  152. dev_err(radio->dev, WL1273_FM_DRIVER_NAME
  153. ": %s: read_rds error r == %i)\n",
  154. __func__, r);
  155. }
  156. status = rds.block;
  157. if (!WL1273_FIFO_HAS_DATA(status))
  158. break;
  159. /* copy bits 0-2 (the block ID) to bits 3-5 */
  160. rds.block = V4L2_RDS_BLOCK_MSK & status;
  161. rds.block |= rds.block << 3;
  162. /* copy the error bits to standard positions */
  163. if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
  164. rds.block |= V4L2_RDS_BLOCK_ERROR;
  165. rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
  166. } else if (WL1273_RDS_CORRECTABLE_ERROR & status) {
  167. rds.block &= ~V4L2_RDS_BLOCK_ERROR;
  168. rds.block |= V4L2_RDS_BLOCK_CORRECTED;
  169. }
  170. /* copy RDS block to internal buffer */
  171. memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
  172. radio->wr_index += 3;
  173. /* wrap write pointer */
  174. if (radio->wr_index >= radio->buf_size)
  175. radio->wr_index = 0;
  176. /* check for overflow & start over */
  177. if (radio->wr_index == radio->rd_index) {
  178. dev_dbg(radio->dev, "RDS OVERFLOW");
  179. radio->rd_index = 0;
  180. radio->wr_index = 0;
  181. break;
  182. }
  183. } while (WL1273_FIFO_HAS_DATA(status));
  184. /* wake up read queue */
  185. if (radio->wr_index != radio->rd_index)
  186. wake_up_interruptible(&radio->read_queue);
  187. return 0;
  188. }
  189. static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
  190. {
  191. struct wl1273_device *radio = dev_id;
  192. struct wl1273_core *core = radio->core;
  193. u16 flags;
  194. int r;
  195. r = core->read(core, WL1273_FLAG_GET, &flags);
  196. if (r)
  197. goto out;
  198. if (flags & WL1273_BL_EVENT) {
  199. radio->irq_received = flags;
  200. dev_dbg(radio->dev, "IRQ: BL\n");
  201. }
  202. if (flags & WL1273_RDS_EVENT) {
  203. msleep(200);
  204. wl1273_fm_rds(radio);
  205. }
  206. if (flags & WL1273_BBLK_EVENT)
  207. dev_dbg(radio->dev, "IRQ: BBLK\n");
  208. if (flags & WL1273_LSYNC_EVENT)
  209. dev_dbg(radio->dev, "IRQ: LSYNC\n");
  210. if (flags & WL1273_LEV_EVENT) {
  211. u16 level;
  212. r = core->read(core, WL1273_RSSI_LVL_GET, &level);
  213. if (r)
  214. goto out;
  215. if (level > 14)
  216. dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
  217. }
  218. if (flags & WL1273_IFFR_EVENT)
  219. dev_dbg(radio->dev, "IRQ: IFFR\n");
  220. if (flags & WL1273_PI_EVENT)
  221. dev_dbg(radio->dev, "IRQ: PI\n");
  222. if (flags & WL1273_PD_EVENT)
  223. dev_dbg(radio->dev, "IRQ: PD\n");
  224. if (flags & WL1273_STIC_EVENT)
  225. dev_dbg(radio->dev, "IRQ: STIC\n");
  226. if (flags & WL1273_MAL_EVENT)
  227. dev_dbg(radio->dev, "IRQ: MAL\n");
  228. if (flags & WL1273_POW_ENB_EVENT) {
  229. complete(&radio->busy);
  230. dev_dbg(radio->dev, "NOT BUSY\n");
  231. dev_dbg(radio->dev, "IRQ: POW_ENB\n");
  232. }
  233. if (flags & WL1273_SCAN_OVER_EVENT)
  234. dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
  235. if (flags & WL1273_ERROR_EVENT)
  236. dev_dbg(radio->dev, "IRQ: ERROR\n");
  237. if (flags & WL1273_FR_EVENT) {
  238. u16 freq;
  239. dev_dbg(radio->dev, "IRQ: FR:\n");
  240. if (core->mode == WL1273_MODE_RX) {
  241. r = core->write(core, WL1273_TUNER_MODE_SET,
  242. TUNER_MODE_STOP_SEARCH);
  243. if (r) {
  244. dev_err(radio->dev,
  245. "%s: TUNER_MODE_SET fails: %d\n",
  246. __func__, r);
  247. goto out;
  248. }
  249. r = core->read(core, WL1273_FREQ_SET, &freq);
  250. if (r)
  251. goto out;
  252. if (radio->band == WL1273_BAND_JAPAN)
  253. radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
  254. freq * 50;
  255. else
  256. radio->rx_frequency = WL1273_BAND_OTHER_LOW +
  257. freq * 50;
  258. /*
  259. * The driver works better with this msleep,
  260. * the documentation doesn't mention it.
  261. */
  262. usleep_range(10000, 15000);
  263. dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
  264. } else {
  265. r = core->read(core, WL1273_CHANL_SET, &freq);
  266. if (r)
  267. goto out;
  268. dev_dbg(radio->dev, "%dkHz\n", freq);
  269. }
  270. dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
  271. }
  272. out:
  273. core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
  274. complete(&radio->busy);
  275. return IRQ_HANDLED;
  276. }
  277. static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
  278. {
  279. struct wl1273_core *core = radio->core;
  280. int r = 0;
  281. if (freq < WL1273_BAND_TX_LOW) {
  282. dev_err(radio->dev,
  283. "Frequency out of range: %d < %d\n", freq,
  284. WL1273_BAND_TX_LOW);
  285. return -ERANGE;
  286. }
  287. if (freq > WL1273_BAND_TX_HIGH) {
  288. dev_err(radio->dev,
  289. "Frequency out of range: %d > %d\n", freq,
  290. WL1273_BAND_TX_HIGH);
  291. return -ERANGE;
  292. }
  293. /*
  294. * The driver works better with this sleep,
  295. * the documentation doesn't mention it.
  296. */
  297. usleep_range(5000, 10000);
  298. dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
  299. /* Set the current tx channel */
  300. r = core->write(core, WL1273_CHANL_SET, freq / 10);
  301. if (r)
  302. return r;
  303. INIT_COMPLETION(radio->busy);
  304. /* wait for the FR IRQ */
  305. r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
  306. if (!r)
  307. return -ETIMEDOUT;
  308. dev_dbg(radio->dev, "WL1273_CHANL_SET: %d\n", r);
  309. /* Enable the output power */
  310. r = core->write(core, WL1273_POWER_ENB_SET, 1);
  311. if (r)
  312. return r;
  313. INIT_COMPLETION(radio->busy);
  314. /* wait for the POWER_ENB IRQ */
  315. r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
  316. if (!r)
  317. return -ETIMEDOUT;
  318. radio->tx_frequency = freq;
  319. dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %d\n", r);
  320. return 0;
  321. }
  322. static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
  323. {
  324. struct wl1273_core *core = radio->core;
  325. int r, f;
  326. if (freq < radio->rangelow) {
  327. dev_err(radio->dev,
  328. "Frequency out of range: %d < %d\n", freq,
  329. radio->rangelow);
  330. r = -ERANGE;
  331. goto err;
  332. }
  333. if (freq > radio->rangehigh) {
  334. dev_err(radio->dev,
  335. "Frequency out of range: %d > %d\n", freq,
  336. radio->rangehigh);
  337. r = -ERANGE;
  338. goto err;
  339. }
  340. dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
  341. core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
  342. if (radio->band == WL1273_BAND_JAPAN)
  343. f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
  344. else
  345. f = (freq - WL1273_BAND_OTHER_LOW) / 50;
  346. r = core->write(core, WL1273_FREQ_SET, f);
  347. if (r) {
  348. dev_err(radio->dev, "FREQ_SET fails\n");
  349. goto err;
  350. }
  351. r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
  352. if (r) {
  353. dev_err(radio->dev, "TUNER_MODE_SET fails\n");
  354. goto err;
  355. }
  356. INIT_COMPLETION(radio->busy);
  357. r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
  358. if (!r) {
  359. dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
  360. return -ETIMEDOUT;
  361. }
  362. radio->rd_index = 0;
  363. radio->wr_index = 0;
  364. radio->rx_frequency = freq;
  365. return 0;
  366. err:
  367. return r;
  368. }
  369. static int wl1273_fm_get_freq(struct wl1273_device *radio)
  370. {
  371. struct wl1273_core *core = radio->core;
  372. unsigned int freq;
  373. u16 f;
  374. int r;
  375. if (core->mode == WL1273_MODE_RX) {
  376. r = core->read(core, WL1273_FREQ_SET, &f);
  377. if (r)
  378. return r;
  379. dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
  380. if (radio->band == WL1273_BAND_JAPAN)
  381. freq = WL1273_BAND_JAPAN_LOW + 50 * f;
  382. else
  383. freq = WL1273_BAND_OTHER_LOW + 50 * f;
  384. } else {
  385. r = core->read(core, WL1273_CHANL_SET, &f);
  386. if (r)
  387. return r;
  388. freq = f * 10;
  389. }
  390. return freq;
  391. }
  392. /**
  393. * wl1273_fm_upload_firmware_patch() - Upload the firmware.
  394. * @radio: A pointer to the device struct.
  395. *
  396. * The firmware file consists of arrays of bytes where the first byte
  397. * gives the array length. The first byte in the file gives the
  398. * number of these arrays.
  399. */
  400. static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
  401. {
  402. struct wl1273_core *core = radio->core;
  403. unsigned int packet_num;
  404. const struct firmware *fw_p;
  405. const char *fw_name = "radio-wl1273-fw.bin";
  406. struct device *dev = radio->dev;
  407. __u8 *ptr;
  408. int r;
  409. dev_dbg(dev, "%s:\n", __func__);
  410. /*
  411. * Uploading the firmware patch is not always necessary,
  412. * so we only print an info message.
  413. */
  414. if (request_firmware(&fw_p, fw_name, dev)) {
  415. dev_info(dev, "%s - %s not found\n", __func__, fw_name);
  416. return 0;
  417. }
  418. ptr = (__u8 *) fw_p->data;
  419. packet_num = ptr[0];
  420. dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
  421. r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
  422. if (r) {
  423. dev_err(dev, "FW upload error: %d\n", r);
  424. goto out;
  425. }
  426. /* ignore possible error here */
  427. core->write(core, WL1273_RESET, 0);
  428. dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
  429. out:
  430. release_firmware(fw_p);
  431. return r;
  432. }
  433. static int wl1273_fm_stop(struct wl1273_device *radio)
  434. {
  435. struct wl1273_core *core = radio->core;
  436. if (core->mode == WL1273_MODE_RX) {
  437. int r = core->write(core, WL1273_POWER_SET,
  438. WL1273_POWER_SET_OFF);
  439. if (r)
  440. dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
  441. __func__, r);
  442. } else if (core->mode == WL1273_MODE_TX) {
  443. int r = core->write(core, WL1273_PUPD_SET,
  444. WL1273_PUPD_SET_OFF);
  445. if (r)
  446. dev_err(radio->dev,
  447. "%s: PUPD_SET fails: %d\n", __func__, r);
  448. }
  449. if (core->pdata->disable) {
  450. core->pdata->disable();
  451. dev_dbg(radio->dev, "Back to reset\n");
  452. }
  453. return 0;
  454. }
  455. static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
  456. {
  457. struct wl1273_core *core = radio->core;
  458. struct wl1273_fm_platform_data *pdata = core->pdata;
  459. struct device *dev = radio->dev;
  460. int r = -EINVAL;
  461. if (pdata->enable && core->mode == WL1273_MODE_OFF) {
  462. dev_dbg(radio->dev, "Out of reset\n");
  463. pdata->enable();
  464. msleep(250);
  465. }
  466. if (new_mode == WL1273_MODE_RX) {
  467. u16 val = WL1273_POWER_SET_FM;
  468. if (radio->rds_on)
  469. val |= WL1273_POWER_SET_RDS;
  470. /* If this fails try again */
  471. r = core->write(core, WL1273_POWER_SET, val);
  472. if (r) {
  473. msleep(100);
  474. r = core->write(core, WL1273_POWER_SET, val);
  475. if (r) {
  476. dev_err(dev, "%s: POWER_SET fails\n", __func__);
  477. goto fail;
  478. }
  479. }
  480. /* rds buffer configuration */
  481. radio->wr_index = 0;
  482. radio->rd_index = 0;
  483. } else if (new_mode == WL1273_MODE_TX) {
  484. /* If this fails try again once */
  485. r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
  486. if (r) {
  487. msleep(100);
  488. r = core->write(core, WL1273_PUPD_SET,
  489. WL1273_PUPD_SET_ON);
  490. if (r) {
  491. dev_err(dev, "%s: PUPD_SET fails\n", __func__);
  492. goto fail;
  493. }
  494. }
  495. if (radio->rds_on)
  496. r = core->write(core, WL1273_RDS_DATA_ENB, 1);
  497. else
  498. r = core->write(core, WL1273_RDS_DATA_ENB, 0);
  499. } else {
  500. dev_warn(dev, "%s: Illegal mode.\n", __func__);
  501. }
  502. if (core->mode == WL1273_MODE_OFF) {
  503. r = wl1273_fm_upload_firmware_patch(radio);
  504. if (r)
  505. dev_warn(dev, "Firmware upload failed.\n");
  506. /*
  507. * Sometimes the chip is in a wrong power state at this point.
  508. * So we set the power once again.
  509. */
  510. if (new_mode == WL1273_MODE_RX) {
  511. u16 val = WL1273_POWER_SET_FM;
  512. if (radio->rds_on)
  513. val |= WL1273_POWER_SET_RDS;
  514. r = core->write(core, WL1273_POWER_SET, val);
  515. if (r) {
  516. dev_err(dev, "%s: POWER_SET fails\n", __func__);
  517. goto fail;
  518. }
  519. } else if (new_mode == WL1273_MODE_TX) {
  520. r = core->write(core, WL1273_PUPD_SET,
  521. WL1273_PUPD_SET_ON);
  522. if (r) {
  523. dev_err(dev, "%s: PUPD_SET fails\n", __func__);
  524. goto fail;
  525. }
  526. }
  527. }
  528. return 0;
  529. fail:
  530. if (pdata->disable)
  531. pdata->disable();
  532. dev_dbg(dev, "%s: return: %d\n", __func__, r);
  533. return r;
  534. }
  535. static int wl1273_fm_suspend(struct wl1273_device *radio)
  536. {
  537. struct wl1273_core *core = radio->core;
  538. int r = 0;
  539. /* Cannot go from OFF to SUSPENDED */
  540. if (core->mode == WL1273_MODE_RX)
  541. r = core->write(core, WL1273_POWER_SET,
  542. WL1273_POWER_SET_RETENTION);
  543. else if (core->mode == WL1273_MODE_TX)
  544. r = core->write(core, WL1273_PUPD_SET,
  545. WL1273_PUPD_SET_RETENTION);
  546. else
  547. r = -EINVAL;
  548. if (r) {
  549. dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
  550. goto out;
  551. }
  552. out:
  553. return r;
  554. }
  555. static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
  556. {
  557. struct wl1273_core *core = radio->core;
  558. struct device *dev = radio->dev;
  559. int old_mode;
  560. int r;
  561. dev_dbg(dev, "%s\n", __func__);
  562. dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
  563. old_mode = core->mode;
  564. if (mode & radio->forbidden) {
  565. r = -EPERM;
  566. goto out;
  567. }
  568. switch (mode) {
  569. case WL1273_MODE_RX:
  570. case WL1273_MODE_TX:
  571. r = wl1273_fm_start(radio, mode);
  572. if (r) {
  573. dev_err(dev, "%s: Cannot start.\n", __func__);
  574. wl1273_fm_stop(radio);
  575. goto out;
  576. }
  577. core->mode = mode;
  578. r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
  579. if (r) {
  580. dev_err(dev, "INT_MASK_SET fails.\n");
  581. goto out;
  582. }
  583. /* remember previous settings */
  584. if (mode == WL1273_MODE_RX) {
  585. r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
  586. if (r) {
  587. dev_err(dev, "set freq fails: %d.\n", r);
  588. goto out;
  589. }
  590. r = core->set_volume(core, core->volume);
  591. if (r) {
  592. dev_err(dev, "set volume fails: %d.\n", r);
  593. goto out;
  594. }
  595. dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
  596. core->volume);
  597. } else {
  598. r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
  599. if (r) {
  600. dev_err(dev, "set freq fails: %d.\n", r);
  601. goto out;
  602. }
  603. }
  604. dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
  605. r = core->set_audio(core, core->audio_mode);
  606. if (r)
  607. dev_err(dev, "Cannot set audio mode.\n");
  608. break;
  609. case WL1273_MODE_OFF:
  610. r = wl1273_fm_stop(radio);
  611. if (r)
  612. dev_err(dev, "%s: Off fails: %d\n", __func__, r);
  613. else
  614. core->mode = WL1273_MODE_OFF;
  615. break;
  616. case WL1273_MODE_SUSPENDED:
  617. r = wl1273_fm_suspend(radio);
  618. if (r)
  619. dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
  620. else
  621. core->mode = WL1273_MODE_SUSPENDED;
  622. break;
  623. default:
  624. dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
  625. r = -EINVAL;
  626. break;
  627. }
  628. out:
  629. if (r)
  630. core->mode = old_mode;
  631. return r;
  632. }
  633. static int wl1273_fm_set_seek(struct wl1273_device *radio,
  634. unsigned int wrap_around,
  635. unsigned int seek_upward,
  636. int level)
  637. {
  638. struct wl1273_core *core = radio->core;
  639. int r = 0;
  640. unsigned int dir = (seek_upward == 0) ? 0 : 1;
  641. unsigned int f;
  642. f = radio->rx_frequency;
  643. dev_dbg(radio->dev, "rx_frequency: %d\n", f);
  644. if (dir && f + radio->spacing <= radio->rangehigh)
  645. r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
  646. else if (dir && wrap_around)
  647. r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
  648. else if (f - radio->spacing >= radio->rangelow)
  649. r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
  650. else if (wrap_around)
  651. r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
  652. if (r)
  653. goto out;
  654. if (level < SCHAR_MIN || level > SCHAR_MAX)
  655. return -EINVAL;
  656. INIT_COMPLETION(radio->busy);
  657. dev_dbg(radio->dev, "%s: BUSY\n", __func__);
  658. r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
  659. if (r)
  660. goto out;
  661. dev_dbg(radio->dev, "%s\n", __func__);
  662. r = core->write(core, WL1273_SEARCH_LVL_SET, level);
  663. if (r)
  664. goto out;
  665. r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
  666. if (r)
  667. goto out;
  668. r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
  669. if (r)
  670. goto out;
  671. wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
  672. if (!(radio->irq_received & WL1273_BL_EVENT))
  673. goto out;
  674. radio->irq_received &= ~WL1273_BL_EVENT;
  675. if (!wrap_around)
  676. goto out;
  677. /* Wrap around */
  678. dev_dbg(radio->dev, "Wrap around in HW seek.\n");
  679. if (seek_upward)
  680. f = radio->rangelow;
  681. else
  682. f = radio->rangehigh;
  683. r = wl1273_fm_set_rx_freq(radio, f);
  684. if (r)
  685. goto out;
  686. INIT_COMPLETION(radio->busy);
  687. dev_dbg(radio->dev, "%s: BUSY\n", __func__);
  688. r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
  689. if (r)
  690. goto out;
  691. wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
  692. out:
  693. dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
  694. return r;
  695. }
  696. /**
  697. * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
  698. * @radio: A pointer to the device struct.
  699. */
  700. static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
  701. {
  702. struct wl1273_core *core = radio->core;
  703. struct device *dev = radio->dev;
  704. u16 val;
  705. int r;
  706. if (core->mode == WL1273_MODE_OFF ||
  707. core->mode == WL1273_MODE_SUSPENDED)
  708. return -EPERM;
  709. r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
  710. if (r) {
  711. dev_err(dev, "%s: read error: %d\n", __func__, r);
  712. goto out;
  713. }
  714. out:
  715. return val;
  716. }
  717. /**
  718. * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
  719. * @radio: A pointer to the device struct.
  720. * @preemphasis: The new pre-amphasis value.
  721. *
  722. * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
  723. * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
  724. */
  725. static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
  726. unsigned int preemphasis)
  727. {
  728. struct wl1273_core *core = radio->core;
  729. int r;
  730. u16 em;
  731. if (core->mode == WL1273_MODE_OFF ||
  732. core->mode == WL1273_MODE_SUSPENDED)
  733. return -EPERM;
  734. mutex_lock(&core->lock);
  735. switch (preemphasis) {
  736. case V4L2_PREEMPHASIS_DISABLED:
  737. em = 1;
  738. break;
  739. case V4L2_PREEMPHASIS_50_uS:
  740. em = 0;
  741. break;
  742. case V4L2_PREEMPHASIS_75_uS:
  743. em = 2;
  744. break;
  745. default:
  746. r = -EINVAL;
  747. goto out;
  748. }
  749. r = core->write(core, WL1273_PREMPH_SET, em);
  750. if (r)
  751. goto out;
  752. radio->preemphasis = preemphasis;
  753. out:
  754. mutex_unlock(&core->lock);
  755. return r;
  756. }
  757. static int wl1273_fm_rds_on(struct wl1273_device *radio)
  758. {
  759. struct wl1273_core *core = radio->core;
  760. int r;
  761. dev_dbg(radio->dev, "%s\n", __func__);
  762. if (radio->rds_on)
  763. return 0;
  764. r = core->write(core, WL1273_POWER_SET,
  765. WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
  766. if (r)
  767. goto out;
  768. r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
  769. if (r)
  770. dev_err(radio->dev, "set freq fails: %d.\n", r);
  771. out:
  772. return r;
  773. }
  774. static int wl1273_fm_rds_off(struct wl1273_device *radio)
  775. {
  776. struct wl1273_core *core = radio->core;
  777. int r;
  778. if (!radio->rds_on)
  779. return 0;
  780. radio->irq_flags &= ~WL1273_RDS_EVENT;
  781. r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
  782. if (r)
  783. goto out;
  784. /* Service pending read */
  785. wake_up_interruptible(&radio->read_queue);
  786. dev_dbg(radio->dev, "%s\n", __func__);
  787. r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
  788. if (r)
  789. goto out;
  790. r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
  791. if (r)
  792. dev_err(radio->dev, "set freq fails: %d.\n", r);
  793. out:
  794. dev_dbg(radio->dev, "%s: exiting...\n", __func__);
  795. return r;
  796. }
  797. static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
  798. {
  799. int r = 0;
  800. struct wl1273_core *core = radio->core;
  801. if (core->mode == WL1273_MODE_OFF ||
  802. core->mode == WL1273_MODE_SUSPENDED)
  803. return -EPERM;
  804. if (new_mode == WL1273_RDS_RESET) {
  805. r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
  806. return r;
  807. }
  808. if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
  809. r = core->write(core, WL1273_RDS_DATA_ENB, 0);
  810. } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
  811. r = core->write(core, WL1273_RDS_DATA_ENB, 1);
  812. } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
  813. r = wl1273_fm_rds_off(radio);
  814. } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
  815. r = wl1273_fm_rds_on(radio);
  816. } else {
  817. dev_err(radio->dev, "%s: Unknown mode: %d\n",
  818. __func__, new_mode);
  819. r = -EINVAL;
  820. }
  821. if (!r)
  822. radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
  823. return r;
  824. }
  825. static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
  826. size_t count, loff_t *ppos)
  827. {
  828. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  829. struct wl1273_core *core = radio->core;
  830. u16 val;
  831. int r;
  832. dev_dbg(radio->dev, "%s\n", __func__);
  833. if (core->mode != WL1273_MODE_TX)
  834. return count;
  835. if (radio->rds_users == 0) {
  836. dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
  837. return 0;
  838. }
  839. if (mutex_lock_interruptible(&core->lock))
  840. return -EINTR;
  841. /*
  842. * Multiple processes can open the device, but only
  843. * one gets to write to it.
  844. */
  845. if (radio->owner && radio->owner != file) {
  846. r = -EBUSY;
  847. goto out;
  848. }
  849. radio->owner = file;
  850. /* Manual Mode */
  851. if (count > 255)
  852. val = 255;
  853. else
  854. val = count;
  855. core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
  856. if (copy_from_user(radio->write_buf + 1, buf, val)) {
  857. r = -EFAULT;
  858. goto out;
  859. }
  860. dev_dbg(radio->dev, "Count: %d\n", val);
  861. dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
  862. radio->write_buf[0] = WL1273_RDS_DATA_SET;
  863. core->write_data(core, radio->write_buf, val + 1);
  864. r = val;
  865. out:
  866. mutex_unlock(&core->lock);
  867. return r;
  868. }
  869. static unsigned int wl1273_fm_fops_poll(struct file *file,
  870. struct poll_table_struct *pts)
  871. {
  872. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  873. struct wl1273_core *core = radio->core;
  874. if (radio->owner && radio->owner != file)
  875. return -EBUSY;
  876. radio->owner = file;
  877. if (core->mode == WL1273_MODE_RX) {
  878. poll_wait(file, &radio->read_queue, pts);
  879. if (radio->rd_index != radio->wr_index)
  880. return POLLIN | POLLRDNORM;
  881. } else if (core->mode == WL1273_MODE_TX) {
  882. return POLLOUT | POLLWRNORM;
  883. }
  884. return 0;
  885. }
  886. static int wl1273_fm_fops_open(struct file *file)
  887. {
  888. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  889. struct wl1273_core *core = radio->core;
  890. int r = 0;
  891. dev_dbg(radio->dev, "%s\n", __func__);
  892. if (core->mode == WL1273_MODE_RX && radio->rds_on &&
  893. !radio->rds_users) {
  894. dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
  895. if (mutex_lock_interruptible(&core->lock))
  896. return -EINTR;
  897. radio->irq_flags |= WL1273_RDS_EVENT;
  898. r = core->write(core, WL1273_INT_MASK_SET,
  899. radio->irq_flags);
  900. if (r) {
  901. mutex_unlock(&core->lock);
  902. goto out;
  903. }
  904. radio->rds_users++;
  905. mutex_unlock(&core->lock);
  906. }
  907. out:
  908. return r;
  909. }
  910. static int wl1273_fm_fops_release(struct file *file)
  911. {
  912. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  913. struct wl1273_core *core = radio->core;
  914. int r = 0;
  915. dev_dbg(radio->dev, "%s\n", __func__);
  916. if (radio->rds_users > 0) {
  917. radio->rds_users--;
  918. if (radio->rds_users == 0) {
  919. if (mutex_lock_interruptible(&core->lock))
  920. return -EINTR;
  921. radio->irq_flags &= ~WL1273_RDS_EVENT;
  922. if (core->mode == WL1273_MODE_RX) {
  923. r = core->write(core,
  924. WL1273_INT_MASK_SET,
  925. radio->irq_flags);
  926. if (r) {
  927. mutex_unlock(&core->lock);
  928. goto out;
  929. }
  930. }
  931. mutex_unlock(&core->lock);
  932. }
  933. }
  934. if (file == radio->owner)
  935. radio->owner = NULL;
  936. out:
  937. return r;
  938. }
  939. static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
  940. size_t count, loff_t *ppos)
  941. {
  942. int r = 0;
  943. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  944. struct wl1273_core *core = radio->core;
  945. unsigned int block_count = 0;
  946. u16 val;
  947. dev_dbg(radio->dev, "%s\n", __func__);
  948. if (core->mode != WL1273_MODE_RX)
  949. return 0;
  950. if (radio->rds_users == 0) {
  951. dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
  952. return 0;
  953. }
  954. if (mutex_lock_interruptible(&core->lock))
  955. return -EINTR;
  956. /*
  957. * Multiple processes can open the device, but only
  958. * one at a time gets read access.
  959. */
  960. if (radio->owner && radio->owner != file) {
  961. r = -EBUSY;
  962. goto out;
  963. }
  964. radio->owner = file;
  965. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  966. if (r) {
  967. dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
  968. goto out;
  969. } else if (val == 0) {
  970. dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
  971. r = -ENODATA;
  972. goto out;
  973. }
  974. /* block if no new data available */
  975. while (radio->wr_index == radio->rd_index) {
  976. if (file->f_flags & O_NONBLOCK) {
  977. r = -EWOULDBLOCK;
  978. goto out;
  979. }
  980. dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
  981. if (wait_event_interruptible(radio->read_queue,
  982. radio->wr_index !=
  983. radio->rd_index) < 0) {
  984. r = -EINTR;
  985. goto out;
  986. }
  987. }
  988. /* calculate block count from byte count */
  989. count /= RDS_BLOCK_SIZE;
  990. /* copy RDS blocks from the internal buffer and to user buffer */
  991. while (block_count < count) {
  992. if (radio->rd_index == radio->wr_index)
  993. break;
  994. /* always transfer complete RDS blocks */
  995. if (copy_to_user(buf, &radio->buffer[radio->rd_index],
  996. RDS_BLOCK_SIZE))
  997. break;
  998. /* increment and wrap the read pointer */
  999. radio->rd_index += RDS_BLOCK_SIZE;
  1000. if (radio->rd_index >= radio->buf_size)
  1001. radio->rd_index = 0;
  1002. /* increment counters */
  1003. block_count++;
  1004. buf += RDS_BLOCK_SIZE;
  1005. r += RDS_BLOCK_SIZE;
  1006. }
  1007. out:
  1008. dev_dbg(radio->dev, "%s: exit\n", __func__);
  1009. mutex_unlock(&core->lock);
  1010. return r;
  1011. }
  1012. static const struct v4l2_file_operations wl1273_fops = {
  1013. .owner = THIS_MODULE,
  1014. .read = wl1273_fm_fops_read,
  1015. .write = wl1273_fm_fops_write,
  1016. .poll = wl1273_fm_fops_poll,
  1017. .unlocked_ioctl = video_ioctl2,
  1018. .open = wl1273_fm_fops_open,
  1019. .release = wl1273_fm_fops_release,
  1020. };
  1021. static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
  1022. struct v4l2_capability *capability)
  1023. {
  1024. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1025. dev_dbg(radio->dev, "%s\n", __func__);
  1026. strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
  1027. sizeof(capability->driver));
  1028. strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
  1029. sizeof(capability->card));
  1030. strlcpy(capability->bus_info, radio->bus_type,
  1031. sizeof(capability->bus_info));
  1032. capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
  1033. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
  1034. V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
  1035. V4L2_CAP_RDS_OUTPUT;
  1036. return 0;
  1037. }
  1038. static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
  1039. unsigned int *i)
  1040. {
  1041. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1042. dev_dbg(radio->dev, "%s\n", __func__);
  1043. *i = 0;
  1044. return 0;
  1045. }
  1046. static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
  1047. unsigned int i)
  1048. {
  1049. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1050. dev_dbg(radio->dev, "%s\n", __func__);
  1051. if (i != 0)
  1052. return -EINVAL;
  1053. return 0;
  1054. }
  1055. /**
  1056. * wl1273_fm_set_tx_power() - Set the transmission power value.
  1057. * @core: A pointer to the device struct.
  1058. * @power: The new power value.
  1059. */
  1060. static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
  1061. {
  1062. struct wl1273_core *core = radio->core;
  1063. int r;
  1064. if (core->mode == WL1273_MODE_OFF ||
  1065. core->mode == WL1273_MODE_SUSPENDED)
  1066. return -EPERM;
  1067. mutex_lock(&core->lock);
  1068. /* Convert the dBuV value to chip presentation */
  1069. r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
  1070. if (r)
  1071. goto out;
  1072. radio->tx_power = power;
  1073. out:
  1074. mutex_unlock(&core->lock);
  1075. return r;
  1076. }
  1077. #define WL1273_SPACING_50kHz 1
  1078. #define WL1273_SPACING_100kHz 2
  1079. #define WL1273_SPACING_200kHz 4
  1080. static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
  1081. unsigned int spacing)
  1082. {
  1083. struct wl1273_core *core = radio->core;
  1084. int r;
  1085. if (spacing == 0) {
  1086. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1087. WL1273_SPACING_100kHz);
  1088. radio->spacing = 100;
  1089. } else if (spacing - 50000 < 25000) {
  1090. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1091. WL1273_SPACING_50kHz);
  1092. radio->spacing = 50;
  1093. } else if (spacing - 100000 < 50000) {
  1094. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1095. WL1273_SPACING_100kHz);
  1096. radio->spacing = 100;
  1097. } else {
  1098. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1099. WL1273_SPACING_200kHz);
  1100. radio->spacing = 200;
  1101. }
  1102. return r;
  1103. }
  1104. static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1105. {
  1106. struct wl1273_device *radio = ctrl->priv;
  1107. struct wl1273_core *core = radio->core;
  1108. dev_dbg(radio->dev, "%s\n", __func__);
  1109. if (mutex_lock_interruptible(&core->lock))
  1110. return -EINTR;
  1111. switch (ctrl->id) {
  1112. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1113. ctrl->cur.val = wl1273_fm_get_tx_ctune(radio);
  1114. break;
  1115. default:
  1116. dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
  1117. __func__, ctrl->id);
  1118. break;
  1119. }
  1120. mutex_unlock(&core->lock);
  1121. return 0;
  1122. }
  1123. #define WL1273_MUTE_SOFT_ENABLE (1 << 0)
  1124. #define WL1273_MUTE_AC (1 << 1)
  1125. #define WL1273_MUTE_HARD_LEFT (1 << 2)
  1126. #define WL1273_MUTE_HARD_RIGHT (1 << 3)
  1127. #define WL1273_MUTE_SOFT_FORCE (1 << 4)
  1128. static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
  1129. {
  1130. return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
  1131. }
  1132. static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
  1133. {
  1134. struct wl1273_device *radio = to_radio(ctrl);
  1135. struct wl1273_core *core = radio->core;
  1136. int r = 0;
  1137. dev_dbg(radio->dev, "%s\n", __func__);
  1138. switch (ctrl->id) {
  1139. case V4L2_CID_AUDIO_MUTE:
  1140. if (mutex_lock_interruptible(&core->lock))
  1141. return -EINTR;
  1142. if (core->mode == WL1273_MODE_RX && ctrl->val)
  1143. r = core->write(core,
  1144. WL1273_MUTE_STATUS_SET,
  1145. WL1273_MUTE_HARD_LEFT |
  1146. WL1273_MUTE_HARD_RIGHT);
  1147. else if (core->mode == WL1273_MODE_RX)
  1148. r = core->write(core,
  1149. WL1273_MUTE_STATUS_SET, 0x0);
  1150. else if (core->mode == WL1273_MODE_TX && ctrl->val)
  1151. r = core->write(core, WL1273_MUTE, 1);
  1152. else if (core->mode == WL1273_MODE_TX)
  1153. r = core->write(core, WL1273_MUTE, 0);
  1154. mutex_unlock(&core->lock);
  1155. break;
  1156. case V4L2_CID_AUDIO_VOLUME:
  1157. if (ctrl->val == 0)
  1158. r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
  1159. else
  1160. r = core->set_volume(core, core->volume);
  1161. break;
  1162. case V4L2_CID_TUNE_PREEMPHASIS:
  1163. r = wl1273_fm_set_preemphasis(radio, ctrl->val);
  1164. break;
  1165. case V4L2_CID_TUNE_POWER_LEVEL:
  1166. r = wl1273_fm_set_tx_power(radio, ctrl->val);
  1167. break;
  1168. default:
  1169. dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
  1170. __func__, ctrl->id);
  1171. break;
  1172. }
  1173. dev_dbg(radio->dev, "%s\n", __func__);
  1174. return r;
  1175. }
  1176. static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
  1177. struct v4l2_audio *audio)
  1178. {
  1179. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1180. dev_dbg(radio->dev, "%s\n", __func__);
  1181. if (audio->index > 1)
  1182. return -EINVAL;
  1183. strlcpy(audio->name, "Radio", sizeof(audio->name));
  1184. audio->capability = V4L2_AUDCAP_STEREO;
  1185. return 0;
  1186. }
  1187. static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
  1188. struct v4l2_audio *audio)
  1189. {
  1190. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1191. dev_dbg(radio->dev, "%s\n", __func__);
  1192. if (audio->index != 0)
  1193. return -EINVAL;
  1194. return 0;
  1195. }
  1196. #define WL1273_RDS_NOT_SYNCHRONIZED 0
  1197. #define WL1273_RDS_SYNCHRONIZED 1
  1198. static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
  1199. struct v4l2_tuner *tuner)
  1200. {
  1201. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1202. struct wl1273_core *core = radio->core;
  1203. u16 val;
  1204. int r;
  1205. dev_dbg(radio->dev, "%s\n", __func__);
  1206. if (tuner->index > 0)
  1207. return -EINVAL;
  1208. strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
  1209. tuner->type = V4L2_TUNER_RADIO;
  1210. tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
  1211. tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
  1212. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
  1213. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
  1214. if (radio->stereo)
  1215. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  1216. else
  1217. tuner->audmode = V4L2_TUNER_MODE_MONO;
  1218. if (core->mode != WL1273_MODE_RX)
  1219. return 0;
  1220. if (mutex_lock_interruptible(&core->lock))
  1221. return -EINTR;
  1222. r = core->read(core, WL1273_STEREO_GET, &val);
  1223. if (r)
  1224. goto out;
  1225. if (val == 1)
  1226. tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1227. else
  1228. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  1229. r = core->read(core, WL1273_RSSI_LVL_GET, &val);
  1230. if (r)
  1231. goto out;
  1232. tuner->signal = (s16) val;
  1233. dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
  1234. tuner->afc = 0;
  1235. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  1236. if (r)
  1237. goto out;
  1238. if (val == WL1273_RDS_SYNCHRONIZED)
  1239. tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
  1240. out:
  1241. mutex_unlock(&core->lock);
  1242. return r;
  1243. }
  1244. static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
  1245. struct v4l2_tuner *tuner)
  1246. {
  1247. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1248. struct wl1273_core *core = radio->core;
  1249. int r = 0;
  1250. dev_dbg(radio->dev, "%s\n", __func__);
  1251. dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
  1252. dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
  1253. dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
  1254. dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
  1255. dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
  1256. dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
  1257. if (tuner->index > 0)
  1258. return -EINVAL;
  1259. if (mutex_lock_interruptible(&core->lock))
  1260. return -EINTR;
  1261. r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
  1262. if (r)
  1263. goto out;
  1264. if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
  1265. r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
  1266. else
  1267. r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
  1268. if (r)
  1269. dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
  1270. if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
  1271. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
  1272. if (r < 0) {
  1273. dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
  1274. __func__, r);
  1275. goto out;
  1276. }
  1277. radio->stereo = false;
  1278. } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
  1279. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
  1280. if (r < 0) {
  1281. dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
  1282. __func__, r);
  1283. goto out;
  1284. }
  1285. radio->stereo = true;
  1286. } else {
  1287. dev_err(radio->dev, "%s: tuner->audmode: %d\n",
  1288. __func__, tuner->audmode);
  1289. r = -EINVAL;
  1290. goto out;
  1291. }
  1292. out:
  1293. mutex_unlock(&core->lock);
  1294. return r;
  1295. }
  1296. static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
  1297. struct v4l2_frequency *freq)
  1298. {
  1299. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1300. struct wl1273_core *core = radio->core;
  1301. dev_dbg(radio->dev, "%s\n", __func__);
  1302. if (mutex_lock_interruptible(&core->lock))
  1303. return -EINTR;
  1304. freq->type = V4L2_TUNER_RADIO;
  1305. freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
  1306. mutex_unlock(&core->lock);
  1307. return 0;
  1308. }
  1309. static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
  1310. struct v4l2_frequency *freq)
  1311. {
  1312. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1313. struct wl1273_core *core = radio->core;
  1314. int r;
  1315. dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
  1316. if (freq->type != V4L2_TUNER_RADIO) {
  1317. dev_dbg(radio->dev,
  1318. "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
  1319. return -EINVAL;
  1320. }
  1321. if (mutex_lock_interruptible(&core->lock))
  1322. return -EINTR;
  1323. if (core->mode == WL1273_MODE_RX) {
  1324. dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
  1325. r = wl1273_fm_set_rx_freq(radio,
  1326. WL1273_INV_FREQ(freq->frequency));
  1327. if (r)
  1328. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1329. ": set frequency failed with %d\n", r);
  1330. } else {
  1331. r = wl1273_fm_set_tx_freq(radio,
  1332. WL1273_INV_FREQ(freq->frequency));
  1333. if (r)
  1334. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1335. ": set frequency failed with %d\n", r);
  1336. }
  1337. mutex_unlock(&core->lock);
  1338. dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
  1339. return r;
  1340. }
  1341. #define WL1273_DEFAULT_SEEK_LEVEL 7
  1342. static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
  1343. struct v4l2_hw_freq_seek *seek)
  1344. {
  1345. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1346. struct wl1273_core *core = radio->core;
  1347. int r;
  1348. dev_dbg(radio->dev, "%s\n", __func__);
  1349. if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
  1350. return -EINVAL;
  1351. if (mutex_lock_interruptible(&core->lock))
  1352. return -EINTR;
  1353. r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
  1354. if (r)
  1355. goto out;
  1356. r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
  1357. if (r)
  1358. dev_warn(radio->dev, "HW seek failed: %d\n", r);
  1359. r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
  1360. WL1273_DEFAULT_SEEK_LEVEL);
  1361. if (r)
  1362. dev_warn(radio->dev, "HW seek failed: %d\n", r);
  1363. out:
  1364. mutex_unlock(&core->lock);
  1365. return r;
  1366. }
  1367. static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
  1368. struct v4l2_modulator *modulator)
  1369. {
  1370. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1371. struct wl1273_core *core = radio->core;
  1372. int r = 0;
  1373. dev_dbg(radio->dev, "%s\n", __func__);
  1374. if (modulator->index > 0)
  1375. return -EINVAL;
  1376. if (mutex_lock_interruptible(&core->lock))
  1377. return -EINTR;
  1378. r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
  1379. if (r)
  1380. goto out;
  1381. if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
  1382. r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
  1383. else
  1384. r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
  1385. if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
  1386. r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
  1387. else
  1388. r = core->write(core, WL1273_MONO_SET,
  1389. WL1273_RX_STEREO);
  1390. if (r < 0)
  1391. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1392. "MONO_SET fails: %d\n", r);
  1393. out:
  1394. mutex_unlock(&core->lock);
  1395. return r;
  1396. }
  1397. static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
  1398. struct v4l2_modulator *modulator)
  1399. {
  1400. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1401. struct wl1273_core *core = radio->core;
  1402. u16 val;
  1403. int r;
  1404. dev_dbg(radio->dev, "%s\n", __func__);
  1405. strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
  1406. sizeof(modulator->name));
  1407. modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
  1408. modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
  1409. modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
  1410. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
  1411. if (core->mode != WL1273_MODE_TX)
  1412. return 0;
  1413. if (mutex_lock_interruptible(&core->lock))
  1414. return -EINTR;
  1415. r = core->read(core, WL1273_MONO_SET, &val);
  1416. if (r)
  1417. goto out;
  1418. if (val == WL1273_TX_STEREO)
  1419. modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
  1420. else
  1421. modulator->txsubchans = V4L2_TUNER_SUB_MONO;
  1422. if (radio->rds_on)
  1423. modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
  1424. out:
  1425. mutex_unlock(&core->lock);
  1426. return 0;
  1427. }
  1428. static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
  1429. {
  1430. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1431. struct wl1273_core *core = radio->core;
  1432. struct device *dev = radio->dev;
  1433. u16 val;
  1434. int r;
  1435. dev_info(dev, DRIVER_DESC);
  1436. if (core->mode == WL1273_MODE_OFF) {
  1437. dev_info(dev, "Mode: Off\n");
  1438. return 0;
  1439. }
  1440. if (core->mode == WL1273_MODE_SUSPENDED) {
  1441. dev_info(dev, "Mode: Suspended\n");
  1442. return 0;
  1443. }
  1444. r = core->read(core, WL1273_ASIC_ID_GET, &val);
  1445. if (r)
  1446. dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
  1447. else
  1448. dev_info(dev, "ASIC_ID: 0x%04x\n", val);
  1449. r = core->read(core, WL1273_ASIC_VER_GET, &val);
  1450. if (r)
  1451. dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
  1452. else
  1453. dev_info(dev, "ASIC Version: 0x%04x\n", val);
  1454. r = core->read(core, WL1273_FIRM_VER_GET, &val);
  1455. if (r)
  1456. dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
  1457. else
  1458. dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
  1459. r = core->read(core, WL1273_BAND_SET, &val);
  1460. if (r)
  1461. dev_err(dev, "%s: Get BAND fails.\n", __func__);
  1462. else
  1463. dev_info(dev, "BAND: %d\n", val);
  1464. if (core->mode == WL1273_MODE_TX) {
  1465. r = core->read(core, WL1273_PUPD_SET, &val);
  1466. if (r)
  1467. dev_err(dev, "%s: Get PUPD fails.\n", __func__);
  1468. else
  1469. dev_info(dev, "PUPD: 0x%04x\n", val);
  1470. r = core->read(core, WL1273_CHANL_SET, &val);
  1471. if (r)
  1472. dev_err(dev, "%s: Get CHANL fails.\n", __func__);
  1473. else
  1474. dev_info(dev, "Tx frequency: %dkHz\n", val*10);
  1475. } else if (core->mode == WL1273_MODE_RX) {
  1476. int bf = radio->rangelow;
  1477. r = core->read(core, WL1273_FREQ_SET, &val);
  1478. if (r)
  1479. dev_err(dev, "%s: Get FREQ fails.\n", __func__);
  1480. else
  1481. dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
  1482. r = core->read(core, WL1273_MOST_MODE_SET, &val);
  1483. if (r)
  1484. dev_err(dev, "%s: Get MOST_MODE fails.\n",
  1485. __func__);
  1486. else if (val == 0)
  1487. dev_info(dev, "MOST_MODE: Stereo according to blend\n");
  1488. else if (val == 1)
  1489. dev_info(dev, "MOST_MODE: Force mono output\n");
  1490. else
  1491. dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
  1492. r = core->read(core, WL1273_MOST_BLEND_SET, &val);
  1493. if (r)
  1494. dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
  1495. else if (val == 0)
  1496. dev_info(dev,
  1497. "MOST_BLEND: Switched blend & hysteresis.\n");
  1498. else if (val == 1)
  1499. dev_info(dev, "MOST_BLEND: Soft blend.\n");
  1500. else
  1501. dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
  1502. r = core->read(core, WL1273_STEREO_GET, &val);
  1503. if (r)
  1504. dev_err(dev, "%s: Get STEREO fails.\n", __func__);
  1505. else if (val == 0)
  1506. dev_info(dev, "STEREO: Not detected\n");
  1507. else if (val == 1)
  1508. dev_info(dev, "STEREO: Detected\n");
  1509. else
  1510. dev_info(dev, "STEREO: Unexpected value: %d\n", val);
  1511. r = core->read(core, WL1273_RSSI_LVL_GET, &val);
  1512. if (r)
  1513. dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
  1514. else
  1515. dev_info(dev, "RX signal strength: %d\n", (s16) val);
  1516. r = core->read(core, WL1273_POWER_SET, &val);
  1517. if (r)
  1518. dev_err(dev, "%s: Get POWER fails.\n", __func__);
  1519. else
  1520. dev_info(dev, "POWER: 0x%04x\n", val);
  1521. r = core->read(core, WL1273_INT_MASK_SET, &val);
  1522. if (r)
  1523. dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
  1524. else
  1525. dev_info(dev, "INT_MASK: 0x%04x\n", val);
  1526. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  1527. if (r)
  1528. dev_err(dev, "%s: Get RDS_SYNC fails.\n",
  1529. __func__);
  1530. else if (val == 0)
  1531. dev_info(dev, "RDS_SYNC: Not synchronized\n");
  1532. else if (val == 1)
  1533. dev_info(dev, "RDS_SYNC: Synchronized\n");
  1534. else
  1535. dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
  1536. r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
  1537. if (r)
  1538. dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
  1539. __func__);
  1540. else
  1541. dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
  1542. r = core->read(core, WL1273_VOLUME_SET, &val);
  1543. if (r)
  1544. dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
  1545. else
  1546. dev_info(dev, "VOLUME: 0x%04x\n", val);
  1547. }
  1548. return 0;
  1549. }
  1550. static void wl1273_vdev_release(struct video_device *dev)
  1551. {
  1552. }
  1553. static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
  1554. .s_ctrl = wl1273_fm_vidioc_s_ctrl,
  1555. .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
  1556. };
  1557. static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
  1558. .vidioc_querycap = wl1273_fm_vidioc_querycap,
  1559. .vidioc_g_input = wl1273_fm_vidioc_g_input,
  1560. .vidioc_s_input = wl1273_fm_vidioc_s_input,
  1561. .vidioc_g_audio = wl1273_fm_vidioc_g_audio,
  1562. .vidioc_s_audio = wl1273_fm_vidioc_s_audio,
  1563. .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
  1564. .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
  1565. .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
  1566. .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
  1567. .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
  1568. .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
  1569. .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
  1570. .vidioc_log_status = wl1273_fm_vidioc_log_status,
  1571. };
  1572. static struct video_device wl1273_viddev_template = {
  1573. .fops = &wl1273_fops,
  1574. .ioctl_ops = &wl1273_ioctl_ops,
  1575. .name = WL1273_FM_DRIVER_NAME,
  1576. .release = wl1273_vdev_release,
  1577. };
  1578. static int wl1273_fm_radio_remove(struct platform_device *pdev)
  1579. {
  1580. struct wl1273_device *radio = platform_get_drvdata(pdev);
  1581. struct wl1273_core *core = radio->core;
  1582. dev_info(&pdev->dev, "%s.\n", __func__);
  1583. free_irq(core->client->irq, radio);
  1584. core->pdata->free_resources();
  1585. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  1586. video_unregister_device(&radio->videodev);
  1587. v4l2_device_unregister(&radio->v4l2dev);
  1588. kfree(radio->buffer);
  1589. kfree(radio->write_buf);
  1590. kfree(radio);
  1591. return 0;
  1592. }
  1593. static int __devinit wl1273_fm_radio_probe(struct platform_device *pdev)
  1594. {
  1595. struct wl1273_core **core = pdev->dev.platform_data;
  1596. struct wl1273_device *radio;
  1597. struct v4l2_ctrl *ctrl;
  1598. int r = 0;
  1599. pr_debug("%s\n", __func__);
  1600. if (!core) {
  1601. dev_err(&pdev->dev, "No platform data.\n");
  1602. r = -EINVAL;
  1603. goto pdata_err;
  1604. }
  1605. radio = kzalloc(sizeof(*radio), GFP_KERNEL);
  1606. if (!radio) {
  1607. r = -ENOMEM;
  1608. goto pdata_err;
  1609. }
  1610. /* RDS buffer allocation */
  1611. radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
  1612. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  1613. if (!radio->buffer) {
  1614. pr_err("Cannot allocate memory for RDS buffer.\n");
  1615. r = -ENOMEM;
  1616. goto err_kmalloc;
  1617. }
  1618. radio->core = *core;
  1619. radio->irq_flags = WL1273_IRQ_MASK;
  1620. radio->dev = &radio->core->client->dev;
  1621. radio->rds_on = false;
  1622. radio->core->mode = WL1273_MODE_OFF;
  1623. radio->tx_power = 118;
  1624. radio->core->audio_mode = WL1273_AUDIO_ANALOG;
  1625. radio->band = WL1273_BAND_OTHER;
  1626. radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
  1627. radio->core->channel_number = 2;
  1628. radio->core->volume = WL1273_DEFAULT_VOLUME;
  1629. radio->rx_frequency = WL1273_BAND_OTHER_LOW;
  1630. radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
  1631. radio->rangelow = WL1273_BAND_OTHER_LOW;
  1632. radio->rangehigh = WL1273_BAND_OTHER_HIGH;
  1633. radio->stereo = true;
  1634. radio->bus_type = "I2C";
  1635. if (radio->core->pdata->request_resources) {
  1636. r = radio->core->pdata->request_resources(radio->core->client);
  1637. if (r) {
  1638. dev_err(radio->dev, WL1273_FM_DRIVER_NAME
  1639. ": Cannot get platform data\n");
  1640. goto err_resources;
  1641. }
  1642. dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
  1643. r = request_threaded_irq(radio->core->client->irq, NULL,
  1644. wl1273_fm_irq_thread_handler,
  1645. IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
  1646. "wl1273-fm", radio);
  1647. if (r < 0) {
  1648. dev_err(radio->dev, WL1273_FM_DRIVER_NAME
  1649. ": Unable to register IRQ handler: %d\n", r);
  1650. goto err_request_irq;
  1651. }
  1652. } else {
  1653. dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ"
  1654. " not configured");
  1655. r = -EINVAL;
  1656. goto err_resources;
  1657. }
  1658. init_completion(&radio->busy);
  1659. init_waitqueue_head(&radio->read_queue);
  1660. radio->write_buf = kmalloc(256, GFP_KERNEL);
  1661. if (!radio->write_buf) {
  1662. r = -ENOMEM;
  1663. goto write_buf_err;
  1664. }
  1665. radio->dev = &pdev->dev;
  1666. radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
  1667. radio->rds_users = 0;
  1668. r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
  1669. if (r) {
  1670. dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
  1671. goto device_register_err;
  1672. }
  1673. /* V4L2 configuration */
  1674. memcpy(&radio->videodev, &wl1273_viddev_template,
  1675. sizeof(wl1273_viddev_template));
  1676. radio->videodev.v4l2_dev = &radio->v4l2dev;
  1677. v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
  1678. /* add in ascending ID order */
  1679. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1680. V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
  1681. WL1273_DEFAULT_VOLUME);
  1682. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1683. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  1684. v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1685. V4L2_CID_TUNE_PREEMPHASIS,
  1686. V4L2_PREEMPHASIS_75_uS, 0x03,
  1687. V4L2_PREEMPHASIS_50_uS);
  1688. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1689. V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
  1690. ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1691. V4L2_CID_TUNE_ANTENNA_CAPACITOR,
  1692. 0, 255, 1, 255);
  1693. if (ctrl)
  1694. ctrl->is_volatile = 1;
  1695. if (radio->ctrl_handler.error) {
  1696. r = radio->ctrl_handler.error;
  1697. dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
  1698. goto handler_init_err;
  1699. }
  1700. video_set_drvdata(&radio->videodev, radio);
  1701. platform_set_drvdata(pdev, radio);
  1702. /* register video device */
  1703. r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
  1704. if (r) {
  1705. dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
  1706. ": Could not register video device\n");
  1707. goto handler_init_err;
  1708. }
  1709. return 0;
  1710. handler_init_err:
  1711. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  1712. v4l2_device_unregister(&radio->v4l2dev);
  1713. device_register_err:
  1714. kfree(radio->write_buf);
  1715. write_buf_err:
  1716. free_irq(radio->core->client->irq, radio);
  1717. err_request_irq:
  1718. radio->core->pdata->free_resources();
  1719. err_resources:
  1720. kfree(radio->buffer);
  1721. err_kmalloc:
  1722. kfree(radio);
  1723. pdata_err:
  1724. return r;
  1725. }
  1726. MODULE_ALIAS("platform:wl1273_fm_radio");
  1727. static struct platform_driver wl1273_fm_radio_driver = {
  1728. .probe = wl1273_fm_radio_probe,
  1729. .remove = __devexit_p(wl1273_fm_radio_remove),
  1730. .driver = {
  1731. .name = "wl1273_fm_radio",
  1732. .owner = THIS_MODULE,
  1733. },
  1734. };
  1735. static int __init wl1273_fm_module_init(void)
  1736. {
  1737. pr_info("%s\n", __func__);
  1738. return platform_driver_register(&wl1273_fm_radio_driver);
  1739. }
  1740. module_init(wl1273_fm_module_init);
  1741. static void __exit wl1273_fm_module_exit(void)
  1742. {
  1743. platform_driver_unregister(&wl1273_fm_radio_driver);
  1744. pr_info(DRIVER_DESC ", Exiting.\n");
  1745. }
  1746. module_exit(wl1273_fm_module_exit);
  1747. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  1748. MODULE_DESCRIPTION(DRIVER_DESC);
  1749. MODULE_LICENSE("GPL");