radio-wl1273.c 50 KB

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