si4713.c 41 KB

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