radio-wl1273.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  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. return 0;
  415. ptr = (__u8 *) fw_p->data;
  416. packet_num = ptr[0];
  417. dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
  418. r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
  419. if (r) {
  420. dev_err(dev, "FW upload error: %d\n", r);
  421. goto out;
  422. }
  423. /* ignore possible error here */
  424. core->write(core, WL1273_RESET, 0);
  425. dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
  426. out:
  427. release_firmware(fw_p);
  428. return r;
  429. }
  430. static int wl1273_fm_stop(struct wl1273_device *radio)
  431. {
  432. struct wl1273_core *core = radio->core;
  433. if (core->mode == WL1273_MODE_RX) {
  434. int r = core->write(core, WL1273_POWER_SET,
  435. WL1273_POWER_SET_OFF);
  436. if (r)
  437. dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
  438. __func__, r);
  439. } else if (core->mode == WL1273_MODE_TX) {
  440. int r = core->write(core, WL1273_PUPD_SET,
  441. WL1273_PUPD_SET_OFF);
  442. if (r)
  443. dev_err(radio->dev,
  444. "%s: PUPD_SET fails: %d\n", __func__, r);
  445. }
  446. if (core->pdata->disable) {
  447. core->pdata->disable();
  448. dev_dbg(radio->dev, "Back to reset\n");
  449. }
  450. return 0;
  451. }
  452. static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
  453. {
  454. struct wl1273_core *core = radio->core;
  455. struct wl1273_fm_platform_data *pdata = core->pdata;
  456. struct device *dev = radio->dev;
  457. int r = -EINVAL;
  458. if (pdata->enable && core->mode == WL1273_MODE_OFF) {
  459. dev_dbg(radio->dev, "Out of reset\n");
  460. pdata->enable();
  461. msleep(250);
  462. }
  463. if (new_mode == WL1273_MODE_RX) {
  464. u16 val = WL1273_POWER_SET_FM;
  465. if (radio->rds_on)
  466. val |= WL1273_POWER_SET_RDS;
  467. /* If this fails try again */
  468. r = core->write(core, WL1273_POWER_SET, val);
  469. if (r) {
  470. msleep(100);
  471. r = core->write(core, WL1273_POWER_SET, val);
  472. if (r) {
  473. dev_err(dev, "%s: POWER_SET fails\n", __func__);
  474. goto fail;
  475. }
  476. }
  477. /* rds buffer configuration */
  478. radio->wr_index = 0;
  479. radio->rd_index = 0;
  480. } else if (new_mode == WL1273_MODE_TX) {
  481. /* If this fails try again once */
  482. r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
  483. if (r) {
  484. msleep(100);
  485. r = core->write(core, WL1273_PUPD_SET,
  486. WL1273_PUPD_SET_ON);
  487. if (r) {
  488. dev_err(dev, "%s: PUPD_SET fails\n", __func__);
  489. goto fail;
  490. }
  491. }
  492. if (radio->rds_on) {
  493. r = core->write(core, WL1273_RDS_DATA_ENB, 1);
  494. if (r) {
  495. dev_err(dev, "%s: RDS_DATA_ENB ON fails\n",
  496. __func__);
  497. goto fail;
  498. }
  499. } else {
  500. r = core->write(core, WL1273_RDS_DATA_ENB, 0);
  501. if (r) {
  502. dev_err(dev, "%s: RDS_DATA_ENB OFF fails\n",
  503. __func__);
  504. goto fail;
  505. }
  506. }
  507. } else {
  508. dev_warn(dev, "%s: Illegal mode.\n", __func__);
  509. }
  510. if (core->mode == WL1273_MODE_OFF) {
  511. r = wl1273_fm_upload_firmware_patch(radio);
  512. if (r)
  513. dev_warn(dev, "Firmware upload failed.\n");
  514. /*
  515. * Sometimes the chip is in a wrong power state at this point.
  516. * So we set the power once again.
  517. */
  518. if (new_mode == WL1273_MODE_RX) {
  519. u16 val = WL1273_POWER_SET_FM;
  520. if (radio->rds_on)
  521. val |= WL1273_POWER_SET_RDS;
  522. r = core->write(core, WL1273_POWER_SET, val);
  523. if (r) {
  524. dev_err(dev, "%s: POWER_SET fails\n", __func__);
  525. goto fail;
  526. }
  527. } else if (new_mode == WL1273_MODE_TX) {
  528. r = core->write(core, WL1273_PUPD_SET,
  529. WL1273_PUPD_SET_ON);
  530. if (r) {
  531. dev_err(dev, "%s: PUPD_SET fails\n", __func__);
  532. goto fail;
  533. }
  534. }
  535. }
  536. return 0;
  537. fail:
  538. if (pdata->disable)
  539. pdata->disable();
  540. dev_dbg(dev, "%s: return: %d\n", __func__, r);
  541. return r;
  542. }
  543. static int wl1273_fm_suspend(struct wl1273_device *radio)
  544. {
  545. struct wl1273_core *core = radio->core;
  546. int r;
  547. /* Cannot go from OFF to SUSPENDED */
  548. if (core->mode == WL1273_MODE_RX)
  549. r = core->write(core, WL1273_POWER_SET,
  550. WL1273_POWER_SET_RETENTION);
  551. else if (core->mode == WL1273_MODE_TX)
  552. r = core->write(core, WL1273_PUPD_SET,
  553. WL1273_PUPD_SET_RETENTION);
  554. else
  555. r = -EINVAL;
  556. if (r) {
  557. dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
  558. goto out;
  559. }
  560. out:
  561. return r;
  562. }
  563. static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
  564. {
  565. struct wl1273_core *core = radio->core;
  566. struct device *dev = radio->dev;
  567. int old_mode;
  568. int r;
  569. dev_dbg(dev, "%s\n", __func__);
  570. dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
  571. old_mode = core->mode;
  572. if (mode & radio->forbidden) {
  573. r = -EPERM;
  574. goto out;
  575. }
  576. switch (mode) {
  577. case WL1273_MODE_RX:
  578. case WL1273_MODE_TX:
  579. r = wl1273_fm_start(radio, mode);
  580. if (r) {
  581. dev_err(dev, "%s: Cannot start.\n", __func__);
  582. wl1273_fm_stop(radio);
  583. goto out;
  584. }
  585. core->mode = mode;
  586. r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
  587. if (r) {
  588. dev_err(dev, "INT_MASK_SET fails.\n");
  589. goto out;
  590. }
  591. /* remember previous settings */
  592. if (mode == WL1273_MODE_RX) {
  593. r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
  594. if (r) {
  595. dev_err(dev, "set freq fails: %d.\n", r);
  596. goto out;
  597. }
  598. r = core->set_volume(core, core->volume);
  599. if (r) {
  600. dev_err(dev, "set volume fails: %d.\n", r);
  601. goto out;
  602. }
  603. dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
  604. core->volume);
  605. } else {
  606. r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
  607. if (r) {
  608. dev_err(dev, "set freq fails: %d.\n", r);
  609. goto out;
  610. }
  611. }
  612. dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
  613. r = core->set_audio(core, core->audio_mode);
  614. if (r)
  615. dev_err(dev, "Cannot set audio mode.\n");
  616. break;
  617. case WL1273_MODE_OFF:
  618. r = wl1273_fm_stop(radio);
  619. if (r)
  620. dev_err(dev, "%s: Off fails: %d\n", __func__, r);
  621. else
  622. core->mode = WL1273_MODE_OFF;
  623. break;
  624. case WL1273_MODE_SUSPENDED:
  625. r = wl1273_fm_suspend(radio);
  626. if (r)
  627. dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
  628. else
  629. core->mode = WL1273_MODE_SUSPENDED;
  630. break;
  631. default:
  632. dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
  633. r = -EINVAL;
  634. break;
  635. }
  636. out:
  637. if (r)
  638. core->mode = old_mode;
  639. return r;
  640. }
  641. static int wl1273_fm_set_seek(struct wl1273_device *radio,
  642. unsigned int wrap_around,
  643. unsigned int seek_upward,
  644. int level)
  645. {
  646. struct wl1273_core *core = radio->core;
  647. int r = 0;
  648. unsigned int dir = (seek_upward == 0) ? 0 : 1;
  649. unsigned int f;
  650. f = radio->rx_frequency;
  651. dev_dbg(radio->dev, "rx_frequency: %d\n", f);
  652. if (dir && f + radio->spacing <= radio->rangehigh)
  653. r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
  654. else if (dir && wrap_around)
  655. r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
  656. else if (f - radio->spacing >= radio->rangelow)
  657. r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
  658. else if (wrap_around)
  659. r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
  660. if (r)
  661. goto out;
  662. if (level < SCHAR_MIN || level > SCHAR_MAX)
  663. return -EINVAL;
  664. reinit_completion(&radio->busy);
  665. dev_dbg(radio->dev, "%s: BUSY\n", __func__);
  666. r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
  667. if (r)
  668. goto out;
  669. dev_dbg(radio->dev, "%s\n", __func__);
  670. r = core->write(core, WL1273_SEARCH_LVL_SET, level);
  671. if (r)
  672. goto out;
  673. r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
  674. if (r)
  675. goto out;
  676. r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
  677. if (r)
  678. goto out;
  679. /* wait for the FR IRQ */
  680. wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
  681. if (!(radio->irq_received & WL1273_BL_EVENT)) {
  682. r = -ETIMEDOUT;
  683. goto out;
  684. }
  685. radio->irq_received &= ~WL1273_BL_EVENT;
  686. if (!wrap_around)
  687. goto out;
  688. /* Wrap around */
  689. dev_dbg(radio->dev, "Wrap around in HW seek.\n");
  690. if (seek_upward)
  691. f = radio->rangelow;
  692. else
  693. f = radio->rangehigh;
  694. r = wl1273_fm_set_rx_freq(radio, f);
  695. if (r)
  696. goto out;
  697. reinit_completion(&radio->busy);
  698. dev_dbg(radio->dev, "%s: BUSY\n", __func__);
  699. r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
  700. if (r)
  701. goto out;
  702. /* wait for the FR IRQ */
  703. if (!wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)))
  704. r = -ETIMEDOUT;
  705. out:
  706. dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
  707. return r;
  708. }
  709. /**
  710. * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
  711. * @radio: A pointer to the device struct.
  712. */
  713. static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
  714. {
  715. struct wl1273_core *core = radio->core;
  716. struct device *dev = radio->dev;
  717. u16 val;
  718. int r;
  719. if (core->mode == WL1273_MODE_OFF ||
  720. core->mode == WL1273_MODE_SUSPENDED)
  721. return -EPERM;
  722. r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
  723. if (r) {
  724. dev_err(dev, "%s: read error: %d\n", __func__, r);
  725. goto out;
  726. }
  727. out:
  728. return val;
  729. }
  730. /**
  731. * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
  732. * @radio: A pointer to the device struct.
  733. * @preemphasis: The new pre-amphasis value.
  734. *
  735. * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
  736. * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
  737. */
  738. static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
  739. unsigned int preemphasis)
  740. {
  741. struct wl1273_core *core = radio->core;
  742. int r;
  743. u16 em;
  744. if (core->mode == WL1273_MODE_OFF ||
  745. core->mode == WL1273_MODE_SUSPENDED)
  746. return -EPERM;
  747. mutex_lock(&core->lock);
  748. switch (preemphasis) {
  749. case V4L2_PREEMPHASIS_DISABLED:
  750. em = 1;
  751. break;
  752. case V4L2_PREEMPHASIS_50_uS:
  753. em = 0;
  754. break;
  755. case V4L2_PREEMPHASIS_75_uS:
  756. em = 2;
  757. break;
  758. default:
  759. r = -EINVAL;
  760. goto out;
  761. }
  762. r = core->write(core, WL1273_PREMPH_SET, em);
  763. if (r)
  764. goto out;
  765. radio->preemphasis = preemphasis;
  766. out:
  767. mutex_unlock(&core->lock);
  768. return r;
  769. }
  770. static int wl1273_fm_rds_on(struct wl1273_device *radio)
  771. {
  772. struct wl1273_core *core = radio->core;
  773. int r;
  774. dev_dbg(radio->dev, "%s\n", __func__);
  775. if (radio->rds_on)
  776. return 0;
  777. r = core->write(core, WL1273_POWER_SET,
  778. WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
  779. if (r)
  780. goto out;
  781. r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
  782. if (r)
  783. dev_err(radio->dev, "set freq fails: %d.\n", r);
  784. out:
  785. return r;
  786. }
  787. static int wl1273_fm_rds_off(struct wl1273_device *radio)
  788. {
  789. struct wl1273_core *core = radio->core;
  790. int r;
  791. if (!radio->rds_on)
  792. return 0;
  793. radio->irq_flags &= ~WL1273_RDS_EVENT;
  794. r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
  795. if (r)
  796. goto out;
  797. /* Service pending read */
  798. wake_up_interruptible(&radio->read_queue);
  799. dev_dbg(radio->dev, "%s\n", __func__);
  800. r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
  801. if (r)
  802. goto out;
  803. r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
  804. if (r)
  805. dev_err(radio->dev, "set freq fails: %d.\n", r);
  806. out:
  807. dev_dbg(radio->dev, "%s: exiting...\n", __func__);
  808. return r;
  809. }
  810. static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
  811. {
  812. int r = 0;
  813. struct wl1273_core *core = radio->core;
  814. if (core->mode == WL1273_MODE_OFF ||
  815. core->mode == WL1273_MODE_SUSPENDED)
  816. return -EPERM;
  817. if (new_mode == WL1273_RDS_RESET) {
  818. r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
  819. return r;
  820. }
  821. if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
  822. r = core->write(core, WL1273_RDS_DATA_ENB, 0);
  823. } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
  824. r = core->write(core, WL1273_RDS_DATA_ENB, 1);
  825. } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
  826. r = wl1273_fm_rds_off(radio);
  827. } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
  828. r = wl1273_fm_rds_on(radio);
  829. } else {
  830. dev_err(radio->dev, "%s: Unknown mode: %d\n",
  831. __func__, new_mode);
  832. r = -EINVAL;
  833. }
  834. if (!r)
  835. radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
  836. return r;
  837. }
  838. static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
  839. size_t count, loff_t *ppos)
  840. {
  841. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  842. struct wl1273_core *core = radio->core;
  843. u16 val;
  844. int r;
  845. dev_dbg(radio->dev, "%s\n", __func__);
  846. if (core->mode != WL1273_MODE_TX)
  847. return count;
  848. if (radio->rds_users == 0) {
  849. dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
  850. return 0;
  851. }
  852. if (mutex_lock_interruptible(&core->lock))
  853. return -EINTR;
  854. /*
  855. * Multiple processes can open the device, but only
  856. * one gets to write to it.
  857. */
  858. if (radio->owner && radio->owner != file) {
  859. r = -EBUSY;
  860. goto out;
  861. }
  862. radio->owner = file;
  863. /* Manual Mode */
  864. if (count > 255)
  865. val = 255;
  866. else
  867. val = count;
  868. core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
  869. if (copy_from_user(radio->write_buf + 1, buf, val)) {
  870. r = -EFAULT;
  871. goto out;
  872. }
  873. dev_dbg(radio->dev, "Count: %d\n", val);
  874. dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
  875. radio->write_buf[0] = WL1273_RDS_DATA_SET;
  876. core->write_data(core, radio->write_buf, val + 1);
  877. r = val;
  878. out:
  879. mutex_unlock(&core->lock);
  880. return r;
  881. }
  882. static __poll_t wl1273_fm_fops_poll(struct file *file,
  883. struct poll_table_struct *pts)
  884. {
  885. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  886. struct wl1273_core *core = radio->core;
  887. if (radio->owner && radio->owner != file)
  888. return EPOLLERR;
  889. radio->owner = file;
  890. if (core->mode == WL1273_MODE_RX) {
  891. poll_wait(file, &radio->read_queue, pts);
  892. if (radio->rd_index != radio->wr_index)
  893. return EPOLLIN | EPOLLRDNORM;
  894. } else if (core->mode == WL1273_MODE_TX) {
  895. return EPOLLOUT | EPOLLWRNORM;
  896. }
  897. return 0;
  898. }
  899. static int wl1273_fm_fops_open(struct file *file)
  900. {
  901. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  902. struct wl1273_core *core = radio->core;
  903. int r = 0;
  904. dev_dbg(radio->dev, "%s\n", __func__);
  905. if (core->mode == WL1273_MODE_RX && radio->rds_on &&
  906. !radio->rds_users) {
  907. dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
  908. if (mutex_lock_interruptible(&core->lock))
  909. return -EINTR;
  910. radio->irq_flags |= WL1273_RDS_EVENT;
  911. r = core->write(core, WL1273_INT_MASK_SET,
  912. radio->irq_flags);
  913. if (r) {
  914. mutex_unlock(&core->lock);
  915. goto out;
  916. }
  917. radio->rds_users++;
  918. mutex_unlock(&core->lock);
  919. }
  920. out:
  921. return r;
  922. }
  923. static int wl1273_fm_fops_release(struct file *file)
  924. {
  925. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  926. struct wl1273_core *core = radio->core;
  927. int r = 0;
  928. dev_dbg(radio->dev, "%s\n", __func__);
  929. if (radio->rds_users > 0) {
  930. radio->rds_users--;
  931. if (radio->rds_users == 0) {
  932. mutex_lock(&core->lock);
  933. radio->irq_flags &= ~WL1273_RDS_EVENT;
  934. if (core->mode == WL1273_MODE_RX) {
  935. r = core->write(core,
  936. WL1273_INT_MASK_SET,
  937. radio->irq_flags);
  938. if (r) {
  939. mutex_unlock(&core->lock);
  940. goto out;
  941. }
  942. }
  943. mutex_unlock(&core->lock);
  944. }
  945. }
  946. if (file == radio->owner)
  947. radio->owner = NULL;
  948. out:
  949. return r;
  950. }
  951. static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
  952. size_t count, loff_t *ppos)
  953. {
  954. int r = 0;
  955. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  956. struct wl1273_core *core = radio->core;
  957. unsigned int block_count = 0;
  958. u16 val;
  959. dev_dbg(radio->dev, "%s\n", __func__);
  960. if (core->mode != WL1273_MODE_RX)
  961. return 0;
  962. if (radio->rds_users == 0) {
  963. dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
  964. return 0;
  965. }
  966. if (mutex_lock_interruptible(&core->lock))
  967. return -EINTR;
  968. /*
  969. * Multiple processes can open the device, but only
  970. * one at a time gets read access.
  971. */
  972. if (radio->owner && radio->owner != file) {
  973. r = -EBUSY;
  974. goto out;
  975. }
  976. radio->owner = file;
  977. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  978. if (r) {
  979. dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
  980. goto out;
  981. } else if (val == 0) {
  982. dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
  983. r = -ENODATA;
  984. goto out;
  985. }
  986. /* block if no new data available */
  987. while (radio->wr_index == radio->rd_index) {
  988. if (file->f_flags & O_NONBLOCK) {
  989. r = -EWOULDBLOCK;
  990. goto out;
  991. }
  992. dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
  993. if (wait_event_interruptible(radio->read_queue,
  994. radio->wr_index !=
  995. radio->rd_index) < 0) {
  996. r = -EINTR;
  997. goto out;
  998. }
  999. }
  1000. /* calculate block count from byte count */
  1001. count /= RDS_BLOCK_SIZE;
  1002. /* copy RDS blocks from the internal buffer and to user buffer */
  1003. while (block_count < count) {
  1004. if (radio->rd_index == radio->wr_index)
  1005. break;
  1006. /* always transfer complete RDS blocks */
  1007. if (copy_to_user(buf, &radio->buffer[radio->rd_index],
  1008. RDS_BLOCK_SIZE))
  1009. break;
  1010. /* increment and wrap the read pointer */
  1011. radio->rd_index += RDS_BLOCK_SIZE;
  1012. if (radio->rd_index >= radio->buf_size)
  1013. radio->rd_index = 0;
  1014. /* increment counters */
  1015. block_count++;
  1016. buf += RDS_BLOCK_SIZE;
  1017. r += RDS_BLOCK_SIZE;
  1018. }
  1019. out:
  1020. dev_dbg(radio->dev, "%s: exit\n", __func__);
  1021. mutex_unlock(&core->lock);
  1022. return r;
  1023. }
  1024. static const struct v4l2_file_operations wl1273_fops = {
  1025. .owner = THIS_MODULE,
  1026. .read = wl1273_fm_fops_read,
  1027. .write = wl1273_fm_fops_write,
  1028. .poll = wl1273_fm_fops_poll,
  1029. .unlocked_ioctl = video_ioctl2,
  1030. .open = wl1273_fm_fops_open,
  1031. .release = wl1273_fm_fops_release,
  1032. };
  1033. static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
  1034. struct v4l2_capability *capability)
  1035. {
  1036. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1037. dev_dbg(radio->dev, "%s\n", __func__);
  1038. strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
  1039. sizeof(capability->driver));
  1040. strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
  1041. sizeof(capability->card));
  1042. strlcpy(capability->bus_info, radio->bus_type,
  1043. sizeof(capability->bus_info));
  1044. capability->device_caps = V4L2_CAP_HW_FREQ_SEEK |
  1045. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
  1046. V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
  1047. V4L2_CAP_RDS_OUTPUT;
  1048. capability->capabilities = capability->device_caps |
  1049. V4L2_CAP_DEVICE_CAPS;
  1050. return 0;
  1051. }
  1052. static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
  1053. unsigned int *i)
  1054. {
  1055. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1056. dev_dbg(radio->dev, "%s\n", __func__);
  1057. *i = 0;
  1058. return 0;
  1059. }
  1060. static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
  1061. unsigned int i)
  1062. {
  1063. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1064. dev_dbg(radio->dev, "%s\n", __func__);
  1065. if (i != 0)
  1066. return -EINVAL;
  1067. return 0;
  1068. }
  1069. /**
  1070. * wl1273_fm_set_tx_power() - Set the transmission power value.
  1071. * @radio: A pointer to the device struct.
  1072. * @power: The new power value.
  1073. */
  1074. static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
  1075. {
  1076. struct wl1273_core *core = radio->core;
  1077. int r;
  1078. if (core->mode == WL1273_MODE_OFF ||
  1079. core->mode == WL1273_MODE_SUSPENDED)
  1080. return -EPERM;
  1081. mutex_lock(&core->lock);
  1082. /* Convert the dBuV value to chip presentation */
  1083. r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
  1084. if (r)
  1085. goto out;
  1086. radio->tx_power = power;
  1087. out:
  1088. mutex_unlock(&core->lock);
  1089. return r;
  1090. }
  1091. #define WL1273_SPACING_50kHz 1
  1092. #define WL1273_SPACING_100kHz 2
  1093. #define WL1273_SPACING_200kHz 4
  1094. static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
  1095. unsigned int spacing)
  1096. {
  1097. struct wl1273_core *core = radio->core;
  1098. int r;
  1099. if (spacing == 0) {
  1100. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1101. WL1273_SPACING_100kHz);
  1102. radio->spacing = 100;
  1103. } else if (spacing - 50000 < 25000) {
  1104. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1105. WL1273_SPACING_50kHz);
  1106. radio->spacing = 50;
  1107. } else if (spacing - 100000 < 50000) {
  1108. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1109. WL1273_SPACING_100kHz);
  1110. radio->spacing = 100;
  1111. } else {
  1112. r = core->write(core, WL1273_SCAN_SPACING_SET,
  1113. WL1273_SPACING_200kHz);
  1114. radio->spacing = 200;
  1115. }
  1116. return r;
  1117. }
  1118. static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1119. {
  1120. struct wl1273_device *radio = ctrl->priv;
  1121. struct wl1273_core *core = radio->core;
  1122. dev_dbg(radio->dev, "%s\n", __func__);
  1123. if (mutex_lock_interruptible(&core->lock))
  1124. return -EINTR;
  1125. switch (ctrl->id) {
  1126. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1127. ctrl->val = wl1273_fm_get_tx_ctune(radio);
  1128. break;
  1129. default:
  1130. dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
  1131. __func__, ctrl->id);
  1132. break;
  1133. }
  1134. mutex_unlock(&core->lock);
  1135. return 0;
  1136. }
  1137. #define WL1273_MUTE_SOFT_ENABLE (1 << 0)
  1138. #define WL1273_MUTE_AC (1 << 1)
  1139. #define WL1273_MUTE_HARD_LEFT (1 << 2)
  1140. #define WL1273_MUTE_HARD_RIGHT (1 << 3)
  1141. #define WL1273_MUTE_SOFT_FORCE (1 << 4)
  1142. static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
  1143. {
  1144. return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
  1145. }
  1146. static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
  1147. {
  1148. struct wl1273_device *radio = to_radio(ctrl);
  1149. struct wl1273_core *core = radio->core;
  1150. int r = 0;
  1151. dev_dbg(radio->dev, "%s\n", __func__);
  1152. switch (ctrl->id) {
  1153. case V4L2_CID_AUDIO_MUTE:
  1154. if (mutex_lock_interruptible(&core->lock))
  1155. return -EINTR;
  1156. if (core->mode == WL1273_MODE_RX && ctrl->val)
  1157. r = core->write(core,
  1158. WL1273_MUTE_STATUS_SET,
  1159. WL1273_MUTE_HARD_LEFT |
  1160. WL1273_MUTE_HARD_RIGHT);
  1161. else if (core->mode == WL1273_MODE_RX)
  1162. r = core->write(core,
  1163. WL1273_MUTE_STATUS_SET, 0x0);
  1164. else if (core->mode == WL1273_MODE_TX && ctrl->val)
  1165. r = core->write(core, WL1273_MUTE, 1);
  1166. else if (core->mode == WL1273_MODE_TX)
  1167. r = core->write(core, WL1273_MUTE, 0);
  1168. mutex_unlock(&core->lock);
  1169. break;
  1170. case V4L2_CID_AUDIO_VOLUME:
  1171. if (ctrl->val == 0)
  1172. r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
  1173. else
  1174. r = core->set_volume(core, core->volume);
  1175. break;
  1176. case V4L2_CID_TUNE_PREEMPHASIS:
  1177. r = wl1273_fm_set_preemphasis(radio, ctrl->val);
  1178. break;
  1179. case V4L2_CID_TUNE_POWER_LEVEL:
  1180. r = wl1273_fm_set_tx_power(radio, ctrl->val);
  1181. break;
  1182. default:
  1183. dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
  1184. __func__, ctrl->id);
  1185. break;
  1186. }
  1187. dev_dbg(radio->dev, "%s\n", __func__);
  1188. return r;
  1189. }
  1190. static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
  1191. struct v4l2_audio *audio)
  1192. {
  1193. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1194. dev_dbg(radio->dev, "%s\n", __func__);
  1195. if (audio->index > 1)
  1196. return -EINVAL;
  1197. strlcpy(audio->name, "Radio", sizeof(audio->name));
  1198. audio->capability = V4L2_AUDCAP_STEREO;
  1199. return 0;
  1200. }
  1201. static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
  1202. const struct v4l2_audio *audio)
  1203. {
  1204. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1205. dev_dbg(radio->dev, "%s\n", __func__);
  1206. if (audio->index != 0)
  1207. return -EINVAL;
  1208. return 0;
  1209. }
  1210. #define WL1273_RDS_NOT_SYNCHRONIZED 0
  1211. #define WL1273_RDS_SYNCHRONIZED 1
  1212. static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
  1213. struct v4l2_tuner *tuner)
  1214. {
  1215. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1216. struct wl1273_core *core = radio->core;
  1217. u16 val;
  1218. int r;
  1219. dev_dbg(radio->dev, "%s\n", __func__);
  1220. if (tuner->index > 0)
  1221. return -EINVAL;
  1222. strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
  1223. tuner->type = V4L2_TUNER_RADIO;
  1224. tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
  1225. tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
  1226. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
  1227. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO |
  1228. V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP;
  1229. if (radio->stereo)
  1230. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  1231. else
  1232. tuner->audmode = V4L2_TUNER_MODE_MONO;
  1233. if (core->mode != WL1273_MODE_RX)
  1234. return 0;
  1235. if (mutex_lock_interruptible(&core->lock))
  1236. return -EINTR;
  1237. r = core->read(core, WL1273_STEREO_GET, &val);
  1238. if (r)
  1239. goto out;
  1240. if (val == 1)
  1241. tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1242. else
  1243. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  1244. r = core->read(core, WL1273_RSSI_LVL_GET, &val);
  1245. if (r)
  1246. goto out;
  1247. tuner->signal = (s16) val;
  1248. dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
  1249. tuner->afc = 0;
  1250. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  1251. if (r)
  1252. goto out;
  1253. if (val == WL1273_RDS_SYNCHRONIZED)
  1254. tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
  1255. out:
  1256. mutex_unlock(&core->lock);
  1257. return r;
  1258. }
  1259. static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
  1260. const struct v4l2_tuner *tuner)
  1261. {
  1262. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1263. struct wl1273_core *core = radio->core;
  1264. int r = 0;
  1265. dev_dbg(radio->dev, "%s\n", __func__);
  1266. dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
  1267. dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
  1268. dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
  1269. dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
  1270. dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
  1271. dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
  1272. if (tuner->index > 0)
  1273. return -EINVAL;
  1274. if (mutex_lock_interruptible(&core->lock))
  1275. return -EINTR;
  1276. r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
  1277. if (r)
  1278. goto out;
  1279. if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
  1280. r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
  1281. else
  1282. r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
  1283. if (r)
  1284. dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
  1285. if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
  1286. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
  1287. if (r < 0) {
  1288. dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
  1289. __func__, r);
  1290. goto out;
  1291. }
  1292. radio->stereo = false;
  1293. } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
  1294. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
  1295. if (r < 0) {
  1296. dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
  1297. __func__, r);
  1298. goto out;
  1299. }
  1300. radio->stereo = true;
  1301. } else {
  1302. dev_err(radio->dev, "%s: tuner->audmode: %d\n",
  1303. __func__, tuner->audmode);
  1304. r = -EINVAL;
  1305. goto out;
  1306. }
  1307. out:
  1308. mutex_unlock(&core->lock);
  1309. return r;
  1310. }
  1311. static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
  1312. struct v4l2_frequency *freq)
  1313. {
  1314. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1315. struct wl1273_core *core = radio->core;
  1316. dev_dbg(radio->dev, "%s\n", __func__);
  1317. if (mutex_lock_interruptible(&core->lock))
  1318. return -EINTR;
  1319. freq->type = V4L2_TUNER_RADIO;
  1320. freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
  1321. mutex_unlock(&core->lock);
  1322. return 0;
  1323. }
  1324. static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
  1325. const struct v4l2_frequency *freq)
  1326. {
  1327. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1328. struct wl1273_core *core = radio->core;
  1329. int r;
  1330. dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
  1331. if (freq->type != V4L2_TUNER_RADIO) {
  1332. dev_dbg(radio->dev,
  1333. "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
  1334. return -EINVAL;
  1335. }
  1336. if (mutex_lock_interruptible(&core->lock))
  1337. return -EINTR;
  1338. if (core->mode == WL1273_MODE_RX) {
  1339. dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
  1340. r = wl1273_fm_set_rx_freq(radio,
  1341. WL1273_INV_FREQ(freq->frequency));
  1342. if (r)
  1343. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1344. ": set frequency failed with %d\n", r);
  1345. } else {
  1346. r = wl1273_fm_set_tx_freq(radio,
  1347. WL1273_INV_FREQ(freq->frequency));
  1348. if (r)
  1349. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1350. ": set frequency failed with %d\n", r);
  1351. }
  1352. mutex_unlock(&core->lock);
  1353. dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
  1354. return r;
  1355. }
  1356. #define WL1273_DEFAULT_SEEK_LEVEL 7
  1357. static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
  1358. const struct v4l2_hw_freq_seek *seek)
  1359. {
  1360. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1361. struct wl1273_core *core = radio->core;
  1362. int r;
  1363. dev_dbg(radio->dev, "%s\n", __func__);
  1364. if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
  1365. return -EINVAL;
  1366. if (file->f_flags & O_NONBLOCK)
  1367. return -EWOULDBLOCK;
  1368. if (mutex_lock_interruptible(&core->lock))
  1369. return -EINTR;
  1370. r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
  1371. if (r)
  1372. goto out;
  1373. r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
  1374. if (r)
  1375. dev_warn(radio->dev, "HW seek failed: %d\n", r);
  1376. r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
  1377. WL1273_DEFAULT_SEEK_LEVEL);
  1378. if (r)
  1379. dev_warn(radio->dev, "HW seek failed: %d\n", r);
  1380. out:
  1381. mutex_unlock(&core->lock);
  1382. return r;
  1383. }
  1384. static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
  1385. const struct v4l2_modulator *modulator)
  1386. {
  1387. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1388. struct wl1273_core *core = radio->core;
  1389. int r = 0;
  1390. dev_dbg(radio->dev, "%s\n", __func__);
  1391. if (modulator->index > 0)
  1392. return -EINVAL;
  1393. if (mutex_lock_interruptible(&core->lock))
  1394. return -EINTR;
  1395. r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
  1396. if (r)
  1397. goto out;
  1398. if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
  1399. r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
  1400. else
  1401. r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
  1402. if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
  1403. r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
  1404. else
  1405. r = core->write(core, WL1273_MONO_SET,
  1406. WL1273_RX_STEREO);
  1407. if (r < 0)
  1408. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1409. "MONO_SET fails: %d\n", r);
  1410. out:
  1411. mutex_unlock(&core->lock);
  1412. return r;
  1413. }
  1414. static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
  1415. struct v4l2_modulator *modulator)
  1416. {
  1417. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1418. struct wl1273_core *core = radio->core;
  1419. u16 val;
  1420. int r;
  1421. dev_dbg(radio->dev, "%s\n", __func__);
  1422. strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
  1423. sizeof(modulator->name));
  1424. modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
  1425. modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
  1426. modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
  1427. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
  1428. if (core->mode != WL1273_MODE_TX)
  1429. return 0;
  1430. if (mutex_lock_interruptible(&core->lock))
  1431. return -EINTR;
  1432. r = core->read(core, WL1273_MONO_SET, &val);
  1433. if (r)
  1434. goto out;
  1435. if (val == WL1273_TX_STEREO)
  1436. modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
  1437. else
  1438. modulator->txsubchans = V4L2_TUNER_SUB_MONO;
  1439. if (radio->rds_on)
  1440. modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
  1441. out:
  1442. mutex_unlock(&core->lock);
  1443. return 0;
  1444. }
  1445. static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
  1446. {
  1447. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1448. struct wl1273_core *core = radio->core;
  1449. struct device *dev = radio->dev;
  1450. u16 val;
  1451. int r;
  1452. dev_info(dev, DRIVER_DESC);
  1453. if (core->mode == WL1273_MODE_OFF) {
  1454. dev_info(dev, "Mode: Off\n");
  1455. return 0;
  1456. }
  1457. if (core->mode == WL1273_MODE_SUSPENDED) {
  1458. dev_info(dev, "Mode: Suspended\n");
  1459. return 0;
  1460. }
  1461. r = core->read(core, WL1273_ASIC_ID_GET, &val);
  1462. if (r)
  1463. dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
  1464. else
  1465. dev_info(dev, "ASIC_ID: 0x%04x\n", val);
  1466. r = core->read(core, WL1273_ASIC_VER_GET, &val);
  1467. if (r)
  1468. dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
  1469. else
  1470. dev_info(dev, "ASIC Version: 0x%04x\n", val);
  1471. r = core->read(core, WL1273_FIRM_VER_GET, &val);
  1472. if (r)
  1473. dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
  1474. else
  1475. dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
  1476. r = core->read(core, WL1273_BAND_SET, &val);
  1477. if (r)
  1478. dev_err(dev, "%s: Get BAND fails.\n", __func__);
  1479. else
  1480. dev_info(dev, "BAND: %d\n", val);
  1481. if (core->mode == WL1273_MODE_TX) {
  1482. r = core->read(core, WL1273_PUPD_SET, &val);
  1483. if (r)
  1484. dev_err(dev, "%s: Get PUPD fails.\n", __func__);
  1485. else
  1486. dev_info(dev, "PUPD: 0x%04x\n", val);
  1487. r = core->read(core, WL1273_CHANL_SET, &val);
  1488. if (r)
  1489. dev_err(dev, "%s: Get CHANL fails.\n", __func__);
  1490. else
  1491. dev_info(dev, "Tx frequency: %dkHz\n", val*10);
  1492. } else if (core->mode == WL1273_MODE_RX) {
  1493. int bf = radio->rangelow;
  1494. r = core->read(core, WL1273_FREQ_SET, &val);
  1495. if (r)
  1496. dev_err(dev, "%s: Get FREQ fails.\n", __func__);
  1497. else
  1498. dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
  1499. r = core->read(core, WL1273_MOST_MODE_SET, &val);
  1500. if (r)
  1501. dev_err(dev, "%s: Get MOST_MODE fails.\n",
  1502. __func__);
  1503. else if (val == 0)
  1504. dev_info(dev, "MOST_MODE: Stereo according to blend\n");
  1505. else if (val == 1)
  1506. dev_info(dev, "MOST_MODE: Force mono output\n");
  1507. else
  1508. dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
  1509. r = core->read(core, WL1273_MOST_BLEND_SET, &val);
  1510. if (r)
  1511. dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
  1512. else if (val == 0)
  1513. dev_info(dev,
  1514. "MOST_BLEND: Switched blend & hysteresis.\n");
  1515. else if (val == 1)
  1516. dev_info(dev, "MOST_BLEND: Soft blend.\n");
  1517. else
  1518. dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
  1519. r = core->read(core, WL1273_STEREO_GET, &val);
  1520. if (r)
  1521. dev_err(dev, "%s: Get STEREO fails.\n", __func__);
  1522. else if (val == 0)
  1523. dev_info(dev, "STEREO: Not detected\n");
  1524. else if (val == 1)
  1525. dev_info(dev, "STEREO: Detected\n");
  1526. else
  1527. dev_info(dev, "STEREO: Unexpected value: %d\n", val);
  1528. r = core->read(core, WL1273_RSSI_LVL_GET, &val);
  1529. if (r)
  1530. dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
  1531. else
  1532. dev_info(dev, "RX signal strength: %d\n", (s16) val);
  1533. r = core->read(core, WL1273_POWER_SET, &val);
  1534. if (r)
  1535. dev_err(dev, "%s: Get POWER fails.\n", __func__);
  1536. else
  1537. dev_info(dev, "POWER: 0x%04x\n", val);
  1538. r = core->read(core, WL1273_INT_MASK_SET, &val);
  1539. if (r)
  1540. dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
  1541. else
  1542. dev_info(dev, "INT_MASK: 0x%04x\n", val);
  1543. r = core->read(core, WL1273_RDS_SYNC_GET, &val);
  1544. if (r)
  1545. dev_err(dev, "%s: Get RDS_SYNC fails.\n",
  1546. __func__);
  1547. else if (val == 0)
  1548. dev_info(dev, "RDS_SYNC: Not synchronized\n");
  1549. else if (val == 1)
  1550. dev_info(dev, "RDS_SYNC: Synchronized\n");
  1551. else
  1552. dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
  1553. r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
  1554. if (r)
  1555. dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
  1556. __func__);
  1557. else
  1558. dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
  1559. r = core->read(core, WL1273_VOLUME_SET, &val);
  1560. if (r)
  1561. dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
  1562. else
  1563. dev_info(dev, "VOLUME: 0x%04x\n", val);
  1564. }
  1565. return 0;
  1566. }
  1567. static void wl1273_vdev_release(struct video_device *dev)
  1568. {
  1569. }
  1570. static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
  1571. .s_ctrl = wl1273_fm_vidioc_s_ctrl,
  1572. .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
  1573. };
  1574. static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
  1575. .vidioc_querycap = wl1273_fm_vidioc_querycap,
  1576. .vidioc_g_input = wl1273_fm_vidioc_g_input,
  1577. .vidioc_s_input = wl1273_fm_vidioc_s_input,
  1578. .vidioc_g_audio = wl1273_fm_vidioc_g_audio,
  1579. .vidioc_s_audio = wl1273_fm_vidioc_s_audio,
  1580. .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
  1581. .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
  1582. .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
  1583. .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
  1584. .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
  1585. .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
  1586. .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
  1587. .vidioc_log_status = wl1273_fm_vidioc_log_status,
  1588. };
  1589. static const struct video_device wl1273_viddev_template = {
  1590. .fops = &wl1273_fops,
  1591. .ioctl_ops = &wl1273_ioctl_ops,
  1592. .name = WL1273_FM_DRIVER_NAME,
  1593. .release = wl1273_vdev_release,
  1594. .vfl_dir = VFL_DIR_TX,
  1595. };
  1596. static int wl1273_fm_radio_remove(struct platform_device *pdev)
  1597. {
  1598. struct wl1273_device *radio = platform_get_drvdata(pdev);
  1599. struct wl1273_core *core = radio->core;
  1600. dev_info(&pdev->dev, "%s.\n", __func__);
  1601. free_irq(core->client->irq, radio);
  1602. core->pdata->free_resources();
  1603. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  1604. video_unregister_device(&radio->videodev);
  1605. v4l2_device_unregister(&radio->v4l2dev);
  1606. return 0;
  1607. }
  1608. static int wl1273_fm_radio_probe(struct platform_device *pdev)
  1609. {
  1610. struct wl1273_core **core = pdev->dev.platform_data;
  1611. struct wl1273_device *radio;
  1612. struct v4l2_ctrl *ctrl;
  1613. int r = 0;
  1614. pr_debug("%s\n", __func__);
  1615. if (!core) {
  1616. dev_err(&pdev->dev, "No platform data.\n");
  1617. r = -EINVAL;
  1618. goto pdata_err;
  1619. }
  1620. radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
  1621. if (!radio) {
  1622. r = -ENOMEM;
  1623. goto pdata_err;
  1624. }
  1625. /* RDS buffer allocation */
  1626. radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
  1627. radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL);
  1628. if (!radio->buffer) {
  1629. pr_err("Cannot allocate memory for RDS buffer.\n");
  1630. r = -ENOMEM;
  1631. goto pdata_err;
  1632. }
  1633. radio->core = *core;
  1634. radio->irq_flags = WL1273_IRQ_MASK;
  1635. radio->dev = &radio->core->client->dev;
  1636. radio->rds_on = false;
  1637. radio->core->mode = WL1273_MODE_OFF;
  1638. radio->tx_power = 118;
  1639. radio->core->audio_mode = WL1273_AUDIO_ANALOG;
  1640. radio->band = WL1273_BAND_OTHER;
  1641. radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
  1642. radio->core->channel_number = 2;
  1643. radio->core->volume = WL1273_DEFAULT_VOLUME;
  1644. radio->rx_frequency = WL1273_BAND_OTHER_LOW;
  1645. radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
  1646. radio->rangelow = WL1273_BAND_OTHER_LOW;
  1647. radio->rangehigh = WL1273_BAND_OTHER_HIGH;
  1648. radio->stereo = true;
  1649. radio->bus_type = "I2C";
  1650. if (radio->core->pdata->request_resources) {
  1651. r = radio->core->pdata->request_resources(radio->core->client);
  1652. if (r) {
  1653. dev_err(radio->dev, WL1273_FM_DRIVER_NAME
  1654. ": Cannot get platform data\n");
  1655. goto pdata_err;
  1656. }
  1657. dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
  1658. r = request_threaded_irq(radio->core->client->irq, NULL,
  1659. wl1273_fm_irq_thread_handler,
  1660. IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
  1661. "wl1273-fm", radio);
  1662. if (r < 0) {
  1663. dev_err(radio->dev, WL1273_FM_DRIVER_NAME
  1664. ": Unable to register IRQ handler: %d\n", r);
  1665. goto err_request_irq;
  1666. }
  1667. } else {
  1668. dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ not configured");
  1669. r = -EINVAL;
  1670. goto pdata_err;
  1671. }
  1672. init_completion(&radio->busy);
  1673. init_waitqueue_head(&radio->read_queue);
  1674. radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL);
  1675. if (!radio->write_buf) {
  1676. r = -ENOMEM;
  1677. goto write_buf_err;
  1678. }
  1679. radio->dev = &pdev->dev;
  1680. radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
  1681. radio->rds_users = 0;
  1682. r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
  1683. if (r) {
  1684. dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
  1685. goto write_buf_err;
  1686. }
  1687. /* V4L2 configuration */
  1688. radio->videodev = wl1273_viddev_template;
  1689. radio->videodev.v4l2_dev = &radio->v4l2dev;
  1690. v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
  1691. /* add in ascending ID order */
  1692. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1693. V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
  1694. WL1273_DEFAULT_VOLUME);
  1695. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1696. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  1697. v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1698. V4L2_CID_TUNE_PREEMPHASIS,
  1699. V4L2_PREEMPHASIS_75_uS, 0x03,
  1700. V4L2_PREEMPHASIS_50_uS);
  1701. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1702. V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
  1703. ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1704. V4L2_CID_TUNE_ANTENNA_CAPACITOR,
  1705. 0, 255, 1, 255);
  1706. if (ctrl)
  1707. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1708. if (radio->ctrl_handler.error) {
  1709. r = radio->ctrl_handler.error;
  1710. dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
  1711. goto handler_init_err;
  1712. }
  1713. video_set_drvdata(&radio->videodev, radio);
  1714. platform_set_drvdata(pdev, radio);
  1715. /* register video device */
  1716. r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
  1717. if (r) {
  1718. dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
  1719. ": Could not register video device\n");
  1720. goto handler_init_err;
  1721. }
  1722. return 0;
  1723. handler_init_err:
  1724. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  1725. v4l2_device_unregister(&radio->v4l2dev);
  1726. write_buf_err:
  1727. free_irq(radio->core->client->irq, radio);
  1728. err_request_irq:
  1729. radio->core->pdata->free_resources();
  1730. pdata_err:
  1731. return r;
  1732. }
  1733. static struct platform_driver wl1273_fm_radio_driver = {
  1734. .probe = wl1273_fm_radio_probe,
  1735. .remove = wl1273_fm_radio_remove,
  1736. .driver = {
  1737. .name = "wl1273_fm_radio",
  1738. },
  1739. };
  1740. module_platform_driver(wl1273_fm_radio_driver);
  1741. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  1742. MODULE_DESCRIPTION(DRIVER_DESC);
  1743. MODULE_LICENSE("GPL");
  1744. MODULE_ALIAS("platform:wl1273_fm_radio");