vp27smpx.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/slab.h>
  22. #include <linux/ioctl.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/i2c.h>
  25. #include <linux/videodev2.h>
  26. #include <media/v4l2-device.h>
  27. MODULE_DESCRIPTION("vp27smpx driver");
  28. MODULE_AUTHOR("Hans Verkuil");
  29. MODULE_LICENSE("GPL");
  30. /* ----------------------------------------------------------------------- */
  31. struct vp27smpx_state {
  32. struct v4l2_subdev sd;
  33. int radio;
  34. u32 audmode;
  35. };
  36. static inline struct vp27smpx_state *to_state(struct v4l2_subdev *sd)
  37. {
  38. return container_of(sd, struct vp27smpx_state, sd);
  39. }
  40. static void vp27smpx_set_audmode(struct v4l2_subdev *sd, u32 audmode)
  41. {
  42. struct vp27smpx_state *state = to_state(sd);
  43. struct i2c_client *client = v4l2_get_subdevdata(sd);
  44. u8 data[3] = { 0x00, 0x00, 0x04 };
  45. switch (audmode) {
  46. case V4L2_TUNER_MODE_MONO:
  47. case V4L2_TUNER_MODE_LANG1:
  48. break;
  49. case V4L2_TUNER_MODE_STEREO:
  50. case V4L2_TUNER_MODE_LANG1_LANG2:
  51. data[1] = 0x01;
  52. break;
  53. case V4L2_TUNER_MODE_LANG2:
  54. data[1] = 0x02;
  55. break;
  56. }
  57. if (i2c_master_send(client, data, sizeof(data)) != sizeof(data))
  58. v4l2_err(sd, "I/O error setting audmode\n");
  59. else
  60. state->audmode = audmode;
  61. }
  62. static int vp27smpx_s_radio(struct v4l2_subdev *sd)
  63. {
  64. struct vp27smpx_state *state = to_state(sd);
  65. state->radio = 1;
  66. return 0;
  67. }
  68. static int vp27smpx_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  69. {
  70. struct vp27smpx_state *state = to_state(sd);
  71. state->radio = 0;
  72. return 0;
  73. }
  74. static int vp27smpx_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
  75. {
  76. struct vp27smpx_state *state = to_state(sd);
  77. if (!state->radio)
  78. vp27smpx_set_audmode(sd, vt->audmode);
  79. return 0;
  80. }
  81. static int vp27smpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  82. {
  83. struct vp27smpx_state *state = to_state(sd);
  84. if (state->radio)
  85. return 0;
  86. vt->audmode = state->audmode;
  87. vt->capability = V4L2_TUNER_CAP_STEREO |
  88. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  89. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  90. return 0;
  91. }
  92. static int vp27smpx_log_status(struct v4l2_subdev *sd)
  93. {
  94. struct vp27smpx_state *state = to_state(sd);
  95. v4l2_info(sd, "Audio Mode: %u%s\n", state->audmode,
  96. state->radio ? " (Radio)" : "");
  97. return 0;
  98. }
  99. /* ----------------------------------------------------------------------- */
  100. static const struct v4l2_subdev_core_ops vp27smpx_core_ops = {
  101. .log_status = vp27smpx_log_status,
  102. };
  103. static const struct v4l2_subdev_tuner_ops vp27smpx_tuner_ops = {
  104. .s_radio = vp27smpx_s_radio,
  105. .s_tuner = vp27smpx_s_tuner,
  106. .g_tuner = vp27smpx_g_tuner,
  107. };
  108. static const struct v4l2_subdev_video_ops vp27smpx_video_ops = {
  109. .s_std = vp27smpx_s_std,
  110. };
  111. static const struct v4l2_subdev_ops vp27smpx_ops = {
  112. .core = &vp27smpx_core_ops,
  113. .tuner = &vp27smpx_tuner_ops,
  114. .video = &vp27smpx_video_ops,
  115. };
  116. /* ----------------------------------------------------------------------- */
  117. /* i2c implementation */
  118. /*
  119. * Generic i2c probe
  120. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  121. */
  122. static int vp27smpx_probe(struct i2c_client *client,
  123. const struct i2c_device_id *id)
  124. {
  125. struct vp27smpx_state *state;
  126. struct v4l2_subdev *sd;
  127. /* Check if the adapter supports the needed features */
  128. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  129. return -EIO;
  130. v4l_info(client, "chip found @ 0x%x (%s)\n",
  131. client->addr << 1, client->adapter->name);
  132. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  133. if (state == NULL)
  134. return -ENOMEM;
  135. sd = &state->sd;
  136. v4l2_i2c_subdev_init(sd, client, &vp27smpx_ops);
  137. state->audmode = V4L2_TUNER_MODE_STEREO;
  138. /* initialize vp27smpx */
  139. vp27smpx_set_audmode(sd, state->audmode);
  140. return 0;
  141. }
  142. static int vp27smpx_remove(struct i2c_client *client)
  143. {
  144. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  145. v4l2_device_unregister_subdev(sd);
  146. return 0;
  147. }
  148. /* ----------------------------------------------------------------------- */
  149. static const struct i2c_device_id vp27smpx_id[] = {
  150. { "vp27smpx", 0 },
  151. { }
  152. };
  153. MODULE_DEVICE_TABLE(i2c, vp27smpx_id);
  154. static struct i2c_driver vp27smpx_driver = {
  155. .driver = {
  156. .name = "vp27smpx",
  157. },
  158. .probe = vp27smpx_probe,
  159. .remove = vp27smpx_remove,
  160. .id_table = vp27smpx_id,
  161. };
  162. module_i2c_driver(vp27smpx_driver);