vp27smpx.c 5.0 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/ioctl.h>
  26. #include <asm/uaccess.h>
  27. #include <linux/i2c.h>
  28. #include <linux/i2c-id.h>
  29. #include <linux/videodev.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-chip-ident.h>
  32. MODULE_DESCRIPTION("vp27smpx driver");
  33. MODULE_AUTHOR("Hans Verkuil");
  34. MODULE_LICENSE("GPL");
  35. static unsigned short normal_i2c[] = { 0xb6 >> 1, I2C_CLIENT_END };
  36. I2C_CLIENT_INSMOD;
  37. /* ----------------------------------------------------------------------- */
  38. struct vp27smpx_state {
  39. int radio;
  40. u32 audmode;
  41. };
  42. static void vp27smpx_set_audmode(struct i2c_client *client, u32 audmode)
  43. {
  44. struct vp27smpx_state *state = i2c_get_clientdata(client);
  45. u8 data[3] = { 0x00, 0x00, 0x04 };
  46. switch (audmode) {
  47. case V4L2_TUNER_MODE_MONO:
  48. case V4L2_TUNER_MODE_LANG1:
  49. break;
  50. case V4L2_TUNER_MODE_STEREO:
  51. case V4L2_TUNER_MODE_LANG1_LANG2:
  52. data[1] = 0x01;
  53. break;
  54. case V4L2_TUNER_MODE_LANG2:
  55. data[1] = 0x02;
  56. break;
  57. }
  58. if (i2c_master_send(client, data, sizeof(data)) != sizeof(data)) {
  59. v4l_err(client, "%s: I/O error setting audmode\n", client->name);
  60. }
  61. else {
  62. state->audmode = audmode;
  63. }
  64. }
  65. static int vp27smpx_command(struct i2c_client *client, unsigned int cmd,
  66. void *arg)
  67. {
  68. struct vp27smpx_state *state = i2c_get_clientdata(client);
  69. struct v4l2_tuner *vt = arg;
  70. switch (cmd) {
  71. case AUDC_SET_RADIO:
  72. state->radio = 1;
  73. break;
  74. case VIDIOC_S_STD:
  75. state->radio = 0;
  76. break;
  77. case VIDIOC_S_TUNER:
  78. if (!state->radio)
  79. vp27smpx_set_audmode(client, vt->audmode);
  80. break;
  81. case VIDIOC_G_TUNER:
  82. if (state->radio)
  83. break;
  84. vt->audmode = state->audmode;
  85. vt->capability = V4L2_TUNER_CAP_STEREO |
  86. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  87. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  88. break;
  89. case VIDIOC_G_CHIP_IDENT:
  90. return v4l2_chip_ident_i2c_client(client, arg, V4L2_IDENT_VP27SMPX, 0);
  91. case VIDIOC_LOG_STATUS:
  92. v4l_info(client, "Audio Mode: %u%s\n", state->audmode,
  93. state->radio ? " (Radio)" : "");
  94. break;
  95. default:
  96. return -EINVAL;
  97. }
  98. return 0;
  99. }
  100. /* ----------------------------------------------------------------------- */
  101. /* i2c implementation */
  102. /*
  103. * Generic i2c probe
  104. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  105. */
  106. static struct i2c_driver i2c_driver;
  107. static int vp27smpx_attach(struct i2c_adapter *adapter, int address, int kind)
  108. {
  109. struct i2c_client *client;
  110. struct vp27smpx_state *state;
  111. /* Check if the adapter supports the needed features */
  112. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  113. return 0;
  114. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  115. if (client == 0)
  116. return -ENOMEM;
  117. client->addr = address;
  118. client->adapter = adapter;
  119. client->driver = &i2c_driver;
  120. snprintf(client->name, sizeof(client->name) - 1, "vp27smpx");
  121. v4l_info(client, "chip found @ 0x%x (%s)\n", address << 1, adapter->name);
  122. state = kzalloc(sizeof(struct vp27smpx_state), GFP_KERNEL);
  123. if (state == NULL) {
  124. kfree(client);
  125. return -ENOMEM;
  126. }
  127. state->audmode = V4L2_TUNER_MODE_STEREO;
  128. i2c_set_clientdata(client, state);
  129. /* initialize vp27smpx */
  130. vp27smpx_set_audmode(client, state->audmode);
  131. i2c_attach_client(client);
  132. return 0;
  133. }
  134. static int vp27smpx_probe(struct i2c_adapter *adapter)
  135. {
  136. if (adapter->class & I2C_CLASS_TV_ANALOG)
  137. return i2c_probe(adapter, &addr_data, vp27smpx_attach);
  138. return 0;
  139. }
  140. static int vp27smpx_detach(struct i2c_client *client)
  141. {
  142. struct vp27smpx_state *state = i2c_get_clientdata(client);
  143. int err;
  144. err = i2c_detach_client(client);
  145. if (err) {
  146. return err;
  147. }
  148. kfree(state);
  149. kfree(client);
  150. return 0;
  151. }
  152. /* ----------------------------------------------------------------------- */
  153. /* i2c implementation */
  154. static struct i2c_driver i2c_driver = {
  155. .driver = {
  156. .name = "vp27smpx",
  157. },
  158. .id = I2C_DRIVERID_VP27SMPX,
  159. .attach_adapter = vp27smpx_probe,
  160. .detach_client = vp27smpx_detach,
  161. .command = vp27smpx_command,
  162. };
  163. static int __init vp27smpx_init_module(void)
  164. {
  165. return i2c_add_driver(&i2c_driver);
  166. }
  167. static void __exit vp27smpx_cleanup_module(void)
  168. {
  169. i2c_del_driver(&i2c_driver);
  170. }
  171. module_init(vp27smpx_init_module);
  172. module_exit(vp27smpx_cleanup_module);