si4713.c 42 KB

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