si4713-i2c.c 49 KB

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