wm8739.c 7.8 KB

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