tuner-3036.c 4.9 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/tuner.h>
  27. static int debug; /* insmod parameter */
  28. static int this_adap;
  29. static struct i2c_client client_template;
  30. /* Addresses to scan */
  31. static unsigned short normal_i2c[] = { 0x60, 0x61, I2C_CLIENT_END };
  32. static unsigned short ignore = I2C_CLIENT_END;
  33. static struct i2c_client_address_data addr_data = {
  34. .normal_i2c = normal_i2c,
  35. .probe = &ignore,
  36. .ignore = &ignore,
  37. };
  38. /* ---------------------------------------------------------------------- */
  39. static unsigned char
  40. tuner_getstatus (struct i2c_client *c)
  41. {
  42. unsigned char byte;
  43. if (i2c_master_recv(c, &byte, 1) != 1)
  44. printk(KERN_ERR "tuner-3036: I/O error.\n");
  45. return byte;
  46. }
  47. #define TUNER_FL 0x80
  48. static int
  49. tuner_islocked (struct i2c_client *c)
  50. {
  51. return (tuner_getstatus(c) & TUNER_FL);
  52. }
  53. /* ---------------------------------------------------------------------- */
  54. static void
  55. set_tv_freq(struct i2c_client *c, int freq)
  56. {
  57. u16 div = ((freq * 20) / 16);
  58. unsigned long give_up = jiffies + HZ;
  59. unsigned char buffer[2];
  60. if (debug)
  61. printk(KERN_DEBUG "tuner: setting frequency %dMHz, divisor %x\n", freq / 16, div);
  62. /* Select high tuning current */
  63. buffer[0] = 0x29;
  64. buffer[1] = 0x3e;
  65. if (i2c_master_send(c, buffer, 2) != 2)
  66. printk("tuner: i2c i/o error 1\n");
  67. buffer[0] = 0x80 | ((div>>8) & 0x7f);
  68. buffer[1] = div & 0xff;
  69. if (i2c_master_send(c, buffer, 2) != 2)
  70. printk("tuner: i2c i/o error 2\n");
  71. while (!tuner_islocked(c) && time_before(jiffies, give_up))
  72. schedule();
  73. if (!tuner_islocked(c))
  74. printk(KERN_WARNING "tuner: failed to achieve PLL lock\n");
  75. /* Select low tuning current and engage AFC */
  76. buffer[0] = 0x29;
  77. buffer[1] = 0xb2;
  78. if (i2c_master_send(c, buffer, 2) != 2)
  79. printk("tuner: i2c i/o error 3\n");
  80. if (debug)
  81. printk(KERN_DEBUG "tuner: status %02x\n", tuner_getstatus(c));
  82. }
  83. /* ---------------------------------------------------------------------- */
  84. static int
  85. tuner_attach(struct i2c_adapter *adap, int addr, int kind)
  86. {
  87. static unsigned char buffer[] = { 0x29, 0x32, 0x2a, 0, 0x2b, 0 };
  88. struct i2c_client *client;
  89. if (this_adap > 0)
  90. return -1;
  91. this_adap++;
  92. client_template.adapter = adap;
  93. client_template.addr = addr;
  94. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  95. if (client == NULL)
  96. return -ENOMEM;
  97. memcpy(client, &client_template, sizeof(struct i2c_client));
  98. printk("tuner: SAB3036 found, status %02x\n", tuner_getstatus(client));
  99. i2c_attach_client(client);
  100. if (i2c_master_send(client, buffer, 2) != 2)
  101. printk("tuner: i2c i/o error 1\n");
  102. if (i2c_master_send(client, buffer+2, 2) != 2)
  103. printk("tuner: i2c i/o error 2\n");
  104. if (i2c_master_send(client, buffer+4, 2) != 2)
  105. printk("tuner: i2c i/o error 3\n");
  106. return 0;
  107. }
  108. static int
  109. tuner_detach(struct i2c_client *c)
  110. {
  111. return 0;
  112. }
  113. static int
  114. tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
  115. {
  116. int *iarg = (int*)arg;
  117. switch (cmd)
  118. {
  119. case VIDIOCSFREQ:
  120. set_tv_freq(client, *iarg);
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. return 0;
  126. }
  127. static int
  128. tuner_probe(struct i2c_adapter *adap)
  129. {
  130. this_adap = 0;
  131. if (adap->id == I2C_HW_B_LP)
  132. return i2c_probe(adap, &addr_data, tuner_attach);
  133. return 0;
  134. }
  135. /* ----------------------------------------------------------------------- */
  136. static struct i2c_driver
  137. i2c_driver_tuner =
  138. {
  139. .owner = THIS_MODULE,
  140. .name = "sab3036",
  141. .id = I2C_DRIVERID_SAB3036,
  142. .flags = I2C_DF_NOTIFY,
  143. .attach_adapter = tuner_probe,
  144. .detach_client = tuner_detach,
  145. .command = tuner_command
  146. };
  147. static struct i2c_client client_template =
  148. {
  149. .driver = &i2c_driver_tuner,
  150. .name = "SAB3036",
  151. };
  152. static int __init
  153. tuner3036_init(void)
  154. {
  155. i2c_add_driver(&i2c_driver_tuner);
  156. return 0;
  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);