wm8775.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * wm8775 - driver version 0.0.1
  3. *
  4. * Copyright (C) 2004 Ulf Eklund <ivtv at eklund.to>
  5. *
  6. * Based on saa7115 driver
  7. *
  8. * Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
  9. * - Cleanup
  10. * - V4L2 API update
  11. * - sound fixes
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include <linux/ioctl.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/i2c.h>
  32. #include <linux/i2c-id.h>
  33. #include <linux/videodev.h>
  34. #include <media/audiochip.h>
  35. MODULE_DESCRIPTION("wm8775 driver");
  36. MODULE_AUTHOR("Ulf Eklund, Hans Verkuil");
  37. MODULE_LICENSE("GPL");
  38. #define wm8775_err(fmt, arg...) do { \
  39. printk(KERN_ERR "%s %d-%04x: " fmt, client->driver->driver.name, \
  40. i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0)
  41. #define wm8775_info(fmt, arg...) do { \
  42. printk(KERN_INFO "%s %d-%04x: " fmt, client->driver->driver.name, \
  43. i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0)
  44. static unsigned short normal_i2c[] = { 0x36 >> 1, I2C_CLIENT_END };
  45. I2C_CLIENT_INSMOD;
  46. /* ----------------------------------------------------------------------- */
  47. enum {
  48. R7 = 7, R11 = 11,
  49. R12, R13, R14, R15, R16, R17, R18, R19, R20, R21, R23 = 23,
  50. TOT_REGS
  51. };
  52. struct wm8775_state {
  53. u8 input; /* Last selected input (0-0xf) */
  54. u8 muted;
  55. };
  56. static int wm8775_write(struct i2c_client *client, int reg, u16 val)
  57. {
  58. int i;
  59. if (reg < 0 || reg >= TOT_REGS) {
  60. wm8775_err("Invalid register R%d\n", reg);
  61. return -1;
  62. }
  63. for (i = 0; i < 3; i++) {
  64. if (i2c_smbus_write_byte_data(client, (reg << 1) |
  65. (val >> 8), val & 0xff) == 0) {
  66. return 0;
  67. }
  68. }
  69. wm8775_err("I2C: cannot write %03x to register R%d\n", val, reg);
  70. return -1;
  71. }
  72. static int wm8775_command(struct i2c_client *client, unsigned int cmd,
  73. void *arg)
  74. {
  75. struct wm8775_state *state = i2c_get_clientdata(client);
  76. int *input = arg;
  77. switch (cmd) {
  78. case AUDC_SET_INPUT:
  79. wm8775_write(client, R21, 0x0c0);
  80. wm8775_write(client, R14, 0x1d4);
  81. wm8775_write(client, R15, 0x1d4);
  82. if (*input == AUDIO_RADIO) {
  83. wm8775_write(client, R21, 0x108);
  84. state->input = 8;
  85. state->muted = 0;
  86. break;
  87. }
  88. if (*input == AUDIO_MUTE) {
  89. state->muted = 1;
  90. break;
  91. }
  92. if (*input == AUDIO_UNMUTE) {
  93. wm8775_write(client, R21, 0x100 + state->input);
  94. state->muted = 0;
  95. break;
  96. }
  97. /* All other inputs... */
  98. wm8775_write(client, R21, 0x102);
  99. state->input = 2;
  100. state->muted = 0;
  101. break;
  102. case VIDIOC_LOG_STATUS:
  103. wm8775_info("Input: %s%s\n",
  104. state->input == 8 ? "radio" : "default",
  105. state->muted ? " (muted)" : "");
  106. break;
  107. case VIDIOC_S_FREQUENCY:
  108. /* If I remove this, then it can happen that I have no
  109. sound the first time I tune from static to a valid channel.
  110. It's difficult to reproduce and is almost certainly related
  111. to the zero cross detect circuit. */
  112. wm8775_write(client, R21, 0x0c0);
  113. wm8775_write(client, R14, 0x1d4);
  114. wm8775_write(client, R15, 0x1d4);
  115. wm8775_write(client, R21, 0x100 + state->input);
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. /* ----------------------------------------------------------------------- */
  123. /* i2c implementation */
  124. /*
  125. * Generic i2c probe
  126. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  127. */
  128. static struct i2c_driver i2c_driver;
  129. static int wm8775_attach(struct i2c_adapter *adapter, int address, int kind)
  130. {
  131. struct i2c_client *client;
  132. struct wm8775_state *state;
  133. /* Check if the adapter supports the needed features */
  134. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  135. return 0;
  136. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  137. if (client == 0)
  138. return -ENOMEM;
  139. memset(client, 0, sizeof(struct i2c_client));
  140. client->addr = address;
  141. client->adapter = adapter;
  142. client->driver = &i2c_driver;
  143. snprintf(client->name, sizeof(client->name) - 1, "wm8775");
  144. wm8775_info("chip found @ 0x%x (%s)\n", address << 1, adapter->name);
  145. state = kmalloc(sizeof(struct wm8775_state), GFP_KERNEL);
  146. if (state == NULL) {
  147. kfree(client);
  148. return -ENOMEM;
  149. }
  150. state->input = 2;
  151. state->muted = 0;
  152. i2c_set_clientdata(client, state);
  153. /* initialize wm8775 */
  154. wm8775_write(client, R23, 0x000); /* RESET */
  155. wm8775_write(client, R7, 0x000); /* Disable zero cross detect timeout */
  156. wm8775_write(client, R11, 0x021); /* Left justified, 24-bit mode */
  157. wm8775_write(client, R12, 0x102); /* Master mode, clock ratio 256fs */
  158. wm8775_write(client, R13, 0x000); /* Powered up */
  159. wm8775_write(client, R14, 0x1d4); /* ADC gain +2.5dB, enable zero cross */
  160. wm8775_write(client, R15, 0x1d4); /* ADC gain +2.5dB, enable zero cross */
  161. wm8775_write(client, R16, 0x1bf); /* ALC Stereo, ALC target level -1dB FS */
  162. /* max gain +8dB */
  163. wm8775_write(client, R17, 0x185); /* Enable gain control, use zero cross */
  164. /* detection, ALC hold time 42.6 ms */
  165. wm8775_write(client, R18, 0x0a2); /* ALC gain ramp up delay 34 s, */
  166. /* ALC gain ramp down delay 33 ms */
  167. wm8775_write(client, R19, 0x005); /* Enable noise gate, threshold -72dBfs */
  168. wm8775_write(client, R20, 0x07a); /* Transient window 4ms, lower PGA gain */
  169. /* limit -1dB */
  170. wm8775_write(client, R21, 0x102); /* LRBOTH = 1, use input 2. */
  171. i2c_attach_client(client);
  172. return 0;
  173. }
  174. static int wm8775_probe(struct i2c_adapter *adapter)
  175. {
  176. if (adapter->class & I2C_CLASS_TV_ANALOG)
  177. return i2c_probe(adapter, &addr_data, wm8775_attach);
  178. return 0;
  179. }
  180. static int wm8775_detach(struct i2c_client *client)
  181. {
  182. int err;
  183. err = i2c_detach_client(client);
  184. if (err) {
  185. return err;
  186. }
  187. kfree(client);
  188. return 0;
  189. }
  190. /* ----------------------------------------------------------------------- */
  191. /* i2c implementation */
  192. static struct i2c_driver i2c_driver = {
  193. .driver = {
  194. .name = "wm8775",
  195. },
  196. .id = I2C_DRIVERID_WM8775,
  197. .attach_adapter = wm8775_probe,
  198. .detach_client = wm8775_detach,
  199. .command = wm8775_command,
  200. };
  201. static int __init wm8775_init_module(void)
  202. {
  203. return i2c_add_driver(&i2c_driver);
  204. }
  205. static void __exit wm8775_cleanup_module(void)
  206. {
  207. i2c_del_driver(&i2c_driver);
  208. }
  209. module_init(wm8775_init_module);
  210. module_exit(wm8775_cleanup_module);