saa6588.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 rbds;
  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(rbds, int, 0);
  42. MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0");
  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. };
  67. static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd)
  68. {
  69. return container_of(sd, struct saa6588, sd);
  70. }
  71. /* ---------------------------------------------------------------------- */
  72. /*
  73. * SAA6588 defines
  74. */
  75. /* Initialization and mode control byte (0w) */
  76. /* bit 0+1 (DAC0/DAC1) */
  77. #define cModeStandard 0x00
  78. #define cModeFastPI 0x01
  79. #define cModeReducedRequest 0x02
  80. #define cModeInvalid 0x03
  81. /* bit 2 (RBDS) */
  82. #define cProcessingModeRDS 0x00
  83. #define cProcessingModeRBDS 0x04
  84. /* bit 3+4 (SYM0/SYM1) */
  85. #define cErrCorrectionNone 0x00
  86. #define cErrCorrection2Bits 0x08
  87. #define cErrCorrection5Bits 0x10
  88. #define cErrCorrectionNoneRBDS 0x18
  89. /* bit 5 (NWSY) */
  90. #define cSyncNormal 0x00
  91. #define cSyncRestart 0x20
  92. /* bit 6 (TSQD) */
  93. #define cSigQualityDetectOFF 0x00
  94. #define cSigQualityDetectON 0x40
  95. /* bit 7 (SQCM) */
  96. #define cSigQualityTriggered 0x00
  97. #define cSigQualityContinous 0x80
  98. /* Pause level and flywheel control byte (1w) */
  99. /* bits 0..5 (FEB0..FEB5) */
  100. #define cFlywheelMaxBlocksMask 0x3F
  101. #define cFlywheelDefault 0x20
  102. /* bits 6+7 (PL0/PL1) */
  103. #define cPauseLevel_11mV 0x00
  104. #define cPauseLevel_17mV 0x40
  105. #define cPauseLevel_27mV 0x80
  106. #define cPauseLevel_43mV 0xC0
  107. /* Pause time/oscillator frequency/quality detector control byte (1w) */
  108. /* bits 0..4 (SQS0..SQS4) */
  109. #define cQualityDetectSensMask 0x1F
  110. #define cQualityDetectDefault 0x0F
  111. /* bit 5 (SOSC) */
  112. #define cSelectOscFreqOFF 0x00
  113. #define cSelectOscFreqON 0x20
  114. /* bit 6+7 (PTF0/PTF1) */
  115. #define cOscFreq_4332kHz 0x00
  116. #define cOscFreq_8664kHz 0x40
  117. #define cOscFreq_12996kHz 0x80
  118. #define cOscFreq_17328kHz 0xC0
  119. /* ---------------------------------------------------------------------- */
  120. static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf)
  121. {
  122. int i;
  123. if (s->rd_index == s->wr_index) {
  124. if (debug > 2)
  125. dprintk(PREFIX "Read: buffer empty.\n");
  126. return 0;
  127. }
  128. if (debug > 2) {
  129. dprintk(PREFIX "Read: ");
  130. for (i = s->rd_index; i < s->rd_index + 3; i++)
  131. dprintk("0x%02x ", s->buffer[i]);
  132. }
  133. if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
  134. return -EFAULT;
  135. s->rd_index += 3;
  136. if (s->rd_index >= s->buf_size)
  137. s->rd_index = 0;
  138. s->block_count--;
  139. if (debug > 2)
  140. dprintk("%d blocks total.\n", s->block_count);
  141. return 1;
  142. }
  143. static void read_from_buf(struct saa6588 *s, struct rds_command *a)
  144. {
  145. unsigned long flags;
  146. unsigned char __user *buf_ptr = a->buffer;
  147. unsigned int i;
  148. unsigned int rd_blocks;
  149. a->result = 0;
  150. if (!a->buffer)
  151. return;
  152. while (!s->data_available_for_read) {
  153. int ret = wait_event_interruptible(s->read_queue,
  154. s->data_available_for_read);
  155. if (ret == -ERESTARTSYS) {
  156. a->result = -EINTR;
  157. return;
  158. }
  159. }
  160. spin_lock_irqsave(&s->lock, flags);
  161. rd_blocks = a->block_count;
  162. if (rd_blocks > s->block_count)
  163. rd_blocks = s->block_count;
  164. if (!rd_blocks) {
  165. spin_unlock_irqrestore(&s->lock, flags);
  166. return;
  167. }
  168. for (i = 0; i < rd_blocks; i++) {
  169. if (block_to_user_buf(s, buf_ptr)) {
  170. buf_ptr += 3;
  171. a->result++;
  172. } else
  173. break;
  174. }
  175. a->result *= 3;
  176. s->data_available_for_read = (s->block_count > 0);
  177. spin_unlock_irqrestore(&s->lock, flags);
  178. }
  179. static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
  180. {
  181. unsigned int i;
  182. if (debug > 3)
  183. dprintk(PREFIX "New block: ");
  184. for (i = 0; i < 3; ++i) {
  185. if (debug > 3)
  186. dprintk("0x%02x ", blockbuf[i]);
  187. s->buffer[s->wr_index] = blockbuf[i];
  188. s->wr_index++;
  189. }
  190. if (s->wr_index >= s->buf_size)
  191. s->wr_index = 0;
  192. if (s->wr_index == s->rd_index) {
  193. s->rd_index += 3;
  194. if (s->rd_index >= s->buf_size)
  195. s->rd_index = 0;
  196. } else
  197. s->block_count++;
  198. if (debug > 3)
  199. dprintk("%d blocks total.\n", s->block_count);
  200. }
  201. static void saa6588_i2c_poll(struct saa6588 *s)
  202. {
  203. struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
  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(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_work(struct work_struct *work)
  253. {
  254. struct saa6588 *s = container_of(work, struct saa6588, work.work);
  255. saa6588_i2c_poll(s);
  256. schedule_delayed_work(&s->work, msecs_to_jiffies(20));
  257. }
  258. static int saa6588_configure(struct saa6588 *s)
  259. {
  260. struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
  261. unsigned char buf[3];
  262. int rc;
  263. buf[0] = cSyncRestart;
  264. if (rbds)
  265. buf[0] |= cProcessingModeRBDS;
  266. buf[1] = cFlywheelDefault;
  267. switch (plvl) {
  268. case 0:
  269. buf[1] |= cPauseLevel_11mV;
  270. break;
  271. case 1:
  272. buf[1] |= cPauseLevel_17mV;
  273. break;
  274. case 2:
  275. buf[1] |= cPauseLevel_27mV;
  276. break;
  277. case 3:
  278. buf[1] |= cPauseLevel_43mV;
  279. break;
  280. default: /* nothing */
  281. break;
  282. }
  283. buf[2] = cQualityDetectDefault | cSelectOscFreqON;
  284. switch (xtal) {
  285. case 0:
  286. buf[2] |= cOscFreq_4332kHz;
  287. break;
  288. case 1:
  289. buf[2] |= cOscFreq_8664kHz;
  290. break;
  291. case 2:
  292. buf[2] |= cOscFreq_12996kHz;
  293. break;
  294. case 3:
  295. buf[2] |= cOscFreq_17328kHz;
  296. break;
  297. default: /* nothing */
  298. break;
  299. }
  300. dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
  301. buf[0], buf[1], buf[2]);
  302. rc = i2c_master_send(client, buf, 3);
  303. if (rc != 3)
  304. printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
  305. return 0;
  306. }
  307. /* ---------------------------------------------------------------------- */
  308. static long saa6588_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  309. {
  310. struct saa6588 *s = to_saa6588(sd);
  311. struct rds_command *a = arg;
  312. switch (cmd) {
  313. /* --- open() for /dev/radio --- */
  314. case RDS_CMD_OPEN:
  315. a->result = 0; /* return error if chip doesn't work ??? */
  316. break;
  317. /* --- close() for /dev/radio --- */
  318. case RDS_CMD_CLOSE:
  319. s->data_available_for_read = 1;
  320. wake_up_interruptible(&s->read_queue);
  321. a->result = 0;
  322. break;
  323. /* --- read() for /dev/radio --- */
  324. case RDS_CMD_READ:
  325. read_from_buf(s, a);
  326. break;
  327. /* --- poll() for /dev/radio --- */
  328. case RDS_CMD_POLL:
  329. a->result = 0;
  330. if (s->data_available_for_read) {
  331. a->result |= POLLIN | POLLRDNORM;
  332. }
  333. poll_wait(a->instance, &s->read_queue, a->event_list);
  334. break;
  335. default:
  336. /* nothing */
  337. return -ENOIOCTLCMD;
  338. }
  339. return 0;
  340. }
  341. static int saa6588_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  342. {
  343. struct i2c_client *client = v4l2_get_subdevdata(sd);
  344. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA6588, 0);
  345. }
  346. /* ----------------------------------------------------------------------- */
  347. static const struct v4l2_subdev_core_ops saa6588_core_ops = {
  348. .g_chip_ident = saa6588_g_chip_ident,
  349. .ioctl = saa6588_ioctl,
  350. };
  351. static const struct v4l2_subdev_ops saa6588_ops = {
  352. .core = &saa6588_core_ops,
  353. };
  354. /* ---------------------------------------------------------------------- */
  355. static int saa6588_probe(struct i2c_client *client,
  356. const struct i2c_device_id *id)
  357. {
  358. struct saa6588 *s;
  359. struct v4l2_subdev *sd;
  360. v4l_info(client, "saa6588 found @ 0x%x (%s)\n",
  361. client->addr << 1, client->adapter->name);
  362. s = kzalloc(sizeof(*s), GFP_KERNEL);
  363. if (s == NULL)
  364. return -ENOMEM;
  365. s->buf_size = bufblocks * 3;
  366. s->buffer = kmalloc(s->buf_size, GFP_KERNEL);
  367. if (s->buffer == NULL) {
  368. kfree(s);
  369. return -ENOMEM;
  370. }
  371. sd = &s->sd;
  372. v4l2_i2c_subdev_init(sd, client, &saa6588_ops);
  373. spin_lock_init(&s->lock);
  374. s->block_count = 0;
  375. s->wr_index = 0;
  376. s->rd_index = 0;
  377. s->last_blocknum = 0xff;
  378. init_waitqueue_head(&s->read_queue);
  379. s->data_available_for_read = 0;
  380. saa6588_configure(s);
  381. /* start polling via eventd */
  382. INIT_DELAYED_WORK(&s->work, saa6588_work);
  383. schedule_delayed_work(&s->work, 0);
  384. return 0;
  385. }
  386. static int saa6588_remove(struct i2c_client *client)
  387. {
  388. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  389. struct saa6588 *s = to_saa6588(sd);
  390. v4l2_device_unregister_subdev(sd);
  391. cancel_delayed_work_sync(&s->work);
  392. kfree(s->buffer);
  393. kfree(s);
  394. return 0;
  395. }
  396. /* ----------------------------------------------------------------------- */
  397. static const struct i2c_device_id saa6588_id[] = {
  398. { "saa6588", 0 },
  399. { }
  400. };
  401. MODULE_DEVICE_TABLE(i2c, saa6588_id);
  402. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  403. .name = "saa6588",
  404. .probe = saa6588_probe,
  405. .remove = saa6588_remove,
  406. .id_table = saa6588_id,
  407. };