si4713-i2c.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. /*
  2. * drivers/media/radio/si4713-i2c.c
  3. *
  4. * Silicon Labs Si4713 FM Radio Transmitter I2C commands.
  5. *
  6. * Copyright (c) 2009 Nokia Corporation
  7. * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/mutex.h>
  24. #include <linux/completion.h>
  25. #include <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/i2c.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-ioctl.h>
  30. #include <media/v4l2-common.h>
  31. #include "si4713-i2c.h"
  32. /* module parameters */
  33. static int debug;
  34. module_param(debug, int, S_IRUGO | S_IWUSR);
  35. MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
  36. MODULE_LICENSE("GPL");
  37. MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
  38. MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
  39. MODULE_VERSION("0.0.1");
  40. #define DEFAULT_RDS_PI 0x00
  41. #define DEFAULT_RDS_PTY 0x00
  42. #define DEFAULT_RDS_PS_NAME ""
  43. #define DEFAULT_RDS_RADIO_TEXT DEFAULT_RDS_PS_NAME
  44. #define DEFAULT_RDS_DEVIATION 0x00C8
  45. #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
  46. #define DEFAULT_LIMITER_RTIME 0x1392
  47. #define DEFAULT_LIMITER_DEV 0x102CA
  48. #define DEFAULT_PILOT_FREQUENCY 0x4A38
  49. #define DEFAULT_PILOT_DEVIATION 0x1A5E
  50. #define DEFAULT_ACOMP_ATIME 0x0000
  51. #define DEFAULT_ACOMP_RTIME 0xF4240L
  52. #define DEFAULT_ACOMP_GAIN 0x0F
  53. #define DEFAULT_ACOMP_THRESHOLD (-0x28)
  54. #define DEFAULT_MUTE 0x01
  55. #define DEFAULT_POWER_LEVEL 88
  56. #define DEFAULT_FREQUENCY 8800
  57. #define DEFAULT_PREEMPHASIS FMPE_EU
  58. #define DEFAULT_TUNE_RNL 0xFF
  59. #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
  60. /* frequency domain transformation (using times 10 to avoid floats) */
  61. #define FREQDEV_UNIT 100000
  62. #define FREQV4L2_MULTI 625
  63. #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
  64. #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
  65. #define FREQ_RANGE_LOW 7600
  66. #define FREQ_RANGE_HIGH 10800
  67. #define MAX_ARGS 7
  68. #define RDS_BLOCK 8
  69. #define RDS_BLOCK_CLEAR 0x03
  70. #define RDS_BLOCK_LOAD 0x04
  71. #define RDS_RADIOTEXT_2A 0x20
  72. #define RDS_RADIOTEXT_BLK_SIZE 4
  73. #define RDS_RADIOTEXT_INDEX_MAX 0x0F
  74. #define RDS_CARRIAGE_RETURN 0x0D
  75. #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
  76. #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
  77. #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
  78. #define ATTACK_TIME_UNIT 500
  79. #define POWER_OFF 0x00
  80. #define POWER_ON 0x01
  81. #define msb(x) ((u8)((u16) x >> 8))
  82. #define lsb(x) ((u8)((u16) x & 0x00FF))
  83. #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
  84. #define check_command_failed(status) (!(status & SI4713_CTS) || \
  85. (status & SI4713_ERR))
  86. /* mute definition */
  87. #define set_mute(p) ((p & 1) | ((p & 1) << 1));
  88. #define get_mute(p) (p & 0x01)
  89. #ifdef DEBUG
  90. #define DBG_BUFFER(device, message, buffer, size) \
  91. { \
  92. int i; \
  93. char str[(size)*5]; \
  94. for (i = 0; i < size; i++) \
  95. sprintf(str + i * 5, " 0x%02x", buffer[i]); \
  96. v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
  97. }
  98. #else
  99. #define DBG_BUFFER(device, message, buffer, size)
  100. #endif
  101. /*
  102. * Values for limiter release time (sorted by second column)
  103. * device release
  104. * value time (us)
  105. */
  106. static long limiter_times[] = {
  107. 2000, 250,
  108. 1000, 500,
  109. 510, 1000,
  110. 255, 2000,
  111. 170, 3000,
  112. 127, 4020,
  113. 102, 5010,
  114. 85, 6020,
  115. 73, 7010,
  116. 64, 7990,
  117. 57, 8970,
  118. 51, 10030,
  119. 25, 20470,
  120. 17, 30110,
  121. 13, 39380,
  122. 10, 51190,
  123. 8, 63690,
  124. 7, 73140,
  125. 6, 85330,
  126. 5, 102390,
  127. };
  128. /*
  129. * Values for audio compression release time (sorted by second column)
  130. * device release
  131. * value time (us)
  132. */
  133. static unsigned long acomp_rtimes[] = {
  134. 0, 100000,
  135. 1, 200000,
  136. 2, 350000,
  137. 3, 525000,
  138. 4, 1000000,
  139. };
  140. /*
  141. * Values for preemphasis (sorted by second column)
  142. * device preemphasis
  143. * value value (v4l2)
  144. */
  145. static unsigned long preemphasis_values[] = {
  146. FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
  147. FMPE_EU, V4L2_PREEMPHASIS_50_uS,
  148. FMPE_USA, V4L2_PREEMPHASIS_75_uS,
  149. };
  150. static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
  151. int size)
  152. {
  153. int i;
  154. int rval = -EINVAL;
  155. for (i = 0; i < size / 2; i++)
  156. if (array[(i * 2) + 1] >= usecs) {
  157. rval = array[i * 2];
  158. break;
  159. }
  160. return rval;
  161. }
  162. static unsigned long dev_to_usecs(int value, unsigned long const array[],
  163. int size)
  164. {
  165. int i;
  166. int rval = -EINVAL;
  167. for (i = 0; i < size / 2; i++)
  168. if (array[i * 2] == value) {
  169. rval = array[(i * 2) + 1];
  170. break;
  171. }
  172. return rval;
  173. }
  174. /* si4713_handler: IRQ handler, just complete work */
  175. static irqreturn_t si4713_handler(int irq, void *dev)
  176. {
  177. struct si4713_device *sdev = dev;
  178. v4l2_dbg(2, debug, &sdev->sd,
  179. "%s: sending signal to completion work.\n", __func__);
  180. complete(&sdev->work);
  181. return IRQ_HANDLED;
  182. }
  183. /*
  184. * si4713_send_command - sends a command to si4713 and waits its response
  185. * @sdev: si4713_device structure for the device we are communicating
  186. * @command: command id
  187. * @args: command arguments we are sending (up to 7)
  188. * @argn: actual size of @args
  189. * @response: buffer to place the expected response from the device (up to 15)
  190. * @respn: actual size of @response
  191. * @usecs: amount of time to wait before reading the response (in usecs)
  192. */
  193. static int si4713_send_command(struct si4713_device *sdev, const u8 command,
  194. const u8 args[], const int argn,
  195. u8 response[], const int respn, const int usecs)
  196. {
  197. struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
  198. u8 data1[MAX_ARGS + 1];
  199. int err;
  200. if (!client->adapter)
  201. return -ENODEV;
  202. /* First send the command and its arguments */
  203. data1[0] = command;
  204. memcpy(data1 + 1, args, argn);
  205. DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
  206. err = i2c_master_send(client, data1, argn + 1);
  207. if (err != argn + 1) {
  208. v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
  209. command);
  210. return (err > 0) ? -EIO : err;
  211. }
  212. /* Wait response from interrupt */
  213. if (!wait_for_completion_timeout(&sdev->work,
  214. usecs_to_jiffies(usecs) + 1))
  215. v4l2_warn(&sdev->sd,
  216. "(%s) Device took too much time to answer.\n",
  217. __func__);
  218. /* Then get the response */
  219. err = i2c_master_recv(client, response, respn);
  220. if (err != respn) {
  221. v4l2_err(&sdev->sd,
  222. "Error while reading response for command 0x%02x\n",
  223. command);
  224. return (err > 0) ? -EIO : err;
  225. }
  226. DBG_BUFFER(&sdev->sd, "Response", response, respn);
  227. if (check_command_failed(response[0]))
  228. return -EBUSY;
  229. return 0;
  230. }
  231. /*
  232. * si4713_read_property - reads a si4713 property
  233. * @sdev: si4713_device structure for the device we are communicating
  234. * @prop: property identification number
  235. * @pv: property value to be returned on success
  236. */
  237. static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
  238. {
  239. int err;
  240. u8 val[SI4713_GET_PROP_NRESP];
  241. /*
  242. * .First byte = 0
  243. * .Second byte = property's MSB
  244. * .Third byte = property's LSB
  245. */
  246. const u8 args[SI4713_GET_PROP_NARGS] = {
  247. 0x00,
  248. msb(prop),
  249. lsb(prop),
  250. };
  251. err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
  252. args, ARRAY_SIZE(args), val,
  253. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  254. if (err < 0)
  255. return err;
  256. *pv = compose_u16(val[2], val[3]);
  257. v4l2_dbg(1, debug, &sdev->sd,
  258. "%s: property=0x%02x value=0x%02x status=0x%02x\n",
  259. __func__, prop, *pv, val[0]);
  260. return err;
  261. }
  262. /*
  263. * si4713_write_property - modifies a si4713 property
  264. * @sdev: si4713_device structure for the device we are communicating
  265. * @prop: property identification number
  266. * @val: new value for that property
  267. */
  268. static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
  269. {
  270. int rval;
  271. u8 resp[SI4713_SET_PROP_NRESP];
  272. /*
  273. * .First byte = 0
  274. * .Second byte = property's MSB
  275. * .Third byte = property's LSB
  276. * .Fourth byte = value's MSB
  277. * .Fifth byte = value's LSB
  278. */
  279. const u8 args[SI4713_SET_PROP_NARGS] = {
  280. 0x00,
  281. msb(prop),
  282. lsb(prop),
  283. msb(val),
  284. lsb(val),
  285. };
  286. rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
  287. args, ARRAY_SIZE(args),
  288. resp, ARRAY_SIZE(resp),
  289. DEFAULT_TIMEOUT);
  290. if (rval < 0)
  291. return rval;
  292. v4l2_dbg(1, debug, &sdev->sd,
  293. "%s: property=0x%02x value=0x%02x status=0x%02x\n",
  294. __func__, prop, val, resp[0]);
  295. /*
  296. * As there is no command response for SET_PROPERTY,
  297. * wait Tcomp time to finish before proceed, in order
  298. * to have property properly set.
  299. */
  300. msleep(TIMEOUT_SET_PROPERTY);
  301. return rval;
  302. }
  303. /*
  304. * si4713_powerup - Powers the device up
  305. * @sdev: si4713_device structure for the device we are communicating
  306. */
  307. static int si4713_powerup(struct si4713_device *sdev)
  308. {
  309. int err;
  310. u8 resp[SI4713_PWUP_NRESP];
  311. /*
  312. * .First byte = Enabled interrupts and boot function
  313. * .Second byte = Input operation mode
  314. */
  315. const u8 args[SI4713_PWUP_NARGS] = {
  316. SI4713_PWUP_CTSIEN | SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
  317. SI4713_PWUP_OPMOD_ANALOG,
  318. };
  319. if (sdev->power_state)
  320. return 0;
  321. sdev->platform_data->set_power(1);
  322. err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
  323. args, ARRAY_SIZE(args),
  324. resp, ARRAY_SIZE(resp),
  325. TIMEOUT_POWER_UP);
  326. if (!err) {
  327. v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
  328. resp[0]);
  329. v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
  330. sdev->power_state = POWER_ON;
  331. err = si4713_write_property(sdev, SI4713_GPO_IEN,
  332. SI4713_STC_INT | SI4713_CTS);
  333. } else {
  334. sdev->platform_data->set_power(0);
  335. }
  336. return err;
  337. }
  338. /*
  339. * si4713_powerdown - Powers the device down
  340. * @sdev: si4713_device structure for the device we are communicating
  341. */
  342. static int si4713_powerdown(struct si4713_device *sdev)
  343. {
  344. int err;
  345. u8 resp[SI4713_PWDN_NRESP];
  346. if (!sdev->power_state)
  347. return 0;
  348. err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
  349. NULL, 0,
  350. resp, ARRAY_SIZE(resp),
  351. DEFAULT_TIMEOUT);
  352. if (!err) {
  353. v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
  354. resp[0]);
  355. v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
  356. sdev->platform_data->set_power(0);
  357. sdev->power_state = POWER_OFF;
  358. }
  359. return err;
  360. }
  361. /*
  362. * si4713_checkrev - Checks if we are treating a device with the correct rev.
  363. * @sdev: si4713_device structure for the device we are communicating
  364. */
  365. static int si4713_checkrev(struct si4713_device *sdev)
  366. {
  367. struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
  368. int rval;
  369. u8 resp[SI4713_GETREV_NRESP];
  370. mutex_lock(&sdev->mutex);
  371. rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
  372. NULL, 0,
  373. resp, ARRAY_SIZE(resp),
  374. DEFAULT_TIMEOUT);
  375. if (rval < 0)
  376. goto unlock;
  377. if (resp[1] == SI4713_PRODUCT_NUMBER) {
  378. v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
  379. client->addr << 1, client->adapter->name);
  380. } else {
  381. v4l2_err(&sdev->sd, "Invalid product number\n");
  382. rval = -EINVAL;
  383. }
  384. unlock:
  385. mutex_unlock(&sdev->mutex);
  386. return rval;
  387. }
  388. /*
  389. * si4713_wait_stc - Waits STC interrupt and clears status bits. Usefull
  390. * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
  391. * @sdev: si4713_device structure for the device we are communicating
  392. * @usecs: timeout to wait for STC interrupt signal
  393. */
  394. static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
  395. {
  396. int err;
  397. u8 resp[SI4713_GET_STATUS_NRESP];
  398. /* Wait response from STC interrupt */
  399. if (!wait_for_completion_timeout(&sdev->work,
  400. usecs_to_jiffies(usecs) + 1))
  401. v4l2_warn(&sdev->sd,
  402. "%s: device took too much time to answer (%d usec).\n",
  403. __func__, usecs);
  404. /* Clear status bits */
  405. err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
  406. NULL, 0,
  407. resp, ARRAY_SIZE(resp),
  408. DEFAULT_TIMEOUT);
  409. if (err < 0)
  410. goto exit;
  411. v4l2_dbg(1, debug, &sdev->sd,
  412. "%s: status bits: 0x%02x\n", __func__, resp[0]);
  413. if (!(resp[0] & SI4713_STC_INT))
  414. err = -EIO;
  415. exit:
  416. return err;
  417. }
  418. /*
  419. * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
  420. * frequency between 76 and 108 MHz in 10 kHz units and
  421. * steps of 50 kHz.
  422. * @sdev: si4713_device structure for the device we are communicating
  423. * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
  424. */
  425. static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
  426. {
  427. int err;
  428. u8 val[SI4713_TXFREQ_NRESP];
  429. /*
  430. * .First byte = 0
  431. * .Second byte = frequency's MSB
  432. * .Third byte = frequency's LSB
  433. */
  434. const u8 args[SI4713_TXFREQ_NARGS] = {
  435. 0x00,
  436. msb(frequency),
  437. lsb(frequency),
  438. };
  439. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
  440. args, ARRAY_SIZE(args), val,
  441. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  442. if (err < 0)
  443. return err;
  444. v4l2_dbg(1, debug, &sdev->sd,
  445. "%s: frequency=0x%02x status=0x%02x\n", __func__,
  446. frequency, val[0]);
  447. err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
  448. if (err < 0)
  449. return err;
  450. return compose_u16(args[1], args[2]);
  451. }
  452. /*
  453. * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
  454. * 1 dB units. A value of 0x00 indicates off. The command
  455. * also sets the antenna tuning capacitance. A value of 0
  456. * indicates autotuning, and a value of 1 - 191 indicates
  457. * a manual override, which results in a tuning
  458. * capacitance of 0.25 pF x @antcap.
  459. * @sdev: si4713_device structure for the device we are communicating
  460. * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
  461. * @antcap: value of antenna tuning capacitor (0 - 191)
  462. */
  463. static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
  464. u8 antcap)
  465. {
  466. int err;
  467. u8 val[SI4713_TXPWR_NRESP];
  468. /*
  469. * .First byte = 0
  470. * .Second byte = 0
  471. * .Third byte = power
  472. * .Fourth byte = antcap
  473. */
  474. const u8 args[SI4713_TXPWR_NARGS] = {
  475. 0x00,
  476. 0x00,
  477. power,
  478. antcap,
  479. };
  480. if (((power > 0) && (power < SI4713_MIN_POWER)) ||
  481. power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
  482. return -EDOM;
  483. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
  484. args, ARRAY_SIZE(args), val,
  485. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  486. if (err < 0)
  487. return err;
  488. v4l2_dbg(1, debug, &sdev->sd,
  489. "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
  490. __func__, power, antcap, val[0]);
  491. return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
  492. }
  493. /*
  494. * si4713_tx_tune_measure - Enters receive mode and measures the received noise
  495. * level in units of dBuV on the selected frequency.
  496. * The Frequency must be between 76 and 108 MHz in 10 kHz
  497. * units and steps of 50 kHz. The command also sets the
  498. * antenna tuning capacitance. A value of 0 means
  499. * autotuning, and a value of 1 to 191 indicates manual
  500. * override.
  501. * @sdev: si4713_device structure for the device we are communicating
  502. * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
  503. * @antcap: value of antenna tuning capacitor (0 - 191)
  504. */
  505. static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
  506. u8 antcap)
  507. {
  508. int err;
  509. u8 val[SI4713_TXMEA_NRESP];
  510. /*
  511. * .First byte = 0
  512. * .Second byte = frequency's MSB
  513. * .Third byte = frequency's LSB
  514. * .Fourth byte = antcap
  515. */
  516. const u8 args[SI4713_TXMEA_NARGS] = {
  517. 0x00,
  518. msb(frequency),
  519. lsb(frequency),
  520. antcap,
  521. };
  522. sdev->tune_rnl = DEFAULT_TUNE_RNL;
  523. if (antcap > SI4713_MAX_ANTCAP)
  524. return -EDOM;
  525. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
  526. args, ARRAY_SIZE(args), val,
  527. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  528. if (err < 0)
  529. return err;
  530. v4l2_dbg(1, debug, &sdev->sd,
  531. "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
  532. __func__, frequency, antcap, val[0]);
  533. return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
  534. }
  535. /*
  536. * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
  537. * tx_tune_power commands. This command return the current
  538. * frequency, output voltage in dBuV, the antenna tunning
  539. * capacitance value and the received noise level. The
  540. * command also clears the stcint interrupt bit when the
  541. * first bit of its arguments is high.
  542. * @sdev: si4713_device structure for the device we are communicating
  543. * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
  544. * @frequency: returned frequency
  545. * @power: returned power
  546. * @antcap: returned antenna capacitance
  547. * @noise: returned noise level
  548. */
  549. static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
  550. u16 *frequency, u8 *power,
  551. u8 *antcap, u8 *noise)
  552. {
  553. int err;
  554. u8 val[SI4713_TXSTATUS_NRESP];
  555. /*
  556. * .First byte = intack bit
  557. */
  558. const u8 args[SI4713_TXSTATUS_NARGS] = {
  559. intack & SI4713_INTACK_MASK,
  560. };
  561. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
  562. args, ARRAY_SIZE(args), val,
  563. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  564. if (!err) {
  565. v4l2_dbg(1, debug, &sdev->sd,
  566. "%s: status=0x%02x\n", __func__, val[0]);
  567. *frequency = compose_u16(val[2], val[3]);
  568. sdev->frequency = *frequency;
  569. *power = val[5];
  570. *antcap = val[6];
  571. *noise = val[7];
  572. v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
  573. "(power %d, antcap %d, rnl %d)\n", __func__,
  574. *frequency, *power, *antcap, *noise);
  575. }
  576. return err;
  577. }
  578. /*
  579. * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
  580. * @sdev: si4713_device structure for the device we are communicating
  581. * @mode: the buffer operation mode.
  582. * @rdsb: RDS Block B
  583. * @rdsc: RDS Block C
  584. * @rdsd: RDS Block D
  585. * @cbleft: returns the number of available circular buffer blocks minus the
  586. * number of used circular buffer blocks.
  587. */
  588. static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
  589. u16 rdsc, u16 rdsd, s8 *cbleft)
  590. {
  591. int err;
  592. u8 val[SI4713_RDSBUFF_NRESP];
  593. const u8 args[SI4713_RDSBUFF_NARGS] = {
  594. mode & SI4713_RDSBUFF_MODE_MASK,
  595. msb(rdsb),
  596. lsb(rdsb),
  597. msb(rdsc),
  598. lsb(rdsc),
  599. msb(rdsd),
  600. lsb(rdsd),
  601. };
  602. err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
  603. args, ARRAY_SIZE(args), val,
  604. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  605. if (!err) {
  606. v4l2_dbg(1, debug, &sdev->sd,
  607. "%s: status=0x%02x\n", __func__, val[0]);
  608. *cbleft = (s8)val[2] - val[3];
  609. v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
  610. " 0x%02x cb avail: %d cb used %d fifo avail"
  611. " %d fifo used %d\n", __func__, val[1],
  612. val[2], val[3], val[4], val[5]);
  613. }
  614. return err;
  615. }
  616. /*
  617. * si4713_tx_rds_ps - Loads the program service buffer.
  618. * @sdev: si4713_device structure for the device we are communicating
  619. * @psid: program service id to be loaded.
  620. * @pschar: assumed 4 size char array to be loaded into the program service
  621. */
  622. static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
  623. unsigned char *pschar)
  624. {
  625. int err;
  626. u8 val[SI4713_RDSPS_NRESP];
  627. const u8 args[SI4713_RDSPS_NARGS] = {
  628. psid & SI4713_RDSPS_PSID_MASK,
  629. pschar[0],
  630. pschar[1],
  631. pschar[2],
  632. pschar[3],
  633. };
  634. err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
  635. args, ARRAY_SIZE(args), val,
  636. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  637. if (err < 0)
  638. return err;
  639. v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
  640. return err;
  641. }
  642. static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
  643. {
  644. int rval;
  645. mutex_lock(&sdev->mutex);
  646. if (value)
  647. rval = si4713_powerup(sdev);
  648. else
  649. rval = si4713_powerdown(sdev);
  650. mutex_unlock(&sdev->mutex);
  651. return rval;
  652. }
  653. static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
  654. {
  655. int rval = 0;
  656. mute = set_mute(mute);
  657. mutex_lock(&sdev->mutex);
  658. if (sdev->power_state)
  659. rval = si4713_write_property(sdev,
  660. SI4713_TX_LINE_INPUT_MUTE, mute);
  661. if (rval >= 0)
  662. sdev->mute = get_mute(mute);
  663. mutex_unlock(&sdev->mutex);
  664. return rval;
  665. }
  666. static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
  667. {
  668. int rval = 0, i;
  669. u8 len = 0;
  670. /* We want to clear the whole thing */
  671. if (!strlen(ps_name))
  672. memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
  673. mutex_lock(&sdev->mutex);
  674. if (sdev->power_state) {
  675. /* Write the new ps name and clear the padding */
  676. for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
  677. rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
  678. ps_name + i);
  679. if (rval < 0)
  680. goto unlock;
  681. }
  682. /* Setup the size to be sent */
  683. if (strlen(ps_name))
  684. len = strlen(ps_name) - 1;
  685. else
  686. len = 1;
  687. rval = si4713_write_property(sdev,
  688. SI4713_TX_RDS_PS_MESSAGE_COUNT,
  689. rds_ps_nblocks(len));
  690. if (rval < 0)
  691. goto unlock;
  692. rval = si4713_write_property(sdev,
  693. SI4713_TX_RDS_PS_REPEAT_COUNT,
  694. DEFAULT_RDS_PS_REPEAT_COUNT * 2);
  695. if (rval < 0)
  696. goto unlock;
  697. }
  698. strncpy(sdev->rds_info.ps_name, ps_name, MAX_RDS_PS_NAME);
  699. unlock:
  700. mutex_unlock(&sdev->mutex);
  701. return rval;
  702. }
  703. static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
  704. {
  705. int rval = 0, i;
  706. u16 t_index = 0;
  707. u8 b_index = 0, cr_inserted = 0;
  708. s8 left;
  709. mutex_lock(&sdev->mutex);
  710. if (!sdev->power_state)
  711. goto copy;
  712. rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
  713. if (rval < 0)
  714. goto unlock;
  715. if (!strlen(rt))
  716. goto copy;
  717. do {
  718. /* RDS spec says that if the last block isn't used,
  719. * then apply a carriage return
  720. */
  721. if (t_index < (RDS_RADIOTEXT_INDEX_MAX *
  722. RDS_RADIOTEXT_BLK_SIZE)) {
  723. for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
  724. if (!rt[t_index + i] || rt[t_index + i] ==
  725. RDS_CARRIAGE_RETURN) {
  726. rt[t_index + i] = RDS_CARRIAGE_RETURN;
  727. cr_inserted = 1;
  728. break;
  729. }
  730. }
  731. }
  732. rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
  733. compose_u16(RDS_RADIOTEXT_2A, b_index++),
  734. compose_u16(rt[t_index], rt[t_index + 1]),
  735. compose_u16(rt[t_index + 2], rt[t_index + 3]),
  736. &left);
  737. if (rval < 0)
  738. goto unlock;
  739. t_index += RDS_RADIOTEXT_BLK_SIZE;
  740. if (cr_inserted)
  741. break;
  742. } while (left > 0);
  743. copy:
  744. strncpy(sdev->rds_info.radio_text, rt, MAX_RDS_RADIO_TEXT);
  745. unlock:
  746. mutex_unlock(&sdev->mutex);
  747. return rval;
  748. }
  749. static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
  750. u32 **shadow, s32 *bit, s32 *mask, u16 *property, int *mul,
  751. unsigned long **table, int *size)
  752. {
  753. s32 rval = 0;
  754. switch (id) {
  755. /* FM_TX class controls */
  756. case V4L2_CID_RDS_TX_PI:
  757. *property = SI4713_TX_RDS_PI;
  758. *mul = 1;
  759. *shadow = &sdev->rds_info.pi;
  760. break;
  761. case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
  762. *property = SI4713_TX_ACOMP_THRESHOLD;
  763. *mul = 1;
  764. *shadow = &sdev->acomp_info.threshold;
  765. break;
  766. case V4L2_CID_AUDIO_COMPRESSION_GAIN:
  767. *property = SI4713_TX_ACOMP_GAIN;
  768. *mul = 1;
  769. *shadow = &sdev->acomp_info.gain;
  770. break;
  771. case V4L2_CID_PILOT_TONE_FREQUENCY:
  772. *property = SI4713_TX_PILOT_FREQUENCY;
  773. *mul = 1;
  774. *shadow = &sdev->pilot_info.frequency;
  775. break;
  776. case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
  777. *property = SI4713_TX_ACOMP_ATTACK_TIME;
  778. *mul = ATTACK_TIME_UNIT;
  779. *shadow = &sdev->acomp_info.attack_time;
  780. break;
  781. case V4L2_CID_PILOT_TONE_DEVIATION:
  782. *property = SI4713_TX_PILOT_DEVIATION;
  783. *mul = 10;
  784. *shadow = &sdev->pilot_info.deviation;
  785. break;
  786. case V4L2_CID_AUDIO_LIMITER_DEVIATION:
  787. *property = SI4713_TX_AUDIO_DEVIATION;
  788. *mul = 10;
  789. *shadow = &sdev->limiter_info.deviation;
  790. break;
  791. case V4L2_CID_RDS_TX_DEVIATION:
  792. *property = SI4713_TX_RDS_DEVIATION;
  793. *mul = 1;
  794. *shadow = &sdev->rds_info.deviation;
  795. break;
  796. case V4L2_CID_RDS_TX_PTY:
  797. *property = SI4713_TX_RDS_PS_MISC;
  798. *bit = 5;
  799. *mask = 0x1F << 5;
  800. *shadow = &sdev->rds_info.pty;
  801. break;
  802. case V4L2_CID_AUDIO_LIMITER_ENABLED:
  803. *property = SI4713_TX_ACOMP_ENABLE;
  804. *bit = 1;
  805. *mask = 1 << 1;
  806. *shadow = &sdev->limiter_info.enabled;
  807. break;
  808. case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
  809. *property = SI4713_TX_ACOMP_ENABLE;
  810. *bit = 0;
  811. *mask = 1 << 0;
  812. *shadow = &sdev->acomp_info.enabled;
  813. break;
  814. case V4L2_CID_PILOT_TONE_ENABLED:
  815. *property = SI4713_TX_COMPONENT_ENABLE;
  816. *bit = 0;
  817. *mask = 1 << 0;
  818. *shadow = &sdev->pilot_info.enabled;
  819. break;
  820. case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
  821. *property = SI4713_TX_LIMITER_RELEASE_TIME;
  822. *table = limiter_times;
  823. *size = ARRAY_SIZE(limiter_times);
  824. *shadow = &sdev->limiter_info.release_time;
  825. break;
  826. case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
  827. *property = SI4713_TX_ACOMP_RELEASE_TIME;
  828. *table = acomp_rtimes;
  829. *size = ARRAY_SIZE(acomp_rtimes);
  830. *shadow = &sdev->acomp_info.release_time;
  831. break;
  832. case V4L2_CID_TUNE_PREEMPHASIS:
  833. *property = SI4713_TX_PREEMPHASIS;
  834. *table = preemphasis_values;
  835. *size = ARRAY_SIZE(preemphasis_values);
  836. *shadow = &sdev->preemphasis;
  837. break;
  838. default:
  839. rval = -EINVAL;
  840. };
  841. return rval;
  842. }
  843. static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
  844. /* write string property */
  845. static int si4713_write_econtrol_string(struct si4713_device *sdev,
  846. struct v4l2_ext_control *control)
  847. {
  848. struct v4l2_queryctrl vqc;
  849. int len;
  850. s32 rval = 0;
  851. vqc.id = control->id;
  852. rval = si4713_queryctrl(&sdev->sd, &vqc);
  853. if (rval < 0)
  854. goto exit;
  855. switch (control->id) {
  856. case V4L2_CID_RDS_TX_PS_NAME: {
  857. char ps_name[MAX_RDS_PS_NAME + 1];
  858. len = control->size - 1;
  859. if (len > MAX_RDS_PS_NAME) {
  860. rval = -ERANGE;
  861. goto exit;
  862. }
  863. rval = copy_from_user(ps_name, control->string, len);
  864. if (rval < 0)
  865. goto exit;
  866. ps_name[len] = '\0';
  867. if (strlen(ps_name) % vqc.step) {
  868. rval = -ERANGE;
  869. goto exit;
  870. }
  871. rval = si4713_set_rds_ps_name(sdev, ps_name);
  872. }
  873. break;
  874. case V4L2_CID_RDS_TX_RADIO_TEXT: {
  875. char radio_text[MAX_RDS_RADIO_TEXT + 1];
  876. len = control->size - 1;
  877. if (len > MAX_RDS_RADIO_TEXT) {
  878. rval = -ERANGE;
  879. goto exit;
  880. }
  881. rval = copy_from_user(radio_text, control->string, len);
  882. if (rval < 0)
  883. goto exit;
  884. radio_text[len] = '\0';
  885. if (strlen(radio_text) % vqc.step) {
  886. rval = -ERANGE;
  887. goto exit;
  888. }
  889. rval = si4713_set_rds_radio_text(sdev, radio_text);
  890. }
  891. break;
  892. default:
  893. rval = -EINVAL;
  894. break;
  895. };
  896. exit:
  897. return rval;
  898. }
  899. static int validate_range(struct v4l2_subdev *sd,
  900. struct v4l2_ext_control *control)
  901. {
  902. struct v4l2_queryctrl vqc;
  903. int rval;
  904. vqc.id = control->id;
  905. rval = si4713_queryctrl(sd, &vqc);
  906. if (rval < 0)
  907. goto exit;
  908. if (control->value < vqc.minimum || control->value > vqc.maximum)
  909. rval = -ERANGE;
  910. exit:
  911. return rval;
  912. }
  913. /* properties which use tx_tune_power*/
  914. static int si4713_write_econtrol_tune(struct si4713_device *sdev,
  915. struct v4l2_ext_control *control)
  916. {
  917. s32 rval = 0;
  918. u8 power, antcap;
  919. rval = validate_range(&sdev->sd, control);
  920. if (rval < 0)
  921. goto exit;
  922. mutex_lock(&sdev->mutex);
  923. switch (control->id) {
  924. case V4L2_CID_TUNE_POWER_LEVEL:
  925. power = control->value;
  926. antcap = sdev->antenna_capacitor;
  927. break;
  928. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  929. power = sdev->power_level;
  930. antcap = control->value;
  931. break;
  932. default:
  933. rval = -EINVAL;
  934. goto unlock;
  935. };
  936. if (sdev->power_state)
  937. rval = si4713_tx_tune_power(sdev, power, antcap);
  938. if (rval == 0) {
  939. sdev->power_level = power;
  940. sdev->antenna_capacitor = antcap;
  941. }
  942. unlock:
  943. mutex_unlock(&sdev->mutex);
  944. exit:
  945. return rval;
  946. }
  947. static int si4713_write_econtrol_integers(struct si4713_device *sdev,
  948. struct v4l2_ext_control *control)
  949. {
  950. s32 rval;
  951. u32 *shadow = NULL, val = 0;
  952. s32 bit = 0, mask = 0;
  953. u16 property = 0;
  954. int mul = 0;
  955. unsigned long *table = NULL;
  956. int size = 0;
  957. rval = validate_range(&sdev->sd, control);
  958. if (rval < 0)
  959. goto exit;
  960. rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
  961. &mask, &property, &mul, &table, &size);
  962. if (rval < 0)
  963. goto exit;
  964. val = control->value;
  965. if (mul) {
  966. val = control->value / mul;
  967. } else if (table) {
  968. rval = usecs_to_dev(control->value, table, size);
  969. if (rval < 0)
  970. goto exit;
  971. val = rval;
  972. rval = 0;
  973. }
  974. mutex_lock(&sdev->mutex);
  975. if (sdev->power_state) {
  976. if (mask) {
  977. rval = si4713_read_property(sdev, property, &val);
  978. if (rval < 0)
  979. goto unlock;
  980. val = set_bits(val, control->value, bit, mask);
  981. }
  982. rval = si4713_write_property(sdev, property, val);
  983. if (rval < 0)
  984. goto unlock;
  985. if (mask)
  986. val = control->value;
  987. }
  988. if (mul) {
  989. *shadow = val * mul;
  990. } else if (table) {
  991. rval = dev_to_usecs(val, table, size);
  992. if (rval < 0)
  993. goto unlock;
  994. *shadow = rval;
  995. rval = 0;
  996. } else {
  997. *shadow = val;
  998. }
  999. unlock:
  1000. mutex_unlock(&sdev->mutex);
  1001. exit:
  1002. return rval;
  1003. }
  1004. static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
  1005. static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *);
  1006. /*
  1007. * si4713_setup - Sets the device up with current configuration.
  1008. * @sdev: si4713_device structure for the device we are communicating
  1009. */
  1010. static int si4713_setup(struct si4713_device *sdev)
  1011. {
  1012. struct v4l2_ext_control ctrl;
  1013. struct v4l2_frequency f;
  1014. struct v4l2_modulator vm;
  1015. struct si4713_device *tmp;
  1016. int rval = 0;
  1017. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  1018. if (!tmp)
  1019. return -ENOMEM;
  1020. /* Get a local copy to avoid race */
  1021. mutex_lock(&sdev->mutex);
  1022. memcpy(tmp, sdev, sizeof(*sdev));
  1023. mutex_unlock(&sdev->mutex);
  1024. ctrl.id = V4L2_CID_RDS_TX_PI;
  1025. ctrl.value = tmp->rds_info.pi;
  1026. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1027. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_THRESHOLD;
  1028. ctrl.value = tmp->acomp_info.threshold;
  1029. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1030. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_GAIN;
  1031. ctrl.value = tmp->acomp_info.gain;
  1032. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1033. ctrl.id = V4L2_CID_PILOT_TONE_FREQUENCY;
  1034. ctrl.value = tmp->pilot_info.frequency;
  1035. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1036. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME;
  1037. ctrl.value = tmp->acomp_info.attack_time;
  1038. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1039. ctrl.id = V4L2_CID_PILOT_TONE_DEVIATION;
  1040. ctrl.value = tmp->pilot_info.deviation;
  1041. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1042. ctrl.id = V4L2_CID_AUDIO_LIMITER_DEVIATION;
  1043. ctrl.value = tmp->limiter_info.deviation;
  1044. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1045. ctrl.id = V4L2_CID_RDS_TX_DEVIATION;
  1046. ctrl.value = tmp->rds_info.deviation;
  1047. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1048. ctrl.id = V4L2_CID_RDS_TX_PTY;
  1049. ctrl.value = tmp->rds_info.pty;
  1050. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1051. ctrl.id = V4L2_CID_AUDIO_LIMITER_ENABLED;
  1052. ctrl.value = tmp->limiter_info.enabled;
  1053. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1054. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ENABLED;
  1055. ctrl.value = tmp->acomp_info.enabled;
  1056. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1057. ctrl.id = V4L2_CID_PILOT_TONE_ENABLED;
  1058. ctrl.value = tmp->pilot_info.enabled;
  1059. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1060. ctrl.id = V4L2_CID_AUDIO_LIMITER_RELEASE_TIME;
  1061. ctrl.value = tmp->limiter_info.release_time;
  1062. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1063. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME;
  1064. ctrl.value = tmp->acomp_info.release_time;
  1065. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1066. ctrl.id = V4L2_CID_TUNE_PREEMPHASIS;
  1067. ctrl.value = tmp->preemphasis;
  1068. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1069. ctrl.id = V4L2_CID_RDS_TX_PS_NAME;
  1070. rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
  1071. ctrl.id = V4L2_CID_RDS_TX_RADIO_TEXT;
  1072. rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
  1073. /* Device procedure needs to set frequency first */
  1074. f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
  1075. f.frequency = si4713_to_v4l2(f.frequency);
  1076. rval |= si4713_s_frequency(&sdev->sd, &f);
  1077. ctrl.id = V4L2_CID_TUNE_POWER_LEVEL;
  1078. ctrl.value = tmp->power_level;
  1079. rval |= si4713_write_econtrol_tune(sdev, &ctrl);
  1080. ctrl.id = V4L2_CID_TUNE_ANTENNA_CAPACITOR;
  1081. ctrl.value = tmp->antenna_capacitor;
  1082. rval |= si4713_write_econtrol_tune(sdev, &ctrl);
  1083. vm.index = 0;
  1084. if (tmp->stereo)
  1085. vm.txsubchans = V4L2_TUNER_SUB_STEREO;
  1086. else
  1087. vm.txsubchans = V4L2_TUNER_SUB_MONO;
  1088. if (tmp->rds_info.enabled)
  1089. vm.txsubchans |= V4L2_TUNER_SUB_RDS;
  1090. si4713_s_modulator(&sdev->sd, &vm);
  1091. kfree(tmp);
  1092. return rval;
  1093. }
  1094. /*
  1095. * si4713_initialize - Sets the device up with default configuration.
  1096. * @sdev: si4713_device structure for the device we are communicating
  1097. */
  1098. static int si4713_initialize(struct si4713_device *sdev)
  1099. {
  1100. int rval;
  1101. rval = si4713_set_power_state(sdev, POWER_ON);
  1102. if (rval < 0)
  1103. goto exit;
  1104. rval = si4713_checkrev(sdev);
  1105. if (rval < 0)
  1106. goto exit;
  1107. rval = si4713_set_power_state(sdev, POWER_OFF);
  1108. if (rval < 0)
  1109. goto exit;
  1110. mutex_lock(&sdev->mutex);
  1111. sdev->rds_info.pi = DEFAULT_RDS_PI;
  1112. sdev->rds_info.pty = DEFAULT_RDS_PTY;
  1113. sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
  1114. strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
  1115. strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
  1116. MAX_RDS_RADIO_TEXT);
  1117. sdev->rds_info.enabled = 1;
  1118. sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
  1119. sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
  1120. sdev->limiter_info.enabled = 1;
  1121. sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
  1122. sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
  1123. sdev->pilot_info.enabled = 1;
  1124. sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
  1125. sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
  1126. sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
  1127. sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
  1128. sdev->acomp_info.enabled = 1;
  1129. sdev->frequency = DEFAULT_FREQUENCY;
  1130. sdev->preemphasis = DEFAULT_PREEMPHASIS;
  1131. sdev->mute = DEFAULT_MUTE;
  1132. sdev->power_level = DEFAULT_POWER_LEVEL;
  1133. sdev->antenna_capacitor = 0;
  1134. sdev->stereo = 1;
  1135. sdev->tune_rnl = DEFAULT_TUNE_RNL;
  1136. mutex_unlock(&sdev->mutex);
  1137. exit:
  1138. return rval;
  1139. }
  1140. /* read string property */
  1141. static int si4713_read_econtrol_string(struct si4713_device *sdev,
  1142. struct v4l2_ext_control *control)
  1143. {
  1144. s32 rval = 0;
  1145. switch (control->id) {
  1146. case V4L2_CID_RDS_TX_PS_NAME:
  1147. if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
  1148. control->size = MAX_RDS_PS_NAME + 1;
  1149. rval = -ENOSPC;
  1150. goto exit;
  1151. }
  1152. rval = copy_to_user(control->string, sdev->rds_info.ps_name,
  1153. strlen(sdev->rds_info.ps_name) + 1);
  1154. break;
  1155. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1156. if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
  1157. control->size = MAX_RDS_RADIO_TEXT + 1;
  1158. rval = -ENOSPC;
  1159. goto exit;
  1160. }
  1161. rval = copy_to_user(control->string, sdev->rds_info.radio_text,
  1162. strlen(sdev->rds_info.radio_text) + 1);
  1163. break;
  1164. default:
  1165. rval = -EINVAL;
  1166. break;
  1167. };
  1168. exit:
  1169. return rval;
  1170. }
  1171. /*
  1172. * si4713_update_tune_status - update properties from tx_tune_status
  1173. * command. Must be called with sdev->mutex held.
  1174. * @sdev: si4713_device structure for the device we are communicating
  1175. */
  1176. static int si4713_update_tune_status(struct si4713_device *sdev)
  1177. {
  1178. int rval;
  1179. u16 f = 0;
  1180. u8 p = 0, a = 0, n = 0;
  1181. rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
  1182. if (rval < 0)
  1183. goto exit;
  1184. sdev->power_level = p;
  1185. sdev->antenna_capacitor = a;
  1186. sdev->tune_rnl = n;
  1187. exit:
  1188. return rval;
  1189. }
  1190. /* properties which use tx_tune_status */
  1191. static int si4713_read_econtrol_tune(struct si4713_device *sdev,
  1192. struct v4l2_ext_control *control)
  1193. {
  1194. s32 rval = 0;
  1195. mutex_lock(&sdev->mutex);
  1196. if (sdev->power_state) {
  1197. rval = si4713_update_tune_status(sdev);
  1198. if (rval < 0)
  1199. goto unlock;
  1200. }
  1201. switch (control->id) {
  1202. case V4L2_CID_TUNE_POWER_LEVEL:
  1203. control->value = sdev->power_level;
  1204. break;
  1205. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1206. control->value = sdev->antenna_capacitor;
  1207. break;
  1208. default:
  1209. rval = -EINVAL;
  1210. };
  1211. unlock:
  1212. mutex_unlock(&sdev->mutex);
  1213. return rval;
  1214. }
  1215. static int si4713_read_econtrol_integers(struct si4713_device *sdev,
  1216. struct v4l2_ext_control *control)
  1217. {
  1218. s32 rval;
  1219. u32 *shadow = NULL, val = 0;
  1220. s32 bit = 0, mask = 0;
  1221. u16 property = 0;
  1222. int mul = 0;
  1223. unsigned long *table = NULL;
  1224. int size = 0;
  1225. rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
  1226. &mask, &property, &mul, &table, &size);
  1227. if (rval < 0)
  1228. goto exit;
  1229. mutex_lock(&sdev->mutex);
  1230. if (sdev->power_state) {
  1231. rval = si4713_read_property(sdev, property, &val);
  1232. if (rval < 0)
  1233. goto unlock;
  1234. /* Keep negative values for threshold */
  1235. if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
  1236. *shadow = (s16)val;
  1237. else if (mask)
  1238. *shadow = get_status_bit(val, bit, mask);
  1239. else if (mul)
  1240. *shadow = val * mul;
  1241. else
  1242. *shadow = dev_to_usecs(val, table, size);
  1243. }
  1244. control->value = *shadow;
  1245. unlock:
  1246. mutex_unlock(&sdev->mutex);
  1247. exit:
  1248. return rval;
  1249. }
  1250. /*
  1251. * Video4Linux Subdev Interface
  1252. */
  1253. /* si4713_s_ext_ctrls - set extended controls value */
  1254. static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
  1255. struct v4l2_ext_controls *ctrls)
  1256. {
  1257. struct si4713_device *sdev = to_si4713_device(sd);
  1258. int i;
  1259. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  1260. return -EINVAL;
  1261. for (i = 0; i < ctrls->count; i++) {
  1262. int err;
  1263. switch ((ctrls->controls + i)->id) {
  1264. case V4L2_CID_RDS_TX_PS_NAME:
  1265. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1266. err = si4713_write_econtrol_string(sdev,
  1267. ctrls->controls + i);
  1268. break;
  1269. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1270. case V4L2_CID_TUNE_POWER_LEVEL:
  1271. err = si4713_write_econtrol_tune(sdev,
  1272. ctrls->controls + i);
  1273. break;
  1274. default:
  1275. err = si4713_write_econtrol_integers(sdev,
  1276. ctrls->controls + i);
  1277. }
  1278. if (err < 0) {
  1279. ctrls->error_idx = i;
  1280. return err;
  1281. }
  1282. }
  1283. return 0;
  1284. }
  1285. /* si4713_g_ext_ctrls - get extended controls value */
  1286. static int si4713_g_ext_ctrls(struct v4l2_subdev *sd,
  1287. struct v4l2_ext_controls *ctrls)
  1288. {
  1289. struct si4713_device *sdev = to_si4713_device(sd);
  1290. int i;
  1291. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  1292. return -EINVAL;
  1293. for (i = 0; i < ctrls->count; i++) {
  1294. int err;
  1295. switch ((ctrls->controls + i)->id) {
  1296. case V4L2_CID_RDS_TX_PS_NAME:
  1297. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1298. err = si4713_read_econtrol_string(sdev,
  1299. ctrls->controls + i);
  1300. break;
  1301. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1302. case V4L2_CID_TUNE_POWER_LEVEL:
  1303. err = si4713_read_econtrol_tune(sdev,
  1304. ctrls->controls + i);
  1305. break;
  1306. default:
  1307. err = si4713_read_econtrol_integers(sdev,
  1308. ctrls->controls + i);
  1309. }
  1310. if (err < 0) {
  1311. ctrls->error_idx = i;
  1312. return err;
  1313. }
  1314. }
  1315. return 0;
  1316. }
  1317. /* si4713_queryctrl - enumerate control items */
  1318. static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  1319. {
  1320. int rval = 0;
  1321. switch (qc->id) {
  1322. /* User class controls */
  1323. case V4L2_CID_AUDIO_MUTE:
  1324. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
  1325. break;
  1326. /* FM_TX class controls */
  1327. case V4L2_CID_RDS_TX_PI:
  1328. rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
  1329. break;
  1330. case V4L2_CID_RDS_TX_PTY:
  1331. rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
  1332. break;
  1333. case V4L2_CID_RDS_TX_DEVIATION:
  1334. rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_DEVIATION,
  1335. 10, DEFAULT_RDS_DEVIATION);
  1336. break;
  1337. case V4L2_CID_RDS_TX_PS_NAME:
  1338. /*
  1339. * Report step as 8. From RDS spec, psname
  1340. * should be 8. But there are receivers which scroll strings
  1341. * sized as 8xN.
  1342. */
  1343. rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
  1344. break;
  1345. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1346. /*
  1347. * Report step as 32 (2A block). From RDS spec,
  1348. * radio text should be 32 for 2A block. But there are receivers
  1349. * which scroll strings sized as 32xN. Setting default to 32.
  1350. */
  1351. rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
  1352. break;
  1353. case V4L2_CID_AUDIO_LIMITER_ENABLED:
  1354. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1355. break;
  1356. case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
  1357. rval = v4l2_ctrl_query_fill(qc, 250, MAX_LIMITER_RELEASE_TIME,
  1358. 50, DEFAULT_LIMITER_RTIME);
  1359. break;
  1360. case V4L2_CID_AUDIO_LIMITER_DEVIATION:
  1361. rval = v4l2_ctrl_query_fill(qc, 0, MAX_LIMITER_DEVIATION,
  1362. 10, DEFAULT_LIMITER_DEV);
  1363. break;
  1364. case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
  1365. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1366. break;
  1367. case V4L2_CID_AUDIO_COMPRESSION_GAIN:
  1368. rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
  1369. DEFAULT_ACOMP_GAIN);
  1370. break;
  1371. case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
  1372. rval = v4l2_ctrl_query_fill(qc, MIN_ACOMP_THRESHOLD,
  1373. MAX_ACOMP_THRESHOLD, 1,
  1374. DEFAULT_ACOMP_THRESHOLD);
  1375. break;
  1376. case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
  1377. rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_ATTACK_TIME,
  1378. 500, DEFAULT_ACOMP_ATIME);
  1379. break;
  1380. case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
  1381. rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
  1382. 100000, DEFAULT_ACOMP_RTIME);
  1383. break;
  1384. case V4L2_CID_PILOT_TONE_ENABLED:
  1385. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1386. break;
  1387. case V4L2_CID_PILOT_TONE_DEVIATION:
  1388. rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_DEVIATION,
  1389. 10, DEFAULT_PILOT_DEVIATION);
  1390. break;
  1391. case V4L2_CID_PILOT_TONE_FREQUENCY:
  1392. rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_FREQUENCY,
  1393. 1, DEFAULT_PILOT_FREQUENCY);
  1394. break;
  1395. case V4L2_CID_TUNE_PREEMPHASIS:
  1396. rval = v4l2_ctrl_query_fill(qc, V4L2_PREEMPHASIS_DISABLED,
  1397. V4L2_PREEMPHASIS_75_uS, 1,
  1398. V4L2_PREEMPHASIS_50_uS);
  1399. break;
  1400. case V4L2_CID_TUNE_POWER_LEVEL:
  1401. rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
  1402. break;
  1403. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1404. rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
  1405. break;
  1406. default:
  1407. rval = -EINVAL;
  1408. break;
  1409. };
  1410. return rval;
  1411. }
  1412. /* si4713_g_ctrl - get the value of a control */
  1413. static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1414. {
  1415. struct si4713_device *sdev = to_si4713_device(sd);
  1416. int rval = 0;
  1417. if (!sdev)
  1418. return -ENODEV;
  1419. mutex_lock(&sdev->mutex);
  1420. if (sdev->power_state) {
  1421. rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
  1422. &sdev->mute);
  1423. if (rval < 0)
  1424. goto unlock;
  1425. }
  1426. switch (ctrl->id) {
  1427. case V4L2_CID_AUDIO_MUTE:
  1428. ctrl->value = get_mute(sdev->mute);
  1429. break;
  1430. }
  1431. unlock:
  1432. mutex_unlock(&sdev->mutex);
  1433. return rval;
  1434. }
  1435. /* si4713_s_ctrl - set the value of a control */
  1436. static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1437. {
  1438. struct si4713_device *sdev = to_si4713_device(sd);
  1439. int rval = 0;
  1440. if (!sdev)
  1441. return -ENODEV;
  1442. switch (ctrl->id) {
  1443. case V4L2_CID_AUDIO_MUTE:
  1444. if (ctrl->value) {
  1445. rval = si4713_set_mute(sdev, ctrl->value);
  1446. if (rval < 0)
  1447. goto exit;
  1448. rval = si4713_set_power_state(sdev, POWER_DOWN);
  1449. } else {
  1450. rval = si4713_set_power_state(sdev, POWER_UP);
  1451. if (rval < 0)
  1452. goto exit;
  1453. rval = si4713_setup(sdev);
  1454. if (rval < 0)
  1455. goto exit;
  1456. rval = si4713_set_mute(sdev, ctrl->value);
  1457. }
  1458. break;
  1459. }
  1460. exit:
  1461. return rval;
  1462. }
  1463. /* si4713_ioctl - deal with private ioctls (only rnl for now) */
  1464. long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  1465. {
  1466. struct si4713_device *sdev = to_si4713_device(sd);
  1467. struct si4713_rnl *rnl = arg;
  1468. u16 frequency;
  1469. int rval = 0;
  1470. if (!arg)
  1471. return -EINVAL;
  1472. mutex_lock(&sdev->mutex);
  1473. switch (cmd) {
  1474. case SI4713_IOC_MEASURE_RNL:
  1475. frequency = v4l2_to_si4713(rnl->frequency);
  1476. if (sdev->power_state) {
  1477. /* Set desired measurement frequency */
  1478. rval = si4713_tx_tune_measure(sdev, frequency, 0);
  1479. if (rval < 0)
  1480. goto unlock;
  1481. /* get results from tune status */
  1482. rval = si4713_update_tune_status(sdev);
  1483. if (rval < 0)
  1484. goto unlock;
  1485. }
  1486. rnl->rnl = sdev->tune_rnl;
  1487. break;
  1488. default:
  1489. /* nothing */
  1490. rval = -ENOIOCTLCMD;
  1491. }
  1492. unlock:
  1493. mutex_unlock(&sdev->mutex);
  1494. return rval;
  1495. }
  1496. static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
  1497. .queryctrl = si4713_queryctrl,
  1498. .g_ext_ctrls = si4713_g_ext_ctrls,
  1499. .s_ext_ctrls = si4713_s_ext_ctrls,
  1500. .g_ctrl = si4713_g_ctrl,
  1501. .s_ctrl = si4713_s_ctrl,
  1502. .ioctl = si4713_ioctl,
  1503. };
  1504. /* si4713_g_modulator - get modulator attributes */
  1505. static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
  1506. {
  1507. struct si4713_device *sdev = to_si4713_device(sd);
  1508. int rval = 0;
  1509. if (!sdev) {
  1510. rval = -ENODEV;
  1511. goto exit;
  1512. }
  1513. if (vm->index > 0) {
  1514. rval = -EINVAL;
  1515. goto exit;
  1516. }
  1517. strncpy(vm->name, "FM Modulator", 32);
  1518. vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
  1519. V4L2_TUNER_CAP_RDS;
  1520. /* Report current frequency range limits */
  1521. vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
  1522. vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
  1523. mutex_lock(&sdev->mutex);
  1524. if (sdev->power_state) {
  1525. u32 comp_en = 0;
  1526. rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
  1527. &comp_en);
  1528. if (rval < 0)
  1529. goto unlock;
  1530. sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
  1531. sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
  1532. }
  1533. /* Report current audio mode: mono or stereo */
  1534. if (sdev->stereo)
  1535. vm->txsubchans = V4L2_TUNER_SUB_STEREO;
  1536. else
  1537. vm->txsubchans = V4L2_TUNER_SUB_MONO;
  1538. /* Report rds feature status */
  1539. if (sdev->rds_info.enabled)
  1540. vm->txsubchans |= V4L2_TUNER_SUB_RDS;
  1541. else
  1542. vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
  1543. unlock:
  1544. mutex_unlock(&sdev->mutex);
  1545. exit:
  1546. return rval;
  1547. }
  1548. /* si4713_s_modulator - set modulator attributes */
  1549. static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
  1550. {
  1551. struct si4713_device *sdev = to_si4713_device(sd);
  1552. int rval = 0;
  1553. u16 stereo, rds;
  1554. u32 p;
  1555. if (!sdev)
  1556. return -ENODEV;
  1557. if (vm->index > 0)
  1558. return -EINVAL;
  1559. /* Set audio mode: mono or stereo */
  1560. if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
  1561. stereo = 1;
  1562. else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
  1563. stereo = 0;
  1564. else
  1565. return -EINVAL;
  1566. rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
  1567. mutex_lock(&sdev->mutex);
  1568. if (sdev->power_state) {
  1569. rval = si4713_read_property(sdev,
  1570. SI4713_TX_COMPONENT_ENABLE, &p);
  1571. if (rval < 0)
  1572. goto unlock;
  1573. p = set_bits(p, stereo, 1, 1 << 1);
  1574. p = set_bits(p, rds, 2, 1 << 2);
  1575. rval = si4713_write_property(sdev,
  1576. SI4713_TX_COMPONENT_ENABLE, p);
  1577. if (rval < 0)
  1578. goto unlock;
  1579. }
  1580. sdev->stereo = stereo;
  1581. sdev->rds_info.enabled = rds;
  1582. unlock:
  1583. mutex_unlock(&sdev->mutex);
  1584. return rval;
  1585. }
  1586. /* si4713_g_frequency - get tuner or modulator radio frequency */
  1587. static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  1588. {
  1589. struct si4713_device *sdev = to_si4713_device(sd);
  1590. int rval = 0;
  1591. f->type = V4L2_TUNER_RADIO;
  1592. mutex_lock(&sdev->mutex);
  1593. if (sdev->power_state) {
  1594. u16 freq;
  1595. u8 p, a, n;
  1596. rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
  1597. if (rval < 0)
  1598. goto unlock;
  1599. sdev->frequency = freq;
  1600. }
  1601. f->frequency = si4713_to_v4l2(sdev->frequency);
  1602. unlock:
  1603. mutex_unlock(&sdev->mutex);
  1604. return rval;
  1605. }
  1606. /* si4713_s_frequency - set tuner or modulator radio frequency */
  1607. static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  1608. {
  1609. struct si4713_device *sdev = to_si4713_device(sd);
  1610. int rval = 0;
  1611. u16 frequency = v4l2_to_si4713(f->frequency);
  1612. /* Check frequency range */
  1613. if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
  1614. return -EDOM;
  1615. mutex_lock(&sdev->mutex);
  1616. if (sdev->power_state) {
  1617. rval = si4713_tx_tune_freq(sdev, frequency);
  1618. if (rval < 0)
  1619. goto unlock;
  1620. frequency = rval;
  1621. rval = 0;
  1622. }
  1623. sdev->frequency = frequency;
  1624. f->frequency = si4713_to_v4l2(frequency);
  1625. unlock:
  1626. mutex_unlock(&sdev->mutex);
  1627. return rval;
  1628. }
  1629. static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
  1630. .g_frequency = si4713_g_frequency,
  1631. .s_frequency = si4713_s_frequency,
  1632. .g_modulator = si4713_g_modulator,
  1633. .s_modulator = si4713_s_modulator,
  1634. };
  1635. static const struct v4l2_subdev_ops si4713_subdev_ops = {
  1636. .core = &si4713_subdev_core_ops,
  1637. .tuner = &si4713_subdev_tuner_ops,
  1638. };
  1639. /*
  1640. * I2C driver interface
  1641. */
  1642. /* si4713_probe - probe for the device */
  1643. static int si4713_probe(struct i2c_client *client,
  1644. const struct i2c_device_id *id)
  1645. {
  1646. struct si4713_device *sdev;
  1647. int rval;
  1648. sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
  1649. if (!sdev) {
  1650. dev_err(&client->dev, "Failed to alloc video device.\n");
  1651. rval = -ENOMEM;
  1652. goto exit;
  1653. }
  1654. sdev->platform_data = client->dev.platform_data;
  1655. if (!sdev->platform_data) {
  1656. v4l2_err(&sdev->sd, "No platform data registered.\n");
  1657. rval = -ENODEV;
  1658. goto free_sdev;
  1659. }
  1660. v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
  1661. mutex_init(&sdev->mutex);
  1662. init_completion(&sdev->work);
  1663. if (client->irq) {
  1664. rval = request_irq(client->irq,
  1665. si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
  1666. client->name, sdev);
  1667. if (rval < 0) {
  1668. v4l2_err(&sdev->sd, "Could not request IRQ\n");
  1669. goto free_sdev;
  1670. }
  1671. v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
  1672. } else {
  1673. v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
  1674. }
  1675. rval = si4713_initialize(sdev);
  1676. if (rval < 0) {
  1677. v4l2_err(&sdev->sd, "Failed to probe device information.\n");
  1678. goto free_irq;
  1679. }
  1680. return 0;
  1681. free_irq:
  1682. if (client->irq)
  1683. free_irq(client->irq, sdev);
  1684. free_sdev:
  1685. kfree(sdev);
  1686. exit:
  1687. return rval;
  1688. }
  1689. /* si4713_remove - remove the device */
  1690. static int si4713_remove(struct i2c_client *client)
  1691. {
  1692. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1693. struct si4713_device *sdev = to_si4713_device(sd);
  1694. if (sdev->power_state)
  1695. si4713_set_power_state(sdev, POWER_DOWN);
  1696. if (client->irq > 0)
  1697. free_irq(client->irq, sdev);
  1698. v4l2_device_unregister_subdev(sd);
  1699. kfree(sdev);
  1700. return 0;
  1701. }
  1702. /* si4713_i2c_driver - i2c driver interface */
  1703. static const struct i2c_device_id si4713_id[] = {
  1704. { "si4713" , 0 },
  1705. { },
  1706. };
  1707. MODULE_DEVICE_TABLE(i2c, si4713_id);
  1708. static struct i2c_driver si4713_i2c_driver = {
  1709. .driver = {
  1710. .name = "si4713",
  1711. },
  1712. .probe = si4713_probe,
  1713. .remove = si4713_remove,
  1714. .id_table = si4713_id,
  1715. };
  1716. /* Module Interface */
  1717. static int __init si4713_module_init(void)
  1718. {
  1719. return i2c_add_driver(&si4713_i2c_driver);
  1720. }
  1721. static void __exit si4713_module_exit(void)
  1722. {
  1723. i2c_del_driver(&si4713_i2c_driver);
  1724. }
  1725. module_init(si4713_module_init);
  1726. module_exit(si4713_module_exit);