saa6588.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. spin_unlock_irqrestore(&s->lock, flags);
  169. return;
  170. }
  171. for (i = 0; i < rd_blocks; i++) {
  172. if (block_to_user_buf(s, buf_ptr)) {
  173. buf_ptr += 3;
  174. a->result++;
  175. } else
  176. break;
  177. }
  178. a->result *= 3;
  179. s->data_available_for_read = (s->block_count > 0);
  180. spin_unlock_irqrestore(&s->lock, flags);
  181. }
  182. static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
  183. {
  184. unsigned int i;
  185. if (debug > 3)
  186. dprintk(PREFIX "New block: ");
  187. for (i = 0; i < 3; ++i) {
  188. if (debug > 3)
  189. dprintk("0x%02x ", blockbuf[i]);
  190. s->buffer[s->wr_index] = blockbuf[i];
  191. s->wr_index++;
  192. }
  193. if (s->wr_index >= s->buf_size)
  194. s->wr_index = 0;
  195. if (s->wr_index == s->rd_index) {
  196. s->rd_index += 3;
  197. if (s->rd_index >= s->buf_size)
  198. s->rd_index = 0;
  199. } else
  200. s->block_count++;
  201. if (debug > 3)
  202. dprintk("%d blocks total.\n", s->block_count);
  203. }
  204. static void saa6588_i2c_poll(struct saa6588 *s)
  205. {
  206. unsigned long flags;
  207. unsigned char tmpbuf[6];
  208. unsigned char blocknum;
  209. unsigned char tmp;
  210. /* Although we only need 3 bytes, we have to read at least 6.
  211. SAA6588 returns garbage otherwise */
  212. if (6 != i2c_master_recv(&s->client, &tmpbuf[0], 6)) {
  213. if (debug > 1)
  214. dprintk(PREFIX "read error!\n");
  215. return;
  216. }
  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: Received Offset. Indicates the offset received
  233. by the sync system.
  234. Bits 2-0: Offset Name. Indicates the offset applied to this data.
  235. SAA6588 byte order is Status-MSB-LSB, so we have to swap the
  236. first and the last of the 3 bytes block.
  237. */
  238. tmp = tmpbuf[2];
  239. tmpbuf[2] = tmpbuf[0];
  240. tmpbuf[0] = tmp;
  241. tmp = blocknum;
  242. tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */
  243. if ((tmpbuf[2] & 0x03) == 0x03)
  244. tmp |= 0x80; /* uncorrectable error */
  245. else if ((tmpbuf[2] & 0x03) != 0x00)
  246. tmp |= 0x40; /* 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_timer(unsigned long data)
  255. {
  256. struct saa6588 *s = (struct saa6588 *)data;
  257. schedule_work(&s->work);
  258. }
  259. static void saa6588_work(struct work_struct *work)
  260. {
  261. struct saa6588 *s = container_of(work, struct saa6588, work);
  262. saa6588_i2c_poll(s);
  263. mod_timer(&s->timer, jiffies + msecs_to_jiffies(20));
  264. }
  265. static int saa6588_configure(struct saa6588 *s)
  266. {
  267. unsigned char buf[3];
  268. int rc;
  269. buf[0] = cSyncRestart;
  270. if (rbds)
  271. buf[0] |= cProcessingModeRBDS;
  272. buf[1] = cFlywheelDefault;
  273. switch (plvl) {
  274. case 0:
  275. buf[1] |= cPauseLevel_11mV;
  276. break;
  277. case 1:
  278. buf[1] |= cPauseLevel_17mV;
  279. break;
  280. case 2:
  281. buf[1] |= cPauseLevel_27mV;
  282. break;
  283. case 3:
  284. buf[1] |= cPauseLevel_43mV;
  285. break;
  286. default: /* nothing */
  287. break;
  288. }
  289. buf[2] = cQualityDetectDefault | cSelectOscFreqON;
  290. switch (xtal) {
  291. case 0:
  292. buf[2] |= cOscFreq_4332kHz;
  293. break;
  294. case 1:
  295. buf[2] |= cOscFreq_8664kHz;
  296. break;
  297. case 2:
  298. buf[2] |= cOscFreq_12996kHz;
  299. break;
  300. case 3:
  301. buf[2] |= cOscFreq_17328kHz;
  302. break;
  303. default: /* nothing */
  304. break;
  305. }
  306. dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
  307. buf[0], buf[1], buf[2]);
  308. if (3 != (rc = i2c_master_send(&s->client, buf, 3)))
  309. printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
  310. return 0;
  311. }
  312. /* ---------------------------------------------------------------------- */
  313. static int saa6588_attach(struct i2c_adapter *adap, int addr, int kind)
  314. {
  315. struct saa6588 *s;
  316. client_template.adapter = adap;
  317. client_template.addr = addr;
  318. printk(PREFIX "chip found @ 0x%x\n", addr << 1);
  319. if (NULL == (s = kmalloc(sizeof(*s), GFP_KERNEL)))
  320. return -ENOMEM;
  321. s->buf_size = bufblocks * 3;
  322. if (NULL == (s->buffer = kmalloc(s->buf_size, GFP_KERNEL))) {
  323. kfree(s);
  324. return -ENOMEM;
  325. }
  326. s->client = client_template;
  327. s->block_count = 0;
  328. s->wr_index = 0;
  329. s->rd_index = 0;
  330. s->last_blocknum = 0xff;
  331. init_waitqueue_head(&s->read_queue);
  332. s->data_available_for_read = 0;
  333. i2c_set_clientdata(&s->client, s);
  334. i2c_attach_client(&s->client);
  335. saa6588_configure(s);
  336. /* start polling via eventd */
  337. INIT_WORK(&s->work, saa6588_work);
  338. init_timer(&s->timer);
  339. s->timer.function = saa6588_timer;
  340. s->timer.data = (unsigned long)s;
  341. schedule_work(&s->work);
  342. return 0;
  343. }
  344. static int saa6588_probe(struct i2c_adapter *adap)
  345. {
  346. if (adap->class & I2C_CLASS_TV_ANALOG)
  347. return i2c_probe(adap, &addr_data, saa6588_attach);
  348. return 0;
  349. }
  350. static int saa6588_detach(struct i2c_client *client)
  351. {
  352. struct saa6588 *s = i2c_get_clientdata(client);
  353. del_timer_sync(&s->timer);
  354. flush_scheduled_work();
  355. i2c_detach_client(client);
  356. kfree(s->buffer);
  357. kfree(s);
  358. return 0;
  359. }
  360. static int saa6588_command(struct i2c_client *client, unsigned int cmd,
  361. void *arg)
  362. {
  363. struct saa6588 *s = i2c_get_clientdata(client);
  364. struct rds_command *a = (struct rds_command *)arg;
  365. switch (cmd) {
  366. /* --- open() for /dev/radio --- */
  367. case RDS_CMD_OPEN:
  368. a->result = 0; /* return error if chip doesn't work ??? */
  369. break;
  370. /* --- close() for /dev/radio --- */
  371. case RDS_CMD_CLOSE:
  372. s->data_available_for_read = 1;
  373. wake_up_interruptible(&s->read_queue);
  374. a->result = 0;
  375. break;
  376. /* --- read() for /dev/radio --- */
  377. case RDS_CMD_READ:
  378. read_from_buf(s, a);
  379. break;
  380. /* --- poll() for /dev/radio --- */
  381. case RDS_CMD_POLL:
  382. a->result = 0;
  383. if (s->data_available_for_read) {
  384. a->result |= POLLIN | POLLRDNORM;
  385. }
  386. poll_wait(a->instance, &s->read_queue, a->event_list);
  387. break;
  388. default:
  389. /* nothing */
  390. break;
  391. }
  392. return 0;
  393. }
  394. /* ----------------------------------------------------------------------- */
  395. static struct i2c_driver driver = {
  396. .driver = {
  397. .name = "saa6588",
  398. },
  399. .id = -1, /* FIXME */
  400. .attach_adapter = saa6588_probe,
  401. .detach_client = saa6588_detach,
  402. .command = saa6588_command,
  403. };
  404. static struct i2c_client client_template = {
  405. .name = "saa6588",
  406. .driver = &driver,
  407. };
  408. static int __init saa6588_init_module(void)
  409. {
  410. return i2c_add_driver(&driver);
  411. }
  412. static void __exit saa6588_cleanup_module(void)
  413. {
  414. i2c_del_driver(&driver);
  415. }
  416. module_init(saa6588_init_module);
  417. module_exit(saa6588_cleanup_module);
  418. /*
  419. * Overrides for Emacs so that we follow Linus's tabbing style.
  420. * ---------------------------------------------------------------------------
  421. * Local variables:
  422. * c-basic-offset: 8
  423. * End:
  424. */