vp27smpx.c 5.3 KB

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