si4713.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/i2c.h>
  24. #include <linux/slab.h>
  25. #include <linux/gpio.h>
  26. #include <linux/module.h>
  27. #include <media/v4l2-device.h>
  28. #include <media/v4l2-ioctl.h>
  29. #include <media/v4l2-common.h>
  30. #include "si4713.h"
  31. /* module parameters */
  32. static int debug;
  33. module_param(debug, int, S_IRUGO | S_IWUSR);
  34. MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
  35. MODULE_LICENSE("GPL");
  36. MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
  37. MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
  38. MODULE_VERSION("0.0.1");
  39. #define DEFAULT_RDS_PI 0x00
  40. #define DEFAULT_RDS_PTY 0x00
  41. #define DEFAULT_RDS_DEVIATION 0x00C8
  42. #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
  43. #define DEFAULT_LIMITER_RTIME 0x1392
  44. #define DEFAULT_LIMITER_DEV 0x102CA
  45. #define DEFAULT_PILOT_FREQUENCY 0x4A38
  46. #define DEFAULT_PILOT_DEVIATION 0x1A5E
  47. #define DEFAULT_ACOMP_ATIME 0x0000
  48. #define DEFAULT_ACOMP_RTIME 0xF4240L
  49. #define DEFAULT_ACOMP_GAIN 0x0F
  50. #define DEFAULT_ACOMP_THRESHOLD (-0x28)
  51. #define DEFAULT_MUTE 0x01
  52. #define DEFAULT_POWER_LEVEL 88
  53. #define DEFAULT_FREQUENCY 8800
  54. #define DEFAULT_PREEMPHASIS FMPE_EU
  55. #define DEFAULT_TUNE_RNL 0xFF
  56. #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
  57. /* frequency domain transformation (using times 10 to avoid floats) */
  58. #define FREQDEV_UNIT 100000
  59. #define FREQV4L2_MULTI 625
  60. #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
  61. #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
  62. #define FREQ_RANGE_LOW 7600
  63. #define FREQ_RANGE_HIGH 10800
  64. #define MAX_ARGS 7
  65. #define RDS_BLOCK 8
  66. #define RDS_BLOCK_CLEAR 0x03
  67. #define RDS_BLOCK_LOAD 0x04
  68. #define RDS_RADIOTEXT_2A 0x20
  69. #define RDS_RADIOTEXT_BLK_SIZE 4
  70. #define RDS_RADIOTEXT_INDEX_MAX 0x0F
  71. #define RDS_CARRIAGE_RETURN 0x0D
  72. #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
  73. #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
  74. #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
  75. #define ATTACK_TIME_UNIT 500
  76. #define POWER_OFF 0x00
  77. #define POWER_ON 0x01
  78. #define msb(x) ((u8)((u16) x >> 8))
  79. #define lsb(x) ((u8)((u16) x & 0x00FF))
  80. #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
  81. #define check_command_failed(status) (!(status & SI4713_CTS) || \
  82. (status & SI4713_ERR))
  83. /* mute definition */
  84. #define set_mute(p) ((p & 1) | ((p & 1) << 1));
  85. #ifdef DEBUG
  86. #define DBG_BUFFER(device, message, buffer, size) \
  87. { \
  88. int i; \
  89. char str[(size)*5]; \
  90. for (i = 0; i < size; i++) \
  91. sprintf(str + i * 5, " 0x%02x", buffer[i]); \
  92. v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
  93. }
  94. #else
  95. #define DBG_BUFFER(device, message, buffer, size)
  96. #endif
  97. /*
  98. * Values for limiter release time (sorted by second column)
  99. * device release
  100. * value time (us)
  101. */
  102. static long limiter_times[] = {
  103. 2000, 250,
  104. 1000, 500,
  105. 510, 1000,
  106. 255, 2000,
  107. 170, 3000,
  108. 127, 4020,
  109. 102, 5010,
  110. 85, 6020,
  111. 73, 7010,
  112. 64, 7990,
  113. 57, 8970,
  114. 51, 10030,
  115. 25, 20470,
  116. 17, 30110,
  117. 13, 39380,
  118. 10, 51190,
  119. 8, 63690,
  120. 7, 73140,
  121. 6, 85330,
  122. 5, 102390,
  123. };
  124. /*
  125. * Values for audio compression release time (sorted by second column)
  126. * device release
  127. * value time (us)
  128. */
  129. static unsigned long acomp_rtimes[] = {
  130. 0, 100000,
  131. 1, 200000,
  132. 2, 350000,
  133. 3, 525000,
  134. 4, 1000000,
  135. };
  136. /*
  137. * Values for preemphasis (sorted by second column)
  138. * device preemphasis
  139. * value value (v4l2)
  140. */
  141. static unsigned long preemphasis_values[] = {
  142. FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
  143. FMPE_EU, V4L2_PREEMPHASIS_50_uS,
  144. FMPE_USA, V4L2_PREEMPHASIS_75_uS,
  145. };
  146. static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
  147. int size)
  148. {
  149. int i;
  150. int rval = -EINVAL;
  151. for (i = 0; i < size / 2; i++)
  152. if (array[(i * 2) + 1] >= usecs) {
  153. rval = array[i * 2];
  154. break;
  155. }
  156. return rval;
  157. }
  158. /* si4713_handler: IRQ handler, just complete work */
  159. static irqreturn_t si4713_handler(int irq, void *dev)
  160. {
  161. struct si4713_device *sdev = dev;
  162. v4l2_dbg(2, debug, &sdev->sd,
  163. "%s: sending signal to completion work.\n", __func__);
  164. complete(&sdev->work);
  165. return IRQ_HANDLED;
  166. }
  167. /*
  168. * si4713_send_command - sends a command to si4713 and waits its response
  169. * @sdev: si4713_device structure for the device we are communicating
  170. * @command: command id
  171. * @args: command arguments we are sending (up to 7)
  172. * @argn: actual size of @args
  173. * @response: buffer to place the expected response from the device (up to 15)
  174. * @respn: actual size of @response
  175. * @usecs: amount of time to wait before reading the response (in usecs)
  176. */
  177. static int si4713_send_command(struct si4713_device *sdev, const u8 command,
  178. const u8 args[], const int argn,
  179. u8 response[], const int respn, const int usecs)
  180. {
  181. struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
  182. unsigned long until_jiffies;
  183. u8 data1[MAX_ARGS + 1];
  184. int err;
  185. if (!client->adapter)
  186. return -ENODEV;
  187. /* First send the command and its arguments */
  188. data1[0] = command;
  189. memcpy(data1 + 1, args, argn);
  190. DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
  191. err = i2c_master_send(client, data1, argn + 1);
  192. if (err != argn + 1) {
  193. v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
  194. command);
  195. return err < 0 ? err : -EIO;
  196. }
  197. until_jiffies = jiffies + usecs_to_jiffies(usecs) + 1;
  198. /* Wait response from interrupt */
  199. if (client->irq) {
  200. if (!wait_for_completion_timeout(&sdev->work,
  201. usecs_to_jiffies(usecs) + 1))
  202. v4l2_warn(&sdev->sd,
  203. "(%s) Device took too much time to answer.\n",
  204. __func__);
  205. }
  206. do {
  207. err = i2c_master_recv(client, response, respn);
  208. if (err != respn) {
  209. v4l2_err(&sdev->sd,
  210. "Error %d while reading response for command 0x%02x\n",
  211. err, command);
  212. return err < 0 ? err : -EIO;
  213. }
  214. DBG_BUFFER(&sdev->sd, "Response", response, respn);
  215. if (!check_command_failed(response[0]))
  216. return 0;
  217. if (client->irq)
  218. return -EBUSY;
  219. if (usecs <= 1000)
  220. usleep_range(usecs, 1000);
  221. else
  222. usleep_range(1000, 2000);
  223. } while (time_is_after_jiffies(until_jiffies));
  224. return -EBUSY;
  225. }
  226. /*
  227. * si4713_read_property - reads a si4713 property
  228. * @sdev: si4713_device structure for the device we are communicating
  229. * @prop: property identification number
  230. * @pv: property value to be returned on success
  231. */
  232. static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
  233. {
  234. int err;
  235. u8 val[SI4713_GET_PROP_NRESP];
  236. /*
  237. * .First byte = 0
  238. * .Second byte = property's MSB
  239. * .Third byte = property's LSB
  240. */
  241. const u8 args[SI4713_GET_PROP_NARGS] = {
  242. 0x00,
  243. msb(prop),
  244. lsb(prop),
  245. };
  246. err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
  247. args, ARRAY_SIZE(args), val,
  248. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  249. if (err < 0)
  250. return err;
  251. *pv = compose_u16(val[2], val[3]);
  252. v4l2_dbg(1, debug, &sdev->sd,
  253. "%s: property=0x%02x value=0x%02x status=0x%02x\n",
  254. __func__, prop, *pv, val[0]);
  255. return err;
  256. }
  257. /*
  258. * si4713_write_property - modifies a si4713 property
  259. * @sdev: si4713_device structure for the device we are communicating
  260. * @prop: property identification number
  261. * @val: new value for that property
  262. */
  263. static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
  264. {
  265. int rval;
  266. u8 resp[SI4713_SET_PROP_NRESP];
  267. /*
  268. * .First byte = 0
  269. * .Second byte = property's MSB
  270. * .Third byte = property's LSB
  271. * .Fourth byte = value's MSB
  272. * .Fifth byte = value's LSB
  273. */
  274. const u8 args[SI4713_SET_PROP_NARGS] = {
  275. 0x00,
  276. msb(prop),
  277. lsb(prop),
  278. msb(val),
  279. lsb(val),
  280. };
  281. rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
  282. args, ARRAY_SIZE(args),
  283. resp, ARRAY_SIZE(resp),
  284. DEFAULT_TIMEOUT);
  285. if (rval < 0)
  286. return rval;
  287. v4l2_dbg(1, debug, &sdev->sd,
  288. "%s: property=0x%02x value=0x%02x status=0x%02x\n",
  289. __func__, prop, val, resp[0]);
  290. /*
  291. * As there is no command response for SET_PROPERTY,
  292. * wait Tcomp time to finish before proceed, in order
  293. * to have property properly set.
  294. */
  295. msleep(TIMEOUT_SET_PROPERTY);
  296. return rval;
  297. }
  298. /*
  299. * si4713_powerup - Powers the device up
  300. * @sdev: si4713_device structure for the device we are communicating
  301. */
  302. static int si4713_powerup(struct si4713_device *sdev)
  303. {
  304. struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
  305. int err;
  306. u8 resp[SI4713_PWUP_NRESP];
  307. /*
  308. * .First byte = Enabled interrupts and boot function
  309. * .Second byte = Input operation mode
  310. */
  311. u8 args[SI4713_PWUP_NARGS] = {
  312. SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
  313. SI4713_PWUP_OPMOD_ANALOG,
  314. };
  315. if (sdev->power_state)
  316. return 0;
  317. if (sdev->vdd) {
  318. err = regulator_enable(sdev->vdd);
  319. if (err) {
  320. v4l2_err(&sdev->sd, "Failed to enable vdd: %d\n", err);
  321. return err;
  322. }
  323. }
  324. if (sdev->vio) {
  325. err = regulator_enable(sdev->vio);
  326. if (err) {
  327. v4l2_err(&sdev->sd, "Failed to enable vio: %d\n", err);
  328. return err;
  329. }
  330. }
  331. if (sdev->gpio_reset) {
  332. udelay(50);
  333. gpiod_set_value(sdev->gpio_reset, 1);
  334. }
  335. if (client->irq)
  336. args[0] |= SI4713_PWUP_CTSIEN;
  337. err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
  338. args, ARRAY_SIZE(args),
  339. resp, ARRAY_SIZE(resp),
  340. TIMEOUT_POWER_UP);
  341. if (!err) {
  342. v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
  343. resp[0]);
  344. v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
  345. sdev->power_state = POWER_ON;
  346. if (client->irq)
  347. err = si4713_write_property(sdev, SI4713_GPO_IEN,
  348. SI4713_STC_INT | SI4713_CTS);
  349. return err;
  350. }
  351. gpiod_set_value(sdev->gpio_reset, 0);
  352. if (sdev->vdd) {
  353. err = regulator_disable(sdev->vdd);
  354. if (err)
  355. v4l2_err(&sdev->sd, "Failed to disable vdd: %d\n", err);
  356. }
  357. if (sdev->vio) {
  358. err = regulator_disable(sdev->vio);
  359. if (err)
  360. v4l2_err(&sdev->sd, "Failed to disable vio: %d\n", err);
  361. }
  362. return err;
  363. }
  364. /*
  365. * si4713_powerdown - Powers the device down
  366. * @sdev: si4713_device structure for the device we are communicating
  367. */
  368. static int si4713_powerdown(struct si4713_device *sdev)
  369. {
  370. int err;
  371. u8 resp[SI4713_PWDN_NRESP];
  372. if (!sdev->power_state)
  373. return 0;
  374. err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
  375. NULL, 0,
  376. resp, ARRAY_SIZE(resp),
  377. DEFAULT_TIMEOUT);
  378. if (!err) {
  379. v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
  380. resp[0]);
  381. v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
  382. if (sdev->gpio_reset)
  383. gpiod_set_value(sdev->gpio_reset, 0);
  384. if (sdev->vdd) {
  385. err = regulator_disable(sdev->vdd);
  386. if (err) {
  387. v4l2_err(&sdev->sd,
  388. "Failed to disable vdd: %d\n", err);
  389. }
  390. }
  391. if (sdev->vio) {
  392. err = regulator_disable(sdev->vio);
  393. if (err) {
  394. v4l2_err(&sdev->sd,
  395. "Failed to disable vio: %d\n", err);
  396. }
  397. }
  398. sdev->power_state = POWER_OFF;
  399. }
  400. return err;
  401. }
  402. /*
  403. * si4713_checkrev - Checks if we are treating a device with the correct rev.
  404. * @sdev: si4713_device structure for the device we are communicating
  405. */
  406. static int si4713_checkrev(struct si4713_device *sdev)
  407. {
  408. struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
  409. int rval;
  410. u8 resp[SI4713_GETREV_NRESP];
  411. rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
  412. NULL, 0,
  413. resp, ARRAY_SIZE(resp),
  414. DEFAULT_TIMEOUT);
  415. if (rval < 0)
  416. return rval;
  417. if (resp[1] == SI4713_PRODUCT_NUMBER) {
  418. v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
  419. client->addr << 1, client->adapter->name);
  420. } else {
  421. v4l2_err(&sdev->sd, "Invalid product number 0x%X\n", resp[1]);
  422. rval = -EINVAL;
  423. }
  424. return rval;
  425. }
  426. /*
  427. * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
  428. * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
  429. * @sdev: si4713_device structure for the device we are communicating
  430. * @usecs: timeout to wait for STC interrupt signal
  431. */
  432. static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
  433. {
  434. struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
  435. u8 resp[SI4713_GET_STATUS_NRESP];
  436. unsigned long start_jiffies = jiffies;
  437. int err;
  438. if (client->irq &&
  439. !wait_for_completion_timeout(&sdev->work, usecs_to_jiffies(usecs) + 1))
  440. v4l2_warn(&sdev->sd,
  441. "(%s) Device took too much time to answer.\n", __func__);
  442. for (;;) {
  443. /* Clear status bits */
  444. err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
  445. NULL, 0,
  446. resp, ARRAY_SIZE(resp),
  447. DEFAULT_TIMEOUT);
  448. /* The USB device returns errors when it waits for the
  449. * STC bit to be set. Hence polling */
  450. if (err >= 0) {
  451. v4l2_dbg(1, debug, &sdev->sd,
  452. "%s: status bits: 0x%02x\n", __func__, resp[0]);
  453. if (resp[0] & SI4713_STC_INT)
  454. return 0;
  455. }
  456. if (jiffies_to_usecs(jiffies - start_jiffies) > usecs)
  457. return err < 0 ? err : -EIO;
  458. /* We sleep here for 3-4 ms in order to avoid flooding the device
  459. * with USB requests. The si4713 USB driver was developed
  460. * by reverse engineering the Windows USB driver. The windows
  461. * driver also has a ~2.5 ms delay between responses. */
  462. usleep_range(3000, 4000);
  463. }
  464. }
  465. /*
  466. * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
  467. * frequency between 76 and 108 MHz in 10 kHz units and
  468. * steps of 50 kHz.
  469. * @sdev: si4713_device structure for the device we are communicating
  470. * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
  471. */
  472. static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
  473. {
  474. int err;
  475. u8 val[SI4713_TXFREQ_NRESP];
  476. /*
  477. * .First byte = 0
  478. * .Second byte = frequency's MSB
  479. * .Third byte = frequency's LSB
  480. */
  481. const u8 args[SI4713_TXFREQ_NARGS] = {
  482. 0x00,
  483. msb(frequency),
  484. lsb(frequency),
  485. };
  486. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
  487. args, ARRAY_SIZE(args), val,
  488. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  489. if (err < 0)
  490. return err;
  491. v4l2_dbg(1, debug, &sdev->sd,
  492. "%s: frequency=0x%02x status=0x%02x\n", __func__,
  493. frequency, val[0]);
  494. err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
  495. if (err < 0)
  496. return err;
  497. return compose_u16(args[1], args[2]);
  498. }
  499. /*
  500. * si4713_tx_tune_power - Sets the RF voltage level between 88 and 120 dBuV in
  501. * 1 dB units. A value of 0x00 indicates off. The command
  502. * also sets the antenna tuning capacitance. A value of 0
  503. * indicates autotuning, and a value of 1 - 191 indicates
  504. * a manual override, which results in a tuning
  505. * capacitance of 0.25 pF x @antcap.
  506. * @sdev: si4713_device structure for the device we are communicating
  507. * @power: tuning power (88 - 120 dBuV, unit/step 1 dB)
  508. * @antcap: value of antenna tuning capacitor (0 - 191)
  509. */
  510. static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
  511. u8 antcap)
  512. {
  513. int err;
  514. u8 val[SI4713_TXPWR_NRESP];
  515. /*
  516. * .First byte = 0
  517. * .Second byte = 0
  518. * .Third byte = power
  519. * .Fourth byte = antcap
  520. */
  521. u8 args[SI4713_TXPWR_NARGS] = {
  522. 0x00,
  523. 0x00,
  524. power,
  525. antcap,
  526. };
  527. /* Map power values 1-87 to MIN_POWER (88) */
  528. if (power > 0 && power < SI4713_MIN_POWER)
  529. args[2] = power = SI4713_MIN_POWER;
  530. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
  531. args, ARRAY_SIZE(args), val,
  532. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  533. if (err < 0)
  534. return err;
  535. v4l2_dbg(1, debug, &sdev->sd,
  536. "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
  537. __func__, power, antcap, val[0]);
  538. return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
  539. }
  540. /*
  541. * si4713_tx_tune_measure - Enters receive mode and measures the received noise
  542. * level in units of dBuV on the selected frequency.
  543. * The Frequency must be between 76 and 108 MHz in 10 kHz
  544. * units and steps of 50 kHz. The command also sets the
  545. * antenna tuning capacitance. A value of 0 means
  546. * autotuning, and a value of 1 to 191 indicates manual
  547. * override.
  548. * @sdev: si4713_device structure for the device we are communicating
  549. * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
  550. * @antcap: value of antenna tuning capacitor (0 - 191)
  551. */
  552. static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
  553. u8 antcap)
  554. {
  555. int err;
  556. u8 val[SI4713_TXMEA_NRESP];
  557. /*
  558. * .First byte = 0
  559. * .Second byte = frequency's MSB
  560. * .Third byte = frequency's LSB
  561. * .Fourth byte = antcap
  562. */
  563. const u8 args[SI4713_TXMEA_NARGS] = {
  564. 0x00,
  565. msb(frequency),
  566. lsb(frequency),
  567. antcap,
  568. };
  569. sdev->tune_rnl = DEFAULT_TUNE_RNL;
  570. if (antcap > SI4713_MAX_ANTCAP)
  571. return -EDOM;
  572. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
  573. args, ARRAY_SIZE(args), val,
  574. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  575. if (err < 0)
  576. return err;
  577. v4l2_dbg(1, debug, &sdev->sd,
  578. "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
  579. __func__, frequency, antcap, val[0]);
  580. return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
  581. }
  582. /*
  583. * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
  584. * tx_tune_power commands. This command return the current
  585. * frequency, output voltage in dBuV, the antenna tunning
  586. * capacitance value and the received noise level. The
  587. * command also clears the stcint interrupt bit when the
  588. * first bit of its arguments is high.
  589. * @sdev: si4713_device structure for the device we are communicating
  590. * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
  591. * @frequency: returned frequency
  592. * @power: returned power
  593. * @antcap: returned antenna capacitance
  594. * @noise: returned noise level
  595. */
  596. static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
  597. u16 *frequency, u8 *power,
  598. u8 *antcap, u8 *noise)
  599. {
  600. int err;
  601. u8 val[SI4713_TXSTATUS_NRESP];
  602. /*
  603. * .First byte = intack bit
  604. */
  605. const u8 args[SI4713_TXSTATUS_NARGS] = {
  606. intack & SI4713_INTACK_MASK,
  607. };
  608. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
  609. args, ARRAY_SIZE(args), val,
  610. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  611. if (!err) {
  612. v4l2_dbg(1, debug, &sdev->sd,
  613. "%s: status=0x%02x\n", __func__, val[0]);
  614. *frequency = compose_u16(val[2], val[3]);
  615. sdev->frequency = *frequency;
  616. *power = val[5];
  617. *antcap = val[6];
  618. *noise = val[7];
  619. v4l2_dbg(1, debug, &sdev->sd,
  620. "%s: response: %d x 10 kHz (power %d, antcap %d, rnl %d)\n",
  621. __func__, *frequency, *power, *antcap, *noise);
  622. }
  623. return err;
  624. }
  625. /*
  626. * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
  627. * @sdev: si4713_device structure for the device we are communicating
  628. * @mode: the buffer operation mode.
  629. * @rdsb: RDS Block B
  630. * @rdsc: RDS Block C
  631. * @rdsd: RDS Block D
  632. * @cbleft: returns the number of available circular buffer blocks minus the
  633. * number of used circular buffer blocks.
  634. */
  635. static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
  636. u16 rdsc, u16 rdsd, s8 *cbleft)
  637. {
  638. int err;
  639. u8 val[SI4713_RDSBUFF_NRESP];
  640. const u8 args[SI4713_RDSBUFF_NARGS] = {
  641. mode & SI4713_RDSBUFF_MODE_MASK,
  642. msb(rdsb),
  643. lsb(rdsb),
  644. msb(rdsc),
  645. lsb(rdsc),
  646. msb(rdsd),
  647. lsb(rdsd),
  648. };
  649. err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
  650. args, ARRAY_SIZE(args), val,
  651. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  652. if (!err) {
  653. v4l2_dbg(1, debug, &sdev->sd,
  654. "%s: status=0x%02x\n", __func__, val[0]);
  655. *cbleft = (s8)val[2] - val[3];
  656. v4l2_dbg(1, debug, &sdev->sd,
  657. "%s: response: interrupts 0x%02x cb avail: %d cb used %d fifo avail %d fifo used %d\n",
  658. __func__, val[1], val[2], val[3], val[4], val[5]);
  659. }
  660. return err;
  661. }
  662. /*
  663. * si4713_tx_rds_ps - Loads the program service buffer.
  664. * @sdev: si4713_device structure for the device we are communicating
  665. * @psid: program service id to be loaded.
  666. * @pschar: assumed 4 size char array to be loaded into the program service
  667. */
  668. static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
  669. unsigned char *pschar)
  670. {
  671. int err;
  672. u8 val[SI4713_RDSPS_NRESP];
  673. const u8 args[SI4713_RDSPS_NARGS] = {
  674. psid & SI4713_RDSPS_PSID_MASK,
  675. pschar[0],
  676. pschar[1],
  677. pschar[2],
  678. pschar[3],
  679. };
  680. err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
  681. args, ARRAY_SIZE(args), val,
  682. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  683. if (err < 0)
  684. return err;
  685. v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
  686. return err;
  687. }
  688. static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
  689. {
  690. if (value)
  691. return si4713_powerup(sdev);
  692. return si4713_powerdown(sdev);
  693. }
  694. static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
  695. {
  696. int rval = 0;
  697. mute = set_mute(mute);
  698. if (sdev->power_state)
  699. rval = si4713_write_property(sdev,
  700. SI4713_TX_LINE_INPUT_MUTE, mute);
  701. return rval;
  702. }
  703. static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
  704. {
  705. int rval = 0, i;
  706. u8 len = 0;
  707. /* We want to clear the whole thing */
  708. if (!strlen(ps_name))
  709. memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
  710. if (sdev->power_state) {
  711. /* Write the new ps name and clear the padding */
  712. for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
  713. rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
  714. ps_name + i);
  715. if (rval < 0)
  716. return rval;
  717. }
  718. /* Setup the size to be sent */
  719. if (strlen(ps_name))
  720. len = strlen(ps_name) - 1;
  721. else
  722. len = 1;
  723. rval = si4713_write_property(sdev,
  724. SI4713_TX_RDS_PS_MESSAGE_COUNT,
  725. rds_ps_nblocks(len));
  726. if (rval < 0)
  727. return rval;
  728. rval = si4713_write_property(sdev,
  729. SI4713_TX_RDS_PS_REPEAT_COUNT,
  730. DEFAULT_RDS_PS_REPEAT_COUNT * 2);
  731. if (rval < 0)
  732. return rval;
  733. }
  734. return rval;
  735. }
  736. static int si4713_set_rds_radio_text(struct si4713_device *sdev, const char *rt)
  737. {
  738. static const char cr[RDS_RADIOTEXT_BLK_SIZE] = { RDS_CARRIAGE_RETURN, 0 };
  739. int rval = 0, i;
  740. u16 t_index = 0;
  741. u8 b_index = 0, cr_inserted = 0;
  742. s8 left;
  743. if (!sdev->power_state)
  744. return rval;
  745. rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
  746. if (rval < 0)
  747. return rval;
  748. if (!strlen(rt))
  749. return rval;
  750. do {
  751. /* RDS spec says that if the last block isn't used,
  752. * then apply a carriage return
  753. */
  754. if (t_index < (RDS_RADIOTEXT_INDEX_MAX * RDS_RADIOTEXT_BLK_SIZE)) {
  755. for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
  756. if (!rt[t_index + i] ||
  757. rt[t_index + i] == RDS_CARRIAGE_RETURN) {
  758. rt = cr;
  759. cr_inserted = 1;
  760. break;
  761. }
  762. }
  763. }
  764. rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
  765. compose_u16(RDS_RADIOTEXT_2A, b_index++),
  766. compose_u16(rt[t_index], rt[t_index + 1]),
  767. compose_u16(rt[t_index + 2], rt[t_index + 3]),
  768. &left);
  769. if (rval < 0)
  770. return rval;
  771. t_index += RDS_RADIOTEXT_BLK_SIZE;
  772. if (cr_inserted)
  773. break;
  774. } while (left > 0);
  775. return rval;
  776. }
  777. /*
  778. * si4713_update_tune_status - update properties from tx_tune_status
  779. * command. Must be called with sdev->mutex held.
  780. * @sdev: si4713_device structure for the device we are communicating
  781. */
  782. static int si4713_update_tune_status(struct si4713_device *sdev)
  783. {
  784. int rval;
  785. u16 f = 0;
  786. u8 p = 0, a = 0, n = 0;
  787. rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
  788. if (rval < 0)
  789. goto exit;
  790. /* TODO: check that power_level and antenna_capacitor really are not
  791. changed by the hardware. If they are, then these controls should become
  792. volatiles.
  793. sdev->power_level = p;
  794. sdev->antenna_capacitor = a;*/
  795. sdev->tune_rnl = n;
  796. exit:
  797. return rval;
  798. }
  799. static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
  800. s32 *bit, s32 *mask, u16 *property, int *mul,
  801. unsigned long **table, int *size)
  802. {
  803. s32 rval = 0;
  804. switch (id) {
  805. /* FM_TX class controls */
  806. case V4L2_CID_RDS_TX_PI:
  807. *property = SI4713_TX_RDS_PI;
  808. *mul = 1;
  809. break;
  810. case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
  811. *property = SI4713_TX_ACOMP_THRESHOLD;
  812. *mul = 1;
  813. break;
  814. case V4L2_CID_AUDIO_COMPRESSION_GAIN:
  815. *property = SI4713_TX_ACOMP_GAIN;
  816. *mul = 1;
  817. break;
  818. case V4L2_CID_PILOT_TONE_FREQUENCY:
  819. *property = SI4713_TX_PILOT_FREQUENCY;
  820. *mul = 1;
  821. break;
  822. case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
  823. *property = SI4713_TX_ACOMP_ATTACK_TIME;
  824. *mul = ATTACK_TIME_UNIT;
  825. break;
  826. case V4L2_CID_PILOT_TONE_DEVIATION:
  827. *property = SI4713_TX_PILOT_DEVIATION;
  828. *mul = 10;
  829. break;
  830. case V4L2_CID_AUDIO_LIMITER_DEVIATION:
  831. *property = SI4713_TX_AUDIO_DEVIATION;
  832. *mul = 10;
  833. break;
  834. case V4L2_CID_RDS_TX_DEVIATION:
  835. *property = SI4713_TX_RDS_DEVIATION;
  836. *mul = 1;
  837. break;
  838. case V4L2_CID_RDS_TX_PTY:
  839. *property = SI4713_TX_RDS_PS_MISC;
  840. *bit = 5;
  841. *mask = 0x1F << 5;
  842. break;
  843. case V4L2_CID_RDS_TX_DYNAMIC_PTY:
  844. *property = SI4713_TX_RDS_PS_MISC;
  845. *bit = 15;
  846. *mask = 1 << 15;
  847. break;
  848. case V4L2_CID_RDS_TX_COMPRESSED:
  849. *property = SI4713_TX_RDS_PS_MISC;
  850. *bit = 14;
  851. *mask = 1 << 14;
  852. break;
  853. case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:
  854. *property = SI4713_TX_RDS_PS_MISC;
  855. *bit = 13;
  856. *mask = 1 << 13;
  857. break;
  858. case V4L2_CID_RDS_TX_MONO_STEREO:
  859. *property = SI4713_TX_RDS_PS_MISC;
  860. *bit = 12;
  861. *mask = 1 << 12;
  862. break;
  863. case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:
  864. *property = SI4713_TX_RDS_PS_MISC;
  865. *bit = 10;
  866. *mask = 1 << 10;
  867. break;
  868. case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT:
  869. *property = SI4713_TX_RDS_PS_MISC;
  870. *bit = 4;
  871. *mask = 1 << 4;
  872. break;
  873. case V4L2_CID_RDS_TX_MUSIC_SPEECH:
  874. *property = SI4713_TX_RDS_PS_MISC;
  875. *bit = 3;
  876. *mask = 1 << 3;
  877. break;
  878. case V4L2_CID_AUDIO_LIMITER_ENABLED:
  879. *property = SI4713_TX_ACOMP_ENABLE;
  880. *bit = 1;
  881. *mask = 1 << 1;
  882. break;
  883. case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
  884. *property = SI4713_TX_ACOMP_ENABLE;
  885. *bit = 0;
  886. *mask = 1 << 0;
  887. break;
  888. case V4L2_CID_PILOT_TONE_ENABLED:
  889. *property = SI4713_TX_COMPONENT_ENABLE;
  890. *bit = 0;
  891. *mask = 1 << 0;
  892. break;
  893. case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
  894. *property = SI4713_TX_LIMITER_RELEASE_TIME;
  895. *table = limiter_times;
  896. *size = ARRAY_SIZE(limiter_times);
  897. break;
  898. case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
  899. *property = SI4713_TX_ACOMP_RELEASE_TIME;
  900. *table = acomp_rtimes;
  901. *size = ARRAY_SIZE(acomp_rtimes);
  902. break;
  903. case V4L2_CID_TUNE_PREEMPHASIS:
  904. *property = SI4713_TX_PREEMPHASIS;
  905. *table = preemphasis_values;
  906. *size = ARRAY_SIZE(preemphasis_values);
  907. break;
  908. default:
  909. rval = -EINVAL;
  910. break;
  911. }
  912. return rval;
  913. }
  914. static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f);
  915. static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *);
  916. /*
  917. * si4713_setup - Sets the device up with current configuration.
  918. * @sdev: si4713_device structure for the device we are communicating
  919. */
  920. static int si4713_setup(struct si4713_device *sdev)
  921. {
  922. struct v4l2_frequency f;
  923. struct v4l2_modulator vm;
  924. int rval;
  925. /* Device procedure needs to set frequency first */
  926. f.tuner = 0;
  927. f.frequency = sdev->frequency ? sdev->frequency : DEFAULT_FREQUENCY;
  928. f.frequency = si4713_to_v4l2(f.frequency);
  929. rval = si4713_s_frequency(&sdev->sd, &f);
  930. vm.index = 0;
  931. if (sdev->stereo)
  932. vm.txsubchans = V4L2_TUNER_SUB_STEREO;
  933. else
  934. vm.txsubchans = V4L2_TUNER_SUB_MONO;
  935. if (sdev->rds_enabled)
  936. vm.txsubchans |= V4L2_TUNER_SUB_RDS;
  937. si4713_s_modulator(&sdev->sd, &vm);
  938. return rval;
  939. }
  940. /*
  941. * si4713_initialize - Sets the device up with default configuration.
  942. * @sdev: si4713_device structure for the device we are communicating
  943. */
  944. static int si4713_initialize(struct si4713_device *sdev)
  945. {
  946. int rval;
  947. rval = si4713_set_power_state(sdev, POWER_ON);
  948. if (rval < 0)
  949. return rval;
  950. rval = si4713_checkrev(sdev);
  951. if (rval < 0)
  952. return rval;
  953. rval = si4713_set_power_state(sdev, POWER_OFF);
  954. if (rval < 0)
  955. return rval;
  956. sdev->frequency = DEFAULT_FREQUENCY;
  957. sdev->stereo = 1;
  958. sdev->tune_rnl = DEFAULT_TUNE_RNL;
  959. return 0;
  960. }
  961. /* si4713_s_ctrl - set the value of a control */
  962. static int si4713_s_ctrl(struct v4l2_ctrl *ctrl)
  963. {
  964. struct si4713_device *sdev =
  965. container_of(ctrl->handler, struct si4713_device, ctrl_handler);
  966. u32 val = 0;
  967. s32 bit = 0, mask = 0;
  968. u16 property = 0;
  969. int mul = 0;
  970. unsigned long *table = NULL;
  971. int size = 0;
  972. bool force = false;
  973. int c;
  974. int ret = 0;
  975. if (ctrl->id != V4L2_CID_AUDIO_MUTE)
  976. return -EINVAL;
  977. if (ctrl->is_new) {
  978. if (ctrl->val) {
  979. ret = si4713_set_mute(sdev, ctrl->val);
  980. if (!ret)
  981. ret = si4713_set_power_state(sdev, POWER_DOWN);
  982. return ret;
  983. }
  984. ret = si4713_set_power_state(sdev, POWER_UP);
  985. if (!ret)
  986. ret = si4713_set_mute(sdev, ctrl->val);
  987. if (!ret)
  988. ret = si4713_setup(sdev);
  989. if (ret)
  990. return ret;
  991. force = true;
  992. }
  993. if (!sdev->power_state)
  994. return 0;
  995. for (c = 1; !ret && c < ctrl->ncontrols; c++) {
  996. ctrl = ctrl->cluster[c];
  997. if (!force && !ctrl->is_new)
  998. continue;
  999. switch (ctrl->id) {
  1000. case V4L2_CID_RDS_TX_PS_NAME:
  1001. ret = si4713_set_rds_ps_name(sdev, ctrl->p_new.p_char);
  1002. break;
  1003. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1004. ret = si4713_set_rds_radio_text(sdev, ctrl->p_new.p_char);
  1005. break;
  1006. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1007. /* don't handle this control if we force setting all
  1008. * controls since in that case it will be handled by
  1009. * V4L2_CID_TUNE_POWER_LEVEL. */
  1010. if (force)
  1011. break;
  1012. /* fall through */
  1013. case V4L2_CID_TUNE_POWER_LEVEL:
  1014. ret = si4713_tx_tune_power(sdev,
  1015. sdev->tune_pwr_level->val, sdev->tune_ant_cap->val);
  1016. if (!ret) {
  1017. /* Make sure we don't set this twice */
  1018. sdev->tune_ant_cap->is_new = false;
  1019. sdev->tune_pwr_level->is_new = false;
  1020. }
  1021. break;
  1022. case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:
  1023. case V4L2_CID_RDS_TX_ALT_FREQS:
  1024. if (sdev->rds_alt_freqs_enable->val) {
  1025. val = sdev->rds_alt_freqs->p_new.p_u32[0];
  1026. val = val / 100 - 876 + 0xe101;
  1027. } else {
  1028. val = 0xe0e0;
  1029. }
  1030. ret = si4713_write_property(sdev, SI4713_TX_RDS_PS_AF, val);
  1031. break;
  1032. default:
  1033. ret = si4713_choose_econtrol_action(sdev, ctrl->id, &bit,
  1034. &mask, &property, &mul, &table, &size);
  1035. if (ret < 0)
  1036. break;
  1037. val = ctrl->val;
  1038. if (mul) {
  1039. val = val / mul;
  1040. } else if (table) {
  1041. ret = usecs_to_dev(val, table, size);
  1042. if (ret < 0)
  1043. break;
  1044. val = ret;
  1045. ret = 0;
  1046. }
  1047. if (mask) {
  1048. ret = si4713_read_property(sdev, property, &val);
  1049. if (ret < 0)
  1050. break;
  1051. val = set_bits(val, ctrl->val, bit, mask);
  1052. }
  1053. ret = si4713_write_property(sdev, property, val);
  1054. if (ret < 0)
  1055. break;
  1056. if (mask)
  1057. val = ctrl->val;
  1058. break;
  1059. }
  1060. }
  1061. return ret;
  1062. }
  1063. /* si4713_ioctl - deal with private ioctls (only rnl for now) */
  1064. static long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  1065. {
  1066. struct si4713_device *sdev = to_si4713_device(sd);
  1067. struct si4713_rnl *rnl = arg;
  1068. u16 frequency;
  1069. int rval = 0;
  1070. if (!arg)
  1071. return -EINVAL;
  1072. switch (cmd) {
  1073. case SI4713_IOC_MEASURE_RNL:
  1074. frequency = v4l2_to_si4713(rnl->frequency);
  1075. if (sdev->power_state) {
  1076. /* Set desired measurement frequency */
  1077. rval = si4713_tx_tune_measure(sdev, frequency, 0);
  1078. if (rval < 0)
  1079. return rval;
  1080. /* get results from tune status */
  1081. rval = si4713_update_tune_status(sdev);
  1082. if (rval < 0)
  1083. return rval;
  1084. }
  1085. rnl->rnl = sdev->tune_rnl;
  1086. break;
  1087. default:
  1088. /* nothing */
  1089. rval = -ENOIOCTLCMD;
  1090. }
  1091. return rval;
  1092. }
  1093. /* si4713_g_modulator - get modulator attributes */
  1094. static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
  1095. {
  1096. struct si4713_device *sdev = to_si4713_device(sd);
  1097. int rval = 0;
  1098. if (!sdev)
  1099. return -ENODEV;
  1100. if (vm->index > 0)
  1101. return -EINVAL;
  1102. strncpy(vm->name, "FM Modulator", 32);
  1103. vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
  1104. V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
  1105. /* Report current frequency range limits */
  1106. vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
  1107. vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
  1108. if (sdev->power_state) {
  1109. u32 comp_en = 0;
  1110. rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
  1111. &comp_en);
  1112. if (rval < 0)
  1113. return rval;
  1114. sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
  1115. }
  1116. /* Report current audio mode: mono or stereo */
  1117. if (sdev->stereo)
  1118. vm->txsubchans = V4L2_TUNER_SUB_STEREO;
  1119. else
  1120. vm->txsubchans = V4L2_TUNER_SUB_MONO;
  1121. /* Report rds feature status */
  1122. if (sdev->rds_enabled)
  1123. vm->txsubchans |= V4L2_TUNER_SUB_RDS;
  1124. else
  1125. vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
  1126. return rval;
  1127. }
  1128. /* si4713_s_modulator - set modulator attributes */
  1129. static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *vm)
  1130. {
  1131. struct si4713_device *sdev = to_si4713_device(sd);
  1132. int rval = 0;
  1133. u16 stereo, rds;
  1134. u32 p;
  1135. if (!sdev)
  1136. return -ENODEV;
  1137. if (vm->index > 0)
  1138. return -EINVAL;
  1139. /* Set audio mode: mono or stereo */
  1140. if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
  1141. stereo = 1;
  1142. else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
  1143. stereo = 0;
  1144. else
  1145. return -EINVAL;
  1146. rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
  1147. if (sdev->power_state) {
  1148. rval = si4713_read_property(sdev,
  1149. SI4713_TX_COMPONENT_ENABLE, &p);
  1150. if (rval < 0)
  1151. return rval;
  1152. p = set_bits(p, stereo, 1, 1 << 1);
  1153. p = set_bits(p, rds, 2, 1 << 2);
  1154. rval = si4713_write_property(sdev,
  1155. SI4713_TX_COMPONENT_ENABLE, p);
  1156. if (rval < 0)
  1157. return rval;
  1158. }
  1159. sdev->stereo = stereo;
  1160. sdev->rds_enabled = rds;
  1161. return rval;
  1162. }
  1163. /* si4713_g_frequency - get tuner or modulator radio frequency */
  1164. static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  1165. {
  1166. struct si4713_device *sdev = to_si4713_device(sd);
  1167. int rval = 0;
  1168. if (f->tuner)
  1169. return -EINVAL;
  1170. if (sdev->power_state) {
  1171. u16 freq;
  1172. u8 p, a, n;
  1173. rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
  1174. if (rval < 0)
  1175. return rval;
  1176. sdev->frequency = freq;
  1177. }
  1178. f->frequency = si4713_to_v4l2(sdev->frequency);
  1179. return rval;
  1180. }
  1181. /* si4713_s_frequency - set tuner or modulator radio frequency */
  1182. static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
  1183. {
  1184. struct si4713_device *sdev = to_si4713_device(sd);
  1185. int rval = 0;
  1186. u16 frequency = v4l2_to_si4713(f->frequency);
  1187. if (f->tuner)
  1188. return -EINVAL;
  1189. /* Check frequency range */
  1190. frequency = clamp_t(u16, frequency, FREQ_RANGE_LOW, FREQ_RANGE_HIGH);
  1191. if (sdev->power_state) {
  1192. rval = si4713_tx_tune_freq(sdev, frequency);
  1193. if (rval < 0)
  1194. return rval;
  1195. frequency = rval;
  1196. rval = 0;
  1197. }
  1198. sdev->frequency = frequency;
  1199. return rval;
  1200. }
  1201. static const struct v4l2_ctrl_ops si4713_ctrl_ops = {
  1202. .s_ctrl = si4713_s_ctrl,
  1203. };
  1204. static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
  1205. .ioctl = si4713_ioctl,
  1206. };
  1207. static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
  1208. .g_frequency = si4713_g_frequency,
  1209. .s_frequency = si4713_s_frequency,
  1210. .g_modulator = si4713_g_modulator,
  1211. .s_modulator = si4713_s_modulator,
  1212. };
  1213. static const struct v4l2_subdev_ops si4713_subdev_ops = {
  1214. .core = &si4713_subdev_core_ops,
  1215. .tuner = &si4713_subdev_tuner_ops,
  1216. };
  1217. static const struct v4l2_ctrl_config si4713_alt_freqs_ctrl = {
  1218. .id = V4L2_CID_RDS_TX_ALT_FREQS,
  1219. .type = V4L2_CTRL_TYPE_U32,
  1220. .min = 87600,
  1221. .max = 107900,
  1222. .step = 100,
  1223. .def = 87600,
  1224. .dims = { 1 },
  1225. .elem_size = sizeof(u32),
  1226. };
  1227. /*
  1228. * I2C driver interface
  1229. */
  1230. /* si4713_probe - probe for the device */
  1231. static int si4713_probe(struct i2c_client *client,
  1232. const struct i2c_device_id *id)
  1233. {
  1234. struct si4713_device *sdev;
  1235. struct v4l2_ctrl_handler *hdl;
  1236. struct si4713_platform_data *pdata = client->dev.platform_data;
  1237. struct device_node *np = client->dev.of_node;
  1238. struct radio_si4713_platform_data si4713_pdev_pdata;
  1239. struct platform_device *si4713_pdev;
  1240. int rval;
  1241. sdev = devm_kzalloc(&client->dev, sizeof(*sdev), GFP_KERNEL);
  1242. if (!sdev) {
  1243. dev_err(&client->dev, "Failed to alloc video device.\n");
  1244. rval = -ENOMEM;
  1245. goto exit;
  1246. }
  1247. sdev->gpio_reset = devm_gpiod_get_optional(&client->dev, "reset",
  1248. GPIOD_OUT_LOW);
  1249. if (IS_ERR(sdev->gpio_reset)) {
  1250. rval = PTR_ERR(sdev->gpio_reset);
  1251. dev_err(&client->dev, "Failed to request gpio: %d\n", rval);
  1252. goto exit;
  1253. }
  1254. sdev->vdd = devm_regulator_get_optional(&client->dev, "vdd");
  1255. if (IS_ERR(sdev->vdd)) {
  1256. rval = PTR_ERR(sdev->vdd);
  1257. if (rval == -EPROBE_DEFER)
  1258. goto exit;
  1259. dev_dbg(&client->dev, "no vdd regulator found: %d\n", rval);
  1260. sdev->vdd = NULL;
  1261. }
  1262. sdev->vio = devm_regulator_get_optional(&client->dev, "vio");
  1263. if (IS_ERR(sdev->vio)) {
  1264. rval = PTR_ERR(sdev->vio);
  1265. if (rval == -EPROBE_DEFER)
  1266. goto exit;
  1267. dev_dbg(&client->dev, "no vio regulator found: %d\n", rval);
  1268. sdev->vio = NULL;
  1269. }
  1270. v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
  1271. init_completion(&sdev->work);
  1272. hdl = &sdev->ctrl_handler;
  1273. v4l2_ctrl_handler_init(hdl, 20);
  1274. sdev->mute = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1275. V4L2_CID_AUDIO_MUTE, 0, 1, 1, DEFAULT_MUTE);
  1276. sdev->rds_pi = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1277. V4L2_CID_RDS_TX_PI, 0, 0xffff, 1, DEFAULT_RDS_PI);
  1278. sdev->rds_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1279. V4L2_CID_RDS_TX_PTY, 0, 31, 1, DEFAULT_RDS_PTY);
  1280. sdev->rds_compressed = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1281. V4L2_CID_RDS_TX_COMPRESSED, 0, 1, 1, 0);
  1282. sdev->rds_art_head = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1283. V4L2_CID_RDS_TX_ARTIFICIAL_HEAD, 0, 1, 1, 0);
  1284. sdev->rds_stereo = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1285. V4L2_CID_RDS_TX_MONO_STEREO, 0, 1, 1, 1);
  1286. sdev->rds_tp = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1287. V4L2_CID_RDS_TX_TRAFFIC_PROGRAM, 0, 1, 1, 0);
  1288. sdev->rds_ta = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1289. V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT, 0, 1, 1, 0);
  1290. sdev->rds_ms = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1291. V4L2_CID_RDS_TX_MUSIC_SPEECH, 0, 1, 1, 1);
  1292. sdev->rds_dyn_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1293. V4L2_CID_RDS_TX_DYNAMIC_PTY, 0, 1, 1, 0);
  1294. sdev->rds_alt_freqs_enable = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1295. V4L2_CID_RDS_TX_ALT_FREQS_ENABLE, 0, 1, 1, 0);
  1296. sdev->rds_alt_freqs = v4l2_ctrl_new_custom(hdl, &si4713_alt_freqs_ctrl, NULL);
  1297. sdev->rds_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1298. V4L2_CID_RDS_TX_DEVIATION, 0, MAX_RDS_DEVIATION,
  1299. 10, DEFAULT_RDS_DEVIATION);
  1300. /*
  1301. * Report step as 8. From RDS spec, psname
  1302. * should be 8. But there are receivers which scroll strings
  1303. * sized as 8xN.
  1304. */
  1305. sdev->rds_ps_name = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1306. V4L2_CID_RDS_TX_PS_NAME, 0, MAX_RDS_PS_NAME, 8, 0);
  1307. /*
  1308. * Report step as 32 (2A block). From RDS spec,
  1309. * radio text should be 32 for 2A block. But there are receivers
  1310. * which scroll strings sized as 32xN. Setting default to 32.
  1311. */
  1312. sdev->rds_radio_text = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1313. V4L2_CID_RDS_TX_RADIO_TEXT, 0, MAX_RDS_RADIO_TEXT, 32, 0);
  1314. sdev->limiter_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1315. V4L2_CID_AUDIO_LIMITER_ENABLED, 0, 1, 1, 1);
  1316. sdev->limiter_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1317. V4L2_CID_AUDIO_LIMITER_RELEASE_TIME, 250,
  1318. MAX_LIMITER_RELEASE_TIME, 10, DEFAULT_LIMITER_RTIME);
  1319. sdev->limiter_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1320. V4L2_CID_AUDIO_LIMITER_DEVIATION, 0,
  1321. MAX_LIMITER_DEVIATION, 10, DEFAULT_LIMITER_DEV);
  1322. sdev->compression_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1323. V4L2_CID_AUDIO_COMPRESSION_ENABLED, 0, 1, 1, 1);
  1324. sdev->compression_gain = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1325. V4L2_CID_AUDIO_COMPRESSION_GAIN, 0, MAX_ACOMP_GAIN, 1,
  1326. DEFAULT_ACOMP_GAIN);
  1327. sdev->compression_threshold = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1328. V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
  1329. MIN_ACOMP_THRESHOLD, MAX_ACOMP_THRESHOLD, 1,
  1330. DEFAULT_ACOMP_THRESHOLD);
  1331. sdev->compression_attack_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1332. V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME, 0,
  1333. MAX_ACOMP_ATTACK_TIME, 500, DEFAULT_ACOMP_ATIME);
  1334. sdev->compression_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1335. V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME, 100000,
  1336. MAX_ACOMP_RELEASE_TIME, 100000, DEFAULT_ACOMP_RTIME);
  1337. sdev->pilot_tone_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1338. V4L2_CID_PILOT_TONE_ENABLED, 0, 1, 1, 1);
  1339. sdev->pilot_tone_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1340. V4L2_CID_PILOT_TONE_DEVIATION, 0, MAX_PILOT_DEVIATION,
  1341. 10, DEFAULT_PILOT_DEVIATION);
  1342. sdev->pilot_tone_freq = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1343. V4L2_CID_PILOT_TONE_FREQUENCY, 0, MAX_PILOT_FREQUENCY,
  1344. 1, DEFAULT_PILOT_FREQUENCY);
  1345. sdev->tune_preemphasis = v4l2_ctrl_new_std_menu(hdl, &si4713_ctrl_ops,
  1346. V4L2_CID_TUNE_PREEMPHASIS,
  1347. V4L2_PREEMPHASIS_75_uS, 0, V4L2_PREEMPHASIS_50_uS);
  1348. sdev->tune_pwr_level = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1349. V4L2_CID_TUNE_POWER_LEVEL, 0, SI4713_MAX_POWER,
  1350. 1, DEFAULT_POWER_LEVEL);
  1351. sdev->tune_ant_cap = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
  1352. V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0, SI4713_MAX_ANTCAP,
  1353. 1, 0);
  1354. if (hdl->error) {
  1355. rval = hdl->error;
  1356. goto free_ctrls;
  1357. }
  1358. v4l2_ctrl_cluster(29, &sdev->mute);
  1359. sdev->sd.ctrl_handler = hdl;
  1360. if (client->irq) {
  1361. rval = devm_request_irq(&client->dev, client->irq,
  1362. si4713_handler, IRQF_TRIGGER_FALLING,
  1363. client->name, sdev);
  1364. if (rval < 0) {
  1365. v4l2_err(&sdev->sd, "Could not request IRQ\n");
  1366. goto free_ctrls;
  1367. }
  1368. v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
  1369. } else {
  1370. v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
  1371. }
  1372. rval = si4713_initialize(sdev);
  1373. if (rval < 0) {
  1374. v4l2_err(&sdev->sd, "Failed to probe device information.\n");
  1375. goto free_ctrls;
  1376. }
  1377. if (!np && (!pdata || !pdata->is_platform_device))
  1378. return 0;
  1379. si4713_pdev = platform_device_alloc("radio-si4713", -1);
  1380. if (!si4713_pdev) {
  1381. rval = -ENOMEM;
  1382. goto put_main_pdev;
  1383. }
  1384. si4713_pdev_pdata.subdev = client;
  1385. rval = platform_device_add_data(si4713_pdev, &si4713_pdev_pdata,
  1386. sizeof(si4713_pdev_pdata));
  1387. if (rval)
  1388. goto put_main_pdev;
  1389. rval = platform_device_add(si4713_pdev);
  1390. if (rval)
  1391. goto put_main_pdev;
  1392. sdev->pd = si4713_pdev;
  1393. return 0;
  1394. put_main_pdev:
  1395. platform_device_put(si4713_pdev);
  1396. v4l2_device_unregister_subdev(&sdev->sd);
  1397. free_ctrls:
  1398. v4l2_ctrl_handler_free(hdl);
  1399. exit:
  1400. return rval;
  1401. }
  1402. /* si4713_remove - remove the device */
  1403. static int si4713_remove(struct i2c_client *client)
  1404. {
  1405. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1406. struct si4713_device *sdev = to_si4713_device(sd);
  1407. platform_device_unregister(sdev->pd);
  1408. if (sdev->power_state)
  1409. si4713_set_power_state(sdev, POWER_DOWN);
  1410. v4l2_device_unregister_subdev(sd);
  1411. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1412. return 0;
  1413. }
  1414. /* si4713_i2c_driver - i2c driver interface */
  1415. static const struct i2c_device_id si4713_id[] = {
  1416. { "si4713" , 0 },
  1417. { },
  1418. };
  1419. MODULE_DEVICE_TABLE(i2c, si4713_id);
  1420. #if IS_ENABLED(CONFIG_OF)
  1421. static const struct of_device_id si4713_of_match[] = {
  1422. { .compatible = "silabs,si4713" },
  1423. { },
  1424. };
  1425. MODULE_DEVICE_TABLE(of, si4713_of_match);
  1426. #endif
  1427. static struct i2c_driver si4713_i2c_driver = {
  1428. .driver = {
  1429. .name = "si4713",
  1430. .of_match_table = of_match_ptr(si4713_of_match),
  1431. },
  1432. .probe = si4713_probe,
  1433. .remove = si4713_remove,
  1434. .id_table = si4713_id,
  1435. };
  1436. module_i2c_driver(si4713_i2c_driver);