tef6862.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * tef6862.c Philips TEF6862 Car Radio Enhanced Selectivity Tuner
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/i2c.h>
  24. #include <linux/slab.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/v4l2-device.h>
  27. #define DRIVER_NAME "tef6862"
  28. #define FREQ_MUL 16000
  29. #define TEF6862_LO_FREQ (875 * FREQ_MUL / 10)
  30. #define TEF6862_HI_FREQ (108 * FREQ_MUL)
  31. /* Write mode sub addresses */
  32. #define WM_SUB_BANDWIDTH 0x0
  33. #define WM_SUB_PLLM 0x1
  34. #define WM_SUB_PLLL 0x2
  35. #define WM_SUB_DAA 0x3
  36. #define WM_SUB_AGC 0x4
  37. #define WM_SUB_BAND 0x5
  38. #define WM_SUB_CONTROL 0x6
  39. #define WM_SUB_LEVEL 0x7
  40. #define WM_SUB_IFCF 0x8
  41. #define WM_SUB_IFCAP 0x9
  42. #define WM_SUB_ACD 0xA
  43. #define WM_SUB_TEST 0xF
  44. /* Different modes of the MSA register */
  45. #define MODE_BUFFER 0x0
  46. #define MODE_PRESET 0x1
  47. #define MODE_SEARCH 0x2
  48. #define MODE_AF_UPDATE 0x3
  49. #define MODE_JUMP 0x4
  50. #define MODE_CHECK 0x5
  51. #define MODE_LOAD 0x6
  52. #define MODE_END 0x7
  53. #define MODE_SHIFT 5
  54. struct tef6862_state {
  55. struct v4l2_subdev sd;
  56. unsigned long freq;
  57. };
  58. static inline struct tef6862_state *to_state(struct v4l2_subdev *sd)
  59. {
  60. return container_of(sd, struct tef6862_state, sd);
  61. }
  62. static u16 tef6862_sigstr(struct i2c_client *client)
  63. {
  64. u8 buf[4];
  65. int err = i2c_master_recv(client, buf, sizeof(buf));
  66. if (err == sizeof(buf))
  67. return buf[3] << 8;
  68. return 0;
  69. }
  70. static int tef6862_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
  71. {
  72. if (v->index > 0)
  73. return -EINVAL;
  74. /* only support FM for now */
  75. strlcpy(v->name, "FM", sizeof(v->name));
  76. v->type = V4L2_TUNER_RADIO;
  77. v->rangelow = TEF6862_LO_FREQ;
  78. v->rangehigh = TEF6862_HI_FREQ;
  79. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  80. v->capability = V4L2_TUNER_CAP_LOW;
  81. v->audmode = V4L2_TUNER_MODE_STEREO;
  82. v->signal = tef6862_sigstr(v4l2_get_subdevdata(sd));
  83. return 0;
  84. }
  85. static int tef6862_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
  86. {
  87. return v->index ? -EINVAL : 0;
  88. }
  89. static int tef6862_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
  90. {
  91. struct tef6862_state *state = to_state(sd);
  92. struct i2c_client *client = v4l2_get_subdevdata(sd);
  93. u16 pll;
  94. u8 i2cmsg[3];
  95. int err;
  96. if (f->tuner != 0)
  97. return -EINVAL;
  98. pll = 1964 + ((f->frequency - TEF6862_LO_FREQ) * 20) / FREQ_MUL;
  99. i2cmsg[0] = (MODE_PRESET << MODE_SHIFT) | WM_SUB_PLLM;
  100. i2cmsg[1] = (pll >> 8) & 0xff;
  101. i2cmsg[2] = pll & 0xff;
  102. err = i2c_master_send(client, i2cmsg, sizeof(i2cmsg));
  103. if (err != sizeof(i2cmsg))
  104. return err < 0 ? err : -EIO;
  105. state->freq = f->frequency;
  106. return 0;
  107. }
  108. static int tef6862_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  109. {
  110. struct tef6862_state *state = to_state(sd);
  111. if (f->tuner != 0)
  112. return -EINVAL;
  113. f->type = V4L2_TUNER_RADIO;
  114. f->frequency = state->freq;
  115. return 0;
  116. }
  117. static const struct v4l2_subdev_tuner_ops tef6862_tuner_ops = {
  118. .g_tuner = tef6862_g_tuner,
  119. .s_tuner = tef6862_s_tuner,
  120. .s_frequency = tef6862_s_frequency,
  121. .g_frequency = tef6862_g_frequency,
  122. };
  123. static const struct v4l2_subdev_ops tef6862_ops = {
  124. .tuner = &tef6862_tuner_ops,
  125. };
  126. /*
  127. * Generic i2c probe
  128. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  129. */
  130. static int tef6862_probe(struct i2c_client *client,
  131. const struct i2c_device_id *id)
  132. {
  133. struct tef6862_state *state;
  134. struct v4l2_subdev *sd;
  135. /* Check if the adapter supports the needed features */
  136. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  137. return -EIO;
  138. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  139. client->addr << 1, client->adapter->name);
  140. state = kzalloc(sizeof(struct tef6862_state), GFP_KERNEL);
  141. if (state == NULL)
  142. return -ENOMEM;
  143. state->freq = TEF6862_LO_FREQ;
  144. sd = &state->sd;
  145. v4l2_i2c_subdev_init(sd, client, &tef6862_ops);
  146. return 0;
  147. }
  148. static int tef6862_remove(struct i2c_client *client)
  149. {
  150. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  151. v4l2_device_unregister_subdev(sd);
  152. kfree(to_state(sd));
  153. return 0;
  154. }
  155. static const struct i2c_device_id tef6862_id[] = {
  156. {DRIVER_NAME, 0},
  157. {},
  158. };
  159. MODULE_DEVICE_TABLE(i2c, tef6862_id);
  160. static struct i2c_driver tef6862_driver = {
  161. .driver = {
  162. .owner = THIS_MODULE,
  163. .name = DRIVER_NAME,
  164. },
  165. .probe = tef6862_probe,
  166. .remove = tef6862_remove,
  167. .id_table = tef6862_id,
  168. };
  169. module_i2c_driver(tef6862_driver);
  170. MODULE_DESCRIPTION("TEF6862 Car Radio Enhanced Selectivity Tuner");
  171. MODULE_AUTHOR("Mocean Laboratories");
  172. MODULE_LICENSE("GPL v2");