radio-wl1273.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  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. if (mutex_lock_interruptible(&core->lock))
  935. return -EINTR;
  936. radio->irq_flags &= ~WL1273_RDS_EVENT;
  937. if (core->mode == WL1273_MODE_RX) {
  938. r = core->write(core,
  939. WL1273_INT_MASK_SET,
  940. radio->irq_flags);
  941. if (r) {
  942. mutex_unlock(&core->lock);
  943. goto out;
  944. }
  945. }
  946. mutex_unlock(&core->lock);
  947. }
  948. }
  949. if (file == radio->owner)
  950. radio->owner = NULL;
  951. out:
  952. return r;
  953. }
  954. static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
  955. size_t count, loff_t *ppos)
  956. {
  957. int r = 0;
  958. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  959. struct wl1273_core *core = radio->core;
  960. unsigned int block_count = 0;
  961. u16 val;
  962. dev_dbg(radio->dev, "%s\n", __func__);
  963. if (core->mode != WL1273_MODE_RX)
  964. return 0;
  965. if (radio->rds_users == 0) {
  966. dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
  967. return 0;
  968. }
  969. if (mutex_lock_interruptible(&core->lock))
  970. return -EINTR;
  971. /*
  972. * Multiple processes can open the device, but only
  973. * one at a time gets read access.
  974. */
  975. if (radio->owner && radio->owner != file) {
  976. r = -EBUSY;
  977. goto out;
  978. }
  979. radio->owner = file;
  980. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  981. if (r) {
  982. dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
  983. goto out;
  984. } else if (val == 0) {
  985. dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
  986. r = -ENODATA;
  987. goto out;
  988. }
  989. /* block if no new data available */
  990. while (radio->wr_index == radio->rd_index) {
  991. if (file->f_flags & O_NONBLOCK) {
  992. r = -EWOULDBLOCK;
  993. goto out;
  994. }
  995. dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
  996. if (wait_event_interruptible(radio->read_queue,
  997. radio->wr_index !=
  998. radio->rd_index) < 0) {
  999. r = -EINTR;
  1000. goto out;
  1001. }
  1002. }
  1003. /* calculate block count from byte count */
  1004. count /= RDS_BLOCK_SIZE;
  1005. /* copy RDS blocks from the internal buffer and to user buffer */
  1006. while (block_count < count) {
  1007. if (radio->rd_index == radio->wr_index)
  1008. break;
  1009. /* always transfer complete RDS blocks */
  1010. if (copy_to_user(buf, &radio->buffer[radio->rd_index],
  1011. RDS_BLOCK_SIZE))
  1012. break;
  1013. /* increment and wrap the read pointer */
  1014. radio->rd_index += RDS_BLOCK_SIZE;
  1015. if (radio->rd_index >= radio->buf_size)
  1016. radio->rd_index = 0;
  1017. /* increment counters */
  1018. block_count++;
  1019. buf += RDS_BLOCK_SIZE;
  1020. r += RDS_BLOCK_SIZE;
  1021. }
  1022. out:
  1023. dev_dbg(radio->dev, "%s: exit\n", __func__);
  1024. mutex_unlock(&core->lock);
  1025. return r;
  1026. }
  1027. static const struct v4l2_file_operations wl1273_fops = {
  1028. .owner = THIS_MODULE,
  1029. .read = wl1273_fm_fops_read,
  1030. .write = wl1273_fm_fops_write,
  1031. .poll = wl1273_fm_fops_poll,
  1032. .unlocked_ioctl = video_ioctl2,
  1033. .open = wl1273_fm_fops_open,
  1034. .release = wl1273_fm_fops_release,
  1035. };
  1036. static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
  1037. struct v4l2_capability *capability)
  1038. {
  1039. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1040. dev_dbg(radio->dev, "%s\n", __func__);
  1041. strscpy(capability->driver, WL1273_FM_DRIVER_NAME,
  1042. sizeof(capability->driver));
  1043. strscpy(capability->card, "Texas Instruments Wl1273 FM Radio",
  1044. sizeof(capability->card));
  1045. strscpy(capability->bus_info, radio->bus_type,
  1046. sizeof(capability->bus_info));
  1047. capability->device_caps = V4L2_CAP_HW_FREQ_SEEK |
  1048. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
  1049. V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
  1050. V4L2_CAP_RDS_OUTPUT;
  1051. capability->capabilities = capability->device_caps |
  1052. V4L2_CAP_DEVICE_CAPS;
  1053. return 0;
  1054. }
  1055. static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
  1056. unsigned int *i)
  1057. {
  1058. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1059. dev_dbg(radio->dev, "%s\n", __func__);
  1060. *i = 0;
  1061. return 0;
  1062. }
  1063. static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
  1064. unsigned int i)
  1065. {
  1066. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1067. dev_dbg(radio->dev, "%s\n", __func__);
  1068. if (i != 0)
  1069. return -EINVAL;
  1070. return 0;
  1071. }
  1072. /**
  1073. * wl1273_fm_set_tx_power() - Set the transmission power value.
  1074. * @radio: A pointer to the device struct.
  1075. * @power: The new power value.
  1076. */
  1077. static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
  1078. {
  1079. struct wl1273_core *core = radio->core;
  1080. int r;
  1081. if (core->mode == WL1273_MODE_OFF ||
  1082. core->mode == WL1273_MODE_SUSPENDED)
  1083. return -EPERM;
  1084. mutex_lock(&core->lock);
  1085. /* Convert the dBuV value to chip presentation */
  1086. r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
  1087. if (r)
  1088. goto out;
  1089. radio->tx_power = power;
  1090. out:
  1091. mutex_unlock(&core->lock);
  1092. return r;
  1093. }
  1094. #define WL1273_SPACING_50kHz 1
  1095. #define WL1273_SPACING_100kHz 2
  1096. #define WL1273_SPACING_200kHz 4
  1097. static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
  1098. unsigned int spacing)
  1099. {
  1100. struct wl1273_core *core = radio->core;
  1101. int r;
  1102. if (spacing == 0) {
  1103. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1104. WL1273_SPACING_100kHz);
  1105. radio->spacing = 100;
  1106. } else if (spacing - 50000 < 25000) {
  1107. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1108. WL1273_SPACING_50kHz);
  1109. radio->spacing = 50;
  1110. } else if (spacing - 100000 < 50000) {
  1111. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1112. WL1273_SPACING_100kHz);
  1113. radio->spacing = 100;
  1114. } else {
  1115. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1116. WL1273_SPACING_200kHz);
  1117. radio->spacing = 200;
  1118. }
  1119. return r;
  1120. }
  1121. static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1122. {
  1123. struct wl1273_device *radio = ctrl->priv;
  1124. struct wl1273_core *core = radio->core;
  1125. dev_dbg(radio->dev, "%s\n", __func__);
  1126. if (mutex_lock_interruptible(&core->lock))
  1127. return -EINTR;
  1128. switch (ctrl->id) {
  1129. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1130. ctrl->val = wl1273_fm_get_tx_ctune(radio);
  1131. break;
  1132. default:
  1133. dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
  1134. __func__, ctrl->id);
  1135. break;
  1136. }
  1137. mutex_unlock(&core->lock);
  1138. return 0;
  1139. }
  1140. #define WL1273_MUTE_SOFT_ENABLE (1 << 0)
  1141. #define WL1273_MUTE_AC (1 << 1)
  1142. #define WL1273_MUTE_HARD_LEFT (1 << 2)
  1143. #define WL1273_MUTE_HARD_RIGHT (1 << 3)
  1144. #define WL1273_MUTE_SOFT_FORCE (1 << 4)
  1145. static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
  1146. {
  1147. return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
  1148. }
  1149. static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
  1150. {
  1151. struct wl1273_device *radio = to_radio(ctrl);
  1152. struct wl1273_core *core = radio->core;
  1153. int r = 0;
  1154. dev_dbg(radio->dev, "%s\n", __func__);
  1155. switch (ctrl->id) {
  1156. case V4L2_CID_AUDIO_MUTE:
  1157. if (mutex_lock_interruptible(&core->lock))
  1158. return -EINTR;
  1159. if (core->mode == WL1273_MODE_RX && ctrl->val)
  1160. r = core->write(core,
  1161. WL1273_MUTE_STATUS_SET,
  1162. WL1273_MUTE_HARD_LEFT |
  1163. WL1273_MUTE_HARD_RIGHT);
  1164. else if (core->mode == WL1273_MODE_RX)
  1165. r = core->write(core,
  1166. WL1273_MUTE_STATUS_SET, 0x0);
  1167. else if (core->mode == WL1273_MODE_TX && ctrl->val)
  1168. r = core->write(core, WL1273_MUTE, 1);
  1169. else if (core->mode == WL1273_MODE_TX)
  1170. r = core->write(core, WL1273_MUTE, 0);
  1171. mutex_unlock(&core->lock);
  1172. break;
  1173. case V4L2_CID_AUDIO_VOLUME:
  1174. if (ctrl->val == 0)
  1175. r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
  1176. else
  1177. r = core->set_volume(core, core->volume);
  1178. break;
  1179. case V4L2_CID_TUNE_PREEMPHASIS:
  1180. r = wl1273_fm_set_preemphasis(radio, ctrl->val);
  1181. break;
  1182. case V4L2_CID_TUNE_POWER_LEVEL:
  1183. r = wl1273_fm_set_tx_power(radio, ctrl->val);
  1184. break;
  1185. default:
  1186. dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
  1187. __func__, ctrl->id);
  1188. break;
  1189. }
  1190. dev_dbg(radio->dev, "%s\n", __func__);
  1191. return r;
  1192. }
  1193. static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
  1194. struct v4l2_audio *audio)
  1195. {
  1196. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1197. dev_dbg(radio->dev, "%s\n", __func__);
  1198. if (audio->index > 1)
  1199. return -EINVAL;
  1200. strscpy(audio->name, "Radio", sizeof(audio->name));
  1201. audio->capability = V4L2_AUDCAP_STEREO;
  1202. return 0;
  1203. }
  1204. static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
  1205. const struct v4l2_audio *audio)
  1206. {
  1207. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1208. dev_dbg(radio->dev, "%s\n", __func__);
  1209. if (audio->index != 0)
  1210. return -EINVAL;
  1211. return 0;
  1212. }
  1213. #define WL1273_RDS_NOT_SYNCHRONIZED 0
  1214. #define WL1273_RDS_SYNCHRONIZED 1
  1215. static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
  1216. struct v4l2_tuner *tuner)
  1217. {
  1218. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1219. struct wl1273_core *core = radio->core;
  1220. u16 val;
  1221. int r;
  1222. dev_dbg(radio->dev, "%s\n", __func__);
  1223. if (tuner->index > 0)
  1224. return -EINVAL;
  1225. strscpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
  1226. tuner->type = V4L2_TUNER_RADIO;
  1227. tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
  1228. tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
  1229. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
  1230. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO |
  1231. V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP;
  1232. if (radio->stereo)
  1233. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  1234. else
  1235. tuner->audmode = V4L2_TUNER_MODE_MONO;
  1236. if (core->mode != WL1273_MODE_RX)
  1237. return 0;
  1238. if (mutex_lock_interruptible(&core->lock))
  1239. return -EINTR;
  1240. r = core->read(core, WL1273_STEREO_GET, &val);
  1241. if (r)
  1242. goto out;
  1243. if (val == 1)
  1244. tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1245. else
  1246. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  1247. r = core->read(core, WL1273_RSSI_LVL_GET, &val);
  1248. if (r)
  1249. goto out;
  1250. tuner->signal = (s16) val;
  1251. dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
  1252. tuner->afc = 0;
  1253. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  1254. if (r)
  1255. goto out;
  1256. if (val == WL1273_RDS_SYNCHRONIZED)
  1257. tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
  1258. out:
  1259. mutex_unlock(&core->lock);
  1260. return r;
  1261. }
  1262. static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
  1263. const struct v4l2_tuner *tuner)
  1264. {
  1265. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1266. struct wl1273_core *core = radio->core;
  1267. int r = 0;
  1268. dev_dbg(radio->dev, "%s\n", __func__);
  1269. dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
  1270. dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
  1271. dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
  1272. dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
  1273. dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
  1274. dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
  1275. if (tuner->index > 0)
  1276. return -EINVAL;
  1277. if (mutex_lock_interruptible(&core->lock))
  1278. return -EINTR;
  1279. r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
  1280. if (r)
  1281. goto out;
  1282. if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
  1283. r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
  1284. else
  1285. r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
  1286. if (r)
  1287. dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
  1288. if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
  1289. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
  1290. if (r < 0) {
  1291. dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
  1292. __func__, r);
  1293. goto out;
  1294. }
  1295. radio->stereo = false;
  1296. } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
  1297. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
  1298. if (r < 0) {
  1299. dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
  1300. __func__, r);
  1301. goto out;
  1302. }
  1303. radio->stereo = true;
  1304. } else {
  1305. dev_err(radio->dev, "%s: tuner->audmode: %d\n",
  1306. __func__, tuner->audmode);
  1307. r = -EINVAL;
  1308. goto out;
  1309. }
  1310. out:
  1311. mutex_unlock(&core->lock);
  1312. return r;
  1313. }
  1314. static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
  1315. struct v4l2_frequency *freq)
  1316. {
  1317. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1318. struct wl1273_core *core = radio->core;
  1319. dev_dbg(radio->dev, "%s\n", __func__);
  1320. if (mutex_lock_interruptible(&core->lock))
  1321. return -EINTR;
  1322. freq->type = V4L2_TUNER_RADIO;
  1323. freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
  1324. mutex_unlock(&core->lock);
  1325. return 0;
  1326. }
  1327. static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
  1328. const struct v4l2_frequency *freq)
  1329. {
  1330. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1331. struct wl1273_core *core = radio->core;
  1332. int r;
  1333. dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
  1334. if (freq->type != V4L2_TUNER_RADIO) {
  1335. dev_dbg(radio->dev,
  1336. "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
  1337. return -EINVAL;
  1338. }
  1339. if (mutex_lock_interruptible(&core->lock))
  1340. return -EINTR;
  1341. if (core->mode == WL1273_MODE_RX) {
  1342. dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
  1343. r = wl1273_fm_set_rx_freq(radio,
  1344. WL1273_INV_FREQ(freq->frequency));
  1345. if (r)
  1346. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1347. ": set frequency failed with %d\n", r);
  1348. } else {
  1349. r = wl1273_fm_set_tx_freq(radio,
  1350. WL1273_INV_FREQ(freq->frequency));
  1351. if (r)
  1352. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1353. ": set frequency failed with %d\n", r);
  1354. }
  1355. mutex_unlock(&core->lock);
  1356. dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
  1357. return r;
  1358. }
  1359. #define WL1273_DEFAULT_SEEK_LEVEL 7
  1360. static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
  1361. const struct v4l2_hw_freq_seek *seek)
  1362. {
  1363. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1364. struct wl1273_core *core = radio->core;
  1365. int r;
  1366. dev_dbg(radio->dev, "%s\n", __func__);
  1367. if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
  1368. return -EINVAL;
  1369. if (file->f_flags & O_NONBLOCK)
  1370. return -EWOULDBLOCK;
  1371. if (mutex_lock_interruptible(&core->lock))
  1372. return -EINTR;
  1373. r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
  1374. if (r)
  1375. goto out;
  1376. r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
  1377. if (r)
  1378. dev_warn(radio->dev, "HW seek failed: %d\n", r);
  1379. r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
  1380. WL1273_DEFAULT_SEEK_LEVEL);
  1381. if (r)
  1382. dev_warn(radio->dev, "HW seek failed: %d\n", r);
  1383. out:
  1384. mutex_unlock(&core->lock);
  1385. return r;
  1386. }
  1387. static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
  1388. const struct v4l2_modulator *modulator)
  1389. {
  1390. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1391. struct wl1273_core *core = radio->core;
  1392. int r = 0;
  1393. dev_dbg(radio->dev, "%s\n", __func__);
  1394. if (modulator->index > 0)
  1395. return -EINVAL;
  1396. if (mutex_lock_interruptible(&core->lock))
  1397. return -EINTR;
  1398. r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
  1399. if (r)
  1400. goto out;
  1401. if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
  1402. r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
  1403. else
  1404. r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
  1405. if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
  1406. r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
  1407. else
  1408. r = core->write(core, WL1273_MONO_SET,
  1409. WL1273_RX_STEREO);
  1410. if (r < 0)
  1411. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1412. "MONO_SET fails: %d\n", r);
  1413. out:
  1414. mutex_unlock(&core->lock);
  1415. return r;
  1416. }
  1417. static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
  1418. struct v4l2_modulator *modulator)
  1419. {
  1420. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1421. struct wl1273_core *core = radio->core;
  1422. u16 val;
  1423. int r;
  1424. dev_dbg(radio->dev, "%s\n", __func__);
  1425. strscpy(modulator->name, WL1273_FM_DRIVER_NAME,
  1426. sizeof(modulator->name));
  1427. modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
  1428. modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
  1429. modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
  1430. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
  1431. if (core->mode != WL1273_MODE_TX)
  1432. return 0;
  1433. if (mutex_lock_interruptible(&core->lock))
  1434. return -EINTR;
  1435. r = core->read(core, WL1273_MONO_SET, &val);
  1436. if (r)
  1437. goto out;
  1438. if (val == WL1273_TX_STEREO)
  1439. modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
  1440. else
  1441. modulator->txsubchans = V4L2_TUNER_SUB_MONO;
  1442. if (radio->rds_on)
  1443. modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
  1444. out:
  1445. mutex_unlock(&core->lock);
  1446. return 0;
  1447. }
  1448. static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
  1449. {
  1450. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1451. struct wl1273_core *core = radio->core;
  1452. struct device *dev = radio->dev;
  1453. u16 val;
  1454. int r;
  1455. dev_info(dev, DRIVER_DESC);
  1456. if (core->mode == WL1273_MODE_OFF) {
  1457. dev_info(dev, "Mode: Off\n");
  1458. return 0;
  1459. }
  1460. if (core->mode == WL1273_MODE_SUSPENDED) {
  1461. dev_info(dev, "Mode: Suspended\n");
  1462. return 0;
  1463. }
  1464. r = core->read(core, WL1273_ASIC_ID_GET, &val);
  1465. if (r)
  1466. dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
  1467. else
  1468. dev_info(dev, "ASIC_ID: 0x%04x\n", val);
  1469. r = core->read(core, WL1273_ASIC_VER_GET, &val);
  1470. if (r)
  1471. dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
  1472. else
  1473. dev_info(dev, "ASIC Version: 0x%04x\n", val);
  1474. r = core->read(core, WL1273_FIRM_VER_GET, &val);
  1475. if (r)
  1476. dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
  1477. else
  1478. dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
  1479. r = core->read(core, WL1273_BAND_SET, &val);
  1480. if (r)
  1481. dev_err(dev, "%s: Get BAND fails.\n", __func__);
  1482. else
  1483. dev_info(dev, "BAND: %d\n", val);
  1484. if (core->mode == WL1273_MODE_TX) {
  1485. r = core->read(core, WL1273_PUPD_SET, &val);
  1486. if (r)
  1487. dev_err(dev, "%s: Get PUPD fails.\n", __func__);
  1488. else
  1489. dev_info(dev, "PUPD: 0x%04x\n", val);
  1490. r = core->read(core, WL1273_CHANL_SET, &val);
  1491. if (r)
  1492. dev_err(dev, "%s: Get CHANL fails.\n", __func__);
  1493. else
  1494. dev_info(dev, "Tx frequency: %dkHz\n", val*10);
  1495. } else if (core->mode == WL1273_MODE_RX) {
  1496. int bf = radio->rangelow;
  1497. r = core->read(core, WL1273_FREQ_SET, &val);
  1498. if (r)
  1499. dev_err(dev, "%s: Get FREQ fails.\n", __func__);
  1500. else
  1501. dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
  1502. r = core->read(core, WL1273_MOST_MODE_SET, &val);
  1503. if (r)
  1504. dev_err(dev, "%s: Get MOST_MODE fails.\n",
  1505. __func__);
  1506. else if (val == 0)
  1507. dev_info(dev, "MOST_MODE: Stereo according to blend\n");
  1508. else if (val == 1)
  1509. dev_info(dev, "MOST_MODE: Force mono output\n");
  1510. else
  1511. dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
  1512. r = core->read(core, WL1273_MOST_BLEND_SET, &val);
  1513. if (r)
  1514. dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
  1515. else if (val == 0)
  1516. dev_info(dev,
  1517. "MOST_BLEND: Switched blend & hysteresis.\n");
  1518. else if (val == 1)
  1519. dev_info(dev, "MOST_BLEND: Soft blend.\n");
  1520. else
  1521. dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
  1522. r = core->read(core, WL1273_STEREO_GET, &val);
  1523. if (r)
  1524. dev_err(dev, "%s: Get STEREO fails.\n", __func__);
  1525. else if (val == 0)
  1526. dev_info(dev, "STEREO: Not detected\n");
  1527. else if (val == 1)
  1528. dev_info(dev, "STEREO: Detected\n");
  1529. else
  1530. dev_info(dev, "STEREO: Unexpected value: %d\n", val);
  1531. r = core->read(core, WL1273_RSSI_LVL_GET, &val);
  1532. if (r)
  1533. dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
  1534. else
  1535. dev_info(dev, "RX signal strength: %d\n", (s16) val);
  1536. r = core->read(core, WL1273_POWER_SET, &val);
  1537. if (r)
  1538. dev_err(dev, "%s: Get POWER fails.\n", __func__);
  1539. else
  1540. dev_info(dev, "POWER: 0x%04x\n", val);
  1541. r = core->read(core, WL1273_INT_MASK_SET, &val);
  1542. if (r)
  1543. dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
  1544. else
  1545. dev_info(dev, "INT_MASK: 0x%04x\n", val);
  1546. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  1547. if (r)
  1548. dev_err(dev, "%s: Get RDS_SYNC fails.\n",
  1549. __func__);
  1550. else if (val == 0)
  1551. dev_info(dev, "RDS_SYNC: Not synchronized\n");
  1552. else if (val == 1)
  1553. dev_info(dev, "RDS_SYNC: Synchronized\n");
  1554. else
  1555. dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
  1556. r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
  1557. if (r)
  1558. dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
  1559. __func__);
  1560. else
  1561. dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
  1562. r = core->read(core, WL1273_VOLUME_SET, &val);
  1563. if (r)
  1564. dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
  1565. else
  1566. dev_info(dev, "VOLUME: 0x%04x\n", val);
  1567. }
  1568. return 0;
  1569. }
  1570. static void wl1273_vdev_release(struct video_device *dev)
  1571. {
  1572. }
  1573. static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
  1574. .s_ctrl = wl1273_fm_vidioc_s_ctrl,
  1575. .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
  1576. };
  1577. static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
  1578. .vidioc_querycap = wl1273_fm_vidioc_querycap,
  1579. .vidioc_g_input = wl1273_fm_vidioc_g_input,
  1580. .vidioc_s_input = wl1273_fm_vidioc_s_input,
  1581. .vidioc_g_audio = wl1273_fm_vidioc_g_audio,
  1582. .vidioc_s_audio = wl1273_fm_vidioc_s_audio,
  1583. .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
  1584. .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
  1585. .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
  1586. .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
  1587. .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
  1588. .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
  1589. .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
  1590. .vidioc_log_status = wl1273_fm_vidioc_log_status,
  1591. };
  1592. static const struct video_device wl1273_viddev_template = {
  1593. .fops = &wl1273_fops,
  1594. .ioctl_ops = &wl1273_ioctl_ops,
  1595. .name = WL1273_FM_DRIVER_NAME,
  1596. .release = wl1273_vdev_release,
  1597. .vfl_dir = VFL_DIR_TX,
  1598. };
  1599. static int wl1273_fm_radio_remove(struct platform_device *pdev)
  1600. {
  1601. struct wl1273_device *radio = platform_get_drvdata(pdev);
  1602. struct wl1273_core *core = radio->core;
  1603. dev_info(&pdev->dev, "%s.\n", __func__);
  1604. free_irq(core->client->irq, radio);
  1605. core->pdata->free_resources();
  1606. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  1607. video_unregister_device(&radio->videodev);
  1608. v4l2_device_unregister(&radio->v4l2dev);
  1609. return 0;
  1610. }
  1611. static int wl1273_fm_radio_probe(struct platform_device *pdev)
  1612. {
  1613. struct wl1273_core **core = pdev->dev.platform_data;
  1614. struct wl1273_device *radio;
  1615. struct v4l2_ctrl *ctrl;
  1616. int r = 0;
  1617. pr_debug("%s\n", __func__);
  1618. if (!core) {
  1619. dev_err(&pdev->dev, "No platform data.\n");
  1620. r = -EINVAL;
  1621. goto pdata_err;
  1622. }
  1623. radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
  1624. if (!radio) {
  1625. r = -ENOMEM;
  1626. goto pdata_err;
  1627. }
  1628. /* RDS buffer allocation */
  1629. radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
  1630. radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL);
  1631. if (!radio->buffer) {
  1632. pr_err("Cannot allocate memory for RDS buffer.\n");
  1633. r = -ENOMEM;
  1634. goto pdata_err;
  1635. }
  1636. radio->core = *core;
  1637. radio->irq_flags = WL1273_IRQ_MASK;
  1638. radio->dev = &radio->core->client->dev;
  1639. radio->rds_on = false;
  1640. radio->core->mode = WL1273_MODE_OFF;
  1641. radio->tx_power = 118;
  1642. radio->core->audio_mode = WL1273_AUDIO_ANALOG;
  1643. radio->band = WL1273_BAND_OTHER;
  1644. radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
  1645. radio->core->channel_number = 2;
  1646. radio->core->volume = WL1273_DEFAULT_VOLUME;
  1647. radio->rx_frequency = WL1273_BAND_OTHER_LOW;
  1648. radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
  1649. radio->rangelow = WL1273_BAND_OTHER_LOW;
  1650. radio->rangehigh = WL1273_BAND_OTHER_HIGH;
  1651. radio->stereo = true;
  1652. radio->bus_type = "I2C";
  1653. if (radio->core->pdata->request_resources) {
  1654. r = radio->core->pdata->request_resources(radio->core->client);
  1655. if (r) {
  1656. dev_err(radio->dev, WL1273_FM_DRIVER_NAME
  1657. ": Cannot get platform data\n");
  1658. goto pdata_err;
  1659. }
  1660. dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
  1661. r = request_threaded_irq(radio->core->client->irq, NULL,
  1662. wl1273_fm_irq_thread_handler,
  1663. IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
  1664. "wl1273-fm", radio);
  1665. if (r < 0) {
  1666. dev_err(radio->dev, WL1273_FM_DRIVER_NAME
  1667. ": Unable to register IRQ handler: %d\n", r);
  1668. goto err_request_irq;
  1669. }
  1670. } else {
  1671. dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ not configured");
  1672. r = -EINVAL;
  1673. goto pdata_err;
  1674. }
  1675. init_completion(&radio->busy);
  1676. init_waitqueue_head(&radio->read_queue);
  1677. radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL);
  1678. if (!radio->write_buf) {
  1679. r = -ENOMEM;
  1680. goto write_buf_err;
  1681. }
  1682. radio->dev = &pdev->dev;
  1683. radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
  1684. radio->rds_users = 0;
  1685. r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
  1686. if (r) {
  1687. dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
  1688. goto write_buf_err;
  1689. }
  1690. /* V4L2 configuration */
  1691. radio->videodev = wl1273_viddev_template;
  1692. radio->videodev.v4l2_dev = &radio->v4l2dev;
  1693. v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
  1694. /* add in ascending ID order */
  1695. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1696. V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
  1697. WL1273_DEFAULT_VOLUME);
  1698. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1699. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  1700. v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1701. V4L2_CID_TUNE_PREEMPHASIS,
  1702. V4L2_PREEMPHASIS_75_uS, 0x03,
  1703. V4L2_PREEMPHASIS_50_uS);
  1704. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1705. V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
  1706. ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1707. V4L2_CID_TUNE_ANTENNA_CAPACITOR,
  1708. 0, 255, 1, 255);
  1709. if (ctrl)
  1710. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1711. if (radio->ctrl_handler.error) {
  1712. r = radio->ctrl_handler.error;
  1713. dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
  1714. goto handler_init_err;
  1715. }
  1716. video_set_drvdata(&radio->videodev, radio);
  1717. platform_set_drvdata(pdev, radio);
  1718. /* register video device */
  1719. r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
  1720. if (r) {
  1721. dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
  1722. ": Could not register video device\n");
  1723. goto handler_init_err;
  1724. }
  1725. return 0;
  1726. handler_init_err:
  1727. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  1728. v4l2_device_unregister(&radio->v4l2dev);
  1729. write_buf_err:
  1730. free_irq(radio->core->client->irq, radio);
  1731. err_request_irq:
  1732. radio->core->pdata->free_resources();
  1733. pdata_err:
  1734. return r;
  1735. }
  1736. static struct platform_driver wl1273_fm_radio_driver = {
  1737. .probe = wl1273_fm_radio_probe,
  1738. .remove = wl1273_fm_radio_remove,
  1739. .driver = {
  1740. .name = "wl1273_fm_radio",
  1741. },
  1742. };
  1743. module_platform_driver(wl1273_fm_radio_driver);
  1744. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  1745. MODULE_DESCRIPTION(DRIVER_DESC);
  1746. MODULE_LICENSE("GPL");
  1747. MODULE_ALIAS("platform:wl1273_fm_radio");