radio-wl1273.c 55 KB

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