wm8739.c 7.6 KB

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