radio-wl1273.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  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 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, "%s - download OK, r: %d\n", __func__, r);
  545. out:
  546. release_firmware(fw_p);
  547. return r;
  548. }
  549. static int wl1273_fm_stop(struct wl1273_device *radio)
  550. {
  551. struct wl1273_core *core = radio->core;
  552. if (core->mode == WL1273_MODE_RX) {
  553. int r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
  554. WL1273_POWER_SET_OFF);
  555. if (r)
  556. dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
  557. __func__, r);
  558. } else if (core->mode == WL1273_MODE_TX) {
  559. int r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
  560. WL1273_PUPD_SET_OFF);
  561. if (r)
  562. dev_err(radio->dev,
  563. "%s: PUPD_SET fails: %d\n", __func__, r);
  564. }
  565. if (core->pdata->disable) {
  566. core->pdata->disable();
  567. dev_dbg(radio->dev, "Back to reset\n");
  568. }
  569. return 0;
  570. }
  571. static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
  572. {
  573. struct wl1273_core *core = radio->core;
  574. struct wl1273_fm_platform_data *pdata = core->pdata;
  575. struct device *dev = radio->dev;
  576. int r = -EINVAL;
  577. if (pdata->enable && core->mode == WL1273_MODE_OFF) {
  578. dev_dbg(radio->dev, "Out of reset\n");
  579. pdata->enable();
  580. msleep(250);
  581. }
  582. if (new_mode == WL1273_MODE_RX) {
  583. u16 val = WL1273_POWER_SET_FM;
  584. if (radio->rds_on)
  585. val |= WL1273_POWER_SET_RDS;
  586. /* If this fails try again */
  587. r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val);
  588. if (r) {
  589. msleep(100);
  590. r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val);
  591. if (r) {
  592. dev_err(dev, "%s: POWER_SET fails\n", __func__);
  593. goto fail;
  594. }
  595. }
  596. /* rds buffer configuration */
  597. radio->wr_index = 0;
  598. radio->rd_index = 0;
  599. } else if (new_mode == WL1273_MODE_TX) {
  600. /* If this fails try again once */
  601. r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
  602. WL1273_PUPD_SET_ON);
  603. if (r) {
  604. msleep(100);
  605. r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
  606. WL1273_PUPD_SET_ON);
  607. if (r) {
  608. dev_err(dev, "%s: PUPD_SET fails\n", __func__);
  609. goto fail;
  610. }
  611. }
  612. if (radio->rds_on)
  613. r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 1);
  614. else
  615. r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 0);
  616. } else {
  617. dev_warn(dev, "%s: Illegal mode.\n", __func__);
  618. }
  619. if (core->mode == WL1273_MODE_OFF) {
  620. r = wl1273_fm_upload_firmware_patch(radio);
  621. if (r)
  622. dev_warn(dev, "Firmware upload failed.\n");
  623. /*
  624. * Sometimes the chip is in a wrong power state at this point.
  625. * So we set the power once again.
  626. */
  627. if (new_mode == WL1273_MODE_RX) {
  628. u16 val = WL1273_POWER_SET_FM;
  629. if (radio->rds_on)
  630. val |= WL1273_POWER_SET_RDS;
  631. r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val);
  632. if (r) {
  633. dev_err(dev, "%s: POWER_SET fails\n", __func__);
  634. goto fail;
  635. }
  636. } else if (new_mode == WL1273_MODE_TX) {
  637. r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
  638. WL1273_PUPD_SET_ON);
  639. if (r) {
  640. dev_err(dev, "%s: PUPD_SET fails\n", __func__);
  641. goto fail;
  642. }
  643. }
  644. }
  645. return 0;
  646. fail:
  647. if (pdata->disable)
  648. pdata->disable();
  649. dev_dbg(dev, "%s: return: %d\n", __func__, r);
  650. return r;
  651. }
  652. static int wl1273_fm_suspend(struct wl1273_device *radio)
  653. {
  654. struct wl1273_core *core = radio->core;
  655. int r = 0;
  656. /* Cannot go from OFF to SUSPENDED */
  657. if (core->mode == WL1273_MODE_RX)
  658. r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
  659. WL1273_POWER_SET_RETENTION);
  660. else if (core->mode == WL1273_MODE_TX)
  661. r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
  662. WL1273_PUPD_SET_RETENTION);
  663. else
  664. r = -EINVAL;
  665. if (r) {
  666. dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
  667. goto out;
  668. }
  669. out:
  670. return r;
  671. }
  672. static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
  673. {
  674. struct wl1273_core *core = radio->core;
  675. struct device *dev = radio->dev;
  676. int old_mode;
  677. int r;
  678. dev_dbg(dev, "%s\n", __func__);
  679. dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
  680. old_mode = core->mode;
  681. if (mode & radio->forbidden) {
  682. r = -EPERM;
  683. goto out;
  684. }
  685. switch (mode) {
  686. case WL1273_MODE_RX:
  687. case WL1273_MODE_TX:
  688. r = wl1273_fm_start(radio, mode);
  689. if (r) {
  690. dev_err(dev, "%s: Cannot start.\n", __func__);
  691. wl1273_fm_stop(radio);
  692. goto out;
  693. }
  694. core->mode = mode;
  695. r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
  696. radio->irq_flags);
  697. if (r) {
  698. dev_err(dev, "INT_MASK_SET fails.\n");
  699. goto out;
  700. }
  701. /* remember previous settings */
  702. if (mode == WL1273_MODE_RX) {
  703. r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
  704. if (r) {
  705. dev_err(dev, "set freq fails: %d.\n", r);
  706. goto out;
  707. }
  708. r = core->set_volume(core, core->volume);
  709. if (r) {
  710. dev_err(dev, "set volume fails: %d.\n", r);
  711. goto out;
  712. }
  713. dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
  714. core->volume);
  715. } else {
  716. r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
  717. if (r) {
  718. dev_err(dev, "set freq fails: %d.\n", r);
  719. goto out;
  720. }
  721. }
  722. dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
  723. r = core->set_audio(core, core->audio_mode);
  724. if (r)
  725. dev_err(dev, "Cannot set audio mode.\n");
  726. break;
  727. case WL1273_MODE_OFF:
  728. r = wl1273_fm_stop(radio);
  729. if (r)
  730. dev_err(dev, "%s: Off fails: %d\n", __func__, r);
  731. else
  732. core->mode = WL1273_MODE_OFF;
  733. break;
  734. case WL1273_MODE_SUSPENDED:
  735. r = wl1273_fm_suspend(radio);
  736. if (r)
  737. dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
  738. else
  739. core->mode = WL1273_MODE_SUSPENDED;
  740. break;
  741. default:
  742. dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
  743. r = -EINVAL;
  744. break;
  745. }
  746. out:
  747. if (r)
  748. core->mode = old_mode;
  749. return r;
  750. }
  751. static int wl1273_fm_set_seek(struct wl1273_device *radio,
  752. unsigned int wrap_around,
  753. unsigned int seek_upward,
  754. int level)
  755. {
  756. struct wl1273_core *core = radio->core;
  757. int r = 0;
  758. unsigned int dir = (seek_upward == 0) ? 0 : 1;
  759. unsigned int f;
  760. f = radio->rx_frequency;
  761. dev_dbg(radio->dev, "rx_frequency: %d\n", f);
  762. if (dir && f + radio->spacing <= radio->rangehigh)
  763. r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
  764. else if (dir && wrap_around)
  765. r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
  766. else if (f - radio->spacing >= radio->rangelow)
  767. r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
  768. else if (wrap_around)
  769. r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
  770. if (r)
  771. goto out;
  772. if (level < SCHAR_MIN || level > SCHAR_MAX)
  773. return -EINVAL;
  774. INIT_COMPLETION(radio->busy);
  775. dev_dbg(radio->dev, "%s: BUSY\n", __func__);
  776. r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags);
  777. if (r)
  778. goto out;
  779. dev_dbg(radio->dev, "%s\n", __func__);
  780. r = wl1273_fm_write_cmd(core, WL1273_SEARCH_LVL_SET, level);
  781. if (r)
  782. goto out;
  783. r = wl1273_fm_write_cmd(core, WL1273_SEARCH_DIR_SET, dir);
  784. if (r)
  785. goto out;
  786. r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
  787. TUNER_MODE_AUTO_SEEK);
  788. if (r)
  789. goto out;
  790. wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
  791. if (!(radio->irq_received & WL1273_BL_EVENT))
  792. goto out;
  793. radio->irq_received &= ~WL1273_BL_EVENT;
  794. if (!wrap_around)
  795. goto out;
  796. /* Wrap around */
  797. dev_dbg(radio->dev, "Wrap around in HW seek.\n");
  798. if (seek_upward)
  799. f = radio->rangelow;
  800. else
  801. f = radio->rangehigh;
  802. r = wl1273_fm_set_rx_freq(radio, f);
  803. if (r)
  804. goto out;
  805. INIT_COMPLETION(radio->busy);
  806. dev_dbg(radio->dev, "%s: BUSY\n", __func__);
  807. r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
  808. TUNER_MODE_AUTO_SEEK);
  809. if (r)
  810. goto out;
  811. wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
  812. out:
  813. dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
  814. return r;
  815. }
  816. /**
  817. * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
  818. * @radio: A pointer to the device struct.
  819. */
  820. static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
  821. {
  822. struct wl1273_core *core = radio->core;
  823. struct device *dev = radio->dev;
  824. u16 val;
  825. int r;
  826. if (core->mode == WL1273_MODE_OFF ||
  827. core->mode == WL1273_MODE_SUSPENDED)
  828. return -EPERM;
  829. r = wl1273_fm_read_reg(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
  830. if (r) {
  831. dev_err(dev, "%s: read error: %d\n", __func__, r);
  832. goto out;
  833. }
  834. out:
  835. return val;
  836. }
  837. /**
  838. * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
  839. * @radio: A pointer to the device struct.
  840. * @preemphasis: The new pre-amphasis value.
  841. *
  842. * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
  843. * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
  844. */
  845. static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
  846. unsigned int preemphasis)
  847. {
  848. struct wl1273_core *core = radio->core;
  849. int r;
  850. u16 em;
  851. if (core->mode == WL1273_MODE_OFF ||
  852. core->mode == WL1273_MODE_SUSPENDED)
  853. return -EPERM;
  854. mutex_lock(&core->lock);
  855. switch (preemphasis) {
  856. case V4L2_PREEMPHASIS_DISABLED:
  857. em = 1;
  858. break;
  859. case V4L2_PREEMPHASIS_50_uS:
  860. em = 0;
  861. break;
  862. case V4L2_PREEMPHASIS_75_uS:
  863. em = 2;
  864. break;
  865. default:
  866. r = -EINVAL;
  867. goto out;
  868. }
  869. r = wl1273_fm_write_cmd(core, WL1273_PREMPH_SET, em);
  870. if (r)
  871. goto out;
  872. radio->preemphasis = preemphasis;
  873. out:
  874. mutex_unlock(&core->lock);
  875. return r;
  876. }
  877. static int wl1273_fm_rds_on(struct wl1273_device *radio)
  878. {
  879. struct wl1273_core *core = radio->core;
  880. int r;
  881. dev_dbg(radio->dev, "%s\n", __func__);
  882. if (radio->rds_on)
  883. return 0;
  884. r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
  885. WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
  886. if (r)
  887. goto out;
  888. r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
  889. if (r)
  890. dev_err(radio->dev, "set freq fails: %d.\n", r);
  891. out:
  892. return r;
  893. }
  894. static int wl1273_fm_rds_off(struct wl1273_device *radio)
  895. {
  896. struct wl1273_core *core = radio->core;
  897. int r;
  898. if (!radio->rds_on)
  899. return 0;
  900. radio->irq_flags &= ~WL1273_RDS_EVENT;
  901. r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags);
  902. if (r)
  903. goto out;
  904. /* stop rds reception */
  905. cancel_delayed_work(&radio->work);
  906. /* Service pending read */
  907. wake_up_interruptible(&radio->read_queue);
  908. dev_dbg(radio->dev, "%s\n", __func__);
  909. r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
  910. if (r)
  911. goto out;
  912. r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
  913. if (r)
  914. dev_err(radio->dev, "set freq fails: %d.\n", r);
  915. out:
  916. dev_dbg(radio->dev, "%s: exiting...\n", __func__);
  917. return r;
  918. }
  919. static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
  920. {
  921. int r = 0;
  922. struct wl1273_core *core = radio->core;
  923. if (core->mode == WL1273_MODE_OFF ||
  924. core->mode == WL1273_MODE_SUSPENDED)
  925. return -EPERM;
  926. if (new_mode == WL1273_RDS_RESET) {
  927. r = wl1273_fm_write_cmd(core, WL1273_RDS_CNTRL_SET, 1);
  928. return r;
  929. }
  930. if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
  931. r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 0);
  932. } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
  933. r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 1);
  934. } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
  935. r = wl1273_fm_rds_off(radio);
  936. } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
  937. r = wl1273_fm_rds_on(radio);
  938. } else {
  939. dev_err(radio->dev, "%s: Unknown mode: %d\n",
  940. __func__, new_mode);
  941. r = -EINVAL;
  942. }
  943. if (!r)
  944. radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
  945. return r;
  946. }
  947. static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
  948. size_t count, loff_t *ppos)
  949. {
  950. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  951. u16 val;
  952. int r;
  953. dev_dbg(radio->dev, "%s\n", __func__);
  954. if (radio->core->mode != WL1273_MODE_TX)
  955. return count;
  956. if (radio->rds_users == 0) {
  957. dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
  958. return 0;
  959. }
  960. if (mutex_lock_interruptible(&radio->core->lock))
  961. return -EINTR;
  962. /*
  963. * Multiple processes can open the device, but only
  964. * one gets to write to it.
  965. */
  966. if (radio->owner && radio->owner != file) {
  967. r = -EBUSY;
  968. goto out;
  969. }
  970. radio->owner = file;
  971. /* Manual Mode */
  972. if (count > 255)
  973. val = 255;
  974. else
  975. val = count;
  976. wl1273_fm_write_cmd(radio->core, WL1273_RDS_CONFIG_DATA_SET, val);
  977. if (copy_from_user(radio->write_buf + 1, buf, val)) {
  978. r = -EFAULT;
  979. goto out;
  980. }
  981. dev_dbg(radio->dev, "Count: %d\n", val);
  982. dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
  983. radio->write_buf[0] = WL1273_RDS_DATA_SET;
  984. wl1273_fm_write_data(radio->core, radio->write_buf, val + 1);
  985. r = val;
  986. out:
  987. mutex_unlock(&radio->core->lock);
  988. return r;
  989. }
  990. static unsigned int wl1273_fm_fops_poll(struct file *file,
  991. struct poll_table_struct *pts)
  992. {
  993. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  994. struct wl1273_core *core = radio->core;
  995. if (radio->owner && radio->owner != file)
  996. return -EBUSY;
  997. radio->owner = file;
  998. if (core->mode == WL1273_MODE_RX) {
  999. poll_wait(file, &radio->read_queue, pts);
  1000. if (radio->rd_index != radio->wr_index)
  1001. return POLLIN | POLLRDNORM;
  1002. } else if (core->mode == WL1273_MODE_TX) {
  1003. return POLLOUT | POLLWRNORM;
  1004. }
  1005. return 0;
  1006. }
  1007. static int wl1273_fm_fops_open(struct file *file)
  1008. {
  1009. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1010. struct wl1273_core *core = radio->core;
  1011. int r = 0;
  1012. dev_dbg(radio->dev, "%s\n", __func__);
  1013. if (core->mode == WL1273_MODE_RX && radio->rds_on &&
  1014. !radio->rds_users) {
  1015. dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
  1016. if (mutex_lock_interruptible(&core->lock))
  1017. return -EINTR;
  1018. radio->irq_flags |= WL1273_RDS_EVENT;
  1019. r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
  1020. radio->irq_flags);
  1021. if (r) {
  1022. mutex_unlock(&core->lock);
  1023. goto out;
  1024. }
  1025. radio->rds_users++;
  1026. mutex_unlock(&core->lock);
  1027. }
  1028. out:
  1029. return r;
  1030. }
  1031. static int wl1273_fm_fops_release(struct file *file)
  1032. {
  1033. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1034. struct wl1273_core *core = radio->core;
  1035. int r = 0;
  1036. dev_dbg(radio->dev, "%s\n", __func__);
  1037. if (radio->rds_users > 0) {
  1038. radio->rds_users--;
  1039. if (radio->rds_users == 0) {
  1040. if (mutex_lock_interruptible(&core->lock))
  1041. return -EINTR;
  1042. radio->irq_flags &= ~WL1273_RDS_EVENT;
  1043. if (core->mode == WL1273_MODE_RX) {
  1044. r = wl1273_fm_write_cmd(core,
  1045. WL1273_INT_MASK_SET,
  1046. radio->irq_flags);
  1047. if (r) {
  1048. mutex_unlock(&core->lock);
  1049. goto out;
  1050. }
  1051. }
  1052. mutex_unlock(&core->lock);
  1053. }
  1054. }
  1055. if (file == radio->owner)
  1056. radio->owner = NULL;
  1057. out:
  1058. return r;
  1059. }
  1060. static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
  1061. size_t count, loff_t *ppos)
  1062. {
  1063. int r = 0;
  1064. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1065. struct wl1273_core *core = radio->core;
  1066. unsigned int block_count = 0;
  1067. u16 val;
  1068. dev_dbg(radio->dev, "%s\n", __func__);
  1069. if (radio->core->mode != WL1273_MODE_RX)
  1070. return 0;
  1071. if (radio->rds_users == 0) {
  1072. dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
  1073. return 0;
  1074. }
  1075. if (mutex_lock_interruptible(&core->lock))
  1076. return -EINTR;
  1077. /*
  1078. * Multiple processes can open the device, but only
  1079. * one at a time gets read access.
  1080. */
  1081. if (radio->owner && radio->owner != file) {
  1082. r = -EBUSY;
  1083. goto out;
  1084. }
  1085. radio->owner = file;
  1086. r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
  1087. if (r) {
  1088. dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
  1089. goto out;
  1090. } else if (val == 0) {
  1091. dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
  1092. r = -ENODATA;
  1093. goto out;
  1094. }
  1095. /* block if no new data available */
  1096. while (radio->wr_index == radio->rd_index) {
  1097. if (file->f_flags & O_NONBLOCK) {
  1098. r = -EWOULDBLOCK;
  1099. goto out;
  1100. }
  1101. dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
  1102. if (wait_event_interruptible(radio->read_queue,
  1103. radio->wr_index !=
  1104. radio->rd_index) < 0) {
  1105. r = -EINTR;
  1106. goto out;
  1107. }
  1108. }
  1109. /* calculate block count from byte count */
  1110. count /= RDS_BLOCK_SIZE;
  1111. /* copy RDS blocks from the internal buffer and to user buffer */
  1112. while (block_count < count) {
  1113. if (radio->rd_index == radio->wr_index)
  1114. break;
  1115. /* always transfer complete RDS blocks */
  1116. if (copy_to_user(buf, &radio->buffer[radio->rd_index],
  1117. RDS_BLOCK_SIZE))
  1118. break;
  1119. /* increment and wrap the read pointer */
  1120. radio->rd_index += RDS_BLOCK_SIZE;
  1121. if (radio->rd_index >= radio->buf_size)
  1122. radio->rd_index = 0;
  1123. /* increment counters */
  1124. block_count++;
  1125. buf += RDS_BLOCK_SIZE;
  1126. r += RDS_BLOCK_SIZE;
  1127. }
  1128. out:
  1129. dev_dbg(radio->dev, "%s: exit\n", __func__);
  1130. mutex_unlock(&core->lock);
  1131. return r;
  1132. }
  1133. static const struct v4l2_file_operations wl1273_fops = {
  1134. .owner = THIS_MODULE,
  1135. .read = wl1273_fm_fops_read,
  1136. .write = wl1273_fm_fops_write,
  1137. .poll = wl1273_fm_fops_poll,
  1138. .ioctl = video_ioctl2,
  1139. .open = wl1273_fm_fops_open,
  1140. .release = wl1273_fm_fops_release,
  1141. };
  1142. static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
  1143. struct v4l2_capability *capability)
  1144. {
  1145. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1146. dev_dbg(radio->dev, "%s\n", __func__);
  1147. strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
  1148. sizeof(capability->driver));
  1149. strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
  1150. sizeof(capability->card));
  1151. strlcpy(capability->bus_info, radio->bus_type,
  1152. sizeof(capability->bus_info));
  1153. capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
  1154. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
  1155. V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
  1156. V4L2_CAP_RDS_OUTPUT;
  1157. return 0;
  1158. }
  1159. static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
  1160. unsigned int *i)
  1161. {
  1162. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1163. dev_dbg(radio->dev, "%s\n", __func__);
  1164. *i = 0;
  1165. return 0;
  1166. }
  1167. static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
  1168. unsigned int i)
  1169. {
  1170. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1171. dev_dbg(radio->dev, "%s\n", __func__);
  1172. if (i != 0)
  1173. return -EINVAL;
  1174. return 0;
  1175. }
  1176. /**
  1177. * wl1273_fm_set_tx_power() - Set the transmission power value.
  1178. * @core: A pointer to the device struct.
  1179. * @power: The new power value.
  1180. */
  1181. static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
  1182. {
  1183. int r;
  1184. if (radio->core->mode == WL1273_MODE_OFF ||
  1185. radio->core->mode == WL1273_MODE_SUSPENDED)
  1186. return -EPERM;
  1187. mutex_lock(&radio->core->lock);
  1188. /* Convert the dBuV value to chip presentation */
  1189. r = wl1273_fm_write_cmd(radio->core, WL1273_POWER_LEV_SET, 122 - power);
  1190. if (r)
  1191. goto out;
  1192. radio->tx_power = power;
  1193. out:
  1194. mutex_unlock(&radio->core->lock);
  1195. return r;
  1196. }
  1197. #define WL1273_SPACING_50kHz 1
  1198. #define WL1273_SPACING_100kHz 2
  1199. #define WL1273_SPACING_200kHz 4
  1200. static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
  1201. unsigned int spacing)
  1202. {
  1203. int r;
  1204. if (spacing == 0) {
  1205. r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
  1206. WL1273_SPACING_100kHz);
  1207. radio->spacing = 100;
  1208. } else if (spacing - 50000 < 25000) {
  1209. r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
  1210. WL1273_SPACING_50kHz);
  1211. radio->spacing = 50;
  1212. } else if (spacing - 100000 < 50000) {
  1213. r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
  1214. WL1273_SPACING_100kHz);
  1215. radio->spacing = 100;
  1216. } else {
  1217. r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
  1218. WL1273_SPACING_200kHz);
  1219. radio->spacing = 200;
  1220. }
  1221. return r;
  1222. }
  1223. static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1224. {
  1225. struct wl1273_device *radio = ctrl->priv;
  1226. struct wl1273_core *core = radio->core;
  1227. dev_dbg(radio->dev, "%s\n", __func__);
  1228. if (mutex_lock_interruptible(&core->lock))
  1229. return -EINTR;
  1230. switch (ctrl->id) {
  1231. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1232. ctrl->val = wl1273_fm_get_tx_ctune(radio);
  1233. break;
  1234. default:
  1235. dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
  1236. __func__, ctrl->id);
  1237. break;
  1238. }
  1239. mutex_unlock(&core->lock);
  1240. return 0;
  1241. }
  1242. #define WL1273_MUTE_SOFT_ENABLE (1 << 0)
  1243. #define WL1273_MUTE_AC (1 << 1)
  1244. #define WL1273_MUTE_HARD_LEFT (1 << 2)
  1245. #define WL1273_MUTE_HARD_RIGHT (1 << 3)
  1246. #define WL1273_MUTE_SOFT_FORCE (1 << 4)
  1247. static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
  1248. {
  1249. return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
  1250. }
  1251. static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
  1252. {
  1253. struct wl1273_device *radio = to_radio(ctrl);
  1254. struct wl1273_core *core = radio->core;
  1255. int r = 0;
  1256. dev_dbg(radio->dev, "%s\n", __func__);
  1257. switch (ctrl->id) {
  1258. case V4L2_CID_AUDIO_MUTE:
  1259. if (mutex_lock_interruptible(&core->lock))
  1260. return -EINTR;
  1261. if (core->mode == WL1273_MODE_RX && ctrl->val)
  1262. r = wl1273_fm_write_cmd(core,
  1263. WL1273_MUTE_STATUS_SET,
  1264. WL1273_MUTE_HARD_LEFT |
  1265. WL1273_MUTE_HARD_RIGHT);
  1266. else if (core->mode == WL1273_MODE_RX)
  1267. r = wl1273_fm_write_cmd(core,
  1268. WL1273_MUTE_STATUS_SET, 0x0);
  1269. else if (core->mode == WL1273_MODE_TX && ctrl->val)
  1270. r = wl1273_fm_write_cmd(core, WL1273_MUTE, 1);
  1271. else if (core->mode == WL1273_MODE_TX)
  1272. r = wl1273_fm_write_cmd(core, WL1273_MUTE, 0);
  1273. mutex_unlock(&core->lock);
  1274. break;
  1275. case V4L2_CID_AUDIO_VOLUME:
  1276. if (ctrl->val == 0)
  1277. r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
  1278. else
  1279. r = core->set_volume(core, core->volume);
  1280. break;
  1281. case V4L2_CID_TUNE_PREEMPHASIS:
  1282. r = wl1273_fm_set_preemphasis(radio, ctrl->val);
  1283. break;
  1284. case V4L2_CID_TUNE_POWER_LEVEL:
  1285. r = wl1273_fm_set_tx_power(radio, ctrl->val);
  1286. break;
  1287. default:
  1288. dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
  1289. __func__, ctrl->id);
  1290. break;
  1291. }
  1292. dev_dbg(radio->dev, "%s\n", __func__);
  1293. return r;
  1294. }
  1295. static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
  1296. struct v4l2_audio *audio)
  1297. {
  1298. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1299. dev_dbg(radio->dev, "%s\n", __func__);
  1300. if (audio->index > 1)
  1301. return -EINVAL;
  1302. strlcpy(audio->name, "Radio", sizeof(audio->name));
  1303. audio->capability = V4L2_AUDCAP_STEREO;
  1304. return 0;
  1305. }
  1306. static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
  1307. struct v4l2_audio *audio)
  1308. {
  1309. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1310. dev_dbg(radio->dev, "%s\n", __func__);
  1311. if (audio->index != 0)
  1312. return -EINVAL;
  1313. return 0;
  1314. }
  1315. #define WL1273_RDS_NOT_SYNCHRONIZED 0
  1316. #define WL1273_RDS_SYNCHRONIZED 1
  1317. static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
  1318. struct v4l2_tuner *tuner)
  1319. {
  1320. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1321. struct wl1273_core *core = radio->core;
  1322. u16 val;
  1323. int r;
  1324. dev_dbg(radio->dev, "%s\n", __func__);
  1325. if (tuner->index > 0)
  1326. return -EINVAL;
  1327. strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
  1328. tuner->type = V4L2_TUNER_RADIO;
  1329. tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
  1330. tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
  1331. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
  1332. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
  1333. if (radio->stereo)
  1334. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  1335. else
  1336. tuner->audmode = V4L2_TUNER_MODE_MONO;
  1337. if (core->mode != WL1273_MODE_RX)
  1338. return 0;
  1339. if (mutex_lock_interruptible(&core->lock))
  1340. return -EINTR;
  1341. r = wl1273_fm_read_reg(core, WL1273_STEREO_GET, &val);
  1342. if (r)
  1343. goto out;
  1344. if (val == 1)
  1345. tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1346. else
  1347. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  1348. r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val);
  1349. if (r)
  1350. goto out;
  1351. tuner->signal = (s16) val;
  1352. dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
  1353. tuner->afc = 0;
  1354. r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
  1355. if (r)
  1356. goto out;
  1357. if (val == WL1273_RDS_SYNCHRONIZED)
  1358. tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
  1359. out:
  1360. mutex_unlock(&core->lock);
  1361. return r;
  1362. }
  1363. static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
  1364. struct v4l2_tuner *tuner)
  1365. {
  1366. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1367. struct wl1273_core *core = radio->core;
  1368. int r = 0;
  1369. dev_dbg(radio->dev, "%s\n", __func__);
  1370. dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
  1371. dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
  1372. dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
  1373. dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
  1374. dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
  1375. dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
  1376. if (tuner->index > 0)
  1377. return -EINVAL;
  1378. if (mutex_lock_interruptible(&core->lock))
  1379. return -EINTR;
  1380. r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
  1381. if (r)
  1382. goto out;
  1383. if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
  1384. r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
  1385. else
  1386. r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
  1387. if (r)
  1388. dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
  1389. if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
  1390. r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
  1391. WL1273_RX_MONO);
  1392. if (r < 0) {
  1393. dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
  1394. __func__, r);
  1395. goto out;
  1396. }
  1397. radio->stereo = false;
  1398. } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
  1399. r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
  1400. WL1273_RX_STEREO);
  1401. if (r < 0) {
  1402. dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
  1403. __func__, r);
  1404. goto out;
  1405. }
  1406. radio->stereo = true;
  1407. } else {
  1408. dev_err(radio->dev, "%s: tuner->audmode: %d\n",
  1409. __func__, tuner->audmode);
  1410. r = -EINVAL;
  1411. goto out;
  1412. }
  1413. out:
  1414. mutex_unlock(&core->lock);
  1415. return r;
  1416. }
  1417. static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
  1418. struct v4l2_frequency *freq)
  1419. {
  1420. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1421. struct wl1273_core *core = radio->core;
  1422. dev_dbg(radio->dev, "%s\n", __func__);
  1423. if (mutex_lock_interruptible(&core->lock))
  1424. return -EINTR;
  1425. freq->type = V4L2_TUNER_RADIO;
  1426. freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
  1427. mutex_unlock(&core->lock);
  1428. return 0;
  1429. }
  1430. static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
  1431. struct v4l2_frequency *freq)
  1432. {
  1433. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1434. struct wl1273_core *core = radio->core;
  1435. int r;
  1436. dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
  1437. if (freq->type != V4L2_TUNER_RADIO) {
  1438. dev_dbg(radio->dev,
  1439. "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
  1440. return -EINVAL;
  1441. }
  1442. if (mutex_lock_interruptible(&core->lock))
  1443. return -EINTR;
  1444. if (core->mode == WL1273_MODE_RX) {
  1445. dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
  1446. r = wl1273_fm_set_rx_freq(radio,
  1447. WL1273_INV_FREQ(freq->frequency));
  1448. if (r)
  1449. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1450. ": set frequency failed with %d\n", r);
  1451. } else {
  1452. r = wl1273_fm_set_tx_freq(radio,
  1453. WL1273_INV_FREQ(freq->frequency));
  1454. if (r)
  1455. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1456. ": set frequency failed with %d\n", r);
  1457. }
  1458. mutex_unlock(&core->lock);
  1459. dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
  1460. return r;
  1461. }
  1462. #define WL1273_DEFAULT_SEEK_LEVEL 7
  1463. static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
  1464. struct v4l2_hw_freq_seek *seek)
  1465. {
  1466. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1467. struct wl1273_core *core = radio->core;
  1468. int r;
  1469. dev_dbg(radio->dev, "%s\n", __func__);
  1470. if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
  1471. return -EINVAL;
  1472. if (mutex_lock_interruptible(&core->lock))
  1473. return -EINTR;
  1474. r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
  1475. if (r)
  1476. goto out;
  1477. r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
  1478. if (r)
  1479. dev_warn(radio->dev, "HW seek failed: %d\n", r);
  1480. r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
  1481. WL1273_DEFAULT_SEEK_LEVEL);
  1482. if (r)
  1483. dev_warn(radio->dev, "HW seek failed: %d\n", r);
  1484. out:
  1485. mutex_unlock(&core->lock);
  1486. return r;
  1487. }
  1488. static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
  1489. struct v4l2_modulator *modulator)
  1490. {
  1491. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1492. struct wl1273_core *core = radio->core;
  1493. int r = 0;
  1494. dev_dbg(radio->dev, "%s\n", __func__);
  1495. if (modulator->index > 0)
  1496. return -EINVAL;
  1497. if (mutex_lock_interruptible(&core->lock))
  1498. return -EINTR;
  1499. r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
  1500. if (r)
  1501. goto out;
  1502. if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
  1503. r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
  1504. else
  1505. r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
  1506. if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
  1507. r = wl1273_fm_write_cmd(core, WL1273_MONO_SET, WL1273_TX_MONO);
  1508. else
  1509. r = wl1273_fm_write_cmd(core, WL1273_MONO_SET,
  1510. WL1273_RX_STEREO);
  1511. if (r < 0)
  1512. dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
  1513. "MONO_SET fails: %d\n", r);
  1514. out:
  1515. mutex_unlock(&core->lock);
  1516. return r;
  1517. }
  1518. static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
  1519. struct v4l2_modulator *modulator)
  1520. {
  1521. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1522. struct wl1273_core *core = radio->core;
  1523. u16 val;
  1524. int r;
  1525. dev_dbg(radio->dev, "%s\n", __func__);
  1526. strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
  1527. sizeof(modulator->name));
  1528. modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
  1529. modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
  1530. modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
  1531. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
  1532. if (core->mode != WL1273_MODE_TX)
  1533. return 0;
  1534. if (mutex_lock_interruptible(&core->lock))
  1535. return -EINTR;
  1536. r = wl1273_fm_read_reg(core, WL1273_MONO_SET, &val);
  1537. if (r)
  1538. goto out;
  1539. if (val == WL1273_TX_STEREO)
  1540. modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
  1541. else
  1542. modulator->txsubchans = V4L2_TUNER_SUB_MONO;
  1543. if (radio->rds_on)
  1544. modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
  1545. out:
  1546. mutex_unlock(&core->lock);
  1547. return 0;
  1548. }
  1549. static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
  1550. {
  1551. struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
  1552. struct wl1273_core *core = radio->core;
  1553. struct device *dev = radio->dev;
  1554. u16 val;
  1555. int r;
  1556. dev_info(dev, DRIVER_DESC);
  1557. if (core->mode == WL1273_MODE_OFF) {
  1558. dev_info(dev, "Mode: Off\n");
  1559. return 0;
  1560. }
  1561. if (core->mode == WL1273_MODE_SUSPENDED) {
  1562. dev_info(dev, "Mode: Suspended\n");
  1563. return 0;
  1564. }
  1565. r = wl1273_fm_read_reg(core, WL1273_ASIC_ID_GET, &val);
  1566. if (r)
  1567. dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
  1568. else
  1569. dev_info(dev, "ASIC_ID: 0x%04x\n", val);
  1570. r = wl1273_fm_read_reg(core, WL1273_ASIC_VER_GET, &val);
  1571. if (r)
  1572. dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
  1573. else
  1574. dev_info(dev, "ASIC Version: 0x%04x\n", val);
  1575. r = wl1273_fm_read_reg(core, WL1273_FIRM_VER_GET, &val);
  1576. if (r)
  1577. dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
  1578. else
  1579. dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
  1580. r = wl1273_fm_read_reg(core, WL1273_BAND_SET, &val);
  1581. if (r)
  1582. dev_err(dev, "%s: Get BAND fails.\n", __func__);
  1583. else
  1584. dev_info(dev, "BAND: %d\n", val);
  1585. if (core->mode == WL1273_MODE_TX) {
  1586. r = wl1273_fm_read_reg(core, WL1273_PUPD_SET, &val);
  1587. if (r)
  1588. dev_err(dev, "%s: Get PUPD fails.\n", __func__);
  1589. else
  1590. dev_info(dev, "PUPD: 0x%04x\n", val);
  1591. r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &val);
  1592. if (r)
  1593. dev_err(dev, "%s: Get CHANL fails.\n", __func__);
  1594. else
  1595. dev_info(dev, "Tx frequency: %dkHz\n", val*10);
  1596. } else if (core->mode == WL1273_MODE_RX) {
  1597. int bf = radio->rangelow;
  1598. r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &val);
  1599. if (r)
  1600. dev_err(dev, "%s: Get FREQ fails.\n", __func__);
  1601. else
  1602. dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
  1603. r = wl1273_fm_read_reg(core, WL1273_MOST_MODE_SET, &val);
  1604. if (r)
  1605. dev_err(dev, "%s: Get MOST_MODE fails.\n",
  1606. __func__);
  1607. else if (val == 0)
  1608. dev_info(dev, "MOST_MODE: Stereo according to blend\n");
  1609. else if (val == 1)
  1610. dev_info(dev, "MOST_MODE: Force mono output\n");
  1611. else
  1612. dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
  1613. r = wl1273_fm_read_reg(core, WL1273_MOST_BLEND_SET, &val);
  1614. if (r)
  1615. dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
  1616. else if (val == 0)
  1617. dev_info(dev,
  1618. "MOST_BLEND: Switched blend & hysteresis.\n");
  1619. else if (val == 1)
  1620. dev_info(dev, "MOST_BLEND: Soft blend.\n");
  1621. else
  1622. dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
  1623. r = wl1273_fm_read_reg(core, WL1273_STEREO_GET, &val);
  1624. if (r)
  1625. dev_err(dev, "%s: Get STEREO fails.\n", __func__);
  1626. else if (val == 0)
  1627. dev_info(dev, "STEREO: Not detected\n");
  1628. else if (val == 1)
  1629. dev_info(dev, "STEREO: Detected\n");
  1630. else
  1631. dev_info(dev, "STEREO: Unexpected value: %d\n", val);
  1632. r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val);
  1633. if (r)
  1634. dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
  1635. else
  1636. dev_info(dev, "RX signal strength: %d\n", (s16) val);
  1637. r = wl1273_fm_read_reg(core, WL1273_POWER_SET, &val);
  1638. if (r)
  1639. dev_err(dev, "%s: Get POWER fails.\n", __func__);
  1640. else
  1641. dev_info(dev, "POWER: 0x%04x\n", val);
  1642. r = wl1273_fm_read_reg(core, WL1273_INT_MASK_SET, &val);
  1643. if (r)
  1644. dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
  1645. else
  1646. dev_info(dev, "INT_MASK: 0x%04x\n", val);
  1647. r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
  1648. if (r)
  1649. dev_err(dev, "%s: Get RDS_SYNC fails.\n",
  1650. __func__);
  1651. else if (val == 0)
  1652. dev_info(dev, "RDS_SYNC: Not synchronized\n");
  1653. else if (val == 1)
  1654. dev_info(dev, "RDS_SYNC: Synchronized\n");
  1655. else
  1656. dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
  1657. r = wl1273_fm_read_reg(core, WL1273_I2S_MODE_CONFIG_SET, &val);
  1658. if (r)
  1659. dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
  1660. __func__);
  1661. else
  1662. dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
  1663. r = wl1273_fm_read_reg(core, WL1273_VOLUME_SET, &val);
  1664. if (r)
  1665. dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
  1666. else
  1667. dev_info(dev, "VOLUME: 0x%04x\n", val);
  1668. }
  1669. return 0;
  1670. }
  1671. static void wl1273_vdev_release(struct video_device *dev)
  1672. {
  1673. }
  1674. static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
  1675. .s_ctrl = wl1273_fm_vidioc_s_ctrl,
  1676. .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
  1677. };
  1678. static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
  1679. .vidioc_querycap = wl1273_fm_vidioc_querycap,
  1680. .vidioc_g_input = wl1273_fm_vidioc_g_input,
  1681. .vidioc_s_input = wl1273_fm_vidioc_s_input,
  1682. .vidioc_g_audio = wl1273_fm_vidioc_g_audio,
  1683. .vidioc_s_audio = wl1273_fm_vidioc_s_audio,
  1684. .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
  1685. .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
  1686. .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
  1687. .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
  1688. .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
  1689. .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
  1690. .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
  1691. .vidioc_log_status = wl1273_fm_vidioc_log_status,
  1692. };
  1693. static struct video_device wl1273_viddev_template = {
  1694. .fops = &wl1273_fops,
  1695. .ioctl_ops = &wl1273_ioctl_ops,
  1696. .name = WL1273_FM_DRIVER_NAME,
  1697. .release = wl1273_vdev_release,
  1698. };
  1699. static int wl1273_fm_radio_remove(struct platform_device *pdev)
  1700. {
  1701. struct wl1273_device *radio = platform_get_drvdata(pdev);
  1702. struct wl1273_core *core = radio->core;
  1703. dev_info(&pdev->dev, "%s.\n", __func__);
  1704. free_irq(core->client->irq, radio);
  1705. core->pdata->free_resources();
  1706. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  1707. video_unregister_device(&radio->videodev);
  1708. v4l2_device_unregister(&radio->v4l2dev);
  1709. kfree(radio->buffer);
  1710. kfree(radio->write_buf);
  1711. kfree(radio);
  1712. return 0;
  1713. }
  1714. static int __devinit wl1273_fm_radio_probe(struct platform_device *pdev)
  1715. {
  1716. struct wl1273_core **core = pdev->dev.platform_data;
  1717. struct wl1273_device *radio;
  1718. struct v4l2_ctrl *ctrl;
  1719. int r = 0;
  1720. pr_debug("%s\n", __func__);
  1721. if (!core) {
  1722. dev_err(&pdev->dev, "No platform data.\n");
  1723. r = -EINVAL;
  1724. goto pdata_err;
  1725. }
  1726. radio = kzalloc(sizeof(*radio), GFP_KERNEL);
  1727. if (!radio) {
  1728. r = -ENOMEM;
  1729. goto pdata_err;
  1730. }
  1731. /* RDS buffer allocation */
  1732. radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
  1733. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  1734. if (!radio->buffer) {
  1735. pr_err("Cannot allocate memory for RDS buffer.\n");
  1736. r = -ENOMEM;
  1737. goto err_kmalloc;
  1738. }
  1739. radio->core = *core;
  1740. radio->irq_flags = WL1273_IRQ_MASK;
  1741. radio->dev = &radio->core->client->dev;
  1742. radio->rds_on = false;
  1743. radio->core->mode = WL1273_MODE_OFF;
  1744. radio->tx_power = 118;
  1745. radio->core->audio_mode = WL1273_AUDIO_ANALOG;
  1746. radio->band = WL1273_BAND_OTHER;
  1747. radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
  1748. radio->core->channel_number = 2;
  1749. radio->core->volume = WL1273_DEFAULT_VOLUME;
  1750. radio->rx_frequency = WL1273_BAND_OTHER_LOW;
  1751. radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
  1752. radio->rangelow = WL1273_BAND_OTHER_LOW;
  1753. radio->rangehigh = WL1273_BAND_OTHER_HIGH;
  1754. radio->stereo = true;
  1755. radio->bus_type = "I2C";
  1756. radio->core->write = wl1273_fm_write_cmd;
  1757. radio->core->set_audio = wl1273_fm_set_audio;
  1758. radio->core->set_volume = wl1273_fm_set_volume;
  1759. if (radio->core->pdata->request_resources) {
  1760. r = radio->core->pdata->request_resources(radio->core->client);
  1761. if (r) {
  1762. dev_err(radio->dev, WL1273_FM_DRIVER_NAME
  1763. ": Cannot get platform data\n");
  1764. goto err_resources;
  1765. }
  1766. dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
  1767. r = request_threaded_irq(radio->core->client->irq, NULL,
  1768. wl1273_fm_irq_thread_handler,
  1769. IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
  1770. "wl1273-fm", radio);
  1771. if (r < 0) {
  1772. dev_err(radio->dev, WL1273_FM_DRIVER_NAME
  1773. ": Unable to register IRQ handler: %d\n", r);
  1774. goto err_request_irq;
  1775. }
  1776. } else {
  1777. dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ"
  1778. " not configured");
  1779. r = -EINVAL;
  1780. goto err_resources;
  1781. }
  1782. init_completion(&radio->busy);
  1783. init_waitqueue_head(&radio->read_queue);
  1784. radio->write_buf = kmalloc(256, GFP_KERNEL);
  1785. if (!radio->write_buf) {
  1786. r = -ENOMEM;
  1787. goto write_buf_err;
  1788. }
  1789. radio->dev = &pdev->dev;
  1790. radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
  1791. radio->rds_users = 0;
  1792. r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
  1793. if (r) {
  1794. dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
  1795. goto device_register_err;
  1796. }
  1797. /* V4L2 configuration */
  1798. memcpy(&radio->videodev, &wl1273_viddev_template,
  1799. sizeof(wl1273_viddev_template));
  1800. radio->videodev.v4l2_dev = &radio->v4l2dev;
  1801. v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
  1802. /* add in ascending ID order */
  1803. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1804. V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
  1805. WL1273_DEFAULT_VOLUME);
  1806. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1807. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  1808. v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1809. V4L2_CID_TUNE_PREEMPHASIS,
  1810. V4L2_PREEMPHASIS_75_uS, 0x03,
  1811. V4L2_PREEMPHASIS_50_uS);
  1812. v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1813. V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
  1814. ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
  1815. V4L2_CID_TUNE_ANTENNA_CAPACITOR,
  1816. 0, 255, 1, 255);
  1817. if (ctrl)
  1818. ctrl->is_volatile = 1;
  1819. if (radio->ctrl_handler.error) {
  1820. r = radio->ctrl_handler.error;
  1821. dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
  1822. goto handler_init_err;
  1823. }
  1824. video_set_drvdata(&radio->videodev, radio);
  1825. platform_set_drvdata(pdev, radio);
  1826. /* register video device */
  1827. r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
  1828. if (r) {
  1829. dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
  1830. ": Could not register video device\n");
  1831. goto handler_init_err;
  1832. }
  1833. return 0;
  1834. handler_init_err:
  1835. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  1836. v4l2_device_unregister(&radio->v4l2dev);
  1837. device_register_err:
  1838. kfree(radio->write_buf);
  1839. write_buf_err:
  1840. free_irq(radio->core->client->irq, radio);
  1841. err_request_irq:
  1842. radio->core->pdata->free_resources();
  1843. err_resources:
  1844. kfree(radio->buffer);
  1845. err_kmalloc:
  1846. kfree(radio);
  1847. pdata_err:
  1848. return r;
  1849. }
  1850. MODULE_ALIAS("platform:wl1273_fm_radio");
  1851. static struct platform_driver wl1273_fm_radio_driver = {
  1852. .probe = wl1273_fm_radio_probe,
  1853. .remove = __devexit_p(wl1273_fm_radio_remove),
  1854. .driver = {
  1855. .name = "wl1273_fm_radio",
  1856. .owner = THIS_MODULE,
  1857. },
  1858. };
  1859. static int __init wl1273_fm_module_init(void)
  1860. {
  1861. pr_info("%s\n", __func__);
  1862. return platform_driver_register(&wl1273_fm_radio_driver);
  1863. }
  1864. module_init(wl1273_fm_module_init);
  1865. static void __exit wl1273_fm_module_exit(void)
  1866. {
  1867. flush_scheduled_work();
  1868. platform_driver_unregister(&wl1273_fm_radio_driver);
  1869. pr_info(DRIVER_DESC ", Exiting.\n");
  1870. }
  1871. module_exit(wl1273_fm_module_exit);
  1872. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  1873. MODULE_DESCRIPTION(DRIVER_DESC);
  1874. MODULE_LICENSE("GPL");