saa6588.c 12 KB

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