cs5345.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * cs5345 Cirrus Logic 24-bit, 192 kHz Stereo Audio ADC
  3. * Copyright (C) 2007 Hans Verkuil
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/version.h>
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/i2c.h>
  23. #include <linux/videodev2.h>
  24. #include <media/v4l2-i2c-drv.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-chip-ident.h>
  27. MODULE_DESCRIPTION("i2c device driver for cs5345 Audio ADC");
  28. MODULE_AUTHOR("Hans Verkuil");
  29. MODULE_LICENSE("GPL");
  30. static int debug;
  31. module_param(debug, bool, 0644);
  32. MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On");
  33. /* ----------------------------------------------------------------------- */
  34. static inline int cs5345_write(struct i2c_client *client, u8 reg, u8 value)
  35. {
  36. return i2c_smbus_write_byte_data(client, reg, value);
  37. }
  38. static inline int cs5345_read(struct i2c_client *client, u8 reg)
  39. {
  40. return i2c_smbus_read_byte_data(client, reg);
  41. }
  42. static int cs5345_command(struct i2c_client *client, unsigned cmd, void *arg)
  43. {
  44. struct v4l2_routing *route = arg;
  45. struct v4l2_control *ctrl = arg;
  46. switch (cmd) {
  47. case VIDIOC_INT_G_AUDIO_ROUTING:
  48. route->input = cs5345_read(client, 0x09) & 7;
  49. route->input |= cs5345_read(client, 0x05) & 0x70;
  50. route->output = 0;
  51. break;
  52. case VIDIOC_INT_S_AUDIO_ROUTING:
  53. if ((route->input & 0xf) > 6) {
  54. v4l_err(client, "Invalid input %d.\n", route->input);
  55. return -EINVAL;
  56. }
  57. cs5345_write(client, 0x09, route->input & 0xf);
  58. cs5345_write(client, 0x05, route->input & 0xf0);
  59. break;
  60. case VIDIOC_G_CTRL:
  61. if (ctrl->id == V4L2_CID_AUDIO_MUTE) {
  62. ctrl->value = (cs5345_read(client, 0x04) & 0x08) != 0;
  63. break;
  64. }
  65. if (ctrl->id != V4L2_CID_AUDIO_VOLUME)
  66. return -EINVAL;
  67. ctrl->value = cs5345_read(client, 0x07) & 0x3f;
  68. if (ctrl->value >= 32)
  69. ctrl->value = ctrl->value - 64;
  70. break;
  71. case VIDIOC_S_CTRL:
  72. break;
  73. if (ctrl->id == V4L2_CID_AUDIO_MUTE) {
  74. cs5345_write(client, 0x04, ctrl->value ? 0x80 : 0);
  75. break;
  76. }
  77. if (ctrl->id != V4L2_CID_AUDIO_VOLUME)
  78. return -EINVAL;
  79. if (ctrl->value > 24 || ctrl->value < -24)
  80. return -EINVAL;
  81. cs5345_write(client, 0x07, ((u8)ctrl->value) & 0x3f);
  82. cs5345_write(client, 0x08, ((u8)ctrl->value) & 0x3f);
  83. break;
  84. #ifdef CONFIG_VIDEO_ADV_DEBUG
  85. case VIDIOC_DBG_G_REGISTER:
  86. case VIDIOC_DBG_S_REGISTER:
  87. {
  88. struct v4l2_register *reg = arg;
  89. if (!v4l2_chip_match_i2c_client(client,
  90. reg->match_type, reg->match_chip))
  91. return -EINVAL;
  92. if (!capable(CAP_SYS_ADMIN))
  93. return -EPERM;
  94. if (cmd == VIDIOC_DBG_G_REGISTER)
  95. reg->val = cs5345_read(client, reg->reg & 0x1f);
  96. else
  97. cs5345_write(client, reg->reg & 0x1f, reg->val & 0xff);
  98. break;
  99. }
  100. #endif
  101. case VIDIOC_G_CHIP_IDENT:
  102. return v4l2_chip_ident_i2c_client(client,
  103. arg, V4L2_IDENT_CS5345, 0);
  104. case VIDIOC_LOG_STATUS:
  105. {
  106. u8 v = cs5345_read(client, 0x09) & 7;
  107. u8 m = cs5345_read(client, 0x04);
  108. int vol = cs5345_read(client, 0x08) & 0x3f;
  109. v4l_info(client, "Input: %d%s\n", v,
  110. (m & 0x80) ? " (muted)" : "");
  111. if (vol >= 32)
  112. vol = vol - 64;
  113. v4l_info(client, "Volume: %d dB\n", vol);
  114. break;
  115. }
  116. default:
  117. return -EINVAL;
  118. }
  119. return 0;
  120. }
  121. /* ----------------------------------------------------------------------- */
  122. static int cs5345_probe(struct i2c_client *client,
  123. const struct i2c_device_id *id)
  124. {
  125. /* Check if the adapter supports the needed features */
  126. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  127. return -EIO;
  128. v4l_info(client, "chip found @ 0x%x (%s)\n",
  129. client->addr << 1, client->adapter->name);
  130. cs5345_write(client, 0x02, 0x00);
  131. cs5345_write(client, 0x04, 0x01);
  132. cs5345_write(client, 0x09, 0x01);
  133. return 0;
  134. }
  135. /* ----------------------------------------------------------------------- */
  136. static const struct i2c_device_id cs5345_id[] = {
  137. { "cs5345", 0 },
  138. { }
  139. };
  140. MODULE_DEVICE_TABLE(i2c, cs5345_id);
  141. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  142. .name = "cs5345",
  143. .driverid = I2C_DRIVERID_CS5345,
  144. .command = cs5345_command,
  145. .probe = cs5345_probe,
  146. .id_table = cs5345_id,
  147. };