saa6588.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_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 0;
  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. if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
  133. return -EFAULT;
  134. s->rd_index += 3;
  135. if (s->rd_index >= s->buf_size)
  136. s->rd_index = 0;
  137. s->block_count--;
  138. if (debug > 2)
  139. dprintk("%d blocks total.\n", s->block_count);
  140. return 1;
  141. }
  142. static void read_from_buf(struct saa6588 *s, struct saa6588_command *a)
  143. {
  144. unsigned long flags;
  145. unsigned char __user *buf_ptr = a->buffer;
  146. unsigned int i;
  147. unsigned int rd_blocks;
  148. a->result = 0;
  149. if (!a->buffer)
  150. return;
  151. while (!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. spin_lock_irqsave(&s->lock, flags);
  160. rd_blocks = a->block_count;
  161. if (rd_blocks > s->block_count)
  162. rd_blocks = s->block_count;
  163. if (!rd_blocks) {
  164. spin_unlock_irqrestore(&s->lock, flags);
  165. return;
  166. }
  167. for (i = 0; i < rd_blocks; i++) {
  168. if (block_to_user_buf(s, buf_ptr)) {
  169. buf_ptr += 3;
  170. a->result++;
  171. } else
  172. break;
  173. }
  174. a->result *= 3;
  175. s->data_available_for_read = (s->block_count > 0);
  176. spin_unlock_irqrestore(&s->lock, flags);
  177. }
  178. static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
  179. {
  180. unsigned int i;
  181. if (debug > 3)
  182. dprintk(PREFIX "New block: ");
  183. for (i = 0; i < 3; ++i) {
  184. if (debug > 3)
  185. dprintk("0x%02x ", blockbuf[i]);
  186. s->buffer[s->wr_index] = blockbuf[i];
  187. s->wr_index++;
  188. }
  189. if (s->wr_index >= s->buf_size)
  190. s->wr_index = 0;
  191. if (s->wr_index == s->rd_index) {
  192. s->rd_index += 3;
  193. if (s->rd_index >= s->buf_size)
  194. s->rd_index = 0;
  195. } else
  196. s->block_count++;
  197. if (debug > 3)
  198. dprintk("%d blocks total.\n", s->block_count);
  199. }
  200. static void saa6588_i2c_poll(struct saa6588 *s)
  201. {
  202. struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
  203. unsigned long flags;
  204. unsigned char tmpbuf[6];
  205. unsigned char blocknum;
  206. unsigned char tmp;
  207. /* Although we only need 3 bytes, we have to read at least 6.
  208. SAA6588 returns garbage otherwise. */
  209. if (6 != i2c_master_recv(client, &tmpbuf[0], 6)) {
  210. if (debug > 1)
  211. dprintk(PREFIX "read error!\n");
  212. return;
  213. }
  214. s->sync = tmpbuf[0] & 0x10;
  215. if (!s->sync)
  216. return;
  217. blocknum = tmpbuf[0] >> 5;
  218. if (blocknum == s->last_blocknum) {
  219. if (debug > 3)
  220. dprintk("Saw block %d again.\n", blocknum);
  221. return;
  222. }
  223. s->last_blocknum = blocknum;
  224. /*
  225. Byte order according to v4l2 specification:
  226. Byte 0: Least Significant Byte of RDS Block
  227. Byte 1: Most Significant Byte of RDS Block
  228. Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
  229. occurred during reception of this block.
  230. Bit 6: Corrected bit. Indicates that an error was
  231. corrected for this data block.
  232. Bits 5-3: Same as bits 0-2.
  233. Bits 2-0: Block number.
  234. SAA6588 byte order is Status-MSB-LSB, so we have to swap the
  235. first and the last of the 3 bytes block.
  236. */
  237. tmp = tmpbuf[2];
  238. tmpbuf[2] = tmpbuf[0];
  239. tmpbuf[0] = tmp;
  240. /* Map 'Invalid block E' to 'Invalid Block' */
  241. if (blocknum == 6)
  242. blocknum = V4L2_RDS_BLOCK_INVALID;
  243. /* And if are not in mmbs mode, then 'Block E' is also mapped
  244. to 'Invalid Block'. As far as I can tell MMBS is discontinued,
  245. and if there is ever a need to support E blocks, then please
  246. contact the linux-media mailinglist. */
  247. else if (!mmbs && blocknum == 5)
  248. blocknum = V4L2_RDS_BLOCK_INVALID;
  249. tmp = blocknum;
  250. tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */
  251. if ((tmpbuf[2] & 0x03) == 0x03)
  252. tmp |= V4L2_RDS_BLOCK_ERROR; /* uncorrectable error */
  253. else if ((tmpbuf[2] & 0x03) != 0x00)
  254. tmp |= V4L2_RDS_BLOCK_CORRECTED; /* corrected error */
  255. tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */
  256. spin_lock_irqsave(&s->lock, flags);
  257. block_to_buf(s, tmpbuf);
  258. spin_unlock_irqrestore(&s->lock, flags);
  259. s->data_available_for_read = 1;
  260. wake_up_interruptible(&s->read_queue);
  261. }
  262. static void saa6588_work(struct work_struct *work)
  263. {
  264. struct saa6588 *s = container_of(work, struct saa6588, work.work);
  265. saa6588_i2c_poll(s);
  266. schedule_delayed_work(&s->work, msecs_to_jiffies(20));
  267. }
  268. static void saa6588_configure(struct saa6588 *s)
  269. {
  270. struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
  271. unsigned char buf[3];
  272. int rc;
  273. buf[0] = cSyncRestart;
  274. if (mmbs)
  275. buf[0] |= cProcessingModeRBDS;
  276. buf[1] = cFlywheelDefault;
  277. switch (plvl) {
  278. case 0:
  279. buf[1] |= cPauseLevel_11mV;
  280. break;
  281. case 1:
  282. buf[1] |= cPauseLevel_17mV;
  283. break;
  284. case 2:
  285. buf[1] |= cPauseLevel_27mV;
  286. break;
  287. case 3:
  288. buf[1] |= cPauseLevel_43mV;
  289. break;
  290. default: /* nothing */
  291. break;
  292. }
  293. buf[2] = cQualityDetectDefault | cSelectOscFreqON;
  294. switch (xtal) {
  295. case 0:
  296. buf[2] |= cOscFreq_4332kHz;
  297. break;
  298. case 1:
  299. buf[2] |= cOscFreq_8664kHz;
  300. break;
  301. case 2:
  302. buf[2] |= cOscFreq_12996kHz;
  303. break;
  304. case 3:
  305. buf[2] |= cOscFreq_17328kHz;
  306. break;
  307. default: /* nothing */
  308. break;
  309. }
  310. dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
  311. buf[0], buf[1], buf[2]);
  312. rc = i2c_master_send(client, buf, 3);
  313. if (rc != 3)
  314. printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
  315. }
  316. /* ---------------------------------------------------------------------- */
  317. static long saa6588_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  318. {
  319. struct saa6588 *s = to_saa6588(sd);
  320. struct saa6588_command *a = arg;
  321. switch (cmd) {
  322. /* --- open() for /dev/radio --- */
  323. case SAA6588_CMD_OPEN:
  324. a->result = 0; /* return error if chip doesn't work ??? */
  325. break;
  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. a->result = 0;
  331. break;
  332. /* --- read() for /dev/radio --- */
  333. case SAA6588_CMD_READ:
  334. read_from_buf(s, a);
  335. break;
  336. /* --- poll() for /dev/radio --- */
  337. case SAA6588_CMD_POLL:
  338. a->result = 0;
  339. if (s->data_available_for_read) {
  340. a->result |= POLLIN | POLLRDNORM;
  341. }
  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. .owner = THIS_MODULE,
  423. .name = "saa6588",
  424. },
  425. .probe = saa6588_probe,
  426. .remove = saa6588_remove,
  427. .id_table = saa6588_id,
  428. };
  429. module_i2c_driver(saa6588_driver);