wm8739.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. MODULE_DESCRIPTION("wm8739 driver");
  32. MODULE_AUTHOR("T. Adachi, Hans Verkuil");
  33. MODULE_LICENSE("GPL");
  34. static int debug = 0;
  35. static unsigned short normal_i2c[] = { 0x34 >> 1, 0x36 >> 1, I2C_CLIENT_END };
  36. module_param(debug, int, 0644);
  37. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  38. I2C_CLIENT_INSMOD;
  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, (reg << 1) |
  64. (val >> 8), val & 0xff) == 0) {
  65. return 0;
  66. }
  67. }
  68. v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg);
  69. return -1;
  70. }
  71. /* write regs to set audio volume etc */
  72. static void wm8739_set_audio(struct i2c_client *client)
  73. {
  74. struct wm8739_state *state = i2c_get_clientdata(client);
  75. u16 mute = state->muted ? 0x80 : 0;
  76. /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
  77. * Default setting: 0x17 = 0 dB
  78. */
  79. wm8739_write(client, R0, (state->vol_l & 0x1f) | mute);
  80. wm8739_write(client, R1, (state->vol_r & 0x1f) | mute);
  81. }
  82. static int wm8739_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  83. {
  84. struct wm8739_state *state = i2c_get_clientdata(client);
  85. switch (ctrl->id) {
  86. case V4L2_CID_AUDIO_MUTE:
  87. ctrl->value = state->muted;
  88. break;
  89. case V4L2_CID_AUDIO_VOLUME:
  90. ctrl->value = state->volume;
  91. break;
  92. case V4L2_CID_AUDIO_BALANCE:
  93. ctrl->value = state->balance;
  94. break;
  95. default:
  96. return -EINVAL;
  97. }
  98. return 0;
  99. }
  100. static int wm8739_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  101. {
  102. struct wm8739_state *state = i2c_get_clientdata(client);
  103. unsigned int work_l, work_r;
  104. switch (ctrl->id) {
  105. case V4L2_CID_AUDIO_MUTE:
  106. state->muted = ctrl->value;
  107. break;
  108. case V4L2_CID_AUDIO_VOLUME:
  109. state->volume = ctrl->value;
  110. break;
  111. case V4L2_CID_AUDIO_BALANCE:
  112. state->balance = ctrl->value;
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */
  118. work_l = (min(65536 - state->balance, 32768) * state->volume) / 32768;
  119. work_r = (min(state->balance, (u16)32768) * state->volume) / 32768;
  120. state->vol_l = (long)work_l * 31 / 65535;
  121. state->vol_r = (long)work_r * 31 / 65535;
  122. /* set audio volume etc. */
  123. wm8739_set_audio(client);
  124. return 0;
  125. }
  126. /* ------------------------------------------------------------------------ */
  127. static struct v4l2_queryctrl wm8739_qctrl[] = {
  128. {
  129. .id = V4L2_CID_AUDIO_VOLUME,
  130. .name = "Volume",
  131. .minimum = 0,
  132. .maximum = 65535,
  133. .step = 65535/100,
  134. .default_value = 58880,
  135. .flags = 0,
  136. .type = V4L2_CTRL_TYPE_INTEGER,
  137. },{
  138. .id = V4L2_CID_AUDIO_MUTE,
  139. .name = "Mute",
  140. .minimum = 0,
  141. .maximum = 1,
  142. .step = 1,
  143. .default_value = 1,
  144. .flags = 0,
  145. .type = V4L2_CTRL_TYPE_BOOLEAN,
  146. },{
  147. .id = V4L2_CID_AUDIO_BALANCE,
  148. .name = "Balance",
  149. .minimum = 0,
  150. .maximum = 65535,
  151. .step = 65535/100,
  152. .default_value = 32768,
  153. .flags = 0,
  154. .type = V4L2_CTRL_TYPE_INTEGER,
  155. }
  156. };
  157. /* ------------------------------------------------------------------------ */
  158. static int wm8739_command(struct i2c_client *client, unsigned int cmd, void *arg)
  159. {
  160. struct wm8739_state *state = i2c_get_clientdata(client);
  161. switch (cmd) {
  162. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  163. {
  164. u32 audiofreq = *(u32 *)arg;
  165. state->clock_freq = audiofreq;
  166. wm8739_write(client, R9, 0x000); /* de-activate */
  167. switch (audiofreq) {
  168. case 44100:
  169. wm8739_write(client, R8, 0x020); /* 256fps, fs=44.1k */
  170. break;
  171. case 48000:
  172. wm8739_write(client, R8, 0x000); /* 256fps, fs=48k */
  173. break;
  174. case 32000:
  175. wm8739_write(client, R8, 0x018); /* 256fps, fs=32k */
  176. break;
  177. default:
  178. break;
  179. }
  180. wm8739_write(client, R9, 0x001); /* activate */
  181. break;
  182. }
  183. case VIDIOC_G_CTRL:
  184. return wm8739_get_ctrl(client, arg);
  185. case VIDIOC_S_CTRL:
  186. return wm8739_set_ctrl(client, arg);
  187. case VIDIOC_QUERYCTRL:
  188. {
  189. struct v4l2_queryctrl *qc = arg;
  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. case VIDIOC_LOG_STATUS:
  199. v4l_info(client, "Frequency: %u Hz\n", state->clock_freq);
  200. v4l_info(client, "Volume L: %02x%s\n", state->vol_l & 0x1f,
  201. state->muted ? " (muted)" : "");
  202. v4l_info(client, "Volume R: %02x%s\n", state->vol_r & 0x1f,
  203. state->muted ? " (muted)" : "");
  204. break;
  205. default:
  206. return -EINVAL;
  207. }
  208. return 0;
  209. }
  210. /* ------------------------------------------------------------------------ */
  211. /* i2c implementation */
  212. static struct i2c_driver i2c_driver;
  213. static int wm8739_attach(struct i2c_adapter *adapter, int address, int kind)
  214. {
  215. struct i2c_client *client;
  216. struct wm8739_state *state;
  217. /* Check if the adapter supports the needed features */
  218. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  219. return 0;
  220. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  221. if (client == NULL)
  222. return -ENOMEM;
  223. client->addr = address;
  224. client->adapter = adapter;
  225. client->driver = &i2c_driver;
  226. snprintf(client->name, sizeof(client->name) - 1, "wm8739");
  227. v4l_info(client, "chip found @ 0x%x (%s)\n", address << 1, adapter->name);
  228. state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL);
  229. if (state == NULL) {
  230. kfree(client);
  231. return -ENOMEM;
  232. }
  233. state->vol_l = 0x17; /* 0dB */
  234. state->vol_r = 0x17; /* 0dB */
  235. state->muted = 0;
  236. state->balance = 32768;
  237. /* normalize (12dB(31) to -34.5dB(0) [0dB(23)] -> 65535 to 0) */
  238. state->volume = ((long)state->vol_l + 1) * 65535 / 31;
  239. state->clock_freq = 48000;
  240. i2c_set_clientdata(client, state);
  241. /* initialize wm8739 */
  242. wm8739_write(client, R15, 0x00); /* reset */
  243. wm8739_write(client, R5, 0x000); /* filter setting, high path, offet clear */
  244. wm8739_write(client, R6, 0x000); /* ADC, OSC, Power Off mode Disable */
  245. wm8739_write(client, R7, 0x049); /* Digital Audio interface format */
  246. /* Enable Master mode */
  247. /* 24 bit, MSB first/left justified */
  248. wm8739_write(client, R8, 0x000); /* sampling control */
  249. /* normal, 256fs, 48KHz sampling rate */
  250. wm8739_write(client, R9, 0x001); /* activate */
  251. wm8739_set_audio(client); /* set volume/mute */
  252. i2c_attach_client(client);
  253. return 0;
  254. }
  255. static int wm8739_probe(struct i2c_adapter *adapter)
  256. {
  257. if (adapter->class & I2C_CLASS_TV_ANALOG)
  258. return i2c_probe(adapter, &addr_data, wm8739_attach);
  259. return 0;
  260. }
  261. static int wm8739_detach(struct i2c_client *client)
  262. {
  263. int err;
  264. err = i2c_detach_client(client);
  265. if (err)
  266. return err;
  267. kfree(client);
  268. return 0;
  269. }
  270. /* ----------------------------------------------------------------------- */
  271. /* i2c implementation */
  272. static struct i2c_driver i2c_driver = {
  273. .driver = {
  274. .name = "wm8739",
  275. },
  276. .id = I2C_DRIVERID_WM8739,
  277. .attach_adapter = wm8739_probe,
  278. .detach_client = wm8739_detach,
  279. .command = wm8739_command,
  280. };
  281. static int __init wm8739_init_module(void)
  282. {
  283. return i2c_add_driver(&i2c_driver);
  284. }
  285. static void __exit wm8739_cleanup_module(void)
  286. {
  287. i2c_del_driver(&i2c_driver);
  288. }
  289. module_init(wm8739_init_module);
  290. module_exit(wm8739_cleanup_module);