wm8739.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * wm8739
  4. *
  5. * Copyright (C) 2005 T. Adachi <tadachi@tadachi-net.com>
  6. *
  7. * Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
  8. * - Cleanup
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/ioctl.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/i2c.h>
  16. #include <linux/videodev2.h>
  17. #include <media/v4l2-device.h>
  18. #include <media/v4l2-ctrls.h>
  19. MODULE_DESCRIPTION("wm8739 driver");
  20. MODULE_AUTHOR("T. Adachi, Hans Verkuil");
  21. MODULE_LICENSE("GPL");
  22. static int debug;
  23. module_param(debug, int, 0644);
  24. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  25. /* ------------------------------------------------------------------------ */
  26. enum {
  27. R0 = 0, R1,
  28. R5 = 5, R6, R7, R8, R9, R15 = 15,
  29. TOT_REGS
  30. };
  31. struct wm8739_state {
  32. struct v4l2_subdev sd;
  33. struct v4l2_ctrl_handler hdl;
  34. struct {
  35. /* audio cluster */
  36. struct v4l2_ctrl *volume;
  37. struct v4l2_ctrl *mute;
  38. struct v4l2_ctrl *balance;
  39. };
  40. u32 clock_freq;
  41. };
  42. static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
  43. {
  44. return container_of(sd, struct wm8739_state, sd);
  45. }
  46. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  47. {
  48. return &container_of(ctrl->handler, struct wm8739_state, hdl)->sd;
  49. }
  50. /* ------------------------------------------------------------------------ */
  51. static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
  52. {
  53. struct i2c_client *client = v4l2_get_subdevdata(sd);
  54. int i;
  55. if (reg < 0 || reg >= TOT_REGS) {
  56. v4l2_err(sd, "Invalid register R%d\n", reg);
  57. return -1;
  58. }
  59. v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
  60. for (i = 0; i < 3; i++)
  61. if (i2c_smbus_write_byte_data(client,
  62. (reg << 1) | (val >> 8), val & 0xff) == 0)
  63. return 0;
  64. v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
  65. return -1;
  66. }
  67. static int wm8739_s_ctrl(struct v4l2_ctrl *ctrl)
  68. {
  69. struct v4l2_subdev *sd = to_sd(ctrl);
  70. struct wm8739_state *state = to_state(sd);
  71. unsigned int work_l, work_r;
  72. u8 vol_l; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
  73. u8 vol_r; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
  74. u16 mute;
  75. switch (ctrl->id) {
  76. case V4L2_CID_AUDIO_VOLUME:
  77. break;
  78. default:
  79. return -EINVAL;
  80. }
  81. /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */
  82. work_l = (min(65536 - state->balance->val, 32768) * state->volume->val) / 32768;
  83. work_r = (min(state->balance->val, 32768) * state->volume->val) / 32768;
  84. vol_l = (long)work_l * 31 / 65535;
  85. vol_r = (long)work_r * 31 / 65535;
  86. /* set audio volume etc. */
  87. mute = state->mute->val ? 0x80 : 0;
  88. /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
  89. * Default setting: 0x17 = 0 dB
  90. */
  91. wm8739_write(sd, R0, (vol_l & 0x1f) | mute);
  92. wm8739_write(sd, R1, (vol_r & 0x1f) | mute);
  93. return 0;
  94. }
  95. /* ------------------------------------------------------------------------ */
  96. static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
  97. {
  98. struct wm8739_state *state = to_state(sd);
  99. state->clock_freq = audiofreq;
  100. /* de-activate */
  101. wm8739_write(sd, R9, 0x000);
  102. switch (audiofreq) {
  103. case 44100:
  104. /* 256fps, fs=44.1k */
  105. wm8739_write(sd, R8, 0x020);
  106. break;
  107. case 48000:
  108. /* 256fps, fs=48k */
  109. wm8739_write(sd, R8, 0x000);
  110. break;
  111. case 32000:
  112. /* 256fps, fs=32k */
  113. wm8739_write(sd, R8, 0x018);
  114. break;
  115. default:
  116. break;
  117. }
  118. /* activate */
  119. wm8739_write(sd, R9, 0x001);
  120. return 0;
  121. }
  122. static int wm8739_log_status(struct v4l2_subdev *sd)
  123. {
  124. struct wm8739_state *state = to_state(sd);
  125. v4l2_info(sd, "Frequency: %u Hz\n", state->clock_freq);
  126. v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
  127. return 0;
  128. }
  129. /* ----------------------------------------------------------------------- */
  130. static const struct v4l2_ctrl_ops wm8739_ctrl_ops = {
  131. .s_ctrl = wm8739_s_ctrl,
  132. };
  133. static const struct v4l2_subdev_core_ops wm8739_core_ops = {
  134. .log_status = wm8739_log_status,
  135. };
  136. static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
  137. .s_clock_freq = wm8739_s_clock_freq,
  138. };
  139. static const struct v4l2_subdev_ops wm8739_ops = {
  140. .core = &wm8739_core_ops,
  141. .audio = &wm8739_audio_ops,
  142. };
  143. /* ------------------------------------------------------------------------ */
  144. /* i2c implementation */
  145. static int wm8739_probe(struct i2c_client *client,
  146. const struct i2c_device_id *id)
  147. {
  148. struct wm8739_state *state;
  149. struct v4l2_subdev *sd;
  150. /* Check if the adapter supports the needed features */
  151. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  152. return -EIO;
  153. v4l_info(client, "chip found @ 0x%x (%s)\n",
  154. client->addr << 1, client->adapter->name);
  155. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  156. if (state == NULL)
  157. return -ENOMEM;
  158. sd = &state->sd;
  159. v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
  160. v4l2_ctrl_handler_init(&state->hdl, 2);
  161. state->volume = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
  162. V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 50736);
  163. state->mute = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
  164. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  165. state->balance = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
  166. V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
  167. sd->ctrl_handler = &state->hdl;
  168. if (state->hdl.error) {
  169. int err = state->hdl.error;
  170. v4l2_ctrl_handler_free(&state->hdl);
  171. return err;
  172. }
  173. v4l2_ctrl_cluster(3, &state->volume);
  174. state->clock_freq = 48000;
  175. /* Initialize wm8739 */
  176. /* reset */
  177. wm8739_write(sd, R15, 0x00);
  178. /* filter setting, high path, offet clear */
  179. wm8739_write(sd, R5, 0x000);
  180. /* ADC, OSC, Power Off mode Disable */
  181. wm8739_write(sd, R6, 0x000);
  182. /* Digital Audio interface format:
  183. Enable Master mode, 24 bit, MSB first/left justified */
  184. wm8739_write(sd, R7, 0x049);
  185. /* sampling control: normal, 256fs, 48KHz sampling rate */
  186. wm8739_write(sd, R8, 0x000);
  187. /* activate */
  188. wm8739_write(sd, R9, 0x001);
  189. /* set volume/mute */
  190. v4l2_ctrl_handler_setup(&state->hdl);
  191. return 0;
  192. }
  193. static int wm8739_remove(struct i2c_client *client)
  194. {
  195. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  196. struct wm8739_state *state = to_state(sd);
  197. v4l2_device_unregister_subdev(sd);
  198. v4l2_ctrl_handler_free(&state->hdl);
  199. return 0;
  200. }
  201. static const struct i2c_device_id wm8739_id[] = {
  202. { "wm8739", 0 },
  203. { }
  204. };
  205. MODULE_DEVICE_TABLE(i2c, wm8739_id);
  206. static struct i2c_driver wm8739_driver = {
  207. .driver = {
  208. .name = "wm8739",
  209. },
  210. .probe = wm8739_probe,
  211. .remove = wm8739_remove,
  212. .id_table = wm8739_id,
  213. };
  214. module_i2c_driver(wm8739_driver);