keywest.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * common keywest i2c layer
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/i2c.h>
  22. #include <linux/delay.h>
  23. #include <sound/core.h>
  24. #include "pmac.h"
  25. /*
  26. * we have to keep a static variable here since i2c attach_adapter
  27. * callback cannot pass a private data.
  28. */
  29. static struct pmac_keywest *keywest_ctx;
  30. static bool keywest_probed;
  31. static int keywest_probe(struct i2c_client *client,
  32. const struct i2c_device_id *id)
  33. {
  34. keywest_probed = true;
  35. /* If instantiated via i2c-powermac, we still need to set the client */
  36. if (!keywest_ctx->client)
  37. keywest_ctx->client = client;
  38. i2c_set_clientdata(client, keywest_ctx);
  39. return 0;
  40. }
  41. /*
  42. * This is kind of a hack, best would be to turn powermac to fixed i2c
  43. * bus numbers and declare the sound device as part of platform
  44. * initialization
  45. */
  46. static int keywest_attach_adapter(struct i2c_adapter *adapter)
  47. {
  48. struct i2c_board_info info;
  49. if (! keywest_ctx)
  50. return -EINVAL;
  51. if (strncmp(adapter->name, "mac-io", 6))
  52. return -EINVAL; /* ignored */
  53. memset(&info, 0, sizeof(struct i2c_board_info));
  54. strlcpy(info.type, "keywest", I2C_NAME_SIZE);
  55. info.addr = keywest_ctx->addr;
  56. keywest_ctx->client = i2c_new_device(adapter, &info);
  57. if (!keywest_ctx->client)
  58. return -ENODEV;
  59. /*
  60. * We know the driver is already loaded, so the device should be
  61. * already bound. If not it means binding failed, and then there
  62. * is no point in keeping the device instantiated.
  63. */
  64. if (!keywest_ctx->client->dev.driver) {
  65. i2c_unregister_device(keywest_ctx->client);
  66. keywest_ctx->client = NULL;
  67. return -ENODEV;
  68. }
  69. /*
  70. * Let i2c-core delete that device on driver removal.
  71. * This is safe because i2c-core holds the core_lock mutex for us.
  72. */
  73. list_add_tail(&keywest_ctx->client->detected,
  74. &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
  75. return 0;
  76. }
  77. static int keywest_remove(struct i2c_client *client)
  78. {
  79. if (! keywest_ctx)
  80. return 0;
  81. if (client == keywest_ctx->client)
  82. keywest_ctx->client = NULL;
  83. return 0;
  84. }
  85. static const struct i2c_device_id keywest_i2c_id[] = {
  86. { "MAC,tas3004", 0 }, /* instantiated by i2c-powermac */
  87. { "keywest", 0 }, /* instantiated by us if needed */
  88. { }
  89. };
  90. static struct i2c_driver keywest_driver = {
  91. .driver = {
  92. .name = "PMac Keywest Audio",
  93. },
  94. .probe = keywest_probe,
  95. .remove = keywest_remove,
  96. .id_table = keywest_i2c_id,
  97. };
  98. /* exported */
  99. void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
  100. {
  101. if (keywest_ctx && keywest_ctx == i2c) {
  102. i2c_del_driver(&keywest_driver);
  103. keywest_ctx = NULL;
  104. }
  105. }
  106. int snd_pmac_tumbler_post_init(void)
  107. {
  108. int err;
  109. if (!keywest_ctx || !keywest_ctx->client)
  110. return -ENXIO;
  111. if ((err = keywest_ctx->init_client(keywest_ctx)) < 0) {
  112. snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
  113. return err;
  114. }
  115. return 0;
  116. }
  117. /* exported */
  118. int snd_pmac_keywest_init(struct pmac_keywest *i2c)
  119. {
  120. struct i2c_adapter *adap;
  121. int err, i = 0;
  122. if (keywest_ctx)
  123. return -EBUSY;
  124. adap = i2c_get_adapter(0);
  125. if (!adap)
  126. return -EPROBE_DEFER;
  127. keywest_ctx = i2c;
  128. if ((err = i2c_add_driver(&keywest_driver))) {
  129. snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
  130. i2c_put_adapter(adap);
  131. return err;
  132. }
  133. /* There was already a device from i2c-powermac. Great, let's return */
  134. if (keywest_probed)
  135. return 0;
  136. /* We assume Macs have consecutive I2C bus numbers starting at 0 */
  137. while (adap) {
  138. /* Scan for devices to be bound to */
  139. err = keywest_attach_adapter(adap);
  140. if (!err)
  141. return 0;
  142. i2c_put_adapter(adap);
  143. adap = i2c_get_adapter(++i);
  144. }
  145. return -ENODEV;
  146. }