saa6588.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Driver for SAA6588 RDS decoder
  4. (c) 2005 Hans J. Koch
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/i2c.h>
  9. #include <linux/types.h>
  10. #include <linux/videodev2.h>
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include <linux/poll.h>
  15. #include <linux/wait.h>
  16. #include <linux/uaccess.h>
  17. #include <media/i2c/saa6588.h>
  18. #include <media/v4l2-device.h>
  19. /* insmod options */
  20. static unsigned int debug;
  21. static unsigned int xtal;
  22. static unsigned int mmbs;
  23. static unsigned int plvl;
  24. static unsigned int bufblocks = 100;
  25. module_param(debug, int, 0644);
  26. MODULE_PARM_DESC(debug, "enable debug messages");
  27. module_param(xtal, int, 0);
  28. MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
  29. module_param(mmbs, int, 0);
  30. MODULE_PARM_DESC(mmbs, "enable MMBS mode: 0=off (default), 1=on");
  31. module_param(plvl, int, 0);
  32. MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
  33. module_param(bufblocks, int, 0);
  34. MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
  35. MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
  36. MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
  37. MODULE_LICENSE("GPL");
  38. /* ---------------------------------------------------------------------- */
  39. #define UNSET (-1U)
  40. struct saa6588 {
  41. struct v4l2_subdev sd;
  42. struct delayed_work work;
  43. spinlock_t lock;
  44. unsigned char *buffer;
  45. unsigned int buf_size;
  46. unsigned int rd_index;
  47. unsigned int wr_index;
  48. unsigned int block_count;
  49. unsigned char last_blocknum;
  50. wait_queue_head_t read_queue;
  51. int data_available_for_read;
  52. u8 sync;
  53. };
  54. static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd)
  55. {
  56. return container_of(sd, struct saa6588, sd);
  57. }
  58. /* ---------------------------------------------------------------------- */
  59. /*
  60. * SAA6588 defines
  61. */
  62. /* Initialization and mode control byte (0w) */
  63. /* bit 0+1 (DAC0/DAC1) */
  64. #define cModeStandard 0x00
  65. #define cModeFastPI 0x01
  66. #define cModeReducedRequest 0x02
  67. #define cModeInvalid 0x03
  68. /* bit 2 (RBDS) */
  69. #define cProcessingModeRDS 0x00
  70. #define cProcessingModeRBDS 0x04
  71. /* bit 3+4 (SYM0/SYM1) */
  72. #define cErrCorrectionNone 0x00
  73. #define cErrCorrection2Bits 0x08
  74. #define cErrCorrection5Bits 0x10
  75. #define cErrCorrectionNoneRBDS 0x18
  76. /* bit 5 (NWSY) */
  77. #define cSyncNormal 0x00
  78. #define cSyncRestart 0x20
  79. /* bit 6 (TSQD) */
  80. #define cSigQualityDetectOFF 0x00
  81. #define cSigQualityDetectON 0x40
  82. /* bit 7 (SQCM) */
  83. #define cSigQualityTriggered 0x00
  84. #define cSigQualityContinous 0x80
  85. /* Pause level and flywheel control byte (1w) */
  86. /* bits 0..5 (FEB0..FEB5) */
  87. #define cFlywheelMaxBlocksMask 0x3F
  88. #define cFlywheelDefault 0x20
  89. /* bits 6+7 (PL0/PL1) */
  90. #define cPauseLevel_11mV 0x00
  91. #define cPauseLevel_17mV 0x40
  92. #define cPauseLevel_27mV 0x80
  93. #define cPauseLevel_43mV 0xC0
  94. /* Pause time/oscillator frequency/quality detector control byte (1w) */
  95. /* bits 0..4 (SQS0..SQS4) */
  96. #define cQualityDetectSensMask 0x1F
  97. #define cQualityDetectDefault 0x0F
  98. /* bit 5 (SOSC) */
  99. #define cSelectOscFreqOFF 0x00
  100. #define cSelectOscFreqON 0x20
  101. /* bit 6+7 (PTF0/PTF1) */
  102. #define cOscFreq_4332kHz 0x00
  103. #define cOscFreq_8664kHz 0x40
  104. #define cOscFreq_12996kHz 0x80
  105. #define cOscFreq_17328kHz 0xC0
  106. /* ---------------------------------------------------------------------- */
  107. static bool block_from_buf(struct saa6588 *s, unsigned char *buf)
  108. {
  109. int i;
  110. if (s->rd_index == s->wr_index) {
  111. if (debug > 2)
  112. v4l2_info(&s->sd, "Read: buffer empty.\n");
  113. return false;
  114. }
  115. if (debug > 2) {
  116. v4l2_info(&s->sd, "Read: ");
  117. for (i = s->rd_index; i < s->rd_index + 3; i++)
  118. v4l2_info(&s->sd, "0x%02x ", s->buffer[i]);
  119. }
  120. memcpy(buf, &s->buffer[s->rd_index], 3);
  121. s->rd_index += 3;
  122. if (s->rd_index >= s->buf_size)
  123. s->rd_index = 0;
  124. s->block_count--;
  125. if (debug > 2)
  126. v4l2_info(&s->sd, "%d blocks total.\n", s->block_count);
  127. return true;
  128. }
  129. static void read_from_buf(struct saa6588 *s, struct saa6588_command *a)
  130. {
  131. unsigned char __user *buf_ptr = a->buffer;
  132. unsigned char buf[3];
  133. unsigned long flags;
  134. unsigned int rd_blocks;
  135. unsigned int i;
  136. a->result = 0;
  137. if (!a->buffer)
  138. return;
  139. while (!a->nonblocking && !s->data_available_for_read) {
  140. int ret = wait_event_interruptible(s->read_queue,
  141. s->data_available_for_read);
  142. if (ret == -ERESTARTSYS) {
  143. a->result = -EINTR;
  144. return;
  145. }
  146. }
  147. rd_blocks = a->block_count;
  148. spin_lock_irqsave(&s->lock, flags);
  149. if (rd_blocks > s->block_count)
  150. rd_blocks = s->block_count;
  151. spin_unlock_irqrestore(&s->lock, flags);
  152. if (!rd_blocks)
  153. return;
  154. for (i = 0; i < rd_blocks; i++) {
  155. bool got_block;
  156. spin_lock_irqsave(&s->lock, flags);
  157. got_block = block_from_buf(s, buf);
  158. spin_unlock_irqrestore(&s->lock, flags);
  159. if (!got_block)
  160. break;
  161. if (copy_to_user(buf_ptr, buf, 3)) {
  162. a->result = -EFAULT;
  163. return;
  164. }
  165. buf_ptr += 3;
  166. a->result += 3;
  167. }
  168. spin_lock_irqsave(&s->lock, flags);
  169. s->data_available_for_read = (s->block_count > 0);
  170. spin_unlock_irqrestore(&s->lock, flags);
  171. }
  172. static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
  173. {
  174. unsigned int i;
  175. if (debug > 3)
  176. v4l2_info(&s->sd, "New block: ");
  177. for (i = 0; i < 3; ++i) {
  178. if (debug > 3)
  179. v4l2_info(&s->sd, "0x%02x ", blockbuf[i]);
  180. s->buffer[s->wr_index] = blockbuf[i];
  181. s->wr_index++;
  182. }
  183. if (s->wr_index >= s->buf_size)
  184. s->wr_index = 0;
  185. if (s->wr_index == s->rd_index) {
  186. s->rd_index += 3;
  187. if (s->rd_index >= s->buf_size)
  188. s->rd_index = 0;
  189. } else
  190. s->block_count++;
  191. if (debug > 3)
  192. v4l2_info(&s->sd, "%d blocks total.\n", s->block_count);
  193. }
  194. static void saa6588_i2c_poll(struct saa6588 *s)
  195. {
  196. struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
  197. unsigned long flags;
  198. unsigned char tmpbuf[6];
  199. unsigned char blocknum;
  200. unsigned char tmp;
  201. /* Although we only need 3 bytes, we have to read at least 6.
  202. SAA6588 returns garbage otherwise. */
  203. if (6 != i2c_master_recv(client, &tmpbuf[0], 6)) {
  204. if (debug > 1)
  205. v4l2_info(&s->sd, "read error!\n");
  206. return;
  207. }
  208. s->sync = tmpbuf[0] & 0x10;
  209. if (!s->sync)
  210. return;
  211. blocknum = tmpbuf[0] >> 5;
  212. if (blocknum == s->last_blocknum) {
  213. if (debug > 3)
  214. v4l2_info(&s->sd, "Saw block %d again.\n", blocknum);
  215. return;
  216. }
  217. s->last_blocknum = blocknum;
  218. /*
  219. Byte order according to v4l2 specification:
  220. Byte 0: Least Significant Byte of RDS Block
  221. Byte 1: Most Significant Byte of RDS Block
  222. Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
  223. occurred during reception of this block.
  224. Bit 6: Corrected bit. Indicates that an error was
  225. corrected for this data block.
  226. Bits 5-3: Same as bits 0-2.
  227. Bits 2-0: Block number.
  228. SAA6588 byte order is Status-MSB-LSB, so we have to swap the
  229. first and the last of the 3 bytes block.
  230. */
  231. swap(tmpbuf[2], tmpbuf[0]);
  232. /* Map 'Invalid block E' to 'Invalid Block' */
  233. if (blocknum == 6)
  234. blocknum = V4L2_RDS_BLOCK_INVALID;
  235. /* And if are not in mmbs mode, then 'Block E' is also mapped
  236. to 'Invalid Block'. As far as I can tell MMBS is discontinued,
  237. and if there is ever a need to support E blocks, then please
  238. contact the linux-media mailinglist. */
  239. else if (!mmbs && blocknum == 5)
  240. blocknum = V4L2_RDS_BLOCK_INVALID;
  241. tmp = blocknum;
  242. tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */
  243. if ((tmpbuf[2] & 0x03) == 0x03)
  244. tmp |= V4L2_RDS_BLOCK_ERROR; /* uncorrectable error */
  245. else if ((tmpbuf[2] & 0x03) != 0x00)
  246. tmp |= V4L2_RDS_BLOCK_CORRECTED; /* corrected error */
  247. tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */
  248. spin_lock_irqsave(&s->lock, flags);
  249. block_to_buf(s, tmpbuf);
  250. spin_unlock_irqrestore(&s->lock, flags);
  251. s->data_available_for_read = 1;
  252. wake_up_interruptible(&s->read_queue);
  253. }
  254. static void saa6588_work(struct work_struct *work)
  255. {
  256. struct saa6588 *s = container_of(work, struct saa6588, work.work);
  257. saa6588_i2c_poll(s);
  258. schedule_delayed_work(&s->work, msecs_to_jiffies(20));
  259. }
  260. static void saa6588_configure(struct saa6588 *s)
  261. {
  262. struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
  263. unsigned char buf[3];
  264. int rc;
  265. buf[0] = cSyncRestart;
  266. if (mmbs)
  267. buf[0] |= cProcessingModeRBDS;
  268. buf[1] = cFlywheelDefault;
  269. switch (plvl) {
  270. case 0:
  271. buf[1] |= cPauseLevel_11mV;
  272. break;
  273. case 1:
  274. buf[1] |= cPauseLevel_17mV;
  275. break;
  276. case 2:
  277. buf[1] |= cPauseLevel_27mV;
  278. break;
  279. case 3:
  280. buf[1] |= cPauseLevel_43mV;
  281. break;
  282. default: /* nothing */
  283. break;
  284. }
  285. buf[2] = cQualityDetectDefault | cSelectOscFreqON;
  286. switch (xtal) {
  287. case 0:
  288. buf[2] |= cOscFreq_4332kHz;
  289. break;
  290. case 1:
  291. buf[2] |= cOscFreq_8664kHz;
  292. break;
  293. case 2:
  294. buf[2] |= cOscFreq_12996kHz;
  295. break;
  296. case 3:
  297. buf[2] |= cOscFreq_17328kHz;
  298. break;
  299. default: /* nothing */
  300. break;
  301. }
  302. if (debug)
  303. v4l2_info(&s->sd, "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
  304. buf[0], buf[1], buf[2]);
  305. rc = i2c_master_send(client, buf, 3);
  306. if (rc != 3)
  307. v4l2_info(&s->sd, "i2c i/o error: rc == %d (should be 3)\n", rc);
  308. }
  309. /* ---------------------------------------------------------------------- */
  310. static long saa6588_command(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  311. {
  312. struct saa6588 *s = to_saa6588(sd);
  313. struct saa6588_command *a = arg;
  314. switch (cmd) {
  315. /* --- close() for /dev/radio --- */
  316. case SAA6588_CMD_CLOSE:
  317. s->data_available_for_read = 1;
  318. wake_up_interruptible(&s->read_queue);
  319. s->data_available_for_read = 0;
  320. a->result = 0;
  321. break;
  322. /* --- read() for /dev/radio --- */
  323. case SAA6588_CMD_READ:
  324. read_from_buf(s, a);
  325. break;
  326. /* --- poll() for /dev/radio --- */
  327. case SAA6588_CMD_POLL:
  328. a->poll_mask = 0;
  329. if (s->data_available_for_read)
  330. a->poll_mask |= EPOLLIN | EPOLLRDNORM;
  331. poll_wait(a->instance, &s->read_queue, a->event_list);
  332. break;
  333. default:
  334. /* nothing */
  335. return -ENOIOCTLCMD;
  336. }
  337. return 0;
  338. }
  339. static int saa6588_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  340. {
  341. struct saa6588 *s = to_saa6588(sd);
  342. vt->capability |= V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO;
  343. if (s->sync)
  344. vt->rxsubchans |= V4L2_TUNER_SUB_RDS;
  345. return 0;
  346. }
  347. static int saa6588_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
  348. {
  349. struct saa6588 *s = to_saa6588(sd);
  350. saa6588_configure(s);
  351. return 0;
  352. }
  353. /* ----------------------------------------------------------------------- */
  354. static const struct v4l2_subdev_core_ops saa6588_core_ops = {
  355. .command = saa6588_command,
  356. };
  357. static const struct v4l2_subdev_tuner_ops saa6588_tuner_ops = {
  358. .g_tuner = saa6588_g_tuner,
  359. .s_tuner = saa6588_s_tuner,
  360. };
  361. static const struct v4l2_subdev_ops saa6588_ops = {
  362. .core = &saa6588_core_ops,
  363. .tuner = &saa6588_tuner_ops,
  364. };
  365. /* ---------------------------------------------------------------------- */
  366. static int saa6588_probe(struct i2c_client *client)
  367. {
  368. struct saa6588 *s;
  369. struct v4l2_subdev *sd;
  370. v4l_info(client, "saa6588 found @ 0x%x (%s)\n",
  371. client->addr << 1, client->adapter->name);
  372. s = devm_kzalloc(&client->dev, sizeof(*s), GFP_KERNEL);
  373. if (s == NULL)
  374. return -ENOMEM;
  375. s->buf_size = bufblocks * 3;
  376. s->buffer = devm_kzalloc(&client->dev, s->buf_size, GFP_KERNEL);
  377. if (s->buffer == NULL)
  378. return -ENOMEM;
  379. sd = &s->sd;
  380. v4l2_i2c_subdev_init(sd, client, &saa6588_ops);
  381. spin_lock_init(&s->lock);
  382. s->block_count = 0;
  383. s->wr_index = 0;
  384. s->rd_index = 0;
  385. s->last_blocknum = 0xff;
  386. init_waitqueue_head(&s->read_queue);
  387. s->data_available_for_read = 0;
  388. saa6588_configure(s);
  389. /* start polling via eventd */
  390. INIT_DELAYED_WORK(&s->work, saa6588_work);
  391. schedule_delayed_work(&s->work, 0);
  392. return 0;
  393. }
  394. static void saa6588_remove(struct i2c_client *client)
  395. {
  396. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  397. struct saa6588 *s = to_saa6588(sd);
  398. v4l2_device_unregister_subdev(sd);
  399. cancel_delayed_work_sync(&s->work);
  400. }
  401. /* ----------------------------------------------------------------------- */
  402. static const struct i2c_device_id saa6588_id[] = {
  403. { "saa6588" },
  404. { }
  405. };
  406. MODULE_DEVICE_TABLE(i2c, saa6588_id);
  407. static struct i2c_driver saa6588_driver = {
  408. .driver = {
  409. .name = "saa6588",
  410. },
  411. .probe = saa6588_probe,
  412. .remove = saa6588_remove,
  413. .id_table = saa6588_id,
  414. };
  415. module_i2c_driver(saa6588_driver);