radio-wl1273.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169
  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 = "/*(DEBLOBBED)*/";
  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 (reject_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");