saa6588.c 13 KB

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