saa6588.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/videodev.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. /* Addresses to scan */
  29. static unsigned short normal_i2c[] = {
  30. 0x20 >> 1,
  31. 0x22 >> 1,
  32. I2C_CLIENT_END,
  33. };
  34. I2C_CLIENT_INSMOD;
  35. /* insmod options */
  36. static unsigned int debug = 0;
  37. static unsigned int xtal = 0;
  38. static unsigned int rbds = 0;
  39. static unsigned int plvl = 0;
  40. static unsigned int bufblocks = 100;
  41. module_param(debug, int, 0644);
  42. MODULE_PARM_DESC(debug, "enable debug messages");
  43. module_param(xtal, int, 0);
  44. MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
  45. module_param(rbds, int, 0);
  46. MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0");
  47. module_param(plvl, int, 0);
  48. MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
  49. module_param(bufblocks, int, 0);
  50. MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
  51. MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
  52. MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
  53. MODULE_LICENSE("GPL");
  54. /* ---------------------------------------------------------------------- */
  55. #define UNSET (-1U)
  56. #define PREFIX "saa6588: "
  57. #define dprintk if (debug) printk
  58. struct saa6588 {
  59. struct i2c_client client;
  60. struct work_struct work;
  61. struct timer_list timer;
  62. spinlock_t lock;
  63. unsigned char *buffer;
  64. unsigned int buf_size;
  65. unsigned int rd_index;
  66. unsigned int wr_index;
  67. unsigned int block_count;
  68. unsigned char last_blocknum;
  69. wait_queue_head_t read_queue;
  70. int data_available_for_read;
  71. };
  72. static struct i2c_driver driver;
  73. static struct i2c_client client_template;
  74. /* ---------------------------------------------------------------------- */
  75. /*
  76. * SAA6588 defines
  77. */
  78. /* Initialization and mode control byte (0w) */
  79. /* bit 0+1 (DAC0/DAC1) */
  80. #define cModeStandard 0x00
  81. #define cModeFastPI 0x01
  82. #define cModeReducedRequest 0x02
  83. #define cModeInvalid 0x03
  84. /* bit 2 (RBDS) */
  85. #define cProcessingModeRDS 0x00
  86. #define cProcessingModeRBDS 0x04
  87. /* bit 3+4 (SYM0/SYM1) */
  88. #define cErrCorrectionNone 0x00
  89. #define cErrCorrection2Bits 0x08
  90. #define cErrCorrection5Bits 0x10
  91. #define cErrCorrectionNoneRBDS 0x18
  92. /* bit 5 (NWSY) */
  93. #define cSyncNormal 0x00
  94. #define cSyncRestart 0x20
  95. /* bit 6 (TSQD) */
  96. #define cSigQualityDetectOFF 0x00
  97. #define cSigQualityDetectON 0x40
  98. /* bit 7 (SQCM) */
  99. #define cSigQualityTriggered 0x00
  100. #define cSigQualityContinous 0x80
  101. /* Pause level and flywheel control byte (1w) */
  102. /* bits 0..5 (FEB0..FEB5) */
  103. #define cFlywheelMaxBlocksMask 0x3F
  104. #define cFlywheelDefault 0x20
  105. /* bits 6+7 (PL0/PL1) */
  106. #define cPauseLevel_11mV 0x00
  107. #define cPauseLevel_17mV 0x40
  108. #define cPauseLevel_27mV 0x80
  109. #define cPauseLevel_43mV 0xC0
  110. /* Pause time/oscillator frequency/quality detector control byte (1w) */
  111. /* bits 0..4 (SQS0..SQS4) */
  112. #define cQualityDetectSensMask 0x1F
  113. #define cQualityDetectDefault 0x0F
  114. /* bit 5 (SOSC) */
  115. #define cSelectOscFreqOFF 0x00
  116. #define cSelectOscFreqON 0x20
  117. /* bit 6+7 (PTF0/PTF1) */
  118. #define cOscFreq_4332kHz 0x00
  119. #define cOscFreq_8664kHz 0x40
  120. #define cOscFreq_12996kHz 0x80
  121. #define cOscFreq_17328kHz 0xC0
  122. /* ---------------------------------------------------------------------- */
  123. static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf)
  124. {
  125. int i;
  126. if (s->rd_index == s->wr_index) {
  127. if (debug > 2)
  128. dprintk(PREFIX "Read: buffer empty.\n");
  129. return 0;
  130. }
  131. if (debug > 2) {
  132. dprintk(PREFIX "Read: ");
  133. for (i = s->rd_index; i < s->rd_index + 3; i++)
  134. dprintk("0x%02x ", s->buffer[i]);
  135. }
  136. if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
  137. return -EFAULT;
  138. s->rd_index += 3;
  139. if (s->rd_index >= s->buf_size)
  140. s->rd_index = 0;
  141. s->block_count--;
  142. if (debug > 2)
  143. dprintk("%d blocks total.\n", s->block_count);
  144. return 1;
  145. }
  146. static void read_from_buf(struct saa6588 *s, struct rds_command *a)
  147. {
  148. unsigned long flags;
  149. unsigned char __user *buf_ptr = a->buffer;
  150. unsigned int i;
  151. unsigned int rd_blocks;
  152. a->result = 0;
  153. if (!a->buffer)
  154. return;
  155. while (!s->data_available_for_read) {
  156. int ret = wait_event_interruptible(s->read_queue,
  157. s->data_available_for_read);
  158. if (ret == -ERESTARTSYS) {
  159. a->result = -EINTR;
  160. return;
  161. }
  162. }
  163. spin_lock_irqsave(&s->lock, flags);
  164. rd_blocks = a->block_count;
  165. if (rd_blocks > s->block_count)
  166. rd_blocks = s->block_count;
  167. if (!rd_blocks)
  168. return;
  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. unsigned long flags;
  205. unsigned char tmpbuf[6];
  206. unsigned char blocknum;
  207. unsigned char tmp;
  208. /* Although we only need 3 bytes, we have to read at least 6.
  209. SAA6588 returns garbage otherwise */
  210. if (6 != i2c_master_recv(&s->client, &tmpbuf[0], 6)) {
  211. if (debug > 1)
  212. dprintk(PREFIX "read error!\n");
  213. return;
  214. }
  215. blocknum = tmpbuf[0] >> 5;
  216. if (blocknum == s->last_blocknum) {
  217. if (debug > 3)
  218. dprintk("Saw block %d again.\n", blocknum);
  219. return;
  220. }
  221. s->last_blocknum = blocknum;
  222. /*
  223. Byte order according to v4l2 specification:
  224. Byte 0: Least Significant Byte of RDS Block
  225. Byte 1: Most Significant Byte of RDS Block
  226. Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
  227. occurred during reception of this block.
  228. Bit 6: Corrected bit. Indicates that an error was
  229. corrected for this data block.
  230. Bits 5-3: Received Offset. Indicates the offset received
  231. by the sync system.
  232. Bits 2-0: Offset Name. Indicates the offset applied to this data.
  233. SAA6588 byte order is Status-MSB-LSB, so we have to swap the
  234. first and the last of the 3 bytes block.
  235. */
  236. tmp = tmpbuf[2];
  237. tmpbuf[2] = tmpbuf[0];
  238. tmpbuf[0] = tmp;
  239. tmp = blocknum;
  240. tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */
  241. if ((tmpbuf[2] & 0x03) == 0x03)
  242. tmp |= 0x80; /* uncorrectable error */
  243. else if ((tmpbuf[2] & 0x03) != 0x00)
  244. tmp |= 0x40; /* corrected error */
  245. tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */
  246. spin_lock_irqsave(&s->lock, flags);
  247. block_to_buf(s, tmpbuf);
  248. spin_unlock_irqrestore(&s->lock, flags);
  249. s->data_available_for_read = 1;
  250. wake_up_interruptible(&s->read_queue);
  251. }
  252. static void saa6588_timer(unsigned long data)
  253. {
  254. struct saa6588 *s = (struct saa6588 *)data;
  255. schedule_work(&s->work);
  256. }
  257. static void saa6588_work(void *data)
  258. {
  259. struct saa6588 *s = (struct saa6588 *)data;
  260. saa6588_i2c_poll(s);
  261. mod_timer(&s->timer, jiffies + msecs_to_jiffies(20));
  262. }
  263. static int saa6588_configure(struct saa6588 *s)
  264. {
  265. unsigned char buf[3];
  266. int rc;
  267. buf[0] = cSyncRestart;
  268. if (rbds)
  269. buf[0] |= cProcessingModeRBDS;
  270. buf[1] = cFlywheelDefault;
  271. switch (plvl) {
  272. case 0:
  273. buf[1] |= cPauseLevel_11mV;
  274. break;
  275. case 1:
  276. buf[1] |= cPauseLevel_17mV;
  277. break;
  278. case 2:
  279. buf[1] |= cPauseLevel_27mV;
  280. break;
  281. case 3:
  282. buf[1] |= cPauseLevel_43mV;
  283. break;
  284. default: /* nothing */
  285. break;
  286. }
  287. buf[2] = cQualityDetectDefault | cSelectOscFreqON;
  288. switch (xtal) {
  289. case 0:
  290. buf[2] |= cOscFreq_4332kHz;
  291. break;
  292. case 1:
  293. buf[2] |= cOscFreq_8664kHz;
  294. break;
  295. case 2:
  296. buf[2] |= cOscFreq_12996kHz;
  297. break;
  298. case 3:
  299. buf[2] |= cOscFreq_17328kHz;
  300. break;
  301. default: /* nothing */
  302. break;
  303. }
  304. dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
  305. buf[0], buf[1], buf[2]);
  306. if (3 != (rc = i2c_master_send(&s->client, buf, 3)))
  307. printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
  308. return 0;
  309. }
  310. /* ---------------------------------------------------------------------- */
  311. static int saa6588_attach(struct i2c_adapter *adap, int addr, int kind)
  312. {
  313. struct saa6588 *s;
  314. client_template.adapter = adap;
  315. client_template.addr = addr;
  316. printk(PREFIX "chip found @ 0x%x\n", addr << 1);
  317. if (NULL == (s = kmalloc(sizeof(*s), GFP_KERNEL)))
  318. return -ENOMEM;
  319. s->buf_size = bufblocks * 3;
  320. if (NULL == (s->buffer = kmalloc(s->buf_size, GFP_KERNEL))) {
  321. kfree(s);
  322. return -ENOMEM;
  323. }
  324. s->client = client_template;
  325. s->block_count = 0;
  326. s->wr_index = 0;
  327. s->rd_index = 0;
  328. s->last_blocknum = 0xff;
  329. init_waitqueue_head(&s->read_queue);
  330. s->data_available_for_read = 0;
  331. i2c_set_clientdata(&s->client, s);
  332. i2c_attach_client(&s->client);
  333. saa6588_configure(s);
  334. /* start polling via eventd */
  335. INIT_WORK(&s->work, saa6588_work, s);
  336. init_timer(&s->timer);
  337. s->timer.function = saa6588_timer;
  338. s->timer.data = (unsigned long)s;
  339. schedule_work(&s->work);
  340. return 0;
  341. }
  342. static int saa6588_probe(struct i2c_adapter *adap)
  343. {
  344. if (adap->class & I2C_CLASS_TV_ANALOG)
  345. return i2c_probe(adap, &addr_data, saa6588_attach);
  346. return 0;
  347. }
  348. static int saa6588_detach(struct i2c_client *client)
  349. {
  350. struct saa6588 *s = i2c_get_clientdata(client);
  351. del_timer_sync(&s->timer);
  352. flush_scheduled_work();
  353. i2c_detach_client(client);
  354. kfree(s->buffer);
  355. kfree(s);
  356. return 0;
  357. }
  358. static int saa6588_command(struct i2c_client *client, unsigned int cmd,
  359. void *arg)
  360. {
  361. struct saa6588 *s = i2c_get_clientdata(client);
  362. struct rds_command *a = (struct rds_command *)arg;
  363. switch (cmd) {
  364. /* --- open() for /dev/radio --- */
  365. case RDS_CMD_OPEN:
  366. a->result = 0; /* return error if chip doesn't work ??? */
  367. break;
  368. /* --- close() for /dev/radio --- */
  369. case RDS_CMD_CLOSE:
  370. s->data_available_for_read = 1;
  371. wake_up_interruptible(&s->read_queue);
  372. a->result = 0;
  373. break;
  374. /* --- read() for /dev/radio --- */
  375. case RDS_CMD_READ:
  376. read_from_buf(s, a);
  377. break;
  378. /* --- poll() for /dev/radio --- */
  379. case RDS_CMD_POLL:
  380. a->result = 0;
  381. if (s->data_available_for_read) {
  382. a->result |= POLLIN | POLLRDNORM;
  383. }
  384. poll_wait(a->instance, &s->read_queue, a->event_list);
  385. break;
  386. default:
  387. /* nothing */
  388. break;
  389. }
  390. return 0;
  391. }
  392. /* ----------------------------------------------------------------------- */
  393. static struct i2c_driver driver = {
  394. .driver = {
  395. .name = "saa6588",
  396. },
  397. .id = -1, /* FIXME */
  398. .attach_adapter = saa6588_probe,
  399. .detach_client = saa6588_detach,
  400. .command = saa6588_command,
  401. };
  402. static struct i2c_client client_template = {
  403. .name = "saa6588",
  404. .driver = &driver,
  405. };
  406. static int __init saa6588_init_module(void)
  407. {
  408. return i2c_add_driver(&driver);
  409. }
  410. static void __exit saa6588_cleanup_module(void)
  411. {
  412. i2c_del_driver(&driver);
  413. }
  414. module_init(saa6588_init_module);
  415. module_exit(saa6588_cleanup_module);
  416. /*
  417. * Overrides for Emacs so that we follow Linus's tabbing style.
  418. * ---------------------------------------------------------------------------
  419. * Local variables:
  420. * c-basic-offset: 8
  421. * End:
  422. */