si4713-i2c.c 38 KB

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