radio-wl1273.c 50 KB

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