saa6588.c 12 KB

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