si4713-i2c.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  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 < 0)
  866. goto exit;
  867. ps_name[len] = '\0';
  868. if (strlen(ps_name) % vqc.step) {
  869. rval = -ERANGE;
  870. goto exit;
  871. }
  872. rval = si4713_set_rds_ps_name(sdev, ps_name);
  873. }
  874. break;
  875. case V4L2_CID_RDS_TX_RADIO_TEXT: {
  876. char radio_text[MAX_RDS_RADIO_TEXT + 1];
  877. len = control->size - 1;
  878. if (len > MAX_RDS_RADIO_TEXT) {
  879. rval = -ERANGE;
  880. goto exit;
  881. }
  882. rval = copy_from_user(radio_text, control->string, len);
  883. if (rval < 0)
  884. goto exit;
  885. radio_text[len] = '\0';
  886. if (strlen(radio_text) % vqc.step) {
  887. rval = -ERANGE;
  888. goto exit;
  889. }
  890. rval = si4713_set_rds_radio_text(sdev, radio_text);
  891. }
  892. break;
  893. default:
  894. rval = -EINVAL;
  895. break;
  896. };
  897. exit:
  898. return rval;
  899. }
  900. static int validate_range(struct v4l2_subdev *sd,
  901. struct v4l2_ext_control *control)
  902. {
  903. struct v4l2_queryctrl vqc;
  904. int rval;
  905. vqc.id = control->id;
  906. rval = si4713_queryctrl(sd, &vqc);
  907. if (rval < 0)
  908. goto exit;
  909. if (control->value < vqc.minimum || control->value > vqc.maximum)
  910. rval = -ERANGE;
  911. exit:
  912. return rval;
  913. }
  914. /* properties which use tx_tune_power*/
  915. static int si4713_write_econtrol_tune(struct si4713_device *sdev,
  916. struct v4l2_ext_control *control)
  917. {
  918. s32 rval = 0;
  919. u8 power, antcap;
  920. rval = validate_range(&sdev->sd, control);
  921. if (rval < 0)
  922. goto exit;
  923. mutex_lock(&sdev->mutex);
  924. switch (control->id) {
  925. case V4L2_CID_TUNE_POWER_LEVEL:
  926. power = control->value;
  927. antcap = sdev->antenna_capacitor;
  928. break;
  929. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  930. power = sdev->power_level;
  931. antcap = control->value;
  932. break;
  933. default:
  934. rval = -EINVAL;
  935. goto unlock;
  936. };
  937. if (sdev->power_state)
  938. rval = si4713_tx_tune_power(sdev, power, antcap);
  939. if (rval == 0) {
  940. sdev->power_level = power;
  941. sdev->antenna_capacitor = antcap;
  942. }
  943. unlock:
  944. mutex_unlock(&sdev->mutex);
  945. exit:
  946. return rval;
  947. }
  948. static int si4713_write_econtrol_integers(struct si4713_device *sdev,
  949. struct v4l2_ext_control *control)
  950. {
  951. s32 rval;
  952. u32 *shadow = NULL, val = 0;
  953. s32 bit = 0, mask = 0;
  954. u16 property = 0;
  955. int mul = 0;
  956. unsigned long *table = NULL;
  957. int size = 0;
  958. rval = validate_range(&sdev->sd, control);
  959. if (rval < 0)
  960. goto exit;
  961. rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
  962. &mask, &property, &mul, &table, &size);
  963. if (rval < 0)
  964. goto exit;
  965. val = control->value;
  966. if (mul) {
  967. val = control->value / mul;
  968. } else if (table) {
  969. rval = usecs_to_dev(control->value, table, size);
  970. if (rval < 0)
  971. goto exit;
  972. val = rval;
  973. rval = 0;
  974. }
  975. mutex_lock(&sdev->mutex);
  976. if (sdev->power_state) {
  977. if (mask) {
  978. rval = si4713_read_property(sdev, property, &val);
  979. if (rval < 0)
  980. goto unlock;
  981. val = set_bits(val, control->value, bit, mask);
  982. }
  983. rval = si4713_write_property(sdev, property, val);
  984. if (rval < 0)
  985. goto unlock;
  986. if (mask)
  987. val = control->value;
  988. }
  989. if (mul) {
  990. *shadow = val * mul;
  991. } else if (table) {
  992. rval = dev_to_usecs(val, table, size);
  993. if (rval < 0)
  994. goto unlock;
  995. *shadow = rval;
  996. rval = 0;
  997. } else {
  998. *shadow = val;
  999. }
  1000. unlock:
  1001. mutex_unlock(&sdev->mutex);
  1002. exit:
  1003. return rval;
  1004. }
  1005. static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
  1006. static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *);
  1007. /*
  1008. * si4713_setup - Sets the device up with current configuration.
  1009. * @sdev: si4713_device structure for the device we are communicating
  1010. */
  1011. static int si4713_setup(struct si4713_device *sdev)
  1012. {
  1013. struct v4l2_ext_control ctrl;
  1014. struct v4l2_frequency f;
  1015. struct v4l2_modulator vm;
  1016. struct si4713_device *tmp;
  1017. int rval = 0;
  1018. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  1019. if (!tmp)
  1020. return -ENOMEM;
  1021. /* Get a local copy to avoid race */
  1022. mutex_lock(&sdev->mutex);
  1023. memcpy(tmp, sdev, sizeof(*sdev));
  1024. mutex_unlock(&sdev->mutex);
  1025. ctrl.id = V4L2_CID_RDS_TX_PI;
  1026. ctrl.value = tmp->rds_info.pi;
  1027. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1028. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_THRESHOLD;
  1029. ctrl.value = tmp->acomp_info.threshold;
  1030. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1031. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_GAIN;
  1032. ctrl.value = tmp->acomp_info.gain;
  1033. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1034. ctrl.id = V4L2_CID_PILOT_TONE_FREQUENCY;
  1035. ctrl.value = tmp->pilot_info.frequency;
  1036. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1037. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME;
  1038. ctrl.value = tmp->acomp_info.attack_time;
  1039. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1040. ctrl.id = V4L2_CID_PILOT_TONE_DEVIATION;
  1041. ctrl.value = tmp->pilot_info.deviation;
  1042. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1043. ctrl.id = V4L2_CID_AUDIO_LIMITER_DEVIATION;
  1044. ctrl.value = tmp->limiter_info.deviation;
  1045. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1046. ctrl.id = V4L2_CID_RDS_TX_DEVIATION;
  1047. ctrl.value = tmp->rds_info.deviation;
  1048. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1049. ctrl.id = V4L2_CID_RDS_TX_PTY;
  1050. ctrl.value = tmp->rds_info.pty;
  1051. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1052. ctrl.id = V4L2_CID_AUDIO_LIMITER_ENABLED;
  1053. ctrl.value = tmp->limiter_info.enabled;
  1054. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1055. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ENABLED;
  1056. ctrl.value = tmp->acomp_info.enabled;
  1057. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1058. ctrl.id = V4L2_CID_PILOT_TONE_ENABLED;
  1059. ctrl.value = tmp->pilot_info.enabled;
  1060. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1061. ctrl.id = V4L2_CID_AUDIO_LIMITER_RELEASE_TIME;
  1062. ctrl.value = tmp->limiter_info.release_time;
  1063. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1064. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME;
  1065. ctrl.value = tmp->acomp_info.release_time;
  1066. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1067. ctrl.id = V4L2_CID_TUNE_PREEMPHASIS;
  1068. ctrl.value = tmp->preemphasis;
  1069. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1070. ctrl.id = V4L2_CID_RDS_TX_PS_NAME;
  1071. rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
  1072. ctrl.id = V4L2_CID_RDS_TX_RADIO_TEXT;
  1073. rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
  1074. /* Device procedure needs to set frequency first */
  1075. f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
  1076. f.frequency = si4713_to_v4l2(f.frequency);
  1077. rval |= si4713_s_frequency(&sdev->sd, &f);
  1078. ctrl.id = V4L2_CID_TUNE_POWER_LEVEL;
  1079. ctrl.value = tmp->power_level;
  1080. rval |= si4713_write_econtrol_tune(sdev, &ctrl);
  1081. ctrl.id = V4L2_CID_TUNE_ANTENNA_CAPACITOR;
  1082. ctrl.value = tmp->antenna_capacitor;
  1083. rval |= si4713_write_econtrol_tune(sdev, &ctrl);
  1084. vm.index = 0;
  1085. if (tmp->stereo)
  1086. vm.txsubchans = V4L2_TUNER_SUB_STEREO;
  1087. else
  1088. vm.txsubchans = V4L2_TUNER_SUB_MONO;
  1089. if (tmp->rds_info.enabled)
  1090. vm.txsubchans |= V4L2_TUNER_SUB_RDS;
  1091. si4713_s_modulator(&sdev->sd, &vm);
  1092. kfree(tmp);
  1093. return rval;
  1094. }
  1095. /*
  1096. * si4713_initialize - Sets the device up with default configuration.
  1097. * @sdev: si4713_device structure for the device we are communicating
  1098. */
  1099. static int si4713_initialize(struct si4713_device *sdev)
  1100. {
  1101. int rval;
  1102. rval = si4713_set_power_state(sdev, POWER_ON);
  1103. if (rval < 0)
  1104. goto exit;
  1105. rval = si4713_checkrev(sdev);
  1106. if (rval < 0)
  1107. goto exit;
  1108. rval = si4713_set_power_state(sdev, POWER_OFF);
  1109. if (rval < 0)
  1110. goto exit;
  1111. mutex_lock(&sdev->mutex);
  1112. sdev->rds_info.pi = DEFAULT_RDS_PI;
  1113. sdev->rds_info.pty = DEFAULT_RDS_PTY;
  1114. sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
  1115. strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
  1116. strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
  1117. MAX_RDS_RADIO_TEXT);
  1118. sdev->rds_info.enabled = 1;
  1119. sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
  1120. sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
  1121. sdev->limiter_info.enabled = 1;
  1122. sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
  1123. sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
  1124. sdev->pilot_info.enabled = 1;
  1125. sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
  1126. sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
  1127. sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
  1128. sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
  1129. sdev->acomp_info.enabled = 1;
  1130. sdev->frequency = DEFAULT_FREQUENCY;
  1131. sdev->preemphasis = DEFAULT_PREEMPHASIS;
  1132. sdev->mute = DEFAULT_MUTE;
  1133. sdev->power_level = DEFAULT_POWER_LEVEL;
  1134. sdev->antenna_capacitor = 0;
  1135. sdev->stereo = 1;
  1136. sdev->tune_rnl = DEFAULT_TUNE_RNL;
  1137. mutex_unlock(&sdev->mutex);
  1138. exit:
  1139. return rval;
  1140. }
  1141. /* read string property */
  1142. static int si4713_read_econtrol_string(struct si4713_device *sdev,
  1143. struct v4l2_ext_control *control)
  1144. {
  1145. s32 rval = 0;
  1146. switch (control->id) {
  1147. case V4L2_CID_RDS_TX_PS_NAME:
  1148. if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
  1149. control->size = MAX_RDS_PS_NAME + 1;
  1150. rval = -ENOSPC;
  1151. goto exit;
  1152. }
  1153. rval = copy_to_user(control->string, sdev->rds_info.ps_name,
  1154. strlen(sdev->rds_info.ps_name) + 1);
  1155. break;
  1156. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1157. if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
  1158. control->size = MAX_RDS_RADIO_TEXT + 1;
  1159. rval = -ENOSPC;
  1160. goto exit;
  1161. }
  1162. rval = copy_to_user(control->string, sdev->rds_info.radio_text,
  1163. strlen(sdev->rds_info.radio_text) + 1);
  1164. break;
  1165. default:
  1166. rval = -EINVAL;
  1167. break;
  1168. };
  1169. exit:
  1170. return rval;
  1171. }
  1172. /*
  1173. * si4713_update_tune_status - update properties from tx_tune_status
  1174. * command. Must be called with sdev->mutex held.
  1175. * @sdev: si4713_device structure for the device we are communicating
  1176. */
  1177. static int si4713_update_tune_status(struct si4713_device *sdev)
  1178. {
  1179. int rval;
  1180. u16 f = 0;
  1181. u8 p = 0, a = 0, n = 0;
  1182. rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
  1183. if (rval < 0)
  1184. goto exit;
  1185. sdev->power_level = p;
  1186. sdev->antenna_capacitor = a;
  1187. sdev->tune_rnl = n;
  1188. exit:
  1189. return rval;
  1190. }
  1191. /* properties which use tx_tune_status */
  1192. static int si4713_read_econtrol_tune(struct si4713_device *sdev,
  1193. struct v4l2_ext_control *control)
  1194. {
  1195. s32 rval = 0;
  1196. mutex_lock(&sdev->mutex);
  1197. if (sdev->power_state) {
  1198. rval = si4713_update_tune_status(sdev);
  1199. if (rval < 0)
  1200. goto unlock;
  1201. }
  1202. switch (control->id) {
  1203. case V4L2_CID_TUNE_POWER_LEVEL:
  1204. control->value = sdev->power_level;
  1205. break;
  1206. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1207. control->value = sdev->antenna_capacitor;
  1208. break;
  1209. default:
  1210. rval = -EINVAL;
  1211. };
  1212. unlock:
  1213. mutex_unlock(&sdev->mutex);
  1214. return rval;
  1215. }
  1216. static int si4713_read_econtrol_integers(struct si4713_device *sdev,
  1217. struct v4l2_ext_control *control)
  1218. {
  1219. s32 rval;
  1220. u32 *shadow = NULL, val = 0;
  1221. s32 bit = 0, mask = 0;
  1222. u16 property = 0;
  1223. int mul = 0;
  1224. unsigned long *table = NULL;
  1225. int size = 0;
  1226. rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
  1227. &mask, &property, &mul, &table, &size);
  1228. if (rval < 0)
  1229. goto exit;
  1230. mutex_lock(&sdev->mutex);
  1231. if (sdev->power_state) {
  1232. rval = si4713_read_property(sdev, property, &val);
  1233. if (rval < 0)
  1234. goto unlock;
  1235. /* Keep negative values for threshold */
  1236. if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
  1237. *shadow = (s16)val;
  1238. else if (mask)
  1239. *shadow = get_status_bit(val, bit, mask);
  1240. else if (mul)
  1241. *shadow = val * mul;
  1242. else
  1243. *shadow = dev_to_usecs(val, table, size);
  1244. }
  1245. control->value = *shadow;
  1246. unlock:
  1247. mutex_unlock(&sdev->mutex);
  1248. exit:
  1249. return rval;
  1250. }
  1251. /*
  1252. * Video4Linux Subdev Interface
  1253. */
  1254. /* si4713_s_ext_ctrls - set extended controls value */
  1255. static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
  1256. struct v4l2_ext_controls *ctrls)
  1257. {
  1258. struct si4713_device *sdev = to_si4713_device(sd);
  1259. int i;
  1260. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  1261. return -EINVAL;
  1262. for (i = 0; i < ctrls->count; i++) {
  1263. int err;
  1264. switch ((ctrls->controls + i)->id) {
  1265. case V4L2_CID_RDS_TX_PS_NAME:
  1266. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1267. err = si4713_write_econtrol_string(sdev,
  1268. ctrls->controls + i);
  1269. break;
  1270. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1271. case V4L2_CID_TUNE_POWER_LEVEL:
  1272. err = si4713_write_econtrol_tune(sdev,
  1273. ctrls->controls + i);
  1274. break;
  1275. default:
  1276. err = si4713_write_econtrol_integers(sdev,
  1277. ctrls->controls + i);
  1278. }
  1279. if (err < 0) {
  1280. ctrls->error_idx = i;
  1281. return err;
  1282. }
  1283. }
  1284. return 0;
  1285. }
  1286. /* si4713_g_ext_ctrls - get extended controls value */
  1287. static int si4713_g_ext_ctrls(struct v4l2_subdev *sd,
  1288. struct v4l2_ext_controls *ctrls)
  1289. {
  1290. struct si4713_device *sdev = to_si4713_device(sd);
  1291. int i;
  1292. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  1293. return -EINVAL;
  1294. for (i = 0; i < ctrls->count; i++) {
  1295. int err;
  1296. switch ((ctrls->controls + i)->id) {
  1297. case V4L2_CID_RDS_TX_PS_NAME:
  1298. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1299. err = si4713_read_econtrol_string(sdev,
  1300. ctrls->controls + i);
  1301. break;
  1302. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1303. case V4L2_CID_TUNE_POWER_LEVEL:
  1304. err = si4713_read_econtrol_tune(sdev,
  1305. ctrls->controls + i);
  1306. break;
  1307. default:
  1308. err = si4713_read_econtrol_integers(sdev,
  1309. ctrls->controls + i);
  1310. }
  1311. if (err < 0) {
  1312. ctrls->error_idx = i;
  1313. return err;
  1314. }
  1315. }
  1316. return 0;
  1317. }
  1318. /* si4713_queryctrl - enumerate control items */
  1319. static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  1320. {
  1321. int rval = 0;
  1322. switch (qc->id) {
  1323. /* User class controls */
  1324. case V4L2_CID_AUDIO_MUTE:
  1325. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
  1326. break;
  1327. /* FM_TX class controls */
  1328. case V4L2_CID_RDS_TX_PI:
  1329. rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
  1330. break;
  1331. case V4L2_CID_RDS_TX_PTY:
  1332. rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
  1333. break;
  1334. case V4L2_CID_RDS_TX_DEVIATION:
  1335. rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_DEVIATION,
  1336. 10, DEFAULT_RDS_DEVIATION);
  1337. break;
  1338. case V4L2_CID_RDS_TX_PS_NAME:
  1339. /*
  1340. * Report step as 8. From RDS spec, psname
  1341. * should be 8. But there are receivers which scroll strings
  1342. * sized as 8xN.
  1343. */
  1344. rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
  1345. break;
  1346. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1347. /*
  1348. * Report step as 32 (2A block). From RDS spec,
  1349. * radio text should be 32 for 2A block. But there are receivers
  1350. * which scroll strings sized as 32xN. Setting default to 32.
  1351. */
  1352. rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
  1353. break;
  1354. case V4L2_CID_AUDIO_LIMITER_ENABLED:
  1355. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1356. break;
  1357. case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
  1358. rval = v4l2_ctrl_query_fill(qc, 250, MAX_LIMITER_RELEASE_TIME,
  1359. 50, DEFAULT_LIMITER_RTIME);
  1360. break;
  1361. case V4L2_CID_AUDIO_LIMITER_DEVIATION:
  1362. rval = v4l2_ctrl_query_fill(qc, 0, MAX_LIMITER_DEVIATION,
  1363. 10, DEFAULT_LIMITER_DEV);
  1364. break;
  1365. case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
  1366. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1367. break;
  1368. case V4L2_CID_AUDIO_COMPRESSION_GAIN:
  1369. rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
  1370. DEFAULT_ACOMP_GAIN);
  1371. break;
  1372. case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
  1373. rval = v4l2_ctrl_query_fill(qc, MIN_ACOMP_THRESHOLD,
  1374. MAX_ACOMP_THRESHOLD, 1,
  1375. DEFAULT_ACOMP_THRESHOLD);
  1376. break;
  1377. case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
  1378. rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_ATTACK_TIME,
  1379. 500, DEFAULT_ACOMP_ATIME);
  1380. break;
  1381. case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
  1382. rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
  1383. 100000, DEFAULT_ACOMP_RTIME);
  1384. break;
  1385. case V4L2_CID_PILOT_TONE_ENABLED:
  1386. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1387. break;
  1388. case V4L2_CID_PILOT_TONE_DEVIATION:
  1389. rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_DEVIATION,
  1390. 10, DEFAULT_PILOT_DEVIATION);
  1391. break;
  1392. case V4L2_CID_PILOT_TONE_FREQUENCY:
  1393. rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_FREQUENCY,
  1394. 1, DEFAULT_PILOT_FREQUENCY);
  1395. break;
  1396. case V4L2_CID_TUNE_PREEMPHASIS:
  1397. rval = v4l2_ctrl_query_fill(qc, V4L2_PREEMPHASIS_DISABLED,
  1398. V4L2_PREEMPHASIS_75_uS, 1,
  1399. V4L2_PREEMPHASIS_50_uS);
  1400. break;
  1401. case V4L2_CID_TUNE_POWER_LEVEL:
  1402. rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
  1403. break;
  1404. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1405. rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
  1406. break;
  1407. default:
  1408. rval = -EINVAL;
  1409. break;
  1410. };
  1411. return rval;
  1412. }
  1413. /* si4713_g_ctrl - get the value of a control */
  1414. static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1415. {
  1416. struct si4713_device *sdev = to_si4713_device(sd);
  1417. int rval = 0;
  1418. if (!sdev)
  1419. return -ENODEV;
  1420. mutex_lock(&sdev->mutex);
  1421. if (sdev->power_state) {
  1422. rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
  1423. &sdev->mute);
  1424. if (rval < 0)
  1425. goto unlock;
  1426. }
  1427. switch (ctrl->id) {
  1428. case V4L2_CID_AUDIO_MUTE:
  1429. ctrl->value = get_mute(sdev->mute);
  1430. break;
  1431. }
  1432. unlock:
  1433. mutex_unlock(&sdev->mutex);
  1434. return rval;
  1435. }
  1436. /* si4713_s_ctrl - set the value of a control */
  1437. static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1438. {
  1439. struct si4713_device *sdev = to_si4713_device(sd);
  1440. int rval = 0;
  1441. if (!sdev)
  1442. return -ENODEV;
  1443. switch (ctrl->id) {
  1444. case V4L2_CID_AUDIO_MUTE:
  1445. if (ctrl->value) {
  1446. rval = si4713_set_mute(sdev, ctrl->value);
  1447. if (rval < 0)
  1448. goto exit;
  1449. rval = si4713_set_power_state(sdev, POWER_DOWN);
  1450. } else {
  1451. rval = si4713_set_power_state(sdev, POWER_UP);
  1452. if (rval < 0)
  1453. goto exit;
  1454. rval = si4713_setup(sdev);
  1455. if (rval < 0)
  1456. goto exit;
  1457. rval = si4713_set_mute(sdev, ctrl->value);
  1458. }
  1459. break;
  1460. }
  1461. exit:
  1462. return rval;
  1463. }
  1464. /* si4713_ioctl - deal with private ioctls (only rnl for now) */
  1465. long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  1466. {
  1467. struct si4713_device *sdev = to_si4713_device(sd);
  1468. struct si4713_rnl *rnl = arg;
  1469. u16 frequency;
  1470. int rval = 0;
  1471. if (!arg)
  1472. return -EINVAL;
  1473. mutex_lock(&sdev->mutex);
  1474. switch (cmd) {
  1475. case SI4713_IOC_MEASURE_RNL:
  1476. frequency = v4l2_to_si4713(rnl->frequency);
  1477. if (sdev->power_state) {
  1478. /* Set desired measurement frequency */
  1479. rval = si4713_tx_tune_measure(sdev, frequency, 0);
  1480. if (rval < 0)
  1481. goto unlock;
  1482. /* get results from tune status */
  1483. rval = si4713_update_tune_status(sdev);
  1484. if (rval < 0)
  1485. goto unlock;
  1486. }
  1487. rnl->rnl = sdev->tune_rnl;
  1488. break;
  1489. default:
  1490. /* nothing */
  1491. rval = -ENOIOCTLCMD;
  1492. }
  1493. unlock:
  1494. mutex_unlock(&sdev->mutex);
  1495. return rval;
  1496. }
  1497. static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
  1498. .queryctrl = si4713_queryctrl,
  1499. .g_ext_ctrls = si4713_g_ext_ctrls,
  1500. .s_ext_ctrls = si4713_s_ext_ctrls,
  1501. .g_ctrl = si4713_g_ctrl,
  1502. .s_ctrl = si4713_s_ctrl,
  1503. .ioctl = si4713_ioctl,
  1504. };
  1505. /* si4713_g_modulator - get modulator attributes */
  1506. static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
  1507. {
  1508. struct si4713_device *sdev = to_si4713_device(sd);
  1509. int rval = 0;
  1510. if (!sdev) {
  1511. rval = -ENODEV;
  1512. goto exit;
  1513. }
  1514. if (vm->index > 0) {
  1515. rval = -EINVAL;
  1516. goto exit;
  1517. }
  1518. strncpy(vm->name, "FM Modulator", 32);
  1519. vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
  1520. V4L2_TUNER_CAP_RDS;
  1521. /* Report current frequency range limits */
  1522. vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
  1523. vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
  1524. mutex_lock(&sdev->mutex);
  1525. if (sdev->power_state) {
  1526. u32 comp_en = 0;
  1527. rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
  1528. &comp_en);
  1529. if (rval < 0)
  1530. goto unlock;
  1531. sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
  1532. sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
  1533. }
  1534. /* Report current audio mode: mono or stereo */
  1535. if (sdev->stereo)
  1536. vm->txsubchans = V4L2_TUNER_SUB_STEREO;
  1537. else
  1538. vm->txsubchans = V4L2_TUNER_SUB_MONO;
  1539. /* Report rds feature status */
  1540. if (sdev->rds_info.enabled)
  1541. vm->txsubchans |= V4L2_TUNER_SUB_RDS;
  1542. else
  1543. vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
  1544. unlock:
  1545. mutex_unlock(&sdev->mutex);
  1546. exit:
  1547. return rval;
  1548. }
  1549. /* si4713_s_modulator - set modulator attributes */
  1550. static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
  1551. {
  1552. struct si4713_device *sdev = to_si4713_device(sd);
  1553. int rval = 0;
  1554. u16 stereo, rds;
  1555. u32 p;
  1556. if (!sdev)
  1557. return -ENODEV;
  1558. if (vm->index > 0)
  1559. return -EINVAL;
  1560. /* Set audio mode: mono or stereo */
  1561. if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
  1562. stereo = 1;
  1563. else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
  1564. stereo = 0;
  1565. else
  1566. return -EINVAL;
  1567. rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
  1568. mutex_lock(&sdev->mutex);
  1569. if (sdev->power_state) {
  1570. rval = si4713_read_property(sdev,
  1571. SI4713_TX_COMPONENT_ENABLE, &p);
  1572. if (rval < 0)
  1573. goto unlock;
  1574. p = set_bits(p, stereo, 1, 1 << 1);
  1575. p = set_bits(p, rds, 2, 1 << 2);
  1576. rval = si4713_write_property(sdev,
  1577. SI4713_TX_COMPONENT_ENABLE, p);
  1578. if (rval < 0)
  1579. goto unlock;
  1580. }
  1581. sdev->stereo = stereo;
  1582. sdev->rds_info.enabled = rds;
  1583. unlock:
  1584. mutex_unlock(&sdev->mutex);
  1585. return rval;
  1586. }
  1587. /* si4713_g_frequency - get tuner or modulator radio frequency */
  1588. static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  1589. {
  1590. struct si4713_device *sdev = to_si4713_device(sd);
  1591. int rval = 0;
  1592. f->type = V4L2_TUNER_RADIO;
  1593. mutex_lock(&sdev->mutex);
  1594. if (sdev->power_state) {
  1595. u16 freq;
  1596. u8 p, a, n;
  1597. rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
  1598. if (rval < 0)
  1599. goto unlock;
  1600. sdev->frequency = freq;
  1601. }
  1602. f->frequency = si4713_to_v4l2(sdev->frequency);
  1603. unlock:
  1604. mutex_unlock(&sdev->mutex);
  1605. return rval;
  1606. }
  1607. /* si4713_s_frequency - set tuner or modulator radio frequency */
  1608. static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  1609. {
  1610. struct si4713_device *sdev = to_si4713_device(sd);
  1611. int rval = 0;
  1612. u16 frequency = v4l2_to_si4713(f->frequency);
  1613. /* Check frequency range */
  1614. if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
  1615. return -EDOM;
  1616. mutex_lock(&sdev->mutex);
  1617. if (sdev->power_state) {
  1618. rval = si4713_tx_tune_freq(sdev, frequency);
  1619. if (rval < 0)
  1620. goto unlock;
  1621. frequency = rval;
  1622. rval = 0;
  1623. }
  1624. sdev->frequency = frequency;
  1625. f->frequency = si4713_to_v4l2(frequency);
  1626. unlock:
  1627. mutex_unlock(&sdev->mutex);
  1628. return rval;
  1629. }
  1630. static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
  1631. .g_frequency = si4713_g_frequency,
  1632. .s_frequency = si4713_s_frequency,
  1633. .g_modulator = si4713_g_modulator,
  1634. .s_modulator = si4713_s_modulator,
  1635. };
  1636. static const struct v4l2_subdev_ops si4713_subdev_ops = {
  1637. .core = &si4713_subdev_core_ops,
  1638. .tuner = &si4713_subdev_tuner_ops,
  1639. };
  1640. /*
  1641. * I2C driver interface
  1642. */
  1643. /* si4713_probe - probe for the device */
  1644. static int si4713_probe(struct i2c_client *client,
  1645. const struct i2c_device_id *id)
  1646. {
  1647. struct si4713_device *sdev;
  1648. int rval;
  1649. sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
  1650. if (!sdev) {
  1651. dev_err(&client->dev, "Failed to alloc video device.\n");
  1652. rval = -ENOMEM;
  1653. goto exit;
  1654. }
  1655. sdev->platform_data = client->dev.platform_data;
  1656. if (!sdev->platform_data) {
  1657. v4l2_err(&sdev->sd, "No platform data registered.\n");
  1658. rval = -ENODEV;
  1659. goto free_sdev;
  1660. }
  1661. v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
  1662. mutex_init(&sdev->mutex);
  1663. init_completion(&sdev->work);
  1664. if (client->irq) {
  1665. rval = request_irq(client->irq,
  1666. si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
  1667. client->name, sdev);
  1668. if (rval < 0) {
  1669. v4l2_err(&sdev->sd, "Could not request IRQ\n");
  1670. goto free_sdev;
  1671. }
  1672. v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
  1673. } else {
  1674. v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
  1675. }
  1676. rval = si4713_initialize(sdev);
  1677. if (rval < 0) {
  1678. v4l2_err(&sdev->sd, "Failed to probe device information.\n");
  1679. goto free_irq;
  1680. }
  1681. return 0;
  1682. free_irq:
  1683. if (client->irq)
  1684. free_irq(client->irq, sdev);
  1685. free_sdev:
  1686. kfree(sdev);
  1687. exit:
  1688. return rval;
  1689. }
  1690. /* si4713_remove - remove the device */
  1691. static int si4713_remove(struct i2c_client *client)
  1692. {
  1693. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1694. struct si4713_device *sdev = to_si4713_device(sd);
  1695. if (sdev->power_state)
  1696. si4713_set_power_state(sdev, POWER_DOWN);
  1697. if (client->irq > 0)
  1698. free_irq(client->irq, sdev);
  1699. v4l2_device_unregister_subdev(sd);
  1700. kfree(sdev);
  1701. return 0;
  1702. }
  1703. /* si4713_i2c_driver - i2c driver interface */
  1704. static const struct i2c_device_id si4713_id[] = {
  1705. { "si4713" , 0 },
  1706. { },
  1707. };
  1708. MODULE_DEVICE_TABLE(i2c, si4713_id);
  1709. static struct i2c_driver si4713_i2c_driver = {
  1710. .driver = {
  1711. .name = "si4713",
  1712. },
  1713. .probe = si4713_probe,
  1714. .remove = si4713_remove,
  1715. .id_table = si4713_id,
  1716. };
  1717. /* Module Interface */
  1718. static int __init si4713_module_init(void)
  1719. {
  1720. return i2c_add_driver(&si4713_i2c_driver);
  1721. }
  1722. static void __exit si4713_module_exit(void)
  1723. {
  1724. i2c_del_driver(&si4713_i2c_driver);
  1725. }
  1726. module_init(si4713_module_init);
  1727. module_exit(si4713_module_exit);