radio-wl1273.c 50 KB

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