tuner-3036.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Driver for Philips SAB3036 "CITAC" tuner control chip.
  3. *
  4. * Author: Phil Blundell <philb@gnu.org>
  5. *
  6. * The SAB3036 is just about different enough from the chips that
  7. * tuner.c copes with to make it not worth the effort to crowbar
  8. * the support into that file. So instead we have a separate driver.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/timer.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/init.h>
  24. #include <linux/i2c.h>
  25. #include <linux/videodev.h>
  26. #include <media/v4l2-common.h>
  27. #include <media/tuner.h>
  28. static int debug; /* insmod parameter */
  29. static int this_adap;
  30. static struct i2c_client client_template;
  31. /* Addresses to scan */
  32. static unsigned short normal_i2c[] = { 0x60, 0x61, I2C_CLIENT_END };
  33. static unsigned short ignore = I2C_CLIENT_END;
  34. static struct i2c_client_address_data addr_data = {
  35. .normal_i2c = normal_i2c,
  36. .probe = &ignore,
  37. .ignore = &ignore,
  38. };
  39. /* ---------------------------------------------------------------------- */
  40. static unsigned char
  41. tuner_getstatus (struct i2c_client *c)
  42. {
  43. unsigned char byte;
  44. if (i2c_master_recv(c, &byte, 1) != 1)
  45. printk(KERN_ERR "tuner-3036: I/O error.\n");
  46. return byte;
  47. }
  48. #define TUNER_FL 0x80
  49. static int
  50. tuner_islocked (struct i2c_client *c)
  51. {
  52. return (tuner_getstatus(c) & TUNER_FL);
  53. }
  54. /* ---------------------------------------------------------------------- */
  55. static void
  56. set_tv_freq(struct i2c_client *c, int freq)
  57. {
  58. u16 div = ((freq * 20) / 16);
  59. unsigned long give_up = jiffies + HZ;
  60. unsigned char buffer[2];
  61. if (debug)
  62. printk(KERN_DEBUG "tuner: setting frequency %dMHz, divisor %x\n", freq / 16, div);
  63. /* Select high tuning current */
  64. buffer[0] = 0x29;
  65. buffer[1] = 0x3e;
  66. if (i2c_master_send(c, buffer, 2) != 2)
  67. printk("tuner: i2c i/o error 1\n");
  68. buffer[0] = 0x80 | ((div>>8) & 0x7f);
  69. buffer[1] = div & 0xff;
  70. if (i2c_master_send(c, buffer, 2) != 2)
  71. printk("tuner: i2c i/o error 2\n");
  72. while (!tuner_islocked(c) && time_before(jiffies, give_up))
  73. schedule();
  74. if (!tuner_islocked(c))
  75. printk(KERN_WARNING "tuner: failed to achieve PLL lock\n");
  76. /* Select low tuning current and engage AFC */
  77. buffer[0] = 0x29;
  78. buffer[1] = 0xb2;
  79. if (i2c_master_send(c, buffer, 2) != 2)
  80. printk("tuner: i2c i/o error 3\n");
  81. if (debug)
  82. printk(KERN_DEBUG "tuner: status %02x\n", tuner_getstatus(c));
  83. }
  84. /* ---------------------------------------------------------------------- */
  85. static int
  86. tuner_attach(struct i2c_adapter *adap, int addr, int kind)
  87. {
  88. static unsigned char buffer[] = { 0x29, 0x32, 0x2a, 0, 0x2b, 0 };
  89. struct i2c_client *client;
  90. if (this_adap > 0)
  91. return -1;
  92. this_adap++;
  93. client_template.adapter = adap;
  94. client_template.addr = addr;
  95. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  96. if (client == NULL)
  97. return -ENOMEM;
  98. memcpy(client, &client_template, sizeof(struct i2c_client));
  99. printk("tuner: SAB3036 found, status %02x\n", tuner_getstatus(client));
  100. i2c_attach_client(client);
  101. if (i2c_master_send(client, buffer, 2) != 2)
  102. printk("tuner: i2c i/o error 1\n");
  103. if (i2c_master_send(client, buffer+2, 2) != 2)
  104. printk("tuner: i2c i/o error 2\n");
  105. if (i2c_master_send(client, buffer+4, 2) != 2)
  106. printk("tuner: i2c i/o error 3\n");
  107. return 0;
  108. }
  109. static int
  110. tuner_detach(struct i2c_client *c)
  111. {
  112. return 0;
  113. }
  114. static int
  115. tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
  116. {
  117. int *iarg = (int*)arg;
  118. switch (cmd)
  119. {
  120. case VIDIOCSFREQ:
  121. set_tv_freq(client, *iarg);
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. return 0;
  127. }
  128. static int
  129. tuner_probe(struct i2c_adapter *adap)
  130. {
  131. this_adap = 0;
  132. if (adap->id == I2C_HW_B_LP)
  133. return i2c_probe(adap, &addr_data, tuner_attach);
  134. return 0;
  135. }
  136. /* ----------------------------------------------------------------------- */
  137. static struct i2c_driver
  138. i2c_driver_tuner =
  139. {
  140. .driver = {
  141. .name = "sab3036",
  142. },
  143. .id = I2C_DRIVERID_SAB3036,
  144. .attach_adapter = tuner_probe,
  145. .detach_client = tuner_detach,
  146. .command = tuner_command
  147. };
  148. static struct i2c_client client_template =
  149. {
  150. .driver = &i2c_driver_tuner,
  151. .name = "SAB3036",
  152. };
  153. static int __init
  154. tuner3036_init(void)
  155. {
  156. return i2c_add_driver(&i2c_driver_tuner);
  157. }
  158. static void __exit
  159. tuner3036_exit(void)
  160. {
  161. i2c_del_driver(&i2c_driver_tuner);
  162. }
  163. MODULE_DESCRIPTION("SAB3036 tuner driver");
  164. MODULE_AUTHOR("Philip Blundell <philb@gnu.org>");
  165. MODULE_LICENSE("GPL");
  166. module_param(debug, int, 0);
  167. MODULE_PARM_DESC(debug,"Enable debugging output");
  168. module_init(tuner3036_init);
  169. module_exit(tuner3036_exit);