dac3550a.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. .owner = THIS_MODULE,
  38. .name = "DAC3550A driver V " DACA_VERSION,
  39. },
  40. .id = I2C_DRIVERID_DACA,
  41. .attach_adapter = daca_attach_adapter,
  42. .detach_client = daca_detach_client,
  43. };
  44. #define VOL_MAX ((1<<20) - 1)
  45. void daca_get_volume(uint * left_vol, uint *right_vol)
  46. {
  47. *left_vol = cur_left_vol >> 5;
  48. *right_vol = cur_right_vol >> 5;
  49. }
  50. int daca_set_volume(uint left_vol, uint right_vol)
  51. {
  52. unsigned short voldata;
  53. if (!daca_client)
  54. return -1;
  55. /* Derived from experience, not from any specific values */
  56. left_vol <<= 5;
  57. right_vol <<= 5;
  58. if (left_vol > VOL_MAX)
  59. left_vol = VOL_MAX;
  60. if (right_vol > VOL_MAX)
  61. right_vol = VOL_MAX;
  62. voldata = ((left_vol >> 14) & 0x3f) << 8;
  63. voldata |= (right_vol >> 14) & 0x3f;
  64. if (i2c_smbus_write_word_data(daca_client, 2, voldata) < 0) {
  65. printk("daca: failed to set volume \n");
  66. return -1;
  67. }
  68. cur_left_vol = left_vol;
  69. cur_right_vol = right_vol;
  70. return 0;
  71. }
  72. int daca_leave_sleep(void)
  73. {
  74. if (!daca_client)
  75. return -1;
  76. /* Do a short sleep, just to make sure I2C bus is awake and paying
  77. * attention to us
  78. */
  79. msleep(20);
  80. /* Write the sample rate reg the value it needs */
  81. i2c_smbus_write_byte_data(daca_client, 1, 8);
  82. daca_set_volume(cur_left_vol >> 5, cur_right_vol >> 5);
  83. /* Another short delay, just to make sure the other I2C bus writes
  84. * have taken...
  85. */
  86. msleep(20);
  87. /* Write the global config reg - invert right power amp,
  88. * DAC on, use 5-volt mode */
  89. i2c_smbus_write_byte_data(daca_client, 3, 0x45);
  90. return 0;
  91. }
  92. int daca_enter_sleep(void)
  93. {
  94. if (!daca_client)
  95. return -1;
  96. i2c_smbus_write_byte_data(daca_client, 1, 8);
  97. daca_set_volume(cur_left_vol >> 5, cur_right_vol >> 5);
  98. /* Write the global config reg - invert right power amp,
  99. * DAC on, enter low-power mode, use 5-volt mode
  100. */
  101. i2c_smbus_write_byte_data(daca_client, 3, 0x65);
  102. return 0;
  103. }
  104. static int daca_attach_adapter(struct i2c_adapter *adapter)
  105. {
  106. if (!strncmp(adapter->name, "mac-io", 6))
  107. daca_detect_client(adapter, 0x4d);
  108. return 0;
  109. }
  110. static int daca_init_client(struct i2c_client * new_client)
  111. {
  112. /*
  113. * Probe is not working with the current i2c-keywest
  114. * driver. We try to use addr 0x4d on each adapters
  115. * instead, by setting the format register.
  116. *
  117. * FIXME: I'm sure that can be obtained from the
  118. * device-tree. --BenH.
  119. */
  120. /* Write the global config reg - invert right power amp,
  121. * DAC on, use 5-volt mode
  122. */
  123. if (i2c_smbus_write_byte_data(new_client, 3, 0x45))
  124. return -1;
  125. i2c_smbus_write_byte_data(new_client, 1, 8);
  126. daca_client = new_client;
  127. daca_set_volume(15000, 15000);
  128. return 0;
  129. }
  130. static int daca_detect_client(struct i2c_adapter *adapter, int address)
  131. {
  132. const char *client_name = "DAC 3550A Digital Equalizer";
  133. struct i2c_client *new_client;
  134. int rc = -ENODEV;
  135. new_client = kmalloc(sizeof(*new_client), GFP_KERNEL);
  136. if (!new_client)
  137. return -ENOMEM;
  138. memset(new_client, 0, sizeof(*new_client));
  139. new_client->addr = address;
  140. new_client->adapter = adapter;
  141. new_client->driver = &daca_driver;
  142. new_client->flags = 0;
  143. strcpy(new_client->name, client_name);
  144. if (daca_init_client(new_client))
  145. goto bail;
  146. /* Tell the i2c layer a new client has arrived */
  147. if (i2c_attach_client(new_client))
  148. goto bail;
  149. return 0;
  150. bail:
  151. kfree(new_client);
  152. return rc;
  153. }
  154. static int daca_detach_client(struct i2c_client *client)
  155. {
  156. if (client == daca_client)
  157. daca_client = NULL;
  158. i2c_detach_client(client);
  159. kfree(client);
  160. return 0;
  161. }
  162. void daca_cleanup(void)
  163. {
  164. i2c_del_driver(&daca_driver);
  165. }
  166. int daca_init(void)
  167. {
  168. printk("dac3550a driver version %s (%s)\n",DACA_VERSION,DACA_DATE);
  169. return i2c_add_driver(&daca_driver);
  170. }