dac3550a.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Driver for the i2c/i2s based DAC3550a sound chip used
  3. * on some Apple iBooks. Also known as "DACA".
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file COPYING in the main directory of this archive
  7. * for more details.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/delay.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/ioport.h>
  14. #include <linux/sysctl.h>
  15. #include <linux/types.h>
  16. #include <linux/i2c.h>
  17. #include <linux/init.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/errno.h>
  20. #include <asm/io.h>
  21. #include "dmasound.h"
  22. /* FYI: This code was derived from the tas3001c.c Texas/Tumbler mixer
  23. * control code, as well as info derived from the AppleDACAAudio driver
  24. * from Darwin CVS (main thing I derived being register numbers and
  25. * values, as well as when to make the calls). */
  26. #define I2C_DRIVERID_DACA (0xFDCB)
  27. #define DACA_VERSION "0.1"
  28. #define DACA_DATE "20010930"
  29. static int cur_left_vol;
  30. static int cur_right_vol;
  31. static struct i2c_client *daca_client;
  32. static int daca_attach_adapter(struct i2c_adapter *adapter);
  33. static int daca_detect_client(struct i2c_adapter *adapter, int address);
  34. static int daca_detach_client(struct i2c_client *client);
  35. struct i2c_driver daca_driver = {
  36. .driver = {
  37. .name = "DAC3550A driver V " DACA_VERSION,
  38. },
  39. .id = I2C_DRIVERID_DACA,
  40. .attach_adapter = daca_attach_adapter,
  41. .detach_client = daca_detach_client,
  42. };
  43. #define VOL_MAX ((1<<20) - 1)
  44. void daca_get_volume(uint * left_vol, uint *right_vol)
  45. {
  46. *left_vol = cur_left_vol >> 5;
  47. *right_vol = cur_right_vol >> 5;
  48. }
  49. int daca_set_volume(uint left_vol, uint right_vol)
  50. {
  51. unsigned short voldata;
  52. if (!daca_client)
  53. return -1;
  54. /* Derived from experience, not from any specific values */
  55. left_vol <<= 5;
  56. right_vol <<= 5;
  57. if (left_vol > VOL_MAX)
  58. left_vol = VOL_MAX;
  59. if (right_vol > VOL_MAX)
  60. right_vol = VOL_MAX;
  61. voldata = ((left_vol >> 14) & 0x3f) << 8;
  62. voldata |= (right_vol >> 14) & 0x3f;
  63. if (i2c_smbus_write_word_data(daca_client, 2, voldata) < 0) {
  64. printk("daca: failed to set volume \n");
  65. return -1;
  66. }
  67. cur_left_vol = left_vol;
  68. cur_right_vol = right_vol;
  69. return 0;
  70. }
  71. int daca_leave_sleep(void)
  72. {
  73. if (!daca_client)
  74. return -1;
  75. /* Do a short sleep, just to make sure I2C bus is awake and paying
  76. * attention to us
  77. */
  78. msleep(20);
  79. /* Write the sample rate reg the value it needs */
  80. i2c_smbus_write_byte_data(daca_client, 1, 8);
  81. daca_set_volume(cur_left_vol >> 5, cur_right_vol >> 5);
  82. /* Another short delay, just to make sure the other I2C bus writes
  83. * have taken...
  84. */
  85. msleep(20);
  86. /* Write the global config reg - invert right power amp,
  87. * DAC on, use 5-volt mode */
  88. i2c_smbus_write_byte_data(daca_client, 3, 0x45);
  89. return 0;
  90. }
  91. int daca_enter_sleep(void)
  92. {
  93. if (!daca_client)
  94. return -1;
  95. i2c_smbus_write_byte_data(daca_client, 1, 8);
  96. daca_set_volume(cur_left_vol >> 5, cur_right_vol >> 5);
  97. /* Write the global config reg - invert right power amp,
  98. * DAC on, enter low-power mode, use 5-volt mode
  99. */
  100. i2c_smbus_write_byte_data(daca_client, 3, 0x65);
  101. return 0;
  102. }
  103. static int daca_attach_adapter(struct i2c_adapter *adapter)
  104. {
  105. if (!strncmp(adapter->name, "mac-io", 6))
  106. daca_detect_client(adapter, 0x4d);
  107. return 0;
  108. }
  109. static int daca_init_client(struct i2c_client * new_client)
  110. {
  111. /*
  112. * Probe is not working with the current i2c-keywest
  113. * driver. We try to use addr 0x4d on each adapters
  114. * instead, by setting the format register.
  115. *
  116. * FIXME: I'm sure that can be obtained from the
  117. * device-tree. --BenH.
  118. */
  119. /* Write the global config reg - invert right power amp,
  120. * DAC on, use 5-volt mode
  121. */
  122. if (i2c_smbus_write_byte_data(new_client, 3, 0x45))
  123. return -1;
  124. i2c_smbus_write_byte_data(new_client, 1, 8);
  125. daca_client = new_client;
  126. daca_set_volume(15000, 15000);
  127. return 0;
  128. }
  129. static int daca_detect_client(struct i2c_adapter *adapter, int address)
  130. {
  131. const char *client_name = "DAC 3550A Digital Equalizer";
  132. struct i2c_client *new_client;
  133. int rc = -ENODEV;
  134. new_client = kmalloc(sizeof(*new_client), GFP_KERNEL);
  135. if (!new_client)
  136. return -ENOMEM;
  137. memset(new_client, 0, sizeof(*new_client));
  138. new_client->addr = address;
  139. new_client->adapter = adapter;
  140. new_client->driver = &daca_driver;
  141. new_client->flags = 0;
  142. strcpy(new_client->name, client_name);
  143. if (daca_init_client(new_client))
  144. goto bail;
  145. /* Tell the i2c layer a new client has arrived */
  146. if (i2c_attach_client(new_client))
  147. goto bail;
  148. return 0;
  149. bail:
  150. kfree(new_client);
  151. return rc;
  152. }
  153. static int daca_detach_client(struct i2c_client *client)
  154. {
  155. if (client == daca_client)
  156. daca_client = NULL;
  157. i2c_detach_client(client);
  158. kfree(client);
  159. return 0;
  160. }
  161. void daca_cleanup(void)
  162. {
  163. i2c_del_driver(&daca_driver);
  164. }
  165. int daca_init(void)
  166. {
  167. printk("dac3550a driver version %s (%s)\n",DACA_VERSION,DACA_DATE);
  168. return i2c_add_driver(&daca_driver);
  169. }