si4713-i2c.c 49 KB

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