saa6588.c 13 KB

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