si4713-i2c.c 50 KB

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