si4713-i2c.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  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/mutex.h>
  24. #include <linux/completion.h>
  25. #include <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/i2c.h>
  28. #include <linux/slab.h>
  29. #include <linux/gpio.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <linux/module.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <media/v4l2-common.h>
  35. #include "si4713-i2c.h"
  36. /* module parameters */
  37. static int debug;
  38. module_param(debug, int, S_IRUGO | S_IWUSR);
  39. MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
  40. MODULE_LICENSE("GPL");
  41. MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
  42. MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
  43. MODULE_VERSION("0.0.1");
  44. static const char *si4713_supply_names[SI4713_NUM_SUPPLIES] = {
  45. "vio",
  46. "vdd",
  47. };
  48. #define DEFAULT_RDS_PI 0x00
  49. #define DEFAULT_RDS_PTY 0x00
  50. #define DEFAULT_RDS_PS_NAME ""
  51. #define DEFAULT_RDS_RADIO_TEXT DEFAULT_RDS_PS_NAME
  52. #define DEFAULT_RDS_DEVIATION 0x00C8
  53. #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
  54. #define DEFAULT_LIMITER_RTIME 0x1392
  55. #define DEFAULT_LIMITER_DEV 0x102CA
  56. #define DEFAULT_PILOT_FREQUENCY 0x4A38
  57. #define DEFAULT_PILOT_DEVIATION 0x1A5E
  58. #define DEFAULT_ACOMP_ATIME 0x0000
  59. #define DEFAULT_ACOMP_RTIME 0xF4240L
  60. #define DEFAULT_ACOMP_GAIN 0x0F
  61. #define DEFAULT_ACOMP_THRESHOLD (-0x28)
  62. #define DEFAULT_MUTE 0x01
  63. #define DEFAULT_POWER_LEVEL 88
  64. #define DEFAULT_FREQUENCY 8800
  65. #define DEFAULT_PREEMPHASIS FMPE_EU
  66. #define DEFAULT_TUNE_RNL 0xFF
  67. #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
  68. /* frequency domain transformation (using times 10 to avoid floats) */
  69. #define FREQDEV_UNIT 100000
  70. #define FREQV4L2_MULTI 625
  71. #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
  72. #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
  73. #define FREQ_RANGE_LOW 7600
  74. #define FREQ_RANGE_HIGH 10800
  75. #define MAX_ARGS 7
  76. #define RDS_BLOCK 8
  77. #define RDS_BLOCK_CLEAR 0x03
  78. #define RDS_BLOCK_LOAD 0x04
  79. #define RDS_RADIOTEXT_2A 0x20
  80. #define RDS_RADIOTEXT_BLK_SIZE 4
  81. #define RDS_RADIOTEXT_INDEX_MAX 0x0F
  82. #define RDS_CARRIAGE_RETURN 0x0D
  83. #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
  84. #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
  85. #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
  86. #define ATTACK_TIME_UNIT 500
  87. #define POWER_OFF 0x00
  88. #define POWER_ON 0x01
  89. #define msb(x) ((u8)((u16) x >> 8))
  90. #define lsb(x) ((u8)((u16) x & 0x00FF))
  91. #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
  92. #define check_command_failed(status) (!(status & SI4713_CTS) || \
  93. (status & SI4713_ERR))
  94. /* mute definition */
  95. #define set_mute(p) ((p & 1) | ((p & 1) << 1));
  96. #define get_mute(p) (p & 0x01)
  97. #ifdef DEBUG
  98. #define DBG_BUFFER(device, message, buffer, size) \
  99. { \
  100. int i; \
  101. char str[(size)*5]; \
  102. for (i = 0; i < size; i++) \
  103. sprintf(str + i * 5, " 0x%02x", buffer[i]); \
  104. v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
  105. }
  106. #else
  107. #define DBG_BUFFER(device, message, buffer, size)
  108. #endif
  109. /*
  110. * Values for limiter release time (sorted by second column)
  111. * device release
  112. * value time (us)
  113. */
  114. static long limiter_times[] = {
  115. 2000, 250,
  116. 1000, 500,
  117. 510, 1000,
  118. 255, 2000,
  119. 170, 3000,
  120. 127, 4020,
  121. 102, 5010,
  122. 85, 6020,
  123. 73, 7010,
  124. 64, 7990,
  125. 57, 8970,
  126. 51, 10030,
  127. 25, 20470,
  128. 17, 30110,
  129. 13, 39380,
  130. 10, 51190,
  131. 8, 63690,
  132. 7, 73140,
  133. 6, 85330,
  134. 5, 102390,
  135. };
  136. /*
  137. * Values for audio compression release time (sorted by second column)
  138. * device release
  139. * value time (us)
  140. */
  141. static unsigned long acomp_rtimes[] = {
  142. 0, 100000,
  143. 1, 200000,
  144. 2, 350000,
  145. 3, 525000,
  146. 4, 1000000,
  147. };
  148. /*
  149. * Values for preemphasis (sorted by second column)
  150. * device preemphasis
  151. * value value (v4l2)
  152. */
  153. static unsigned long preemphasis_values[] = {
  154. FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
  155. FMPE_EU, V4L2_PREEMPHASIS_50_uS,
  156. FMPE_USA, V4L2_PREEMPHASIS_75_uS,
  157. };
  158. static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
  159. int size)
  160. {
  161. int i;
  162. int rval = -EINVAL;
  163. for (i = 0; i < size / 2; i++)
  164. if (array[(i * 2) + 1] >= usecs) {
  165. rval = array[i * 2];
  166. break;
  167. }
  168. return rval;
  169. }
  170. static unsigned long dev_to_usecs(int value, unsigned long const array[],
  171. int size)
  172. {
  173. int i;
  174. int rval = -EINVAL;
  175. for (i = 0; i < size / 2; i++)
  176. if (array[i * 2] == value) {
  177. rval = array[(i * 2) + 1];
  178. break;
  179. }
  180. return rval;
  181. }
  182. /* si4713_handler: IRQ handler, just complete work */
  183. static irqreturn_t si4713_handler(int irq, void *dev)
  184. {
  185. struct si4713_device *sdev = dev;
  186. v4l2_dbg(2, debug, &sdev->sd,
  187. "%s: sending signal to completion work.\n", __func__);
  188. complete(&sdev->work);
  189. return IRQ_HANDLED;
  190. }
  191. /*
  192. * si4713_send_command - sends a command to si4713 and waits its response
  193. * @sdev: si4713_device structure for the device we are communicating
  194. * @command: command id
  195. * @args: command arguments we are sending (up to 7)
  196. * @argn: actual size of @args
  197. * @response: buffer to place the expected response from the device (up to 15)
  198. * @respn: actual size of @response
  199. * @usecs: amount of time to wait before reading the response (in usecs)
  200. */
  201. static int si4713_send_command(struct si4713_device *sdev, const u8 command,
  202. const u8 args[], const int argn,
  203. u8 response[], const int respn, const int usecs)
  204. {
  205. struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
  206. u8 data1[MAX_ARGS + 1];
  207. int err;
  208. if (!client->adapter)
  209. return -ENODEV;
  210. /* First send the command and its arguments */
  211. data1[0] = command;
  212. memcpy(data1 + 1, args, argn);
  213. DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
  214. err = i2c_master_send(client, data1, argn + 1);
  215. if (err != argn + 1) {
  216. v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
  217. command);
  218. return (err > 0) ? -EIO : err;
  219. }
  220. /* Wait response from interrupt */
  221. if (!wait_for_completion_timeout(&sdev->work,
  222. usecs_to_jiffies(usecs) + 1))
  223. v4l2_warn(&sdev->sd,
  224. "(%s) Device took too much time to answer.\n",
  225. __func__);
  226. /* Then get the response */
  227. err = i2c_master_recv(client, response, respn);
  228. if (err != respn) {
  229. v4l2_err(&sdev->sd,
  230. "Error while reading response for command 0x%02x\n",
  231. command);
  232. return (err > 0) ? -EIO : err;
  233. }
  234. DBG_BUFFER(&sdev->sd, "Response", response, respn);
  235. if (check_command_failed(response[0]))
  236. return -EBUSY;
  237. return 0;
  238. }
  239. /*
  240. * si4713_read_property - reads a si4713 property
  241. * @sdev: si4713_device structure for the device we are communicating
  242. * @prop: property identification number
  243. * @pv: property value to be returned on success
  244. */
  245. static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
  246. {
  247. int err;
  248. u8 val[SI4713_GET_PROP_NRESP];
  249. /*
  250. * .First byte = 0
  251. * .Second byte = property's MSB
  252. * .Third byte = property's LSB
  253. */
  254. const u8 args[SI4713_GET_PROP_NARGS] = {
  255. 0x00,
  256. msb(prop),
  257. lsb(prop),
  258. };
  259. err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
  260. args, ARRAY_SIZE(args), val,
  261. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  262. if (err < 0)
  263. return err;
  264. *pv = compose_u16(val[2], val[3]);
  265. v4l2_dbg(1, debug, &sdev->sd,
  266. "%s: property=0x%02x value=0x%02x status=0x%02x\n",
  267. __func__, prop, *pv, val[0]);
  268. return err;
  269. }
  270. /*
  271. * si4713_write_property - modifies a si4713 property
  272. * @sdev: si4713_device structure for the device we are communicating
  273. * @prop: property identification number
  274. * @val: new value for that property
  275. */
  276. static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
  277. {
  278. int rval;
  279. u8 resp[SI4713_SET_PROP_NRESP];
  280. /*
  281. * .First byte = 0
  282. * .Second byte = property's MSB
  283. * .Third byte = property's LSB
  284. * .Fourth byte = value's MSB
  285. * .Fifth byte = value's LSB
  286. */
  287. const u8 args[SI4713_SET_PROP_NARGS] = {
  288. 0x00,
  289. msb(prop),
  290. lsb(prop),
  291. msb(val),
  292. lsb(val),
  293. };
  294. rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
  295. args, ARRAY_SIZE(args),
  296. resp, ARRAY_SIZE(resp),
  297. DEFAULT_TIMEOUT);
  298. if (rval < 0)
  299. return rval;
  300. v4l2_dbg(1, debug, &sdev->sd,
  301. "%s: property=0x%02x value=0x%02x status=0x%02x\n",
  302. __func__, prop, val, resp[0]);
  303. /*
  304. * As there is no command response for SET_PROPERTY,
  305. * wait Tcomp time to finish before proceed, in order
  306. * to have property properly set.
  307. */
  308. msleep(TIMEOUT_SET_PROPERTY);
  309. return rval;
  310. }
  311. /*
  312. * si4713_powerup - Powers the device up
  313. * @sdev: si4713_device structure for the device we are communicating
  314. */
  315. static int si4713_powerup(struct si4713_device *sdev)
  316. {
  317. int err;
  318. u8 resp[SI4713_PWUP_NRESP];
  319. /*
  320. * .First byte = Enabled interrupts and boot function
  321. * .Second byte = Input operation mode
  322. */
  323. const u8 args[SI4713_PWUP_NARGS] = {
  324. SI4713_PWUP_CTSIEN | SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
  325. SI4713_PWUP_OPMOD_ANALOG,
  326. };
  327. if (sdev->power_state)
  328. return 0;
  329. err = regulator_bulk_enable(ARRAY_SIZE(sdev->supplies),
  330. sdev->supplies);
  331. if (err) {
  332. v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err);
  333. return err;
  334. }
  335. if (gpio_is_valid(sdev->gpio_reset)) {
  336. udelay(50);
  337. gpio_set_value(sdev->gpio_reset, 1);
  338. }
  339. err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
  340. args, ARRAY_SIZE(args),
  341. resp, ARRAY_SIZE(resp),
  342. TIMEOUT_POWER_UP);
  343. if (!err) {
  344. v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
  345. resp[0]);
  346. v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
  347. sdev->power_state = POWER_ON;
  348. err = si4713_write_property(sdev, SI4713_GPO_IEN,
  349. SI4713_STC_INT | SI4713_CTS);
  350. } else {
  351. if (gpio_is_valid(sdev->gpio_reset))
  352. gpio_set_value(sdev->gpio_reset, 0);
  353. err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
  354. sdev->supplies);
  355. if (err)
  356. v4l2_err(&sdev->sd,
  357. "Failed to disable supplies: %d\n", err);
  358. }
  359. return err;
  360. }
  361. /*
  362. * si4713_powerdown - Powers the device down
  363. * @sdev: si4713_device structure for the device we are communicating
  364. */
  365. static int si4713_powerdown(struct si4713_device *sdev)
  366. {
  367. int err;
  368. u8 resp[SI4713_PWDN_NRESP];
  369. if (!sdev->power_state)
  370. return 0;
  371. err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
  372. NULL, 0,
  373. resp, ARRAY_SIZE(resp),
  374. DEFAULT_TIMEOUT);
  375. if (!err) {
  376. v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
  377. resp[0]);
  378. v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
  379. if (gpio_is_valid(sdev->gpio_reset))
  380. gpio_set_value(sdev->gpio_reset, 0);
  381. err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
  382. sdev->supplies);
  383. if (err)
  384. v4l2_err(&sdev->sd,
  385. "Failed to disable supplies: %d\n", err);
  386. sdev->power_state = POWER_OFF;
  387. }
  388. return err;
  389. }
  390. /*
  391. * si4713_checkrev - Checks if we are treating a device with the correct rev.
  392. * @sdev: si4713_device structure for the device we are communicating
  393. */
  394. static int si4713_checkrev(struct si4713_device *sdev)
  395. {
  396. struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
  397. int rval;
  398. u8 resp[SI4713_GETREV_NRESP];
  399. mutex_lock(&sdev->mutex);
  400. rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
  401. NULL, 0,
  402. resp, ARRAY_SIZE(resp),
  403. DEFAULT_TIMEOUT);
  404. if (rval < 0)
  405. goto unlock;
  406. if (resp[1] == SI4713_PRODUCT_NUMBER) {
  407. v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
  408. client->addr << 1, client->adapter->name);
  409. } else {
  410. v4l2_err(&sdev->sd, "Invalid product number\n");
  411. rval = -EINVAL;
  412. }
  413. unlock:
  414. mutex_unlock(&sdev->mutex);
  415. return rval;
  416. }
  417. /*
  418. * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
  419. * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
  420. * @sdev: si4713_device structure for the device we are communicating
  421. * @usecs: timeout to wait for STC interrupt signal
  422. */
  423. static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
  424. {
  425. int err;
  426. u8 resp[SI4713_GET_STATUS_NRESP];
  427. /* Wait response from STC interrupt */
  428. if (!wait_for_completion_timeout(&sdev->work,
  429. usecs_to_jiffies(usecs) + 1))
  430. v4l2_warn(&sdev->sd,
  431. "%s: device took too much time to answer (%d usec).\n",
  432. __func__, usecs);
  433. /* Clear status bits */
  434. err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
  435. NULL, 0,
  436. resp, ARRAY_SIZE(resp),
  437. DEFAULT_TIMEOUT);
  438. if (err < 0)
  439. goto exit;
  440. v4l2_dbg(1, debug, &sdev->sd,
  441. "%s: status bits: 0x%02x\n", __func__, resp[0]);
  442. if (!(resp[0] & SI4713_STC_INT))
  443. err = -EIO;
  444. exit:
  445. return err;
  446. }
  447. /*
  448. * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
  449. * frequency between 76 and 108 MHz in 10 kHz units and
  450. * steps of 50 kHz.
  451. * @sdev: si4713_device structure for the device we are communicating
  452. * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
  453. */
  454. static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
  455. {
  456. int err;
  457. u8 val[SI4713_TXFREQ_NRESP];
  458. /*
  459. * .First byte = 0
  460. * .Second byte = frequency's MSB
  461. * .Third byte = frequency's LSB
  462. */
  463. const u8 args[SI4713_TXFREQ_NARGS] = {
  464. 0x00,
  465. msb(frequency),
  466. lsb(frequency),
  467. };
  468. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
  469. args, ARRAY_SIZE(args), val,
  470. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  471. if (err < 0)
  472. return err;
  473. v4l2_dbg(1, debug, &sdev->sd,
  474. "%s: frequency=0x%02x status=0x%02x\n", __func__,
  475. frequency, val[0]);
  476. err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
  477. if (err < 0)
  478. return err;
  479. return compose_u16(args[1], args[2]);
  480. }
  481. /*
  482. * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
  483. * 1 dB units. A value of 0x00 indicates off. The command
  484. * also sets the antenna tuning capacitance. A value of 0
  485. * indicates autotuning, and a value of 1 - 191 indicates
  486. * a manual override, which results in a tuning
  487. * capacitance of 0.25 pF x @antcap.
  488. * @sdev: si4713_device structure for the device we are communicating
  489. * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
  490. * @antcap: value of antenna tuning capacitor (0 - 191)
  491. */
  492. static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
  493. u8 antcap)
  494. {
  495. int err;
  496. u8 val[SI4713_TXPWR_NRESP];
  497. /*
  498. * .First byte = 0
  499. * .Second byte = 0
  500. * .Third byte = power
  501. * .Fourth byte = antcap
  502. */
  503. const u8 args[SI4713_TXPWR_NARGS] = {
  504. 0x00,
  505. 0x00,
  506. power,
  507. antcap,
  508. };
  509. if (((power > 0) && (power < SI4713_MIN_POWER)) ||
  510. power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
  511. return -EDOM;
  512. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
  513. args, ARRAY_SIZE(args), val,
  514. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  515. if (err < 0)
  516. return err;
  517. v4l2_dbg(1, debug, &sdev->sd,
  518. "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
  519. __func__, power, antcap, val[0]);
  520. return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
  521. }
  522. /*
  523. * si4713_tx_tune_measure - Enters receive mode and measures the received noise
  524. * level in units of dBuV on the selected frequency.
  525. * The Frequency must be between 76 and 108 MHz in 10 kHz
  526. * units and steps of 50 kHz. The command also sets the
  527. * antenna tuning capacitance. A value of 0 means
  528. * autotuning, and a value of 1 to 191 indicates manual
  529. * override.
  530. * @sdev: si4713_device structure for the device we are communicating
  531. * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
  532. * @antcap: value of antenna tuning capacitor (0 - 191)
  533. */
  534. static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
  535. u8 antcap)
  536. {
  537. int err;
  538. u8 val[SI4713_TXMEA_NRESP];
  539. /*
  540. * .First byte = 0
  541. * .Second byte = frequency's MSB
  542. * .Third byte = frequency's LSB
  543. * .Fourth byte = antcap
  544. */
  545. const u8 args[SI4713_TXMEA_NARGS] = {
  546. 0x00,
  547. msb(frequency),
  548. lsb(frequency),
  549. antcap,
  550. };
  551. sdev->tune_rnl = DEFAULT_TUNE_RNL;
  552. if (antcap > SI4713_MAX_ANTCAP)
  553. return -EDOM;
  554. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
  555. args, ARRAY_SIZE(args), val,
  556. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  557. if (err < 0)
  558. return err;
  559. v4l2_dbg(1, debug, &sdev->sd,
  560. "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
  561. __func__, frequency, antcap, val[0]);
  562. return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
  563. }
  564. /*
  565. * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
  566. * tx_tune_power commands. This command return the current
  567. * frequency, output voltage in dBuV, the antenna tunning
  568. * capacitance value and the received noise level. The
  569. * command also clears the stcint interrupt bit when the
  570. * first bit of its arguments is high.
  571. * @sdev: si4713_device structure for the device we are communicating
  572. * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
  573. * @frequency: returned frequency
  574. * @power: returned power
  575. * @antcap: returned antenna capacitance
  576. * @noise: returned noise level
  577. */
  578. static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
  579. u16 *frequency, u8 *power,
  580. u8 *antcap, u8 *noise)
  581. {
  582. int err;
  583. u8 val[SI4713_TXSTATUS_NRESP];
  584. /*
  585. * .First byte = intack bit
  586. */
  587. const u8 args[SI4713_TXSTATUS_NARGS] = {
  588. intack & SI4713_INTACK_MASK,
  589. };
  590. err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
  591. args, ARRAY_SIZE(args), val,
  592. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  593. if (!err) {
  594. v4l2_dbg(1, debug, &sdev->sd,
  595. "%s: status=0x%02x\n", __func__, val[0]);
  596. *frequency = compose_u16(val[2], val[3]);
  597. sdev->frequency = *frequency;
  598. *power = val[5];
  599. *antcap = val[6];
  600. *noise = val[7];
  601. v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
  602. "(power %d, antcap %d, rnl %d)\n", __func__,
  603. *frequency, *power, *antcap, *noise);
  604. }
  605. return err;
  606. }
  607. /*
  608. * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
  609. * @sdev: si4713_device structure for the device we are communicating
  610. * @mode: the buffer operation mode.
  611. * @rdsb: RDS Block B
  612. * @rdsc: RDS Block C
  613. * @rdsd: RDS Block D
  614. * @cbleft: returns the number of available circular buffer blocks minus the
  615. * number of used circular buffer blocks.
  616. */
  617. static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
  618. u16 rdsc, u16 rdsd, s8 *cbleft)
  619. {
  620. int err;
  621. u8 val[SI4713_RDSBUFF_NRESP];
  622. const u8 args[SI4713_RDSBUFF_NARGS] = {
  623. mode & SI4713_RDSBUFF_MODE_MASK,
  624. msb(rdsb),
  625. lsb(rdsb),
  626. msb(rdsc),
  627. lsb(rdsc),
  628. msb(rdsd),
  629. lsb(rdsd),
  630. };
  631. err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
  632. args, ARRAY_SIZE(args), val,
  633. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  634. if (!err) {
  635. v4l2_dbg(1, debug, &sdev->sd,
  636. "%s: status=0x%02x\n", __func__, val[0]);
  637. *cbleft = (s8)val[2] - val[3];
  638. v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
  639. " 0x%02x cb avail: %d cb used %d fifo avail"
  640. " %d fifo used %d\n", __func__, val[1],
  641. val[2], val[3], val[4], val[5]);
  642. }
  643. return err;
  644. }
  645. /*
  646. * si4713_tx_rds_ps - Loads the program service buffer.
  647. * @sdev: si4713_device structure for the device we are communicating
  648. * @psid: program service id to be loaded.
  649. * @pschar: assumed 4 size char array to be loaded into the program service
  650. */
  651. static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
  652. unsigned char *pschar)
  653. {
  654. int err;
  655. u8 val[SI4713_RDSPS_NRESP];
  656. const u8 args[SI4713_RDSPS_NARGS] = {
  657. psid & SI4713_RDSPS_PSID_MASK,
  658. pschar[0],
  659. pschar[1],
  660. pschar[2],
  661. pschar[3],
  662. };
  663. err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
  664. args, ARRAY_SIZE(args), val,
  665. ARRAY_SIZE(val), DEFAULT_TIMEOUT);
  666. if (err < 0)
  667. return err;
  668. v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
  669. return err;
  670. }
  671. static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
  672. {
  673. int rval;
  674. mutex_lock(&sdev->mutex);
  675. if (value)
  676. rval = si4713_powerup(sdev);
  677. else
  678. rval = si4713_powerdown(sdev);
  679. mutex_unlock(&sdev->mutex);
  680. return rval;
  681. }
  682. static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
  683. {
  684. int rval = 0;
  685. mute = set_mute(mute);
  686. mutex_lock(&sdev->mutex);
  687. if (sdev->power_state)
  688. rval = si4713_write_property(sdev,
  689. SI4713_TX_LINE_INPUT_MUTE, mute);
  690. if (rval >= 0)
  691. sdev->mute = get_mute(mute);
  692. mutex_unlock(&sdev->mutex);
  693. return rval;
  694. }
  695. static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
  696. {
  697. int rval = 0, i;
  698. u8 len = 0;
  699. /* We want to clear the whole thing */
  700. if (!strlen(ps_name))
  701. memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
  702. mutex_lock(&sdev->mutex);
  703. if (sdev->power_state) {
  704. /* Write the new ps name and clear the padding */
  705. for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
  706. rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
  707. ps_name + i);
  708. if (rval < 0)
  709. goto unlock;
  710. }
  711. /* Setup the size to be sent */
  712. if (strlen(ps_name))
  713. len = strlen(ps_name) - 1;
  714. else
  715. len = 1;
  716. rval = si4713_write_property(sdev,
  717. SI4713_TX_RDS_PS_MESSAGE_COUNT,
  718. rds_ps_nblocks(len));
  719. if (rval < 0)
  720. goto unlock;
  721. rval = si4713_write_property(sdev,
  722. SI4713_TX_RDS_PS_REPEAT_COUNT,
  723. DEFAULT_RDS_PS_REPEAT_COUNT * 2);
  724. if (rval < 0)
  725. goto unlock;
  726. }
  727. strncpy(sdev->rds_info.ps_name, ps_name, MAX_RDS_PS_NAME);
  728. unlock:
  729. mutex_unlock(&sdev->mutex);
  730. return rval;
  731. }
  732. static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
  733. {
  734. int rval = 0, i;
  735. u16 t_index = 0;
  736. u8 b_index = 0, cr_inserted = 0;
  737. s8 left;
  738. mutex_lock(&sdev->mutex);
  739. if (!sdev->power_state)
  740. goto copy;
  741. rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
  742. if (rval < 0)
  743. goto unlock;
  744. if (!strlen(rt))
  745. goto copy;
  746. do {
  747. /* RDS spec says that if the last block isn't used,
  748. * then apply a carriage return
  749. */
  750. if (t_index < (RDS_RADIOTEXT_INDEX_MAX *
  751. RDS_RADIOTEXT_BLK_SIZE)) {
  752. for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
  753. if (!rt[t_index + i] || rt[t_index + i] ==
  754. RDS_CARRIAGE_RETURN) {
  755. rt[t_index + i] = RDS_CARRIAGE_RETURN;
  756. cr_inserted = 1;
  757. break;
  758. }
  759. }
  760. }
  761. rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
  762. compose_u16(RDS_RADIOTEXT_2A, b_index++),
  763. compose_u16(rt[t_index], rt[t_index + 1]),
  764. compose_u16(rt[t_index + 2], rt[t_index + 3]),
  765. &left);
  766. if (rval < 0)
  767. goto unlock;
  768. t_index += RDS_RADIOTEXT_BLK_SIZE;
  769. if (cr_inserted)
  770. break;
  771. } while (left > 0);
  772. copy:
  773. strncpy(sdev->rds_info.radio_text, rt, MAX_RDS_RADIO_TEXT);
  774. unlock:
  775. mutex_unlock(&sdev->mutex);
  776. return rval;
  777. }
  778. static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
  779. u32 **shadow, s32 *bit, s32 *mask, u16 *property, int *mul,
  780. unsigned long **table, int *size)
  781. {
  782. s32 rval = 0;
  783. switch (id) {
  784. /* FM_TX class controls */
  785. case V4L2_CID_RDS_TX_PI:
  786. *property = SI4713_TX_RDS_PI;
  787. *mul = 1;
  788. *shadow = &sdev->rds_info.pi;
  789. break;
  790. case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
  791. *property = SI4713_TX_ACOMP_THRESHOLD;
  792. *mul = 1;
  793. *shadow = &sdev->acomp_info.threshold;
  794. break;
  795. case V4L2_CID_AUDIO_COMPRESSION_GAIN:
  796. *property = SI4713_TX_ACOMP_GAIN;
  797. *mul = 1;
  798. *shadow = &sdev->acomp_info.gain;
  799. break;
  800. case V4L2_CID_PILOT_TONE_FREQUENCY:
  801. *property = SI4713_TX_PILOT_FREQUENCY;
  802. *mul = 1;
  803. *shadow = &sdev->pilot_info.frequency;
  804. break;
  805. case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
  806. *property = SI4713_TX_ACOMP_ATTACK_TIME;
  807. *mul = ATTACK_TIME_UNIT;
  808. *shadow = &sdev->acomp_info.attack_time;
  809. break;
  810. case V4L2_CID_PILOT_TONE_DEVIATION:
  811. *property = SI4713_TX_PILOT_DEVIATION;
  812. *mul = 10;
  813. *shadow = &sdev->pilot_info.deviation;
  814. break;
  815. case V4L2_CID_AUDIO_LIMITER_DEVIATION:
  816. *property = SI4713_TX_AUDIO_DEVIATION;
  817. *mul = 10;
  818. *shadow = &sdev->limiter_info.deviation;
  819. break;
  820. case V4L2_CID_RDS_TX_DEVIATION:
  821. *property = SI4713_TX_RDS_DEVIATION;
  822. *mul = 1;
  823. *shadow = &sdev->rds_info.deviation;
  824. break;
  825. case V4L2_CID_RDS_TX_PTY:
  826. *property = SI4713_TX_RDS_PS_MISC;
  827. *bit = 5;
  828. *mask = 0x1F << 5;
  829. *shadow = &sdev->rds_info.pty;
  830. break;
  831. case V4L2_CID_AUDIO_LIMITER_ENABLED:
  832. *property = SI4713_TX_ACOMP_ENABLE;
  833. *bit = 1;
  834. *mask = 1 << 1;
  835. *shadow = &sdev->limiter_info.enabled;
  836. break;
  837. case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
  838. *property = SI4713_TX_ACOMP_ENABLE;
  839. *bit = 0;
  840. *mask = 1 << 0;
  841. *shadow = &sdev->acomp_info.enabled;
  842. break;
  843. case V4L2_CID_PILOT_TONE_ENABLED:
  844. *property = SI4713_TX_COMPONENT_ENABLE;
  845. *bit = 0;
  846. *mask = 1 << 0;
  847. *shadow = &sdev->pilot_info.enabled;
  848. break;
  849. case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
  850. *property = SI4713_TX_LIMITER_RELEASE_TIME;
  851. *table = limiter_times;
  852. *size = ARRAY_SIZE(limiter_times);
  853. *shadow = &sdev->limiter_info.release_time;
  854. break;
  855. case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
  856. *property = SI4713_TX_ACOMP_RELEASE_TIME;
  857. *table = acomp_rtimes;
  858. *size = ARRAY_SIZE(acomp_rtimes);
  859. *shadow = &sdev->acomp_info.release_time;
  860. break;
  861. case V4L2_CID_TUNE_PREEMPHASIS:
  862. *property = SI4713_TX_PREEMPHASIS;
  863. *table = preemphasis_values;
  864. *size = ARRAY_SIZE(preemphasis_values);
  865. *shadow = &sdev->preemphasis;
  866. break;
  867. default:
  868. rval = -EINVAL;
  869. };
  870. return rval;
  871. }
  872. static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
  873. /* write string property */
  874. static int si4713_write_econtrol_string(struct si4713_device *sdev,
  875. struct v4l2_ext_control *control)
  876. {
  877. struct v4l2_queryctrl vqc;
  878. int len;
  879. s32 rval = 0;
  880. vqc.id = control->id;
  881. rval = si4713_queryctrl(&sdev->sd, &vqc);
  882. if (rval < 0)
  883. goto exit;
  884. switch (control->id) {
  885. case V4L2_CID_RDS_TX_PS_NAME: {
  886. char ps_name[MAX_RDS_PS_NAME + 1];
  887. len = control->size - 1;
  888. if (len < 0 || len > MAX_RDS_PS_NAME) {
  889. rval = -ERANGE;
  890. goto exit;
  891. }
  892. rval = copy_from_user(ps_name, control->string, len);
  893. if (rval) {
  894. rval = -EFAULT;
  895. goto exit;
  896. }
  897. ps_name[len] = '\0';
  898. if (strlen(ps_name) % vqc.step) {
  899. rval = -ERANGE;
  900. goto exit;
  901. }
  902. rval = si4713_set_rds_ps_name(sdev, ps_name);
  903. }
  904. break;
  905. case V4L2_CID_RDS_TX_RADIO_TEXT: {
  906. char radio_text[MAX_RDS_RADIO_TEXT + 1];
  907. len = control->size - 1;
  908. if (len < 0 || len > MAX_RDS_RADIO_TEXT) {
  909. rval = -ERANGE;
  910. goto exit;
  911. }
  912. rval = copy_from_user(radio_text, control->string, len);
  913. if (rval) {
  914. rval = -EFAULT;
  915. goto exit;
  916. }
  917. radio_text[len] = '\0';
  918. if (strlen(radio_text) % vqc.step) {
  919. rval = -ERANGE;
  920. goto exit;
  921. }
  922. rval = si4713_set_rds_radio_text(sdev, radio_text);
  923. }
  924. break;
  925. default:
  926. rval = -EINVAL;
  927. break;
  928. };
  929. exit:
  930. return rval;
  931. }
  932. static int validate_range(struct v4l2_subdev *sd,
  933. struct v4l2_ext_control *control)
  934. {
  935. struct v4l2_queryctrl vqc;
  936. int rval;
  937. vqc.id = control->id;
  938. rval = si4713_queryctrl(sd, &vqc);
  939. if (rval < 0)
  940. goto exit;
  941. if (control->value < vqc.minimum || control->value > vqc.maximum)
  942. rval = -ERANGE;
  943. exit:
  944. return rval;
  945. }
  946. /* properties which use tx_tune_power*/
  947. static int si4713_write_econtrol_tune(struct si4713_device *sdev,
  948. struct v4l2_ext_control *control)
  949. {
  950. s32 rval = 0;
  951. u8 power, antcap;
  952. rval = validate_range(&sdev->sd, control);
  953. if (rval < 0)
  954. goto exit;
  955. mutex_lock(&sdev->mutex);
  956. switch (control->id) {
  957. case V4L2_CID_TUNE_POWER_LEVEL:
  958. power = control->value;
  959. antcap = sdev->antenna_capacitor;
  960. break;
  961. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  962. power = sdev->power_level;
  963. antcap = control->value;
  964. break;
  965. default:
  966. rval = -EINVAL;
  967. goto unlock;
  968. };
  969. if (sdev->power_state)
  970. rval = si4713_tx_tune_power(sdev, power, antcap);
  971. if (rval == 0) {
  972. sdev->power_level = power;
  973. sdev->antenna_capacitor = antcap;
  974. }
  975. unlock:
  976. mutex_unlock(&sdev->mutex);
  977. exit:
  978. return rval;
  979. }
  980. static int si4713_write_econtrol_integers(struct si4713_device *sdev,
  981. struct v4l2_ext_control *control)
  982. {
  983. s32 rval;
  984. u32 *shadow = NULL, val = 0;
  985. s32 bit = 0, mask = 0;
  986. u16 property = 0;
  987. int mul = 0;
  988. unsigned long *table = NULL;
  989. int size = 0;
  990. rval = validate_range(&sdev->sd, control);
  991. if (rval < 0)
  992. goto exit;
  993. rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
  994. &mask, &property, &mul, &table, &size);
  995. if (rval < 0)
  996. goto exit;
  997. val = control->value;
  998. if (mul) {
  999. val = control->value / mul;
  1000. } else if (table) {
  1001. rval = usecs_to_dev(control->value, table, size);
  1002. if (rval < 0)
  1003. goto exit;
  1004. val = rval;
  1005. rval = 0;
  1006. }
  1007. mutex_lock(&sdev->mutex);
  1008. if (sdev->power_state) {
  1009. if (mask) {
  1010. rval = si4713_read_property(sdev, property, &val);
  1011. if (rval < 0)
  1012. goto unlock;
  1013. val = set_bits(val, control->value, bit, mask);
  1014. }
  1015. rval = si4713_write_property(sdev, property, val);
  1016. if (rval < 0)
  1017. goto unlock;
  1018. if (mask)
  1019. val = control->value;
  1020. }
  1021. if (mul) {
  1022. *shadow = val * mul;
  1023. } else if (table) {
  1024. rval = dev_to_usecs(val, table, size);
  1025. if (rval < 0)
  1026. goto unlock;
  1027. *shadow = rval;
  1028. rval = 0;
  1029. } else {
  1030. *shadow = val;
  1031. }
  1032. unlock:
  1033. mutex_unlock(&sdev->mutex);
  1034. exit:
  1035. return rval;
  1036. }
  1037. static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
  1038. static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *);
  1039. /*
  1040. * si4713_setup - Sets the device up with current configuration.
  1041. * @sdev: si4713_device structure for the device we are communicating
  1042. */
  1043. static int si4713_setup(struct si4713_device *sdev)
  1044. {
  1045. struct v4l2_ext_control ctrl;
  1046. struct v4l2_frequency f;
  1047. struct v4l2_modulator vm;
  1048. struct si4713_device *tmp;
  1049. int rval = 0;
  1050. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  1051. if (!tmp)
  1052. return -ENOMEM;
  1053. /* Get a local copy to avoid race */
  1054. mutex_lock(&sdev->mutex);
  1055. memcpy(tmp, sdev, sizeof(*sdev));
  1056. mutex_unlock(&sdev->mutex);
  1057. ctrl.id = V4L2_CID_RDS_TX_PI;
  1058. ctrl.value = tmp->rds_info.pi;
  1059. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1060. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_THRESHOLD;
  1061. ctrl.value = tmp->acomp_info.threshold;
  1062. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1063. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_GAIN;
  1064. ctrl.value = tmp->acomp_info.gain;
  1065. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1066. ctrl.id = V4L2_CID_PILOT_TONE_FREQUENCY;
  1067. ctrl.value = tmp->pilot_info.frequency;
  1068. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1069. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME;
  1070. ctrl.value = tmp->acomp_info.attack_time;
  1071. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1072. ctrl.id = V4L2_CID_PILOT_TONE_DEVIATION;
  1073. ctrl.value = tmp->pilot_info.deviation;
  1074. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1075. ctrl.id = V4L2_CID_AUDIO_LIMITER_DEVIATION;
  1076. ctrl.value = tmp->limiter_info.deviation;
  1077. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1078. ctrl.id = V4L2_CID_RDS_TX_DEVIATION;
  1079. ctrl.value = tmp->rds_info.deviation;
  1080. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1081. ctrl.id = V4L2_CID_RDS_TX_PTY;
  1082. ctrl.value = tmp->rds_info.pty;
  1083. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1084. ctrl.id = V4L2_CID_AUDIO_LIMITER_ENABLED;
  1085. ctrl.value = tmp->limiter_info.enabled;
  1086. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1087. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ENABLED;
  1088. ctrl.value = tmp->acomp_info.enabled;
  1089. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1090. ctrl.id = V4L2_CID_PILOT_TONE_ENABLED;
  1091. ctrl.value = tmp->pilot_info.enabled;
  1092. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1093. ctrl.id = V4L2_CID_AUDIO_LIMITER_RELEASE_TIME;
  1094. ctrl.value = tmp->limiter_info.release_time;
  1095. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1096. ctrl.id = V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME;
  1097. ctrl.value = tmp->acomp_info.release_time;
  1098. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1099. ctrl.id = V4L2_CID_TUNE_PREEMPHASIS;
  1100. ctrl.value = tmp->preemphasis;
  1101. rval |= si4713_write_econtrol_integers(sdev, &ctrl);
  1102. ctrl.id = V4L2_CID_RDS_TX_PS_NAME;
  1103. rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
  1104. ctrl.id = V4L2_CID_RDS_TX_RADIO_TEXT;
  1105. rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
  1106. /* Device procedure needs to set frequency first */
  1107. f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
  1108. f.frequency = si4713_to_v4l2(f.frequency);
  1109. rval |= si4713_s_frequency(&sdev->sd, &f);
  1110. ctrl.id = V4L2_CID_TUNE_POWER_LEVEL;
  1111. ctrl.value = tmp->power_level;
  1112. rval |= si4713_write_econtrol_tune(sdev, &ctrl);
  1113. ctrl.id = V4L2_CID_TUNE_ANTENNA_CAPACITOR;
  1114. ctrl.value = tmp->antenna_capacitor;
  1115. rval |= si4713_write_econtrol_tune(sdev, &ctrl);
  1116. vm.index = 0;
  1117. if (tmp->stereo)
  1118. vm.txsubchans = V4L2_TUNER_SUB_STEREO;
  1119. else
  1120. vm.txsubchans = V4L2_TUNER_SUB_MONO;
  1121. if (tmp->rds_info.enabled)
  1122. vm.txsubchans |= V4L2_TUNER_SUB_RDS;
  1123. si4713_s_modulator(&sdev->sd, &vm);
  1124. kfree(tmp);
  1125. return rval;
  1126. }
  1127. /*
  1128. * si4713_initialize - Sets the device up with default configuration.
  1129. * @sdev: si4713_device structure for the device we are communicating
  1130. */
  1131. static int si4713_initialize(struct si4713_device *sdev)
  1132. {
  1133. int rval;
  1134. rval = si4713_set_power_state(sdev, POWER_ON);
  1135. if (rval < 0)
  1136. goto exit;
  1137. rval = si4713_checkrev(sdev);
  1138. if (rval < 0)
  1139. goto exit;
  1140. rval = si4713_set_power_state(sdev, POWER_OFF);
  1141. if (rval < 0)
  1142. goto exit;
  1143. mutex_lock(&sdev->mutex);
  1144. sdev->rds_info.pi = DEFAULT_RDS_PI;
  1145. sdev->rds_info.pty = DEFAULT_RDS_PTY;
  1146. sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
  1147. strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
  1148. strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
  1149. MAX_RDS_RADIO_TEXT);
  1150. sdev->rds_info.enabled = 1;
  1151. sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
  1152. sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
  1153. sdev->limiter_info.enabled = 1;
  1154. sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
  1155. sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
  1156. sdev->pilot_info.enabled = 1;
  1157. sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
  1158. sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
  1159. sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
  1160. sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
  1161. sdev->acomp_info.enabled = 1;
  1162. sdev->frequency = DEFAULT_FREQUENCY;
  1163. sdev->preemphasis = DEFAULT_PREEMPHASIS;
  1164. sdev->mute = DEFAULT_MUTE;
  1165. sdev->power_level = DEFAULT_POWER_LEVEL;
  1166. sdev->antenna_capacitor = 0;
  1167. sdev->stereo = 1;
  1168. sdev->tune_rnl = DEFAULT_TUNE_RNL;
  1169. mutex_unlock(&sdev->mutex);
  1170. exit:
  1171. return rval;
  1172. }
  1173. /* read string property */
  1174. static int si4713_read_econtrol_string(struct si4713_device *sdev,
  1175. struct v4l2_ext_control *control)
  1176. {
  1177. s32 rval = 0;
  1178. switch (control->id) {
  1179. case V4L2_CID_RDS_TX_PS_NAME:
  1180. if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
  1181. control->size = MAX_RDS_PS_NAME + 1;
  1182. rval = -ENOSPC;
  1183. goto exit;
  1184. }
  1185. rval = copy_to_user(control->string, sdev->rds_info.ps_name,
  1186. strlen(sdev->rds_info.ps_name) + 1);
  1187. if (rval)
  1188. rval = -EFAULT;
  1189. break;
  1190. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1191. if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
  1192. control->size = MAX_RDS_RADIO_TEXT + 1;
  1193. rval = -ENOSPC;
  1194. goto exit;
  1195. }
  1196. rval = copy_to_user(control->string, sdev->rds_info.radio_text,
  1197. strlen(sdev->rds_info.radio_text) + 1);
  1198. if (rval)
  1199. rval = -EFAULT;
  1200. break;
  1201. default:
  1202. rval = -EINVAL;
  1203. break;
  1204. };
  1205. exit:
  1206. return rval;
  1207. }
  1208. /*
  1209. * si4713_update_tune_status - update properties from tx_tune_status
  1210. * command. Must be called with sdev->mutex held.
  1211. * @sdev: si4713_device structure for the device we are communicating
  1212. */
  1213. static int si4713_update_tune_status(struct si4713_device *sdev)
  1214. {
  1215. int rval;
  1216. u16 f = 0;
  1217. u8 p = 0, a = 0, n = 0;
  1218. rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
  1219. if (rval < 0)
  1220. goto exit;
  1221. sdev->power_level = p;
  1222. sdev->antenna_capacitor = a;
  1223. sdev->tune_rnl = n;
  1224. exit:
  1225. return rval;
  1226. }
  1227. /* properties which use tx_tune_status */
  1228. static int si4713_read_econtrol_tune(struct si4713_device *sdev,
  1229. struct v4l2_ext_control *control)
  1230. {
  1231. s32 rval = 0;
  1232. mutex_lock(&sdev->mutex);
  1233. if (sdev->power_state) {
  1234. rval = si4713_update_tune_status(sdev);
  1235. if (rval < 0)
  1236. goto unlock;
  1237. }
  1238. switch (control->id) {
  1239. case V4L2_CID_TUNE_POWER_LEVEL:
  1240. control->value = sdev->power_level;
  1241. break;
  1242. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1243. control->value = sdev->antenna_capacitor;
  1244. break;
  1245. default:
  1246. rval = -EINVAL;
  1247. };
  1248. unlock:
  1249. mutex_unlock(&sdev->mutex);
  1250. return rval;
  1251. }
  1252. static int si4713_read_econtrol_integers(struct si4713_device *sdev,
  1253. struct v4l2_ext_control *control)
  1254. {
  1255. s32 rval;
  1256. u32 *shadow = NULL, val = 0;
  1257. s32 bit = 0, mask = 0;
  1258. u16 property = 0;
  1259. int mul = 0;
  1260. unsigned long *table = NULL;
  1261. int size = 0;
  1262. rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
  1263. &mask, &property, &mul, &table, &size);
  1264. if (rval < 0)
  1265. goto exit;
  1266. mutex_lock(&sdev->mutex);
  1267. if (sdev->power_state) {
  1268. rval = si4713_read_property(sdev, property, &val);
  1269. if (rval < 0)
  1270. goto unlock;
  1271. /* Keep negative values for threshold */
  1272. if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
  1273. *shadow = (s16)val;
  1274. else if (mask)
  1275. *shadow = get_status_bit(val, bit, mask);
  1276. else if (mul)
  1277. *shadow = val * mul;
  1278. else
  1279. *shadow = dev_to_usecs(val, table, size);
  1280. }
  1281. control->value = *shadow;
  1282. unlock:
  1283. mutex_unlock(&sdev->mutex);
  1284. exit:
  1285. return rval;
  1286. }
  1287. /*
  1288. * Video4Linux Subdev Interface
  1289. */
  1290. /* si4713_s_ext_ctrls - set extended controls value */
  1291. static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
  1292. struct v4l2_ext_controls *ctrls)
  1293. {
  1294. struct si4713_device *sdev = to_si4713_device(sd);
  1295. int i;
  1296. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  1297. return -EINVAL;
  1298. for (i = 0; i < ctrls->count; i++) {
  1299. int err;
  1300. switch ((ctrls->controls + i)->id) {
  1301. case V4L2_CID_RDS_TX_PS_NAME:
  1302. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1303. err = si4713_write_econtrol_string(sdev,
  1304. ctrls->controls + i);
  1305. break;
  1306. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1307. case V4L2_CID_TUNE_POWER_LEVEL:
  1308. err = si4713_write_econtrol_tune(sdev,
  1309. ctrls->controls + i);
  1310. break;
  1311. default:
  1312. err = si4713_write_econtrol_integers(sdev,
  1313. ctrls->controls + i);
  1314. }
  1315. if (err < 0) {
  1316. ctrls->error_idx = i;
  1317. return err;
  1318. }
  1319. }
  1320. return 0;
  1321. }
  1322. /* si4713_g_ext_ctrls - get extended controls value */
  1323. static int si4713_g_ext_ctrls(struct v4l2_subdev *sd,
  1324. struct v4l2_ext_controls *ctrls)
  1325. {
  1326. struct si4713_device *sdev = to_si4713_device(sd);
  1327. int i;
  1328. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  1329. return -EINVAL;
  1330. for (i = 0; i < ctrls->count; i++) {
  1331. int err;
  1332. switch ((ctrls->controls + i)->id) {
  1333. case V4L2_CID_RDS_TX_PS_NAME:
  1334. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1335. err = si4713_read_econtrol_string(sdev,
  1336. ctrls->controls + i);
  1337. break;
  1338. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1339. case V4L2_CID_TUNE_POWER_LEVEL:
  1340. err = si4713_read_econtrol_tune(sdev,
  1341. ctrls->controls + i);
  1342. break;
  1343. default:
  1344. err = si4713_read_econtrol_integers(sdev,
  1345. ctrls->controls + i);
  1346. }
  1347. if (err < 0) {
  1348. ctrls->error_idx = i;
  1349. return err;
  1350. }
  1351. }
  1352. return 0;
  1353. }
  1354. /* si4713_queryctrl - enumerate control items */
  1355. static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  1356. {
  1357. int rval = 0;
  1358. switch (qc->id) {
  1359. /* User class controls */
  1360. case V4L2_CID_AUDIO_MUTE:
  1361. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
  1362. break;
  1363. /* FM_TX class controls */
  1364. case V4L2_CID_RDS_TX_PI:
  1365. rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
  1366. break;
  1367. case V4L2_CID_RDS_TX_PTY:
  1368. rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
  1369. break;
  1370. case V4L2_CID_RDS_TX_DEVIATION:
  1371. rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_DEVIATION,
  1372. 10, DEFAULT_RDS_DEVIATION);
  1373. break;
  1374. case V4L2_CID_RDS_TX_PS_NAME:
  1375. /*
  1376. * Report step as 8. From RDS spec, psname
  1377. * should be 8. But there are receivers which scroll strings
  1378. * sized as 8xN.
  1379. */
  1380. rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
  1381. break;
  1382. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1383. /*
  1384. * Report step as 32 (2A block). From RDS spec,
  1385. * radio text should be 32 for 2A block. But there are receivers
  1386. * which scroll strings sized as 32xN. Setting default to 32.
  1387. */
  1388. rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
  1389. break;
  1390. case V4L2_CID_AUDIO_LIMITER_ENABLED:
  1391. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1392. break;
  1393. case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
  1394. rval = v4l2_ctrl_query_fill(qc, 250, MAX_LIMITER_RELEASE_TIME,
  1395. 50, DEFAULT_LIMITER_RTIME);
  1396. break;
  1397. case V4L2_CID_AUDIO_LIMITER_DEVIATION:
  1398. rval = v4l2_ctrl_query_fill(qc, 0, MAX_LIMITER_DEVIATION,
  1399. 10, DEFAULT_LIMITER_DEV);
  1400. break;
  1401. case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
  1402. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1403. break;
  1404. case V4L2_CID_AUDIO_COMPRESSION_GAIN:
  1405. rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
  1406. DEFAULT_ACOMP_GAIN);
  1407. break;
  1408. case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
  1409. rval = v4l2_ctrl_query_fill(qc, MIN_ACOMP_THRESHOLD,
  1410. MAX_ACOMP_THRESHOLD, 1,
  1411. DEFAULT_ACOMP_THRESHOLD);
  1412. break;
  1413. case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
  1414. rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_ATTACK_TIME,
  1415. 500, DEFAULT_ACOMP_ATIME);
  1416. break;
  1417. case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
  1418. rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
  1419. 100000, DEFAULT_ACOMP_RTIME);
  1420. break;
  1421. case V4L2_CID_PILOT_TONE_ENABLED:
  1422. rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1423. break;
  1424. case V4L2_CID_PILOT_TONE_DEVIATION:
  1425. rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_DEVIATION,
  1426. 10, DEFAULT_PILOT_DEVIATION);
  1427. break;
  1428. case V4L2_CID_PILOT_TONE_FREQUENCY:
  1429. rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_FREQUENCY,
  1430. 1, DEFAULT_PILOT_FREQUENCY);
  1431. break;
  1432. case V4L2_CID_TUNE_PREEMPHASIS:
  1433. rval = v4l2_ctrl_query_fill(qc, V4L2_PREEMPHASIS_DISABLED,
  1434. V4L2_PREEMPHASIS_75_uS, 1,
  1435. V4L2_PREEMPHASIS_50_uS);
  1436. break;
  1437. case V4L2_CID_TUNE_POWER_LEVEL:
  1438. rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
  1439. break;
  1440. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1441. rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
  1442. break;
  1443. default:
  1444. rval = -EINVAL;
  1445. break;
  1446. };
  1447. return rval;
  1448. }
  1449. /* si4713_g_ctrl - get the value of a control */
  1450. static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1451. {
  1452. struct si4713_device *sdev = to_si4713_device(sd);
  1453. int rval = 0;
  1454. if (!sdev)
  1455. return -ENODEV;
  1456. mutex_lock(&sdev->mutex);
  1457. if (sdev->power_state) {
  1458. rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
  1459. &sdev->mute);
  1460. if (rval < 0)
  1461. goto unlock;
  1462. }
  1463. switch (ctrl->id) {
  1464. case V4L2_CID_AUDIO_MUTE:
  1465. ctrl->value = get_mute(sdev->mute);
  1466. break;
  1467. }
  1468. unlock:
  1469. mutex_unlock(&sdev->mutex);
  1470. return rval;
  1471. }
  1472. /* si4713_s_ctrl - set the value of a control */
  1473. static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1474. {
  1475. struct si4713_device *sdev = to_si4713_device(sd);
  1476. int rval = 0;
  1477. if (!sdev)
  1478. return -ENODEV;
  1479. switch (ctrl->id) {
  1480. case V4L2_CID_AUDIO_MUTE:
  1481. if (ctrl->value) {
  1482. rval = si4713_set_mute(sdev, ctrl->value);
  1483. if (rval < 0)
  1484. goto exit;
  1485. rval = si4713_set_power_state(sdev, POWER_DOWN);
  1486. } else {
  1487. rval = si4713_set_power_state(sdev, POWER_UP);
  1488. if (rval < 0)
  1489. goto exit;
  1490. rval = si4713_setup(sdev);
  1491. if (rval < 0)
  1492. goto exit;
  1493. rval = si4713_set_mute(sdev, ctrl->value);
  1494. }
  1495. break;
  1496. }
  1497. exit:
  1498. return rval;
  1499. }
  1500. /* si4713_ioctl - deal with private ioctls (only rnl for now) */
  1501. long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  1502. {
  1503. struct si4713_device *sdev = to_si4713_device(sd);
  1504. struct si4713_rnl *rnl = arg;
  1505. u16 frequency;
  1506. int rval = 0;
  1507. if (!arg)
  1508. return -EINVAL;
  1509. mutex_lock(&sdev->mutex);
  1510. switch (cmd) {
  1511. case SI4713_IOC_MEASURE_RNL:
  1512. frequency = v4l2_to_si4713(rnl->frequency);
  1513. if (sdev->power_state) {
  1514. /* Set desired measurement frequency */
  1515. rval = si4713_tx_tune_measure(sdev, frequency, 0);
  1516. if (rval < 0)
  1517. goto unlock;
  1518. /* get results from tune status */
  1519. rval = si4713_update_tune_status(sdev);
  1520. if (rval < 0)
  1521. goto unlock;
  1522. }
  1523. rnl->rnl = sdev->tune_rnl;
  1524. break;
  1525. default:
  1526. /* nothing */
  1527. rval = -ENOIOCTLCMD;
  1528. }
  1529. unlock:
  1530. mutex_unlock(&sdev->mutex);
  1531. return rval;
  1532. }
  1533. static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
  1534. .queryctrl = si4713_queryctrl,
  1535. .g_ext_ctrls = si4713_g_ext_ctrls,
  1536. .s_ext_ctrls = si4713_s_ext_ctrls,
  1537. .g_ctrl = si4713_g_ctrl,
  1538. .s_ctrl = si4713_s_ctrl,
  1539. .ioctl = si4713_ioctl,
  1540. };
  1541. /* si4713_g_modulator - get modulator attributes */
  1542. static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
  1543. {
  1544. struct si4713_device *sdev = to_si4713_device(sd);
  1545. int rval = 0;
  1546. if (!sdev) {
  1547. rval = -ENODEV;
  1548. goto exit;
  1549. }
  1550. if (vm->index > 0) {
  1551. rval = -EINVAL;
  1552. goto exit;
  1553. }
  1554. strncpy(vm->name, "FM Modulator", 32);
  1555. vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
  1556. V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
  1557. /* Report current frequency range limits */
  1558. vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
  1559. vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
  1560. mutex_lock(&sdev->mutex);
  1561. if (sdev->power_state) {
  1562. u32 comp_en = 0;
  1563. rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
  1564. &comp_en);
  1565. if (rval < 0)
  1566. goto unlock;
  1567. sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
  1568. sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
  1569. }
  1570. /* Report current audio mode: mono or stereo */
  1571. if (sdev->stereo)
  1572. vm->txsubchans = V4L2_TUNER_SUB_STEREO;
  1573. else
  1574. vm->txsubchans = V4L2_TUNER_SUB_MONO;
  1575. /* Report rds feature status */
  1576. if (sdev->rds_info.enabled)
  1577. vm->txsubchans |= V4L2_TUNER_SUB_RDS;
  1578. else
  1579. vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
  1580. unlock:
  1581. mutex_unlock(&sdev->mutex);
  1582. exit:
  1583. return rval;
  1584. }
  1585. /* si4713_s_modulator - set modulator attributes */
  1586. static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
  1587. {
  1588. struct si4713_device *sdev = to_si4713_device(sd);
  1589. int rval = 0;
  1590. u16 stereo, rds;
  1591. u32 p;
  1592. if (!sdev)
  1593. return -ENODEV;
  1594. if (vm->index > 0)
  1595. return -EINVAL;
  1596. /* Set audio mode: mono or stereo */
  1597. if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
  1598. stereo = 1;
  1599. else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
  1600. stereo = 0;
  1601. else
  1602. return -EINVAL;
  1603. rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
  1604. mutex_lock(&sdev->mutex);
  1605. if (sdev->power_state) {
  1606. rval = si4713_read_property(sdev,
  1607. SI4713_TX_COMPONENT_ENABLE, &p);
  1608. if (rval < 0)
  1609. goto unlock;
  1610. p = set_bits(p, stereo, 1, 1 << 1);
  1611. p = set_bits(p, rds, 2, 1 << 2);
  1612. rval = si4713_write_property(sdev,
  1613. SI4713_TX_COMPONENT_ENABLE, p);
  1614. if (rval < 0)
  1615. goto unlock;
  1616. }
  1617. sdev->stereo = stereo;
  1618. sdev->rds_info.enabled = rds;
  1619. unlock:
  1620. mutex_unlock(&sdev->mutex);
  1621. return rval;
  1622. }
  1623. /* si4713_g_frequency - get tuner or modulator radio frequency */
  1624. static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  1625. {
  1626. struct si4713_device *sdev = to_si4713_device(sd);
  1627. int rval = 0;
  1628. f->type = V4L2_TUNER_RADIO;
  1629. mutex_lock(&sdev->mutex);
  1630. if (sdev->power_state) {
  1631. u16 freq;
  1632. u8 p, a, n;
  1633. rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
  1634. if (rval < 0)
  1635. goto unlock;
  1636. sdev->frequency = freq;
  1637. }
  1638. f->frequency = si4713_to_v4l2(sdev->frequency);
  1639. unlock:
  1640. mutex_unlock(&sdev->mutex);
  1641. return rval;
  1642. }
  1643. /* si4713_s_frequency - set tuner or modulator radio frequency */
  1644. static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  1645. {
  1646. struct si4713_device *sdev = to_si4713_device(sd);
  1647. int rval = 0;
  1648. u16 frequency = v4l2_to_si4713(f->frequency);
  1649. /* Check frequency range */
  1650. if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
  1651. return -EDOM;
  1652. mutex_lock(&sdev->mutex);
  1653. if (sdev->power_state) {
  1654. rval = si4713_tx_tune_freq(sdev, frequency);
  1655. if (rval < 0)
  1656. goto unlock;
  1657. frequency = rval;
  1658. rval = 0;
  1659. }
  1660. sdev->frequency = frequency;
  1661. f->frequency = si4713_to_v4l2(frequency);
  1662. unlock:
  1663. mutex_unlock(&sdev->mutex);
  1664. return rval;
  1665. }
  1666. static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
  1667. .g_frequency = si4713_g_frequency,
  1668. .s_frequency = si4713_s_frequency,
  1669. .g_modulator = si4713_g_modulator,
  1670. .s_modulator = si4713_s_modulator,
  1671. };
  1672. static const struct v4l2_subdev_ops si4713_subdev_ops = {
  1673. .core = &si4713_subdev_core_ops,
  1674. .tuner = &si4713_subdev_tuner_ops,
  1675. };
  1676. /*
  1677. * I2C driver interface
  1678. */
  1679. /* si4713_probe - probe for the device */
  1680. static int si4713_probe(struct i2c_client *client,
  1681. const struct i2c_device_id *id)
  1682. {
  1683. struct si4713_device *sdev;
  1684. struct si4713_platform_data *pdata = client->dev.platform_data;
  1685. int rval, i;
  1686. sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
  1687. if (!sdev) {
  1688. dev_err(&client->dev, "Failed to alloc video device.\n");
  1689. rval = -ENOMEM;
  1690. goto exit;
  1691. }
  1692. sdev->gpio_reset = -1;
  1693. if (pdata && gpio_is_valid(pdata->gpio_reset)) {
  1694. rval = gpio_request(pdata->gpio_reset, "si4713 reset");
  1695. if (rval) {
  1696. dev_err(&client->dev,
  1697. "Failed to request gpio: %d\n", rval);
  1698. goto free_sdev;
  1699. }
  1700. sdev->gpio_reset = pdata->gpio_reset;
  1701. gpio_direction_output(sdev->gpio_reset, 0);
  1702. }
  1703. for (i = 0; i < ARRAY_SIZE(sdev->supplies); i++)
  1704. sdev->supplies[i].supply = si4713_supply_names[i];
  1705. rval = regulator_bulk_get(&client->dev, ARRAY_SIZE(sdev->supplies),
  1706. sdev->supplies);
  1707. if (rval) {
  1708. dev_err(&client->dev, "Cannot get regulators: %d\n", rval);
  1709. goto free_gpio;
  1710. }
  1711. v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
  1712. mutex_init(&sdev->mutex);
  1713. init_completion(&sdev->work);
  1714. if (client->irq) {
  1715. rval = request_irq(client->irq,
  1716. si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
  1717. client->name, sdev);
  1718. if (rval < 0) {
  1719. v4l2_err(&sdev->sd, "Could not request IRQ\n");
  1720. goto put_reg;
  1721. }
  1722. v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
  1723. } else {
  1724. v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
  1725. }
  1726. rval = si4713_initialize(sdev);
  1727. if (rval < 0) {
  1728. v4l2_err(&sdev->sd, "Failed to probe device information.\n");
  1729. goto free_irq;
  1730. }
  1731. return 0;
  1732. free_irq:
  1733. if (client->irq)
  1734. free_irq(client->irq, sdev);
  1735. put_reg:
  1736. regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
  1737. free_gpio:
  1738. if (gpio_is_valid(sdev->gpio_reset))
  1739. gpio_free(sdev->gpio_reset);
  1740. free_sdev:
  1741. kfree(sdev);
  1742. exit:
  1743. return rval;
  1744. }
  1745. /* si4713_remove - remove the device */
  1746. static int si4713_remove(struct i2c_client *client)
  1747. {
  1748. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1749. struct si4713_device *sdev = to_si4713_device(sd);
  1750. if (sdev->power_state)
  1751. si4713_set_power_state(sdev, POWER_DOWN);
  1752. if (client->irq > 0)
  1753. free_irq(client->irq, sdev);
  1754. v4l2_device_unregister_subdev(sd);
  1755. regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
  1756. if (gpio_is_valid(sdev->gpio_reset))
  1757. gpio_free(sdev->gpio_reset);
  1758. kfree(sdev);
  1759. return 0;
  1760. }
  1761. /* si4713_i2c_driver - i2c driver interface */
  1762. static const struct i2c_device_id si4713_id[] = {
  1763. { "si4713" , 0 },
  1764. { },
  1765. };
  1766. MODULE_DEVICE_TABLE(i2c, si4713_id);
  1767. static struct i2c_driver si4713_i2c_driver = {
  1768. .driver = {
  1769. .name = "si4713",
  1770. },
  1771. .probe = si4713_probe,
  1772. .remove = si4713_remove,
  1773. .id_table = si4713_id,
  1774. };
  1775. module_i2c_driver(si4713_i2c_driver);