keywest.c 3.4 KB

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