vp27smpx.c 5.3 KB

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