tef6862.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tef6862.c Philips TEF6862 Car Radio Enhanced Selectivity Tuner
  4. * Copyright (c) 2009 Intel Corporation
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/i2c.h>
  12. #include <linux/slab.h>
  13. #include <media/v4l2-ioctl.h>
  14. #include <media/v4l2-device.h>
  15. #define DRIVER_NAME "tef6862"
  16. #define FREQ_MUL 16000
  17. #define TEF6862_LO_FREQ (875U * FREQ_MUL / 10)
  18. #define TEF6862_HI_FREQ (108U * FREQ_MUL)
  19. /* Write mode sub addresses */
  20. #define WM_SUB_BANDWIDTH 0x0
  21. #define WM_SUB_PLLM 0x1
  22. #define WM_SUB_PLLL 0x2
  23. #define WM_SUB_DAA 0x3
  24. #define WM_SUB_AGC 0x4
  25. #define WM_SUB_BAND 0x5
  26. #define WM_SUB_CONTROL 0x6
  27. #define WM_SUB_LEVEL 0x7
  28. #define WM_SUB_IFCF 0x8
  29. #define WM_SUB_IFCAP 0x9
  30. #define WM_SUB_ACD 0xA
  31. #define WM_SUB_TEST 0xF
  32. /* Different modes of the MSA register */
  33. #define MSA_MODE_BUFFER 0x0
  34. #define MSA_MODE_PRESET 0x1
  35. #define MSA_MODE_SEARCH 0x2
  36. #define MSA_MODE_AF_UPDATE 0x3
  37. #define MSA_MODE_JUMP 0x4
  38. #define MSA_MODE_CHECK 0x5
  39. #define MSA_MODE_LOAD 0x6
  40. #define MSA_MODE_END 0x7
  41. #define MSA_MODE_SHIFT 5
  42. struct tef6862_state {
  43. struct v4l2_subdev sd;
  44. unsigned long freq;
  45. };
  46. static inline struct tef6862_state *to_state(struct v4l2_subdev *sd)
  47. {
  48. return container_of(sd, struct tef6862_state, sd);
  49. }
  50. static u16 tef6862_sigstr(struct i2c_client *client)
  51. {
  52. u8 buf[4];
  53. int err = i2c_master_recv(client, buf, sizeof(buf));
  54. if (err == sizeof(buf))
  55. return buf[3] << 8;
  56. return 0;
  57. }
  58. static int tef6862_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
  59. {
  60. if (v->index > 0)
  61. return -EINVAL;
  62. /* only support FM for now */
  63. strscpy(v->name, "FM", sizeof(v->name));
  64. v->type = V4L2_TUNER_RADIO;
  65. v->rangelow = TEF6862_LO_FREQ;
  66. v->rangehigh = TEF6862_HI_FREQ;
  67. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  68. v->capability = V4L2_TUNER_CAP_LOW;
  69. v->audmode = V4L2_TUNER_MODE_STEREO;
  70. v->signal = tef6862_sigstr(v4l2_get_subdevdata(sd));
  71. return 0;
  72. }
  73. static int tef6862_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
  74. {
  75. return v->index ? -EINVAL : 0;
  76. }
  77. static int tef6862_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
  78. {
  79. struct tef6862_state *state = to_state(sd);
  80. struct i2c_client *client = v4l2_get_subdevdata(sd);
  81. unsigned freq = f->frequency;
  82. u16 pll;
  83. u8 i2cmsg[3];
  84. int err;
  85. if (f->tuner != 0)
  86. return -EINVAL;
  87. freq = clamp(freq, TEF6862_LO_FREQ, TEF6862_HI_FREQ);
  88. pll = 1964 + ((freq - TEF6862_LO_FREQ) * 20) / FREQ_MUL;
  89. i2cmsg[0] = (MSA_MODE_PRESET << MSA_MODE_SHIFT) | WM_SUB_PLLM;
  90. i2cmsg[1] = (pll >> 8) & 0xff;
  91. i2cmsg[2] = pll & 0xff;
  92. err = i2c_master_send(client, i2cmsg, sizeof(i2cmsg));
  93. if (err != sizeof(i2cmsg))
  94. return err < 0 ? err : -EIO;
  95. state->freq = freq;
  96. return 0;
  97. }
  98. static int tef6862_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  99. {
  100. struct tef6862_state *state = to_state(sd);
  101. if (f->tuner != 0)
  102. return -EINVAL;
  103. f->type = V4L2_TUNER_RADIO;
  104. f->frequency = state->freq;
  105. return 0;
  106. }
  107. static const struct v4l2_subdev_tuner_ops tef6862_tuner_ops = {
  108. .g_tuner = tef6862_g_tuner,
  109. .s_tuner = tef6862_s_tuner,
  110. .s_frequency = tef6862_s_frequency,
  111. .g_frequency = tef6862_g_frequency,
  112. };
  113. static const struct v4l2_subdev_ops tef6862_ops = {
  114. .tuner = &tef6862_tuner_ops,
  115. };
  116. /*
  117. * Generic i2c probe
  118. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  119. */
  120. static int tef6862_probe(struct i2c_client *client,
  121. const struct i2c_device_id *id)
  122. {
  123. struct tef6862_state *state;
  124. struct v4l2_subdev *sd;
  125. /* Check if the adapter supports the needed features */
  126. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  127. return -EIO;
  128. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  129. client->addr << 1, client->adapter->name);
  130. state = kzalloc(sizeof(struct tef6862_state), GFP_KERNEL);
  131. if (state == NULL)
  132. return -ENOMEM;
  133. state->freq = TEF6862_LO_FREQ;
  134. sd = &state->sd;
  135. v4l2_i2c_subdev_init(sd, client, &tef6862_ops);
  136. return 0;
  137. }
  138. static void tef6862_remove(struct i2c_client *client)
  139. {
  140. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  141. v4l2_device_unregister_subdev(sd);
  142. kfree(to_state(sd));
  143. }
  144. static const struct i2c_device_id tef6862_id[] = {
  145. {DRIVER_NAME, 0},
  146. {},
  147. };
  148. MODULE_DEVICE_TABLE(i2c, tef6862_id);
  149. static struct i2c_driver tef6862_driver = {
  150. .driver = {
  151. .name = DRIVER_NAME,
  152. },
  153. .probe = tef6862_probe,
  154. .remove = tef6862_remove,
  155. .id_table = tef6862_id,
  156. };
  157. module_i2c_driver(tef6862_driver);
  158. MODULE_DESCRIPTION("TEF6862 Car Radio Enhanced Selectivity Tuner");
  159. MODULE_AUTHOR("Mocean Laboratories");
  160. MODULE_LICENSE("GPL v2");