vp27smpx.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * vp27smpx - driver version 0.0.1
  3. *
  4. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  5. *
  6. * Based on a tvaudio patch from Takahiro Adachi <tadachi@tadachi-net.com>
  7. * and Kazuhiko Kawakami <kazz-0@mail.goo.ne.jp>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/slab.h>
  26. #include <linux/ioctl.h>
  27. #include <asm/uaccess.h>
  28. #include <linux/i2c.h>
  29. #include <linux/videodev2.h>
  30. #include <media/v4l2-device.h>
  31. MODULE_DESCRIPTION("vp27smpx driver");
  32. MODULE_AUTHOR("Hans Verkuil");
  33. MODULE_LICENSE("GPL");
  34. /* ----------------------------------------------------------------------- */
  35. struct vp27smpx_state {
  36. struct v4l2_subdev sd;
  37. int radio;
  38. u32 audmode;
  39. };
  40. static inline struct vp27smpx_state *to_state(struct v4l2_subdev *sd)
  41. {
  42. return container_of(sd, struct vp27smpx_state, sd);
  43. }
  44. static void vp27smpx_set_audmode(struct v4l2_subdev *sd, u32 audmode)
  45. {
  46. struct vp27smpx_state *state = to_state(sd);
  47. struct i2c_client *client = v4l2_get_subdevdata(sd);
  48. u8 data[3] = { 0x00, 0x00, 0x04 };
  49. switch (audmode) {
  50. case V4L2_TUNER_MODE_MONO:
  51. case V4L2_TUNER_MODE_LANG1:
  52. break;
  53. case V4L2_TUNER_MODE_STEREO:
  54. case V4L2_TUNER_MODE_LANG1_LANG2:
  55. data[1] = 0x01;
  56. break;
  57. case V4L2_TUNER_MODE_LANG2:
  58. data[1] = 0x02;
  59. break;
  60. }
  61. if (i2c_master_send(client, data, sizeof(data)) != sizeof(data))
  62. v4l2_err(sd, "I/O error setting audmode\n");
  63. else
  64. state->audmode = audmode;
  65. }
  66. static int vp27smpx_s_radio(struct v4l2_subdev *sd)
  67. {
  68. struct vp27smpx_state *state = to_state(sd);
  69. state->radio = 1;
  70. return 0;
  71. }
  72. static int vp27smpx_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  73. {
  74. struct vp27smpx_state *state = to_state(sd);
  75. state->radio = 0;
  76. return 0;
  77. }
  78. static int vp27smpx_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
  79. {
  80. struct vp27smpx_state *state = to_state(sd);
  81. if (!state->radio)
  82. vp27smpx_set_audmode(sd, vt->audmode);
  83. return 0;
  84. }
  85. static int vp27smpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  86. {
  87. struct vp27smpx_state *state = to_state(sd);
  88. if (state->radio)
  89. return 0;
  90. vt->audmode = state->audmode;
  91. vt->capability = V4L2_TUNER_CAP_STEREO |
  92. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  93. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  94. return 0;
  95. }
  96. static int vp27smpx_log_status(struct v4l2_subdev *sd)
  97. {
  98. struct vp27smpx_state *state = to_state(sd);
  99. v4l2_info(sd, "Audio Mode: %u%s\n", state->audmode,
  100. state->radio ? " (Radio)" : "");
  101. return 0;
  102. }
  103. /* ----------------------------------------------------------------------- */
  104. static const struct v4l2_subdev_core_ops vp27smpx_core_ops = {
  105. .log_status = vp27smpx_log_status,
  106. };
  107. static const struct v4l2_subdev_tuner_ops vp27smpx_tuner_ops = {
  108. .s_radio = vp27smpx_s_radio,
  109. .s_tuner = vp27smpx_s_tuner,
  110. .g_tuner = vp27smpx_g_tuner,
  111. };
  112. static const struct v4l2_subdev_video_ops vp27smpx_video_ops = {
  113. .s_std = vp27smpx_s_std,
  114. };
  115. static const struct v4l2_subdev_ops vp27smpx_ops = {
  116. .core = &vp27smpx_core_ops,
  117. .tuner = &vp27smpx_tuner_ops,
  118. .video = &vp27smpx_video_ops,
  119. };
  120. /* ----------------------------------------------------------------------- */
  121. /* i2c implementation */
  122. /*
  123. * Generic i2c probe
  124. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  125. */
  126. static int vp27smpx_probe(struct i2c_client *client,
  127. const struct i2c_device_id *id)
  128. {
  129. struct vp27smpx_state *state;
  130. struct v4l2_subdev *sd;
  131. /* Check if the adapter supports the needed features */
  132. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  133. return -EIO;
  134. v4l_info(client, "chip found @ 0x%x (%s)\n",
  135. client->addr << 1, client->adapter->name);
  136. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  137. if (state == NULL)
  138. return -ENOMEM;
  139. sd = &state->sd;
  140. v4l2_i2c_subdev_init(sd, client, &vp27smpx_ops);
  141. state->audmode = V4L2_TUNER_MODE_STEREO;
  142. /* initialize vp27smpx */
  143. vp27smpx_set_audmode(sd, state->audmode);
  144. return 0;
  145. }
  146. static int vp27smpx_remove(struct i2c_client *client)
  147. {
  148. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  149. v4l2_device_unregister_subdev(sd);
  150. return 0;
  151. }
  152. /* ----------------------------------------------------------------------- */
  153. static const struct i2c_device_id vp27smpx_id[] = {
  154. { "vp27smpx", 0 },
  155. { }
  156. };
  157. MODULE_DEVICE_TABLE(i2c, vp27smpx_id);
  158. static struct i2c_driver vp27smpx_driver = {
  159. .driver = {
  160. .name = "vp27smpx",
  161. },
  162. .probe = vp27smpx_probe,
  163. .remove = vp27smpx_remove,
  164. .id_table = vp27smpx_id,
  165. };
  166. module_i2c_driver(vp27smpx_driver);