/drivers/media/video/wm8739.c

https://bitbucket.org/abioy/linux · C · 345 lines · 237 code · 57 blank · 51 comment · 15 complexity · 4e048d27dfe53b96adf2172bd3e8e400 MD5 · raw file

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