wm8739.c 8.5 KB

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