wm8739.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/ioctl.h>
  26. #include <asm/uaccess.h>
  27. #include <linux/i2c.h>
  28. #include <linux/i2c-id.h>
  29. #include <linux/videodev.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-chip-ident.h>
  32. #include <media/v4l2-i2c-drv-legacy.h>
  33. MODULE_DESCRIPTION("wm8739 driver");
  34. MODULE_AUTHOR("T. Adachi, Hans Verkuil");
  35. MODULE_LICENSE("GPL");
  36. static int debug = 0;
  37. static unsigned short normal_i2c[] = { 0x34 >> 1, 0x36 >> 1, I2C_CLIENT_END };
  38. module_param(debug, int, 0644);
  39. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  40. I2C_CLIENT_INSMOD;
  41. /* ------------------------------------------------------------------------ */
  42. enum {
  43. R0 = 0, R1,
  44. R5 = 5, R6, R7, R8, R9, R15 = 15,
  45. TOT_REGS
  46. };
  47. struct wm8739_state {
  48. u32 clock_freq;
  49. u8 muted;
  50. u16 volume;
  51. u16 balance;
  52. u8 vol_l; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
  53. u8 vol_r; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
  54. };
  55. /* ------------------------------------------------------------------------ */
  56. static int wm8739_write(struct i2c_client *client, int reg, u16 val)
  57. {
  58. int i;
  59. if (reg < 0 || reg >= TOT_REGS) {
  60. v4l_err(client, "Invalid register R%d\n", reg);
  61. return -1;
  62. }
  63. v4l_dbg(1, debug, client, "write: %02x %02x\n", reg, val);
  64. for (i = 0; i < 3; i++) {
  65. if (i2c_smbus_write_byte_data(client, (reg << 1) |
  66. (val >> 8), val & 0xff) == 0) {
  67. return 0;
  68. }
  69. }
  70. v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg);
  71. return -1;
  72. }
  73. /* write regs to set audio volume etc */
  74. static void wm8739_set_audio(struct i2c_client *client)
  75. {
  76. struct wm8739_state *state = i2c_get_clientdata(client);
  77. u16 mute = state->muted ? 0x80 : 0;
  78. /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
  79. * Default setting: 0x17 = 0 dB
  80. */
  81. wm8739_write(client, R0, (state->vol_l & 0x1f) | mute);
  82. wm8739_write(client, R1, (state->vol_r & 0x1f) | mute);
  83. }
  84. static int wm8739_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  85. {
  86. struct wm8739_state *state = i2c_get_clientdata(client);
  87. switch (ctrl->id) {
  88. case V4L2_CID_AUDIO_MUTE:
  89. ctrl->value = state->muted;
  90. break;
  91. case V4L2_CID_AUDIO_VOLUME:
  92. ctrl->value = state->volume;
  93. break;
  94. case V4L2_CID_AUDIO_BALANCE:
  95. ctrl->value = state->balance;
  96. break;
  97. default:
  98. return -EINVAL;
  99. }
  100. return 0;
  101. }
  102. static int wm8739_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  103. {
  104. struct wm8739_state *state = i2c_get_clientdata(client);
  105. unsigned int work_l, work_r;
  106. switch (ctrl->id) {
  107. case V4L2_CID_AUDIO_MUTE:
  108. state->muted = ctrl->value;
  109. break;
  110. case V4L2_CID_AUDIO_VOLUME:
  111. state->volume = ctrl->value;
  112. break;
  113. case V4L2_CID_AUDIO_BALANCE:
  114. state->balance = ctrl->value;
  115. break;
  116. default:
  117. return -EINVAL;
  118. }
  119. /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */
  120. work_l = (min(65536 - state->balance, 32768) * state->volume) / 32768;
  121. work_r = (min(state->balance, (u16)32768) * state->volume) / 32768;
  122. state->vol_l = (long)work_l * 31 / 65535;
  123. state->vol_r = (long)work_r * 31 / 65535;
  124. /* set audio volume etc. */
  125. wm8739_set_audio(client);
  126. return 0;
  127. }
  128. /* ------------------------------------------------------------------------ */
  129. static struct v4l2_queryctrl wm8739_qctrl[] = {
  130. {
  131. .id = V4L2_CID_AUDIO_VOLUME,
  132. .name = "Volume",
  133. .minimum = 0,
  134. .maximum = 65535,
  135. .step = 65535/100,
  136. .default_value = 58880,
  137. .flags = 0,
  138. .type = V4L2_CTRL_TYPE_INTEGER,
  139. },{
  140. .id = V4L2_CID_AUDIO_MUTE,
  141. .name = "Mute",
  142. .minimum = 0,
  143. .maximum = 1,
  144. .step = 1,
  145. .default_value = 1,
  146. .flags = 0,
  147. .type = V4L2_CTRL_TYPE_BOOLEAN,
  148. },{
  149. .id = V4L2_CID_AUDIO_BALANCE,
  150. .name = "Balance",
  151. .minimum = 0,
  152. .maximum = 65535,
  153. .step = 65535/100,
  154. .default_value = 32768,
  155. .flags = 0,
  156. .type = V4L2_CTRL_TYPE_INTEGER,
  157. }
  158. };
  159. /* ------------------------------------------------------------------------ */
  160. static int wm8739_command(struct i2c_client *client, unsigned int cmd, void *arg)
  161. {
  162. struct wm8739_state *state = i2c_get_clientdata(client);
  163. switch (cmd) {
  164. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  165. {
  166. u32 audiofreq = *(u32 *)arg;
  167. state->clock_freq = audiofreq;
  168. wm8739_write(client, R9, 0x000); /* de-activate */
  169. switch (audiofreq) {
  170. case 44100:
  171. wm8739_write(client, R8, 0x020); /* 256fps, fs=44.1k */
  172. break;
  173. case 48000:
  174. wm8739_write(client, R8, 0x000); /* 256fps, fs=48k */
  175. break;
  176. case 32000:
  177. wm8739_write(client, R8, 0x018); /* 256fps, fs=32k */
  178. break;
  179. default:
  180. break;
  181. }
  182. wm8739_write(client, R9, 0x001); /* activate */
  183. break;
  184. }
  185. case VIDIOC_G_CTRL:
  186. return wm8739_get_ctrl(client, arg);
  187. case VIDIOC_S_CTRL:
  188. return wm8739_set_ctrl(client, arg);
  189. case VIDIOC_QUERYCTRL:
  190. {
  191. struct v4l2_queryctrl *qc = arg;
  192. int i;
  193. for (i = 0; i < ARRAY_SIZE(wm8739_qctrl); i++)
  194. if (qc->id && qc->id == wm8739_qctrl[i].id) {
  195. memcpy(qc, &wm8739_qctrl[i], sizeof(*qc));
  196. return 0;
  197. }
  198. return -EINVAL;
  199. }
  200. case VIDIOC_G_CHIP_IDENT:
  201. return v4l2_chip_ident_i2c_client(client, arg, V4L2_IDENT_WM8739, 0);
  202. case VIDIOC_LOG_STATUS:
  203. v4l_info(client, "Frequency: %u Hz\n", state->clock_freq);
  204. v4l_info(client, "Volume L: %02x%s\n", state->vol_l & 0x1f,
  205. state->muted ? " (muted)" : "");
  206. v4l_info(client, "Volume R: %02x%s\n", state->vol_r & 0x1f,
  207. state->muted ? " (muted)" : "");
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. return 0;
  213. }
  214. /* ------------------------------------------------------------------------ */
  215. /* i2c implementation */
  216. static int wm8739_probe(struct i2c_client *client)
  217. {
  218. struct wm8739_state *state;
  219. v4l_info(client, "chip found @ 0x%x (%s)\n", client->addr << 1, client->adapter->name);
  220. state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL);
  221. if (state == NULL) {
  222. kfree(client);
  223. return -ENOMEM;
  224. }
  225. state->vol_l = 0x17; /* 0dB */
  226. state->vol_r = 0x17; /* 0dB */
  227. state->muted = 0;
  228. state->balance = 32768;
  229. /* normalize (12dB(31) to -34.5dB(0) [0dB(23)] -> 65535 to 0) */
  230. state->volume = ((long)state->vol_l + 1) * 65535 / 31;
  231. state->clock_freq = 48000;
  232. i2c_set_clientdata(client, state);
  233. /* initialize wm8739 */
  234. wm8739_write(client, R15, 0x00); /* reset */
  235. wm8739_write(client, R5, 0x000); /* filter setting, high path, offet clear */
  236. wm8739_write(client, R6, 0x000); /* ADC, OSC, Power Off mode Disable */
  237. wm8739_write(client, R7, 0x049); /* Digital Audio interface format */
  238. /* Enable Master mode */
  239. /* 24 bit, MSB first/left justified */
  240. wm8739_write(client, R8, 0x000); /* sampling control */
  241. /* normal, 256fs, 48KHz sampling rate */
  242. wm8739_write(client, R9, 0x001); /* activate */
  243. wm8739_set_audio(client); /* set volume/mute */
  244. return 0;
  245. }
  246. static int wm8739_remove(struct i2c_client *client)
  247. {
  248. kfree(i2c_get_clientdata(client));
  249. return 0;
  250. }
  251. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  252. .name = "wm8739",
  253. .driverid = I2C_DRIVERID_WM8739,
  254. .command = wm8739_command,
  255. .probe = wm8739_probe,
  256. .remove = wm8739_remove,
  257. };