si4713.c 42 KB

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