wm8739.c 6.8 KB

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