cs5345.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cs5345 Cirrus Logic 24-bit, 192 kHz Stereo Audio ADC
  4. * Copyright (C) 2007 Hans Verkuil
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/i2c.h>
  9. #include <linux/videodev2.h>
  10. #include <linux/slab.h>
  11. #include <media/v4l2-device.h>
  12. #include <media/v4l2-ctrls.h>
  13. MODULE_DESCRIPTION("i2c device driver for cs5345 Audio ADC");
  14. MODULE_AUTHOR("Hans Verkuil");
  15. MODULE_LICENSE("GPL");
  16. static bool debug;
  17. module_param(debug, bool, 0644);
  18. MODULE_PARM_DESC(debug, "Debugging messages, 0=Off (default), 1=On");
  19. struct cs5345_state {
  20. struct v4l2_subdev sd;
  21. struct v4l2_ctrl_handler hdl;
  22. };
  23. static inline struct cs5345_state *to_state(struct v4l2_subdev *sd)
  24. {
  25. return container_of(sd, struct cs5345_state, sd);
  26. }
  27. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  28. {
  29. return &container_of(ctrl->handler, struct cs5345_state, hdl)->sd;
  30. }
  31. /* ----------------------------------------------------------------------- */
  32. static inline int cs5345_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  33. {
  34. struct i2c_client *client = v4l2_get_subdevdata(sd);
  35. return i2c_smbus_write_byte_data(client, reg, value);
  36. }
  37. static inline int cs5345_read(struct v4l2_subdev *sd, u8 reg)
  38. {
  39. struct i2c_client *client = v4l2_get_subdevdata(sd);
  40. return i2c_smbus_read_byte_data(client, reg);
  41. }
  42. static int cs5345_s_routing(struct v4l2_subdev *sd,
  43. u32 input, u32 output, u32 config)
  44. {
  45. if ((input & 0xf) > 6) {
  46. v4l2_err(sd, "Invalid input %d.\n", input);
  47. return -EINVAL;
  48. }
  49. cs5345_write(sd, 0x09, input & 0xf);
  50. cs5345_write(sd, 0x05, input & 0xf0);
  51. return 0;
  52. }
  53. static int cs5345_s_ctrl(struct v4l2_ctrl *ctrl)
  54. {
  55. struct v4l2_subdev *sd = to_sd(ctrl);
  56. switch (ctrl->id) {
  57. case V4L2_CID_AUDIO_MUTE:
  58. cs5345_write(sd, 0x04, ctrl->val ? 0x80 : 0);
  59. return 0;
  60. case V4L2_CID_AUDIO_VOLUME:
  61. cs5345_write(sd, 0x07, ((u8)ctrl->val) & 0x3f);
  62. cs5345_write(sd, 0x08, ((u8)ctrl->val) & 0x3f);
  63. return 0;
  64. }
  65. return -EINVAL;
  66. }
  67. #ifdef CONFIG_VIDEO_ADV_DEBUG
  68. static int cs5345_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  69. {
  70. reg->size = 1;
  71. reg->val = cs5345_read(sd, reg->reg & 0x1f);
  72. return 0;
  73. }
  74. static int cs5345_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  75. {
  76. cs5345_write(sd, reg->reg & 0x1f, reg->val & 0xff);
  77. return 0;
  78. }
  79. #endif
  80. static int cs5345_log_status(struct v4l2_subdev *sd)
  81. {
  82. u8 v = cs5345_read(sd, 0x09) & 7;
  83. u8 m = cs5345_read(sd, 0x04);
  84. int vol = cs5345_read(sd, 0x08) & 0x3f;
  85. v4l2_info(sd, "Input: %d%s\n", v,
  86. (m & 0x80) ? " (muted)" : "");
  87. if (vol >= 32)
  88. vol = vol - 64;
  89. v4l2_info(sd, "Volume: %d dB\n", vol);
  90. return 0;
  91. }
  92. /* ----------------------------------------------------------------------- */
  93. static const struct v4l2_ctrl_ops cs5345_ctrl_ops = {
  94. .s_ctrl = cs5345_s_ctrl,
  95. };
  96. static const struct v4l2_subdev_core_ops cs5345_core_ops = {
  97. .log_status = cs5345_log_status,
  98. #ifdef CONFIG_VIDEO_ADV_DEBUG
  99. .g_register = cs5345_g_register,
  100. .s_register = cs5345_s_register,
  101. #endif
  102. };
  103. static const struct v4l2_subdev_audio_ops cs5345_audio_ops = {
  104. .s_routing = cs5345_s_routing,
  105. };
  106. static const struct v4l2_subdev_ops cs5345_ops = {
  107. .core = &cs5345_core_ops,
  108. .audio = &cs5345_audio_ops,
  109. };
  110. /* ----------------------------------------------------------------------- */
  111. static int cs5345_probe(struct i2c_client *client,
  112. const struct i2c_device_id *id)
  113. {
  114. struct cs5345_state *state;
  115. struct v4l2_subdev *sd;
  116. /* Check if the adapter supports the needed features */
  117. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  118. return -EIO;
  119. v4l_info(client, "chip found @ 0x%x (%s)\n",
  120. client->addr << 1, client->adapter->name);
  121. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  122. if (state == NULL)
  123. return -ENOMEM;
  124. sd = &state->sd;
  125. v4l2_i2c_subdev_init(sd, client, &cs5345_ops);
  126. v4l2_ctrl_handler_init(&state->hdl, 2);
  127. v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops,
  128. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  129. v4l2_ctrl_new_std(&state->hdl, &cs5345_ctrl_ops,
  130. V4L2_CID_AUDIO_VOLUME, -24, 24, 1, 0);
  131. sd->ctrl_handler = &state->hdl;
  132. if (state->hdl.error) {
  133. int err = state->hdl.error;
  134. v4l2_ctrl_handler_free(&state->hdl);
  135. return err;
  136. }
  137. /* set volume/mute */
  138. v4l2_ctrl_handler_setup(&state->hdl);
  139. cs5345_write(sd, 0x02, 0x00);
  140. cs5345_write(sd, 0x04, 0x01);
  141. cs5345_write(sd, 0x09, 0x01);
  142. return 0;
  143. }
  144. /* ----------------------------------------------------------------------- */
  145. static void cs5345_remove(struct i2c_client *client)
  146. {
  147. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  148. struct cs5345_state *state = to_state(sd);
  149. v4l2_device_unregister_subdev(sd);
  150. v4l2_ctrl_handler_free(&state->hdl);
  151. }
  152. /* ----------------------------------------------------------------------- */
  153. static const struct i2c_device_id cs5345_id[] = {
  154. { "cs5345", 0 },
  155. { }
  156. };
  157. MODULE_DEVICE_TABLE(i2c, cs5345_id);
  158. static struct i2c_driver cs5345_driver = {
  159. .driver = {
  160. .name = "cs5345",
  161. },
  162. .probe = cs5345_probe,
  163. .remove = cs5345_remove,
  164. .id_table = cs5345_id,
  165. };
  166. module_i2c_driver(cs5345_driver);