tlv320aic23b.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * tlv320aic23b - driver version 0.0.1
  4. *
  5. * Copyright (C) 2006 Scott Alfter <[email protected]>
  6. *
  7. * Based on wm8775 driver
  8. *
  9. * Copyright (C) 2004 Ulf Eklund <ivtv at eklund.to>
  10. * Copyright (C) 2005 Hans Verkuil <[email protected]>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/ioctl.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/i2c.h>
  18. #include <linux/videodev2.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-ctrls.h>
  21. MODULE_DESCRIPTION("tlv320aic23b driver");
  22. MODULE_AUTHOR("Scott Alfter, Ulf Eklund, Hans Verkuil");
  23. MODULE_LICENSE("GPL");
  24. /* ----------------------------------------------------------------------- */
  25. struct tlv320aic23b_state {
  26. struct v4l2_subdev sd;
  27. struct v4l2_ctrl_handler hdl;
  28. };
  29. static inline struct tlv320aic23b_state *to_state(struct v4l2_subdev *sd)
  30. {
  31. return container_of(sd, struct tlv320aic23b_state, sd);
  32. }
  33. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  34. {
  35. return &container_of(ctrl->handler, struct tlv320aic23b_state, hdl)->sd;
  36. }
  37. static int tlv320aic23b_write(struct v4l2_subdev *sd, int reg, u16 val)
  38. {
  39. struct i2c_client *client = v4l2_get_subdevdata(sd);
  40. int i;
  41. if ((reg < 0 || reg > 9) && (reg != 15)) {
  42. v4l2_err(sd, "Invalid register R%d\n", reg);
  43. return -1;
  44. }
  45. for (i = 0; i < 3; i++)
  46. if (i2c_smbus_write_byte_data(client,
  47. (reg << 1) | (val >> 8), val & 0xff) == 0)
  48. return 0;
  49. v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
  50. return -1;
  51. }
  52. static int tlv320aic23b_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  53. {
  54. switch (freq) {
  55. case 32000: /* set sample rate to 32 kHz */
  56. tlv320aic23b_write(sd, 8, 0x018);
  57. break;
  58. case 44100: /* set sample rate to 44.1 kHz */
  59. tlv320aic23b_write(sd, 8, 0x022);
  60. break;
  61. case 48000: /* set sample rate to 48 kHz */
  62. tlv320aic23b_write(sd, 8, 0x000);
  63. break;
  64. default:
  65. return -EINVAL;
  66. }
  67. return 0;
  68. }
  69. static int tlv320aic23b_s_ctrl(struct v4l2_ctrl *ctrl)
  70. {
  71. struct v4l2_subdev *sd = to_sd(ctrl);
  72. switch (ctrl->id) {
  73. case V4L2_CID_AUDIO_MUTE:
  74. tlv320aic23b_write(sd, 0, 0x180); /* mute both channels */
  75. /* set gain on both channels to +3.0 dB */
  76. if (!ctrl->val)
  77. tlv320aic23b_write(sd, 0, 0x119);
  78. return 0;
  79. }
  80. return -EINVAL;
  81. }
  82. static int tlv320aic23b_log_status(struct v4l2_subdev *sd)
  83. {
  84. struct tlv320aic23b_state *state = to_state(sd);
  85. v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
  86. return 0;
  87. }
  88. /* ----------------------------------------------------------------------- */
  89. static const struct v4l2_ctrl_ops tlv320aic23b_ctrl_ops = {
  90. .s_ctrl = tlv320aic23b_s_ctrl,
  91. };
  92. static const struct v4l2_subdev_core_ops tlv320aic23b_core_ops = {
  93. .log_status = tlv320aic23b_log_status,
  94. };
  95. static const struct v4l2_subdev_audio_ops tlv320aic23b_audio_ops = {
  96. .s_clock_freq = tlv320aic23b_s_clock_freq,
  97. };
  98. static const struct v4l2_subdev_ops tlv320aic23b_ops = {
  99. .core = &tlv320aic23b_core_ops,
  100. .audio = &tlv320aic23b_audio_ops,
  101. };
  102. /* ----------------------------------------------------------------------- */
  103. /* i2c implementation */
  104. /*
  105. * Generic i2c probe
  106. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  107. */
  108. static int tlv320aic23b_probe(struct i2c_client *client,
  109. const struct i2c_device_id *id)
  110. {
  111. struct tlv320aic23b_state *state;
  112. struct v4l2_subdev *sd;
  113. /* Check if the adapter supports the needed features */
  114. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  115. return -EIO;
  116. v4l_info(client, "chip found @ 0x%x (%s)\n",
  117. client->addr << 1, client->adapter->name);
  118. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  119. if (state == NULL)
  120. return -ENOMEM;
  121. sd = &state->sd;
  122. v4l2_i2c_subdev_init(sd, client, &tlv320aic23b_ops);
  123. /* Initialize tlv320aic23b */
  124. /* RESET */
  125. tlv320aic23b_write(sd, 15, 0x000);
  126. /* turn off DAC & mic input */
  127. tlv320aic23b_write(sd, 6, 0x00A);
  128. /* left-justified, 24-bit, master mode */
  129. tlv320aic23b_write(sd, 7, 0x049);
  130. /* set gain on both channels to +3.0 dB */
  131. tlv320aic23b_write(sd, 0, 0x119);
  132. /* set sample rate to 48 kHz */
  133. tlv320aic23b_write(sd, 8, 0x000);
  134. /* activate digital interface */
  135. tlv320aic23b_write(sd, 9, 0x001);
  136. v4l2_ctrl_handler_init(&state->hdl, 1);
  137. v4l2_ctrl_new_std(&state->hdl, &tlv320aic23b_ctrl_ops,
  138. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  139. sd->ctrl_handler = &state->hdl;
  140. if (state->hdl.error) {
  141. int err = state->hdl.error;
  142. v4l2_ctrl_handler_free(&state->hdl);
  143. return err;
  144. }
  145. v4l2_ctrl_handler_setup(&state->hdl);
  146. return 0;
  147. }
  148. static void tlv320aic23b_remove(struct i2c_client *client)
  149. {
  150. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  151. struct tlv320aic23b_state *state = to_state(sd);
  152. v4l2_device_unregister_subdev(sd);
  153. v4l2_ctrl_handler_free(&state->hdl);
  154. }
  155. /* ----------------------------------------------------------------------- */
  156. static const struct i2c_device_id tlv320aic23b_id[] = {
  157. { "tlv320aic23b", 0 },
  158. { }
  159. };
  160. MODULE_DEVICE_TABLE(i2c, tlv320aic23b_id);
  161. static struct i2c_driver tlv320aic23b_driver = {
  162. .driver = {
  163. .name = "tlv320aic23b",
  164. },
  165. .probe = tlv320aic23b_probe,
  166. .remove = tlv320aic23b_remove,
  167. .id_table = tlv320aic23b_id,
  168. };
  169. module_i2c_driver(tlv320aic23b_driver);