keywest.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <sound/driver.h>
  21. #include <linux/init.h>
  22. #include <linux/i2c.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <sound/core.h>
  26. #include "pmac.h"
  27. /*
  28. * we have to keep a static variable here since i2c attach_adapter
  29. * callback cannot pass a private data.
  30. */
  31. static struct pmac_keywest *keywest_ctx;
  32. #define I2C_DRIVERID_KEYWEST 0xFEBA
  33. static int keywest_attach_adapter(struct i2c_adapter *adapter);
  34. static int keywest_detach_client(struct i2c_client *client);
  35. struct i2c_driver keywest_driver = {
  36. .driver = {
  37. .name = "PMac Keywest Audio",
  38. },
  39. .id = I2C_DRIVERID_KEYWEST,
  40. .attach_adapter = &keywest_attach_adapter,
  41. .detach_client = &keywest_detach_client,
  42. };
  43. #ifndef i2c_device_name
  44. #define i2c_device_name(x) ((x)->name)
  45. #endif
  46. static int keywest_attach_adapter(struct i2c_adapter *adapter)
  47. {
  48. int err;
  49. struct i2c_client *new_client;
  50. if (! keywest_ctx)
  51. return -EINVAL;
  52. if (strncmp(i2c_device_name(adapter), "mac-io", 6))
  53. return 0; /* ignored */
  54. new_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  55. if (! new_client)
  56. return -ENOMEM;
  57. new_client->addr = keywest_ctx->addr;
  58. i2c_set_clientdata(new_client, keywest_ctx);
  59. new_client->adapter = adapter;
  60. new_client->driver = &keywest_driver;
  61. new_client->flags = 0;
  62. strcpy(i2c_device_name(new_client), keywest_ctx->name);
  63. keywest_ctx->client = new_client;
  64. /* Tell the i2c layer a new client has arrived */
  65. if (i2c_attach_client(new_client)) {
  66. snd_printk(KERN_ERR "tumbler: cannot attach i2c client\n");
  67. err = -ENODEV;
  68. goto __err;
  69. }
  70. return 0;
  71. __err:
  72. kfree(new_client);
  73. keywest_ctx->client = NULL;
  74. return err;
  75. }
  76. static int keywest_detach_client(struct i2c_client *client)
  77. {
  78. if (! keywest_ctx)
  79. return 0;
  80. if (client == keywest_ctx->client)
  81. keywest_ctx->client = NULL;
  82. i2c_detach_client(client);
  83. kfree(client);
  84. return 0;
  85. }
  86. /* exported */
  87. void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
  88. {
  89. if (keywest_ctx && keywest_ctx == i2c) {
  90. i2c_del_driver(&keywest_driver);
  91. keywest_ctx = NULL;
  92. }
  93. }
  94. int __init snd_pmac_tumbler_post_init(void)
  95. {
  96. int err;
  97. if (!keywest_ctx || !keywest_ctx->client)
  98. return -ENXIO;
  99. if ((err = keywest_ctx->init_client(keywest_ctx)) < 0) {
  100. snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
  101. return err;
  102. }
  103. return 0;
  104. }
  105. /* exported */
  106. int __init snd_pmac_keywest_init(struct pmac_keywest *i2c)
  107. {
  108. int err;
  109. if (keywest_ctx)
  110. return -EBUSY;
  111. keywest_ctx = i2c;
  112. if ((err = i2c_add_driver(&keywest_driver))) {
  113. snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
  114. return err;
  115. }
  116. return 0;
  117. }