radio-wl1273.c 50 KB

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