radio-wl1273.c 50 KB

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